New upstream version 16.11.5
[deb_dpdk.git] / drivers / net / ixgbe / base / ixgbe_mbx.c
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         /* update stats */
448         hw->mbx.stats.msgs_tx++;
449
450         /* Drop VFU and interrupt the PF to tell it a message has been sent */
451         IXGBE_WRITE_REG(hw, IXGBE_VFMAILBOX, IXGBE_VFMAILBOX_REQ);
452
453 out_no_write:
454         return ret_val;
455 }
456
457 /**
458  *  ixgbe_read_mbx_vf - Reads a message from the inbox intended for vf
459  *  @hw: pointer to the HW structure
460  *  @msg: The message buffer
461  *  @size: Length of buffer
462  *  @mbx_id: id of mailbox to read
463  *
464  *  returns SUCCESS if it successfully read message from buffer
465  **/
466 STATIC s32 ixgbe_read_mbx_vf(struct ixgbe_hw *hw, u32 *msg, u16 size,
467                              u16 mbx_id)
468 {
469         s32 ret_val = IXGBE_SUCCESS;
470         u16 i;
471
472         DEBUGFUNC("ixgbe_read_mbx_vf");
473         UNREFERENCED_1PARAMETER(mbx_id);
474
475         /* lock the mailbox to prevent pf/vf race condition */
476         ret_val = ixgbe_obtain_mbx_lock_vf(hw);
477         if (ret_val)
478                 goto out_no_read;
479
480         /* copy the message from the mailbox memory buffer */
481         for (i = 0; i < size; i++)
482                 msg[i] = IXGBE_READ_REG_ARRAY(hw, IXGBE_VFMBMEM, i);
483
484         /* Acknowledge receipt and release mailbox, then we're done */
485         IXGBE_WRITE_REG(hw, IXGBE_VFMAILBOX, IXGBE_VFMAILBOX_ACK);
486
487         /* update stats */
488         hw->mbx.stats.msgs_rx++;
489
490 out_no_read:
491         return ret_val;
492 }
493
494 /**
495  *  ixgbe_init_mbx_params_vf - set initial values for vf mailbox
496  *  @hw: pointer to the HW structure
497  *
498  *  Initializes the hw->mbx struct to correct values for vf mailbox
499  */
500 void ixgbe_init_mbx_params_vf(struct ixgbe_hw *hw)
501 {
502         struct ixgbe_mbx_info *mbx = &hw->mbx;
503
504         /* start mailbox as timed out and let the reset_hw call set the timeout
505          * value to begin communications */
506         mbx->timeout = 0;
507         mbx->usec_delay = IXGBE_VF_MBX_INIT_DELAY;
508
509         mbx->size = IXGBE_VFMAILBOX_SIZE;
510
511         mbx->ops.read = ixgbe_read_mbx_vf;
512         mbx->ops.write = ixgbe_write_mbx_vf;
513         mbx->ops.read_posted = ixgbe_read_posted_mbx;
514         mbx->ops.write_posted = ixgbe_write_posted_mbx;
515         mbx->ops.check_for_msg = ixgbe_check_for_msg_vf;
516         mbx->ops.check_for_ack = ixgbe_check_for_ack_vf;
517         mbx->ops.check_for_rst = ixgbe_check_for_rst_vf;
518
519         mbx->stats.msgs_tx = 0;
520         mbx->stats.msgs_rx = 0;
521         mbx->stats.reqs = 0;
522         mbx->stats.acks = 0;
523         mbx->stats.rsts = 0;
524 }
525
526 STATIC s32 ixgbe_check_for_bit_pf(struct ixgbe_hw *hw, u32 mask, s32 index)
527 {
528         u32 mbvficr = IXGBE_READ_REG(hw, IXGBE_MBVFICR(index));
529         s32 ret_val = IXGBE_ERR_MBX;
530
531         if (mbvficr & mask) {
532                 ret_val = IXGBE_SUCCESS;
533                 IXGBE_WRITE_REG(hw, IXGBE_MBVFICR(index), mask);
534         }
535
536         return ret_val;
537 }
538
539 /**
540  *  ixgbe_check_for_msg_pf - checks to see if the VF has sent mail
541  *  @hw: pointer to the HW structure
542  *  @vf_number: the VF index
543  *
544  *  returns SUCCESS if the VF has set the Status bit or else ERR_MBX
545  **/
546 STATIC s32 ixgbe_check_for_msg_pf(struct ixgbe_hw *hw, u16 vf_number)
547 {
548         s32 ret_val = IXGBE_ERR_MBX;
549         s32 index = IXGBE_MBVFICR_INDEX(vf_number);
550         u32 vf_bit = vf_number % 16;
551
552         DEBUGFUNC("ixgbe_check_for_msg_pf");
553
554         if (!ixgbe_check_for_bit_pf(hw, IXGBE_MBVFICR_VFREQ_VF1 << vf_bit,
555                                     index)) {
556                 ret_val = IXGBE_SUCCESS;
557                 hw->mbx.stats.reqs++;
558         }
559
560         return ret_val;
561 }
562
563 /**
564  *  ixgbe_check_for_ack_pf - checks to see if the VF has ACKed
565  *  @hw: pointer to the HW structure
566  *  @vf_number: the VF index
567  *
568  *  returns SUCCESS if the VF has set the Status bit or else ERR_MBX
569  **/
570 STATIC s32 ixgbe_check_for_ack_pf(struct ixgbe_hw *hw, u16 vf_number)
571 {
572         s32 ret_val = IXGBE_ERR_MBX;
573         s32 index = IXGBE_MBVFICR_INDEX(vf_number);
574         u32 vf_bit = vf_number % 16;
575
576         DEBUGFUNC("ixgbe_check_for_ack_pf");
577
578         if (!ixgbe_check_for_bit_pf(hw, IXGBE_MBVFICR_VFACK_VF1 << vf_bit,
579                                     index)) {
580                 ret_val = IXGBE_SUCCESS;
581                 hw->mbx.stats.acks++;
582         }
583
584         return ret_val;
585 }
586
587 /**
588  *  ixgbe_check_for_rst_pf - checks to see if the VF has reset
589  *  @hw: pointer to the HW structure
590  *  @vf_number: the VF index
591  *
592  *  returns SUCCESS if the VF has set the Status bit or else ERR_MBX
593  **/
594 STATIC s32 ixgbe_check_for_rst_pf(struct ixgbe_hw *hw, u16 vf_number)
595 {
596         u32 reg_offset = (vf_number < 32) ? 0 : 1;
597         u32 vf_shift = vf_number % 32;
598         u32 vflre = 0;
599         s32 ret_val = IXGBE_ERR_MBX;
600
601         DEBUGFUNC("ixgbe_check_for_rst_pf");
602
603         switch (hw->mac.type) {
604         case ixgbe_mac_82599EB:
605                 vflre = IXGBE_READ_REG(hw, IXGBE_VFLRE(reg_offset));
606                 break;
607         case ixgbe_mac_X550:
608         case ixgbe_mac_X550EM_x:
609         case ixgbe_mac_X550EM_a:
610         case ixgbe_mac_X540:
611                 vflre = IXGBE_READ_REG(hw, IXGBE_VFLREC(reg_offset));
612                 break;
613         default:
614                 break;
615         }
616
617         if (vflre & (1 << vf_shift)) {
618                 ret_val = IXGBE_SUCCESS;
619                 IXGBE_WRITE_REG(hw, IXGBE_VFLREC(reg_offset), (1 << vf_shift));
620                 hw->mbx.stats.rsts++;
621         }
622
623         return ret_val;
624 }
625
626 /**
627  *  ixgbe_obtain_mbx_lock_pf - obtain mailbox lock
628  *  @hw: pointer to the HW structure
629  *  @vf_number: the VF index
630  *
631  *  return SUCCESS if we obtained the mailbox lock
632  **/
633 STATIC s32 ixgbe_obtain_mbx_lock_pf(struct ixgbe_hw *hw, u16 vf_number)
634 {
635         s32 ret_val = IXGBE_ERR_MBX;
636         u32 p2v_mailbox;
637
638         DEBUGFUNC("ixgbe_obtain_mbx_lock_pf");
639
640         /* Take ownership of the buffer */
641         IXGBE_WRITE_REG(hw, IXGBE_PFMAILBOX(vf_number), IXGBE_PFMAILBOX_PFU);
642
643         /* reserve mailbox for vf use */
644         p2v_mailbox = IXGBE_READ_REG(hw, IXGBE_PFMAILBOX(vf_number));
645         if (p2v_mailbox & IXGBE_PFMAILBOX_PFU)
646                 ret_val = IXGBE_SUCCESS;
647         else
648                 ERROR_REPORT2(IXGBE_ERROR_POLLING,
649                            "Failed to obtain mailbox lock for VF%d", vf_number);
650
651
652         return ret_val;
653 }
654
655 /**
656  *  ixgbe_write_mbx_pf - Places a message in the mailbox
657  *  @hw: pointer to the HW structure
658  *  @msg: The message buffer
659  *  @size: Length of buffer
660  *  @vf_number: the VF index
661  *
662  *  returns SUCCESS if it successfully copied message into the buffer
663  **/
664 STATIC s32 ixgbe_write_mbx_pf(struct ixgbe_hw *hw, u32 *msg, u16 size,
665                               u16 vf_number)
666 {
667         s32 ret_val;
668         u16 i;
669
670         DEBUGFUNC("ixgbe_write_mbx_pf");
671
672         /* lock the mailbox to prevent pf/vf race condition */
673         ret_val = ixgbe_obtain_mbx_lock_pf(hw, vf_number);
674         if (ret_val)
675                 goto out_no_write;
676
677         /* flush msg and acks as we are overwriting the message buffer */
678         ixgbe_check_for_msg_pf(hw, vf_number);
679         ixgbe_check_for_ack_pf(hw, vf_number);
680
681         /* copy the caller specified message to the mailbox memory buffer */
682         for (i = 0; i < size; i++)
683                 IXGBE_WRITE_REG_ARRAY(hw, IXGBE_PFMBMEM(vf_number), i, msg[i]);
684
685         /* Interrupt VF to tell it a message has been sent and release buffer*/
686         IXGBE_WRITE_REG(hw, IXGBE_PFMAILBOX(vf_number), IXGBE_PFMAILBOX_STS);
687
688         /* update stats */
689         hw->mbx.stats.msgs_tx++;
690
691 out_no_write:
692         return ret_val;
693
694 }
695
696 /**
697  *  ixgbe_read_mbx_pf - Read a message from the mailbox
698  *  @hw: pointer to the HW structure
699  *  @msg: The message buffer
700  *  @size: Length of buffer
701  *  @vf_number: the VF index
702  *
703  *  This function copies a message from the mailbox buffer to the caller's
704  *  memory buffer.  The presumption is that the caller knows that there was
705  *  a message due to a VF request so no polling for message is needed.
706  **/
707 STATIC s32 ixgbe_read_mbx_pf(struct ixgbe_hw *hw, u32 *msg, u16 size,
708                              u16 vf_number)
709 {
710         s32 ret_val;
711         u16 i;
712
713         DEBUGFUNC("ixgbe_read_mbx_pf");
714
715         /* lock the mailbox to prevent pf/vf race condition */
716         ret_val = ixgbe_obtain_mbx_lock_pf(hw, vf_number);
717         if (ret_val)
718                 goto out_no_read;
719
720         /* copy the message to the mailbox memory buffer */
721         for (i = 0; i < size; i++)
722                 msg[i] = IXGBE_READ_REG_ARRAY(hw, IXGBE_PFMBMEM(vf_number), i);
723
724         /* Acknowledge the message and release buffer */
725         IXGBE_WRITE_REG(hw, IXGBE_PFMAILBOX(vf_number), IXGBE_PFMAILBOX_ACK);
726
727         /* update stats */
728         hw->mbx.stats.msgs_rx++;
729
730 out_no_read:
731         return ret_val;
732 }
733
734 /**
735  *  ixgbe_init_mbx_params_pf - set initial values for pf mailbox
736  *  @hw: pointer to the HW structure
737  *
738  *  Initializes the hw->mbx struct to correct values for pf mailbox
739  */
740 void ixgbe_init_mbx_params_pf(struct ixgbe_hw *hw)
741 {
742         struct ixgbe_mbx_info *mbx = &hw->mbx;
743
744         if (hw->mac.type != ixgbe_mac_82599EB &&
745             hw->mac.type != ixgbe_mac_X550 &&
746             hw->mac.type != ixgbe_mac_X550EM_x &&
747             hw->mac.type != ixgbe_mac_X550EM_a &&
748             hw->mac.type != ixgbe_mac_X540)
749                 return;
750
751         mbx->timeout = 0;
752         mbx->usec_delay = 0;
753
754         mbx->size = IXGBE_VFMAILBOX_SIZE;
755
756         mbx->ops.read = ixgbe_read_mbx_pf;
757         mbx->ops.write = ixgbe_write_mbx_pf;
758         mbx->ops.read_posted = ixgbe_read_posted_mbx;
759         mbx->ops.write_posted = ixgbe_write_posted_mbx;
760         mbx->ops.check_for_msg = ixgbe_check_for_msg_pf;
761         mbx->ops.check_for_ack = ixgbe_check_for_ack_pf;
762         mbx->ops.check_for_rst = ixgbe_check_for_rst_pf;
763
764         mbx->stats.msgs_tx = 0;
765         mbx->stats.msgs_rx = 0;
766         mbx->stats.reqs = 0;
767         mbx->stats.acks = 0;
768         mbx->stats.rsts = 0;
769 }