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