Merge tag 'trace-printf-v6.13' of git://git.kernel.org/pub/scm/linux/kernel/git/trace...
[drm/drm-misc.git] / drivers / net / ethernet / intel / igbvf / mbx.c
blobd15282ee5ea8f7cc8bb63a246dc4d2421a018e39
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 2009 - 2018 Intel Corporation. */
4 #include <linux/bitfield.h>
5 #include "mbx.h"
7 /**
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
12 **/
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)
19 goto out;
21 while (countdown && mbx->ops.check_for_msg(hw)) {
22 countdown--;
23 udelay(mbx->usec_delay);
26 /* if we failed, all future posted messages fail until reset */
27 if (!countdown)
28 mbx->timeout = 0;
29 out:
30 return countdown ? E1000_SUCCESS : -E1000_ERR_MBX;
33 /**
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
38 **/
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)
45 goto out;
47 while (countdown && mbx->ops.check_for_ack(hw)) {
48 countdown--;
49 udelay(mbx->usec_delay);
52 /* if we failed, all future posted messages fail until reset */
53 if (!countdown)
54 mbx->timeout = 0;
55 out:
56 return countdown ? E1000_SUCCESS : -E1000_ERR_MBX;
59 /**
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.
67 **/
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;
73 if (!mbx->ops.read)
74 goto out;
76 ret_val = e1000_poll_for_msg(hw);
78 /* if ack received read message, otherwise we timed out */
79 if (!ret_val)
80 ret_val = mbx->ops.read(hw, msg, size);
81 out:
82 return ret_val;
85 /**
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
93 **/
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)
101 goto out;
103 /* send msg*/
104 ret_val = mbx->ops.write(hw, msg, size);
106 /* if msg sent wait until we receive an ack */
107 if (!ret_val)
108 ret_val = e1000_poll_for_ack(hw);
109 out:
110 return ret_val;
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
118 * clear status bits.
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;
127 return v2p_mailbox;
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
136 * the V2P mailbox.
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;
148 return ret_val;
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++;
166 return ret_val;
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++;
184 return ret_val;
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++;
203 return ret_val;
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;
215 int count = 10;
217 do {
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) {
223 ret_val = 0;
224 break;
226 udelay(1000);
227 } while (count-- > 0);
229 return ret_val;
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)
242 s32 err;
243 u16 i;
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);
249 if (err)
250 goto out_no_write;
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]);
260 /* update stats */
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);
266 out_no_write:
267 return err;
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)
280 s32 err;
281 u16 i;
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);
287 if (err)
288 goto out_no_read;
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);
297 /* update stats */
298 hw->mbx.stats.msgs_rx++;
300 out_no_read:
301 return err;
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
317 mbx->timeout = 0;
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;
332 mbx->stats.reqs = 0;
333 mbx->stats.acks = 0;
334 mbx->stats.rsts = 0;
336 return E1000_SUCCESS;