xref: /OK3568_Linux_fs/kernel/drivers/net/ethernet/intel/ixgbevf/mbx.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /* Copyright(c) 1999 - 2018 Intel Corporation. */
3*4882a593Smuzhiyun 
4*4882a593Smuzhiyun #include "mbx.h"
5*4882a593Smuzhiyun #include "ixgbevf.h"
6*4882a593Smuzhiyun 
7*4882a593Smuzhiyun /**
8*4882a593Smuzhiyun  *  ixgbevf_poll_for_msg - Wait for message notification
9*4882a593Smuzhiyun  *  @hw: pointer to the HW structure
10*4882a593Smuzhiyun  *
11*4882a593Smuzhiyun  *  returns 0 if it successfully received a message notification
12*4882a593Smuzhiyun  **/
ixgbevf_poll_for_msg(struct ixgbe_hw * hw)13*4882a593Smuzhiyun static s32 ixgbevf_poll_for_msg(struct ixgbe_hw *hw)
14*4882a593Smuzhiyun {
15*4882a593Smuzhiyun 	struct ixgbe_mbx_info *mbx = &hw->mbx;
16*4882a593Smuzhiyun 	int countdown = mbx->timeout;
17*4882a593Smuzhiyun 
18*4882a593Smuzhiyun 	while (countdown && mbx->ops.check_for_msg(hw)) {
19*4882a593Smuzhiyun 		countdown--;
20*4882a593Smuzhiyun 		udelay(mbx->udelay);
21*4882a593Smuzhiyun 	}
22*4882a593Smuzhiyun 
23*4882a593Smuzhiyun 	/* if we failed, all future posted messages fail until reset */
24*4882a593Smuzhiyun 	if (!countdown)
25*4882a593Smuzhiyun 		mbx->timeout = 0;
26*4882a593Smuzhiyun 
27*4882a593Smuzhiyun 	return countdown ? 0 : IXGBE_ERR_MBX;
28*4882a593Smuzhiyun }
29*4882a593Smuzhiyun 
30*4882a593Smuzhiyun /**
31*4882a593Smuzhiyun  *  ixgbevf_poll_for_ack - Wait for message acknowledgment
32*4882a593Smuzhiyun  *  @hw: pointer to the HW structure
33*4882a593Smuzhiyun  *
34*4882a593Smuzhiyun  *  returns 0 if it successfully received a message acknowledgment
35*4882a593Smuzhiyun  **/
ixgbevf_poll_for_ack(struct ixgbe_hw * hw)36*4882a593Smuzhiyun static s32 ixgbevf_poll_for_ack(struct ixgbe_hw *hw)
37*4882a593Smuzhiyun {
38*4882a593Smuzhiyun 	struct ixgbe_mbx_info *mbx = &hw->mbx;
39*4882a593Smuzhiyun 	int countdown = mbx->timeout;
40*4882a593Smuzhiyun 
41*4882a593Smuzhiyun 	while (countdown && mbx->ops.check_for_ack(hw)) {
42*4882a593Smuzhiyun 		countdown--;
43*4882a593Smuzhiyun 		udelay(mbx->udelay);
44*4882a593Smuzhiyun 	}
45*4882a593Smuzhiyun 
46*4882a593Smuzhiyun 	/* if we failed, all future posted messages fail until reset */
47*4882a593Smuzhiyun 	if (!countdown)
48*4882a593Smuzhiyun 		mbx->timeout = 0;
49*4882a593Smuzhiyun 
50*4882a593Smuzhiyun 	return countdown ? 0 : IXGBE_ERR_MBX;
51*4882a593Smuzhiyun }
52*4882a593Smuzhiyun 
53*4882a593Smuzhiyun /**
54*4882a593Smuzhiyun  *  ixgbevf_read_posted_mbx - Wait for message notification and receive message
55*4882a593Smuzhiyun  *  @hw: pointer to the HW structure
56*4882a593Smuzhiyun  *  @msg: The message buffer
57*4882a593Smuzhiyun  *  @size: Length of buffer
58*4882a593Smuzhiyun  *
59*4882a593Smuzhiyun  *  returns 0 if it successfully received a message notification and
60*4882a593Smuzhiyun  *  copied it into the receive buffer.
61*4882a593Smuzhiyun  **/
ixgbevf_read_posted_mbx(struct ixgbe_hw * hw,u32 * msg,u16 size)62*4882a593Smuzhiyun static s32 ixgbevf_read_posted_mbx(struct ixgbe_hw *hw, u32 *msg, u16 size)
63*4882a593Smuzhiyun {
64*4882a593Smuzhiyun 	struct ixgbe_mbx_info *mbx = &hw->mbx;
65*4882a593Smuzhiyun 	s32 ret_val = IXGBE_ERR_MBX;
66*4882a593Smuzhiyun 
67*4882a593Smuzhiyun 	if (!mbx->ops.read)
68*4882a593Smuzhiyun 		goto out;
69*4882a593Smuzhiyun 
70*4882a593Smuzhiyun 	ret_val = ixgbevf_poll_for_msg(hw);
71*4882a593Smuzhiyun 
72*4882a593Smuzhiyun 	/* if ack received read message, otherwise we timed out */
73*4882a593Smuzhiyun 	if (!ret_val)
74*4882a593Smuzhiyun 		ret_val = mbx->ops.read(hw, msg, size);
75*4882a593Smuzhiyun out:
76*4882a593Smuzhiyun 	return ret_val;
77*4882a593Smuzhiyun }
78*4882a593Smuzhiyun 
79*4882a593Smuzhiyun /**
80*4882a593Smuzhiyun  *  ixgbevf_write_posted_mbx - Write a message to the mailbox, wait for ack
81*4882a593Smuzhiyun  *  @hw: pointer to the HW structure
82*4882a593Smuzhiyun  *  @msg: The message buffer
83*4882a593Smuzhiyun  *  @size: Length of buffer
84*4882a593Smuzhiyun  *
85*4882a593Smuzhiyun  *  returns 0 if it successfully copied message into the buffer and
86*4882a593Smuzhiyun  *  received an ack to that message within delay * timeout period
87*4882a593Smuzhiyun  **/
ixgbevf_write_posted_mbx(struct ixgbe_hw * hw,u32 * msg,u16 size)88*4882a593Smuzhiyun static s32 ixgbevf_write_posted_mbx(struct ixgbe_hw *hw, u32 *msg, u16 size)
89*4882a593Smuzhiyun {
90*4882a593Smuzhiyun 	struct ixgbe_mbx_info *mbx = &hw->mbx;
91*4882a593Smuzhiyun 	s32 ret_val = IXGBE_ERR_MBX;
92*4882a593Smuzhiyun 
93*4882a593Smuzhiyun 	/* exit if either we can't write or there isn't a defined timeout */
94*4882a593Smuzhiyun 	if (!mbx->ops.write || !mbx->timeout)
95*4882a593Smuzhiyun 		goto out;
96*4882a593Smuzhiyun 
97*4882a593Smuzhiyun 	/* send msg */
98*4882a593Smuzhiyun 	ret_val = mbx->ops.write(hw, msg, size);
99*4882a593Smuzhiyun 
100*4882a593Smuzhiyun 	/* if msg sent wait until we receive an ack */
101*4882a593Smuzhiyun 	if (!ret_val)
102*4882a593Smuzhiyun 		ret_val = ixgbevf_poll_for_ack(hw);
103*4882a593Smuzhiyun out:
104*4882a593Smuzhiyun 	return ret_val;
105*4882a593Smuzhiyun }
106*4882a593Smuzhiyun 
107*4882a593Smuzhiyun /**
108*4882a593Smuzhiyun  *  ixgbevf_read_v2p_mailbox - read v2p mailbox
109*4882a593Smuzhiyun  *  @hw: pointer to the HW structure
110*4882a593Smuzhiyun  *
111*4882a593Smuzhiyun  *  This function is used to read the v2p mailbox without losing the read to
112*4882a593Smuzhiyun  *  clear status bits.
113*4882a593Smuzhiyun  **/
ixgbevf_read_v2p_mailbox(struct ixgbe_hw * hw)114*4882a593Smuzhiyun static u32 ixgbevf_read_v2p_mailbox(struct ixgbe_hw *hw)
115*4882a593Smuzhiyun {
116*4882a593Smuzhiyun 	u32 v2p_mailbox = IXGBE_READ_REG(hw, IXGBE_VFMAILBOX);
117*4882a593Smuzhiyun 
118*4882a593Smuzhiyun 	v2p_mailbox |= hw->mbx.v2p_mailbox;
119*4882a593Smuzhiyun 	hw->mbx.v2p_mailbox |= v2p_mailbox & IXGBE_VFMAILBOX_R2C_BITS;
120*4882a593Smuzhiyun 
121*4882a593Smuzhiyun 	return v2p_mailbox;
122*4882a593Smuzhiyun }
123*4882a593Smuzhiyun 
124*4882a593Smuzhiyun /**
125*4882a593Smuzhiyun  *  ixgbevf_check_for_bit_vf - Determine if a status bit was set
126*4882a593Smuzhiyun  *  @hw: pointer to the HW structure
127*4882a593Smuzhiyun  *  @mask: bitmask for bits to be tested and cleared
128*4882a593Smuzhiyun  *
129*4882a593Smuzhiyun  *  This function is used to check for the read to clear bits within
130*4882a593Smuzhiyun  *  the V2P mailbox.
131*4882a593Smuzhiyun  **/
ixgbevf_check_for_bit_vf(struct ixgbe_hw * hw,u32 mask)132*4882a593Smuzhiyun static s32 ixgbevf_check_for_bit_vf(struct ixgbe_hw *hw, u32 mask)
133*4882a593Smuzhiyun {
134*4882a593Smuzhiyun 	u32 v2p_mailbox = ixgbevf_read_v2p_mailbox(hw);
135*4882a593Smuzhiyun 	s32 ret_val = IXGBE_ERR_MBX;
136*4882a593Smuzhiyun 
137*4882a593Smuzhiyun 	if (v2p_mailbox & mask)
138*4882a593Smuzhiyun 		ret_val = 0;
139*4882a593Smuzhiyun 
140*4882a593Smuzhiyun 	hw->mbx.v2p_mailbox &= ~mask;
141*4882a593Smuzhiyun 
142*4882a593Smuzhiyun 	return ret_val;
143*4882a593Smuzhiyun }
144*4882a593Smuzhiyun 
145*4882a593Smuzhiyun /**
146*4882a593Smuzhiyun  *  ixgbevf_check_for_msg_vf - checks to see if the PF has sent mail
147*4882a593Smuzhiyun  *  @hw: pointer to the HW structure
148*4882a593Smuzhiyun  *
149*4882a593Smuzhiyun  *  returns 0 if the PF has set the Status bit or else ERR_MBX
150*4882a593Smuzhiyun  **/
ixgbevf_check_for_msg_vf(struct ixgbe_hw * hw)151*4882a593Smuzhiyun static s32 ixgbevf_check_for_msg_vf(struct ixgbe_hw *hw)
152*4882a593Smuzhiyun {
153*4882a593Smuzhiyun 	s32 ret_val = IXGBE_ERR_MBX;
154*4882a593Smuzhiyun 
155*4882a593Smuzhiyun 	if (!ixgbevf_check_for_bit_vf(hw, IXGBE_VFMAILBOX_PFSTS)) {
156*4882a593Smuzhiyun 		ret_val = 0;
157*4882a593Smuzhiyun 		hw->mbx.stats.reqs++;
158*4882a593Smuzhiyun 	}
159*4882a593Smuzhiyun 
160*4882a593Smuzhiyun 	return ret_val;
161*4882a593Smuzhiyun }
162*4882a593Smuzhiyun 
163*4882a593Smuzhiyun /**
164*4882a593Smuzhiyun  *  ixgbevf_check_for_ack_vf - checks to see if the PF has ACK'd
165*4882a593Smuzhiyun  *  @hw: pointer to the HW structure
166*4882a593Smuzhiyun  *
167*4882a593Smuzhiyun  *  returns 0 if the PF has set the ACK bit or else ERR_MBX
168*4882a593Smuzhiyun  **/
ixgbevf_check_for_ack_vf(struct ixgbe_hw * hw)169*4882a593Smuzhiyun static s32 ixgbevf_check_for_ack_vf(struct ixgbe_hw *hw)
170*4882a593Smuzhiyun {
171*4882a593Smuzhiyun 	s32 ret_val = IXGBE_ERR_MBX;
172*4882a593Smuzhiyun 
173*4882a593Smuzhiyun 	if (!ixgbevf_check_for_bit_vf(hw, IXGBE_VFMAILBOX_PFACK)) {
174*4882a593Smuzhiyun 		ret_val = 0;
175*4882a593Smuzhiyun 		hw->mbx.stats.acks++;
176*4882a593Smuzhiyun 	}
177*4882a593Smuzhiyun 
178*4882a593Smuzhiyun 	return ret_val;
179*4882a593Smuzhiyun }
180*4882a593Smuzhiyun 
181*4882a593Smuzhiyun /**
182*4882a593Smuzhiyun  *  ixgbevf_check_for_rst_vf - checks to see if the PF has reset
183*4882a593Smuzhiyun  *  @hw: pointer to the HW structure
184*4882a593Smuzhiyun  *
185*4882a593Smuzhiyun  *  returns true if the PF has set the reset done bit or else false
186*4882a593Smuzhiyun  **/
ixgbevf_check_for_rst_vf(struct ixgbe_hw * hw)187*4882a593Smuzhiyun static s32 ixgbevf_check_for_rst_vf(struct ixgbe_hw *hw)
188*4882a593Smuzhiyun {
189*4882a593Smuzhiyun 	s32 ret_val = IXGBE_ERR_MBX;
190*4882a593Smuzhiyun 
191*4882a593Smuzhiyun 	if (!ixgbevf_check_for_bit_vf(hw, (IXGBE_VFMAILBOX_RSTD |
192*4882a593Smuzhiyun 					   IXGBE_VFMAILBOX_RSTI))) {
193*4882a593Smuzhiyun 		ret_val = 0;
194*4882a593Smuzhiyun 		hw->mbx.stats.rsts++;
195*4882a593Smuzhiyun 	}
196*4882a593Smuzhiyun 
197*4882a593Smuzhiyun 	return ret_val;
198*4882a593Smuzhiyun }
199*4882a593Smuzhiyun 
200*4882a593Smuzhiyun /**
201*4882a593Smuzhiyun  *  ixgbevf_obtain_mbx_lock_vf - obtain mailbox lock
202*4882a593Smuzhiyun  *  @hw: pointer to the HW structure
203*4882a593Smuzhiyun  *
204*4882a593Smuzhiyun  *  return 0 if we obtained the mailbox lock
205*4882a593Smuzhiyun  **/
ixgbevf_obtain_mbx_lock_vf(struct ixgbe_hw * hw)206*4882a593Smuzhiyun static s32 ixgbevf_obtain_mbx_lock_vf(struct ixgbe_hw *hw)
207*4882a593Smuzhiyun {
208*4882a593Smuzhiyun 	s32 ret_val = IXGBE_ERR_MBX;
209*4882a593Smuzhiyun 
210*4882a593Smuzhiyun 	/* Take ownership of the buffer */
211*4882a593Smuzhiyun 	IXGBE_WRITE_REG(hw, IXGBE_VFMAILBOX, IXGBE_VFMAILBOX_VFU);
212*4882a593Smuzhiyun 
213*4882a593Smuzhiyun 	/* reserve mailbox for VF use */
214*4882a593Smuzhiyun 	if (ixgbevf_read_v2p_mailbox(hw) & IXGBE_VFMAILBOX_VFU)
215*4882a593Smuzhiyun 		ret_val = 0;
216*4882a593Smuzhiyun 
217*4882a593Smuzhiyun 	return ret_val;
218*4882a593Smuzhiyun }
219*4882a593Smuzhiyun 
220*4882a593Smuzhiyun /**
221*4882a593Smuzhiyun  *  ixgbevf_write_mbx_vf - Write a message to the mailbox
222*4882a593Smuzhiyun  *  @hw: pointer to the HW structure
223*4882a593Smuzhiyun  *  @msg: The message buffer
224*4882a593Smuzhiyun  *  @size: Length of buffer
225*4882a593Smuzhiyun  *
226*4882a593Smuzhiyun  *  returns 0 if it successfully copied message into the buffer
227*4882a593Smuzhiyun  **/
ixgbevf_write_mbx_vf(struct ixgbe_hw * hw,u32 * msg,u16 size)228*4882a593Smuzhiyun static s32 ixgbevf_write_mbx_vf(struct ixgbe_hw *hw, u32 *msg, u16 size)
229*4882a593Smuzhiyun {
230*4882a593Smuzhiyun 	s32 ret_val;
231*4882a593Smuzhiyun 	u16 i;
232*4882a593Smuzhiyun 
233*4882a593Smuzhiyun 	/* lock the mailbox to prevent PF/VF race condition */
234*4882a593Smuzhiyun 	ret_val = ixgbevf_obtain_mbx_lock_vf(hw);
235*4882a593Smuzhiyun 	if (ret_val)
236*4882a593Smuzhiyun 		goto out_no_write;
237*4882a593Smuzhiyun 
238*4882a593Smuzhiyun 	/* flush msg and acks as we are overwriting the message buffer */
239*4882a593Smuzhiyun 	ixgbevf_check_for_msg_vf(hw);
240*4882a593Smuzhiyun 	ixgbevf_check_for_ack_vf(hw);
241*4882a593Smuzhiyun 
242*4882a593Smuzhiyun 	/* copy the caller specified message to the mailbox memory buffer */
243*4882a593Smuzhiyun 	for (i = 0; i < size; i++)
244*4882a593Smuzhiyun 		IXGBE_WRITE_REG_ARRAY(hw, IXGBE_VFMBMEM, i, msg[i]);
245*4882a593Smuzhiyun 
246*4882a593Smuzhiyun 	/* update stats */
247*4882a593Smuzhiyun 	hw->mbx.stats.msgs_tx++;
248*4882a593Smuzhiyun 
249*4882a593Smuzhiyun 	/* Drop VFU and interrupt the PF to tell it a message has been sent */
250*4882a593Smuzhiyun 	IXGBE_WRITE_REG(hw, IXGBE_VFMAILBOX, IXGBE_VFMAILBOX_REQ);
251*4882a593Smuzhiyun 
252*4882a593Smuzhiyun out_no_write:
253*4882a593Smuzhiyun 	return ret_val;
254*4882a593Smuzhiyun }
255*4882a593Smuzhiyun 
256*4882a593Smuzhiyun /**
257*4882a593Smuzhiyun  *  ixgbevf_read_mbx_vf - Reads a message from the inbox intended for VF
258*4882a593Smuzhiyun  *  @hw: pointer to the HW structure
259*4882a593Smuzhiyun  *  @msg: The message buffer
260*4882a593Smuzhiyun  *  @size: Length of buffer
261*4882a593Smuzhiyun  *
262*4882a593Smuzhiyun  *  returns 0 if it successfully read message from buffer
263*4882a593Smuzhiyun  **/
ixgbevf_read_mbx_vf(struct ixgbe_hw * hw,u32 * msg,u16 size)264*4882a593Smuzhiyun static s32 ixgbevf_read_mbx_vf(struct ixgbe_hw *hw, u32 *msg, u16 size)
265*4882a593Smuzhiyun {
266*4882a593Smuzhiyun 	s32 ret_val = 0;
267*4882a593Smuzhiyun 	u16 i;
268*4882a593Smuzhiyun 
269*4882a593Smuzhiyun 	/* lock the mailbox to prevent PF/VF race condition */
270*4882a593Smuzhiyun 	ret_val = ixgbevf_obtain_mbx_lock_vf(hw);
271*4882a593Smuzhiyun 	if (ret_val)
272*4882a593Smuzhiyun 		goto out_no_read;
273*4882a593Smuzhiyun 
274*4882a593Smuzhiyun 	/* copy the message from the mailbox memory buffer */
275*4882a593Smuzhiyun 	for (i = 0; i < size; i++)
276*4882a593Smuzhiyun 		msg[i] = IXGBE_READ_REG_ARRAY(hw, IXGBE_VFMBMEM, i);
277*4882a593Smuzhiyun 
278*4882a593Smuzhiyun 	/* Acknowledge receipt and release mailbox, then we're done */
279*4882a593Smuzhiyun 	IXGBE_WRITE_REG(hw, IXGBE_VFMAILBOX, IXGBE_VFMAILBOX_ACK);
280*4882a593Smuzhiyun 
281*4882a593Smuzhiyun 	/* update stats */
282*4882a593Smuzhiyun 	hw->mbx.stats.msgs_rx++;
283*4882a593Smuzhiyun 
284*4882a593Smuzhiyun out_no_read:
285*4882a593Smuzhiyun 	return ret_val;
286*4882a593Smuzhiyun }
287*4882a593Smuzhiyun 
288*4882a593Smuzhiyun /**
289*4882a593Smuzhiyun  *  ixgbevf_init_mbx_params_vf - set initial values for VF mailbox
290*4882a593Smuzhiyun  *  @hw: pointer to the HW structure
291*4882a593Smuzhiyun  *
292*4882a593Smuzhiyun  *  Initializes the hw->mbx struct to correct values for VF mailbox
293*4882a593Smuzhiyun  */
ixgbevf_init_mbx_params_vf(struct ixgbe_hw * hw)294*4882a593Smuzhiyun static s32 ixgbevf_init_mbx_params_vf(struct ixgbe_hw *hw)
295*4882a593Smuzhiyun {
296*4882a593Smuzhiyun 	struct ixgbe_mbx_info *mbx = &hw->mbx;
297*4882a593Smuzhiyun 
298*4882a593Smuzhiyun 	/* start mailbox as timed out and let the reset_hw call set the timeout
299*4882a593Smuzhiyun 	 * value to begin communications
300*4882a593Smuzhiyun 	 */
301*4882a593Smuzhiyun 	mbx->timeout = 0;
302*4882a593Smuzhiyun 	mbx->udelay = IXGBE_VF_MBX_INIT_DELAY;
303*4882a593Smuzhiyun 
304*4882a593Smuzhiyun 	mbx->size = IXGBE_VFMAILBOX_SIZE;
305*4882a593Smuzhiyun 
306*4882a593Smuzhiyun 	mbx->stats.msgs_tx = 0;
307*4882a593Smuzhiyun 	mbx->stats.msgs_rx = 0;
308*4882a593Smuzhiyun 	mbx->stats.reqs = 0;
309*4882a593Smuzhiyun 	mbx->stats.acks = 0;
310*4882a593Smuzhiyun 	mbx->stats.rsts = 0;
311*4882a593Smuzhiyun 
312*4882a593Smuzhiyun 	return 0;
313*4882a593Smuzhiyun }
314*4882a593Smuzhiyun 
315*4882a593Smuzhiyun const struct ixgbe_mbx_operations ixgbevf_mbx_ops = {
316*4882a593Smuzhiyun 	.init_params	= ixgbevf_init_mbx_params_vf,
317*4882a593Smuzhiyun 	.read		= ixgbevf_read_mbx_vf,
318*4882a593Smuzhiyun 	.write		= ixgbevf_write_mbx_vf,
319*4882a593Smuzhiyun 	.read_posted	= ixgbevf_read_posted_mbx,
320*4882a593Smuzhiyun 	.write_posted	= ixgbevf_write_posted_mbx,
321*4882a593Smuzhiyun 	.check_for_msg	= ixgbevf_check_for_msg_vf,
322*4882a593Smuzhiyun 	.check_for_ack	= ixgbevf_check_for_ack_vf,
323*4882a593Smuzhiyun 	.check_for_rst	= ixgbevf_check_for_rst_vf,
324*4882a593Smuzhiyun };
325*4882a593Smuzhiyun 
326*4882a593Smuzhiyun /* Mailbox operations when running on Hyper-V.
327*4882a593Smuzhiyun  * On Hyper-V, PF/VF communication is not through the
328*4882a593Smuzhiyun  * hardware mailbox; this communication is through
329*4882a593Smuzhiyun  * a software mediated path.
330*4882a593Smuzhiyun  * Most mail box operations are noop while running on
331*4882a593Smuzhiyun  * Hyper-V.
332*4882a593Smuzhiyun  */
333*4882a593Smuzhiyun const struct ixgbe_mbx_operations ixgbevf_hv_mbx_ops = {
334*4882a593Smuzhiyun 	.init_params	= ixgbevf_init_mbx_params_vf,
335*4882a593Smuzhiyun 	.check_for_rst	= ixgbevf_check_for_rst_vf,
336*4882a593Smuzhiyun };
337