1 /*******************************************************************************
3 Copyright (c) 2001-2012, Intel Corporation
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions are met:
9 1. Redistributions of source code must retain the above copyright notice,
10 this list of conditions and the following disclaimer.
12 2. Redistributions in binary form must reproduce the above copyright
13 notice, this list of conditions and the following disclaimer in the
14 documentation and/or other materials provided with the distribution.
16 3. Neither the name of the Intel Corporation nor the names of its
17 contributors may be used to endorse or promote products derived from
18 this software without specific prior written permission.
20 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 POSSIBILITY OF SUCH DAMAGE.
32 ***************************************************************************/
34 #include "e1000_mbx.h"
37 * e1000_null_mbx_check_for_flag - No-op function, return 0
38 * @hw: pointer to the HW structure
40 static s32 e1000_null_mbx_check_for_flag(struct e1000_hw *hw, u16 mbx_id)
42 DEBUGFUNC("e1000_null_mbx_check_flag");
48 * e1000_null_mbx_transact - No-op function, return 0
49 * @hw: pointer to the HW structure
51 static s32 e1000_null_mbx_transact(struct e1000_hw *hw, u32 *msg, u16 size,
54 DEBUGFUNC("e1000_null_mbx_rw_msg");
60 * e1000_read_mbx - Reads a message from the mailbox
61 * @hw: pointer to the HW structure
62 * @msg: The message buffer
63 * @size: Length of buffer
64 * @mbx_id: id of mailbox to read
66 * returns SUCCESS if it successfuly read message from buffer
68 s32 e1000_read_mbx(struct e1000_hw *hw, u32 *msg, u16 size, u16 mbx_id)
70 struct e1000_mbx_info *mbx = &hw->mbx;
71 s32 ret_val = -E1000_ERR_MBX;
73 DEBUGFUNC("e1000_read_mbx");
75 /* limit read to size of mailbox */
80 ret_val = mbx->ops.read(hw, msg, size, mbx_id);
86 * e1000_write_mbx - Write a message to the mailbox
87 * @hw: pointer to the HW structure
88 * @msg: The message buffer
89 * @size: Length of buffer
90 * @mbx_id: id of mailbox to write
92 * returns SUCCESS if it successfully copied message into the buffer
94 s32 e1000_write_mbx(struct e1000_hw *hw, u32 *msg, u16 size, u16 mbx_id)
96 struct e1000_mbx_info *mbx = &hw->mbx;
97 s32 ret_val = E1000_SUCCESS;
99 DEBUGFUNC("e1000_write_mbx");
101 if (size > mbx->size)
102 ret_val = -E1000_ERR_MBX;
104 else if (mbx->ops.write)
105 ret_val = mbx->ops.write(hw, msg, size, mbx_id);
111 * e1000_check_for_msg - checks to see if someone sent us mail
112 * @hw: pointer to the HW structure
113 * @mbx_id: id of mailbox to check
115 * returns SUCCESS if the Status bit was found or else ERR_MBX
117 s32 e1000_check_for_msg(struct e1000_hw *hw, u16 mbx_id)
119 struct e1000_mbx_info *mbx = &hw->mbx;
120 s32 ret_val = -E1000_ERR_MBX;
122 DEBUGFUNC("e1000_check_for_msg");
124 if (mbx->ops.check_for_msg)
125 ret_val = mbx->ops.check_for_msg(hw, mbx_id);
131 * e1000_check_for_ack - checks to see if someone sent us ACK
132 * @hw: pointer to the HW structure
133 * @mbx_id: id of mailbox to check
135 * returns SUCCESS if the Status bit was found or else ERR_MBX
137 s32 e1000_check_for_ack(struct e1000_hw *hw, u16 mbx_id)
139 struct e1000_mbx_info *mbx = &hw->mbx;
140 s32 ret_val = -E1000_ERR_MBX;
142 DEBUGFUNC("e1000_check_for_ack");
144 if (mbx->ops.check_for_ack)
145 ret_val = mbx->ops.check_for_ack(hw, mbx_id);
151 * e1000_check_for_rst - checks to see if other side has reset
152 * @hw: pointer to the HW structure
153 * @mbx_id: id of mailbox to check
155 * returns SUCCESS if the Status bit was found or else ERR_MBX
157 s32 e1000_check_for_rst(struct e1000_hw *hw, u16 mbx_id)
159 struct e1000_mbx_info *mbx = &hw->mbx;
160 s32 ret_val = -E1000_ERR_MBX;
162 DEBUGFUNC("e1000_check_for_rst");
164 if (mbx->ops.check_for_rst)
165 ret_val = mbx->ops.check_for_rst(hw, mbx_id);
171 * e1000_poll_for_msg - Wait for message notification
172 * @hw: pointer to the HW structure
173 * @mbx_id: id of mailbox to write
175 * returns SUCCESS if it successfully received a message notification
177 static s32 e1000_poll_for_msg(struct e1000_hw *hw, u16 mbx_id)
179 struct e1000_mbx_info *mbx = &hw->mbx;
180 int countdown = mbx->timeout;
182 DEBUGFUNC("e1000_poll_for_msg");
184 if (!countdown || !mbx->ops.check_for_msg)
187 while (countdown && mbx->ops.check_for_msg(hw, mbx_id)) {
191 usec_delay(mbx->usec_delay);
194 /* if we failed, all future posted messages fail until reset */
198 return countdown ? E1000_SUCCESS : -E1000_ERR_MBX;
202 * e1000_poll_for_ack - Wait for message acknowledgement
203 * @hw: pointer to the HW structure
204 * @mbx_id: id of mailbox to write
206 * returns SUCCESS if it successfully received a message acknowledgement
208 static s32 e1000_poll_for_ack(struct e1000_hw *hw, u16 mbx_id)
210 struct e1000_mbx_info *mbx = &hw->mbx;
211 int countdown = mbx->timeout;
213 DEBUGFUNC("e1000_poll_for_ack");
215 if (!countdown || !mbx->ops.check_for_ack)
218 while (countdown && mbx->ops.check_for_ack(hw, mbx_id)) {
222 usec_delay(mbx->usec_delay);
225 /* if we failed, all future posted messages fail until reset */
229 return countdown ? E1000_SUCCESS : -E1000_ERR_MBX;
233 * e1000_read_posted_mbx - Wait for message notification and receive message
234 * @hw: pointer to the HW structure
235 * @msg: The message buffer
236 * @size: Length of buffer
237 * @mbx_id: id of mailbox to write
239 * returns SUCCESS if it successfully received a message notification and
240 * copied it into the receive buffer.
242 s32 e1000_read_posted_mbx(struct e1000_hw *hw, u32 *msg, u16 size, u16 mbx_id)
244 struct e1000_mbx_info *mbx = &hw->mbx;
245 s32 ret_val = -E1000_ERR_MBX;
247 DEBUGFUNC("e1000_read_posted_mbx");
252 ret_val = e1000_poll_for_msg(hw, mbx_id);
254 /* if ack received read message, otherwise we timed out */
256 ret_val = mbx->ops.read(hw, msg, size, mbx_id);
262 * e1000_write_posted_mbx - Write a message to the mailbox, wait for ack
263 * @hw: pointer to the HW structure
264 * @msg: The message buffer
265 * @size: Length of buffer
266 * @mbx_id: id of mailbox to write
268 * returns SUCCESS if it successfully copied message into the buffer and
269 * received an ack to that message within delay * timeout period
271 s32 e1000_write_posted_mbx(struct e1000_hw *hw, u32 *msg, u16 size, u16 mbx_id)
273 struct e1000_mbx_info *mbx = &hw->mbx;
274 s32 ret_val = -E1000_ERR_MBX;
276 DEBUGFUNC("e1000_write_posted_mbx");
278 /* exit if either we can't write or there isn't a defined timeout */
279 if (!mbx->ops.write || !mbx->timeout)
283 ret_val = mbx->ops.write(hw, msg, size, mbx_id);
285 /* if msg sent wait until we receive an ack */
287 ret_val = e1000_poll_for_ack(hw, mbx_id);
293 * e1000_init_mbx_ops_generic - Initialize mbx function pointers
294 * @hw: pointer to the HW structure
296 * Sets the function pointers to no-op functions
298 void e1000_init_mbx_ops_generic(struct e1000_hw *hw)
300 struct e1000_mbx_info *mbx = &hw->mbx;
301 mbx->ops.init_params = e1000_null_ops_generic;
302 mbx->ops.read = e1000_null_mbx_transact;
303 mbx->ops.write = e1000_null_mbx_transact;
304 mbx->ops.check_for_msg = e1000_null_mbx_check_for_flag;
305 mbx->ops.check_for_ack = e1000_null_mbx_check_for_flag;
306 mbx->ops.check_for_rst = e1000_null_mbx_check_for_flag;
307 mbx->ops.read_posted = e1000_read_posted_mbx;
308 mbx->ops.write_posted = e1000_write_posted_mbx;
312 * e1000_read_v2p_mailbox - read v2p mailbox
313 * @hw: pointer to the HW structure
315 * This function is used to read the v2p mailbox without losing the read to
318 static u32 e1000_read_v2p_mailbox(struct e1000_hw *hw)
320 u32 v2p_mailbox = E1000_READ_REG(hw, E1000_V2PMAILBOX(0));
322 v2p_mailbox |= hw->dev_spec.vf.v2p_mailbox;
323 hw->dev_spec.vf.v2p_mailbox |= v2p_mailbox & E1000_V2PMAILBOX_R2C_BITS;
329 * e1000_check_for_bit_vf - Determine if a status bit was set
330 * @hw: pointer to the HW structure
331 * @mask: bitmask for bits to be tested and cleared
333 * This function is used to check for the read to clear bits within
336 static s32 e1000_check_for_bit_vf(struct e1000_hw *hw, u32 mask)
338 u32 v2p_mailbox = e1000_read_v2p_mailbox(hw);
339 s32 ret_val = -E1000_ERR_MBX;
341 if (v2p_mailbox & mask)
342 ret_val = E1000_SUCCESS;
344 hw->dev_spec.vf.v2p_mailbox &= ~mask;
350 * e1000_check_for_msg_vf - checks to see if the PF has sent mail
351 * @hw: pointer to the HW structure
352 * @mbx_id: id of mailbox to check
354 * returns SUCCESS if the PF has set the Status bit or else ERR_MBX
356 static s32 e1000_check_for_msg_vf(struct e1000_hw *hw, u16 mbx_id)
358 s32 ret_val = -E1000_ERR_MBX;
360 DEBUGFUNC("e1000_check_for_msg_vf");
362 if (!e1000_check_for_bit_vf(hw, E1000_V2PMAILBOX_PFSTS)) {
363 ret_val = E1000_SUCCESS;
364 hw->mbx.stats.reqs++;
371 * e1000_check_for_ack_vf - checks to see if the PF has ACK'd
372 * @hw: pointer to the HW structure
373 * @mbx_id: id of mailbox to check
375 * returns SUCCESS if the PF has set the ACK bit or else ERR_MBX
377 static s32 e1000_check_for_ack_vf(struct e1000_hw *hw, u16 mbx_id)
379 s32 ret_val = -E1000_ERR_MBX;
381 DEBUGFUNC("e1000_check_for_ack_vf");
383 if (!e1000_check_for_bit_vf(hw, E1000_V2PMAILBOX_PFACK)) {
384 ret_val = E1000_SUCCESS;
385 hw->mbx.stats.acks++;
392 * e1000_check_for_rst_vf - checks to see if the PF has reset
393 * @hw: pointer to the HW structure
394 * @mbx_id: id of mailbox to check
396 * returns TRUE if the PF has set the reset done bit or else FALSE
398 static s32 e1000_check_for_rst_vf(struct e1000_hw *hw, u16 mbx_id)
400 s32 ret_val = -E1000_ERR_MBX;
402 DEBUGFUNC("e1000_check_for_rst_vf");
404 if (!e1000_check_for_bit_vf(hw, (E1000_V2PMAILBOX_RSTD |
405 E1000_V2PMAILBOX_RSTI))) {
406 ret_val = E1000_SUCCESS;
407 hw->mbx.stats.rsts++;
414 * e1000_obtain_mbx_lock_vf - obtain mailbox lock
415 * @hw: pointer to the HW structure
417 * return SUCCESS if we obtained the mailbox lock
419 static s32 e1000_obtain_mbx_lock_vf(struct e1000_hw *hw)
421 s32 ret_val = -E1000_ERR_MBX;
423 DEBUGFUNC("e1000_obtain_mbx_lock_vf");
425 /* Take ownership of the buffer */
426 E1000_WRITE_REG(hw, E1000_V2PMAILBOX(0), E1000_V2PMAILBOX_VFU);
428 /* reserve mailbox for vf use */
429 if (e1000_read_v2p_mailbox(hw) & E1000_V2PMAILBOX_VFU)
430 ret_val = E1000_SUCCESS;
436 * e1000_write_mbx_vf - Write a message to the mailbox
437 * @hw: pointer to the HW structure
438 * @msg: The message buffer
439 * @size: Length of buffer
440 * @mbx_id: id of mailbox to write
442 * returns SUCCESS if it successfully copied message into the buffer
444 static s32 e1000_write_mbx_vf(struct e1000_hw *hw, u32 *msg, u16 size,
451 DEBUGFUNC("e1000_write_mbx_vf");
453 /* lock the mailbox to prevent pf/vf race condition */
454 ret_val = e1000_obtain_mbx_lock_vf(hw);
458 /* flush msg and acks as we are overwriting the message buffer */
459 e1000_check_for_msg_vf(hw, 0);
460 e1000_check_for_ack_vf(hw, 0);
462 /* copy the caller specified message to the mailbox memory buffer */
463 for (i = 0; i < size; i++)
464 E1000_WRITE_REG_ARRAY(hw, E1000_VMBMEM(0), i, msg[i]);
467 hw->mbx.stats.msgs_tx++;
469 /* Drop VFU and interrupt the PF to tell it a message has been sent */
470 E1000_WRITE_REG(hw, E1000_V2PMAILBOX(0), E1000_V2PMAILBOX_REQ);
477 * e1000_read_mbx_vf - Reads a message from the inbox intended for vf
478 * @hw: pointer to the HW structure
479 * @msg: The message buffer
480 * @size: Length of buffer
481 * @mbx_id: id of mailbox to read
483 * returns SUCCESS if it successfuly read message from buffer
485 static s32 e1000_read_mbx_vf(struct e1000_hw *hw, u32 *msg, u16 size,
488 s32 ret_val = E1000_SUCCESS;
491 DEBUGFUNC("e1000_read_mbx_vf");
493 /* lock the mailbox to prevent pf/vf race condition */
494 ret_val = e1000_obtain_mbx_lock_vf(hw);
498 /* copy the message from the mailbox memory buffer */
499 for (i = 0; i < size; i++)
500 msg[i] = E1000_READ_REG_ARRAY(hw, E1000_VMBMEM(0), i);
502 /* Acknowledge receipt and release mailbox, then we're done */
503 E1000_WRITE_REG(hw, E1000_V2PMAILBOX(0), E1000_V2PMAILBOX_ACK);
506 hw->mbx.stats.msgs_rx++;
513 * e1000_init_mbx_params_vf - set initial values for vf mailbox
514 * @hw: pointer to the HW structure
516 * Initializes the hw->mbx struct to correct values for vf mailbox
518 s32 e1000_init_mbx_params_vf(struct e1000_hw *hw)
520 struct e1000_mbx_info *mbx = &hw->mbx;
522 /* start mailbox as timed out and let the reset_hw call set the timeout
523 * value to begin communications */
525 mbx->usec_delay = E1000_VF_MBX_INIT_DELAY;
527 mbx->size = E1000_VFMAILBOX_SIZE;
529 mbx->ops.read = e1000_read_mbx_vf;
530 mbx->ops.write = e1000_write_mbx_vf;
531 mbx->ops.read_posted = e1000_read_posted_mbx;
532 mbx->ops.write_posted = e1000_write_posted_mbx;
533 mbx->ops.check_for_msg = e1000_check_for_msg_vf;
534 mbx->ops.check_for_ack = e1000_check_for_ack_vf;
535 mbx->ops.check_for_rst = e1000_check_for_rst_vf;
537 mbx->stats.msgs_tx = 0;
538 mbx->stats.msgs_rx = 0;
543 return E1000_SUCCESS;
546 static s32 e1000_check_for_bit_pf(struct e1000_hw *hw, u32 mask)
548 u32 mbvficr = E1000_READ_REG(hw, E1000_MBVFICR);
549 s32 ret_val = -E1000_ERR_MBX;
551 if (mbvficr & mask) {
552 ret_val = E1000_SUCCESS;
553 E1000_WRITE_REG(hw, E1000_MBVFICR, mask);
560 * e1000_check_for_msg_pf - checks to see if the VF has sent mail
561 * @hw: pointer to the HW structure
562 * @vf_number: the VF index
564 * returns SUCCESS if the VF has set the Status bit or else ERR_MBX
566 static s32 e1000_check_for_msg_pf(struct e1000_hw *hw, u16 vf_number)
568 s32 ret_val = -E1000_ERR_MBX;
570 DEBUGFUNC("e1000_check_for_msg_pf");
572 if (!e1000_check_for_bit_pf(hw, E1000_MBVFICR_VFREQ_VF1 << vf_number)) {
573 ret_val = E1000_SUCCESS;
574 hw->mbx.stats.reqs++;
581 * e1000_check_for_ack_pf - checks to see if the VF has ACKed
582 * @hw: pointer to the HW structure
583 * @vf_number: the VF index
585 * returns SUCCESS if the VF has set the Status bit or else ERR_MBX
587 static s32 e1000_check_for_ack_pf(struct e1000_hw *hw, u16 vf_number)
589 s32 ret_val = -E1000_ERR_MBX;
591 DEBUGFUNC("e1000_check_for_ack_pf");
593 if (!e1000_check_for_bit_pf(hw, E1000_MBVFICR_VFACK_VF1 << vf_number)) {
594 ret_val = E1000_SUCCESS;
595 hw->mbx.stats.acks++;
602 * e1000_check_for_rst_pf - checks to see if the VF has reset
603 * @hw: pointer to the HW structure
604 * @vf_number: the VF index
606 * returns SUCCESS if the VF has set the Status bit or else ERR_MBX
608 static s32 e1000_check_for_rst_pf(struct e1000_hw *hw, u16 vf_number)
610 u32 vflre = E1000_READ_REG(hw, E1000_VFLRE);
611 s32 ret_val = -E1000_ERR_MBX;
613 DEBUGFUNC("e1000_check_for_rst_pf");
615 if (vflre & (1 << vf_number)) {
616 ret_val = E1000_SUCCESS;
617 E1000_WRITE_REG(hw, E1000_VFLRE, (1 << vf_number));
618 hw->mbx.stats.rsts++;
625 * e1000_obtain_mbx_lock_pf - obtain mailbox lock
626 * @hw: pointer to the HW structure
627 * @vf_number: the VF index
629 * return SUCCESS if we obtained the mailbox lock
631 static s32 e1000_obtain_mbx_lock_pf(struct e1000_hw *hw, u16 vf_number)
633 s32 ret_val = -E1000_ERR_MBX;
636 DEBUGFUNC("e1000_obtain_mbx_lock_pf");
638 /* Take ownership of the buffer */
639 E1000_WRITE_REG(hw, E1000_P2VMAILBOX(vf_number), E1000_P2VMAILBOX_PFU);
641 /* reserve mailbox for vf use */
642 p2v_mailbox = E1000_READ_REG(hw, E1000_P2VMAILBOX(vf_number));
643 if (p2v_mailbox & E1000_P2VMAILBOX_PFU)
644 ret_val = E1000_SUCCESS;
650 * e1000_write_mbx_pf - Places a message in the mailbox
651 * @hw: pointer to the HW structure
652 * @msg: The message buffer
653 * @size: Length of buffer
654 * @vf_number: the VF index
656 * returns SUCCESS if it successfully copied message into the buffer
658 static s32 e1000_write_mbx_pf(struct e1000_hw *hw, u32 *msg, u16 size,
664 DEBUGFUNC("e1000_write_mbx_pf");
666 /* lock the mailbox to prevent pf/vf race condition */
667 ret_val = e1000_obtain_mbx_lock_pf(hw, vf_number);
671 /* flush msg and acks as we are overwriting the message buffer */
672 e1000_check_for_msg_pf(hw, vf_number);
673 e1000_check_for_ack_pf(hw, vf_number);
675 /* copy the caller specified message to the mailbox memory buffer */
676 for (i = 0; i < size; i++)
677 E1000_WRITE_REG_ARRAY(hw, E1000_VMBMEM(vf_number), i, msg[i]);
679 /* Interrupt VF to tell it a message has been sent and release buffer*/
680 E1000_WRITE_REG(hw, E1000_P2VMAILBOX(vf_number), E1000_P2VMAILBOX_STS);
683 hw->mbx.stats.msgs_tx++;
691 * e1000_read_mbx_pf - Read a message from the mailbox
692 * @hw: pointer to the HW structure
693 * @msg: The message buffer
694 * @size: Length of buffer
695 * @vf_number: the VF index
697 * This function copies a message from the mailbox buffer to the caller's
698 * memory buffer. The presumption is that the caller knows that there was
699 * a message due to a VF request so no polling for message is needed.
701 static s32 e1000_read_mbx_pf(struct e1000_hw *hw, u32 *msg, u16 size,
707 DEBUGFUNC("e1000_read_mbx_pf");
709 /* lock the mailbox to prevent pf/vf race condition */
710 ret_val = e1000_obtain_mbx_lock_pf(hw, vf_number);
714 /* copy the message to the mailbox memory buffer */
715 for (i = 0; i < size; i++)
716 msg[i] = E1000_READ_REG_ARRAY(hw, E1000_VMBMEM(vf_number), i);
718 /* Acknowledge the message and release buffer */
719 E1000_WRITE_REG(hw, E1000_P2VMAILBOX(vf_number), E1000_P2VMAILBOX_ACK);
722 hw->mbx.stats.msgs_rx++;
729 * e1000_init_mbx_params_pf - set initial values for pf mailbox
730 * @hw: pointer to the HW structure
732 * Initializes the hw->mbx struct to correct values for pf mailbox
734 s32 e1000_init_mbx_params_pf(struct e1000_hw *hw)
736 struct e1000_mbx_info *mbx = &hw->mbx;
738 switch (hw->mac.type) {
744 mbx->size = E1000_VFMAILBOX_SIZE;
746 mbx->ops.read = e1000_read_mbx_pf;
747 mbx->ops.write = e1000_write_mbx_pf;
748 mbx->ops.read_posted = e1000_read_posted_mbx;
749 mbx->ops.write_posted = e1000_write_posted_mbx;
750 mbx->ops.check_for_msg = e1000_check_for_msg_pf;
751 mbx->ops.check_for_ack = e1000_check_for_ack_pf;
752 mbx->ops.check_for_rst = e1000_check_for_rst_pf;
754 mbx->stats.msgs_tx = 0;
755 mbx->stats.msgs_rx = 0;
760 return E1000_SUCCESS;