net/sonic: Refactor duplicated code
authorFinn Thain <fthain@telegraphics.com.au>
Sat, 15 Feb 2020 21:03:32 +0000 (08:03 +1100)
committerDavid S. Miller <davem@davemloft.net>
Mon, 17 Feb 2020 03:48:21 +0000 (19:48 -0800)
No functional change.

Tested-by: Stan Johnson <userm57@yahoo.com>
Signed-off-by: Finn Thain <fthain@telegraphics.com.au>
Signed-off-by: David S. Miller <davem@davemloft.net>
drivers/net/ethernet/natsemi/jazzsonic.c
drivers/net/ethernet/natsemi/macsonic.c
drivers/net/ethernet/natsemi/sonic.c
drivers/net/ethernet/natsemi/sonic.h
drivers/net/ethernet/natsemi/xtsonic.c

index 51fa82b429a3cf348be4cc1d67903fd9a594a2c0..bfa0c0d39600750b19e72c1915680737e38d744a 100644 (file)
@@ -147,39 +147,12 @@ static int sonic_probe1(struct net_device *dev)
                dev->dev_addr[i*2+1] = val >> 8;
        }
 
-       err = -ENOMEM;
-
-       /* Initialize the device structure. */
-
        lp->dma_bitmode = SONIC_BITMODE32;
 
-       /* Allocate the entire chunk of memory for the descriptors.
-           Note that this cannot cross a 64K boundary. */
-       lp->descriptors = dma_alloc_coherent(lp->device,
-                                            SIZEOF_SONIC_DESC *
-                                            SONIC_BUS_SCALE(lp->dma_bitmode),
-                                            &lp->descriptors_laddr,
-                                            GFP_KERNEL);
-       if (lp->descriptors == NULL)
+       err = sonic_alloc_descriptors(dev);
+       if (err)
                goto out;
 
-       /* Now set up the pointers to point to the appropriate places */
-       lp->cda = lp->descriptors;
-       lp->tda = lp->cda + (SIZEOF_SONIC_CDA
-                            * SONIC_BUS_SCALE(lp->dma_bitmode));
-       lp->rda = lp->tda + (SIZEOF_SONIC_TD * SONIC_NUM_TDS
-                            * SONIC_BUS_SCALE(lp->dma_bitmode));
-       lp->rra = lp->rda + (SIZEOF_SONIC_RD * SONIC_NUM_RDS
-                            * SONIC_BUS_SCALE(lp->dma_bitmode));
-
-       lp->cda_laddr = lp->descriptors_laddr;
-       lp->tda_laddr = lp->cda_laddr + (SIZEOF_SONIC_CDA
-                            * SONIC_BUS_SCALE(lp->dma_bitmode));
-       lp->rda_laddr = lp->tda_laddr + (SIZEOF_SONIC_TD * SONIC_NUM_TDS
-                            * SONIC_BUS_SCALE(lp->dma_bitmode));
-       lp->rra_laddr = lp->rda_laddr + (SIZEOF_SONIC_RD * SONIC_NUM_RDS
-                            * SONIC_BUS_SCALE(lp->dma_bitmode));
-
        dev->netdev_ops = &sonic_netdev_ops;
        dev->watchdog_timeo = TX_TIMEOUT;
 
index 0937fc2a928ed50d168ffcc7870dda0aa0b5770f..0f4d0c25d626ceb92d7ed3dc4305f8eac6ef9fa4 100644 (file)
@@ -186,33 +186,10 @@ static const struct net_device_ops macsonic_netdev_ops = {
 static int macsonic_init(struct net_device *dev)
 {
        struct sonic_local* lp = netdev_priv(dev);
+       int err = sonic_alloc_descriptors(dev);
 
-       /* Allocate the entire chunk of memory for the descriptors.
-           Note that this cannot cross a 64K boundary. */
-       lp->descriptors = dma_alloc_coherent(lp->device,
-                                            SIZEOF_SONIC_DESC *
-                                            SONIC_BUS_SCALE(lp->dma_bitmode),
-                                            &lp->descriptors_laddr,
-                                            GFP_KERNEL);
-       if (lp->descriptors == NULL)
-               return -ENOMEM;
-
-       /* Now set up the pointers to point to the appropriate places */
-       lp->cda = lp->descriptors;
-       lp->tda = lp->cda + (SIZEOF_SONIC_CDA
-                            * SONIC_BUS_SCALE(lp->dma_bitmode));
-       lp->rda = lp->tda + (SIZEOF_SONIC_TD * SONIC_NUM_TDS
-                            * SONIC_BUS_SCALE(lp->dma_bitmode));
-       lp->rra = lp->rda + (SIZEOF_SONIC_RD * SONIC_NUM_RDS
-                            * SONIC_BUS_SCALE(lp->dma_bitmode));
-
-       lp->cda_laddr = lp->descriptors_laddr;
-       lp->tda_laddr = lp->cda_laddr + (SIZEOF_SONIC_CDA
-                            * SONIC_BUS_SCALE(lp->dma_bitmode));
-       lp->rda_laddr = lp->tda_laddr + (SIZEOF_SONIC_TD * SONIC_NUM_TDS
-                            * SONIC_BUS_SCALE(lp->dma_bitmode));
-       lp->rra_laddr = lp->rda_laddr + (SIZEOF_SONIC_RD * SONIC_NUM_RDS
-                            * SONIC_BUS_SCALE(lp->dma_bitmode));
+       if (err)
+               return err;
 
        dev->netdev_ops = &macsonic_netdev_ops;
        dev->watchdog_timeo = TX_TIMEOUT;
index e01273654f81586fc5b393af498d0dbe979f5f71..c066510b348efd829ee50726f5a54c0585bae57a 100644 (file)
@@ -50,6 +50,42 @@ static void sonic_msg_init(struct net_device *dev)
                netif_dbg(lp, drv, dev, "%s", version);
 }
 
+static int sonic_alloc_descriptors(struct net_device *dev)
+{
+       struct sonic_local *lp = netdev_priv(dev);
+
+       /* Allocate a chunk of memory for the descriptors. Note that this
+        * must not cross a 64K boundary. It is smaller than one page which
+        * means that page alignment is a sufficient condition.
+        */
+       lp->descriptors =
+               dma_alloc_coherent(lp->device,
+                                  SIZEOF_SONIC_DESC *
+                                  SONIC_BUS_SCALE(lp->dma_bitmode),
+                                  &lp->descriptors_laddr, GFP_KERNEL);
+
+       if (!lp->descriptors)
+               return -ENOMEM;
+
+       lp->cda = lp->descriptors;
+       lp->tda = lp->cda + SIZEOF_SONIC_CDA *
+                           SONIC_BUS_SCALE(lp->dma_bitmode);
+       lp->rda = lp->tda + SIZEOF_SONIC_TD * SONIC_NUM_TDS *
+                           SONIC_BUS_SCALE(lp->dma_bitmode);
+       lp->rra = lp->rda + SIZEOF_SONIC_RD * SONIC_NUM_RDS *
+                           SONIC_BUS_SCALE(lp->dma_bitmode);
+
+       lp->cda_laddr = lp->descriptors_laddr;
+       lp->tda_laddr = lp->cda_laddr + SIZEOF_SONIC_CDA *
+                                       SONIC_BUS_SCALE(lp->dma_bitmode);
+       lp->rda_laddr = lp->tda_laddr + SIZEOF_SONIC_TD * SONIC_NUM_TDS *
+                                       SONIC_BUS_SCALE(lp->dma_bitmode);
+       lp->rra_laddr = lp->rda_laddr + SIZEOF_SONIC_RD * SONIC_NUM_RDS *
+                                       SONIC_BUS_SCALE(lp->dma_bitmode);
+
+       return 0;
+}
+
 /*
  * Open/initialize the SONIC controller.
  *
index e0e4cba6f6f6fdeab2a60ea10916e7d8d688883d..053f12f5cf4a60a005cacf7a6204a54243a2788e 100644 (file)
@@ -342,6 +342,7 @@ static void sonic_multicast_list(struct net_device *dev);
 static int sonic_init(struct net_device *dev);
 static void sonic_tx_timeout(struct net_device *dev, unsigned int txqueue);
 static void sonic_msg_init(struct net_device *dev);
+static int sonic_alloc_descriptors(struct net_device *dev);
 
 /* Internal inlines for reading/writing DMA buffers.  Note that bus
    size and endianness matter here, whereas they don't for registers,
index e1b886e87a762ce1f006e135ab796872c9075b70..dda9ec7d9cee460e5c96de536f967c3d3724626e 100644 (file)
@@ -167,47 +167,11 @@ static int __init sonic_probe1(struct net_device *dev)
                dev->dev_addr[i*2+1] = val >> 8;
        }
 
-       /* Initialize the device structure. */
-
        lp->dma_bitmode = SONIC_BITMODE32;
 
-       /*
-        *  Allocate local private descriptor areas in uncached space.
-        *  The entire structure must be located within the same 64kb segment.
-        *  A simple way to ensure this is to allocate twice the
-        *  size of the structure -- given that the structure is
-        *  much less than 64 kB, at least one of the halves of
-        *  the allocated area will be contained entirely in 64 kB.
-        *  We also allocate extra space for a pointer to allow freeing
-        *  this structure later on (in xtsonic_cleanup_module()).
-        */
-       lp->descriptors = dma_alloc_coherent(lp->device,
-                                            SIZEOF_SONIC_DESC *
-                                            SONIC_BUS_SCALE(lp->dma_bitmode),
-                                            &lp->descriptors_laddr,
-                                            GFP_KERNEL);
-       if (lp->descriptors == NULL) {
-               err = -ENOMEM;
+       err = sonic_alloc_descriptors(dev);
+       if (err)
                goto out;
-       }
-
-       lp->cda = lp->descriptors;
-       lp->tda = lp->cda + (SIZEOF_SONIC_CDA
-                            * SONIC_BUS_SCALE(lp->dma_bitmode));
-       lp->rda = lp->tda + (SIZEOF_SONIC_TD * SONIC_NUM_TDS
-                            * SONIC_BUS_SCALE(lp->dma_bitmode));
-       lp->rra = lp->rda + (SIZEOF_SONIC_RD * SONIC_NUM_RDS
-                            * SONIC_BUS_SCALE(lp->dma_bitmode));
-
-       /* get the virtual dma address */
-
-       lp->cda_laddr = lp->descriptors_laddr;
-       lp->tda_laddr = lp->cda_laddr + (SIZEOF_SONIC_CDA
-                                        * SONIC_BUS_SCALE(lp->dma_bitmode));
-       lp->rda_laddr = lp->tda_laddr + (SIZEOF_SONIC_TD * SONIC_NUM_TDS
-                                        * SONIC_BUS_SCALE(lp->dma_bitmode));
-       lp->rra_laddr = lp->rda_laddr + (SIZEOF_SONIC_RD * SONIC_NUM_RDS
-                                        * SONIC_BUS_SCALE(lp->dma_bitmode));
 
        dev->netdev_ops         = &xtsonic_netdev_ops;
        dev->watchdog_timeo     = TX_TIMEOUT;