544a074a783034cfa919ffc6fdfc4ff0e18cf48f
[openwrt/staging/ynezz.git] /
1 From 34a5102c3235c470a6c77fba16cb971964d9c136 Mon Sep 17 00:00:00 2001
2 From: =?UTF-8?q?Rafa=C5=82=20Mi=C5=82ecki?= <rafal@milecki.pl>
3 Date: Tue, 31 Jan 2017 19:37:54 +0100
4 Subject: [PATCH 1/3] net: bgmac: allocate struct bgmac just once & don't copy
5 it
6 MIME-Version: 1.0
7 Content-Type: text/plain; charset=UTF-8
8 Content-Transfer-Encoding: 8bit
9
10 So far were were allocating struct bgmac in 3 places: platform code,
11 bcma code and shared bgmac_enet_probe function. The reason for this was
12 bgmac_enet_probe:
13 1) Requiring early-filled struct bgmac
14 2) Calling alloc_etherdev on its own in order to use netdev_priv later
15
16 This solution got few drawbacks:
17 1) Was duplicating allocating code
18 2) Required copying early-filled struct
19 3) Resulted in platform/bcma code having access only to unused struct
20
21 Solve this situation by simply extracting some probe code into the new
22 bgmac_alloc function.
23
24 Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
25 Reviewed-by: Florian Fainelli <f.fainelli@gmail.com>
26 Signed-off-by: David S. Miller <davem@davemloft.net>
27 ---
28 drivers/net/ethernet/broadcom/bgmac-bcma.c | 4 +---
29 drivers/net/ethernet/broadcom/bgmac-platform.c | 2 +-
30 drivers/net/ethernet/broadcom/bgmac.c | 25 +++++++++++++++++--------
31 drivers/net/ethernet/broadcom/bgmac.h | 3 ++-
32 4 files changed, 21 insertions(+), 13 deletions(-)
33
34 --- a/drivers/net/ethernet/broadcom/bgmac-bcma.c
35 +++ b/drivers/net/ethernet/broadcom/bgmac-bcma.c
36 @@ -99,12 +99,11 @@ static int bgmac_probe(struct bcma_devic
37 u8 *mac;
38 int err;
39
40 - bgmac = kzalloc(sizeof(*bgmac), GFP_KERNEL);
41 + bgmac = bgmac_alloc(&core->dev);
42 if (!bgmac)
43 return -ENOMEM;
44
45 bgmac->bcma.core = core;
46 - bgmac->dev = &core->dev;
47 bgmac->dma_dev = core->dma_dev;
48 bgmac->irq = core->irq;
49
50 @@ -285,7 +284,6 @@ static int bgmac_probe(struct bcma_devic
51 err1:
52 bcma_mdio_mii_unregister(bgmac->mii_bus);
53 err:
54 - kfree(bgmac);
55 bcma_set_drvdata(core, NULL);
56
57 return err;
58 --- a/drivers/net/ethernet/broadcom/bgmac-platform.c
59 +++ b/drivers/net/ethernet/broadcom/bgmac-platform.c
60 @@ -93,7 +93,7 @@ static int bgmac_probe(struct platform_d
61 struct resource *regs;
62 const u8 *mac_addr;
63
64 - bgmac = devm_kzalloc(&pdev->dev, sizeof(*bgmac), GFP_KERNEL);
65 + bgmac = bgmac_alloc(&pdev->dev);
66 if (!bgmac)
67 return -ENOMEM;
68
69 --- a/drivers/net/ethernet/broadcom/bgmac.c
70 +++ b/drivers/net/ethernet/broadcom/bgmac.c
71 @@ -1475,22 +1475,32 @@ static int bgmac_phy_connect(struct bgma
72 return 0;
73 }
74
75 -int bgmac_enet_probe(struct bgmac *info)
76 +struct bgmac *bgmac_alloc(struct device *dev)
77 {
78 struct net_device *net_dev;
79 struct bgmac *bgmac;
80 - int err;
81
82 /* Allocation and references */
83 - net_dev = alloc_etherdev(sizeof(*bgmac));
84 + net_dev = devm_alloc_etherdev(dev, sizeof(*bgmac));
85 if (!net_dev)
86 - return -ENOMEM;
87 + return NULL;
88
89 net_dev->netdev_ops = &bgmac_netdev_ops;
90 net_dev->ethtool_ops = &bgmac_ethtool_ops;
91 +
92 bgmac = netdev_priv(net_dev);
93 - memcpy(bgmac, info, sizeof(*bgmac));
94 + bgmac->dev = dev;
95 bgmac->net_dev = net_dev;
96 +
97 + return bgmac;
98 +}
99 +EXPORT_SYMBOL_GPL(bgmac_alloc);
100 +
101 +int bgmac_enet_probe(struct bgmac *bgmac)
102 +{
103 + struct net_device *net_dev = bgmac->net_dev;
104 + int err;
105 +
106 net_dev->irq = bgmac->irq;
107 SET_NETDEV_DEV(net_dev, bgmac->dev);
108
109 @@ -1517,7 +1527,7 @@ int bgmac_enet_probe(struct bgmac *info)
110 err = bgmac_dma_alloc(bgmac);
111 if (err) {
112 dev_err(bgmac->dev, "Unable to alloc memory for DMA\n");
113 - goto err_netdev_free;
114 + goto err_out;
115 }
116
117 bgmac->int_mask = BGMAC_IS_ERRMASK | BGMAC_IS_RX | BGMAC_IS_TX_MASK;
118 @@ -1553,8 +1563,7 @@ err_phy_disconnect:
119 phy_disconnect(net_dev->phydev);
120 err_dma_free:
121 bgmac_dma_free(bgmac);
122 -err_netdev_free:
123 - free_netdev(net_dev);
124 +err_out:
125
126 return err;
127 }
128 --- a/drivers/net/ethernet/broadcom/bgmac.h
129 +++ b/drivers/net/ethernet/broadcom/bgmac.h
130 @@ -515,7 +515,8 @@ struct bgmac {
131 u32 set);
132 };
133
134 -int bgmac_enet_probe(struct bgmac *info);
135 +struct bgmac *bgmac_alloc(struct device *dev);
136 +int bgmac_enet_probe(struct bgmac *bgmac);
137 void bgmac_enet_remove(struct bgmac *bgmac);
138
139 struct mii_bus *bcma_mdio_mii_register(struct bcma_device *core, u8 phyaddr);