From 6158d0b42411165d34635a2ddfea17e12dce3329 Mon Sep 17 00:00:00 2001 From: Bin Meng Date: Thu, 7 Sep 2017 06:13:20 -0700 Subject: usb: storage: Refactor to use max_xfer_blk from struct us_data This adds a new memeber max_xfer_blk in struct us_data to record the maximum number of transfer blocks for the storage device. It is set per HCD setting, and so far is to 65535 for EHCD and 20 for everything else. Signed-off-by: Bin Meng --- common/usb_storage.c | 49 ++++++++++++++++++++++++++++++------------------- 1 file changed, 30 insertions(+), 19 deletions(-) (limited to 'common') diff --git a/common/usb_storage.c b/common/usb_storage.c index df0b0573087..957ccdb2973 100644 --- a/common/usb_storage.c +++ b/common/usb_storage.c @@ -98,19 +98,9 @@ struct us_data { struct scsi_cmd *srb; /* current srb */ trans_reset transport_reset; /* reset routine */ trans_cmnd transport; /* transport routine */ + unsigned short max_xfer_blk; /* maximum transfer blocks */ }; -#ifdef CONFIG_USB_EHCI_HCD -/* - * The U-Boot EHCI driver can handle any transfer length as long as there is - * enough free heap space left, but the SCSI READ(10) and WRITE(10) commands are - * limited to 65535 blocks. - */ -#define USB_MAX_XFER_BLK 65535 -#else -#define USB_MAX_XFER_BLK 20 -#endif - #ifndef CONFIG_BLK static struct us_data usb_stor[USB_MAX_STOR_DEV]; #endif @@ -949,6 +939,23 @@ do_retry: return USB_STOR_TRANSPORT_FAILED; } +static void usb_stor_set_max_xfer_blk(struct us_data *us) +{ + unsigned short blk; + +#ifdef CONFIG_USB_EHCI_HCD + /* + * The U-Boot EHCI driver can handle any transfer length as long as + * there is enough free heap space left, but the SCSI READ(10) and + * WRITE(10) commands are limited to 65535 blocks. + */ + blk = USHRT_MAX; +#else + blk = 20; +#endif + + us->max_xfer_blk = blk; +} static int usb_inquiry(struct scsi_cmd *srb, struct us_data *ss) { @@ -1150,12 +1157,12 @@ static unsigned long usb_stor_read(struct blk_desc *block_dev, lbaint_t blknr, /* XXX need some comment here */ retry = 2; srb->pdata = (unsigned char *)buf_addr; - if (blks > USB_MAX_XFER_BLK) - smallblks = USB_MAX_XFER_BLK; + if (blks > ss->max_xfer_blk) + smallblks = ss->max_xfer_blk; else smallblks = (unsigned short) blks; retry_it: - if (smallblks == USB_MAX_XFER_BLK) + if (smallblks == ss->max_xfer_blk) usb_show_progress(); srb->datalen = block_dev->blksz * smallblks; srb->pdata = (unsigned char *)buf_addr; @@ -1178,7 +1185,7 @@ retry_it: start, smallblks, buf_addr); usb_disable_asynch(0); /* asynch transfer allowed */ - if (blkcnt >= USB_MAX_XFER_BLK) + if (blkcnt >= ss->max_xfer_blk) debug("\n"); return blkcnt; } @@ -1236,12 +1243,12 @@ static unsigned long usb_stor_write(struct blk_desc *block_dev, lbaint_t blknr, */ retry = 2; srb->pdata = (unsigned char *)buf_addr; - if (blks > USB_MAX_XFER_BLK) - smallblks = USB_MAX_XFER_BLK; + if (blks > ss->max_xfer_blk) + smallblks = ss->max_xfer_blk; else smallblks = (unsigned short) blks; retry_it: - if (smallblks == USB_MAX_XFER_BLK) + if (smallblks == ss->max_xfer_blk) usb_show_progress(); srb->datalen = block_dev->blksz * smallblks; srb->pdata = (unsigned char *)buf_addr; @@ -1263,7 +1270,7 @@ retry_it: PRIxPTR "\n", start, smallblks, buf_addr); usb_disable_asynch(0); /* asynch transfer allowed */ - if (blkcnt >= USB_MAX_XFER_BLK) + if (blkcnt >= ss->max_xfer_blk) debug("\n"); return blkcnt; @@ -1384,6 +1391,10 @@ int usb_storage_probe(struct usb_device *dev, unsigned int ifnum, ss->irqmaxp = usb_maxpacket(dev, ss->irqpipe); dev->irq_handle = usb_stor_irq; } + + /* Set the maximum transfer size per host controller setting */ + usb_stor_set_max_xfer_blk(ss); + dev->privptr = (void *)ss; return 1; } -- cgit v1.3.1 From ea7fad91019654fb21e6cd04f963e0482869dba5 Mon Sep 17 00:00:00 2001 From: Bin Meng Date: Thu, 7 Sep 2017 06:13:21 -0700 Subject: dm: usb: storage: Fix broken read/write when both EHCD and xHCD are enabled When EHCD and xHCD are enabled at the same time, USB storage device driver will fail to read/write from/to the storage device attached to the xHCI interface, due to its transfer blocks exceeds the xHCD driver limitation. With driver model, we have an API to get the controller's maximum transfer size and we can use that to determine the storage driver's capability of read/write. Note: the non-DM version driver is still broken with xHCD and the intent here is not to fix the non-DM one, since the xHCD itself is already broken in places like 3.0 hub support, etc. Signed-off-by: Bin Meng --- common/usb_storage.c | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) (limited to 'common') diff --git a/common/usb_storage.c b/common/usb_storage.c index 957ccdb2973..a57570b73f3 100644 --- a/common/usb_storage.c +++ b/common/usb_storage.c @@ -939,10 +939,14 @@ do_retry: return USB_STOR_TRANSPORT_FAILED; } -static void usb_stor_set_max_xfer_blk(struct us_data *us) +static void usb_stor_set_max_xfer_blk(struct usb_device *udev, + struct us_data *us) { unsigned short blk; + size_t __maybe_unused size; + int __maybe_unused ret; +#ifndef CONFIG_DM_USB #ifdef CONFIG_USB_EHCI_HCD /* * The U-Boot EHCI driver can handle any transfer length as long as @@ -953,6 +957,17 @@ static void usb_stor_set_max_xfer_blk(struct us_data *us) #else blk = 20; #endif +#else + ret = usb_get_max_xfer_size(udev, (size_t *)&size); + if (ret < 0) { + /* unimplemented, let's use default 20 */ + blk = 20; + } else { + if (size > USHRT_MAX * 512) + blk = USHRT_MAX; + blk = size / 512; + } +#endif us->max_xfer_blk = blk; } @@ -1393,7 +1408,7 @@ int usb_storage_probe(struct usb_device *dev, unsigned int ifnum, } /* Set the maximum transfer size per host controller setting */ - usb_stor_set_max_xfer_blk(ss); + usb_stor_set_max_xfer_blk(dev, ss); dev->privptr = (void *)ss; return 1; -- cgit v1.3.1