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