1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 2009 - 2018 Intel Corporation. */
4 #include <linux/bitfield.h>
8 * e1000_poll_for_msg - Wait for message notification
9 * @hw: pointer to the HW structure
11 * returns SUCCESS if it successfully received a message notification
13 static s32 e1000_poll_for_msg(struct e1000_hw *hw)
15 struct e1000_mbx_info *mbx = &hw->mbx;
16 int countdown = mbx->timeout;
18 if (!mbx->ops.check_for_msg)
21 while (countdown && mbx->ops.check_for_msg(hw)) {
23 udelay(mbx->usec_delay);
26 /* if we failed, all future posted messages fail until reset */
30 return countdown ? E1000_SUCCESS : -E1000_ERR_MBX;
34 * e1000_poll_for_ack - Wait for message acknowledgment
35 * @hw: pointer to the HW structure
37 * returns SUCCESS if it successfully received a message acknowledgment
39 static s32 e1000_poll_for_ack(struct e1000_hw *hw)
41 struct e1000_mbx_info *mbx = &hw->mbx;
42 int countdown = mbx->timeout;
44 if (!mbx->ops.check_for_ack)
47 while (countdown && mbx->ops.check_for_ack(hw)) {
49 udelay(mbx->usec_delay);
52 /* if we failed, all future posted messages fail until reset */
56 return countdown ? E1000_SUCCESS : -E1000_ERR_MBX;
60 * e1000_read_posted_mbx - Wait for message notification and receive message
61 * @hw: pointer to the HW structure
62 * @msg: The message buffer
63 * @size: Length of buffer
65 * returns SUCCESS if it successfully received a message notification and
66 * copied it into the receive buffer.
68 static s32 e1000_read_posted_mbx(struct e1000_hw *hw, u32 *msg, u16 size)
70 struct e1000_mbx_info *mbx = &hw->mbx;
71 s32 ret_val = -E1000_ERR_MBX;
76 ret_val = e1000_poll_for_msg(hw);
78 /* if ack received read message, otherwise we timed out */
80 ret_val = mbx->ops.read(hw, msg, size);
86 * e1000_write_posted_mbx - Write a message to the mailbox, wait for ack
87 * @hw: pointer to the HW structure
88 * @msg: The message buffer
89 * @size: Length of buffer
91 * returns SUCCESS if it successfully copied message into the buffer and
92 * received an ack to that message within delay * timeout period
94 static s32 e1000_write_posted_mbx(struct e1000_hw *hw, u32 *msg, u16 size)
96 struct e1000_mbx_info *mbx = &hw->mbx;
97 s32 ret_val = -E1000_ERR_MBX;
99 /* exit if we either can't write or there isn't a defined timeout */
100 if (!mbx->ops.write || !mbx->timeout)
104 ret_val = mbx->ops.write(hw, msg, size);
106 /* if msg sent wait until we receive an ack */
108 ret_val = e1000_poll_for_ack(hw);
114 * e1000_read_v2p_mailbox - read v2p mailbox
115 * @hw: pointer to the HW structure
117 * This function is used to read the v2p mailbox without losing the read to
120 static u32 e1000_read_v2p_mailbox(struct e1000_hw *hw)
122 u32 v2p_mailbox = er32(V2PMAILBOX(0));
124 v2p_mailbox |= hw->dev_spec.vf.v2p_mailbox;
125 hw->dev_spec.vf.v2p_mailbox |= v2p_mailbox & E1000_V2PMAILBOX_R2C_BITS;
131 * e1000_check_for_bit_vf - Determine if a status bit was set
132 * @hw: pointer to the HW structure
133 * @mask: bitmask for bits to be tested and cleared
135 * This function is used to check for the read to clear bits within
138 static s32 e1000_check_for_bit_vf(struct e1000_hw *hw, u32 mask)
140 u32 v2p_mailbox = e1000_read_v2p_mailbox(hw);
141 s32 ret_val = -E1000_ERR_MBX;
143 if (v2p_mailbox & mask)
144 ret_val = E1000_SUCCESS;
146 hw->dev_spec.vf.v2p_mailbox &= ~mask;
152 * e1000_check_for_msg_vf - checks to see if the PF has sent mail
153 * @hw: pointer to the HW structure
155 * returns SUCCESS if the PF has set the Status bit or else ERR_MBX
157 static s32 e1000_check_for_msg_vf(struct e1000_hw *hw)
159 s32 ret_val = -E1000_ERR_MBX;
161 if (!e1000_check_for_bit_vf(hw, E1000_V2PMAILBOX_PFSTS)) {
162 ret_val = E1000_SUCCESS;
163 hw->mbx.stats.reqs++;
170 * e1000_check_for_ack_vf - checks to see if the PF has ACK'd
171 * @hw: pointer to the HW structure
173 * returns SUCCESS if the PF has set the ACK bit or else ERR_MBX
175 static s32 e1000_check_for_ack_vf(struct e1000_hw *hw)
177 s32 ret_val = -E1000_ERR_MBX;
179 if (!e1000_check_for_bit_vf(hw, E1000_V2PMAILBOX_PFACK)) {
180 ret_val = E1000_SUCCESS;
181 hw->mbx.stats.acks++;
188 * e1000_check_for_rst_vf - checks to see if the PF has reset
189 * @hw: pointer to the HW structure
191 * returns true if the PF has set the reset done bit or else false
193 static s32 e1000_check_for_rst_vf(struct e1000_hw *hw)
195 s32 ret_val = -E1000_ERR_MBX;
197 if (!e1000_check_for_bit_vf(hw, (E1000_V2PMAILBOX_RSTD |
198 E1000_V2PMAILBOX_RSTI))) {
199 ret_val = E1000_SUCCESS;
200 hw->mbx.stats.rsts++;
207 * e1000_obtain_mbx_lock_vf - obtain mailbox lock
208 * @hw: pointer to the HW structure
210 * return SUCCESS if we obtained the mailbox lock
212 static s32 e1000_obtain_mbx_lock_vf(struct e1000_hw *hw)
214 s32 ret_val = -E1000_ERR_MBX;
218 /* Take ownership of the buffer */
219 ew32(V2PMAILBOX(0), E1000_V2PMAILBOX_VFU);
221 /* reserve mailbox for VF use */
222 if (e1000_read_v2p_mailbox(hw) & E1000_V2PMAILBOX_VFU) {
227 } while (count-- > 0);
233 * e1000_write_mbx_vf - Write a message to the mailbox
234 * @hw: pointer to the HW structure
235 * @msg: The message buffer
236 * @size: Length of buffer
238 * returns SUCCESS if it successfully copied message into the buffer
240 static s32 e1000_write_mbx_vf(struct e1000_hw *hw, u32 *msg, u16 size)
245 lockdep_assert_held(&hw->mbx_lock);
247 /* lock the mailbox to prevent pf/vf race condition */
248 err = e1000_obtain_mbx_lock_vf(hw);
252 /* flush any ack or msg as we are going to overwrite mailbox */
253 e1000_check_for_ack_vf(hw);
254 e1000_check_for_msg_vf(hw);
256 /* copy the caller specified message to the mailbox memory buffer */
257 for (i = 0; i < size; i++)
258 array_ew32(VMBMEM(0), i, msg[i]);
261 hw->mbx.stats.msgs_tx++;
263 /* Drop VFU and interrupt the PF to tell it a message has been sent */
264 ew32(V2PMAILBOX(0), E1000_V2PMAILBOX_REQ);
271 * e1000_read_mbx_vf - Reads a message from the inbox intended for VF
272 * @hw: pointer to the HW structure
273 * @msg: The message buffer
274 * @size: Length of buffer
276 * returns SUCCESS if it successfully read message from buffer
278 static s32 e1000_read_mbx_vf(struct e1000_hw *hw, u32 *msg, u16 size)
283 lockdep_assert_held(&hw->mbx_lock);
285 /* lock the mailbox to prevent pf/vf race condition */
286 err = e1000_obtain_mbx_lock_vf(hw);
290 /* copy the message from the mailbox memory buffer */
291 for (i = 0; i < size; i++)
292 msg[i] = array_er32(VMBMEM(0), i);
294 /* Acknowledge receipt and release mailbox, then we're done */
295 ew32(V2PMAILBOX(0), E1000_V2PMAILBOX_ACK);
298 hw->mbx.stats.msgs_rx++;
305 * e1000_init_mbx_params_vf - set initial values for VF mailbox
306 * @hw: pointer to the HW structure
308 * Initializes the hw->mbx struct to correct values for VF mailbox
310 s32 e1000_init_mbx_params_vf(struct e1000_hw *hw)
312 struct e1000_mbx_info *mbx = &hw->mbx;
314 /* start mailbox as timed out and let the reset_hw call set the timeout
315 * value to being communications
318 mbx->usec_delay = E1000_VF_MBX_INIT_DELAY;
320 mbx->size = E1000_VFMAILBOX_SIZE;
322 mbx->ops.read = e1000_read_mbx_vf;
323 mbx->ops.write = e1000_write_mbx_vf;
324 mbx->ops.read_posted = e1000_read_posted_mbx;
325 mbx->ops.write_posted = e1000_write_posted_mbx;
326 mbx->ops.check_for_msg = e1000_check_for_msg_vf;
327 mbx->ops.check_for_ack = e1000_check_for_ack_vf;
328 mbx->ops.check_for_rst = e1000_check_for_rst_vf;
330 mbx->stats.msgs_tx = 0;
331 mbx->stats.msgs_rx = 0;
336 return E1000_SUCCESS;