qp.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966
  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. refcount_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 (refcount_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, u8 usage)
  215. {
  216. u32 in_modifier = RES_QP | (((u32)usage & 3) << 30);
  217. u64 in_param = 0;
  218. u64 out_param;
  219. int err;
  220. /* Turn off all unsupported QP allocation flags */
  221. flags &= dev->caps.alloc_res_qp_mask;
  222. if (mlx4_is_mfunc(dev)) {
  223. set_param_l(&in_param, (((u32)flags) << 24) | (u32)cnt);
  224. set_param_h(&in_param, align);
  225. err = mlx4_cmd_imm(dev, in_param, &out_param,
  226. in_modifier, RES_OP_RESERVE,
  227. MLX4_CMD_ALLOC_RES,
  228. MLX4_CMD_TIME_CLASS_A, MLX4_CMD_WRAPPED);
  229. if (err)
  230. return err;
  231. *base = get_param_l(&out_param);
  232. return 0;
  233. }
  234. return __mlx4_qp_reserve_range(dev, cnt, align, base, flags);
  235. }
  236. EXPORT_SYMBOL_GPL(mlx4_qp_reserve_range);
  237. void __mlx4_qp_release_range(struct mlx4_dev *dev, int base_qpn, int cnt)
  238. {
  239. struct mlx4_priv *priv = mlx4_priv(dev);
  240. struct mlx4_qp_table *qp_table = &priv->qp_table;
  241. if (mlx4_is_qp_reserved(dev, (u32) base_qpn))
  242. return;
  243. mlx4_zone_free_entries_unique(qp_table->zones, base_qpn, cnt);
  244. }
  245. void mlx4_qp_release_range(struct mlx4_dev *dev, int base_qpn, int cnt)
  246. {
  247. u64 in_param = 0;
  248. int err;
  249. if (!cnt)
  250. return;
  251. if (mlx4_is_mfunc(dev)) {
  252. set_param_l(&in_param, base_qpn);
  253. set_param_h(&in_param, cnt);
  254. err = mlx4_cmd(dev, in_param, RES_QP, RES_OP_RESERVE,
  255. MLX4_CMD_FREE_RES,
  256. MLX4_CMD_TIME_CLASS_A, MLX4_CMD_WRAPPED);
  257. if (err) {
  258. mlx4_warn(dev, "Failed to release qp range base:%d cnt:%d\n",
  259. base_qpn, cnt);
  260. }
  261. } else
  262. __mlx4_qp_release_range(dev, base_qpn, cnt);
  263. }
  264. EXPORT_SYMBOL_GPL(mlx4_qp_release_range);
  265. int __mlx4_qp_alloc_icm(struct mlx4_dev *dev, int qpn)
  266. {
  267. struct mlx4_priv *priv = mlx4_priv(dev);
  268. struct mlx4_qp_table *qp_table = &priv->qp_table;
  269. int err;
  270. err = mlx4_table_get(dev, &qp_table->qp_table, qpn);
  271. if (err)
  272. goto err_out;
  273. err = mlx4_table_get(dev, &qp_table->auxc_table, qpn);
  274. if (err)
  275. goto err_put_qp;
  276. err = mlx4_table_get(dev, &qp_table->altc_table, qpn);
  277. if (err)
  278. goto err_put_auxc;
  279. err = mlx4_table_get(dev, &qp_table->rdmarc_table, qpn);
  280. if (err)
  281. goto err_put_altc;
  282. err = mlx4_table_get(dev, &qp_table->cmpt_table, qpn);
  283. if (err)
  284. goto err_put_rdmarc;
  285. return 0;
  286. err_put_rdmarc:
  287. mlx4_table_put(dev, &qp_table->rdmarc_table, qpn);
  288. err_put_altc:
  289. mlx4_table_put(dev, &qp_table->altc_table, qpn);
  290. err_put_auxc:
  291. mlx4_table_put(dev, &qp_table->auxc_table, qpn);
  292. err_put_qp:
  293. mlx4_table_put(dev, &qp_table->qp_table, qpn);
  294. err_out:
  295. return err;
  296. }
  297. static int mlx4_qp_alloc_icm(struct mlx4_dev *dev, int qpn)
  298. {
  299. u64 param = 0;
  300. if (mlx4_is_mfunc(dev)) {
  301. set_param_l(&param, qpn);
  302. return mlx4_cmd_imm(dev, param, &param, RES_QP, RES_OP_MAP_ICM,
  303. MLX4_CMD_ALLOC_RES, MLX4_CMD_TIME_CLASS_A,
  304. MLX4_CMD_WRAPPED);
  305. }
  306. return __mlx4_qp_alloc_icm(dev, qpn);
  307. }
  308. void __mlx4_qp_free_icm(struct mlx4_dev *dev, int qpn)
  309. {
  310. struct mlx4_priv *priv = mlx4_priv(dev);
  311. struct mlx4_qp_table *qp_table = &priv->qp_table;
  312. mlx4_table_put(dev, &qp_table->cmpt_table, qpn);
  313. mlx4_table_put(dev, &qp_table->rdmarc_table, qpn);
  314. mlx4_table_put(dev, &qp_table->altc_table, qpn);
  315. mlx4_table_put(dev, &qp_table->auxc_table, qpn);
  316. mlx4_table_put(dev, &qp_table->qp_table, qpn);
  317. }
  318. static void mlx4_qp_free_icm(struct mlx4_dev *dev, int qpn)
  319. {
  320. u64 in_param = 0;
  321. if (mlx4_is_mfunc(dev)) {
  322. set_param_l(&in_param, qpn);
  323. if (mlx4_cmd(dev, in_param, RES_QP, RES_OP_MAP_ICM,
  324. MLX4_CMD_FREE_RES, MLX4_CMD_TIME_CLASS_A,
  325. MLX4_CMD_WRAPPED))
  326. mlx4_warn(dev, "Failed to free icm of qp:%d\n", qpn);
  327. } else
  328. __mlx4_qp_free_icm(dev, qpn);
  329. }
  330. struct mlx4_qp *mlx4_qp_lookup(struct mlx4_dev *dev, u32 qpn)
  331. {
  332. struct mlx4_qp_table *qp_table = &mlx4_priv(dev)->qp_table;
  333. struct mlx4_qp *qp;
  334. spin_lock_irq(&qp_table->lock);
  335. qp = __mlx4_qp_lookup(dev, qpn);
  336. spin_unlock_irq(&qp_table->lock);
  337. return qp;
  338. }
  339. int mlx4_qp_alloc(struct mlx4_dev *dev, int qpn, struct mlx4_qp *qp)
  340. {
  341. struct mlx4_priv *priv = mlx4_priv(dev);
  342. struct mlx4_qp_table *qp_table = &priv->qp_table;
  343. int err;
  344. if (!qpn)
  345. return -EINVAL;
  346. qp->qpn = qpn;
  347. err = mlx4_qp_alloc_icm(dev, qpn);
  348. if (err)
  349. return err;
  350. spin_lock_irq(&qp_table->lock);
  351. err = radix_tree_insert(&dev->qp_table_tree, qp->qpn &
  352. (dev->caps.num_qps - 1), qp);
  353. spin_unlock_irq(&qp_table->lock);
  354. if (err)
  355. goto err_icm;
  356. refcount_set(&qp->refcount, 1);
  357. init_completion(&qp->free);
  358. return 0;
  359. err_icm:
  360. mlx4_qp_free_icm(dev, qpn);
  361. return err;
  362. }
  363. EXPORT_SYMBOL_GPL(mlx4_qp_alloc);
  364. int mlx4_update_qp(struct mlx4_dev *dev, u32 qpn,
  365. enum mlx4_update_qp_attr attr,
  366. struct mlx4_update_qp_params *params)
  367. {
  368. struct mlx4_cmd_mailbox *mailbox;
  369. struct mlx4_update_qp_context *cmd;
  370. u64 pri_addr_path_mask = 0;
  371. u64 qp_mask = 0;
  372. int err = 0;
  373. if (!attr || (attr & ~MLX4_UPDATE_QP_SUPPORTED_ATTRS))
  374. return -EINVAL;
  375. mailbox = mlx4_alloc_cmd_mailbox(dev);
  376. if (IS_ERR(mailbox))
  377. return PTR_ERR(mailbox);
  378. cmd = (struct mlx4_update_qp_context *)mailbox->buf;
  379. if (attr & MLX4_UPDATE_QP_SMAC) {
  380. pri_addr_path_mask |= 1ULL << MLX4_UPD_QP_PATH_MASK_MAC_INDEX;
  381. cmd->qp_context.pri_path.grh_mylmc = params->smac_index;
  382. }
  383. if (attr & MLX4_UPDATE_QP_ETH_SRC_CHECK_MC_LB) {
  384. if (!(dev->caps.flags2
  385. & MLX4_DEV_CAP_FLAG2_UPDATE_QP_SRC_CHECK_LB)) {
  386. mlx4_warn(dev,
  387. "Trying to set src check LB, but it isn't supported\n");
  388. err = -EOPNOTSUPP;
  389. goto out;
  390. }
  391. pri_addr_path_mask |=
  392. 1ULL << MLX4_UPD_QP_PATH_MASK_ETH_SRC_CHECK_MC_LB;
  393. if (params->flags &
  394. MLX4_UPDATE_QP_PARAMS_FLAGS_ETH_CHECK_MC_LB) {
  395. cmd->qp_context.pri_path.fl |=
  396. MLX4_FL_ETH_SRC_CHECK_MC_LB;
  397. }
  398. }
  399. if (attr & MLX4_UPDATE_QP_VSD) {
  400. qp_mask |= 1ULL << MLX4_UPD_QP_MASK_VSD;
  401. if (params->flags & MLX4_UPDATE_QP_PARAMS_FLAGS_VSD_ENABLE)
  402. cmd->qp_context.param3 |= cpu_to_be32(MLX4_STRIP_VLAN);
  403. }
  404. if (attr & MLX4_UPDATE_QP_RATE_LIMIT) {
  405. qp_mask |= 1ULL << MLX4_UPD_QP_MASK_RATE_LIMIT;
  406. cmd->qp_context.rate_limit_params = cpu_to_be16((params->rate_unit << 14) | params->rate_val);
  407. }
  408. if (attr & MLX4_UPDATE_QP_QOS_VPORT) {
  409. if (!(dev->caps.flags2 & MLX4_DEV_CAP_FLAG2_QOS_VPP)) {
  410. mlx4_warn(dev, "Granular QoS per VF is not enabled\n");
  411. err = -EOPNOTSUPP;
  412. goto out;
  413. }
  414. qp_mask |= 1ULL << MLX4_UPD_QP_MASK_QOS_VPP;
  415. cmd->qp_context.qos_vport = params->qos_vport;
  416. }
  417. cmd->primary_addr_path_mask = cpu_to_be64(pri_addr_path_mask);
  418. cmd->qp_mask = cpu_to_be64(qp_mask);
  419. err = mlx4_cmd(dev, mailbox->dma, qpn & 0xffffff, 0,
  420. MLX4_CMD_UPDATE_QP, MLX4_CMD_TIME_CLASS_A,
  421. MLX4_CMD_NATIVE);
  422. out:
  423. mlx4_free_cmd_mailbox(dev, mailbox);
  424. return err;
  425. }
  426. EXPORT_SYMBOL_GPL(mlx4_update_qp);
  427. void mlx4_qp_remove(struct mlx4_dev *dev, struct mlx4_qp *qp)
  428. {
  429. struct mlx4_qp_table *qp_table = &mlx4_priv(dev)->qp_table;
  430. unsigned long flags;
  431. spin_lock_irqsave(&qp_table->lock, flags);
  432. radix_tree_delete(&dev->qp_table_tree, qp->qpn & (dev->caps.num_qps - 1));
  433. spin_unlock_irqrestore(&qp_table->lock, flags);
  434. }
  435. EXPORT_SYMBOL_GPL(mlx4_qp_remove);
  436. void mlx4_qp_free(struct mlx4_dev *dev, struct mlx4_qp *qp)
  437. {
  438. if (refcount_dec_and_test(&qp->refcount))
  439. complete(&qp->free);
  440. wait_for_completion(&qp->free);
  441. mlx4_qp_free_icm(dev, qp->qpn);
  442. }
  443. EXPORT_SYMBOL_GPL(mlx4_qp_free);
  444. static int mlx4_CONF_SPECIAL_QP(struct mlx4_dev *dev, u32 base_qpn)
  445. {
  446. return mlx4_cmd(dev, 0, base_qpn, 0, MLX4_CMD_CONF_SPECIAL_QP,
  447. MLX4_CMD_TIME_CLASS_B, MLX4_CMD_NATIVE);
  448. }
  449. #define MLX4_QP_TABLE_RSS_ETH_PRIORITY 2
  450. #define MLX4_QP_TABLE_RAW_ETH_PRIORITY 1
  451. #define MLX4_QP_TABLE_RAW_ETH_SIZE 256
  452. static int mlx4_create_zones(struct mlx4_dev *dev,
  453. u32 reserved_bottom_general,
  454. u32 reserved_top_general,
  455. u32 reserved_bottom_rss,
  456. u32 start_offset_rss,
  457. u32 max_table_offset)
  458. {
  459. struct mlx4_qp_table *qp_table = &mlx4_priv(dev)->qp_table;
  460. struct mlx4_bitmap (*bitmap)[MLX4_QP_TABLE_ZONE_NUM] = NULL;
  461. int bitmap_initialized = 0;
  462. u32 last_offset;
  463. int k;
  464. int err;
  465. qp_table->zones = mlx4_zone_allocator_create(MLX4_ZONE_ALLOC_FLAGS_NO_OVERLAP);
  466. if (NULL == qp_table->zones)
  467. return -ENOMEM;
  468. bitmap = kmalloc(sizeof(*bitmap), GFP_KERNEL);
  469. if (NULL == bitmap) {
  470. err = -ENOMEM;
  471. goto free_zone;
  472. }
  473. err = mlx4_bitmap_init(*bitmap + MLX4_QP_TABLE_ZONE_GENERAL, dev->caps.num_qps,
  474. (1 << 23) - 1, reserved_bottom_general,
  475. reserved_top_general);
  476. if (err)
  477. goto free_bitmap;
  478. ++bitmap_initialized;
  479. err = mlx4_zone_add_one(qp_table->zones, *bitmap + MLX4_QP_TABLE_ZONE_GENERAL,
  480. MLX4_ZONE_FALLBACK_TO_HIGHER_PRIO |
  481. MLX4_ZONE_USE_RR, 0,
  482. 0, qp_table->zones_uids + MLX4_QP_TABLE_ZONE_GENERAL);
  483. if (err)
  484. goto free_bitmap;
  485. err = mlx4_bitmap_init(*bitmap + MLX4_QP_TABLE_ZONE_RSS,
  486. reserved_bottom_rss,
  487. reserved_bottom_rss - 1,
  488. dev->caps.reserved_qps_cnt[MLX4_QP_REGION_FW],
  489. reserved_bottom_rss - start_offset_rss);
  490. if (err)
  491. goto free_bitmap;
  492. ++bitmap_initialized;
  493. err = mlx4_zone_add_one(qp_table->zones, *bitmap + MLX4_QP_TABLE_ZONE_RSS,
  494. MLX4_ZONE_ALLOW_ALLOC_FROM_LOWER_PRIO |
  495. MLX4_ZONE_ALLOW_ALLOC_FROM_EQ_PRIO |
  496. MLX4_ZONE_USE_RR, MLX4_QP_TABLE_RSS_ETH_PRIORITY,
  497. 0, qp_table->zones_uids + MLX4_QP_TABLE_ZONE_RSS);
  498. if (err)
  499. goto free_bitmap;
  500. last_offset = dev->caps.reserved_qps_cnt[MLX4_QP_REGION_FW];
  501. /* We have a single zone for the A0 steering QPs area of the FW. This area
  502. * needs to be split into subareas. One set of subareas is for RSS QPs
  503. * (in which qp number bits 6 and/or 7 are set); the other set of subareas
  504. * is for RAW_ETH QPs, which require that both bits 6 and 7 are zero.
  505. * Currently, the values returned by the FW (A0 steering area starting qp number
  506. * and A0 steering area size) are such that there are only two subareas -- one
  507. * for RSS and one for RAW_ETH.
  508. */
  509. for (k = MLX4_QP_TABLE_ZONE_RSS + 1; k < sizeof(*bitmap)/sizeof((*bitmap)[0]);
  510. k++) {
  511. int size;
  512. u32 offset = start_offset_rss;
  513. u32 bf_mask;
  514. u32 requested_size;
  515. /* Assuming MLX4_BF_QP_SKIP_MASK is consecutive ones, this calculates
  516. * a mask of all LSB bits set until (and not including) the first
  517. * set bit of MLX4_BF_QP_SKIP_MASK. For example, if MLX4_BF_QP_SKIP_MASK
  518. * is 0xc0, bf_mask will be 0x3f.
  519. */
  520. bf_mask = (MLX4_BF_QP_SKIP_MASK & ~(MLX4_BF_QP_SKIP_MASK - 1)) - 1;
  521. requested_size = min((u32)MLX4_QP_TABLE_RAW_ETH_SIZE, bf_mask + 1);
  522. if (((last_offset & MLX4_BF_QP_SKIP_MASK) &&
  523. ((int)(max_table_offset - last_offset)) >=
  524. roundup_pow_of_two(MLX4_BF_QP_SKIP_MASK)) ||
  525. (!(last_offset & MLX4_BF_QP_SKIP_MASK) &&
  526. !((last_offset + requested_size - 1) &
  527. MLX4_BF_QP_SKIP_MASK)))
  528. size = requested_size;
  529. else {
  530. u32 candidate_offset =
  531. (last_offset | MLX4_BF_QP_SKIP_MASK | bf_mask) + 1;
  532. if (last_offset & MLX4_BF_QP_SKIP_MASK)
  533. last_offset = candidate_offset;
  534. /* From this point, the BF bits are 0 */
  535. if (last_offset > max_table_offset) {
  536. /* need to skip */
  537. size = -1;
  538. } else {
  539. size = min3(max_table_offset - last_offset,
  540. bf_mask - (last_offset & bf_mask),
  541. requested_size);
  542. if (size < requested_size) {
  543. int candidate_size;
  544. candidate_size = min3(
  545. max_table_offset - candidate_offset,
  546. bf_mask - (last_offset & bf_mask),
  547. requested_size);
  548. /* We will not take this path if last_offset was
  549. * already set above to candidate_offset
  550. */
  551. if (candidate_size > size) {
  552. last_offset = candidate_offset;
  553. size = candidate_size;
  554. }
  555. }
  556. }
  557. }
  558. if (size > 0) {
  559. /* mlx4_bitmap_alloc_range will find a contiguous range of "size"
  560. * QPs in which both bits 6 and 7 are zero, because we pass it the
  561. * MLX4_BF_SKIP_MASK).
  562. */
  563. offset = mlx4_bitmap_alloc_range(
  564. *bitmap + MLX4_QP_TABLE_ZONE_RSS,
  565. size, 1,
  566. MLX4_BF_QP_SKIP_MASK);
  567. if (offset == (u32)-1) {
  568. err = -ENOMEM;
  569. break;
  570. }
  571. last_offset = offset + size;
  572. err = mlx4_bitmap_init(*bitmap + k, roundup_pow_of_two(size),
  573. roundup_pow_of_two(size) - 1, 0,
  574. roundup_pow_of_two(size) - size);
  575. } else {
  576. /* Add an empty bitmap, we'll allocate from different zones (since
  577. * at least one is reserved)
  578. */
  579. err = mlx4_bitmap_init(*bitmap + k, 1,
  580. MLX4_QP_TABLE_RAW_ETH_SIZE - 1, 0,
  581. 0);
  582. mlx4_bitmap_alloc_range(*bitmap + k, 1, 1, 0);
  583. }
  584. if (err)
  585. break;
  586. ++bitmap_initialized;
  587. err = mlx4_zone_add_one(qp_table->zones, *bitmap + k,
  588. MLX4_ZONE_ALLOW_ALLOC_FROM_LOWER_PRIO |
  589. MLX4_ZONE_ALLOW_ALLOC_FROM_EQ_PRIO |
  590. MLX4_ZONE_USE_RR, MLX4_QP_TABLE_RAW_ETH_PRIORITY,
  591. offset, qp_table->zones_uids + k);
  592. if (err)
  593. break;
  594. }
  595. if (err)
  596. goto free_bitmap;
  597. qp_table->bitmap_gen = *bitmap;
  598. return err;
  599. free_bitmap:
  600. for (k = 0; k < bitmap_initialized; k++)
  601. mlx4_bitmap_cleanup(*bitmap + k);
  602. kfree(bitmap);
  603. free_zone:
  604. mlx4_zone_allocator_destroy(qp_table->zones);
  605. return err;
  606. }
  607. static void mlx4_cleanup_qp_zones(struct mlx4_dev *dev)
  608. {
  609. struct mlx4_qp_table *qp_table = &mlx4_priv(dev)->qp_table;
  610. if (qp_table->zones) {
  611. int i;
  612. for (i = 0;
  613. i < sizeof(qp_table->zones_uids)/sizeof(qp_table->zones_uids[0]);
  614. i++) {
  615. struct mlx4_bitmap *bitmap =
  616. mlx4_zone_get_bitmap(qp_table->zones,
  617. qp_table->zones_uids[i]);
  618. mlx4_zone_remove_one(qp_table->zones, qp_table->zones_uids[i]);
  619. if (NULL == bitmap)
  620. continue;
  621. mlx4_bitmap_cleanup(bitmap);
  622. }
  623. mlx4_zone_allocator_destroy(qp_table->zones);
  624. kfree(qp_table->bitmap_gen);
  625. qp_table->bitmap_gen = NULL;
  626. qp_table->zones = NULL;
  627. }
  628. }
  629. int mlx4_init_qp_table(struct mlx4_dev *dev)
  630. {
  631. struct mlx4_qp_table *qp_table = &mlx4_priv(dev)->qp_table;
  632. int err;
  633. int reserved_from_top = 0;
  634. int reserved_from_bot;
  635. int k;
  636. int fixed_reserved_from_bot_rv = 0;
  637. int bottom_reserved_for_rss_bitmap;
  638. u32 max_table_offset = dev->caps.dmfs_high_rate_qpn_base +
  639. dev->caps.dmfs_high_rate_qpn_range;
  640. spin_lock_init(&qp_table->lock);
  641. INIT_RADIX_TREE(&dev->qp_table_tree, GFP_ATOMIC);
  642. if (mlx4_is_slave(dev))
  643. return 0;
  644. /* We reserve 2 extra QPs per port for the special QPs. The
  645. * block of special QPs must be aligned to a multiple of 8, so
  646. * round up.
  647. *
  648. * We also reserve the MSB of the 24-bit QP number to indicate
  649. * that a QP is an XRC QP.
  650. */
  651. for (k = 0; k <= MLX4_QP_REGION_BOTTOM; k++)
  652. fixed_reserved_from_bot_rv += dev->caps.reserved_qps_cnt[k];
  653. if (fixed_reserved_from_bot_rv < max_table_offset)
  654. fixed_reserved_from_bot_rv = max_table_offset;
  655. /* We reserve at least 1 extra for bitmaps that we don't have enough space for*/
  656. bottom_reserved_for_rss_bitmap =
  657. roundup_pow_of_two(fixed_reserved_from_bot_rv + 1);
  658. dev->phys_caps.base_sqpn = ALIGN(bottom_reserved_for_rss_bitmap, 8);
  659. {
  660. int sort[MLX4_NUM_QP_REGION];
  661. int i, j;
  662. int last_base = dev->caps.num_qps;
  663. for (i = 1; i < MLX4_NUM_QP_REGION; ++i)
  664. sort[i] = i;
  665. for (i = MLX4_NUM_QP_REGION; i > MLX4_QP_REGION_BOTTOM; --i) {
  666. for (j = MLX4_QP_REGION_BOTTOM + 2; j < i; ++j) {
  667. if (dev->caps.reserved_qps_cnt[sort[j]] >
  668. dev->caps.reserved_qps_cnt[sort[j - 1]])
  669. swap(sort[j], sort[j - 1]);
  670. }
  671. }
  672. for (i = MLX4_QP_REGION_BOTTOM + 1; i < MLX4_NUM_QP_REGION; ++i) {
  673. last_base -= dev->caps.reserved_qps_cnt[sort[i]];
  674. dev->caps.reserved_qps_base[sort[i]] = last_base;
  675. reserved_from_top +=
  676. dev->caps.reserved_qps_cnt[sort[i]];
  677. }
  678. }
  679. /* Reserve 8 real SQPs in both native and SRIOV modes.
  680. * In addition, in SRIOV mode, reserve 8 proxy SQPs per function
  681. * (for all PFs and VFs), and 8 corresponding tunnel QPs.
  682. * Each proxy SQP works opposite its own tunnel QP.
  683. *
  684. * The QPs are arranged as follows:
  685. * a. 8 real SQPs
  686. * b. All the proxy SQPs (8 per function)
  687. * c. All the tunnel QPs (8 per function)
  688. */
  689. reserved_from_bot = mlx4_num_reserved_sqps(dev);
  690. if (reserved_from_bot + reserved_from_top > dev->caps.num_qps) {
  691. mlx4_err(dev, "Number of reserved QPs is higher than number of QPs\n");
  692. return -EINVAL;
  693. }
  694. err = mlx4_create_zones(dev, reserved_from_bot, reserved_from_bot,
  695. bottom_reserved_for_rss_bitmap,
  696. fixed_reserved_from_bot_rv,
  697. max_table_offset);
  698. if (err)
  699. return err;
  700. if (mlx4_is_mfunc(dev)) {
  701. /* for PPF use */
  702. dev->phys_caps.base_proxy_sqpn = dev->phys_caps.base_sqpn + 8;
  703. dev->phys_caps.base_tunnel_sqpn = dev->phys_caps.base_sqpn + 8 + 8 * MLX4_MFUNC_MAX;
  704. /* In mfunc, calculate proxy and tunnel qp offsets for the PF here,
  705. * since the PF does not call mlx4_slave_caps */
  706. dev->caps.spec_qps = kcalloc(dev->caps.num_ports,
  707. sizeof(*dev->caps.spec_qps),
  708. GFP_KERNEL);
  709. if (!dev->caps.spec_qps) {
  710. err = -ENOMEM;
  711. goto err_mem;
  712. }
  713. for (k = 0; k < dev->caps.num_ports; k++) {
  714. dev->caps.spec_qps[k].qp0_proxy = dev->phys_caps.base_proxy_sqpn +
  715. 8 * mlx4_master_func_num(dev) + k;
  716. dev->caps.spec_qps[k].qp0_tunnel = dev->caps.spec_qps[k].qp0_proxy + 8 * MLX4_MFUNC_MAX;
  717. dev->caps.spec_qps[k].qp1_proxy = dev->phys_caps.base_proxy_sqpn +
  718. 8 * mlx4_master_func_num(dev) + MLX4_MAX_PORTS + k;
  719. dev->caps.spec_qps[k].qp1_tunnel = dev->caps.spec_qps[k].qp1_proxy + 8 * MLX4_MFUNC_MAX;
  720. }
  721. }
  722. err = mlx4_CONF_SPECIAL_QP(dev, dev->phys_caps.base_sqpn);
  723. if (err)
  724. goto err_mem;
  725. return err;
  726. err_mem:
  727. kfree(dev->caps.spec_qps);
  728. dev->caps.spec_qps = NULL;
  729. mlx4_cleanup_qp_zones(dev);
  730. return err;
  731. }
  732. void mlx4_cleanup_qp_table(struct mlx4_dev *dev)
  733. {
  734. if (mlx4_is_slave(dev))
  735. return;
  736. mlx4_CONF_SPECIAL_QP(dev, 0);
  737. mlx4_cleanup_qp_zones(dev);
  738. }
  739. int mlx4_qp_query(struct mlx4_dev *dev, struct mlx4_qp *qp,
  740. struct mlx4_qp_context *context)
  741. {
  742. struct mlx4_cmd_mailbox *mailbox;
  743. int err;
  744. mailbox = mlx4_alloc_cmd_mailbox(dev);
  745. if (IS_ERR(mailbox))
  746. return PTR_ERR(mailbox);
  747. err = mlx4_cmd_box(dev, 0, mailbox->dma, qp->qpn, 0,
  748. MLX4_CMD_QUERY_QP, MLX4_CMD_TIME_CLASS_A,
  749. MLX4_CMD_WRAPPED);
  750. if (!err)
  751. memcpy(context, mailbox->buf + 8, sizeof(*context));
  752. mlx4_free_cmd_mailbox(dev, mailbox);
  753. return err;
  754. }
  755. EXPORT_SYMBOL_GPL(mlx4_qp_query);
  756. int mlx4_qp_to_ready(struct mlx4_dev *dev, struct mlx4_mtt *mtt,
  757. struct mlx4_qp_context *context,
  758. struct mlx4_qp *qp, enum mlx4_qp_state *qp_state)
  759. {
  760. int err;
  761. int i;
  762. enum mlx4_qp_state states[] = {
  763. MLX4_QP_STATE_RST,
  764. MLX4_QP_STATE_INIT,
  765. MLX4_QP_STATE_RTR,
  766. MLX4_QP_STATE_RTS
  767. };
  768. for (i = 0; i < ARRAY_SIZE(states) - 1; i++) {
  769. context->flags &= cpu_to_be32(~(0xf << 28));
  770. context->flags |= cpu_to_be32(states[i + 1] << 28);
  771. if (states[i + 1] != MLX4_QP_STATE_RTR)
  772. context->params2 &= ~cpu_to_be32(MLX4_QP_BIT_FPP);
  773. err = mlx4_qp_modify(dev, mtt, states[i], states[i + 1],
  774. context, 0, 0, qp);
  775. if (err) {
  776. mlx4_err(dev, "Failed to bring QP to state: %d with error: %d\n",
  777. states[i + 1], err);
  778. return err;
  779. }
  780. *qp_state = states[i + 1];
  781. }
  782. return 0;
  783. }
  784. EXPORT_SYMBOL_GPL(mlx4_qp_to_ready);
  785. u16 mlx4_qp_roce_entropy(struct mlx4_dev *dev, u32 qpn)
  786. {
  787. struct mlx4_qp_context context;
  788. struct mlx4_qp qp;
  789. int err;
  790. qp.qpn = qpn;
  791. err = mlx4_qp_query(dev, &qp, &context);
  792. if (!err) {
  793. u32 dest_qpn = be32_to_cpu(context.remote_qpn) & 0xffffff;
  794. u16 folded_dst = folded_qp(dest_qpn);
  795. u16 folded_src = folded_qp(qpn);
  796. return (dest_qpn != qpn) ?
  797. ((folded_dst ^ folded_src) | 0xC000) :
  798. folded_src | 0xC000;
  799. }
  800. return 0xdead;
  801. }