1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2001-2018
5 #include "ixgbe_type.h"
9 * ixgbe_read_mbx - Reads a message from the mailbox
10 * @hw: pointer to the HW structure
11 * @msg: The message buffer
12 * @size: Length of buffer
13 * @mbx_id: id of mailbox to read
15 * returns SUCCESS if it successfully read message from buffer
17 s32 ixgbe_read_mbx(struct ixgbe_hw *hw, u32 *msg, u16 size, u16 mbx_id)
19 struct ixgbe_mbx_info *mbx = &hw->mbx;
20 s32 ret_val = IXGBE_ERR_MBX;
22 DEBUGFUNC("ixgbe_read_mbx");
24 /* limit read to size of mailbox */
29 ret_val = mbx->ops.read(hw, msg, size, mbx_id);
35 * ixgbe_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 SUCCESS if it successfully copied message into the buffer
43 s32 ixgbe_write_mbx(struct ixgbe_hw *hw, u32 *msg, u16 size, u16 mbx_id)
45 struct ixgbe_mbx_info *mbx = &hw->mbx;
46 s32 ret_val = IXGBE_SUCCESS;
48 DEBUGFUNC("ixgbe_write_mbx");
50 if (size > mbx->size) {
51 ret_val = IXGBE_ERR_MBX;
52 ERROR_REPORT2(IXGBE_ERROR_ARGUMENT,
53 "Invalid mailbox message size %d", size);
54 } else if (mbx->ops.write)
55 ret_val = mbx->ops.write(hw, msg, size, mbx_id);
61 * ixgbe_check_for_msg - checks to see if someone sent us mail
62 * @hw: pointer to the HW structure
63 * @mbx_id: id of mailbox to check
65 * returns SUCCESS if the Status bit was found or else ERR_MBX
67 s32 ixgbe_check_for_msg(struct ixgbe_hw *hw, u16 mbx_id)
69 struct ixgbe_mbx_info *mbx = &hw->mbx;
70 s32 ret_val = IXGBE_ERR_MBX;
72 DEBUGFUNC("ixgbe_check_for_msg");
74 if (mbx->ops.check_for_msg)
75 ret_val = mbx->ops.check_for_msg(hw, mbx_id);
81 * ixgbe_check_for_ack - checks to see if someone sent us ACK
82 * @hw: pointer to the HW structure
83 * @mbx_id: id of mailbox to check
85 * returns SUCCESS if the Status bit was found or else ERR_MBX
87 s32 ixgbe_check_for_ack(struct ixgbe_hw *hw, u16 mbx_id)
89 struct ixgbe_mbx_info *mbx = &hw->mbx;
90 s32 ret_val = IXGBE_ERR_MBX;
92 DEBUGFUNC("ixgbe_check_for_ack");
94 if (mbx->ops.check_for_ack)
95 ret_val = mbx->ops.check_for_ack(hw, mbx_id);
101 * ixgbe_check_for_rst - checks to see if other side has reset
102 * @hw: pointer to the HW structure
103 * @mbx_id: id of mailbox to check
105 * returns SUCCESS if the Status bit was found or else ERR_MBX
107 s32 ixgbe_check_for_rst(struct ixgbe_hw *hw, u16 mbx_id)
109 struct ixgbe_mbx_info *mbx = &hw->mbx;
110 s32 ret_val = IXGBE_ERR_MBX;
112 DEBUGFUNC("ixgbe_check_for_rst");
114 if (mbx->ops.check_for_rst)
115 ret_val = mbx->ops.check_for_rst(hw, mbx_id);
121 * ixgbe_poll_for_msg - Wait for message notification
122 * @hw: pointer to the HW structure
123 * @mbx_id: id of mailbox to write
125 * returns SUCCESS if it successfully received a message notification
127 STATIC s32 ixgbe_poll_for_msg(struct ixgbe_hw *hw, u16 mbx_id)
129 struct ixgbe_mbx_info *mbx = &hw->mbx;
130 int countdown = mbx->timeout;
132 DEBUGFUNC("ixgbe_poll_for_msg");
134 if (!countdown || !mbx->ops.check_for_msg)
137 while (countdown && mbx->ops.check_for_msg(hw, mbx_id)) {
141 usec_delay(mbx->usec_delay);
145 ERROR_REPORT2(IXGBE_ERROR_POLLING,
146 "Polling for VF%d mailbox message timedout", mbx_id);
149 return countdown ? IXGBE_SUCCESS : IXGBE_ERR_MBX;
153 * ixgbe_poll_for_ack - Wait for message acknowledgement
154 * @hw: pointer to the HW structure
155 * @mbx_id: id of mailbox to write
157 * returns SUCCESS if it successfully received a message acknowledgement
159 STATIC s32 ixgbe_poll_for_ack(struct ixgbe_hw *hw, u16 mbx_id)
161 struct ixgbe_mbx_info *mbx = &hw->mbx;
162 int countdown = mbx->timeout;
164 DEBUGFUNC("ixgbe_poll_for_ack");
166 if (!countdown || !mbx->ops.check_for_ack)
169 while (countdown && mbx->ops.check_for_ack(hw, mbx_id)) {
173 usec_delay(mbx->usec_delay);
177 ERROR_REPORT2(IXGBE_ERROR_POLLING,
178 "Polling for VF%d mailbox ack timedout", mbx_id);
181 return countdown ? IXGBE_SUCCESS : IXGBE_ERR_MBX;
185 * ixgbe_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 ixgbe_read_posted_mbx(struct ixgbe_hw *hw, u32 *msg, u16 size, u16 mbx_id)
196 struct ixgbe_mbx_info *mbx = &hw->mbx;
197 s32 ret_val = IXGBE_ERR_MBX;
199 DEBUGFUNC("ixgbe_read_posted_mbx");
204 ret_val = ixgbe_poll_for_msg(hw, mbx_id);
206 /* if ack received read message, otherwise we timed out */
208 ret_val = mbx->ops.read(hw, msg, size, mbx_id);
214 * ixgbe_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 ixgbe_write_posted_mbx(struct ixgbe_hw *hw, u32 *msg, u16 size,
226 struct ixgbe_mbx_info *mbx = &hw->mbx;
227 s32 ret_val = IXGBE_ERR_MBX;
229 DEBUGFUNC("ixgbe_write_posted_mbx");
231 /* exit if either we can't write or there isn't a defined timeout */
232 if (!mbx->ops.write || !mbx->timeout)
236 ret_val = mbx->ops.write(hw, msg, size, mbx_id);
238 /* if msg sent wait until we receive an ack */
240 ret_val = ixgbe_poll_for_ack(hw, mbx_id);
246 * ixgbe_init_mbx_ops_generic - Initialize MB function pointers
247 * @hw: pointer to the HW structure
249 * Setups up the mailbox read and write message function pointers
251 void ixgbe_init_mbx_ops_generic(struct ixgbe_hw *hw)
253 struct ixgbe_mbx_info *mbx = &hw->mbx;
255 mbx->ops.read_posted = ixgbe_read_posted_mbx;
256 mbx->ops.write_posted = ixgbe_write_posted_mbx;
260 * ixgbe_read_v2p_mailbox - read v2p mailbox
261 * @hw: pointer to the HW structure
263 * This function is used to read the v2p mailbox without losing the read to
266 STATIC u32 ixgbe_read_v2p_mailbox(struct ixgbe_hw *hw)
268 u32 v2p_mailbox = IXGBE_READ_REG(hw, IXGBE_VFMAILBOX);
270 v2p_mailbox |= hw->mbx.v2p_mailbox;
271 hw->mbx.v2p_mailbox |= v2p_mailbox & IXGBE_VFMAILBOX_R2C_BITS;
277 * ixgbe_check_for_bit_vf - Determine if a status bit was set
278 * @hw: pointer to the HW structure
279 * @mask: bitmask for bits to be tested and cleared
281 * This function is used to check for the read to clear bits within
284 STATIC s32 ixgbe_check_for_bit_vf(struct ixgbe_hw *hw, u32 mask)
286 u32 v2p_mailbox = ixgbe_read_v2p_mailbox(hw);
287 s32 ret_val = IXGBE_ERR_MBX;
289 if (v2p_mailbox & mask)
290 ret_val = IXGBE_SUCCESS;
292 hw->mbx.v2p_mailbox &= ~mask;
298 * ixgbe_check_for_msg_vf - checks to see if the PF has sent mail
299 * @hw: pointer to the HW structure
300 * @mbx_id: id of mailbox to check
302 * returns SUCCESS if the PF has set the Status bit or else ERR_MBX
304 STATIC s32 ixgbe_check_for_msg_vf(struct ixgbe_hw *hw, u16 mbx_id)
306 s32 ret_val = IXGBE_ERR_MBX;
308 UNREFERENCED_1PARAMETER(mbx_id);
309 DEBUGFUNC("ixgbe_check_for_msg_vf");
311 if (!ixgbe_check_for_bit_vf(hw, IXGBE_VFMAILBOX_PFSTS)) {
312 ret_val = IXGBE_SUCCESS;
313 hw->mbx.stats.reqs++;
320 * ixgbe_check_for_ack_vf - checks to see if the PF has ACK'd
321 * @hw: pointer to the HW structure
322 * @mbx_id: id of mailbox to check
324 * returns SUCCESS if the PF has set the ACK bit or else ERR_MBX
326 STATIC s32 ixgbe_check_for_ack_vf(struct ixgbe_hw *hw, u16 mbx_id)
328 s32 ret_val = IXGBE_ERR_MBX;
330 UNREFERENCED_1PARAMETER(mbx_id);
331 DEBUGFUNC("ixgbe_check_for_ack_vf");
333 if (!ixgbe_check_for_bit_vf(hw, IXGBE_VFMAILBOX_PFACK)) {
334 ret_val = IXGBE_SUCCESS;
335 hw->mbx.stats.acks++;
342 * ixgbe_check_for_rst_vf - checks to see if the PF has reset
343 * @hw: pointer to the HW structure
344 * @mbx_id: id of mailbox to check
346 * returns true if the PF has set the reset done bit or else false
348 STATIC s32 ixgbe_check_for_rst_vf(struct ixgbe_hw *hw, u16 mbx_id)
350 s32 ret_val = IXGBE_ERR_MBX;
352 UNREFERENCED_1PARAMETER(mbx_id);
353 DEBUGFUNC("ixgbe_check_for_rst_vf");
355 if (!ixgbe_check_for_bit_vf(hw, (IXGBE_VFMAILBOX_RSTD |
356 IXGBE_VFMAILBOX_RSTI))) {
357 ret_val = IXGBE_SUCCESS;
358 hw->mbx.stats.rsts++;
365 * ixgbe_obtain_mbx_lock_vf - obtain mailbox lock
366 * @hw: pointer to the HW structure
368 * return SUCCESS if we obtained the mailbox lock
370 STATIC s32 ixgbe_obtain_mbx_lock_vf(struct ixgbe_hw *hw)
372 s32 ret_val = IXGBE_ERR_MBX;
374 DEBUGFUNC("ixgbe_obtain_mbx_lock_vf");
376 /* Take ownership of the buffer */
377 IXGBE_WRITE_REG(hw, IXGBE_VFMAILBOX, IXGBE_VFMAILBOX_VFU);
379 /* reserve mailbox for vf use */
380 if (ixgbe_read_v2p_mailbox(hw) & IXGBE_VFMAILBOX_VFU)
381 ret_val = IXGBE_SUCCESS;
387 * ixgbe_write_mbx_vf - Write a message to the mailbox
388 * @hw: pointer to the HW structure
389 * @msg: The message buffer
390 * @size: Length of buffer
391 * @mbx_id: id of mailbox to write
393 * returns SUCCESS if it successfully copied message into the buffer
395 STATIC s32 ixgbe_write_mbx_vf(struct ixgbe_hw *hw, u32 *msg, u16 size,
401 UNREFERENCED_1PARAMETER(mbx_id);
403 DEBUGFUNC("ixgbe_write_mbx_vf");
405 /* lock the mailbox to prevent pf/vf race condition */
406 ret_val = ixgbe_obtain_mbx_lock_vf(hw);
410 /* flush msg and acks as we are overwriting the message buffer */
411 ixgbe_check_for_msg_vf(hw, 0);
412 ixgbe_check_for_ack_vf(hw, 0);
414 /* copy the caller specified message to the mailbox memory buffer */
415 for (i = 0; i < size; i++)
416 IXGBE_WRITE_REG_ARRAY(hw, IXGBE_VFMBMEM, i, msg[i]);
419 hw->mbx.stats.msgs_tx++;
421 /* Drop VFU and interrupt the PF to tell it a message has been sent */
422 IXGBE_WRITE_REG(hw, IXGBE_VFMAILBOX, IXGBE_VFMAILBOX_REQ);
429 * ixgbe_read_mbx_vf - Reads a message from the inbox intended for vf
430 * @hw: pointer to the HW structure
431 * @msg: The message buffer
432 * @size: Length of buffer
433 * @mbx_id: id of mailbox to read
435 * returns SUCCESS if it successfully read message from buffer
437 STATIC s32 ixgbe_read_mbx_vf(struct ixgbe_hw *hw, u32 *msg, u16 size,
440 s32 ret_val = IXGBE_SUCCESS;
443 DEBUGFUNC("ixgbe_read_mbx_vf");
444 UNREFERENCED_1PARAMETER(mbx_id);
446 /* lock the mailbox to prevent pf/vf race condition */
447 ret_val = ixgbe_obtain_mbx_lock_vf(hw);
451 /* copy the message from the mailbox memory buffer */
452 for (i = 0; i < size; i++)
453 msg[i] = IXGBE_READ_REG_ARRAY(hw, IXGBE_VFMBMEM, i);
455 /* Acknowledge receipt and release mailbox, then we're done */
456 IXGBE_WRITE_REG(hw, IXGBE_VFMAILBOX, IXGBE_VFMAILBOX_ACK);
459 hw->mbx.stats.msgs_rx++;
466 * ixgbe_init_mbx_params_vf - set initial values for vf mailbox
467 * @hw: pointer to the HW structure
469 * Initializes the hw->mbx struct to correct values for vf mailbox
471 void ixgbe_init_mbx_params_vf(struct ixgbe_hw *hw)
473 struct ixgbe_mbx_info *mbx = &hw->mbx;
475 /* start mailbox as timed out and let the reset_hw call set the timeout
476 * value to begin communications */
478 mbx->usec_delay = IXGBE_VF_MBX_INIT_DELAY;
480 mbx->size = IXGBE_VFMAILBOX_SIZE;
482 mbx->ops.read = ixgbe_read_mbx_vf;
483 mbx->ops.write = ixgbe_write_mbx_vf;
484 mbx->ops.read_posted = ixgbe_read_posted_mbx;
485 mbx->ops.write_posted = ixgbe_write_posted_mbx;
486 mbx->ops.check_for_msg = ixgbe_check_for_msg_vf;
487 mbx->ops.check_for_ack = ixgbe_check_for_ack_vf;
488 mbx->ops.check_for_rst = ixgbe_check_for_rst_vf;
490 mbx->stats.msgs_tx = 0;
491 mbx->stats.msgs_rx = 0;
497 STATIC s32 ixgbe_check_for_bit_pf(struct ixgbe_hw *hw, u32 mask, s32 index)
499 u32 mbvficr = IXGBE_READ_REG(hw, IXGBE_MBVFICR(index));
500 s32 ret_val = IXGBE_ERR_MBX;
502 if (mbvficr & mask) {
503 ret_val = IXGBE_SUCCESS;
504 IXGBE_WRITE_REG(hw, IXGBE_MBVFICR(index), mask);
511 * ixgbe_check_for_msg_pf - checks to see if the VF has sent mail
512 * @hw: pointer to the HW structure
513 * @vf_number: the VF index
515 * returns SUCCESS if the VF has set the Status bit or else ERR_MBX
517 STATIC s32 ixgbe_check_for_msg_pf(struct ixgbe_hw *hw, u16 vf_number)
519 s32 ret_val = IXGBE_ERR_MBX;
520 s32 index = IXGBE_MBVFICR_INDEX(vf_number);
521 u32 vf_bit = vf_number % 16;
523 DEBUGFUNC("ixgbe_check_for_msg_pf");
525 if (!ixgbe_check_for_bit_pf(hw, IXGBE_MBVFICR_VFREQ_VF1 << vf_bit,
527 ret_val = IXGBE_SUCCESS;
528 hw->mbx.stats.reqs++;
535 * ixgbe_check_for_ack_pf - checks to see if the VF has ACKed
536 * @hw: pointer to the HW structure
537 * @vf_number: the VF index
539 * returns SUCCESS if the VF has set the Status bit or else ERR_MBX
541 STATIC s32 ixgbe_check_for_ack_pf(struct ixgbe_hw *hw, u16 vf_number)
543 s32 ret_val = IXGBE_ERR_MBX;
544 s32 index = IXGBE_MBVFICR_INDEX(vf_number);
545 u32 vf_bit = vf_number % 16;
547 DEBUGFUNC("ixgbe_check_for_ack_pf");
549 if (!ixgbe_check_for_bit_pf(hw, IXGBE_MBVFICR_VFACK_VF1 << vf_bit,
551 ret_val = IXGBE_SUCCESS;
552 hw->mbx.stats.acks++;
559 * ixgbe_check_for_rst_pf - checks to see if the VF has reset
560 * @hw: pointer to the HW structure
561 * @vf_number: the VF index
563 * returns SUCCESS if the VF has set the Status bit or else ERR_MBX
565 STATIC s32 ixgbe_check_for_rst_pf(struct ixgbe_hw *hw, u16 vf_number)
567 u32 reg_offset = (vf_number < 32) ? 0 : 1;
568 u32 vf_shift = vf_number % 32;
570 s32 ret_val = IXGBE_ERR_MBX;
572 DEBUGFUNC("ixgbe_check_for_rst_pf");
574 switch (hw->mac.type) {
575 case ixgbe_mac_82599EB:
576 vflre = IXGBE_READ_REG(hw, IXGBE_VFLRE(reg_offset));
579 case ixgbe_mac_X550EM_x:
580 case ixgbe_mac_X550EM_a:
582 vflre = IXGBE_READ_REG(hw, IXGBE_VFLREC(reg_offset));
588 if (vflre & (1 << vf_shift)) {
589 ret_val = IXGBE_SUCCESS;
590 IXGBE_WRITE_REG(hw, IXGBE_VFLREC(reg_offset), (1 << vf_shift));
591 hw->mbx.stats.rsts++;
598 * ixgbe_obtain_mbx_lock_pf - obtain mailbox lock
599 * @hw: pointer to the HW structure
600 * @vf_number: the VF index
602 * return SUCCESS if we obtained the mailbox lock
604 STATIC s32 ixgbe_obtain_mbx_lock_pf(struct ixgbe_hw *hw, u16 vf_number)
606 s32 ret_val = IXGBE_ERR_MBX;
609 DEBUGFUNC("ixgbe_obtain_mbx_lock_pf");
611 /* Take ownership of the buffer */
612 IXGBE_WRITE_REG(hw, IXGBE_PFMAILBOX(vf_number), IXGBE_PFMAILBOX_PFU);
614 /* reserve mailbox for vf use */
615 p2v_mailbox = IXGBE_READ_REG(hw, IXGBE_PFMAILBOX(vf_number));
616 if (p2v_mailbox & IXGBE_PFMAILBOX_PFU)
617 ret_val = IXGBE_SUCCESS;
619 ERROR_REPORT2(IXGBE_ERROR_POLLING,
620 "Failed to obtain mailbox lock for VF%d", vf_number);
627 * ixgbe_write_mbx_pf - Places a message in the mailbox
628 * @hw: pointer to the HW structure
629 * @msg: The message buffer
630 * @size: Length of buffer
631 * @vf_number: the VF index
633 * returns SUCCESS if it successfully copied message into the buffer
635 STATIC s32 ixgbe_write_mbx_pf(struct ixgbe_hw *hw, u32 *msg, u16 size,
641 DEBUGFUNC("ixgbe_write_mbx_pf");
643 /* lock the mailbox to prevent pf/vf race condition */
644 ret_val = ixgbe_obtain_mbx_lock_pf(hw, vf_number);
648 /* flush msg and acks as we are overwriting the message buffer */
649 ixgbe_check_for_msg_pf(hw, vf_number);
650 ixgbe_check_for_ack_pf(hw, vf_number);
652 /* copy the caller specified message to the mailbox memory buffer */
653 for (i = 0; i < size; i++)
654 IXGBE_WRITE_REG_ARRAY(hw, IXGBE_PFMBMEM(vf_number), i, msg[i]);
656 /* Interrupt VF to tell it a message has been sent and release buffer*/
657 IXGBE_WRITE_REG(hw, IXGBE_PFMAILBOX(vf_number), IXGBE_PFMAILBOX_STS);
660 hw->mbx.stats.msgs_tx++;
668 * ixgbe_read_mbx_pf - Read a message from the mailbox
669 * @hw: pointer to the HW structure
670 * @msg: The message buffer
671 * @size: Length of buffer
672 * @vf_number: the VF index
674 * This function copies a message from the mailbox buffer to the caller's
675 * memory buffer. The presumption is that the caller knows that there was
676 * a message due to a VF request so no polling for message is needed.
678 STATIC s32 ixgbe_read_mbx_pf(struct ixgbe_hw *hw, u32 *msg, u16 size,
684 DEBUGFUNC("ixgbe_read_mbx_pf");
686 /* lock the mailbox to prevent pf/vf race condition */
687 ret_val = ixgbe_obtain_mbx_lock_pf(hw, vf_number);
691 /* copy the message to the mailbox memory buffer */
692 for (i = 0; i < size; i++)
693 msg[i] = IXGBE_READ_REG_ARRAY(hw, IXGBE_PFMBMEM(vf_number), i);
695 /* Acknowledge the message and release buffer */
696 IXGBE_WRITE_REG(hw, IXGBE_PFMAILBOX(vf_number), IXGBE_PFMAILBOX_ACK);
699 hw->mbx.stats.msgs_rx++;
706 * ixgbe_init_mbx_params_pf - set initial values for pf mailbox
707 * @hw: pointer to the HW structure
709 * Initializes the hw->mbx struct to correct values for pf mailbox
711 void ixgbe_init_mbx_params_pf(struct ixgbe_hw *hw)
713 struct ixgbe_mbx_info *mbx = &hw->mbx;
715 if (hw->mac.type != ixgbe_mac_82599EB &&
716 hw->mac.type != ixgbe_mac_X550 &&
717 hw->mac.type != ixgbe_mac_X550EM_x &&
718 hw->mac.type != ixgbe_mac_X550EM_a &&
719 hw->mac.type != ixgbe_mac_X540)
725 mbx->size = IXGBE_VFMAILBOX_SIZE;
727 mbx->ops.read = ixgbe_read_mbx_pf;
728 mbx->ops.write = ixgbe_write_mbx_pf;
729 mbx->ops.read_posted = ixgbe_read_posted_mbx;
730 mbx->ops.write_posted = ixgbe_write_posted_mbx;
731 mbx->ops.check_for_msg = ixgbe_check_for_msg_pf;
732 mbx->ops.check_for_ack = ixgbe_check_for_ack_pf;
733 mbx->ops.check_for_rst = ixgbe_check_for_rst_pf;
735 mbx->stats.msgs_tx = 0;
736 mbx->stats.msgs_rx = 0;