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