blob: 2715e61dbb74613371b13e324c7f76c7482cb3d3 [file] [log] [blame]
Roland Dreier225c7b12007-05-08 18:00:38 -07001/*
2 * Copyright (c) 2004 Topspin Communications. All rights reserved.
3 * Copyright (c) 2005, 2006, 2007 Cisco Systems, Inc. All rights reserved.
Jack Morgenstein51a379d2008-07-25 10:32:52 -07004 * Copyright (c) 2005, 2006, 2007, 2008 Mellanox Technologies. All rights reserved.
Roland Dreier225c7b12007-05-08 18:00:38 -07005 * 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
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090036#include <linux/gfp.h>
Paul Gortmakeree40fa02011-05-27 16:14:23 -040037#include <linux/export.h>
Jack Morgensteinfe9a2602011-12-13 04:13:22 +000038#include <linux/init.h>
39
Roland Dreier225c7b12007-05-08 18:00:38 -070040#include <linux/mlx4/cmd.h>
41#include <linux/mlx4/qp.h>
42
43#include "mlx4.h"
44#include "icm.h"
45
46void mlx4_qp_event(struct mlx4_dev *dev, u32 qpn, int event_type)
47{
48 struct mlx4_qp_table *qp_table = &mlx4_priv(dev)->qp_table;
49 struct mlx4_qp *qp;
50
51 spin_lock(&qp_table->lock);
52
53 qp = __mlx4_qp_lookup(dev, qpn);
54 if (qp)
55 atomic_inc(&qp->refcount);
56
57 spin_unlock(&qp_table->lock);
58
59 if (!qp) {
Jack Morgensteinfe9a2602011-12-13 04:13:22 +000060 mlx4_dbg(dev, "Async event for none existent QP %08x\n", qpn);
Roland Dreier225c7b12007-05-08 18:00:38 -070061 return;
62 }
63
64 qp->event(qp, event_type);
65
66 if (atomic_dec_and_test(&qp->refcount))
67 complete(&qp->free);
68}
69
Jack Morgenstein980e9002012-08-03 08:40:53 +000070/* used for INIT/CLOSE port logic */
Jack Morgenstein47605df2012-08-03 08:40:57 +000071static int is_master_qp0(struct mlx4_dev *dev, struct mlx4_qp *qp, int *real_qp0, int *proxy_qp0)
Jack Morgensteinfe9a2602011-12-13 04:13:22 +000072{
Jack Morgenstein47605df2012-08-03 08:40:57 +000073 /* this procedure is called after we already know we are on the master */
Jack Morgenstein980e9002012-08-03 08:40:53 +000074 /* qp0 is either the proxy qp0, or the real qp0 */
Jack Morgenstein47605df2012-08-03 08:40:57 +000075 u32 pf_proxy_offset = dev->phys_caps.base_proxy_sqpn + 8 * mlx4_master_func_num(dev);
76 *proxy_qp0 = qp->qpn >= pf_proxy_offset && qp->qpn <= pf_proxy_offset + 1;
Jack Morgenstein980e9002012-08-03 08:40:53 +000077
Jack Morgenstein47605df2012-08-03 08:40:57 +000078 *real_qp0 = qp->qpn >= dev->phys_caps.base_sqpn &&
79 qp->qpn <= dev->phys_caps.base_sqpn + 1;
Jack Morgenstein980e9002012-08-03 08:40:53 +000080
81 return *real_qp0 || *proxy_qp0;
Jack Morgensteinfe9a2602011-12-13 04:13:22 +000082}
83
84static int __mlx4_qp_modify(struct mlx4_dev *dev, struct mlx4_mtt *mtt,
85 enum mlx4_qp_state cur_state, enum mlx4_qp_state new_state,
86 struct mlx4_qp_context *context,
87 enum mlx4_qp_optpar optpar,
88 int sqd_event, struct mlx4_qp *qp, int native)
Roland Dreier225c7b12007-05-08 18:00:38 -070089{
90 static const u16 op[MLX4_QP_NUM_STATE][MLX4_QP_NUM_STATE] = {
91 [MLX4_QP_STATE_RST] = {
92 [MLX4_QP_STATE_RST] = MLX4_CMD_2RST_QP,
93 [MLX4_QP_STATE_ERR] = MLX4_CMD_2ERR_QP,
94 [MLX4_QP_STATE_INIT] = MLX4_CMD_RST2INIT_QP,
95 },
96 [MLX4_QP_STATE_INIT] = {
97 [MLX4_QP_STATE_RST] = MLX4_CMD_2RST_QP,
98 [MLX4_QP_STATE_ERR] = MLX4_CMD_2ERR_QP,
99 [MLX4_QP_STATE_INIT] = MLX4_CMD_INIT2INIT_QP,
100 [MLX4_QP_STATE_RTR] = MLX4_CMD_INIT2RTR_QP,
101 },
102 [MLX4_QP_STATE_RTR] = {
103 [MLX4_QP_STATE_RST] = MLX4_CMD_2RST_QP,
104 [MLX4_QP_STATE_ERR] = MLX4_CMD_2ERR_QP,
105 [MLX4_QP_STATE_RTS] = MLX4_CMD_RTR2RTS_QP,
106 },
107 [MLX4_QP_STATE_RTS] = {
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_RTS2RTS_QP,
111 [MLX4_QP_STATE_SQD] = MLX4_CMD_RTS2SQD_QP,
112 },
113 [MLX4_QP_STATE_SQD] = {
114 [MLX4_QP_STATE_RST] = MLX4_CMD_2RST_QP,
115 [MLX4_QP_STATE_ERR] = MLX4_CMD_2ERR_QP,
116 [MLX4_QP_STATE_RTS] = MLX4_CMD_SQD2RTS_QP,
117 [MLX4_QP_STATE_SQD] = MLX4_CMD_SQD2SQD_QP,
118 },
119 [MLX4_QP_STATE_SQER] = {
120 [MLX4_QP_STATE_RST] = MLX4_CMD_2RST_QP,
121 [MLX4_QP_STATE_ERR] = MLX4_CMD_2ERR_QP,
122 [MLX4_QP_STATE_RTS] = MLX4_CMD_SQERR2RTS_QP,
123 },
124 [MLX4_QP_STATE_ERR] = {
125 [MLX4_QP_STATE_RST] = MLX4_CMD_2RST_QP,
126 [MLX4_QP_STATE_ERR] = MLX4_CMD_2ERR_QP,
127 }
128 };
129
Jack Morgensteinfe9a2602011-12-13 04:13:22 +0000130 struct mlx4_priv *priv = mlx4_priv(dev);
Roland Dreier225c7b12007-05-08 18:00:38 -0700131 struct mlx4_cmd_mailbox *mailbox;
132 int ret = 0;
Jack Morgenstein980e9002012-08-03 08:40:53 +0000133 int real_qp0 = 0;
134 int proxy_qp0 = 0;
Jack Morgensteinfe9a2602011-12-13 04:13:22 +0000135 u8 port;
Roland Dreier225c7b12007-05-08 18:00:38 -0700136
Jack Morgenstein9ed87fd2007-11-20 13:01:28 -0800137 if (cur_state >= MLX4_QP_NUM_STATE || new_state >= MLX4_QP_NUM_STATE ||
Roland Dreier225c7b12007-05-08 18:00:38 -0700138 !op[cur_state][new_state])
139 return -EINVAL;
140
Jack Morgensteinfe9a2602011-12-13 04:13:22 +0000141 if (op[cur_state][new_state] == MLX4_CMD_2RST_QP) {
142 ret = mlx4_cmd(dev, 0, qp->qpn, 2,
143 MLX4_CMD_2RST_QP, MLX4_CMD_TIME_CLASS_A, native);
144 if (mlx4_is_master(dev) && cur_state != MLX4_QP_STATE_ERR &&
145 cur_state != MLX4_QP_STATE_RST &&
Jack Morgenstein47605df2012-08-03 08:40:57 +0000146 is_master_qp0(dev, qp, &real_qp0, &proxy_qp0)) {
Jack Morgensteinfe9a2602011-12-13 04:13:22 +0000147 port = (qp->qpn & 1) + 1;
Jack Morgenstein980e9002012-08-03 08:40:53 +0000148 if (proxy_qp0)
149 priv->mfunc.master.qp0_state[port].proxy_qp0_active = 0;
150 else
151 priv->mfunc.master.qp0_state[port].qp0_active = 0;
Jack Morgensteinfe9a2602011-12-13 04:13:22 +0000152 }
153 return ret;
154 }
Roland Dreier225c7b12007-05-08 18:00:38 -0700155
156 mailbox = mlx4_alloc_cmd_mailbox(dev);
157 if (IS_ERR(mailbox))
158 return PTR_ERR(mailbox);
159
160 if (cur_state == MLX4_QP_STATE_RST && new_state == MLX4_QP_STATE_INIT) {
161 u64 mtt_addr = mlx4_mtt_addr(dev, mtt);
162 context->mtt_base_addr_h = mtt_addr >> 32;
163 context->mtt_base_addr_l = cpu_to_be32(mtt_addr & 0xffffffff);
164 context->log_page_size = mtt->page_shift - MLX4_ICM_PAGE_SHIFT;
165 }
166
167 *(__be32 *) mailbox->buf = cpu_to_be32(optpar);
168 memcpy(mailbox->buf + 8, context, sizeof *context);
169
170 ((struct mlx4_qp_context *) (mailbox->buf + 8))->local_qpn =
171 cpu_to_be32(qp->qpn);
172
Marcel Apfelbaumeb410492012-01-19 09:45:19 +0000173 ret = mlx4_cmd(dev, mailbox->dma,
Jack Morgensteinfe9a2602011-12-13 04:13:22 +0000174 qp->qpn | (!!sqd_event << 31),
Roland Dreier225c7b12007-05-08 18:00:38 -0700175 new_state == MLX4_QP_STATE_RST ? 2 : 0,
Jack Morgensteinfe9a2602011-12-13 04:13:22 +0000176 op[cur_state][new_state], MLX4_CMD_TIME_CLASS_C, native);
Roland Dreier225c7b12007-05-08 18:00:38 -0700177
Jack Morgenstein47605df2012-08-03 08:40:57 +0000178 if (mlx4_is_master(dev) && is_master_qp0(dev, qp, &real_qp0, &proxy_qp0)) {
Jack Morgenstein980e9002012-08-03 08:40:53 +0000179 port = (qp->qpn & 1) + 1;
180 if (cur_state != MLX4_QP_STATE_ERR &&
181 cur_state != MLX4_QP_STATE_RST &&
182 new_state == MLX4_QP_STATE_ERR) {
183 if (proxy_qp0)
184 priv->mfunc.master.qp0_state[port].proxy_qp0_active = 0;
185 else
186 priv->mfunc.master.qp0_state[port].qp0_active = 0;
187 } else if (new_state == MLX4_QP_STATE_RTR) {
188 if (proxy_qp0)
189 priv->mfunc.master.qp0_state[port].proxy_qp0_active = 1;
190 else
191 priv->mfunc.master.qp0_state[port].qp0_active = 1;
192 }
193 }
194
Roland Dreier225c7b12007-05-08 18:00:38 -0700195 mlx4_free_cmd_mailbox(dev, mailbox);
196 return ret;
197}
Jack Morgensteinfe9a2602011-12-13 04:13:22 +0000198
199int mlx4_qp_modify(struct mlx4_dev *dev, struct mlx4_mtt *mtt,
200 enum mlx4_qp_state cur_state, enum mlx4_qp_state new_state,
201 struct mlx4_qp_context *context,
202 enum mlx4_qp_optpar optpar,
203 int sqd_event, struct mlx4_qp *qp)
204{
205 return __mlx4_qp_modify(dev, mtt, cur_state, new_state, context,
206 optpar, sqd_event, qp, 0);
207}
Roland Dreier225c7b12007-05-08 18:00:38 -0700208EXPORT_SYMBOL_GPL(mlx4_qp_modify);
209
Eli Cohenc82e9aa2011-12-13 04:15:24 +0000210int __mlx4_qp_reserve_range(struct mlx4_dev *dev, int cnt, int align,
Jack Morgensteinfe9a2602011-12-13 04:13:22 +0000211 int *base)
212{
213 struct mlx4_priv *priv = mlx4_priv(dev);
214 struct mlx4_qp_table *qp_table = &priv->qp_table;
215
216 *base = mlx4_bitmap_alloc_range(&qp_table->bitmap, cnt, align);
217 if (*base == -1)
218 return -ENOMEM;
219
220 return 0;
221}
222
Yevgeny Petrilina3cdcbf2008-10-10 12:01:37 -0700223int mlx4_qp_reserve_range(struct mlx4_dev *dev, int cnt, int align, int *base)
224{
Jack Morgensteine7dbeba2013-03-07 03:46:54 +0000225 u64 in_param = 0;
Jack Morgensteinfe9a2602011-12-13 04:13:22 +0000226 u64 out_param;
227 int err;
Yevgeny Petrilina3cdcbf2008-10-10 12:01:37 -0700228
Jack Morgensteinfe9a2602011-12-13 04:13:22 +0000229 if (mlx4_is_mfunc(dev)) {
230 set_param_l(&in_param, cnt);
231 set_param_h(&in_param, align);
232 err = mlx4_cmd_imm(dev, in_param, &out_param,
233 RES_QP, RES_OP_RESERVE,
234 MLX4_CMD_ALLOC_RES,
235 MLX4_CMD_TIME_CLASS_A, MLX4_CMD_WRAPPED);
236 if (err)
237 return err;
Yevgeny Petrilina3cdcbf2008-10-10 12:01:37 -0700238
Jack Morgensteinfe9a2602011-12-13 04:13:22 +0000239 *base = get_param_l(&out_param);
240 return 0;
241 }
242 return __mlx4_qp_reserve_range(dev, cnt, align, base);
Yevgeny Petrilina3cdcbf2008-10-10 12:01:37 -0700243}
244EXPORT_SYMBOL_GPL(mlx4_qp_reserve_range);
245
Eli Cohenc82e9aa2011-12-13 04:15:24 +0000246void __mlx4_qp_release_range(struct mlx4_dev *dev, int base_qpn, int cnt)
Yevgeny Petrilina3cdcbf2008-10-10 12:01:37 -0700247{
248 struct mlx4_priv *priv = mlx4_priv(dev);
249 struct mlx4_qp_table *qp_table = &priv->qp_table;
Yevgeny Petrilina3cdcbf2008-10-10 12:01:37 -0700250
Jack Morgensteinfe9a2602011-12-13 04:13:22 +0000251 if (mlx4_is_qp_reserved(dev, (u32) base_qpn))
252 return;
Yevgeny Petrilina3cdcbf2008-10-10 12:01:37 -0700253 mlx4_bitmap_free_range(&qp_table->bitmap, base_qpn, cnt);
254}
Jack Morgensteinfe9a2602011-12-13 04:13:22 +0000255
256void mlx4_qp_release_range(struct mlx4_dev *dev, int base_qpn, int cnt)
257{
Jack Morgensteine7dbeba2013-03-07 03:46:54 +0000258 u64 in_param = 0;
Jack Morgensteinfe9a2602011-12-13 04:13:22 +0000259 int err;
260
261 if (mlx4_is_mfunc(dev)) {
262 set_param_l(&in_param, base_qpn);
263 set_param_h(&in_param, cnt);
264 err = mlx4_cmd(dev, in_param, RES_QP, RES_OP_RESERVE,
265 MLX4_CMD_FREE_RES,
266 MLX4_CMD_TIME_CLASS_A, MLX4_CMD_WRAPPED);
267 if (err) {
268 mlx4_warn(dev, "Failed to release qp range"
269 " base:%d cnt:%d\n", base_qpn, cnt);
270 }
271 } else
272 __mlx4_qp_release_range(dev, base_qpn, cnt);
273}
Yevgeny Petrilina3cdcbf2008-10-10 12:01:37 -0700274EXPORT_SYMBOL_GPL(mlx4_qp_release_range);
275
Eli Cohenc82e9aa2011-12-13 04:15:24 +0000276int __mlx4_qp_alloc_icm(struct mlx4_dev *dev, int qpn)
Jack Morgensteinfe9a2602011-12-13 04:13:22 +0000277{
278 struct mlx4_priv *priv = mlx4_priv(dev);
279 struct mlx4_qp_table *qp_table = &priv->qp_table;
280 int err;
281
282 err = mlx4_table_get(dev, &qp_table->qp_table, qpn);
283 if (err)
284 goto err_out;
285
286 err = mlx4_table_get(dev, &qp_table->auxc_table, qpn);
287 if (err)
288 goto err_put_qp;
289
290 err = mlx4_table_get(dev, &qp_table->altc_table, qpn);
291 if (err)
292 goto err_put_auxc;
293
294 err = mlx4_table_get(dev, &qp_table->rdmarc_table, qpn);
295 if (err)
296 goto err_put_altc;
297
298 err = mlx4_table_get(dev, &qp_table->cmpt_table, qpn);
299 if (err)
300 goto err_put_rdmarc;
301
302 return 0;
303
304err_put_rdmarc:
305 mlx4_table_put(dev, &qp_table->rdmarc_table, qpn);
306
307err_put_altc:
308 mlx4_table_put(dev, &qp_table->altc_table, qpn);
309
310err_put_auxc:
311 mlx4_table_put(dev, &qp_table->auxc_table, qpn);
312
313err_put_qp:
314 mlx4_table_put(dev, &qp_table->qp_table, qpn);
315
316err_out:
317 return err;
318}
319
320static int mlx4_qp_alloc_icm(struct mlx4_dev *dev, int qpn)
321{
Jack Morgensteine7dbeba2013-03-07 03:46:54 +0000322 u64 param = 0;
Jack Morgensteinfe9a2602011-12-13 04:13:22 +0000323
324 if (mlx4_is_mfunc(dev)) {
325 set_param_l(&param, qpn);
326 return mlx4_cmd_imm(dev, param, &param, RES_QP, RES_OP_MAP_ICM,
327 MLX4_CMD_ALLOC_RES, MLX4_CMD_TIME_CLASS_A,
328 MLX4_CMD_WRAPPED);
329 }
330 return __mlx4_qp_alloc_icm(dev, qpn);
331}
332
Eli Cohenc82e9aa2011-12-13 04:15:24 +0000333void __mlx4_qp_free_icm(struct mlx4_dev *dev, int qpn)
Jack Morgensteinfe9a2602011-12-13 04:13:22 +0000334{
335 struct mlx4_priv *priv = mlx4_priv(dev);
336 struct mlx4_qp_table *qp_table = &priv->qp_table;
337
338 mlx4_table_put(dev, &qp_table->cmpt_table, qpn);
339 mlx4_table_put(dev, &qp_table->rdmarc_table, qpn);
340 mlx4_table_put(dev, &qp_table->altc_table, qpn);
341 mlx4_table_put(dev, &qp_table->auxc_table, qpn);
342 mlx4_table_put(dev, &qp_table->qp_table, qpn);
343}
344
345static void mlx4_qp_free_icm(struct mlx4_dev *dev, int qpn)
346{
Jack Morgensteine7dbeba2013-03-07 03:46:54 +0000347 u64 in_param = 0;
Jack Morgensteinfe9a2602011-12-13 04:13:22 +0000348
349 if (mlx4_is_mfunc(dev)) {
350 set_param_l(&in_param, qpn);
351 if (mlx4_cmd(dev, in_param, RES_QP, RES_OP_MAP_ICM,
352 MLX4_CMD_FREE_RES, MLX4_CMD_TIME_CLASS_A,
353 MLX4_CMD_WRAPPED))
354 mlx4_warn(dev, "Failed to free icm of qp:%d\n", qpn);
355 } else
356 __mlx4_qp_free_icm(dev, qpn);
357}
358
Yevgeny Petrilina3cdcbf2008-10-10 12:01:37 -0700359int mlx4_qp_alloc(struct mlx4_dev *dev, int qpn, struct mlx4_qp *qp)
Roland Dreier225c7b12007-05-08 18:00:38 -0700360{
361 struct mlx4_priv *priv = mlx4_priv(dev);
362 struct mlx4_qp_table *qp_table = &priv->qp_table;
363 int err;
364
Yevgeny Petrilina3cdcbf2008-10-10 12:01:37 -0700365 if (!qpn)
366 return -EINVAL;
367
368 qp->qpn = qpn;
Roland Dreier225c7b12007-05-08 18:00:38 -0700369
Jack Morgensteinfe9a2602011-12-13 04:13:22 +0000370 err = mlx4_qp_alloc_icm(dev, qpn);
Roland Dreier225c7b12007-05-08 18:00:38 -0700371 if (err)
Jack Morgensteinfe9a2602011-12-13 04:13:22 +0000372 return err;
Roland Dreier225c7b12007-05-08 18:00:38 -0700373
374 spin_lock_irq(&qp_table->lock);
Jack Morgensteinfe9a2602011-12-13 04:13:22 +0000375 err = radix_tree_insert(&dev->qp_table_tree, qp->qpn &
376 (dev->caps.num_qps - 1), qp);
Roland Dreier225c7b12007-05-08 18:00:38 -0700377 spin_unlock_irq(&qp_table->lock);
378 if (err)
Jack Morgensteinfe9a2602011-12-13 04:13:22 +0000379 goto err_icm;
Roland Dreier225c7b12007-05-08 18:00:38 -0700380
381 atomic_set(&qp->refcount, 1);
382 init_completion(&qp->free);
383
384 return 0;
385
Jack Morgensteinfe9a2602011-12-13 04:13:22 +0000386err_icm:
387 mlx4_qp_free_icm(dev, qpn);
Roland Dreier225c7b12007-05-08 18:00:38 -0700388 return err;
389}
Jack Morgensteinfe9a2602011-12-13 04:13:22 +0000390
Roland Dreier225c7b12007-05-08 18:00:38 -0700391EXPORT_SYMBOL_GPL(mlx4_qp_alloc);
392
393void mlx4_qp_remove(struct mlx4_dev *dev, struct mlx4_qp *qp)
394{
395 struct mlx4_qp_table *qp_table = &mlx4_priv(dev)->qp_table;
396 unsigned long flags;
397
398 spin_lock_irqsave(&qp_table->lock, flags);
399 radix_tree_delete(&dev->qp_table_tree, qp->qpn & (dev->caps.num_qps - 1));
400 spin_unlock_irqrestore(&qp_table->lock, flags);
401}
402EXPORT_SYMBOL_GPL(mlx4_qp_remove);
403
404void mlx4_qp_free(struct mlx4_dev *dev, struct mlx4_qp *qp)
405{
Roland Dreier225c7b12007-05-08 18:00:38 -0700406 if (atomic_dec_and_test(&qp->refcount))
407 complete(&qp->free);
408 wait_for_completion(&qp->free);
409
Jack Morgensteinfe9a2602011-12-13 04:13:22 +0000410 mlx4_qp_free_icm(dev, qp->qpn);
Roland Dreier225c7b12007-05-08 18:00:38 -0700411}
412EXPORT_SYMBOL_GPL(mlx4_qp_free);
413
414static int mlx4_CONF_SPECIAL_QP(struct mlx4_dev *dev, u32 base_qpn)
415{
416 return mlx4_cmd(dev, 0, base_qpn, 0, MLX4_CMD_CONF_SPECIAL_QP,
Jack Morgensteinf9baff52011-12-13 04:10:51 +0000417 MLX4_CMD_TIME_CLASS_B, MLX4_CMD_NATIVE);
Roland Dreier225c7b12007-05-08 18:00:38 -0700418}
419
Roland Dreier3d73c282007-10-10 15:43:54 -0700420int mlx4_init_qp_table(struct mlx4_dev *dev)
Roland Dreier225c7b12007-05-08 18:00:38 -0700421{
422 struct mlx4_qp_table *qp_table = &mlx4_priv(dev)->qp_table;
423 int err;
Yevgeny Petrilin93fc9e12008-10-22 10:25:29 -0700424 int reserved_from_top = 0;
Jack Morgenstein47605df2012-08-03 08:40:57 +0000425 int k;
Roland Dreier225c7b12007-05-08 18:00:38 -0700426
427 spin_lock_init(&qp_table->lock);
428 INIT_RADIX_TREE(&dev->qp_table_tree, GFP_ATOMIC);
Jack Morgensteinfe9a2602011-12-13 04:13:22 +0000429 if (mlx4_is_slave(dev))
430 return 0;
Roland Dreier225c7b12007-05-08 18:00:38 -0700431
432 /*
433 * We reserve 2 extra QPs per port for the special QPs. The
434 * block of special QPs must be aligned to a multiple of 8, so
435 * round up.
Sean Hefty0a1405d2011-06-02 11:32:15 -0700436 *
437 * We also reserve the MSB of the 24-bit QP number to indicate
438 * that a QP is an XRC QP.
Roland Dreier225c7b12007-05-08 18:00:38 -0700439 */
Jack Morgenstein47605df2012-08-03 08:40:57 +0000440 dev->phys_caps.base_sqpn =
Yevgeny Petrilin93fc9e12008-10-22 10:25:29 -0700441 ALIGN(dev->caps.reserved_qps_cnt[MLX4_QP_REGION_FW], 8);
442
443 {
444 int sort[MLX4_NUM_QP_REGION];
445 int i, j, tmp;
446 int last_base = dev->caps.num_qps;
447
448 for (i = 1; i < MLX4_NUM_QP_REGION; ++i)
449 sort[i] = i;
450
451 for (i = MLX4_NUM_QP_REGION; i > 0; --i) {
452 for (j = 2; j < i; ++j) {
453 if (dev->caps.reserved_qps_cnt[sort[j]] >
454 dev->caps.reserved_qps_cnt[sort[j - 1]]) {
455 tmp = sort[j];
456 sort[j] = sort[j - 1];
457 sort[j - 1] = tmp;
458 }
459 }
460 }
461
462 for (i = 1; i < MLX4_NUM_QP_REGION; ++i) {
463 last_base -= dev->caps.reserved_qps_cnt[sort[i]];
464 dev->caps.reserved_qps_base[sort[i]] = last_base;
465 reserved_from_top +=
466 dev->caps.reserved_qps_cnt[sort[i]];
467 }
468
469 }
470
Jack Morgensteine2c76822012-08-03 08:40:41 +0000471 /* Reserve 8 real SQPs in both native and SRIOV modes.
472 * In addition, in SRIOV mode, reserve 8 proxy SQPs per function
473 * (for all PFs and VFs), and 8 corresponding tunnel QPs.
474 * Each proxy SQP works opposite its own tunnel QP.
475 *
476 * The QPs are arranged as follows:
477 * a. 8 real SQPs
478 * b. All the proxy SQPs (8 per function)
479 * c. All the tunnel QPs (8 per function)
480 */
481
Roland Dreier225c7b12007-05-08 18:00:38 -0700482 err = mlx4_bitmap_init(&qp_table->bitmap, dev->caps.num_qps,
Jack Morgenstein5a0d0a62013-11-03 10:03:23 +0200483 (1 << 23) - 1, mlx4_num_reserved_sqps(dev),
Yevgeny Petrilin93fc9e12008-10-22 10:25:29 -0700484 reserved_from_top);
Roland Dreier225c7b12007-05-08 18:00:38 -0700485 if (err)
486 return err;
487
Jack Morgenstein47605df2012-08-03 08:40:57 +0000488 if (mlx4_is_mfunc(dev)) {
489 /* for PPF use */
490 dev->phys_caps.base_proxy_sqpn = dev->phys_caps.base_sqpn + 8;
491 dev->phys_caps.base_tunnel_sqpn = dev->phys_caps.base_sqpn + 8 + 8 * MLX4_MFUNC_MAX;
492
493 /* In mfunc, calculate proxy and tunnel qp offsets for the PF here,
494 * since the PF does not call mlx4_slave_caps */
495 dev->caps.qp0_tunnel = kcalloc(dev->caps.num_ports, sizeof (u32), GFP_KERNEL);
496 dev->caps.qp0_proxy = kcalloc(dev->caps.num_ports, sizeof (u32), GFP_KERNEL);
497 dev->caps.qp1_tunnel = kcalloc(dev->caps.num_ports, sizeof (u32), GFP_KERNEL);
498 dev->caps.qp1_proxy = kcalloc(dev->caps.num_ports, sizeof (u32), GFP_KERNEL);
499
500 if (!dev->caps.qp0_tunnel || !dev->caps.qp0_proxy ||
501 !dev->caps.qp1_tunnel || !dev->caps.qp1_proxy) {
502 err = -ENOMEM;
503 goto err_mem;
504 }
505
506 for (k = 0; k < dev->caps.num_ports; k++) {
507 dev->caps.qp0_proxy[k] = dev->phys_caps.base_proxy_sqpn +
508 8 * mlx4_master_func_num(dev) + k;
509 dev->caps.qp0_tunnel[k] = dev->caps.qp0_proxy[k] + 8 * MLX4_MFUNC_MAX;
510 dev->caps.qp1_proxy[k] = dev->phys_caps.base_proxy_sqpn +
511 8 * mlx4_master_func_num(dev) + MLX4_MAX_PORTS + k;
512 dev->caps.qp1_tunnel[k] = dev->caps.qp1_proxy[k] + 8 * MLX4_MFUNC_MAX;
513 }
514 }
515
516
517 err = mlx4_CONF_SPECIAL_QP(dev, dev->phys_caps.base_sqpn);
518 if (err)
519 goto err_mem;
520 return 0;
521
522err_mem:
523 kfree(dev->caps.qp0_tunnel);
524 kfree(dev->caps.qp0_proxy);
525 kfree(dev->caps.qp1_tunnel);
526 kfree(dev->caps.qp1_proxy);
527 dev->caps.qp0_tunnel = dev->caps.qp0_proxy =
528 dev->caps.qp1_tunnel = dev->caps.qp1_proxy = NULL;
529 return err;
Roland Dreier225c7b12007-05-08 18:00:38 -0700530}
531
532void mlx4_cleanup_qp_table(struct mlx4_dev *dev)
533{
Jack Morgensteinfe9a2602011-12-13 04:13:22 +0000534 if (mlx4_is_slave(dev))
535 return;
536
Roland Dreier225c7b12007-05-08 18:00:38 -0700537 mlx4_CONF_SPECIAL_QP(dev, 0);
538 mlx4_bitmap_cleanup(&mlx4_priv(dev)->qp_table.bitmap);
539}
Jack Morgenstein6a775e22007-06-21 12:27:47 +0300540
541int mlx4_qp_query(struct mlx4_dev *dev, struct mlx4_qp *qp,
542 struct mlx4_qp_context *context)
543{
544 struct mlx4_cmd_mailbox *mailbox;
545 int err;
546
547 mailbox = mlx4_alloc_cmd_mailbox(dev);
548 if (IS_ERR(mailbox))
549 return PTR_ERR(mailbox);
550
551 err = mlx4_cmd_box(dev, 0, mailbox->dma, qp->qpn, 0,
Jack Morgensteinf9baff52011-12-13 04:10:51 +0000552 MLX4_CMD_QUERY_QP, MLX4_CMD_TIME_CLASS_A,
553 MLX4_CMD_WRAPPED);
Jack Morgenstein6a775e22007-06-21 12:27:47 +0300554 if (!err)
555 memcpy(context, mailbox->buf + 8, sizeof *context);
556
557 mlx4_free_cmd_mailbox(dev, mailbox);
558 return err;
559}
560EXPORT_SYMBOL_GPL(mlx4_qp_query);
561
Yevgeny Petrilined4d3c12008-04-25 14:52:32 -0700562int mlx4_qp_to_ready(struct mlx4_dev *dev, struct mlx4_mtt *mtt,
563 struct mlx4_qp_context *context,
564 struct mlx4_qp *qp, enum mlx4_qp_state *qp_state)
565{
566 int err;
567 int i;
568 enum mlx4_qp_state states[] = {
569 MLX4_QP_STATE_RST,
570 MLX4_QP_STATE_INIT,
571 MLX4_QP_STATE_RTR,
572 MLX4_QP_STATE_RTS
573 };
574
575 for (i = 0; i < ARRAY_SIZE(states) - 1; i++) {
576 context->flags &= cpu_to_be32(~(0xf << 28));
577 context->flags |= cpu_to_be32(states[i + 1] << 28);
578 err = mlx4_qp_modify(dev, mtt, states[i], states[i + 1],
579 context, 0, 0, qp);
580 if (err) {
581 mlx4_err(dev, "Failed to bring QP to state: "
582 "%d with error: %d\n",
583 states[i + 1], err);
584 return err;
585 }
586
587 *qp_state = states[i + 1];
588 }
589
590 return 0;
591}
592EXPORT_SYMBOL_GPL(mlx4_qp_to_ready);