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
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 
42*1d0e28a9SBrett Creeley static const u32 ice_legacy_aq_to_vc_speed[15] = {
43*1d0e28a9SBrett Creeley 	VIRTCHNL_LINK_SPEED_100MB,	/* BIT(0) */
44*1d0e28a9SBrett Creeley 	VIRTCHNL_LINK_SPEED_100MB,
45*1d0e28a9SBrett Creeley 	VIRTCHNL_LINK_SPEED_1GB,
46*1d0e28a9SBrett Creeley 	VIRTCHNL_LINK_SPEED_1GB,
47*1d0e28a9SBrett Creeley 	VIRTCHNL_LINK_SPEED_1GB,
48*1d0e28a9SBrett Creeley 	VIRTCHNL_LINK_SPEED_10GB,
49*1d0e28a9SBrett Creeley 	VIRTCHNL_LINK_SPEED_20GB,
50*1d0e28a9SBrett Creeley 	VIRTCHNL_LINK_SPEED_25GB,
51*1d0e28a9SBrett Creeley 	VIRTCHNL_LINK_SPEED_40GB,
52*1d0e28a9SBrett Creeley 	VIRTCHNL_LINK_SPEED_40GB,
53*1d0e28a9SBrett Creeley 	VIRTCHNL_LINK_SPEED_40GB,
54*1d0e28a9SBrett Creeley 	VIRTCHNL_LINK_SPEED_UNKNOWN,
55*1d0e28a9SBrett Creeley 	VIRTCHNL_LINK_SPEED_UNKNOWN,
56*1d0e28a9SBrett Creeley 	VIRTCHNL_LINK_SPEED_UNKNOWN,
57*1d0e28a9SBrett Creeley 	VIRTCHNL_LINK_SPEED_UNKNOWN	/* BIT(14) */
58*1d0e28a9SBrett Creeley };
59*1d0e28a9SBrett Creeley 
60d775155aSJacob Keller /**
61d775155aSJacob Keller  * ice_conv_link_speed_to_virtchnl
62d775155aSJacob Keller  * @adv_link_support: determines the format of the returned link speed
63d775155aSJacob Keller  * @link_speed: variable containing the link_speed to be converted
64d775155aSJacob Keller  *
65d775155aSJacob Keller  * Convert link speed supported by HW to link speed supported by virtchnl.
66d775155aSJacob Keller  * If adv_link_support is true, then return link speed in Mbps. Else return
67d775155aSJacob Keller  * link speed as a VIRTCHNL_LINK_SPEED_* casted to a u32. Note that the caller
68d775155aSJacob Keller  * needs to cast back to an enum virtchnl_link_speed in the case where
69d775155aSJacob Keller  * adv_link_support is false, but when adv_link_support is true the caller can
70d775155aSJacob Keller  * expect the speed in Mbps.
71d775155aSJacob Keller  */
72d775155aSJacob Keller u32 ice_conv_link_speed_to_virtchnl(bool adv_link_support, u16 link_speed)
73d775155aSJacob Keller {
74d775155aSJacob Keller 	u32 speed;
75d775155aSJacob Keller 
76*1d0e28a9SBrett Creeley 	if (adv_link_support) {
77*1d0e28a9SBrett Creeley 		/* convert a BIT() value into an array index */
78*1d0e28a9SBrett Creeley 		speed = ice_get_link_speed(fls(link_speed) - 1);
79*1d0e28a9SBrett Creeley 	} else {
80d775155aSJacob Keller 		/* Virtchnl speeds are not defined for every speed supported in
81d775155aSJacob Keller 		 * the hardware. To maintain compatibility with older AVF
82d775155aSJacob Keller 		 * drivers, while reporting the speed the new speed values are
83d775155aSJacob Keller 		 * resolved to the closest known virtchnl speeds
84d775155aSJacob Keller 		 */
85*1d0e28a9SBrett Creeley 		speed = ice_legacy_aq_to_vc_speed[fls(link_speed) - 1];
86d775155aSJacob Keller 	}
87d775155aSJacob Keller 
88d775155aSJacob Keller 	return speed;
89d775155aSJacob Keller }
90d775155aSJacob Keller 
91d775155aSJacob Keller /* The mailbox overflow detection algorithm helps to check if there
92d775155aSJacob Keller  * is a possibility of a malicious VF transmitting too many MBX messages to the
93d775155aSJacob Keller  * PF.
94d775155aSJacob Keller  * 1. The mailbox snapshot structure, ice_mbx_snapshot, is initialized during
95d775155aSJacob Keller  * driver initialization in ice_init_hw() using ice_mbx_init_snapshot().
96d775155aSJacob Keller  * The struct ice_mbx_snapshot helps to track and traverse a static window of
97d775155aSJacob Keller  * messages within the mailbox queue while looking for a malicious VF.
98d775155aSJacob Keller  *
99d775155aSJacob Keller  * 2. When the caller starts processing its mailbox queue in response to an
100d775155aSJacob Keller  * interrupt, the structure ice_mbx_snapshot is expected to be cleared before
101d775155aSJacob Keller  * the algorithm can be run for the first time for that interrupt. This can be
102d775155aSJacob Keller  * done via ice_mbx_reset_snapshot().
103d775155aSJacob Keller  *
104d775155aSJacob Keller  * 3. For every message read by the caller from the MBX Queue, the caller must
105d775155aSJacob Keller  * call the detection algorithm's entry function ice_mbx_vf_state_handler().
106d775155aSJacob Keller  * Before every call to ice_mbx_vf_state_handler() the struct ice_mbx_data is
107d775155aSJacob Keller  * filled as it is required to be passed to the algorithm.
108d775155aSJacob Keller  *
109d775155aSJacob Keller  * 4. Every time a message is read from the MBX queue, a VFId is received which
110d775155aSJacob Keller  * is passed to the state handler. The boolean output is_malvf of the state
111d775155aSJacob Keller  * handler ice_mbx_vf_state_handler() serves as an indicator to the caller
112d775155aSJacob Keller  * whether this VF is malicious or not.
113d775155aSJacob Keller  *
114d775155aSJacob Keller  * 5. When a VF is identified to be malicious, the caller can send a message
115d775155aSJacob Keller  * to the system administrator. The caller can invoke ice_mbx_report_malvf()
116d775155aSJacob Keller  * to help determine if a malicious VF is to be reported or not. This function
117d775155aSJacob Keller  * requires the caller to maintain a global bitmap to track all malicious VFs
118d775155aSJacob Keller  * and pass that to ice_mbx_report_malvf() along with the VFID which was identified
119d775155aSJacob Keller  * to be malicious by ice_mbx_vf_state_handler().
120d775155aSJacob Keller  *
121d775155aSJacob Keller  * 6. The global bitmap maintained by PF can be cleared completely if PF is in
122d775155aSJacob Keller  * reset or the bit corresponding to a VF can be cleared if that VF is in reset.
123d775155aSJacob Keller  * When a VF is shut down and brought back up, we assume that the new VF
124d775155aSJacob Keller  * brought up is not malicious and hence report it if found malicious.
125d775155aSJacob Keller  *
126d775155aSJacob Keller  * 7. The function ice_mbx_reset_snapshot() is called to reset the information
127d775155aSJacob Keller  * in ice_mbx_snapshot for every new mailbox interrupt handled.
128d775155aSJacob Keller  *
129d775155aSJacob Keller  * 8. The memory allocated for variables in ice_mbx_snapshot is de-allocated
130d775155aSJacob Keller  * when driver is unloaded.
131d775155aSJacob Keller  */
132d775155aSJacob Keller #define ICE_RQ_DATA_MASK(rq_data) ((rq_data) & PF_MBX_ARQH_ARQH_M)
133d775155aSJacob Keller /* Using the highest value for an unsigned 16-bit value 0xFFFF to indicate that
134d775155aSJacob Keller  * the max messages check must be ignored in the algorithm
135d775155aSJacob Keller  */
136d775155aSJacob Keller #define ICE_IGNORE_MAX_MSG_CNT	0xFFFF
137d775155aSJacob Keller 
138d775155aSJacob Keller /**
139d775155aSJacob Keller  * ice_mbx_traverse - Pass through mailbox snapshot
140d775155aSJacob Keller  * @hw: pointer to the HW struct
141d775155aSJacob Keller  * @new_state: new algorithm state
142d775155aSJacob Keller  *
143d775155aSJacob Keller  * Traversing the mailbox static snapshot without checking
144d775155aSJacob Keller  * for malicious VFs.
145d775155aSJacob Keller  */
146d775155aSJacob Keller static void
147d775155aSJacob Keller ice_mbx_traverse(struct ice_hw *hw,
148d775155aSJacob Keller 		 enum ice_mbx_snapshot_state *new_state)
149d775155aSJacob Keller {
150d775155aSJacob Keller 	struct ice_mbx_snap_buffer_data *snap_buf;
151d775155aSJacob Keller 	u32 num_iterations;
152d775155aSJacob Keller 
153d775155aSJacob Keller 	snap_buf = &hw->mbx_snapshot.mbx_buf;
154d775155aSJacob Keller 
155d775155aSJacob Keller 	/* As mailbox buffer is circular, applying a mask
156d775155aSJacob Keller 	 * on the incremented iteration count.
157d775155aSJacob Keller 	 */
158d775155aSJacob Keller 	num_iterations = ICE_RQ_DATA_MASK(++snap_buf->num_iterations);
159d775155aSJacob Keller 
160d775155aSJacob Keller 	/* Checking either of the below conditions to exit snapshot traversal:
161d775155aSJacob Keller 	 * Condition-1: If the number of iterations in the mailbox is equal to
162d775155aSJacob Keller 	 * the mailbox head which would indicate that we have reached the end
163d775155aSJacob Keller 	 * of the static snapshot.
164d775155aSJacob Keller 	 * Condition-2: If the maximum messages serviced in the mailbox for a
165d775155aSJacob Keller 	 * given interrupt is the highest possible value then there is no need
166d775155aSJacob Keller 	 * to check if the number of messages processed is equal to it. If not
167d775155aSJacob Keller 	 * check if the number of messages processed is greater than or equal
168d775155aSJacob Keller 	 * to the maximum number of mailbox entries serviced in current work item.
169d775155aSJacob Keller 	 */
170d775155aSJacob Keller 	if (num_iterations == snap_buf->head ||
171d775155aSJacob Keller 	    (snap_buf->max_num_msgs_mbx < ICE_IGNORE_MAX_MSG_CNT &&
172d775155aSJacob Keller 	     ++snap_buf->num_msg_proc >= snap_buf->max_num_msgs_mbx))
173d775155aSJacob Keller 		*new_state = ICE_MAL_VF_DETECT_STATE_NEW_SNAPSHOT;
174d775155aSJacob Keller }
175d775155aSJacob Keller 
176d775155aSJacob Keller /**
177d775155aSJacob Keller  * ice_mbx_detect_malvf - Detect malicious VF in snapshot
178d775155aSJacob Keller  * @hw: pointer to the HW struct
179d775155aSJacob Keller  * @vf_id: relative virtual function ID
180d775155aSJacob Keller  * @new_state: new algorithm state
181d775155aSJacob Keller  * @is_malvf: boolean output to indicate if VF is malicious
182d775155aSJacob Keller  *
183d775155aSJacob Keller  * This function tracks the number of asynchronous messages
184d775155aSJacob Keller  * sent per VF and marks the VF as malicious if it exceeds
185d775155aSJacob Keller  * the permissible number of messages to send.
186d775155aSJacob Keller  */
187d775155aSJacob Keller static int
188d775155aSJacob Keller ice_mbx_detect_malvf(struct ice_hw *hw, u16 vf_id,
189d775155aSJacob Keller 		     enum ice_mbx_snapshot_state *new_state,
190d775155aSJacob Keller 		     bool *is_malvf)
191d775155aSJacob Keller {
192d775155aSJacob Keller 	struct ice_mbx_snapshot *snap = &hw->mbx_snapshot;
193d775155aSJacob Keller 
194d775155aSJacob Keller 	if (vf_id >= snap->mbx_vf.vfcntr_len)
195d775155aSJacob Keller 		return -EIO;
196d775155aSJacob Keller 
197d775155aSJacob Keller 	/* increment the message count in the VF array */
198d775155aSJacob Keller 	snap->mbx_vf.vf_cntr[vf_id]++;
199d775155aSJacob Keller 
200d775155aSJacob Keller 	if (snap->mbx_vf.vf_cntr[vf_id] >= ICE_ASYNC_VF_MSG_THRESHOLD)
201d775155aSJacob Keller 		*is_malvf = true;
202d775155aSJacob Keller 
203d775155aSJacob Keller 	/* continue to iterate through the mailbox snapshot */
204d775155aSJacob Keller 	ice_mbx_traverse(hw, new_state);
205d775155aSJacob Keller 
206d775155aSJacob Keller 	return 0;
207d775155aSJacob Keller }
208d775155aSJacob Keller 
209d775155aSJacob Keller /**
210d775155aSJacob Keller  * ice_mbx_reset_snapshot - Reset mailbox snapshot structure
211d775155aSJacob Keller  * @snap: pointer to mailbox snapshot structure in the ice_hw struct
212d775155aSJacob Keller  *
213d775155aSJacob Keller  * Reset the mailbox snapshot structure and clear VF counter array.
214d775155aSJacob Keller  */
215d775155aSJacob Keller static void ice_mbx_reset_snapshot(struct ice_mbx_snapshot *snap)
216d775155aSJacob Keller {
217d775155aSJacob Keller 	u32 vfcntr_len;
218d775155aSJacob Keller 
219d775155aSJacob Keller 	if (!snap || !snap->mbx_vf.vf_cntr)
220d775155aSJacob Keller 		return;
221d775155aSJacob Keller 
222d775155aSJacob Keller 	/* Clear VF counters. */
223d775155aSJacob Keller 	vfcntr_len = snap->mbx_vf.vfcntr_len;
224d775155aSJacob Keller 	if (vfcntr_len)
225d775155aSJacob Keller 		memset(snap->mbx_vf.vf_cntr, 0,
226d775155aSJacob Keller 		       (vfcntr_len * sizeof(*snap->mbx_vf.vf_cntr)));
227d775155aSJacob Keller 
228d775155aSJacob Keller 	/* Reset mailbox snapshot for a new capture. */
229d775155aSJacob Keller 	memset(&snap->mbx_buf, 0, sizeof(snap->mbx_buf));
230d775155aSJacob Keller 	snap->mbx_buf.state = ICE_MAL_VF_DETECT_STATE_NEW_SNAPSHOT;
231d775155aSJacob Keller }
232d775155aSJacob Keller 
233d775155aSJacob Keller /**
234d775155aSJacob Keller  * ice_mbx_vf_state_handler - Handle states of the overflow algorithm
235d775155aSJacob Keller  * @hw: pointer to the HW struct
236d775155aSJacob Keller  * @mbx_data: pointer to structure containing mailbox data
237d775155aSJacob Keller  * @vf_id: relative virtual function (VF) ID
238d775155aSJacob Keller  * @is_malvf: boolean output to indicate if VF is malicious
239d775155aSJacob Keller  *
240d775155aSJacob Keller  * The function serves as an entry point for the malicious VF
241d775155aSJacob Keller  * detection algorithm by handling the different states and state
242d775155aSJacob Keller  * transitions of the algorithm:
243d775155aSJacob Keller  * New snapshot: This state is entered when creating a new static
244d775155aSJacob Keller  * snapshot. The data from any previous mailbox snapshot is
245d775155aSJacob Keller  * cleared and a new capture of the mailbox head and tail is
246d775155aSJacob Keller  * logged. This will be the new static snapshot to detect
247d775155aSJacob Keller  * asynchronous messages sent by VFs. On capturing the snapshot
248d775155aSJacob Keller  * and depending on whether the number of pending messages in that
249d775155aSJacob Keller  * snapshot exceed the watermark value, the state machine enters
250d775155aSJacob Keller  * traverse or detect states.
251d775155aSJacob Keller  * Traverse: If pending message count is below watermark then iterate
252d775155aSJacob Keller  * through the snapshot without any action on VF.
253d775155aSJacob Keller  * Detect: If pending message count exceeds watermark traverse
254d775155aSJacob Keller  * the static snapshot and look for a malicious VF.
255d775155aSJacob Keller  */
256d775155aSJacob Keller int
257d775155aSJacob Keller ice_mbx_vf_state_handler(struct ice_hw *hw,
258d775155aSJacob Keller 			 struct ice_mbx_data *mbx_data, u16 vf_id,
259d775155aSJacob Keller 			 bool *is_malvf)
260d775155aSJacob Keller {
261d775155aSJacob Keller 	struct ice_mbx_snapshot *snap = &hw->mbx_snapshot;
262d775155aSJacob Keller 	struct ice_mbx_snap_buffer_data *snap_buf;
263d775155aSJacob Keller 	struct ice_ctl_q_info *cq = &hw->mailboxq;
264d775155aSJacob Keller 	enum ice_mbx_snapshot_state new_state;
265d775155aSJacob Keller 	int status = 0;
266d775155aSJacob Keller 
267d775155aSJacob Keller 	if (!is_malvf || !mbx_data)
268d775155aSJacob Keller 		return -EINVAL;
269d775155aSJacob Keller 
270d775155aSJacob Keller 	/* When entering the mailbox state machine assume that the VF
271d775155aSJacob Keller 	 * is not malicious until detected.
272d775155aSJacob Keller 	 */
273d775155aSJacob Keller 	*is_malvf = false;
274d775155aSJacob Keller 
275d775155aSJacob Keller 	 /* Checking if max messages allowed to be processed while servicing current
276d775155aSJacob Keller 	  * interrupt is not less than the defined AVF message threshold.
277d775155aSJacob Keller 	  */
278d775155aSJacob Keller 	if (mbx_data->max_num_msgs_mbx <= ICE_ASYNC_VF_MSG_THRESHOLD)
279d775155aSJacob Keller 		return -EINVAL;
280d775155aSJacob Keller 
281d775155aSJacob Keller 	/* The watermark value should not be lesser than the threshold limit
282d775155aSJacob Keller 	 * set for the number of asynchronous messages a VF can send to mailbox
283d775155aSJacob Keller 	 * nor should it be greater than the maximum number of messages in the
284d775155aSJacob Keller 	 * mailbox serviced in current interrupt.
285d775155aSJacob Keller 	 */
286d775155aSJacob Keller 	if (mbx_data->async_watermark_val < ICE_ASYNC_VF_MSG_THRESHOLD ||
287d775155aSJacob Keller 	    mbx_data->async_watermark_val > mbx_data->max_num_msgs_mbx)
288d775155aSJacob Keller 		return -EINVAL;
289d775155aSJacob Keller 
290d775155aSJacob Keller 	new_state = ICE_MAL_VF_DETECT_STATE_INVALID;
291d775155aSJacob Keller 	snap_buf = &snap->mbx_buf;
292d775155aSJacob Keller 
293d775155aSJacob Keller 	switch (snap_buf->state) {
294d775155aSJacob Keller 	case ICE_MAL_VF_DETECT_STATE_NEW_SNAPSHOT:
295d775155aSJacob Keller 		/* Clear any previously held data in mailbox snapshot structure. */
296d775155aSJacob Keller 		ice_mbx_reset_snapshot(snap);
297d775155aSJacob Keller 
298d775155aSJacob Keller 		/* Collect the pending ARQ count, number of messages processed and
299d775155aSJacob Keller 		 * the maximum number of messages allowed to be processed from the
300d775155aSJacob Keller 		 * Mailbox for current interrupt.
301d775155aSJacob Keller 		 */
302d775155aSJacob Keller 		snap_buf->num_pending_arq = mbx_data->num_pending_arq;
303d775155aSJacob Keller 		snap_buf->num_msg_proc = mbx_data->num_msg_proc;
304d775155aSJacob Keller 		snap_buf->max_num_msgs_mbx = mbx_data->max_num_msgs_mbx;
305d775155aSJacob Keller 
306d775155aSJacob Keller 		/* Capture a new static snapshot of the mailbox by logging the
307d775155aSJacob Keller 		 * head and tail of snapshot and set num_iterations to the tail
308d775155aSJacob Keller 		 * value to mark the start of the iteration through the snapshot.
309d775155aSJacob Keller 		 */
310d775155aSJacob Keller 		snap_buf->head = ICE_RQ_DATA_MASK(cq->rq.next_to_clean +
311d775155aSJacob Keller 						  mbx_data->num_pending_arq);
312d775155aSJacob Keller 		snap_buf->tail = ICE_RQ_DATA_MASK(cq->rq.next_to_clean - 1);
313d775155aSJacob Keller 		snap_buf->num_iterations = snap_buf->tail;
314d775155aSJacob Keller 
315d775155aSJacob Keller 		/* Pending ARQ messages returned by ice_clean_rq_elem
316d775155aSJacob Keller 		 * is the difference between the head and tail of the
317d775155aSJacob Keller 		 * mailbox queue. Comparing this value against the watermark
318d775155aSJacob Keller 		 * helps to check if we potentially have malicious VFs.
319d775155aSJacob Keller 		 */
320d775155aSJacob Keller 		if (snap_buf->num_pending_arq >=
321d775155aSJacob Keller 		    mbx_data->async_watermark_val) {
322d775155aSJacob Keller 			new_state = ICE_MAL_VF_DETECT_STATE_DETECT;
323d775155aSJacob Keller 			status = ice_mbx_detect_malvf(hw, vf_id, &new_state, is_malvf);
324d775155aSJacob Keller 		} else {
325d775155aSJacob Keller 			new_state = ICE_MAL_VF_DETECT_STATE_TRAVERSE;
326d775155aSJacob Keller 			ice_mbx_traverse(hw, &new_state);
327d775155aSJacob Keller 		}
328d775155aSJacob Keller 		break;
329d775155aSJacob Keller 
330d775155aSJacob Keller 	case ICE_MAL_VF_DETECT_STATE_TRAVERSE:
331d775155aSJacob Keller 		new_state = ICE_MAL_VF_DETECT_STATE_TRAVERSE;
332d775155aSJacob Keller 		ice_mbx_traverse(hw, &new_state);
333d775155aSJacob Keller 		break;
334d775155aSJacob Keller 
335d775155aSJacob Keller 	case ICE_MAL_VF_DETECT_STATE_DETECT:
336d775155aSJacob Keller 		new_state = ICE_MAL_VF_DETECT_STATE_DETECT;
337d775155aSJacob Keller 		status = ice_mbx_detect_malvf(hw, vf_id, &new_state, is_malvf);
338d775155aSJacob Keller 		break;
339d775155aSJacob Keller 
340d775155aSJacob Keller 	default:
341d775155aSJacob Keller 		new_state = ICE_MAL_VF_DETECT_STATE_INVALID;
342d775155aSJacob Keller 		status = -EIO;
343d775155aSJacob Keller 	}
344d775155aSJacob Keller 
345d775155aSJacob Keller 	snap_buf->state = new_state;
346d775155aSJacob Keller 
347d775155aSJacob Keller 	return status;
348d775155aSJacob Keller }
349d775155aSJacob Keller 
350d775155aSJacob Keller /**
351d775155aSJacob Keller  * ice_mbx_report_malvf - Track and note malicious VF
352d775155aSJacob Keller  * @hw: pointer to the HW struct
353d775155aSJacob Keller  * @all_malvfs: all malicious VFs tracked by PF
354d775155aSJacob Keller  * @bitmap_len: length of bitmap in bits
355d775155aSJacob Keller  * @vf_id: relative virtual function ID of the malicious VF
356d775155aSJacob Keller  * @report_malvf: boolean to indicate if malicious VF must be reported
357d775155aSJacob Keller  *
358d775155aSJacob Keller  * This function will update a bitmap that keeps track of the malicious
359d775155aSJacob Keller  * VFs attached to the PF. A malicious VF must be reported only once if
360d775155aSJacob Keller  * discovered between VF resets or loading so the function checks
361d775155aSJacob Keller  * the input vf_id against the bitmap to verify if the VF has been
362d775155aSJacob Keller  * detected in any previous mailbox iterations.
363d775155aSJacob Keller  */
364d775155aSJacob Keller int
365d775155aSJacob Keller ice_mbx_report_malvf(struct ice_hw *hw, unsigned long *all_malvfs,
366d775155aSJacob Keller 		     u16 bitmap_len, u16 vf_id, bool *report_malvf)
367d775155aSJacob Keller {
368d775155aSJacob Keller 	if (!all_malvfs || !report_malvf)
369d775155aSJacob Keller 		return -EINVAL;
370d775155aSJacob Keller 
371d775155aSJacob Keller 	*report_malvf = false;
372d775155aSJacob Keller 
373d775155aSJacob Keller 	if (bitmap_len < hw->mbx_snapshot.mbx_vf.vfcntr_len)
374d775155aSJacob Keller 		return -EINVAL;
375d775155aSJacob Keller 
376d775155aSJacob Keller 	if (vf_id >= bitmap_len)
377d775155aSJacob Keller 		return -EIO;
378d775155aSJacob Keller 
379d775155aSJacob Keller 	/* If the vf_id is found in the bitmap set bit and boolean to true */
380d775155aSJacob Keller 	if (!test_and_set_bit(vf_id, all_malvfs))
381d775155aSJacob Keller 		*report_malvf = true;
382d775155aSJacob Keller 
383d775155aSJacob Keller 	return 0;
384d775155aSJacob Keller }
385d775155aSJacob Keller 
386d775155aSJacob Keller /**
387d775155aSJacob Keller  * ice_mbx_clear_malvf - Clear VF bitmap and counter for VF ID
388d775155aSJacob Keller  * @snap: pointer to the mailbox snapshot structure
389d775155aSJacob Keller  * @all_malvfs: all malicious VFs tracked by PF
390d775155aSJacob Keller  * @bitmap_len: length of bitmap in bits
391d775155aSJacob Keller  * @vf_id: relative virtual function ID of the malicious VF
392d775155aSJacob Keller  *
393d775155aSJacob Keller  * In case of a VF reset, this function can be called to clear
394d775155aSJacob Keller  * the bit corresponding to the VF ID in the bitmap tracking all
395d775155aSJacob Keller  * malicious VFs attached to the PF. The function also clears the
396d775155aSJacob Keller  * VF counter array at the index of the VF ID. This is to ensure
397d775155aSJacob Keller  * that the new VF loaded is not considered malicious before going
398d775155aSJacob Keller  * through the overflow detection algorithm.
399d775155aSJacob Keller  */
400d775155aSJacob Keller int
401d775155aSJacob Keller ice_mbx_clear_malvf(struct ice_mbx_snapshot *snap, unsigned long *all_malvfs,
402d775155aSJacob Keller 		    u16 bitmap_len, u16 vf_id)
403d775155aSJacob Keller {
404d775155aSJacob Keller 	if (!snap || !all_malvfs)
405d775155aSJacob Keller 		return -EINVAL;
406d775155aSJacob Keller 
407d775155aSJacob Keller 	if (bitmap_len < snap->mbx_vf.vfcntr_len)
408d775155aSJacob Keller 		return -EINVAL;
409d775155aSJacob Keller 
410d775155aSJacob Keller 	/* Ensure VF ID value is not larger than bitmap or VF counter length */
411d775155aSJacob Keller 	if (vf_id >= bitmap_len || vf_id >= snap->mbx_vf.vfcntr_len)
412d775155aSJacob Keller 		return -EIO;
413d775155aSJacob Keller 
414d775155aSJacob Keller 	/* Clear VF ID bit in the bitmap tracking malicious VFs attached to PF */
415d775155aSJacob Keller 	clear_bit(vf_id, all_malvfs);
416d775155aSJacob Keller 
417d775155aSJacob Keller 	/* Clear the VF counter in the mailbox snapshot structure for that VF ID.
418d775155aSJacob Keller 	 * This is to ensure that if a VF is unloaded and a new one brought back
419d775155aSJacob Keller 	 * up with the same VF ID for a snapshot currently in traversal or detect
420d775155aSJacob Keller 	 * state the counter for that VF ID does not increment on top of existing
421d775155aSJacob Keller 	 * values in the mailbox overflow detection algorithm.
422d775155aSJacob Keller 	 */
423d775155aSJacob Keller 	snap->mbx_vf.vf_cntr[vf_id] = 0;
424d775155aSJacob Keller 
425d775155aSJacob Keller 	return 0;
426d775155aSJacob Keller }
427d775155aSJacob Keller 
428d775155aSJacob Keller /**
429d775155aSJacob Keller  * ice_mbx_init_snapshot - Initialize mailbox snapshot structure
430d775155aSJacob Keller  * @hw: pointer to the hardware structure
431d775155aSJacob Keller  * @vf_count: number of VFs allocated on a PF
432d775155aSJacob Keller  *
433d775155aSJacob Keller  * Clear the mailbox snapshot structure and allocate memory
434d775155aSJacob Keller  * for the VF counter array based on the number of VFs allocated
435d775155aSJacob Keller  * on that PF.
436d775155aSJacob Keller  *
437d775155aSJacob Keller  * Assumption: This function will assume ice_get_caps() has already been
438d775155aSJacob Keller  * called to ensure that the vf_count can be compared against the number
439d775155aSJacob Keller  * of VFs supported as defined in the functional capabilities of the device.
440d775155aSJacob Keller  */
441d775155aSJacob Keller int ice_mbx_init_snapshot(struct ice_hw *hw, u16 vf_count)
442d775155aSJacob Keller {
443d775155aSJacob Keller 	struct ice_mbx_snapshot *snap = &hw->mbx_snapshot;
444d775155aSJacob Keller 
445d775155aSJacob Keller 	/* Ensure that the number of VFs allocated is non-zero and
446d775155aSJacob Keller 	 * is not greater than the number of supported VFs defined in
447d775155aSJacob Keller 	 * the functional capabilities of the PF.
448d775155aSJacob Keller 	 */
449d775155aSJacob Keller 	if (!vf_count || vf_count > hw->func_caps.num_allocd_vfs)
450d775155aSJacob Keller 		return -EINVAL;
451d775155aSJacob Keller 
452d775155aSJacob Keller 	snap->mbx_vf.vf_cntr = devm_kcalloc(ice_hw_to_dev(hw), vf_count,
453d775155aSJacob Keller 					    sizeof(*snap->mbx_vf.vf_cntr),
454d775155aSJacob Keller 					    GFP_KERNEL);
455d775155aSJacob Keller 	if (!snap->mbx_vf.vf_cntr)
456d775155aSJacob Keller 		return -ENOMEM;
457d775155aSJacob Keller 
458d775155aSJacob Keller 	/* Setting the VF counter length to the number of allocated
459d775155aSJacob Keller 	 * VFs for given PF's functional capabilities.
460d775155aSJacob Keller 	 */
461d775155aSJacob Keller 	snap->mbx_vf.vfcntr_len = vf_count;
462d775155aSJacob Keller 
463d775155aSJacob Keller 	/* Clear mbx_buf in the mailbox snaphot structure and setting the
464d775155aSJacob Keller 	 * mailbox snapshot state to a new capture.
465d775155aSJacob Keller 	 */
466d775155aSJacob Keller 	memset(&snap->mbx_buf, 0, sizeof(snap->mbx_buf));
467d775155aSJacob Keller 	snap->mbx_buf.state = ICE_MAL_VF_DETECT_STATE_NEW_SNAPSHOT;
468d775155aSJacob Keller 
469d775155aSJacob Keller 	return 0;
470d775155aSJacob Keller }
471d775155aSJacob Keller 
472d775155aSJacob Keller /**
473d775155aSJacob Keller  * ice_mbx_deinit_snapshot - Free mailbox snapshot structure
474d775155aSJacob Keller  * @hw: pointer to the hardware structure
475d775155aSJacob Keller  *
476d775155aSJacob Keller  * Clear the mailbox snapshot structure and free the VF counter array.
477d775155aSJacob Keller  */
478d775155aSJacob Keller void ice_mbx_deinit_snapshot(struct ice_hw *hw)
479d775155aSJacob Keller {
480d775155aSJacob Keller 	struct ice_mbx_snapshot *snap = &hw->mbx_snapshot;
481d775155aSJacob Keller 
482d775155aSJacob Keller 	/* Free VF counter array and reset VF counter length */
483d775155aSJacob Keller 	devm_kfree(ice_hw_to_dev(hw), snap->mbx_vf.vf_cntr);
484d775155aSJacob Keller 	snap->mbx_vf.vfcntr_len = 0;
485d775155aSJacob Keller 
486d775155aSJacob Keller 	/* Clear mbx_buf in the mailbox snaphot structure */
487d775155aSJacob Keller 	memset(&snap->mbx_buf, 0, sizeof(snap->mbx_buf));
488d775155aSJacob Keller }
489