1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2015-2020 Beijing WangXun Technology Co., Ltd.
3 * Copyright(c) 2010-2017 Intel Corporation
6 #include "txgbe_type.h"
11 * txgbe_read_mbx - Reads a message from the mailbox
12 * @hw: pointer to the HW structure
13 * @msg: The message buffer
14 * @size: Length of buffer
15 * @mbx_id: id of mailbox to read
17 * returns 0 if it successfully read message from buffer
19 s32 txgbe_read_mbx(struct txgbe_hw *hw, u32 *msg, u16 size, u16 mbx_id)
21 struct txgbe_mbx_info *mbx = &hw->mbx;
22 s32 ret_val = TXGBE_ERR_MBX;
24 DEBUGFUNC("txgbe_read_mbx");
26 /* limit read to size of mailbox */
31 ret_val = mbx->read(hw, msg, size, mbx_id);
37 * txgbe_write_mbx - Write a message to the mailbox
38 * @hw: pointer to the HW structure
39 * @msg: The message buffer
40 * @size: Length of buffer
41 * @mbx_id: id of mailbox to write
43 * returns 0 if it successfully copied message into the buffer
45 s32 txgbe_write_mbx(struct txgbe_hw *hw, u32 *msg, u16 size, u16 mbx_id)
47 struct txgbe_mbx_info *mbx = &hw->mbx;
50 DEBUGFUNC("txgbe_write_mbx");
52 if (size > mbx->size) {
53 ret_val = TXGBE_ERR_MBX;
54 DEBUGOUT("Invalid mailbox message size %d", size);
55 } else if (mbx->write) {
56 ret_val = mbx->write(hw, msg, size, mbx_id);
63 * txgbe_check_for_msg - checks to see if someone sent us mail
64 * @hw: pointer to the HW structure
65 * @mbx_id: id of mailbox to check
67 * returns 0 if the Status bit was found or else ERR_MBX
69 s32 txgbe_check_for_msg(struct txgbe_hw *hw, u16 mbx_id)
71 struct txgbe_mbx_info *mbx = &hw->mbx;
72 s32 ret_val = TXGBE_ERR_MBX;
74 DEBUGFUNC("txgbe_check_for_msg");
76 if (mbx->check_for_msg)
77 ret_val = mbx->check_for_msg(hw, mbx_id);
83 * txgbe_check_for_ack - checks to see if someone sent us ACK
84 * @hw: pointer to the HW structure
85 * @mbx_id: id of mailbox to check
87 * returns 0 if the Status bit was found or else ERR_MBX
89 s32 txgbe_check_for_ack(struct txgbe_hw *hw, u16 mbx_id)
91 struct txgbe_mbx_info *mbx = &hw->mbx;
92 s32 ret_val = TXGBE_ERR_MBX;
94 DEBUGFUNC("txgbe_check_for_ack");
96 if (mbx->check_for_ack)
97 ret_val = mbx->check_for_ack(hw, mbx_id);
103 * txgbe_check_for_rst - checks to see if other side has reset
104 * @hw: pointer to the HW structure
105 * @mbx_id: id of mailbox to check
107 * returns 0 if the Status bit was found or else ERR_MBX
109 s32 txgbe_check_for_rst(struct txgbe_hw *hw, u16 mbx_id)
111 struct txgbe_mbx_info *mbx = &hw->mbx;
112 s32 ret_val = TXGBE_ERR_MBX;
114 DEBUGFUNC("txgbe_check_for_rst");
116 if (mbx->check_for_rst)
117 ret_val = mbx->check_for_rst(hw, mbx_id);
123 * txgbe_poll_for_msg - Wait for message notification
124 * @hw: pointer to the HW structure
125 * @mbx_id: id of mailbox to write
127 * returns SUCCESS if it successfully received a message notification
129 STATIC s32 txgbe_poll_for_msg(struct txgbe_hw *hw, u16 mbx_id)
131 struct txgbe_mbx_info *mbx = &hw->mbx;
132 int countdown = mbx->timeout;
134 DEBUGFUNC("txgbe_poll_for_msg");
136 if (!countdown || !mbx->check_for_msg)
139 while (countdown && mbx->check_for_msg(hw, mbx_id)) {
143 usec_delay(mbx->usec_delay);
147 DEBUGOUT("Polling for VF%d mailbox message timedout", mbx_id);
150 return countdown ? 0 : TXGBE_ERR_MBX;
154 * txgbe_poll_for_ack - Wait for message acknowledgment
155 * @hw: pointer to the HW structure
156 * @mbx_id: id of mailbox to write
158 * returns SUCCESS if it successfully received a message acknowledgment
160 STATIC s32 txgbe_poll_for_ack(struct txgbe_hw *hw, u16 mbx_id)
162 struct txgbe_mbx_info *mbx = &hw->mbx;
163 int countdown = mbx->timeout;
165 DEBUGFUNC("txgbe_poll_for_ack");
167 if (!countdown || !mbx->check_for_ack)
170 while (countdown && mbx->check_for_ack(hw, mbx_id)) {
174 usec_delay(mbx->usec_delay);
178 DEBUGOUT("Polling for VF%d mailbox ack timedout", mbx_id);
181 return countdown ? 0 : TXGBE_ERR_MBX;
185 * txgbe_read_posted_mbx - Wait for message notification and receive message
186 * @hw: pointer to the HW structure
187 * @msg: The message buffer
188 * @size: Length of buffer
189 * @mbx_id: id of mailbox to write
191 * returns SUCCESS if it successfully received a message notification and
192 * copied it into the receive buffer.
194 s32 txgbe_read_posted_mbx(struct txgbe_hw *hw, u32 *msg, u16 size, u16 mbx_id)
196 struct txgbe_mbx_info *mbx = &hw->mbx;
197 s32 ret_val = TXGBE_ERR_MBX;
199 DEBUGFUNC("txgbe_read_posted_mbx");
204 ret_val = txgbe_poll_for_msg(hw, mbx_id);
206 /* if ack received read message, otherwise we timed out */
208 ret_val = mbx->read(hw, msg, size, mbx_id);
214 * txgbe_write_posted_mbx - Write a message to the mailbox, wait for ack
215 * @hw: pointer to the HW structure
216 * @msg: The message buffer
217 * @size: Length of buffer
218 * @mbx_id: id of mailbox to write
220 * returns SUCCESS if it successfully copied message into the buffer and
221 * received an ack to that message within delay * timeout period
223 s32 txgbe_write_posted_mbx(struct txgbe_hw *hw, u32 *msg, u16 size,
226 struct txgbe_mbx_info *mbx = &hw->mbx;
227 s32 ret_val = TXGBE_ERR_MBX;
229 DEBUGFUNC("txgbe_write_posted_mbx");
231 /* exit if either we can't write or there isn't a defined timeout */
232 if (!mbx->write || !mbx->timeout)
236 ret_val = mbx->write(hw, msg, size, mbx_id);
238 /* if msg sent wait until we receive an ack */
240 ret_val = txgbe_poll_for_ack(hw, mbx_id);
246 * txgbe_read_v2p_mailbox - read v2p mailbox
247 * @hw: pointer to the HW structure
249 * This function is used to read the v2p mailbox without losing the read to
252 STATIC u32 txgbe_read_v2p_mailbox(struct txgbe_hw *hw)
254 u32 v2p_mailbox = rd32(hw, TXGBE_VFMBCTL);
256 v2p_mailbox |= hw->mbx.v2p_mailbox;
257 hw->mbx.v2p_mailbox |= v2p_mailbox & TXGBE_VFMBCTL_R2C_BITS;
263 * txgbe_check_for_bit_vf - Determine if a status bit was set
264 * @hw: pointer to the HW structure
265 * @mask: bitmask for bits to be tested and cleared
267 * This function is used to check for the read to clear bits within
270 STATIC s32 txgbe_check_for_bit_vf(struct txgbe_hw *hw, u32 mask)
272 u32 v2p_mailbox = txgbe_read_v2p_mailbox(hw);
273 s32 ret_val = TXGBE_ERR_MBX;
275 if (v2p_mailbox & mask)
278 hw->mbx.v2p_mailbox &= ~mask;
284 * txgbe_check_for_msg_vf - checks to see if the PF has sent mail
285 * @hw: pointer to the HW structure
286 * @mbx_id: id of mailbox to check
288 * returns SUCCESS if the PF has set the Status bit or else ERR_MBX
290 s32 txgbe_check_for_msg_vf(struct txgbe_hw *hw, u16 mbx_id)
292 s32 ret_val = TXGBE_ERR_MBX;
294 UNREFERENCED_PARAMETER(mbx_id);
295 DEBUGFUNC("txgbe_check_for_msg_vf");
297 if (!txgbe_check_for_bit_vf(hw, TXGBE_VFMBCTL_PFSTS)) {
299 hw->mbx.stats.reqs++;
306 * txgbe_check_for_ack_vf - checks to see if the PF has ACK'd
307 * @hw: pointer to the HW structure
308 * @mbx_id: id of mailbox to check
310 * returns SUCCESS if the PF has set the ACK bit or else ERR_MBX
312 s32 txgbe_check_for_ack_vf(struct txgbe_hw *hw, u16 mbx_id)
314 s32 ret_val = TXGBE_ERR_MBX;
316 UNREFERENCED_PARAMETER(mbx_id);
317 DEBUGFUNC("txgbe_check_for_ack_vf");
319 if (!txgbe_check_for_bit_vf(hw, TXGBE_VFMBCTL_PFACK)) {
321 hw->mbx.stats.acks++;
328 * txgbe_check_for_rst_vf - checks to see if the PF has reset
329 * @hw: pointer to the HW structure
330 * @mbx_id: id of mailbox to check
332 * returns true if the PF has set the reset done bit or else false
334 s32 txgbe_check_for_rst_vf(struct txgbe_hw *hw, u16 mbx_id)
336 s32 ret_val = TXGBE_ERR_MBX;
338 UNREFERENCED_PARAMETER(mbx_id);
339 DEBUGFUNC("txgbe_check_for_rst_vf");
341 if (!txgbe_check_for_bit_vf(hw, (TXGBE_VFMBCTL_RSTD |
342 TXGBE_VFMBCTL_RSTI))) {
344 hw->mbx.stats.rsts++;
351 * txgbe_obtain_mbx_lock_vf - obtain mailbox lock
352 * @hw: pointer to the HW structure
354 * return SUCCESS if we obtained the mailbox lock
356 STATIC s32 txgbe_obtain_mbx_lock_vf(struct txgbe_hw *hw)
358 s32 ret_val = TXGBE_ERR_MBX;
360 DEBUGFUNC("txgbe_obtain_mbx_lock_vf");
362 /* Take ownership of the buffer */
363 wr32(hw, TXGBE_VFMBCTL, TXGBE_VFMBCTL_VFU);
365 /* reserve mailbox for vf use */
366 if (txgbe_read_v2p_mailbox(hw) & TXGBE_VFMBCTL_VFU)
373 * txgbe_write_mbx_vf - Write a message to the mailbox
374 * @hw: pointer to the HW structure
375 * @msg: The message buffer
376 * @size: Length of buffer
377 * @mbx_id: id of mailbox to write
379 * returns SUCCESS if it successfully copied message into the buffer
381 s32 txgbe_write_mbx_vf(struct txgbe_hw *hw, u32 *msg, u16 size,
387 UNREFERENCED_PARAMETER(mbx_id);
389 DEBUGFUNC("txgbe_write_mbx_vf");
391 /* lock the mailbox to prevent pf/vf race condition */
392 ret_val = txgbe_obtain_mbx_lock_vf(hw);
396 /* flush msg and acks as we are overwriting the message buffer */
397 txgbe_check_for_msg_vf(hw, 0);
398 txgbe_check_for_ack_vf(hw, 0);
400 /* copy the caller specified message to the mailbox memory buffer */
401 for (i = 0; i < size; i++)
402 wr32a(hw, TXGBE_VFMBX, i, msg[i]);
405 hw->mbx.stats.msgs_tx++;
407 /* Drop VFU and interrupt the PF to tell it a message has been sent */
408 wr32(hw, TXGBE_VFMBCTL, TXGBE_VFMBCTL_REQ);
415 * txgbe_read_mbx_vf - Reads a message from the inbox intended for vf
416 * @hw: pointer to the HW structure
417 * @msg: The message buffer
418 * @size: Length of buffer
419 * @mbx_id: id of mailbox to read
421 * returns SUCCESS if it successfully read message from buffer
423 s32 txgbe_read_mbx_vf(struct txgbe_hw *hw, u32 *msg, u16 size,
429 DEBUGFUNC("txgbe_read_mbx_vf");
430 UNREFERENCED_PARAMETER(mbx_id);
432 /* lock the mailbox to prevent pf/vf race condition */
433 ret_val = txgbe_obtain_mbx_lock_vf(hw);
437 /* copy the message from the mailbox memory buffer */
438 for (i = 0; i < size; i++)
439 msg[i] = rd32a(hw, TXGBE_VFMBX, i);
441 /* Acknowledge receipt and release mailbox, then we're done */
442 wr32(hw, TXGBE_VFMBCTL, TXGBE_VFMBCTL_ACK);
445 hw->mbx.stats.msgs_rx++;
452 * txgbe_init_mbx_params_vf - set initial values for vf mailbox
453 * @hw: pointer to the HW structure
455 * Initializes the hw->mbx struct to correct values for vf mailbox
457 void txgbe_init_mbx_params_vf(struct txgbe_hw *hw)
459 struct txgbe_mbx_info *mbx = &hw->mbx;
461 /* start mailbox as timed out and let the reset_hw call set the timeout
462 * value to begin communications
465 mbx->usec_delay = TXGBE_VF_MBX_INIT_DELAY;
467 mbx->size = TXGBE_P2VMBX_SIZE;
469 mbx->stats.msgs_tx = 0;
470 mbx->stats.msgs_rx = 0;
476 STATIC s32 txgbe_check_for_bit_pf(struct txgbe_hw *hw, u32 mask, s32 index)
478 u32 mbvficr = rd32(hw, TXGBE_MBVFICR(index));
479 s32 ret_val = TXGBE_ERR_MBX;
481 if (mbvficr & mask) {
483 wr32(hw, TXGBE_MBVFICR(index), mask);
490 * txgbe_check_for_msg_pf - checks to see if the VF has sent mail
491 * @hw: pointer to the HW structure
492 * @vf_number: the VF index
494 * returns 0 if the VF has set the Status bit or else ERR_MBX
496 s32 txgbe_check_for_msg_pf(struct txgbe_hw *hw, u16 vf_number)
498 s32 ret_val = TXGBE_ERR_MBX;
499 s32 index = TXGBE_MBVFICR_INDEX(vf_number);
500 u32 vf_bit = vf_number % 16;
502 DEBUGFUNC("txgbe_check_for_msg_pf");
504 if (!txgbe_check_for_bit_pf(hw, TXGBE_MBVFICR_VFREQ_VF1 << vf_bit,
507 hw->mbx.stats.reqs++;
514 * txgbe_check_for_ack_pf - checks to see if the VF has ACKed
515 * @hw: pointer to the HW structure
516 * @vf_number: the VF index
518 * returns 0 if the VF has set the Status bit or else ERR_MBX
520 s32 txgbe_check_for_ack_pf(struct txgbe_hw *hw, u16 vf_number)
522 s32 ret_val = TXGBE_ERR_MBX;
523 s32 index = TXGBE_MBVFICR_INDEX(vf_number);
524 u32 vf_bit = vf_number % 16;
526 DEBUGFUNC("txgbe_check_for_ack_pf");
528 if (!txgbe_check_for_bit_pf(hw, TXGBE_MBVFICR_VFACK_VF1 << vf_bit,
531 hw->mbx.stats.acks++;
538 * txgbe_check_for_rst_pf - checks to see if the VF has reset
539 * @hw: pointer to the HW structure
540 * @vf_number: the VF index
542 * returns 0 if the VF has set the Status bit or else ERR_MBX
544 s32 txgbe_check_for_rst_pf(struct txgbe_hw *hw, u16 vf_number)
546 u32 reg_offset = (vf_number < 32) ? 0 : 1;
547 u32 vf_shift = vf_number % 32;
549 s32 ret_val = TXGBE_ERR_MBX;
551 DEBUGFUNC("txgbe_check_for_rst_pf");
553 vflre = rd32(hw, TXGBE_FLRVFE(reg_offset));
554 if (vflre & (1 << vf_shift)) {
556 wr32(hw, TXGBE_FLRVFEC(reg_offset), (1 << vf_shift));
557 hw->mbx.stats.rsts++;
564 * txgbe_obtain_mbx_lock_pf - obtain mailbox lock
565 * @hw: pointer to the HW structure
566 * @vf_number: the VF index
568 * return 0 if we obtained the mailbox lock
570 STATIC s32 txgbe_obtain_mbx_lock_pf(struct txgbe_hw *hw, u16 vf_number)
572 s32 ret_val = TXGBE_ERR_MBX;
575 DEBUGFUNC("txgbe_obtain_mbx_lock_pf");
577 /* Take ownership of the buffer */
578 wr32(hw, TXGBE_MBCTL(vf_number), TXGBE_MBCTL_PFU);
580 /* reserve mailbox for vf use */
581 p2v_mailbox = rd32(hw, TXGBE_MBCTL(vf_number));
582 if (p2v_mailbox & TXGBE_MBCTL_PFU)
585 DEBUGOUT("Failed to obtain mailbox lock for VF%d", vf_number);
592 * txgbe_write_mbx_pf - Places a message in the mailbox
593 * @hw: pointer to the HW structure
594 * @msg: The message buffer
595 * @size: Length of buffer
596 * @vf_number: the VF index
598 * returns 0 if it successfully copied message into the buffer
600 s32 txgbe_write_mbx_pf(struct txgbe_hw *hw, u32 *msg, u16 size, u16 vf_number)
605 DEBUGFUNC("txgbe_write_mbx_pf");
607 /* lock the mailbox to prevent pf/vf race condition */
608 ret_val = txgbe_obtain_mbx_lock_pf(hw, vf_number);
612 /* flush msg and acks as we are overwriting the message buffer */
613 txgbe_check_for_msg_pf(hw, vf_number);
614 txgbe_check_for_ack_pf(hw, vf_number);
616 /* copy the caller specified message to the mailbox memory buffer */
617 for (i = 0; i < size; i++)
618 wr32a(hw, TXGBE_MBMEM(vf_number), i, msg[i]);
620 /* Interrupt VF to tell it a message has been sent and release buffer*/
621 wr32(hw, TXGBE_MBCTL(vf_number), TXGBE_MBCTL_STS);
624 hw->mbx.stats.msgs_tx++;
631 * txgbe_read_mbx_pf - Read a message from the mailbox
632 * @hw: pointer to the HW structure
633 * @msg: The message buffer
634 * @size: Length of buffer
635 * @vf_number: the VF index
637 * This function copies a message from the mailbox buffer to the caller's
638 * memory buffer. The presumption is that the caller knows that there was
639 * a message due to a VF request so no polling for message is needed.
641 s32 txgbe_read_mbx_pf(struct txgbe_hw *hw, u32 *msg, u16 size, u16 vf_number)
646 DEBUGFUNC("txgbe_read_mbx_pf");
648 /* lock the mailbox to prevent pf/vf race condition */
649 ret_val = txgbe_obtain_mbx_lock_pf(hw, vf_number);
653 /* copy the message to the mailbox memory buffer */
654 for (i = 0; i < size; i++)
655 msg[i] = rd32a(hw, TXGBE_MBMEM(vf_number), i);
657 /* Acknowledge the message and release buffer */
658 wr32(hw, TXGBE_MBCTL(vf_number), TXGBE_MBCTL_ACK);
661 hw->mbx.stats.msgs_rx++;
668 * txgbe_init_mbx_params_pf - set initial values for pf mailbox
669 * @hw: pointer to the HW structure
671 * Initializes the hw->mbx struct to correct values for pf mailbox
673 void txgbe_init_mbx_params_pf(struct txgbe_hw *hw)
675 struct txgbe_mbx_info *mbx = &hw->mbx;
680 mbx->size = TXGBE_P2VMBX_SIZE;
682 mbx->stats.msgs_tx = 0;
683 mbx->stats.msgs_rx = 0;