]> git.baikalelectronics.ru Git - kernel.git/commitdiff
net: hns3: add new function hclge_get_speed_bit()
authorGuangbin Huang <huangguangbin2@huawei.com>
Sat, 28 Aug 2021 06:55:18 +0000 (14:55 +0800)
committerDavid S. Miller <davem@davemloft.net>
Sat, 28 Aug 2021 10:20:05 +0000 (11:20 +0100)
Currently, function hclge_check_port_speed() uses switch/case statement
to get speed bit according to speed. To reuse this part of code and
improve code readability and maintainability, add a new function
hclge_get_speed_bit() to get speed bit according to map relationship
of speed and speed bit defined in array speed_bit_map.

Signed-off-by: Guangbin Huang <huangguangbin2@huawei.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
drivers/net/ethernet/hisilicon/hns3/hns3pf/hclge_main.c
drivers/net/ethernet/hisilicon/hns3/hns3pf/hclge_main.h

index 750390c2533afd0122ec0db0f43ebe3fee4c63a6..a1dcdf76fdfe4a7a3f69cffec5e145d970239842 100644 (file)
@@ -993,44 +993,43 @@ static int hclge_parse_speed(u8 speed_cmd, u32 *speed)
        return 0;
 }
 
+static const struct hclge_speed_bit_map speed_bit_map[] = {
+       {HCLGE_MAC_SPEED_10M, HCLGE_SUPPORT_10M_BIT},
+       {HCLGE_MAC_SPEED_100M, HCLGE_SUPPORT_100M_BIT},
+       {HCLGE_MAC_SPEED_1G, HCLGE_SUPPORT_1G_BIT},
+       {HCLGE_MAC_SPEED_10G, HCLGE_SUPPORT_10G_BIT},
+       {HCLGE_MAC_SPEED_25G, HCLGE_SUPPORT_25G_BIT},
+       {HCLGE_MAC_SPEED_40G, HCLGE_SUPPORT_40G_BIT},
+       {HCLGE_MAC_SPEED_50G, HCLGE_SUPPORT_50G_BIT},
+       {HCLGE_MAC_SPEED_100G, HCLGE_SUPPORT_100G_BIT},
+       {HCLGE_MAC_SPEED_200G, HCLGE_SUPPORT_200G_BIT},
+};
+
+static int hclge_get_speed_bit(u32 speed, u32 *speed_bit)
+{
+       u16 i;
+
+       for (i = 0; i < ARRAY_SIZE(speed_bit_map); i++) {
+               if (speed == speed_bit_map[i].speed) {
+                       *speed_bit = speed_bit_map[i].speed_bit;
+                       return 0;
+               }
+       }
+
+       return -EINVAL;
+}
+
 static int hclge_check_port_speed(struct hnae3_handle *handle, u32 speed)
 {
        struct hclge_vport *vport = hclge_get_vport(handle);
        struct hclge_dev *hdev = vport->back;
        u32 speed_ability = hdev->hw.mac.speed_ability;
        u32 speed_bit = 0;
+       int ret;
 
-       switch (speed) {
-       case HCLGE_MAC_SPEED_10M:
-               speed_bit = HCLGE_SUPPORT_10M_BIT;
-               break;
-       case HCLGE_MAC_SPEED_100M:
-               speed_bit = HCLGE_SUPPORT_100M_BIT;
-               break;
-       case HCLGE_MAC_SPEED_1G:
-               speed_bit = HCLGE_SUPPORT_1G_BIT;
-               break;
-       case HCLGE_MAC_SPEED_10G:
-               speed_bit = HCLGE_SUPPORT_10G_BIT;
-               break;
-       case HCLGE_MAC_SPEED_25G:
-               speed_bit = HCLGE_SUPPORT_25G_BIT;
-               break;
-       case HCLGE_MAC_SPEED_40G:
-               speed_bit = HCLGE_SUPPORT_40G_BIT;
-               break;
-       case HCLGE_MAC_SPEED_50G:
-               speed_bit = HCLGE_SUPPORT_50G_BIT;
-               break;
-       case HCLGE_MAC_SPEED_100G:
-               speed_bit = HCLGE_SUPPORT_100G_BIT;
-               break;
-       case HCLGE_MAC_SPEED_200G:
-               speed_bit = HCLGE_SUPPORT_200G_BIT;
-               break;
-       default:
-               return -EINVAL;
-       }
+       ret = hclge_get_speed_bit(speed, &speed_bit);
+       if (ret)
+               return ret;
 
        if (speed_bit & speed_ability)
                return 0;
index 9ca7bb26912a84496f1b6ce2c13f1484d903ccde..de6afbcbfbac48e956a7bb73e7f6e885aa14f659 100644 (file)
@@ -1058,6 +1058,11 @@ struct hclge_vport {
        struct list_head vlan_list;     /* Store VF vlan table */
 };
 
+struct hclge_speed_bit_map {
+       u32 speed;
+       u32 speed_bit;
+};
+
 int hclge_set_vport_promisc_mode(struct hclge_vport *vport, bool en_uc_pmc,
                                 bool en_mc_pmc, bool en_bc_pmc);
 int hclge_add_uc_addr_common(struct hclge_vport *vport,