qp.c 26 KB

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