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 {
41211e6c80SMajd Dibbiny 	u32 in[MLX5_ST_SZ_DW(query_adapter_in)];
42211e6c80SMajd Dibbiny 
43211e6c80SMajd Dibbiny 	memset(in, 0, sizeof(in));
44211e6c80SMajd Dibbiny 
45211e6c80SMajd Dibbiny 	MLX5_SET(query_adapter_in, in, opcode, MLX5_CMD_OP_QUERY_ADAPTER);
46211e6c80SMajd Dibbiny 
47211e6c80SMajd Dibbiny 	return mlx5_cmd_exec_check_status(dev, in, sizeof(in), out, outlen);
48211e6c80SMajd Dibbiny }
49211e6c80SMajd Dibbiny 
50211e6c80SMajd Dibbiny int mlx5_query_board_id(struct mlx5_core_dev *dev)
51211e6c80SMajd Dibbiny {
52211e6c80SMajd Dibbiny 	u32 *out;
53211e6c80SMajd Dibbiny 	int outlen = MLX5_ST_SZ_BYTES(query_adapter_out);
54e126ba97SEli Cohen 	int err;
55e126ba97SEli Cohen 
56211e6c80SMajd Dibbiny 	out = kzalloc(outlen, GFP_KERNEL);
57e126ba97SEli Cohen 	if (!out)
58e126ba97SEli Cohen 		return -ENOMEM;
59e126ba97SEli Cohen 
60211e6c80SMajd Dibbiny 	err = mlx5_cmd_query_adapter(dev, out, outlen);
61e126ba97SEli Cohen 	if (err)
62211e6c80SMajd Dibbiny 		goto out;
63e126ba97SEli Cohen 
64211e6c80SMajd Dibbiny 	memcpy(dev->board_id,
65211e6c80SMajd Dibbiny 	       MLX5_ADDR_OF(query_adapter_out, out,
66211e6c80SMajd Dibbiny 			    query_adapter_struct.vsd_contd_psid),
67211e6c80SMajd Dibbiny 	       MLX5_FLD_SZ_BYTES(query_adapter_out,
68211e6c80SMajd Dibbiny 				 query_adapter_struct.vsd_contd_psid));
69e126ba97SEli Cohen 
70211e6c80SMajd Dibbiny out:
71e126ba97SEli Cohen 	kfree(out);
72e126ba97SEli Cohen 	return err;
73e126ba97SEli Cohen }
74e126ba97SEli Cohen 
75211e6c80SMajd Dibbiny int mlx5_core_query_vendor_id(struct mlx5_core_dev *mdev, u32 *vendor_id)
76211e6c80SMajd Dibbiny {
77211e6c80SMajd Dibbiny 	u32 *out;
78211e6c80SMajd Dibbiny 	int outlen = MLX5_ST_SZ_BYTES(query_adapter_out);
79211e6c80SMajd Dibbiny 	int err;
80211e6c80SMajd Dibbiny 
81211e6c80SMajd Dibbiny 	out = kzalloc(outlen, GFP_KERNEL);
82211e6c80SMajd Dibbiny 	if (!out)
83211e6c80SMajd Dibbiny 		return -ENOMEM;
84211e6c80SMajd Dibbiny 
85211e6c80SMajd Dibbiny 	err = mlx5_cmd_query_adapter(mdev, out, outlen);
86211e6c80SMajd Dibbiny 	if (err)
87211e6c80SMajd Dibbiny 		goto out;
88211e6c80SMajd Dibbiny 
89211e6c80SMajd Dibbiny 	*vendor_id = MLX5_GET(query_adapter_out, out,
90211e6c80SMajd Dibbiny 			      query_adapter_struct.ieee_vendor_id);
91211e6c80SMajd Dibbiny out:
92211e6c80SMajd Dibbiny 	kfree(out);
93211e6c80SMajd Dibbiny 	return err;
94211e6c80SMajd Dibbiny }
95211e6c80SMajd Dibbiny EXPORT_SYMBOL(mlx5_core_query_vendor_id);
96211e6c80SMajd Dibbiny 
97938fe83cSSaeed Mahameed int mlx5_query_hca_caps(struct mlx5_core_dev *dev)
98e126ba97SEli Cohen {
99e420f0c0SHaggai Eran 	int err;
100e420f0c0SHaggai Eran 
101938fe83cSSaeed Mahameed 	err = mlx5_core_get_caps(dev, MLX5_CAP_GENERAL, HCA_CAP_OPMOD_GET_CUR);
102e420f0c0SHaggai Eran 	if (err)
103938fe83cSSaeed Mahameed 		return err;
104e420f0c0SHaggai Eran 
105938fe83cSSaeed Mahameed 	err = mlx5_core_get_caps(dev, MLX5_CAP_GENERAL, HCA_CAP_OPMOD_GET_MAX);
106938fe83cSSaeed Mahameed 	if (err)
107938fe83cSSaeed Mahameed 		return err;
108e420f0c0SHaggai Eran 
109938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(dev, eth_net_offloads)) {
110938fe83cSSaeed Mahameed 		err = mlx5_core_get_caps(dev, MLX5_CAP_ETHERNET_OFFLOADS,
111938fe83cSSaeed Mahameed 					 HCA_CAP_OPMOD_GET_CUR);
112938fe83cSSaeed Mahameed 		if (err)
113938fe83cSSaeed Mahameed 			return err;
114938fe83cSSaeed Mahameed 		err = mlx5_core_get_caps(dev, MLX5_CAP_ETHERNET_OFFLOADS,
115938fe83cSSaeed Mahameed 					 HCA_CAP_OPMOD_GET_MAX);
116938fe83cSSaeed Mahameed 		if (err)
117e420f0c0SHaggai Eran 			return err;
118e420f0c0SHaggai Eran 	}
119938fe83cSSaeed Mahameed 
120938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(dev, pg)) {
121938fe83cSSaeed Mahameed 		err = mlx5_core_get_caps(dev, MLX5_CAP_ODP,
122938fe83cSSaeed Mahameed 					 HCA_CAP_OPMOD_GET_CUR);
123938fe83cSSaeed Mahameed 		if (err)
124938fe83cSSaeed Mahameed 			return err;
125938fe83cSSaeed Mahameed 		err = mlx5_core_get_caps(dev, MLX5_CAP_ODP,
126938fe83cSSaeed Mahameed 					 HCA_CAP_OPMOD_GET_MAX);
127938fe83cSSaeed Mahameed 		if (err)
128938fe83cSSaeed Mahameed 			return err;
129938fe83cSSaeed Mahameed 	}
130938fe83cSSaeed Mahameed 
131938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(dev, atomic)) {
132938fe83cSSaeed Mahameed 		err = mlx5_core_get_caps(dev, MLX5_CAP_ATOMIC,
133938fe83cSSaeed Mahameed 					 HCA_CAP_OPMOD_GET_CUR);
134938fe83cSSaeed Mahameed 		if (err)
135938fe83cSSaeed Mahameed 			return err;
136938fe83cSSaeed Mahameed 		err = mlx5_core_get_caps(dev, MLX5_CAP_ATOMIC,
137938fe83cSSaeed Mahameed 					 HCA_CAP_OPMOD_GET_MAX);
138938fe83cSSaeed Mahameed 		if (err)
139938fe83cSSaeed Mahameed 			return err;
140938fe83cSSaeed Mahameed 	}
141938fe83cSSaeed Mahameed 
142938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(dev, roce)) {
143938fe83cSSaeed Mahameed 		err = mlx5_core_get_caps(dev, MLX5_CAP_ROCE,
144938fe83cSSaeed Mahameed 					 HCA_CAP_OPMOD_GET_CUR);
145938fe83cSSaeed Mahameed 		if (err)
146938fe83cSSaeed Mahameed 			return err;
147938fe83cSSaeed Mahameed 		err = mlx5_core_get_caps(dev, MLX5_CAP_ROCE,
148938fe83cSSaeed Mahameed 					 HCA_CAP_OPMOD_GET_MAX);
149938fe83cSSaeed Mahameed 		if (err)
150938fe83cSSaeed Mahameed 			return err;
151938fe83cSSaeed Mahameed 	}
152938fe83cSSaeed Mahameed 
153938fe83cSSaeed Mahameed 	if (MLX5_CAP_GEN(dev, nic_flow_table)) {
154938fe83cSSaeed Mahameed 		err = mlx5_core_get_caps(dev, MLX5_CAP_FLOW_TABLE,
155938fe83cSSaeed Mahameed 					 HCA_CAP_OPMOD_GET_CUR);
156938fe83cSSaeed Mahameed 		if (err)
157938fe83cSSaeed Mahameed 			return err;
158938fe83cSSaeed Mahameed 		err = mlx5_core_get_caps(dev, MLX5_CAP_FLOW_TABLE,
159938fe83cSSaeed Mahameed 					 HCA_CAP_OPMOD_GET_MAX);
160938fe83cSSaeed Mahameed 		if (err)
161938fe83cSSaeed Mahameed 			return err;
162938fe83cSSaeed Mahameed 	}
163938fe83cSSaeed Mahameed 	return 0;
164938fe83cSSaeed Mahameed }
165e420f0c0SHaggai Eran 
166e126ba97SEli Cohen int mlx5_cmd_init_hca(struct mlx5_core_dev *dev)
167e126ba97SEli Cohen {
168e126ba97SEli Cohen 	struct mlx5_cmd_init_hca_mbox_in in;
169e126ba97SEli Cohen 	struct mlx5_cmd_init_hca_mbox_out out;
170e126ba97SEli Cohen 	int err;
171e126ba97SEli Cohen 
172e126ba97SEli Cohen 	memset(&in, 0, sizeof(in));
173e126ba97SEli Cohen 	memset(&out, 0, sizeof(out));
174e126ba97SEli Cohen 	in.hdr.opcode = cpu_to_be16(MLX5_CMD_OP_INIT_HCA);
175e126ba97SEli Cohen 	err = mlx5_cmd_exec(dev, &in, sizeof(in), &out, sizeof(out));
176e126ba97SEli Cohen 	if (err)
177e126ba97SEli Cohen 		return err;
178e126ba97SEli Cohen 
179e126ba97SEli Cohen 	if (out.hdr.status)
180e126ba97SEli Cohen 		err = mlx5_cmd_status_to_err(&out.hdr);
181e126ba97SEli Cohen 
182e126ba97SEli Cohen 	return err;
183e126ba97SEli Cohen }
184e126ba97SEli Cohen 
185e126ba97SEli Cohen int mlx5_cmd_teardown_hca(struct mlx5_core_dev *dev)
186e126ba97SEli Cohen {
187e126ba97SEli Cohen 	struct mlx5_cmd_teardown_hca_mbox_in in;
188e126ba97SEli Cohen 	struct mlx5_cmd_teardown_hca_mbox_out out;
189e126ba97SEli Cohen 	int err;
190e126ba97SEli Cohen 
191e126ba97SEli Cohen 	memset(&in, 0, sizeof(in));
192e126ba97SEli Cohen 	memset(&out, 0, sizeof(out));
193e126ba97SEli Cohen 	in.hdr.opcode = cpu_to_be16(MLX5_CMD_OP_TEARDOWN_HCA);
194e126ba97SEli Cohen 	err = mlx5_cmd_exec(dev, &in, sizeof(in), &out, sizeof(out));
195e126ba97SEli Cohen 	if (err)
196e126ba97SEli Cohen 		return err;
197e126ba97SEli Cohen 
198e126ba97SEli Cohen 	if (out.hdr.status)
199e126ba97SEli Cohen 		err = mlx5_cmd_status_to_err(&out.hdr);
200e126ba97SEli Cohen 
201e126ba97SEli Cohen 	return err;
202e126ba97SEli Cohen }
203