xref: /freebsd/sys/dev/e1000/e1000_mbx.c (revision e1e636193db45630c7881246d25902e57c43d24e)
1 /******************************************************************************
2   SPDX-License-Identifier: BSD-3-Clause
3 
4   Copyright (c) 2001-2020, Intel Corporation
5   All rights reserved.
6 
7   Redistribution and use in source and binary forms, with or without
8   modification, are permitted provided that the following conditions are met:
9 
10    1. Redistributions of source code must retain the above copyright notice,
11       this list of conditions and the following disclaimer.
12 
13    2. Redistributions in binary form must reproduce the above copyright
14       notice, this list of conditions and the following disclaimer in the
15       documentation and/or other materials provided with the distribution.
16 
17    3. Neither the name of the Intel Corporation nor the names of its
18       contributors may be used to endorse or promote products derived from
19       this software without specific prior written permission.
20 
21   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22   AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24   ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
25   LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26   CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27   SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29   CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30   ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31   POSSIBILITY OF SUCH DAMAGE.
32 
33 ******************************************************************************/
34 
35 #include "e1000_mbx.h"
36 
37 /**
38  *  e1000_null_mbx_check_for_flag - No-op function, return 0
39  *  @hw: pointer to the HW structure
40  *  @mbx_id: id of mailbox to read
41  **/
42 static s32 e1000_null_mbx_check_for_flag(struct e1000_hw E1000_UNUSEDARG *hw,
43 					 u16 E1000_UNUSEDARG mbx_id)
44 {
45 	DEBUGFUNC("e1000_null_mbx_check_flag");
46 
47 	return E1000_SUCCESS;
48 }
49 
50 /**
51  *  e1000_null_mbx_transact - No-op function, return 0
52  *  @hw: pointer to the HW structure
53  *  @msg: The message buffer
54  *  @size: Length of buffer
55  *  @mbx_id: id of mailbox to read
56  **/
57 static s32 e1000_null_mbx_transact(struct e1000_hw E1000_UNUSEDARG *hw,
58 				   u32 E1000_UNUSEDARG *msg,
59 				   u16 E1000_UNUSEDARG size,
60 				   u16 E1000_UNUSEDARG mbx_id)
61 {
62 	DEBUGFUNC("e1000_null_mbx_rw_msg");
63 
64 	return E1000_SUCCESS;
65 }
66 
67 /**
68  *  e1000_read_mbx - Reads a message from the mailbox
69  *  @hw: pointer to the HW structure
70  *  @msg: The message buffer
71  *  @size: Length of buffer
72  *  @mbx_id: id of mailbox to read
73  *
74  *  returns SUCCESS if it successfully read message from buffer
75  **/
76 s32 e1000_read_mbx(struct e1000_hw *hw, u32 *msg, u16 size, u16 mbx_id)
77 {
78 	struct e1000_mbx_info *mbx = &hw->mbx;
79 	s32 ret_val = -E1000_ERR_MBX;
80 
81 	DEBUGFUNC("e1000_read_mbx");
82 
83 	/* limit read to size of mailbox */
84 	if (size > mbx->size)
85 		size = mbx->size;
86 
87 	if (mbx->ops.read)
88 		ret_val = mbx->ops.read(hw, msg, size, mbx_id);
89 
90 	return ret_val;
91 }
92 
93 /**
94  *  e1000_write_mbx - Write a message to the mailbox
95  *  @hw: pointer to the HW structure
96  *  @msg: The message buffer
97  *  @size: Length of buffer
98  *  @mbx_id: id of mailbox to write
99  *
100  *  returns SUCCESS if it successfully copied message into the buffer
101  **/
102 s32 e1000_write_mbx(struct e1000_hw *hw, u32 *msg, u16 size, u16 mbx_id)
103 {
104 	struct e1000_mbx_info *mbx = &hw->mbx;
105 	s32 ret_val = E1000_SUCCESS;
106 
107 	DEBUGFUNC("e1000_write_mbx");
108 
109 	if (size > mbx->size)
110 		ret_val = -E1000_ERR_MBX;
111 
112 	else if (mbx->ops.write)
113 		ret_val = mbx->ops.write(hw, msg, size, mbx_id);
114 
115 	return ret_val;
116 }
117 
118 /**
119  *  e1000_check_for_msg - checks to see if someone sent us mail
120  *  @hw: pointer to the HW structure
121  *  @mbx_id: id of mailbox to check
122  *
123  *  returns SUCCESS if the Status bit was found or else ERR_MBX
124  **/
125 s32 e1000_check_for_msg(struct e1000_hw *hw, u16 mbx_id)
126 {
127 	struct e1000_mbx_info *mbx = &hw->mbx;
128 	s32 ret_val = -E1000_ERR_MBX;
129 
130 	DEBUGFUNC("e1000_check_for_msg");
131 
132 	if (mbx->ops.check_for_msg)
133 		ret_val = mbx->ops.check_for_msg(hw, mbx_id);
134 
135 	return ret_val;
136 }
137 
138 /**
139  *  e1000_check_for_ack - checks to see if someone sent us ACK
140  *  @hw: pointer to the HW structure
141  *  @mbx_id: id of mailbox to check
142  *
143  *  returns SUCCESS if the Status bit was found or else ERR_MBX
144  **/
145 s32 e1000_check_for_ack(struct e1000_hw *hw, u16 mbx_id)
146 {
147 	struct e1000_mbx_info *mbx = &hw->mbx;
148 	s32 ret_val = -E1000_ERR_MBX;
149 
150 	DEBUGFUNC("e1000_check_for_ack");
151 
152 	if (mbx->ops.check_for_ack)
153 		ret_val = mbx->ops.check_for_ack(hw, mbx_id);
154 
155 	return ret_val;
156 }
157 
158 /**
159  *  e1000_check_for_rst - checks to see if other side has reset
160  *  @hw: pointer to the HW structure
161  *  @mbx_id: id of mailbox to check
162  *
163  *  returns SUCCESS if the Status bit was found or else ERR_MBX
164  **/
165 s32 e1000_check_for_rst(struct e1000_hw *hw, u16 mbx_id)
166 {
167 	struct e1000_mbx_info *mbx = &hw->mbx;
168 	s32 ret_val = -E1000_ERR_MBX;
169 
170 	DEBUGFUNC("e1000_check_for_rst");
171 
172 	if (mbx->ops.check_for_rst)
173 		ret_val = mbx->ops.check_for_rst(hw, mbx_id);
174 
175 	return ret_val;
176 }
177 
178 /**
179  *  e1000_poll_for_msg - Wait for message notification
180  *  @hw: pointer to the HW structure
181  *  @mbx_id: id of mailbox to write
182  *
183  *  returns SUCCESS if it successfully received a message notification
184  **/
185 static s32 e1000_poll_for_msg(struct e1000_hw *hw, u16 mbx_id)
186 {
187 	struct e1000_mbx_info *mbx = &hw->mbx;
188 	int countdown = mbx->timeout;
189 
190 	DEBUGFUNC("e1000_poll_for_msg");
191 
192 	if (!countdown || !mbx->ops.check_for_msg)
193 		goto out;
194 
195 	while (countdown && mbx->ops.check_for_msg(hw, mbx_id)) {
196 		countdown--;
197 		if (!countdown)
198 			break;
199 		usec_delay(mbx->usec_delay);
200 	}
201 
202 	/* if we failed, all future posted messages fail until reset */
203 	if (!countdown)
204 		mbx->timeout = 0;
205 out:
206 	return countdown ? E1000_SUCCESS : -E1000_ERR_MBX;
207 }
208 
209 /**
210  *  e1000_poll_for_ack - Wait for message acknowledgement
211  *  @hw: pointer to the HW structure
212  *  @mbx_id: id of mailbox to write
213  *
214  *  returns SUCCESS if it successfully received a message acknowledgement
215  **/
216 static s32 e1000_poll_for_ack(struct e1000_hw *hw, u16 mbx_id)
217 {
218 	struct e1000_mbx_info *mbx = &hw->mbx;
219 	int countdown = mbx->timeout;
220 
221 	DEBUGFUNC("e1000_poll_for_ack");
222 
223 	if (!countdown || !mbx->ops.check_for_ack)
224 		goto out;
225 
226 	while (countdown && mbx->ops.check_for_ack(hw, mbx_id)) {
227 		countdown--;
228 		if (!countdown)
229 			break;
230 		usec_delay(mbx->usec_delay);
231 	}
232 
233 	/* if we failed, all future posted messages fail until reset */
234 	if (!countdown)
235 		mbx->timeout = 0;
236 out:
237 	return countdown ? E1000_SUCCESS : -E1000_ERR_MBX;
238 }
239 
240 /**
241  *  e1000_read_posted_mbx - Wait for message notification and receive message
242  *  @hw: pointer to the HW structure
243  *  @msg: The message buffer
244  *  @size: Length of buffer
245  *  @mbx_id: id of mailbox to write
246  *
247  *  returns SUCCESS if it successfully received a message notification and
248  *  copied it into the receive buffer.
249  **/
250 s32 e1000_read_posted_mbx(struct e1000_hw *hw, u32 *msg, u16 size, u16 mbx_id)
251 {
252 	struct e1000_mbx_info *mbx = &hw->mbx;
253 	s32 ret_val = -E1000_ERR_MBX;
254 
255 	DEBUGFUNC("e1000_read_posted_mbx");
256 
257 	if (!mbx->ops.read)
258 		goto out;
259 
260 	ret_val = e1000_poll_for_msg(hw, mbx_id);
261 
262 	/* if ack received read message, otherwise we timed out */
263 	if (!ret_val)
264 		ret_val = mbx->ops.read(hw, msg, size, mbx_id);
265 out:
266 	return ret_val;
267 }
268 
269 /**
270  *  e1000_write_posted_mbx - Write a message to the mailbox, wait for ack
271  *  @hw: pointer to the HW structure
272  *  @msg: The message buffer
273  *  @size: Length of buffer
274  *  @mbx_id: id of mailbox to write
275  *
276  *  returns SUCCESS if it successfully copied message into the buffer and
277  *  received an ack to that message within delay * timeout period
278  **/
279 s32 e1000_write_posted_mbx(struct e1000_hw *hw, u32 *msg, u16 size, u16 mbx_id)
280 {
281 	struct e1000_mbx_info *mbx = &hw->mbx;
282 	s32 ret_val = -E1000_ERR_MBX;
283 
284 	DEBUGFUNC("e1000_write_posted_mbx");
285 
286 	/* exit if either we can't write or there isn't a defined timeout */
287 	if (!mbx->ops.write || !mbx->timeout)
288 		goto out;
289 
290 	/* send msg */
291 	ret_val = mbx->ops.write(hw, msg, size, mbx_id);
292 
293 	/* if msg sent wait until we receive an ack */
294 	if (!ret_val)
295 		ret_val = e1000_poll_for_ack(hw, mbx_id);
296 out:
297 	return ret_val;
298 }
299 
300 /**
301  *  e1000_init_mbx_ops_generic - Initialize mbx function pointers
302  *  @hw: pointer to the HW structure
303  *
304  *  Sets the function pointers to no-op functions
305  **/
306 void e1000_init_mbx_ops_generic(struct e1000_hw *hw)
307 {
308 	struct e1000_mbx_info *mbx = &hw->mbx;
309 	mbx->ops.init_params = e1000_null_ops_generic;
310 	mbx->ops.read = e1000_null_mbx_transact;
311 	mbx->ops.write = e1000_null_mbx_transact;
312 	mbx->ops.check_for_msg = e1000_null_mbx_check_for_flag;
313 	mbx->ops.check_for_ack = e1000_null_mbx_check_for_flag;
314 	mbx->ops.check_for_rst = e1000_null_mbx_check_for_flag;
315 	mbx->ops.read_posted = e1000_read_posted_mbx;
316 	mbx->ops.write_posted = e1000_write_posted_mbx;
317 }
318 
319 /**
320  *  e1000_read_v2p_mailbox - read v2p mailbox
321  *  @hw: pointer to the HW structure
322  *
323  *  This function is used to read the v2p mailbox without losing the read to
324  *  clear status bits.
325  **/
326 static u32 e1000_read_v2p_mailbox(struct e1000_hw *hw)
327 {
328 	u32 v2p_mailbox = E1000_READ_REG(hw, E1000_V2PMAILBOX(0));
329 
330 	v2p_mailbox |= hw->dev_spec.vf.v2p_mailbox;
331 	hw->dev_spec.vf.v2p_mailbox |= v2p_mailbox & E1000_V2PMAILBOX_R2C_BITS;
332 
333 	return v2p_mailbox;
334 }
335 
336 /**
337  *  e1000_check_for_bit_vf - Determine if a status bit was set
338  *  @hw: pointer to the HW structure
339  *  @mask: bitmask for bits to be tested and cleared
340  *
341  *  This function is used to check for the read to clear bits within
342  *  the V2P mailbox.
343  **/
344 static s32 e1000_check_for_bit_vf(struct e1000_hw *hw, u32 mask)
345 {
346 	u32 v2p_mailbox = e1000_read_v2p_mailbox(hw);
347 	s32 ret_val = -E1000_ERR_MBX;
348 
349 	if (v2p_mailbox & mask)
350 		ret_val = E1000_SUCCESS;
351 
352 	hw->dev_spec.vf.v2p_mailbox &= ~mask;
353 
354 	return ret_val;
355 }
356 
357 /**
358  *  e1000_check_for_msg_vf - checks to see if the PF has sent mail
359  *  @hw: pointer to the HW structure
360  *  @mbx_id: id of mailbox to check
361  *
362  *  returns SUCCESS if the PF has set the Status bit or else ERR_MBX
363  **/
364 static s32 e1000_check_for_msg_vf(struct e1000_hw *hw,
365 				  u16 E1000_UNUSEDARG mbx_id)
366 {
367 	s32 ret_val = -E1000_ERR_MBX;
368 
369 	DEBUGFUNC("e1000_check_for_msg_vf");
370 
371 	if (!e1000_check_for_bit_vf(hw, E1000_V2PMAILBOX_PFSTS)) {
372 		ret_val = E1000_SUCCESS;
373 		hw->mbx.stats.reqs++;
374 	}
375 
376 	return ret_val;
377 }
378 
379 /**
380  *  e1000_check_for_ack_vf - checks to see if the PF has ACK'd
381  *  @hw: pointer to the HW structure
382  *  @mbx_id: id of mailbox to check
383  *
384  *  returns SUCCESS if the PF has set the ACK bit or else ERR_MBX
385  **/
386 static s32 e1000_check_for_ack_vf(struct e1000_hw *hw,
387 				  u16 E1000_UNUSEDARG mbx_id)
388 {
389 	s32 ret_val = -E1000_ERR_MBX;
390 
391 	DEBUGFUNC("e1000_check_for_ack_vf");
392 
393 	if (!e1000_check_for_bit_vf(hw, E1000_V2PMAILBOX_PFACK)) {
394 		ret_val = E1000_SUCCESS;
395 		hw->mbx.stats.acks++;
396 	}
397 
398 	return ret_val;
399 }
400 
401 /**
402  *  e1000_check_for_rst_vf - checks to see if the PF has reset
403  *  @hw: pointer to the HW structure
404  *  @mbx_id: id of mailbox to check
405  *
406  *  returns true if the PF has set the reset done bit or else false
407  **/
408 static s32 e1000_check_for_rst_vf(struct e1000_hw *hw,
409 				  u16 E1000_UNUSEDARG mbx_id)
410 {
411 	s32 ret_val = -E1000_ERR_MBX;
412 
413 	DEBUGFUNC("e1000_check_for_rst_vf");
414 
415 	if (!e1000_check_for_bit_vf(hw, (E1000_V2PMAILBOX_RSTD |
416 					 E1000_V2PMAILBOX_RSTI))) {
417 		ret_val = E1000_SUCCESS;
418 		hw->mbx.stats.rsts++;
419 	}
420 
421 	return ret_val;
422 }
423 
424 /**
425  *  e1000_obtain_mbx_lock_vf - obtain mailbox lock
426  *  @hw: pointer to the HW structure
427  *
428  *  return SUCCESS if we obtained the mailbox lock
429  **/
430 static s32 e1000_obtain_mbx_lock_vf(struct e1000_hw *hw)
431 {
432 	s32 ret_val = -E1000_ERR_MBX;
433 	int count = 10;
434 
435 	DEBUGFUNC("e1000_obtain_mbx_lock_vf");
436 
437 	do {
438 		/* Take ownership of the buffer */
439 		E1000_WRITE_REG(hw, E1000_V2PMAILBOX(0), E1000_V2PMAILBOX_VFU);
440 
441 		/* reserve mailbox for vf use */
442 		if (e1000_read_v2p_mailbox(hw) & E1000_V2PMAILBOX_VFU) {
443 			ret_val = E1000_SUCCESS;
444 			break;
445 		}
446 		usec_delay(1000);
447 	} while (count-- > 0);
448 
449 	return ret_val;
450 }
451 
452 /**
453  *  e1000_write_mbx_vf - Write a message to the mailbox
454  *  @hw: pointer to the HW structure
455  *  @msg: The message buffer
456  *  @size: Length of buffer
457  *  @mbx_id: id of mailbox to write
458  *
459  *  returns SUCCESS if it successfully copied message into the buffer
460  **/
461 static s32 e1000_write_mbx_vf(struct e1000_hw *hw, u32 *msg, u16 size,
462 			      u16 E1000_UNUSEDARG mbx_id)
463 {
464 	s32 ret_val;
465 	u16 i;
466 
467 
468 	DEBUGFUNC("e1000_write_mbx_vf");
469 
470 	/* lock the mailbox to prevent pf/vf race condition */
471 	ret_val = e1000_obtain_mbx_lock_vf(hw);
472 	if (ret_val)
473 		goto out_no_write;
474 
475 	/* flush msg and acks as we are overwriting the message buffer */
476 	e1000_check_for_msg_vf(hw, 0);
477 	e1000_check_for_ack_vf(hw, 0);
478 
479 	/* copy the caller specified message to the mailbox memory buffer */
480 	for (i = 0; i < size; i++)
481 		E1000_WRITE_REG_ARRAY(hw, E1000_VMBMEM(0), i, msg[i]);
482 
483 	/* update stats */
484 	hw->mbx.stats.msgs_tx++;
485 
486 	/* Drop VFU and interrupt the PF to tell it a message has been sent */
487 	E1000_WRITE_REG(hw, E1000_V2PMAILBOX(0), E1000_V2PMAILBOX_REQ);
488 
489 out_no_write:
490 	return ret_val;
491 }
492 
493 /**
494  *  e1000_read_mbx_vf - Reads a message from the inbox intended for vf
495  *  @hw: pointer to the HW structure
496  *  @msg: The message buffer
497  *  @size: Length of buffer
498  *  @mbx_id: id of mailbox to read
499  *
500  *  returns SUCCESS if it successfully read message from buffer
501  **/
502 static s32 e1000_read_mbx_vf(struct e1000_hw *hw, u32 *msg, u16 size,
503 			     u16 E1000_UNUSEDARG mbx_id)
504 {
505 	s32 ret_val = E1000_SUCCESS;
506 	u16 i;
507 
508 	DEBUGFUNC("e1000_read_mbx_vf");
509 
510 	/* lock the mailbox to prevent pf/vf race condition */
511 	ret_val = e1000_obtain_mbx_lock_vf(hw);
512 	if (ret_val)
513 		goto out_no_read;
514 
515 	/* copy the message from the mailbox memory buffer */
516 	for (i = 0; i < size; i++)
517 		msg[i] = E1000_READ_REG_ARRAY(hw, E1000_VMBMEM(0), i);
518 
519 	/* Acknowledge receipt and release mailbox, then we're done */
520 	E1000_WRITE_REG(hw, E1000_V2PMAILBOX(0), E1000_V2PMAILBOX_ACK);
521 
522 	/* update stats */
523 	hw->mbx.stats.msgs_rx++;
524 
525 out_no_read:
526 	return ret_val;
527 }
528 
529 /**
530  *  e1000_init_mbx_params_vf - set initial values for vf mailbox
531  *  @hw: pointer to the HW structure
532  *
533  *  Initializes the hw->mbx struct to correct values for vf mailbox
534  */
535 s32 e1000_init_mbx_params_vf(struct e1000_hw *hw)
536 {
537 	struct e1000_mbx_info *mbx = &hw->mbx;
538 
539 	/* start mailbox as timed out and let the reset_hw call set the timeout
540 	 * value to begin communications */
541 	mbx->timeout = 0;
542 	mbx->usec_delay = E1000_VF_MBX_INIT_DELAY;
543 
544 	mbx->size = E1000_VFMAILBOX_SIZE;
545 
546 	mbx->ops.read = e1000_read_mbx_vf;
547 	mbx->ops.write = e1000_write_mbx_vf;
548 	mbx->ops.read_posted = e1000_read_posted_mbx;
549 	mbx->ops.write_posted = e1000_write_posted_mbx;
550 	mbx->ops.check_for_msg = e1000_check_for_msg_vf;
551 	mbx->ops.check_for_ack = e1000_check_for_ack_vf;
552 	mbx->ops.check_for_rst = e1000_check_for_rst_vf;
553 
554 	mbx->stats.msgs_tx = 0;
555 	mbx->stats.msgs_rx = 0;
556 	mbx->stats.reqs = 0;
557 	mbx->stats.acks = 0;
558 	mbx->stats.rsts = 0;
559 
560 	return E1000_SUCCESS;
561 }
562 
563 static s32 e1000_check_for_bit_pf(struct e1000_hw *hw, u32 mask)
564 {
565 	u32 mbvficr = E1000_READ_REG(hw, E1000_MBVFICR);
566 	s32 ret_val = -E1000_ERR_MBX;
567 
568 	if (mbvficr & mask) {
569 		ret_val = E1000_SUCCESS;
570 		E1000_WRITE_REG(hw, E1000_MBVFICR, mask);
571 	}
572 
573 	return ret_val;
574 }
575 
576 /**
577  *  e1000_check_for_msg_pf - checks to see if the VF has sent mail
578  *  @hw: pointer to the HW structure
579  *  @vf_number: the VF index
580  *
581  *  returns SUCCESS if the VF has set the Status bit or else ERR_MBX
582  **/
583 static s32 e1000_check_for_msg_pf(struct e1000_hw *hw, u16 vf_number)
584 {
585 	s32 ret_val = -E1000_ERR_MBX;
586 
587 	DEBUGFUNC("e1000_check_for_msg_pf");
588 
589 	if (!e1000_check_for_bit_pf(hw, E1000_MBVFICR_VFREQ_VF1 << vf_number)) {
590 		ret_val = E1000_SUCCESS;
591 		hw->mbx.stats.reqs++;
592 	}
593 
594 	return ret_val;
595 }
596 
597 /**
598  *  e1000_check_for_ack_pf - checks to see if the VF has ACKed
599  *  @hw: pointer to the HW structure
600  *  @vf_number: the VF index
601  *
602  *  returns SUCCESS if the VF has set the Status bit or else ERR_MBX
603  **/
604 static s32 e1000_check_for_ack_pf(struct e1000_hw *hw, u16 vf_number)
605 {
606 	s32 ret_val = -E1000_ERR_MBX;
607 
608 	DEBUGFUNC("e1000_check_for_ack_pf");
609 
610 	if (!e1000_check_for_bit_pf(hw, E1000_MBVFICR_VFACK_VF1 << vf_number)) {
611 		ret_val = E1000_SUCCESS;
612 		hw->mbx.stats.acks++;
613 	}
614 
615 	return ret_val;
616 }
617 
618 /**
619  *  e1000_check_for_rst_pf - checks to see if the VF has reset
620  *  @hw: pointer to the HW structure
621  *  @vf_number: the VF index
622  *
623  *  returns SUCCESS if the VF has set the Status bit or else ERR_MBX
624  **/
625 static s32 e1000_check_for_rst_pf(struct e1000_hw *hw, u16 vf_number)
626 {
627 	u32 vflre = E1000_READ_REG(hw, E1000_VFLRE);
628 	s32 ret_val = -E1000_ERR_MBX;
629 
630 	DEBUGFUNC("e1000_check_for_rst_pf");
631 
632 	if (vflre & (1 << vf_number)) {
633 		ret_val = E1000_SUCCESS;
634 		E1000_WRITE_REG(hw, E1000_VFLRE, (1 << vf_number));
635 		hw->mbx.stats.rsts++;
636 	}
637 
638 	return ret_val;
639 }
640 
641 /**
642  *  e1000_obtain_mbx_lock_pf - obtain mailbox lock
643  *  @hw: pointer to the HW structure
644  *  @vf_number: the VF index
645  *
646  *  return SUCCESS if we obtained the mailbox lock
647  **/
648 static s32 e1000_obtain_mbx_lock_pf(struct e1000_hw *hw, u16 vf_number)
649 {
650 	s32 ret_val = -E1000_ERR_MBX;
651 	u32 p2v_mailbox;
652 	int count = 10;
653 
654 	DEBUGFUNC("e1000_obtain_mbx_lock_pf");
655 
656 	do {
657 		/* Take ownership of the buffer */
658 		E1000_WRITE_REG(hw, E1000_P2VMAILBOX(vf_number),
659 				E1000_P2VMAILBOX_PFU);
660 
661 		/* reserve mailbox for pf use */
662 		p2v_mailbox = E1000_READ_REG(hw, E1000_P2VMAILBOX(vf_number));
663 		if (p2v_mailbox & E1000_P2VMAILBOX_PFU) {
664 			ret_val = E1000_SUCCESS;
665 			break;
666 		}
667 		usec_delay(1000);
668 	} while (count-- > 0);
669 
670 	return ret_val;
671 
672 }
673 
674 /**
675  *  e1000_write_mbx_pf - Places a message in the mailbox
676  *  @hw: pointer to the HW structure
677  *  @msg: The message buffer
678  *  @size: Length of buffer
679  *  @vf_number: the VF index
680  *
681  *  returns SUCCESS if it successfully copied message into the buffer
682  **/
683 static s32 e1000_write_mbx_pf(struct e1000_hw *hw, u32 *msg, u16 size,
684 			      u16 vf_number)
685 {
686 	s32 ret_val;
687 	u16 i;
688 
689 	DEBUGFUNC("e1000_write_mbx_pf");
690 
691 	/* lock the mailbox to prevent pf/vf race condition */
692 	ret_val = e1000_obtain_mbx_lock_pf(hw, vf_number);
693 	if (ret_val)
694 		goto out_no_write;
695 
696 	/* flush msg and acks as we are overwriting the message buffer */
697 	e1000_check_for_msg_pf(hw, vf_number);
698 	e1000_check_for_ack_pf(hw, vf_number);
699 
700 	/* copy the caller specified message to the mailbox memory buffer */
701 	for (i = 0; i < size; i++)
702 		E1000_WRITE_REG_ARRAY(hw, E1000_VMBMEM(vf_number), i, msg[i]);
703 
704 	/* Interrupt VF to tell it a message has been sent and release buffer*/
705 	E1000_WRITE_REG(hw, E1000_P2VMAILBOX(vf_number), E1000_P2VMAILBOX_STS);
706 
707 	/* update stats */
708 	hw->mbx.stats.msgs_tx++;
709 
710 out_no_write:
711 	return ret_val;
712 
713 }
714 
715 /**
716  *  e1000_read_mbx_pf - Read a message from the mailbox
717  *  @hw: pointer to the HW structure
718  *  @msg: The message buffer
719  *  @size: Length of buffer
720  *  @vf_number: the VF index
721  *
722  *  This function copies a message from the mailbox buffer to the caller's
723  *  memory buffer.  The presumption is that the caller knows that there was
724  *  a message due to a VF request so no polling for message is needed.
725  **/
726 static s32 e1000_read_mbx_pf(struct e1000_hw *hw, u32 *msg, u16 size,
727 			     u16 vf_number)
728 {
729 	s32 ret_val;
730 	u16 i;
731 
732 	DEBUGFUNC("e1000_read_mbx_pf");
733 
734 	/* lock the mailbox to prevent pf/vf race condition */
735 	ret_val = e1000_obtain_mbx_lock_pf(hw, vf_number);
736 	if (ret_val)
737 		goto out_no_read;
738 
739 	/* copy the message to the mailbox memory buffer */
740 	for (i = 0; i < size; i++)
741 		msg[i] = E1000_READ_REG_ARRAY(hw, E1000_VMBMEM(vf_number), i);
742 
743 	/* Acknowledge the message and release buffer */
744 	E1000_WRITE_REG(hw, E1000_P2VMAILBOX(vf_number), E1000_P2VMAILBOX_ACK);
745 
746 	/* update stats */
747 	hw->mbx.stats.msgs_rx++;
748 
749 out_no_read:
750 	return ret_val;
751 }
752 
753 /**
754  *  e1000_init_mbx_params_pf - set initial values for pf mailbox
755  *  @hw: pointer to the HW structure
756  *
757  *  Initializes the hw->mbx struct to correct values for pf mailbox
758  */
759 s32 e1000_init_mbx_params_pf(struct e1000_hw *hw)
760 {
761 	struct e1000_mbx_info *mbx = &hw->mbx;
762 
763 	switch (hw->mac.type) {
764 	case e1000_82576:
765 	case e1000_i350:
766 	case e1000_i354:
767 		mbx->timeout = 0;
768 		mbx->usec_delay = 0;
769 
770 		mbx->size = E1000_VFMAILBOX_SIZE;
771 
772 		mbx->ops.read = e1000_read_mbx_pf;
773 		mbx->ops.write = e1000_write_mbx_pf;
774 		mbx->ops.read_posted = e1000_read_posted_mbx;
775 		mbx->ops.write_posted = e1000_write_posted_mbx;
776 		mbx->ops.check_for_msg = e1000_check_for_msg_pf;
777 		mbx->ops.check_for_ack = e1000_check_for_ack_pf;
778 		mbx->ops.check_for_rst = e1000_check_for_rst_pf;
779 
780 		mbx->stats.msgs_tx = 0;
781 		mbx->stats.msgs_rx = 0;
782 		mbx->stats.reqs = 0;
783 		mbx->stats.acks = 0;
784 		mbx->stats.rsts = 0;
785 		/* FALLTHROUGH */
786 	default:
787 		return E1000_SUCCESS;
788 	}
789 }
790 
791