2 * Copyright (c) 2013-2015, Mellanox Technologies, Ltd. 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
33 #include <linux/mlx5/driver.h>
34 #include "mlx5_core.h"
35 #include <linux/mlx5/transobj.h>
37 int mlx5_core_alloc_transport_domain(struct mlx5_core_dev *dev, u32 *tdn)
39 u32 in[MLX5_ST_SZ_DW(alloc_transport_domain_in)] = {0};
40 u32 out[MLX5_ST_SZ_DW(alloc_transport_domain_out)] = {0};
43 MLX5_SET(alloc_transport_domain_in, in, opcode,
44 MLX5_CMD_OP_ALLOC_TRANSPORT_DOMAIN);
46 err = mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out));
48 *tdn = MLX5_GET(alloc_transport_domain_out, out,
53 EXPORT_SYMBOL(mlx5_core_alloc_transport_domain);
55 void mlx5_core_dealloc_transport_domain(struct mlx5_core_dev *dev, u32 tdn)
57 u32 in[MLX5_ST_SZ_DW(dealloc_transport_domain_in)] = {0};
58 u32 out[MLX5_ST_SZ_DW(dealloc_transport_domain_out)] = {0};
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));
65 EXPORT_SYMBOL(mlx5_core_dealloc_transport_domain);
67 int mlx5_core_create_rq(struct mlx5_core_dev *dev, u32 *in, int inlen, u32 *rqn)
69 u32 out[MLX5_ST_SZ_DW(create_rq_out)] = {0};
72 MLX5_SET(create_rq_in, in, opcode, MLX5_CMD_OP_CREATE_RQ);
73 err = mlx5_cmd_exec(dev, in, inlen, out, sizeof(out));
75 *rqn = MLX5_GET(create_rq_out, out, rqn);
79 EXPORT_SYMBOL(mlx5_core_create_rq);
81 int mlx5_core_modify_rq(struct mlx5_core_dev *dev, u32 rqn, u32 *in, int inlen)
83 u32 out[MLX5_ST_SZ_DW(modify_rq_out)];
85 MLX5_SET(modify_rq_in, in, rqn, rqn);
86 MLX5_SET(modify_rq_in, in, opcode, MLX5_CMD_OP_MODIFY_RQ);
88 memset(out, 0, sizeof(out));
89 return mlx5_cmd_exec(dev, in, inlen, out, sizeof(out));
91 EXPORT_SYMBOL(mlx5_core_modify_rq);
93 void mlx5_core_destroy_rq(struct mlx5_core_dev *dev, u32 rqn)
95 u32 in[MLX5_ST_SZ_DW(destroy_rq_in)] = {0};
96 u32 out[MLX5_ST_SZ_DW(destroy_rq_out)] = {0};
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));
102 EXPORT_SYMBOL(mlx5_core_destroy_rq);
104 int mlx5_core_query_rq(struct mlx5_core_dev *dev, u32 rqn, u32 *out)
106 u32 in[MLX5_ST_SZ_DW(query_rq_in)] = {0};
107 int outlen = MLX5_ST_SZ_BYTES(query_rq_out);
109 MLX5_SET(query_rq_in, in, opcode, MLX5_CMD_OP_QUERY_RQ);
110 MLX5_SET(query_rq_in, in, rqn, rqn);
112 return mlx5_cmd_exec(dev, in, sizeof(in), out, outlen);
114 EXPORT_SYMBOL(mlx5_core_query_rq);
116 int mlx5_core_create_sq(struct mlx5_core_dev *dev, u32 *in, int inlen, u32 *sqn)
118 u32 out[MLX5_ST_SZ_DW(create_sq_out)] = {0};
121 MLX5_SET(create_sq_in, in, opcode, MLX5_CMD_OP_CREATE_SQ);
122 err = mlx5_cmd_exec(dev, in, inlen, out, sizeof(out));
124 *sqn = MLX5_GET(create_sq_out, out, sqn);
129 int mlx5_core_modify_sq(struct mlx5_core_dev *dev, u32 sqn, u32 *in, int inlen)
131 u32 out[MLX5_ST_SZ_DW(modify_sq_out)] = {0};
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));
137 EXPORT_SYMBOL(mlx5_core_modify_sq);
139 void mlx5_core_destroy_sq(struct mlx5_core_dev *dev, u32 sqn)
141 u32 in[MLX5_ST_SZ_DW(destroy_sq_in)] = {0};
142 u32 out[MLX5_ST_SZ_DW(destroy_sq_out)] = {0};
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));
149 int mlx5_core_query_sq(struct mlx5_core_dev *dev, u32 sqn, u32 *out)
151 u32 in[MLX5_ST_SZ_DW(query_sq_in)] = {0};
152 int outlen = MLX5_ST_SZ_BYTES(query_sq_out);
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);
158 EXPORT_SYMBOL(mlx5_core_query_sq);
160 int mlx5_core_query_sq_state(struct mlx5_core_dev *dev, u32 sqn, u8 *state)
167 inlen = MLX5_ST_SZ_BYTES(query_sq_out);
168 out = kvzalloc(inlen, GFP_KERNEL);
172 err = mlx5_core_query_sq(dev, sqn, out);
176 sqc = MLX5_ADDR_OF(query_sq_out, out, sq_context);
177 *state = MLX5_GET(sqc, sqc, state);
183 EXPORT_SYMBOL_GPL(mlx5_core_query_sq_state);
185 int mlx5_core_create_tir(struct mlx5_core_dev *dev, u32 *in, int inlen,
188 u32 out[MLX5_ST_SZ_DW(create_tir_out)] = {0};
191 MLX5_SET(create_tir_in, in, opcode, MLX5_CMD_OP_CREATE_TIR);
193 memset(out, 0, sizeof(out));
194 err = mlx5_cmd_exec(dev, in, inlen, out, sizeof(out));
196 *tirn = MLX5_GET(create_tir_out, out, tirn);
200 EXPORT_SYMBOL(mlx5_core_create_tir);
202 int mlx5_core_modify_tir(struct mlx5_core_dev *dev, u32 tirn, u32 *in,
205 u32 out[MLX5_ST_SZ_DW(modify_tir_out)] = {0};
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));
212 void mlx5_core_destroy_tir(struct mlx5_core_dev *dev, u32 tirn)
214 u32 in[MLX5_ST_SZ_DW(destroy_tir_in)] = {0};
215 u32 out[MLX5_ST_SZ_DW(destroy_tir_out)] = {0};
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));
221 EXPORT_SYMBOL(mlx5_core_destroy_tir);
223 int mlx5_core_create_tis(struct mlx5_core_dev *dev, u32 *in, int inlen,
226 u32 out[MLX5_ST_SZ_DW(create_tis_out)] = {0};
229 MLX5_SET(create_tis_in, in, opcode, MLX5_CMD_OP_CREATE_TIS);
230 err = mlx5_cmd_exec(dev, in, inlen, out, sizeof(out));
232 *tisn = MLX5_GET(create_tis_out, out, tisn);
236 EXPORT_SYMBOL(mlx5_core_create_tis);
238 int mlx5_core_modify_tis(struct mlx5_core_dev *dev, u32 tisn, u32 *in,
241 u32 out[MLX5_ST_SZ_DW(modify_tis_out)] = {0};
243 MLX5_SET(modify_tis_in, in, tisn, tisn);
244 MLX5_SET(modify_tis_in, in, opcode, MLX5_CMD_OP_MODIFY_TIS);
246 return mlx5_cmd_exec(dev, in, inlen, out, sizeof(out));
248 EXPORT_SYMBOL(mlx5_core_modify_tis);
250 void mlx5_core_destroy_tis(struct mlx5_core_dev *dev, u32 tisn)
252 u32 in[MLX5_ST_SZ_DW(destroy_tis_in)] = {0};
253 u32 out[MLX5_ST_SZ_DW(destroy_tis_out)] = {0};
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));
259 EXPORT_SYMBOL(mlx5_core_destroy_tis);
261 int mlx5_core_create_rqt(struct mlx5_core_dev *dev, u32 *in, int inlen,
264 u32 out[MLX5_ST_SZ_DW(create_rqt_out)] = {0};
267 MLX5_SET(create_rqt_in, in, opcode, MLX5_CMD_OP_CREATE_RQT);
268 err = mlx5_cmd_exec(dev, in, inlen, out, sizeof(out));
270 *rqtn = MLX5_GET(create_rqt_out, out, rqtn);
274 EXPORT_SYMBOL(mlx5_core_create_rqt);
276 int mlx5_core_modify_rqt(struct mlx5_core_dev *dev, u32 rqtn, u32 *in,
279 u32 out[MLX5_ST_SZ_DW(modify_rqt_out)] = {0};
281 MLX5_SET(modify_rqt_in, in, rqtn, rqtn);
282 MLX5_SET(modify_rqt_in, in, opcode, MLX5_CMD_OP_MODIFY_RQT);
283 return mlx5_cmd_exec(dev, in, inlen, out, sizeof(out));
286 void mlx5_core_destroy_rqt(struct mlx5_core_dev *dev, u32 rqtn)
288 u32 in[MLX5_ST_SZ_DW(destroy_rqt_in)] = {0};
289 u32 out[MLX5_ST_SZ_DW(destroy_rqt_out)] = {0};
291 MLX5_SET(destroy_rqt_in, in, opcode, MLX5_CMD_OP_DESTROY_RQT);
292 MLX5_SET(destroy_rqt_in, in, rqtn, rqtn);
293 mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out));
295 EXPORT_SYMBOL(mlx5_core_destroy_rqt);
297 static int mlx5_hairpin_create_rq(struct mlx5_core_dev *mdev,
298 struct mlx5_hairpin_params *params, u32 *rqn)
300 u32 in[MLX5_ST_SZ_DW(create_rq_in)] = {0};
303 rqc = MLX5_ADDR_OF(create_rq_in, in, ctx);
304 wq = MLX5_ADDR_OF(rqc, rqc, wq);
306 MLX5_SET(rqc, rqc, hairpin, 1);
307 MLX5_SET(rqc, rqc, state, MLX5_RQC_STATE_RST);
308 MLX5_SET(rqc, rqc, counter_set_id, params->q_counter);
310 MLX5_SET(wq, wq, log_hairpin_data_sz, params->log_data_size);
311 MLX5_SET(wq, wq, log_hairpin_num_packets, params->log_num_packets);
313 return mlx5_core_create_rq(mdev, in, MLX5_ST_SZ_BYTES(create_rq_in), rqn);
316 static int mlx5_hairpin_create_sq(struct mlx5_core_dev *mdev,
317 struct mlx5_hairpin_params *params, u32 *sqn)
319 u32 in[MLX5_ST_SZ_DW(create_sq_in)] = {0};
322 sqc = MLX5_ADDR_OF(create_sq_in, in, ctx);
323 wq = MLX5_ADDR_OF(sqc, sqc, wq);
325 MLX5_SET(sqc, sqc, hairpin, 1);
326 MLX5_SET(sqc, sqc, state, MLX5_SQC_STATE_RST);
328 MLX5_SET(wq, wq, log_hairpin_data_sz, params->log_data_size);
329 MLX5_SET(wq, wq, log_hairpin_num_packets, params->log_num_packets);
331 return mlx5_core_create_sq(mdev, in, MLX5_ST_SZ_BYTES(create_sq_in), sqn);
334 static int mlx5_hairpin_create_queues(struct mlx5_hairpin *hp,
335 struct mlx5_hairpin_params *params)
339 for (i = 0; i < hp->num_channels; i++) {
340 err = mlx5_hairpin_create_rq(hp->func_mdev, params, &hp->rqn[i]);
345 for (i = 0; i < hp->num_channels; i++) {
346 err = mlx5_hairpin_create_sq(hp->peer_mdev, params, &hp->sqn[i]);
354 for (j = 0; j < i; j++)
355 mlx5_core_destroy_sq(hp->peer_mdev, hp->sqn[j]);
356 i = hp->num_channels;
358 for (j = 0; j < i; j++)
359 mlx5_core_destroy_rq(hp->func_mdev, hp->rqn[j]);
363 static void mlx5_hairpin_destroy_queues(struct mlx5_hairpin *hp)
367 for (i = 0; i < hp->num_channels; i++) {
368 mlx5_core_destroy_rq(hp->func_mdev, hp->rqn[i]);
370 mlx5_core_destroy_sq(hp->peer_mdev, hp->sqn[i]);
374 static int mlx5_hairpin_modify_rq(struct mlx5_core_dev *func_mdev, u32 rqn,
375 int curr_state, int next_state,
376 u16 peer_vhca, u32 peer_sq)
378 u32 in[MLX5_ST_SZ_DW(modify_rq_in)] = {0};
381 rqc = MLX5_ADDR_OF(modify_rq_in, in, ctx);
383 if (next_state == MLX5_RQC_STATE_RDY) {
384 MLX5_SET(rqc, rqc, hairpin_peer_sq, peer_sq);
385 MLX5_SET(rqc, rqc, hairpin_peer_vhca, peer_vhca);
388 MLX5_SET(modify_rq_in, in, rq_state, curr_state);
389 MLX5_SET(rqc, rqc, state, next_state);
391 return mlx5_core_modify_rq(func_mdev, rqn,
392 in, MLX5_ST_SZ_BYTES(modify_rq_in));
395 static int mlx5_hairpin_modify_sq(struct mlx5_core_dev *peer_mdev, u32 sqn,
396 int curr_state, int next_state,
397 u16 peer_vhca, u32 peer_rq)
399 u32 in[MLX5_ST_SZ_DW(modify_sq_in)] = {0};
402 sqc = MLX5_ADDR_OF(modify_sq_in, in, ctx);
404 if (next_state == MLX5_SQC_STATE_RDY) {
405 MLX5_SET(sqc, sqc, hairpin_peer_rq, peer_rq);
406 MLX5_SET(sqc, sqc, hairpin_peer_vhca, peer_vhca);
409 MLX5_SET(modify_sq_in, in, sq_state, curr_state);
410 MLX5_SET(sqc, sqc, state, next_state);
412 return mlx5_core_modify_sq(peer_mdev, sqn,
413 in, MLX5_ST_SZ_BYTES(modify_sq_in));
416 static int mlx5_hairpin_pair_queues(struct mlx5_hairpin *hp)
421 for (i = 0; i < hp->num_channels; i++) {
422 err = mlx5_hairpin_modify_sq(hp->peer_mdev, hp->sqn[i],
423 MLX5_SQC_STATE_RST, MLX5_SQC_STATE_RDY,
424 MLX5_CAP_GEN(hp->func_mdev, vhca_id), hp->rqn[i]);
430 for (i = 0; i < hp->num_channels; i++) {
431 err = mlx5_hairpin_modify_rq(hp->func_mdev, hp->rqn[i],
432 MLX5_RQC_STATE_RST, MLX5_RQC_STATE_RDY,
433 MLX5_CAP_GEN(hp->peer_mdev, vhca_id), hp->sqn[i]);
441 for (j = 0; j < i; j++)
442 mlx5_hairpin_modify_rq(hp->func_mdev, hp->rqn[j], MLX5_RQC_STATE_RDY,
443 MLX5_RQC_STATE_RST, 0, 0);
444 i = hp->num_channels;
446 for (j = 0; j < i; j++)
447 mlx5_hairpin_modify_sq(hp->peer_mdev, hp->sqn[j], MLX5_SQC_STATE_RDY,
448 MLX5_SQC_STATE_RST, 0, 0);
452 static void mlx5_hairpin_unpair_queues(struct mlx5_hairpin *hp)
457 for (i = 0; i < hp->num_channels; i++)
458 mlx5_hairpin_modify_rq(hp->func_mdev, hp->rqn[i], MLX5_RQC_STATE_RDY,
459 MLX5_RQC_STATE_RST, 0, 0);
464 for (i = 0; i < hp->num_channels; i++)
465 mlx5_hairpin_modify_sq(hp->peer_mdev, hp->sqn[i], MLX5_SQC_STATE_RDY,
466 MLX5_SQC_STATE_RST, 0, 0);
469 struct mlx5_hairpin *
470 mlx5_core_hairpin_create(struct mlx5_core_dev *func_mdev,
471 struct mlx5_core_dev *peer_mdev,
472 struct mlx5_hairpin_params *params)
474 struct mlx5_hairpin *hp;
477 size = sizeof(*hp) + params->num_channels * 2 * sizeof(u32);
478 hp = kzalloc(size, GFP_KERNEL);
480 return ERR_PTR(-ENOMEM);
482 hp->func_mdev = func_mdev;
483 hp->peer_mdev = peer_mdev;
484 hp->num_channels = params->num_channels;
486 hp->rqn = (void *)hp + sizeof(*hp);
487 hp->sqn = hp->rqn + params->num_channels;
489 /* alloc and pair func --> peer hairpin */
490 err = mlx5_hairpin_create_queues(hp, params);
492 goto err_create_queues;
494 err = mlx5_hairpin_pair_queues(hp);
496 goto err_pair_queues;
501 mlx5_hairpin_destroy_queues(hp);
507 void mlx5_core_hairpin_destroy(struct mlx5_hairpin *hp)
509 mlx5_hairpin_unpair_queues(hp);
510 mlx5_hairpin_destroy_queues(hp);