Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

net: dsa: Have the switch driver allocate there own private memory

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>

authored by

Andrew Lunn and committed by
David S. Miller
7543a6d5 bbb8d793

+86 -22
+8 -2
drivers/net/dsa/bcm_sf2.c
··· 136 136 } 137 137 138 138 static char *bcm_sf2_sw_probe(struct device *dsa_dev, struct device *host_dev, 139 - int sw_addr) 139 + int sw_addr, void **_priv) 140 140 { 141 + struct bcm_sf2_priv *priv; 142 + 143 + priv = devm_kzalloc(dsa_dev, sizeof(*priv), GFP_KERNEL); 144 + if (!priv) 145 + return NULL; 146 + *_priv = priv; 147 + 141 148 return "Broadcom Starfighter 2"; 142 149 } 143 150 ··· 1370 1363 1371 1364 static struct dsa_switch_driver bcm_sf2_switch_driver = { 1372 1365 .tag_protocol = DSA_TAG_PROTO_BRCM, 1373 - .priv_size = sizeof(struct bcm_sf2_priv), 1374 1366 .probe = bcm_sf2_sw_probe, 1375 1367 .setup = bcm_sf2_sw_setup, 1376 1368 .set_addr = bcm_sf2_sw_set_addr,
+2 -1
drivers/net/dsa/mv88e6060.c
··· 58 58 }) 59 59 60 60 static char *mv88e6060_probe(struct device *dsa_dev, struct device *host_dev, 61 - int sw_addr) 61 + int sw_addr, void **priv) 62 62 { 63 63 struct mii_bus *bus = dsa_host_dev_to_mii_bus(host_dev); 64 64 int ret; 65 65 66 + *priv = NULL; 66 67 if (bus == NULL) 67 68 return NULL; 68 69
+14 -3
drivers/net/dsa/mv88e6123.c
··· 30 30 }; 31 31 32 32 static char *mv88e6123_probe(struct device *dsa_dev, struct device *host_dev, 33 - int sw_addr) 33 + int sw_addr, void **priv) 34 34 { 35 - return mv88e6xxx_lookup_name(host_dev, sw_addr, mv88e6123_table, 35 + struct mv88e6xxx_priv_state *ps; 36 + char *name; 37 + 38 + name = mv88e6xxx_lookup_name(host_dev, sw_addr, mv88e6123_table, 36 39 ARRAY_SIZE(mv88e6123_table)); 40 + if (name) { 41 + ps = devm_kzalloc(dsa_dev, sizeof(*ps), GFP_KERNEL); 42 + if (!ps) 43 + return NULL; 44 + *priv = ps; 45 + } 46 + return name; 37 47 } 38 48 39 49 static int mv88e6123_setup_global(struct dsa_switch *ds) ··· 84 74 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); 85 75 int ret; 86 76 77 + ps->ds = ds; 78 + 87 79 ret = mv88e6xxx_setup_common(ds); 88 80 if (ret < 0) 89 81 return ret; ··· 115 103 116 104 struct dsa_switch_driver mv88e6123_switch_driver = { 117 105 .tag_protocol = DSA_TAG_PROTO_EDSA, 118 - .priv_size = sizeof(struct mv88e6xxx_priv_state), 119 106 .probe = mv88e6123_probe, 120 107 .setup = mv88e6123_setup, 121 108 .set_addr = mv88e6xxx_set_addr_indirect,
+14 -3
drivers/net/dsa/mv88e6131.c
··· 26 26 }; 27 27 28 28 static char *mv88e6131_probe(struct device *dsa_dev, struct device *host_dev, 29 - int sw_addr) 29 + int sw_addr, void **priv) 30 30 { 31 - return mv88e6xxx_lookup_name(host_dev, sw_addr, mv88e6131_table, 31 + struct mv88e6xxx_priv_state *ps; 32 + char *name; 33 + 34 + name = mv88e6xxx_lookup_name(host_dev, sw_addr, mv88e6131_table, 32 35 ARRAY_SIZE(mv88e6131_table)); 36 + if (name) { 37 + ps = devm_kzalloc(dsa_dev, sizeof(*ps), GFP_KERNEL); 38 + if (!ps) 39 + return NULL; 40 + *priv = ps; 41 + } 42 + return name; 33 43 } 34 44 35 45 static int mv88e6131_setup_global(struct dsa_switch *ds) ··· 101 91 { 102 92 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); 103 93 int ret; 94 + 95 + ps->ds = ds; 104 96 105 97 ret = mv88e6xxx_setup_common(ds); 106 98 if (ret < 0) ··· 172 160 173 161 struct dsa_switch_driver mv88e6131_switch_driver = { 174 162 .tag_protocol = DSA_TAG_PROTO_DSA, 175 - .priv_size = sizeof(struct mv88e6xxx_priv_state), 176 163 .probe = mv88e6131_probe, 177 164 .setup = mv88e6131_setup, 178 165 .set_addr = mv88e6xxx_set_addr_direct,
+14 -3
drivers/net/dsa/mv88e6171.c
··· 25 25 }; 26 26 27 27 static char *mv88e6171_probe(struct device *dsa_dev, struct device *host_dev, 28 - int sw_addr) 28 + int sw_addr, void **priv) 29 29 { 30 - return mv88e6xxx_lookup_name(host_dev, sw_addr, mv88e6171_table, 30 + struct mv88e6xxx_priv_state *ps; 31 + char *name; 32 + 33 + name = mv88e6xxx_lookup_name(host_dev, sw_addr, mv88e6171_table, 31 34 ARRAY_SIZE(mv88e6171_table)); 35 + if (name) { 36 + ps = devm_kzalloc(dsa_dev, sizeof(*ps), GFP_KERNEL); 37 + if (!ps) 38 + return NULL; 39 + *priv = ps; 40 + } 41 + return name; 32 42 } 33 43 34 44 static int mv88e6171_setup_global(struct dsa_switch *ds) ··· 80 70 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); 81 71 int ret; 82 72 73 + ps->ds = ds; 74 + 83 75 ret = mv88e6xxx_setup_common(ds); 84 76 if (ret < 0) 85 77 return ret; ··· 101 89 102 90 struct dsa_switch_driver mv88e6171_switch_driver = { 103 91 .tag_protocol = DSA_TAG_PROTO_EDSA, 104 - .priv_size = sizeof(struct mv88e6xxx_priv_state), 105 92 .probe = mv88e6171_probe, 106 93 .setup = mv88e6171_setup, 107 94 .set_addr = mv88e6xxx_set_addr_indirect,
+14 -3
drivers/net/dsa/mv88e6352.c
··· 38 38 }; 39 39 40 40 static char *mv88e6352_probe(struct device *dsa_dev, struct device *host_dev, 41 - int sw_addr) 41 + int sw_addr, void **priv) 42 42 { 43 - return mv88e6xxx_lookup_name(host_dev, sw_addr, mv88e6352_table, 43 + struct mv88e6xxx_priv_state *ps; 44 + char *name; 45 + 46 + name = mv88e6xxx_lookup_name(host_dev, sw_addr, mv88e6352_table, 44 47 ARRAY_SIZE(mv88e6352_table)); 48 + if (name) { 49 + ps = devm_kzalloc(dsa_dev, sizeof(*ps), GFP_KERNEL); 50 + if (!ps) 51 + return NULL; 52 + *priv = ps; 53 + } 54 + return name; 45 55 } 46 56 47 57 static int mv88e6352_setup_global(struct dsa_switch *ds) ··· 91 81 { 92 82 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); 93 83 int ret; 84 + 85 + ps->ds = ds; 94 86 95 87 ret = mv88e6xxx_setup_common(ds); 96 88 if (ret < 0) ··· 315 303 316 304 struct dsa_switch_driver mv88e6352_switch_driver = { 317 305 .tag_protocol = DSA_TAG_PROTO_EDSA, 318 - .priv_size = sizeof(struct mv88e6xxx_priv_state), 319 306 .probe = mv88e6352_probe, 320 307 .setup = mv88e6352_setup, 321 308 .set_addr = mv88e6xxx_set_addr_indirect,
+4 -2
drivers/net/dsa/mv88e6xxx.c
··· 281 281 282 282 ps = container_of(ugly, struct mv88e6xxx_priv_state, ppu_work); 283 283 if (mutex_trylock(&ps->ppu_mutex)) { 284 - struct dsa_switch *ds = ((struct dsa_switch *)ps) - 1; 284 + struct dsa_switch *ds = ps->ds; 285 285 286 286 if (mv88e6xxx_ppu_enable(ds) == 0) 287 287 ps->ppu_disabled = 0; ··· 2322 2322 int port; 2323 2323 2324 2324 ps = container_of(work, struct mv88e6xxx_priv_state, bridge_work); 2325 - ds = ((struct dsa_switch *)ps) - 1; 2325 + ds = ps->ds; 2326 2326 2327 2327 mutex_lock(&ps->smi_mutex); 2328 2328 ··· 2669 2669 int mv88e6xxx_setup_common(struct dsa_switch *ds) 2670 2670 { 2671 2671 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds); 2672 + 2673 + ps->ds = ds; 2672 2674 2673 2675 mutex_init(&ps->smi_mutex); 2674 2676
+3
drivers/net/dsa/mv88e6xxx.h
··· 397 397 }; 398 398 399 399 struct mv88e6xxx_priv_state { 400 + /* The dsa_switch this private structure is related to */ 401 + struct dsa_switch *ds; 402 + 400 403 /* When using multi-chip addressing, this mutex protects 401 404 * access to the indirect access registers. (In single-chip 402 405 * mode, this mutex is effectively useless.)
+8 -2
include/net/dsa.h
··· 130 130 int index; 131 131 132 132 /* 133 + * Give the switch driver somewhere to hang its private data 134 + * structure. 135 + */ 136 + void *priv; 137 + 138 + /* 133 139 * Tagging protocol understood by this switch 134 140 */ 135 141 enum dsa_tag_protocol tag_protocol; ··· 219 213 * Probing and setup. 220 214 */ 221 215 char *(*probe)(struct device *dsa_dev, struct device *host_dev, 222 - int sw_addr); 216 + int sw_addr, void **priv); 223 217 int (*setup)(struct dsa_switch *ds); 224 218 int (*set_addr)(struct dsa_switch *ds, u8 *addr); 225 219 u32 (*get_phy_flags)(struct dsa_switch *ds, int port); ··· 348 342 349 343 static inline void *ds_to_priv(struct dsa_switch *ds) 350 344 { 351 - return (void *)(ds + 1); 345 + return ds->priv; 352 346 } 353 347 354 348 static inline bool dsa_uses_tagged_protocol(struct dsa_switch_tree *dst)
+5 -3
net/dsa/dsa.c
··· 52 52 53 53 static struct dsa_switch_driver * 54 54 dsa_switch_probe(struct device *parent, struct device *host_dev, int sw_addr, 55 - char **_name) 55 + char **_name, void **priv) 56 56 { 57 57 struct dsa_switch_driver *ret; 58 58 struct list_head *list; ··· 67 67 68 68 drv = list_entry(list, struct dsa_switch_driver, list); 69 69 70 - name = drv->probe(parent, host_dev, sw_addr); 70 + name = drv->probe(parent, host_dev, sw_addr, priv); 71 71 if (name != NULL) { 72 72 ret = drv; 73 73 break; ··· 384 384 struct dsa_switch *ds; 385 385 int ret; 386 386 char *name; 387 + void *priv; 387 388 388 389 /* 389 390 * Probe for switch model. 390 391 */ 391 - drv = dsa_switch_probe(parent, host_dev, pd->sw_addr, &name); 392 + drv = dsa_switch_probe(parent, host_dev, pd->sw_addr, &name, &priv); 392 393 if (drv == NULL) { 393 394 netdev_err(dst->master_netdev, "[%d]: could not detect attached switch\n", 394 395 index); ··· 410 409 ds->index = index; 411 410 ds->pd = pd; 412 411 ds->drv = drv; 412 + ds->priv = priv; 413 413 ds->tag_protocol = drv->tag_protocol; 414 414 ds->master_dev = host_dev; 415 415