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