xref: /linux/drivers/net/ethernet/mellanox/mlx5/core/cq.c (revision d0309c054362a235077327b46f727bc48878a3bc)
1 /*
2  * Copyright (c) 2013-2015, Mellanox Technologies. All rights reserved.
3  *
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:
9  *
10  *     Redistribution and use in source and binary forms, with or
11  *     without modification, are permitted provided that the following
12  *     conditions are met:
13  *
14  *      - Redistributions of source code must retain the above
15  *        copyright notice, this list of conditions and the following
16  *        disclaimer.
17  *
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.
22  *
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
30  * SOFTWARE.
31  */
32 
33 #include <linux/kernel.h>
34 #include <linux/hardirq.h>
35 #include <linux/mlx5/driver.h>
36 #include <rdma/ib_verbs.h>
37 #include <linux/mlx5/cq.h>
38 #include "mlx5_core.h"
39 #include "lib/eq.h"
40 
41 #define TASKLET_MAX_TIME 2
42 #define TASKLET_MAX_TIME_JIFFIES msecs_to_jiffies(TASKLET_MAX_TIME)
43 
mlx5_cq_tasklet_cb(struct tasklet_struct * t)44 void mlx5_cq_tasklet_cb(struct tasklet_struct *t)
45 {
46 	unsigned long flags;
47 	unsigned long end = jiffies + TASKLET_MAX_TIME_JIFFIES;
48 	struct mlx5_eq_tasklet *ctx = from_tasklet(ctx, t, task);
49 	struct mlx5_core_cq *mcq;
50 	struct mlx5_core_cq *temp;
51 
52 	spin_lock_irqsave(&ctx->lock, flags);
53 	list_splice_tail_init(&ctx->list, &ctx->process_list);
54 	spin_unlock_irqrestore(&ctx->lock, flags);
55 
56 	list_for_each_entry_safe(mcq, temp, &ctx->process_list,
57 				 tasklet_ctx.list) {
58 		list_del_init(&mcq->tasklet_ctx.list);
59 		mcq->tasklet_ctx.comp(mcq, NULL);
60 		mlx5_cq_put(mcq);
61 		if (time_after(jiffies, end))
62 			break;
63 	}
64 
65 	if (!list_empty(&ctx->process_list))
66 		tasklet_schedule(&ctx->task);
67 }
68 
mlx5_add_cq_to_tasklet(struct mlx5_core_cq * cq,struct mlx5_eqe * eqe)69 void mlx5_add_cq_to_tasklet(struct mlx5_core_cq *cq,
70 			    struct mlx5_eqe *eqe)
71 {
72 	unsigned long flags;
73 	struct mlx5_eq_tasklet *tasklet_ctx = cq->tasklet_ctx.priv;
74 	bool schedule_tasklet = false;
75 
76 	spin_lock_irqsave(&tasklet_ctx->lock, flags);
77 	/* When migrating CQs between EQs will be implemented, please note
78 	 * that you need to sync this point. It is possible that
79 	 * while migrating a CQ, completions on the old EQs could
80 	 * still arrive.
81 	 */
82 	if (list_empty_careful(&cq->tasklet_ctx.list)) {
83 		mlx5_cq_hold(cq);
84 		/* If the tasklet CQ work list isn't empty, mlx5_cq_tasklet_cb()
85 		 * is scheduled/running and hasn't processed the list yet, so it
86 		 * will see this added CQ when it runs. If the list is empty,
87 		 * the tasklet needs to be scheduled to pick up the CQ. The
88 		 * spinlock avoids any race with the tasklet accessing the list.
89 		 */
90 		schedule_tasklet = list_empty(&tasklet_ctx->list);
91 		list_add_tail(&cq->tasklet_ctx.list, &tasklet_ctx->list);
92 	}
93 	spin_unlock_irqrestore(&tasklet_ctx->lock, flags);
94 
95 	if (schedule_tasklet)
96 		tasklet_schedule(&tasklet_ctx->task);
97 }
98 EXPORT_SYMBOL(mlx5_add_cq_to_tasklet);
99 
mlx5_core_cq_dummy_cb(struct mlx5_core_cq * cq,struct mlx5_eqe * eqe)100 static void mlx5_core_cq_dummy_cb(struct mlx5_core_cq *cq, struct mlx5_eqe *eqe)
101 {
102 	mlx5_core_err(cq->eq->core.dev,
103 		      "CQ default completion callback, CQ #%u\n", cq->cqn);
104 }
105 
106 #define MLX5_CQ_INIT_CMD_SN cpu_to_be32(2 << 28)
107 /* Callers must verify outbox status in case of err */
mlx5_create_cq(struct mlx5_core_dev * dev,struct mlx5_core_cq * cq,u32 * in,int inlen,u32 * out,int outlen)108 int mlx5_create_cq(struct mlx5_core_dev *dev, struct mlx5_core_cq *cq,
109 		   u32 *in, int inlen, u32 *out, int outlen)
110 {
111 	int eqn = MLX5_GET(cqc, MLX5_ADDR_OF(create_cq_in, in, cq_context),
112 			   c_eqn_or_apu_element);
113 	u32 din[MLX5_ST_SZ_DW(destroy_cq_in)] = {};
114 	struct mlx5_eq_comp *eq;
115 	int err;
116 
117 	eq = mlx5_eqn2comp_eq(dev, eqn);
118 	if (IS_ERR(eq))
119 		return PTR_ERR(eq);
120 
121 	memset(out, 0, outlen);
122 	MLX5_SET(create_cq_in, in, opcode, MLX5_CMD_OP_CREATE_CQ);
123 	err = mlx5_cmd_do(dev, in, inlen, out, outlen);
124 	if (err)
125 		return err;
126 
127 	cq->cqn = MLX5_GET(create_cq_out, out, cqn);
128 	cq->cons_index = 0;
129 	cq->arm_sn     = 0;
130 	cq->eq         = eq;
131 	cq->uid = MLX5_GET(create_cq_in, in, uid);
132 
133 	/* Kernel CQs must set the arm_db address prior to calling
134 	 * this function, allowing for the proper value to be
135 	 * initialized. User CQs are responsible for their own
136 	 * initialization since they do not use the arm_db field.
137 	 */
138 	if (cq->arm_db)
139 		*cq->arm_db = MLX5_CQ_INIT_CMD_SN;
140 
141 	refcount_set(&cq->refcount, 1);
142 	init_completion(&cq->free);
143 	if (!cq->comp)
144 		cq->comp = mlx5_core_cq_dummy_cb;
145 	/* assuming CQ will be deleted before the EQ */
146 	cq->tasklet_ctx.priv = &eq->tasklet_ctx;
147 	INIT_LIST_HEAD(&cq->tasklet_ctx.list);
148 
149 	/* Add to comp EQ CQ tree to recv comp events */
150 	err = mlx5_eq_add_cq(&eq->core, cq);
151 	if (err)
152 		goto err_cmd;
153 
154 	/* Add to async EQ CQ tree to recv async events */
155 	err = mlx5_eq_add_cq(mlx5_get_async_eq(dev), cq);
156 	if (err)
157 		goto err_cq_add;
158 
159 	cq->pid = current->pid;
160 	err = mlx5_debug_cq_add(dev, cq);
161 	if (err)
162 		mlx5_core_dbg(dev, "failed adding CP 0x%x to debug file system\n",
163 			      cq->cqn);
164 
165 	cq->irqn = eq->core.irqn;
166 
167 	return 0;
168 
169 err_cq_add:
170 	mlx5_eq_del_cq(&eq->core, cq);
171 err_cmd:
172 	MLX5_SET(destroy_cq_in, din, opcode, MLX5_CMD_OP_DESTROY_CQ);
173 	MLX5_SET(destroy_cq_in, din, cqn, cq->cqn);
174 	MLX5_SET(destroy_cq_in, din, uid, cq->uid);
175 	mlx5_cmd_exec_in(dev, destroy_cq, din);
176 	return err;
177 }
178 EXPORT_SYMBOL(mlx5_create_cq);
179 
180 /* oubox is checked and err val is normalized */
mlx5_core_create_cq(struct mlx5_core_dev * dev,struct mlx5_core_cq * cq,u32 * in,int inlen,u32 * out,int outlen)181 int mlx5_core_create_cq(struct mlx5_core_dev *dev, struct mlx5_core_cq *cq,
182 			u32 *in, int inlen, u32 *out, int outlen)
183 {
184 	int err = mlx5_create_cq(dev, cq, in, inlen, out, outlen);
185 
186 	return mlx5_cmd_check(dev, err, in, out);
187 }
188 EXPORT_SYMBOL(mlx5_core_create_cq);
189 
mlx5_core_destroy_cq(struct mlx5_core_dev * dev,struct mlx5_core_cq * cq)190 int mlx5_core_destroy_cq(struct mlx5_core_dev *dev, struct mlx5_core_cq *cq)
191 {
192 	u32 in[MLX5_ST_SZ_DW(destroy_cq_in)] = {};
193 	int err;
194 
195 	mlx5_debug_cq_remove(dev, cq);
196 
197 	mlx5_eq_del_cq(mlx5_get_async_eq(dev), cq);
198 	mlx5_eq_del_cq(&cq->eq->core, cq);
199 
200 	MLX5_SET(destroy_cq_in, in, opcode, MLX5_CMD_OP_DESTROY_CQ);
201 	MLX5_SET(destroy_cq_in, in, cqn, cq->cqn);
202 	MLX5_SET(destroy_cq_in, in, uid, cq->uid);
203 	err = mlx5_cmd_exec_in(dev, destroy_cq, in);
204 	if (err)
205 		return err;
206 
207 	synchronize_irq(cq->irqn);
208 	mlx5_cq_put(cq);
209 	wait_for_completion(&cq->free);
210 
211 	return 0;
212 }
213 EXPORT_SYMBOL(mlx5_core_destroy_cq);
214 
mlx5_core_query_cq(struct mlx5_core_dev * dev,struct mlx5_core_cq * cq,u32 * out)215 int mlx5_core_query_cq(struct mlx5_core_dev *dev, struct mlx5_core_cq *cq,
216 		       u32 *out)
217 {
218 	u32 in[MLX5_ST_SZ_DW(query_cq_in)] = {};
219 
220 	MLX5_SET(query_cq_in, in, opcode, MLX5_CMD_OP_QUERY_CQ);
221 	MLX5_SET(query_cq_in, in, cqn, cq->cqn);
222 	return mlx5_cmd_exec_inout(dev, query_cq, in, out);
223 }
224 EXPORT_SYMBOL(mlx5_core_query_cq);
225 
mlx5_core_modify_cq(struct mlx5_core_dev * dev,struct mlx5_core_cq * cq,u32 * in,int inlen)226 int mlx5_core_modify_cq(struct mlx5_core_dev *dev, struct mlx5_core_cq *cq,
227 			u32 *in, int inlen)
228 {
229 	u32 out[MLX5_ST_SZ_DW(modify_cq_out)] = {};
230 
231 	MLX5_SET(modify_cq_in, in, opcode, MLX5_CMD_OP_MODIFY_CQ);
232 	MLX5_SET(modify_cq_in, in, uid, cq->uid);
233 	return mlx5_cmd_exec(dev, in, inlen, out, sizeof(out));
234 }
235 EXPORT_SYMBOL(mlx5_core_modify_cq);
236 
mlx5_core_modify_cq_moderation(struct mlx5_core_dev * dev,struct mlx5_core_cq * cq,u16 cq_period,u16 cq_max_count)237 int mlx5_core_modify_cq_moderation(struct mlx5_core_dev *dev,
238 				   struct mlx5_core_cq *cq,
239 				   u16 cq_period,
240 				   u16 cq_max_count)
241 {
242 	u32 in[MLX5_ST_SZ_DW(modify_cq_in)] = {};
243 	void *cqc;
244 
245 	MLX5_SET(modify_cq_in, in, cqn, cq->cqn);
246 	cqc = MLX5_ADDR_OF(modify_cq_in, in, cq_context);
247 	MLX5_SET(cqc, cqc, cq_period, cq_period);
248 	MLX5_SET(cqc, cqc, cq_max_count, cq_max_count);
249 	MLX5_SET(modify_cq_in, in,
250 		 modify_field_select_resize_field_select.modify_field_select.modify_field_select,
251 		 MLX5_CQ_MODIFY_PERIOD | MLX5_CQ_MODIFY_COUNT);
252 
253 	return mlx5_core_modify_cq(dev, cq, in, sizeof(in));
254 }
255 EXPORT_SYMBOL(mlx5_core_modify_cq_moderation);
256