qp.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972
  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. /* QP to support BF should have bits 6,7 cleared */
  42. #define MLX4_BF_QP_SKIP_MASK 0xc0
  43. #define MLX4_MAX_BF_QP_RANGE 0x40
  44. void mlx4_qp_event(struct mlx4_dev *dev, u32 qpn, int event_type)
  45. {
  46. struct mlx4_qp_table *qp_table = &mlx4_priv(dev)->qp_table;
  47. struct mlx4_qp *qp;
  48. spin_lock(&qp_table->lock);
  49. qp = __mlx4_qp_lookup(dev, qpn);
  50. if (qp)
  51. atomic_inc(&qp->refcount);
  52. spin_unlock(&qp_table->lock);
  53. if (!qp) {
  54. mlx4_dbg(dev, "Async event for none existent QP %08x\n", qpn);
  55. return;
  56. }
  57. qp->event(qp, event_type);
  58. if (atomic_dec_and_test(&qp->refcount))
  59. complete(&qp->free);
  60. }
  61. /* used for INIT/CLOSE port logic */
  62. static int is_master_qp0(struct mlx4_dev *dev, struct mlx4_qp *qp, int *real_qp0, int *proxy_qp0)
  63. {
  64. /* this procedure is called after we already know we are on the master */
  65. /* qp0 is either the proxy qp0, or the real qp0 */
  66. u32 pf_proxy_offset = dev->phys_caps.base_proxy_sqpn + 8 * mlx4_master_func_num(dev);
  67. *proxy_qp0 = qp->qpn >= pf_proxy_offset && qp->qpn <= pf_proxy_offset + 1;
  68. *real_qp0 = qp->qpn >= dev->phys_caps.base_sqpn &&
  69. qp->qpn <= dev->phys_caps.base_sqpn + 1;
  70. return *real_qp0 || *proxy_qp0;
  71. }
  72. static int __mlx4_qp_modify(struct mlx4_dev *dev, struct mlx4_mtt *mtt,
  73. enum mlx4_qp_state cur_state, enum mlx4_qp_state new_state,
  74. struct mlx4_qp_context *context,
  75. enum mlx4_qp_optpar optpar,
  76. int sqd_event, struct mlx4_qp *qp, int native)
  77. {
  78. static const u16 op[MLX4_QP_NUM_STATE][MLX4_QP_NUM_STATE] = {
  79. [MLX4_QP_STATE_RST] = {
  80. [MLX4_QP_STATE_RST] = MLX4_CMD_2RST_QP,
  81. [MLX4_QP_STATE_ERR] = MLX4_CMD_2ERR_QP,
  82. [MLX4_QP_STATE_INIT] = MLX4_CMD_RST2INIT_QP,
  83. },
  84. [MLX4_QP_STATE_INIT] = {
  85. [MLX4_QP_STATE_RST] = MLX4_CMD_2RST_QP,
  86. [MLX4_QP_STATE_ERR] = MLX4_CMD_2ERR_QP,
  87. [MLX4_QP_STATE_INIT] = MLX4_CMD_INIT2INIT_QP,
  88. [MLX4_QP_STATE_RTR] = MLX4_CMD_INIT2RTR_QP,
  89. },
  90. [MLX4_QP_STATE_RTR] = {
  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_RTR2RTS_QP,
  94. },
  95. [MLX4_QP_STATE_RTS] = {
  96. [MLX4_QP_STATE_RST] = MLX4_CMD_2RST_QP,
  97. [MLX4_QP_STATE_ERR] = MLX4_CMD_2ERR_QP,
  98. [MLX4_QP_STATE_RTS] = MLX4_CMD_RTS2RTS_QP,
  99. [MLX4_QP_STATE_SQD] = MLX4_CMD_RTS2SQD_QP,
  100. },
  101. [MLX4_QP_STATE_SQD] = {
  102. [MLX4_QP_STATE_RST] = MLX4_CMD_2RST_QP,
  103. [MLX4_QP_STATE_ERR] = MLX4_CMD_2ERR_QP,
  104. [MLX4_QP_STATE_RTS] = MLX4_CMD_SQD2RTS_QP,
  105. [MLX4_QP_STATE_SQD] = MLX4_CMD_SQD2SQD_QP,
  106. },
  107. [MLX4_QP_STATE_SQER] = {
  108. [MLX4_QP_STATE_RST] = MLX4_CMD_2RST_QP,
  109. [MLX4_QP_STATE_ERR] = MLX4_CMD_2ERR_QP,
  110. [MLX4_QP_STATE_RTS] = MLX4_CMD_SQERR2RTS_QP,
  111. },
  112. [MLX4_QP_STATE_ERR] = {
  113. [MLX4_QP_STATE_RST] = MLX4_CMD_2RST_QP,
  114. [MLX4_QP_STATE_ERR] = MLX4_CMD_2ERR_QP,
  115. }
  116. };
  117. struct mlx4_priv *priv = mlx4_priv(dev);
  118. struct mlx4_cmd_mailbox *mailbox;
  119. int ret = 0;
  120. int real_qp0 = 0;
  121. int proxy_qp0 = 0;
  122. u8 port;
  123. if (cur_state >= MLX4_QP_NUM_STATE || new_state >= MLX4_QP_NUM_STATE ||
  124. !op[cur_state][new_state])
  125. return -EINVAL;
  126. if (op[cur_state][new_state] == MLX4_CMD_2RST_QP) {
  127. ret = mlx4_cmd(dev, 0, qp->qpn, 2,
  128. MLX4_CMD_2RST_QP, MLX4_CMD_TIME_CLASS_A, native);
  129. if (mlx4_is_master(dev) && cur_state != MLX4_QP_STATE_ERR &&
  130. cur_state != MLX4_QP_STATE_RST &&
  131. is_master_qp0(dev, qp, &real_qp0, &proxy_qp0)) {
  132. port = (qp->qpn & 1) + 1;
  133. if (proxy_qp0)
  134. priv->mfunc.master.qp0_state[port].proxy_qp0_active = 0;
  135. else
  136. priv->mfunc.master.qp0_state[port].qp0_active = 0;
  137. }
  138. return ret;
  139. }
  140. mailbox = mlx4_alloc_cmd_mailbox(dev);
  141. if (IS_ERR(mailbox))
  142. return PTR_ERR(mailbox);
  143. if (cur_state == MLX4_QP_STATE_RST && new_state == MLX4_QP_STATE_INIT) {
  144. u64 mtt_addr = mlx4_mtt_addr(dev, mtt);
  145. context->mtt_base_addr_h = mtt_addr >> 32;
  146. context->mtt_base_addr_l = cpu_to_be32(mtt_addr & 0xffffffff);
  147. context->log_page_size = mtt->page_shift - MLX4_ICM_PAGE_SHIFT;
  148. }
  149. if ((cur_state == MLX4_QP_STATE_RTR) &&
  150. (new_state == MLX4_QP_STATE_RTS) &&
  151. dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_ROCE_V1_V2)
  152. context->roce_entropy =
  153. cpu_to_be16(mlx4_qp_roce_entropy(dev, qp->qpn));
  154. *(__be32 *) mailbox->buf = cpu_to_be32(optpar);
  155. memcpy(mailbox->buf + 8, context, sizeof *context);
  156. ((struct mlx4_qp_context *) (mailbox->buf + 8))->local_qpn =
  157. cpu_to_be32(qp->qpn);
  158. ret = mlx4_cmd(dev, mailbox->dma,
  159. qp->qpn | (!!sqd_event << 31),
  160. new_state == MLX4_QP_STATE_RST ? 2 : 0,
  161. op[cur_state][new_state], MLX4_CMD_TIME_CLASS_C, native);
  162. if (mlx4_is_master(dev) && is_master_qp0(dev, qp, &real_qp0, &proxy_qp0)) {
  163. port = (qp->qpn & 1) + 1;
  164. if (cur_state != MLX4_QP_STATE_ERR &&
  165. cur_state != MLX4_QP_STATE_RST &&
  166. new_state == MLX4_QP_STATE_ERR) {
  167. if (proxy_qp0)
  168. priv->mfunc.master.qp0_state[port].proxy_qp0_active = 0;
  169. else
  170. priv->mfunc.master.qp0_state[port].qp0_active = 0;
  171. } else if (new_state == MLX4_QP_STATE_RTR) {
  172. if (proxy_qp0)
  173. priv->mfunc.master.qp0_state[port].proxy_qp0_active = 1;
  174. else
  175. priv->mfunc.master.qp0_state[port].qp0_active = 1;
  176. }
  177. }
  178. mlx4_free_cmd_mailbox(dev, mailbox);
  179. return ret;
  180. }
  181. int mlx4_qp_modify(struct mlx4_dev *dev, struct mlx4_mtt *mtt,
  182. enum mlx4_qp_state cur_state, enum mlx4_qp_state new_state,
  183. struct mlx4_qp_context *context,
  184. enum mlx4_qp_optpar optpar,
  185. int sqd_event, struct mlx4_qp *qp)
  186. {
  187. return __mlx4_qp_modify(dev, mtt, cur_state, new_state, context,
  188. optpar, sqd_event, qp, 0);
  189. }
  190. EXPORT_SYMBOL_GPL(mlx4_qp_modify);
  191. int __mlx4_qp_reserve_range(struct mlx4_dev *dev, int cnt, int align,
  192. int *base, u8 flags)
  193. {
  194. u32 uid;
  195. int bf_qp = !!(flags & (u8)MLX4_RESERVE_ETH_BF_QP);
  196. struct mlx4_priv *priv = mlx4_priv(dev);
  197. struct mlx4_qp_table *qp_table = &priv->qp_table;
  198. if (cnt > MLX4_MAX_BF_QP_RANGE && bf_qp)
  199. return -ENOMEM;
  200. uid = MLX4_QP_TABLE_ZONE_GENERAL;
  201. if (flags & (u8)MLX4_RESERVE_A0_QP) {
  202. if (bf_qp)
  203. uid = MLX4_QP_TABLE_ZONE_RAW_ETH;
  204. else
  205. uid = MLX4_QP_TABLE_ZONE_RSS;
  206. }
  207. *base = mlx4_zone_alloc_entries(qp_table->zones, uid, cnt, align,
  208. bf_qp ? MLX4_BF_QP_SKIP_MASK : 0, NULL);
  209. if (*base == -1)
  210. return -ENOMEM;
  211. return 0;
  212. }
  213. int mlx4_qp_reserve_range(struct mlx4_dev *dev, int cnt, int align,
  214. int *base, u8 flags)
  215. {
  216. u64 in_param = 0;
  217. u64 out_param;
  218. int err;
  219. /* Turn off all unsupported QP allocation flags */
  220. flags &= dev->caps.alloc_res_qp_mask;
  221. if (mlx4_is_mfunc(dev)) {
  222. set_param_l(&in_param, (((u32)flags) << 24) | (u32)cnt);
  223. set_param_h(&in_param, align);
  224. err = mlx4_cmd_imm(dev, in_param, &out_param,
  225. RES_QP, RES_OP_RESERVE,
  226. MLX4_CMD_ALLOC_RES,
  227. MLX4_CMD_TIME_CLASS_A, MLX4_CMD_WRAPPED);
  228. if (err)
  229. return err;
  230. *base = get_param_l(&out_param);
  231. return 0;
  232. }
  233. return __mlx4_qp_reserve_range(dev, cnt, align, base, flags);
  234. }
  235. EXPORT_SYMBOL_GPL(mlx4_qp_reserve_range);
  236. void __mlx4_qp_release_range(struct mlx4_dev *dev, int base_qpn, int cnt)
  237. {
  238. struct mlx4_priv *priv = mlx4_priv(dev);
  239. struct mlx4_qp_table *qp_table = &priv->qp_table;
  240. if (mlx4_is_qp_reserved(dev, (u32) base_qpn))
  241. return;
  242. mlx4_zone_free_entries_unique(qp_table->zones, base_qpn, cnt);
  243. }
  244. void mlx4_qp_release_range(struct mlx4_dev *dev, int base_qpn, int cnt)
  245. {
  246. u64 in_param = 0;
  247. int err;
  248. if (!cnt)
  249. return;
  250. if (mlx4_is_mfunc(dev)) {
  251. set_param_l(&in_param, base_qpn);
  252. set_param_h(&in_param, cnt);
  253. err = mlx4_cmd(dev, in_param, RES_QP, RES_OP_RESERVE,
  254. MLX4_CMD_FREE_RES,
  255. MLX4_CMD_TIME_CLASS_A, MLX4_CMD_WRAPPED);
  256. if (err) {
  257. mlx4_warn(dev, "Failed to release qp range base:%d cnt:%d\n",
  258. base_qpn, cnt);
  259. }
  260. } else
  261. __mlx4_qp_release_range(dev, base_qpn, cnt);
  262. }
  263. EXPORT_SYMBOL_GPL(mlx4_qp_release_range);
  264. int __mlx4_qp_alloc_icm(struct mlx4_dev *dev, int qpn, gfp_t gfp)
  265. {
  266. struct mlx4_priv *priv = mlx4_priv(dev);
  267. struct mlx4_qp_table *qp_table = &priv->qp_table;
  268. int err;
  269. err = mlx4_table_get(dev, &qp_table->qp_table, qpn, gfp);
  270. if (err)
  271. goto err_out;
  272. err = mlx4_table_get(dev, &qp_table->auxc_table, qpn, gfp);
  273. if (err)
  274. goto err_put_qp;
  275. err = mlx4_table_get(dev, &qp_table->altc_table, qpn, gfp);
  276. if (err)
  277. goto err_put_auxc;
  278. err = mlx4_table_get(dev, &qp_table->rdmarc_table, qpn, gfp);
  279. if (err)
  280. goto err_put_altc;
  281. err = mlx4_table_get(dev, &qp_table->cmpt_table, qpn, gfp);
  282. if (err)
  283. goto err_put_rdmarc;
  284. return 0;
  285. err_put_rdmarc:
  286. mlx4_table_put(dev, &qp_table->rdmarc_table, qpn);
  287. err_put_altc:
  288. mlx4_table_put(dev, &qp_table->altc_table, qpn);
  289. err_put_auxc:
  290. mlx4_table_put(dev, &qp_table->auxc_table, qpn);
  291. err_put_qp:
  292. mlx4_table_put(dev, &qp_table->qp_table, qpn);
  293. err_out:
  294. return err;
  295. }
  296. static int mlx4_qp_alloc_icm(struct mlx4_dev *dev, int qpn, gfp_t gfp)
  297. {
  298. u64 param = 0;
  299. if (mlx4_is_mfunc(dev)) {
  300. set_param_l(&param, qpn);
  301. return mlx4_cmd_imm(dev, param, &param, RES_QP, RES_OP_MAP_ICM,
  302. MLX4_CMD_ALLOC_RES, MLX4_CMD_TIME_CLASS_A,
  303. MLX4_CMD_WRAPPED);
  304. }
  305. return __mlx4_qp_alloc_icm(dev, qpn, gfp);
  306. }
  307. void __mlx4_qp_free_icm(struct mlx4_dev *dev, int qpn)
  308. {
  309. struct mlx4_priv *priv = mlx4_priv(dev);
  310. struct mlx4_qp_table *qp_table = &priv->qp_table;
  311. mlx4_table_put(dev, &qp_table->cmpt_table, qpn);
  312. mlx4_table_put(dev, &qp_table->rdmarc_table, qpn);
  313. mlx4_table_put(dev, &qp_table->altc_table, qpn);
  314. mlx4_table_put(dev, &qp_table->auxc_table, qpn);
  315. mlx4_table_put(dev, &qp_table->qp_table, qpn);
  316. }
  317. static void mlx4_qp_free_icm(struct mlx4_dev *dev, int qpn)
  318. {
  319. u64 in_param = 0;
  320. if (mlx4_is_mfunc(dev)) {
  321. set_param_l(&in_param, qpn);
  322. if (mlx4_cmd(dev, in_param, RES_QP, RES_OP_MAP_ICM,
  323. MLX4_CMD_FREE_RES, MLX4_CMD_TIME_CLASS_A,
  324. MLX4_CMD_WRAPPED))
  325. mlx4_warn(dev, "Failed to free icm of qp:%d\n", qpn);
  326. } else
  327. __mlx4_qp_free_icm(dev, qpn);
  328. }
  329. struct mlx4_qp *mlx4_qp_lookup(struct mlx4_dev *dev, u32 qpn)
  330. {
  331. struct mlx4_qp_table *qp_table = &mlx4_priv(dev)->qp_table;
  332. struct mlx4_qp *qp;
  333. spin_lock_irq(&qp_table->lock);
  334. qp = __mlx4_qp_lookup(dev, qpn);
  335. spin_unlock_irq(&qp_table->lock);
  336. return qp;
  337. }
  338. int mlx4_qp_alloc(struct mlx4_dev *dev, int qpn, struct mlx4_qp *qp, gfp_t gfp)
  339. {
  340. struct mlx4_priv *priv = mlx4_priv(dev);
  341. struct mlx4_qp_table *qp_table = &priv->qp_table;
  342. int err;
  343. if (!qpn)
  344. return -EINVAL;
  345. qp->qpn = qpn;
  346. err = mlx4_qp_alloc_icm(dev, qpn, gfp);
  347. if (err)
  348. return err;
  349. spin_lock_irq(&qp_table->lock);
  350. err = radix_tree_insert(&dev->qp_table_tree, qp->qpn &
  351. (dev->caps.num_qps - 1), qp);
  352. spin_unlock_irq(&qp_table->lock);
  353. if (err)
  354. goto err_icm;
  355. atomic_set(&qp->refcount, 1);
  356. init_completion(&qp->free);
  357. return 0;
  358. err_icm:
  359. mlx4_qp_free_icm(dev, qpn);
  360. return err;
  361. }
  362. EXPORT_SYMBOL_GPL(mlx4_qp_alloc);
  363. int mlx4_update_qp(struct mlx4_dev *dev, u32 qpn,
  364. enum mlx4_update_qp_attr attr,
  365. struct mlx4_update_qp_params *params)
  366. {
  367. struct mlx4_cmd_mailbox *mailbox;
  368. struct mlx4_update_qp_context *cmd;
  369. u64 pri_addr_path_mask = 0;
  370. u64 qp_mask = 0;
  371. int err = 0;
  372. if (!attr || (attr & ~MLX4_UPDATE_QP_SUPPORTED_ATTRS))
  373. return -EINVAL;
  374. mailbox = mlx4_alloc_cmd_mailbox(dev);
  375. if (IS_ERR(mailbox))
  376. return PTR_ERR(mailbox);
  377. cmd = (struct mlx4_update_qp_context *)mailbox->buf;
  378. if (attr & MLX4_UPDATE_QP_SMAC) {
  379. pri_addr_path_mask |= 1ULL << MLX4_UPD_QP_PATH_MASK_MAC_INDEX;
  380. cmd->qp_context.pri_path.grh_mylmc = params->smac_index;
  381. }
  382. if (attr & MLX4_UPDATE_QP_ETH_SRC_CHECK_MC_LB) {
  383. if (!(dev->caps.flags2
  384. & MLX4_DEV_CAP_FLAG2_UPDATE_QP_SRC_CHECK_LB)) {
  385. mlx4_warn(dev,
  386. "Trying to set src check LB, but it isn't supported\n");
  387. err = -ENOTSUPP;
  388. goto out;
  389. }
  390. pri_addr_path_mask |=
  391. 1ULL << MLX4_UPD_QP_PATH_MASK_ETH_SRC_CHECK_MC_LB;
  392. if (params->flags &
  393. MLX4_UPDATE_QP_PARAMS_FLAGS_ETH_CHECK_MC_LB) {
  394. cmd->qp_context.pri_path.fl |=
  395. MLX4_FL_ETH_SRC_CHECK_MC_LB;
  396. }
  397. }
  398. if (attr & MLX4_UPDATE_QP_VSD) {
  399. qp_mask |= 1ULL << MLX4_UPD_QP_MASK_VSD;
  400. if (params->flags & MLX4_UPDATE_QP_PARAMS_FLAGS_VSD_ENABLE)
  401. cmd->qp_context.param3 |= cpu_to_be32(MLX4_STRIP_VLAN);
  402. }
  403. if (attr & MLX4_UPDATE_QP_RATE_LIMIT) {
  404. qp_mask |= 1ULL << MLX4_UPD_QP_MASK_RATE_LIMIT;
  405. cmd->qp_context.rate_limit_params = cpu_to_be16((params->rate_unit << 14) | params->rate_val);
  406. }
  407. if (attr & MLX4_UPDATE_QP_QOS_VPORT) {
  408. if (!(dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_QOS_VPP)) {
  409. mlx4_warn(dev, "Granular QoS per VF is not enabled\n");
  410. err = -EOPNOTSUPP;
  411. goto out;
  412. }
  413. qp_mask |= 1ULL << MLX4_UPD_QP_MASK_QOS_VPP;
  414. cmd->qp_context.qos_vport = params->qos_vport;
  415. }
  416. cmd->primary_addr_path_mask = cpu_to_be64(pri_addr_path_mask);
  417. cmd->qp_mask = cpu_to_be64(qp_mask);
  418. err = mlx4_cmd(dev, mailbox->dma, qpn & 0xffffff, 0,
  419. MLX4_CMD_UPDATE_QP, MLX4_CMD_TIME_CLASS_A,
  420. MLX4_CMD_NATIVE);
  421. out:
  422. mlx4_free_cmd_mailbox(dev, mailbox);
  423. return err;
  424. }
  425. EXPORT_SYMBOL_GPL(mlx4_update_qp);
  426. void mlx4_qp_remove(struct mlx4_dev *dev, struct mlx4_qp *qp)
  427. {
  428. struct mlx4_qp_table *qp_table = &mlx4_priv(dev)->qp_table;
  429. unsigned long flags;
  430. spin_lock_irqsave(&qp_table->lock, flags);
  431. radix_tree_delete(&dev->qp_table_tree, qp->qpn & (dev->caps.num_qps - 1));
  432. spin_unlock_irqrestore(&qp_table->lock, flags);
  433. }
  434. EXPORT_SYMBOL_GPL(mlx4_qp_remove);
  435. void mlx4_qp_free(struct mlx4_dev *dev, struct mlx4_qp *qp)
  436. {
  437. if (atomic_dec_and_test(&qp->refcount))
  438. complete(&qp->free);
  439. wait_for_completion(&qp->free);
  440. mlx4_qp_free_icm(dev, qp->qpn);
  441. }
  442. EXPORT_SYMBOL_GPL(mlx4_qp_free);
  443. static int mlx4_CONF_SPECIAL_QP(struct mlx4_dev *dev, u32 base_qpn)
  444. {
  445. return mlx4_cmd(dev, 0, base_qpn, 0, MLX4_CMD_CONF_SPECIAL_QP,
  446. MLX4_CMD_TIME_CLASS_B, MLX4_CMD_NATIVE);
  447. }
  448. #define MLX4_QP_TABLE_RSS_ETH_PRIORITY 2
  449. #define MLX4_QP_TABLE_RAW_ETH_PRIORITY 1
  450. #define MLX4_QP_TABLE_RAW_ETH_SIZE 256
  451. static int mlx4_create_zones(struct mlx4_dev *dev,
  452. u32 reserved_bottom_general,
  453. u32 reserved_top_general,
  454. u32 reserved_bottom_rss,
  455. u32 start_offset_rss,
  456. u32 max_table_offset)
  457. {
  458. struct mlx4_qp_table *qp_table = &mlx4_priv(dev)->qp_table;
  459. struct mlx4_bitmap (*bitmap)[MLX4_QP_TABLE_ZONE_NUM] = NULL;
  460. int bitmap_initialized = 0;
  461. u32 last_offset;
  462. int k;
  463. int err;
  464. qp_table->zones = mlx4_zone_allocator_create(MLX4_ZONE_ALLOC_FLAGS_NO_OVERLAP);
  465. if (NULL == qp_table->zones)
  466. return -ENOMEM;
  467. bitmap = kmalloc(sizeof(*bitmap), GFP_KERNEL);
  468. if (NULL == bitmap) {
  469. err = -ENOMEM;
  470. goto free_zone;
  471. }
  472. err = mlx4_bitmap_init(*bitmap + MLX4_QP_TABLE_ZONE_GENERAL, dev->caps.num_qps,
  473. (1 << 23) - 1, reserved_bottom_general,
  474. reserved_top_general);
  475. if (err)
  476. goto free_bitmap;
  477. ++bitmap_initialized;
  478. err = mlx4_zone_add_one(qp_table->zones, *bitmap + MLX4_QP_TABLE_ZONE_GENERAL,
  479. MLX4_ZONE_FALLBACK_TO_HIGHER_PRIO |
  480. MLX4_ZONE_USE_RR, 0,
  481. 0, qp_table->zones_uids + MLX4_QP_TABLE_ZONE_GENERAL);
  482. if (err)
  483. goto free_bitmap;
  484. err = mlx4_bitmap_init(*bitmap + MLX4_QP_TABLE_ZONE_RSS,
  485. reserved_bottom_rss,
  486. reserved_bottom_rss - 1,
  487. dev->caps.reserved_qps_cnt[MLX4_QP_REGION_FW],
  488. reserved_bottom_rss - start_offset_rss);
  489. if (err)
  490. goto free_bitmap;
  491. ++bitmap_initialized;
  492. err = mlx4_zone_add_one(qp_table->zones, *bitmap + MLX4_QP_TABLE_ZONE_RSS,
  493. MLX4_ZONE_ALLOW_ALLOC_FROM_LOWER_PRIO |
  494. MLX4_ZONE_ALLOW_ALLOC_FROM_EQ_PRIO |
  495. MLX4_ZONE_USE_RR, MLX4_QP_TABLE_RSS_ETH_PRIORITY,
  496. 0, qp_table->zones_uids + MLX4_QP_TABLE_ZONE_RSS);
  497. if (err)
  498. goto free_bitmap;
  499. last_offset = dev->caps.reserved_qps_cnt[MLX4_QP_REGION_FW];
  500. /* We have a single zone for the A0 steering QPs area of the FW. This area
  501. * needs to be split into subareas. One set of subareas is for RSS QPs
  502. * (in which qp number bits 6 and/or 7 are set); the other set of subareas
  503. * is for RAW_ETH QPs, which require that both bits 6 and 7 are zero.
  504. * Currently, the values returned by the FW (A0 steering area starting qp number
  505. * and A0 steering area size) are such that there are only two subareas -- one
  506. * for RSS and one for RAW_ETH.
  507. */
  508. for (k = MLX4_QP_TABLE_ZONE_RSS + 1; k < sizeof(*bitmap)/sizeof((*bitmap)[0]);
  509. k++) {
  510. int size;
  511. u32 offset = start_offset_rss;
  512. u32 bf_mask;
  513. u32 requested_size;
  514. /* Assuming MLX4_BF_QP_SKIP_MASK is consecutive ones, this calculates
  515. * a mask of all LSB bits set until (and not including) the first
  516. * set bit of MLX4_BF_QP_SKIP_MASK. For example, if MLX4_BF_QP_SKIP_MASK
  517. * is 0xc0, bf_mask will be 0x3f.
  518. */
  519. bf_mask = (MLX4_BF_QP_SKIP_MASK & ~(MLX4_BF_QP_SKIP_MASK - 1)) - 1;
  520. requested_size = min((u32)MLX4_QP_TABLE_RAW_ETH_SIZE, bf_mask + 1);
  521. if (((last_offset & MLX4_BF_QP_SKIP_MASK) &&
  522. ((int)(max_table_offset - last_offset)) >=
  523. roundup_pow_of_two(MLX4_BF_QP_SKIP_MASK)) ||
  524. (!(last_offset & MLX4_BF_QP_SKIP_MASK) &&
  525. !((last_offset + requested_size - 1) &
  526. MLX4_BF_QP_SKIP_MASK)))
  527. size = requested_size;
  528. else {
  529. u32 candidate_offset =
  530. (last_offset | MLX4_BF_QP_SKIP_MASK | bf_mask) + 1;
  531. if (last_offset & MLX4_BF_QP_SKIP_MASK)
  532. last_offset = candidate_offset;
  533. /* From this point, the BF bits are 0 */
  534. if (last_offset > max_table_offset) {
  535. /* need to skip */
  536. size = -1;
  537. } else {
  538. size = min3(max_table_offset - last_offset,
  539. bf_mask - (last_offset & bf_mask),
  540. requested_size);
  541. if (size < requested_size) {
  542. int candidate_size;
  543. candidate_size = min3(
  544. max_table_offset - candidate_offset,
  545. bf_mask - (last_offset & bf_mask),
  546. requested_size);
  547. /* We will not take this path if last_offset was
  548. * already set above to candidate_offset
  549. */
  550. if (candidate_size > size) {
  551. last_offset = candidate_offset;
  552. size = candidate_size;
  553. }
  554. }
  555. }
  556. }
  557. if (size > 0) {
  558. /* mlx4_bitmap_alloc_range will find a contiguous range of "size"
  559. * QPs in which both bits 6 and 7 are zero, because we pass it the
  560. * MLX4_BF_SKIP_MASK).
  561. */
  562. offset = mlx4_bitmap_alloc_range(
  563. *bitmap + MLX4_QP_TABLE_ZONE_RSS,
  564. size, 1,
  565. MLX4_BF_QP_SKIP_MASK);
  566. if (offset == (u32)-1) {
  567. err = -ENOMEM;
  568. break;
  569. }
  570. last_offset = offset + size;
  571. err = mlx4_bitmap_init(*bitmap + k, roundup_pow_of_two(size),
  572. roundup_pow_of_two(size) - 1, 0,
  573. roundup_pow_of_two(size) - size);
  574. } else {
  575. /* Add an empty bitmap, we'll allocate from different zones (since
  576. * at least one is reserved)
  577. */
  578. err = mlx4_bitmap_init(*bitmap + k, 1,
  579. MLX4_QP_TABLE_RAW_ETH_SIZE - 1, 0,
  580. 0);
  581. mlx4_bitmap_alloc_range(*bitmap + k, 1, 1, 0);
  582. }
  583. if (err)
  584. break;
  585. ++bitmap_initialized;
  586. err = mlx4_zone_add_one(qp_table->zones, *bitmap + k,
  587. MLX4_ZONE_ALLOW_ALLOC_FROM_LOWER_PRIO |
  588. MLX4_ZONE_ALLOW_ALLOC_FROM_EQ_PRIO |
  589. MLX4_ZONE_USE_RR, MLX4_QP_TABLE_RAW_ETH_PRIORITY,
  590. offset, qp_table->zones_uids + k);
  591. if (err)
  592. break;
  593. }
  594. if (err)
  595. goto free_bitmap;
  596. qp_table->bitmap_gen = *bitmap;
  597. return err;
  598. free_bitmap:
  599. for (k = 0; k < bitmap_initialized; k++)
  600. mlx4_bitmap_cleanup(*bitmap + k);
  601. kfree(bitmap);
  602. free_zone:
  603. mlx4_zone_allocator_destroy(qp_table->zones);
  604. return err;
  605. }
  606. static void mlx4_cleanup_qp_zones(struct mlx4_dev *dev)
  607. {
  608. struct mlx4_qp_table *qp_table = &mlx4_priv(dev)->qp_table;
  609. if (qp_table->zones) {
  610. int i;
  611. for (i = 0;
  612. i < sizeof(qp_table->zones_uids)/sizeof(qp_table->zones_uids[0]);
  613. i++) {
  614. struct mlx4_bitmap *bitmap =
  615. mlx4_zone_get_bitmap(qp_table->zones,
  616. qp_table->zones_uids[i]);
  617. mlx4_zone_remove_one(qp_table->zones, qp_table->zones_uids[i]);
  618. if (NULL == bitmap)
  619. continue;
  620. mlx4_bitmap_cleanup(bitmap);
  621. }
  622. mlx4_zone_allocator_destroy(qp_table->zones);
  623. kfree(qp_table->bitmap_gen);
  624. qp_table->bitmap_gen = NULL;
  625. qp_table->zones = NULL;
  626. }
  627. }
  628. int mlx4_init_qp_table(struct mlx4_dev *dev)
  629. {
  630. struct mlx4_qp_table *qp_table = &mlx4_priv(dev)->qp_table;
  631. int err;
  632. int reserved_from_top = 0;
  633. int reserved_from_bot;
  634. int k;
  635. int fixed_reserved_from_bot_rv = 0;
  636. int bottom_reserved_for_rss_bitmap;
  637. u32 max_table_offset = dev->caps.dmfs_high_rate_qpn_base +
  638. dev->caps.dmfs_high_rate_qpn_range;
  639. spin_lock_init(&qp_table->lock);
  640. INIT_RADIX_TREE(&dev->qp_table_tree, GFP_ATOMIC);
  641. if (mlx4_is_slave(dev))
  642. return 0;
  643. /* We reserve 2 extra QPs per port for the special QPs. The
  644. * block of special QPs must be aligned to a multiple of 8, so
  645. * round up.
  646. *
  647. * We also reserve the MSB of the 24-bit QP number to indicate
  648. * that a QP is an XRC QP.
  649. */
  650. for (k = 0; k <= MLX4_QP_REGION_BOTTOM; k++)
  651. fixed_reserved_from_bot_rv += dev->caps.reserved_qps_cnt[k];
  652. if (fixed_reserved_from_bot_rv < max_table_offset)
  653. fixed_reserved_from_bot_rv = max_table_offset;
  654. /* We reserve at least 1 extra for bitmaps that we don't have enough space for*/
  655. bottom_reserved_for_rss_bitmap =
  656. roundup_pow_of_two(fixed_reserved_from_bot_rv + 1);
  657. dev->phys_caps.base_sqpn = ALIGN(bottom_reserved_for_rss_bitmap, 8);
  658. {
  659. int sort[MLX4_NUM_QP_REGION];
  660. int i, j;
  661. int last_base = dev->caps.num_qps;
  662. for (i = 1; i < MLX4_NUM_QP_REGION; ++i)
  663. sort[i] = i;
  664. for (i = MLX4_NUM_QP_REGION; i > MLX4_QP_REGION_BOTTOM; --i) {
  665. for (j = MLX4_QP_REGION_BOTTOM + 2; j < i; ++j) {
  666. if (dev->caps.reserved_qps_cnt[sort[j]] >
  667. dev->caps.reserved_qps_cnt[sort[j - 1]])
  668. swap(sort[j], sort[j - 1]);
  669. }
  670. }
  671. for (i = MLX4_QP_REGION_BOTTOM + 1; i < MLX4_NUM_QP_REGION; ++i) {
  672. last_base -= dev->caps.reserved_qps_cnt[sort[i]];
  673. dev->caps.reserved_qps_base[sort[i]] = last_base;
  674. reserved_from_top +=
  675. dev->caps.reserved_qps_cnt[sort[i]];
  676. }
  677. }
  678. /* Reserve 8 real SQPs in both native and SRIOV modes.
  679. * In addition, in SRIOV mode, reserve 8 proxy SQPs per function
  680. * (for all PFs and VFs), and 8 corresponding tunnel QPs.
  681. * Each proxy SQP works opposite its own tunnel QP.
  682. *
  683. * The QPs are arranged as follows:
  684. * a. 8 real SQPs
  685. * b. All the proxy SQPs (8 per function)
  686. * c. All the tunnel QPs (8 per function)
  687. */
  688. reserved_from_bot = mlx4_num_reserved_sqps(dev);
  689. if (reserved_from_bot + reserved_from_top > dev->caps.num_qps) {
  690. mlx4_err(dev, "Number of reserved QPs is higher than number of QPs\n");
  691. return -EINVAL;
  692. }
  693. err = mlx4_create_zones(dev, reserved_from_bot, reserved_from_bot,
  694. bottom_reserved_for_rss_bitmap,
  695. fixed_reserved_from_bot_rv,
  696. max_table_offset);
  697. if (err)
  698. return err;
  699. if (mlx4_is_mfunc(dev)) {
  700. /* for PPF use */
  701. dev->phys_caps.base_proxy_sqpn = dev->phys_caps.base_sqpn + 8;
  702. dev->phys_caps.base_tunnel_sqpn = dev->phys_caps.base_sqpn + 8 + 8 * MLX4_MFUNC_MAX;
  703. /* In mfunc, calculate proxy and tunnel qp offsets for the PF here,
  704. * since the PF does not call mlx4_slave_caps */
  705. dev->caps.qp0_tunnel = kcalloc(dev->caps.num_ports, sizeof (u32), GFP_KERNEL);
  706. dev->caps.qp0_proxy = kcalloc(dev->caps.num_ports, sizeof (u32), GFP_KERNEL);
  707. dev->caps.qp1_tunnel = kcalloc(dev->caps.num_ports, sizeof (u32), GFP_KERNEL);
  708. dev->caps.qp1_proxy = kcalloc(dev->caps.num_ports, sizeof (u32), GFP_KERNEL);
  709. if (!dev->caps.qp0_tunnel || !dev->caps.qp0_proxy ||
  710. !dev->caps.qp1_tunnel || !dev->caps.qp1_proxy) {
  711. err = -ENOMEM;
  712. goto err_mem;
  713. }
  714. for (k = 0; k < dev->caps.num_ports; k++) {
  715. dev->caps.qp0_proxy[k] = dev->phys_caps.base_proxy_sqpn +
  716. 8 * mlx4_master_func_num(dev) + k;
  717. dev->caps.qp0_tunnel[k] = dev->caps.qp0_proxy[k] + 8 * MLX4_MFUNC_MAX;
  718. dev->caps.qp1_proxy[k] = dev->phys_caps.base_proxy_sqpn +
  719. 8 * mlx4_master_func_num(dev) + MLX4_MAX_PORTS + k;
  720. dev->caps.qp1_tunnel[k] = dev->caps.qp1_proxy[k] + 8 * MLX4_MFUNC_MAX;
  721. }
  722. }
  723. err = mlx4_CONF_SPECIAL_QP(dev, dev->phys_caps.base_sqpn);
  724. if (err)
  725. goto err_mem;
  726. return err;
  727. err_mem:
  728. kfree(dev->caps.qp0_tunnel);
  729. kfree(dev->caps.qp0_proxy);
  730. kfree(dev->caps.qp1_tunnel);
  731. kfree(dev->caps.qp1_proxy);
  732. dev->caps.qp0_tunnel = dev->caps.qp0_proxy =
  733. dev->caps.qp1_tunnel = dev->caps.qp1_proxy = NULL;
  734. mlx4_cleanup_qp_zones(dev);
  735. return err;
  736. }
  737. void mlx4_cleanup_qp_table(struct mlx4_dev *dev)
  738. {
  739. if (mlx4_is_slave(dev))
  740. return;
  741. mlx4_CONF_SPECIAL_QP(dev, 0);
  742. mlx4_cleanup_qp_zones(dev);
  743. }
  744. int mlx4_qp_query(struct mlx4_dev *dev, struct mlx4_qp *qp,
  745. struct mlx4_qp_context *context)
  746. {
  747. struct mlx4_cmd_mailbox *mailbox;
  748. int err;
  749. mailbox = mlx4_alloc_cmd_mailbox(dev);
  750. if (IS_ERR(mailbox))
  751. return PTR_ERR(mailbox);
  752. err = mlx4_cmd_box(dev, 0, mailbox->dma, qp->qpn, 0,
  753. MLX4_CMD_QUERY_QP, MLX4_CMD_TIME_CLASS_A,
  754. MLX4_CMD_WRAPPED);
  755. if (!err)
  756. memcpy(context, mailbox->buf + 8, sizeof *context);
  757. mlx4_free_cmd_mailbox(dev, mailbox);
  758. return err;
  759. }
  760. EXPORT_SYMBOL_GPL(mlx4_qp_query);
  761. int mlx4_qp_to_ready(struct mlx4_dev *dev, struct mlx4_mtt *mtt,
  762. struct mlx4_qp_context *context,
  763. struct mlx4_qp *qp, enum mlx4_qp_state *qp_state)
  764. {
  765. int err;
  766. int i;
  767. enum mlx4_qp_state states[] = {
  768. MLX4_QP_STATE_RST,
  769. MLX4_QP_STATE_INIT,
  770. MLX4_QP_STATE_RTR,
  771. MLX4_QP_STATE_RTS
  772. };
  773. for (i = 0; i < ARRAY_SIZE(states) - 1; i++) {
  774. context->flags &= cpu_to_be32(~(0xf << 28));
  775. context->flags |= cpu_to_be32(states[i + 1] << 28);
  776. if (states[i + 1] != MLX4_QP_STATE_RTR)
  777. context->params2 &= ~MLX4_QP_BIT_FPP;
  778. err = mlx4_qp_modify(dev, mtt, states[i], states[i + 1],
  779. context, 0, 0, qp);
  780. if (err) {
  781. mlx4_err(dev, "Failed to bring QP to state: %d with error: %d\n",
  782. states[i + 1], err);
  783. return err;
  784. }
  785. *qp_state = states[i + 1];
  786. }
  787. return 0;
  788. }
  789. EXPORT_SYMBOL_GPL(mlx4_qp_to_ready);
  790. u16 mlx4_qp_roce_entropy(struct mlx4_dev *dev, u32 qpn)
  791. {
  792. struct mlx4_qp_context context;
  793. struct mlx4_qp qp;
  794. int err;
  795. qp.qpn = qpn;
  796. err = mlx4_qp_query(dev, &qp, &context);
  797. if (!err) {
  798. u32 dest_qpn = be32_to_cpu(context.remote_qpn) & 0xffffff;
  799. u16 folded_dst = folded_qp(dest_qpn);
  800. u16 folded_src = folded_qp(qpn);
  801. return (dest_qpn != qpn) ?
  802. ((folded_dst ^ folded_src) | 0xC000) :
  803. folded_src | 0xC000;
  804. }
  805. return 0xdead;
  806. }