Page 1 of 1

xhci: OS crash after send ENABLE_SLOT command.

Posted: Sat Sep 03, 2022 4:57 am
by longjin
Hello!
I'm writing a xhci driver for my OS, all of the code runs well on QEMU.
But, when I run my OS on my computer, after the ENABLE_SLOT command sent, it looks like it is stuck.
I can ensure that the interrupt handler of XHCI can work normally, and I can receive the interrupt from the event ring when I reset the port. However, after the ENABLE SLOT command is issued, the handler does not run. Then the system looks jammed.


The code which related to the problem: Line 1326.
https://github.com/fslongjin/DragonOS/b ... ci.c#L1326

And, after I ring the doorbell at Line 1565, the problem come into being:
https://github.com/fslongjin/DragonOS/b ... ci.c#L1565

I have thought for a long time and still can't solve the problem. How can I solve this?

Re: xhci: OS crash after send ENABLE_SLOT command.

Posted: Sat Sep 03, 2022 6:30 am
by Demindiro
It's hard to tell what's wrong at a glance, but have you confirmed that the command ring works with No Op TRBs?

Also, it appears you're not assigning any scratchpad pages?

Re: xhci: OS crash after send ENABLE_SLOT command.

Posted: Sat Sep 03, 2022 9:44 pm
by longjin
Demindiro wrote:It's hard to tell what's wrong at a glance, but have you confirmed that the command ring works with No Op TRBs?

Also, it appears you're not assigning any scratchpad pages?
Yeah, the NOP trb can run properly after I ring the doorbell. And, is the scratchpad buffer necessary?

Re: xhci: OS crash after send ENABLE_SLOT command.

Posted: Sat Sep 03, 2022 11:08 pm
by Demindiro
longjin wrote:And, is the scratchpad buffer necessary?
It absolutely is. Imagine what would happen to your program if it had no memory / something started randomly overwriting memory in it :)

Emulators such as QEMU do not need scratchpad buffers since they have access to malloc() which is faster & safer anyways.

Re: xhci: OS crash after send ENABLE_SLOT command.

Posted: Sun Sep 04, 2022 12:06 am
by longjin
Demindiro wrote:
It absolutely is. Imagine what would happen to your program if it had no memory / something started randomly overwriting memory in it :)

Emulators such as QEMU do not need scratchpad buffers since they have access to malloc() which is faster & safer anyways.
I haven't thought about this before. The problem solved with your help, thank you!!! :D

May I ask you another question?

After I reset the port, and try to send ADDRESS_DEVICE again, something came wrong. I received an event TRB, which has completion code=17, trb type=33. It means a Parameter Error. (It can work on QEMU but the computer)
I only want to get the device descriptor, so I only set the slot_context and control EP context in the input_context. How can I solve this?
The code: https://github.com/fslongjin/DragonOS/b ... ci.c#L1379

Re: xhci: OS crash after send ENABLE_SLOT command.

Posted: Sun Sep 04, 2022 12:15 am
by Demindiro
longjin wrote:I received an event TRB, which has completion code=17, trb type=33. It means a Parameter Error. (It can work on QEMU but the computer)
QEMU is quite lax and ignores many of the parameters it doesn't need (unlike real hardware).

Parameter Error means you didn't properly configure one or more fields in the Input Context. Looking at your code it appears you're not initializing a lot of fields, such as the maximum packet size. If you set the same fields as I do it should work fine (my code is GPLv3 but I give you permission to use it).

Re: xhci: OS crash after send ENABLE_SLOT command.

Posted: Sun Sep 04, 2022 2:27 am
by longjin
Demindiro wrote: Looking at your code it appears you're not initializing a lot of fields, such as the maximum packet size. If you set the same fields as I do it should work fine (my code is GPLv3 but I give you permission to use it).
Hmm, what I do is, copy the slot_context and ep_context from device_context of the port to the input context directly. I think this should contain all the information we need. The function works at the first time. But at the second time, it failed.

Re: xhci: OS crash after send ENABLE_SLOT command.

Posted: Sun Sep 04, 2022 3:21 am
by longjin
And, I try to directly send the ADDRESS_DEVICE command with BSR=0 to the device (just comment out Line 1356~1375), and successfully got the descriptor.

So, why I cannot request the first 8 bytes and reset the device, then set address again, and request all 18 bytes?

Re: xhci: OS crash after send ENABLE_SLOT command.

Posted: Mon Sep 05, 2022 2:43 pm
by xeyes
longjin wrote: Hmm, what I do is, copy the slot_context and ep_context from device_context of the port to the input context directly. I think this should contain all the information we need.
Unfortunately, the controller takes the same shortcut. It copies most fields from the input context as is to the output context,
longjin wrote: So, why I cannot request the first 8 bytes and reset the device, then set address again, and request all 18 bytes?
I'm almost certain that you can. On the other hand, you could also send a evaluate context cmd instead of resetting the device.

Re: xhci: OS crash after send ENABLE_SLOT command.

Posted: Tue Sep 06, 2022 12:14 am
by longjin
xeyes wrote: I'm almost certain that you can. On the other hand, you could also send a evaluate context cmd instead of resetting the device.
Hmm, I'll try it. Thank u!!!