Loading...
1// SPDX-License-Identifier: GPL-2.0
2/* Copyright(c) 1999 - 2018 Intel Corporation. */
3
4#include "mbx.h"
5#include "ixgbevf.h"
6
7/**
8 * ixgbevf_poll_for_msg - Wait for message notification
9 * @hw: pointer to the HW structure
10 *
11 * returns 0 if it successfully received a message notification
12 **/
13static s32 ixgbevf_poll_for_msg(struct ixgbe_hw *hw)
14{
15 struct ixgbe_mbx_info *mbx = &hw->mbx;
16 int countdown = mbx->timeout;
17
18 if (!countdown || !mbx->ops.check_for_msg)
19 return IXGBE_ERR_CONFIG;
20
21 while (countdown && mbx->ops.check_for_msg(hw)) {
22 countdown--;
23 udelay(mbx->udelay);
24 }
25
26 return countdown ? 0 : IXGBE_ERR_TIMEOUT;
27}
28
29/**
30 * ixgbevf_poll_for_ack - Wait for message acknowledgment
31 * @hw: pointer to the HW structure
32 *
33 * returns 0 if it successfully received a message acknowledgment
34 **/
35static s32 ixgbevf_poll_for_ack(struct ixgbe_hw *hw)
36{
37 struct ixgbe_mbx_info *mbx = &hw->mbx;
38 int countdown = mbx->timeout;
39
40 if (!countdown || !mbx->ops.check_for_ack)
41 return IXGBE_ERR_CONFIG;
42
43 while (countdown && mbx->ops.check_for_ack(hw)) {
44 countdown--;
45 udelay(mbx->udelay);
46 }
47
48 return countdown ? 0 : IXGBE_ERR_TIMEOUT;
49}
50
51/**
52 * ixgbevf_read_mailbox_vf - read VF's mailbox register
53 * @hw: pointer to the HW structure
54 *
55 * This function is used to read the mailbox register dedicated for VF without
56 * losing the read to clear status bits.
57 **/
58static u32 ixgbevf_read_mailbox_vf(struct ixgbe_hw *hw)
59{
60 u32 vf_mailbox = IXGBE_READ_REG(hw, IXGBE_VFMAILBOX);
61
62 vf_mailbox |= hw->mbx.vf_mailbox;
63 hw->mbx.vf_mailbox |= vf_mailbox & IXGBE_VFMAILBOX_R2C_BITS;
64
65 return vf_mailbox;
66}
67
68/**
69 * ixgbevf_clear_msg_vf - clear PF status bit
70 * @hw: pointer to the HW structure
71 *
72 * This function is used to clear PFSTS bit in the VFMAILBOX register
73 **/
74static void ixgbevf_clear_msg_vf(struct ixgbe_hw *hw)
75{
76 u32 vf_mailbox = ixgbevf_read_mailbox_vf(hw);
77
78 if (vf_mailbox & IXGBE_VFMAILBOX_PFSTS) {
79 hw->mbx.stats.reqs++;
80 hw->mbx.vf_mailbox &= ~IXGBE_VFMAILBOX_PFSTS;
81 }
82}
83
84/**
85 * ixgbevf_clear_ack_vf - clear PF ACK bit
86 * @hw: pointer to the HW structure
87 *
88 * This function is used to clear PFACK bit in the VFMAILBOX register
89 **/
90static void ixgbevf_clear_ack_vf(struct ixgbe_hw *hw)
91{
92 u32 vf_mailbox = ixgbevf_read_mailbox_vf(hw);
93
94 if (vf_mailbox & IXGBE_VFMAILBOX_PFACK) {
95 hw->mbx.stats.acks++;
96 hw->mbx.vf_mailbox &= ~IXGBE_VFMAILBOX_PFACK;
97 }
98}
99
100/**
101 * ixgbevf_clear_rst_vf - clear PF reset bit
102 * @hw: pointer to the HW structure
103 *
104 * This function is used to clear reset indication and reset done bit in
105 * VFMAILBOX register after reset the shared resources and the reset sequence.
106 **/
107static void ixgbevf_clear_rst_vf(struct ixgbe_hw *hw)
108{
109 u32 vf_mailbox = ixgbevf_read_mailbox_vf(hw);
110
111 if (vf_mailbox & (IXGBE_VFMAILBOX_RSTI | IXGBE_VFMAILBOX_RSTD)) {
112 hw->mbx.stats.rsts++;
113 hw->mbx.vf_mailbox &= ~(IXGBE_VFMAILBOX_RSTI |
114 IXGBE_VFMAILBOX_RSTD);
115 }
116}
117
118/**
119 * ixgbevf_check_for_bit_vf - Determine if a status bit was set
120 * @hw: pointer to the HW structure
121 * @mask: bitmask for bits to be tested and cleared
122 *
123 * This function is used to check for the read to clear bits within
124 * the V2P mailbox.
125 **/
126static s32 ixgbevf_check_for_bit_vf(struct ixgbe_hw *hw, u32 mask)
127{
128 u32 vf_mailbox = ixgbevf_read_mailbox_vf(hw);
129 s32 ret_val = IXGBE_ERR_MBX;
130
131 if (vf_mailbox & mask)
132 ret_val = 0;
133
134 return ret_val;
135}
136
137/**
138 * ixgbevf_check_for_msg_vf - checks to see if the PF has sent mail
139 * @hw: pointer to the HW structure
140 *
141 * returns 0 if the PF has set the Status bit or else ERR_MBX
142 **/
143static s32 ixgbevf_check_for_msg_vf(struct ixgbe_hw *hw)
144{
145 s32 ret_val = IXGBE_ERR_MBX;
146
147 if (!ixgbevf_check_for_bit_vf(hw, IXGBE_VFMAILBOX_PFSTS)) {
148 ret_val = 0;
149 hw->mbx.stats.reqs++;
150 }
151
152 return ret_val;
153}
154
155/**
156 * ixgbevf_check_for_ack_vf - checks to see if the PF has ACK'd
157 * @hw: pointer to the HW structure
158 *
159 * returns 0 if the PF has set the ACK bit or else ERR_MBX
160 **/
161static s32 ixgbevf_check_for_ack_vf(struct ixgbe_hw *hw)
162{
163 s32 ret_val = IXGBE_ERR_MBX;
164
165 if (!ixgbevf_check_for_bit_vf(hw, IXGBE_VFMAILBOX_PFACK)) {
166 ret_val = 0;
167 ixgbevf_clear_ack_vf(hw);
168 hw->mbx.stats.acks++;
169 }
170
171 return ret_val;
172}
173
174/**
175 * ixgbevf_check_for_rst_vf - checks to see if the PF has reset
176 * @hw: pointer to the HW structure
177 *
178 * returns true if the PF has set the reset done bit or else false
179 **/
180static s32 ixgbevf_check_for_rst_vf(struct ixgbe_hw *hw)
181{
182 s32 ret_val = IXGBE_ERR_MBX;
183
184 if (!ixgbevf_check_for_bit_vf(hw, (IXGBE_VFMAILBOX_RSTD |
185 IXGBE_VFMAILBOX_RSTI))) {
186 ret_val = 0;
187 ixgbevf_clear_rst_vf(hw);
188 hw->mbx.stats.rsts++;
189 }
190
191 return ret_val;
192}
193
194/**
195 * ixgbevf_obtain_mbx_lock_vf - obtain mailbox lock
196 * @hw: pointer to the HW structure
197 *
198 * return 0 if we obtained the mailbox lock
199 **/
200static s32 ixgbevf_obtain_mbx_lock_vf(struct ixgbe_hw *hw)
201{
202 struct ixgbe_mbx_info *mbx = &hw->mbx;
203 s32 ret_val = IXGBE_ERR_CONFIG;
204 int countdown = mbx->timeout;
205 u32 vf_mailbox;
206
207 if (!mbx->timeout)
208 return ret_val;
209
210 while (countdown--) {
211 /* Reserve mailbox for VF use */
212 vf_mailbox = ixgbevf_read_mailbox_vf(hw);
213 vf_mailbox |= IXGBE_VFMAILBOX_VFU;
214 IXGBE_WRITE_REG(hw, IXGBE_VFMAILBOX, vf_mailbox);
215
216 /* Verify that VF is the owner of the lock */
217 if (ixgbevf_read_mailbox_vf(hw) & IXGBE_VFMAILBOX_VFU) {
218 ret_val = 0;
219 break;
220 }
221
222 /* Wait a bit before trying again */
223 udelay(mbx->udelay);
224 }
225
226 if (ret_val)
227 ret_val = IXGBE_ERR_TIMEOUT;
228
229 return ret_val;
230}
231
232/**
233 * ixgbevf_release_mbx_lock_vf - release mailbox lock
234 * @hw: pointer to the HW structure
235 **/
236static void ixgbevf_release_mbx_lock_vf(struct ixgbe_hw *hw)
237{
238 u32 vf_mailbox;
239
240 /* Return ownership of the buffer */
241 vf_mailbox = ixgbevf_read_mailbox_vf(hw);
242 vf_mailbox &= ~IXGBE_VFMAILBOX_VFU;
243 IXGBE_WRITE_REG(hw, IXGBE_VFMAILBOX, vf_mailbox);
244}
245
246/**
247 * ixgbevf_release_mbx_lock_vf_legacy - release mailbox lock
248 * @hw: pointer to the HW structure
249 **/
250static void ixgbevf_release_mbx_lock_vf_legacy(struct ixgbe_hw *__always_unused hw)
251{
252}
253
254/**
255 * ixgbevf_write_mbx_vf - Write a message to the mailbox
256 * @hw: pointer to the HW structure
257 * @msg: The message buffer
258 * @size: Length of buffer
259 *
260 * returns 0 if it successfully copied message into the buffer
261 **/
262static s32 ixgbevf_write_mbx_vf(struct ixgbe_hw *hw, u32 *msg, u16 size)
263{
264 u32 vf_mailbox;
265 s32 ret_val;
266 u16 i;
267
268 /* lock the mailbox to prevent PF/VF race condition */
269 ret_val = ixgbevf_obtain_mbx_lock_vf(hw);
270 if (ret_val)
271 goto out_no_write;
272
273 /* flush msg and acks as we are overwriting the message buffer */
274 ixgbevf_clear_msg_vf(hw);
275 ixgbevf_clear_ack_vf(hw);
276
277 /* copy the caller specified message to the mailbox memory buffer */
278 for (i = 0; i < size; i++)
279 IXGBE_WRITE_REG_ARRAY(hw, IXGBE_VFMBMEM, i, msg[i]);
280
281 /* update stats */
282 hw->mbx.stats.msgs_tx++;
283
284 /* interrupt the PF to tell it a message has been sent */
285 vf_mailbox = ixgbevf_read_mailbox_vf(hw);
286 vf_mailbox |= IXGBE_VFMAILBOX_REQ;
287 IXGBE_WRITE_REG(hw, IXGBE_VFMAILBOX, vf_mailbox);
288
289 /* if msg sent wait until we receive an ack */
290 ret_val = ixgbevf_poll_for_ack(hw);
291
292out_no_write:
293 hw->mbx.ops.release(hw);
294
295 return ret_val;
296}
297
298/**
299 * ixgbevf_write_mbx_vf_legacy - Write a message to the mailbox
300 * @hw: pointer to the HW structure
301 * @msg: The message buffer
302 * @size: Length of buffer
303 *
304 * returns 0 if it successfully copied message into the buffer
305 **/
306static s32 ixgbevf_write_mbx_vf_legacy(struct ixgbe_hw *hw, u32 *msg, u16 size)
307{
308 s32 ret_val;
309 u16 i;
310
311 /* lock the mailbox to prevent PF/VF race condition */
312 ret_val = ixgbevf_obtain_mbx_lock_vf(hw);
313 if (ret_val)
314 goto out_no_write;
315
316 /* flush msg and acks as we are overwriting the message buffer */
317 ixgbevf_check_for_msg_vf(hw);
318 ixgbevf_clear_msg_vf(hw);
319 ixgbevf_check_for_ack_vf(hw);
320 ixgbevf_clear_ack_vf(hw);
321
322 /* copy the caller specified message to the mailbox memory buffer */
323 for (i = 0; i < size; i++)
324 IXGBE_WRITE_REG_ARRAY(hw, IXGBE_VFMBMEM, i, msg[i]);
325
326 /* update stats */
327 hw->mbx.stats.msgs_tx++;
328
329 /* Drop VFU and interrupt the PF to tell it a message has been sent */
330 IXGBE_WRITE_REG(hw, IXGBE_VFMAILBOX, IXGBE_VFMAILBOX_REQ);
331
332out_no_write:
333 return ret_val;
334}
335
336/**
337 * ixgbevf_read_mbx_vf - Reads a message from the inbox intended for VF
338 * @hw: pointer to the HW structure
339 * @msg: The message buffer
340 * @size: Length of buffer
341 *
342 * returns 0 if it successfully read message from buffer
343 **/
344static s32 ixgbevf_read_mbx_vf(struct ixgbe_hw *hw, u32 *msg, u16 size)
345{
346 u32 vf_mailbox;
347 s32 ret_val;
348 u16 i;
349
350 /* check if there is a message from PF */
351 ret_val = ixgbevf_check_for_msg_vf(hw);
352 if (ret_val)
353 return ret_val;
354
355 ixgbevf_clear_msg_vf(hw);
356
357 /* copy the message from the mailbox memory buffer */
358 for (i = 0; i < size; i++)
359 msg[i] = IXGBE_READ_REG_ARRAY(hw, IXGBE_VFMBMEM, i);
360
361 /* Acknowledge receipt */
362 vf_mailbox = ixgbevf_read_mailbox_vf(hw);
363 vf_mailbox |= IXGBE_VFMAILBOX_ACK;
364 IXGBE_WRITE_REG(hw, IXGBE_VFMAILBOX, vf_mailbox);
365
366 /* update stats */
367 hw->mbx.stats.msgs_rx++;
368
369 return ret_val;
370}
371
372/**
373 * ixgbevf_read_mbx_vf_legacy - Reads a message from the inbox intended for VF
374 * @hw: pointer to the HW structure
375 * @msg: The message buffer
376 * @size: Length of buffer
377 *
378 * returns 0 if it successfully read message from buffer
379 **/
380static s32 ixgbevf_read_mbx_vf_legacy(struct ixgbe_hw *hw, u32 *msg, u16 size)
381{
382 s32 ret_val = 0;
383 u16 i;
384
385 /* lock the mailbox to prevent PF/VF race condition */
386 ret_val = ixgbevf_obtain_mbx_lock_vf(hw);
387 if (ret_val)
388 goto out_no_read;
389
390 /* copy the message from the mailbox memory buffer */
391 for (i = 0; i < size; i++)
392 msg[i] = IXGBE_READ_REG_ARRAY(hw, IXGBE_VFMBMEM, i);
393
394 /* Acknowledge receipt and release mailbox, then we're done */
395 IXGBE_WRITE_REG(hw, IXGBE_VFMAILBOX, IXGBE_VFMAILBOX_ACK);
396
397 /* update stats */
398 hw->mbx.stats.msgs_rx++;
399
400out_no_read:
401 return ret_val;
402}
403
404/**
405 * ixgbevf_init_mbx_params_vf - set initial values for VF mailbox
406 * @hw: pointer to the HW structure
407 *
408 * Initializes the hw->mbx struct to correct values for VF mailbox
409 */
410static s32 ixgbevf_init_mbx_params_vf(struct ixgbe_hw *hw)
411{
412 struct ixgbe_mbx_info *mbx = &hw->mbx;
413
414 /* start mailbox as timed out and let the reset_hw call set the timeout
415 * value to begin communications
416 */
417 mbx->timeout = IXGBE_VF_MBX_INIT_TIMEOUT;
418 mbx->udelay = IXGBE_VF_MBX_INIT_DELAY;
419
420 mbx->size = IXGBE_VFMAILBOX_SIZE;
421
422 mbx->stats.msgs_tx = 0;
423 mbx->stats.msgs_rx = 0;
424 mbx->stats.reqs = 0;
425 mbx->stats.acks = 0;
426 mbx->stats.rsts = 0;
427
428 return 0;
429}
430
431/**
432 * ixgbevf_poll_mbx - Wait for message and read it from the mailbox
433 * @hw: pointer to the HW structure
434 * @msg: The message buffer
435 * @size: Length of buffer
436 *
437 * returns 0 if it successfully read message from buffer
438 **/
439s32 ixgbevf_poll_mbx(struct ixgbe_hw *hw, u32 *msg, u16 size)
440{
441 struct ixgbe_mbx_info *mbx = &hw->mbx;
442 s32 ret_val = IXGBE_ERR_CONFIG;
443
444 if (!mbx->ops.read || !mbx->ops.check_for_msg || !mbx->timeout)
445 return ret_val;
446
447 /* limit read to size of mailbox */
448 if (size > mbx->size)
449 size = mbx->size;
450
451 ret_val = ixgbevf_poll_for_msg(hw);
452 /* if ack received read message, otherwise we timed out */
453 if (!ret_val)
454 ret_val = mbx->ops.read(hw, msg, size);
455
456 return ret_val;
457}
458
459/**
460 * ixgbevf_write_mbx - Write a message to the mailbox and wait for ACK
461 * @hw: pointer to the HW structure
462 * @msg: The message buffer
463 * @size: Length of buffer
464 *
465 * returns 0 if it successfully copied message into the buffer and
466 * received an ACK to that message within specified period
467 **/
468s32 ixgbevf_write_mbx(struct ixgbe_hw *hw, u32 *msg, u16 size)
469{
470 struct ixgbe_mbx_info *mbx = &hw->mbx;
471 s32 ret_val = IXGBE_ERR_CONFIG;
472
473 /**
474 * exit if either we can't write, release
475 * or there is no timeout defined
476 */
477 if (!mbx->ops.write || !mbx->ops.check_for_ack || !mbx->ops.release ||
478 !mbx->timeout)
479 return ret_val;
480
481 if (size > mbx->size)
482 ret_val = IXGBE_ERR_PARAM;
483 else
484 ret_val = mbx->ops.write(hw, msg, size);
485
486 return ret_val;
487}
488
489const struct ixgbe_mbx_operations ixgbevf_mbx_ops = {
490 .init_params = ixgbevf_init_mbx_params_vf,
491 .release = ixgbevf_release_mbx_lock_vf,
492 .read = ixgbevf_read_mbx_vf,
493 .write = ixgbevf_write_mbx_vf,
494 .check_for_msg = ixgbevf_check_for_msg_vf,
495 .check_for_ack = ixgbevf_check_for_ack_vf,
496 .check_for_rst = ixgbevf_check_for_rst_vf,
497};
498
499const struct ixgbe_mbx_operations ixgbevf_mbx_ops_legacy = {
500 .init_params = ixgbevf_init_mbx_params_vf,
501 .release = ixgbevf_release_mbx_lock_vf_legacy,
502 .read = ixgbevf_read_mbx_vf_legacy,
503 .write = ixgbevf_write_mbx_vf_legacy,
504 .check_for_msg = ixgbevf_check_for_msg_vf,
505 .check_for_ack = ixgbevf_check_for_ack_vf,
506 .check_for_rst = ixgbevf_check_for_rst_vf,
507};
508
509/* Mailbox operations when running on Hyper-V.
510 * On Hyper-V, PF/VF communication is not through the
511 * hardware mailbox; this communication is through
512 * a software mediated path.
513 * Most mail box operations are noop while running on
514 * Hyper-V.
515 */
516const struct ixgbe_mbx_operations ixgbevf_hv_mbx_ops = {
517 .init_params = ixgbevf_init_mbx_params_vf,
518 .check_for_rst = ixgbevf_check_for_rst_vf,
519};
1/*******************************************************************************
2
3 Intel 82599 Virtual Function driver
4 Copyright(c) 1999 - 2015 Intel Corporation.
5
6 This program is free software; you can redistribute it and/or modify it
7 under the terms and conditions of the GNU General Public License,
8 version 2, as published by the Free Software Foundation.
9
10 This program is distributed in the hope it will be useful, but WITHOUT
11 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 more details.
14
15 You should have received a copy of the GNU General Public License along with
16 this program; if not, see <http://www.gnu.org/licenses/>.
17
18 The full GNU General Public License is included in this distribution in
19 the file called "COPYING".
20
21 Contact Information:
22 e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
23 Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
24
25*******************************************************************************/
26
27#include "mbx.h"
28#include "ixgbevf.h"
29
30/**
31 * ixgbevf_poll_for_msg - Wait for message notification
32 * @hw: pointer to the HW structure
33 *
34 * returns 0 if it successfully received a message notification
35 **/
36static s32 ixgbevf_poll_for_msg(struct ixgbe_hw *hw)
37{
38 struct ixgbe_mbx_info *mbx = &hw->mbx;
39 int countdown = mbx->timeout;
40
41 while (countdown && mbx->ops.check_for_msg(hw)) {
42 countdown--;
43 udelay(mbx->udelay);
44 }
45
46 /* if we failed, all future posted messages fail until reset */
47 if (!countdown)
48 mbx->timeout = 0;
49
50 return countdown ? 0 : IXGBE_ERR_MBX;
51}
52
53/**
54 * ixgbevf_poll_for_ack - Wait for message acknowledgment
55 * @hw: pointer to the HW structure
56 *
57 * returns 0 if it successfully received a message acknowledgment
58 **/
59static s32 ixgbevf_poll_for_ack(struct ixgbe_hw *hw)
60{
61 struct ixgbe_mbx_info *mbx = &hw->mbx;
62 int countdown = mbx->timeout;
63
64 while (countdown && mbx->ops.check_for_ack(hw)) {
65 countdown--;
66 udelay(mbx->udelay);
67 }
68
69 /* if we failed, all future posted messages fail until reset */
70 if (!countdown)
71 mbx->timeout = 0;
72
73 return countdown ? 0 : IXGBE_ERR_MBX;
74}
75
76/**
77 * ixgbevf_read_posted_mbx - Wait for message notification and receive message
78 * @hw: pointer to the HW structure
79 * @msg: The message buffer
80 * @size: Length of buffer
81 *
82 * returns 0 if it successfully received a message notification and
83 * copied it into the receive buffer.
84 **/
85static s32 ixgbevf_read_posted_mbx(struct ixgbe_hw *hw, u32 *msg, u16 size)
86{
87 struct ixgbe_mbx_info *mbx = &hw->mbx;
88 s32 ret_val = -IXGBE_ERR_MBX;
89
90 if (!mbx->ops.read)
91 goto out;
92
93 ret_val = ixgbevf_poll_for_msg(hw);
94
95 /* if ack received read message, otherwise we timed out */
96 if (!ret_val)
97 ret_val = mbx->ops.read(hw, msg, size);
98out:
99 return ret_val;
100}
101
102/**
103 * ixgbevf_write_posted_mbx - Write a message to the mailbox, wait for ack
104 * @hw: pointer to the HW structure
105 * @msg: The message buffer
106 * @size: Length of buffer
107 *
108 * returns 0 if it successfully copied message into the buffer and
109 * received an ack to that message within delay * timeout period
110 **/
111static s32 ixgbevf_write_posted_mbx(struct ixgbe_hw *hw, u32 *msg, u16 size)
112{
113 struct ixgbe_mbx_info *mbx = &hw->mbx;
114 s32 ret_val = -IXGBE_ERR_MBX;
115
116 /* exit if either we can't write or there isn't a defined timeout */
117 if (!mbx->ops.write || !mbx->timeout)
118 goto out;
119
120 /* send msg */
121 ret_val = mbx->ops.write(hw, msg, size);
122
123 /* if msg sent wait until we receive an ack */
124 if (!ret_val)
125 ret_val = ixgbevf_poll_for_ack(hw);
126out:
127 return ret_val;
128}
129
130/**
131 * ixgbevf_read_v2p_mailbox - read v2p mailbox
132 * @hw: pointer to the HW structure
133 *
134 * This function is used to read the v2p mailbox without losing the read to
135 * clear status bits.
136 **/
137static u32 ixgbevf_read_v2p_mailbox(struct ixgbe_hw *hw)
138{
139 u32 v2p_mailbox = IXGBE_READ_REG(hw, IXGBE_VFMAILBOX);
140
141 v2p_mailbox |= hw->mbx.v2p_mailbox;
142 hw->mbx.v2p_mailbox |= v2p_mailbox & IXGBE_VFMAILBOX_R2C_BITS;
143
144 return v2p_mailbox;
145}
146
147/**
148 * ixgbevf_check_for_bit_vf - Determine if a status bit was set
149 * @hw: pointer to the HW structure
150 * @mask: bitmask for bits to be tested and cleared
151 *
152 * This function is used to check for the read to clear bits within
153 * the V2P mailbox.
154 **/
155static s32 ixgbevf_check_for_bit_vf(struct ixgbe_hw *hw, u32 mask)
156{
157 u32 v2p_mailbox = ixgbevf_read_v2p_mailbox(hw);
158 s32 ret_val = IXGBE_ERR_MBX;
159
160 if (v2p_mailbox & mask)
161 ret_val = 0;
162
163 hw->mbx.v2p_mailbox &= ~mask;
164
165 return ret_val;
166}
167
168/**
169 * ixgbevf_check_for_msg_vf - checks to see if the PF has sent mail
170 * @hw: pointer to the HW structure
171 *
172 * returns 0 if the PF has set the Status bit or else ERR_MBX
173 **/
174static s32 ixgbevf_check_for_msg_vf(struct ixgbe_hw *hw)
175{
176 s32 ret_val = IXGBE_ERR_MBX;
177
178 if (!ixgbevf_check_for_bit_vf(hw, IXGBE_VFMAILBOX_PFSTS)) {
179 ret_val = 0;
180 hw->mbx.stats.reqs++;
181 }
182
183 return ret_val;
184}
185
186/**
187 * ixgbevf_check_for_ack_vf - checks to see if the PF has ACK'd
188 * @hw: pointer to the HW structure
189 *
190 * returns 0 if the PF has set the ACK bit or else ERR_MBX
191 **/
192static s32 ixgbevf_check_for_ack_vf(struct ixgbe_hw *hw)
193{
194 s32 ret_val = IXGBE_ERR_MBX;
195
196 if (!ixgbevf_check_for_bit_vf(hw, IXGBE_VFMAILBOX_PFACK)) {
197 ret_val = 0;
198 hw->mbx.stats.acks++;
199 }
200
201 return ret_val;
202}
203
204/**
205 * ixgbevf_check_for_rst_vf - checks to see if the PF has reset
206 * @hw: pointer to the HW structure
207 *
208 * returns true if the PF has set the reset done bit or else false
209 **/
210static s32 ixgbevf_check_for_rst_vf(struct ixgbe_hw *hw)
211{
212 s32 ret_val = IXGBE_ERR_MBX;
213
214 if (!ixgbevf_check_for_bit_vf(hw, (IXGBE_VFMAILBOX_RSTD |
215 IXGBE_VFMAILBOX_RSTI))) {
216 ret_val = 0;
217 hw->mbx.stats.rsts++;
218 }
219
220 return ret_val;
221}
222
223/**
224 * ixgbevf_obtain_mbx_lock_vf - obtain mailbox lock
225 * @hw: pointer to the HW structure
226 *
227 * return 0 if we obtained the mailbox lock
228 **/
229static s32 ixgbevf_obtain_mbx_lock_vf(struct ixgbe_hw *hw)
230{
231 s32 ret_val = IXGBE_ERR_MBX;
232
233 /* Take ownership of the buffer */
234 IXGBE_WRITE_REG(hw, IXGBE_VFMAILBOX, IXGBE_VFMAILBOX_VFU);
235
236 /* reserve mailbox for VF use */
237 if (ixgbevf_read_v2p_mailbox(hw) & IXGBE_VFMAILBOX_VFU)
238 ret_val = 0;
239
240 return ret_val;
241}
242
243/**
244 * ixgbevf_write_mbx_vf - Write a message to the mailbox
245 * @hw: pointer to the HW structure
246 * @msg: The message buffer
247 * @size: Length of buffer
248 *
249 * returns 0 if it successfully copied message into the buffer
250 **/
251static s32 ixgbevf_write_mbx_vf(struct ixgbe_hw *hw, u32 *msg, u16 size)
252{
253 s32 ret_val;
254 u16 i;
255
256 /* lock the mailbox to prevent PF/VF race condition */
257 ret_val = ixgbevf_obtain_mbx_lock_vf(hw);
258 if (ret_val)
259 goto out_no_write;
260
261 /* flush msg and acks as we are overwriting the message buffer */
262 ixgbevf_check_for_msg_vf(hw);
263 ixgbevf_check_for_ack_vf(hw);
264
265 /* copy the caller specified message to the mailbox memory buffer */
266 for (i = 0; i < size; i++)
267 IXGBE_WRITE_REG_ARRAY(hw, IXGBE_VFMBMEM, i, msg[i]);
268
269 /* update stats */
270 hw->mbx.stats.msgs_tx++;
271
272 /* Drop VFU and interrupt the PF to tell it a message has been sent */
273 IXGBE_WRITE_REG(hw, IXGBE_VFMAILBOX, IXGBE_VFMAILBOX_REQ);
274
275out_no_write:
276 return ret_val;
277}
278
279/**
280 * ixgbevf_read_mbx_vf - Reads a message from the inbox intended for VF
281 * @hw: pointer to the HW structure
282 * @msg: The message buffer
283 * @size: Length of buffer
284 *
285 * returns 0 if it successfully read message from buffer
286 **/
287static s32 ixgbevf_read_mbx_vf(struct ixgbe_hw *hw, u32 *msg, u16 size)
288{
289 s32 ret_val = 0;
290 u16 i;
291
292 /* lock the mailbox to prevent PF/VF race condition */
293 ret_val = ixgbevf_obtain_mbx_lock_vf(hw);
294 if (ret_val)
295 goto out_no_read;
296
297 /* copy the message from the mailbox memory buffer */
298 for (i = 0; i < size; i++)
299 msg[i] = IXGBE_READ_REG_ARRAY(hw, IXGBE_VFMBMEM, i);
300
301 /* Acknowledge receipt and release mailbox, then we're done */
302 IXGBE_WRITE_REG(hw, IXGBE_VFMAILBOX, IXGBE_VFMAILBOX_ACK);
303
304 /* update stats */
305 hw->mbx.stats.msgs_rx++;
306
307out_no_read:
308 return ret_val;
309}
310
311/**
312 * ixgbevf_init_mbx_params_vf - set initial values for VF mailbox
313 * @hw: pointer to the HW structure
314 *
315 * Initializes the hw->mbx struct to correct values for VF mailbox
316 */
317static s32 ixgbevf_init_mbx_params_vf(struct ixgbe_hw *hw)
318{
319 struct ixgbe_mbx_info *mbx = &hw->mbx;
320
321 /* start mailbox as timed out and let the reset_hw call set the timeout
322 * value to begin communications
323 */
324 mbx->timeout = 0;
325 mbx->udelay = IXGBE_VF_MBX_INIT_DELAY;
326
327 mbx->size = IXGBE_VFMAILBOX_SIZE;
328
329 mbx->stats.msgs_tx = 0;
330 mbx->stats.msgs_rx = 0;
331 mbx->stats.reqs = 0;
332 mbx->stats.acks = 0;
333 mbx->stats.rsts = 0;
334
335 return 0;
336}
337
338const struct ixgbe_mbx_operations ixgbevf_mbx_ops = {
339 .init_params = ixgbevf_init_mbx_params_vf,
340 .read = ixgbevf_read_mbx_vf,
341 .write = ixgbevf_write_mbx_vf,
342 .read_posted = ixgbevf_read_posted_mbx,
343 .write_posted = ixgbevf_write_posted_mbx,
344 .check_for_msg = ixgbevf_check_for_msg_vf,
345 .check_for_ack = ixgbevf_check_for_ack_vf,
346 .check_for_rst = ixgbevf_check_for_rst_vf,
347};
348