This is an automated email from the ASF dual-hosted git repository. xiaoxiang781216 pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/nuttx.git
commit 1d110d75c8bddc8ef9a6084626b2d48b28883f69 Author: yangsong8 <[email protected]> AuthorDate: Fri Sep 11 17:37:49 2026 +0800 drivers/usbdev/usbmsc: enlarge bulk-OUT requests per write transfer usbmsc_cmdwritestate re-queued each bulk-OUT (host->device) request at one maxpacket, so a host->device WRITE completed one USB transfer at a time, capping write throughput while the already-optimized read path was faster. Re-queue each bulk-OUT request at up to its full allocated buffer (priv->bulkoutreqlen) so the controller DMA coalesces many maxpacket USB packets into one completion, cutting the EP_SUBMIT / completion-IRQ / worker round-trips that dominate the OUT throughput. CONFIG_USBMSC_NRDREQS bulk-OUT requests are already queued (each at one maxpacket) when a write command is accepted, so usbmsc_wrreqinit seeds priv->residue with (total_dataout - NRDREQS*maxpacket) and, during the CMDWRITE data phase, reuses it as the count of bytes still to request: only the bytes beyond that already-queued capacity are requested in larger bulkoutreqlen chunks. This guarantees the total bytes ever requested for the command never exceeds the host's data-out length, so an enlarged request can never stall waiting for bytes the host will not send; once it is exhausted the re-queue falls back to one maxpacket so any leftover queued requests stay maxpacket-sized for the next command. Reusing the otherwise-redundant priv->residue (which equals xfrlen*sectorsize throughout the write drain) avoids adding a dedicated field; the CSW residue is rebuilt from xfrlen*sectorsize when the data phase ends. bulkoutreqlen is recorded at bind time from the same expression used to allocate the request buffer (CONFIG_USBMSC_BULKOUTREQLEN for HS/FS, SSBULKMAXPACKET*(MAXBURST+1) for SuperSpeed), so it can never exceed the buffer. Signed-off-by: yangsong8 <[email protected]> --- drivers/usbdev/Kconfig | 2 ++ drivers/usbdev/usbmsc.c | 46 +++++++++++++++++------------ drivers/usbdev/usbmsc.h | 3 ++ drivers/usbdev/usbmsc_scsi.c | 69 ++++++++++++++++++++++++++++++++++++++++++-- 4 files changed, 98 insertions(+), 22 deletions(-) diff --git a/drivers/usbdev/Kconfig b/drivers/usbdev/Kconfig index bfa81189598..f270cab5a38 100644 --- a/drivers/usbdev/Kconfig +++ b/drivers/usbdev/Kconfig @@ -1119,6 +1119,8 @@ config USBMSC_BULKOUTREQLEN int "Bulk OUT request size" default 512 if USBDEV_DUALSPEED default 64 if !USBDEV_DUALSPEED + range 512 65536 if USBDEV_DUALSPEED + range 64 65536 if !USBDEV_DUALSPEED ---help--- The size of the buffer in each READ request. This value should to be at least as large as the endpoint maxpacket size. Most DCDs will return diff --git a/drivers/usbdev/usbmsc.c b/drivers/usbdev/usbmsc.c index 2d489809ee2..981ab9d7b1a 100644 --- a/drivers/usbdev/usbmsc.c +++ b/drivers/usbdev/usbmsc.c @@ -336,25 +336,32 @@ static int usbmsc_bind(FAR struct usbdevclass_driver_s *driver, priv->epbulkout->priv = priv; + /* Compute the bulk OUT request buffer size once (SS vs non-SS) and use it + * both to allocate the requests below and (via priv->bulkoutreqlen) as the + * upper bound when usbmsc_cmdwritestate enlarges a bulk-OUT re-submit. + * Single source of truth for the buffer size. + */ + +#ifdef CONFIG_USBDEV_SUPERSPEED + if (dev->speed == USB_SPEED_SUPER || + dev->speed == USB_SPEED_SUPER_PLUS) + { + priv->bulkoutreqlen = USBMSC_SSBULKMAXPACKET * + (USBMSC_SSBULKMAXBURST + 1); + } + else +#endif + { + priv->bulkoutreqlen = CONFIG_USBMSC_BULKOUTREQLEN; + } + /* Pre-allocate read requests */ for (i = 0; i < CONFIG_USBMSC_NRDREQS; i++) { reqcontainer = &priv->rdreqs[i]; -#ifdef CONFIG_USBDEV_SUPERSPEED - if (dev->speed == USB_SPEED_SUPER || - dev->speed == USB_SPEED_SUPER_PLUS) - { - reqcontainer->req = usbdev_allocreq(priv->epbulkout, - USBMSC_SSBULKMAXPACKET * - (USBMSC_SSBULKMAXBURST + 1)); - } - else -#endif - { - reqcontainer->req = usbdev_allocreq(priv->epbulkout, - CONFIG_USBMSC_BULKOUTREQLEN); - } + reqcontainer->req = usbdev_allocreq(priv->epbulkout, + priv->bulkoutreqlen); if (reqcontainer->req == NULL) { @@ -1062,14 +1069,15 @@ int usbmsc_setconfig(FAR struct usbmsc_dev_s *priv, uint8_t config) priv->epbulkout->priv = priv; /* The per-request buffers were allocated at bind time from - * priv->bulkinreqlen. The bulk-IN fill length - * (MAX(bulkinreqlen, maxpacket) in usbmsc_cmdreadstate) and - * assume each buffer holds at least one maxpacket; - * a configured REQLEN smaller than maxpacket would overflow req->buf. - * Catch such a misconfiguration early. + * priv->bulkinreqlen / priv->bulkoutreqlen. The bulk-IN fill length + * (MAX(bulkinreqlen, maxpacket) in usbmsc_cmdreadstate) and the bulk-OUT + * submit length (maxpacket, queued below) both assume each buffer holds at + * least one maxpacket; a configured REQLEN smaller than maxpacket would + * overflow req->buf. Catch such a misconfiguration early. */ DEBUGASSERT(priv->bulkinreqlen >= priv->epbulkin->maxpacket); + DEBUGASSERT(priv->bulkoutreqlen >= priv->epbulkout->maxpacket); /* Queue read requests in the bulk OUT endpoint */ diff --git a/drivers/usbdev/usbmsc.h b/drivers/usbdev/usbmsc.h index fb88616a5b8..4b8edfed697 100644 --- a/drivers/usbdev/usbmsc.h +++ b/drivers/usbdev/usbmsc.h @@ -402,6 +402,9 @@ struct usbmsc_dev_s uint32_t bulkinreqlen; /* Allocated size of each bulk IN request * buffer (== alloc expr; used to fill a * full request per bulk-IN transfer) */ + uint32_t bulkoutreqlen; /* Allocated size of each bulk OUT request + * buffer (== alloc expr; upper bound on + * an enlarged bulk-OUT request) */ /* SCSI command processing */ diff --git a/drivers/usbdev/usbmsc_scsi.c b/drivers/usbdev/usbmsc_scsi.c index 2b7410601b1..5433fdfd189 100644 --- a/drivers/usbdev/usbmsc_scsi.c +++ b/drivers/usbdev/usbmsc_scsi.c @@ -568,6 +568,38 @@ static inline int usbmsc_cmdread6(FAR struct usbmsc_dev_s *priv) return ret; } +/**************************************************************************** + * Name: usbmsc_wrreqinit + * + * Description: + * Called once when a SCSI write command is accepted (transition into the + * USBMSC_STATE_CMDWRITE state) to arm the enlarged bulk-OUT re-submit + * sizing used by usbmsc_cmdwritestate. + * + * CONFIG_USBMSC_NRDREQS bulk-OUT requests are already queued on the + * endpoint, each sized at one maxpacket, so they will absorb the first + * NRDREQS*maxpacket bytes of this data-out phase. Only the remaining + * bytes may be requested in larger (bulkoutreqlen) chunks. Pre-accounting + * the already-queued capacity here guarantees the total bytes ever + * requested for this command never exceeds the host's data-out length, so + * an enlarged request can never stall waiting for bytes the host will not + * send. + * + * The seed is stored in priv->residue, which during the CMDWRITE data + * phase is reused as the "bytes still to request" counter. When the data + * phase ends, usbmsc_cmdwritestate rebuilds priv->residue from + * xfrlen * sectorsize so the CSW reports the correct untransferred amount. + * + ****************************************************************************/ + +static void usbmsc_wrreqinit(FAR struct usbmsc_dev_s *priv) +{ + uint32_t total = priv->u.xfrlen * priv->lun->sectorsize; + uint32_t prealloc = CONFIG_USBMSC_NRDREQS * priv->epbulkout->maxpacket; + + priv->residue = total > prealloc ? total - prealloc : 0; +} + /**************************************************************************** * Name: usbmsc_cmdwrite6 * @@ -636,6 +668,7 @@ static inline int usbmsc_cmdwrite6(FAR struct usbmsc_dev_s *priv) { usbtrace(TRACE_CLASSSTATE(USBMSC_CLASSSTATE_CMDPARSECMDWRITE6), priv->cdb[0]); + usbmsc_wrreqinit(priv); priv->thstate = USBMSC_STATE_CMDWRITE; } } @@ -1198,6 +1231,7 @@ static inline int usbmsc_cmdwrite10(FAR struct usbmsc_dev_s *priv) { usbtrace(TRACE_CLASSSTATE(USBMSC_CLASSSTATE_CMDPARSECMDWRITE10), priv->cdb[0]); + usbmsc_wrreqinit(priv); priv->thstate = USBMSC_STATE_CMDWRITE; } } @@ -1548,6 +1582,7 @@ static inline int usbmsc_cmdwrite12(FAR struct usbmsc_dev_s *priv) { usbtrace(TRACE_CLASSSTATE(USBMSC_CLASSSTATE_CMDPARSECMDWRITE12), priv->cdb[0]); + usbmsc_wrreqinit(priv); priv->thstate = USBMSC_STATE_CMDWRITE; return OK; } @@ -2398,6 +2433,7 @@ static int usbmsc_cmdwritestate(FAR struct usbmsc_dev_s *priv) irqstate_t flags; ssize_t nwritten; uint32_t xfrd; + uint32_t reqlen; FAR uint8_t *src; FAR uint8_t *dest; int nbytes; @@ -2499,7 +2535,6 @@ static int usbmsc_cmdwritestate(FAR struct usbmsc_dev_s *priv) } priv->nsectbytes = 0; - priv->residue -= lun->sectorsize * nrbufs; priv->u.xfrlen -= nrbufs; priv->sector += nrbufs; } @@ -2520,7 +2555,6 @@ static int usbmsc_cmdwritestate(FAR struct usbmsc_dev_s *priv) } priv->nsectbytes = 0; - priv->residue -= lun->sectorsize; priv->u.xfrlen--; priv->sector++; } @@ -2530,9 +2564,31 @@ static int usbmsc_cmdwritestate(FAR struct usbmsc_dev_s *priv) /* In either case, we are finished with this read request and can * return it to the endpoint. Then we will go back to the top of the * top and attempt to get the next read request. + * + * While there are still data-out bytes not yet requested (beyond the + * initially queued maxpacket requests, pre-accounted in priv->residue), + * request them in full bulkoutreqlen chunks so the controller DMA + * coalesces many maxpacket USB packets into one completion, cutting + * the EP_SUBMIT / completion-IRQ / worker round-trips that dominate + * the OUT throughput. Because priv->residue was seeded with + * (total - NRDREQS*maxpacket), the total bytes ever requested can + * never exceed the host's data-out length, so an + * enlarged request can never stall. Once exhausted, fall back to one + * maxpacket so leftover queued requests stay maxpacket-sized for the + * next command. */ - req->len = priv->epbulkout->maxpacket; + if (priv->residue > 0) + { + reqlen = MIN(priv->bulkoutreqlen, priv->residue); + priv->residue -= reqlen; + } + else + { + reqlen = priv->epbulkout->maxpacket; + } + + req->len = reqlen; req->priv = privreq; req->callback = usbmsc_rdcomplete; @@ -2556,6 +2612,13 @@ static int usbmsc_cmdwritestate(FAR struct usbmsc_dev_s *priv) } errout: + /* Rebuild the CSW residue (bytes not written to the block driver) from the + * still-untransferred sector count. During the data phase priv->residue is + * reused to size the enlarged bulk-OUT re-submits; xfrlen * sectorsize is + * the exact untransferred byte count to report in the CSW. + */ + + priv->residue = priv->u.xfrlen * lun->sectorsize; usbtrace(TRACE_CLASSSTATE(USBMSC_CLASSSTATE_CMDWRITECMDFINISH), priv->u.xfrlen); priv->thstate = USBMSC_STATE_CMDFINISH;
