Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /* Copyright(c) 2009 - 2018 Intel Corporation. */
0003 
0004 #include "vf.h"
0005 
0006 static s32 e1000_check_for_link_vf(struct e1000_hw *hw);
0007 static s32 e1000_get_link_up_info_vf(struct e1000_hw *hw, u16 *speed,
0008                      u16 *duplex);
0009 static s32 e1000_init_hw_vf(struct e1000_hw *hw);
0010 static s32 e1000_reset_hw_vf(struct e1000_hw *hw);
0011 
0012 static void e1000_update_mc_addr_list_vf(struct e1000_hw *hw, u8 *,
0013                      u32, u32, u32);
0014 static void e1000_rar_set_vf(struct e1000_hw *, u8 *, u32);
0015 static s32 e1000_read_mac_addr_vf(struct e1000_hw *);
0016 static s32 e1000_set_uc_addr_vf(struct e1000_hw *hw, u32 subcmd, u8 *addr);
0017 static s32 e1000_set_vfta_vf(struct e1000_hw *, u16, bool);
0018 
0019 /**
0020  *  e1000_init_mac_params_vf - Inits MAC params
0021  *  @hw: pointer to the HW structure
0022  **/
0023 static s32 e1000_init_mac_params_vf(struct e1000_hw *hw)
0024 {
0025     struct e1000_mac_info *mac = &hw->mac;
0026 
0027     /* VF's have no MTA Registers - PF feature only */
0028     mac->mta_reg_count = 128;
0029     /* VF's have no access to RAR entries  */
0030     mac->rar_entry_count = 1;
0031 
0032     /* Function pointers */
0033     /* reset */
0034     mac->ops.reset_hw = e1000_reset_hw_vf;
0035     /* hw initialization */
0036     mac->ops.init_hw = e1000_init_hw_vf;
0037     /* check for link */
0038     mac->ops.check_for_link = e1000_check_for_link_vf;
0039     /* link info */
0040     mac->ops.get_link_up_info = e1000_get_link_up_info_vf;
0041     /* multicast address update */
0042     mac->ops.update_mc_addr_list = e1000_update_mc_addr_list_vf;
0043     /* set mac address */
0044     mac->ops.rar_set = e1000_rar_set_vf;
0045     /* read mac address */
0046     mac->ops.read_mac_addr = e1000_read_mac_addr_vf;
0047     /* set mac filter */
0048     mac->ops.set_uc_addr = e1000_set_uc_addr_vf;
0049     /* set vlan filter table array */
0050     mac->ops.set_vfta = e1000_set_vfta_vf;
0051 
0052     return E1000_SUCCESS;
0053 }
0054 
0055 /**
0056  *  e1000_init_function_pointers_vf - Inits function pointers
0057  *  @hw: pointer to the HW structure
0058  **/
0059 void e1000_init_function_pointers_vf(struct e1000_hw *hw)
0060 {
0061     hw->mac.ops.init_params = e1000_init_mac_params_vf;
0062     hw->mbx.ops.init_params = e1000_init_mbx_params_vf;
0063 }
0064 
0065 /**
0066  *  e1000_get_link_up_info_vf - Gets link info.
0067  *  @hw: pointer to the HW structure
0068  *  @speed: pointer to 16 bit value to store link speed.
0069  *  @duplex: pointer to 16 bit value to store duplex.
0070  *
0071  *  Since we cannot read the PHY and get accurate link info, we must rely upon
0072  *  the status register's data which is often stale and inaccurate.
0073  **/
0074 static s32 e1000_get_link_up_info_vf(struct e1000_hw *hw, u16 *speed,
0075                      u16 *duplex)
0076 {
0077     s32 status;
0078 
0079     status = er32(STATUS);
0080     if (status & E1000_STATUS_SPEED_1000)
0081         *speed = SPEED_1000;
0082     else if (status & E1000_STATUS_SPEED_100)
0083         *speed = SPEED_100;
0084     else
0085         *speed = SPEED_10;
0086 
0087     if (status & E1000_STATUS_FD)
0088         *duplex = FULL_DUPLEX;
0089     else
0090         *duplex = HALF_DUPLEX;
0091 
0092     return E1000_SUCCESS;
0093 }
0094 
0095 /**
0096  *  e1000_reset_hw_vf - Resets the HW
0097  *  @hw: pointer to the HW structure
0098  *
0099  *  VF's provide a function level reset. This is done using bit 26 of ctrl_reg.
0100  *  This is all the reset we can perform on a VF.
0101  **/
0102 static s32 e1000_reset_hw_vf(struct e1000_hw *hw)
0103 {
0104     struct e1000_mbx_info *mbx = &hw->mbx;
0105     u32 timeout = E1000_VF_INIT_TIMEOUT;
0106     u32 ret_val = -E1000_ERR_MAC_INIT;
0107     u32 msgbuf[3];
0108     u8 *addr = (u8 *)(&msgbuf[1]);
0109     u32 ctrl;
0110 
0111     /* assert VF queue/interrupt reset */
0112     ctrl = er32(CTRL);
0113     ew32(CTRL, ctrl | E1000_CTRL_RST);
0114 
0115     /* we cannot initialize while the RSTI / RSTD bits are asserted */
0116     while (!mbx->ops.check_for_rst(hw) && timeout) {
0117         timeout--;
0118         udelay(5);
0119     }
0120 
0121     if (timeout) {
0122         /* mailbox timeout can now become active */
0123         mbx->timeout = E1000_VF_MBX_INIT_TIMEOUT;
0124 
0125         /* notify PF of VF reset completion */
0126         msgbuf[0] = E1000_VF_RESET;
0127         mbx->ops.write_posted(hw, msgbuf, 1);
0128 
0129         mdelay(10);
0130 
0131         /* set our "perm_addr" based on info provided by PF */
0132         ret_val = mbx->ops.read_posted(hw, msgbuf, 3);
0133         if (!ret_val) {
0134             if (msgbuf[0] == (E1000_VF_RESET |
0135                       E1000_VT_MSGTYPE_ACK))
0136                 memcpy(hw->mac.perm_addr, addr, ETH_ALEN);
0137             else
0138                 ret_val = -E1000_ERR_MAC_INIT;
0139         }
0140     }
0141 
0142     return ret_val;
0143 }
0144 
0145 /**
0146  *  e1000_init_hw_vf - Inits the HW
0147  *  @hw: pointer to the HW structure
0148  *
0149  *  Not much to do here except clear the PF Reset indication if there is one.
0150  **/
0151 static s32 e1000_init_hw_vf(struct e1000_hw *hw)
0152 {
0153     /* attempt to set and restore our mac address */
0154     e1000_rar_set_vf(hw, hw->mac.addr, 0);
0155 
0156     return E1000_SUCCESS;
0157 }
0158 
0159 /**
0160  *  e1000_hash_mc_addr_vf - Generate a multicast hash value
0161  *  @hw: pointer to the HW structure
0162  *  @mc_addr: pointer to a multicast address
0163  *
0164  *  Generates a multicast address hash value which is used to determine
0165  *  the multicast filter table array address and new table value.  See
0166  *  e1000_mta_set_generic()
0167  **/
0168 static u32 e1000_hash_mc_addr_vf(struct e1000_hw *hw, u8 *mc_addr)
0169 {
0170     u32 hash_value, hash_mask;
0171     u8 bit_shift = 0;
0172 
0173     /* Register count multiplied by bits per register */
0174     hash_mask = (hw->mac.mta_reg_count * 32) - 1;
0175 
0176     /* The bit_shift is the number of left-shifts
0177      * where 0xFF would still fall within the hash mask.
0178      */
0179     while (hash_mask >> bit_shift != 0xFF)
0180         bit_shift++;
0181 
0182     hash_value = hash_mask & (((mc_addr[4] >> (8 - bit_shift)) |
0183                   (((u16)mc_addr[5]) << bit_shift)));
0184 
0185     return hash_value;
0186 }
0187 
0188 /**
0189  *  e1000_update_mc_addr_list_vf - Update Multicast addresses
0190  *  @hw: pointer to the HW structure
0191  *  @mc_addr_list: array of multicast addresses to program
0192  *  @mc_addr_count: number of multicast addresses to program
0193  *  @rar_used_count: the first RAR register free to program
0194  *  @rar_count: total number of supported Receive Address Registers
0195  *
0196  *  Updates the Receive Address Registers and Multicast Table Array.
0197  *  The caller must have a packed mc_addr_list of multicast addresses.
0198  *  The parameter rar_count will usually be hw->mac.rar_entry_count
0199  *  unless there are workarounds that change this.
0200  **/
0201 static void e1000_update_mc_addr_list_vf(struct e1000_hw *hw,
0202                      u8 *mc_addr_list, u32 mc_addr_count,
0203                      u32 rar_used_count, u32 rar_count)
0204 {
0205     struct e1000_mbx_info *mbx = &hw->mbx;
0206     u32 msgbuf[E1000_VFMAILBOX_SIZE];
0207     u16 *hash_list = (u16 *)&msgbuf[1];
0208     u32 hash_value;
0209     u32 cnt, i;
0210     s32 ret_val;
0211 
0212     /* Each entry in the list uses 1 16 bit word.  We have 30
0213      * 16 bit words available in our HW msg buffer (minus 1 for the
0214      * msg type).  That's 30 hash values if we pack 'em right.  If
0215      * there are more than 30 MC addresses to add then punt the
0216      * extras for now and then add code to handle more than 30 later.
0217      * It would be unusual for a server to request that many multi-cast
0218      * addresses except for in large enterprise network environments.
0219      */
0220 
0221     cnt = (mc_addr_count > 30) ? 30 : mc_addr_count;
0222     msgbuf[0] = E1000_VF_SET_MULTICAST;
0223     msgbuf[0] |= cnt << E1000_VT_MSGINFO_SHIFT;
0224 
0225     for (i = 0; i < cnt; i++) {
0226         hash_value = e1000_hash_mc_addr_vf(hw, mc_addr_list);
0227         hash_list[i] = hash_value & 0x0FFFF;
0228         mc_addr_list += ETH_ALEN;
0229     }
0230 
0231     ret_val = mbx->ops.write_posted(hw, msgbuf, E1000_VFMAILBOX_SIZE);
0232     if (!ret_val)
0233         mbx->ops.read_posted(hw, msgbuf, 1);
0234 }
0235 
0236 /**
0237  *  e1000_set_vfta_vf - Set/Unset vlan filter table address
0238  *  @hw: pointer to the HW structure
0239  *  @vid: determines the vfta register and bit to set/unset
0240  *  @set: if true then set bit, else clear bit
0241  **/
0242 static s32 e1000_set_vfta_vf(struct e1000_hw *hw, u16 vid, bool set)
0243 {
0244     struct e1000_mbx_info *mbx = &hw->mbx;
0245     u32 msgbuf[2];
0246     s32 err;
0247 
0248     msgbuf[0] = E1000_VF_SET_VLAN;
0249     msgbuf[1] = vid;
0250     /* Setting the 8 bit field MSG INFO to true indicates "add" */
0251     if (set)
0252         msgbuf[0] |= BIT(E1000_VT_MSGINFO_SHIFT);
0253 
0254     mbx->ops.write_posted(hw, msgbuf, 2);
0255 
0256     err = mbx->ops.read_posted(hw, msgbuf, 2);
0257 
0258     msgbuf[0] &= ~E1000_VT_MSGTYPE_CTS;
0259 
0260     /* if nacked the vlan was rejected */
0261     if (!err && (msgbuf[0] == (E1000_VF_SET_VLAN | E1000_VT_MSGTYPE_NACK)))
0262         err = -E1000_ERR_MAC_INIT;
0263 
0264     return err;
0265 }
0266 
0267 /**
0268  *  e1000_rlpml_set_vf - Set the maximum receive packet length
0269  *  @hw: pointer to the HW structure
0270  *  @max_size: value to assign to max frame size
0271  **/
0272 void e1000_rlpml_set_vf(struct e1000_hw *hw, u16 max_size)
0273 {
0274     struct e1000_mbx_info *mbx = &hw->mbx;
0275     u32 msgbuf[2];
0276     s32 ret_val;
0277 
0278     msgbuf[0] = E1000_VF_SET_LPE;
0279     msgbuf[1] = max_size;
0280 
0281     ret_val = mbx->ops.write_posted(hw, msgbuf, 2);
0282     if (!ret_val)
0283         mbx->ops.read_posted(hw, msgbuf, 1);
0284 }
0285 
0286 /**
0287  *  e1000_rar_set_vf - set device MAC address
0288  *  @hw: pointer to the HW structure
0289  *  @addr: pointer to the receive address
0290  *  @index: receive address array register
0291  **/
0292 static void e1000_rar_set_vf(struct e1000_hw *hw, u8 *addr, u32 index)
0293 {
0294     struct e1000_mbx_info *mbx = &hw->mbx;
0295     u32 msgbuf[3];
0296     u8 *msg_addr = (u8 *)(&msgbuf[1]);
0297     s32 ret_val;
0298 
0299     memset(msgbuf, 0, 12);
0300     msgbuf[0] = E1000_VF_SET_MAC_ADDR;
0301     memcpy(msg_addr, addr, ETH_ALEN);
0302     ret_val = mbx->ops.write_posted(hw, msgbuf, 3);
0303 
0304     if (!ret_val)
0305         ret_val = mbx->ops.read_posted(hw, msgbuf, 3);
0306 
0307     msgbuf[0] &= ~E1000_VT_MSGTYPE_CTS;
0308 
0309     /* if nacked the address was rejected, use "perm_addr" */
0310     if (!ret_val &&
0311         (msgbuf[0] == (E1000_VF_SET_MAC_ADDR | E1000_VT_MSGTYPE_NACK)))
0312         e1000_read_mac_addr_vf(hw);
0313 }
0314 
0315 /**
0316  *  e1000_read_mac_addr_vf - Read device MAC address
0317  *  @hw: pointer to the HW structure
0318  **/
0319 static s32 e1000_read_mac_addr_vf(struct e1000_hw *hw)
0320 {
0321     memcpy(hw->mac.addr, hw->mac.perm_addr, ETH_ALEN);
0322 
0323     return E1000_SUCCESS;
0324 }
0325 
0326 /**
0327  *  e1000_set_uc_addr_vf - Set or clear unicast filters
0328  *  @hw: pointer to the HW structure
0329  *  @sub_cmd: add or clear filters
0330  *  @addr: pointer to the filter MAC address
0331  **/
0332 static s32 e1000_set_uc_addr_vf(struct e1000_hw *hw, u32 sub_cmd, u8 *addr)
0333 {
0334     struct e1000_mbx_info *mbx = &hw->mbx;
0335     u32 msgbuf[3], msgbuf_chk;
0336     u8 *msg_addr = (u8 *)(&msgbuf[1]);
0337     s32 ret_val;
0338 
0339     memset(msgbuf, 0, sizeof(msgbuf));
0340     msgbuf[0] |= sub_cmd;
0341     msgbuf[0] |= E1000_VF_SET_MAC_ADDR;
0342     msgbuf_chk = msgbuf[0];
0343 
0344     if (addr)
0345         memcpy(msg_addr, addr, ETH_ALEN);
0346 
0347     ret_val = mbx->ops.write_posted(hw, msgbuf, 3);
0348 
0349     if (!ret_val)
0350         ret_val = mbx->ops.read_posted(hw, msgbuf, 3);
0351 
0352     msgbuf[0] &= ~E1000_VT_MSGTYPE_CTS;
0353 
0354     if (!ret_val) {
0355         msgbuf[0] &= ~E1000_VT_MSGTYPE_CTS;
0356 
0357         if (msgbuf[0] == (msgbuf_chk | E1000_VT_MSGTYPE_NACK))
0358             return -ENOSPC;
0359     }
0360 
0361     return ret_val;
0362 }
0363 
0364 /**
0365  *  e1000_check_for_link_vf - Check for link for a virtual interface
0366  *  @hw: pointer to the HW structure
0367  *
0368  *  Checks to see if the underlying PF is still talking to the VF and
0369  *  if it is then it reports the link state to the hardware, otherwise
0370  *  it reports link down and returns an error.
0371  **/
0372 static s32 e1000_check_for_link_vf(struct e1000_hw *hw)
0373 {
0374     struct e1000_mbx_info *mbx = &hw->mbx;
0375     struct e1000_mac_info *mac = &hw->mac;
0376     s32 ret_val = E1000_SUCCESS;
0377     u32 in_msg = 0;
0378 
0379     /* We only want to run this if there has been a rst asserted.
0380      * in this case that could mean a link change, device reset,
0381      * or a virtual function reset
0382      */
0383 
0384     /* If we were hit with a reset or timeout drop the link */
0385     if (!mbx->ops.check_for_rst(hw) || !mbx->timeout)
0386         mac->get_link_status = true;
0387 
0388     if (!mac->get_link_status)
0389         goto out;
0390 
0391     /* if link status is down no point in checking to see if PF is up */
0392     if (!(er32(STATUS) & E1000_STATUS_LU))
0393         goto out;
0394 
0395     /* if the read failed it could just be a mailbox collision, best wait
0396      * until we are called again and don't report an error
0397      */
0398     if (mbx->ops.read(hw, &in_msg, 1))
0399         goto out;
0400 
0401     /* if incoming message isn't clear to send we are waiting on response */
0402     if (!(in_msg & E1000_VT_MSGTYPE_CTS)) {
0403         /* msg is not CTS and is NACK we must have lost CTS status */
0404         if (in_msg & E1000_VT_MSGTYPE_NACK)
0405             ret_val = -E1000_ERR_MAC_INIT;
0406         goto out;
0407     }
0408 
0409     /* the PF is talking, if we timed out in the past we reinit */
0410     if (!mbx->timeout) {
0411         ret_val = -E1000_ERR_MAC_INIT;
0412         goto out;
0413     }
0414 
0415     /* if we passed all the tests above then the link is up and we no
0416      * longer need to check for link
0417      */
0418     mac->get_link_status = false;
0419 
0420 out:
0421     return ret_val;
0422 }
0423