net: dsa: Have the switch driver allocate there own private memory
authorAndrew Lunn <andrew@lunn.ch>
Wed, 13 Apr 2016 00:40:40 +0000 (02:40 +0200)
committerDavid S. Miller <davem@davemloft.net>
Wed, 13 Apr 2016 22:15:23 +0000 (18:15 -0400)
Now the switch devices have a dev pointer, make use of it for allocating
the drivers private data structures using a devm_kzalloc().

Signed-off-by: Andrew Lunn <andrew@lunn.ch>
Acked-by: Florian Fainelli <f.fainelli@gmail.com>
Tested-by: Vivien Didelot <vivien.didelot@savoirfairelinux.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
drivers/net/dsa/bcm_sf2.c
drivers/net/dsa/mv88e6060.c
drivers/net/dsa/mv88e6123.c
drivers/net/dsa/mv88e6131.c
drivers/net/dsa/mv88e6171.c
drivers/net/dsa/mv88e6352.c
drivers/net/dsa/mv88e6xxx.c
drivers/net/dsa/mv88e6xxx.h
include/net/dsa.h
net/dsa/dsa.c

index 18a79579141f433408826f7edf05504c8c02005b..7d62802a66d591884f4381d06bfb9ca9aff302d7 100644 (file)
@@ -136,8 +136,15 @@ static int bcm_sf2_sw_get_sset_count(struct dsa_switch *ds)
 }
 
 static char *bcm_sf2_sw_probe(struct device *dsa_dev, struct device *host_dev,
-                             int sw_addr)
+                             int sw_addr, void **_priv)
 {
+       struct bcm_sf2_priv *priv;
+
+       priv = devm_kzalloc(dsa_dev, sizeof(*priv), GFP_KERNEL);
+       if (!priv)
+               return NULL;
+       *_priv = priv;
+
        return "Broadcom Starfighter 2";
 }
 
@@ -1363,7 +1370,6 @@ static int bcm_sf2_sw_set_wol(struct dsa_switch *ds, int port,
 
 static struct dsa_switch_driver bcm_sf2_switch_driver = {
        .tag_protocol           = DSA_TAG_PROTO_BRCM,
-       .priv_size              = sizeof(struct bcm_sf2_priv),
        .probe                  = bcm_sf2_sw_probe,
        .setup                  = bcm_sf2_sw_setup,
        .set_addr               = bcm_sf2_sw_set_addr,
index 34d0f9fe19db5530bed57d83a236042a31b1fbd6..41195f1417f1f9f0f9aa77f1c8c971cdae8517a6 100644 (file)
@@ -58,11 +58,12 @@ static int reg_write(struct dsa_switch *ds, int addr, int reg, u16 val)
        })
 
 static char *mv88e6060_probe(struct device *dsa_dev, struct device *host_dev,
-                            int sw_addr)
+                            int sw_addr, void **priv)
 {
        struct mii_bus *bus = dsa_host_dev_to_mii_bus(host_dev);
        int ret;
 
+       *priv = NULL;
        if (bus == NULL)
                return NULL;
 
index ede4c6f0b31ad63252f3ea803b8b7d5ec643d2d9..bcab3ef22448bfcfdcf42d6e4498edeaba1e7999 100644 (file)
@@ -30,10 +30,20 @@ static const struct mv88e6xxx_switch_id mv88e6123_table[] = {
 };
 
 static char *mv88e6123_probe(struct device *dsa_dev, struct device *host_dev,
-                            int sw_addr)
+                            int sw_addr, void **priv)
 {
-       return mv88e6xxx_lookup_name(host_dev, sw_addr, mv88e6123_table,
+       struct mv88e6xxx_priv_state *ps;
+       char *name;
+
+       name = mv88e6xxx_lookup_name(host_dev, sw_addr, mv88e6123_table,
                                     ARRAY_SIZE(mv88e6123_table));
+       if (name) {
+               ps = devm_kzalloc(dsa_dev, sizeof(*ps), GFP_KERNEL);
+               if (!ps)
+                       return NULL;
+               *priv = ps;
+       }
+       return name;
 }
 
 static int mv88e6123_setup_global(struct dsa_switch *ds)
@@ -74,6 +84,8 @@ static int mv88e6123_setup(struct dsa_switch *ds)
        struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
        int ret;
 
+       ps->ds = ds;
+
        ret = mv88e6xxx_setup_common(ds);
        if (ret < 0)
                return ret;
@@ -103,7 +115,6 @@ static int mv88e6123_setup(struct dsa_switch *ds)
 
 struct dsa_switch_driver mv88e6123_switch_driver = {
        .tag_protocol           = DSA_TAG_PROTO_EDSA,
-       .priv_size              = sizeof(struct mv88e6xxx_priv_state),
        .probe                  = mv88e6123_probe,
        .setup                  = mv88e6123_setup,
        .set_addr               = mv88e6xxx_set_addr_indirect,
index bfadfd2cbb8d53ebe22d10ecc1da54b85c206261..b9f9b009f65a26897bfd479724cd42e001114880 100644 (file)
@@ -26,10 +26,20 @@ static const struct mv88e6xxx_switch_id mv88e6131_table[] = {
 };
 
 static char *mv88e6131_probe(struct device *dsa_dev, struct device *host_dev,
-                            int sw_addr)
+                            int sw_addr, void **priv)
 {
-       return mv88e6xxx_lookup_name(host_dev, sw_addr, mv88e6131_table,
+       struct mv88e6xxx_priv_state *ps;
+       char *name;
+
+       name = mv88e6xxx_lookup_name(host_dev, sw_addr, mv88e6131_table,
                                     ARRAY_SIZE(mv88e6131_table));
+       if (name) {
+               ps = devm_kzalloc(dsa_dev, sizeof(*ps), GFP_KERNEL);
+               if (!ps)
+                       return NULL;
+               *priv = ps;
+       }
+       return name;
 }
 
 static int mv88e6131_setup_global(struct dsa_switch *ds)
@@ -92,6 +102,8 @@ static int mv88e6131_setup(struct dsa_switch *ds)
        struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
        int ret;
 
+       ps->ds = ds;
+
        ret = mv88e6xxx_setup_common(ds);
        if (ret < 0)
                return ret;
@@ -160,7 +172,6 @@ mv88e6131_phy_write(struct dsa_switch *ds,
 
 struct dsa_switch_driver mv88e6131_switch_driver = {
        .tag_protocol           = DSA_TAG_PROTO_DSA,
-       .priv_size              = sizeof(struct mv88e6xxx_priv_state),
        .probe                  = mv88e6131_probe,
        .setup                  = mv88e6131_setup,
        .set_addr               = mv88e6xxx_set_addr_direct,
index fb35d3ac1644c6313dd9b64d3b551212feb70f8c..15200928ceccb3274817ff5769ad94b37e521b20 100644 (file)
@@ -25,10 +25,20 @@ static const struct mv88e6xxx_switch_id mv88e6171_table[] = {
 };
 
 static char *mv88e6171_probe(struct device *dsa_dev, struct device *host_dev,
-                            int sw_addr)
+                            int sw_addr, void **priv)
 {
-       return mv88e6xxx_lookup_name(host_dev, sw_addr, mv88e6171_table,
+       struct mv88e6xxx_priv_state *ps;
+       char *name;
+
+       name = mv88e6xxx_lookup_name(host_dev, sw_addr, mv88e6171_table,
                                     ARRAY_SIZE(mv88e6171_table));
+               if (name) {
+               ps = devm_kzalloc(dsa_dev, sizeof(*ps), GFP_KERNEL);
+               if (!ps)
+                       return NULL;
+               *priv = ps;
+       }
+       return name;
 }
 
 static int mv88e6171_setup_global(struct dsa_switch *ds)
@@ -70,6 +80,8 @@ static int mv88e6171_setup(struct dsa_switch *ds)
        struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
        int ret;
 
+       ps->ds = ds;
+
        ret = mv88e6xxx_setup_common(ds);
        if (ret < 0)
                return ret;
@@ -89,7 +101,6 @@ static int mv88e6171_setup(struct dsa_switch *ds)
 
 struct dsa_switch_driver mv88e6171_switch_driver = {
        .tag_protocol           = DSA_TAG_PROTO_EDSA,
-       .priv_size              = sizeof(struct mv88e6xxx_priv_state),
        .probe                  = mv88e6171_probe,
        .setup                  = mv88e6171_setup,
        .set_addr               = mv88e6xxx_set_addr_indirect,
index 577ab2cfa944df336f251d18d555a4b7b2632d61..7081a78a67e102a927be0f38d9012fa7a4a2f7ff 100644 (file)
@@ -38,10 +38,20 @@ static const struct mv88e6xxx_switch_id mv88e6352_table[] = {
 };
 
 static char *mv88e6352_probe(struct device *dsa_dev, struct device *host_dev,
-                            int sw_addr)
+                            int sw_addr, void **priv)
 {
-       return mv88e6xxx_lookup_name(host_dev, sw_addr, mv88e6352_table,
+       struct mv88e6xxx_priv_state *ps;
+       char *name;
+
+       name = mv88e6xxx_lookup_name(host_dev, sw_addr, mv88e6352_table,
                                     ARRAY_SIZE(mv88e6352_table));
+       if (name) {
+               ps = devm_kzalloc(dsa_dev, sizeof(*ps), GFP_KERNEL);
+               if (!ps)
+                       return NULL;
+               *priv = ps;
+       }
+       return name;
 }
 
 static int mv88e6352_setup_global(struct dsa_switch *ds)
@@ -82,6 +92,8 @@ static int mv88e6352_setup(struct dsa_switch *ds)
        struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
        int ret;
 
+       ps->ds = ds;
+
        ret = mv88e6xxx_setup_common(ds);
        if (ret < 0)
                return ret;
@@ -303,7 +315,6 @@ static int mv88e6352_set_eeprom(struct dsa_switch *ds,
 
 struct dsa_switch_driver mv88e6352_switch_driver = {
        .tag_protocol           = DSA_TAG_PROTO_EDSA,
-       .priv_size              = sizeof(struct mv88e6xxx_priv_state),
        .probe                  = mv88e6352_probe,
        .setup                  = mv88e6352_setup,
        .set_addr               = mv88e6xxx_set_addr_indirect,
index 62320fca6712204cdc1828eaf36273a63af224bb..085fc4a49eb217b2b7dc59b4902dc30646286404 100644 (file)
@@ -281,7 +281,7 @@ static void mv88e6xxx_ppu_reenable_work(struct work_struct *ugly)
 
        ps = container_of(ugly, struct mv88e6xxx_priv_state, ppu_work);
        if (mutex_trylock(&ps->ppu_mutex)) {
-               struct dsa_switch *ds = ((struct dsa_switch *)ps) - 1;
+               struct dsa_switch *ds = ps->ds;
 
                if (mv88e6xxx_ppu_enable(ds) == 0)
                        ps->ppu_disabled = 0;
@@ -2322,7 +2322,7 @@ static void mv88e6xxx_bridge_work(struct work_struct *work)
        int port;
 
        ps = container_of(work, struct mv88e6xxx_priv_state, bridge_work);
-       ds = ((struct dsa_switch *)ps) - 1;
+       ds = ps->ds;
 
        mutex_lock(&ps->smi_mutex);
 
@@ -2670,6 +2670,8 @@ int mv88e6xxx_setup_common(struct dsa_switch *ds)
 {
        struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
 
+       ps->ds = ds;
+
        mutex_init(&ps->smi_mutex);
 
        ps->id = REG_READ(REG_PORT(0), PORT_SWITCH_ID) & 0xfff0;
index 236bcaa606e73d2e7dea13295c0570ead096ea8c..0322e3e0e7d9d757b9eba28f0f8d59ac1bb25d96 100644 (file)
@@ -397,6 +397,9 @@ struct mv88e6xxx_priv_port {
 };
 
 struct mv88e6xxx_priv_state {
+       /* The dsa_switch this private structure is related to */
+       struct dsa_switch *ds;
+
        /* When using multi-chip addressing, this mutex protects
         * access to the indirect access registers.  (In single-chip
         * mode, this mutex is effectively useless.)
index 0f9f6f38f2554800525d3d0616c09dac3ab871c2..7bc7bd9b5dedd171197949fbd61699046de21f70 100644 (file)
@@ -129,6 +129,12 @@ struct dsa_switch {
        struct dsa_switch_tree  *dst;
        int                     index;
 
+       /*
+        * Give the switch driver somewhere to hang its private data
+        * structure.
+        */
+       void *priv;
+
        /*
         * Tagging protocol understood by this switch
         */
@@ -213,7 +219,7 @@ struct dsa_switch_driver {
         * Probing and setup.
         */
        char    *(*probe)(struct device *dsa_dev, struct device *host_dev,
-                         int sw_addr);
+                         int sw_addr, void **priv);
        int     (*setup)(struct dsa_switch *ds);
        int     (*set_addr)(struct dsa_switch *ds, u8 *addr);
        u32     (*get_phy_flags)(struct dsa_switch *ds, int port);
@@ -342,7 +348,7 @@ struct mii_bus *dsa_host_dev_to_mii_bus(struct device *dev);
 
 static inline void *ds_to_priv(struct dsa_switch *ds)
 {
-       return (void *)(ds + 1);
+       return ds->priv;
 }
 
 static inline bool dsa_uses_tagged_protocol(struct dsa_switch_tree *dst)
index c06275311cb23e0b3a3435210253608ca137cce5..7ef8a92a9e395e4bb3d13f3a60381ce1de26e592 100644 (file)
@@ -52,7 +52,7 @@ EXPORT_SYMBOL_GPL(unregister_switch_driver);
 
 static struct dsa_switch_driver *
 dsa_switch_probe(struct device *parent, struct device *host_dev, int sw_addr,
-                char **_name)
+                char **_name, void **priv)
 {
        struct dsa_switch_driver *ret;
        struct list_head *list;
@@ -67,7 +67,7 @@ dsa_switch_probe(struct device *parent, struct device *host_dev, int sw_addr,
 
                drv = list_entry(list, struct dsa_switch_driver, list);
 
-               name = drv->probe(parent, host_dev, sw_addr);
+               name = drv->probe(parent, host_dev, sw_addr, priv);
                if (name != NULL) {
                        ret = drv;
                        break;
@@ -384,11 +384,12 @@ dsa_switch_setup(struct dsa_switch_tree *dst, int index,
        struct dsa_switch *ds;
        int ret;
        char *name;
+       void *priv;
 
        /*
         * Probe for switch model.
         */
-       drv = dsa_switch_probe(parent, host_dev, pd->sw_addr, &name);
+       drv = dsa_switch_probe(parent, host_dev, pd->sw_addr, &name, &priv);
        if (drv == NULL) {
                netdev_err(dst->master_netdev, "[%d]: could not detect attached switch\n",
                           index);
@@ -409,6 +410,7 @@ dsa_switch_setup(struct dsa_switch_tree *dst, int index,
        ds->index = index;
        ds->pd = pd;
        ds->drv = drv;
+       ds->priv = priv;
        ds->tag_protocol = drv->tag_protocol;
        ds->master_dev = host_dev;