net/axgbe: support flow control
[dpdk.git] / drivers / net / axgbe / axgbe_ethdev.c
index 70a4209..dbd8d50 100644 (file)
@@ -20,6 +20,21 @@ static int axgbe_dev_promiscuous_enable(struct rte_eth_dev *dev);
 static int axgbe_dev_promiscuous_disable(struct rte_eth_dev *dev);
 static int axgbe_dev_allmulticast_enable(struct rte_eth_dev *dev);
 static int axgbe_dev_allmulticast_disable(struct rte_eth_dev *dev);
+static int axgbe_dev_mac_addr_set(struct rte_eth_dev *dev,
+                                 struct rte_ether_addr *mac_addr);
+static int axgbe_dev_mac_addr_add(struct rte_eth_dev *dev,
+                                 struct rte_ether_addr *mac_addr,
+                                 uint32_t index,
+                                 uint32_t vmdq);
+static void axgbe_dev_mac_addr_remove(struct rte_eth_dev *dev, uint32_t index);
+static int axgbe_dev_set_mc_addr_list(struct rte_eth_dev *dev,
+                                     struct rte_ether_addr *mc_addr_set,
+                                     uint32_t nb_mc_addr);
+static int axgbe_dev_uc_hash_table_set(struct rte_eth_dev *dev,
+                                      struct rte_ether_addr *mac_addr,
+                                      uint8_t add);
+static int axgbe_dev_uc_all_hash_table_set(struct rte_eth_dev *dev,
+                                          uint8_t add);
 static int axgbe_dev_link_update(struct rte_eth_dev *dev,
                                 int wait_to_complete);
 static int axgbe_dev_get_regs(struct rte_eth_dev *dev,
@@ -47,6 +62,10 @@ axgbe_dev_xstats_get_names_by_id(struct rte_eth_dev *dev,
 static int axgbe_dev_xstats_reset(struct rte_eth_dev *dev);
 static int  axgbe_dev_info_get(struct rte_eth_dev *dev,
                               struct rte_eth_dev_info *dev_info);
+static int axgbe_flow_ctrl_get(struct rte_eth_dev *dev,
+                               struct rte_eth_fc_conf *fc_conf);
+static int axgbe_flow_ctrl_set(struct rte_eth_dev *dev,
+                               struct rte_eth_fc_conf *fc_conf);
 
 struct axgbe_xstats {
        char name[RTE_ETH_XSTATS_NAME_SIZE];
@@ -160,6 +179,12 @@ static const struct eth_dev_ops axgbe_eth_dev_ops = {
        .promiscuous_disable  = axgbe_dev_promiscuous_disable,
        .allmulticast_enable  = axgbe_dev_allmulticast_enable,
        .allmulticast_disable = axgbe_dev_allmulticast_disable,
+       .mac_addr_set         = axgbe_dev_mac_addr_set,
+       .mac_addr_add         = axgbe_dev_mac_addr_add,
+       .mac_addr_remove      = axgbe_dev_mac_addr_remove,
+       .set_mc_addr_list     = axgbe_dev_set_mc_addr_list,
+       .uc_hash_table_set    = axgbe_dev_uc_hash_table_set,
+       .uc_all_hash_table_set = axgbe_dev_uc_all_hash_table_set,
        .link_update          = axgbe_dev_link_update,
        .get_reg              = axgbe_dev_get_regs,
        .stats_get            = axgbe_dev_stats_get,
@@ -174,6 +199,8 @@ static const struct eth_dev_ops axgbe_eth_dev_ops = {
        .rx_queue_release     = axgbe_dev_rx_queue_release,
        .tx_queue_setup       = axgbe_dev_tx_queue_setup,
        .tx_queue_release     = axgbe_dev_tx_queue_release,
+       .flow_ctrl_get        = axgbe_flow_ctrl_get,
+       .flow_ctrl_set        = axgbe_flow_ctrl_set,
 };
 
 static int axgbe_phy_reset(struct axgbe_port *pdata)
@@ -384,6 +411,133 @@ axgbe_dev_allmulticast_disable(struct rte_eth_dev *dev)
        return 0;
 }
 
+static int
+axgbe_dev_mac_addr_set(struct rte_eth_dev *dev, struct rte_ether_addr *mac_addr)
+{
+       struct axgbe_port *pdata = dev->data->dev_private;
+
+       /* Set Default MAC Addr */
+       axgbe_set_mac_addn_addr(pdata, (u8 *)mac_addr, 0);
+
+       return 0;
+}
+
+static int
+axgbe_dev_mac_addr_add(struct rte_eth_dev *dev, struct rte_ether_addr *mac_addr,
+                             uint32_t index, uint32_t pool __rte_unused)
+{
+       struct axgbe_port *pdata = dev->data->dev_private;
+       struct axgbe_hw_features *hw_feat = &pdata->hw_feat;
+
+       if (index > hw_feat->addn_mac) {
+               PMD_DRV_LOG(ERR, "Invalid Index %d\n", index);
+               return -EINVAL;
+       }
+       axgbe_set_mac_addn_addr(pdata, (u8 *)mac_addr, index);
+       return 0;
+}
+
+static void
+axgbe_dev_mac_addr_remove(struct rte_eth_dev *dev, uint32_t index)
+{
+       struct axgbe_port *pdata = dev->data->dev_private;
+       struct axgbe_hw_features *hw_feat = &pdata->hw_feat;
+
+       if (index > hw_feat->addn_mac) {
+               PMD_DRV_LOG(ERR, "Invalid Index %d\n", index);
+               return;
+       }
+       axgbe_set_mac_addn_addr(pdata, NULL, index);
+}
+
+static int
+axgbe_dev_set_mc_addr_list(struct rte_eth_dev *dev,
+                                     struct rte_ether_addr *mc_addr_set,
+                                     uint32_t nb_mc_addr)
+{
+       struct axgbe_port *pdata = dev->data->dev_private;
+       struct axgbe_hw_features *hw_feat = &pdata->hw_feat;
+       uint32_t index = 1; /* 0 is always default mac */
+       uint32_t i;
+
+       if (nb_mc_addr > hw_feat->addn_mac) {
+               PMD_DRV_LOG(ERR, "Invalid Index %d\n", nb_mc_addr);
+               return -EINVAL;
+       }
+
+       /* clear unicast addresses */
+       for (i = 1; i < hw_feat->addn_mac; i++) {
+               if (rte_is_zero_ether_addr(&dev->data->mac_addrs[i]))
+                       continue;
+               memset(&dev->data->mac_addrs[i], 0,
+                      sizeof(struct rte_ether_addr));
+       }
+
+       while (nb_mc_addr--)
+               axgbe_set_mac_addn_addr(pdata, (u8 *)mc_addr_set++, index++);
+
+       return 0;
+}
+
+static int
+axgbe_dev_uc_hash_table_set(struct rte_eth_dev *dev,
+                           struct rte_ether_addr *mac_addr, uint8_t add)
+{
+       struct axgbe_port *pdata = dev->data->dev_private;
+       struct axgbe_hw_features *hw_feat = &pdata->hw_feat;
+
+       if (!hw_feat->hash_table_size) {
+               PMD_DRV_LOG(ERR, "MAC Hash Table not supported\n");
+               return -ENOTSUP;
+       }
+
+       axgbe_set_mac_hash_table(pdata, (u8 *)mac_addr, add);
+
+       if (pdata->uc_hash_mac_addr > 0) {
+               AXGMAC_IOWRITE_BITS(pdata, MAC_PFR, HPF, 1);
+               AXGMAC_IOWRITE_BITS(pdata, MAC_PFR, HUC, 1);
+       } else {
+               AXGMAC_IOWRITE_BITS(pdata, MAC_PFR, HPF, 0);
+               AXGMAC_IOWRITE_BITS(pdata, MAC_PFR, HUC, 0);
+       }
+       return 0;
+}
+
+static int
+axgbe_dev_uc_all_hash_table_set(struct rte_eth_dev *dev, uint8_t add)
+{
+       struct axgbe_port *pdata = dev->data->dev_private;
+       struct axgbe_hw_features *hw_feat = &pdata->hw_feat;
+       uint32_t index;
+
+       if (!hw_feat->hash_table_size) {
+               PMD_DRV_LOG(ERR, "MAC Hash Table not supported\n");
+               return -ENOTSUP;
+       }
+
+       for (index = 0; index < pdata->hash_table_count; index++) {
+               if (add)
+                       pdata->uc_hash_table[index] = ~0;
+               else
+                       pdata->uc_hash_table[index] = 0;
+
+               PMD_DRV_LOG(DEBUG, "%s MAC hash table at Index %#x\n",
+                           add ? "set" : "clear", index);
+
+               AXGMAC_IOWRITE(pdata, MAC_HTR(index),
+                              pdata->uc_hash_table[index]);
+       }
+
+       if (add) {
+               AXGMAC_IOWRITE_BITS(pdata, MAC_PFR, HPF, 1);
+               AXGMAC_IOWRITE_BITS(pdata, MAC_PFR, HUC, 1);
+       } else {
+               AXGMAC_IOWRITE_BITS(pdata, MAC_PFR, HPF, 0);
+               AXGMAC_IOWRITE_BITS(pdata, MAC_PFR, HUC, 0);
+       }
+       return 0;
+}
+
 /* return 0 means link status changed, -1 means not changed */
 static int
 axgbe_dev_link_update(struct rte_eth_dev *dev,
@@ -823,7 +977,8 @@ axgbe_dev_info_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *dev_info)
        dev_info->max_tx_queues = pdata->tx_ring_count;
        dev_info->min_rx_bufsize = AXGBE_RX_MIN_BUF_SIZE;
        dev_info->max_rx_pktlen = AXGBE_RX_MAX_BUF_SIZE;
-       dev_info->max_mac_addrs = AXGBE_MAX_MAC_ADDRS;
+       dev_info->max_mac_addrs = pdata->hw_feat.addn_mac + 1;
+       dev_info->max_hash_mac_addrs = pdata->hw_feat.hash_table_size;
        dev_info->speed_capa =  ETH_LINK_SPEED_10G;
 
        dev_info->rx_offload_capa =
@@ -859,6 +1014,84 @@ axgbe_dev_info_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *dev_info)
        return 0;
 }
 
+static int
+axgbe_flow_ctrl_get(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
+{
+       struct axgbe_port *pdata = dev->data->dev_private;
+       struct xgbe_fc_info fc = pdata->fc;
+       unsigned int reg, reg_val = 0;
+
+       reg = MAC_Q0TFCR;
+       reg_val = AXGMAC_IOREAD(pdata, reg);
+       fc.low_water[0] =  AXGMAC_MTL_IOREAD_BITS(pdata, 0, MTL_Q_RQFCR, RFA);
+       fc.high_water[0] =  AXGMAC_MTL_IOREAD_BITS(pdata, 0, MTL_Q_RQFCR, RFD);
+       fc.pause_time[0] = AXGMAC_GET_BITS(reg_val, MAC_Q0TFCR, PT);
+       fc.autoneg = pdata->pause_autoneg;
+
+       if (pdata->rx_pause && pdata->tx_pause)
+               fc.mode = RTE_FC_FULL;
+       else if (pdata->rx_pause)
+               fc.mode = RTE_FC_RX_PAUSE;
+       else if (pdata->tx_pause)
+               fc.mode = RTE_FC_TX_PAUSE;
+       else
+               fc.mode = RTE_FC_NONE;
+
+       fc_conf->high_water =  (1024 + (fc.low_water[0] << 9)) / 1024;
+       fc_conf->low_water =  (1024 + (fc.high_water[0] << 9)) / 1024;
+       fc_conf->pause_time = fc.pause_time[0];
+       fc_conf->send_xon = fc.send_xon;
+       fc_conf->mode = fc.mode;
+
+       return 0;
+}
+
+static int
+axgbe_flow_ctrl_set(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
+{
+       struct axgbe_port *pdata = dev->data->dev_private;
+       struct xgbe_fc_info fc = pdata->fc;
+       unsigned int reg, reg_val = 0;
+       reg = MAC_Q0TFCR;
+
+       pdata->pause_autoneg = fc_conf->autoneg;
+       pdata->phy.pause_autoneg = pdata->pause_autoneg;
+       fc.send_xon = fc_conf->send_xon;
+       AXGMAC_MTL_IOWRITE_BITS(pdata, 0, MTL_Q_RQFCR, RFA,
+                       AXGMAC_FLOW_CONTROL_VALUE(1024 * fc_conf->high_water));
+       AXGMAC_MTL_IOWRITE_BITS(pdata, 0, MTL_Q_RQFCR, RFD,
+                       AXGMAC_FLOW_CONTROL_VALUE(1024 * fc_conf->low_water));
+       AXGMAC_SET_BITS(reg_val, MAC_Q0TFCR, PT, fc_conf->pause_time);
+       AXGMAC_IOWRITE(pdata, reg, reg_val);
+       fc.mode = fc_conf->mode;
+
+       if (fc.mode == RTE_FC_FULL) {
+               pdata->tx_pause = 1;
+               pdata->rx_pause = 1;
+       } else if (fc.mode == RTE_FC_RX_PAUSE) {
+               pdata->tx_pause = 0;
+               pdata->rx_pause = 1;
+       } else if (fc.mode == RTE_FC_TX_PAUSE) {
+               pdata->tx_pause = 1;
+               pdata->rx_pause = 0;
+       } else {
+               pdata->tx_pause = 0;
+               pdata->rx_pause = 0;
+       }
+
+       if (pdata->tx_pause != (unsigned int)pdata->phy.tx_pause)
+               pdata->hw_if.config_tx_flow_control(pdata);
+
+       if (pdata->rx_pause != (unsigned int)pdata->phy.rx_pause)
+               pdata->hw_if.config_rx_flow_control(pdata);
+
+       pdata->hw_if.config_flow_control(pdata);
+       pdata->phy.tx_pause = pdata->tx_pause;
+       pdata->phy.rx_pause = pdata->rx_pause;
+
+       return 0;
+}
+
 static void axgbe_get_all_hw_features(struct axgbe_port *pdata)
 {
        unsigned int mac_hfr0, mac_hfr1, mac_hfr2;
@@ -1060,6 +1293,7 @@ eth_axgbe_dev_init(struct rte_eth_dev *eth_dev)
        struct axgbe_port *pdata;
        struct rte_pci_device *pci_dev;
        uint32_t reg, mac_lo, mac_hi;
+       uint32_t len;
        int ret;
 
        eth_dev->dev_ops = &axgbe_eth_dev_ops;
@@ -1128,12 +1362,25 @@ eth_axgbe_dev_init(struct rte_eth_dev *eth_dev)
        pdata->mac_addr.addr_bytes[4] = mac_hi & 0xff;
        pdata->mac_addr.addr_bytes[5] = (mac_hi >> 8)  &  0xff;
 
-       eth_dev->data->mac_addrs = rte_zmalloc("axgbe_mac_addr",
-                                              RTE_ETHER_ADDR_LEN, 0);
+       len = RTE_ETHER_ADDR_LEN * AXGBE_MAX_MAC_ADDRS;
+       eth_dev->data->mac_addrs = rte_zmalloc("axgbe_mac_addr", len, 0);
+
        if (!eth_dev->data->mac_addrs) {
                PMD_INIT_LOG(ERR,
-                            "Failed to alloc %u bytes needed to store MAC addr tbl",
-                            RTE_ETHER_ADDR_LEN);
+                            "Failed to alloc %u bytes needed to "
+                            "store MAC addresses", len);
+               return -ENOMEM;
+       }
+
+       /* Allocate memory for storing hash filter MAC addresses */
+       len = RTE_ETHER_ADDR_LEN * AXGBE_MAX_HASH_MAC_ADDRS;
+       eth_dev->data->hash_mac_addrs = rte_zmalloc("axgbe_hash_mac_addr",
+                                                   len, 0);
+
+       if (eth_dev->data->hash_mac_addrs == NULL) {
+               PMD_INIT_LOG(ERR,
+                            "Failed to allocate %d bytes needed to "
+                            "store MAC addresses", len);
                return -ENOMEM;
        }