linux-pci.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Andreas Noever <andreas.noever@gmail.com>
To: linux-kernel@vger.kernel.org,
	Matthew Garrett <matthew.garrett@nebula.com>,
	Greg KH <greg@kroah.com>, Bjorn Helgaas <bhelgaas@google.com>,
	linux-pci@vger.kernel.org
Cc: Andreas Noever <andreas.noever@gmail.com>
Subject: [PATCH v3 06/15] thunderbolt: Add thunderbolt capability handling
Date: Mon, 26 May 2014 17:18:03 +0200	[thread overview]
Message-ID: <1401117492-2870-7-git-send-email-andreas.noever@gmail.com> (raw)
In-Reply-To: <1401117492-2870-1-git-send-email-andreas.noever@gmail.com>

Thunderbolt config areas contain capability lists similar to those found
on pci devices. This patch introduces a tb_find_cap utility method to
search for capabilities.

Signed-off-by: Andreas Noever <andreas.noever@gmail.com>
---
 drivers/thunderbolt/Makefile |   2 +-
 drivers/thunderbolt/cap.c    | 116 +++++++++++++++++++++++++++++++++++++++++++
 drivers/thunderbolt/tb.h     |   2 +
 3 files changed, 119 insertions(+), 1 deletion(-)
 create mode 100644 drivers/thunderbolt/cap.c

diff --git a/drivers/thunderbolt/Makefile b/drivers/thunderbolt/Makefile
index 4ac18d9..617b314 100644
--- a/drivers/thunderbolt/Makefile
+++ b/drivers/thunderbolt/Makefile
@@ -1,3 +1,3 @@
 obj-${CONFIG_THUNDERBOLT} := thunderbolt.o
-thunderbolt-objs := nhi.o ctl.o tb.o switch.o
+thunderbolt-objs := nhi.o ctl.o tb.o switch.o cap.o
 
diff --git a/drivers/thunderbolt/cap.c b/drivers/thunderbolt/cap.c
new file mode 100644
index 0000000..a7b47e7
--- /dev/null
+++ b/drivers/thunderbolt/cap.c
@@ -0,0 +1,116 @@
+/*
+ * Thunderbolt Cactus Ridge driver - capabilities lookup
+ *
+ * Copyright (c) 2014 Andreas Noever <andreas.noever@gmail.com>
+ */
+
+#include <linux/slab.h>
+#include <linux/errno.h>
+
+#include "tb.h"
+
+
+struct tb_cap_any {
+	union {
+		struct tb_cap_basic basic;
+		struct tb_cap_extended_short extended_short;
+		struct tb_cap_extended_long extended_long;
+	};
+} __packed;
+
+static bool tb_cap_is_basic(struct tb_cap_any *cap)
+{
+	/* basic.cap is u8. This checks only the lower 8 bit of cap. */
+	return cap->basic.cap != 5;
+}
+
+static bool tb_cap_is_long(struct tb_cap_any *cap)
+{
+	return !tb_cap_is_basic(cap)
+	       && cap->extended_short.next == 0
+	       && cap->extended_short.length == 0;
+}
+
+static enum tb_cap tb_cap(struct tb_cap_any *cap)
+{
+	if (tb_cap_is_basic(cap))
+		return cap->basic.cap;
+	else
+		/* extended_short/long have cap at the same offset. */
+		return cap->extended_short.cap;
+}
+
+static u32 tb_cap_next(struct tb_cap_any *cap, u32 offset)
+{
+	int next;
+	if (offset == 1) {
+		/*
+		 * The first pointer is part of the switch header and always
+		 * a simple pointer.
+		 */
+		next = cap->basic.next;
+	} else {
+		/*
+		 * Somehow Intel decided to use 3 different types of capability
+		 * headers. It is not like anyone could have predicted that
+		 * single byte offsets are not enough...
+		 */
+		if (tb_cap_is_basic(cap))
+			next = cap->basic.next;
+		else if (!tb_cap_is_long(cap))
+			next = cap->extended_short.next;
+		else
+			next = cap->extended_long.next;
+	}
+	/*
+	 * "Hey, we could terminate some capability lists with a null offset
+	 *  and others with a pointer to the last element." - "Great idea!"
+	 */
+	if (next == offset)
+		return 0;
+	return next;
+}
+
+/**
+ * tb_find_cap() - find a capability
+ *
+ * Return: Returns a positive offset if the capability was found and 0 if not.
+ * Returns an error code on failure.
+ */
+int tb_find_cap(struct tb_port *port, enum tb_cfg_space space, enum tb_cap cap)
+{
+	u32 offset = 1;
+	struct tb_cap_any header;
+	int res;
+	int retries = 10;
+	while (retries--) {
+		res = tb_port_read(port, &header, space, offset, 1);
+		if (res) {
+			/* Intel needs some help with linked lists. */
+			if (space == TB_CFG_PORT && offset == 0xa
+			    && port->config.type == TB_TYPE_DP_HDMI_OUT) {
+				offset = 0x39;
+				continue;
+			}
+			return res;
+		}
+		if (offset != 1) {
+			if (tb_cap(&header) == cap)
+				return offset;
+			if (tb_cap_is_long(&header)) {
+				/* tb_cap_extended_long is 2 dwords */
+				res = tb_port_read(port, &header, space,
+						   offset, 2);
+				if (res)
+					return res;
+			}
+		}
+		offset = tb_cap_next(&header, offset);
+		if (!offset)
+			return 0;
+	}
+	tb_port_WARN(port,
+		     "run out of retries while looking for cap %#x in config space %d, last offset: %#x\n",
+		     cap, space, offset);
+	return -EIO;
+}
diff --git a/drivers/thunderbolt/tb.h b/drivers/thunderbolt/tb.h
index 389dbb4..38e4c23 100644
--- a/drivers/thunderbolt/tb.h
+++ b/drivers/thunderbolt/tb.h
@@ -159,6 +159,8 @@ void thunderbolt_shutdown_and_free(struct tb *tb);
 struct tb_switch *tb_switch_alloc(struct tb *tb, u64 route);
 void tb_switch_free(struct tb_switch *sw);
 
+int tb_find_cap(struct tb_port *port, enum tb_cfg_space space, u32 value);
+
 
 static inline int tb_route_length(u64 route)
 {
-- 
1.9.3


  parent reply	other threads:[~2014-05-26 15:18 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-05-26 15:17 [PATCH v3 00/15] Thunderbolt driver for Apple MacBooks Andreas Noever
2014-05-26 15:17 ` [PATCH v3 01/15] thunderbolt: Add initial cactus ridge NHI support Andreas Noever
2014-05-26 15:17 ` [PATCH v3 02/15] thunderbolt: Add control channel interface Andreas Noever
2014-05-26 15:18 ` [PATCH v3 03/15] thunderbolt: Setup control channel Andreas Noever
2014-05-26 15:18 ` [PATCH v3 04/15] thunderbolt: Add tb_regs.h Andreas Noever
2014-05-26 15:18 ` [PATCH v3 05/15] thunderbolt: Initialize root switch and ports Andreas Noever
2014-05-26 15:18 ` Andreas Noever [this message]
2014-05-26 15:18 ` [PATCH v3 07/15] thunderbolt: Enable plug events Andreas Noever
2014-05-26 15:18 ` [PATCH v3 08/15] thunderbolt: Scan for downstream switches Andreas Noever
2014-05-26 15:18 ` [PATCH v3 09/15] thunderbolt: Handle hotplug events Andreas Noever
2014-05-28 22:26   ` Bjorn Helgaas
2014-05-26 15:18 ` [PATCH v3 10/15] thunderbolt: Add path setup code Andreas Noever
2014-05-28 22:30   ` Bjorn Helgaas
2014-05-26 15:18 ` [PATCH v3 11/15] thunderbolt: Add support for simple pci tunnels Andreas Noever
2014-05-26 15:18 ` [PATCH v3 12/15] pci: Add pci_fixup_suspend_late quirk pass Andreas Noever
2014-05-28 22:36   ` Bjorn Helgaas
2014-05-30 14:33     ` Andreas Noever
2014-05-30 16:09       ` Greg KH
2014-05-26 15:18 ` [PATCH v3 13/15] pci: Suspend/resume quirks for appel thunderbolt Andreas Noever
2014-05-27 14:07   ` Matthew Garrett
2014-05-28 22:43   ` Bjorn Helgaas
2014-05-26 15:18 ` [PATCH v3 14/15] thunderbolt: Read switch uid from EEPROM Andreas Noever
2014-05-26 15:18 ` [PATCH v3 15/15] thunderbolt: Add suspend/hibernate support Andreas Noever
2014-05-27 14:09 ` [PATCH v3 00/15] Thunderbolt driver for Apple MacBooks Matthew Garrett

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1401117492-2870-7-git-send-email-andreas.noever@gmail.com \
    --to=andreas.noever@gmail.com \
    --cc=bhelgaas@google.com \
    --cc=greg@kroah.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-pci@vger.kernel.org \
    --cc=matthew.garrett@nebula.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).