1 /******************************************************************************
3 Copyright (c) 2001-2014, 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 ******************************************************************************/
35 #include "e1000_mbx.h"
38 * e1000_null_mbx_check_for_flag - No-op function, return 0
39 * @hw: pointer to the HW structure
41 static s32 e1000_null_mbx_check_for_flag(struct e1000_hw E1000_UNUSEDARG *hw,
42 u16 E1000_UNUSEDARG mbx_id)
44 DEBUGFUNC("e1000_null_mbx_check_flag");
50 * e1000_null_mbx_transact - No-op function, return 0
51 * @hw: pointer to the HW structure
53 static s32 e1000_null_mbx_transact(struct e1000_hw E1000_UNUSEDARG *hw,
54 u32 E1000_UNUSEDARG *msg,
55 u16 E1000_UNUSEDARG size,
56 u16 E1000_UNUSEDARG mbx_id)
58 DEBUGFUNC("e1000_null_mbx_rw_msg");
64 * e1000_read_mbx - Reads a message from the mailbox
65 * @hw: pointer to the HW structure
66 * @msg: The message buffer
67 * @size: Length of buffer
68 * @mbx_id: id of mailbox to read
70 * returns SUCCESS if it successfuly read message from buffer
72 s32 e1000_read_mbx(struct e1000_hw *hw, u32 *msg, u16 size, u16 mbx_id)
74 struct e1000_mbx_info *mbx = &hw->mbx;
75 s32 ret_val = -E1000_ERR_MBX;
77 DEBUGFUNC("e1000_read_mbx");
79 /* limit read to size of mailbox */
84 ret_val = mbx->ops.read(hw, msg, size, mbx_id);
90 * e1000_write_mbx - Write a message to the mailbox
91 * @hw: pointer to the HW structure
92 * @msg: The message buffer
93 * @size: Length of buffer
94 * @mbx_id: id of mailbox to write
96 * returns SUCCESS if it successfully copied message into the buffer
98 s32 e1000_write_mbx(struct e1000_hw *hw, u32 *msg, u16 size, u16 mbx_id)
100 struct e1000_mbx_info *mbx = &hw->mbx;
101 s32 ret_val = E1000_SUCCESS;
103 DEBUGFUNC("e1000_write_mbx");
105 if (size > mbx->size)
106 ret_val = -E1000_ERR_MBX;
108 else if (mbx->ops.write)
109 ret_val = mbx->ops.write(hw, msg, size, mbx_id);
115 * e1000_check_for_msg - checks to see if someone sent us mail
116 * @hw: pointer to the HW structure
117 * @mbx_id: id of mailbox to check
119 * returns SUCCESS if the Status bit was found or else ERR_MBX
121 s32 e1000_check_for_msg(struct e1000_hw *hw, u16 mbx_id)
123 struct e1000_mbx_info *mbx = &hw->mbx;
124 s32 ret_val = -E1000_ERR_MBX;
126 DEBUGFUNC("e1000_check_for_msg");
128 if (mbx->ops.check_for_msg)
129 ret_val = mbx->ops.check_for_msg(hw, mbx_id);
135 * e1000_check_for_ack - checks to see if someone sent us ACK
136 * @hw: pointer to the HW structure
137 * @mbx_id: id of mailbox to check
139 * returns SUCCESS if the Status bit was found or else ERR_MBX
141 s32 e1000_check_for_ack(struct e1000_hw *hw, u16 mbx_id)
143 struct e1000_mbx_info *mbx = &hw->mbx;
144 s32 ret_val = -E1000_ERR_MBX;
146 DEBUGFUNC("e1000_check_for_ack");
148 if (mbx->ops.check_for_ack)
149 ret_val = mbx->ops.check_for_ack(hw, mbx_id);
155 * e1000_check_for_rst - checks to see if other side has reset
156 * @hw: pointer to the HW structure
157 * @mbx_id: id of mailbox to check
159 * returns SUCCESS if the Status bit was found or else ERR_MBX
161 s32 e1000_check_for_rst(struct e1000_hw *hw, u16 mbx_id)
163 struct e1000_mbx_info *mbx = &hw->mbx;
164 s32 ret_val = -E1000_ERR_MBX;
166 DEBUGFUNC("e1000_check_for_rst");
168 if (mbx->ops.check_for_rst)
169 ret_val = mbx->ops.check_for_rst(hw, mbx_id);
175 * e1000_poll_for_msg - Wait for message notification
176 * @hw: pointer to the HW structure
177 * @mbx_id: id of mailbox to write
179 * returns SUCCESS if it successfully received a message notification
181 static s32 e1000_poll_for_msg(struct e1000_hw *hw, u16 mbx_id)
183 struct e1000_mbx_info *mbx = &hw->mbx;
184 int countdown = mbx->timeout;
186 DEBUGFUNC("e1000_poll_for_msg");
188 if (!countdown || !mbx->ops.check_for_msg)
191 while (countdown && mbx->ops.check_for_msg(hw, mbx_id)) {
195 usec_delay(mbx->usec_delay);
198 /* if we failed, all future posted messages fail until reset */
202 return countdown ? E1000_SUCCESS : -E1000_ERR_MBX;
206 * e1000_poll_for_ack - Wait for message acknowledgement
207 * @hw: pointer to the HW structure
208 * @mbx_id: id of mailbox to write
210 * returns SUCCESS if it successfully received a message acknowledgement
212 static s32 e1000_poll_for_ack(struct e1000_hw *hw, u16 mbx_id)
214 struct e1000_mbx_info *mbx = &hw->mbx;
215 int countdown = mbx->timeout;
217 DEBUGFUNC("e1000_poll_for_ack");
219 if (!countdown || !mbx->ops.check_for_ack)
222 while (countdown && mbx->ops.check_for_ack(hw, mbx_id)) {
226 usec_delay(mbx->usec_delay);
229 /* if we failed, all future posted messages fail until reset */
233 return countdown ? E1000_SUCCESS : -E1000_ERR_MBX;
237 * e1000_read_posted_mbx - Wait for message notification and receive message
238 * @hw: pointer to the HW structure
239 * @msg: The message buffer
240 * @size: Length of buffer
241 * @mbx_id: id of mailbox to write
243 * returns SUCCESS if it successfully received a message notification and
244 * copied it into the receive buffer.
246 s32 e1000_read_posted_mbx(struct e1000_hw *hw, u32 *msg, u16 size, u16 mbx_id)
248 struct e1000_mbx_info *mbx = &hw->mbx;
249 s32 ret_val = -E1000_ERR_MBX;
251 DEBUGFUNC("e1000_read_posted_mbx");
256 ret_val = e1000_poll_for_msg(hw, mbx_id);
258 /* if ack received read message, otherwise we timed out */
260 ret_val = mbx->ops.read(hw, msg, size, mbx_id);
266 * e1000_write_posted_mbx - Write a message to the mailbox, wait for ack
267 * @hw: pointer to the HW structure
268 * @msg: The message buffer
269 * @size: Length of buffer
270 * @mbx_id: id of mailbox to write
272 * returns SUCCESS if it successfully copied message into the buffer and
273 * received an ack to that message within delay * timeout period
275 s32 e1000_write_posted_mbx(struct e1000_hw *hw, u32 *msg, u16 size, u16 mbx_id)
277 struct e1000_mbx_info *mbx = &hw->mbx;
278 s32 ret_val = -E1000_ERR_MBX;
280 DEBUGFUNC("e1000_write_posted_mbx");
282 /* exit if either we can't write or there isn't a defined timeout */
283 if (!mbx->ops.write || !mbx->timeout)
287 ret_val = mbx->ops.write(hw, msg, size, mbx_id);
289 /* if msg sent wait until we receive an ack */
291 ret_val = e1000_poll_for_ack(hw, mbx_id);
297 * e1000_init_mbx_ops_generic - Initialize mbx function pointers
298 * @hw: pointer to the HW structure
300 * Sets the function pointers to no-op functions
302 void e1000_init_mbx_ops_generic(struct e1000_hw *hw)
304 struct e1000_mbx_info *mbx = &hw->mbx;
305 mbx->ops.init_params = e1000_null_ops_generic;
306 mbx->ops.read = e1000_null_mbx_transact;
307 mbx->ops.write = e1000_null_mbx_transact;
308 mbx->ops.check_for_msg = e1000_null_mbx_check_for_flag;
309 mbx->ops.check_for_ack = e1000_null_mbx_check_for_flag;
310 mbx->ops.check_for_rst = e1000_null_mbx_check_for_flag;
311 mbx->ops.read_posted = e1000_read_posted_mbx;
312 mbx->ops.write_posted = e1000_write_posted_mbx;
316 * e1000_read_v2p_mailbox - read v2p mailbox
317 * @hw: pointer to the HW structure
319 * This function is used to read the v2p mailbox without losing the read to
322 static u32 e1000_read_v2p_mailbox(struct e1000_hw *hw)
324 u32 v2p_mailbox = E1000_READ_REG(hw, E1000_V2PMAILBOX(0));
326 v2p_mailbox |= hw->dev_spec.vf.v2p_mailbox;
327 hw->dev_spec.vf.v2p_mailbox |= v2p_mailbox & E1000_V2PMAILBOX_R2C_BITS;
333 * e1000_check_for_bit_vf - Determine if a status bit was set
334 * @hw: pointer to the HW structure
335 * @mask: bitmask for bits to be tested and cleared
337 * This function is used to check for the read to clear bits within
340 static s32 e1000_check_for_bit_vf(struct e1000_hw *hw, u32 mask)
342 u32 v2p_mailbox = e1000_read_v2p_mailbox(hw);
343 s32 ret_val = -E1000_ERR_MBX;
345 if (v2p_mailbox & mask)
346 ret_val = E1000_SUCCESS;
348 hw->dev_spec.vf.v2p_mailbox &= ~mask;
354 * e1000_check_for_msg_vf - checks to see if the PF has sent mail
355 * @hw: pointer to the HW structure
356 * @mbx_id: id of mailbox to check
358 * returns SUCCESS if the PF has set the Status bit or else ERR_MBX
360 static s32 e1000_check_for_msg_vf(struct e1000_hw *hw,
361 u16 E1000_UNUSEDARG mbx_id)
363 s32 ret_val = -E1000_ERR_MBX;
365 DEBUGFUNC("e1000_check_for_msg_vf");
367 if (!e1000_check_for_bit_vf(hw, E1000_V2PMAILBOX_PFSTS)) {
368 ret_val = E1000_SUCCESS;
369 hw->mbx.stats.reqs++;
376 * e1000_check_for_ack_vf - checks to see if the PF has ACK'd
377 * @hw: pointer to the HW structure
378 * @mbx_id: id of mailbox to check
380 * returns SUCCESS if the PF has set the ACK bit or else ERR_MBX
382 static s32 e1000_check_for_ack_vf(struct e1000_hw *hw,
383 u16 E1000_UNUSEDARG mbx_id)
385 s32 ret_val = -E1000_ERR_MBX;
387 DEBUGFUNC("e1000_check_for_ack_vf");
389 if (!e1000_check_for_bit_vf(hw, E1000_V2PMAILBOX_PFACK)) {
390 ret_val = E1000_SUCCESS;
391 hw->mbx.stats.acks++;
398 * e1000_check_for_rst_vf - checks to see if the PF has reset
399 * @hw: pointer to the HW structure
400 * @mbx_id: id of mailbox to check
402 * returns TRUE if the PF has set the reset done bit or else FALSE
404 static s32 e1000_check_for_rst_vf(struct e1000_hw *hw,
405 u16 E1000_UNUSEDARG mbx_id)
407 s32 ret_val = -E1000_ERR_MBX;
409 DEBUGFUNC("e1000_check_for_rst_vf");
411 if (!e1000_check_for_bit_vf(hw, (E1000_V2PMAILBOX_RSTD |
412 E1000_V2PMAILBOX_RSTI))) {
413 ret_val = E1000_SUCCESS;
414 hw->mbx.stats.rsts++;
421 * e1000_obtain_mbx_lock_vf - obtain mailbox lock
422 * @hw: pointer to the HW structure
424 * return SUCCESS if we obtained the mailbox lock
426 static s32 e1000_obtain_mbx_lock_vf(struct e1000_hw *hw)
428 s32 ret_val = -E1000_ERR_MBX;
430 DEBUGFUNC("e1000_obtain_mbx_lock_vf");
432 /* Take ownership of the buffer */
433 E1000_WRITE_REG(hw, E1000_V2PMAILBOX(0), E1000_V2PMAILBOX_VFU);
435 /* reserve mailbox for vf use */
436 if (e1000_read_v2p_mailbox(hw) & E1000_V2PMAILBOX_VFU)
437 ret_val = E1000_SUCCESS;
443 * e1000_write_mbx_vf - Write a message to the mailbox
444 * @hw: pointer to the HW structure
445 * @msg: The message buffer
446 * @size: Length of buffer
447 * @mbx_id: id of mailbox to write
449 * returns SUCCESS if it successfully copied message into the buffer
451 static s32 e1000_write_mbx_vf(struct e1000_hw *hw, u32 *msg, u16 size,
452 u16 E1000_UNUSEDARG mbx_id)
458 DEBUGFUNC("e1000_write_mbx_vf");
460 /* lock the mailbox to prevent pf/vf race condition */
461 ret_val = e1000_obtain_mbx_lock_vf(hw);
465 /* flush msg and acks as we are overwriting the message buffer */
466 e1000_check_for_msg_vf(hw, 0);
467 e1000_check_for_ack_vf(hw, 0);
469 /* copy the caller specified message to the mailbox memory buffer */
470 for (i = 0; i < size; i++)
471 E1000_WRITE_REG_ARRAY(hw, E1000_VMBMEM(0), i, msg[i]);
474 hw->mbx.stats.msgs_tx++;
476 /* Drop VFU and interrupt the PF to tell it a message has been sent */
477 E1000_WRITE_REG(hw, E1000_V2PMAILBOX(0), E1000_V2PMAILBOX_REQ);
484 * e1000_read_mbx_vf - Reads a message from the inbox intended for vf
485 * @hw: pointer to the HW structure
486 * @msg: The message buffer
487 * @size: Length of buffer
488 * @mbx_id: id of mailbox to read
490 * returns SUCCESS if it successfuly read message from buffer
492 static s32 e1000_read_mbx_vf(struct e1000_hw *hw, u32 *msg, u16 size,
493 u16 E1000_UNUSEDARG mbx_id)
495 s32 ret_val = E1000_SUCCESS;
498 DEBUGFUNC("e1000_read_mbx_vf");
500 /* lock the mailbox to prevent pf/vf race condition */
501 ret_val = e1000_obtain_mbx_lock_vf(hw);
505 /* copy the message from the mailbox memory buffer */
506 for (i = 0; i < size; i++)
507 msg[i] = E1000_READ_REG_ARRAY(hw, E1000_VMBMEM(0), i);
509 /* Acknowledge receipt and release mailbox, then we're done */
510 E1000_WRITE_REG(hw, E1000_V2PMAILBOX(0), E1000_V2PMAILBOX_ACK);
513 hw->mbx.stats.msgs_rx++;
520 * e1000_init_mbx_params_vf - set initial values for vf mailbox
521 * @hw: pointer to the HW structure
523 * Initializes the hw->mbx struct to correct values for vf mailbox
525 s32 e1000_init_mbx_params_vf(struct e1000_hw *hw)
527 struct e1000_mbx_info *mbx = &hw->mbx;
529 /* start mailbox as timed out and let the reset_hw call set the timeout
530 * value to begin communications */
532 mbx->usec_delay = E1000_VF_MBX_INIT_DELAY;
534 mbx->size = E1000_VFMAILBOX_SIZE;
536 mbx->ops.read = e1000_read_mbx_vf;
537 mbx->ops.write = e1000_write_mbx_vf;
538 mbx->ops.read_posted = e1000_read_posted_mbx;
539 mbx->ops.write_posted = e1000_write_posted_mbx;
540 mbx->ops.check_for_msg = e1000_check_for_msg_vf;
541 mbx->ops.check_for_ack = e1000_check_for_ack_vf;
542 mbx->ops.check_for_rst = e1000_check_for_rst_vf;
544 mbx->stats.msgs_tx = 0;
545 mbx->stats.msgs_rx = 0;
550 return E1000_SUCCESS;
553 static s32 e1000_check_for_bit_pf(struct e1000_hw *hw, u32 mask)
555 u32 mbvficr = E1000_READ_REG(hw, E1000_MBVFICR);
556 s32 ret_val = -E1000_ERR_MBX;
558 if (mbvficr & mask) {
559 ret_val = E1000_SUCCESS;
560 E1000_WRITE_REG(hw, E1000_MBVFICR, mask);
567 * e1000_check_for_msg_pf - checks to see if the VF has sent mail
568 * @hw: pointer to the HW structure
569 * @vf_number: the VF index
571 * returns SUCCESS if the VF has set the Status bit or else ERR_MBX
573 static s32 e1000_check_for_msg_pf(struct e1000_hw *hw, u16 vf_number)
575 s32 ret_val = -E1000_ERR_MBX;
577 DEBUGFUNC("e1000_check_for_msg_pf");
579 if (!e1000_check_for_bit_pf(hw, E1000_MBVFICR_VFREQ_VF1 << vf_number)) {
580 ret_val = E1000_SUCCESS;
581 hw->mbx.stats.reqs++;
588 * e1000_check_for_ack_pf - checks to see if the VF has ACKed
589 * @hw: pointer to the HW structure
590 * @vf_number: the VF index
592 * returns SUCCESS if the VF has set the Status bit or else ERR_MBX
594 static s32 e1000_check_for_ack_pf(struct e1000_hw *hw, u16 vf_number)
596 s32 ret_val = -E1000_ERR_MBX;
598 DEBUGFUNC("e1000_check_for_ack_pf");
600 if (!e1000_check_for_bit_pf(hw, E1000_MBVFICR_VFACK_VF1 << vf_number)) {
601 ret_val = E1000_SUCCESS;
602 hw->mbx.stats.acks++;
609 * e1000_check_for_rst_pf - checks to see if the VF has reset
610 * @hw: pointer to the HW structure
611 * @vf_number: the VF index
613 * returns SUCCESS if the VF has set the Status bit or else ERR_MBX
615 static s32 e1000_check_for_rst_pf(struct e1000_hw *hw, u16 vf_number)
617 u32 vflre = E1000_READ_REG(hw, E1000_VFLRE);
618 s32 ret_val = -E1000_ERR_MBX;
620 DEBUGFUNC("e1000_check_for_rst_pf");
622 if (vflre & (1 << vf_number)) {
623 ret_val = E1000_SUCCESS;
624 E1000_WRITE_REG(hw, E1000_VFLRE, (1 << vf_number));
625 hw->mbx.stats.rsts++;
632 * e1000_obtain_mbx_lock_pf - obtain mailbox lock
633 * @hw: pointer to the HW structure
634 * @vf_number: the VF index
636 * return SUCCESS if we obtained the mailbox lock
638 static s32 e1000_obtain_mbx_lock_pf(struct e1000_hw *hw, u16 vf_number)
640 s32 ret_val = -E1000_ERR_MBX;
643 DEBUGFUNC("e1000_obtain_mbx_lock_pf");
645 /* Take ownership of the buffer */
646 E1000_WRITE_REG(hw, E1000_P2VMAILBOX(vf_number), E1000_P2VMAILBOX_PFU);
648 /* reserve mailbox for vf use */
649 p2v_mailbox = E1000_READ_REG(hw, E1000_P2VMAILBOX(vf_number));
650 if (p2v_mailbox & E1000_P2VMAILBOX_PFU)
651 ret_val = E1000_SUCCESS;
657 * e1000_write_mbx_pf - Places a message in the mailbox
658 * @hw: pointer to the HW structure
659 * @msg: The message buffer
660 * @size: Length of buffer
661 * @vf_number: the VF index
663 * returns SUCCESS if it successfully copied message into the buffer
665 static s32 e1000_write_mbx_pf(struct e1000_hw *hw, u32 *msg, u16 size,
671 DEBUGFUNC("e1000_write_mbx_pf");
673 /* lock the mailbox to prevent pf/vf race condition */
674 ret_val = e1000_obtain_mbx_lock_pf(hw, vf_number);
678 /* flush msg and acks as we are overwriting the message buffer */
679 e1000_check_for_msg_pf(hw, vf_number);
680 e1000_check_for_ack_pf(hw, vf_number);
682 /* copy the caller specified message to the mailbox memory buffer */
683 for (i = 0; i < size; i++)
684 E1000_WRITE_REG_ARRAY(hw, E1000_VMBMEM(vf_number), i, msg[i]);
686 /* Interrupt VF to tell it a message has been sent and release buffer*/
687 E1000_WRITE_REG(hw, E1000_P2VMAILBOX(vf_number), E1000_P2VMAILBOX_STS);
690 hw->mbx.stats.msgs_tx++;
698 * e1000_read_mbx_pf - Read a message from the mailbox
699 * @hw: pointer to the HW structure
700 * @msg: The message buffer
701 * @size: Length of buffer
702 * @vf_number: the VF index
704 * This function copies a message from the mailbox buffer to the caller's
705 * memory buffer. The presumption is that the caller knows that there was
706 * a message due to a VF request so no polling for message is needed.
708 static s32 e1000_read_mbx_pf(struct e1000_hw *hw, u32 *msg, u16 size,
714 DEBUGFUNC("e1000_read_mbx_pf");
716 /* lock the mailbox to prevent pf/vf race condition */
717 ret_val = e1000_obtain_mbx_lock_pf(hw, vf_number);
721 /* copy the message to the mailbox memory buffer */
722 for (i = 0; i < size; i++)
723 msg[i] = E1000_READ_REG_ARRAY(hw, E1000_VMBMEM(vf_number), i);
725 /* Acknowledge the message and release buffer */
726 E1000_WRITE_REG(hw, E1000_P2VMAILBOX(vf_number), E1000_P2VMAILBOX_ACK);
729 hw->mbx.stats.msgs_rx++;
736 * e1000_init_mbx_params_pf - set initial values for pf mailbox
737 * @hw: pointer to the HW structure
739 * Initializes the hw->mbx struct to correct values for pf mailbox
741 s32 e1000_init_mbx_params_pf(struct e1000_hw *hw)
743 struct e1000_mbx_info *mbx = &hw->mbx;
745 switch (hw->mac.type) {
752 mbx->size = E1000_VFMAILBOX_SIZE;
754 mbx->ops.read = e1000_read_mbx_pf;
755 mbx->ops.write = e1000_write_mbx_pf;
756 mbx->ops.read_posted = e1000_read_posted_mbx;
757 mbx->ops.write_posted = e1000_write_posted_mbx;
758 mbx->ops.check_for_msg = e1000_check_for_msg_pf;
759 mbx->ops.check_for_ack = e1000_check_for_ack_pf;
760 mbx->ops.check_for_rst = e1000_check_for_rst_pf;
762 mbx->stats.msgs_tx = 0;
763 mbx->stats.msgs_rx = 0;
768 return E1000_SUCCESS;