xref: /openbmc/linux/drivers/net/ethernet/intel/ice/ice_vf_mbx.c (revision 0f9b4c3ca5fdf3e177266ef994071b1a03f07318)
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 /**
214*52a98adcSPaul Greenwalt  * ice_mbx_vf_dec_trig_e830 - Decrements the VF mailbox queue counter
215*52a98adcSPaul Greenwalt  * @hw: pointer to the HW struct
216*52a98adcSPaul Greenwalt  * @event: pointer to the control queue receive event
217*52a98adcSPaul Greenwalt  *
218*52a98adcSPaul Greenwalt  * This function triggers to decrement the counter
219*52a98adcSPaul Greenwalt  * MBX_VF_IN_FLIGHT_MSGS_AT_PF_CNT when the driver replenishes
220*52a98adcSPaul Greenwalt  * the buffers at the PF mailbox queue.
221*52a98adcSPaul Greenwalt  */
ice_mbx_vf_dec_trig_e830(const struct ice_hw * hw,const struct ice_rq_event_info * event)222*52a98adcSPaul Greenwalt void ice_mbx_vf_dec_trig_e830(const struct ice_hw *hw,
223*52a98adcSPaul Greenwalt 			      const struct ice_rq_event_info *event)
224*52a98adcSPaul Greenwalt {
225*52a98adcSPaul Greenwalt 	u16 vfid = le16_to_cpu(event->desc.retval);
226*52a98adcSPaul Greenwalt 
227*52a98adcSPaul Greenwalt 	wr32(hw, E830_MBX_VF_DEC_TRIG(vfid), 1);
228*52a98adcSPaul Greenwalt }
229*52a98adcSPaul Greenwalt 
230*52a98adcSPaul Greenwalt /**
231*52a98adcSPaul Greenwalt  * ice_mbx_vf_clear_cnt_e830 - Clear the VF mailbox queue count
232*52a98adcSPaul Greenwalt  * @hw: pointer to the HW struct
233*52a98adcSPaul Greenwalt  * @vf_id: VF ID in the PF space
234*52a98adcSPaul Greenwalt  *
235*52a98adcSPaul Greenwalt  * This function clears the counter MBX_VF_IN_FLIGHT_MSGS_AT_PF_CNT, and should
236*52a98adcSPaul Greenwalt  * be called when a VF is created and on VF reset.
237*52a98adcSPaul Greenwalt  */
ice_mbx_vf_clear_cnt_e830(const struct ice_hw * hw,u16 vf_id)238*52a98adcSPaul Greenwalt void ice_mbx_vf_clear_cnt_e830(const struct ice_hw *hw, u16 vf_id)
239*52a98adcSPaul Greenwalt {
240*52a98adcSPaul Greenwalt 	u32 reg = rd32(hw, E830_MBX_VF_IN_FLIGHT_MSGS_AT_PF_CNT(vf_id));
241*52a98adcSPaul Greenwalt 
242*52a98adcSPaul Greenwalt 	wr32(hw, E830_MBX_VF_DEC_TRIG(vf_id), reg);
243*52a98adcSPaul Greenwalt }
244*52a98adcSPaul Greenwalt 
245*52a98adcSPaul Greenwalt /**
246d775155aSJacob Keller  * ice_mbx_vf_state_handler - Handle states of the overflow algorithm
247d775155aSJacob Keller  * @hw: pointer to the HW struct
248d775155aSJacob Keller  * @mbx_data: pointer to structure containing mailbox data
2498cd8a6b1SJacob Keller  * @vf_info: mailbox tracking structure for the VF in question
25007cc1a94SJacob Keller  * @report_malvf: boolean output to indicate whether VF should be reported
251d775155aSJacob Keller  *
252d775155aSJacob Keller  * The function serves as an entry point for the malicious VF
253d775155aSJacob Keller  * detection algorithm by handling the different states and state
254d775155aSJacob Keller  * transitions of the algorithm:
255d775155aSJacob Keller  * New snapshot: This state is entered when creating a new static
256d775155aSJacob Keller  * snapshot. The data from any previous mailbox snapshot is
257d775155aSJacob Keller  * cleared and a new capture of the mailbox head and tail is
258d775155aSJacob Keller  * logged. This will be the new static snapshot to detect
259d775155aSJacob Keller  * asynchronous messages sent by VFs. On capturing the snapshot
260d775155aSJacob Keller  * and depending on whether the number of pending messages in that
261d775155aSJacob Keller  * snapshot exceed the watermark value, the state machine enters
262d775155aSJacob Keller  * traverse or detect states.
263d775155aSJacob Keller  * Traverse: If pending message count is below watermark then iterate
264d775155aSJacob Keller  * through the snapshot without any action on VF.
265d775155aSJacob Keller  * Detect: If pending message count exceeds watermark traverse
266d775155aSJacob Keller  * the static snapshot and look for a malicious VF.
267d775155aSJacob Keller  */
268d775155aSJacob 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)26907cc1a94SJacob Keller ice_mbx_vf_state_handler(struct ice_hw *hw, struct ice_mbx_data *mbx_data,
27007cc1a94SJacob Keller 			 struct ice_mbx_vf_info *vf_info, bool *report_malvf)
271d775155aSJacob Keller {
272d775155aSJacob Keller 	struct ice_mbx_snapshot *snap = &hw->mbx_snapshot;
273d775155aSJacob Keller 	struct ice_mbx_snap_buffer_data *snap_buf;
274d775155aSJacob Keller 	struct ice_ctl_q_info *cq = &hw->mailboxq;
275d775155aSJacob Keller 	enum ice_mbx_snapshot_state new_state;
27607cc1a94SJacob Keller 	bool is_malvf = false;
277d775155aSJacob Keller 	int status = 0;
278d775155aSJacob Keller 
27907cc1a94SJacob Keller 	if (!report_malvf || !mbx_data || !vf_info)
280d775155aSJacob Keller 		return -EINVAL;
281d775155aSJacob Keller 
28207cc1a94SJacob Keller 	*report_malvf = false;
28307cc1a94SJacob Keller 
284d775155aSJacob Keller 	/* When entering the mailbox state machine assume that the VF
285d775155aSJacob Keller 	 * is not malicious until detected.
286d775155aSJacob Keller 	 */
287d775155aSJacob Keller 	 /* Checking if max messages allowed to be processed while servicing current
288d775155aSJacob Keller 	  * interrupt is not less than the defined AVF message threshold.
289d775155aSJacob Keller 	  */
290d775155aSJacob Keller 	if (mbx_data->max_num_msgs_mbx <= ICE_ASYNC_VF_MSG_THRESHOLD)
291d775155aSJacob Keller 		return -EINVAL;
292d775155aSJacob Keller 
293d775155aSJacob Keller 	/* The watermark value should not be lesser than the threshold limit
294d775155aSJacob Keller 	 * set for the number of asynchronous messages a VF can send to mailbox
295d775155aSJacob Keller 	 * nor should it be greater than the maximum number of messages in the
296d775155aSJacob Keller 	 * mailbox serviced in current interrupt.
297d775155aSJacob Keller 	 */
298d775155aSJacob Keller 	if (mbx_data->async_watermark_val < ICE_ASYNC_VF_MSG_THRESHOLD ||
299d775155aSJacob Keller 	    mbx_data->async_watermark_val > mbx_data->max_num_msgs_mbx)
300d775155aSJacob Keller 		return -EINVAL;
301d775155aSJacob Keller 
302d775155aSJacob Keller 	new_state = ICE_MAL_VF_DETECT_STATE_INVALID;
303d775155aSJacob Keller 	snap_buf = &snap->mbx_buf;
304d775155aSJacob Keller 
305d775155aSJacob Keller 	switch (snap_buf->state) {
306d775155aSJacob Keller 	case ICE_MAL_VF_DETECT_STATE_NEW_SNAPSHOT:
307d775155aSJacob Keller 		/* Clear any previously held data in mailbox snapshot structure. */
308d775155aSJacob Keller 		ice_mbx_reset_snapshot(snap);
309d775155aSJacob Keller 
310d775155aSJacob Keller 		/* Collect the pending ARQ count, number of messages processed and
311d775155aSJacob Keller 		 * the maximum number of messages allowed to be processed from the
312d775155aSJacob Keller 		 * Mailbox for current interrupt.
313d775155aSJacob Keller 		 */
314d775155aSJacob Keller 		snap_buf->num_pending_arq = mbx_data->num_pending_arq;
315d775155aSJacob Keller 		snap_buf->num_msg_proc = mbx_data->num_msg_proc;
316d775155aSJacob Keller 		snap_buf->max_num_msgs_mbx = mbx_data->max_num_msgs_mbx;
317d775155aSJacob Keller 
318d775155aSJacob Keller 		/* Capture a new static snapshot of the mailbox by logging the
319d775155aSJacob Keller 		 * head and tail of snapshot and set num_iterations to the tail
320d775155aSJacob Keller 		 * value to mark the start of the iteration through the snapshot.
321d775155aSJacob Keller 		 */
322d775155aSJacob Keller 		snap_buf->head = ICE_RQ_DATA_MASK(cq->rq.next_to_clean +
323d775155aSJacob Keller 						  mbx_data->num_pending_arq);
324d775155aSJacob Keller 		snap_buf->tail = ICE_RQ_DATA_MASK(cq->rq.next_to_clean - 1);
325d775155aSJacob Keller 		snap_buf->num_iterations = snap_buf->tail;
326d775155aSJacob Keller 
327d775155aSJacob Keller 		/* Pending ARQ messages returned by ice_clean_rq_elem
328d775155aSJacob Keller 		 * is the difference between the head and tail of the
329d775155aSJacob Keller 		 * mailbox queue. Comparing this value against the watermark
330d775155aSJacob Keller 		 * helps to check if we potentially have malicious VFs.
331d775155aSJacob Keller 		 */
332d775155aSJacob Keller 		if (snap_buf->num_pending_arq >=
333d775155aSJacob Keller 		    mbx_data->async_watermark_val) {
334d775155aSJacob Keller 			new_state = ICE_MAL_VF_DETECT_STATE_DETECT;
33507cc1a94SJacob Keller 			status = ice_mbx_detect_malvf(hw, vf_info, &new_state, &is_malvf);
336d775155aSJacob Keller 		} else {
337d775155aSJacob Keller 			new_state = ICE_MAL_VF_DETECT_STATE_TRAVERSE;
338d775155aSJacob Keller 			ice_mbx_traverse(hw, &new_state);
339d775155aSJacob Keller 		}
340d775155aSJacob Keller 		break;
341d775155aSJacob Keller 
342d775155aSJacob Keller 	case ICE_MAL_VF_DETECT_STATE_TRAVERSE:
343d775155aSJacob Keller 		new_state = ICE_MAL_VF_DETECT_STATE_TRAVERSE;
344d775155aSJacob Keller 		ice_mbx_traverse(hw, &new_state);
345d775155aSJacob Keller 		break;
346d775155aSJacob Keller 
347d775155aSJacob Keller 	case ICE_MAL_VF_DETECT_STATE_DETECT:
348d775155aSJacob Keller 		new_state = ICE_MAL_VF_DETECT_STATE_DETECT;
34907cc1a94SJacob Keller 		status = ice_mbx_detect_malvf(hw, vf_info, &new_state, &is_malvf);
350d775155aSJacob Keller 		break;
351d775155aSJacob Keller 
352d775155aSJacob Keller 	default:
353d775155aSJacob Keller 		new_state = ICE_MAL_VF_DETECT_STATE_INVALID;
354d775155aSJacob Keller 		status = -EIO;
355d775155aSJacob Keller 	}
356d775155aSJacob Keller 
357d775155aSJacob Keller 	snap_buf->state = new_state;
358d775155aSJacob Keller 
35907cc1a94SJacob Keller 	/* Only report VFs as malicious the first time we detect it */
36007cc1a94SJacob Keller 	if (is_malvf && !vf_info->malicious) {
36107cc1a94SJacob Keller 		vf_info->malicious = 1;
36207cc1a94SJacob Keller 		*report_malvf = true;
363d775155aSJacob Keller 	}
364d775155aSJacob Keller 
36507cc1a94SJacob Keller 	return status;
366d775155aSJacob Keller }
367d775155aSJacob Keller 
368d775155aSJacob Keller /**
3698cd8a6b1SJacob Keller  * ice_mbx_clear_malvf - Clear VF mailbox info
3708cd8a6b1SJacob Keller  * @vf_info: the mailbox tracking structure for a VF
371d775155aSJacob Keller  *
372e4eaf893SJacob Keller  * In case of a VF reset, this function shall be called to clear the VF's
373e4eaf893SJacob Keller  * current mailbox tracking state.
374d775155aSJacob Keller  */
ice_mbx_clear_malvf(struct ice_mbx_vf_info * vf_info)3758cd8a6b1SJacob Keller void ice_mbx_clear_malvf(struct ice_mbx_vf_info *vf_info)
376d775155aSJacob Keller {
377e4eaf893SJacob Keller 	vf_info->malicious = 0;
3788cd8a6b1SJacob Keller 	vf_info->msg_count = 0;
379d775155aSJacob Keller }
380d775155aSJacob Keller 
381d775155aSJacob Keller /**
382e4eaf893SJacob Keller  * ice_mbx_init_vf_info - Initialize a new VF mailbox tracking info
383e4eaf893SJacob Keller  * @hw: pointer to the hardware structure
384e4eaf893SJacob Keller  * @vf_info: the mailbox tracking info structure for a VF
385e4eaf893SJacob Keller  *
3868cd8a6b1SJacob Keller  * Initialize a VF mailbox tracking info structure and insert it into the
3878cd8a6b1SJacob Keller  * snapshot list.
3888cd8a6b1SJacob Keller  *
3898cd8a6b1SJacob Keller  * If you remove the VF, you must also delete the associated VF info structure
3908cd8a6b1SJacob Keller  * from the linked list.
391e4eaf893SJacob Keller  */
ice_mbx_init_vf_info(struct ice_hw * hw,struct ice_mbx_vf_info * vf_info)392e4eaf893SJacob Keller void ice_mbx_init_vf_info(struct ice_hw *hw, struct ice_mbx_vf_info *vf_info)
393e4eaf893SJacob Keller {
3948cd8a6b1SJacob Keller 	struct ice_mbx_snapshot *snap = &hw->mbx_snapshot;
3958cd8a6b1SJacob Keller 
3968cd8a6b1SJacob Keller 	ice_mbx_clear_malvf(vf_info);
3978cd8a6b1SJacob Keller 	list_add(&vf_info->list_entry, &snap->mbx_vf);
398e4eaf893SJacob Keller }
399e4eaf893SJacob Keller 
400e4eaf893SJacob Keller /**
4018cd8a6b1SJacob Keller  * ice_mbx_init_snapshot - Initialize mailbox snapshot data
402d775155aSJacob Keller  * @hw: pointer to the hardware structure
403d775155aSJacob Keller  *
4048cd8a6b1SJacob Keller  * Clear the mailbox snapshot structure and initialize the VF mailbox list.
405d775155aSJacob Keller  */
ice_mbx_init_snapshot(struct ice_hw * hw)4068cd8a6b1SJacob Keller void ice_mbx_init_snapshot(struct ice_hw *hw)
407d775155aSJacob Keller {
408d775155aSJacob Keller 	struct ice_mbx_snapshot *snap = &hw->mbx_snapshot;
409d775155aSJacob Keller 
4108cd8a6b1SJacob Keller 	INIT_LIST_HEAD(&snap->mbx_vf);
4118cd8a6b1SJacob Keller 	ice_mbx_reset_snapshot(snap);
412d775155aSJacob Keller }
413