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
34 #include <linux/gfp.h>
35 #include <linux/export.h>
36 #include <linux/mlx5/cmd.h>
37 #include <linux/mlx5/qp.h>
38 #include <linux/mlx5/driver.h>
39 #include <linux/mlx5/transobj.h>
41 #include "mlx5_core.h"
43 static struct mlx5_core_rsc_common *mlx5_get_rsc(struct mlx5_core_dev *dev,
46 struct mlx5_qp_table *table = &dev->priv.qp_table;
47 struct mlx5_core_rsc_common *common;
49 spin_lock(&table->lock);
51 common = radix_tree_lookup(&table->tree, rsn);
53 atomic_inc(&common->refcount);
55 spin_unlock(&table->lock);
58 mlx5_core_warn(dev, "Async event for bogus resource 0x%x\n",
65 void mlx5_core_put_rsc(struct mlx5_core_rsc_common *common)
67 if (atomic_dec_and_test(&common->refcount))
68 complete(&common->free);
71 static u64 qp_allowed_event_types(void)
75 mask = BIT(MLX5_EVENT_TYPE_PATH_MIG) |
76 BIT(MLX5_EVENT_TYPE_COMM_EST) |
77 BIT(MLX5_EVENT_TYPE_SQ_DRAINED) |
78 BIT(MLX5_EVENT_TYPE_SRQ_LAST_WQE) |
79 BIT(MLX5_EVENT_TYPE_WQ_CATAS_ERROR) |
80 BIT(MLX5_EVENT_TYPE_PATH_MIG_FAILED) |
81 BIT(MLX5_EVENT_TYPE_WQ_INVAL_REQ_ERROR) |
82 BIT(MLX5_EVENT_TYPE_WQ_ACCESS_ERROR);
87 static u64 rq_allowed_event_types(void)
91 mask = BIT(MLX5_EVENT_TYPE_SRQ_LAST_WQE) |
92 BIT(MLX5_EVENT_TYPE_WQ_CATAS_ERROR);
97 static u64 sq_allowed_event_types(void)
99 return BIT(MLX5_EVENT_TYPE_WQ_CATAS_ERROR);
102 static bool is_event_type_allowed(int rsc_type, int event_type)
105 case MLX5_EVENT_QUEUE_TYPE_QP:
106 return BIT(event_type) & qp_allowed_event_types();
107 case MLX5_EVENT_QUEUE_TYPE_RQ:
108 return BIT(event_type) & rq_allowed_event_types();
109 case MLX5_EVENT_QUEUE_TYPE_SQ:
110 return BIT(event_type) & sq_allowed_event_types();
112 WARN(1, "Event arrived for unknown resource type");
117 void mlx5_rsc_event(struct mlx5_core_dev *dev, u32 rsn, int event_type)
119 struct mlx5_core_rsc_common *common = mlx5_get_rsc(dev, rsn);
120 struct mlx5_core_qp *qp;
125 if (!is_event_type_allowed((rsn >> MLX5_USER_INDEX_LEN), event_type)) {
126 mlx5_core_warn(dev, "event 0x%.2x is not allowed on resource 0x%.8x\n",
131 switch (common->res) {
135 qp = (struct mlx5_core_qp *)common;
136 qp->event(qp, event_type);
140 mlx5_core_warn(dev, "invalid resource type for 0x%x\n", rsn);
143 mlx5_core_put_rsc(common);
146 static int create_qprqsq_common(struct mlx5_core_dev *dev,
147 struct mlx5_core_qp *qp,
150 struct mlx5_qp_table *table = &dev->priv.qp_table;
153 qp->common.res = rsc_type;
154 spin_lock_irq(&table->lock);
155 err = radix_tree_insert(&table->tree,
156 qp->qpn | (rsc_type << MLX5_USER_INDEX_LEN),
158 spin_unlock_irq(&table->lock);
162 atomic_set(&qp->common.refcount, 1);
163 init_completion(&qp->common.free);
164 qp->pid = current->pid;
169 static void destroy_qprqsq_common(struct mlx5_core_dev *dev,
170 struct mlx5_core_qp *qp)
172 struct mlx5_qp_table *table = &dev->priv.qp_table;
175 spin_lock_irqsave(&table->lock, flags);
176 radix_tree_delete(&table->tree,
177 qp->qpn | (qp->common.res << MLX5_USER_INDEX_LEN));
178 spin_unlock_irqrestore(&table->lock, flags);
179 mlx5_core_put_rsc((struct mlx5_core_rsc_common *)qp);
180 wait_for_completion(&qp->common.free);
183 int mlx5_core_create_qp(struct mlx5_core_dev *dev,
184 struct mlx5_core_qp *qp,
187 u32 out[MLX5_ST_SZ_DW(create_qp_out)] = {0};
188 u32 dout[MLX5_ST_SZ_DW(destroy_qp_out)];
189 u32 din[MLX5_ST_SZ_DW(destroy_qp_in)];
192 MLX5_SET(create_qp_in, in, opcode, MLX5_CMD_OP_CREATE_QP);
194 err = mlx5_cmd_exec(dev, in, inlen, out, sizeof(out));
198 qp->qpn = MLX5_GET(create_qp_out, out, qpn);
199 mlx5_core_dbg(dev, "qpn = 0x%x\n", qp->qpn);
201 err = create_qprqsq_common(dev, qp, MLX5_RES_QP);
205 err = mlx5_debug_qp_add(dev, qp);
207 mlx5_core_dbg(dev, "failed adding QP 0x%x to debug file system\n",
210 atomic_inc(&dev->num_qps);
215 memset(din, 0, sizeof(din));
216 memset(dout, 0, sizeof(dout));
217 MLX5_SET(destroy_qp_in, in, opcode, MLX5_CMD_OP_DESTROY_QP);
218 MLX5_SET(destroy_qp_in, in, qpn, qp->qpn);
219 mlx5_cmd_exec(dev, din, sizeof(din), dout, sizeof(dout));
222 EXPORT_SYMBOL_GPL(mlx5_core_create_qp);
224 int mlx5_core_destroy_qp(struct mlx5_core_dev *dev,
225 struct mlx5_core_qp *qp)
227 u32 out[MLX5_ST_SZ_DW(destroy_qp_out)] = {0};
228 u32 in[MLX5_ST_SZ_DW(destroy_qp_in)] = {0};
231 mlx5_debug_qp_remove(dev, qp);
233 destroy_qprqsq_common(dev, qp);
235 MLX5_SET(destroy_qp_in, in, opcode, MLX5_CMD_OP_DESTROY_QP);
236 MLX5_SET(destroy_qp_in, in, qpn, qp->qpn);
237 err = mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out));
241 atomic_dec(&dev->num_qps);
244 EXPORT_SYMBOL_GPL(mlx5_core_destroy_qp);
253 static int mbox_alloc(struct mbox_info *mbox, int inlen, int outlen)
256 mbox->outlen = outlen;
257 mbox->in = kzalloc(mbox->inlen, GFP_KERNEL);
258 mbox->out = kzalloc(mbox->outlen, GFP_KERNEL);
259 if (!mbox->in || !mbox->out) {
268 static void mbox_free(struct mbox_info *mbox)
274 static int modify_qp_mbox_alloc(struct mlx5_core_dev *dev, u16 opcode, int qpn,
275 u32 opt_param_mask, void *qpc,
276 struct mbox_info *mbox)
281 #define MBOX_ALLOC(mbox, typ) \
282 mbox_alloc(mbox, MLX5_ST_SZ_BYTES(typ##_in), MLX5_ST_SZ_BYTES(typ##_out))
284 #define MOD_QP_IN_SET(typ, in, _opcode, _qpn) \
285 MLX5_SET(typ##_in, in, opcode, _opcode); \
286 MLX5_SET(typ##_in, in, qpn, _qpn)
288 #define MOD_QP_IN_SET_QPC(typ, in, _opcode, _qpn, _opt_p, _qpc) \
289 MOD_QP_IN_SET(typ, in, _opcode, _qpn); \
290 MLX5_SET(typ##_in, in, opt_param_mask, _opt_p); \
291 memcpy(MLX5_ADDR_OF(typ##_in, in, qpc), _qpc, MLX5_ST_SZ_BYTES(qpc))
295 case MLX5_CMD_OP_2RST_QP:
296 if (MBOX_ALLOC(mbox, qp_2rst))
298 MOD_QP_IN_SET(qp_2rst, mbox->in, opcode, qpn);
300 case MLX5_CMD_OP_2ERR_QP:
301 if (MBOX_ALLOC(mbox, qp_2err))
303 MOD_QP_IN_SET(qp_2err, mbox->in, opcode, qpn);
306 /* MODIFY with QPC */
307 case MLX5_CMD_OP_RST2INIT_QP:
308 if (MBOX_ALLOC(mbox, rst2init_qp))
310 MOD_QP_IN_SET_QPC(rst2init_qp, mbox->in, opcode, qpn,
311 opt_param_mask, qpc);
313 case MLX5_CMD_OP_INIT2RTR_QP:
314 if (MBOX_ALLOC(mbox, init2rtr_qp))
316 MOD_QP_IN_SET_QPC(init2rtr_qp, mbox->in, opcode, qpn,
317 opt_param_mask, qpc);
319 case MLX5_CMD_OP_RTR2RTS_QP:
320 if (MBOX_ALLOC(mbox, rtr2rts_qp))
322 MOD_QP_IN_SET_QPC(rtr2rts_qp, mbox->in, opcode, qpn,
323 opt_param_mask, qpc);
325 case MLX5_CMD_OP_RTS2RTS_QP:
326 if (MBOX_ALLOC(mbox, rts2rts_qp))
328 MOD_QP_IN_SET_QPC(rts2rts_qp, mbox->in, opcode, qpn,
329 opt_param_mask, qpc);
331 case MLX5_CMD_OP_SQERR2RTS_QP:
332 if (MBOX_ALLOC(mbox, sqerr2rts_qp))
334 MOD_QP_IN_SET_QPC(sqerr2rts_qp, mbox->in, opcode, qpn,
335 opt_param_mask, qpc);
337 case MLX5_CMD_OP_INIT2INIT_QP:
338 if (MBOX_ALLOC(mbox, init2init_qp))
340 MOD_QP_IN_SET_QPC(init2init_qp, mbox->in, opcode, qpn,
341 opt_param_mask, qpc);
344 mlx5_core_err(dev, "Unknown transition for modify QP: OP(0x%x) QPN(0x%x)\n",
351 int mlx5_core_qp_modify(struct mlx5_core_dev *dev, u16 opcode,
352 u32 opt_param_mask, void *qpc,
353 struct mlx5_core_qp *qp)
355 struct mbox_info mbox;
358 err = modify_qp_mbox_alloc(dev, opcode, qp->qpn,
359 opt_param_mask, qpc, &mbox);
363 err = mlx5_cmd_exec(dev, mbox.in, mbox.inlen, mbox.out, mbox.outlen);
367 EXPORT_SYMBOL_GPL(mlx5_core_qp_modify);
369 void mlx5_init_qp_table(struct mlx5_core_dev *dev)
371 struct mlx5_qp_table *table = &dev->priv.qp_table;
373 memset(table, 0, sizeof(*table));
374 spin_lock_init(&table->lock);
375 INIT_RADIX_TREE(&table->tree, GFP_ATOMIC);
376 mlx5_qp_debugfs_init(dev);
379 void mlx5_cleanup_qp_table(struct mlx5_core_dev *dev)
381 mlx5_qp_debugfs_cleanup(dev);
384 int mlx5_core_qp_query(struct mlx5_core_dev *dev, struct mlx5_core_qp *qp,
385 u32 *out, int outlen)
387 u32 in[MLX5_ST_SZ_DW(query_qp_in)] = {0};
389 MLX5_SET(query_qp_in, in, opcode, MLX5_CMD_OP_QUERY_QP);
390 MLX5_SET(query_qp_in, in, qpn, qp->qpn);
391 return mlx5_cmd_exec(dev, in, sizeof(in), out, outlen);
393 EXPORT_SYMBOL_GPL(mlx5_core_qp_query);
395 int mlx5_core_xrcd_alloc(struct mlx5_core_dev *dev, u32 *xrcdn)
397 u32 out[MLX5_ST_SZ_DW(alloc_xrcd_out)] = {0};
398 u32 in[MLX5_ST_SZ_DW(alloc_xrcd_in)] = {0};
401 MLX5_SET(alloc_xrcd_in, in, opcode, MLX5_CMD_OP_ALLOC_XRCD);
402 err = mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out));
404 *xrcdn = MLX5_GET(alloc_xrcd_out, out, xrcd);
407 EXPORT_SYMBOL_GPL(mlx5_core_xrcd_alloc);
409 int mlx5_core_xrcd_dealloc(struct mlx5_core_dev *dev, u32 xrcdn)
411 u32 out[MLX5_ST_SZ_DW(dealloc_xrcd_out)] = {0};
412 u32 in[MLX5_ST_SZ_DW(dealloc_xrcd_in)] = {0};
414 MLX5_SET(dealloc_xrcd_in, in, opcode, MLX5_CMD_OP_DEALLOC_XRCD);
415 MLX5_SET(dealloc_xrcd_in, in, xrcd, xrcdn);
416 return mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out));
418 EXPORT_SYMBOL_GPL(mlx5_core_xrcd_dealloc);
420 int mlx5_core_create_rq_tracked(struct mlx5_core_dev *dev, u32 *in, int inlen,
421 struct mlx5_core_qp *rq)
426 err = mlx5_core_create_rq(dev, in, inlen, &rqn);
431 err = create_qprqsq_common(dev, rq, MLX5_RES_RQ);
438 mlx5_core_destroy_rq(dev, rq->qpn);
442 EXPORT_SYMBOL(mlx5_core_create_rq_tracked);
444 void mlx5_core_destroy_rq_tracked(struct mlx5_core_dev *dev,
445 struct mlx5_core_qp *rq)
447 destroy_qprqsq_common(dev, rq);
448 mlx5_core_destroy_rq(dev, rq->qpn);
450 EXPORT_SYMBOL(mlx5_core_destroy_rq_tracked);
452 int mlx5_core_create_sq_tracked(struct mlx5_core_dev *dev, u32 *in, int inlen,
453 struct mlx5_core_qp *sq)
458 err = mlx5_core_create_sq(dev, in, inlen, &sqn);
463 err = create_qprqsq_common(dev, sq, MLX5_RES_SQ);
470 mlx5_core_destroy_sq(dev, sq->qpn);
474 EXPORT_SYMBOL(mlx5_core_create_sq_tracked);
476 void mlx5_core_destroy_sq_tracked(struct mlx5_core_dev *dev,
477 struct mlx5_core_qp *sq)
479 destroy_qprqsq_common(dev, sq);
480 mlx5_core_destroy_sq(dev, sq->qpn);
482 EXPORT_SYMBOL(mlx5_core_destroy_sq_tracked);
484 int mlx5_core_alloc_q_counter(struct mlx5_core_dev *dev, u16 *counter_id)
486 u32 in[MLX5_ST_SZ_DW(alloc_q_counter_in)] = {0};
487 u32 out[MLX5_ST_SZ_DW(alloc_q_counter_out)] = {0};
490 MLX5_SET(alloc_q_counter_in, in, opcode, MLX5_CMD_OP_ALLOC_Q_COUNTER);
491 err = mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out));
493 *counter_id = MLX5_GET(alloc_q_counter_out, out,
497 EXPORT_SYMBOL_GPL(mlx5_core_alloc_q_counter);
499 int mlx5_core_dealloc_q_counter(struct mlx5_core_dev *dev, u16 counter_id)
501 u32 in[MLX5_ST_SZ_DW(dealloc_q_counter_in)] = {0};
502 u32 out[MLX5_ST_SZ_DW(dealloc_q_counter_out)] = {0};
504 MLX5_SET(dealloc_q_counter_in, in, opcode,
505 MLX5_CMD_OP_DEALLOC_Q_COUNTER);
506 MLX5_SET(dealloc_q_counter_in, in, counter_set_id, counter_id);
507 return mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out));
509 EXPORT_SYMBOL_GPL(mlx5_core_dealloc_q_counter);
511 int mlx5_core_query_q_counter(struct mlx5_core_dev *dev, u16 counter_id,
512 int reset, void *out, int out_size)
514 u32 in[MLX5_ST_SZ_DW(query_q_counter_in)] = {0};
516 MLX5_SET(query_q_counter_in, in, opcode, MLX5_CMD_OP_QUERY_Q_COUNTER);
517 MLX5_SET(query_q_counter_in, in, clear, reset);
518 MLX5_SET(query_q_counter_in, in, counter_set_id, counter_id);
519 return mlx5_cmd_exec(dev, in, sizeof(in), out, out_size);
521 EXPORT_SYMBOL_GPL(mlx5_core_query_q_counter);
523 int mlx5_core_query_out_of_buffer(struct mlx5_core_dev *dev, u16 counter_id,
526 int outlen = MLX5_ST_SZ_BYTES(query_q_counter_out);
530 out = mlx5_vzalloc(outlen);
534 err = mlx5_core_query_q_counter(dev, counter_id, 0, out, outlen);
536 *out_of_buffer = MLX5_GET(query_q_counter_out, out,