fp@2685: /* Intel(R) Gigabit Ethernet Linux driver fp@2685: * Copyright(c) 2007-2014 Intel Corporation. fp@2685: * fp@2685: * This program is free software; you can redistribute it and/or modify it fp@2685: * under the terms and conditions of the GNU General Public License, fp@2685: * version 2, as published by the Free Software Foundation. fp@2685: * fp@2685: * This program is distributed in the hope it will be useful, but WITHOUT fp@2685: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or fp@2685: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for fp@2685: * more details. fp@2685: * fp@2685: * You should have received a copy of the GNU General Public License along with fp@2685: * this program; if not, see . fp@2685: * fp@2685: * The full GNU General Public License is included in this distribution in fp@2685: * the file called "COPYING". fp@2685: * fp@2685: * Contact Information: fp@2685: * e1000-devel Mailing List fp@2685: * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497 fp@2685: */ fp@2685: fp@2685: #include "e1000_mbx.h" fp@2685: fp@2685: /** fp@2685: * igb_read_mbx - Reads a message from the mailbox fp@2685: * @hw: pointer to the HW structure fp@2685: * @msg: The message buffer fp@2685: * @size: Length of buffer fp@2685: * @mbx_id: id of mailbox to read fp@2685: * fp@2685: * returns SUCCESS if it successfully read message from buffer fp@2685: **/ fp@2685: s32 igb_read_mbx(struct e1000_hw *hw, u32 *msg, u16 size, u16 mbx_id) fp@2685: { fp@2685: struct e1000_mbx_info *mbx = &hw->mbx; fp@2685: s32 ret_val = -E1000_ERR_MBX; fp@2685: fp@2685: /* limit read to size of mailbox */ fp@2685: if (size > mbx->size) fp@2685: size = mbx->size; fp@2685: fp@2685: if (mbx->ops.read) fp@2685: ret_val = mbx->ops.read(hw, msg, size, mbx_id); fp@2685: fp@2685: return ret_val; fp@2685: } fp@2685: fp@2685: /** fp@2685: * igb_write_mbx - Write a message to the mailbox fp@2685: * @hw: pointer to the HW structure fp@2685: * @msg: The message buffer fp@2685: * @size: Length of buffer fp@2685: * @mbx_id: id of mailbox to write fp@2685: * fp@2685: * returns SUCCESS if it successfully copied message into the buffer fp@2685: **/ fp@2685: s32 igb_write_mbx(struct e1000_hw *hw, u32 *msg, u16 size, u16 mbx_id) fp@2685: { fp@2685: struct e1000_mbx_info *mbx = &hw->mbx; fp@2685: s32 ret_val = 0; fp@2685: fp@2685: if (size > mbx->size) fp@2685: ret_val = -E1000_ERR_MBX; fp@2685: fp@2685: else if (mbx->ops.write) fp@2685: ret_val = mbx->ops.write(hw, msg, size, mbx_id); fp@2685: fp@2685: return ret_val; fp@2685: } fp@2685: fp@2685: /** fp@2685: * igb_check_for_msg - checks to see if someone sent us mail fp@2685: * @hw: pointer to the HW structure fp@2685: * @mbx_id: id of mailbox to check fp@2685: * fp@2685: * returns SUCCESS if the Status bit was found or else ERR_MBX fp@2685: **/ fp@2685: s32 igb_check_for_msg(struct e1000_hw *hw, u16 mbx_id) fp@2685: { fp@2685: struct e1000_mbx_info *mbx = &hw->mbx; fp@2685: s32 ret_val = -E1000_ERR_MBX; fp@2685: fp@2685: if (mbx->ops.check_for_msg) fp@2685: ret_val = mbx->ops.check_for_msg(hw, mbx_id); fp@2685: fp@2685: return ret_val; fp@2685: } fp@2685: fp@2685: /** fp@2685: * igb_check_for_ack - checks to see if someone sent us ACK fp@2685: * @hw: pointer to the HW structure fp@2685: * @mbx_id: id of mailbox to check fp@2685: * fp@2685: * returns SUCCESS if the Status bit was found or else ERR_MBX fp@2685: **/ fp@2685: s32 igb_check_for_ack(struct e1000_hw *hw, u16 mbx_id) fp@2685: { fp@2685: struct e1000_mbx_info *mbx = &hw->mbx; fp@2685: s32 ret_val = -E1000_ERR_MBX; fp@2685: fp@2685: if (mbx->ops.check_for_ack) fp@2685: ret_val = mbx->ops.check_for_ack(hw, mbx_id); fp@2685: fp@2685: return ret_val; fp@2685: } fp@2685: fp@2685: /** fp@2685: * igb_check_for_rst - checks to see if other side has reset fp@2685: * @hw: pointer to the HW structure fp@2685: * @mbx_id: id of mailbox to check fp@2685: * fp@2685: * returns SUCCESS if the Status bit was found or else ERR_MBX fp@2685: **/ fp@2685: s32 igb_check_for_rst(struct e1000_hw *hw, u16 mbx_id) fp@2685: { fp@2685: struct e1000_mbx_info *mbx = &hw->mbx; fp@2685: s32 ret_val = -E1000_ERR_MBX; fp@2685: fp@2685: if (mbx->ops.check_for_rst) fp@2685: ret_val = mbx->ops.check_for_rst(hw, mbx_id); fp@2685: fp@2685: return ret_val; fp@2685: } fp@2685: fp@2685: /** fp@2685: * igb_poll_for_msg - Wait for message notification fp@2685: * @hw: pointer to the HW structure fp@2685: * @mbx_id: id of mailbox to write fp@2685: * fp@2685: * returns SUCCESS if it successfully received a message notification fp@2685: **/ fp@2685: static s32 igb_poll_for_msg(struct e1000_hw *hw, u16 mbx_id) fp@2685: { fp@2685: struct e1000_mbx_info *mbx = &hw->mbx; fp@2685: int countdown = mbx->timeout; fp@2685: fp@2685: if (!countdown || !mbx->ops.check_for_msg) fp@2685: goto out; fp@2685: fp@2685: while (countdown && mbx->ops.check_for_msg(hw, mbx_id)) { fp@2685: countdown--; fp@2685: if (!countdown) fp@2685: break; fp@2685: udelay(mbx->usec_delay); fp@2685: } fp@2685: fp@2685: /* if we failed, all future posted messages fail until reset */ fp@2685: if (!countdown) fp@2685: mbx->timeout = 0; fp@2685: out: fp@2685: return countdown ? 0 : -E1000_ERR_MBX; fp@2685: } fp@2685: fp@2685: /** fp@2685: * igb_poll_for_ack - Wait for message acknowledgement fp@2685: * @hw: pointer to the HW structure fp@2685: * @mbx_id: id of mailbox to write fp@2685: * fp@2685: * returns SUCCESS if it successfully received a message acknowledgement fp@2685: **/ fp@2685: static s32 igb_poll_for_ack(struct e1000_hw *hw, u16 mbx_id) fp@2685: { fp@2685: struct e1000_mbx_info *mbx = &hw->mbx; fp@2685: int countdown = mbx->timeout; fp@2685: fp@2685: if (!countdown || !mbx->ops.check_for_ack) fp@2685: goto out; fp@2685: fp@2685: while (countdown && mbx->ops.check_for_ack(hw, mbx_id)) { fp@2685: countdown--; fp@2685: if (!countdown) fp@2685: break; fp@2685: udelay(mbx->usec_delay); fp@2685: } fp@2685: fp@2685: /* if we failed, all future posted messages fail until reset */ fp@2685: if (!countdown) fp@2685: mbx->timeout = 0; fp@2685: out: fp@2685: return countdown ? 0 : -E1000_ERR_MBX; fp@2685: } fp@2685: fp@2685: /** fp@2685: * igb_read_posted_mbx - Wait for message notification and receive message fp@2685: * @hw: pointer to the HW structure fp@2685: * @msg: The message buffer fp@2685: * @size: Length of buffer fp@2685: * @mbx_id: id of mailbox to write fp@2685: * fp@2685: * returns SUCCESS if it successfully received a message notification and fp@2685: * copied it into the receive buffer. fp@2685: **/ fp@2685: static s32 igb_read_posted_mbx(struct e1000_hw *hw, u32 *msg, u16 size, fp@2685: u16 mbx_id) fp@2685: { fp@2685: struct e1000_mbx_info *mbx = &hw->mbx; fp@2685: s32 ret_val = -E1000_ERR_MBX; fp@2685: fp@2685: if (!mbx->ops.read) fp@2685: goto out; fp@2685: fp@2685: ret_val = igb_poll_for_msg(hw, mbx_id); fp@2685: fp@2685: if (!ret_val) fp@2685: ret_val = mbx->ops.read(hw, msg, size, mbx_id); fp@2685: out: fp@2685: return ret_val; fp@2685: } fp@2685: fp@2685: /** fp@2685: * igb_write_posted_mbx - Write a message to the mailbox, wait for ack fp@2685: * @hw: pointer to the HW structure fp@2685: * @msg: The message buffer fp@2685: * @size: Length of buffer fp@2685: * @mbx_id: id of mailbox to write fp@2685: * fp@2685: * returns SUCCESS if it successfully copied message into the buffer and fp@2685: * received an ack to that message within delay * timeout period fp@2685: **/ fp@2685: static s32 igb_write_posted_mbx(struct e1000_hw *hw, u32 *msg, u16 size, fp@2685: u16 mbx_id) fp@2685: { fp@2685: struct e1000_mbx_info *mbx = &hw->mbx; fp@2685: s32 ret_val = -E1000_ERR_MBX; fp@2685: fp@2685: /* exit if either we can't write or there isn't a defined timeout */ fp@2685: if (!mbx->ops.write || !mbx->timeout) fp@2685: goto out; fp@2685: fp@2685: /* send msg */ fp@2685: ret_val = mbx->ops.write(hw, msg, size, mbx_id); fp@2685: fp@2685: /* if msg sent wait until we receive an ack */ fp@2685: if (!ret_val) fp@2685: ret_val = igb_poll_for_ack(hw, mbx_id); fp@2685: out: fp@2685: return ret_val; fp@2685: } fp@2685: fp@2685: static s32 igb_check_for_bit_pf(struct e1000_hw *hw, u32 mask) fp@2685: { fp@2685: u32 mbvficr = rd32(E1000_MBVFICR); fp@2685: s32 ret_val = -E1000_ERR_MBX; fp@2685: fp@2685: if (mbvficr & mask) { fp@2685: ret_val = 0; fp@2685: wr32(E1000_MBVFICR, mask); fp@2685: } fp@2685: fp@2685: return ret_val; fp@2685: } fp@2685: fp@2685: /** fp@2685: * igb_check_for_msg_pf - checks to see if the VF has sent mail fp@2685: * @hw: pointer to the HW structure fp@2685: * @vf_number: the VF index fp@2685: * fp@2685: * returns SUCCESS if the VF has set the Status bit or else ERR_MBX fp@2685: **/ fp@2685: static s32 igb_check_for_msg_pf(struct e1000_hw *hw, u16 vf_number) fp@2685: { fp@2685: s32 ret_val = -E1000_ERR_MBX; fp@2685: fp@2685: if (!igb_check_for_bit_pf(hw, E1000_MBVFICR_VFREQ_VF1 << vf_number)) { fp@2685: ret_val = 0; fp@2685: hw->mbx.stats.reqs++; fp@2685: } fp@2685: fp@2685: return ret_val; fp@2685: } fp@2685: fp@2685: /** fp@2685: * igb_check_for_ack_pf - checks to see if the VF has ACKed fp@2685: * @hw: pointer to the HW structure fp@2685: * @vf_number: the VF index fp@2685: * fp@2685: * returns SUCCESS if the VF has set the Status bit or else ERR_MBX fp@2685: **/ fp@2685: static s32 igb_check_for_ack_pf(struct e1000_hw *hw, u16 vf_number) fp@2685: { fp@2685: s32 ret_val = -E1000_ERR_MBX; fp@2685: fp@2685: if (!igb_check_for_bit_pf(hw, E1000_MBVFICR_VFACK_VF1 << vf_number)) { fp@2685: ret_val = 0; fp@2685: hw->mbx.stats.acks++; fp@2685: } fp@2685: fp@2685: return ret_val; fp@2685: } fp@2685: fp@2685: /** fp@2685: * igb_check_for_rst_pf - checks to see if the VF has reset fp@2685: * @hw: pointer to the HW structure fp@2685: * @vf_number: the VF index fp@2685: * fp@2685: * returns SUCCESS if the VF has set the Status bit or else ERR_MBX fp@2685: **/ fp@2685: static s32 igb_check_for_rst_pf(struct e1000_hw *hw, u16 vf_number) fp@2685: { fp@2685: u32 vflre = rd32(E1000_VFLRE); fp@2685: s32 ret_val = -E1000_ERR_MBX; fp@2685: fp@2685: if (vflre & (1 << vf_number)) { fp@2685: ret_val = 0; fp@2685: wr32(E1000_VFLRE, (1 << vf_number)); fp@2685: hw->mbx.stats.rsts++; fp@2685: } fp@2685: fp@2685: return ret_val; fp@2685: } fp@2685: fp@2685: /** fp@2685: * igb_obtain_mbx_lock_pf - obtain mailbox lock fp@2685: * @hw: pointer to the HW structure fp@2685: * @vf_number: the VF index fp@2685: * fp@2685: * return SUCCESS if we obtained the mailbox lock fp@2685: **/ fp@2685: static s32 igb_obtain_mbx_lock_pf(struct e1000_hw *hw, u16 vf_number) fp@2685: { fp@2685: s32 ret_val = -E1000_ERR_MBX; fp@2685: u32 p2v_mailbox; fp@2685: fp@2685: /* Take ownership of the buffer */ fp@2685: wr32(E1000_P2VMAILBOX(vf_number), E1000_P2VMAILBOX_PFU); fp@2685: fp@2685: /* reserve mailbox for vf use */ fp@2685: p2v_mailbox = rd32(E1000_P2VMAILBOX(vf_number)); fp@2685: if (p2v_mailbox & E1000_P2VMAILBOX_PFU) fp@2685: ret_val = 0; fp@2685: fp@2685: return ret_val; fp@2685: } fp@2685: fp@2685: /** fp@2685: * igb_write_mbx_pf - Places a message in the mailbox fp@2685: * @hw: pointer to the HW structure fp@2685: * @msg: The message buffer fp@2685: * @size: Length of buffer fp@2685: * @vf_number: the VF index fp@2685: * fp@2685: * returns SUCCESS if it successfully copied message into the buffer fp@2685: **/ fp@2685: static s32 igb_write_mbx_pf(struct e1000_hw *hw, u32 *msg, u16 size, fp@2685: u16 vf_number) fp@2685: { fp@2685: s32 ret_val; fp@2685: u16 i; fp@2685: fp@2685: /* lock the mailbox to prevent pf/vf race condition */ fp@2685: ret_val = igb_obtain_mbx_lock_pf(hw, vf_number); fp@2685: if (ret_val) fp@2685: goto out_no_write; fp@2685: fp@2685: /* flush msg and acks as we are overwriting the message buffer */ fp@2685: igb_check_for_msg_pf(hw, vf_number); fp@2685: igb_check_for_ack_pf(hw, vf_number); fp@2685: fp@2685: /* copy the caller specified message to the mailbox memory buffer */ fp@2685: for (i = 0; i < size; i++) fp@2685: array_wr32(E1000_VMBMEM(vf_number), i, msg[i]); fp@2685: fp@2685: /* Interrupt VF to tell it a message has been sent and release buffer*/ fp@2685: wr32(E1000_P2VMAILBOX(vf_number), E1000_P2VMAILBOX_STS); fp@2685: fp@2685: /* update stats */ fp@2685: hw->mbx.stats.msgs_tx++; fp@2685: fp@2685: out_no_write: fp@2685: return ret_val; fp@2685: fp@2685: } fp@2685: fp@2685: /** fp@2685: * igb_read_mbx_pf - Read a message from the mailbox fp@2685: * @hw: pointer to the HW structure fp@2685: * @msg: The message buffer fp@2685: * @size: Length of buffer fp@2685: * @vf_number: the VF index fp@2685: * fp@2685: * This function copies a message from the mailbox buffer to the caller's fp@2685: * memory buffer. The presumption is that the caller knows that there was fp@2685: * a message due to a VF request so no polling for message is needed. fp@2685: **/ fp@2685: static s32 igb_read_mbx_pf(struct e1000_hw *hw, u32 *msg, u16 size, fp@2685: u16 vf_number) fp@2685: { fp@2685: s32 ret_val; fp@2685: u16 i; fp@2685: fp@2685: /* lock the mailbox to prevent pf/vf race condition */ fp@2685: ret_val = igb_obtain_mbx_lock_pf(hw, vf_number); fp@2685: if (ret_val) fp@2685: goto out_no_read; fp@2685: fp@2685: /* copy the message to the mailbox memory buffer */ fp@2685: for (i = 0; i < size; i++) fp@2685: msg[i] = array_rd32(E1000_VMBMEM(vf_number), i); fp@2685: fp@2685: /* Acknowledge the message and release buffer */ fp@2685: wr32(E1000_P2VMAILBOX(vf_number), E1000_P2VMAILBOX_ACK); fp@2685: fp@2685: /* update stats */ fp@2685: hw->mbx.stats.msgs_rx++; fp@2685: fp@2685: out_no_read: fp@2685: return ret_val; fp@2685: } fp@2685: fp@2685: /** fp@2685: * e1000_init_mbx_params_pf - set initial values for pf mailbox fp@2685: * @hw: pointer to the HW structure fp@2685: * fp@2685: * Initializes the hw->mbx struct to correct values for pf mailbox fp@2685: */ fp@2685: s32 igb_init_mbx_params_pf(struct e1000_hw *hw) fp@2685: { fp@2685: struct e1000_mbx_info *mbx = &hw->mbx; fp@2685: fp@2685: mbx->timeout = 0; fp@2685: mbx->usec_delay = 0; fp@2685: fp@2685: mbx->size = E1000_VFMAILBOX_SIZE; fp@2685: fp@2685: mbx->ops.read = igb_read_mbx_pf; fp@2685: mbx->ops.write = igb_write_mbx_pf; fp@2685: mbx->ops.read_posted = igb_read_posted_mbx; fp@2685: mbx->ops.write_posted = igb_write_posted_mbx; fp@2685: mbx->ops.check_for_msg = igb_check_for_msg_pf; fp@2685: mbx->ops.check_for_ack = igb_check_for_ack_pf; fp@2685: mbx->ops.check_for_rst = igb_check_for_rst_pf; fp@2685: fp@2685: mbx->stats.msgs_tx = 0; fp@2685: mbx->stats.msgs_rx = 0; fp@2685: mbx->stats.reqs = 0; fp@2685: mbx->stats.acks = 0; fp@2685: mbx->stats.rsts = 0; fp@2685: fp@2685: return 0; fp@2685: } fp@2685: