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