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 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 */ 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 96d775155aSJacob Keller * the algorithm can be run for the first time for that interrupt. This can be 97d775155aSJacob Keller * done via ice_mbx_reset_snapshot(). 98d775155aSJacob Keller * 99d775155aSJacob Keller * 3. For every message read by the caller from the MBX Queue, the caller must 100d775155aSJacob Keller * call the detection algorithm's entry function ice_mbx_vf_state_handler(). 101d775155aSJacob Keller * Before every call to ice_mbx_vf_state_handler() the struct ice_mbx_data is 102d775155aSJacob Keller * filled as it is required to be passed to the algorithm. 103d775155aSJacob Keller * 104d775155aSJacob Keller * 4. Every time a message is read from the MBX queue, a VFId is received which 105d775155aSJacob Keller * is passed to the state handler. The boolean output is_malvf of the state 106d775155aSJacob Keller * handler ice_mbx_vf_state_handler() serves as an indicator to the caller 107d775155aSJacob Keller * whether this VF is malicious or not. 108d775155aSJacob Keller * 109d775155aSJacob Keller * 5. When a VF is identified to be malicious, the caller can send a message 110d775155aSJacob Keller * to the system administrator. The caller can invoke ice_mbx_report_malvf() 111d775155aSJacob Keller * to help determine if a malicious VF is to be reported or not. This function 112d775155aSJacob Keller * requires the caller to maintain a global bitmap to track all malicious VFs 113d775155aSJacob Keller * and pass that to ice_mbx_report_malvf() along with the VFID which was identified 114d775155aSJacob Keller * to be malicious by ice_mbx_vf_state_handler(). 115d775155aSJacob Keller * 116d775155aSJacob Keller * 6. The global bitmap maintained by PF can be cleared completely if PF is in 117d775155aSJacob Keller * reset or the bit corresponding to a VF can be cleared if that VF is in reset. 118d775155aSJacob Keller * When a VF is shut down and brought back up, we assume that the new VF 119d775155aSJacob Keller * brought up is not malicious and hence report it if found malicious. 120d775155aSJacob Keller * 121d775155aSJacob Keller * 7. The function ice_mbx_reset_snapshot() is called to reset the information 122d775155aSJacob Keller * in ice_mbx_snapshot for every new mailbox interrupt handled. 123d775155aSJacob Keller * 124d775155aSJacob Keller * 8. The memory allocated for variables in ice_mbx_snapshot is de-allocated 125d775155aSJacob Keller * when driver is unloaded. 126d775155aSJacob Keller */ 127d775155aSJacob Keller #define ICE_RQ_DATA_MASK(rq_data) ((rq_data) & PF_MBX_ARQH_ARQH_M) 128d775155aSJacob Keller /* Using the highest value for an unsigned 16-bit value 0xFFFF to indicate that 129d775155aSJacob Keller * the max messages check must be ignored in the algorithm 130d775155aSJacob Keller */ 131d775155aSJacob Keller #define ICE_IGNORE_MAX_MSG_CNT 0xFFFF 132d775155aSJacob Keller 133d775155aSJacob Keller /** 134504ce971SJacob Keller * ice_mbx_reset_snapshot - Reset mailbox snapshot structure 135504ce971SJacob Keller * @snap: pointer to mailbox snapshot structure in the ice_hw struct 136504ce971SJacob Keller * 137504ce971SJacob Keller * Reset the mailbox snapshot structure and clear VF counter array. 138504ce971SJacob Keller */ 139504ce971SJacob Keller static void ice_mbx_reset_snapshot(struct ice_mbx_snapshot *snap) 140504ce971SJacob Keller { 141504ce971SJacob Keller u32 vfcntr_len; 142504ce971SJacob Keller 143504ce971SJacob Keller if (!snap || !snap->mbx_vf.vf_cntr) 144504ce971SJacob Keller return; 145504ce971SJacob Keller 146504ce971SJacob Keller /* Clear VF counters. */ 147504ce971SJacob Keller vfcntr_len = snap->mbx_vf.vfcntr_len; 148504ce971SJacob Keller if (vfcntr_len) 149504ce971SJacob Keller memset(snap->mbx_vf.vf_cntr, 0, 150504ce971SJacob Keller (vfcntr_len * sizeof(*snap->mbx_vf.vf_cntr))); 151504ce971SJacob Keller 152504ce971SJacob Keller /* Reset mailbox snapshot for a new capture. */ 153504ce971SJacob Keller memset(&snap->mbx_buf, 0, sizeof(snap->mbx_buf)); 154504ce971SJacob Keller snap->mbx_buf.state = ICE_MAL_VF_DETECT_STATE_NEW_SNAPSHOT; 155504ce971SJacob Keller } 156504ce971SJacob Keller 157504ce971SJacob Keller /** 158d775155aSJacob Keller * ice_mbx_traverse - Pass through mailbox snapshot 159d775155aSJacob Keller * @hw: pointer to the HW struct 160d775155aSJacob Keller * @new_state: new algorithm state 161d775155aSJacob Keller * 162d775155aSJacob Keller * Traversing the mailbox static snapshot without checking 163d775155aSJacob Keller * for malicious VFs. 164d775155aSJacob Keller */ 165d775155aSJacob Keller static void 166d775155aSJacob Keller ice_mbx_traverse(struct ice_hw *hw, 167d775155aSJacob Keller enum ice_mbx_snapshot_state *new_state) 168d775155aSJacob Keller { 169d775155aSJacob Keller struct ice_mbx_snap_buffer_data *snap_buf; 170d775155aSJacob Keller u32 num_iterations; 171d775155aSJacob Keller 172d775155aSJacob Keller snap_buf = &hw->mbx_snapshot.mbx_buf; 173d775155aSJacob Keller 174d775155aSJacob Keller /* As mailbox buffer is circular, applying a mask 175d775155aSJacob Keller * on the incremented iteration count. 176d775155aSJacob Keller */ 177d775155aSJacob Keller num_iterations = ICE_RQ_DATA_MASK(++snap_buf->num_iterations); 178d775155aSJacob Keller 179d775155aSJacob Keller /* Checking either of the below conditions to exit snapshot traversal: 180d775155aSJacob Keller * Condition-1: If the number of iterations in the mailbox is equal to 181d775155aSJacob Keller * the mailbox head which would indicate that we have reached the end 182d775155aSJacob Keller * of the static snapshot. 183d775155aSJacob Keller * Condition-2: If the maximum messages serviced in the mailbox for a 184d775155aSJacob Keller * given interrupt is the highest possible value then there is no need 185d775155aSJacob Keller * to check if the number of messages processed is equal to it. If not 186d775155aSJacob Keller * check if the number of messages processed is greater than or equal 187d775155aSJacob Keller * to the maximum number of mailbox entries serviced in current work item. 188d775155aSJacob Keller */ 189d775155aSJacob Keller if (num_iterations == snap_buf->head || 190d775155aSJacob Keller (snap_buf->max_num_msgs_mbx < ICE_IGNORE_MAX_MSG_CNT && 191d775155aSJacob Keller ++snap_buf->num_msg_proc >= snap_buf->max_num_msgs_mbx)) 192d775155aSJacob Keller *new_state = ICE_MAL_VF_DETECT_STATE_NEW_SNAPSHOT; 193d775155aSJacob Keller } 194d775155aSJacob Keller 195d775155aSJacob Keller /** 196d775155aSJacob Keller * ice_mbx_detect_malvf - Detect malicious VF in snapshot 197d775155aSJacob Keller * @hw: pointer to the HW struct 198d775155aSJacob Keller * @vf_id: relative virtual function ID 199d775155aSJacob Keller * @new_state: new algorithm state 200d775155aSJacob Keller * @is_malvf: boolean output to indicate if VF is malicious 201d775155aSJacob Keller * 202d775155aSJacob Keller * This function tracks the number of asynchronous messages 203d775155aSJacob Keller * sent per VF and marks the VF as malicious if it exceeds 204d775155aSJacob Keller * the permissible number of messages to send. 205d775155aSJacob Keller */ 206d775155aSJacob Keller static int 207d775155aSJacob Keller ice_mbx_detect_malvf(struct ice_hw *hw, u16 vf_id, 208d775155aSJacob Keller enum ice_mbx_snapshot_state *new_state, 209d775155aSJacob Keller bool *is_malvf) 210d775155aSJacob Keller { 211d775155aSJacob Keller struct ice_mbx_snapshot *snap = &hw->mbx_snapshot; 212d775155aSJacob Keller 213d775155aSJacob Keller if (vf_id >= snap->mbx_vf.vfcntr_len) 214d775155aSJacob Keller return -EIO; 215d775155aSJacob Keller 216d775155aSJacob Keller /* increment the message count in the VF array */ 217d775155aSJacob Keller snap->mbx_vf.vf_cntr[vf_id]++; 218d775155aSJacob Keller 219d775155aSJacob Keller if (snap->mbx_vf.vf_cntr[vf_id] >= ICE_ASYNC_VF_MSG_THRESHOLD) 220d775155aSJacob Keller *is_malvf = true; 221d775155aSJacob Keller 222d775155aSJacob Keller /* continue to iterate through the mailbox snapshot */ 223d775155aSJacob Keller ice_mbx_traverse(hw, new_state); 224d775155aSJacob Keller 225d775155aSJacob Keller return 0; 226d775155aSJacob Keller } 227d775155aSJacob Keller 228d775155aSJacob Keller /** 229d775155aSJacob Keller * ice_mbx_vf_state_handler - Handle states of the overflow algorithm 230d775155aSJacob Keller * @hw: pointer to the HW struct 231d775155aSJacob Keller * @mbx_data: pointer to structure containing mailbox data 232d775155aSJacob Keller * @vf_id: relative virtual function (VF) ID 233d775155aSJacob Keller * @is_malvf: boolean output to indicate if VF is malicious 234d775155aSJacob Keller * 235d775155aSJacob Keller * The function serves as an entry point for the malicious VF 236d775155aSJacob Keller * detection algorithm by handling the different states and state 237d775155aSJacob Keller * transitions of the algorithm: 238d775155aSJacob Keller * New snapshot: This state is entered when creating a new static 239d775155aSJacob Keller * snapshot. The data from any previous mailbox snapshot is 240d775155aSJacob Keller * cleared and a new capture of the mailbox head and tail is 241d775155aSJacob Keller * logged. This will be the new static snapshot to detect 242d775155aSJacob Keller * asynchronous messages sent by VFs. On capturing the snapshot 243d775155aSJacob Keller * and depending on whether the number of pending messages in that 244d775155aSJacob Keller * snapshot exceed the watermark value, the state machine enters 245d775155aSJacob Keller * traverse or detect states. 246d775155aSJacob Keller * Traverse: If pending message count is below watermark then iterate 247d775155aSJacob Keller * through the snapshot without any action on VF. 248d775155aSJacob Keller * Detect: If pending message count exceeds watermark traverse 249d775155aSJacob Keller * the static snapshot and look for a malicious VF. 250d775155aSJacob Keller */ 251d775155aSJacob Keller int 252d775155aSJacob Keller ice_mbx_vf_state_handler(struct ice_hw *hw, 253d775155aSJacob Keller struct ice_mbx_data *mbx_data, u16 vf_id, 254d775155aSJacob Keller bool *is_malvf) 255d775155aSJacob Keller { 256d775155aSJacob Keller struct ice_mbx_snapshot *snap = &hw->mbx_snapshot; 257d775155aSJacob Keller struct ice_mbx_snap_buffer_data *snap_buf; 258d775155aSJacob Keller struct ice_ctl_q_info *cq = &hw->mailboxq; 259d775155aSJacob Keller enum ice_mbx_snapshot_state new_state; 260d775155aSJacob Keller int status = 0; 261d775155aSJacob Keller 262d775155aSJacob Keller if (!is_malvf || !mbx_data) 263d775155aSJacob Keller return -EINVAL; 264d775155aSJacob Keller 265d775155aSJacob Keller /* When entering the mailbox state machine assume that the VF 266d775155aSJacob Keller * is not malicious until detected. 267d775155aSJacob Keller */ 268d775155aSJacob Keller *is_malvf = false; 269d775155aSJacob Keller 270d775155aSJacob Keller /* Checking if max messages allowed to be processed while servicing current 271d775155aSJacob Keller * interrupt is not less than the defined AVF message threshold. 272d775155aSJacob Keller */ 273d775155aSJacob Keller if (mbx_data->max_num_msgs_mbx <= ICE_ASYNC_VF_MSG_THRESHOLD) 274d775155aSJacob Keller return -EINVAL; 275d775155aSJacob Keller 276d775155aSJacob Keller /* The watermark value should not be lesser than the threshold limit 277d775155aSJacob Keller * set for the number of asynchronous messages a VF can send to mailbox 278d775155aSJacob Keller * nor should it be greater than the maximum number of messages in the 279d775155aSJacob Keller * mailbox serviced in current interrupt. 280d775155aSJacob Keller */ 281d775155aSJacob Keller if (mbx_data->async_watermark_val < ICE_ASYNC_VF_MSG_THRESHOLD || 282d775155aSJacob Keller mbx_data->async_watermark_val > mbx_data->max_num_msgs_mbx) 283d775155aSJacob Keller return -EINVAL; 284d775155aSJacob Keller 285d775155aSJacob Keller new_state = ICE_MAL_VF_DETECT_STATE_INVALID; 286d775155aSJacob Keller snap_buf = &snap->mbx_buf; 287d775155aSJacob Keller 288d775155aSJacob Keller switch (snap_buf->state) { 289d775155aSJacob Keller case ICE_MAL_VF_DETECT_STATE_NEW_SNAPSHOT: 290d775155aSJacob Keller /* Clear any previously held data in mailbox snapshot structure. */ 291d775155aSJacob Keller ice_mbx_reset_snapshot(snap); 292d775155aSJacob Keller 293d775155aSJacob Keller /* Collect the pending ARQ count, number of messages processed and 294d775155aSJacob Keller * the maximum number of messages allowed to be processed from the 295d775155aSJacob Keller * Mailbox for current interrupt. 296d775155aSJacob Keller */ 297d775155aSJacob Keller snap_buf->num_pending_arq = mbx_data->num_pending_arq; 298d775155aSJacob Keller snap_buf->num_msg_proc = mbx_data->num_msg_proc; 299d775155aSJacob Keller snap_buf->max_num_msgs_mbx = mbx_data->max_num_msgs_mbx; 300d775155aSJacob Keller 301d775155aSJacob Keller /* Capture a new static snapshot of the mailbox by logging the 302d775155aSJacob Keller * head and tail of snapshot and set num_iterations to the tail 303d775155aSJacob Keller * value to mark the start of the iteration through the snapshot. 304d775155aSJacob Keller */ 305d775155aSJacob Keller snap_buf->head = ICE_RQ_DATA_MASK(cq->rq.next_to_clean + 306d775155aSJacob Keller mbx_data->num_pending_arq); 307d775155aSJacob Keller snap_buf->tail = ICE_RQ_DATA_MASK(cq->rq.next_to_clean - 1); 308d775155aSJacob Keller snap_buf->num_iterations = snap_buf->tail; 309d775155aSJacob Keller 310d775155aSJacob Keller /* Pending ARQ messages returned by ice_clean_rq_elem 311d775155aSJacob Keller * is the difference between the head and tail of the 312d775155aSJacob Keller * mailbox queue. Comparing this value against the watermark 313d775155aSJacob Keller * helps to check if we potentially have malicious VFs. 314d775155aSJacob Keller */ 315d775155aSJacob Keller if (snap_buf->num_pending_arq >= 316d775155aSJacob Keller mbx_data->async_watermark_val) { 317d775155aSJacob Keller new_state = ICE_MAL_VF_DETECT_STATE_DETECT; 318d775155aSJacob Keller status = ice_mbx_detect_malvf(hw, vf_id, &new_state, is_malvf); 319d775155aSJacob Keller } else { 320d775155aSJacob Keller new_state = ICE_MAL_VF_DETECT_STATE_TRAVERSE; 321d775155aSJacob Keller ice_mbx_traverse(hw, &new_state); 322d775155aSJacob Keller } 323d775155aSJacob Keller break; 324d775155aSJacob Keller 325d775155aSJacob Keller case ICE_MAL_VF_DETECT_STATE_TRAVERSE: 326d775155aSJacob Keller new_state = ICE_MAL_VF_DETECT_STATE_TRAVERSE; 327d775155aSJacob Keller ice_mbx_traverse(hw, &new_state); 328d775155aSJacob Keller break; 329d775155aSJacob Keller 330d775155aSJacob Keller case ICE_MAL_VF_DETECT_STATE_DETECT: 331d775155aSJacob Keller new_state = ICE_MAL_VF_DETECT_STATE_DETECT; 332d775155aSJacob Keller status = ice_mbx_detect_malvf(hw, vf_id, &new_state, is_malvf); 333d775155aSJacob Keller break; 334d775155aSJacob Keller 335d775155aSJacob Keller default: 336d775155aSJacob Keller new_state = ICE_MAL_VF_DETECT_STATE_INVALID; 337d775155aSJacob Keller status = -EIO; 338d775155aSJacob Keller } 339d775155aSJacob Keller 340d775155aSJacob Keller snap_buf->state = new_state; 341d775155aSJacob Keller 342d775155aSJacob Keller return status; 343d775155aSJacob Keller } 344d775155aSJacob Keller 345d775155aSJacob Keller /** 346d775155aSJacob Keller * ice_mbx_report_malvf - Track and note malicious VF 347d775155aSJacob Keller * @hw: pointer to the HW struct 348*e4eaf893SJacob Keller * @vf_info: the mailbox tracking info structure for a VF 349d775155aSJacob Keller * @report_malvf: boolean to indicate if malicious VF must be reported 350d775155aSJacob Keller * 351*e4eaf893SJacob Keller * This function updates the malicious indicator bit in the VF mailbox 352*e4eaf893SJacob Keller * tracking structure. A malicious VF must be reported only once if discovered 353*e4eaf893SJacob Keller * between VF resets or loading so the function first checks if the VF has 354*e4eaf893SJacob Keller * already been detected in any previous mailbox iterations. 355d775155aSJacob Keller */ 356d775155aSJacob Keller int 357*e4eaf893SJacob Keller ice_mbx_report_malvf(struct ice_hw *hw, struct ice_mbx_vf_info *vf_info, 358*e4eaf893SJacob Keller bool *report_malvf) 359d775155aSJacob Keller { 360*e4eaf893SJacob Keller if (!report_malvf) 361d775155aSJacob Keller return -EINVAL; 362d775155aSJacob Keller 363*e4eaf893SJacob Keller *report_malvf = !vf_info->malicious; 364*e4eaf893SJacob Keller vf_info->malicious = 1; 365d775155aSJacob Keller 366d775155aSJacob Keller return 0; 367d775155aSJacob Keller } 368d775155aSJacob Keller 369d775155aSJacob Keller /** 370d775155aSJacob Keller * ice_mbx_clear_malvf - Clear VF bitmap and counter for VF ID 371d775155aSJacob Keller * @snap: pointer to the mailbox snapshot structure 372d775155aSJacob Keller * @vf_id: relative virtual function ID of the malicious VF 373*e4eaf893SJacob Keller * @vf_info: mailbox tracking structure for this VF 374d775155aSJacob Keller * 375*e4eaf893SJacob Keller * In case of a VF reset, this function shall be called to clear the VF's 376*e4eaf893SJacob Keller * current mailbox tracking state. 377d775155aSJacob Keller */ 37828756d9eSJacob Keller void 379*e4eaf893SJacob Keller ice_mbx_clear_malvf(struct ice_mbx_snapshot *snap, u16 vf_id, 380*e4eaf893SJacob Keller struct ice_mbx_vf_info *vf_info) 381d775155aSJacob Keller { 382*e4eaf893SJacob Keller if (WARN_ON(!snap)) 38328756d9eSJacob Keller return; 384d775155aSJacob Keller 385d775155aSJacob Keller /* Ensure VF ID value is not larger than bitmap or VF counter length */ 386*e4eaf893SJacob Keller if (WARN_ON(vf_id >= snap->mbx_vf.vfcntr_len)) 38728756d9eSJacob Keller return; 388d775155aSJacob Keller 389*e4eaf893SJacob Keller vf_info->malicious = 0; 390d775155aSJacob Keller 391d775155aSJacob Keller /* Clear the VF counter in the mailbox snapshot structure for that VF ID. 392d775155aSJacob Keller * This is to ensure that if a VF is unloaded and a new one brought back 393d775155aSJacob Keller * up with the same VF ID for a snapshot currently in traversal or detect 394d775155aSJacob Keller * state the counter for that VF ID does not increment on top of existing 395d775155aSJacob Keller * values in the mailbox overflow detection algorithm. 396d775155aSJacob Keller */ 397d775155aSJacob Keller snap->mbx_vf.vf_cntr[vf_id] = 0; 398d775155aSJacob Keller } 399d775155aSJacob Keller 400d775155aSJacob Keller /** 401*e4eaf893SJacob Keller * ice_mbx_init_vf_info - Initialize a new VF mailbox tracking info 402*e4eaf893SJacob Keller * @hw: pointer to the hardware structure 403*e4eaf893SJacob Keller * @vf_info: the mailbox tracking info structure for a VF 404*e4eaf893SJacob Keller * 405*e4eaf893SJacob Keller * Initialize a VF mailbox tracking info structure. 406*e4eaf893SJacob Keller */ 407*e4eaf893SJacob Keller void ice_mbx_init_vf_info(struct ice_hw *hw, struct ice_mbx_vf_info *vf_info) 408*e4eaf893SJacob Keller { 409*e4eaf893SJacob Keller vf_info->malicious = 0; 410*e4eaf893SJacob Keller } 411*e4eaf893SJacob Keller 412*e4eaf893SJacob Keller /** 413d775155aSJacob Keller * ice_mbx_init_snapshot - Initialize mailbox snapshot structure 414d775155aSJacob Keller * @hw: pointer to the hardware structure 415d775155aSJacob Keller * @vf_count: number of VFs allocated on a PF 416d775155aSJacob Keller * 417d775155aSJacob Keller * Clear the mailbox snapshot structure and allocate memory 418d775155aSJacob Keller * for the VF counter array based on the number of VFs allocated 419d775155aSJacob Keller * on that PF. 420d775155aSJacob Keller * 421d775155aSJacob Keller * Assumption: This function will assume ice_get_caps() has already been 422d775155aSJacob Keller * called to ensure that the vf_count can be compared against the number 423d775155aSJacob Keller * of VFs supported as defined in the functional capabilities of the device. 424d775155aSJacob Keller */ 425d775155aSJacob Keller int ice_mbx_init_snapshot(struct ice_hw *hw, u16 vf_count) 426d775155aSJacob Keller { 427d775155aSJacob Keller struct ice_mbx_snapshot *snap = &hw->mbx_snapshot; 428d775155aSJacob Keller 429d775155aSJacob Keller /* Ensure that the number of VFs allocated is non-zero and 430d775155aSJacob Keller * is not greater than the number of supported VFs defined in 431d775155aSJacob Keller * the functional capabilities of the PF. 432d775155aSJacob Keller */ 433d775155aSJacob Keller if (!vf_count || vf_count > hw->func_caps.num_allocd_vfs) 434d775155aSJacob Keller return -EINVAL; 435d775155aSJacob Keller 436d775155aSJacob Keller snap->mbx_vf.vf_cntr = devm_kcalloc(ice_hw_to_dev(hw), vf_count, 437d775155aSJacob Keller sizeof(*snap->mbx_vf.vf_cntr), 438d775155aSJacob Keller GFP_KERNEL); 439d775155aSJacob Keller if (!snap->mbx_vf.vf_cntr) 440d775155aSJacob Keller return -ENOMEM; 441d775155aSJacob Keller 442d775155aSJacob Keller /* Setting the VF counter length to the number of allocated 443d775155aSJacob Keller * VFs for given PF's functional capabilities. 444d775155aSJacob Keller */ 445d775155aSJacob Keller snap->mbx_vf.vfcntr_len = vf_count; 446d775155aSJacob Keller 447d775155aSJacob Keller /* Clear mbx_buf in the mailbox snaphot structure and setting the 448d775155aSJacob Keller * mailbox snapshot state to a new capture. 449d775155aSJacob Keller */ 450d775155aSJacob Keller memset(&snap->mbx_buf, 0, sizeof(snap->mbx_buf)); 451d775155aSJacob Keller snap->mbx_buf.state = ICE_MAL_VF_DETECT_STATE_NEW_SNAPSHOT; 452d775155aSJacob Keller 453d775155aSJacob Keller return 0; 454d775155aSJacob Keller } 455d775155aSJacob Keller 456d775155aSJacob Keller /** 457d775155aSJacob Keller * ice_mbx_deinit_snapshot - Free mailbox snapshot structure 458d775155aSJacob Keller * @hw: pointer to the hardware structure 459d775155aSJacob Keller * 460d775155aSJacob Keller * Clear the mailbox snapshot structure and free the VF counter array. 461d775155aSJacob Keller */ 462d775155aSJacob Keller void ice_mbx_deinit_snapshot(struct ice_hw *hw) 463d775155aSJacob Keller { 464d775155aSJacob Keller struct ice_mbx_snapshot *snap = &hw->mbx_snapshot; 465d775155aSJacob Keller 466d775155aSJacob Keller /* Free VF counter array and reset VF counter length */ 467d775155aSJacob Keller devm_kfree(ice_hw_to_dev(hw), snap->mbx_vf.vf_cntr); 468d775155aSJacob Keller snap->mbx_vf.vfcntr_len = 0; 469d775155aSJacob Keller 470d775155aSJacob Keller /* Clear mbx_buf in the mailbox snaphot structure */ 471d775155aSJacob Keller memset(&snap->mbx_buf, 0, sizeof(snap->mbx_buf)); 472d775155aSJacob Keller } 473