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 /* limit read to size of mailbox */
29 ret_val = mbx->read(hw, msg, size, mbx_id);
35 * txgbe_write_mbx - Write a message to the mailbox
36 * @hw: pointer to the HW structure
37 * @msg: The message buffer
38 * @size: Length of buffer
39 * @mbx_id: id of mailbox to write
41 * returns 0 if it successfully copied message into the buffer
43 s32 txgbe_write_mbx(struct txgbe_hw *hw, u32 *msg, u16 size, u16 mbx_id)
45 struct txgbe_mbx_info *mbx = &hw->mbx;
48 if (size > mbx->size) {
49 ret_val = TXGBE_ERR_MBX;
50 DEBUGOUT("Invalid mailbox message size %d", size);
51 } else if (mbx->write) {
52 ret_val = mbx->write(hw, msg, size, mbx_id);
59 * txgbe_check_for_msg - checks to see if someone sent us mail
60 * @hw: pointer to the HW structure
61 * @mbx_id: id of mailbox to check
63 * returns 0 if the Status bit was found or else ERR_MBX
65 s32 txgbe_check_for_msg(struct txgbe_hw *hw, u16 mbx_id)
67 struct txgbe_mbx_info *mbx = &hw->mbx;
68 s32 ret_val = TXGBE_ERR_MBX;
70 if (mbx->check_for_msg)
71 ret_val = mbx->check_for_msg(hw, mbx_id);
77 * txgbe_check_for_ack - checks to see if someone sent us ACK
78 * @hw: pointer to the HW structure
79 * @mbx_id: id of mailbox to check
81 * returns 0 if the Status bit was found or else ERR_MBX
83 s32 txgbe_check_for_ack(struct txgbe_hw *hw, u16 mbx_id)
85 struct txgbe_mbx_info *mbx = &hw->mbx;
86 s32 ret_val = TXGBE_ERR_MBX;
88 if (mbx->check_for_ack)
89 ret_val = mbx->check_for_ack(hw, mbx_id);
95 * txgbe_check_for_rst - checks to see if other side has reset
96 * @hw: pointer to the HW structure
97 * @mbx_id: id of mailbox to check
99 * returns 0 if the Status bit was found or else ERR_MBX
101 s32 txgbe_check_for_rst(struct txgbe_hw *hw, u16 mbx_id)
103 struct txgbe_mbx_info *mbx = &hw->mbx;
104 s32 ret_val = TXGBE_ERR_MBX;
106 if (mbx->check_for_rst)
107 ret_val = mbx->check_for_rst(hw, mbx_id);
113 * txgbe_poll_for_msg - Wait for message notification
114 * @hw: pointer to the HW structure
115 * @mbx_id: id of mailbox to write
117 * returns SUCCESS if it successfully received a message notification
119 STATIC s32 txgbe_poll_for_msg(struct txgbe_hw *hw, u16 mbx_id)
121 struct txgbe_mbx_info *mbx = &hw->mbx;
122 int countdown = mbx->timeout;
124 if (!countdown || !mbx->check_for_msg)
127 while (countdown && mbx->check_for_msg(hw, mbx_id)) {
131 usec_delay(mbx->usec_delay);
135 DEBUGOUT("Polling for VF%d mailbox message timedout", mbx_id);
138 return countdown ? 0 : TXGBE_ERR_MBX;
142 * txgbe_poll_for_ack - Wait for message acknowledgment
143 * @hw: pointer to the HW structure
144 * @mbx_id: id of mailbox to write
146 * returns SUCCESS if it successfully received a message acknowledgment
148 STATIC s32 txgbe_poll_for_ack(struct txgbe_hw *hw, u16 mbx_id)
150 struct txgbe_mbx_info *mbx = &hw->mbx;
151 int countdown = mbx->timeout;
153 if (!countdown || !mbx->check_for_ack)
156 while (countdown && mbx->check_for_ack(hw, mbx_id)) {
160 usec_delay(mbx->usec_delay);
164 DEBUGOUT("Polling for VF%d mailbox ack timedout", mbx_id);
167 return countdown ? 0 : TXGBE_ERR_MBX;
171 * txgbe_read_posted_mbx - Wait for message notification and receive message
172 * @hw: pointer to the HW structure
173 * @msg: The message buffer
174 * @size: Length of buffer
175 * @mbx_id: id of mailbox to write
177 * returns SUCCESS if it successfully received a message notification and
178 * copied it into the receive buffer.
180 s32 txgbe_read_posted_mbx(struct txgbe_hw *hw, u32 *msg, u16 size, u16 mbx_id)
182 struct txgbe_mbx_info *mbx = &hw->mbx;
183 s32 ret_val = TXGBE_ERR_MBX;
188 ret_val = txgbe_poll_for_msg(hw, mbx_id);
190 /* if ack received read message, otherwise we timed out */
192 ret_val = mbx->read(hw, msg, size, mbx_id);
198 * txgbe_write_posted_mbx - Write a message to the mailbox, wait for ack
199 * @hw: pointer to the HW structure
200 * @msg: The message buffer
201 * @size: Length of buffer
202 * @mbx_id: id of mailbox to write
204 * returns SUCCESS if it successfully copied message into the buffer and
205 * received an ack to that message within delay * timeout period
207 s32 txgbe_write_posted_mbx(struct txgbe_hw *hw, u32 *msg, u16 size,
210 struct txgbe_mbx_info *mbx = &hw->mbx;
211 s32 ret_val = TXGBE_ERR_MBX;
213 /* exit if either we can't write or there isn't a defined timeout */
214 if (!mbx->write || !mbx->timeout)
218 ret_val = mbx->write(hw, msg, size, mbx_id);
220 /* if msg sent wait until we receive an ack */
222 ret_val = txgbe_poll_for_ack(hw, mbx_id);
228 * txgbe_read_v2p_mailbox - read v2p mailbox
229 * @hw: pointer to the HW structure
231 * This function is used to read the v2p mailbox without losing the read to
234 STATIC u32 txgbe_read_v2p_mailbox(struct txgbe_hw *hw)
236 u32 v2p_mailbox = rd32(hw, TXGBE_VFMBCTL);
238 v2p_mailbox |= hw->mbx.v2p_mailbox;
239 hw->mbx.v2p_mailbox |= v2p_mailbox & TXGBE_VFMBCTL_R2C_BITS;
245 * txgbe_check_for_bit_vf - Determine if a status bit was set
246 * @hw: pointer to the HW structure
247 * @mask: bitmask for bits to be tested and cleared
249 * This function is used to check for the read to clear bits within
252 STATIC s32 txgbe_check_for_bit_vf(struct txgbe_hw *hw, u32 mask)
254 u32 v2p_mailbox = txgbe_read_v2p_mailbox(hw);
255 s32 ret_val = TXGBE_ERR_MBX;
257 if (v2p_mailbox & mask)
260 hw->mbx.v2p_mailbox &= ~mask;
266 * txgbe_check_for_msg_vf - checks to see if the PF has sent mail
267 * @hw: pointer to the HW structure
268 * @mbx_id: id of mailbox to check
270 * returns SUCCESS if the PF has set the Status bit or else ERR_MBX
272 s32 txgbe_check_for_msg_vf(struct txgbe_hw *hw, u16 mbx_id)
274 s32 ret_val = TXGBE_ERR_MBX;
276 UNREFERENCED_PARAMETER(mbx_id);
278 if (!txgbe_check_for_bit_vf(hw, TXGBE_VFMBCTL_PFSTS)) {
280 hw->mbx.stats.reqs++;
287 * txgbe_check_for_ack_vf - checks to see if the PF has ACK'd
288 * @hw: pointer to the HW structure
289 * @mbx_id: id of mailbox to check
291 * returns SUCCESS if the PF has set the ACK bit or else ERR_MBX
293 s32 txgbe_check_for_ack_vf(struct txgbe_hw *hw, u16 mbx_id)
295 s32 ret_val = TXGBE_ERR_MBX;
297 UNREFERENCED_PARAMETER(mbx_id);
299 if (!txgbe_check_for_bit_vf(hw, TXGBE_VFMBCTL_PFACK)) {
301 hw->mbx.stats.acks++;
308 * txgbe_check_for_rst_vf - checks to see if the PF has reset
309 * @hw: pointer to the HW structure
310 * @mbx_id: id of mailbox to check
312 * returns true if the PF has set the reset done bit or else false
314 s32 txgbe_check_for_rst_vf(struct txgbe_hw *hw, u16 mbx_id)
316 s32 ret_val = TXGBE_ERR_MBX;
318 UNREFERENCED_PARAMETER(mbx_id);
320 if (!txgbe_check_for_bit_vf(hw, (TXGBE_VFMBCTL_RSTD |
321 TXGBE_VFMBCTL_RSTI))) {
323 hw->mbx.stats.rsts++;
330 * txgbe_obtain_mbx_lock_vf - obtain mailbox lock
331 * @hw: pointer to the HW structure
333 * return SUCCESS if we obtained the mailbox lock
335 STATIC s32 txgbe_obtain_mbx_lock_vf(struct txgbe_hw *hw)
337 s32 ret_val = TXGBE_ERR_MBX;
339 /* Take ownership of the buffer */
340 wr32(hw, TXGBE_VFMBCTL, TXGBE_VFMBCTL_VFU);
342 /* reserve mailbox for vf use */
343 if (txgbe_read_v2p_mailbox(hw) & TXGBE_VFMBCTL_VFU)
350 * txgbe_write_mbx_vf - Write a message to the mailbox
351 * @hw: pointer to the HW structure
352 * @msg: The message buffer
353 * @size: Length of buffer
354 * @mbx_id: id of mailbox to write
356 * returns SUCCESS if it successfully copied message into the buffer
358 s32 txgbe_write_mbx_vf(struct txgbe_hw *hw, u32 *msg, u16 size,
364 UNREFERENCED_PARAMETER(mbx_id);
366 /* lock the mailbox to prevent pf/vf race condition */
367 ret_val = txgbe_obtain_mbx_lock_vf(hw);
371 /* flush msg and acks as we are overwriting the message buffer */
372 txgbe_check_for_msg_vf(hw, 0);
373 txgbe_check_for_ack_vf(hw, 0);
375 /* copy the caller specified message to the mailbox memory buffer */
376 for (i = 0; i < size; i++)
377 wr32a(hw, TXGBE_VFMBX, i, msg[i]);
380 hw->mbx.stats.msgs_tx++;
382 /* Drop VFU and interrupt the PF to tell it a message has been sent */
383 wr32(hw, TXGBE_VFMBCTL, TXGBE_VFMBCTL_REQ);
390 * txgbe_read_mbx_vf - Reads a message from the inbox intended for vf
391 * @hw: pointer to the HW structure
392 * @msg: The message buffer
393 * @size: Length of buffer
394 * @mbx_id: id of mailbox to read
396 * returns SUCCESS if it successfully read message from buffer
398 s32 txgbe_read_mbx_vf(struct txgbe_hw *hw, u32 *msg, u16 size,
404 UNREFERENCED_PARAMETER(mbx_id);
406 /* lock the mailbox to prevent pf/vf race condition */
407 ret_val = txgbe_obtain_mbx_lock_vf(hw);
411 /* copy the message from the mailbox memory buffer */
412 for (i = 0; i < size; i++)
413 msg[i] = rd32a(hw, TXGBE_VFMBX, i);
415 /* Acknowledge receipt and release mailbox, then we're done */
416 wr32(hw, TXGBE_VFMBCTL, TXGBE_VFMBCTL_ACK);
419 hw->mbx.stats.msgs_rx++;
426 * txgbe_init_mbx_params_vf - set initial values for vf mailbox
427 * @hw: pointer to the HW structure
429 * Initializes the hw->mbx struct to correct values for vf mailbox
431 void txgbe_init_mbx_params_vf(struct txgbe_hw *hw)
433 struct txgbe_mbx_info *mbx = &hw->mbx;
435 /* start mailbox as timed out and let the reset_hw call set the timeout
436 * value to begin communications
439 mbx->usec_delay = TXGBE_VF_MBX_INIT_DELAY;
441 mbx->size = TXGBE_P2VMBX_SIZE;
443 mbx->stats.msgs_tx = 0;
444 mbx->stats.msgs_rx = 0;
450 STATIC s32 txgbe_check_for_bit_pf(struct txgbe_hw *hw, u32 mask, s32 index)
452 u32 mbvficr = rd32(hw, TXGBE_MBVFICR(index));
453 s32 ret_val = TXGBE_ERR_MBX;
455 if (mbvficr & mask) {
457 wr32(hw, TXGBE_MBVFICR(index), mask);
464 * txgbe_check_for_msg_pf - checks to see if the VF has sent mail
465 * @hw: pointer to the HW structure
466 * @vf_number: the VF index
468 * returns 0 if the VF has set the Status bit or else ERR_MBX
470 s32 txgbe_check_for_msg_pf(struct txgbe_hw *hw, u16 vf_number)
472 s32 ret_val = TXGBE_ERR_MBX;
473 s32 index = TXGBE_MBVFICR_INDEX(vf_number);
474 u32 vf_bit = vf_number % 16;
476 if (!txgbe_check_for_bit_pf(hw, TXGBE_MBVFICR_VFREQ_VF1 << vf_bit,
479 hw->mbx.stats.reqs++;
486 * txgbe_check_for_ack_pf - checks to see if the VF has ACKed
487 * @hw: pointer to the HW structure
488 * @vf_number: the VF index
490 * returns 0 if the VF has set the Status bit or else ERR_MBX
492 s32 txgbe_check_for_ack_pf(struct txgbe_hw *hw, u16 vf_number)
494 s32 ret_val = TXGBE_ERR_MBX;
495 s32 index = TXGBE_MBVFICR_INDEX(vf_number);
496 u32 vf_bit = vf_number % 16;
498 if (!txgbe_check_for_bit_pf(hw, TXGBE_MBVFICR_VFACK_VF1 << vf_bit,
501 hw->mbx.stats.acks++;
508 * txgbe_check_for_rst_pf - checks to see if the VF has reset
509 * @hw: pointer to the HW structure
510 * @vf_number: the VF index
512 * returns 0 if the VF has set the Status bit or else ERR_MBX
514 s32 txgbe_check_for_rst_pf(struct txgbe_hw *hw, u16 vf_number)
516 u32 reg_offset = (vf_number < 32) ? 0 : 1;
517 u32 vf_shift = vf_number % 32;
519 s32 ret_val = TXGBE_ERR_MBX;
521 vflre = rd32(hw, TXGBE_FLRVFE(reg_offset));
522 if (vflre & (1 << vf_shift)) {
524 wr32(hw, TXGBE_FLRVFEC(reg_offset), (1 << vf_shift));
525 hw->mbx.stats.rsts++;
532 * txgbe_obtain_mbx_lock_pf - obtain mailbox lock
533 * @hw: pointer to the HW structure
534 * @vf_number: the VF index
536 * return 0 if we obtained the mailbox lock
538 STATIC s32 txgbe_obtain_mbx_lock_pf(struct txgbe_hw *hw, u16 vf_number)
540 s32 ret_val = TXGBE_ERR_MBX;
543 /* Take ownership of the buffer */
544 wr32(hw, TXGBE_MBCTL(vf_number), TXGBE_MBCTL_PFU);
546 /* reserve mailbox for vf use */
547 p2v_mailbox = rd32(hw, TXGBE_MBCTL(vf_number));
548 if (p2v_mailbox & TXGBE_MBCTL_PFU)
551 DEBUGOUT("Failed to obtain mailbox lock for VF%d", vf_number);
558 * txgbe_write_mbx_pf - Places a message in the mailbox
559 * @hw: pointer to the HW structure
560 * @msg: The message buffer
561 * @size: Length of buffer
562 * @vf_number: the VF index
564 * returns 0 if it successfully copied message into the buffer
566 s32 txgbe_write_mbx_pf(struct txgbe_hw *hw, u32 *msg, u16 size, u16 vf_number)
571 /* lock the mailbox to prevent pf/vf race condition */
572 ret_val = txgbe_obtain_mbx_lock_pf(hw, vf_number);
576 /* flush msg and acks as we are overwriting the message buffer */
577 txgbe_check_for_msg_pf(hw, vf_number);
578 txgbe_check_for_ack_pf(hw, vf_number);
580 /* copy the caller specified message to the mailbox memory buffer */
581 for (i = 0; i < size; i++)
582 wr32a(hw, TXGBE_MBMEM(vf_number), i, msg[i]);
584 /* Interrupt VF to tell it a message has been sent and release buffer*/
585 wr32(hw, TXGBE_MBCTL(vf_number), TXGBE_MBCTL_STS);
588 hw->mbx.stats.msgs_tx++;
595 * txgbe_read_mbx_pf - Read a message from the mailbox
596 * @hw: pointer to the HW structure
597 * @msg: The message buffer
598 * @size: Length of buffer
599 * @vf_number: the VF index
601 * This function copies a message from the mailbox buffer to the caller's
602 * memory buffer. The presumption is that the caller knows that there was
603 * a message due to a VF request so no polling for message is needed.
605 s32 txgbe_read_mbx_pf(struct txgbe_hw *hw, u32 *msg, u16 size, u16 vf_number)
610 /* lock the mailbox to prevent pf/vf race condition */
611 ret_val = txgbe_obtain_mbx_lock_pf(hw, vf_number);
615 /* copy the message to the mailbox memory buffer */
616 for (i = 0; i < size; i++)
617 msg[i] = rd32a(hw, TXGBE_MBMEM(vf_number), i);
619 /* Acknowledge the message and release buffer */
620 wr32(hw, TXGBE_MBCTL(vf_number), TXGBE_MBCTL_ACK);
623 hw->mbx.stats.msgs_rx++;
630 * txgbe_init_mbx_params_pf - set initial values for pf mailbox
631 * @hw: pointer to the HW structure
633 * Initializes the hw->mbx struct to correct values for pf mailbox
635 void txgbe_init_mbx_params_pf(struct txgbe_hw *hw)
637 struct txgbe_mbx_info *mbx = &hw->mbx;
642 mbx->size = TXGBE_P2VMBX_SIZE;
644 mbx->stats.msgs_tx = 0;
645 mbx->stats.msgs_rx = 0;