0fcf2ca29288f4426390d82af7ffcebe6322b199
[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 successfuly 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         UNREFERENCED_1PARAMETER(mbx_id);
329         DEBUGFUNC("ixgbe_check_for_msg_vf");
330
331         if (!ixgbe_check_for_bit_vf(hw, IXGBE_VFMAILBOX_PFSTS)) {
332                 ret_val = IXGBE_SUCCESS;
333                 hw->mbx.stats.reqs++;
334         }
335
336         return ret_val;
337 }
338
339 /**
340  *  ixgbe_check_for_ack_vf - checks to see if the PF has ACK'd
341  *  @hw: pointer to the HW structure
342  *  @mbx_id: id of mailbox to check
343  *
344  *  returns SUCCESS if the PF has set the ACK bit or else ERR_MBX
345  **/
346 STATIC s32 ixgbe_check_for_ack_vf(struct ixgbe_hw *hw, u16 mbx_id)
347 {
348         s32 ret_val = IXGBE_ERR_MBX;
349
350         UNREFERENCED_1PARAMETER(mbx_id);
351         DEBUGFUNC("ixgbe_check_for_ack_vf");
352
353         if (!ixgbe_check_for_bit_vf(hw, IXGBE_VFMAILBOX_PFACK)) {
354                 ret_val = IXGBE_SUCCESS;
355                 hw->mbx.stats.acks++;
356         }
357
358         return ret_val;
359 }
360
361 /**
362  *  ixgbe_check_for_rst_vf - checks to see if the PF has reset
363  *  @hw: pointer to the HW structure
364  *  @mbx_id: id of mailbox to check
365  *
366  *  returns true if the PF has set the reset done bit or else false
367  **/
368 STATIC s32 ixgbe_check_for_rst_vf(struct ixgbe_hw *hw, u16 mbx_id)
369 {
370         s32 ret_val = IXGBE_ERR_MBX;
371
372         UNREFERENCED_1PARAMETER(mbx_id);
373         DEBUGFUNC("ixgbe_check_for_rst_vf");
374
375         if (!ixgbe_check_for_bit_vf(hw, (IXGBE_VFMAILBOX_RSTD |
376                                          IXGBE_VFMAILBOX_RSTI))) {
377                 ret_val = IXGBE_SUCCESS;
378                 hw->mbx.stats.rsts++;
379         }
380
381         return ret_val;
382 }
383
384 /**
385  *  ixgbe_obtain_mbx_lock_vf - obtain mailbox lock
386  *  @hw: pointer to the HW structure
387  *
388  *  return SUCCESS if we obtained the mailbox lock
389  **/
390 STATIC s32 ixgbe_obtain_mbx_lock_vf(struct ixgbe_hw *hw)
391 {
392         s32 ret_val = IXGBE_ERR_MBX;
393
394         DEBUGFUNC("ixgbe_obtain_mbx_lock_vf");
395
396         /* Take ownership of the buffer */
397         IXGBE_WRITE_REG(hw, IXGBE_VFMAILBOX, IXGBE_VFMAILBOX_VFU);
398
399         /* reserve mailbox for vf use */
400         if (ixgbe_read_v2p_mailbox(hw) & IXGBE_VFMAILBOX_VFU)
401                 ret_val = IXGBE_SUCCESS;
402
403         return ret_val;
404 }
405
406 /**
407  *  ixgbe_write_mbx_vf - Write a message to the mailbox
408  *  @hw: pointer to the HW structure
409  *  @msg: The message buffer
410  *  @size: Length of buffer
411  *  @mbx_id: id of mailbox to write
412  *
413  *  returns SUCCESS if it successfully copied message into the buffer
414  **/
415 STATIC s32 ixgbe_write_mbx_vf(struct ixgbe_hw *hw, u32 *msg, u16 size,
416                               u16 mbx_id)
417 {
418         s32 ret_val;
419         u16 i;
420
421         UNREFERENCED_1PARAMETER(mbx_id);
422
423         DEBUGFUNC("ixgbe_write_mbx_vf");
424
425         /* lock the mailbox to prevent pf/vf race condition */
426         ret_val = ixgbe_obtain_mbx_lock_vf(hw);
427         if (ret_val)
428                 goto out_no_write;
429
430         /* flush msg and acks as we are overwriting the message buffer */
431         ixgbe_check_for_msg_vf(hw, 0);
432         ixgbe_check_for_ack_vf(hw, 0);
433
434         /* copy the caller specified message to the mailbox memory buffer */
435         for (i = 0; i < size; i++)
436                 IXGBE_WRITE_REG_ARRAY(hw, IXGBE_VFMBMEM, i, msg[i]);
437
438         /* update stats */
439         hw->mbx.stats.msgs_tx++;
440
441         /* Drop VFU and interrupt the PF to tell it a message has been sent */
442         IXGBE_WRITE_REG(hw, IXGBE_VFMAILBOX, IXGBE_VFMAILBOX_REQ);
443
444 out_no_write:
445         return ret_val;
446 }
447
448 /**
449  *  ixgbe_read_mbx_vf - Reads a message from the inbox intended for vf
450  *  @hw: pointer to the HW structure
451  *  @msg: The message buffer
452  *  @size: Length of buffer
453  *  @mbx_id: id of mailbox to read
454  *
455  *  returns SUCCESS if it successfuly read message from buffer
456  **/
457 STATIC s32 ixgbe_read_mbx_vf(struct ixgbe_hw *hw, u32 *msg, u16 size,
458                              u16 mbx_id)
459 {
460         s32 ret_val = IXGBE_SUCCESS;
461         u16 i;
462
463         DEBUGFUNC("ixgbe_read_mbx_vf");
464         UNREFERENCED_1PARAMETER(mbx_id);
465
466         /* lock the mailbox to prevent pf/vf race condition */
467         ret_val = ixgbe_obtain_mbx_lock_vf(hw);
468         if (ret_val)
469                 goto out_no_read;
470
471         /* copy the message from the mailbox memory buffer */
472         for (i = 0; i < size; i++)
473                 msg[i] = IXGBE_READ_REG_ARRAY(hw, IXGBE_VFMBMEM, i);
474
475         /* Acknowledge receipt and release mailbox, then we're done */
476         IXGBE_WRITE_REG(hw, IXGBE_VFMAILBOX, IXGBE_VFMAILBOX_ACK);
477
478         /* update stats */
479         hw->mbx.stats.msgs_rx++;
480
481 out_no_read:
482         return ret_val;
483 }
484
485 /**
486  *  ixgbe_init_mbx_params_vf - set initial values for vf mailbox
487  *  @hw: pointer to the HW structure
488  *
489  *  Initializes the hw->mbx struct to correct values for vf mailbox
490  */
491 void ixgbe_init_mbx_params_vf(struct ixgbe_hw *hw)
492 {
493         struct ixgbe_mbx_info *mbx = &hw->mbx;
494
495         /* start mailbox as timed out and let the reset_hw call set the timeout
496          * value to begin communications */
497         mbx->timeout = 0;
498         mbx->usec_delay = IXGBE_VF_MBX_INIT_DELAY;
499
500         mbx->size = IXGBE_VFMAILBOX_SIZE;
501
502         mbx->ops.read = ixgbe_read_mbx_vf;
503         mbx->ops.write = ixgbe_write_mbx_vf;
504         mbx->ops.read_posted = ixgbe_read_posted_mbx;
505         mbx->ops.write_posted = ixgbe_write_posted_mbx;
506         mbx->ops.check_for_msg = ixgbe_check_for_msg_vf;
507         mbx->ops.check_for_ack = ixgbe_check_for_ack_vf;
508         mbx->ops.check_for_rst = ixgbe_check_for_rst_vf;
509
510         mbx->stats.msgs_tx = 0;
511         mbx->stats.msgs_rx = 0;
512         mbx->stats.reqs = 0;
513         mbx->stats.acks = 0;
514         mbx->stats.rsts = 0;
515 }
516
517 STATIC s32 ixgbe_check_for_bit_pf(struct ixgbe_hw *hw, u32 mask, s32 index)
518 {
519         u32 mbvficr = IXGBE_READ_REG(hw, IXGBE_MBVFICR(index));
520         s32 ret_val = IXGBE_ERR_MBX;
521
522         if (mbvficr & mask) {
523                 ret_val = IXGBE_SUCCESS;
524                 IXGBE_WRITE_REG(hw, IXGBE_MBVFICR(index), mask);
525         }
526
527         return ret_val;
528 }
529
530 /**
531  *  ixgbe_check_for_msg_pf - checks to see if the VF has sent mail
532  *  @hw: pointer to the HW structure
533  *  @vf_number: the VF index
534  *
535  *  returns SUCCESS if the VF has set the Status bit or else ERR_MBX
536  **/
537 STATIC s32 ixgbe_check_for_msg_pf(struct ixgbe_hw *hw, u16 vf_number)
538 {
539         s32 ret_val = IXGBE_ERR_MBX;
540         s32 index = IXGBE_MBVFICR_INDEX(vf_number);
541         u32 vf_bit = vf_number % 16;
542
543         DEBUGFUNC("ixgbe_check_for_msg_pf");
544
545         if (!ixgbe_check_for_bit_pf(hw, IXGBE_MBVFICR_VFREQ_VF1 << vf_bit,
546                                     index)) {
547                 ret_val = IXGBE_SUCCESS;
548                 hw->mbx.stats.reqs++;
549         }
550
551         return ret_val;
552 }
553
554 /**
555  *  ixgbe_check_for_ack_pf - checks to see if the VF has ACKed
556  *  @hw: pointer to the HW structure
557  *  @vf_number: the VF index
558  *
559  *  returns SUCCESS if the VF has set the Status bit or else ERR_MBX
560  **/
561 STATIC s32 ixgbe_check_for_ack_pf(struct ixgbe_hw *hw, u16 vf_number)
562 {
563         s32 ret_val = IXGBE_ERR_MBX;
564         s32 index = IXGBE_MBVFICR_INDEX(vf_number);
565         u32 vf_bit = vf_number % 16;
566
567         DEBUGFUNC("ixgbe_check_for_ack_pf");
568
569         if (!ixgbe_check_for_bit_pf(hw, IXGBE_MBVFICR_VFACK_VF1 << vf_bit,
570                                     index)) {
571                 ret_val = IXGBE_SUCCESS;
572                 hw->mbx.stats.acks++;
573         }
574
575         return ret_val;
576 }
577
578 /**
579  *  ixgbe_check_for_rst_pf - checks to see if the VF has reset
580  *  @hw: pointer to the HW structure
581  *  @vf_number: the VF index
582  *
583  *  returns SUCCESS if the VF has set the Status bit or else ERR_MBX
584  **/
585 STATIC s32 ixgbe_check_for_rst_pf(struct ixgbe_hw *hw, u16 vf_number)
586 {
587         u32 reg_offset = (vf_number < 32) ? 0 : 1;
588         u32 vf_shift = vf_number % 32;
589         u32 vflre = 0;
590         s32 ret_val = IXGBE_ERR_MBX;
591
592         DEBUGFUNC("ixgbe_check_for_rst_pf");
593
594         switch (hw->mac.type) {
595         case ixgbe_mac_82599EB:
596                 vflre = IXGBE_READ_REG(hw, IXGBE_VFLRE(reg_offset));
597                 break;
598         case ixgbe_mac_X540:
599                 vflre = IXGBE_READ_REG(hw, IXGBE_VFLREC(reg_offset));
600                 break;
601         default:
602                 break;
603         }
604
605         if (vflre & (1 << vf_shift)) {
606                 ret_val = IXGBE_SUCCESS;
607                 IXGBE_WRITE_REG(hw, IXGBE_VFLREC(reg_offset), (1 << vf_shift));
608                 hw->mbx.stats.rsts++;
609         }
610
611         return ret_val;
612 }
613
614 /**
615  *  ixgbe_obtain_mbx_lock_pf - obtain mailbox lock
616  *  @hw: pointer to the HW structure
617  *  @vf_number: the VF index
618  *
619  *  return SUCCESS if we obtained the mailbox lock
620  **/
621 STATIC s32 ixgbe_obtain_mbx_lock_pf(struct ixgbe_hw *hw, u16 vf_number)
622 {
623         s32 ret_val = IXGBE_ERR_MBX;
624         u32 p2v_mailbox;
625
626         DEBUGFUNC("ixgbe_obtain_mbx_lock_pf");
627
628         /* Take ownership of the buffer */
629         IXGBE_WRITE_REG(hw, IXGBE_PFMAILBOX(vf_number), IXGBE_PFMAILBOX_PFU);
630
631         /* reserve mailbox for vf use */
632         p2v_mailbox = IXGBE_READ_REG(hw, IXGBE_PFMAILBOX(vf_number));
633         if (p2v_mailbox & IXGBE_PFMAILBOX_PFU)
634                 ret_val = IXGBE_SUCCESS;
635
636         return ret_val;
637 }
638
639 /**
640  *  ixgbe_write_mbx_pf - Places a message in the mailbox
641  *  @hw: pointer to the HW structure
642  *  @msg: The message buffer
643  *  @size: Length of buffer
644  *  @vf_number: the VF index
645  *
646  *  returns SUCCESS if it successfully copied message into the buffer
647  **/
648 STATIC s32 ixgbe_write_mbx_pf(struct ixgbe_hw *hw, u32 *msg, u16 size,
649                               u16 vf_number)
650 {
651         s32 ret_val;
652         u16 i;
653
654         DEBUGFUNC("ixgbe_write_mbx_pf");
655
656         /* lock the mailbox to prevent pf/vf race condition */
657         ret_val = ixgbe_obtain_mbx_lock_pf(hw, vf_number);
658         if (ret_val)
659                 goto out_no_write;
660
661         /* flush msg and acks as we are overwriting the message buffer */
662         ixgbe_check_for_msg_pf(hw, vf_number);
663         ixgbe_check_for_ack_pf(hw, vf_number);
664
665         /* copy the caller specified message to the mailbox memory buffer */
666         for (i = 0; i < size; i++)
667                 IXGBE_WRITE_REG_ARRAY(hw, IXGBE_PFMBMEM(vf_number), i, msg[i]);
668
669         /* Interrupt VF to tell it a message has been sent and release buffer*/
670         IXGBE_WRITE_REG(hw, IXGBE_PFMAILBOX(vf_number), IXGBE_PFMAILBOX_STS);
671
672         /* update stats */
673         hw->mbx.stats.msgs_tx++;
674
675 out_no_write:
676         return ret_val;
677
678 }
679
680 /**
681  *  ixgbe_read_mbx_pf - Read a message from the mailbox
682  *  @hw: pointer to the HW structure
683  *  @msg: The message buffer
684  *  @size: Length of buffer
685  *  @vf_number: the VF index
686  *
687  *  This function copies a message from the mailbox buffer to the caller's
688  *  memory buffer.  The presumption is that the caller knows that there was
689  *  a message due to a VF request so no polling for message is needed.
690  **/
691 STATIC s32 ixgbe_read_mbx_pf(struct ixgbe_hw *hw, u32 *msg, u16 size,
692                              u16 vf_number)
693 {
694         s32 ret_val;
695         u16 i;
696
697         DEBUGFUNC("ixgbe_read_mbx_pf");
698
699         /* lock the mailbox to prevent pf/vf race condition */
700         ret_val = ixgbe_obtain_mbx_lock_pf(hw, vf_number);
701         if (ret_val)
702                 goto out_no_read;
703
704         /* copy the message to the mailbox memory buffer */
705         for (i = 0; i < size; i++)
706                 msg[i] = IXGBE_READ_REG_ARRAY(hw, IXGBE_PFMBMEM(vf_number), i);
707
708         /* Acknowledge the message and release buffer */
709         IXGBE_WRITE_REG(hw, IXGBE_PFMAILBOX(vf_number), IXGBE_PFMAILBOX_ACK);
710
711         /* update stats */
712         hw->mbx.stats.msgs_rx++;
713
714 out_no_read:
715         return ret_val;
716 }
717
718 /**
719  *  ixgbe_init_mbx_params_pf - set initial values for pf mailbox
720  *  @hw: pointer to the HW structure
721  *
722  *  Initializes the hw->mbx struct to correct values for pf mailbox
723  */
724 void ixgbe_init_mbx_params_pf(struct ixgbe_hw *hw)
725 {
726         struct ixgbe_mbx_info *mbx = &hw->mbx;
727
728         if (hw->mac.type != ixgbe_mac_82599EB &&
729             hw->mac.type != ixgbe_mac_X540)
730                 return;
731
732         mbx->timeout = 0;
733         mbx->usec_delay = 0;
734
735         mbx->size = IXGBE_VFMAILBOX_SIZE;
736
737         mbx->ops.read = ixgbe_read_mbx_pf;
738         mbx->ops.write = ixgbe_write_mbx_pf;
739         mbx->ops.read_posted = ixgbe_read_posted_mbx;
740         mbx->ops.write_posted = ixgbe_write_posted_mbx;
741         mbx->ops.check_for_msg = ixgbe_check_for_msg_pf;
742         mbx->ops.check_for_ack = ixgbe_check_for_ack_pf;
743         mbx->ops.check_for_rst = ixgbe_check_for_rst_pf;
744
745         mbx->stats.msgs_tx = 0;
746         mbx->stats.msgs_rx = 0;
747         mbx->stats.reqs = 0;
748         mbx->stats.acks = 0;
749         mbx->stats.rsts = 0;
750 }