aboutsummaryrefslogtreecommitdiffstats
path: root/net/dsa/dsa2.c
diff options
context:
space:
mode:
authorVivien Didelot <vivien.didelot@savoirfairelinux.com>2017-11-03 19:05:24 -0400
committerDavid S. Miller <davem@davemloft.net>2017-11-05 08:31:39 -0500
commit1ca28ec9abff927178b1ea9d6431e4c320145b10 (patch)
treeee3531e3e3d447b35c2a9cdf5f037014af5f9466 /net/dsa/dsa2.c
parent65254108b4655dc55e8d8f62ee895960085e73f4 (diff)
net: dsa: provide a find or new tree helper
Rename dsa_get_dst to dsa_tree_find since it doesn't increment the reference counter, rename dsa_add_dst to dsa_tree_alloc for symmetry with dsa_tree_free, and provide a convenient dsa_tree_touch function to find or allocate a new tree. Signed-off-by: Vivien Didelot <vivien.didelot@savoirfairelinux.com> Reviewed-by: Florian Fainelli <f.fainelli@gmail.com> Signed-off-by: David S. Miller <davem@davemloft.net>
Diffstat (limited to 'net/dsa/dsa2.c')
-rw-r--r--net/dsa/dsa2.c32
1 files changed, 21 insertions, 11 deletions
diff --git a/net/dsa/dsa2.c b/net/dsa/dsa2.c
index 609d92684505..bda222cfc02c 100644
--- a/net/dsa/dsa2.c
+++ b/net/dsa/dsa2.c
@@ -21,33 +21,35 @@
21 21
22#include "dsa_priv.h" 22#include "dsa_priv.h"
23 23
24static LIST_HEAD(dsa_switch_trees); 24static LIST_HEAD(dsa_tree_list);
25static DEFINE_MUTEX(dsa2_mutex); 25static DEFINE_MUTEX(dsa2_mutex);
26 26
27static const struct devlink_ops dsa_devlink_ops = { 27static const struct devlink_ops dsa_devlink_ops = {
28}; 28};
29 29
30static struct dsa_switch_tree *dsa_get_dst(unsigned int index) 30static struct dsa_switch_tree *dsa_tree_find(int index)
31{ 31{
32 struct dsa_switch_tree *dst; 32 struct dsa_switch_tree *dst;
33 33
34 list_for_each_entry(dst, &dsa_switch_trees, list) 34 list_for_each_entry(dst, &dsa_tree_list, list)
35 if (dst->index == index) 35 if (dst->index == index)
36 return dst; 36 return dst;
37 37
38 return NULL; 38 return NULL;
39} 39}
40 40
41static struct dsa_switch_tree *dsa_add_dst(unsigned int index) 41static struct dsa_switch_tree *dsa_tree_alloc(int index)
42{ 42{
43 struct dsa_switch_tree *dst; 43 struct dsa_switch_tree *dst;
44 44
45 dst = kzalloc(sizeof(*dst), GFP_KERNEL); 45 dst = kzalloc(sizeof(*dst), GFP_KERNEL);
46 if (!dst) 46 if (!dst)
47 return NULL; 47 return NULL;
48
48 dst->index = index; 49 dst->index = index;
50
49 INIT_LIST_HEAD(&dst->list); 51 INIT_LIST_HEAD(&dst->list);
50 list_add_tail(&dsa_switch_trees, &dst->list); 52 list_add_tail(&dsa_tree_list, &dst->list);
51 53
52 /* Initialize the reference counter to the number of switches, not 1 */ 54 /* Initialize the reference counter to the number of switches, not 1 */
53 kref_init(&dst->refcount); 55 kref_init(&dst->refcount);
@@ -62,6 +64,17 @@ static void dsa_tree_free(struct dsa_switch_tree *dst)
62 kfree(dst); 64 kfree(dst);
63} 65}
64 66
67static struct dsa_switch_tree *dsa_tree_touch(int index)
68{
69 struct dsa_switch_tree *dst;
70
71 dst = dsa_tree_find(index);
72 if (!dst)
73 dst = dsa_tree_alloc(index);
74
75 return dst;
76}
77
65static void dsa_tree_get(struct dsa_switch_tree *dst) 78static void dsa_tree_get(struct dsa_switch_tree *dst)
66{ 79{
67 kref_get(&dst->refcount); 80 kref_get(&dst->refcount);
@@ -745,12 +758,9 @@ static int _dsa_register_switch(struct dsa_switch *ds)
745 return err; 758 return err;
746 } 759 }
747 760
748 dst = dsa_get_dst(tree); 761 dst = dsa_tree_touch(tree);
749 if (!dst) { 762 if (!dst)
750 dst = dsa_add_dst(tree); 763 return -ENOMEM;
751 if (!dst)
752 return -ENOMEM;
753 }
754 764
755 if (dst->ds[index]) 765 if (dst->ds[index])
756 return -EBUSY; 766 return -EBUSY;