1ae06c70bSJeff Kirsher // SPDX-License-Identifier: GPL-2.0
251dce24bSJeff Kirsher /* Copyright(c) 2009 - 2018 Intel Corporation. */
3dee1ad47SJeff Kirsher
4*b9a45254SJesse Brandeburg #include <linux/bitfield.h>
5dee1ad47SJeff Kirsher #include "mbx.h"
6dee1ad47SJeff Kirsher
7dee1ad47SJeff Kirsher /**
8dee1ad47SJeff Kirsher * e1000_poll_for_msg - Wait for message notification
9dee1ad47SJeff Kirsher * @hw: pointer to the HW structure
10dee1ad47SJeff Kirsher *
11dee1ad47SJeff Kirsher * returns SUCCESS if it successfully received a message notification
12dee1ad47SJeff Kirsher **/
e1000_poll_for_msg(struct e1000_hw * hw)13dee1ad47SJeff Kirsher static s32 e1000_poll_for_msg(struct e1000_hw *hw)
14dee1ad47SJeff Kirsher {
15dee1ad47SJeff Kirsher struct e1000_mbx_info *mbx = &hw->mbx;
16dee1ad47SJeff Kirsher int countdown = mbx->timeout;
17dee1ad47SJeff Kirsher
18dee1ad47SJeff Kirsher if (!mbx->ops.check_for_msg)
19dee1ad47SJeff Kirsher goto out;
20dee1ad47SJeff Kirsher
21dee1ad47SJeff Kirsher while (countdown && mbx->ops.check_for_msg(hw)) {
22dee1ad47SJeff Kirsher countdown--;
23dee1ad47SJeff Kirsher udelay(mbx->usec_delay);
24dee1ad47SJeff Kirsher }
25dee1ad47SJeff Kirsher
26dee1ad47SJeff Kirsher /* if we failed, all future posted messages fail until reset */
27dee1ad47SJeff Kirsher if (!countdown)
28dee1ad47SJeff Kirsher mbx->timeout = 0;
29dee1ad47SJeff Kirsher out:
30dee1ad47SJeff Kirsher return countdown ? E1000_SUCCESS : -E1000_ERR_MBX;
31dee1ad47SJeff Kirsher }
32dee1ad47SJeff Kirsher
33dee1ad47SJeff Kirsher /**
340340501bSJeff Kirsher * e1000_poll_for_ack - Wait for message acknowledgment
35dee1ad47SJeff Kirsher * @hw: pointer to the HW structure
36dee1ad47SJeff Kirsher *
370340501bSJeff Kirsher * returns SUCCESS if it successfully received a message acknowledgment
38dee1ad47SJeff Kirsher **/
e1000_poll_for_ack(struct e1000_hw * hw)39dee1ad47SJeff Kirsher static s32 e1000_poll_for_ack(struct e1000_hw *hw)
40dee1ad47SJeff Kirsher {
41dee1ad47SJeff Kirsher struct e1000_mbx_info *mbx = &hw->mbx;
42dee1ad47SJeff Kirsher int countdown = mbx->timeout;
43dee1ad47SJeff Kirsher
44dee1ad47SJeff Kirsher if (!mbx->ops.check_for_ack)
45dee1ad47SJeff Kirsher goto out;
46dee1ad47SJeff Kirsher
47dee1ad47SJeff Kirsher while (countdown && mbx->ops.check_for_ack(hw)) {
48dee1ad47SJeff Kirsher countdown--;
49dee1ad47SJeff Kirsher udelay(mbx->usec_delay);
50dee1ad47SJeff Kirsher }
51dee1ad47SJeff Kirsher
52dee1ad47SJeff Kirsher /* if we failed, all future posted messages fail until reset */
53dee1ad47SJeff Kirsher if (!countdown)
54dee1ad47SJeff Kirsher mbx->timeout = 0;
55dee1ad47SJeff Kirsher out:
56dee1ad47SJeff Kirsher return countdown ? E1000_SUCCESS : -E1000_ERR_MBX;
57dee1ad47SJeff Kirsher }
58dee1ad47SJeff Kirsher
59dee1ad47SJeff Kirsher /**
60dee1ad47SJeff Kirsher * e1000_read_posted_mbx - Wait for message notification and receive message
61dee1ad47SJeff Kirsher * @hw: pointer to the HW structure
62dee1ad47SJeff Kirsher * @msg: The message buffer
63dee1ad47SJeff Kirsher * @size: Length of buffer
64dee1ad47SJeff Kirsher *
65dee1ad47SJeff Kirsher * returns SUCCESS if it successfully received a message notification and
66dee1ad47SJeff Kirsher * copied it into the receive buffer.
67dee1ad47SJeff Kirsher **/
e1000_read_posted_mbx(struct e1000_hw * hw,u32 * msg,u16 size)68dee1ad47SJeff Kirsher static s32 e1000_read_posted_mbx(struct e1000_hw *hw, u32 *msg, u16 size)
69dee1ad47SJeff Kirsher {
70dee1ad47SJeff Kirsher struct e1000_mbx_info *mbx = &hw->mbx;
71dee1ad47SJeff Kirsher s32 ret_val = -E1000_ERR_MBX;
72dee1ad47SJeff Kirsher
73dee1ad47SJeff Kirsher if (!mbx->ops.read)
74dee1ad47SJeff Kirsher goto out;
75dee1ad47SJeff Kirsher
76dee1ad47SJeff Kirsher ret_val = e1000_poll_for_msg(hw);
77dee1ad47SJeff Kirsher
78dee1ad47SJeff Kirsher /* if ack received read message, otherwise we timed out */
79dee1ad47SJeff Kirsher if (!ret_val)
80dee1ad47SJeff Kirsher ret_val = mbx->ops.read(hw, msg, size);
81dee1ad47SJeff Kirsher out:
82dee1ad47SJeff Kirsher return ret_val;
83dee1ad47SJeff Kirsher }
84dee1ad47SJeff Kirsher
85dee1ad47SJeff Kirsher /**
86dee1ad47SJeff Kirsher * e1000_write_posted_mbx - Write a message to the mailbox, wait for ack
87dee1ad47SJeff Kirsher * @hw: pointer to the HW structure
88dee1ad47SJeff Kirsher * @msg: The message buffer
89dee1ad47SJeff Kirsher * @size: Length of buffer
90dee1ad47SJeff Kirsher *
91dee1ad47SJeff Kirsher * returns SUCCESS if it successfully copied message into the buffer and
92dee1ad47SJeff Kirsher * received an ack to that message within delay * timeout period
93dee1ad47SJeff Kirsher **/
e1000_write_posted_mbx(struct e1000_hw * hw,u32 * msg,u16 size)94dee1ad47SJeff Kirsher static s32 e1000_write_posted_mbx(struct e1000_hw *hw, u32 *msg, u16 size)
95dee1ad47SJeff Kirsher {
96dee1ad47SJeff Kirsher struct e1000_mbx_info *mbx = &hw->mbx;
97dee1ad47SJeff Kirsher s32 ret_val = -E1000_ERR_MBX;
98dee1ad47SJeff Kirsher
99dee1ad47SJeff Kirsher /* exit if we either can't write or there isn't a defined timeout */
100dee1ad47SJeff Kirsher if (!mbx->ops.write || !mbx->timeout)
101dee1ad47SJeff Kirsher goto out;
102dee1ad47SJeff Kirsher
103dee1ad47SJeff Kirsher /* send msg*/
104dee1ad47SJeff Kirsher ret_val = mbx->ops.write(hw, msg, size);
105dee1ad47SJeff Kirsher
106dee1ad47SJeff Kirsher /* if msg sent wait until we receive an ack */
107dee1ad47SJeff Kirsher if (!ret_val)
108dee1ad47SJeff Kirsher ret_val = e1000_poll_for_ack(hw);
109dee1ad47SJeff Kirsher out:
110dee1ad47SJeff Kirsher return ret_val;
111dee1ad47SJeff Kirsher }
112dee1ad47SJeff Kirsher
113dee1ad47SJeff Kirsher /**
114dee1ad47SJeff Kirsher * e1000_read_v2p_mailbox - read v2p mailbox
115dee1ad47SJeff Kirsher * @hw: pointer to the HW structure
116dee1ad47SJeff Kirsher *
117dee1ad47SJeff Kirsher * This function is used to read the v2p mailbox without losing the read to
118dee1ad47SJeff Kirsher * clear status bits.
119dee1ad47SJeff Kirsher **/
e1000_read_v2p_mailbox(struct e1000_hw * hw)120dee1ad47SJeff Kirsher static u32 e1000_read_v2p_mailbox(struct e1000_hw *hw)
121dee1ad47SJeff Kirsher {
122dee1ad47SJeff Kirsher u32 v2p_mailbox = er32(V2PMAILBOX(0));
123dee1ad47SJeff Kirsher
124dee1ad47SJeff Kirsher v2p_mailbox |= hw->dev_spec.vf.v2p_mailbox;
125dee1ad47SJeff Kirsher hw->dev_spec.vf.v2p_mailbox |= v2p_mailbox & E1000_V2PMAILBOX_R2C_BITS;
126dee1ad47SJeff Kirsher
127dee1ad47SJeff Kirsher return v2p_mailbox;
128dee1ad47SJeff Kirsher }
129dee1ad47SJeff Kirsher
130dee1ad47SJeff Kirsher /**
131dee1ad47SJeff Kirsher * e1000_check_for_bit_vf - Determine if a status bit was set
132dee1ad47SJeff Kirsher * @hw: pointer to the HW structure
133dee1ad47SJeff Kirsher * @mask: bitmask for bits to be tested and cleared
134dee1ad47SJeff Kirsher *
135dee1ad47SJeff Kirsher * This function is used to check for the read to clear bits within
136dee1ad47SJeff Kirsher * the V2P mailbox.
137dee1ad47SJeff Kirsher **/
e1000_check_for_bit_vf(struct e1000_hw * hw,u32 mask)138dee1ad47SJeff Kirsher static s32 e1000_check_for_bit_vf(struct e1000_hw *hw, u32 mask)
139dee1ad47SJeff Kirsher {
140dee1ad47SJeff Kirsher u32 v2p_mailbox = e1000_read_v2p_mailbox(hw);
141dee1ad47SJeff Kirsher s32 ret_val = -E1000_ERR_MBX;
142dee1ad47SJeff Kirsher
143dee1ad47SJeff Kirsher if (v2p_mailbox & mask)
144dee1ad47SJeff Kirsher ret_val = E1000_SUCCESS;
145dee1ad47SJeff Kirsher
146dee1ad47SJeff Kirsher hw->dev_spec.vf.v2p_mailbox &= ~mask;
147dee1ad47SJeff Kirsher
148dee1ad47SJeff Kirsher return ret_val;
149dee1ad47SJeff Kirsher }
150dee1ad47SJeff Kirsher
151dee1ad47SJeff Kirsher /**
152dee1ad47SJeff Kirsher * e1000_check_for_msg_vf - checks to see if the PF has sent mail
153dee1ad47SJeff Kirsher * @hw: pointer to the HW structure
154dee1ad47SJeff Kirsher *
155dee1ad47SJeff Kirsher * returns SUCCESS if the PF has set the Status bit or else ERR_MBX
156dee1ad47SJeff Kirsher **/
e1000_check_for_msg_vf(struct e1000_hw * hw)157dee1ad47SJeff Kirsher static s32 e1000_check_for_msg_vf(struct e1000_hw *hw)
158dee1ad47SJeff Kirsher {
159dee1ad47SJeff Kirsher s32 ret_val = -E1000_ERR_MBX;
160dee1ad47SJeff Kirsher
161dee1ad47SJeff Kirsher if (!e1000_check_for_bit_vf(hw, E1000_V2PMAILBOX_PFSTS)) {
162dee1ad47SJeff Kirsher ret_val = E1000_SUCCESS;
163dee1ad47SJeff Kirsher hw->mbx.stats.reqs++;
164dee1ad47SJeff Kirsher }
165dee1ad47SJeff Kirsher
166dee1ad47SJeff Kirsher return ret_val;
167dee1ad47SJeff Kirsher }
168dee1ad47SJeff Kirsher
169dee1ad47SJeff Kirsher /**
170dee1ad47SJeff Kirsher * e1000_check_for_ack_vf - checks to see if the PF has ACK'd
171dee1ad47SJeff Kirsher * @hw: pointer to the HW structure
172dee1ad47SJeff Kirsher *
173dee1ad47SJeff Kirsher * returns SUCCESS if the PF has set the ACK bit or else ERR_MBX
174dee1ad47SJeff Kirsher **/
e1000_check_for_ack_vf(struct e1000_hw * hw)175dee1ad47SJeff Kirsher static s32 e1000_check_for_ack_vf(struct e1000_hw *hw)
176dee1ad47SJeff Kirsher {
177dee1ad47SJeff Kirsher s32 ret_val = -E1000_ERR_MBX;
178dee1ad47SJeff Kirsher
179dee1ad47SJeff Kirsher if (!e1000_check_for_bit_vf(hw, E1000_V2PMAILBOX_PFACK)) {
180dee1ad47SJeff Kirsher ret_val = E1000_SUCCESS;
181dee1ad47SJeff Kirsher hw->mbx.stats.acks++;
182dee1ad47SJeff Kirsher }
183dee1ad47SJeff Kirsher
184dee1ad47SJeff Kirsher return ret_val;
185dee1ad47SJeff Kirsher }
186dee1ad47SJeff Kirsher
187dee1ad47SJeff Kirsher /**
188dee1ad47SJeff Kirsher * e1000_check_for_rst_vf - checks to see if the PF has reset
189dee1ad47SJeff Kirsher * @hw: pointer to the HW structure
190dee1ad47SJeff Kirsher *
191dee1ad47SJeff Kirsher * returns true if the PF has set the reset done bit or else false
192dee1ad47SJeff Kirsher **/
e1000_check_for_rst_vf(struct e1000_hw * hw)193dee1ad47SJeff Kirsher static s32 e1000_check_for_rst_vf(struct e1000_hw *hw)
194dee1ad47SJeff Kirsher {
195dee1ad47SJeff Kirsher s32 ret_val = -E1000_ERR_MBX;
196dee1ad47SJeff Kirsher
197dee1ad47SJeff Kirsher if (!e1000_check_for_bit_vf(hw, (E1000_V2PMAILBOX_RSTD |
198dee1ad47SJeff Kirsher E1000_V2PMAILBOX_RSTI))) {
199dee1ad47SJeff Kirsher ret_val = E1000_SUCCESS;
200dee1ad47SJeff Kirsher hw->mbx.stats.rsts++;
201dee1ad47SJeff Kirsher }
202dee1ad47SJeff Kirsher
203dee1ad47SJeff Kirsher return ret_val;
204dee1ad47SJeff Kirsher }
205dee1ad47SJeff Kirsher
206dee1ad47SJeff Kirsher /**
207dee1ad47SJeff Kirsher * e1000_obtain_mbx_lock_vf - obtain mailbox lock
208dee1ad47SJeff Kirsher * @hw: pointer to the HW structure
209dee1ad47SJeff Kirsher *
210dee1ad47SJeff Kirsher * return SUCCESS if we obtained the mailbox lock
211dee1ad47SJeff Kirsher **/
e1000_obtain_mbx_lock_vf(struct e1000_hw * hw)212dee1ad47SJeff Kirsher static s32 e1000_obtain_mbx_lock_vf(struct e1000_hw *hw)
213dee1ad47SJeff Kirsher {
214dee1ad47SJeff Kirsher s32 ret_val = -E1000_ERR_MBX;
2159ce0e8d7SMitch Williams int count = 10;
216dee1ad47SJeff Kirsher
2179ce0e8d7SMitch Williams do {
218dee1ad47SJeff Kirsher /* Take ownership of the buffer */
219dee1ad47SJeff Kirsher ew32(V2PMAILBOX(0), E1000_V2PMAILBOX_VFU);
220dee1ad47SJeff Kirsher
2210340501bSJeff Kirsher /* reserve mailbox for VF use */
2229ce0e8d7SMitch Williams if (e1000_read_v2p_mailbox(hw) & E1000_V2PMAILBOX_VFU) {
2239ce0e8d7SMitch Williams ret_val = 0;
2249ce0e8d7SMitch Williams break;
2259ce0e8d7SMitch Williams }
2269ce0e8d7SMitch Williams udelay(1000);
2279ce0e8d7SMitch Williams } while (count-- > 0);
228dee1ad47SJeff Kirsher
229dee1ad47SJeff Kirsher return ret_val;
230dee1ad47SJeff Kirsher }
231dee1ad47SJeff Kirsher
232dee1ad47SJeff Kirsher /**
233dee1ad47SJeff Kirsher * e1000_write_mbx_vf - Write a message to the mailbox
234dee1ad47SJeff Kirsher * @hw: pointer to the HW structure
235dee1ad47SJeff Kirsher * @msg: The message buffer
236dee1ad47SJeff Kirsher * @size: Length of buffer
237dee1ad47SJeff Kirsher *
238dee1ad47SJeff Kirsher * returns SUCCESS if it successfully copied message into the buffer
239dee1ad47SJeff Kirsher **/
e1000_write_mbx_vf(struct e1000_hw * hw,u32 * msg,u16 size)240dee1ad47SJeff Kirsher static s32 e1000_write_mbx_vf(struct e1000_hw *hw, u32 *msg, u16 size)
241dee1ad47SJeff Kirsher {
242dee1ad47SJeff Kirsher s32 err;
243dee1ad47SJeff Kirsher u16 i;
244dee1ad47SJeff Kirsher
245b8607720SLance Roy lockdep_assert_held(&hw->mbx_lock);
24632652c2aSGreg Edwards
247dee1ad47SJeff Kirsher /* lock the mailbox to prevent pf/vf race condition */
248dee1ad47SJeff Kirsher err = e1000_obtain_mbx_lock_vf(hw);
249dee1ad47SJeff Kirsher if (err)
250dee1ad47SJeff Kirsher goto out_no_write;
251dee1ad47SJeff Kirsher
252dee1ad47SJeff Kirsher /* flush any ack or msg as we are going to overwrite mailbox */
253dee1ad47SJeff Kirsher e1000_check_for_ack_vf(hw);
254dee1ad47SJeff Kirsher e1000_check_for_msg_vf(hw);
255dee1ad47SJeff Kirsher
256dee1ad47SJeff Kirsher /* copy the caller specified message to the mailbox memory buffer */
257dee1ad47SJeff Kirsher for (i = 0; i < size; i++)
258dee1ad47SJeff Kirsher array_ew32(VMBMEM(0), i, msg[i]);
259dee1ad47SJeff Kirsher
260dee1ad47SJeff Kirsher /* update stats */
261dee1ad47SJeff Kirsher hw->mbx.stats.msgs_tx++;
262dee1ad47SJeff Kirsher
263dee1ad47SJeff Kirsher /* Drop VFU and interrupt the PF to tell it a message has been sent */
264dee1ad47SJeff Kirsher ew32(V2PMAILBOX(0), E1000_V2PMAILBOX_REQ);
265dee1ad47SJeff Kirsher
266dee1ad47SJeff Kirsher out_no_write:
267dee1ad47SJeff Kirsher return err;
268dee1ad47SJeff Kirsher }
269dee1ad47SJeff Kirsher
270dee1ad47SJeff Kirsher /**
2710340501bSJeff Kirsher * e1000_read_mbx_vf - Reads a message from the inbox intended for VF
272dee1ad47SJeff Kirsher * @hw: pointer to the HW structure
273dee1ad47SJeff Kirsher * @msg: The message buffer
274dee1ad47SJeff Kirsher * @size: Length of buffer
275dee1ad47SJeff Kirsher *
2768a9ea323SLinus Torvalds * returns SUCCESS if it successfully read message from buffer
277dee1ad47SJeff Kirsher **/
e1000_read_mbx_vf(struct e1000_hw * hw,u32 * msg,u16 size)278dee1ad47SJeff Kirsher static s32 e1000_read_mbx_vf(struct e1000_hw *hw, u32 *msg, u16 size)
279dee1ad47SJeff Kirsher {
280dee1ad47SJeff Kirsher s32 err;
281dee1ad47SJeff Kirsher u16 i;
282dee1ad47SJeff Kirsher
283b8607720SLance Roy lockdep_assert_held(&hw->mbx_lock);
28432652c2aSGreg Edwards
285dee1ad47SJeff Kirsher /* lock the mailbox to prevent pf/vf race condition */
286dee1ad47SJeff Kirsher err = e1000_obtain_mbx_lock_vf(hw);
287dee1ad47SJeff Kirsher if (err)
288dee1ad47SJeff Kirsher goto out_no_read;
289dee1ad47SJeff Kirsher
290dee1ad47SJeff Kirsher /* copy the message from the mailbox memory buffer */
291dee1ad47SJeff Kirsher for (i = 0; i < size; i++)
292dee1ad47SJeff Kirsher msg[i] = array_er32(VMBMEM(0), i);
293dee1ad47SJeff Kirsher
294dee1ad47SJeff Kirsher /* Acknowledge receipt and release mailbox, then we're done */
295dee1ad47SJeff Kirsher ew32(V2PMAILBOX(0), E1000_V2PMAILBOX_ACK);
296dee1ad47SJeff Kirsher
297dee1ad47SJeff Kirsher /* update stats */
298dee1ad47SJeff Kirsher hw->mbx.stats.msgs_rx++;
299dee1ad47SJeff Kirsher
300dee1ad47SJeff Kirsher out_no_read:
301dee1ad47SJeff Kirsher return err;
302dee1ad47SJeff Kirsher }
303dee1ad47SJeff Kirsher
304dee1ad47SJeff Kirsher /**
3050340501bSJeff Kirsher * e1000_init_mbx_params_vf - set initial values for VF mailbox
306dee1ad47SJeff Kirsher * @hw: pointer to the HW structure
307dee1ad47SJeff Kirsher *
3080340501bSJeff Kirsher * Initializes the hw->mbx struct to correct values for VF mailbox
309dee1ad47SJeff Kirsher */
e1000_init_mbx_params_vf(struct e1000_hw * hw)310dee1ad47SJeff Kirsher s32 e1000_init_mbx_params_vf(struct e1000_hw *hw)
311dee1ad47SJeff Kirsher {
312dee1ad47SJeff Kirsher struct e1000_mbx_info *mbx = &hw->mbx;
313dee1ad47SJeff Kirsher
314dee1ad47SJeff Kirsher /* start mailbox as timed out and let the reset_hw call set the timeout
3150340501bSJeff Kirsher * value to being communications
3160340501bSJeff Kirsher */
317dee1ad47SJeff Kirsher mbx->timeout = 0;
318dee1ad47SJeff Kirsher mbx->usec_delay = E1000_VF_MBX_INIT_DELAY;
319dee1ad47SJeff Kirsher
320dee1ad47SJeff Kirsher mbx->size = E1000_VFMAILBOX_SIZE;
321dee1ad47SJeff Kirsher
322dee1ad47SJeff Kirsher mbx->ops.read = e1000_read_mbx_vf;
323dee1ad47SJeff Kirsher mbx->ops.write = e1000_write_mbx_vf;
324dee1ad47SJeff Kirsher mbx->ops.read_posted = e1000_read_posted_mbx;
325dee1ad47SJeff Kirsher mbx->ops.write_posted = e1000_write_posted_mbx;
326dee1ad47SJeff Kirsher mbx->ops.check_for_msg = e1000_check_for_msg_vf;
327dee1ad47SJeff Kirsher mbx->ops.check_for_ack = e1000_check_for_ack_vf;
328dee1ad47SJeff Kirsher mbx->ops.check_for_rst = e1000_check_for_rst_vf;
329dee1ad47SJeff Kirsher
330dee1ad47SJeff Kirsher mbx->stats.msgs_tx = 0;
331dee1ad47SJeff Kirsher mbx->stats.msgs_rx = 0;
332dee1ad47SJeff Kirsher mbx->stats.reqs = 0;
333dee1ad47SJeff Kirsher mbx->stats.acks = 0;
334dee1ad47SJeff Kirsher mbx->stats.rsts = 0;
335dee1ad47SJeff Kirsher
336dee1ad47SJeff Kirsher return E1000_SUCCESS;
337dee1ad47SJeff Kirsher }
338