b4668f2
From patchwork Thu Aug  3 15:52:08 2017
b4668f2
Content-Type: text/plain; charset="utf-8"
b4668f2
MIME-Version: 1.0
b4668f2
Content-Transfer-Encoding: 7bit
b4668f2
Subject: [v3] dma-mapping: skip USB devices when configuring DMA during probe
b4668f2
From: Johan Hovold <johan@kernel.org>
b4668f2
X-Patchwork-Id: 9879371
b4668f2
Message-Id: <20170803155208.22165-1-johan@kernel.org>
b4668f2
To: Christoph Hellwig <hch@lst.de>,
b4668f2
 Marek Szyprowski <m.szyprowski@samsung.com>,
b4668f2
 Greg Kroah-Hartman <gregkh@linuxfoundation.org>
b4668f2
Cc: =?UTF-8?q?Andreas=20F=C3=A4rber?= <afaerber@suse.de>,
b4668f2
 linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org,
b4668f2
 Alan Stern <stern@rowland.harvard.edu>, Johan Hovold <johan@kernel.org>,
b4668f2
 stable <stable@vger.kernel.org>, Robin Murphy <robin.murphy@arm.com>,
b4668f2
 Sricharan R <sricharan@codeaurora.org>,
b4668f2
 Stefan Wahren <stefan.wahren@i2se.com>
b4668f2
Date: Thu,  3 Aug 2017 17:52:08 +0200
b4668f2
b4668f2
USB devices use the DMA mask and offset of the controller, which have
b4668f2
already been setup when a device is probed. Note that modifying the
b4668f2
DMA mask of a USB device would change the mask for the controller (and
b4668f2
all devices on the bus) as the mask is literally shared.
b4668f2
b4668f2
Since commit 2bf698671205 ("USB: of: fix root-hub device-tree node
b4668f2
handling"), of_dma_configure() would be called also for root hubs, which
b4668f2
use the device node of the controller. A separate, long-standing bug
b4668f2
that makes of_dma_configure() generate a 30-bit DMA mask from the RPI3's
b4668f2
"dma-ranges" would thus set a broken mask also for the controller. This
b4668f2
in turn prevents USB devices from enumerating when control transfers
b4668f2
fail:
b4668f2
b4668f2
	dwc2 3f980000.usb: Cannot do DMA to address 0x000000003a166a00
b4668f2
b4668f2
Note that the aforementioned DMA-mask bug was benign for the HCD itself
b4668f2
as the dwc2 driver overwrites the mask previously set by
b4668f2
of_dma_configure() for the platform device in its probe callback. The
b4668f2
mask would only later get corrupted when the root-hub child device was
b4668f2
probed.
b4668f2
b4668f2
Fix this, and similar future problems, by adding a flag to struct device
b4668f2
which prevents driver core from calling dma_configure() during probe and
b4668f2
making sure it is set for USB devices.
b4668f2
b4668f2
Fixes: 09515ef5ddad ("of/acpi: Configure dma operations at probe time for platform/amba/pci bus devices")
b4668f2
Cc: stable <stable@vger.kernel.org>	# 4.12
b4668f2
Cc: Robin Murphy <robin.murphy@arm.com>
b4668f2
Cc: Sricharan R <sricharan@codeaurora.org>
b4668f2
Cc: Stefan Wahren <stefan.wahren@i2se.com>
b4668f2
Reported-by: Hans Verkuil <hverkuil@xs4all.nl>
b4668f2
Signed-off-by: Johan Hovold <johan@kernel.org>
b4668f2
---
b4668f2
b4668f2
v3
b4668f2
 - add flag to struct device to prevent DMA configuration during probe instead
b4668f2
   of checking for the USB bus type, which is not available when USB is built
b4668f2
   as a module as noted by Alan
b4668f2
 - drop moderated rpi list from CC
b4668f2
b4668f2
v2
b4668f2
 - amend commit message and point out that the long-standing 30-bit DMA-mask
b4668f2
   bug was benign to the dwc2 HCD itself (Robin)
b4668f2
 - add and use a new dev_is_usb() helper (Robin)
b4668f2
b4668f2
b4668f2
 drivers/base/dma-mapping.c | 6 ++++++
b4668f2
 drivers/usb/core/usb.c     | 1 +
b4668f2
 include/linux/device.h     | 3 +++
b4668f2
 3 files changed, 10 insertions(+)
b4668f2
b4668f2
diff --git a/drivers/base/dma-mapping.c b/drivers/base/dma-mapping.c
b4668f2
index b555ff9dd8fc..f9f703be0ad1 100644
b4668f2
--- a/drivers/base/dma-mapping.c
b4668f2
+++ b/drivers/base/dma-mapping.c
b4668f2
@@ -345,6 +345,9 @@ int dma_configure(struct device *dev)
b4668f2
 	enum dev_dma_attr attr;
b4668f2
 	int ret = 0;
b4668f2
 
b4668f2
+	if (dev->skip_dma_configure)
b4668f2
+		return 0;
b4668f2
+
b4668f2
 	if (dev_is_pci(dev)) {
b4668f2
 		bridge = pci_get_host_bridge_device(to_pci_dev(dev));
b4668f2
 		dma_dev = bridge;
b4668f2
@@ -369,6 +372,9 @@ int dma_configure(struct device *dev)
b4668f2
 
b4668f2
 void dma_deconfigure(struct device *dev)
b4668f2
 {
b4668f2
+	if (dev->skip_dma_configure)
b4668f2
+		return;
b4668f2
+
b4668f2
 	of_dma_deconfigure(dev);
b4668f2
 	acpi_dma_deconfigure(dev);
b4668f2
 }
b4668f2
diff --git a/drivers/usb/core/usb.c b/drivers/usb/core/usb.c
b4668f2
index 17681d5638ac..2a85d905b539 100644
b4668f2
--- a/drivers/usb/core/usb.c
b4668f2
+++ b/drivers/usb/core/usb.c
b4668f2
@@ -588,6 +588,7 @@ struct usb_device *usb_alloc_dev(struct usb_device *parent,
b4668f2
 	 * Note: calling dma_set_mask() on a USB device would set the
b4668f2
 	 * mask for the entire HCD, so don't do that.
b4668f2
 	 */
b4668f2
+	dev->dev.skip_dma_configure = true;
b4668f2
 	dev->dev.dma_mask = bus->sysdev->dma_mask;
b4668f2
 	dev->dev.dma_pfn_offset = bus->sysdev->dma_pfn_offset;
b4668f2
 	set_dev_node(&dev->dev, dev_to_node(bus->sysdev));
b4668f2
diff --git a/include/linux/device.h b/include/linux/device.h
b4668f2
index 723cd54b94da..022cf258068b 100644
b4668f2
--- a/include/linux/device.h
b4668f2
+++ b/include/linux/device.h
b4668f2
@@ -877,6 +877,8 @@ struct dev_links_info {
b4668f2
  * @offline:	Set after successful invocation of bus type's .offline().
b4668f2
  * @of_node_reused: Set if the device-tree node is shared with an ancestor
b4668f2
  *              device.
b4668f2
+ * @skip_dma_configure: Set if driver core should not configure DMA for this
b4668f2
+ *              device during probe.
b4668f2
  *
b4668f2
  * At the lowest level, every device in a Linux system is represented by an
b4668f2
  * instance of struct device. The device structure contains the information
b4668f2
@@ -965,6 +967,7 @@ struct device {
b4668f2
 	bool			offline_disabled:1;
b4668f2
 	bool			offline:1;
b4668f2
 	bool			of_node_reused:1;
b4668f2
+	bool			skip_dma_configure:1;
b4668f2
 };
b4668f2
 
b4668f2
 static inline struct device *kobj_to_dev(struct kobject *kobj)