1d775155aSJacob Keller // SPDX-License-Identifier: GPL-2.0
2d775155aSJacob Keller /* Copyright (c) 2018, Intel Corporation. */
3d775155aSJacob Keller 
4d775155aSJacob Keller #include "ice_common.h"
5d775155aSJacob Keller #include "ice_vf_mbx.h"
6d775155aSJacob Keller 
7d775155aSJacob Keller /**
8d775155aSJacob Keller  * ice_aq_send_msg_to_vf
9d775155aSJacob Keller  * @hw: pointer to the hardware structure
10d775155aSJacob Keller  * @vfid: VF ID to send msg
11d775155aSJacob Keller  * @v_opcode: opcodes for VF-PF communication
12d775155aSJacob Keller  * @v_retval: return error code
13d775155aSJacob Keller  * @msg: pointer to the msg buffer
14d775155aSJacob Keller  * @msglen: msg length
15d775155aSJacob Keller  * @cd: pointer to command details
16d775155aSJacob Keller  *
17d775155aSJacob Keller  * Send message to VF driver (0x0802) using mailbox
18d775155aSJacob Keller  * queue and asynchronously sending message via
19d775155aSJacob Keller  * ice_sq_send_cmd() function
20d775155aSJacob Keller  */
21d775155aSJacob Keller int
ice_aq_send_msg_to_vf(struct ice_hw * hw,u16 vfid,u32 v_opcode,u32 v_retval,u8 * msg,u16 msglen,struct ice_sq_cd * cd)22d775155aSJacob Keller ice_aq_send_msg_to_vf(struct ice_hw *hw, u16 vfid, u32 v_opcode, u32 v_retval,
23d775155aSJacob Keller 		      u8 *msg, u16 msglen, struct ice_sq_cd *cd)
24d775155aSJacob Keller {
25d775155aSJacob Keller 	struct ice_aqc_pf_vf_msg *cmd;
26d775155aSJacob Keller 	struct ice_aq_desc desc;
27d775155aSJacob Keller 
28d775155aSJacob Keller 	ice_fill_dflt_direct_cmd_desc(&desc, ice_mbx_opc_send_msg_to_vf);
29d775155aSJacob Keller 
30d775155aSJacob Keller 	cmd = &desc.params.virt;
31d775155aSJacob Keller 	cmd->id = cpu_to_le32(vfid);
32d775155aSJacob Keller 
33d775155aSJacob Keller 	desc.cookie_high = cpu_to_le32(v_opcode);
34d775155aSJacob Keller 	desc.cookie_low = cpu_to_le32(v_retval);
35d775155aSJacob Keller 
36d775155aSJacob Keller 	if (msglen)
37d775155aSJacob Keller 		desc.flags |= cpu_to_le16(ICE_AQ_FLAG_RD);
38d775155aSJacob Keller 
39d775155aSJacob Keller 	return ice_sq_send_cmd(hw, &hw->mailboxq, &desc, msg, msglen, cd);
40d775155aSJacob Keller }
41d775155aSJacob Keller 
42b2dbde3aSMichal Swiatkowski static const u32 ice_legacy_aq_to_vc_speed[] = {
431d0e28a9SBrett Creeley 	VIRTCHNL_LINK_SPEED_100MB,	/* BIT(0) */
441d0e28a9SBrett Creeley 	VIRTCHNL_LINK_SPEED_100MB,
451d0e28a9SBrett Creeley 	VIRTCHNL_LINK_SPEED_1GB,
461d0e28a9SBrett Creeley 	VIRTCHNL_LINK_SPEED_1GB,
471d0e28a9SBrett Creeley 	VIRTCHNL_LINK_SPEED_1GB,
481d0e28a9SBrett Creeley 	VIRTCHNL_LINK_SPEED_10GB,
491d0e28a9SBrett Creeley 	VIRTCHNL_LINK_SPEED_20GB,
501d0e28a9SBrett Creeley 	VIRTCHNL_LINK_SPEED_25GB,
511d0e28a9SBrett Creeley 	VIRTCHNL_LINK_SPEED_40GB,
521d0e28a9SBrett Creeley 	VIRTCHNL_LINK_SPEED_40GB,
531d0e28a9SBrett Creeley 	VIRTCHNL_LINK_SPEED_40GB,
541d0e28a9SBrett Creeley };
551d0e28a9SBrett Creeley 
56d775155aSJacob Keller /**
57d775155aSJacob Keller  * ice_conv_link_speed_to_virtchnl
58d775155aSJacob Keller  * @adv_link_support: determines the format of the returned link speed
59d775155aSJacob Keller  * @link_speed: variable containing the link_speed to be converted
60d775155aSJacob Keller  *
61d775155aSJacob Keller  * Convert link speed supported by HW to link speed supported by virtchnl.
62d775155aSJacob Keller  * If adv_link_support is true, then return link speed in Mbps. Else return
63d775155aSJacob Keller  * link speed as a VIRTCHNL_LINK_SPEED_* casted to a u32. Note that the caller
64d775155aSJacob Keller  * needs to cast back to an enum virtchnl_link_speed in the case where
65d775155aSJacob Keller  * adv_link_support is false, but when adv_link_support is true the caller can
66d775155aSJacob Keller  * expect the speed in Mbps.
67d775155aSJacob Keller  */
ice_conv_link_speed_to_virtchnl(bool adv_link_support,u16 link_speed)68d775155aSJacob Keller u32 ice_conv_link_speed_to_virtchnl(bool adv_link_support, u16 link_speed)
69d775155aSJacob Keller {
701d0e28a9SBrett Creeley 	/* convert a BIT() value into an array index */
71b2dbde3aSMichal Swiatkowski 	u32 index = fls(link_speed) - 1;
72b2dbde3aSMichal Swiatkowski 
73b2dbde3aSMichal Swiatkowski 	if (adv_link_support)
74b2dbde3aSMichal Swiatkowski 		return ice_get_link_speed(index);
75b2dbde3aSMichal Swiatkowski 	else if (index < ARRAY_SIZE(ice_legacy_aq_to_vc_speed))
76d775155aSJacob Keller 		/* Virtchnl speeds are not defined for every speed supported in
77d775155aSJacob Keller 		 * the hardware. To maintain compatibility with older AVF
78d775155aSJacob Keller 		 * drivers, while reporting the speed the new speed values are
79d775155aSJacob Keller 		 * resolved to the closest known virtchnl speeds
80d775155aSJacob Keller 		 */
81b2dbde3aSMichal Swiatkowski 		return ice_legacy_aq_to_vc_speed[index];
82d775155aSJacob Keller 
83b2dbde3aSMichal Swiatkowski 	return VIRTCHNL_LINK_SPEED_UNKNOWN;
84d775155aSJacob Keller }
85d775155aSJacob Keller 
86d775155aSJacob Keller /* The mailbox overflow detection algorithm helps to check if there
87d775155aSJacob Keller  * is a possibility of a malicious VF transmitting too many MBX messages to the
88d775155aSJacob Keller  * PF.
89d775155aSJacob Keller  * 1. The mailbox snapshot structure, ice_mbx_snapshot, is initialized during
90d775155aSJacob Keller  * driver initialization in ice_init_hw() using ice_mbx_init_snapshot().
91d775155aSJacob Keller  * The struct ice_mbx_snapshot helps to track and traverse a static window of
92d775155aSJacob Keller  * messages within the mailbox queue while looking for a malicious VF.
93d775155aSJacob Keller  *
94d775155aSJacob Keller  * 2. When the caller starts processing its mailbox queue in response to an
95d775155aSJacob Keller  * interrupt, the structure ice_mbx_snapshot is expected to be cleared before
968cd8a6b1SJacob Keller  * the algorithm can be run for the first time for that interrupt. This
978cd8a6b1SJacob Keller  * requires calling ice_mbx_reset_snapshot() as well as calling
988cd8a6b1SJacob Keller  * ice_mbx_reset_vf_info() for each VF tracking structure.
99d775155aSJacob Keller  *
100d775155aSJacob Keller  * 3. For every message read by the caller from the MBX Queue, the caller must
101d775155aSJacob Keller  * call the detection algorithm's entry function ice_mbx_vf_state_handler().
102d775155aSJacob Keller  * Before every call to ice_mbx_vf_state_handler() the struct ice_mbx_data is
103d775155aSJacob Keller  * filled as it is required to be passed to the algorithm.
104d775155aSJacob Keller  *
1058cd8a6b1SJacob Keller  * 4. Every time a message is read from the MBX queue, a tracking structure
1068cd8a6b1SJacob Keller  * for the VF must be passed to the state handler. The boolean output
1078cd8a6b1SJacob Keller  * report_malvf from ice_mbx_vf_state_handler() serves as an indicator to the
1088cd8a6b1SJacob Keller  * caller whether it must report this VF as malicious or not.
109d775155aSJacob Keller  *
110d775155aSJacob Keller  * 5. When a VF is identified to be malicious, the caller can send a message
1118cd8a6b1SJacob Keller  * to the system administrator.
112d775155aSJacob Keller  *
1138cd8a6b1SJacob Keller  * 6. The PF is responsible for maintaining the struct ice_mbx_vf_info
1148cd8a6b1SJacob Keller  * structure for each VF. The PF should clear the VF tracking structure if the
1158cd8a6b1SJacob Keller  * VF is reset. When a VF is shut down and brought back up, we will then
1168cd8a6b1SJacob Keller  * assume that the new VF is not malicious and may report it again if we
1178cd8a6b1SJacob Keller  * detect it again.
118d775155aSJacob Keller  *
119d775155aSJacob Keller  * 7. The function ice_mbx_reset_snapshot() is called to reset the information
120d775155aSJacob Keller  * in ice_mbx_snapshot for every new mailbox interrupt handled.
121d775155aSJacob Keller  */
122d775155aSJacob Keller #define ICE_RQ_DATA_MASK(rq_data) ((rq_data) & PF_MBX_ARQH_ARQH_M)
123d775155aSJacob Keller /* Using the highest value for an unsigned 16-bit value 0xFFFF to indicate that
124d775155aSJacob Keller  * the max messages check must be ignored in the algorithm
125d775155aSJacob Keller  */
126d775155aSJacob Keller #define ICE_IGNORE_MAX_MSG_CNT	0xFFFF
127d775155aSJacob Keller 
128d775155aSJacob Keller /**
129504ce971SJacob Keller  * ice_mbx_reset_snapshot - Reset mailbox snapshot structure
1308cd8a6b1SJacob Keller  * @snap: pointer to the mailbox snapshot
131504ce971SJacob Keller  */
ice_mbx_reset_snapshot(struct ice_mbx_snapshot * snap)132504ce971SJacob Keller static void ice_mbx_reset_snapshot(struct ice_mbx_snapshot *snap)
133504ce971SJacob Keller {
1348cd8a6b1SJacob Keller 	struct ice_mbx_vf_info *vf_info;
135504ce971SJacob Keller 
1368cd8a6b1SJacob Keller 	/* Clear mbx_buf in the mailbox snaphot structure and setting the
1378cd8a6b1SJacob Keller 	 * mailbox snapshot state to a new capture.
1388cd8a6b1SJacob Keller 	 */
139504ce971SJacob Keller 	memset(&snap->mbx_buf, 0, sizeof(snap->mbx_buf));
140504ce971SJacob Keller 	snap->mbx_buf.state = ICE_MAL_VF_DETECT_STATE_NEW_SNAPSHOT;
1418cd8a6b1SJacob Keller 
1428cd8a6b1SJacob Keller 	/* Reset message counts for all VFs to zero */
1438cd8a6b1SJacob Keller 	list_for_each_entry(vf_info, &snap->mbx_vf, list_entry)
1448cd8a6b1SJacob Keller 		vf_info->msg_count = 0;
145504ce971SJacob Keller }
146504ce971SJacob Keller 
147504ce971SJacob Keller /**
148d775155aSJacob Keller  * ice_mbx_traverse - Pass through mailbox snapshot
149d775155aSJacob Keller  * @hw: pointer to the HW struct
150d775155aSJacob Keller  * @new_state: new algorithm state
151d775155aSJacob Keller  *
152d775155aSJacob Keller  * Traversing the mailbox static snapshot without checking
153d775155aSJacob Keller  * for malicious VFs.
154d775155aSJacob Keller  */
155d775155aSJacob Keller static void
ice_mbx_traverse(struct ice_hw * hw,enum ice_mbx_snapshot_state * new_state)156d775155aSJacob Keller ice_mbx_traverse(struct ice_hw *hw,
157d775155aSJacob Keller 		 enum ice_mbx_snapshot_state *new_state)
158d775155aSJacob Keller {
159d775155aSJacob Keller 	struct ice_mbx_snap_buffer_data *snap_buf;
160d775155aSJacob Keller 	u32 num_iterations;
161d775155aSJacob Keller 
162d775155aSJacob Keller 	snap_buf = &hw->mbx_snapshot.mbx_buf;
163d775155aSJacob Keller 
164d775155aSJacob Keller 	/* As mailbox buffer is circular, applying a mask
165d775155aSJacob Keller 	 * on the incremented iteration count.
166d775155aSJacob Keller 	 */
167d775155aSJacob Keller 	num_iterations = ICE_RQ_DATA_MASK(++snap_buf->num_iterations);
168d775155aSJacob Keller 
169d775155aSJacob Keller 	/* Checking either of the below conditions to exit snapshot traversal:
170d775155aSJacob Keller 	 * Condition-1: If the number of iterations in the mailbox is equal to
171d775155aSJacob Keller 	 * the mailbox head which would indicate that we have reached the end
172d775155aSJacob Keller 	 * of the static snapshot.
173d775155aSJacob Keller 	 * Condition-2: If the maximum messages serviced in the mailbox for a
174d775155aSJacob Keller 	 * given interrupt is the highest possible value then there is no need
175d775155aSJacob Keller 	 * to check if the number of messages processed is equal to it. If not
176d775155aSJacob Keller 	 * check if the number of messages processed is greater than or equal
177d775155aSJacob Keller 	 * to the maximum number of mailbox entries serviced in current work item.
178d775155aSJacob Keller 	 */
179d775155aSJacob Keller 	if (num_iterations == snap_buf->head ||
180d775155aSJacob Keller 	    (snap_buf->max_num_msgs_mbx < ICE_IGNORE_MAX_MSG_CNT &&
181d775155aSJacob Keller 	     ++snap_buf->num_msg_proc >= snap_buf->max_num_msgs_mbx))
182d775155aSJacob Keller 		*new_state = ICE_MAL_VF_DETECT_STATE_NEW_SNAPSHOT;
183d775155aSJacob Keller }
184d775155aSJacob Keller 
185d775155aSJacob Keller /**
186d775155aSJacob Keller  * ice_mbx_detect_malvf - Detect malicious VF in snapshot
187d775155aSJacob Keller  * @hw: pointer to the HW struct
1888cd8a6b1SJacob Keller  * @vf_info: mailbox tracking structure for a VF
189d775155aSJacob Keller  * @new_state: new algorithm state
190d775155aSJacob Keller  * @is_malvf: boolean output to indicate if VF is malicious
191d775155aSJacob Keller  *
192d775155aSJacob Keller  * This function tracks the number of asynchronous messages
193d775155aSJacob Keller  * sent per VF and marks the VF as malicious if it exceeds
194d775155aSJacob Keller  * the permissible number of messages to send.
195d775155aSJacob Keller  */
196d775155aSJacob Keller static int
ice_mbx_detect_malvf(struct ice_hw * hw,struct ice_mbx_vf_info * vf_info,enum ice_mbx_snapshot_state * new_state,bool * is_malvf)1978cd8a6b1SJacob Keller ice_mbx_detect_malvf(struct ice_hw *hw, struct ice_mbx_vf_info *vf_info,
198d775155aSJacob Keller 		     enum ice_mbx_snapshot_state *new_state,
199d775155aSJacob Keller 		     bool *is_malvf)
200d775155aSJacob Keller {
2018cd8a6b1SJacob Keller 	/* increment the message count for this VF */
2028cd8a6b1SJacob Keller 	vf_info->msg_count++;
203d775155aSJacob Keller 
2048cd8a6b1SJacob Keller 	if (vf_info->msg_count >= ICE_ASYNC_VF_MSG_THRESHOLD)
205d775155aSJacob Keller 		*is_malvf = true;
206d775155aSJacob Keller 
207d775155aSJacob Keller 	/* continue to iterate through the mailbox snapshot */
208d775155aSJacob Keller 	ice_mbx_traverse(hw, new_state);
209d775155aSJacob Keller 
210d775155aSJacob Keller 	return 0;
211d775155aSJacob Keller }
212d775155aSJacob Keller 
213d775155aSJacob Keller /**
214d775155aSJacob Keller  * ice_mbx_vf_state_handler - Handle states of the overflow algorithm
215d775155aSJacob Keller  * @hw: pointer to the HW struct
216d775155aSJacob Keller  * @mbx_data: pointer to structure containing mailbox data
2178cd8a6b1SJacob Keller  * @vf_info: mailbox tracking structure for the VF in question
218*07cc1a94SJacob Keller  * @report_malvf: boolean output to indicate whether VF should be reported
219d775155aSJacob Keller  *
220d775155aSJacob Keller  * The function serves as an entry point for the malicious VF
221d775155aSJacob Keller  * detection algorithm by handling the different states and state
222d775155aSJacob Keller  * transitions of the algorithm:
223d775155aSJacob Keller  * New snapshot: This state is entered when creating a new static
224d775155aSJacob Keller  * snapshot. The data from any previous mailbox snapshot is
225d775155aSJacob Keller  * cleared and a new capture of the mailbox head and tail is
226d775155aSJacob Keller  * logged. This will be the new static snapshot to detect
227d775155aSJacob Keller  * asynchronous messages sent by VFs. On capturing the snapshot
228d775155aSJacob Keller  * and depending on whether the number of pending messages in that
229d775155aSJacob Keller  * snapshot exceed the watermark value, the state machine enters
230d775155aSJacob Keller  * traverse or detect states.
231d775155aSJacob Keller  * Traverse: If pending message count is below watermark then iterate
232d775155aSJacob Keller  * through the snapshot without any action on VF.
233d775155aSJacob Keller  * Detect: If pending message count exceeds watermark traverse
234d775155aSJacob Keller  * the static snapshot and look for a malicious VF.
235d775155aSJacob Keller  */
236d775155aSJacob Keller int
ice_mbx_vf_state_handler(struct ice_hw * hw,struct ice_mbx_data * mbx_data,struct ice_mbx_vf_info * vf_info,bool * report_malvf)237*07cc1a94SJacob Keller ice_mbx_vf_state_handler(struct ice_hw *hw, struct ice_mbx_data *mbx_data,
238*07cc1a94SJacob Keller 			 struct ice_mbx_vf_info *vf_info, bool *report_malvf)
239d775155aSJacob Keller {
240d775155aSJacob Keller 	struct ice_mbx_snapshot *snap = &hw->mbx_snapshot;
241d775155aSJacob Keller 	struct ice_mbx_snap_buffer_data *snap_buf;
242d775155aSJacob Keller 	struct ice_ctl_q_info *cq = &hw->mailboxq;
243d775155aSJacob Keller 	enum ice_mbx_snapshot_state new_state;
244*07cc1a94SJacob Keller 	bool is_malvf = false;
245d775155aSJacob Keller 	int status = 0;
246d775155aSJacob Keller 
247*07cc1a94SJacob Keller 	if (!report_malvf || !mbx_data || !vf_info)
248d775155aSJacob Keller 		return -EINVAL;
249d775155aSJacob Keller 
250*07cc1a94SJacob Keller 	*report_malvf = false;
251*07cc1a94SJacob Keller 
252d775155aSJacob Keller 	/* When entering the mailbox state machine assume that the VF
253d775155aSJacob Keller 	 * is not malicious until detected.
254d775155aSJacob Keller 	 */
255d775155aSJacob Keller 	 /* Checking if max messages allowed to be processed while servicing current
256d775155aSJacob Keller 	  * interrupt is not less than the defined AVF message threshold.
257d775155aSJacob Keller 	  */
258d775155aSJacob Keller 	if (mbx_data->max_num_msgs_mbx <= ICE_ASYNC_VF_MSG_THRESHOLD)
259d775155aSJacob Keller 		return -EINVAL;
260d775155aSJacob Keller 
261d775155aSJacob Keller 	/* The watermark value should not be lesser than the threshold limit
262d775155aSJacob Keller 	 * set for the number of asynchronous messages a VF can send to mailbox
263d775155aSJacob Keller 	 * nor should it be greater than the maximum number of messages in the
264d775155aSJacob Keller 	 * mailbox serviced in current interrupt.
265d775155aSJacob Keller 	 */
266d775155aSJacob Keller 	if (mbx_data->async_watermark_val < ICE_ASYNC_VF_MSG_THRESHOLD ||
267d775155aSJacob Keller 	    mbx_data->async_watermark_val > mbx_data->max_num_msgs_mbx)
268d775155aSJacob Keller 		return -EINVAL;
269d775155aSJacob Keller 
270d775155aSJacob Keller 	new_state = ICE_MAL_VF_DETECT_STATE_INVALID;
271d775155aSJacob Keller 	snap_buf = &snap->mbx_buf;
272d775155aSJacob Keller 
273d775155aSJacob Keller 	switch (snap_buf->state) {
274d775155aSJacob Keller 	case ICE_MAL_VF_DETECT_STATE_NEW_SNAPSHOT:
275d775155aSJacob Keller 		/* Clear any previously held data in mailbox snapshot structure. */
276d775155aSJacob Keller 		ice_mbx_reset_snapshot(snap);
277d775155aSJacob Keller 
278d775155aSJacob Keller 		/* Collect the pending ARQ count, number of messages processed and
279d775155aSJacob Keller 		 * the maximum number of messages allowed to be processed from the
280d775155aSJacob Keller 		 * Mailbox for current interrupt.
281d775155aSJacob Keller 		 */
282d775155aSJacob Keller 		snap_buf->num_pending_arq = mbx_data->num_pending_arq;
283d775155aSJacob Keller 		snap_buf->num_msg_proc = mbx_data->num_msg_proc;
284d775155aSJacob Keller 		snap_buf->max_num_msgs_mbx = mbx_data->max_num_msgs_mbx;
285d775155aSJacob Keller 
286d775155aSJacob Keller 		/* Capture a new static snapshot of the mailbox by logging the
287d775155aSJacob Keller 		 * head and tail of snapshot and set num_iterations to the tail
288d775155aSJacob Keller 		 * value to mark the start of the iteration through the snapshot.
289d775155aSJacob Keller 		 */
290d775155aSJacob Keller 		snap_buf->head = ICE_RQ_DATA_MASK(cq->rq.next_to_clean +
291d775155aSJacob Keller 						  mbx_data->num_pending_arq);
292d775155aSJacob Keller 		snap_buf->tail = ICE_RQ_DATA_MASK(cq->rq.next_to_clean - 1);
293d775155aSJacob Keller 		snap_buf->num_iterations = snap_buf->tail;
294d775155aSJacob Keller 
295d775155aSJacob Keller 		/* Pending ARQ messages returned by ice_clean_rq_elem
296d775155aSJacob Keller 		 * is the difference between the head and tail of the
297d775155aSJacob Keller 		 * mailbox queue. Comparing this value against the watermark
298d775155aSJacob Keller 		 * helps to check if we potentially have malicious VFs.
299d775155aSJacob Keller 		 */
300d775155aSJacob Keller 		if (snap_buf->num_pending_arq >=
301d775155aSJacob Keller 		    mbx_data->async_watermark_val) {
302d775155aSJacob Keller 			new_state = ICE_MAL_VF_DETECT_STATE_DETECT;
303*07cc1a94SJacob Keller 			status = ice_mbx_detect_malvf(hw, vf_info, &new_state, &is_malvf);
304d775155aSJacob Keller 		} else {
305d775155aSJacob Keller 			new_state = ICE_MAL_VF_DETECT_STATE_TRAVERSE;
306d775155aSJacob Keller 			ice_mbx_traverse(hw, &new_state);
307d775155aSJacob Keller 		}
308d775155aSJacob Keller 		break;
309d775155aSJacob Keller 
310d775155aSJacob Keller 	case ICE_MAL_VF_DETECT_STATE_TRAVERSE:
311d775155aSJacob Keller 		new_state = ICE_MAL_VF_DETECT_STATE_TRAVERSE;
312d775155aSJacob Keller 		ice_mbx_traverse(hw, &new_state);
313d775155aSJacob Keller 		break;
314d775155aSJacob Keller 
315d775155aSJacob Keller 	case ICE_MAL_VF_DETECT_STATE_DETECT:
316d775155aSJacob Keller 		new_state = ICE_MAL_VF_DETECT_STATE_DETECT;
317*07cc1a94SJacob Keller 		status = ice_mbx_detect_malvf(hw, vf_info, &new_state, &is_malvf);
318d775155aSJacob Keller 		break;
319d775155aSJacob Keller 
320d775155aSJacob Keller 	default:
321d775155aSJacob Keller 		new_state = ICE_MAL_VF_DETECT_STATE_INVALID;
322d775155aSJacob Keller 		status = -EIO;
323d775155aSJacob Keller 	}
324d775155aSJacob Keller 
325d775155aSJacob Keller 	snap_buf->state = new_state;
326d775155aSJacob Keller 
327*07cc1a94SJacob Keller 	/* Only report VFs as malicious the first time we detect it */
328*07cc1a94SJacob Keller 	if (is_malvf && !vf_info->malicious) {
329*07cc1a94SJacob Keller 		vf_info->malicious = 1;
330*07cc1a94SJacob Keller 		*report_malvf = true;
331d775155aSJacob Keller 	}
332d775155aSJacob Keller 
333*07cc1a94SJacob Keller 	return status;
334d775155aSJacob Keller }
335d775155aSJacob Keller 
336d775155aSJacob Keller /**
3378cd8a6b1SJacob Keller  * ice_mbx_clear_malvf - Clear VF mailbox info
3388cd8a6b1SJacob Keller  * @vf_info: the mailbox tracking structure for a VF
339d775155aSJacob Keller  *
340e4eaf893SJacob Keller  * In case of a VF reset, this function shall be called to clear the VF's
341e4eaf893SJacob Keller  * current mailbox tracking state.
342d775155aSJacob Keller  */
ice_mbx_clear_malvf(struct ice_mbx_vf_info * vf_info)3438cd8a6b1SJacob Keller void ice_mbx_clear_malvf(struct ice_mbx_vf_info *vf_info)
344d775155aSJacob Keller {
345e4eaf893SJacob Keller 	vf_info->malicious = 0;
3468cd8a6b1SJacob Keller 	vf_info->msg_count = 0;
347d775155aSJacob Keller }
348d775155aSJacob Keller 
349d775155aSJacob Keller /**
350e4eaf893SJacob Keller  * ice_mbx_init_vf_info - Initialize a new VF mailbox tracking info
351e4eaf893SJacob Keller  * @hw: pointer to the hardware structure
352e4eaf893SJacob Keller  * @vf_info: the mailbox tracking info structure for a VF
353e4eaf893SJacob Keller  *
3548cd8a6b1SJacob Keller  * Initialize a VF mailbox tracking info structure and insert it into the
3558cd8a6b1SJacob Keller  * snapshot list.
3568cd8a6b1SJacob Keller  *
3578cd8a6b1SJacob Keller  * If you remove the VF, you must also delete the associated VF info structure
3588cd8a6b1SJacob Keller  * from the linked list.
359e4eaf893SJacob Keller  */
ice_mbx_init_vf_info(struct ice_hw * hw,struct ice_mbx_vf_info * vf_info)360e4eaf893SJacob Keller void ice_mbx_init_vf_info(struct ice_hw *hw, struct ice_mbx_vf_info *vf_info)
361e4eaf893SJacob Keller {
3628cd8a6b1SJacob Keller 	struct ice_mbx_snapshot *snap = &hw->mbx_snapshot;
3638cd8a6b1SJacob Keller 
3648cd8a6b1SJacob Keller 	ice_mbx_clear_malvf(vf_info);
3658cd8a6b1SJacob Keller 	list_add(&vf_info->list_entry, &snap->mbx_vf);
366e4eaf893SJacob Keller }
367e4eaf893SJacob Keller 
368e4eaf893SJacob Keller /**
3698cd8a6b1SJacob Keller  * ice_mbx_init_snapshot - Initialize mailbox snapshot data
370d775155aSJacob Keller  * @hw: pointer to the hardware structure
371d775155aSJacob Keller  *
3728cd8a6b1SJacob Keller  * Clear the mailbox snapshot structure and initialize the VF mailbox list.
373d775155aSJacob Keller  */
ice_mbx_init_snapshot(struct ice_hw * hw)3748cd8a6b1SJacob Keller void ice_mbx_init_snapshot(struct ice_hw *hw)
375d775155aSJacob Keller {
376d775155aSJacob Keller 	struct ice_mbx_snapshot *snap = &hw->mbx_snapshot;
377d775155aSJacob Keller 
3788cd8a6b1SJacob Keller 	INIT_LIST_HEAD(&snap->mbx_vf);
3798cd8a6b1SJacob Keller 	ice_mbx_reset_snapshot(snap);
380d775155aSJacob Keller }
381