Received: by 2002:ac0:8c8e:0:0:0:0:0 with SMTP id r14csp594064ima; Wed, 6 Feb 2019 05:22:29 -0800 (PST) X-Google-Smtp-Source: AHgI3Ib0mdW55YFutjO9lZVWPumOBNGUFyhZlHsRhUTgueVYBMWCiZIbUPQSxdBRDKokuc5dKYR5 X-Received: by 2002:a63:4105:: with SMTP id o5mr3620791pga.396.1549459349909; Wed, 06 Feb 2019 05:22:29 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1549459349; cv=none; d=google.com; s=arc-20160816; b=jsi3EPRMEMcJY8r/55pjJ62Kwkd+fat3NkZsLOJoTAJSYqyUvQ6PqoI0p36G/XeHTg rJCW60SY3QFPD/nMKYldv1DR85ILY2JigcRedBkZRmSydNzREAkfe1kuDJXO15mh9lXi 68tzdrey4cc/iWnyLDvRKa7gEgbWXkuLpGQC1BKbznLWnbRYXdPMoOUlyGJDFKzvjBD8 pEMYdF2vyA3ynvKzpz+876TRl0WLCKzdLGlWLNfIqASgKFmayT4ApYzBy1VBA4ZHFhJC U8iUVr3G3yAMVAFD17TvYcljvZHyQafH9rRDlMNnYELKSq3fzEf4LVaeazE6RUW6Vz63 jjpw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=BWt768LyvahzuaAheFMC9EPO7fQPO63jOyLO6zdIpV4=; b=i6EVVjQ6Ns5Fg1B9RcKhIIFEaTwDX6d3NtyW0KgQZOyyANTqO990kFjMo4zHRn1+uS 2MwdLJJ+rundpOe1foxe6iI90PHAFu9b9T+DcM6sGiLk9Dmd/KZ52k+K9fLcDacHk/P3 W1J5did6ukHb0ybeWGdU2iKajEb36pI4u4AgspZaSEnCS/LdJg0NcA55+CkUXx6OeA2E 74UqZ7AzVmpaZIlNU/+RXGKHqpKGQNZXLvoB/+GDqms21D34zyc27jUsfn0elGQ+ZhP2 hvhlaoXAJCeQIR4U2bcKcCD4JwA9RcCt60l3h9S1BYQK71hQnNnam6Vq4V2BVQeKIfSU GQzA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id b3si6299311pld.282.2019.02.06.05.22.13; Wed, 06 Feb 2019 05:22:29 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731114AbfBFNVg (ORCPT + 99 others); Wed, 6 Feb 2019 08:21:36 -0500 Received: from mga07.intel.com ([134.134.136.100]:28027 "EHLO mga07.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730250AbfBFNRr (ORCPT ); Wed, 6 Feb 2019 08:17:47 -0500 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga005.fm.intel.com ([10.253.24.32]) by orsmga105.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 06 Feb 2019 05:17:45 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.58,340,1544515200"; d="scan'208";a="318109338" Received: from black.fi.intel.com ([10.237.72.28]) by fmsmga005.fm.intel.com with ESMTP; 06 Feb 2019 05:17:43 -0800 Received: by black.fi.intel.com (Postfix, from userid 1001) id 0D5136AD; Wed, 6 Feb 2019 15:17:39 +0200 (EET) From: Mika Westerberg To: linux-kernel@vger.kernel.org Cc: Michael Jamet , Yehezkel Bernat , Andreas Noever , Lukas Wunner , "David S . Miller" , Mika Westerberg , Andy Shevchenko , netdev@vger.kernel.org Subject: [PATCH v2 13/28] thunderbolt: Add helper function to iterate from one port to another Date: Wed, 6 Feb 2019 16:17:23 +0300 Message-Id: <20190206131738.43696-14-mika.westerberg@linux.intel.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190206131738.43696-1-mika.westerberg@linux.intel.com> References: <20190206131738.43696-1-mika.westerberg@linux.intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org We need to be able to walk from one port to another when we are creating paths where there are multiple switches between two ports. For this reason introduce a new function tb_port_get_next() and a new macro tb_for_each_port(). Signed-off-by: Mika Westerberg --- drivers/thunderbolt/switch.c | 60 ++++++++++++++++++++++++++++++++++++ drivers/thunderbolt/tb.h | 6 ++++ 2 files changed, 66 insertions(+) diff --git a/drivers/thunderbolt/switch.c b/drivers/thunderbolt/switch.c index 320f64ebe8b8..23b6bae8362e 100644 --- a/drivers/thunderbolt/switch.c +++ b/drivers/thunderbolt/switch.c @@ -683,6 +683,66 @@ void tb_port_release_out_hopid(struct tb_port *port, int hopid) ida_simple_remove(&port->out_hopids, hopid); } +/** + * tb_port_get_next() - Return next port for given port + * @start: Start port of the walk + * @end: End port of the walk + * @prev: Previous port (%NULL if this is the first) + * + * This function can be used to walk from one port to another if they + * are connected through zero or more switches. If the @prev is dual + * link port, the function follows that link and returns another end on + * that same link. + * + * If the walk cannot be continued, returns %NULL. + * + * Domain tb->lock must be held when this function is called. + */ +struct tb_port *tb_port_get_next(struct tb_port *start, struct tb_port *end, + struct tb_port *prev) +{ + struct tb_port *port, *next; + + if (!prev) + return start; + + if (prev->sw == end->sw) { + if (prev != end) + return end; + return NULL; + } + + /* Switch back to use primary links for walking */ + if (prev->dual_link_port && prev->link_nr) + port = prev->dual_link_port; + else + port = prev; + + if (start->sw->config.depth < end->sw->config.depth) { + if (port->remote && + port->remote->sw->config.depth > port->sw->config.depth) + next = port->remote; + else + next = tb_port_at(tb_route(end->sw), port->sw); + } else if (start->sw->config.depth > end->sw->config.depth) { + if (tb_is_upstream_port(port)) + next = port->remote; + else + next = tb_upstream_port(port->sw); + } else { + /* Must be the same switch then */ + if (start->sw != end->sw) + return NULL; + return end; + } + + /* If prev was dual link return another end of that link then */ + if (next->dual_link_port && next->link_nr != prev->link_nr) + return next->dual_link_port; + + return next; +} + /** * tb_pci_port_enable() - Enable PCIe adapter port * @port: PCIe port to enable diff --git a/drivers/thunderbolt/tb.h b/drivers/thunderbolt/tb.h index bfa1cee193fd..683725915ff7 100644 --- a/drivers/thunderbolt/tb.h +++ b/drivers/thunderbolt/tb.h @@ -447,6 +447,12 @@ int tb_port_alloc_in_hopid(struct tb_port *port, int hopid, int max_hopid); void tb_port_release_in_hopid(struct tb_port *port, int hopid); int tb_port_alloc_out_hopid(struct tb_port *port, int hopid, int max_hopid); void tb_port_release_out_hopid(struct tb_port *port, int hopid); +struct tb_port *tb_port_get_next(struct tb_port *start, struct tb_port *end, + struct tb_port *prev); + +#define tb_for_each_port(port, start, end) \ + for (port = tb_port_get_next(start, end, NULL); port; \ + port = tb_port_get_next(start, end, port)) int tb_switch_find_vse_cap(struct tb_switch *sw, enum tb_switch_vse_cap vsec); int tb_port_find_cap(struct tb_port *port, enum tb_port_cap cap); -- 2.20.1