Merge remote-tracking branch 'asoc/for-4.17' into asoc-4.18 to resolve a
[sfrench/cifs-2.6.git] / drivers / net / ethernet / mellanox / mlx5 / core / transobj.c
1 /*
2  * Copyright (c) 2013-2015, Mellanox Technologies, Ltd.  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/mlx5/driver.h>
34 #include "mlx5_core.h"
35 #include <linux/mlx5/transobj.h>
36
37 int mlx5_core_alloc_transport_domain(struct mlx5_core_dev *dev, u32 *tdn)
38 {
39         u32 in[MLX5_ST_SZ_DW(alloc_transport_domain_in)]   = {0};
40         u32 out[MLX5_ST_SZ_DW(alloc_transport_domain_out)] = {0};
41         int err;
42
43         MLX5_SET(alloc_transport_domain_in, in, opcode,
44                  MLX5_CMD_OP_ALLOC_TRANSPORT_DOMAIN);
45
46         err = mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out));
47         if (!err)
48                 *tdn = MLX5_GET(alloc_transport_domain_out, out,
49                                 transport_domain);
50
51         return err;
52 }
53 EXPORT_SYMBOL(mlx5_core_alloc_transport_domain);
54
55 void mlx5_core_dealloc_transport_domain(struct mlx5_core_dev *dev, u32 tdn)
56 {
57         u32 in[MLX5_ST_SZ_DW(dealloc_transport_domain_in)]   = {0};
58         u32 out[MLX5_ST_SZ_DW(dealloc_transport_domain_out)] = {0};
59
60         MLX5_SET(dealloc_transport_domain_in, in, opcode,
61                  MLX5_CMD_OP_DEALLOC_TRANSPORT_DOMAIN);
62         MLX5_SET(dealloc_transport_domain_in, in, transport_domain, tdn);
63         mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out));
64 }
65 EXPORT_SYMBOL(mlx5_core_dealloc_transport_domain);
66
67 int mlx5_core_create_rq(struct mlx5_core_dev *dev, u32 *in, int inlen, u32 *rqn)
68 {
69         u32 out[MLX5_ST_SZ_DW(create_rq_out)] = {0};
70         int err;
71
72         MLX5_SET(create_rq_in, in, opcode, MLX5_CMD_OP_CREATE_RQ);
73         err = mlx5_cmd_exec(dev, in, inlen, out, sizeof(out));
74         if (!err)
75                 *rqn = MLX5_GET(create_rq_out, out, rqn);
76
77         return err;
78 }
79 EXPORT_SYMBOL(mlx5_core_create_rq);
80
81 int mlx5_core_modify_rq(struct mlx5_core_dev *dev, u32 rqn, u32 *in, int inlen)
82 {
83         u32 out[MLX5_ST_SZ_DW(modify_rq_out)];
84
85         MLX5_SET(modify_rq_in, in, rqn, rqn);
86         MLX5_SET(modify_rq_in, in, opcode, MLX5_CMD_OP_MODIFY_RQ);
87
88         memset(out, 0, sizeof(out));
89         return mlx5_cmd_exec(dev, in, inlen, out, sizeof(out));
90 }
91 EXPORT_SYMBOL(mlx5_core_modify_rq);
92
93 void mlx5_core_destroy_rq(struct mlx5_core_dev *dev, u32 rqn)
94 {
95         u32 in[MLX5_ST_SZ_DW(destroy_rq_in)]   = {0};
96         u32 out[MLX5_ST_SZ_DW(destroy_rq_out)] = {0};
97
98         MLX5_SET(destroy_rq_in, in, opcode, MLX5_CMD_OP_DESTROY_RQ);
99         MLX5_SET(destroy_rq_in, in, rqn, rqn);
100         mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out));
101 }
102 EXPORT_SYMBOL(mlx5_core_destroy_rq);
103
104 int mlx5_core_query_rq(struct mlx5_core_dev *dev, u32 rqn, u32 *out)
105 {
106         u32 in[MLX5_ST_SZ_DW(query_rq_in)] = {0};
107         int outlen = MLX5_ST_SZ_BYTES(query_rq_out);
108
109         MLX5_SET(query_rq_in, in, opcode, MLX5_CMD_OP_QUERY_RQ);
110         MLX5_SET(query_rq_in, in, rqn, rqn);
111
112         return mlx5_cmd_exec(dev, in, sizeof(in), out, outlen);
113 }
114 EXPORT_SYMBOL(mlx5_core_query_rq);
115
116 int mlx5_core_create_sq(struct mlx5_core_dev *dev, u32 *in, int inlen, u32 *sqn)
117 {
118         u32 out[MLX5_ST_SZ_DW(create_sq_out)] = {0};
119         int err;
120
121         MLX5_SET(create_sq_in, in, opcode, MLX5_CMD_OP_CREATE_SQ);
122         err = mlx5_cmd_exec(dev, in, inlen, out, sizeof(out));
123         if (!err)
124                 *sqn = MLX5_GET(create_sq_out, out, sqn);
125
126         return err;
127 }
128
129 int mlx5_core_modify_sq(struct mlx5_core_dev *dev, u32 sqn, u32 *in, int inlen)
130 {
131         u32 out[MLX5_ST_SZ_DW(modify_sq_out)] = {0};
132
133         MLX5_SET(modify_sq_in, in, sqn, sqn);
134         MLX5_SET(modify_sq_in, in, opcode, MLX5_CMD_OP_MODIFY_SQ);
135         return mlx5_cmd_exec(dev, in, inlen, out, sizeof(out));
136 }
137 EXPORT_SYMBOL(mlx5_core_modify_sq);
138
139 void mlx5_core_destroy_sq(struct mlx5_core_dev *dev, u32 sqn)
140 {
141         u32 in[MLX5_ST_SZ_DW(destroy_sq_in)]   = {0};
142         u32 out[MLX5_ST_SZ_DW(destroy_sq_out)] = {0};
143
144         MLX5_SET(destroy_sq_in, in, opcode, MLX5_CMD_OP_DESTROY_SQ);
145         MLX5_SET(destroy_sq_in, in, sqn, sqn);
146         mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out));
147 }
148
149 int mlx5_core_query_sq(struct mlx5_core_dev *dev, u32 sqn, u32 *out)
150 {
151         u32 in[MLX5_ST_SZ_DW(query_sq_in)] = {0};
152         int outlen = MLX5_ST_SZ_BYTES(query_sq_out);
153
154         MLX5_SET(query_sq_in, in, opcode, MLX5_CMD_OP_QUERY_SQ);
155         MLX5_SET(query_sq_in, in, sqn, sqn);
156         return mlx5_cmd_exec(dev, in, sizeof(in), out, outlen);
157 }
158 EXPORT_SYMBOL(mlx5_core_query_sq);
159
160 int mlx5_core_query_sq_state(struct mlx5_core_dev *dev, u32 sqn, u8 *state)
161 {
162         void *out;
163         void *sqc;
164         int inlen;
165         int err;
166
167         inlen = MLX5_ST_SZ_BYTES(query_sq_out);
168         out = kvzalloc(inlen, GFP_KERNEL);
169         if (!out)
170                 return -ENOMEM;
171
172         err = mlx5_core_query_sq(dev, sqn, out);
173         if (err)
174                 goto out;
175
176         sqc = MLX5_ADDR_OF(query_sq_out, out, sq_context);
177         *state = MLX5_GET(sqc, sqc, state);
178
179 out:
180         kvfree(out);
181         return err;
182 }
183 EXPORT_SYMBOL_GPL(mlx5_core_query_sq_state);
184
185 int mlx5_core_create_tir(struct mlx5_core_dev *dev, u32 *in, int inlen,
186                          u32 *tirn)
187 {
188         u32 out[MLX5_ST_SZ_DW(create_tir_out)] = {0};
189         int err;
190
191         MLX5_SET(create_tir_in, in, opcode, MLX5_CMD_OP_CREATE_TIR);
192
193         memset(out, 0, sizeof(out));
194         err = mlx5_cmd_exec(dev, in, inlen, out, sizeof(out));
195         if (!err)
196                 *tirn = MLX5_GET(create_tir_out, out, tirn);
197
198         return err;
199 }
200 EXPORT_SYMBOL(mlx5_core_create_tir);
201
202 int mlx5_core_modify_tir(struct mlx5_core_dev *dev, u32 tirn, u32 *in,
203                          int inlen)
204 {
205         u32 out[MLX5_ST_SZ_DW(modify_tir_out)] = {0};
206
207         MLX5_SET(modify_tir_in, in, tirn, tirn);
208         MLX5_SET(modify_tir_in, in, opcode, MLX5_CMD_OP_MODIFY_TIR);
209         return mlx5_cmd_exec(dev, in, inlen, out, sizeof(out));
210 }
211
212 void mlx5_core_destroy_tir(struct mlx5_core_dev *dev, u32 tirn)
213 {
214         u32 in[MLX5_ST_SZ_DW(destroy_tir_in)]   = {0};
215         u32 out[MLX5_ST_SZ_DW(destroy_tir_out)] = {0};
216
217         MLX5_SET(destroy_tir_in, in, opcode, MLX5_CMD_OP_DESTROY_TIR);
218         MLX5_SET(destroy_tir_in, in, tirn, tirn);
219         mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out));
220 }
221 EXPORT_SYMBOL(mlx5_core_destroy_tir);
222
223 int mlx5_core_create_tis(struct mlx5_core_dev *dev, u32 *in, int inlen,
224                          u32 *tisn)
225 {
226         u32 out[MLX5_ST_SZ_DW(create_tis_out)] = {0};
227         int err;
228
229         MLX5_SET(create_tis_in, in, opcode, MLX5_CMD_OP_CREATE_TIS);
230         err = mlx5_cmd_exec(dev, in, inlen, out, sizeof(out));
231         if (!err)
232                 *tisn = MLX5_GET(create_tis_out, out, tisn);
233
234         return err;
235 }
236 EXPORT_SYMBOL(mlx5_core_create_tis);
237
238 int mlx5_core_modify_tis(struct mlx5_core_dev *dev, u32 tisn, u32 *in,
239                          int inlen)
240 {
241         u32 out[MLX5_ST_SZ_DW(modify_tis_out)] = {0};
242
243         MLX5_SET(modify_tis_in, in, tisn, tisn);
244         MLX5_SET(modify_tis_in, in, opcode, MLX5_CMD_OP_MODIFY_TIS);
245
246         return mlx5_cmd_exec(dev, in, inlen, out, sizeof(out));
247 }
248 EXPORT_SYMBOL(mlx5_core_modify_tis);
249
250 void mlx5_core_destroy_tis(struct mlx5_core_dev *dev, u32 tisn)
251 {
252         u32 in[MLX5_ST_SZ_DW(destroy_tis_in)]   = {0};
253         u32 out[MLX5_ST_SZ_DW(destroy_tis_out)] = {0};
254
255         MLX5_SET(destroy_tis_in, in, opcode, MLX5_CMD_OP_DESTROY_TIS);
256         MLX5_SET(destroy_tis_in, in, tisn, tisn);
257         mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out));
258 }
259 EXPORT_SYMBOL(mlx5_core_destroy_tis);
260
261 int mlx5_core_create_rmp(struct mlx5_core_dev *dev, u32 *in, int inlen,
262                          u32 *rmpn)
263 {
264         u32 out[MLX5_ST_SZ_DW(create_rmp_out)] = {0};
265         int err;
266
267         MLX5_SET(create_rmp_in, in, opcode, MLX5_CMD_OP_CREATE_RMP);
268         err = mlx5_cmd_exec(dev, in, inlen, out, sizeof(out));
269         if (!err)
270                 *rmpn = MLX5_GET(create_rmp_out, out, rmpn);
271
272         return err;
273 }
274
275 int mlx5_core_modify_rmp(struct mlx5_core_dev *dev, u32 *in, int inlen)
276 {
277         u32 out[MLX5_ST_SZ_DW(modify_rmp_out)] = {0};
278
279         MLX5_SET(modify_rmp_in, in, opcode, MLX5_CMD_OP_MODIFY_RMP);
280         return mlx5_cmd_exec(dev, in, inlen, out, sizeof(out));
281 }
282
283 int mlx5_core_destroy_rmp(struct mlx5_core_dev *dev, u32 rmpn)
284 {
285         u32 in[MLX5_ST_SZ_DW(destroy_rmp_in)]   = {0};
286         u32 out[MLX5_ST_SZ_DW(destroy_rmp_out)] = {0};
287
288         MLX5_SET(destroy_rmp_in, in, opcode, MLX5_CMD_OP_DESTROY_RMP);
289         MLX5_SET(destroy_rmp_in, in, rmpn, rmpn);
290         return mlx5_cmd_exec(dev, in, sizeof(in), out,
291                                           sizeof(out));
292 }
293
294 int mlx5_core_query_rmp(struct mlx5_core_dev *dev, u32 rmpn, u32 *out)
295 {
296         u32 in[MLX5_ST_SZ_DW(query_rmp_in)] = {0};
297         int outlen = MLX5_ST_SZ_BYTES(query_rmp_out);
298
299         MLX5_SET(query_rmp_in, in, opcode, MLX5_CMD_OP_QUERY_RMP);
300         MLX5_SET(query_rmp_in, in, rmpn,   rmpn);
301         return mlx5_cmd_exec(dev, in, sizeof(in), out, outlen);
302 }
303
304 int mlx5_core_arm_rmp(struct mlx5_core_dev *dev, u32 rmpn, u16 lwm)
305 {
306         void *in;
307         void *rmpc;
308         void *wq;
309         void *bitmask;
310         int  err;
311
312         in = kvzalloc(MLX5_ST_SZ_BYTES(modify_rmp_in), GFP_KERNEL);
313         if (!in)
314                 return -ENOMEM;
315
316         rmpc    = MLX5_ADDR_OF(modify_rmp_in,   in,   ctx);
317         bitmask = MLX5_ADDR_OF(modify_rmp_in,   in,   bitmask);
318         wq      = MLX5_ADDR_OF(rmpc,            rmpc, wq);
319
320         MLX5_SET(modify_rmp_in, in,      rmp_state, MLX5_RMPC_STATE_RDY);
321         MLX5_SET(modify_rmp_in, in,      rmpn,      rmpn);
322         MLX5_SET(wq,            wq,      lwm,       lwm);
323         MLX5_SET(rmp_bitmask,   bitmask, lwm,       1);
324         MLX5_SET(rmpc,          rmpc,    state,     MLX5_RMPC_STATE_RDY);
325
326         err =  mlx5_core_modify_rmp(dev, in, MLX5_ST_SZ_BYTES(modify_rmp_in));
327
328         kvfree(in);
329
330         return err;
331 }
332
333 int mlx5_core_create_xsrq(struct mlx5_core_dev *dev, u32 *in, int inlen,
334                           u32 *xsrqn)
335 {
336         u32 out[MLX5_ST_SZ_DW(create_xrc_srq_out)] = {0};
337         int err;
338
339         MLX5_SET(create_xrc_srq_in, in, opcode,     MLX5_CMD_OP_CREATE_XRC_SRQ);
340         err = mlx5_cmd_exec(dev, in, inlen, out, sizeof(out));
341         if (!err)
342                 *xsrqn = MLX5_GET(create_xrc_srq_out, out, xrc_srqn);
343
344         return err;
345 }
346
347 int mlx5_core_destroy_xsrq(struct mlx5_core_dev *dev, u32 xsrqn)
348 {
349         u32 in[MLX5_ST_SZ_DW(destroy_xrc_srq_in)]   = {0};
350         u32 out[MLX5_ST_SZ_DW(destroy_xrc_srq_out)] = {0};
351
352         MLX5_SET(destroy_xrc_srq_in, in, opcode,   MLX5_CMD_OP_DESTROY_XRC_SRQ);
353         MLX5_SET(destroy_xrc_srq_in, in, xrc_srqn, xsrqn);
354         return mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out));
355 }
356
357 int mlx5_core_arm_xsrq(struct mlx5_core_dev *dev, u32 xsrqn, u16 lwm)
358 {
359         u32 in[MLX5_ST_SZ_DW(arm_xrc_srq_in)]   = {0};
360         u32 out[MLX5_ST_SZ_DW(arm_xrc_srq_out)] = {0};
361
362         MLX5_SET(arm_xrc_srq_in, in, opcode,   MLX5_CMD_OP_ARM_XRC_SRQ);
363         MLX5_SET(arm_xrc_srq_in, in, xrc_srqn, xsrqn);
364         MLX5_SET(arm_xrc_srq_in, in, lwm,      lwm);
365         MLX5_SET(arm_xrc_srq_in, in, op_mod,
366                  MLX5_ARM_XRC_SRQ_IN_OP_MOD_XRC_SRQ);
367         return mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out));
368 }
369
370 int mlx5_core_create_rqt(struct mlx5_core_dev *dev, u32 *in, int inlen,
371                          u32 *rqtn)
372 {
373         u32 out[MLX5_ST_SZ_DW(create_rqt_out)] = {0};
374         int err;
375
376         MLX5_SET(create_rqt_in, in, opcode, MLX5_CMD_OP_CREATE_RQT);
377         err = mlx5_cmd_exec(dev, in, inlen, out, sizeof(out));
378         if (!err)
379                 *rqtn = MLX5_GET(create_rqt_out, out, rqtn);
380
381         return err;
382 }
383 EXPORT_SYMBOL(mlx5_core_create_rqt);
384
385 int mlx5_core_modify_rqt(struct mlx5_core_dev *dev, u32 rqtn, u32 *in,
386                          int inlen)
387 {
388         u32 out[MLX5_ST_SZ_DW(modify_rqt_out)] = {0};
389
390         MLX5_SET(modify_rqt_in, in, rqtn, rqtn);
391         MLX5_SET(modify_rqt_in, in, opcode, MLX5_CMD_OP_MODIFY_RQT);
392         return mlx5_cmd_exec(dev, in, inlen, out, sizeof(out));
393 }
394
395 void mlx5_core_destroy_rqt(struct mlx5_core_dev *dev, u32 rqtn)
396 {
397         u32 in[MLX5_ST_SZ_DW(destroy_rqt_in)]   = {0};
398         u32 out[MLX5_ST_SZ_DW(destroy_rqt_out)] = {0};
399
400         MLX5_SET(destroy_rqt_in, in, opcode, MLX5_CMD_OP_DESTROY_RQT);
401         MLX5_SET(destroy_rqt_in, in, rqtn, rqtn);
402         mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out));
403 }
404 EXPORT_SYMBOL(mlx5_core_destroy_rqt);
405
406 static int mlx5_hairpin_create_rq(struct mlx5_core_dev *mdev,
407                                   struct mlx5_hairpin_params *params, u32 *rqn)
408 {
409         u32 in[MLX5_ST_SZ_DW(create_rq_in)] = {0};
410         void *rqc, *wq;
411
412         rqc = MLX5_ADDR_OF(create_rq_in, in, ctx);
413         wq  = MLX5_ADDR_OF(rqc, rqc, wq);
414
415         MLX5_SET(rqc, rqc, hairpin, 1);
416         MLX5_SET(rqc, rqc, state, MLX5_RQC_STATE_RST);
417         MLX5_SET(rqc, rqc, counter_set_id, params->q_counter);
418
419         MLX5_SET(wq, wq, log_hairpin_data_sz, params->log_data_size);
420         MLX5_SET(wq, wq, log_hairpin_num_packets, params->log_num_packets);
421
422         return mlx5_core_create_rq(mdev, in, MLX5_ST_SZ_BYTES(create_rq_in), rqn);
423 }
424
425 static int mlx5_hairpin_create_sq(struct mlx5_core_dev *mdev,
426                                   struct mlx5_hairpin_params *params, u32 *sqn)
427 {
428         u32 in[MLX5_ST_SZ_DW(create_sq_in)] = {0};
429         void *sqc, *wq;
430
431         sqc = MLX5_ADDR_OF(create_sq_in, in, ctx);
432         wq  = MLX5_ADDR_OF(sqc, sqc, wq);
433
434         MLX5_SET(sqc, sqc, hairpin, 1);
435         MLX5_SET(sqc, sqc, state, MLX5_SQC_STATE_RST);
436
437         MLX5_SET(wq, wq, log_hairpin_data_sz, params->log_data_size);
438         MLX5_SET(wq, wq, log_hairpin_num_packets, params->log_num_packets);
439
440         return mlx5_core_create_sq(mdev, in, MLX5_ST_SZ_BYTES(create_sq_in), sqn);
441 }
442
443 static int mlx5_hairpin_create_queues(struct mlx5_hairpin *hp,
444                                       struct mlx5_hairpin_params *params)
445 {
446         int i, j, err;
447
448         for (i = 0; i < hp->num_channels; i++) {
449                 err = mlx5_hairpin_create_rq(hp->func_mdev, params, &hp->rqn[i]);
450                 if (err)
451                         goto out_err_rq;
452         }
453
454         for (i = 0; i < hp->num_channels; i++) {
455                 err = mlx5_hairpin_create_sq(hp->peer_mdev, params, &hp->sqn[i]);
456                 if (err)
457                         goto out_err_sq;
458         }
459
460         return 0;
461
462 out_err_sq:
463         for (j = 0; j < i; j++)
464                 mlx5_core_destroy_sq(hp->peer_mdev, hp->sqn[j]);
465         i = hp->num_channels;
466 out_err_rq:
467         for (j = 0; j < i; j++)
468                 mlx5_core_destroy_rq(hp->func_mdev, hp->rqn[j]);
469         return err;
470 }
471
472 static void mlx5_hairpin_destroy_queues(struct mlx5_hairpin *hp)
473 {
474         int i;
475
476         for (i = 0; i < hp->num_channels; i++) {
477                 mlx5_core_destroy_rq(hp->func_mdev, hp->rqn[i]);
478                 mlx5_core_destroy_sq(hp->peer_mdev, hp->sqn[i]);
479         }
480 }
481
482 static int mlx5_hairpin_modify_rq(struct mlx5_core_dev *func_mdev, u32 rqn,
483                                   int curr_state, int next_state,
484                                   u16 peer_vhca, u32 peer_sq)
485 {
486         u32 in[MLX5_ST_SZ_DW(modify_rq_in)] = {0};
487         void *rqc;
488
489         rqc = MLX5_ADDR_OF(modify_rq_in, in, ctx);
490
491         if (next_state == MLX5_RQC_STATE_RDY) {
492                 MLX5_SET(rqc, rqc, hairpin_peer_sq, peer_sq);
493                 MLX5_SET(rqc, rqc, hairpin_peer_vhca, peer_vhca);
494         }
495
496         MLX5_SET(modify_rq_in, in, rq_state, curr_state);
497         MLX5_SET(rqc, rqc, state, next_state);
498
499         return mlx5_core_modify_rq(func_mdev, rqn,
500                                    in, MLX5_ST_SZ_BYTES(modify_rq_in));
501 }
502
503 static int mlx5_hairpin_modify_sq(struct mlx5_core_dev *peer_mdev, u32 sqn,
504                                   int curr_state, int next_state,
505                                   u16 peer_vhca, u32 peer_rq)
506 {
507         u32 in[MLX5_ST_SZ_DW(modify_sq_in)] = {0};
508         void *sqc;
509
510         sqc = MLX5_ADDR_OF(modify_sq_in, in, ctx);
511
512         if (next_state == MLX5_RQC_STATE_RDY) {
513                 MLX5_SET(sqc, sqc, hairpin_peer_rq, peer_rq);
514                 MLX5_SET(sqc, sqc, hairpin_peer_vhca, peer_vhca);
515         }
516
517         MLX5_SET(modify_sq_in, in, sq_state, curr_state);
518         MLX5_SET(sqc, sqc, state, next_state);
519
520         return mlx5_core_modify_sq(peer_mdev, sqn,
521                                    in, MLX5_ST_SZ_BYTES(modify_sq_in));
522 }
523
524 static int mlx5_hairpin_pair_queues(struct mlx5_hairpin *hp)
525 {
526         int i, j, err;
527
528         /* set peer SQs */
529         for (i = 0; i < hp->num_channels; i++) {
530                 err = mlx5_hairpin_modify_sq(hp->peer_mdev, hp->sqn[i],
531                                              MLX5_SQC_STATE_RST, MLX5_SQC_STATE_RDY,
532                                              MLX5_CAP_GEN(hp->func_mdev, vhca_id), hp->rqn[i]);
533                 if (err)
534                         goto err_modify_sq;
535         }
536
537         /* set func RQs */
538         for (i = 0; i < hp->num_channels; i++) {
539                 err = mlx5_hairpin_modify_rq(hp->func_mdev, hp->rqn[i],
540                                              MLX5_RQC_STATE_RST, MLX5_RQC_STATE_RDY,
541                                              MLX5_CAP_GEN(hp->peer_mdev, vhca_id), hp->sqn[i]);
542                 if (err)
543                         goto err_modify_rq;
544         }
545
546         return 0;
547
548 err_modify_rq:
549         for (j = 0; j < i; j++)
550                 mlx5_hairpin_modify_rq(hp->func_mdev, hp->rqn[j], MLX5_RQC_STATE_RDY,
551                                        MLX5_RQC_STATE_RST, 0, 0);
552         i = hp->num_channels;
553 err_modify_sq:
554         for (j = 0; j < i; j++)
555                 mlx5_hairpin_modify_sq(hp->peer_mdev, hp->sqn[j], MLX5_SQC_STATE_RDY,
556                                        MLX5_SQC_STATE_RST, 0, 0);
557         return err;
558 }
559
560 static void mlx5_hairpin_unpair_queues(struct mlx5_hairpin *hp)
561 {
562         int i;
563
564         /* unset func RQs */
565         for (i = 0; i < hp->num_channels; i++)
566                 mlx5_hairpin_modify_rq(hp->func_mdev, hp->rqn[i], MLX5_RQC_STATE_RDY,
567                                        MLX5_RQC_STATE_RST, 0, 0);
568
569         /* unset peer SQs */
570         for (i = 0; i < hp->num_channels; i++)
571                 mlx5_hairpin_modify_sq(hp->peer_mdev, hp->sqn[i], MLX5_SQC_STATE_RDY,
572                                        MLX5_SQC_STATE_RST, 0, 0);
573 }
574
575 struct mlx5_hairpin *
576 mlx5_core_hairpin_create(struct mlx5_core_dev *func_mdev,
577                          struct mlx5_core_dev *peer_mdev,
578                          struct mlx5_hairpin_params *params)
579 {
580         struct mlx5_hairpin *hp;
581         int size, err;
582
583         size = sizeof(*hp) + params->num_channels * 2 * sizeof(u32);
584         hp = kzalloc(size, GFP_KERNEL);
585         if (!hp)
586                 return ERR_PTR(-ENOMEM);
587
588         hp->func_mdev = func_mdev;
589         hp->peer_mdev = peer_mdev;
590         hp->num_channels = params->num_channels;
591
592         hp->rqn = (void *)hp + sizeof(*hp);
593         hp->sqn = hp->rqn + params->num_channels;
594
595         /* alloc and pair func --> peer hairpin */
596         err = mlx5_hairpin_create_queues(hp, params);
597         if (err)
598                 goto err_create_queues;
599
600         err = mlx5_hairpin_pair_queues(hp);
601         if (err)
602                 goto err_pair_queues;
603
604         return hp;
605
606 err_pair_queues:
607         mlx5_hairpin_destroy_queues(hp);
608 err_create_queues:
609         kfree(hp);
610         return ERR_PTR(err);
611 }
612
613 void mlx5_core_hairpin_destroy(struct mlx5_hairpin *hp)
614 {
615         mlx5_hairpin_unpair_queues(hp);
616         mlx5_hairpin_destroy_queues(hp);
617         kfree(hp);
618 }