Merge branch 'x86-pti-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux-2.6-block.git] / drivers / net / ethernet / mellanox / mlx5 / core / en / reporter_rx.c
CommitLineData
9032e719
AL
1// SPDX-License-Identifier: GPL-2.0
2// Copyright (c) 2019 Mellanox Technologies.
3
4#include "health.h"
5#include "params.h"
6
7static int mlx5e_query_rq_state(struct mlx5_core_dev *dev, u32 rqn, u8 *state)
8{
9 int outlen = MLX5_ST_SZ_BYTES(query_rq_out);
10 void *out;
11 void *rqc;
12 int err;
13
14 out = kvzalloc(outlen, GFP_KERNEL);
15 if (!out)
16 return -ENOMEM;
17
18 err = mlx5_core_query_rq(dev, rqn, out);
19 if (err)
20 goto out;
21
22 rqc = MLX5_ADDR_OF(query_rq_out, out, rq_context);
23 *state = MLX5_GET(rqc, rqc, state);
24
25out:
26 kvfree(out);
27 return err;
28}
29
be5323c8
AL
30static int mlx5e_wait_for_icosq_flush(struct mlx5e_icosq *icosq)
31{
32 unsigned long exp_time = jiffies + msecs_to_jiffies(2000);
33
34 while (time_before(jiffies, exp_time)) {
35 if (icosq->cc == icosq->pc)
36 return 0;
37
38 msleep(20);
39 }
40
41 netdev_err(icosq->channel->netdev,
42 "Wait for ICOSQ 0x%x flush timeout (cc = 0x%x, pc = 0x%x)\n",
43 icosq->sqn, icosq->cc, icosq->pc);
44
45 return -ETIMEDOUT;
46}
47
48static void mlx5e_reset_icosq_cc_pc(struct mlx5e_icosq *icosq)
49{
50 WARN_ONCE(icosq->cc != icosq->pc, "ICOSQ 0x%x: cc (0x%x) != pc (0x%x)\n",
51 icosq->sqn, icosq->cc, icosq->pc);
52 icosq->cc = 0;
53 icosq->pc = 0;
54}
55
56static int mlx5e_rx_reporter_err_icosq_cqe_recover(void *ctx)
57{
58 struct mlx5_core_dev *mdev;
59 struct mlx5e_icosq *icosq;
60 struct net_device *dev;
61 struct mlx5e_rq *rq;
62 u8 state;
63 int err;
64
65 icosq = ctx;
66 rq = &icosq->channel->rq;
67 mdev = icosq->channel->mdev;
68 dev = icosq->channel->netdev;
69 err = mlx5_core_query_sq_state(mdev, icosq->sqn, &state);
70 if (err) {
71 netdev_err(dev, "Failed to query ICOSQ 0x%x state. err = %d\n",
72 icosq->sqn, err);
73 goto out;
74 }
75
76 if (state != MLX5_SQC_STATE_ERR)
77 goto out;
78
79 mlx5e_deactivate_rq(rq);
80 err = mlx5e_wait_for_icosq_flush(icosq);
81 if (err)
82 goto out;
83
84 mlx5e_deactivate_icosq(icosq);
85
86 /* At this point, both the rq and the icosq are disabled */
87
88 err = mlx5e_health_sq_to_ready(icosq->channel, icosq->sqn);
89 if (err)
90 goto out;
91
92 mlx5e_reset_icosq_cc_pc(icosq);
93 mlx5e_free_rx_descs(rq);
94 clear_bit(MLX5E_SQ_STATE_RECOVERING, &icosq->state);
95 mlx5e_activate_icosq(icosq);
96 mlx5e_activate_rq(rq);
97
98 rq->stats->recover++;
99 return 0;
100out:
101 clear_bit(MLX5E_SQ_STATE_RECOVERING, &icosq->state);
102 return err;
103}
104
105void mlx5e_reporter_icosq_cqe_err(struct mlx5e_icosq *icosq)
106{
107 struct mlx5e_priv *priv = icosq->channel->priv;
108 char err_str[MLX5E_REPORTER_PER_Q_MAX_LEN];
109 struct mlx5e_err_ctx err_ctx = {};
110
111 err_ctx.ctx = icosq;
112 err_ctx.recover = mlx5e_rx_reporter_err_icosq_cqe_recover;
113 sprintf(err_str, "ERR CQE on ICOSQ: 0x%x", icosq->sqn);
114
115 mlx5e_health_report(priv, priv->rx_reporter, err_str, &err_ctx);
116}
117
8276ea13
AL
118static int mlx5e_rq_to_ready(struct mlx5e_rq *rq, int curr_state)
119{
120 struct net_device *dev = rq->netdev;
121 int err;
122
123 err = mlx5e_modify_rq_state(rq, curr_state, MLX5_RQC_STATE_RST);
124 if (err) {
125 netdev_err(dev, "Failed to move rq 0x%x to reset\n", rq->rqn);
126 return err;
127 }
128 err = mlx5e_modify_rq_state(rq, MLX5_RQC_STATE_RST, MLX5_RQC_STATE_RDY);
129 if (err) {
130 netdev_err(dev, "Failed to move rq 0x%x to ready\n", rq->rqn);
131 return err;
132 }
133
134 return 0;
135}
136
137static int mlx5e_rx_reporter_err_rq_cqe_recover(void *ctx)
138{
139 struct mlx5_core_dev *mdev;
140 struct net_device *dev;
141 struct mlx5e_rq *rq;
142 u8 state;
143 int err;
144
145 rq = ctx;
146 mdev = rq->mdev;
147 dev = rq->netdev;
148 err = mlx5e_query_rq_state(mdev, rq->rqn, &state);
149 if (err) {
150 netdev_err(dev, "Failed to query RQ 0x%x state. err = %d\n",
151 rq->rqn, err);
152 goto out;
153 }
154
155 if (state != MLX5_RQC_STATE_ERR)
156 goto out;
157
158 mlx5e_deactivate_rq(rq);
159 mlx5e_free_rx_descs(rq);
160
161 err = mlx5e_rq_to_ready(rq, MLX5_RQC_STATE_ERR);
162 if (err)
163 goto out;
164
165 clear_bit(MLX5E_RQ_STATE_RECOVERING, &rq->state);
166 mlx5e_activate_rq(rq);
167 rq->stats->recover++;
168 return 0;
169out:
170 clear_bit(MLX5E_RQ_STATE_RECOVERING, &rq->state);
171 return err;
172}
173
174void mlx5e_reporter_rq_cqe_err(struct mlx5e_rq *rq)
175{
176 struct mlx5e_priv *priv = rq->channel->priv;
177 char err_str[MLX5E_REPORTER_PER_Q_MAX_LEN];
178 struct mlx5e_err_ctx err_ctx = {};
179
180 err_ctx.ctx = rq;
181 err_ctx.recover = mlx5e_rx_reporter_err_rq_cqe_recover;
182 sprintf(err_str, "ERR CQE on RQ: 0x%x", rq->rqn);
183
184 mlx5e_health_report(priv, priv->rx_reporter, err_str, &err_ctx);
185}
186
32c57fb2
AL
187static int mlx5e_rx_reporter_timeout_recover(void *ctx)
188{
189 struct mlx5e_icosq *icosq;
190 struct mlx5_eq_comp *eq;
191 struct mlx5e_rq *rq;
192 int err;
193
194 rq = ctx;
195 icosq = &rq->channel->icosq;
196 eq = rq->cq.mcq.eq;
197 err = mlx5e_health_channel_eq_recover(eq, rq->channel);
198 if (err)
199 clear_bit(MLX5E_SQ_STATE_ENABLED, &icosq->state);
200
201 return err;
202}
203
204void mlx5e_reporter_rx_timeout(struct mlx5e_rq *rq)
205{
206 struct mlx5e_icosq *icosq = &rq->channel->icosq;
207 struct mlx5e_priv *priv = rq->channel->priv;
208 char err_str[MLX5E_REPORTER_PER_Q_MAX_LEN];
209 struct mlx5e_err_ctx err_ctx = {};
210
211 err_ctx.ctx = rq;
212 err_ctx.recover = mlx5e_rx_reporter_timeout_recover;
213 sprintf(err_str, "RX timeout on channel: %d, ICOSQ: 0x%x RQ: 0x%x, CQ: 0x%x\n",
214 icosq->channel->ix, icosq->sqn, rq->rqn, rq->cq.mcq.cqn);
215
216 mlx5e_health_report(priv, priv->rx_reporter, err_str, &err_ctx);
217}
218
be5323c8
AL
219static int mlx5e_rx_reporter_recover_from_ctx(struct mlx5e_err_ctx *err_ctx)
220{
221 return err_ctx->recover(err_ctx->ctx);
222}
223
224static int mlx5e_rx_reporter_recover(struct devlink_health_reporter *reporter,
e7a98105
JP
225 void *context,
226 struct netlink_ext_ack *extack)
be5323c8
AL
227{
228 struct mlx5e_priv *priv = devlink_health_reporter_priv(reporter);
229 struct mlx5e_err_ctx *err_ctx = context;
230
231 return err_ctx ? mlx5e_rx_reporter_recover_from_ctx(err_ctx) :
232 mlx5e_health_recover_channels(priv);
233}
234
9032e719
AL
235static int mlx5e_rx_reporter_build_diagnose_output(struct mlx5e_rq *rq,
236 struct devlink_fmsg *fmsg)
237{
238 struct mlx5e_priv *priv = rq->channel->priv;
239 struct mlx5e_params *params;
240 struct mlx5e_icosq *icosq;
241 u8 icosq_hw_state;
242 int wqes_sz;
243 u8 hw_state;
244 u16 wq_head;
245 int err;
246
247 params = &priv->channels.params;
248 icosq = &rq->channel->icosq;
249 err = mlx5e_query_rq_state(priv->mdev, rq->rqn, &hw_state);
250 if (err)
251 return err;
252
253 err = mlx5_core_query_sq_state(priv->mdev, icosq->sqn, &icosq_hw_state);
254 if (err)
255 return err;
256
257 wqes_sz = mlx5e_rqwq_get_cur_sz(rq);
258 wq_head = params->rq_wq_type == MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ ?
259 rq->mpwqe.wq.head : mlx5_wq_cyc_get_head(&rq->wqe.wq);
260
261 err = devlink_fmsg_obj_nest_start(fmsg);
262 if (err)
263 return err;
264
265 err = devlink_fmsg_u32_pair_put(fmsg, "channel ix", rq->channel->ix);
266 if (err)
267 return err;
268
269 err = devlink_fmsg_u32_pair_put(fmsg, "rqn", rq->rqn);
270 if (err)
271 return err;
272
273 err = devlink_fmsg_u8_pair_put(fmsg, "HW state", hw_state);
274 if (err)
275 return err;
276
277 err = devlink_fmsg_u8_pair_put(fmsg, "SW state", rq->state);
278 if (err)
279 return err;
280
281 err = devlink_fmsg_u32_pair_put(fmsg, "posted WQEs", wqes_sz);
282 if (err)
283 return err;
284
285 err = devlink_fmsg_u32_pair_put(fmsg, "cc", wq_head);
286 if (err)
287 return err;
288
289 err = devlink_fmsg_u8_pair_put(fmsg, "ICOSQ HW state", icosq_hw_state);
290 if (err)
291 return err;
292
293 err = mlx5e_reporter_cq_diagnose(&rq->cq, fmsg);
294 if (err)
295 return err;
296
297 err = devlink_fmsg_obj_nest_end(fmsg);
298 if (err)
299 return err;
300
301 return 0;
302}
303
304static int mlx5e_rx_reporter_diagnose(struct devlink_health_reporter *reporter,
e7a98105
JP
305 struct devlink_fmsg *fmsg,
306 struct netlink_ext_ack *extack)
9032e719
AL
307{
308 struct mlx5e_priv *priv = devlink_health_reporter_priv(reporter);
309 struct mlx5e_params *params = &priv->channels.params;
310 struct mlx5e_rq *generic_rq;
311 u32 rq_stride, rq_sz;
312 int i, err = 0;
313
314 mutex_lock(&priv->state_lock);
315
316 if (!test_bit(MLX5E_STATE_OPENED, &priv->state))
317 goto unlock;
318
319 generic_rq = &priv->channels.c[0]->rq;
320 rq_sz = mlx5e_rqwq_get_size(generic_rq);
321 rq_stride = BIT(mlx5e_mpwqe_get_log_stride_size(priv->mdev, params, NULL));
322
323 err = mlx5e_reporter_named_obj_nest_start(fmsg, "Common config");
324 if (err)
325 goto unlock;
326
327 err = mlx5e_reporter_named_obj_nest_start(fmsg, "RQ");
328 if (err)
329 goto unlock;
330
331 err = devlink_fmsg_u8_pair_put(fmsg, "type", params->rq_wq_type);
332 if (err)
333 goto unlock;
334
335 err = devlink_fmsg_u64_pair_put(fmsg, "stride size", rq_stride);
336 if (err)
337 goto unlock;
338
339 err = devlink_fmsg_u32_pair_put(fmsg, "size", rq_sz);
340 if (err)
341 goto unlock;
342
343 err = mlx5e_reporter_named_obj_nest_end(fmsg);
344 if (err)
345 goto unlock;
346
347 err = mlx5e_reporter_cq_common_diagnose(&generic_rq->cq, fmsg);
348 if (err)
349 goto unlock;
350
351 err = mlx5e_reporter_named_obj_nest_end(fmsg);
352 if (err)
353 goto unlock;
354
355 err = devlink_fmsg_arr_pair_nest_start(fmsg, "RQs");
356 if (err)
357 goto unlock;
358
359 for (i = 0; i < priv->channels.num; i++) {
360 struct mlx5e_rq *rq = &priv->channels.c[i]->rq;
361
362 err = mlx5e_rx_reporter_build_diagnose_output(rq, fmsg);
363 if (err)
364 goto unlock;
365 }
366 err = devlink_fmsg_arr_pair_nest_end(fmsg);
367 if (err)
368 goto unlock;
369unlock:
370 mutex_unlock(&priv->state_lock);
371 return err;
372}
373
374static const struct devlink_health_reporter_ops mlx5_rx_reporter_ops = {
375 .name = "rx",
be5323c8 376 .recover = mlx5e_rx_reporter_recover,
9032e719
AL
377 .diagnose = mlx5e_rx_reporter_diagnose,
378};
379
be5323c8
AL
380#define MLX5E_REPORTER_RX_GRACEFUL_PERIOD 500
381
9032e719
AL
382int mlx5e_reporter_rx_create(struct mlx5e_priv *priv)
383{
384 struct devlink *devlink = priv_to_devlink(priv->mdev);
385 struct devlink_health_reporter *reporter;
386
387 reporter = devlink_health_reporter_create(devlink,
388 &mlx5_rx_reporter_ops,
be5323c8
AL
389 MLX5E_REPORTER_RX_GRACEFUL_PERIOD,
390 true, priv);
9032e719
AL
391 if (IS_ERR(reporter)) {
392 netdev_warn(priv->netdev, "Failed to create rx reporter, err = %ld\n",
393 PTR_ERR(reporter));
394 return PTR_ERR(reporter);
395 }
396 priv->rx_reporter = reporter;
397 return 0;
398}
399
400void mlx5e_reporter_rx_destroy(struct mlx5e_priv *priv)
401{
402 if (!priv->rx_reporter)
403 return;
404
405 devlink_health_reporter_destroy(priv->rx_reporter);
406}