Skip to content

mass storage gadget: default stall=1 gives dmesg errors/warnings #7452

Closed

Description

@macmpi

Describe the bug

Loading g_mass_storage (or g_multi) with defaults (i.e. stall=1), generates many errors into dmesg.
Unclear if it affects performances and how.
Linux docs suggest defaults may be disabled on some USB devices known not to work correctly.
Are Pi devices known to be such citizens, and require use of stall=0 because default driver does not work-around some issues?

Steps to reproduce the behaviour

Load g_mass_storage with options g_mass_storage file=/drive.bin stall=1 removable=1
(same goes with g_multi driver)
After a little while dmesg shows errors as below, repeatedly...
No more dmesg errors with stall=0

Device (s)

Raspberry Pi Zero 2 W

System

any kernel up to 6.18.y, experimented with all PiZeros versions.

$ vcgencmd version
Copyright (c) 2012 Broadcom
version 9e19353907b3bc5c36ea355a409e3a5a151de89e (clean) (release) (start_cd)

(same with default start.elf)

Logs

[ 34.516207] dwc2 20980000.usb: dwc2_hsotg_ep_sethalt(ep 51f609dd ep5in, 1)
[ 34.621578] dwc2 20980000.usb: dwc2_hsotg_ep_sethalt(ep 51f609dd ep5in, 1)
[ 34.621655] dwc2 20980000.usb: dwc2_hsotg_start_req: ep5 is stalled
[ 34.621802] dwc2 20980000.usb: dwc2_hsotg_ep_sethalt(ep 51f609dd ep5in, 0)
[ 34.622059] dwc2 20980000.usb: dwc2_hsotg_ep_sethalt(ep 51f609dd ep5in, 1)
[ 34.731527] dwc2 20980000.usb: dwc2_hsotg_ep_sethalt(ep 51f609dd ep5in, 1)
[ 34.731579] dwc2 20980000.usb: dwc2_hsotg_start_req: ep5 is stalled

Additional context

No response

Activity

  1. pelwell commented on Jun 22, 2026

    @pelwell
    Contributor

    A quick AI diagnosis:

    These messages are from the dwc2 USB gadget (device-mode) driver on a Raspberry Pi (BCM2835/6/7, USB base at 0x20980000), where ep5in is bulk IN
    endpoint 5. Here's what each line means:

    Message Meaning
    sethalt(..., 1) Endpoint is being stalled (HALT feature set)
    sethalt(..., 0) Endpoint stall is being cleared
    start_req: ep5 is stalled A queued transfer was attempted but the endpoint is still stalled, so it was skipped

    The sequence tells a story:

    1. The gadget function driver explicitly stalls ep5in (e.g., to signal a protocol error to the host)
    2. The USB host notices the stall and sends a CLEAR_FEATURE(ENDPOINT_HALT) control request
    3. dwc2 clears the stall (sethalt(..., 0))
    4. The gadget driver immediately re-stalls it (sethalt(..., 1)) — because whatever error condition caused the original stall hasn't been resolved
    5. The cycle repeats

    Root cause is in the gadget function driver sitting above dwc2 — something is calling usb_ep_set_halt() on this endpoint repeatedly. Common triggers:

    • A USB Mass Storage gadget signalling a SCSI error (sense data) to the host
    • A CDC or other class driver receiving a request it can't satisfy
    • A queued request completing with an error, and the driver responding by stalling rather than re-queuing

    The dwc2 driver itself is behaving correctly — it's just faithfully executing what the upper layer asks. The fix, if this is a problem, would be in
    whichever gadget function driver owns ep5in.

    Is there any evidence that isn't correct?

  2. pelwell commented on Jun 22, 2026

    @pelwell
    Contributor

    Previously discussed in your Forum thread: https://forums.raspberrypi.com/viewtopic.php?t=395143

  3. macmpi commented on Jun 22, 2026

    @macmpi
    Author

    Yes, and got no response there on actual matter: hoping there would be more insight through this channel about the root cause and remedies.
    This fail is 100% repeatable on my end (whatever disk used), and had been observed in various other instances (ref1 ,ref2 and al.): the only workaround that spread-out is to use stall=0 with no more explanations (like in PiOS whitepaper)...

    Is there any evidence that isn't correct?

    Is baseline g_mass_storage usecase supposed to always have failure?
    Dunno why the common triggers (SCSI error for instance) would ever happen in such circumstance.

  4. pelwell commented on Jun 22, 2026

    @pelwell
    Contributor

    Further diagnosis, looking now at the mass storage gadget driver:

    The main loop at line 2600–2604 is:
    get_next_command → do_scsi_command → finish_reply → send_status

    finish_reply calls halt_bulk_in_endpoint after queuing the last data buffer via start_in_transfer (line 1613–1618). At that moment the endpoint queue is non-empty (the data transfer is in progress), so dwc2_hsotg_ep_sethalt returns -EAGAIN — generating the first sethalt(ep5in, 1) log entry at t=34.516 without actually stalling the hardware.

    halt_bulk_in_endpoint then sits in a 100 ms polling loop, sleeping and retrying. After ~105 ms the data request completes and the queue is empty, so the retry at t=34.621578 succeeds and the endpoint is actually stalled.

    The main loop then falls through to send_status, which immediately queues the CSW (13-byte status packet) on the now-stalled endpoint. dwc2_hsotg_start_req sees the hardware stall bit set and emits the warning at t=34.621655, leaving the request sitting in the queue.

    When the host sends CLEAR_FEATURE(ENDPOINT_HALT) at t=34.621802, dwc2 clears the stall and starts the queued CSW. The CSW completes quickly, the thread wakes, processes the next buffered CBW from bulk_out, runs through do_scsi_command → finish_reply again with another residue, and the whole cycle repeats — producing the stall at t=34.622059 and the next ~100 ms gap.

    The problem is the 100 ms fixed-sleep retry loop in halt_bulk_in_endpoint. Rather than waiting for the in-flight transfer to complete via the existing io_wait / buffer-state mechanism, it just sleeps and polls. This means:

    1. Every command with residue has at least a 100 ms forced latency before the stall is actually applied, during which the hardware is still active.
    2. The 100 ms delay is also longer than strictly necessary — the pending transfer could complete in microseconds, but the thread always sleeps the full interval.

    The fix would be to replace the msleep_interruptible(100) retry with a proper wait on common->io_wait for the pending buffer to drain, similar to how sleep_thread() works elsewhere in the driver.

    [ Updated to make the quotation clearer - this is not my analysis ]

  5. macmpi commented on Jun 22, 2026

    @macmpi
    Author

    wow... Do you think same would apply to g_multi or even plain libcomposite configfs composition? (the same error happens in any of the 3 configurations)

  6. macmpi commented on Jun 23, 2026

    @macmpi
    Author

    Looking at the host side (6.17.0-35-generic #35~24.04.1-Ubuntu), while PiZeroW runs:
    modprobe g_mass_storage file=/dev/mmcblk0p1 stall=1 removable=1
    Pi side dmesg:

    [   93.892799] Mass Storage Function, version: 2009/09/11
    [   93.892841] LUN: removable file: (no medium)
    [   93.893060] LUN: removable file: /dev/mmcblk0p1
    [   93.893082] Number of LUNs=1
    [   93.900145] g_mass_storage gadget.0: Mass Storage Gadget, version: 2009/09/11
    [   93.900189] g_mass_storage gadget.0: userspace failed to provide iSerialNumber
    [   93.900206] g_mass_storage gadget.0: g_mass_storage ready
    [   93.900363] dwc2 20980000.usb: bound driver g_mass_storage
    [   94.087278] dwc2 20980000.usb: new device is high-speed
    [   94.199467] dwc2 20980000.usb: new device is high-speed
    [   94.256397] dwc2 20980000.usb: new address 30
    [   95.330212] dwc2 20980000.usb: dwc2_hsotg_ep_sethalt(ep 70197aff ep1in, 1)
    [   95.440110] dwc2 20980000.usb: dwc2_hsotg_ep_sethalt(ep 70197aff ep1in, 1)
    [   95.440208] dwc2 20980000.usb: dwc2_hsotg_start_req: ep1 is stalled
    [   95.440483] dwc2 20980000.usb: dwc2_hsotg_ep_sethalt(ep 70197aff ep1in, 0)
    [   95.440993] dwc2 20980000.usb: dwc2_hsotg_ep_sethalt(ep 70197aff ep1in, 1)
    [   95.550120] dwc2 20980000.usb: dwc2_hsotg_ep_sethalt(ep 70197aff ep1in, 1)
    [   95.550219] dwc2 20980000.usb: dwc2_hsotg_start_req: ep1 is stalled
    [   95.550404] dwc2 20980000.usb: dwc2_hsotg_ep_sethalt(ep 70197aff ep1in, 0)
    [   95.556984] dwc2 20980000.usb: dwc2_hsotg_ep_sethalt(ep 70197aff ep1in, 1)
    [   95.660066] dwc2 20980000.usb: dwc2_hsotg_ep_sethalt(ep 70197aff ep1in, 1)
    [   95.660164] dwc2 20980000.usb: dwc2_hsotg_start_req: ep1 is stalled
    [   95.660392] dwc2 20980000.usb: dwc2_hsotg_ep_sethalt(ep 70197aff ep1in, 0)
    [   95.660667] dwc2 20980000.usb: dwc2_hsotg_ep_sethalt(ep 70197aff ep1in, 1)
    [   95.770054] dwc2 20980000.usb: dwc2_hsotg_ep_sethalt(ep 70197aff ep1in, 1)
    [   95.770152] dwc2 20980000.usb: dwc2_hsotg_start_req: ep1 is stalled
    [   95.770451] dwc2 20980000.usb: dwc2_hsotg_ep_sethalt(ep 70197aff ep1in, 0)
    [   96.018347] dwc2 20980000.usb: dwc2_hsotg_ep_sethalt(ep 70197aff ep1in, 1)
    [   96.018431] dwc2 20980000.usb: dwc2_hsotg_start_req: ep1 is stalled
    [   96.018598] dwc2 20980000.usb: dwc2_hsotg_ep_sethalt(ep 70197aff ep1in, 0)
    [   96.043328] dwc2 20980000.usb: dwc2_hsotg_ep_sethalt(ep 70197aff ep1in, 1)
    [   96.150050] dwc2 20980000.usb: dwc2_hsotg_ep_sethalt(ep 70197aff ep1in, 1)
    [   96.150148] dwc2 20980000.usb: dwc2_hsotg_start_req: ep1 is stalled
    [   96.150326] dwc2 20980000.usb: dwc2_hsotg_ep_sethalt(ep 70197aff ep1in, 0)
    

    Host side dmesg:

    [361986.419359] usb 1-6: device descriptor read/64, error -110
    [362002.289402] usb 1-6: device descriptor read/64, error -110
    [362002.393669] usb usb1-port6: attempt power cycle
    [362002.772343] usb 1-6: new full-speed USB device number 82 using xhci_hcd
    [362007.809029] xhci_hcd 0000:00:14.0: Timeout while waiting for setup device command
    [362013.441224] xhci_hcd 0000:00:14.0: Timeout while waiting for setup device command
    [362013.648150] usb 1-6: device not accepting address 82, error -62
    [362013.648253] usb 1-6: WARN: invalid context state for evaluate context command.
    [362013.760280] usb 1-6: new full-speed USB device number 83 using xhci_hcd
    [362019.071648] xhci_hcd 0000:00:14.0: Timeout while waiting for setup device command
    [362024.703001] xhci_hcd 0000:00:14.0: Timeout while waiting for setup device command
    [362024.911864] usb 1-6: device not accepting address 83, error -62
    [362024.912005] usb 1-6: WARN: invalid context state for evaluate context command.
    [362024.912143] usb usb1-port6: unable to enumerate USB device
    [362028.448572] usb 1-6: new high-speed USB device number 84 using xhci_hcd
    [362028.572506] usb 1-6: New USB device found, idVendor=0525, idProduct=a4a5, bcdDevice= 6.18
    [362028.572525] usb 1-6: New USB device strings: Mfr=3, Product=4, SerialNumber=0
    [362028.572533] usb 1-6: Product: Mass Storage Gadget
    [362028.572539] usb 1-6: Manufacturer: Linux 6.18.35-0-rpi with 20980000.usb
    [362028.575346] usb-storage 1-6:1.0: USB Mass Storage device detected
    [362028.575776] usb-storage 1-6:1.0: Quirks match for vid 0525 pid a4a5: 10000
    [362028.575911] scsi host1: usb-storage 1-6:1.0
    [362029.631098] scsi 1:0:0:0: Direct-Access     Linux    File-Stor Gadget 0618 PQ: 0 ANSI: 2
    [362029.632092] sd 1:0:0:0: Attached scsi generic sg0 type 0
    [362029.633409] sd 1:0:0:0: Power-on or device reset occurred
    [362029.634210] sd 1:0:0:0: [sda] 215040 512-byte logical blocks: (110 MB/105 MiB)
    [362029.745031] sd 1:0:0:0: [sda] Write Protect is off
    [362029.745046] sd 1:0:0:0: [sda] Mode Sense: 0f 00 00 00
    [362029.854710] sd 1:0:0:0: [sda] Write cache: enabled, read cache: enabled, doesn't support DPO or FUA
    [362030.081233]  sda:
    [362030.081389] sd 1:0:0:0: [sda] Attached SCSI removable disk
    

    Unsure what cause the initial dwc2_hsotg_ep_sethalt (may even be after Attached SCSI removable disk)

  7. pelwell commented on Jun 23, 2026

    @pelwell
    Contributor

    I see those stall messages using g_mass_storage_gadget - it seems to be intentional:

    		/*
    		 * For Bulk-only, mark the end of the data with a short
    		 * packet.  If we are allowed to stall, halt the bulk-in
    		 * endpoint.  (Note: This violates the Bulk-Only Transport
    		 * specification, which requires us to pad the data if we
    		 * don't halt the endpoint.  Presumably nobody will mind.)
    		 */
    		} else {
    			bh->inreq->zero = 1;
    			if (!start_in_transfer(common, bh))
    				rc = -EIO;
    			common->next_buffhd_to_fill = bh->next;
    			if (common->can_stall)
    				rc = halt_bulk_in_endpoint(common->fsg);
    		}
    

    I don't get any such messages using libcomposite and usb_f_mass_storage.

  8. pelwell commented on Jun 23, 2026

    @pelwell
    Contributor

    Correction - I do if it is properly configured with stall=1.

  9. macmpi commented on Jun 23, 2026

    @macmpi
    Author

    Presumably nobody will mind 😜

    it seems to be intentional

    So should we just consider those dmesg entries as not harmful, and assume this would not affect reliability or performances?
    (don't have non-Pi gadget-capable device to test, and check same messages are shown too with other HW)
    Would (default) stall=1 option then be recommended over stall=0 despite those messages?

  10. pelwell commented on Jun 23, 2026

    @pelwell
    Contributor

    I think it's pointless log spam. See #7453. I think you know what to do with these.

  11. macmpi commented on Jun 23, 2026

    @macmpi
    Author

    Ok, thanks a lot for investigating and clearing it-up!

    FYI @JamesH65 this might help for the remaining bits on related whitepaper: stall=0 may be safely removed in samples.

    Closing.

  12. 42 remaining items

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions