2 * Copyright (c) 2013-2015, Mellanox Technologies. All rights reserved.
4 * This software is available to you under a choice of one of two
5 * licenses. You may choose to be licensed under the terms of the GNU
6 * General Public License (GPL) Version 2, available from the file
7 * COPYING in the main directory of this source tree, or the
8 * OpenIB.org BSD license below:
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
18 * - Redistributions in binary form must reproduce the above
19 * copyright notice, this list of conditions and the following
20 * disclaimer in the documentation and/or other materials
21 * provided with the distribution.
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
33 #include <linux/gfp.h>
34 #include <linux/export.h>
35 #include <linux/mlx5/cmd.h>
36 #include <linux/mlx5/qp.h>
37 #include <linux/mlx5/driver.h>
38 #include <linux/mlx5/transobj.h>
40 #include "mlx5_core.h"
42 static struct mlx5_core_rsc_common *mlx5_get_rsc(struct mlx5_core_dev *dev,
45 struct mlx5_qp_table *table = &dev->priv.qp_table;
46 struct mlx5_core_rsc_common *common;
48 spin_lock(&table->lock);
50 common = radix_tree_lookup(&table->tree, rsn);
52 atomic_inc(&common->refcount);
54 spin_unlock(&table->lock);
57 mlx5_core_warn(dev, "Async event for bogus resource 0x%x\n",
64 void mlx5_core_put_rsc(struct mlx5_core_rsc_common *common)
66 if (atomic_dec_and_test(&common->refcount))
67 complete(&common->free);
70 static u64 qp_allowed_event_types(void)
74 mask = BIT(MLX5_EVENT_TYPE_PATH_MIG) |
75 BIT(MLX5_EVENT_TYPE_COMM_EST) |
76 BIT(MLX5_EVENT_TYPE_SQ_DRAINED) |
77 BIT(MLX5_EVENT_TYPE_SRQ_LAST_WQE) |
78 BIT(MLX5_EVENT_TYPE_WQ_CATAS_ERROR) |
79 BIT(MLX5_EVENT_TYPE_PATH_MIG_FAILED) |
80 BIT(MLX5_EVENT_TYPE_WQ_INVAL_REQ_ERROR) |
81 BIT(MLX5_EVENT_TYPE_WQ_ACCESS_ERROR);
86 static u64 rq_allowed_event_types(void)
90 mask = BIT(MLX5_EVENT_TYPE_SRQ_LAST_WQE) |
91 BIT(MLX5_EVENT_TYPE_WQ_CATAS_ERROR);
96 static u64 sq_allowed_event_types(void)
98 return BIT(MLX5_EVENT_TYPE_WQ_CATAS_ERROR);
101 static bool is_event_type_allowed(int rsc_type, int event_type)
104 case MLX5_EVENT_QUEUE_TYPE_QP:
105 return BIT(event_type) & qp_allowed_event_types();
106 case MLX5_EVENT_QUEUE_TYPE_RQ:
107 return BIT(event_type) & rq_allowed_event_types();
108 case MLX5_EVENT_QUEUE_TYPE_SQ:
109 return BIT(event_type) & sq_allowed_event_types();
111 WARN(1, "Event arrived for unknown resource type");
116 void mlx5_rsc_event(struct mlx5_core_dev *dev, u32 rsn, int event_type)
118 struct mlx5_core_rsc_common *common = mlx5_get_rsc(dev, rsn);
119 struct mlx5_core_qp *qp;
124 if (!is_event_type_allowed((rsn >> MLX5_USER_INDEX_LEN), event_type)) {
125 mlx5_core_warn(dev, "event 0x%.2x is not allowed on resource 0x%.8x\n",
130 switch (common->res) {
134 qp = (struct mlx5_core_qp *)common;
135 qp->event(qp, event_type);
139 mlx5_core_warn(dev, "invalid resource type for 0x%x\n", rsn);
142 mlx5_core_put_rsc(common);
145 static int create_qprqsq_common(struct mlx5_core_dev *dev,
146 struct mlx5_core_qp *qp,
149 struct mlx5_qp_table *table = &dev->priv.qp_table;
152 qp->common.res = rsc_type;
153 spin_lock_irq(&table->lock);
154 err = radix_tree_insert(&table->tree,
155 qp->qpn | (rsc_type << MLX5_USER_INDEX_LEN),
157 spin_unlock_irq(&table->lock);
161 atomic_set(&qp->common.refcount, 1);
162 init_completion(&qp->common.free);
163 qp->pid = current->pid;
168 static void destroy_qprqsq_common(struct mlx5_core_dev *dev,
169 struct mlx5_core_qp *qp)
171 struct mlx5_qp_table *table = &dev->priv.qp_table;
174 spin_lock_irqsave(&table->lock, flags);
175 radix_tree_delete(&table->tree,
176 qp->qpn | (qp->common.res << MLX5_USER_INDEX_LEN));
177 spin_unlock_irqrestore(&table->lock, flags);
178 mlx5_core_put_rsc((struct mlx5_core_rsc_common *)qp);
179 wait_for_completion(&qp->common.free);
182 int mlx5_core_create_qp(struct mlx5_core_dev *dev,
183 struct mlx5_core_qp *qp,
186 u32 out[MLX5_ST_SZ_DW(create_qp_out)] = {0};
187 u32 dout[MLX5_ST_SZ_DW(destroy_qp_out)];
188 u32 din[MLX5_ST_SZ_DW(destroy_qp_in)];
191 MLX5_SET(create_qp_in, in, opcode, MLX5_CMD_OP_CREATE_QP);
193 err = mlx5_cmd_exec(dev, in, inlen, out, sizeof(out));
197 qp->qpn = MLX5_GET(create_qp_out, out, qpn);
198 mlx5_core_dbg(dev, "qpn = 0x%x\n", qp->qpn);
200 err = create_qprqsq_common(dev, qp, MLX5_RES_QP);
204 err = mlx5_debug_qp_add(dev, qp);
206 mlx5_core_dbg(dev, "failed adding QP 0x%x to debug file system\n",
209 atomic_inc(&dev->num_qps);
214 memset(din, 0, sizeof(din));
215 memset(dout, 0, sizeof(dout));
216 MLX5_SET(destroy_qp_in, din, opcode, MLX5_CMD_OP_DESTROY_QP);
217 MLX5_SET(destroy_qp_in, din, qpn, qp->qpn);
218 mlx5_cmd_exec(dev, din, sizeof(din), dout, sizeof(dout));
221 EXPORT_SYMBOL_GPL(mlx5_core_create_qp);
223 int mlx5_core_destroy_qp(struct mlx5_core_dev *dev,
224 struct mlx5_core_qp *qp)
226 u32 out[MLX5_ST_SZ_DW(destroy_qp_out)] = {0};
227 u32 in[MLX5_ST_SZ_DW(destroy_qp_in)] = {0};
230 mlx5_debug_qp_remove(dev, qp);
232 destroy_qprqsq_common(dev, qp);
234 MLX5_SET(destroy_qp_in, in, opcode, MLX5_CMD_OP_DESTROY_QP);
235 MLX5_SET(destroy_qp_in, in, qpn, qp->qpn);
236 err = mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out));
240 atomic_dec(&dev->num_qps);
243 EXPORT_SYMBOL_GPL(mlx5_core_destroy_qp);
245 int mlx5_core_set_delay_drop(struct mlx5_core_dev *dev,
248 u32 out[MLX5_ST_SZ_DW(set_delay_drop_params_out)] = {0};
249 u32 in[MLX5_ST_SZ_DW(set_delay_drop_params_in)] = {0};
251 MLX5_SET(set_delay_drop_params_in, in, opcode,
252 MLX5_CMD_OP_SET_DELAY_DROP_PARAMS);
253 MLX5_SET(set_delay_drop_params_in, in, delay_drop_timeout,
255 return mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out));
257 EXPORT_SYMBOL_GPL(mlx5_core_set_delay_drop);
266 static int mbox_alloc(struct mbox_info *mbox, int inlen, int outlen)
269 mbox->outlen = outlen;
270 mbox->in = kzalloc(mbox->inlen, GFP_KERNEL);
271 mbox->out = kzalloc(mbox->outlen, GFP_KERNEL);
272 if (!mbox->in || !mbox->out) {
281 static void mbox_free(struct mbox_info *mbox)
287 static int modify_qp_mbox_alloc(struct mlx5_core_dev *dev, u16 opcode, int qpn,
288 u32 opt_param_mask, void *qpc,
289 struct mbox_info *mbox)
294 #define MBOX_ALLOC(mbox, typ) \
295 mbox_alloc(mbox, MLX5_ST_SZ_BYTES(typ##_in), MLX5_ST_SZ_BYTES(typ##_out))
297 #define MOD_QP_IN_SET(typ, in, _opcode, _qpn) \
298 MLX5_SET(typ##_in, in, opcode, _opcode); \
299 MLX5_SET(typ##_in, in, qpn, _qpn)
301 #define MOD_QP_IN_SET_QPC(typ, in, _opcode, _qpn, _opt_p, _qpc) \
302 MOD_QP_IN_SET(typ, in, _opcode, _qpn); \
303 MLX5_SET(typ##_in, in, opt_param_mask, _opt_p); \
304 memcpy(MLX5_ADDR_OF(typ##_in, in, qpc), _qpc, MLX5_ST_SZ_BYTES(qpc))
308 case MLX5_CMD_OP_2RST_QP:
309 if (MBOX_ALLOC(mbox, qp_2rst))
311 MOD_QP_IN_SET(qp_2rst, mbox->in, opcode, qpn);
313 case MLX5_CMD_OP_2ERR_QP:
314 if (MBOX_ALLOC(mbox, qp_2err))
316 MOD_QP_IN_SET(qp_2err, mbox->in, opcode, qpn);
319 /* MODIFY with QPC */
320 case MLX5_CMD_OP_RST2INIT_QP:
321 if (MBOX_ALLOC(mbox, rst2init_qp))
323 MOD_QP_IN_SET_QPC(rst2init_qp, mbox->in, opcode, qpn,
324 opt_param_mask, qpc);
326 case MLX5_CMD_OP_INIT2RTR_QP:
327 if (MBOX_ALLOC(mbox, init2rtr_qp))
329 MOD_QP_IN_SET_QPC(init2rtr_qp, mbox->in, opcode, qpn,
330 opt_param_mask, qpc);
332 case MLX5_CMD_OP_RTR2RTS_QP:
333 if (MBOX_ALLOC(mbox, rtr2rts_qp))
335 MOD_QP_IN_SET_QPC(rtr2rts_qp, mbox->in, opcode, qpn,
336 opt_param_mask, qpc);
338 case MLX5_CMD_OP_RTS2RTS_QP:
339 if (MBOX_ALLOC(mbox, rts2rts_qp))
341 MOD_QP_IN_SET_QPC(rts2rts_qp, mbox->in, opcode, qpn,
342 opt_param_mask, qpc);
344 case MLX5_CMD_OP_SQERR2RTS_QP:
345 if (MBOX_ALLOC(mbox, sqerr2rts_qp))
347 MOD_QP_IN_SET_QPC(sqerr2rts_qp, mbox->in, opcode, qpn,
348 opt_param_mask, qpc);
350 case MLX5_CMD_OP_INIT2INIT_QP:
351 if (MBOX_ALLOC(mbox, init2init_qp))
353 MOD_QP_IN_SET_QPC(init2init_qp, mbox->in, opcode, qpn,
354 opt_param_mask, qpc);
357 mlx5_core_err(dev, "Unknown transition for modify QP: OP(0x%x) QPN(0x%x)\n",
364 int mlx5_core_qp_modify(struct mlx5_core_dev *dev, u16 opcode,
365 u32 opt_param_mask, void *qpc,
366 struct mlx5_core_qp *qp)
368 struct mbox_info mbox;
371 err = modify_qp_mbox_alloc(dev, opcode, qp->qpn,
372 opt_param_mask, qpc, &mbox);
376 err = mlx5_cmd_exec(dev, mbox.in, mbox.inlen, mbox.out, mbox.outlen);
380 EXPORT_SYMBOL_GPL(mlx5_core_qp_modify);
382 void mlx5_init_qp_table(struct mlx5_core_dev *dev)
384 struct mlx5_qp_table *table = &dev->priv.qp_table;
386 memset(table, 0, sizeof(*table));
387 spin_lock_init(&table->lock);
388 INIT_RADIX_TREE(&table->tree, GFP_ATOMIC);
389 mlx5_qp_debugfs_init(dev);
392 void mlx5_cleanup_qp_table(struct mlx5_core_dev *dev)
394 mlx5_qp_debugfs_cleanup(dev);
397 int mlx5_core_qp_query(struct mlx5_core_dev *dev, struct mlx5_core_qp *qp,
398 u32 *out, int outlen)
400 u32 in[MLX5_ST_SZ_DW(query_qp_in)] = {0};
402 MLX5_SET(query_qp_in, in, opcode, MLX5_CMD_OP_QUERY_QP);
403 MLX5_SET(query_qp_in, in, qpn, qp->qpn);
404 return mlx5_cmd_exec(dev, in, sizeof(in), out, outlen);
406 EXPORT_SYMBOL_GPL(mlx5_core_qp_query);
408 int mlx5_core_xrcd_alloc(struct mlx5_core_dev *dev, u32 *xrcdn)
410 u32 out[MLX5_ST_SZ_DW(alloc_xrcd_out)] = {0};
411 u32 in[MLX5_ST_SZ_DW(alloc_xrcd_in)] = {0};
414 MLX5_SET(alloc_xrcd_in, in, opcode, MLX5_CMD_OP_ALLOC_XRCD);
415 err = mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out));
417 *xrcdn = MLX5_GET(alloc_xrcd_out, out, xrcd);
420 EXPORT_SYMBOL_GPL(mlx5_core_xrcd_alloc);
422 int mlx5_core_xrcd_dealloc(struct mlx5_core_dev *dev, u32 xrcdn)
424 u32 out[MLX5_ST_SZ_DW(dealloc_xrcd_out)] = {0};
425 u32 in[MLX5_ST_SZ_DW(dealloc_xrcd_in)] = {0};
427 MLX5_SET(dealloc_xrcd_in, in, opcode, MLX5_CMD_OP_DEALLOC_XRCD);
428 MLX5_SET(dealloc_xrcd_in, in, xrcd, xrcdn);
429 return mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out));
431 EXPORT_SYMBOL_GPL(mlx5_core_xrcd_dealloc);
433 int mlx5_core_create_rq_tracked(struct mlx5_core_dev *dev, u32 *in, int inlen,
434 struct mlx5_core_qp *rq)
439 err = mlx5_core_create_rq(dev, in, inlen, &rqn);
444 err = create_qprqsq_common(dev, rq, MLX5_RES_RQ);
451 mlx5_core_destroy_rq(dev, rq->qpn);
455 EXPORT_SYMBOL(mlx5_core_create_rq_tracked);
457 void mlx5_core_destroy_rq_tracked(struct mlx5_core_dev *dev,
458 struct mlx5_core_qp *rq)
460 destroy_qprqsq_common(dev, rq);
461 mlx5_core_destroy_rq(dev, rq->qpn);
463 EXPORT_SYMBOL(mlx5_core_destroy_rq_tracked);
465 int mlx5_core_create_sq_tracked(struct mlx5_core_dev *dev, u32 *in, int inlen,
466 struct mlx5_core_qp *sq)
471 err = mlx5_core_create_sq(dev, in, inlen, &sqn);
476 err = create_qprqsq_common(dev, sq, MLX5_RES_SQ);
483 mlx5_core_destroy_sq(dev, sq->qpn);
487 EXPORT_SYMBOL(mlx5_core_create_sq_tracked);
489 void mlx5_core_destroy_sq_tracked(struct mlx5_core_dev *dev,
490 struct mlx5_core_qp *sq)
492 destroy_qprqsq_common(dev, sq);
493 mlx5_core_destroy_sq(dev, sq->qpn);
495 EXPORT_SYMBOL(mlx5_core_destroy_sq_tracked);
497 int mlx5_core_alloc_q_counter(struct mlx5_core_dev *dev, u16 *counter_id)
499 u32 in[MLX5_ST_SZ_DW(alloc_q_counter_in)] = {0};
500 u32 out[MLX5_ST_SZ_DW(alloc_q_counter_out)] = {0};
503 MLX5_SET(alloc_q_counter_in, in, opcode, MLX5_CMD_OP_ALLOC_Q_COUNTER);
504 err = mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out));
506 *counter_id = MLX5_GET(alloc_q_counter_out, out,
510 EXPORT_SYMBOL_GPL(mlx5_core_alloc_q_counter);
512 int mlx5_core_dealloc_q_counter(struct mlx5_core_dev *dev, u16 counter_id)
514 u32 in[MLX5_ST_SZ_DW(dealloc_q_counter_in)] = {0};
515 u32 out[MLX5_ST_SZ_DW(dealloc_q_counter_out)] = {0};
517 MLX5_SET(dealloc_q_counter_in, in, opcode,
518 MLX5_CMD_OP_DEALLOC_Q_COUNTER);
519 MLX5_SET(dealloc_q_counter_in, in, counter_set_id, counter_id);
520 return mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out));
522 EXPORT_SYMBOL_GPL(mlx5_core_dealloc_q_counter);
524 int mlx5_core_query_q_counter(struct mlx5_core_dev *dev, u16 counter_id,
525 int reset, void *out, int out_size)
527 u32 in[MLX5_ST_SZ_DW(query_q_counter_in)] = {0};
529 MLX5_SET(query_q_counter_in, in, opcode, MLX5_CMD_OP_QUERY_Q_COUNTER);
530 MLX5_SET(query_q_counter_in, in, clear, reset);
531 MLX5_SET(query_q_counter_in, in, counter_set_id, counter_id);
532 return mlx5_cmd_exec(dev, in, sizeof(in), out, out_size);
534 EXPORT_SYMBOL_GPL(mlx5_core_query_q_counter);