*/
 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))
 
 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;
 
 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);
                }
        }
 
  */
 void tb_switch_remove(struct tb_switch *sw)
 {
-       int i;
+       struct tb_port *port;
 
        if (sw->rpm) {
                pm_runtime_get_sync(&sw->dev);
        }
 
        /* 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;
                }
        }
 
  */
 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;
                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");
 
        /*
                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;
 
 
 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);
 
        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);
                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);
        }
 }
 
  */
 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);
 }
 
 /**
  */
 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;
 
 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;
 }
 
 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;
 }
 
 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) {
 
 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)
 
 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) {