]> asedeno.scripts.mit.edu Git - linux.git/commitdiff
thunderbolt: Add helper macro to iterate over switch ports
authorMika Westerberg <mika.westerberg@linux.intel.com>
Mon, 30 Sep 2019 11:07:22 +0000 (14:07 +0300)
committerMika Westerberg <mika.westerberg@linux.intel.com>
Fri, 1 Nov 2019 11:32:00 +0000 (14:32 +0300)
There are quite many places in the driver where we iterate over each
port in the switch. To make it bit more convenient, add a macro that can
be used to iterate over each port and convert existing call sites to use it.

This is based on code by Lukas Wunner.

No functional changes.

Signed-off-by: Mika Westerberg <mika.westerberg@linux.intel.com>
drivers/thunderbolt/icm.c
drivers/thunderbolt/switch.c
drivers/thunderbolt/tb.c
drivers/thunderbolt/tb.h
drivers/thunderbolt/xdomain.c

index 245588f691e79017da5291f142ec5784a08be21b..24625880692e9f49626cc7f6947f2aec910d766d 100644 (file)
@@ -1893,14 +1893,12 @@ static int icm_suspend(struct tb *tb)
  */
 static void icm_unplug_children(struct tb_switch *sw)
 {
-       unsigned int i;
+       struct tb_port *port;
 
        if (tb_route(sw))
                sw->is_unplugged = true;
 
-       for (i = 1; i <= sw->config.max_port_number; i++) {
-               struct tb_port *port = &sw->ports[i];
-
+       tb_switch_for_each_port(sw, port) {
                if (port->xdomain)
                        port->xdomain->is_unplugged = true;
                else if (tb_port_has_remote(port))
@@ -1936,11 +1934,9 @@ static void remove_unplugged_switch(struct tb_switch *sw)
 
 static void icm_free_unplugged_children(struct tb_switch *sw)
 {
-       unsigned int i;
-
-       for (i = 1; i <= sw->config.max_port_number; i++) {
-               struct tb_port *port = &sw->ports[i];
+       struct tb_port *port;
 
+       tb_switch_for_each_port(sw, port) {
                if (port->xdomain && port->xdomain->is_unplugged) {
                        tb_xdomain_remove(port->xdomain);
                        port->xdomain = NULL;
index c6bfdcaf7973520015f96e8937a7ef618044a0b7..849681500daa9f0c56c9c10351a2cc1bf47a02ee 100644 (file)
@@ -1400,14 +1400,14 @@ static const struct attribute_group *switch_groups[] = {
 static void tb_switch_release(struct device *dev)
 {
        struct tb_switch *sw = tb_to_switch(dev);
-       int i;
+       struct tb_port *port;
 
        dma_port_free(sw->dma_port);
 
-       for (i = 1; i <= sw->config.max_port_number; i++) {
-               if (!sw->ports[i].disabled) {
-                       ida_destroy(&sw->ports[i].in_hopids);
-                       ida_destroy(&sw->ports[i].out_hopids);
+       tb_switch_for_each_port(sw, port) {
+               if (!port->disabled) {
+                       ida_destroy(&port->in_hopids);
+                       ida_destroy(&port->out_hopids);
                }
        }
 
@@ -1869,7 +1869,7 @@ int tb_switch_add(struct tb_switch *sw)
  */
 void tb_switch_remove(struct tb_switch *sw)
 {
-       int i;
+       struct tb_port *port;
 
        if (sw->rpm) {
                pm_runtime_get_sync(&sw->dev);
@@ -1877,13 +1877,13 @@ void tb_switch_remove(struct tb_switch *sw)
        }
 
        /* port 0 is the switch itself and never has a remote */
-       for (i = 1; i <= sw->config.max_port_number; i++) {
-               if (tb_port_has_remote(&sw->ports[i])) {
-                       tb_switch_remove(sw->ports[i].remote->sw);
-                       sw->ports[i].remote = NULL;
-               } else if (sw->ports[i].xdomain) {
-                       tb_xdomain_remove(sw->ports[i].xdomain);
-                       sw->ports[i].xdomain = NULL;
+       tb_switch_for_each_port(sw, port) {
+               if (tb_port_has_remote(port)) {
+                       tb_switch_remove(port->remote->sw);
+                       port->remote = NULL;
+               } else if (port->xdomain) {
+                       tb_xdomain_remove(port->xdomain);
+                       port->xdomain = NULL;
                }
        }
 
@@ -1903,7 +1903,8 @@ void tb_switch_remove(struct tb_switch *sw)
  */
 void tb_sw_set_unplugged(struct tb_switch *sw)
 {
-       int i;
+       struct tb_port *port;
+
        if (sw == sw->tb->root_switch) {
                tb_sw_WARN(sw, "cannot unplug root switch\n");
                return;
@@ -1913,17 +1914,19 @@ void tb_sw_set_unplugged(struct tb_switch *sw)
                return;
        }
        sw->is_unplugged = true;
-       for (i = 0; i <= sw->config.max_port_number; i++) {
-               if (tb_port_has_remote(&sw->ports[i]))
-                       tb_sw_set_unplugged(sw->ports[i].remote->sw);
-               else if (sw->ports[i].xdomain)
-                       sw->ports[i].xdomain->is_unplugged = true;
+       tb_switch_for_each_port(sw, port) {
+               if (tb_port_has_remote(port))
+                       tb_sw_set_unplugged(port->remote->sw);
+               else if (port->xdomain)
+                       port->xdomain->is_unplugged = true;
        }
 }
 
 int tb_switch_resume(struct tb_switch *sw)
 {
-       int i, err;
+       struct tb_port *port;
+       int err;
+
        tb_sw_dbg(sw, "resuming switch\n");
 
        /*
@@ -1971,9 +1974,7 @@ int tb_switch_resume(struct tb_switch *sw)
                return err;
 
        /* check for surviving downstream switches */
-       for (i = 1; i <= sw->config.max_port_number; i++) {
-               struct tb_port *port = &sw->ports[i];
-
+       tb_switch_for_each_port(sw, port) {
                if (!tb_port_has_remote(port) && !port->xdomain)
                        continue;
 
@@ -1997,14 +1998,16 @@ int tb_switch_resume(struct tb_switch *sw)
 
 void tb_switch_suspend(struct tb_switch *sw)
 {
-       int i, err;
+       struct tb_port *port;
+       int err;
+
        err = tb_plug_events_active(sw, false);
        if (err)
                return;
 
-       for (i = 1; i <= sw->config.max_port_number; i++) {
-               if (tb_port_has_remote(&sw->ports[i]))
-                       tb_switch_suspend(sw->ports[i].remote->sw);
+       tb_switch_for_each_port(sw, port) {
+               if (tb_port_has_remote(port))
+                       tb_switch_suspend(port->remote->sw);
        }
 
        tb_lc_set_sleep(sw);
index 1f7a9e1cc09c4993443efeb9ef3c7d8f28c719ad..49589b38ff12f4dd8357b61a28c7a962bfeab7b3 100644 (file)
@@ -61,12 +61,10 @@ static void tb_discover_tunnels(struct tb_switch *sw)
        struct tb *tb = sw->tb;
        struct tb_cm *tcm = tb_priv(tb);
        struct tb_port *port;
-       int i;
 
-       for (i = 1; i <= sw->config.max_port_number; i++) {
+       tb_switch_for_each_port(sw, port) {
                struct tb_tunnel *tunnel = NULL;
 
-               port = &sw->ports[i];
                switch (port->config.type) {
                case TB_TYPE_DP_HDMI_IN:
                        tunnel = tb_tunnel_discover_dp(tb, port);
@@ -95,9 +93,9 @@ static void tb_discover_tunnels(struct tb_switch *sw)
                list_add_tail(&tunnel->list, &tcm->tunnel_list);
        }
 
-       for (i = 1; i <= sw->config.max_port_number; i++) {
-               if (tb_port_has_remote(&sw->ports[i]))
-                       tb_discover_tunnels(sw->ports[i].remote->sw);
+       tb_switch_for_each_port(sw, port) {
+               if (tb_port_has_remote(port))
+                       tb_discover_tunnels(port->remote->sw);
        }
 }
 
@@ -130,9 +128,10 @@ static void tb_scan_port(struct tb_port *port);
  */
 static void tb_scan_switch(struct tb_switch *sw)
 {
-       int i;
-       for (i = 1; i <= sw->config.max_port_number; i++)
-               tb_scan_port(&sw->ports[i]);
+       struct tb_port *port;
+
+       tb_switch_for_each_port(sw, port)
+               tb_scan_port(port);
 }
 
 /**
@@ -263,10 +262,9 @@ static void tb_free_invalid_tunnels(struct tb *tb)
  */
 static void tb_free_unplugged_children(struct tb_switch *sw)
 {
-       int i;
-       for (i = 1; i <= sw->config.max_port_number; i++) {
-               struct tb_port *port = &sw->ports[i];
+       struct tb_port *port;
 
+       tb_switch_for_each_port(sw, port) {
                if (!tb_port_has_remote(port))
                        continue;
 
@@ -289,10 +287,13 @@ static void tb_free_unplugged_children(struct tb_switch *sw)
 static struct tb_port *tb_find_port(struct tb_switch *sw,
                                    enum tb_port_type type)
 {
-       int i;
-       for (i = 1; i <= sw->config.max_port_number; i++)
-               if (sw->ports[i].config.type == type)
-                       return &sw->ports[i];
+       struct tb_port *port;
+
+       tb_switch_for_each_port(sw, port) {
+               if (port->config.type == type)
+                       return port;
+       }
+
        return NULL;
 }
 
@@ -304,18 +305,18 @@ static struct tb_port *tb_find_port(struct tb_switch *sw,
 static struct tb_port *tb_find_unused_port(struct tb_switch *sw,
                                           enum tb_port_type type)
 {
-       int i;
+       struct tb_port *port;
 
-       for (i = 1; i <= sw->config.max_port_number; i++) {
-               if (tb_is_upstream_port(&sw->ports[i]))
+       tb_switch_for_each_port(sw, port) {
+               if (tb_is_upstream_port(port))
                        continue;
-               if (sw->ports[i].config.type != type)
+               if (port->config.type != type)
                        continue;
-               if (!sw->ports[i].cap_adap)
+               if (port->cap_adap)
                        continue;
-               if (tb_port_is_enabled(&sw->ports[i]))
+               if (tb_port_is_enabled(port))
                        continue;
-               return &sw->ports[i];
+               return port;
        }
        return NULL;
 }
@@ -734,11 +735,10 @@ static int tb_resume_noirq(struct tb *tb)
 
 static int tb_free_unplugged_xdomains(struct tb_switch *sw)
 {
-       int i, ret = 0;
-
-       for (i = 1; i <= sw->config.max_port_number; i++) {
-               struct tb_port *port = &sw->ports[i];
+       struct tb_port *port;
+       int ret = 0;
 
+       tb_switch_for_each_port(sw, port) {
                if (tb_is_upstream_port(port))
                        continue;
                if (port->xdomain && port->xdomain->is_unplugged) {
index 455ca490ea871ad98d346d2c1f5966ab107c0ac2..4c77d5264660b8660f95526d63d34a688f8f5b56 100644 (file)
@@ -530,6 +530,17 @@ struct tb_switch *tb_switch_find_by_link_depth(struct tb *tb, u8 link,
 struct tb_switch *tb_switch_find_by_uuid(struct tb *tb, const uuid_t *uuid);
 struct tb_switch *tb_switch_find_by_route(struct tb *tb, u64 route);
 
+/**
+ * tb_switch_for_each_port() - Iterate over each switch port
+ * @sw: Switch whose ports to iterate
+ * @p: Port used as iterator
+ *
+ * Iterates over each switch port skipping the control port (port %0).
+ */
+#define tb_switch_for_each_port(sw, p)                                 \
+       for ((p) = &(sw)->ports[1];                                     \
+            (p) <= &(sw)->ports[(sw)->config.max_port_number]; (p)++)
+
 static inline struct tb_switch *tb_switch_get(struct tb_switch *sw)
 {
        if (sw)
index 4e17a7c7bf0ae58857493fed470107dda00620b6..880d784398a379529b3d865f54d27bbce2651472 100644 (file)
@@ -1404,10 +1404,9 @@ struct tb_xdomain_lookup {
 static struct tb_xdomain *switch_find_xdomain(struct tb_switch *sw,
        const struct tb_xdomain_lookup *lookup)
 {
-       int i;
+       struct tb_port *port;
 
-       for (i = 1; i <= sw->config.max_port_number; i++) {
-               struct tb_port *port = &sw->ports[i];
+       tb_switch_for_each_port(sw, port) {
                struct tb_xdomain *xd;
 
                if (port->xdomain) {