net: dsa: use ds->num_ports when possible
[linux-2.6-microblaze.git] / net / dsa / dsa2.c
index 75f5d1f..6e7b3e8 100644 (file)
@@ -98,7 +98,7 @@ static bool dsa_ds_find_port_dn(struct dsa_switch *ds,
 {
        u32 index;
 
-       for (index = 0; index < DSA_MAX_PORTS; index++)
+       for (index = 0; index < ds->num_ports; index++)
                if (ds->ports[index].dn == port)
                        return true;
        return false;
@@ -159,7 +159,7 @@ static int dsa_ds_complete(struct dsa_switch_tree *dst, struct dsa_switch *ds)
        u32 index;
        int err;
 
-       for (index = 0; index < DSA_MAX_PORTS; index++) {
+       for (index = 0; index < ds->num_ports; index++) {
                port = &ds->ports[index];
                if (!dsa_port_is_valid(port))
                        continue;
@@ -312,7 +312,7 @@ static int dsa_ds_apply(struct dsa_switch_tree *dst, struct dsa_switch *ds)
                        return err;
        }
 
-       for (index = 0; index < DSA_MAX_PORTS; index++) {
+       for (index = 0; index < ds->num_ports; index++) {
                port = &ds->ports[index];
                if (!dsa_port_is_valid(port))
                        continue;
@@ -344,7 +344,7 @@ static void dsa_ds_unapply(struct dsa_switch_tree *dst, struct dsa_switch *ds)
        struct dsa_port *port;
        u32 index;
 
-       for (index = 0; index < DSA_MAX_PORTS; index++) {
+       for (index = 0; index < ds->num_ports; index++) {
                port = &ds->ports[index];
                if (!dsa_port_is_valid(port))
                        continue;
@@ -475,7 +475,7 @@ static int dsa_ds_parse(struct dsa_switch_tree *dst, struct dsa_switch *ds)
        u32 index;
        int err;
 
-       for (index = 0; index < DSA_MAX_PORTS; index++) {
+       for (index = 0; index < ds->num_ports; index++) {
                port = &ds->ports[index];
                if (!dsa_port_is_valid(port))
                        continue;
@@ -529,7 +529,7 @@ static int dsa_parse_ports_dn(struct device_node *ports, struct dsa_switch *ds)
                if (err)
                        return err;
 
-               if (reg >= DSA_MAX_PORTS)
+               if (reg >= ds->num_ports)
                        return -EINVAL;
 
                ds->ports[reg].dn = port;
@@ -666,6 +666,22 @@ out:
        return err;
 }
 
+struct dsa_switch *dsa_switch_alloc(struct device *dev, size_t n)
+{
+       size_t size = sizeof(struct dsa_switch) + n * sizeof(struct dsa_port);
+       struct dsa_switch *ds;
+
+       ds = devm_kzalloc(dev, size, GFP_KERNEL);
+       if (!ds)
+               return NULL;
+
+       ds->dev = dev;
+       ds->num_ports = n;
+
+       return ds;
+}
+EXPORT_SYMBOL_GPL(dsa_switch_alloc);
+
 int dsa_register_switch(struct dsa_switch *ds, struct device *dev)
 {
        int err;