Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /* Copyright(c) 1999 - 2018 Intel Corporation. */
0003 
0004 #include "vf.h"
0005 #include "ixgbevf.h"
0006 
0007 /* On Hyper-V, to reset, we need to read from this offset
0008  * from the PCI config space. This is the mechanism used on
0009  * Hyper-V to support PF/VF communication.
0010  */
0011 #define IXGBE_HV_RESET_OFFSET           0x201
0012 
0013 static inline s32 ixgbevf_write_msg_read_ack(struct ixgbe_hw *hw, u32 *msg,
0014                          u32 *retmsg, u16 size)
0015 {
0016     s32 retval = ixgbevf_write_mbx(hw, msg, size);
0017 
0018     if (retval)
0019         return retval;
0020 
0021     return ixgbevf_poll_mbx(hw, retmsg, size);
0022 }
0023 
0024 /**
0025  *  ixgbevf_start_hw_vf - Prepare hardware for Tx/Rx
0026  *  @hw: pointer to hardware structure
0027  *
0028  *  Starts the hardware by filling the bus info structure and media type, clears
0029  *  all on chip counters, initializes receive address registers, multicast
0030  *  table, VLAN filter table, calls routine to set up link and flow control
0031  *  settings, and leaves transmit and receive units disabled and uninitialized
0032  **/
0033 static s32 ixgbevf_start_hw_vf(struct ixgbe_hw *hw)
0034 {
0035     /* Clear adapter stopped flag */
0036     hw->adapter_stopped = false;
0037 
0038     return 0;
0039 }
0040 
0041 /**
0042  *  ixgbevf_init_hw_vf - virtual function hardware initialization
0043  *  @hw: pointer to hardware structure
0044  *
0045  *  Initialize the hardware by resetting the hardware and then starting
0046  *  the hardware
0047  **/
0048 static s32 ixgbevf_init_hw_vf(struct ixgbe_hw *hw)
0049 {
0050     s32 status = hw->mac.ops.start_hw(hw);
0051 
0052     hw->mac.ops.get_mac_addr(hw, hw->mac.addr);
0053 
0054     return status;
0055 }
0056 
0057 /**
0058  *  ixgbevf_reset_hw_vf - Performs hardware reset
0059  *  @hw: pointer to hardware structure
0060  *
0061  *  Resets the hardware by resetting the transmit and receive units, masks and
0062  *  clears all interrupts.
0063  **/
0064 static s32 ixgbevf_reset_hw_vf(struct ixgbe_hw *hw)
0065 {
0066     struct ixgbe_mbx_info *mbx = &hw->mbx;
0067     u32 timeout = IXGBE_VF_INIT_TIMEOUT;
0068     u32 msgbuf[IXGBE_VF_PERMADDR_MSG_LEN];
0069     u8 *addr = (u8 *)(&msgbuf[1]);
0070     s32 ret_val;
0071 
0072     /* Call adapter stop to disable tx/rx and clear interrupts */
0073     hw->mac.ops.stop_adapter(hw);
0074 
0075     /* reset the api version */
0076     hw->api_version = ixgbe_mbox_api_10;
0077     hw->mbx.ops.init_params(hw);
0078     memcpy(&hw->mbx.ops, &ixgbevf_mbx_ops_legacy,
0079            sizeof(struct ixgbe_mbx_operations));
0080 
0081     IXGBE_WRITE_REG(hw, IXGBE_VFCTRL, IXGBE_CTRL_RST);
0082     IXGBE_WRITE_FLUSH(hw);
0083 
0084     /* we cannot reset while the RSTI / RSTD bits are asserted */
0085     while (!mbx->ops.check_for_rst(hw) && timeout) {
0086         timeout--;
0087         udelay(5);
0088     }
0089 
0090     if (!timeout)
0091         return IXGBE_ERR_RESET_FAILED;
0092 
0093     /* mailbox timeout can now become active */
0094     mbx->timeout = IXGBE_VF_MBX_INIT_TIMEOUT;
0095 
0096     msgbuf[0] = IXGBE_VF_RESET;
0097     ixgbevf_write_mbx(hw, msgbuf, 1);
0098 
0099     mdelay(10);
0100 
0101     /* set our "perm_addr" based on info provided by PF
0102      * also set up the mc_filter_type which is piggy backed
0103      * on the mac address in word 3
0104      */
0105     ret_val = ixgbevf_poll_mbx(hw, msgbuf, IXGBE_VF_PERMADDR_MSG_LEN);
0106     if (ret_val)
0107         return ret_val;
0108 
0109     /* New versions of the PF may NACK the reset return message
0110      * to indicate that no MAC address has yet been assigned for
0111      * the VF.
0112      */
0113     if (msgbuf[0] != (IXGBE_VF_RESET | IXGBE_VT_MSGTYPE_SUCCESS) &&
0114         msgbuf[0] != (IXGBE_VF_RESET | IXGBE_VT_MSGTYPE_FAILURE))
0115         return IXGBE_ERR_INVALID_MAC_ADDR;
0116 
0117     if (msgbuf[0] == (IXGBE_VF_RESET | IXGBE_VT_MSGTYPE_SUCCESS))
0118         ether_addr_copy(hw->mac.perm_addr, addr);
0119 
0120     hw->mac.mc_filter_type = msgbuf[IXGBE_VF_MC_TYPE_WORD];
0121 
0122     return 0;
0123 }
0124 
0125 /**
0126  * ixgbevf_hv_reset_hw_vf - reset via Hyper-V
0127  * @hw: pointer to private hardware struct
0128  *
0129  * Hyper-V variant; the VF/PF communication is through the PCI
0130  * config space.
0131  */
0132 static s32 ixgbevf_hv_reset_hw_vf(struct ixgbe_hw *hw)
0133 {
0134 #if IS_ENABLED(CONFIG_PCI_MMCONFIG)
0135     struct ixgbevf_adapter *adapter = hw->back;
0136     int i;
0137 
0138     for (i = 0; i < 6; i++)
0139         pci_read_config_byte(adapter->pdev,
0140                      (i + IXGBE_HV_RESET_OFFSET),
0141                      &hw->mac.perm_addr[i]);
0142     return 0;
0143 #else
0144     pr_err("PCI_MMCONFIG needs to be enabled for Hyper-V\n");
0145     return -EOPNOTSUPP;
0146 #endif
0147 }
0148 
0149 /**
0150  *  ixgbevf_stop_hw_vf - Generic stop Tx/Rx units
0151  *  @hw: pointer to hardware structure
0152  *
0153  *  Sets the adapter_stopped flag within ixgbe_hw struct. Clears interrupts,
0154  *  disables transmit and receive units. The adapter_stopped flag is used by
0155  *  the shared code and drivers to determine if the adapter is in a stopped
0156  *  state and should not touch the hardware.
0157  **/
0158 static s32 ixgbevf_stop_hw_vf(struct ixgbe_hw *hw)
0159 {
0160     u32 number_of_queues;
0161     u32 reg_val;
0162     u16 i;
0163 
0164     /* Set the adapter_stopped flag so other driver functions stop touching
0165      * the hardware
0166      */
0167     hw->adapter_stopped = true;
0168 
0169     /* Disable the receive unit by stopped each queue */
0170     number_of_queues = hw->mac.max_rx_queues;
0171     for (i = 0; i < number_of_queues; i++) {
0172         reg_val = IXGBE_READ_REG(hw, IXGBE_VFRXDCTL(i));
0173         if (reg_val & IXGBE_RXDCTL_ENABLE) {
0174             reg_val &= ~IXGBE_RXDCTL_ENABLE;
0175             IXGBE_WRITE_REG(hw, IXGBE_VFRXDCTL(i), reg_val);
0176         }
0177     }
0178 
0179     IXGBE_WRITE_FLUSH(hw);
0180 
0181     /* Clear interrupt mask to stop from interrupts being generated */
0182     IXGBE_WRITE_REG(hw, IXGBE_VTEIMC, IXGBE_VF_IRQ_CLEAR_MASK);
0183 
0184     /* Clear any pending interrupts */
0185     IXGBE_READ_REG(hw, IXGBE_VTEICR);
0186 
0187     /* Disable the transmit unit.  Each queue must be disabled. */
0188     number_of_queues = hw->mac.max_tx_queues;
0189     for (i = 0; i < number_of_queues; i++) {
0190         reg_val = IXGBE_READ_REG(hw, IXGBE_VFTXDCTL(i));
0191         if (reg_val & IXGBE_TXDCTL_ENABLE) {
0192             reg_val &= ~IXGBE_TXDCTL_ENABLE;
0193             IXGBE_WRITE_REG(hw, IXGBE_VFTXDCTL(i), reg_val);
0194         }
0195     }
0196 
0197     return 0;
0198 }
0199 
0200 /**
0201  *  ixgbevf_mta_vector - Determines bit-vector in multicast table to set
0202  *  @hw: pointer to hardware structure
0203  *  @mc_addr: the multicast address
0204  *
0205  *  Extracts the 12 bits, from a multicast address, to determine which
0206  *  bit-vector to set in the multicast table. The hardware uses 12 bits, from
0207  *  incoming Rx multicast addresses, to determine the bit-vector to check in
0208  *  the MTA. Which of the 4 combination, of 12-bits, the hardware uses is set
0209  *  by the MO field of the MCSTCTRL. The MO field is set during initialization
0210  *  to mc_filter_type.
0211  **/
0212 static s32 ixgbevf_mta_vector(struct ixgbe_hw *hw, u8 *mc_addr)
0213 {
0214     u32 vector = 0;
0215 
0216     switch (hw->mac.mc_filter_type) {
0217     case 0:   /* use bits [47:36] of the address */
0218         vector = ((mc_addr[4] >> 4) | (((u16)mc_addr[5]) << 4));
0219         break;
0220     case 1:   /* use bits [46:35] of the address */
0221         vector = ((mc_addr[4] >> 3) | (((u16)mc_addr[5]) << 5));
0222         break;
0223     case 2:   /* use bits [45:34] of the address */
0224         vector = ((mc_addr[4] >> 2) | (((u16)mc_addr[5]) << 6));
0225         break;
0226     case 3:   /* use bits [43:32] of the address */
0227         vector = ((mc_addr[4]) | (((u16)mc_addr[5]) << 8));
0228         break;
0229     default:  /* Invalid mc_filter_type */
0230         break;
0231     }
0232 
0233     /* vector can only be 12-bits or boundary will be exceeded */
0234     vector &= 0xFFF;
0235     return vector;
0236 }
0237 
0238 /**
0239  *  ixgbevf_get_mac_addr_vf - Read device MAC address
0240  *  @hw: pointer to the HW structure
0241  *  @mac_addr: pointer to storage for retrieved MAC address
0242  **/
0243 static s32 ixgbevf_get_mac_addr_vf(struct ixgbe_hw *hw, u8 *mac_addr)
0244 {
0245     ether_addr_copy(mac_addr, hw->mac.perm_addr);
0246 
0247     return 0;
0248 }
0249 
0250 static s32 ixgbevf_set_uc_addr_vf(struct ixgbe_hw *hw, u32 index, u8 *addr)
0251 {
0252     u32 msgbuf[3], msgbuf_chk;
0253     u8 *msg_addr = (u8 *)(&msgbuf[1]);
0254     s32 ret_val;
0255 
0256     memset(msgbuf, 0, sizeof(msgbuf));
0257     /* If index is one then this is the start of a new list and needs
0258      * indication to the PF so it can do it's own list management.
0259      * If it is zero then that tells the PF to just clear all of
0260      * this VF's macvlans and there is no new list.
0261      */
0262     msgbuf[0] |= index << IXGBE_VT_MSGINFO_SHIFT;
0263     msgbuf[0] |= IXGBE_VF_SET_MACVLAN;
0264     msgbuf_chk = msgbuf[0];
0265 
0266     if (addr)
0267         ether_addr_copy(msg_addr, addr);
0268 
0269     ret_val = ixgbevf_write_msg_read_ack(hw, msgbuf, msgbuf,
0270                          ARRAY_SIZE(msgbuf));
0271     if (!ret_val) {
0272         msgbuf[0] &= ~IXGBE_VT_MSGTYPE_CTS;
0273 
0274         if (msgbuf[0] == (msgbuf_chk | IXGBE_VT_MSGTYPE_FAILURE))
0275             return -ENOMEM;
0276     }
0277 
0278     return ret_val;
0279 }
0280 
0281 static s32 ixgbevf_hv_set_uc_addr_vf(struct ixgbe_hw *hw, u32 index, u8 *addr)
0282 {
0283     return -EOPNOTSUPP;
0284 }
0285 
0286 /**
0287  * ixgbevf_get_reta_locked - get the RSS redirection table (RETA) contents.
0288  * @hw: pointer to hardware structure
0289  * @reta: buffer to fill with RETA contents.
0290  * @num_rx_queues: Number of Rx queues configured for this port
0291  *
0292  * The "reta" buffer should be big enough to contain 32 registers.
0293  *
0294  * Returns: 0 on success.
0295  *          if API doesn't support this operation - (-EOPNOTSUPP).
0296  */
0297 int ixgbevf_get_reta_locked(struct ixgbe_hw *hw, u32 *reta, int num_rx_queues)
0298 {
0299     int err, i, j;
0300     u32 msgbuf[IXGBE_VFMAILBOX_SIZE];
0301     u32 *hw_reta = &msgbuf[1];
0302     u32 mask = 0;
0303 
0304     /* We have to use a mailbox for 82599 and x540 devices only.
0305      * For these devices RETA has 128 entries.
0306      * Also these VFs support up to 4 RSS queues. Therefore PF will compress
0307      * 16 RETA entries in each DWORD giving 2 bits to each entry.
0308      */
0309     int dwords = IXGBEVF_82599_RETA_SIZE / 16;
0310 
0311     /* We support the RSS querying for 82599 and x540 devices only.
0312      * Thus return an error if API doesn't support RETA querying or querying
0313      * is not supported for this device type.
0314      */
0315     switch (hw->api_version) {
0316     case ixgbe_mbox_api_15:
0317     case ixgbe_mbox_api_14:
0318     case ixgbe_mbox_api_13:
0319     case ixgbe_mbox_api_12:
0320         if (hw->mac.type < ixgbe_mac_X550_vf)
0321             break;
0322         fallthrough;
0323     default:
0324         return -EOPNOTSUPP;
0325     }
0326 
0327     msgbuf[0] = IXGBE_VF_GET_RETA;
0328 
0329     err = ixgbevf_write_mbx(hw, msgbuf, 1);
0330 
0331     if (err)
0332         return err;
0333 
0334     err = ixgbevf_poll_mbx(hw, msgbuf, dwords + 1);
0335 
0336     if (err)
0337         return err;
0338 
0339     msgbuf[0] &= ~IXGBE_VT_MSGTYPE_CTS;
0340 
0341     /* If the operation has been refused by a PF return -EPERM */
0342     if (msgbuf[0] == (IXGBE_VF_GET_RETA | IXGBE_VT_MSGTYPE_FAILURE))
0343         return -EPERM;
0344 
0345     /* If we didn't get an ACK there must have been
0346      * some sort of mailbox error so we should treat it
0347      * as such.
0348      */
0349     if (msgbuf[0] != (IXGBE_VF_GET_RETA | IXGBE_VT_MSGTYPE_SUCCESS))
0350         return IXGBE_ERR_MBX;
0351 
0352     /* ixgbevf doesn't support more than 2 queues at the moment */
0353     if (num_rx_queues > 1)
0354         mask = 0x1;
0355 
0356     for (i = 0; i < dwords; i++)
0357         for (j = 0; j < 16; j++)
0358             reta[i * 16 + j] = (hw_reta[i] >> (2 * j)) & mask;
0359 
0360     return 0;
0361 }
0362 
0363 /**
0364  * ixgbevf_get_rss_key_locked - get the RSS Random Key
0365  * @hw: pointer to the HW structure
0366  * @rss_key: buffer to fill with RSS Hash Key contents.
0367  *
0368  * The "rss_key" buffer should be big enough to contain 10 registers.
0369  *
0370  * Returns: 0 on success.
0371  *          if API doesn't support this operation - (-EOPNOTSUPP).
0372  */
0373 int ixgbevf_get_rss_key_locked(struct ixgbe_hw *hw, u8 *rss_key)
0374 {
0375     int err;
0376     u32 msgbuf[IXGBE_VFMAILBOX_SIZE];
0377 
0378     /* We currently support the RSS Random Key retrieval for 82599 and x540
0379      * devices only.
0380      *
0381      * Thus return an error if API doesn't support RSS Random Key retrieval
0382      * or if the operation is not supported for this device type.
0383      */
0384     switch (hw->api_version) {
0385     case ixgbe_mbox_api_15:
0386     case ixgbe_mbox_api_14:
0387     case ixgbe_mbox_api_13:
0388     case ixgbe_mbox_api_12:
0389         if (hw->mac.type < ixgbe_mac_X550_vf)
0390             break;
0391         fallthrough;
0392     default:
0393         return -EOPNOTSUPP;
0394     }
0395 
0396     msgbuf[0] = IXGBE_VF_GET_RSS_KEY;
0397     err = ixgbevf_write_mbx(hw, msgbuf, 1);
0398 
0399     if (err)
0400         return err;
0401 
0402     err = ixgbevf_poll_mbx(hw, msgbuf, 11);
0403 
0404     if (err)
0405         return err;
0406 
0407     msgbuf[0] &= ~IXGBE_VT_MSGTYPE_CTS;
0408 
0409     /* If the operation has been refused by a PF return -EPERM */
0410     if (msgbuf[0] == (IXGBE_VF_GET_RSS_KEY | IXGBE_VT_MSGTYPE_FAILURE))
0411         return -EPERM;
0412 
0413     /* If we didn't get an ACK there must have been
0414      * some sort of mailbox error so we should treat it
0415      * as such.
0416      */
0417     if (msgbuf[0] != (IXGBE_VF_GET_RSS_KEY | IXGBE_VT_MSGTYPE_SUCCESS))
0418         return IXGBE_ERR_MBX;
0419 
0420     memcpy(rss_key, msgbuf + 1, IXGBEVF_RSS_HASH_KEY_SIZE);
0421 
0422     return 0;
0423 }
0424 
0425 /**
0426  *  ixgbevf_set_rar_vf - set device MAC address
0427  *  @hw: pointer to hardware structure
0428  *  @index: Receive address register to write
0429  *  @addr: Address to put into receive address register
0430  *  @vmdq: Unused in this implementation
0431  **/
0432 static s32 ixgbevf_set_rar_vf(struct ixgbe_hw *hw, u32 index, u8 *addr,
0433                   u32 vmdq)
0434 {
0435     u32 msgbuf[3];
0436     u8 *msg_addr = (u8 *)(&msgbuf[1]);
0437     s32 ret_val;
0438 
0439     memset(msgbuf, 0, sizeof(msgbuf));
0440     msgbuf[0] = IXGBE_VF_SET_MAC_ADDR;
0441     ether_addr_copy(msg_addr, addr);
0442 
0443     ret_val = ixgbevf_write_msg_read_ack(hw, msgbuf, msgbuf,
0444                          ARRAY_SIZE(msgbuf));
0445     msgbuf[0] &= ~IXGBE_VT_MSGTYPE_CTS;
0446 
0447     /* if nacked the address was rejected, use "perm_addr" */
0448     if (!ret_val &&
0449         (msgbuf[0] == (IXGBE_VF_SET_MAC_ADDR | IXGBE_VT_MSGTYPE_FAILURE))) {
0450         ixgbevf_get_mac_addr_vf(hw, hw->mac.addr);
0451         return IXGBE_ERR_MBX;
0452     }
0453 
0454     return ret_val;
0455 }
0456 
0457 /**
0458  *  ixgbevf_hv_set_rar_vf - set device MAC address Hyper-V variant
0459  *  @hw: pointer to hardware structure
0460  *  @index: Receive address register to write
0461  *  @addr: Address to put into receive address register
0462  *  @vmdq: Unused in this implementation
0463  *
0464  * We don't really allow setting the device MAC address. However,
0465  * if the address being set is the permanent MAC address we will
0466  * permit that.
0467  **/
0468 static s32 ixgbevf_hv_set_rar_vf(struct ixgbe_hw *hw, u32 index, u8 *addr,
0469                  u32 vmdq)
0470 {
0471     if (ether_addr_equal(addr, hw->mac.perm_addr))
0472         return 0;
0473 
0474     return -EOPNOTSUPP;
0475 }
0476 
0477 /**
0478  *  ixgbevf_update_mc_addr_list_vf - Update Multicast addresses
0479  *  @hw: pointer to the HW structure
0480  *  @netdev: pointer to net device structure
0481  *
0482  *  Updates the Multicast Table Array.
0483  **/
0484 static s32 ixgbevf_update_mc_addr_list_vf(struct ixgbe_hw *hw,
0485                       struct net_device *netdev)
0486 {
0487     struct netdev_hw_addr *ha;
0488     u32 msgbuf[IXGBE_VFMAILBOX_SIZE];
0489     u16 *vector_list = (u16 *)&msgbuf[1];
0490     u32 cnt, i;
0491 
0492     /* Each entry in the list uses 1 16 bit word.  We have 30
0493      * 16 bit words available in our HW msg buffer (minus 1 for the
0494      * msg type).  That's 30 hash values if we pack 'em right.  If
0495      * there are more than 30 MC addresses to add then punt the
0496      * extras for now and then add code to handle more than 30 later.
0497      * It would be unusual for a server to request that many multi-cast
0498      * addresses except for in large enterprise network environments.
0499      */
0500 
0501     cnt = netdev_mc_count(netdev);
0502     if (cnt > 30)
0503         cnt = 30;
0504     msgbuf[0] = IXGBE_VF_SET_MULTICAST;
0505     msgbuf[0] |= cnt << IXGBE_VT_MSGINFO_SHIFT;
0506 
0507     i = 0;
0508     netdev_for_each_mc_addr(ha, netdev) {
0509         if (i == cnt)
0510             break;
0511         if (is_link_local_ether_addr(ha->addr))
0512             continue;
0513 
0514         vector_list[i++] = ixgbevf_mta_vector(hw, ha->addr);
0515     }
0516 
0517     return ixgbevf_write_msg_read_ack(hw, msgbuf, msgbuf,
0518             IXGBE_VFMAILBOX_SIZE);
0519 }
0520 
0521 /**
0522  * ixgbevf_hv_update_mc_addr_list_vf - stub
0523  * @hw: unused
0524  * @netdev: unused
0525  *
0526  * Hyper-V variant - just a stub.
0527  */
0528 static s32 ixgbevf_hv_update_mc_addr_list_vf(struct ixgbe_hw *hw,
0529                          struct net_device *netdev)
0530 {
0531     return -EOPNOTSUPP;
0532 }
0533 
0534 /**
0535  *  ixgbevf_update_xcast_mode - Update Multicast mode
0536  *  @hw: pointer to the HW structure
0537  *  @xcast_mode: new multicast mode
0538  *
0539  *  Updates the Multicast Mode of VF.
0540  **/
0541 static s32 ixgbevf_update_xcast_mode(struct ixgbe_hw *hw, int xcast_mode)
0542 {
0543     u32 msgbuf[2];
0544     s32 err;
0545 
0546     switch (hw->api_version) {
0547     case ixgbe_mbox_api_12:
0548         /* promisc introduced in 1.3 version */
0549         if (xcast_mode == IXGBEVF_XCAST_MODE_PROMISC)
0550             return -EOPNOTSUPP;
0551         fallthrough;
0552     case ixgbe_mbox_api_13:
0553     case ixgbe_mbox_api_14:
0554     case ixgbe_mbox_api_15:
0555         break;
0556     default:
0557         return -EOPNOTSUPP;
0558     }
0559 
0560     msgbuf[0] = IXGBE_VF_UPDATE_XCAST_MODE;
0561     msgbuf[1] = xcast_mode;
0562 
0563     err = ixgbevf_write_msg_read_ack(hw, msgbuf, msgbuf,
0564                      ARRAY_SIZE(msgbuf));
0565     if (err)
0566         return err;
0567 
0568     msgbuf[0] &= ~IXGBE_VT_MSGTYPE_CTS;
0569     if (msgbuf[0] == (IXGBE_VF_UPDATE_XCAST_MODE | IXGBE_VT_MSGTYPE_FAILURE))
0570         return -EPERM;
0571 
0572     return 0;
0573 }
0574 
0575 /**
0576  * ixgbevf_hv_update_xcast_mode - stub
0577  * @hw: unused
0578  * @xcast_mode: unused
0579  *
0580  * Hyper-V variant - just a stub.
0581  */
0582 static s32 ixgbevf_hv_update_xcast_mode(struct ixgbe_hw *hw, int xcast_mode)
0583 {
0584     return -EOPNOTSUPP;
0585 }
0586 
0587 /**
0588  * ixgbevf_get_link_state_vf - Get VF link state from PF
0589  * @hw: pointer to the HW structure
0590  * @link_state: link state storage
0591  *
0592  * Returns state of the operation error or success.
0593  */
0594 static s32 ixgbevf_get_link_state_vf(struct ixgbe_hw *hw, bool *link_state)
0595 {
0596     u32 msgbuf[2];
0597     s32 ret_val;
0598     s32 err;
0599 
0600     msgbuf[0] = IXGBE_VF_GET_LINK_STATE;
0601     msgbuf[1] = 0x0;
0602 
0603     err = ixgbevf_write_msg_read_ack(hw, msgbuf, msgbuf, 2);
0604 
0605     if (err || (msgbuf[0] & IXGBE_VT_MSGTYPE_FAILURE)) {
0606         ret_val = IXGBE_ERR_MBX;
0607     } else {
0608         ret_val = 0;
0609         *link_state = msgbuf[1];
0610     }
0611 
0612     return ret_val;
0613 }
0614 
0615 /**
0616  * ixgbevf_hv_get_link_state_vf - * Hyper-V variant - just a stub.
0617  * @hw: unused
0618  * @link_state: unused
0619  *
0620  * Hyper-V variant; there is no mailbox communication.
0621  */
0622 static s32 ixgbevf_hv_get_link_state_vf(struct ixgbe_hw *hw, bool *link_state)
0623 {
0624     return -EOPNOTSUPP;
0625 }
0626 
0627 /**
0628  *  ixgbevf_set_vfta_vf - Set/Unset VLAN filter table address
0629  *  @hw: pointer to the HW structure
0630  *  @vlan: 12 bit VLAN ID
0631  *  @vind: unused by VF drivers
0632  *  @vlan_on: if true then set bit, else clear bit
0633  **/
0634 static s32 ixgbevf_set_vfta_vf(struct ixgbe_hw *hw, u32 vlan, u32 vind,
0635                    bool vlan_on)
0636 {
0637     u32 msgbuf[2];
0638     s32 err;
0639 
0640     msgbuf[0] = IXGBE_VF_SET_VLAN;
0641     msgbuf[1] = vlan;
0642     /* Setting the 8 bit field MSG INFO to TRUE indicates "add" */
0643     msgbuf[0] |= vlan_on << IXGBE_VT_MSGINFO_SHIFT;
0644 
0645     err = ixgbevf_write_msg_read_ack(hw, msgbuf, msgbuf,
0646                      ARRAY_SIZE(msgbuf));
0647     if (err)
0648         goto mbx_err;
0649 
0650     /* remove extra bits from the message */
0651     msgbuf[0] &= ~IXGBE_VT_MSGTYPE_CTS;
0652     msgbuf[0] &= ~(0xFF << IXGBE_VT_MSGINFO_SHIFT);
0653 
0654     if (msgbuf[0] != (IXGBE_VF_SET_VLAN | IXGBE_VT_MSGTYPE_SUCCESS))
0655         err = IXGBE_ERR_INVALID_ARGUMENT;
0656 
0657 mbx_err:
0658     return err;
0659 }
0660 
0661 /**
0662  * ixgbevf_hv_set_vfta_vf - * Hyper-V variant - just a stub.
0663  * @hw: unused
0664  * @vlan: unused
0665  * @vind: unused
0666  * @vlan_on: unused
0667  */
0668 static s32 ixgbevf_hv_set_vfta_vf(struct ixgbe_hw *hw, u32 vlan, u32 vind,
0669                   bool vlan_on)
0670 {
0671     return -EOPNOTSUPP;
0672 }
0673 
0674 /**
0675  *  ixgbevf_setup_mac_link_vf - Setup MAC link settings
0676  *  @hw: pointer to hardware structure
0677  *  @speed: Unused in this implementation
0678  *  @autoneg: Unused in this implementation
0679  *  @autoneg_wait_to_complete: Unused in this implementation
0680  *
0681  *  Do nothing and return success.  VF drivers are not allowed to change
0682  *  global settings.  Maintained for driver compatibility.
0683  **/
0684 static s32 ixgbevf_setup_mac_link_vf(struct ixgbe_hw *hw,
0685                      ixgbe_link_speed speed, bool autoneg,
0686                      bool autoneg_wait_to_complete)
0687 {
0688     return 0;
0689 }
0690 
0691 /**
0692  *  ixgbevf_check_mac_link_vf - Get link/speed status
0693  *  @hw: pointer to hardware structure
0694  *  @speed: pointer to link speed
0695  *  @link_up: true is link is up, false otherwise
0696  *  @autoneg_wait_to_complete: unused
0697  *
0698  *  Reads the links register to determine if link is up and the current speed
0699  **/
0700 static s32 ixgbevf_check_mac_link_vf(struct ixgbe_hw *hw,
0701                      ixgbe_link_speed *speed,
0702                      bool *link_up,
0703                      bool autoneg_wait_to_complete)
0704 {
0705     struct ixgbe_mbx_info *mbx = &hw->mbx;
0706     struct ixgbe_mac_info *mac = &hw->mac;
0707     s32 ret_val = 0;
0708     u32 links_reg;
0709     u32 in_msg = 0;
0710 
0711     /* If we were hit with a reset drop the link */
0712     if (!mbx->ops.check_for_rst(hw) || !mbx->timeout)
0713         mac->get_link_status = true;
0714 
0715     if (!mac->get_link_status)
0716         goto out;
0717 
0718     /* if link status is down no point in checking to see if pf is up */
0719     links_reg = IXGBE_READ_REG(hw, IXGBE_VFLINKS);
0720     if (!(links_reg & IXGBE_LINKS_UP))
0721         goto out;
0722 
0723     /* for SFP+ modules and DA cables on 82599 it can take up to 500usecs
0724      * before the link status is correct
0725      */
0726     if (mac->type == ixgbe_mac_82599_vf) {
0727         int i;
0728 
0729         for (i = 0; i < 5; i++) {
0730             udelay(100);
0731             links_reg = IXGBE_READ_REG(hw, IXGBE_VFLINKS);
0732 
0733             if (!(links_reg & IXGBE_LINKS_UP))
0734                 goto out;
0735         }
0736     }
0737 
0738     switch (links_reg & IXGBE_LINKS_SPEED_82599) {
0739     case IXGBE_LINKS_SPEED_10G_82599:
0740         *speed = IXGBE_LINK_SPEED_10GB_FULL;
0741         break;
0742     case IXGBE_LINKS_SPEED_1G_82599:
0743         *speed = IXGBE_LINK_SPEED_1GB_FULL;
0744         break;
0745     case IXGBE_LINKS_SPEED_100_82599:
0746         *speed = IXGBE_LINK_SPEED_100_FULL;
0747         break;
0748     }
0749 
0750     /* if the read failed it could just be a mailbox collision, best wait
0751      * until we are called again and don't report an error
0752      */
0753     if (mbx->ops.read(hw, &in_msg, 1)) {
0754         if (hw->api_version >= ixgbe_mbox_api_15)
0755             mac->get_link_status = false;
0756         goto out;
0757     }
0758 
0759     if (!(in_msg & IXGBE_VT_MSGTYPE_CTS)) {
0760         /* msg is not CTS and is NACK we must have lost CTS status */
0761         if (in_msg & IXGBE_VT_MSGTYPE_FAILURE)
0762             ret_val = -1;
0763         goto out;
0764     }
0765 
0766     /* the pf is talking, if we timed out in the past we reinit */
0767     if (!mbx->timeout) {
0768         ret_val = -1;
0769         goto out;
0770     }
0771 
0772     /* if we passed all the tests above then the link is up and we no
0773      * longer need to check for link
0774      */
0775     mac->get_link_status = false;
0776 
0777 out:
0778     *link_up = !mac->get_link_status;
0779     return ret_val;
0780 }
0781 
0782 /**
0783  * ixgbevf_hv_check_mac_link_vf - check link
0784  * @hw: pointer to private hardware struct
0785  * @speed: pointer to link speed
0786  * @link_up: true is link is up, false otherwise
0787  * @autoneg_wait_to_complete: unused
0788  *
0789  * Hyper-V variant; there is no mailbox communication.
0790  */
0791 static s32 ixgbevf_hv_check_mac_link_vf(struct ixgbe_hw *hw,
0792                     ixgbe_link_speed *speed,
0793                     bool *link_up,
0794                     bool autoneg_wait_to_complete)
0795 {
0796     struct ixgbe_mbx_info *mbx = &hw->mbx;
0797     struct ixgbe_mac_info *mac = &hw->mac;
0798     u32 links_reg;
0799 
0800     /* If we were hit with a reset drop the link */
0801     if (!mbx->ops.check_for_rst(hw) || !mbx->timeout)
0802         mac->get_link_status = true;
0803 
0804     if (!mac->get_link_status)
0805         goto out;
0806 
0807     /* if link status is down no point in checking to see if pf is up */
0808     links_reg = IXGBE_READ_REG(hw, IXGBE_VFLINKS);
0809     if (!(links_reg & IXGBE_LINKS_UP))
0810         goto out;
0811 
0812     /* for SFP+ modules and DA cables on 82599 it can take up to 500usecs
0813      * before the link status is correct
0814      */
0815     if (mac->type == ixgbe_mac_82599_vf) {
0816         int i;
0817 
0818         for (i = 0; i < 5; i++) {
0819             udelay(100);
0820             links_reg = IXGBE_READ_REG(hw, IXGBE_VFLINKS);
0821 
0822             if (!(links_reg & IXGBE_LINKS_UP))
0823                 goto out;
0824         }
0825     }
0826 
0827     switch (links_reg & IXGBE_LINKS_SPEED_82599) {
0828     case IXGBE_LINKS_SPEED_10G_82599:
0829         *speed = IXGBE_LINK_SPEED_10GB_FULL;
0830         break;
0831     case IXGBE_LINKS_SPEED_1G_82599:
0832         *speed = IXGBE_LINK_SPEED_1GB_FULL;
0833         break;
0834     case IXGBE_LINKS_SPEED_100_82599:
0835         *speed = IXGBE_LINK_SPEED_100_FULL;
0836         break;
0837     }
0838 
0839     /* if we passed all the tests above then the link is up and we no
0840      * longer need to check for link
0841      */
0842     mac->get_link_status = false;
0843 
0844 out:
0845     *link_up = !mac->get_link_status;
0846     return 0;
0847 }
0848 
0849 /**
0850  *  ixgbevf_set_rlpml_vf - Set the maximum receive packet length
0851  *  @hw: pointer to the HW structure
0852  *  @max_size: value to assign to max frame size
0853  **/
0854 static s32 ixgbevf_set_rlpml_vf(struct ixgbe_hw *hw, u16 max_size)
0855 {
0856     u32 msgbuf[2];
0857     s32 ret_val;
0858 
0859     msgbuf[0] = IXGBE_VF_SET_LPE;
0860     msgbuf[1] = max_size;
0861 
0862     ret_val = ixgbevf_write_msg_read_ack(hw, msgbuf, msgbuf,
0863                          ARRAY_SIZE(msgbuf));
0864     if (ret_val)
0865         return ret_val;
0866     if ((msgbuf[0] & IXGBE_VF_SET_LPE) &&
0867         (msgbuf[0] & IXGBE_VT_MSGTYPE_FAILURE))
0868         return IXGBE_ERR_MBX;
0869 
0870     return 0;
0871 }
0872 
0873 /**
0874  * ixgbevf_hv_set_rlpml_vf - Set the maximum receive packet length
0875  * @hw: pointer to the HW structure
0876  * @max_size: value to assign to max frame size
0877  * Hyper-V variant.
0878  **/
0879 static s32 ixgbevf_hv_set_rlpml_vf(struct ixgbe_hw *hw, u16 max_size)
0880 {
0881     u32 reg;
0882 
0883     /* If we are on Hyper-V, we implement this functionality
0884      * differently.
0885      */
0886     reg =  IXGBE_READ_REG(hw, IXGBE_VFRXDCTL(0));
0887     /* CRC == 4 */
0888     reg |= ((max_size + 4) | IXGBE_RXDCTL_RLPML_EN);
0889     IXGBE_WRITE_REG(hw, IXGBE_VFRXDCTL(0), reg);
0890 
0891     return 0;
0892 }
0893 
0894 /**
0895  *  ixgbevf_negotiate_api_version_vf - Negotiate supported API version
0896  *  @hw: pointer to the HW structure
0897  *  @api: integer containing requested API version
0898  **/
0899 static int ixgbevf_negotiate_api_version_vf(struct ixgbe_hw *hw, int api)
0900 {
0901     int err;
0902     u32 msg[3];
0903 
0904     /* Negotiate the mailbox API version */
0905     msg[0] = IXGBE_VF_API_NEGOTIATE;
0906     msg[1] = api;
0907     msg[2] = 0;
0908 
0909     err = ixgbevf_write_msg_read_ack(hw, msg, msg, ARRAY_SIZE(msg));
0910     if (!err) {
0911         msg[0] &= ~IXGBE_VT_MSGTYPE_CTS;
0912 
0913         /* Store value and return 0 on success */
0914         if (msg[0] == (IXGBE_VF_API_NEGOTIATE |
0915                   IXGBE_VT_MSGTYPE_SUCCESS)) {
0916             hw->api_version = api;
0917             return 0;
0918         }
0919 
0920         err = IXGBE_ERR_INVALID_ARGUMENT;
0921     }
0922 
0923     return err;
0924 }
0925 
0926 /**
0927  *  ixgbevf_hv_negotiate_api_version_vf - Negotiate supported API version
0928  *  @hw: pointer to the HW structure
0929  *  @api: integer containing requested API version
0930  *  Hyper-V version - only ixgbe_mbox_api_10 supported.
0931  **/
0932 static int ixgbevf_hv_negotiate_api_version_vf(struct ixgbe_hw *hw, int api)
0933 {
0934     /* Hyper-V only supports api version ixgbe_mbox_api_10 */
0935     if (api != ixgbe_mbox_api_10)
0936         return IXGBE_ERR_INVALID_ARGUMENT;
0937 
0938     return 0;
0939 }
0940 
0941 int ixgbevf_get_queues(struct ixgbe_hw *hw, unsigned int *num_tcs,
0942                unsigned int *default_tc)
0943 {
0944     int err;
0945     u32 msg[5];
0946 
0947     /* do nothing if API doesn't support ixgbevf_get_queues */
0948     switch (hw->api_version) {
0949     case ixgbe_mbox_api_11:
0950     case ixgbe_mbox_api_12:
0951     case ixgbe_mbox_api_13:
0952     case ixgbe_mbox_api_14:
0953     case ixgbe_mbox_api_15:
0954         break;
0955     default:
0956         return 0;
0957     }
0958 
0959     /* Fetch queue configuration from the PF */
0960     msg[0] = IXGBE_VF_GET_QUEUE;
0961     msg[1] = msg[2] = msg[3] = msg[4] = 0;
0962 
0963     err = ixgbevf_write_msg_read_ack(hw, msg, msg, ARRAY_SIZE(msg));
0964     if (!err) {
0965         msg[0] &= ~IXGBE_VT_MSGTYPE_CTS;
0966 
0967         /* if we didn't get an ACK there must have been
0968          * some sort of mailbox error so we should treat it
0969          * as such
0970          */
0971         if (msg[0] != (IXGBE_VF_GET_QUEUE | IXGBE_VT_MSGTYPE_SUCCESS))
0972             return IXGBE_ERR_MBX;
0973 
0974         /* record and validate values from message */
0975         hw->mac.max_tx_queues = msg[IXGBE_VF_TX_QUEUES];
0976         if (hw->mac.max_tx_queues == 0 ||
0977             hw->mac.max_tx_queues > IXGBE_VF_MAX_TX_QUEUES)
0978             hw->mac.max_tx_queues = IXGBE_VF_MAX_TX_QUEUES;
0979 
0980         hw->mac.max_rx_queues = msg[IXGBE_VF_RX_QUEUES];
0981         if (hw->mac.max_rx_queues == 0 ||
0982             hw->mac.max_rx_queues > IXGBE_VF_MAX_RX_QUEUES)
0983             hw->mac.max_rx_queues = IXGBE_VF_MAX_RX_QUEUES;
0984 
0985         *num_tcs = msg[IXGBE_VF_TRANS_VLAN];
0986         /* in case of unknown state assume we cannot tag frames */
0987         if (*num_tcs > hw->mac.max_rx_queues)
0988             *num_tcs = 1;
0989 
0990         *default_tc = msg[IXGBE_VF_DEF_QUEUE];
0991         /* default to queue 0 on out-of-bounds queue number */
0992         if (*default_tc >= hw->mac.max_tx_queues)
0993             *default_tc = 0;
0994     }
0995 
0996     return err;
0997 }
0998 
0999 static const struct ixgbe_mac_operations ixgbevf_mac_ops = {
1000     .init_hw        = ixgbevf_init_hw_vf,
1001     .reset_hw       = ixgbevf_reset_hw_vf,
1002     .start_hw       = ixgbevf_start_hw_vf,
1003     .get_mac_addr       = ixgbevf_get_mac_addr_vf,
1004     .stop_adapter       = ixgbevf_stop_hw_vf,
1005     .setup_link     = ixgbevf_setup_mac_link_vf,
1006     .check_link     = ixgbevf_check_mac_link_vf,
1007     .negotiate_api_version  = ixgbevf_negotiate_api_version_vf,
1008     .set_rar        = ixgbevf_set_rar_vf,
1009     .update_mc_addr_list    = ixgbevf_update_mc_addr_list_vf,
1010     .update_xcast_mode  = ixgbevf_update_xcast_mode,
1011     .get_link_state     = ixgbevf_get_link_state_vf,
1012     .set_uc_addr        = ixgbevf_set_uc_addr_vf,
1013     .set_vfta       = ixgbevf_set_vfta_vf,
1014     .set_rlpml      = ixgbevf_set_rlpml_vf,
1015 };
1016 
1017 static const struct ixgbe_mac_operations ixgbevf_hv_mac_ops = {
1018     .init_hw        = ixgbevf_init_hw_vf,
1019     .reset_hw       = ixgbevf_hv_reset_hw_vf,
1020     .start_hw       = ixgbevf_start_hw_vf,
1021     .get_mac_addr       = ixgbevf_get_mac_addr_vf,
1022     .stop_adapter       = ixgbevf_stop_hw_vf,
1023     .setup_link     = ixgbevf_setup_mac_link_vf,
1024     .check_link     = ixgbevf_hv_check_mac_link_vf,
1025     .negotiate_api_version  = ixgbevf_hv_negotiate_api_version_vf,
1026     .set_rar        = ixgbevf_hv_set_rar_vf,
1027     .update_mc_addr_list    = ixgbevf_hv_update_mc_addr_list_vf,
1028     .update_xcast_mode  = ixgbevf_hv_update_xcast_mode,
1029     .get_link_state     = ixgbevf_hv_get_link_state_vf,
1030     .set_uc_addr        = ixgbevf_hv_set_uc_addr_vf,
1031     .set_vfta       = ixgbevf_hv_set_vfta_vf,
1032     .set_rlpml      = ixgbevf_hv_set_rlpml_vf,
1033 };
1034 
1035 const struct ixgbevf_info ixgbevf_82599_vf_info = {
1036     .mac = ixgbe_mac_82599_vf,
1037     .mac_ops = &ixgbevf_mac_ops,
1038 };
1039 
1040 const struct ixgbevf_info ixgbevf_82599_vf_hv_info = {
1041     .mac = ixgbe_mac_82599_vf,
1042     .mac_ops = &ixgbevf_hv_mac_ops,
1043 };
1044 
1045 const struct ixgbevf_info ixgbevf_X540_vf_info = {
1046     .mac = ixgbe_mac_X540_vf,
1047     .mac_ops = &ixgbevf_mac_ops,
1048 };
1049 
1050 const struct ixgbevf_info ixgbevf_X540_vf_hv_info = {
1051     .mac = ixgbe_mac_X540_vf,
1052     .mac_ops = &ixgbevf_hv_mac_ops,
1053 };
1054 
1055 const struct ixgbevf_info ixgbevf_X550_vf_info = {
1056     .mac = ixgbe_mac_X550_vf,
1057     .mac_ops = &ixgbevf_mac_ops,
1058 };
1059 
1060 const struct ixgbevf_info ixgbevf_X550_vf_hv_info = {
1061     .mac = ixgbe_mac_X550_vf,
1062     .mac_ops = &ixgbevf_hv_mac_ops,
1063 };
1064 
1065 const struct ixgbevf_info ixgbevf_X550EM_x_vf_info = {
1066     .mac = ixgbe_mac_X550EM_x_vf,
1067     .mac_ops = &ixgbevf_mac_ops,
1068 };
1069 
1070 const struct ixgbevf_info ixgbevf_X550EM_x_vf_hv_info = {
1071     .mac = ixgbe_mac_X550EM_x_vf,
1072     .mac_ops = &ixgbevf_hv_mac_ops,
1073 };
1074 
1075 const struct ixgbevf_info ixgbevf_x550em_a_vf_info = {
1076     .mac = ixgbe_mac_x550em_a_vf,
1077     .mac_ops = &ixgbevf_mac_ops,
1078 };