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