1e126ba97SEli Cohen /*
2302bdf68SSaeed Mahameed  * Copyright (c) 2013-2015, Mellanox Technologies. All rights reserved.
3e126ba97SEli Cohen  *
4e126ba97SEli Cohen  * This software is available to you under a choice of one of two
5e126ba97SEli Cohen  * licenses.  You may choose to be licensed under the terms of the GNU
6e126ba97SEli Cohen  * General Public License (GPL) Version 2, available from the file
7e126ba97SEli Cohen  * COPYING in the main directory of this source tree, or the
8e126ba97SEli Cohen  * OpenIB.org BSD license below:
9e126ba97SEli Cohen  *
10e126ba97SEli Cohen  *     Redistribution and use in source and binary forms, with or
11e126ba97SEli Cohen  *     without modification, are permitted provided that the following
12e126ba97SEli Cohen  *     conditions are met:
13e126ba97SEli Cohen  *
14e126ba97SEli Cohen  *      - Redistributions of source code must retain the above
15e126ba97SEli Cohen  *        copyright notice, this list of conditions and the following
16e126ba97SEli Cohen  *        disclaimer.
17e126ba97SEli Cohen  *
18e126ba97SEli Cohen  *      - Redistributions in binary form must reproduce the above
19e126ba97SEli Cohen  *        copyright notice, this list of conditions and the following
20e126ba97SEli Cohen  *        disclaimer in the documentation and/or other materials
21e126ba97SEli Cohen  *        provided with the distribution.
22e126ba97SEli Cohen  *
23e126ba97SEli Cohen  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24e126ba97SEli Cohen  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25e126ba97SEli Cohen  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26e126ba97SEli Cohen  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27e126ba97SEli Cohen  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28e126ba97SEli Cohen  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29e126ba97SEli Cohen  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30e126ba97SEli Cohen  * SOFTWARE.
31e126ba97SEli Cohen  */
32e126ba97SEli Cohen 
33e126ba97SEli Cohen #include <linux/mlx5/driver.h>
34e126ba97SEli Cohen #include <linux/mlx5/cmd.h>
35e126ba97SEli Cohen #include <linux/module.h>
36e126ba97SEli Cohen #include "mlx5_core.h"
37e126ba97SEli Cohen 
387cf7fa52SMajd Dibbiny static int mlx5_cmd_query_adapter(struct mlx5_core_dev *dev, u32 *out,
397cf7fa52SMajd Dibbiny 				  int outlen)
40e126ba97SEli Cohen {
41c4f287c4SSaeed Mahameed 	u32 in[MLX5_ST_SZ_DW(query_adapter_in)] = {0};
42211e6c80SMajd Dibbiny 
43211e6c80SMajd Dibbiny 	MLX5_SET(query_adapter_in, in, opcode, MLX5_CMD_OP_QUERY_ADAPTER);
44c4f287c4SSaeed Mahameed 	return mlx5_cmd_exec(dev, in, sizeof(in), out, outlen);
45211e6c80SMajd Dibbiny }
46211e6c80SMajd Dibbiny 
47211e6c80SMajd Dibbiny int mlx5_query_board_id(struct mlx5_core_dev *dev)
48211e6c80SMajd Dibbiny {
49211e6c80SMajd Dibbiny 	u32 *out;
50211e6c80SMajd Dibbiny 	int outlen = MLX5_ST_SZ_BYTES(query_adapter_out);
51e126ba97SEli Cohen 	int err;
52e126ba97SEli Cohen 
53211e6c80SMajd Dibbiny 	out = kzalloc(outlen, GFP_KERNEL);
54e126ba97SEli Cohen 	if (!out)
55e126ba97SEli Cohen 		return -ENOMEM;
56e126ba97SEli Cohen 
57211e6c80SMajd Dibbiny 	err = mlx5_cmd_query_adapter(dev, out, outlen);
58e126ba97SEli Cohen 	if (err)
59211e6c80SMajd Dibbiny 		goto out;
60e126ba97SEli Cohen 
61211e6c80SMajd Dibbiny 	memcpy(dev->board_id,
62211e6c80SMajd Dibbiny 	       MLX5_ADDR_OF(query_adapter_out, out,
63211e6c80SMajd Dibbiny 			    query_adapter_struct.vsd_contd_psid),
64211e6c80SMajd Dibbiny 	       MLX5_FLD_SZ_BYTES(query_adapter_out,
65211e6c80SMajd Dibbiny 				 query_adapter_struct.vsd_contd_psid));
66e126ba97SEli Cohen 
67211e6c80SMajd Dibbiny out:
68e126ba97SEli Cohen 	kfree(out);
69e126ba97SEli Cohen 	return err;
70e126ba97SEli Cohen }
71e126ba97SEli Cohen 
72211e6c80SMajd Dibbiny int mlx5_core_query_vendor_id(struct mlx5_core_dev *mdev, u32 *vendor_id)
73211e6c80SMajd Dibbiny {
74211e6c80SMajd Dibbiny 	u32 *out;
75211e6c80SMajd Dibbiny 	int outlen = MLX5_ST_SZ_BYTES(query_adapter_out);
76211e6c80SMajd Dibbiny 	int err;
77211e6c80SMajd Dibbiny 
78211e6c80SMajd Dibbiny 	out = kzalloc(outlen, GFP_KERNEL);
79211e6c80SMajd Dibbiny 	if (!out)
80211e6c80SMajd Dibbiny 		return -ENOMEM;
81211e6c80SMajd Dibbiny 
82211e6c80SMajd Dibbiny 	err = mlx5_cmd_query_adapter(mdev, out, outlen);
83211e6c80SMajd Dibbiny 	if (err)
84211e6c80SMajd Dibbiny 		goto out;
85211e6c80SMajd Dibbiny 
86211e6c80SMajd Dibbiny 	*vendor_id = MLX5_GET(query_adapter_out, out,
87211e6c80SMajd Dibbiny 			      query_adapter_struct.ieee_vendor_id);
88211e6c80SMajd Dibbiny out:
89211e6c80SMajd Dibbiny 	kfree(out);
90211e6c80SMajd Dibbiny 	return err;
91211e6c80SMajd Dibbiny }
92211e6c80SMajd Dibbiny EXPORT_SYMBOL(mlx5_core_query_vendor_id);
93211e6c80SMajd Dibbiny 
94938fe83cSSaeed Mahameed int mlx5_query_hca_caps(struct mlx5_core_dev *dev)
95e126ba97SEli Cohen {
96e420f0c0SHaggai Eran 	int err;
97e420f0c0SHaggai Eran 
98b06e7de8SLeon Romanovsky 	err = mlx5_core_get_caps(dev, MLX5_CAP_GENERAL);
99938fe83cSSaeed Mahameed 	if (err)
100938fe83cSSaeed Mahameed 		return err;
101e420f0c0SHaggai Eran 
102938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(dev, eth_net_offloads)) {
103b06e7de8SLeon Romanovsky 		err = mlx5_core_get_caps(dev, MLX5_CAP_ETHERNET_OFFLOADS);
104938fe83cSSaeed Mahameed 		if (err)
105e420f0c0SHaggai Eran 			return err;
106e420f0c0SHaggai Eran 	}
107938fe83cSSaeed Mahameed 
108938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(dev, pg)) {
109b06e7de8SLeon Romanovsky 		err = mlx5_core_get_caps(dev, MLX5_CAP_ODP);
110938fe83cSSaeed Mahameed 		if (err)
111938fe83cSSaeed Mahameed 			return err;
112938fe83cSSaeed Mahameed 	}
113938fe83cSSaeed Mahameed 
114938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(dev, atomic)) {
115b06e7de8SLeon Romanovsky 		err = mlx5_core_get_caps(dev, MLX5_CAP_ATOMIC);
116938fe83cSSaeed Mahameed 		if (err)
117938fe83cSSaeed Mahameed 			return err;
118938fe83cSSaeed Mahameed 	}
119938fe83cSSaeed Mahameed 
120938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(dev, roce)) {
121b06e7de8SLeon Romanovsky 		err = mlx5_core_get_caps(dev, MLX5_CAP_ROCE);
122938fe83cSSaeed Mahameed 		if (err)
123938fe83cSSaeed Mahameed 			return err;
124938fe83cSSaeed Mahameed 	}
125938fe83cSSaeed Mahameed 
126938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(dev, nic_flow_table)) {
127b06e7de8SLeon Romanovsky 		err = mlx5_core_get_caps(dev, MLX5_CAP_FLOW_TABLE);
128938fe83cSSaeed Mahameed 		if (err)
129938fe83cSSaeed Mahameed 			return err;
130938fe83cSSaeed Mahameed 	}
131495716b1SSaeed Mahameed 
132495716b1SSaeed Mahameed 	if (MLX5_CAP_GEN(dev, vport_group_manager) &&
133495716b1SSaeed Mahameed 	    MLX5_CAP_GEN(dev, eswitch_flow_table)) {
134b06e7de8SLeon Romanovsky 		err = mlx5_core_get_caps(dev, MLX5_CAP_ESWITCH_FLOW_TABLE);
135495716b1SSaeed Mahameed 		if (err)
136495716b1SSaeed Mahameed 			return err;
137495716b1SSaeed Mahameed 	}
138495716b1SSaeed Mahameed 
1399bd0a185SSaeed Mahameed 	if (MLX5_CAP_GEN(dev, eswitch_flow_table)) {
140b06e7de8SLeon Romanovsky 		err = mlx5_core_get_caps(dev, MLX5_CAP_ESWITCH);
141d6666753SSaeed Mahameed 		if (err)
142d6666753SSaeed Mahameed 			return err;
143d6666753SSaeed Mahameed 	}
144d6666753SSaeed Mahameed 
1453f0393a5SSagi Grimberg 	if (MLX5_CAP_GEN(dev, vector_calc)) {
1463f0393a5SSagi Grimberg 		err = mlx5_core_get_caps(dev, MLX5_CAP_VECTOR_CALC);
1473f0393a5SSagi Grimberg 		if (err)
1483f0393a5SSagi Grimberg 			return err;
1493f0393a5SSagi Grimberg 	}
1503f0393a5SSagi Grimberg 
1511466cc5bSYevgeny Petrilin 	if (MLX5_CAP_GEN(dev, qos)) {
1521466cc5bSYevgeny Petrilin 		err = mlx5_core_get_caps(dev, MLX5_CAP_QOS);
1531466cc5bSYevgeny Petrilin 		if (err)
1541466cc5bSYevgeny Petrilin 			return err;
1551466cc5bSYevgeny Petrilin 	}
1561466cc5bSYevgeny Petrilin 
157938fe83cSSaeed Mahameed 	return 0;
158938fe83cSSaeed Mahameed }
159e420f0c0SHaggai Eran 
160e126ba97SEli Cohen int mlx5_cmd_init_hca(struct mlx5_core_dev *dev)
161e126ba97SEli Cohen {
16204ed5ad5SSaeed Mahameed 	u32 out[MLX5_ST_SZ_DW(init_hca_out)] = {0};
16304ed5ad5SSaeed Mahameed 	u32 in[MLX5_ST_SZ_DW(init_hca_in)]   = {0};
164e126ba97SEli Cohen 
16504ed5ad5SSaeed Mahameed 	MLX5_SET(init_hca_in, in, opcode, MLX5_CMD_OP_INIT_HCA);
166c4f287c4SSaeed Mahameed 	return mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out));
167e126ba97SEli Cohen }
168e126ba97SEli Cohen 
169e126ba97SEli Cohen int mlx5_cmd_teardown_hca(struct mlx5_core_dev *dev)
170e126ba97SEli Cohen {
17104ed5ad5SSaeed Mahameed 	u32 out[MLX5_ST_SZ_DW(teardown_hca_out)] = {0};
17204ed5ad5SSaeed Mahameed 	u32 in[MLX5_ST_SZ_DW(teardown_hca_in)]   = {0};
173e126ba97SEli Cohen 
17404ed5ad5SSaeed Mahameed 	MLX5_SET(teardown_hca_in, in, opcode, MLX5_CMD_OP_TEARDOWN_HCA);
175c4f287c4SSaeed Mahameed 	return mlx5_cmd_exec(dev, in, sizeof(in), out, sizeof(out));
176e126ba97SEli Cohen }
177