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