1 /******************************************************************************
3 Copyright (c) 2001-2011, 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 *hw, u16 mbx_id)
43 DEBUGFUNC("e1000_null_mbx_check_flag");
49 * e1000_null_mbx_transact - No-op function, return 0
50 * @hw: pointer to the HW structure
52 static s32 e1000_null_mbx_transact(struct e1000_hw *hw, u32 *msg, u16 size,
55 DEBUGFUNC("e1000_null_mbx_rw_msg");
61 * e1000_read_mbx - Reads a message from the mailbox
62 * @hw: pointer to the HW structure
63 * @msg: The message buffer
64 * @size: Length of buffer
65 * @mbx_id: id of mailbox to read
67 * returns SUCCESS if it successfuly read message from buffer
69 s32 e1000_read_mbx(struct e1000_hw *hw, u32 *msg, u16 size, u16 mbx_id)
71 struct e1000_mbx_info *mbx = &hw->mbx;
72 s32 ret_val = -E1000_ERR_MBX;
74 DEBUGFUNC("e1000_read_mbx");
76 /* limit read to size of mailbox */
81 ret_val = mbx->ops.read(hw, msg, size, mbx_id);
87 * e1000_write_mbx - Write a message to the mailbox
88 * @hw: pointer to the HW structure
89 * @msg: The message buffer
90 * @size: Length of buffer
91 * @mbx_id: id of mailbox to write
93 * returns SUCCESS if it successfully copied message into the buffer
95 s32 e1000_write_mbx(struct e1000_hw *hw, u32 *msg, u16 size, u16 mbx_id)
97 struct e1000_mbx_info *mbx = &hw->mbx;
98 s32 ret_val = E1000_SUCCESS;
100 DEBUGFUNC("e1000_write_mbx");
102 if (size > mbx->size)
103 ret_val = -E1000_ERR_MBX;
105 else if (mbx->ops.write)
106 ret_val = mbx->ops.write(hw, msg, size, mbx_id);
112 * e1000_check_for_msg - checks to see if someone sent us mail
113 * @hw: pointer to the HW structure
114 * @mbx_id: id of mailbox to check
116 * returns SUCCESS if the Status bit was found or else ERR_MBX
118 s32 e1000_check_for_msg(struct e1000_hw *hw, u16 mbx_id)
120 struct e1000_mbx_info *mbx = &hw->mbx;
121 s32 ret_val = -E1000_ERR_MBX;
123 DEBUGFUNC("e1000_check_for_msg");
125 if (mbx->ops.check_for_msg)
126 ret_val = mbx->ops.check_for_msg(hw, mbx_id);
132 * e1000_check_for_ack - checks to see if someone sent us ACK
133 * @hw: pointer to the HW structure
134 * @mbx_id: id of mailbox to check
136 * returns SUCCESS if the Status bit was found or else ERR_MBX
138 s32 e1000_check_for_ack(struct e1000_hw *hw, u16 mbx_id)
140 struct e1000_mbx_info *mbx = &hw->mbx;
141 s32 ret_val = -E1000_ERR_MBX;
143 DEBUGFUNC("e1000_check_for_ack");
145 if (mbx->ops.check_for_ack)
146 ret_val = mbx->ops.check_for_ack(hw, mbx_id);
152 * e1000_check_for_rst - checks to see if other side has reset
153 * @hw: pointer to the HW structure
154 * @mbx_id: id of mailbox to check
156 * returns SUCCESS if the Status bit was found or else ERR_MBX
158 s32 e1000_check_for_rst(struct e1000_hw *hw, u16 mbx_id)
160 struct e1000_mbx_info *mbx = &hw->mbx;
161 s32 ret_val = -E1000_ERR_MBX;
163 DEBUGFUNC("e1000_check_for_rst");
165 if (mbx->ops.check_for_rst)
166 ret_val = mbx->ops.check_for_rst(hw, mbx_id);
172 * e1000_poll_for_msg - Wait for message notification
173 * @hw: pointer to the HW structure
174 * @mbx_id: id of mailbox to write
176 * returns SUCCESS if it successfully received a message notification
178 static s32 e1000_poll_for_msg(struct e1000_hw *hw, u16 mbx_id)
180 struct e1000_mbx_info *mbx = &hw->mbx;
181 int countdown = mbx->timeout;
183 DEBUGFUNC("e1000_poll_for_msg");
185 if (!countdown || !mbx->ops.check_for_msg)
188 while (countdown && mbx->ops.check_for_msg(hw, mbx_id)) {
192 usec_delay(mbx->usec_delay);
195 /* if we failed, all future posted messages fail until reset */
199 return countdown ? E1000_SUCCESS : -E1000_ERR_MBX;
203 * e1000_poll_for_ack - Wait for message acknowledgement
204 * @hw: pointer to the HW structure
205 * @mbx_id: id of mailbox to write
207 * returns SUCCESS if it successfully received a message acknowledgement
209 static s32 e1000_poll_for_ack(struct e1000_hw *hw, u16 mbx_id)
211 struct e1000_mbx_info *mbx = &hw->mbx;
212 int countdown = mbx->timeout;
214 DEBUGFUNC("e1000_poll_for_ack");
216 if (!countdown || !mbx->ops.check_for_ack)
219 while (countdown && mbx->ops.check_for_ack(hw, mbx_id)) {
223 usec_delay(mbx->usec_delay);
226 /* if we failed, all future posted messages fail until reset */
230 return countdown ? E1000_SUCCESS : -E1000_ERR_MBX;
234 * e1000_read_posted_mbx - Wait for message notification and receive message
235 * @hw: pointer to the HW structure
236 * @msg: The message buffer
237 * @size: Length of buffer
238 * @mbx_id: id of mailbox to write
240 * returns SUCCESS if it successfully received a message notification and
241 * copied it into the receive buffer.
243 s32 e1000_read_posted_mbx(struct e1000_hw *hw, u32 *msg, u16 size, u16 mbx_id)
245 struct e1000_mbx_info *mbx = &hw->mbx;
246 s32 ret_val = -E1000_ERR_MBX;
248 DEBUGFUNC("e1000_read_posted_mbx");
253 ret_val = e1000_poll_for_msg(hw, mbx_id);
255 /* if ack received read message, otherwise we timed out */
257 ret_val = mbx->ops.read(hw, msg, size, mbx_id);
263 * e1000_write_posted_mbx - Write a message to the mailbox, wait for ack
264 * @hw: pointer to the HW structure
265 * @msg: The message buffer
266 * @size: Length of buffer
267 * @mbx_id: id of mailbox to write
269 * returns SUCCESS if it successfully copied message into the buffer and
270 * received an ack to that message within delay * timeout period
272 s32 e1000_write_posted_mbx(struct e1000_hw *hw, u32 *msg, u16 size, u16 mbx_id)
274 struct e1000_mbx_info *mbx = &hw->mbx;
275 s32 ret_val = -E1000_ERR_MBX;
277 DEBUGFUNC("e1000_write_posted_mbx");
279 /* exit if either we can't write or there isn't a defined timeout */
280 if (!mbx->ops.write || !mbx->timeout)
284 ret_val = mbx->ops.write(hw, msg, size, mbx_id);
286 /* if msg sent wait until we receive an ack */
288 ret_val = e1000_poll_for_ack(hw, mbx_id);
294 * e1000_init_mbx_ops_generic - Initialize mbx function pointers
295 * @hw: pointer to the HW structure
297 * Sets the function pointers to no-op functions
299 void e1000_init_mbx_ops_generic(struct e1000_hw *hw)
301 struct e1000_mbx_info *mbx = &hw->mbx;
302 mbx->ops.init_params = e1000_null_ops_generic;
303 mbx->ops.read = e1000_null_mbx_transact;
304 mbx->ops.write = e1000_null_mbx_transact;
305 mbx->ops.check_for_msg = e1000_null_mbx_check_for_flag;
306 mbx->ops.check_for_ack = e1000_null_mbx_check_for_flag;
307 mbx->ops.check_for_rst = e1000_null_mbx_check_for_flag;
308 mbx->ops.read_posted = e1000_read_posted_mbx;
309 mbx->ops.write_posted = e1000_write_posted_mbx;
313 * e1000_read_v2p_mailbox - read v2p mailbox
314 * @hw: pointer to the HW structure
316 * This function is used to read the v2p mailbox without losing the read to
319 static u32 e1000_read_v2p_mailbox(struct e1000_hw *hw)
321 u32 v2p_mailbox = E1000_READ_REG(hw, E1000_V2PMAILBOX(0));
323 v2p_mailbox |= hw->dev_spec.vf.v2p_mailbox;
324 hw->dev_spec.vf.v2p_mailbox |= v2p_mailbox & E1000_V2PMAILBOX_R2C_BITS;
330 * e1000_check_for_bit_vf - Determine if a status bit was set
331 * @hw: pointer to the HW structure
332 * @mask: bitmask for bits to be tested and cleared
334 * This function is used to check for the read to clear bits within
337 static s32 e1000_check_for_bit_vf(struct e1000_hw *hw, u32 mask)
339 u32 v2p_mailbox = e1000_read_v2p_mailbox(hw);
340 s32 ret_val = -E1000_ERR_MBX;
342 if (v2p_mailbox & mask)
343 ret_val = E1000_SUCCESS;
345 hw->dev_spec.vf.v2p_mailbox &= ~mask;
351 * e1000_check_for_msg_vf - checks to see if the PF has sent mail
352 * @hw: pointer to the HW structure
353 * @mbx_id: id of mailbox to check
355 * returns SUCCESS if the PF has set the Status bit or else ERR_MBX
357 static s32 e1000_check_for_msg_vf(struct e1000_hw *hw, u16 mbx_id)
359 s32 ret_val = -E1000_ERR_MBX;
361 DEBUGFUNC("e1000_check_for_msg_vf");
363 if (!e1000_check_for_bit_vf(hw, E1000_V2PMAILBOX_PFSTS)) {
364 ret_val = E1000_SUCCESS;
365 hw->mbx.stats.reqs++;
372 * e1000_check_for_ack_vf - checks to see if the PF has ACK'd
373 * @hw: pointer to the HW structure
374 * @mbx_id: id of mailbox to check
376 * returns SUCCESS if the PF has set the ACK bit or else ERR_MBX
378 static s32 e1000_check_for_ack_vf(struct e1000_hw *hw, u16 mbx_id)
380 s32 ret_val = -E1000_ERR_MBX;
382 DEBUGFUNC("e1000_check_for_ack_vf");
384 if (!e1000_check_for_bit_vf(hw, E1000_V2PMAILBOX_PFACK)) {
385 ret_val = E1000_SUCCESS;
386 hw->mbx.stats.acks++;
393 * e1000_check_for_rst_vf - checks to see if the PF has reset
394 * @hw: pointer to the HW structure
395 * @mbx_id: id of mailbox to check
397 * returns TRUE if the PF has set the reset done bit or else FALSE
399 static s32 e1000_check_for_rst_vf(struct e1000_hw *hw, u16 mbx_id)
401 s32 ret_val = -E1000_ERR_MBX;
403 DEBUGFUNC("e1000_check_for_rst_vf");
405 if (!e1000_check_for_bit_vf(hw, (E1000_V2PMAILBOX_RSTD |
406 E1000_V2PMAILBOX_RSTI))) {
407 ret_val = E1000_SUCCESS;
408 hw->mbx.stats.rsts++;
415 * e1000_obtain_mbx_lock_vf - obtain mailbox lock
416 * @hw: pointer to the HW structure
418 * return SUCCESS if we obtained the mailbox lock
420 static s32 e1000_obtain_mbx_lock_vf(struct e1000_hw *hw)
422 s32 ret_val = -E1000_ERR_MBX;
424 DEBUGFUNC("e1000_obtain_mbx_lock_vf");
426 /* Take ownership of the buffer */
427 E1000_WRITE_REG(hw, E1000_V2PMAILBOX(0), E1000_V2PMAILBOX_VFU);
429 /* reserve mailbox for vf use */
430 if (e1000_read_v2p_mailbox(hw) & E1000_V2PMAILBOX_VFU)
431 ret_val = E1000_SUCCESS;
437 * e1000_write_mbx_vf - Write a message to the mailbox
438 * @hw: pointer to the HW structure
439 * @msg: The message buffer
440 * @size: Length of buffer
441 * @mbx_id: id of mailbox to write
443 * returns SUCCESS if it successfully copied message into the buffer
445 static s32 e1000_write_mbx_vf(struct e1000_hw *hw, u32 *msg, u16 size,
452 DEBUGFUNC("e1000_write_mbx_vf");
454 /* lock the mailbox to prevent pf/vf race condition */
455 ret_val = e1000_obtain_mbx_lock_vf(hw);
459 /* flush msg and acks as we are overwriting the message buffer */
460 e1000_check_for_msg_vf(hw, 0);
461 e1000_check_for_ack_vf(hw, 0);
463 /* copy the caller specified message to the mailbox memory buffer */
464 for (i = 0; i < size; i++)
465 E1000_WRITE_REG_ARRAY(hw, E1000_VMBMEM(0), i, msg[i]);
468 hw->mbx.stats.msgs_tx++;
470 /* Drop VFU and interrupt the PF to tell it a message has been sent */
471 E1000_WRITE_REG(hw, E1000_V2PMAILBOX(0), E1000_V2PMAILBOX_REQ);
478 * e1000_read_mbx_vf - Reads a message from the inbox intended for vf
479 * @hw: pointer to the HW structure
480 * @msg: The message buffer
481 * @size: Length of buffer
482 * @mbx_id: id of mailbox to read
484 * returns SUCCESS if it successfuly read message from buffer
486 static s32 e1000_read_mbx_vf(struct e1000_hw *hw, u32 *msg, u16 size,
489 s32 ret_val = E1000_SUCCESS;
492 DEBUGFUNC("e1000_read_mbx_vf");
494 /* lock the mailbox to prevent pf/vf race condition */
495 ret_val = e1000_obtain_mbx_lock_vf(hw);
499 /* copy the message from the mailbox memory buffer */
500 for (i = 0; i < size; i++)
501 msg[i] = E1000_READ_REG_ARRAY(hw, E1000_VMBMEM(0), i);
503 /* Acknowledge receipt and release mailbox, then we're done */
504 E1000_WRITE_REG(hw, E1000_V2PMAILBOX(0), E1000_V2PMAILBOX_ACK);
507 hw->mbx.stats.msgs_rx++;
514 * e1000_init_mbx_params_vf - set initial values for vf mailbox
515 * @hw: pointer to the HW structure
517 * Initializes the hw->mbx struct to correct values for vf mailbox
519 s32 e1000_init_mbx_params_vf(struct e1000_hw *hw)
521 struct e1000_mbx_info *mbx = &hw->mbx;
523 /* start mailbox as timed out and let the reset_hw call set the timeout
524 * value to begin communications */
526 mbx->usec_delay = E1000_VF_MBX_INIT_DELAY;
528 mbx->size = E1000_VFMAILBOX_SIZE;
530 mbx->ops.read = e1000_read_mbx_vf;
531 mbx->ops.write = e1000_write_mbx_vf;
532 mbx->ops.read_posted = e1000_read_posted_mbx;
533 mbx->ops.write_posted = e1000_write_posted_mbx;
534 mbx->ops.check_for_msg = e1000_check_for_msg_vf;
535 mbx->ops.check_for_ack = e1000_check_for_ack_vf;
536 mbx->ops.check_for_rst = e1000_check_for_rst_vf;
538 mbx->stats.msgs_tx = 0;
539 mbx->stats.msgs_rx = 0;
544 return E1000_SUCCESS;
547 static s32 e1000_check_for_bit_pf(struct e1000_hw *hw, u32 mask)
549 u32 mbvficr = E1000_READ_REG(hw, E1000_MBVFICR);
550 s32 ret_val = -E1000_ERR_MBX;
552 if (mbvficr & mask) {
553 ret_val = E1000_SUCCESS;
554 E1000_WRITE_REG(hw, E1000_MBVFICR, mask);
561 * e1000_check_for_msg_pf - checks to see if the VF has sent mail
562 * @hw: pointer to the HW structure
563 * @vf_number: the VF index
565 * returns SUCCESS if the VF has set the Status bit or else ERR_MBX
567 static s32 e1000_check_for_msg_pf(struct e1000_hw *hw, u16 vf_number)
569 s32 ret_val = -E1000_ERR_MBX;
571 DEBUGFUNC("e1000_check_for_msg_pf");
573 if (!e1000_check_for_bit_pf(hw, E1000_MBVFICR_VFREQ_VF1 << vf_number)) {
574 ret_val = E1000_SUCCESS;
575 hw->mbx.stats.reqs++;
582 * e1000_check_for_ack_pf - checks to see if the VF has ACKed
583 * @hw: pointer to the HW structure
584 * @vf_number: the VF index
586 * returns SUCCESS if the VF has set the Status bit or else ERR_MBX
588 static s32 e1000_check_for_ack_pf(struct e1000_hw *hw, u16 vf_number)
590 s32 ret_val = -E1000_ERR_MBX;
592 DEBUGFUNC("e1000_check_for_ack_pf");
594 if (!e1000_check_for_bit_pf(hw, E1000_MBVFICR_VFACK_VF1 << vf_number)) {
595 ret_val = E1000_SUCCESS;
596 hw->mbx.stats.acks++;
603 * e1000_check_for_rst_pf - checks to see if the VF has reset
604 * @hw: pointer to the HW structure
605 * @vf_number: the VF index
607 * returns SUCCESS if the VF has set the Status bit or else ERR_MBX
609 static s32 e1000_check_for_rst_pf(struct e1000_hw *hw, u16 vf_number)
611 u32 vflre = E1000_READ_REG(hw, E1000_VFLRE);
612 s32 ret_val = -E1000_ERR_MBX;
614 DEBUGFUNC("e1000_check_for_rst_pf");
616 if (vflre & (1 << vf_number)) {
617 ret_val = E1000_SUCCESS;
618 E1000_WRITE_REG(hw, E1000_VFLRE, (1 << vf_number));
619 hw->mbx.stats.rsts++;
626 * e1000_obtain_mbx_lock_pf - obtain mailbox lock
627 * @hw: pointer to the HW structure
628 * @vf_number: the VF index
630 * return SUCCESS if we obtained the mailbox lock
632 static s32 e1000_obtain_mbx_lock_pf(struct e1000_hw *hw, u16 vf_number)
634 s32 ret_val = -E1000_ERR_MBX;
637 DEBUGFUNC("e1000_obtain_mbx_lock_pf");
639 /* Take ownership of the buffer */
640 E1000_WRITE_REG(hw, E1000_P2VMAILBOX(vf_number), E1000_P2VMAILBOX_PFU);
642 /* reserve mailbox for vf use */
643 p2v_mailbox = E1000_READ_REG(hw, E1000_P2VMAILBOX(vf_number));
644 if (p2v_mailbox & E1000_P2VMAILBOX_PFU)
645 ret_val = E1000_SUCCESS;
651 * e1000_write_mbx_pf - Places a message in the mailbox
652 * @hw: pointer to the HW structure
653 * @msg: The message buffer
654 * @size: Length of buffer
655 * @vf_number: the VF index
657 * returns SUCCESS if it successfully copied message into the buffer
659 static s32 e1000_write_mbx_pf(struct e1000_hw *hw, u32 *msg, u16 size,
665 DEBUGFUNC("e1000_write_mbx_pf");
667 /* lock the mailbox to prevent pf/vf race condition */
668 ret_val = e1000_obtain_mbx_lock_pf(hw, vf_number);
672 /* flush msg and acks as we are overwriting the message buffer */
673 e1000_check_for_msg_pf(hw, vf_number);
674 e1000_check_for_ack_pf(hw, vf_number);
676 /* copy the caller specified message to the mailbox memory buffer */
677 for (i = 0; i < size; i++)
678 E1000_WRITE_REG_ARRAY(hw, E1000_VMBMEM(vf_number), i, msg[i]);
680 /* Interrupt VF to tell it a message has been sent and release buffer*/
681 E1000_WRITE_REG(hw, E1000_P2VMAILBOX(vf_number), E1000_P2VMAILBOX_STS);
684 hw->mbx.stats.msgs_tx++;
692 * e1000_read_mbx_pf - Read a message from the mailbox
693 * @hw: pointer to the HW structure
694 * @msg: The message buffer
695 * @size: Length of buffer
696 * @vf_number: the VF index
698 * This function copies a message from the mailbox buffer to the caller's
699 * memory buffer. The presumption is that the caller knows that there was
700 * a message due to a VF request so no polling for message is needed.
702 static s32 e1000_read_mbx_pf(struct e1000_hw *hw, u32 *msg, u16 size,
708 DEBUGFUNC("e1000_read_mbx_pf");
710 /* lock the mailbox to prevent pf/vf race condition */
711 ret_val = e1000_obtain_mbx_lock_pf(hw, vf_number);
715 /* copy the message to the mailbox memory buffer */
716 for (i = 0; i < size; i++)
717 msg[i] = E1000_READ_REG_ARRAY(hw, E1000_VMBMEM(vf_number), i);
719 /* Acknowledge the message and release buffer */
720 E1000_WRITE_REG(hw, E1000_P2VMAILBOX(vf_number), E1000_P2VMAILBOX_ACK);
723 hw->mbx.stats.msgs_rx++;
730 * e1000_init_mbx_params_pf - set initial values for pf mailbox
731 * @hw: pointer to the HW structure
733 * Initializes the hw->mbx struct to correct values for pf mailbox
735 s32 e1000_init_mbx_params_pf(struct e1000_hw *hw)
737 struct e1000_mbx_info *mbx = &hw->mbx;
739 switch (hw->mac.type) {
745 mbx->size = E1000_VFMAILBOX_SIZE;
747 mbx->ops.read = e1000_read_mbx_pf;
748 mbx->ops.write = e1000_write_mbx_pf;
749 mbx->ops.read_posted = e1000_read_posted_mbx;
750 mbx->ops.write_posted = e1000_write_posted_mbx;
751 mbx->ops.check_for_msg = e1000_check_for_msg_pf;
752 mbx->ops.check_for_ack = e1000_check_for_ack_pf;
753 mbx->ops.check_for_rst = e1000_check_for_rst_pf;
755 mbx->stats.msgs_tx = 0;
756 mbx->stats.msgs_rx = 0;
761 return E1000_SUCCESS;