ethernet: ucc_geth: don't statically allocate eight ucc_geth_info
authorRasmus Villemoes <rasmus.villemoes@prevas.dk>
Tue, 19 Jan 2021 15:07:56 +0000 (16:07 +0100)
committerJakub Kicinski <kuba@kernel.org>
Thu, 21 Jan 2021 20:19:56 +0000 (12:19 -0800)
struct ucc_geth_info is somewhat large, and on systems with only one
or two UCC instances, that just wastes a few KB of memory. So
allocate and populate a chunk of memory at probe time instead of
initializing them all during driver init.

Note that the existing "ug_info == NULL" check was dead code, as the
address of some static array element can obviously never be NULL.

Signed-off-by: Rasmus Villemoes <rasmus.villemoes@prevas.dk>
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
drivers/net/ethernet/freescale/ucc_geth.c

index 65ef7ae..67b93d6 100644 (file)
@@ -157,8 +157,6 @@ static const struct ucc_geth_info ugeth_primary_info = {
        .riscRx = QE_RISC_ALLOCATION_RISC1_AND_RISC2,
 };
 
-static struct ucc_geth_info ugeth_info[8];
-
 #ifdef DEBUG
 static void mem_disp(u8 *addr, int size)
 {
@@ -3715,25 +3713,23 @@ static int ucc_geth_probe(struct platform_device* ofdev)
        if ((ucc_num < 0) || (ucc_num > 7))
                return -ENODEV;
 
-       ug_info = &ugeth_info[ucc_num];
-       if (ug_info == NULL) {
-               if (netif_msg_probe(&debug))
-                       pr_err("[%d] Missing additional data!\n", ucc_num);
-               return -ENODEV;
-       }
+       ug_info = kmalloc(sizeof(*ug_info), GFP_KERNEL);
+       if (ug_info == NULL)
+               return -ENOMEM;
+       memcpy(ug_info, &ugeth_primary_info, sizeof(*ug_info));
 
        ug_info->uf_info.ucc_num = ucc_num;
 
        err = ucc_geth_parse_clock(np, "rx", &ug_info->uf_info.rx_clock);
        if (err)
-               return err;
+               goto err_free_info;
        err = ucc_geth_parse_clock(np, "tx", &ug_info->uf_info.tx_clock);
        if (err)
-               return err;
+               goto err_free_info;
 
        err = of_address_to_resource(np, 0, &res);
        if (err)
-               return -EINVAL;
+               goto err_free_info;
 
        ug_info->uf_info.regs = res.start;
        ug_info->uf_info.irq = irq_of_parse_and_map(np, 0);
@@ -3746,7 +3742,7 @@ static int ucc_geth_probe(struct platform_device* ofdev)
                 */
                err = of_phy_register_fixed_link(np);
                if (err)
-                       return err;
+                       goto err_free_info;
                ug_info->phy_node = of_node_get(np);
        }
 
@@ -3877,6 +3873,8 @@ err_deregister_fixed_link:
                of_phy_deregister_fixed_link(np);
        of_node_put(ug_info->tbi_node);
        of_node_put(ug_info->phy_node);
+err_free_info:
+       kfree(ug_info);
 
        return err;
 }
@@ -3893,6 +3891,7 @@ static int ucc_geth_remove(struct platform_device* ofdev)
                of_phy_deregister_fixed_link(np);
        of_node_put(ugeth->ug_info->tbi_node);
        of_node_put(ugeth->ug_info->phy_node);
+       kfree(ugeth->ug_info);
        free_netdev(dev);
 
        return 0;
@@ -3921,17 +3920,10 @@ static struct platform_driver ucc_geth_driver = {
 
 static int __init ucc_geth_init(void)
 {
-       int i, ret;
-
        if (netif_msg_drv(&debug))
                pr_info(DRV_DESC "\n");
-       for (i = 0; i < 8; i++)
-               memcpy(&(ugeth_info[i]), &ugeth_primary_info,
-                      sizeof(ugeth_primary_info));
-
-       ret = platform_driver_register(&ucc_geth_driver);
 
-       return ret;
+       return platform_driver_register(&ucc_geth_driver);
 }
 
 static void __exit ucc_geth_exit(void)