2
0

073-v4.10-0001-net-bgmac-allocate-struct-bgmac-just-once-don-t-copy.patch 4.0 KB

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