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