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/module.h>
35 #include <linux/hardirq.h>
36 #include <linux/mlx5/driver.h>
37 #include <rdma/ib_verbs.h>
38 #include <linux/mlx5/cq.h>
39 #include "mlx5_core.h"
40 #include "lib/eq.h"
41
42 #define TASKLET_MAX_TIME 2
43 #define TASKLET_MAX_TIME_JIFFIES msecs_to_jiffies(TASKLET_MAX_TIME)
44
mlx5_cq_tasklet_cb(struct tasklet_struct * t)45 void mlx5_cq_tasklet_cb(struct tasklet_struct *t)
46 {
47 unsigned long flags;
48 unsigned long end = jiffies + TASKLET_MAX_TIME_JIFFIES;
49 struct mlx5_eq_tasklet *ctx = from_tasklet(ctx, t, task);
50 struct mlx5_core_cq *mcq;
51 struct mlx5_core_cq *temp;
52
53 spin_lock_irqsave(&ctx->lock, flags);
54 list_splice_tail_init(&ctx->list, &ctx->process_list);
55 spin_unlock_irqrestore(&ctx->lock, flags);
56
57 list_for_each_entry_safe(mcq, temp, &ctx->process_list,
58 tasklet_ctx.list) {
59 list_del_init(&mcq->tasklet_ctx.list);
60 mcq->tasklet_ctx.comp(mcq, NULL);
61 mlx5_cq_put(mcq);
62 if (time_after(jiffies, end))
63 break;
64 }
65
66 if (!list_empty(&ctx->process_list))
67 tasklet_schedule(&ctx->task);
68 }
69
mlx5_add_cq_to_tasklet(struct mlx5_core_cq * cq,struct mlx5_eqe * eqe)70 static void mlx5_add_cq_to_tasklet(struct mlx5_core_cq *cq,
71 struct mlx5_eqe *eqe)
72 {
73 unsigned long flags;
74 struct mlx5_eq_tasklet *tasklet_ctx = cq->tasklet_ctx.priv;
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 list_add_tail(&cq->tasklet_ctx.list, &tasklet_ctx->list);
85 }
86 spin_unlock_irqrestore(&tasklet_ctx->lock, flags);
87 }
88
mlx5_core_create_cq(struct mlx5_core_dev * dev,struct mlx5_core_cq * cq,u32 * in,int inlen,u32 * out,int outlen)89 int mlx5_core_create_cq(struct mlx5_core_dev *dev, struct mlx5_core_cq *cq,
90 u32 *in, int inlen, u32 *out, int outlen)
91 {
92 int eqn = MLX5_GET(cqc, MLX5_ADDR_OF(create_cq_in, in, cq_context), c_eqn);
93 u32 din[MLX5_ST_SZ_DW(destroy_cq_in)] = {};
94 struct mlx5_eq_comp *eq;
95 int err;
96
97 eq = mlx5_eqn2comp_eq(dev, eqn);
98 if (IS_ERR(eq))
99 return PTR_ERR(eq);
100
101 memset(out, 0, outlen);
102 MLX5_SET(create_cq_in, in, opcode, MLX5_CMD_OP_CREATE_CQ);
103 err = mlx5_cmd_exec(dev, in, inlen, out, outlen);
104 if (err)
105 return err;
106
107 cq->cqn = MLX5_GET(create_cq_out, out, cqn);
108 cq->cons_index = 0;
109 cq->arm_sn = 0;
110 cq->eq = eq;
111 cq->uid = MLX5_GET(create_cq_in, in, uid);
112 refcount_set(&cq->refcount, 1);
113 init_completion(&cq->free);
114 if (!cq->comp)
115 cq->comp = mlx5_add_cq_to_tasklet;
116 /* assuming CQ will be deleted before the EQ */
117 cq->tasklet_ctx.priv = &eq->tasklet_ctx;
118 INIT_LIST_HEAD(&cq->tasklet_ctx.list);
119
120 /* Add to comp EQ CQ tree to recv comp events */
121 err = mlx5_eq_add_cq(&eq->core, cq);
122 if (err)
123 goto err_cmd;
124
125 /* Add to async EQ CQ tree to recv async events */
126 err = mlx5_eq_add_cq(mlx5_get_async_eq(dev), cq);
127 if (err)
128 goto err_cq_add;
129
130 cq->pid = current->pid;
131 err = mlx5_debug_cq_add(dev, cq);
132 if (err)
133 mlx5_core_dbg(dev, "failed adding CP 0x%x to debug file system\n",
134 cq->cqn);
135
136 cq->uar = dev->priv.uar;
137 cq->irqn = eq->core.irqn;
138
139 return 0;
140
141 err_cq_add:
142 mlx5_eq_del_cq(&eq->core, cq);
143 err_cmd:
144 MLX5_SET(destroy_cq_in, din, opcode, MLX5_CMD_OP_DESTROY_CQ);
145 MLX5_SET(destroy_cq_in, din, cqn, cq->cqn);
146 MLX5_SET(destroy_cq_in, din, uid, cq->uid);
147 mlx5_cmd_exec_in(dev, destroy_cq, din);
148 return err;
149 }
150 EXPORT_SYMBOL(mlx5_core_create_cq);
151
mlx5_core_destroy_cq(struct mlx5_core_dev * dev,struct mlx5_core_cq * cq)152 int mlx5_core_destroy_cq(struct mlx5_core_dev *dev, struct mlx5_core_cq *cq)
153 {
154 u32 in[MLX5_ST_SZ_DW(destroy_cq_in)] = {};
155 int err;
156
157 mlx5_debug_cq_remove(dev, cq);
158
159 mlx5_eq_del_cq(mlx5_get_async_eq(dev), cq);
160 mlx5_eq_del_cq(&cq->eq->core, cq);
161
162 MLX5_SET(destroy_cq_in, in, opcode, MLX5_CMD_OP_DESTROY_CQ);
163 MLX5_SET(destroy_cq_in, in, cqn, cq->cqn);
164 MLX5_SET(destroy_cq_in, in, uid, cq->uid);
165 err = mlx5_cmd_exec_in(dev, destroy_cq, in);
166 if (err)
167 return err;
168
169 synchronize_irq(cq->irqn);
170 mlx5_cq_put(cq);
171 wait_for_completion(&cq->free);
172
173 return 0;
174 }
175 EXPORT_SYMBOL(mlx5_core_destroy_cq);
176
mlx5_core_query_cq(struct mlx5_core_dev * dev,struct mlx5_core_cq * cq,u32 * out)177 int mlx5_core_query_cq(struct mlx5_core_dev *dev, struct mlx5_core_cq *cq,
178 u32 *out)
179 {
180 u32 in[MLX5_ST_SZ_DW(query_cq_in)] = {};
181
182 MLX5_SET(query_cq_in, in, opcode, MLX5_CMD_OP_QUERY_CQ);
183 MLX5_SET(query_cq_in, in, cqn, cq->cqn);
184 return mlx5_cmd_exec_inout(dev, query_cq, in, out);
185 }
186 EXPORT_SYMBOL(mlx5_core_query_cq);
187
mlx5_core_modify_cq(struct mlx5_core_dev * dev,struct mlx5_core_cq * cq,u32 * in,int inlen)188 int mlx5_core_modify_cq(struct mlx5_core_dev *dev, struct mlx5_core_cq *cq,
189 u32 *in, int inlen)
190 {
191 u32 out[MLX5_ST_SZ_DW(modify_cq_out)] = {};
192
193 MLX5_SET(modify_cq_in, in, opcode, MLX5_CMD_OP_MODIFY_CQ);
194 MLX5_SET(modify_cq_in, in, uid, cq->uid);
195 return mlx5_cmd_exec(dev, in, inlen, out, sizeof(out));
196 }
197 EXPORT_SYMBOL(mlx5_core_modify_cq);
198
mlx5_core_modify_cq_moderation(struct mlx5_core_dev * dev,struct mlx5_core_cq * cq,u16 cq_period,u16 cq_max_count)199 int mlx5_core_modify_cq_moderation(struct mlx5_core_dev *dev,
200 struct mlx5_core_cq *cq,
201 u16 cq_period,
202 u16 cq_max_count)
203 {
204 u32 in[MLX5_ST_SZ_DW(modify_cq_in)] = {};
205 void *cqc;
206
207 MLX5_SET(modify_cq_in, in, cqn, cq->cqn);
208 cqc = MLX5_ADDR_OF(modify_cq_in, in, cq_context);
209 MLX5_SET(cqc, cqc, cq_period, cq_period);
210 MLX5_SET(cqc, cqc, cq_max_count, cq_max_count);
211 MLX5_SET(modify_cq_in, in,
212 modify_field_select_resize_field_select.modify_field_select.modify_field_select,
213 MLX5_CQ_MODIFY_PERIOD | MLX5_CQ_MODIFY_COUNT);
214
215 return mlx5_core_modify_cq(dev, cq, in, sizeof(in));
216 }
217 EXPORT_SYMBOL(mlx5_core_modify_cq_moderation);
218