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