opentitan icon indicating copy to clipboard operation
opentitan copied to clipboard

[usbdev] Tracking work items

Open vogelpi opened this issue 6 years ago • 28 comments

The goal of this issue is to track work items related to integrating the USB device into the Earlgrey top level.

  • [x] Check usbdev documentation for comportability
  • [ ] Adjust top-level documentation (block diagrams, address map, etc.)
  • [x] Getting the usbdev lint clean: #1093
  • [x] Fix warnings in usbdev thrown at runtime: #1351
  • [ ] Switch from using dedicated IOs to multiplexed IOs for USB: #1677, update: just the sense input will be multiplexed, the rest shall remain dedicated
  • [ ] Add support for differential signals in the pinmux/padctrl: #1677
  • [x] Move generation of both pullup/pullup_en and usb_dp/n_o/usb_dp/n_en_o from the top-level back into the usbdev: See https://github.com/lowRISC/opentitan/pull/1380#discussion_r369317998 #1677, #1747
  • [x] Create inter-module connection for frame_start_o/event_frame signal to allow clock synchronization: #2125
  • [x] Controllability and observability features: #2703
  • [ ] Decide if we want to keep tx_mode_se: #2704
  • [x] Add software controllable receive enable to disable differential receiver in IO if not needed: #2945
  • [ ] Add LinkActiveNoReset state and make sure the reference pulses are produced after deep sleep: https://github.com/lowRISC/opentitan/pull/7950#discussion_r706524805

Other issues not strictly related to the integration

  • [ ] Improve USB DPI module: #44
  • [x] In case of unimplemented endpoints, IN & OUT transactions should be stalled, SETUP transactions should be ignored. Currently we just ignore all transactions. https://github.com/lowRISC/opentitan/pull/1506#issuecomment-584521175 #2357
  • [ ] We should check parameter types and add assertions for invalid parameter values: #2419
  • [x] #10255

vogelpi avatar Jan 23 '20 12:01 vogelpi

Also, we get the following warnings currently from topgen:

WARNING: usbdev contains extra key reset_primary
WARNING: usbdev contains extra key other_reset_list

I should investigate this.

vogelpi avatar Jan 31 '20 15:01 vogelpi

Added items to the list and reorganized:

Features:

  • [x] Decide if we want to keep tx_mode_se: #2704
  • [x] Add LinkActiveNoReset state (or similar) and make sure the reference pulses are produced after deep sleep: #7950
  • [x] Move sense over to an MIO. Direct users to set up MIO retention reg if VBUS is overridden in the USB core. From #6043
  • [x] Detect all three possible state transitions in the AON domain, and report back to usbdev, so it may begin in the correct state from Suspended. From highest to lowest priority, those three are (1) VBUS power interruption, (2) bus reset, (3) bus resume.
  • [x] Add out_endpoint_enable and in_endpoint_enable so the firmware can decide which endpoints are implemented, not just the Verilog parameters.
  • [x] Possibly add set stall/nak bits to buffer registers, so the STALL or NAK response is ready immediately after a buffer is retired.

Documentation:

  • [x] Adjust top-level documentation (block diagrams, address map, etc.)
  • [x] Document what firmware must do to implement suspend correctly w/ deep sleep.
  • [ ] Review the test plan. Remove references to low-speed mode, which usbdev does not support.
  • [x] Correct the documentation's use of "STATUS" transactions where "SETUP" is the correct word.

Bugs:

  • [x] Disable pull-up when sense is low. (from https://github.com/lowRISC/opentitan/issues/7951#issuecomment-990896477). Indicate if VBUS was lost while usbdev was powered down.
  • [x] Ignore packets to unimplemented and/or disabled endpoints. A non-existent endpoint can't respond, and the host would have sent a bad token.
  • [x] Don't reset the data toggle for ignored SETUP packets.
  • [x] Ignore SETUP packets to non-control endpoints. Currently, usbdev only ignores them for endpoints that aren't implemented in Verilog, and it NAKs them for endpoints that don't support SETUP packets. NAK and STALL are never valid responses to SETUP packets.

Testing / Verification:

  • [ ] Improve USB DPI module: #44
  • [x] We should check parameter types and add assertions for invalid parameter values: #2419

a-will avatar Jan 10 '22 16:01 a-will

22c185623d66436e3ffa3da9da33235f6d3d9fb3 obviates the motivation for a separate phy_config bit to turn off the differential receiver, the item listed here:

a-will avatar Jan 14 '22 15:01 a-will

Small documentation fixes to do (copied from an email from @a-will):

  • [ ] The phy_config register claims that only single-ended mode is supported for both RX and TX, but we support differential mode for both as well (at least for now).
  • [ ] Typo referring to STATUS packets and transactions --> Should be SETUP.
  • [ ] If the documentation is correct for NAKing SETUP packets to endpoints that aren't configured to receive them (and it seems like it is), we're doing it wrong. The spec explicitly says, "If a non-control endpoint receives aSETUP token, it must ignore the transaction and return no response." NAK and STALL are never valid responses to SETUP packets.
  • [ ] Consider moving board design advice for FPGAs from IP documentation to elsewhere, maybe a separate whitepaper?

GregAC avatar Jan 20 '22 09:01 GregAC

hi all, given the list @a-will has pointed out, should we have a quick sync? Judging by the things @a-will has identified, i'm not entirely sure we can immediately declare D2 on usbdev.

tjaychen avatar Jan 20 '22 17:01 tjaychen

i should clarify that to me, the incorrect behavior of usbdev in suspend (not detecting all wakeup conditions, no mechanism to come back to the right state) seems especially problematic on the hw side.

The issues with endpoint / stall also seems like it would cause a lot of software usability issues.

tjaychen avatar Jan 20 '22 19:01 tjaychen

As I see it D2 is a 'feature complete' milestone, it doesn't mean no known bugs.

Do we require actual new features to fix these issues? As I understood it they're more bugs in our current implementation.

GregAC avatar Jan 20 '22 19:01 GregAC

I think the correct detection of all events in suspend can reasonably be called a bug. The software usability enhancements are to me features, same with the MIO relocation.

Alex what do you think?

On Thu, Jan 20, 2022 at 11:17 AM Greg Chadwick @.***> wrote:

As I see it D2 is a 'feature complete' milestone, it doesn't mean no known bugs.

Do we require actual new features to fix these issues? As I understood it they're more bugs in our current implementation.

— Reply to this email directly, view it on GitHub https://github.com/lowRISC/opentitan/issues/1403#issuecomment-1017840459, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAH2RSWIWZJ6KJB5KUYVGI3UXBNVJANCNFSM4KKVX5WQ . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

You are receiving this because you commented.Message ID: @.***>

tjaychen avatar Jan 20 '22 19:01 tjaychen

Well it is possible to do post D2 feature changes.

What we should ask is how disruptive are these changes, what do they impact? The MIO relocation is obviously minor without large impact.

How about the software usability enhancements? Significant changes to the programming model or just minor register tweaks?

GregAC avatar Jan 20 '22 19:01 GregAC

I expect the "software usability enhancements" are not going to be extremely disruptive, though they're fairly important to successfully implementing existing classes of communication protocols.

The endpoint enable bits are going to be new registers that qualify inputs to existing logic that determines whether an endpoint should respond to incoming packets. The "set stall/nak" bits are likely going to be a couple new config bits that accompany the buffer descriptor (for TX) or are in a separate register with per-endpoint flags (for RX), and when a transaction has completed, these will trigger a change to the corresponding flags in other registers.

The detection of events in suspend is slightly more disruptive, since it will involve adding the sense input to the AON domain and communicating to usbdev what happened while in deep sleep (so software can read it out and determine what state the device is supposed to be in, then bring it up).

Declaring the IP to be at D2 seems a little silly right now, since wake-from-suspend and functional flow control are "complete... but not really." I feel like we ought to get these few items added, and then we'll be good to go.

a-will avatar Jan 20 '22 20:01 a-will

Thanks for the extra info, does sound like we may need some more work before D2. A couple of things I'd like to clarify:

  1. What does the proposed out_endpoint_enableand in_endpoint_enable add over what is available with the existing 'stall' https://docs.opentitan.org/hw/ip/usbdev/doc/index.html#Reg_stall register? It seems a stall response is produced for non-existent endpoints so forcing a stall response on functional ones should make them appear non-existent. I'm not hugely familiar with USB so may well be missing something

  2. If I've understood the "set stall/nak" bits feature correctly this is more an ease of use/latency reduction feature than needed functionality? In that it's supplying a hardware mechanism for something that could be achieved in software?

As for the rest (wake-from-suspend, functional flow control) I agree they're important to get right but fixing them doesn't seem to be covered by D2 requirements (See signoff checklist here: https://docs.opentitan.org/doc/project/checklist/).

GregAC avatar Jan 20 '22 21:01 GregAC

Thanks for the extra info, does sound like we may need some more work before D2. A couple of things I'd like to clarify:

  1. What does the proposed out_endpoint_enableand in_endpoint_enable add over what is available with the existing 'stall' https://docs.opentitan.org/hw/ip/usbdev/doc/index.html#Reg_stall register? It seems a stall response is produced for non-existent endpoints so forcing a stall response on functional ones should make them appear non-existent. I'm not hugely familiar with USB so may well be missing something

That's incorrect. Non-existent endpoints do not respond--They don't exist, remember? ;) That STALL response is simply a bug / a reading of the spec gone awry.

STALL is a particular kind of handshake in some protocols that is generally used for reporting error conditions back to the host. It is a message from the device's side of the pipe, and the pipe leads to nowhere and has no listener for requests if there is no device-side endpoint.

So what those bits do is give software the ability to properly distinguish between an endpoint that needs to respond with a particular handshake and a disabled endpoint. This one should be quick to implement.

  1. If I've understood the "set stall/nak" bits feature correctly this is more an ease of use/latency reduction feature than needed functionality? In that it's supplying a hardware mechanism for something that could be achieved in software?

Yes, you're right on the point that it's more of a latency issue. It's supplying a hardware mechanism for something that is at risk of not being achievable in software, though (due to the latency). It's up there as "possibly" because it's unclear if we'll be able to meet the deadline. I feel it'd be best not to leave it at "too close to call"--This is something we'd likely only find out when we've got a full operating environment running on the device.

As for the rest (wake-from-suspend, functional flow control) I agree they're important to get right but fixing them doesn't seem to be covered by D2 requirements (See signoff checklist here: https://docs.opentitan.org/doc/project/checklist/).

I'm not totally clear on what these milestones mean, to be honest, but it seems really arbitrary to mark our IP as "feature complete" if we've clearly got features we intended to have that aren't right. We could claim they are features that appeared between D2 and D3, I guess, but is there a purpose in doing so?

a-will avatar Jan 20 '22 21:01 a-will

To @a-will point, I would probably bet on latency being an issue in a real OS. We are most likely not going to have interrupts that directly respond to an event, and instead filed away for later, so if we head that off now that would be best. I think Pirmin (or someone else) also pointed out that the current usbdev sometimes needs the full attention of the processor to properly maintain a connection at times (a different chip application though). That's definitely not something we'll be able to handle.

Regarding the bugs, I understand what you mean that they're covered by D2 requirements, but it is a little odd to declare D2 knowing there are fairly glaring bugs (it's one thing if the feature is there and we're not aware of the bug underneath). Let me check with Anka to see how she feels.

tjaychen avatar Jan 21 '22 00:01 tjaychen

I'm not totally clear on what these milestones mean, to be honest, but it seems really arbitrary to mark our IP as "feature complete" if we've clearly got features we intended to have that aren't right

Well there's no precise definition of "feature complete". Generally I take it to mean all the functionality specified has been implemented but there's no guarantees it all works. It's a useful milestone as it means on the verification side you can test all specified features and know that issues found are bugs rather than planned but not yet complete implementation work.

Some bring-up work should be done, so declaring feature complete if there's a major features that straight up don't work doesn't seem right. Maybe the definition we have for feature complete for D2 needs adjusting to include "no known major bugs in existing features" or similar.

GregAC avatar Jan 21 '22 07:01 GregAC

Hey @a-will , regarding the sending of STALL for unimplemented endpoints, this was discovered by my colleagues at ETH when they did the conformance testing for usbdev prior to their tapeout (see comment in https://github.com/lowRISC/opentitan/pull/1506#issuecomment-584521175). As I result of this discussion, I've implemented this behavior. The PR #2357 got also approved from Mark our USB expert.

Checking the spec again (Section 8.4.6) it indeed says nothing about unimplemented endpoints in this section but just about halted ones:

If the data packet was received error-free and the function’s receiving endpoint is halted, the function returns STALL

So, to me it's not clear what should actually happen for unimplemented endpoints. Your reasoning to ignore transactions if not implemented makes sense but it's contracting the comment of @stefanlippuner . Do you know where in the spec it says that unimplemented endpoints should ignore these transfers? I would just like to be absolutely sure that we are not investing effort here into something that we have to revert again afterwards.

vogelpi avatar Jan 21 '22 09:01 vogelpi

There's this sentence under Section 8.3.1

If a function receives an otherwise valid PID for a transaction type or direction that it does not support, the function must not respond. For example, an IN-only endpoint must ignore an OUT token.

One reading of that is an unimplemented endpoint supports no transaction types so must never respond.

I'd also note earlier in the spec (Section 5.3.1)

Endpoints other than those with endpoint number zero are in an unknown state before being configured and may not be accessed by the host before being configured.

So the host shouldn't be sending any transaction targeted at a non-existent endpoint anyway. Obviously we need to ensure poorly behaving hosts don't break things. Simply ignoring these bad host transactions sounds reasonable.

On a related note as I understand it the device is responsible for providing endpoint descriptors so if it doesn't want to use all 12 endpoints it just doesn't have to tell the host about them? Is the need for out_endpoint_enable and in_endpoint_enable purely to deal with badly implemented hosts that attempt to use them anyway?

GregAC avatar Jan 21 '22 09:01 GregAC

@vogelpi did ETH run some 3rd party conformance test against their implementation which highlighted this issue in the first place? Could be a dodgy conformance test of course, but given the spec isn't hugely explicit I wouldn't be surprised if there's some defacto standards going on here....

GregAC avatar Jan 21 '22 09:01 GregAC

I did most of the protocol stuff on the ETH side and I think this comment (https://github.com/lowRISC/opentitan/pull/1506#issuecomment-584521175) was based on my reading of the USB 2.0 spec, specifically section 8.4.6. We did run the conformance tests provided by the USB IF (USBCV), but I don't think that this is an issue that came up there (could be wrong though, it has been a while).

Looking at it again the spec states in 8.4.6.1 Function Response to IN Transactions:

Table 8-4 shows the possible responses a function may make in response to an IN token. If the function is unable to send data, due to a halt or a flow control condition, it issues a STALL or NAK handshake, respectively. If the function is able to issue data, it does so. If the received token is corrupted, the function returns no response.

I read that as requiring the function/device to respond to any valid token with either a STALL, NAK, or data. In this the STALL would seem to be the most reasonable response.

However it could well be that the specification in Section 8.3.1 actually takes precendence here and not responding at all would be more correct. This is an edge case (misbehaving host) and the spec does not seem very clear to me. In real usage I think both options should be fine. Perhaps a STALL would lead to a clean state more quickly, e.g. by triggering a reset of the device & driver. Overall it does not seem very important to me what the peripheral does in this case.

I have very little experience with USB, so feel free to override my original comment and change the behaviour for unimplemented EPs as you see fit.

stefanlippuner avatar Jan 21 '22 11:01 stefanlippuner

There's this sentence under Section 8.3.1

If a function receives an otherwise valid PID for a transaction type or direction that it does not support, the function must not respond. For example, an IN-only endpoint must ignore an OUT token.

One reading of that is an unimplemented endpoint supports no transaction types so must never respond.

After thinking about it a bit more, I agree with @GregAC that this should probably apply. The language seems reasonable clear and applicable, so it should override a vague reading of 8.4.6.1.

I also went back and saw that we ran the USB CV tests in January 2020, before https://github.com/lowRISC/opentitan/pull/2357 was authored. So going back to the original behaviour should not cause issues there.

stefanlippuner avatar Jan 21 '22 12:01 stefanlippuner

Heh, yeah, there's some overthinking here. Remember that USB's model is a set of pipes which terminate at an endpoint, and packets strictly go over these pipes. An endpoint that isn't there cannot give a response.

Consider the SETUP transaction, and you'll see quickly that any response is not appropriate for what is a bad token packet. Neither STALL nor NAK are valid responses to SETUP, but with the current code, we respond with STALL.

a-will avatar Jan 21 '22 14:01 a-will