1 /*******************************************************************************
2 *
3 * Copyright (c) 2015-2016 Intel Corporation.  All rights reserved.
4 *
5 * This software is available to you under a choice of one of two
6 * licenses.  You may choose to be licensed under the terms of the GNU
7 * General Public License (GPL) Version 2, available from the file
8 * COPYING in the main directory of this source tree, or the
9 * OpenFabrics.org BSD license below:
10 *
11 *   Redistribution and use in source and binary forms, with or
12 *   without modification, are permitted provided that the following
13 *   conditions are met:
14 *
15 *    - Redistributions of source code must retain the above
16 *	copyright notice, this list of conditions and the following
17 *	disclaimer.
18 *
19 *    - Redistributions in binary form must reproduce the above
20 *	copyright notice, this list of conditions and the following
21 *	disclaimer in the documentation and/or other materials
22 *	provided with the distribution.
23 *
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
29 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31 * SOFTWARE.
32 *
33 *******************************************************************************/
34 
35 #include "i40iw_osdep.h"
36 #include "i40iw_register.h"
37 #include "i40iw_status.h"
38 #include "i40iw_hmc.h"
39 #include "i40iw_d.h"
40 #include "i40iw_type.h"
41 #include "i40iw_p.h"
42 #include "i40iw_virtchnl.h"
43 
44 /**
45  * vchnl_vf_send_get_ver_req - Request Channel version
46  * @dev: IWARP device pointer
47  * @vchnl_req: Virtual channel message request pointer
48  */
vchnl_vf_send_get_ver_req(struct i40iw_sc_dev * dev,struct i40iw_virtchnl_req * vchnl_req)49 static enum i40iw_status_code vchnl_vf_send_get_ver_req(struct i40iw_sc_dev *dev,
50 							struct i40iw_virtchnl_req *vchnl_req)
51 {
52 	enum i40iw_status_code ret_code = I40IW_ERR_NOT_READY;
53 	struct i40iw_virtchnl_op_buf *vchnl_msg = vchnl_req->vchnl_msg;
54 
55 	if (!dev->vchnl_up)
56 		return ret_code;
57 
58 	memset(vchnl_msg, 0, sizeof(*vchnl_msg));
59 	vchnl_msg->iw_chnl_op_ctx = (uintptr_t)vchnl_req;
60 	vchnl_msg->iw_chnl_buf_len = sizeof(*vchnl_msg);
61 	vchnl_msg->iw_op_code = I40IW_VCHNL_OP_GET_VER;
62 	vchnl_msg->iw_op_ver = I40IW_VCHNL_OP_GET_VER_V0;
63 	ret_code = dev->vchnl_if.vchnl_send(dev, 0, (u8 *)vchnl_msg, vchnl_msg->iw_chnl_buf_len);
64 	if (ret_code)
65 		i40iw_debug(dev, I40IW_DEBUG_VIRT,
66 			    "%s: virt channel send failed 0x%x\n", __func__, ret_code);
67 	return ret_code;
68 }
69 
70 /**
71  * vchnl_vf_send_get_hmc_fcn_req - Request HMC Function from VF
72  * @dev: IWARP device pointer
73  * @vchnl_req: Virtual channel message request pointer
74  */
vchnl_vf_send_get_hmc_fcn_req(struct i40iw_sc_dev * dev,struct i40iw_virtchnl_req * vchnl_req)75 static enum i40iw_status_code vchnl_vf_send_get_hmc_fcn_req(struct i40iw_sc_dev *dev,
76 							    struct i40iw_virtchnl_req *vchnl_req)
77 {
78 	enum i40iw_status_code ret_code = I40IW_ERR_NOT_READY;
79 	struct i40iw_virtchnl_op_buf *vchnl_msg = vchnl_req->vchnl_msg;
80 
81 	if (!dev->vchnl_up)
82 		return ret_code;
83 
84 	memset(vchnl_msg, 0, sizeof(*vchnl_msg));
85 	vchnl_msg->iw_chnl_op_ctx = (uintptr_t)vchnl_req;
86 	vchnl_msg->iw_chnl_buf_len = sizeof(*vchnl_msg);
87 	vchnl_msg->iw_op_code = I40IW_VCHNL_OP_GET_HMC_FCN;
88 	vchnl_msg->iw_op_ver = I40IW_VCHNL_OP_GET_HMC_FCN_V0;
89 	ret_code = dev->vchnl_if.vchnl_send(dev, 0, (u8 *)vchnl_msg, vchnl_msg->iw_chnl_buf_len);
90 	if (ret_code)
91 		i40iw_debug(dev, I40IW_DEBUG_VIRT,
92 			    "%s: virt channel send failed 0x%x\n", __func__, ret_code);
93 	return ret_code;
94 }
95 
96 /**
97  * vchnl_vf_send_get_pe_stats_req - Request PE stats from VF
98  * @dev: IWARP device pointer
99  * @vchnl_req: Virtual channel message request pointer
100  */
vchnl_vf_send_get_pe_stats_req(struct i40iw_sc_dev * dev,struct i40iw_virtchnl_req * vchnl_req)101 static enum i40iw_status_code vchnl_vf_send_get_pe_stats_req(struct i40iw_sc_dev *dev,
102 							     struct i40iw_virtchnl_req  *vchnl_req)
103 {
104 	enum i40iw_status_code ret_code = I40IW_ERR_NOT_READY;
105 	struct i40iw_virtchnl_op_buf *vchnl_msg = vchnl_req->vchnl_msg;
106 
107 	if (!dev->vchnl_up)
108 		return ret_code;
109 
110 	memset(vchnl_msg, 0, sizeof(*vchnl_msg));
111 	vchnl_msg->iw_chnl_op_ctx = (uintptr_t)vchnl_req;
112 	vchnl_msg->iw_chnl_buf_len = sizeof(*vchnl_msg) + sizeof(struct i40iw_dev_hw_stats) - 1;
113 	vchnl_msg->iw_op_code = I40IW_VCHNL_OP_GET_STATS;
114 	vchnl_msg->iw_op_ver = I40IW_VCHNL_OP_GET_STATS_V0;
115 	ret_code = dev->vchnl_if.vchnl_send(dev, 0, (u8 *)vchnl_msg, vchnl_msg->iw_chnl_buf_len);
116 	if (ret_code)
117 		i40iw_debug(dev, I40IW_DEBUG_VIRT,
118 			    "%s: virt channel send failed 0x%x\n", __func__, ret_code);
119 	return ret_code;
120 }
121 
122 /*
123  * vchnl_vf_send_add_hmc_objs_req - Add HMC objects
124  * @dev: IWARP device pointer
125  * @vchnl_req: Virtual channel message request pointer
126  */
vchnl_vf_send_add_hmc_objs_req(struct i40iw_sc_dev * dev,struct i40iw_virtchnl_req * vchnl_req,enum i40iw_hmc_rsrc_type rsrc_type,u32 start_index,u32 rsrc_count)127 static enum i40iw_status_code vchnl_vf_send_add_hmc_objs_req(struct i40iw_sc_dev *dev,
128 							     struct i40iw_virtchnl_req *vchnl_req,
129 							     enum i40iw_hmc_rsrc_type rsrc_type,
130 							     u32 start_index,
131 							     u32 rsrc_count)
132 {
133 	enum i40iw_status_code ret_code = I40IW_ERR_NOT_READY;
134 	struct i40iw_virtchnl_op_buf *vchnl_msg = vchnl_req->vchnl_msg;
135 	struct i40iw_virtchnl_hmc_obj_range *add_hmc_obj;
136 
137 	if (!dev->vchnl_up)
138 		return ret_code;
139 
140 	add_hmc_obj = (struct i40iw_virtchnl_hmc_obj_range *)vchnl_msg->iw_chnl_buf;
141 	memset(vchnl_msg, 0, sizeof(*vchnl_msg));
142 	memset(add_hmc_obj, 0, sizeof(*add_hmc_obj));
143 	vchnl_msg->iw_chnl_op_ctx = (uintptr_t)vchnl_req;
144 	vchnl_msg->iw_chnl_buf_len = sizeof(*vchnl_msg) + sizeof(struct i40iw_virtchnl_hmc_obj_range) - 1;
145 	vchnl_msg->iw_op_code = I40IW_VCHNL_OP_ADD_HMC_OBJ_RANGE;
146 	vchnl_msg->iw_op_ver = I40IW_VCHNL_OP_ADD_HMC_OBJ_RANGE_V0;
147 	add_hmc_obj->obj_type = (u16)rsrc_type;
148 	add_hmc_obj->start_index = start_index;
149 	add_hmc_obj->obj_count = rsrc_count;
150 	ret_code = dev->vchnl_if.vchnl_send(dev, 0, (u8 *)vchnl_msg, vchnl_msg->iw_chnl_buf_len);
151 	if (ret_code)
152 		i40iw_debug(dev, I40IW_DEBUG_VIRT,
153 			    "%s: virt channel send failed 0x%x\n", __func__, ret_code);
154 	return ret_code;
155 }
156 
157 /**
158  * vchnl_vf_send_del_hmc_objs_req - del HMC objects
159  * @dev: IWARP device pointer
160  * @vchnl_req: Virtual channel message request pointer
161  * @rsrc_type: resource type to delete
162  * @start_index: starting index for resource
163  * @rsrc_count: number of resource type to delete
164  */
vchnl_vf_send_del_hmc_objs_req(struct i40iw_sc_dev * dev,struct i40iw_virtchnl_req * vchnl_req,enum i40iw_hmc_rsrc_type rsrc_type,u32 start_index,u32 rsrc_count)165 static enum i40iw_status_code vchnl_vf_send_del_hmc_objs_req(struct i40iw_sc_dev *dev,
166 							     struct i40iw_virtchnl_req *vchnl_req,
167 							     enum i40iw_hmc_rsrc_type rsrc_type,
168 							     u32 start_index,
169 							     u32 rsrc_count)
170 {
171 	enum i40iw_status_code ret_code = I40IW_ERR_NOT_READY;
172 	struct i40iw_virtchnl_op_buf *vchnl_msg = vchnl_req->vchnl_msg;
173 	struct i40iw_virtchnl_hmc_obj_range *add_hmc_obj;
174 
175 	if (!dev->vchnl_up)
176 		return ret_code;
177 
178 	add_hmc_obj = (struct i40iw_virtchnl_hmc_obj_range *)vchnl_msg->iw_chnl_buf;
179 	memset(vchnl_msg, 0, sizeof(*vchnl_msg));
180 	memset(add_hmc_obj, 0, sizeof(*add_hmc_obj));
181 	vchnl_msg->iw_chnl_op_ctx = (uintptr_t)vchnl_req;
182 	vchnl_msg->iw_chnl_buf_len = sizeof(*vchnl_msg) + sizeof(struct i40iw_virtchnl_hmc_obj_range) - 1;
183 	vchnl_msg->iw_op_code = I40IW_VCHNL_OP_DEL_HMC_OBJ_RANGE;
184 	vchnl_msg->iw_op_ver = I40IW_VCHNL_OP_DEL_HMC_OBJ_RANGE_V0;
185 	add_hmc_obj->obj_type = (u16)rsrc_type;
186 	add_hmc_obj->start_index = start_index;
187 	add_hmc_obj->obj_count = rsrc_count;
188 	ret_code = dev->vchnl_if.vchnl_send(dev, 0, (u8 *)vchnl_msg, vchnl_msg->iw_chnl_buf_len);
189 	if (ret_code)
190 		i40iw_debug(dev, I40IW_DEBUG_VIRT,
191 			    "%s: virt channel send failed 0x%x\n", __func__, ret_code);
192 	return ret_code;
193 }
194 
195 /**
196  * vchnl_pf_send_get_ver_resp - Send channel version to VF
197  * @dev: IWARP device pointer
198  * @vf_id: Virtual function ID associated with the message
199  * @vchnl_msg: Virtual channel message buffer pointer
200  */
vchnl_pf_send_get_ver_resp(struct i40iw_sc_dev * dev,u32 vf_id,struct i40iw_virtchnl_op_buf * vchnl_msg)201 static void vchnl_pf_send_get_ver_resp(struct i40iw_sc_dev *dev,
202 				       u32 vf_id,
203 				       struct i40iw_virtchnl_op_buf *vchnl_msg)
204 {
205 	enum i40iw_status_code ret_code;
206 	u8 resp_buffer[sizeof(struct i40iw_virtchnl_resp_buf) + sizeof(u32) - 1];
207 	struct i40iw_virtchnl_resp_buf *vchnl_msg_resp = (struct i40iw_virtchnl_resp_buf *)resp_buffer;
208 
209 	memset(resp_buffer, 0, sizeof(*resp_buffer));
210 	vchnl_msg_resp->iw_chnl_op_ctx = vchnl_msg->iw_chnl_op_ctx;
211 	vchnl_msg_resp->iw_chnl_buf_len = sizeof(resp_buffer);
212 	vchnl_msg_resp->iw_op_ret_code = I40IW_SUCCESS;
213 	*((u32 *)vchnl_msg_resp->iw_chnl_buf) = I40IW_VCHNL_CHNL_VER_V0;
214 	ret_code = dev->vchnl_if.vchnl_send(dev, vf_id, resp_buffer, sizeof(resp_buffer));
215 	if (ret_code)
216 		i40iw_debug(dev, I40IW_DEBUG_VIRT,
217 			    "%s: virt channel send failed 0x%x\n", __func__, ret_code);
218 }
219 
220 /**
221  * vchnl_pf_send_get_hmc_fcn_resp - Send HMC Function to VF
222  * @dev: IWARP device pointer
223  * @vf_id: Virtual function ID associated with the message
224  * @vchnl_msg: Virtual channel message buffer pointer
225  * @hmc_fcn: HMC function index pointer
226  */
vchnl_pf_send_get_hmc_fcn_resp(struct i40iw_sc_dev * dev,u32 vf_id,struct i40iw_virtchnl_op_buf * vchnl_msg,u16 hmc_fcn)227 static void vchnl_pf_send_get_hmc_fcn_resp(struct i40iw_sc_dev *dev,
228 					   u32 vf_id,
229 					   struct i40iw_virtchnl_op_buf *vchnl_msg,
230 					   u16 hmc_fcn)
231 {
232 	enum i40iw_status_code ret_code;
233 	u8 resp_buffer[sizeof(struct i40iw_virtchnl_resp_buf) + sizeof(u16) - 1];
234 	struct i40iw_virtchnl_resp_buf *vchnl_msg_resp = (struct i40iw_virtchnl_resp_buf *)resp_buffer;
235 
236 	memset(resp_buffer, 0, sizeof(*resp_buffer));
237 	vchnl_msg_resp->iw_chnl_op_ctx = vchnl_msg->iw_chnl_op_ctx;
238 	vchnl_msg_resp->iw_chnl_buf_len = sizeof(resp_buffer);
239 	vchnl_msg_resp->iw_op_ret_code = I40IW_SUCCESS;
240 	*((u16 *)vchnl_msg_resp->iw_chnl_buf) = hmc_fcn;
241 	ret_code = dev->vchnl_if.vchnl_send(dev, vf_id, resp_buffer, sizeof(resp_buffer));
242 	if (ret_code)
243 		i40iw_debug(dev, I40IW_DEBUG_VIRT,
244 			    "%s: virt channel send failed 0x%x\n", __func__, ret_code);
245 }
246 
247 /**
248  * vchnl_pf_send_get_pe_stats_resp - Send PE Stats to VF
249  * @dev: IWARP device pointer
250  * @vf_id: Virtual function ID associated with the message
251  * @vchnl_msg: Virtual channel message buffer pointer
252  * @hw_stats: HW Stats struct
253  */
254 
vchnl_pf_send_get_pe_stats_resp(struct i40iw_sc_dev * dev,u32 vf_id,struct i40iw_virtchnl_op_buf * vchnl_msg,struct i40iw_dev_hw_stats * hw_stats)255 static void vchnl_pf_send_get_pe_stats_resp(struct i40iw_sc_dev *dev,
256 					    u32 vf_id,
257 					    struct i40iw_virtchnl_op_buf *vchnl_msg,
258 					    struct i40iw_dev_hw_stats *hw_stats)
259 {
260 	enum i40iw_status_code ret_code;
261 	u8 resp_buffer[sizeof(struct i40iw_virtchnl_resp_buf) + sizeof(struct i40iw_dev_hw_stats) - 1];
262 	struct i40iw_virtchnl_resp_buf *vchnl_msg_resp = (struct i40iw_virtchnl_resp_buf *)resp_buffer;
263 
264 	memset(resp_buffer, 0, sizeof(*resp_buffer));
265 	vchnl_msg_resp->iw_chnl_op_ctx = vchnl_msg->iw_chnl_op_ctx;
266 	vchnl_msg_resp->iw_chnl_buf_len = sizeof(resp_buffer);
267 	vchnl_msg_resp->iw_op_ret_code = I40IW_SUCCESS;
268 	*((struct i40iw_dev_hw_stats *)vchnl_msg_resp->iw_chnl_buf) = *hw_stats;
269 	ret_code = dev->vchnl_if.vchnl_send(dev, vf_id, resp_buffer, sizeof(resp_buffer));
270 	if (ret_code)
271 		i40iw_debug(dev, I40IW_DEBUG_VIRT,
272 			    "%s: virt channel send failed 0x%x\n", __func__, ret_code);
273 }
274 
275 /**
276  * vchnl_pf_send_error_resp - Send an error response to VF
277  * @dev: IWARP device pointer
278  * @vf_id: Virtual function ID associated with the message
279  * @vchnl_msg: Virtual channel message buffer pointer
280  * @op_ret_code: I40IW_ERR_* status code
281  */
vchnl_pf_send_error_resp(struct i40iw_sc_dev * dev,u32 vf_id,struct i40iw_virtchnl_op_buf * vchnl_msg,u16 op_ret_code)282 static void vchnl_pf_send_error_resp(struct i40iw_sc_dev *dev, u32 vf_id,
283 				     struct i40iw_virtchnl_op_buf *vchnl_msg,
284 				     u16 op_ret_code)
285 {
286 	enum i40iw_status_code ret_code;
287 	u8 resp_buffer[sizeof(struct i40iw_virtchnl_resp_buf)];
288 	struct i40iw_virtchnl_resp_buf *vchnl_msg_resp = (struct i40iw_virtchnl_resp_buf *)resp_buffer;
289 
290 	memset(resp_buffer, 0, sizeof(resp_buffer));
291 	vchnl_msg_resp->iw_chnl_op_ctx = vchnl_msg->iw_chnl_op_ctx;
292 	vchnl_msg_resp->iw_chnl_buf_len = sizeof(resp_buffer);
293 	vchnl_msg_resp->iw_op_ret_code = (u16)op_ret_code;
294 	ret_code = dev->vchnl_if.vchnl_send(dev, vf_id, resp_buffer, sizeof(resp_buffer));
295 	if (ret_code)
296 		i40iw_debug(dev, I40IW_DEBUG_VIRT,
297 			    "%s: virt channel send failed 0x%x\n", __func__, ret_code);
298 }
299 
300 /**
301  * pf_cqp_get_hmc_fcn_callback - Callback for Get HMC Fcn
302  * @dev: IWARP device pointer
303  * @callback_param: unused CQP callback parameter
304  * @cqe_info: CQE information pointer
305  */
pf_cqp_get_hmc_fcn_callback(struct i40iw_sc_dev * dev,void * callback_param,struct i40iw_ccq_cqe_info * cqe_info)306 static void pf_cqp_get_hmc_fcn_callback(struct i40iw_sc_dev *dev, void *callback_param,
307 					struct i40iw_ccq_cqe_info *cqe_info)
308 {
309 	struct i40iw_vfdev *vf_dev = callback_param;
310 	struct i40iw_virt_mem vf_dev_mem;
311 
312 	if (cqe_info->error) {
313 		i40iw_debug(dev, I40IW_DEBUG_VIRT,
314 			    "CQP Completion Error on Get HMC Function.  Maj = 0x%04x, Minor = 0x%04x\n",
315 			    cqe_info->maj_err_code, cqe_info->min_err_code);
316 		dev->vf_dev[vf_dev->iw_vf_idx] = NULL;
317 		vchnl_pf_send_error_resp(dev, vf_dev->vf_id, &vf_dev->vf_msg_buffer.vchnl_msg,
318 					 (u16)I40IW_ERR_CQP_COMPL_ERROR);
319 		vf_dev_mem.va = vf_dev;
320 		vf_dev_mem.size = sizeof(*vf_dev);
321 		i40iw_free_virt_mem(dev->hw, &vf_dev_mem);
322 	} else {
323 		i40iw_debug(dev, I40IW_DEBUG_VIRT,
324 			    "CQP Completion Operation Return information = 0x%08x\n",
325 			    cqe_info->op_ret_val);
326 		vf_dev->pmf_index = (u16)cqe_info->op_ret_val;
327 		vf_dev->msg_count--;
328 		vchnl_pf_send_get_hmc_fcn_resp(dev,
329 					       vf_dev->vf_id,
330 					       &vf_dev->vf_msg_buffer.vchnl_msg,
331 					       vf_dev->pmf_index);
332 	}
333 }
334 
335 /**
336  * pf_add_hmc_obj_callback - Callback for Add HMC Object
337  * @work_vf_dev: pointer to the VF Device
338  */
pf_add_hmc_obj_callback(void * work_vf_dev)339 static void pf_add_hmc_obj_callback(void *work_vf_dev)
340 {
341 	struct i40iw_vfdev *vf_dev = (struct i40iw_vfdev *)work_vf_dev;
342 	struct i40iw_hmc_info *hmc_info = &vf_dev->hmc_info;
343 	struct i40iw_virtchnl_op_buf *vchnl_msg = &vf_dev->vf_msg_buffer.vchnl_msg;
344 	struct i40iw_hmc_create_obj_info info;
345 	struct i40iw_virtchnl_hmc_obj_range *add_hmc_obj;
346 	enum i40iw_status_code ret_code;
347 
348 	if (!vf_dev->pf_hmc_initialized) {
349 		ret_code = i40iw_pf_init_vfhmc(vf_dev->pf_dev, (u8)vf_dev->pmf_index, NULL);
350 		if (ret_code)
351 			goto add_out;
352 		vf_dev->pf_hmc_initialized = true;
353 	}
354 
355 	add_hmc_obj = (struct i40iw_virtchnl_hmc_obj_range *)vchnl_msg->iw_chnl_buf;
356 
357 	memset(&info, 0, sizeof(info));
358 	info.hmc_info = hmc_info;
359 	info.is_pf = false;
360 	info.rsrc_type = (u32)add_hmc_obj->obj_type;
361 	info.entry_type = (info.rsrc_type == I40IW_HMC_IW_PBLE) ? I40IW_SD_TYPE_PAGED : I40IW_SD_TYPE_DIRECT;
362 	info.start_idx = add_hmc_obj->start_index;
363 	info.count = add_hmc_obj->obj_count;
364 	i40iw_debug(vf_dev->pf_dev, I40IW_DEBUG_VIRT,
365 		    "I40IW_VCHNL_OP_ADD_HMC_OBJ_RANGE.  Add %u type %u objects\n",
366 		    info.count, info.rsrc_type);
367 	ret_code = i40iw_sc_create_hmc_obj(vf_dev->pf_dev, &info);
368 	if (!ret_code)
369 		vf_dev->hmc_info.hmc_obj[add_hmc_obj->obj_type].cnt = add_hmc_obj->obj_count;
370 add_out:
371 	vf_dev->msg_count--;
372 	vchnl_pf_send_error_resp(vf_dev->pf_dev, vf_dev->vf_id, vchnl_msg, (u16)ret_code);
373 }
374 
375 /**
376  * pf_del_hmc_obj_callback - Callback for delete HMC Object
377  * @work_vf_dev: pointer to the VF Device
378  */
pf_del_hmc_obj_callback(void * work_vf_dev)379 static void pf_del_hmc_obj_callback(void *work_vf_dev)
380 {
381 	struct i40iw_vfdev *vf_dev = (struct i40iw_vfdev *)work_vf_dev;
382 	struct i40iw_hmc_info *hmc_info = &vf_dev->hmc_info;
383 	struct i40iw_virtchnl_op_buf *vchnl_msg = &vf_dev->vf_msg_buffer.vchnl_msg;
384 	struct i40iw_hmc_del_obj_info info;
385 	struct i40iw_virtchnl_hmc_obj_range *del_hmc_obj;
386 	enum i40iw_status_code ret_code = I40IW_SUCCESS;
387 
388 	if (!vf_dev->pf_hmc_initialized)
389 		goto del_out;
390 
391 	del_hmc_obj = (struct i40iw_virtchnl_hmc_obj_range *)vchnl_msg->iw_chnl_buf;
392 
393 	memset(&info, 0, sizeof(info));
394 	info.hmc_info = hmc_info;
395 	info.is_pf = false;
396 	info.rsrc_type = (u32)del_hmc_obj->obj_type;
397 	info.start_idx = del_hmc_obj->start_index;
398 	info.count = del_hmc_obj->obj_count;
399 	i40iw_debug(vf_dev->pf_dev, I40IW_DEBUG_VIRT,
400 		    "I40IW_VCHNL_OP_DEL_HMC_OBJ_RANGE.  Delete %u type %u objects\n",
401 		    info.count, info.rsrc_type);
402 	ret_code = i40iw_sc_del_hmc_obj(vf_dev->pf_dev, &info, false);
403 del_out:
404 	vf_dev->msg_count--;
405 	vchnl_pf_send_error_resp(vf_dev->pf_dev, vf_dev->vf_id, vchnl_msg, (u16)ret_code);
406 }
407 
408 /**
409  * i40iw_vf_init_pestat - Initialize stats for VF
410  * @dev: pointer to the VF Device
411  * @stats: Statistics structure pointer
412  * @index: Stats index
413  */
i40iw_vf_init_pestat(struct i40iw_sc_dev * dev,struct i40iw_vsi_pestat * stats,u16 index)414 static void i40iw_vf_init_pestat(struct i40iw_sc_dev *dev, struct i40iw_vsi_pestat *stats, u16 index)
415 {
416 	stats->hw = dev->hw;
417 	i40iw_hw_stats_init(stats, (u8)index, false);
418 	spin_lock_init(&stats->lock);
419 }
420 
421 /**
422  * i40iw_vchnl_recv_pf - Receive PF virtual channel messages
423  * @dev: IWARP device pointer
424  * @vf_id: Virtual function ID associated with the message
425  * @msg: Virtual channel message buffer pointer
426  * @len: Length of the virtual channels message
427  */
i40iw_vchnl_recv_pf(struct i40iw_sc_dev * dev,u32 vf_id,u8 * msg,u16 len)428 enum i40iw_status_code i40iw_vchnl_recv_pf(struct i40iw_sc_dev *dev,
429 					   u32 vf_id,
430 					   u8 *msg,
431 					   u16 len)
432 {
433 	struct i40iw_virtchnl_op_buf *vchnl_msg = (struct i40iw_virtchnl_op_buf *)msg;
434 	struct i40iw_vfdev *vf_dev = NULL;
435 	struct i40iw_hmc_fcn_info hmc_fcn_info;
436 	u16 iw_vf_idx;
437 	u16 first_avail_iw_vf = I40IW_MAX_PE_ENABLED_VF_COUNT;
438 	struct i40iw_virt_mem vf_dev_mem;
439 	struct i40iw_virtchnl_work_info work_info;
440 	struct i40iw_vsi_pestat *stats;
441 	enum i40iw_status_code ret_code;
442 
443 	if (!dev || !msg || !len)
444 		return I40IW_ERR_PARAM;
445 
446 	if (!dev->vchnl_up)
447 		return I40IW_ERR_NOT_READY;
448 	if (vchnl_msg->iw_op_code == I40IW_VCHNL_OP_GET_VER) {
449 		vchnl_pf_send_get_ver_resp(dev, vf_id, vchnl_msg);
450 		return I40IW_SUCCESS;
451 	}
452 	for (iw_vf_idx = 0; iw_vf_idx < I40IW_MAX_PE_ENABLED_VF_COUNT; iw_vf_idx++) {
453 		if (!dev->vf_dev[iw_vf_idx]) {
454 			if (first_avail_iw_vf == I40IW_MAX_PE_ENABLED_VF_COUNT)
455 				first_avail_iw_vf = iw_vf_idx;
456 			continue;
457 		}
458 		if (dev->vf_dev[iw_vf_idx]->vf_id == vf_id) {
459 			vf_dev = dev->vf_dev[iw_vf_idx];
460 			break;
461 		}
462 	}
463 	if (vf_dev) {
464 		if (!vf_dev->msg_count) {
465 			vf_dev->msg_count++;
466 		} else {
467 			i40iw_debug(dev, I40IW_DEBUG_VIRT,
468 				    "VF%u already has a channel message in progress.\n",
469 				    vf_id);
470 			return I40IW_SUCCESS;
471 		}
472 	}
473 	switch (vchnl_msg->iw_op_code) {
474 	case I40IW_VCHNL_OP_GET_HMC_FCN:
475 		if (!vf_dev &&
476 		    (first_avail_iw_vf != I40IW_MAX_PE_ENABLED_VF_COUNT)) {
477 			ret_code = i40iw_allocate_virt_mem(dev->hw, &vf_dev_mem, sizeof(struct i40iw_vfdev) +
478 							   (sizeof(struct i40iw_hmc_obj_info) * I40IW_HMC_IW_MAX));
479 			if (!ret_code) {
480 				vf_dev = vf_dev_mem.va;
481 				vf_dev->stats_initialized = false;
482 				vf_dev->pf_dev = dev;
483 				vf_dev->msg_count = 1;
484 				vf_dev->vf_id = vf_id;
485 				vf_dev->iw_vf_idx = first_avail_iw_vf;
486 				vf_dev->pf_hmc_initialized = false;
487 				vf_dev->hmc_info.hmc_obj = (struct i40iw_hmc_obj_info *)(&vf_dev[1]);
488 				i40iw_debug(dev, I40IW_DEBUG_VIRT,
489 					    "vf_dev %p, hmc_info %p, hmc_obj %p\n",
490 					    vf_dev, &vf_dev->hmc_info, vf_dev->hmc_info.hmc_obj);
491 				dev->vf_dev[first_avail_iw_vf] = vf_dev;
492 				iw_vf_idx = first_avail_iw_vf;
493 			} else {
494 				i40iw_debug(dev, I40IW_DEBUG_VIRT,
495 					    "VF%u Unable to allocate a VF device structure.\n",
496 					    vf_id);
497 				vchnl_pf_send_error_resp(dev, vf_id, vchnl_msg, (u16)I40IW_ERR_NO_MEMORY);
498 				return I40IW_SUCCESS;
499 			}
500 			memcpy(&vf_dev->vf_msg_buffer.vchnl_msg, vchnl_msg, len);
501 			hmc_fcn_info.callback_fcn = pf_cqp_get_hmc_fcn_callback;
502 			hmc_fcn_info.vf_id = vf_id;
503 			hmc_fcn_info.iw_vf_idx = vf_dev->iw_vf_idx;
504 			hmc_fcn_info.cqp_callback_param = vf_dev;
505 			hmc_fcn_info.free_fcn = false;
506 			ret_code = i40iw_cqp_manage_hmc_fcn_cmd(dev, &hmc_fcn_info);
507 			if (ret_code)
508 				i40iw_debug(dev, I40IW_DEBUG_VIRT,
509 					    "VF%u error CQP HMC Function operation.\n",
510 					    vf_id);
511 			i40iw_vf_init_pestat(dev, &vf_dev->pestat, vf_dev->pmf_index);
512 			vf_dev->stats_initialized = true;
513 		} else {
514 			if (vf_dev) {
515 				vf_dev->msg_count--;
516 				vchnl_pf_send_get_hmc_fcn_resp(dev, vf_id, vchnl_msg, vf_dev->pmf_index);
517 			} else {
518 				vchnl_pf_send_error_resp(dev, vf_id, vchnl_msg,
519 							 (u16)I40IW_ERR_NO_MEMORY);
520 			}
521 		}
522 		break;
523 	case I40IW_VCHNL_OP_ADD_HMC_OBJ_RANGE:
524 		if (!vf_dev)
525 			return I40IW_ERR_BAD_PTR;
526 		work_info.worker_vf_dev = vf_dev;
527 		work_info.callback_fcn = pf_add_hmc_obj_callback;
528 		memcpy(&vf_dev->vf_msg_buffer.vchnl_msg, vchnl_msg, len);
529 		i40iw_cqp_spawn_worker(dev, &work_info, vf_dev->iw_vf_idx);
530 		break;
531 	case I40IW_VCHNL_OP_DEL_HMC_OBJ_RANGE:
532 		if (!vf_dev)
533 			return I40IW_ERR_BAD_PTR;
534 		work_info.worker_vf_dev = vf_dev;
535 		work_info.callback_fcn = pf_del_hmc_obj_callback;
536 		memcpy(&vf_dev->vf_msg_buffer.vchnl_msg, vchnl_msg, len);
537 		i40iw_cqp_spawn_worker(dev, &work_info, vf_dev->iw_vf_idx);
538 		break;
539 	case I40IW_VCHNL_OP_GET_STATS:
540 		if (!vf_dev)
541 			return I40IW_ERR_BAD_PTR;
542 		stats = &vf_dev->pestat;
543 		i40iw_hw_stats_read_all(stats, &stats->hw_stats);
544 		vf_dev->msg_count--;
545 		vchnl_pf_send_get_pe_stats_resp(dev, vf_id, vchnl_msg, &stats->hw_stats);
546 		break;
547 	default:
548 		i40iw_debug(dev, I40IW_DEBUG_VIRT,
549 			    "40iw_vchnl_recv_pf: Invalid OpCode 0x%x\n",
550 			    vchnl_msg->iw_op_code);
551 		vchnl_pf_send_error_resp(dev, vf_id,
552 					 vchnl_msg, (u16)I40IW_ERR_NOT_IMPLEMENTED);
553 	}
554 	return I40IW_SUCCESS;
555 }
556 
557 /**
558  * i40iw_vchnl_recv_vf - Receive VF virtual channel messages
559  * @dev: IWARP device pointer
560  * @vf_id: Virtual function ID associated with the message
561  * @msg: Virtual channel message buffer pointer
562  * @len: Length of the virtual channels message
563  */
i40iw_vchnl_recv_vf(struct i40iw_sc_dev * dev,u32 vf_id,u8 * msg,u16 len)564 enum i40iw_status_code i40iw_vchnl_recv_vf(struct i40iw_sc_dev *dev,
565 					   u32 vf_id,
566 					   u8 *msg,
567 					   u16 len)
568 {
569 	struct i40iw_virtchnl_resp_buf *vchnl_msg_resp = (struct i40iw_virtchnl_resp_buf *)msg;
570 	struct i40iw_virtchnl_req *vchnl_req;
571 
572 	vchnl_req = (struct i40iw_virtchnl_req *)(uintptr_t)vchnl_msg_resp->iw_chnl_op_ctx;
573 	vchnl_req->ret_code = (enum i40iw_status_code)vchnl_msg_resp->iw_op_ret_code;
574 	if (len == (sizeof(*vchnl_msg_resp) + vchnl_req->parm_len - 1)) {
575 		if (vchnl_req->parm_len && vchnl_req->parm)
576 			memcpy(vchnl_req->parm, vchnl_msg_resp->iw_chnl_buf, vchnl_req->parm_len);
577 		i40iw_debug(dev, I40IW_DEBUG_VIRT,
578 			    "%s: Got response, data size %u\n", __func__,
579 			    vchnl_req->parm_len);
580 	} else {
581 		i40iw_debug(dev, I40IW_DEBUG_VIRT,
582 			    "%s: error length on response, Got %u, expected %u\n", __func__,
583 			    len, (u32)(sizeof(*vchnl_msg_resp) + vchnl_req->parm_len - 1));
584 	}
585 
586 	return I40IW_SUCCESS;
587 }
588 
589 /**
590  * i40iw_vchnl_vf_get_ver - Request Channel version
591  * @dev: IWARP device pointer
592  * @vchnl_ver: Virtual channel message version pointer
593  */
i40iw_vchnl_vf_get_ver(struct i40iw_sc_dev * dev,u32 * vchnl_ver)594 enum i40iw_status_code i40iw_vchnl_vf_get_ver(struct i40iw_sc_dev *dev,
595 					      u32 *vchnl_ver)
596 {
597 	struct i40iw_virtchnl_req vchnl_req;
598 	enum i40iw_status_code ret_code;
599 
600 	if (!i40iw_vf_clear_to_send(dev))
601 		return I40IW_ERR_TIMEOUT;
602 	memset(&vchnl_req, 0, sizeof(vchnl_req));
603 	vchnl_req.dev = dev;
604 	vchnl_req.parm = vchnl_ver;
605 	vchnl_req.parm_len = sizeof(*vchnl_ver);
606 	vchnl_req.vchnl_msg = &dev->vchnl_vf_msg_buf.vchnl_msg;
607 
608 	ret_code = vchnl_vf_send_get_ver_req(dev, &vchnl_req);
609 	if (ret_code) {
610 		i40iw_debug(dev, I40IW_DEBUG_VIRT,
611 			    "%s Send message failed 0x%0x\n", __func__, ret_code);
612 		return ret_code;
613 	}
614 	ret_code = i40iw_vf_wait_vchnl_resp(dev);
615 	if (ret_code)
616 		return ret_code;
617 	else
618 		return vchnl_req.ret_code;
619 }
620 
621 /**
622  * i40iw_vchnl_vf_get_hmc_fcn - Request HMC Function
623  * @dev: IWARP device pointer
624  * @hmc_fcn: HMC function index pointer
625  */
i40iw_vchnl_vf_get_hmc_fcn(struct i40iw_sc_dev * dev,u16 * hmc_fcn)626 enum i40iw_status_code i40iw_vchnl_vf_get_hmc_fcn(struct i40iw_sc_dev *dev,
627 						  u16 *hmc_fcn)
628 {
629 	struct i40iw_virtchnl_req vchnl_req;
630 	enum i40iw_status_code ret_code;
631 
632 	if (!i40iw_vf_clear_to_send(dev))
633 		return I40IW_ERR_TIMEOUT;
634 	memset(&vchnl_req, 0, sizeof(vchnl_req));
635 	vchnl_req.dev = dev;
636 	vchnl_req.parm = hmc_fcn;
637 	vchnl_req.parm_len = sizeof(*hmc_fcn);
638 	vchnl_req.vchnl_msg = &dev->vchnl_vf_msg_buf.vchnl_msg;
639 
640 	ret_code = vchnl_vf_send_get_hmc_fcn_req(dev, &vchnl_req);
641 	if (ret_code) {
642 		i40iw_debug(dev, I40IW_DEBUG_VIRT,
643 			    "%s Send message failed 0x%0x\n", __func__, ret_code);
644 		return ret_code;
645 	}
646 	ret_code = i40iw_vf_wait_vchnl_resp(dev);
647 	if (ret_code)
648 		return ret_code;
649 	else
650 		return vchnl_req.ret_code;
651 }
652 
653 /**
654  * i40iw_vchnl_vf_add_hmc_objs - Add HMC Object
655  * @dev: IWARP device pointer
656  * @rsrc_type: HMC Resource type
657  * @start_index: Starting index of the objects to be added
658  * @rsrc_count: Number of resources to be added
659  */
i40iw_vchnl_vf_add_hmc_objs(struct i40iw_sc_dev * dev,enum i40iw_hmc_rsrc_type rsrc_type,u32 start_index,u32 rsrc_count)660 enum i40iw_status_code i40iw_vchnl_vf_add_hmc_objs(struct i40iw_sc_dev *dev,
661 						   enum i40iw_hmc_rsrc_type rsrc_type,
662 						   u32 start_index,
663 						   u32 rsrc_count)
664 {
665 	struct i40iw_virtchnl_req vchnl_req;
666 	enum i40iw_status_code ret_code;
667 
668 	if (!i40iw_vf_clear_to_send(dev))
669 		return I40IW_ERR_TIMEOUT;
670 	memset(&vchnl_req, 0, sizeof(vchnl_req));
671 	vchnl_req.dev = dev;
672 	vchnl_req.vchnl_msg = &dev->vchnl_vf_msg_buf.vchnl_msg;
673 
674 	ret_code = vchnl_vf_send_add_hmc_objs_req(dev,
675 						  &vchnl_req,
676 						  rsrc_type,
677 						  start_index,
678 						  rsrc_count);
679 	if (ret_code) {
680 		i40iw_debug(dev, I40IW_DEBUG_VIRT,
681 			    "%s Send message failed 0x%0x\n", __func__, ret_code);
682 		return ret_code;
683 	}
684 	ret_code = i40iw_vf_wait_vchnl_resp(dev);
685 	if (ret_code)
686 		return ret_code;
687 	else
688 		return vchnl_req.ret_code;
689 }
690 
691 /**
692  * i40iw_vchnl_vf_del_hmc_obj - del HMC obj
693  * @dev: IWARP device pointer
694  * @rsrc_type: HMC Resource type
695  * @start_index: Starting index of the object to delete
696  * @rsrc_count: Number of resources to be delete
697  */
i40iw_vchnl_vf_del_hmc_obj(struct i40iw_sc_dev * dev,enum i40iw_hmc_rsrc_type rsrc_type,u32 start_index,u32 rsrc_count)698 enum i40iw_status_code i40iw_vchnl_vf_del_hmc_obj(struct i40iw_sc_dev *dev,
699 						  enum i40iw_hmc_rsrc_type rsrc_type,
700 						  u32 start_index,
701 						  u32 rsrc_count)
702 {
703 	struct i40iw_virtchnl_req vchnl_req;
704 	enum i40iw_status_code ret_code;
705 
706 	if (!i40iw_vf_clear_to_send(dev))
707 		return I40IW_ERR_TIMEOUT;
708 	memset(&vchnl_req, 0, sizeof(vchnl_req));
709 	vchnl_req.dev = dev;
710 	vchnl_req.vchnl_msg = &dev->vchnl_vf_msg_buf.vchnl_msg;
711 
712 	ret_code = vchnl_vf_send_del_hmc_objs_req(dev,
713 						  &vchnl_req,
714 						  rsrc_type,
715 						  start_index,
716 						  rsrc_count);
717 	if (ret_code) {
718 		i40iw_debug(dev, I40IW_DEBUG_VIRT,
719 			    "%s Send message failed 0x%0x\n", __func__, ret_code);
720 		return ret_code;
721 	}
722 	ret_code = i40iw_vf_wait_vchnl_resp(dev);
723 	if (ret_code)
724 		return ret_code;
725 	else
726 		return vchnl_req.ret_code;
727 }
728 
729 /**
730  * i40iw_vchnl_vf_get_pe_stats - Get PE stats
731  * @dev: IWARP device pointer
732  * @hw_stats: HW stats struct
733  */
i40iw_vchnl_vf_get_pe_stats(struct i40iw_sc_dev * dev,struct i40iw_dev_hw_stats * hw_stats)734 enum i40iw_status_code i40iw_vchnl_vf_get_pe_stats(struct i40iw_sc_dev *dev,
735 						   struct i40iw_dev_hw_stats *hw_stats)
736 {
737 	struct i40iw_virtchnl_req  vchnl_req;
738 	enum i40iw_status_code ret_code;
739 
740 	if (!i40iw_vf_clear_to_send(dev))
741 		return I40IW_ERR_TIMEOUT;
742 	memset(&vchnl_req, 0, sizeof(vchnl_req));
743 	vchnl_req.dev = dev;
744 	vchnl_req.parm = hw_stats;
745 	vchnl_req.parm_len = sizeof(*hw_stats);
746 	vchnl_req.vchnl_msg = &dev->vchnl_vf_msg_buf.vchnl_msg;
747 
748 	ret_code = vchnl_vf_send_get_pe_stats_req(dev, &vchnl_req);
749 	if (ret_code) {
750 		i40iw_debug(dev, I40IW_DEBUG_VIRT,
751 			    "%s Send message failed 0x%0x\n", __func__, ret_code);
752 		return ret_code;
753 	}
754 	ret_code = i40iw_vf_wait_vchnl_resp(dev);
755 	if (ret_code)
756 		return ret_code;
757 	else
758 		return vchnl_req.ret_code;
759 }
760