Bug 204521

Summary: [new driver] [request] Port rtsx from OpenBSD to FreeBSD
Product: Base System Reporter: evil.lombo
Component: kernAssignee: freebsd-bugs mailing list <bugs>
Status: In Progress ---    
Severity: Affects Many People CC: 0mp, Lutz.Bichler, ae, arrowd, bengta, chmiels, contact, danfe, dch, dirkx, emaste, flo, gljennjohn, greg, hlh, iain.holmes, info, jens.link, jkim, jlduran, jrmeadows, jsm, jyoung15, kamila, l.yotov, lme, lwhsu, ml+freebsd, nomoo, nwhitehorn, osa, sergey.dyatko, sh+freebsd-bugzilla, trueos, vangyzen, zeus.panchenko
Priority: --- Keywords: feature, patch
Version: CURRENT   
Hardware: Any   
OS: Any   
URL: https://lists.freebsd.org/pipermail/freebsd-questions/2015-February/264014.html
See Also: https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=161719
Attachments:
Description Flags
Patch to add SD card insertion/removal
gljennjohn: maintainer-approval?
can read scr
none
dyndbg output of linux kernel driver
none
minimal can still read SCR
none
read works. write does not yet
none
add stop cmd from req and reposition sync_dma
none
Card Inserted dmesg
none
do not read BIPR to often.. none

Description evil.lombo 2015-11-13 13:02:29 UTC
Hi,

in my quest to make FreeBSD fully operational in my laptop, I would like to know whether anyone is working on porting the OpenBSD rtsx to FreeBSD.

I have asked to Robert (https://lists.freebsd.org/pipermail/freebsd-questions/2015-February/264014.html) about its working on the module but he has not yet got the chance to start it.

If not, I'd try to develop it personally, but I have to be sure the better workflow: is it better to integrate it with sdhci/mmc modules or start it brand new?

Thanks,

Simone
Comment 1 Stefan Hagen 2018-06-14 11:39:43 UTC
Hello,

the request to port this driver came up again on freebsd-hackers again. Warner Losh gave some insight about what needs to be done to port rtsx.

https://lists.freebsd.org/pipermail/freebsd-hackers/2018-April/052520.html

This SD Card reader is built into a wide variety of Lenovo Laptops (x240 and up, t440/t540 and up, L440/L540 and up) Dell XPS Series, HP Spectre x360 and up.

Best Regards,
Stefan
Comment 2 Gleb Popov freebsd_committer 2018-09-15 15:06:37 UTC
Adding myself to CC, as I also have this card reader in my notebook.

Simone, do you still plan to work on this?
Comment 3 Gleb Popov freebsd_committer 2019-06-18 12:02:12 UTC
I have posted a bounty of $100 for this bug on BountySource: https://www.bountysource.com/issues/75687739-new-driver-request-port-rtsx-from-openbsd-to-freebsd

Hope this will give a spin.
Comment 4 Gleb Popov freebsd_committer 2019-07-03 10:07:22 UTC
*** Bug 161719 has been marked as a duplicate of this bug. ***
Comment 5 Kamila Součková 2019-11-28 10:52:10 UTC
Started work on this: https://github.com/AnotherKamila/freebsd-rtsz . Very much WIP right now. I intend to make progress on this over the weekend, please ping me if you can help with testing.
Comment 6 Gleb Popov freebsd_committer 2019-11-28 15:53:41 UTC
(In reply to Kamila Součková from comment #5)

Hi Kamilla, thanks for your interest. It worth noting that former FreeBSD member already started working on this, and it seems that he has made more progress than you. I'm not know if that means that you should abandon your work, though.
Comment 7 Kamila Součková 2019-11-28 16:06:22 UTC
(In reply to Gleb Popov from comment #6)

Hi, thank you for letting me know! Do you know who that was? I am aware of https://github.com/ruupert/rtsx/ , which seems to have been abandoned (and I am currently at the same stage, so also able to detect card presence, plus my code might be structured in a way that is more likely to be merged). Are you referring to this, or to something else?

Thanks a lot!
Comment 8 Gleb Popov freebsd_committer 2019-11-28 16:10:42 UTC
(In reply to Kamila Součková from comment #7)

His name is Gary Jennejohn, here is relevant thread on the mailing list: https://lists.freebsd.org/pipermail/freebsd-hackers/2019-November/055200.html

His work is not public, but he reported that has quite a progress.
Comment 9 Henri Hennebert 2019-11-28 18:34:31 UTC
I have also a try at this problem:

https://github.com/hlh-restart/rtsx
Comment 10 Gary Jennejohn 2019-11-29 09:56:46 UTC
To hlh

I've been working on this driver for about 3 weeks.  I looked at your code and it looks quite good.  I'll give it a try and send some feedback.
Comment 11 Gary Jennejohn 2019-11-29 11:26:10 UTC
(In reply to Gary Jennejohn from comment #10)
The driver from hlh is much further along than mine.  Loading the module with a SD card plugged in actually results in MMC seeing the card and reading its capacity.  Plugging in a card after the module is loaded results in a spurious interrupt error, but that can probably be fixed without much trouble.

I had to add ``#include <sys/mutex.h>'' to get it to compile in my FreeBSD13 tree.

It doesn't work too well with my 522A and I see timeouts waiting for interrupts quite often.

In any case, my opinion is that this driver is the way forward and hlh should continue working on it.
Comment 12 Henri Hennebert 2019-11-29 12:09:44 UTC
(In reply to Gary Jennejohn from comment #11)
The detection of a new card inserted after the driver is loaded will be dealt later.

My main problem is that any read io return all 0 on timeout.
So I postpone the write problem.

I try to read the source of openbsd, netbsd and linux to no avail for now.

I almost lost...

Henri
Comment 13 Gary Jennejohn 2019-11-29 14:48:57 UTC
(In reply to hlh from comment #12)

I noticed that, immediately after loading the module, I can successfully run e.g.
dd if=/dev/mmcsd0 of=junk count=20 without errors or timeouts.  But, as you say,
the bytes are all 0x00.  Any further attempt to access the SD card results in a
CRC error.

In fact, I see LOTS of CRC errors (all RTSX_SD_CRC16_ERR) during start up.  I also see CMD7 failing quite often.

I've spent many hours looking at the Linux driver, which was written by Realtek.
There is so much special handling for each of the controllers that it almost seems hopeless to get a driver which works as well as what Linux has.

Whether the errors I see are due to pecularities of the 522a (Linux has lots of special code for this one) I can't say.
Comment 14 Gary Jennejohn 2019-11-30 11:49:24 UTC
(In reply to hlh from comment #12)

I installed OpenBSD on a sacrifical disk and it also returns all 0s when I read from the SD card.  I know there are data on the SD card because I can dump them using a USB2 multi-card reader.

Evidently, OpenBSD does not support my 522A all too well.

Hard to tell whether any errors occur because there's no debug output from the driver.  But then again, I was able to run dd multiple times without an error being reported (which may mean nothing if the driver suppresses errors).
Comment 15 Gary Jennejohn 2019-12-02 16:49:15 UTC
(In reply to Gary Jennejohn from comment #14)

I installed Debian Linux to check whether my Raltek controller really works.

The first read I did from the SD card returned all 0s.

After playing around for some time I discovered that the first megabyte (2048 sectors) was filled with 0s.  Apparently a result of the vendor formatting it with FAT32.

So, OpenBSD may also work if I run dd with skip=2048.

This might also be the reason why hlh is seeing 0s when he reads from his card.

Unfortunately, with the code from hlh I still see CRC errors which prevent me from using the my controller.
Comment 16 Henri Hennebert 2019-12-02 17:30:16 UTC
(In reply to Gary Jennejohn from comment #15)

I dump the sd card on another device and I get:


# hd sdcard.data
00000000  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
000001b0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 82  |................|
000001c0  03 00 0c fe ff ff 00 20  00 00 00 04 b7 03 00 00  |...þÿÿ. ........|
000001d0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
000001f0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 55 aa  |..............Uª|
00000200  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00002000

With `sysctl debug.bootverbose=1` I display some part of the buffer
and it is all 0.

On my pc with rtsx I get:

# dd bs=512 if=/dev/mmcsd0 of=sd.dump count=20
20+0 records in
20+0 records out
10240 bytes transferred in 0.785274 secs (13040 bytes/sec)
# hd sd.dump 
00000000  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00002800

:-(
Comment 17 Gary Jennejohn 2019-12-03 07:44:24 UTC
(In reply to hlh from comment #16)

My Realtek 522A does work correctly under OpenBSD.

There is some difference in behavior between the rtsx code in OpenBSD and what there is in the FreeBSD code we now have.  For example, I do not see CRC errors with OpenBSD.

But there may also be differences between the mmc/mmcsd code in OpenBSD and the corresponding code in FreeBSD.  Hard to say.
Comment 18 Lars Engels freebsd_committer 2019-12-09 20:03:03 UTC
I have a Thinkpad T450s which has a rtsx reader supported by OpenBSD:

rtsx0 at pci1 dev 0 function 0 "Realtek RTS5227 Card Reader" rev 0x01: msi

I'm running FreeBSD 12.1 on it, so if I can test anything, ping me please.
Comment 19 Lars Engels freebsd_committer 2019-12-09 20:03:43 UTC
Change state to "In Progress" as now there are some people working on it.
Comment 20 Alexey Dokuchaev freebsd_committer 2019-12-10 03:21:00 UTC
I've given Henri's current GitHub code a spin today, on an RTS5227 (card=0x1992103c chip=0x522710ec).  I've built and loaded all three modules, with the card removed; it was not able to detect card insertion (every time I pushed the card in/out it just complained about spurious interrupt):

> rtsx0: <Realtek RTS5227 PCI MMC/SD Card Reader> mem 0xd4000000-0xd4000fff
> at device 0.0 on pci2
> rtsx0: Card removed
> rtsx0: Spurious interrupt - no active request
> rtsx0: Spurious interrupt - no active request
> rtsx0: Spurious interrupt - no active request
> rtsx0: Spurious interrupt - no active request
> rtsx0: Spurious interrupt - no active request
Things changed when I've unloaded rtsx.ko, plugged the card, and then loaded it again:

> rtsx0: <Realtek RTS5227 PCI MMC/SD Card Reader> mem 0xd4000000-0xd4000fff
> at device 0.0 on pci2
> rtsx0: Card inserted
> rtsx0: Attaching MMC bus failed [6]
> mmc0: <MMC/SD bus> on rtsx0
> mmcsd0: 129GB <SDHC 00000 1.0 SN A123456Z MFG 05/2014 by 27 SM> at mmc0
> 0.4MHz/1bit/256-block
> rtsx0: CRC error
> rtsx0: Soft reset
> mmcsd0: Error indicated: 2 Bad CRC
> rtsx0: CRC error
> rtsx0: Soft reset
> mmcsd0: Error indicated: 2 Bad CRC
> rtsx0: CRC error
> rtsx0: Soft reset
> mmcsd0: Error indicated: 2 Bad CRC
Those errors come from trying to read anything from /dev/mmcsd0 with dd(1), to no avail.  Sometimes it's possible to read one 512-byte sector off of it, but most of the times it yields an input/output error.
Comment 21 Gary Jennejohn 2019-12-10 08:13:23 UTC
(In reply to Alexey Dokuchaev from comment #20)
I've been looking at Henri's driver for some time.

Unfortunately, there are DMA errors when trying to read larger amounts of data from the SD card.  I've been adding debug output to the driver but haven't yet been able to pinpoint the cause of the errors.

The DMA errors result in the Controller Timeouts you've seen because no interrupt is being raised.

As a result e.g. CMD7 fails.  This is a very important command which is part of the card intialization process.

The SD card protocols are rather complex which makes understanding what is going on kind of difficult.

Since FreeBSD policy dictates that new drivers have to land in HEAD first, it will probably be some time before the driver appears in older versions.
Comment 22 Alexey Dokuchaev freebsd_committer 2019-12-10 08:55:12 UTC
(In reply to Gary Jennejohn from comment #21)
> Since FreeBSD policy dictates that new drivers have to land in HEAD first,
> it will probably be some time before the driver appears in older versions.
I guess this part was reply to Lars Engels from comment #18:
> I'm running FreeBSD 12.1 on it, so if I can test anything, ping me please.
I don't think 12.1 vs -CURRENT would cause a difference here; all one has to do is to clone Henri's GitHub repo, type "make", and play with it on any relatively recent FreeBSD version/branch.  There's even no need to wait for it to land in HEAD (which, given the current state of the driver, is not immediately foreseeable anyway).
Comment 23 Gary Jennejohn 2019-12-10 09:28:00 UTC
(In reply to Alexey Dokuchaev from comment #22)
Yes, that's a good point.  If users of older FreeBSD versions could clone the driver and give it a spin then we'd have some useful information.
Comment 24 Alexey Dokuchaev freebsd_committer 2019-12-12 01:16:39 UTC
Another strange observation which I didn't pay attention earlier: upon "kldload rtsx", the following lines appear in the kernel log before anything else:

> hdac1: <ATI (0x9902) HDA Controller> mem 0xd6044000-0xd6047fff at device 1.1
> on pci0
> hdac1: hdac_get_capabilities: Invalid corb size (0)
> device_attach: hdac1 attach returned 6
Admittedly, I have zero ideas what rtsx(4) has to do with HDA. :-/
Comment 25 Gary Jennejohn 2019-12-12 06:16:37 UTC
(In reply to Alexey Dokuchaev from comment #24)
rtsx has nothing at all to do with hda.  But the "kldload rtsx" results in the kernel rescanning all devices to find a match.

There were recently some changes in the hda code.  How current is your kernel?
Comment 26 Alexey Dokuchaev freebsd_committer 2019-12-12 06:41:17 UTC
(In reply to Gary Jennejohn from comment #25)
> But the "kldload rtsx" results in the kernel rescanning all devices
> to find a match.
That should explain it, thanks.

> There were recently some changes in the hda code.  How current is your kernel?
Not the very latest one (septemberish r352614), I should probably update, but a bit afraid of some potentially boot-breaking USB/CAM problems being reported on the lists*.

*) https://lists.freebsd.org/pipermail/freebsd-current/2019-December/074914.html
Comment 27 Gary Jennejohn 2019-12-12 06:57:59 UTC
(In reply to Alexey Dokuchaev from comment #26)
I sort of understand your reluctance.

I never install a new kernel over the top of the old kernel.  Instead I do this
(as root, of course) in a bash alias:

mkdir -p /boot/test
cd /usr/src
make -s installkernel KODIR=/boot/test
nextboot -k test
cd

Then a reboot will boot the new kernel from /boot/test.  If it fails the next boot will use the old, known-good kernel.

If it succeeds then you can simply run "make installkernel" so that the standard kernel in /boot/kernel will be updated and used.
Comment 28 Gary Jennejohn 2019-12-12 10:33:23 UTC
Created attachment 209887 [details]
Patch to add SD card insertion/removal

A minor patch to Henri's code to add SD card insertion/removal handling.  Doesn't fix the DMA problems, but I felt like doing something else.

Shamelessly stolen from dwmmc.c
Comment 29 Alexey Dokuchaev freebsd_committer 2019-12-12 10:49:06 UTC
(In reply to Gary Jennejohn from comment #28)
> A minor patch to Henri's code to add SD card insertion/removal handling.
Thanks, I confirm it does fix card detection problem for me.
Comment 30 Greg V 2019-12-12 12:05:03 UTC
(In reply to Gary Jennejohn from comment #27)

> I never install a new kernel over the top of the old kernel.  Instead I do this

installkernel does this for you! the old kernel is always moved to /boot/kernel.old and you can select that in the bootloader.
Comment 31 Henri Hennebert 2019-12-12 13:32:07 UTC
(In reply to Gary Jennejohn from comment #28)

I update the git repository with the patch

Thanks a lot
Henri
Comment 32 Gary Jennejohn 2019-12-12 14:21:06 UTC
(In reply to Greg V from comment #30)
I'm well aware of what installkernel does.  But my way saves me the trouble of telling the bootloader to boot from kernel.old if the new kernel fails.  I can simply reboot and the known-working kernel under /boot/kernel will be loaded.
Comment 33 Gary Jennejohn 2019-12-21 08:12:49 UTC
I tried implementing rtsx_xfer_bounce() from the OpenBSD code.

OpenBSD uses this function when the upstream code does not pass in a list of DMA scatter-gather addresses.  Normally, the SD code in OpenBSD allocates its own DMA buffers and passes them to the driver.

Since FreeBSD does not do that - it either uses buffers on the kernel stack or buffers from a struct bio - it seemed logical to me to try rtsx_xfer_bouce().

The DMA transfer raises an interrupt, but the target buffer is always filled with 0s.

I'm running out of ideas on how to get DMA to work.

If I had a laptop I could tear apart I'd put a logic analyzer on the controller pins and compare the behavior between OpenBSD and FreeBSD.  Unfortunately, the laptop I have is a loaner and I can't do that.
Comment 34 Gary Jennejohn 2020-01-08 13:37:37 UTC
I reimplemented rtsx_xfer_bounce() so that it's pretty much one-to-one the same as the way OpenBSD does it.  But now the transfer always times out.  The DMA addresses returned by the kernel all look good and are within the 32-bit range.

If anyone is interested I can attach my code.  It's full of ifdef's and trace output.  Maybe other eyes can see what I can't.  I've looked at the code so much that I may be blind to any errors.

Comparing OpenBSD with the code I'm using doesn't reveal any obvious errors.  The DMA setup for using bounce buffers appears to be the same and the steps preceeding the DMA transfer also seem identical.

I'm at a loss to explain why DMA in FreeBSD does not work.

What I haven't yet tried is to do the DMA setup like dwmmc.c does it.  Guess I'll try that next.
Comment 35 Lyubomir 2020-01-09 16:10:31 UTC
Hi Gary,
All the best to all for the New 2020!
I am not a developer so I will not be able to help with the code but I have access to Lenovo T450 (mine), to a logic analyzer SALEAE Logic pro 16 (not mine :)) and I can also find another hdd to install OpenBSD on it. I would like to help but I would definitely need your guidance.
Do you think this will work?

Best regards,

Lyubo
Comment 36 Gary Jennejohn 2020-01-10 08:54:42 UTC
(In reply to Lyubomir from comment #35)
Thanks for the offer.  I did some research and the Realtek controllers used in laptops are basically black boxes.  They consist of a module with the electronics and the SD card holder and then a rather long cable which gets routed to a connecotr on the motherboard.  I was not able to find any documentation on the cable pinout.  Without that information it would not be possible to connect the SALEAE logic analyzer (I have two of them) to get useful traces.  So my idea to do this was not so good.

At one time there were PCIe cards with suitable Realtek controllers, but I haven't been able to find any on e.g. eBay.  The cards now available no longer use Realtek.
Comment 37 Gary Jennejohn 2020-01-18 09:24:33 UTC
(In reply to hlh from comment #31)
You should add sc->rtsx_intr_status = 0; prior to the call to rtsx_wait_intr() in rtsx_xfer()to be consistent.  This is the only place where that was not being done.  It doesn't change the behavior, but it can prevent false positives in rtsx_wait_intr().
Comment 38 Henri Hennebert 2020-01-18 09:58:39 UTC
(In reply to Gary Jennejohn from comment #37)
Update done. Thanks
Comment 39 Sergey V. Dyatko 2020-01-23 20:49:37 UTC
Hi, I have lenovo thinkpad t470p with 
none4@pci0:4:0:0:       class=0xff0000 rev=0x01 hdr=0x00 vendor=0x10ec device=0x522a subvendor=0x17aa subdevice=0x505d
    vendor     = 'Realtek Semiconductor Co., Ltd.'
    device     = 'RTS522A PCI Express Card Reader'
and can test too
Comment 40 dirkx 2020-03-19 12:34:50 UTC
(In reply to Gary Jennejohn from comment #34)

if (len == 0) set it to min(maxbuff,len) at https://github.com/hlh-restart/rtsx/blob/fa2c5b66d00b7a4bec6eb0e5539b8edbd7a06a9b/rtsx.c#L1472 looks rather odd to me.
Comment 41 Gary Jennejohn 2020-03-19 13:46:26 UTC
(In reply to dirkx from comment #40)
The code is correct.  The {Net,Open}BSD upper layer code always sets xfer_len to the correct value.  FreeBSD does indeed have xfer_len, but the upper layer never sets it.  To simplify implementing the OpenBSD code (and avoid kernel panics) it was convenient for hlh to set xfer_len to a reasonable value.
I must admit that what's on github looks nothing like the code I've tested.  I've made massive changes to DMA code, added lots of variations on how transfers are done based on examining what OpneBSD and Linux do in various situations and added large amounts of debugging.  Unfortunately, nothing I've tried so far results in successful DMA transfers.
One test I did was to set a byte pattern in the virtual address of the DMA buffer, start the transfer (the controller claims that no errors occurred), and then dump the physical address of the buffer using /dev/mem.  The result was that NO bytes were transferred at all, i.e. the contents of the virtual and physical addresses were identical.
There is much more to the {Net,Open}BSD implementation than just the driver.  There is an upper layer which implements the SD Card Host Controller Specification.  It is somewhat different than what was implemented in FreeBSD.  I'm beginning to suspect that these differences may be contributing to the DMA problems.
Comment 42 Gary Jennejohn 2020-03-20 10:35:48 UTC
(In reply to hlh from comment #38)
The message from dirkx led me to look more closely at the OpenBSD code.
OpenBSD never sets intr_status to 0 before calling rtsx_wait_intr().
That makes sense because, theoretically, the interrupt could be raised before rtsx_wait_intr() is entered.  In that case, setting intr_status to 0 would overwrite its setting in the interrupt handler and result in a false timeout error.
I tested NOT setting intr_status to 0 and the driver works just as well (or badly) as before.  Actually, setting it to zero is not really necessary due to this code in rtsx_wait_intr(): sc->rtsx_intr_status &= ~status;
So, I suggest, but do not demand, at least commenting out the lines where intr_status is set to 0.  I leave it up to you.
Comment 43 Henri Hennebert 2020-03-20 13:18:19 UTC
(In reply to Gary Jennejohn from comment #42)

I am not sure this is a problem because sc->rtsx_intr_status is set
to 0 _before_ sending the command and so before the relevant
interrupt can be raised.
Comment 44 dirkx 2020-03-20 13:20:47 UTC
(In reply to hlh from comment #43)

So I put a few 'if (sc->rtsx_intr_status) printf..' just before the  sc->rtsx_intr_status=0; lines to check if there was (I assuse) something pending/non zero at the point this happens. And it seems to happy once or twice - exactly before the timeout.
Comment 45 dirkx 2020-03-20 13:21:18 UTC
(In reply to hlh from comment #43)

So I put a few 'if (sc->rtsx_intr_status) printf..' just before the  sc->rtsx_intr_status=0; lines to check if there was (I assuse) something pending/non zero at the point this happens. And it seems to happy once or twice - exactly before the timeout.
Comment 46 Henri Hennebert 2020-03-20 13:31:14 UTC
(In reply to dirkx from comment #44)
Strange, if you comment the sc->rtsx_intr_status=0 does the timeout disappear?
Comment 47 Gary Jennejohn 2020-03-20 13:57:50 UTC
(In reply to hlh from comment #46)
I see timeouts whether rtsx_intr_status is set to 0 or not.  But dirkx makes a valid point, if setting it to 0 occurs before the command is sent.  In any case, I didn't observe any adverse effects by not setting it to 0, but then again, setting it to 0 may also be harmless.
The timeouts semm to only occur when a transfer using rtsx_xfer_short() is attempted.  But these errors are only visible when boot_verbose is set to 1.
I also see quite a few CRC errors being generated (may be due to the SD card I'm using).
Comment 48 Henri Hennebert 2020-03-20 14:07:23 UTC
(In reply to Gary Jennejohn from comment #4(In reply to Gary Jennejohn from comment #47)

I also encounter CRC errors but if I boot with tails (https://tails.boum.org/)
I can play with the sdcard without problem. So CRC is a problem with the
Freebsd driver/system. Now I think that our problem is due to some wrong
initialization... But I can't find what :-(
Comment 49 Gary Jennejohn 2020-03-20 14:34:14 UTC
(In reply to hlh from comment #48)
Tails is Linux, and the Linux driver(s) were developed by Realtek.  Not surprising that it works.
I also tested with OpenBSD, which also works without error.
So I agree, something is missing in FreeBSD, although your driver seems to faithfully implement the OpenBSD rtsx.c.
But, as I wrote in a previous comment, there's a lot more to the OpenBSD code than just the driver itself.
Comment 50 Jesper Schmitz Mouridsen freebsd_committer 2020-05-06 05:24:32 UTC
Still does not work..

I looked into the bus width thing though.. it sets 1 bit even though capability is 4 bit
in mmc.c
mmc_app_decode_scr(uint32_t *raw_scr, struct mmc_scr *scr)
{
	unsigned int scr_struct;

	memset(scr, 0, sizeof(*scr));

	scr_struct = mmc_get_bits(raw_scr, 64, 60, 4);
	if (scr_struct != 0) {
		printf("Unrecognised SCR structure version %d\n",
		    scr_struct);
		return;
	}
	scr->sda_vsn = mmc_get_bits(raw_scr, 64, 56, 4);
	scr->bus_widths = mmc_get_bits(raw_scr, 64, 48, 4);
}
and in mmc_discover_cards function in mmc.c
if ((host_caps & MMC_CAP_4_BIT_DATA) &&
			    (ivar->scr.bus_widths & SD_SCR_BUS_WIDTH_4))
				ivar->bus_width = bus_width_4;

but bus_width is 1 and iirc 4 on openbsd so ivar->scr.bus_widths & SD_SCR_BUS_WIDTH_4 is false. Removing it sets the bus width to 4 bits, but still controller timeout.

so perhaps mmc_app_decode_scr does not apply to Realteks way of doing things?
Comment 51 Gary Jennejohn 2020-05-06 07:50:25 UTC
(In reply to Jesper Schmitz Mouridsen from comment #50)
AFAIKT the mmc code does what the standard requires.  The real problem is that SCR needs to read 64 bytes from the controller and the DMA fails so that the contents in the buffer are always 0.
Comment 52 Jesper Schmitz Mouridsen freebsd_committer 2020-05-21 15:07:36 UTC
looking hard at the linux driver dyndbg output I have managed to read the short data and getting bus witdh 4 from the SCR :-)
I will add a patch as soon as my code formatting is fixed..
Comment 53 Jesper Schmitz Mouridsen freebsd_committer 2020-05-21 17:17:52 UTC
Created attachment 214731 [details]
can read scr

This was the minimal changes I could make in order to read the scr.
The short data of data len 8 and 64  returns the same output as on openbsd.
I have not pin pointed the exact problem, but I'm using the cmd buffer more, and the sd_set_clock code has changed looking at the linux kernel. 
I hope some of you can have a look.
Comment 54 Gary Jennejohn 2020-05-21 17:49:13 UTC
(In reply to Jesper Schmitz Mouridsen from comment #53)
Thanks!  I'll look at it.  Note that your rtsx_pci_send_cmd() is basically the same as rtsx_send_cmd() already in the code.  The main difference is that your code synchronises the DMA buffers in the routine itself whereas the existing code does it after rtsx_send_cmd() returns.
Did you keep the Linux dyndbg output?  Could be interesting to look at.
Comment 55 Jesper Schmitz Mouridsen freebsd_committer 2020-05-21 18:18:56 UTC
Created attachment 214733 [details]
dyndbg output of linux kernel driver
Comment 56 Jesper Schmitz Mouridsen freebsd_committer 2020-05-21 18:21:18 UTC
(In reply to Gary Jennejohn from comment #54)
Yep rtsx_pci_send_cmd should be merged, note that it does not have mmc_cmd cmd as argument. The dmesg output is now attahced.
Comment 57 Gary Jennejohn 2020-05-22 06:44:42 UTC
(In reply to Jesper Schmitz Mouridsen from comment #56)
The mmc cmd is used to report an error to the MMC stack, if one occurs, so rtsx_send_cmd() would be the better choice in general.
Thanks for looking into this and for attaching the Linux output.
Comment 58 Jesper Schmitz Mouridsen freebsd_committer 2020-05-23 22:50:05 UTC
Created attachment 214797 [details]
minimal can still read SCR

looking closer I can now read SCR with one change.
RTSX_CLK_DIVIDE_MASK is zero also for RTSX_SDCLK_400KHZ.
Tested on 5209 and 5229

xfer besides dlen 8 and 64 still times out..
Comment 59 Henri Hennebert 2020-05-24 07:15:54 UTC
(In reply to Jesper Schmitz Mouridsen from comment #58)

I update git, thanks a lot for your time!
Comment 60 Gary Jennejohn 2020-05-24 07:58:31 UTC
(In reply to hlh from comment #59)
This is really excellent!  Next is to figure out how to do larger transfers.
Comment 61 Jesper Schmitz Mouridsen freebsd_committer 2020-05-25 10:24:59 UTC
Created attachment 214840 [details]
read works. write does not yet

Ok, this was trial and error inspecting what the linux kernel module does.
I even hardcoded a cfg2 value... so the quality can improve..
We need stil to make WRITE cmds work....
So DO NOT use on real data..
Comment 62 Gary Jennejohn 2020-05-25 11:42:00 UTC
(In reply to Jesper Schmitz Mouridsen from comment #61)
Well, you're making progress in any case.  The driver is in better shape than ever now.
Comment 63 Alexey Dokuchaev freebsd_committer 2020-05-25 12:03:52 UTC
Good progress indeed.  However, you might want to make those patches more conforming to style(9), e.g. use tabs instead of spaces for indentation, as this will simplify inclusion of this code into the FreeBSD codebase.
Comment 64 Henri Hennebert 2020-05-25 17:21:38 UTC
(In reply to Jesper Schmitz Mouridsen from comment #61)

Really good job!

git updated.

I do the tab cleaning and made it read only for now because I get a crash
if I load rtsx with gnome running and some write operation is requested by
some component of gnome.
Comment 65 Henri Hennebert 2020-05-25 17:28:50 UTC
(In reply to hlh from comment #64)

In the git update, I swap the implementations of rtsx_bus_power_up()
and rtsx_bus_power_on() instead of swapping the calls in rtsx_mmcbr_update_ios().
Comment 66 Alexey Dokuchaev freebsd_committer 2020-05-26 09:26:13 UTC
(In reply to hlh from comment #64)
> git updated.
Awesome!  I can confirm my RTS5227 (card=0x1992103c chip=0x522710ec) is able to read the SD card successfully now.
Comment 67 Henri Hennebert 2020-05-26 16:27:52 UTC
I complete/update the write code but when mounting a msdosfs I get:

panic: general protection fault
cpuid = 3
time = 1590504848
KDB: stack backtrace:
#0 0xffffffff80695005 at kdb_backtrace+0x65
#1 0xffffffff8064a7ab at vpanic+0x17b
#2 0xffffffff8064a623 at panic+0x43
#3 0xffffffff80a0b301 at trap_fatal+0x391
#4 0xffffffff80a0a787 at trap+0x67
#5 0xffffffff809e4728 at calltrap+0x8
#6 0xffffffff806f387e at bufwrite+0x1fe
#7 0xffffffff807294c0 at vn_fsync_buf+0x250
#8 0xffffffff80aaa84b at VOP_FSYNC_APV+0x7b
#9 0xffffffff806f3a0b at bufsync+0x3b
#10 0xffffffff8071410d at bufobj_invalbuf+0x1ad
#11 0xffffffff807173fe at vgonel+0x17e
#12 0xffffffff80717a46 at vgone+0x36
#13 0xffffffff8052a7f7 at devfs_delete+0x177
#14 0xffffffff8052ad36 at devfs_populate_loop+0x1e6
#15 0xffffffff8052ab3a at devfs_populate+0x2a
#16 0xffffffff8052fbbb at devfs_populate_vp+0x9b
#17 0xffffffff8052dc0b at devfs_lookup+0x2b
Uptime: 53m47s

with sysctl debug.bootverbose=1 :

rtsx0: rtsx_mmcbr_request(CMD17 arg 0x40001 flags 0x35 dlen 512 dflags 0x2)
rtsx0: rtsx_xfer_short() - Read xfer: 512 bytes with block size 512
rtsx0: rtsx_send_cmd()
rtsx0: Interrupt handler - enabled: 0x32000000, status: 0xa0010000
rtsx0: rtsx_send_cmd()
rtsx0: Interrupt handler - enabled: 0x32000000, status: 0xa0010000
rtsx0: rtsx_send_cmd()
rtsx0: Interrupt handler - enabled: 0x32000000, status: 0xa0010000
rtsx0: rtsx_mmcbr_release_host()
rtsx0: rtsx_mmcbr_acquire_host()
rtsx0: rtsx_mmcbr_request(CMD17 arg 0x40000 flags 0x35 dlen 512 dflags 0x2)
rtsx0: rtsx_xfer_short() - Read xfer: 512 bytes with block size 512
rtsx0: rtsx_send_cmd()
rtsx0: Interrupt handler - enabled: 0x32000000, status: 0xa0010000
rtsx0: rtsx_send_cmd()
rtsx0: Interrupt handler - enabled: 0x32000000, status: 0xa0010000
rtsx0: rtsx_send_cmd()
rtsx0: Interrupt handler - enabled: 0x32000000, status: 0xa0010000
rtsx0: rtsx_mmcbr_release_host()
rtsx0: rtsx_mmcbr_acquire_host()
rtsx0: rtsx_mmcbr_request(CMD17 arg 0x40001 flags 0x35 dlen 512 dflags 0x2)
rtsx0: rtsx_xfer_short() - Read xfer: 512 bytes with block size 512
rtsx0: rtsx_send_cmd()
rtsx0: Interrupt handler - enabled: 0x32000000, status: 0xa0010000
rtsx0: rtsx_send_cmd()
rtsx0: Interrupt handler - enabled: 0x32000000, status: 0xa0010000
rtsx0: rtsx_send_cmd()
rtsx0: Interrupt handler - enabled: 0x32000000, status: 0xa0010000
rtsx0: rtsx_mmcbr_release_host()
rtsx0: rtsx_mmcbr_acquire_host()
rtsx0: rtsx_mmcbr_request(CMD17 arg 0x40000 flags 0x35 dlen 512 dflags 0x2)
rtsx0: rtsx_xfer_short() - Read xfer: 512 bytes with block size 512
rtsx0: rtsx_send_cmd()
rtsx0: Interrupt handler - enabled: 0x32000000, status: 0xa0010000
rtsx0: rtsx_send_cmd()
rtsx0: Interrupt handler - enabled: 0x32000000, status: 0xa0010000
rtsx0: rtsx_send_cmd()
rtsx0: Interrupt handler - enabled: 0x32000000, status: 0xa0010000
rtsx0: rtsx_mmcbr_release_host()


Fatal trap 9: general protection fault while in kernel mode
Comment 68 Gary Jennejohn 2020-05-26 17:16:30 UTC
(In reply to hlh from comment #67)
Is the code already in git?
What happens if you mount read only?  I ask that because the fault is happening in bufwrite.
Comment 69 Henri Hennebert 2020-05-26 18:48:29 UTC
(In reply to Gary Jennejohn from comment #68)

The code is already in git.

I think that a mount read only _should_ work.

To be sure see line 1681-1682 to enforce only read requests. Than
you must mount read only.
Comment 70 Gary Jennejohn 2020-05-27 08:40:37 UTC
(In reply to hlh from comment #69)
Unfortunately, with my RTS522A I don't get as far as you do.
The very first CMD18 which reads 8192 bytes with a blocksize of 512 appears to succeed.  The MMC stack then immediately repeats the CMD18 which results in a CRC error.  After that ALL reads result in a CRC error no matter what the transfer size is.
Looks like the soft reset is not clearing the CRC error.  There must be a way to do that, I would think.
Of course, the SD card itself might be the problem.  I'll try a different card and report back.
But at least /dev/mmcsd0 was created :)
Comment 71 Gary Jennejohn 2020-05-27 08:50:11 UTC
(In reply to Gary Jennejohn from comment #70)
No, the card is not the problem.  Exactly the same behavior with a different card.
Comment 72 Lutz Bichler 2020-05-27 09:42:38 UTC
I have to change RTSX_TM_AUTO_READ2 to RTSX_TM_AUTO_READ1 in line 1698 to enable read-only mounts with a RTS522A.
Comment 73 Henri Hennebert 2020-05-27 10:15:30 UTC
(In reply to Lutz Bichler from comment #72)

I will replace READ2 by READ1: both are working for my RTL8411B.
Comment 74 Henri Hennebert 2020-05-27 10:54:03 UTC
On my Realtek RTL8411B write 512 bytes blocks is working - rtsx_xfer_short():

dd if=some_file of=/dev/mmcsd0 bs=512 count=x

dd if=/dev/mmcsd0 bs=512 count=x

With a bs > 512 [rtsx_xfer()] I get interrupt RTSX_TRANS_FAIL_INT :-(
Comment 75 Jesper Schmitz Mouridsen freebsd_committer 2020-05-27 12:45:39 UTC
(In reply to hlh from comment #74)
I can write with bs>512 on 5209 with tmode AUTO_WRITE4 instead of tmode AUTO_WRITE3, but I can not read after I have done a write operation.
Comment 76 Henri Hennebert 2020-05-27 12:57:04 UTC
(In reply to Jesper Schmitz Mouridsen from comment #75)

I update gitub.

I modify rtsx_xfer() according to linux sd_write_long_data()
https://elixir.bootlin.com/linux/v4.3.1/source/drivers/mmc/host/rtsx_pci_sdmmc.c#L499

I can write one block but the next write get a controller timeout and
all following read timeout too. I reset with kldunload rtsx && kldload rtsx.
Comment 77 Gary Jennejohn 2020-05-27 13:09:43 UTC
(In reply to hlh from comment #76)
If I limit MMCBR_IVAR_MAX_DATA to 1, i.e. max transfer of 512 bytes, in rtsx_read_ivar() then I can mount, read from and write to the SD card with no errors.
The change results in all transfers being done with rtsx_xfer_short(), of course.
So, rtsx_xfer() still needs some work.
Don't give up, you're making good progress.
Comment 78 Gary Jennejohn 2020-05-27 14:24:29 UTC
I notice that the driver currently limits the SD clock to a maximum of 50MHz.
In the Linux trace the clock is set to 208MHz.
I wonder whether the clock rate is contributing to the errors seen when rtsx_xfer() is used.  In particular, CMD18 trying to read 8192 bytes in a single transfer may be too fast for the controller/SD card to acheive at a lower clock rate.
Comment 79 Jesper Schmitz Mouridsen freebsd_committer 2020-05-27 14:38:28 UTC
I can make CMD25 succeed by adding 

  bus_dmamap_sync(sc->rtsx_data_dma_tag, sc->rtsx_data_dmamap, BUS_DMASYNC_POSTREAD);
        bus_dmamap_sync(sc->rtsx_data_dma_tag, sc->rtsx_data_dmamap, BUS_DMASYNC_POSTWRITE);

before the cmd->data->data is memcpy'ed to the data dma buffer.

to the current git source...

*BUT* the follwing/subsequent CMD17 and CMD18 fails..
Comment 80 Jesper Schmitz Mouridsen freebsd_committer 2020-05-27 14:41:40 UTC
(In reply to Jesper Schmitz Mouridsen from comment #79)

I meant
    bus_dmamap_sync(sc->rtsx_data_dma_tag, sc->rtsx_data_dmamap, BUS_DMASYNC_PREREAD);
    bus_dmamap_sync(sc->rtsx_data_dma_tag, sc->rtsx_data_dmamap, BUS_DMASYNC_PREWRITE);
Comment 81 Henri Hennebert 2020-05-27 14:47:05 UTC
(In reply to Jesper Schmitz Mouridsen from comment #80)

I try this but as before only the first write complete and then Controller timeout for subsequent operations.
Comment 82 Jesper Schmitz Mouridsen freebsd_committer 2020-05-27 15:37:34 UTC
(In reply to hlh from comment #81)
It works for me now 

	if (read)
        memcpy(cmd->data->data, sc->rtsx_data_dmamem, cmd->data->len);
    else
        rtsx_send_req_get_resp(sc,sc->rtsx_req->stop);
Comment 83 Henri Hennebert 2020-05-27 15:44:58 UTC
(In reply to Jesper Schmitz Mouridsen from comment #82)

WELL DONE!

Where do you find this trick?

Github updated!
Comment 84 Jesper Schmitz Mouridsen freebsd_committer 2020-05-27 15:46:13 UTC
Created attachment 214925 [details]
add stop cmd from req and reposition sync_dma

enables reads after write..
Comment 85 Jesper Schmitz Mouridsen freebsd_committer 2020-05-27 15:47:21 UTC
(In reply to hlh from comment #83)
Thanks,
it was in the dwmmc code...

} else if (sc->flags & PENDING_STOP && !sc->use_auto_stop) {
		sc->flags &= ~PENDING_STOP;
		dwmmc_start_cmd(sc, req->stop);
		return;
	}
Comment 86 Henri Hennebert 2020-05-27 16:07:23 UTC
With The current githup I can

gpart create -s GPT /dev/mmcsd0
gpart add -t freebsd-zfs /dev/mmcsd0
zpool create TEST /dev/mmcsd0p1
...populate the pool...
zpool scrub
zpool status TEST
  pool: TEST
 state: ONLINE
  scan: scrub repaired 0 in 0 days 00:00:17 with 0 errors on Wed May 27 18:01:58 2020
config:

	NAME        STATE     READ WRITE CKSUM
	TEST        ONLINE       0     0     0
	  mmcsd0p1  ONLINE       0     0     0

errors: No known data errors

This is a wonderful day!
Comment 87 Henri Hennebert 2020-05-27 16:16:51 UTC
(In reply to Jesper Schmitz Mouridsen from comment #84)
The bus_dmamap_sync() don't need to be positioned. I keep it at the 'old' place to be coherent with the rest of the code.
Comment 88 Gary Jennejohn 2020-05-27 16:59:07 UTC
Yay!  It works with my 522A also.  People with different controllers need to test this too.  If there are enough reports of success then maybe one of the FreeBSD committers following this (I think they're all ports committers) can convince a src committer to commit it to HEAD.
Comment 89 Lars Engels freebsd_committer 2020-05-27 17:06:42 UTC
Thats awesome teamwork! 

I will test this on my T450s tomorrow and report back.
Comment 90 Eric van Gyzen freebsd_committer 2020-05-27 17:12:00 UTC
I'm a src committer.  Let me know when you think it's ready, and I'll take a look.
Comment 91 Dave Cottlehuber freebsd_committer 2020-05-27 17:16:32 UTC
Amazing! can we get all of this into a single patch (or a git branch)? I'm not
clear which ones of the attachments are needed. I can try it out on my laptop, but I'll need to build it on a bigger box first, before upgrading from 12.1R. Nice idea to try out a zpool, I'll do that too on my Dell XPS13.
Comment 92 Henri Hennebert 2020-05-27 17:19:34 UTC
(In reply to Dave Cottlehuber from comment #91)

Just checkout from https://github.com/hlh-restart/rtsx the latest master.
Comment 93 Jung-uk Kim freebsd_committer 2020-05-27 17:22:33 UTC
(In reply to Eric van Gyzen from comment #90)
I can do it, too.  However, we need a) a manual page, i.e., rtsx(4) and b) thorough style(9) compliance check.  rtsx(4) doesn't have to be complete but we need something, e.g., supported devices, authors, origin, etc.  See sdhci(4) for example.
Comment 94 Henri Hennebert 2020-05-27 17:26:27 UTC
(In reply to Jung-uk Kim from comment #93)

I will do it in the following days. It will be something interesting for me to learn to do a man page ;-)
Comment 95 Florian Smeets freebsd_committer 2020-05-27 18:13:16 UTC
FWIW, another successful test. Hardware is an Intel NUC8i5BE

rtsx0@pci0:2:0:0:	class=0xff0000 rev=0x01 hdr=0x00 vendor=0x10ec device=0x522a subvendor=0x8086 subdevice=0x2074
    vendor     = 'Realtek Semiconductor Co., Ltd.'
    device     = 'RTS522A PCI Express Card Reader'


copied a couple of GB to an SD card successfully, while rsyncing src to it and checking out a ports tree.

Great work. Thanks.
Comment 96 Zeus Panchenko 2020-05-27 21:35:54 UTC
successful test on HP Probook 430 g2

many many thanks!

commit d605a27259dd147f866b4e2640f6289323c2a8c7

> dmesg
rtsx0: <Realtek RTS5227 PCI MMC/SD Card Reader> mem 0xc1200000-0xc1200fff at device 0.0 on pci2
rtsx0: Card inserted/removed
rtsx0: No request running
mmc0: <MMC/SD bus> on rtsx0
mmcsd0: 2GB <SD SD02G 3.8 SN A58756A6 MFG 05/2009 by 2 TM> at mmc0 50.0MHz/4bit/256-block

> pciconf -lv
rtsx0@pci0:7:0:0:       class=0xff0000 card=0x2246103c chip=0x522710ec rev=0x01 hdr=0x00
    vendor     = 'Realtek Semiconductor Co., Ltd.'
    device     = 'RTS5227 PCI Express Card Reader'

> mount_msdosfs /dev/mmcsd0s1 /mnt/da0

> /usr/bin/time -h cp -r /usr/src/tools /mnt/da0/tmp/
7m56.35s (on linux 10m8s)

> /usr/bin/time -h mv /mnt/da0/tmp/tools /tmp
5m1.65s (on linux 1m28s)
Comment 97 Gary Jennejohn 2020-05-28 08:24:30 UTC
It would be useful if people were to include the FreeBSD version with which they tested.  If we can get reports of success with FreeBSD 11/12/13 then that might facilitate getting the driver merged with older versions.
I did all my testing using FreeBSD13.
Comment 98 Henri Hennebert 2020-05-28 08:31:50 UTC
(In reply to Gary Jennejohn from comment #97)

I do all developments and testing on

FreeBSD 12.1-STABLE #0 r360996

with Realtek RTL8411B PCI MMC/SD Card Reader
Comment 99 dirkx 2020-05-28 08:38:52 UTC
(In reply to hlh from comment #98)

Stock FreeBSD 12.1-RELEASE-p2 on a Realtek TRTS522A PCI reader.

Tested against several MSDOS/fat and ZFS formatted cards 4GB-64GB used in cross windows/macosx setting.
Comment 100 Gary Jennejohn 2020-05-28 08:43:43 UTC
(In reply to hlh from comment #98)
An excellent idea to also include the controller model.
I have the RTL522A.

It would be nice if we could get some tests with less common controllers like the 5209 and 525A.
Comment 101 Bengt Ahlgren 2020-05-28 09:58:53 UTC
(In reply to Gary Jennejohn from comment #97)
It does not compile on 11.4-STABLE:

rtsx.c:2072:21: error: use of undeclared identifier 'IO_SEND_OP_COND'
        if (cmd->opcode == IO_SEND_OP_COND &&

Copying that definition from 12-STABLE (sys/dev/mmc/mmcreg.h) makes it compile.

I only made a very simple test with one card, creating one file and read it back without issues.

I have a Thinkpad X250 with:

rtsx0@pci0:2:0:0:       class=0xff0000 card=0x222617aa chip=0x522710ec rev=0x01 hdr=0x00
    vendor     = 'Realtek Semiconductor Co., Ltd.'
    device     = 'RTS5227 PCI Express Card Reader'

Great work, thanks a lot!
Comment 102 Gary Jennejohn 2020-05-28 10:17:57 UTC
(In reply to Bengt Ahlgren from comment #101)
Thanks!
This should be easy to fix with an #ifndef in rtsxreg.h since that gets included after the mmc header files.
Comment 103 Sergey V. Dyatko 2020-05-28 11:08:50 UTC
Hi, 
[tiger@laptop]:/tmp%uname -a
FreeBSD laptop.domain 13.0-CURRENT FreeBSD 13.0-CURRENT #1 r356767M: Sun Feb  9 23:29:26 +03 2020     root@laptop.domain:/usr/obj/usr/src/amd64.amd64/sys/GENERIC-NODEBUG  amd64

_without inserted card_

git clone https://github.com/hlh-restart/rtsx.git
cd rtsx
make
kldload ./rtsx.ko as root
dmesg:


rtsx0: <Realtek RTS522A PCI MMC/SD Card Reader> mem 0xf2100000-0xf2100fff at device 0.0 on pci4
mmc0: <MMC/SD bus> on rtsx0
rtsx0: Controller timeout
rtsx0: Soft reset
rtsx0: Controller timeout
rtsx0: Soft reset
rtsx0: Controller timeout
rtsx0: Soft reset
rtsx0: Controller timeout
rtsx0: Soft reset
rtsx0: Controller timeout
rtsx0: Soft reset
rtsx0: Controller timeout
rtsx0: Soft reset
rtsx0: Controller timeout
rtsx0: Soft reset
rtsx0: Controller timeout
rtsx0: Soft reset
rtsx0: Controller timeout
rtsx0: Soft reset
rtsx0: Controller timeout
rtsx0: Soft reset
rtsx0: Controller timeout
rtsx0: Soft reset
rtsx0: Controller timeout
rtsx0: Soft reset
mmc0: No compatible cards found on bus

should I insert card before kldload ?
Comment 104 Henri Hennebert 2020-05-28 11:18:22 UTC
(In reply to Sergey V. Dyatko from comment #103)

It work with no card inserted before kldload rtsx.

At least on Freebsd 12.1-STABLE with RTL8411B.

Maybe it's the card. Anyway try 
kldunload rtsx
inter card
kldload rtsx
Comment 105 Jesper Schmitz Mouridsen freebsd_committer 2020-05-28 11:22:59 UTC
(In reply to Sergey V. Dyatko from comment #103)
Can you replicate the problem,is it periodic or does it happen all the time?

Can you set sysctl hw.mmc.debug=9
and debug.bootverbose=1 and send the output.
Comment 106 Jesper Schmitz Mouridsen freebsd_committer 2020-05-28 11:31:57 UTC
with the module loaded inserting/removing the card from the reader repeatedly and fast results in the same errors.
Comment 107 Sergey V. Dyatko 2020-05-28 11:33:35 UTC
(In reply to Jesper Schmitz Mouridsen from comment #105)
laptop# kldunload rtsx
laptop#
<dmesg cut>
mmc0: detached
rtsx0: detached
pci4: <unknown> at device 0.0 (no driver attached)
</dmesg cut>
laptop# sysctl hw.mmc.debug=9
hw.mmc.debug: 0 -> 9
laptop# sysctl debug.bootverbose=1 
debug.bootverbose: 0 -> 1
laptop# kldload ./rtsx.ko
load: 0.33  cmd: kldload 96161 [rtsxintr] 1.73r 0.00u 0.05s 0% 1808k
load: 0.33  cmd: kldload 96161 [rtsxintr] 2.03r 0.00u 0.05s 0% 1808k
load: 0.33  cmd: kldload 96161 [rtsxintr] 2.22r 0.00u 0.06s 0% 1808k
load: 0.33  cmd: kldload 96161 [rtsxintr] 2.40r 0.00u 0.06s 0% 1808k
load: 0.33  cmd: kldload 96161 [rtsxintr] 2.57r 0.00u 0.06s 0% 1808k
load: 0.33  cmd: kldload 96161 [rtsxintr] 2.74r 0.00u 0.06s 0% 1808k
load: 0.33  cmd: kldload 96161 [rtsxintr] 2.91r 0.00u 0.06s 0% 1808k
load: 0.33  cmd: kldload 96161 [rtsxintr] 3.09r 0.00u 0.06s 0% 1808k
load: 0.33  cmd: kldload 96161 [rtsxintr] 3.27r 0.00u 0.06s 0% 1808k
load: 0.33  cmd: kldload 96161 [rtsxintr] 3.45r 0.00u 0.06s 0% 1808k
dmesg:
pci0: driver added                                                                                                                                                          
found-> vendor=0x8086, dev=0xa131, revid=0x31                                                                                                                                                                       
        domain=0, bus=0, slot=20, func=2                                                                                                                                                                            
        class=11-80-00, hdrtype=0x00, mfdev=0                                                                                                                                                                       
        cmdreg=0x0002, statreg=0x0010, cachelnsz=0 (dwords)                                                                                                                                                         
        lattimer=0x00 (0 ns), mingnt=0x00 (0 ns), maxlat=0x00 (0 ns)                                                                                                                                                
        intpin=c, irq=255                                                                                                                                                                               
        powerspec 3  supports D0 D3  current D0                                                                                                                                                                     
        MSI supports 1 message                                                                                                                                                                                      
pci0:0:20:2: reprobing on driver added 
found-> vendor=0x8086, dev=0xa13a, revid=0x31                                                                                                                                                                       
        domain=0, bus=0, slot=22, func=0                                                                                                                                                                            
        class=07-80-00, hdrtype=0x00, mfdev=1                                                                                                                                                                       
        cmdreg=0x0002, statreg=0x0010, cachelnsz=0 (dwords)                                                                                                                                                         
        lattimer=0x00 (0 ns), mingnt=0x00 (0 ns), maxlat=0x00 (0 ns)                                                                                                                                                
        intpin=a, irq=255                                                                                                                                                                               
        powerspec 3  supports D0 D3  current D0                                                                                                                                                                     
        MSI supports 1 message, 64 bit                                                                                                                                                                              
pci0:0:22:0: reprobing on driver added                                                                                                                                                                              
found-> vendor=0x8086, dev=0xa121, revid=0x31                                                                                                                                                                       
        domain=0, bus=0, slot=31, func=2                                                                                                                                                                            
        class=05-80-00, hdrtype=0x00, mfdev=1                                                                                                                                                                       
        cmdreg=0x0002, statreg=0x0000, cachelnsz=0 (dwords)                                                                                                                                                         
        lattimer=0x00 (0 ns), mingnt=0x00 (0 ns), maxlat=0x00 (0 ns)                                                                                                                                                
pci0:0:31:2: reprobing on driver added                                                                                                                                                                              
found-> vendor=0x8086, dev=0xa123, revid=0x31                                                                                                                                                                       
        domain=0, bus=0, slot=31, func=4                                                                                                                                                                            
        class=0c-05-00, hdrtype=0x00, mfdev=0                                                                                                                                                                       
        cmdreg=0x0003, statreg=0x0280, cachelnsz=0 (dwords)                                                                                                                                                         
        lattimer=0x00 (0 ns), mingnt=0x00 (0 ns), maxlat=0x00 (0 ns)                                                                                                                                                
        intpin=a, irq=255                                                                                                                                                                               
pci0:0:31:4: reprobing on driver added                                                                                                                                                                              
pci1: driver added                                                                                                                                                          
pci2: driver added                                                                                                                                                          
pci3: driver added                                                                                                                                                          
pci4: driver added                                                                                                                                                          
found-> vendor=0x10ec, dev=0x522a, revid=0x01                                                                                                                                                                       
        domain=0, bus=4, slot=0, func=0                                                                                                                                                                             
        class=ff-00-00, hdrtype=0x00, mfdev=0                                                                                                                                                                       
        cmdreg=0x0406, statreg=0x0010, cachelnsz=0 (dwords)                                                                                                                                                         
        lattimer=0x00 (0 ns), mingnt=0x00 (0 ns), maxlat=0x00 (0 ns)                                                                                                                                                
        intpin=a, irq=255                                                                                                                                                                               
        powerspec 3  supports D0 D1 D2 D3  current D0                                                                                                                                                               
        MSI supports 1 message, 64 bit         
                                   pci0:4:0:0: reprobing on driver added                                                                                                                                                                               
rtsx0: <Realtek RTS522A PCI MMC/SD Card Reader> mem 0xf2100000-0xf2100fff at device 0.0 on pci4                                                                                                                     
rtsx0: Attach - Vendor ID: 0x10ec - Device ID: 0x522a                                                                                                                                                               
rtsx0: attempting to allocate 1 MSI vectors (1 supported)                                                                                                                                                           
msi: routing MSI IRQ 138 to local APIC 2 vector 49                                                                                                                                                                  
rtsx0: using IRQ 138 for MSI                                                                                                                                                                                        
rtsx0: rtsx_irq_res_id: 1 - rtsx_res_id: 16                                                                                                                                                                         
rtsx0: rtsx_init() rtsx_flags = 0x0000                                                                                                                                                                              
rtsx0: Card inserted                                                                                                                                                                
rtsx0: Device attached                                                                                                                                                                      
pci5: driver added                                                                                                                                                          
mmc0: <MMC/SD bus> on rtsx0                                                                                                                                                                                     
rtsx0: rtsx_mmcbr_acquire_host()                                                                                                                                                                                    
rtsx0: Read ivar #9, value 0 / #0                                                                                                                                                                                   
rtsx0: Write ivar #7, value 0x1 / #1                                                                                                                                                                                
rtsx0: Read ivar #6, value 0x3c0000 / #3932160                                                                                                                                                                      
rtsx0: Write ivar #11, value 0x15 / #21                                                                                                                                                                             
rtsx0: Write ivar #0, value 0x1 / #1                                                                                                                                                                                
rtsx0: Write ivar #2, value 0 / #0                                                                                                                                                                                  
rtsx0: Write ivar #1, value 0 / #0                                                                                                                                                                                  
rtsx0: Write ivar #9, value 0x1 / #1                                                                                                                                                                                
rtsx0: Write ivar #3, value 0 / #0                                                                                                                                                                                  
rtsx0: rtsx_mmcbr_update_ios()                                                                                                                                                                                      
rtsx0: rtsx_bus_power_up()                                                                                                                                                                                  
rtsx0: Write ivar #12, value 0x2 / #2                                                                                                                                                                               
rtsx0: rtsx_mmcbr_switch_vccq(330)                                                                                                                                                                                  
rtsx0: Write ivar #3, value 0x61a80 / #400000                                                                                                                                                                       
rtsx0: Write ivar #14, value 0 / #0                                                                                                                                                                                 
rtsx0: Write ivar #9, value 0x2 / #2                                                                                                                                                                                
rtsx0: rtsx_mmcbr_update_ios()                                                                                                                                                                                      
rtsx0: rtsx_set_sd_clock(400000)                                                                                                                                                                                    
rtsx0: Write ivar #0, value 0x2 / #2                                                                                                                                                                                
mmc0: Probing bus                                                                                                                                                       
rtsx0: Write ivar #2, value 0x1 / #1                                                                                                                                                                                
rtsx0: rtsx_mmcbr_update_ios()                                                                                                                                                                                      
mmc0: REQUEST: CMD0 arg 0 flags 0x40                                                                                                                                                                                
rtsx0: rtsx_mmcbr_request(CMD0 arg 0 flags 0x40 dlen 0 dflags 0)                                                                                                                                                    
rtsx0: rtsx_send_cmd()                                                                                                                                                                      
rtsx0: Interrupt handler - enabled: 0x32000000, status: 0xa0010000                                                                                                                                                  
mmc0: CMD0 RESULT: 0                                                                                                                                                                
rtsx0: Write ivar #2, value 0 / #0                                                                                                                                                                                  
rtsx0: rtsx_mmcbr_update_ios()                                                                            
mmc0: REQUEST: CMD8 arg 0x1aa flags 0x75                                                                                                                                                                            
rtsx0: rtsx_mmcbr_request(CMD8 arg 0x1aa flags 0x75 dlen 0 dflags 0)                                                                                                                                                
rtsx0: rtsx_send_cmd()                                                                                                                                                                      
rtsx0: Controller timeout                                                                                                                                                                               
rtsx0: Soft reset                                                                                                                                                       
mmc0: CMD8 RESULT: 1                                                                                                                                                                
mmc0: REQUEST: CMD8 arg 0x1aa flags 0x75                                                                                                                                                                            
rtsx0: rtsx_mmcbr_request(CMD8 arg 0x1aa flags 0x75 dlen 0 dflags 0)                                                                                                                                                
rtsx0: rtsx_send_cmd()                                                                                                                                                                      
rtsx0: Controller timeout                                                                                                                                                                               
rtsx0: Soft reset                                                                                                                                                       
mmc0: CMD8 RESULT: 1                                                                                                                                                                
mmc0: REQUEST: CMD8 arg 0x1aa flags 0x75                                                                                                                                                                            
rtsx0: rtsx_mmcbr_request(CMD8 arg 0x1aa flags 0x75 dlen 0 dflags 0)                                                                                                                                                
rtsx0: rtsx_send_cmd()                                                                                                                                                                      
rtsx0: Controller timeout                                                                                                                                                                               
rtsx0: Soft reset                                                                                                                                                       
mmc0: CMD8 RESULT: 1                                                                                                                                                                
mmc0: REQUEST: CMD8 arg 0x1aa flags 0x75                                                                                                                                                                            
rtsx0: rtsx_mmcbr_request(CMD8 arg 0x1aa flags 0x75 dlen 0 dflags 0)                                                                                                                                                
rtsx0: rtsx_send_cmd()                                                                                                                                                                      
rtsx0: Controller timeout                                                                                                                                                                               
rtsx0: Soft reset                                                                                                                                                       
mmc0: CMD8 RESULT: 1                                                                                                                                                                
mmc0: REQUEST: CMD55 arg 0 flags 0x15                                                                                                                                                                               
rtsx0: rtsx_mmcbr_request(CMD55 arg 0 flags 0x15 dlen 0 dflags 0)                                                                                                                                                   
rtsx0: rtsx_send_cmd()                                                                                                                                                                      
rtsx0: Controller timeout                                                                                                                                                                               
rtsx0: Soft reset                                                                                                                                                       
mmc0: CMD55 RESULT: 1                                                                                                                                                                   
mmc0: REQUEST: CMD55 arg 0 flags 0x15                                                                                                                                                                               
rtsx0: rtsx_mmcbr_request(CMD55 arg 0 flags 0x15 dlen 0 dflags 0)                                                                                                                                                   
rtsx0: rtsx_send_cmd()                                                                                                                                                                      
rtsx0: Controller timeout                                                                                                                                                                               
rtsx0: Soft reset                                                                                                                                                       
mmc0: CMD55 RESULT: 1                                                                                                                                                                   
mmc0: REQUEST: CMD55 arg 0 flags 0x15                                                       
rtsx0: rtsx_mmcbr_request(CMD55 arg 0 flags 0x15 dlen 0 dflags 0)                                                                                                                                                   
rtsx0: rtsx_send_cmd()                                                                                                                                                                      
rtsx0: Controller timeout
rtsx0: Soft reset
mmc0: CMD55 RESULT: 1
mmc0: REQUEST: CMD55 arg 0 flags 0x15
rtsx0: rtsx_mmcbr_request(CMD55 arg 0 flags 0x15 dlen 0 dflags 0)
rtsx0: rtsx_send_cmd()
rtsx0: Controller timeout
rtsx0: Soft reset
mmc0: CMD55 RESULT: 1
mmc0: SD probe: failed
rtsx0: Write ivar #7, value 0 / #0
mmc0: REQUEST: CMD1 arg 0 flags 0x61
rtsx0: rtsx_mmcbr_request(CMD1 arg 0 flags 0x61 dlen 0 dflags 0)
rtsx0: rtsx_send_cmd()
rtsx0: Controller timeout
rtsx0: Soft reset
mmc0: CMD1 RESULT: 1
mmc0: REQUEST: CMD1 arg 0 flags 0x61
rtsx0: rtsx_mmcbr_request(CMD1 arg 0 flags 0x61 dlen 0 dflags 0)
rtsx0: rtsx_send_cmd()
rtsx0: Controller timeout
rtsx0: Soft reset
mmc0: CMD1 RESULT: 1
mmc0: REQUEST: CMD1 arg 0 flags 0x61
rtsx0: rtsx_mmcbr_request(CMD1 arg 0 flags 0x61 dlen 0 dflags 0)
rtsx0: rtsx_send_cmd()
rtsx0: Controller timeout
rtsx0: Soft reset
mmc0: CMD1 RESULT: 1
mmc0: REQUEST: CMD1 arg 0 flags 0x61
rtsx0: rtsx_mmcbr_request(CMD1 arg 0 flags 0x61 dlen 0 dflags 0)
rtsx0: rtsx_send_cmd()
rtsx0: Controller timeout
rtsx0: Soft reset
mmc0: CMD1 RESULT: 1
mmc0: MMC probe: failed
rtsx0: Write ivar #8, value 0 / #0
rtsx0: Read ivar #8, value 0 / #0
rtsx0: Read ivar #8, value 0 / #0
mmc0: Current OCR: 0x00000000
rtsx0: Read ivar #8, value 0 / #0
mmc0: No compatible cards found on bus
rtsx0: Write ivar #0, value 0x1 / #1
rtsx0: Write ivar #2, value 0 / #0
rtsx0: Write ivar #1, value 0 / #0
rtsx0: Write ivar #9, value 0 / #0
rtsx0: Write ivar #3, value 0 / #0
rtsx0: Write ivar #14, value 0 / #0
rtsx0: rtsx_mmcbr_update_ios()
rtsx0: rtsx_set_sd_clock(0)
rtsx0: rtsx_bus_power_off()
rtsx0: rtsx_mmcbr_release_host()

2hlh@restart.be: i'll try with card inserted in the evening, enocard right now :(
Comment 108 Zeus Panchenko 2020-05-28 11:47:45 UTC
(In addition to Zeus Panchenko comment #96)

FreeBSD 12.1-RELEASE-p1
Comment 109 jyoung15 2020-05-28 13:12:44 UTC
For another point of reference, it is still not working for me on a Lenovo P50s with a RTS522A.  The /dev/mmcsd0 file doesn't appear.  Using latest rtsx in github.

$ freebsd-version -kru
12.1-RELEASE-p5
12.1-RELEASE-p5
12.1-RELEASE-p5

$ pciconf -lv pci0:2:0:0    
rtsx0@pci0:2:0:0:       class=0xff0000 card=0x223317aa chip=0x522a10ec rev=0x01 hdr=0x00
    vendor     = 'Realtek Semiconductor Co., Ltd.'
    device     = 'RTS522A PCI Express Card Reader'

The dmesg output with hw.mmc.debug=9 and debug.bootverbose=1 is shown below, after doing kldload rtsx, then removing/inserting the card.


[163] pci0: driver added
[163] found->	vendor=0x8086, dev=0x1911, revid=0x00
[163] 	domain=0, bus=0, slot=8, func=0
[163] 	class=08-80-00, hdrtype=0x00, mfdev=0
[163] 	cmdreg=0x0002, statreg=0x0010, cachelnsz=0 (dwords)
[163] 	lattimer=0x00 (0 ns), mingnt=0x00 (0 ns), maxlat=0x00 (0 ns)
[163] 	intpin=a, irq=255
[163] 	powerspec 2  supports D0 D3  current D0
[163] 	MSI supports 1 message
[163] pci0:0:8:0: reprobing on driver added
[163] found->	vendor=0x8086, dev=0x9d31, revid=0x21
[163] 	domain=0, bus=0, slot=20, func=2
[163] 	class=11-80-00, hdrtype=0x00, mfdev=0
[163] 	cmdreg=0x0002, statreg=0x0010, cachelnsz=0 (dwords)
[163] 	lattimer=0x00 (0 ns), mingnt=0x00 (0 ns), maxlat=0x00 (0 ns)
[163] 	intpin=c, irq=255
[163] 	powerspec 3  supports D0 D3  current D0
[163] 	MSI supports 1 message
[163] pci0:0:20:2: reprobing on driver added
[163] found->	vendor=0x8086, dev=0x9d3a, revid=0x21
[163] 	domain=0, bus=0, slot=22, func=0
[163] 	class=07-80-00, hdrtype=0x00, mfdev=1
[163] 	cmdreg=0x0002, statreg=0x0010, cachelnsz=0 (dwords)
[163] 	lattimer=0x00 (0 ns), mingnt=0x00 (0 ns), maxlat=0x00 (0 ns)
[163] 	intpin=a, irq=255
[163] 	powerspec 3  supports D0 D3  current D0
[163] 	MSI supports 1 message, 64 bit
[163] pci0:0:22:0: reprobing on driver added
[163] found->	vendor=0x8086, dev=0x9d21, revid=0x21
[163] 	domain=0, bus=0, slot=31, func=2
[163] 	class=05-80-00, hdrtype=0x00, mfdev=1
[163] 	cmdreg=0x0002, statreg=0x0000, cachelnsz=0 (dwords)
[163] 	lattimer=0x00 (0 ns), mingnt=0x00 (0 ns), maxlat=0x00 (0 ns)
[163] pci0:0:31:2: reprobing on driver added
[163] found->	vendor=0x8086, dev=0x9d23, revid=0x21
[163] 	domain=0, bus=0, slot=31, func=4
[163] 	class=0c-05-00, hdrtype=0x00, mfdev=0
[163] 	cmdreg=0x0003, statreg=0x0280, cachelnsz=0 (dwords)
[163] 	lattimer=0x00 (0 ns), mingnt=0x00 (0 ns), maxlat=0x00 (0 ns)
[163] 	intpin=a, irq=255
[163] pci0:0:31:4: reprobing on driver added
[163] pci1: driver added
[163] found->	vendor=0x10ec, dev=0x522a, revid=0x01
[163] 	domain=0, bus=2, slot=0, func=0
[163] 	class=ff-00-00, hdrtype=0x00, mfdev=0
[163] 	cmdreg=0x0406, statreg=0x0010, cachelnsz=0 (dwords)
[163] 	lattimer=0x00 (0 ns), mingnt=0x00 (0 ns), maxlat=0x00 (0 ns)
[163] 	intpin=a, irq=255
[163] 	powerspec 3  supports D0 D1 D2 D3  current D0
[163] 	MSI supports 1 message, 64 bit
[163] pci0:2:0:0: reprobing on driver added
[163] rtsx0: <Realtek RTS522A PCI MMC/SD Card Reader> mem 0xf4100000-0xf4100fff at device 0.0 on pci1
[163] rtsx0: Attach - Vendor ID: 0x10ec - Device ID: 0x522a
[163] rtsx0: attempting to allocate 1 MSI vectors (1 supported)
[163] msi: routing MSI IRQ 266 to local APIC 0 vector 59
[163] rtsx0: using IRQ 266 for MSI
[163] rtsx0: rtsx_irq_res_id: 1 - rtsx_res_id: 16
[163] rtsx0: rtsx_init() rtsx_flags = 0x0000
[163] rtsx0: Device attached
[163] pci2: driver added
[163] pci3: driver added
[186] rtsx0: Interrupt handler - enabled: 0x32000000, status: 0x2010000
[186] rtsx0: Card inserted/removed
[186] rtsx0: No request running
[187] rtsx0: Card inserted
[187] mmc0: <MMC/SD bus> on rtsx0
[187] rtsx0: rtsx_mmcbr_acquire_host()
[187] rtsx0: Read ivar #9, value 0 / #0
[187] rtsx0: Write ivar #7, value 0x1 / #1
[187] rtsx0: Read ivar #6, value 0x3c0000 / #3932160
[187] rtsx0: Write ivar #11, value 0x15 / #21
[187] rtsx0: Write ivar #0, value 0x1 / #1
[187] rtsx0: Write ivar #2, value 0 / #0
[187] rtsx0: Write ivar #1, value 0 / #0
[187] rtsx0: Write ivar #9, value 0x1 / #1
[187] rtsx0: Write ivar #3, value 0 / #0
[187] rtsx0: rtsx_mmcbr_update_ios()
[187] rtsx0: rtsx_bus_power_up()
[187] rtsx0: Write ivar #12, value 0x2 / #2
[187] rtsx0: rtsx_mmcbr_switch_vccq(330)
[187] rtsx0: Write ivar #3, value 0x61a80 / #400000
[187] rtsx0: Write ivar #14, value 0 / #0
[187] rtsx0: Write ivar #9, value 0x2 / #2
[187] rtsx0: rtsx_mmcbr_update_ios()
[187] rtsx0: rtsx_set_sd_clock(400000)
[187] rtsx0: Write ivar #0, value 0x2 / #2
[187] mmc0: Probing bus
[187] rtsx0: Write ivar #2, value 0x1 / #1
[187] rtsx0: rtsx_mmcbr_update_ios()
[187] mmc0: REQUEST: CMD0 arg 0 flags 0x40
[187] rtsx0: rtsx_mmcbr_request(CMD0 arg 0 flags 0x40 dlen 0 dflags 0)
[187] rtsx0: rtsx_send_cmd()
[187] rtsx0: Interrupt handler - enabled: 0x32000000, status: 0xa0090000
[187] mmc0: CMD0 RESULT: 0
[187] rtsx0: Write ivar #2, value 0 / #0
[187] rtsx0: rtsx_mmcbr_update_ios()
[187] mmc0: REQUEST: CMD8 arg 0x1aa flags 0x75
[187] rtsx0: rtsx_mmcbr_request(CMD8 arg 0x1aa flags 0x75 dlen 0 dflags 0)
[187] rtsx0: rtsx_send_cmd()
[189] rtsx0: Controller timeout
[189] rtsx0: Soft reset
[189] mmc0: CMD8 RESULT: 1
[189] mmc0: REQUEST: CMD8 arg 0x1aa flags 0x75
[189] rtsx0: rtsx_mmcbr_request(CMD8 arg 0x1aa flags 0x75 dlen 0 dflags 0)
[189] rtsx0: rtsx_send_cmd()
[191] rtsx0: Controller timeout
[191] rtsx0: Soft reset
[191] mmc0: CMD8 RESULT: 1
[191] mmc0: REQUEST: CMD8 arg 0x1aa flags 0x75
[191] rtsx0: rtsx_mmcbr_request(CMD8 arg 0x1aa flags 0x75 dlen 0 dflags 0)
[191] rtsx0: rtsx_send_cmd()
[193] rtsx0: Interrupt handler - enabled: 0x32000000, status: 0x82080000
[193] rtsx0: Card inserted/removed
[193] rtsx0: No DMA transfert pending
[193] rtsx0: Controller timeout
[193] rtsx0: Soft reset
[193] mmc0: CMD8 RESULT: 1
[193] mmc0: REQUEST: CMD8 arg 0x1aa flags 0x75
[193] rtsx0: rtsx_mmcbr_request(CMD8 arg 0x1aa flags 0x75 dlen 0 dflags 0)
[193] rtsx0: rtsx_send_cmd()
[193] rtsx0: Interrupt handler - enabled: 0x32000000, status: 0xa0080000
[193] rtsx0: cmd_buffer: 0x00000868 0x0000aa01 0x7dabff00 0x7dacff01 0x7dadffaa
[193] rtsx0: cmd->resp = 0x000001aa 0x00000000 0x00000000 0x00000000
[193] mmc0: CMD8 RESULT: 0
[193] mmc0: SD 2.0 interface conditions: OK
[193] mmc0: REQUEST: CMD55 arg 0 flags 0x15
[193] rtsx0: rtsx_mmcbr_request(CMD55 arg 0 flags 0x15 dlen 0 dflags 0)
[193] rtsx0: rtsx_send_cmd()
[193] rtsx0: Interrupt handler - enabled: 0x32000000, status: 0xa0080000
[193] rtsx0: cmd_buffer: 0x00003768 0x00002001 0x7dabff00 0x7dacff00 0x7dadff00
[193] rtsx0: cmd->resp = 0x00000120 0x00000000 0x00000000 0x00000000
[193] mmc0: CMD55 RESULT: 0
[193] mmc0: REQUEST: CMD41 arg 0 flags 0x61
[193] rtsx0: rtsx_mmcbr_request(CMD41 arg 0 flags 0x61 dlen 0 dflags 0)
[193] rtsx0: rtsx_send_cmd()
[193] rtsx0: Interrupt handler - enabled: 0x32000000, status: 0xa0080000
[193] rtsx0: cmd_buffer: 0xff003f68 0x00000080 0x7dabff00 0x7dacff00 0x7dadff00
[193] rtsx0: cmd->resp = 0x00ff8000 0x00000000 0x00000000 0x00000000
[193] mmc0: CMD41 RESULT: 0
[193] mmc0: SD probe: OK (OCR: 0x00ff8000)
[193] rtsx0: Write ivar #8, value 0xff8000 / #16744448
[193] rtsx0: Read ivar #8, value 0xff8000 / #16744448
[193] rtsx0: Write ivar #2, value 0x1 / #1
[193] rtsx0: rtsx_mmcbr_update_ios()
[193] mmc0: REQUEST: CMD0 arg 0 flags 0x40
[193] rtsx0: rtsx_mmcbr_request(CMD0 arg 0 flags 0x40 dlen 0 dflags 0)
[193] rtsx0: rtsx_send_cmd()
[193] rtsx0: Interrupt handler - enabled: 0x32000000, status: 0xa0080000
[193] mmc0: CMD0 RESULT: 0
[193] rtsx0: Write ivar #2, value 0 / #0
[193] rtsx0: rtsx_mmcbr_update_ios()
[193] rtsx0: Read ivar #8, value 0xff8000 / #16744448
[193] mmc0: Current OCR: 0x00ff8000
[193] rtsx0: Read ivar #8, value 0xff8000 / #16744448
[193] rtsx0: Read ivar #7, value 0x1 / #1
[193] mmc0: REQUEST: CMD8 arg 0x1aa flags 0x75
[193] rtsx0: rtsx_mmcbr_request(CMD8 arg 0x1aa flags 0x75 dlen 0 dflags 0)
[193] rtsx0: rtsx_send_cmd()
[193] rtsx0: Interrupt handler - enabled: 0x32000000, status: 0xa0080000
[193] rtsx0: cmd_buffer: 0x00000868 0x0000aa01 0x7dabff00 0x7dacff01 0x7dadffaa
[193] rtsx0: cmd->resp = 0x000001aa 0x00000000 0x00000000 0x00000000
[193] mmc0: CMD8 RESULT: 0
[193] rtsx0: Read ivar #8, value 0xff8000 / #16744448
[193] mmc0: REQUEST: CMD55 arg 0 flags 0x15
[193] rtsx0: rtsx_mmcbr_request(CMD55 arg 0 flags 0x15 dlen 0 dflags 0)
[193] rtsx0: rtsx_send_cmd()
[193] rtsx0: Interrupt handler - enabled: 0x32000000, status: 0xa0080000
[193] rtsx0: cmd_buffer: 0x00003768 0x00002001 0x7dabff00 0x7dacff00 0x7dadff00
[193] rtsx0: cmd->resp = 0x00000120 0x00000000 0x00000000 0x00000000
[193] mmc0: CMD55 RESULT: 0
[193] mmc0: REQUEST: CMD41 arg 0x40ff8000 flags 0x61
[193] rtsx0: rtsx_mmcbr_request(CMD41 arg 0x40ff8000 flags 0x61 dlen 0 dflags 0)
[193] rtsx0: rtsx_send_cmd()
[193] rtsx0: Interrupt handler - enabled: 0x32000000, status: 0xa0080000
[193] rtsx0: cmd_buffer: 0xff003f68 0x00000080 0x7dabffff 0x7dacff80 0x7dadff00
[193] rtsx0: cmd->resp = 0x00ff8000 0x00000000 0x00000000 0x00000000
[193] mmc0: CMD41 RESULT: 0
[193] mmc0: REQUEST: CMD55 arg 0 flags 0x15
[193] rtsx0: rtsx_mmcbr_request(CMD55 arg 0 flags 0x15 dlen 0 dflags 0)
[193] rtsx0: rtsx_send_cmd()
[193] rtsx0: Interrupt handler - enabled: 0x32000000, status: 0xa0080000
[193] rtsx0: cmd_buffer: 0x00003768 0x00002001 0x7dabff00 0x7dacff00 0x7dadff00
[193] rtsx0: cmd->resp = 0x00000120 0x00000000 0x00000000 0x00000000
[193] mmc0: CMD55 RESULT: 0
[193] mmc0: REQUEST: CMD41 arg 0x40ff8000 flags 0x61
[193] rtsx0: rtsx_mmcbr_request(CMD41 arg 0x40ff8000 flags 0x61 dlen 0 dflags 0)
[193] rtsx0: rtsx_send_cmd()
[193] rtsx0: Interrupt handler - enabled: 0x32000000, status: 0xa0080000
[193] rtsx0: cmd_buffer: 0xff003f68 0x00000080 0x7dabffff 0x7dacff80 0x7dadff00
[193] rtsx0: cmd->resp = 0x00ff8000 0x00000000 0x00000000 0x00000000
[193] mmc0: CMD41 RESULT: 0
[193] mmc0: REQUEST: CMD55 arg 0 flags 0x15
[193] rtsx0: rtsx_mmcbr_request(CMD55 arg 0 flags 0x15 dlen 0 dflags 0)
[193] rtsx0: rtsx_send_cmd()
[193] rtsx0: Interrupt handler - enabled: 0x32000000, status: 0xa0080000
[193] rtsx0: cmd_buffer: 0x00003768 0x00002001 0x7dabff00 0x7dacff00 0x7dadff00
[193] rtsx0: cmd->resp = 0x00000120 0x00000000 0x00000000 0x00000000
[193] mmc0: CMD55 RESULT: 0
[193] mmc0: REQUEST: CMD41 arg 0x40ff8000 flags 0x61
[193] rtsx0: rtsx_mmcbr_request(CMD41 arg 0x40ff8000 flags 0x61 dlen 0 dflags 0)
[193] rtsx0: rtsx_send_cmd()
[193] rtsx0: Interrupt handler - enabled: 0x32000000, status: 0xa0080000
[193] rtsx0: cmd_buffer: 0xff003f68 0x00000080 0x7dabffff 0x7dacff80 0x7dadff00
[193] rtsx0: cmd->resp = 0x00ff8000 0x00000000 0x00000000 0x00000000
[193] mmc0: CMD41 RESULT: 0
[193] mmc0: REQUEST: CMD55 arg 0 flags 0x15
[193] rtsx0: rtsx_mmcbr_request(CMD55 arg 0 flags 0x15 dlen 0 dflags 0)
[193] rtsx0: rtsx_send_cmd()
[193] rtsx0: Interrupt handler - enabled: 0x32000000, status: 0xa0080000
[193] rtsx0: cmd_buffer: 0x00003768 0x00002001 0x7dabff00 0x7dacff00 0x7dadff00
[193] rtsx0: cmd->resp = 0x00000120 0x00000000 0x00000000 0x00000000
[193] mmc0: CMD55 RESULT: 0
[193] mmc0: REQUEST: CMD41 arg 0x40ff8000 flags 0x61
[193] rtsx0: rtsx_mmcbr_request(CMD41 arg 0x40ff8000 flags 0x61 dlen 0 dflags 0)
[193] rtsx0: rtsx_send_cmd()
[193] rtsx0: Interrupt handler - enabled: 0x32000000, status: 0xa0080000
[193] rtsx0: cmd_buffer: 0xff003f68 0x00000080 0x7dabffff 0x7dacff80 0x7dadff00
[193] rtsx0: cmd->resp = 0x00ff8000 0x00000000 0x00000000 0x00000000
[193] mmc0: CMD41 RESULT: 0
[193] mmc0: REQUEST: CMD55 arg 0 flags 0x15
[193] rtsx0: rtsx_mmcbr_request(CMD55 arg 0 flags 0x15 dlen 0 dflags 0)
[193] rtsx0: rtsx_send_cmd()
[193] rtsx0: Interrupt handler - enabled: 0x32000000, status: 0xa0080000
[193] rtsx0: cmd_buffer: 0x00003768 0x00002068 0x7dabff00 0x7dacff00 0x7dadff00
[193] rtsx0: cmd->resp = 0x00006820 0x00000000 0x00000000 0x00000000
[193] mmc0: CMD55 RESULT: 0
[193] mmc0: REQUEST: CMD41 arg 0x40ff8000 flags 0x61
[193] rtsx0: rtsx_mmcbr_request(CMD41 arg 0x40ff8000 flags 0x61 dlen 0 dflags 0)
[193] rtsx0: rtsx_send_cmd()
[193] rtsx0: Interrupt handler - enabled: 0x32000000, status: 0xa0080000
[193] rtsx0: cmd_buffer: 0xff003f68 0x00000080 0x7dabffff 0x7dacff80 0x7dadff00
[193] rtsx0: cmd->resp = 0x00ff8000 0x00000000 0x00000000 0x00000000
[193] mmc0: CMD41 RESULT: 0
[193] mmc0: REQUEST: CMD55 arg 0 flags 0x15
[193] rtsx0: rtsx_mmcbr_request(CMD55 arg 0 flags 0x15 dlen 0 dflags 0)
[193] rtsx0: rtsx_send_cmd()
[193] rtsx0: Interrupt handler - enabled: 0x32000000, status: 0xa0080000
[193] rtsx0: cmd_buffer: 0x00003768 0x00002001 0x7dabff00 0x7dacff00 0x7dadff00
[193] rtsx0: cmd->resp = 0x00000120 0x00000000 0x00000000 0x00000000
[193] mmc0: CMD55 RESULT: 0
[193] mmc0: REQUEST: CMD41 arg 0x40ff8000 flags 0x61
[193] rtsx0: rtsx_mmcbr_request(CMD41 arg 0x40ff8000 flags 0x61 dlen 0 dflags 0)
[193] rtsx0: rtsx_send_cmd()
[193] rtsx0: Interrupt handler - enabled: 0x32000000, status: 0xa0080000
[193] rtsx0: cmd_buffer: 0xff003f68 0x00000080 0x7dabffff 0x7dacff80 0x7dadff00
[193] rtsx0: cmd->resp = 0x00ff8000 0x00000000 0x00000000 0x00000000
[193] mmc0: CMD41 RESULT: 0
[193] mmc0: REQUEST: CMD55 arg 0 flags 0x15
[193] rtsx0: rtsx_mmcbr_request(CMD55 arg 0 flags 0x15 dlen 0 dflags 0)
[193] rtsx0: rtsx_send_cmd()
[193] rtsx0: Interrupt handler - enabled: 0x32000000, status: 0xa0080000
[193] rtsx0: cmd_buffer: 0x00003768 0x00002001 0x7dabff00 0x7dacff00 0x7dadff00
[193] rtsx0: cmd->resp = 0x00000120 0x00000000 0x00000000 0x00000000
[193] mmc0: CMD55 RESULT: 0
[193] mmc0: REQUEST: CMD41 arg 0x40ff8000 flags 0x61
[193] rtsx0: rtsx_mmcbr_request(CMD41 arg 0x40ff8000 flags 0x61 dlen 0 dflags 0)
[193] rtsx0: rtsx_send_cmd()
[193] rtsx0: Interrupt handler - enabled: 0x32000000, status: 0xa0080000
[193] rtsx0: cmd_buffer: 0xff003f68 0x00000080 0x7dabffff 0x7dacff80 0x7dadff00
[193] rtsx0: cmd->resp = 0x00ff8000 0x00000000 0x00000000 0x00000000
[193] mmc0: CMD41 RESULT: 0
[193] mmc0: REQUEST: CMD55 arg 0 flags 0x15
[193] rtsx0: rtsx_mmcbr_request(CMD55 arg 0 flags 0x15 dlen 0 dflags 0)
[193] rtsx0: rtsx_send_cmd()
[193] rtsx0: Interrupt handler - enabled: 0x32000000, status: 0xa0080000
[193] rtsx0: cmd_buffer: 0x00003768 0x00002001 0x7dabff00 0x7dacff00 0x7dadff00
[193] rtsx0: cmd->resp = 0x00000120 0x00000000 0x00000000 0x00000000
[193] mmc0: CMD55 RESULT: 0
[193] mmc0: REQUEST: CMD41 arg 0x40ff8000 flags 0x61
[193] rtsx0: rtsx_mmcbr_request(CMD41 arg 0x40ff8000 flags 0x61 dlen 0 dflags 0)
[193] rtsx0: rtsx_send_cmd()
[193] rtsx0: Interrupt handler - enabled: 0x32000000, status: 0xa0080000
[193] rtsx0: cmd_buffer: 0xff003f68 0x00000080 0x7dabffff 0x7dacff80 0x7dadff00
[193] rtsx0: cmd->resp = 0x00ff8000 0x00000000 0x00000000 0x00000000
[193] mmc0: CMD41 RESULT: 0
[193] mmc0: REQUEST: CMD55 arg 0 flags 0x15
[193] rtsx0: rtsx_mmcbr_request(CMD55 arg 0 flags 0x15 dlen 0 dflags 0)
[193] rtsx0: rtsx_send_cmd()
[193] rtsx0: Interrupt handler - enabled: 0x32000000, status: 0xa0080000
[193] rtsx0: cmd_buffer: 0x00003768 0x00002068 0x7dabff00 0x7dacff00 0x7dadff00
[193] rtsx0: cmd->resp = 0x00006820 0x00000000 0x00000000 0x00000000
[193] mmc0: CMD55 RESULT: 0
[193] mmc0: REQUEST: CMD41 arg 0x40ff8000 flags 0x61
[193] rtsx0: rtsx_mmcbr_request(CMD41 arg 0x40ff8000 flags 0x61 dlen 0 dflags 0)
[193] rtsx0: rtsx_send_cmd()
[193] rtsx0: Interrupt handler - enabled: 0x32000000, status: 0xa0080000
[193] rtsx0: cmd_buffer: 0xff003f68 0x00000080 0x7dabffff 0x7dacff80 0x7dadff00
[193] rtsx0: cmd->resp = 0x00ff8000 0x00000000 0x00000000 0x00000000
[193] mmc0: CMD41 RESULT: 0
[193] mmc0: REQUEST: CMD55 arg 0 flags 0x15
[193] rtsx0: rtsx_mmcbr_request(CMD55 arg 0 flags 0x15 dlen 0 dflags 0)
[193] rtsx0: rtsx_send_cmd()
[193] rtsx0: Interrupt handler - enabled: 0x32000000, status: 0xa0080000
[193] rtsx0: cmd_buffer: 0x00003768 0x00002001 0x7dabff00 0x7dacff00 0x7dadff00
[193] rtsx0: cmd->resp = 0x00000120 0x00000000 0x00000000 0x00000000
[193] mmc0: CMD55 RESULT: 0
[193] mmc0: REQUEST: CMD41 arg 0x40ff8000 flags 0x61
[193] rtsx0: rtsx_mmcbr_request(CMD41 arg 0x40ff8000 flags 0x61 dlen 0 dflags 0)
[193] rtsx0: rtsx_send_cmd()
[193] rtsx0: Interrupt handler - enabled: 0x32000000, status: 0xa0080000
[193] rtsx0: cmd_buffer: 0xff003f68 0x00000068 0x7dabffff 0x7dacff80 0x7dadff00
[193] rtsx0: cmd->resp = 0x00ff6800 0x00000000 0x00000000 0x00000000
[193] mmc0: CMD41 RESULT: 0
[193] mmc0: REQUEST: CMD55 arg 0 flags 0x15
[193] rtsx0: rtsx_mmcbr_request(CMD55 arg 0 flags 0x15 dlen 0 dflags 0)
[193] rtsx0: rtsx_send_cmd()
[193] rtsx0: Interrupt handler - enabled: 0x32000000, status: 0xa0080000
[193] rtsx0: cmd_buffer: 0x00003768 0x00002001 0x7dabff00 0x7dacff00 0x7dadff00
[193] rtsx0: cmd->resp = 0x00000120 0x00000000 0x00000000 0x00000000
[193] mmc0: CMD55 RESULT: 0
[193] mmc0: REQUEST: CMD41 arg 0x40ff8000 flags 0x61
[193] rtsx0: rtsx_mmcbr_request(CMD41 arg 0x40ff8000 flags 0x61 dlen 0 dflags 0)
[193] rtsx0: rtsx_send_cmd()
[193] rtsx0: Interrupt handler - enabled: 0x32000000, status: 0xa0080000
[193] rtsx0: cmd_buffer: 0xff003f68 0x00000080 0x7dabffff 0x7dacff80 0x7dadff00
[193] rtsx0: cmd->resp = 0x00ff8000 0x00000000 0x00000000 0x00000000
[193] mmc0: CMD41 RESULT: 0
[193] mmc0: REQUEST: CMD55 arg 0 flags 0x15
[193] rtsx0: rtsx_mmcbr_request(CMD55 arg 0 flags 0x15 dlen 0 dflags 0)
[193] rtsx0: rtsx_send_cmd()
[193] rtsx0: Interrupt handler - enabled: 0x32000000, status: 0xa0080000
[193] rtsx0: cmd_buffer: 0x00003768 0x00002001 0x7dabff00 0x7dacff00 0x7dadff00
[193] rtsx0: cmd->resp = 0x00000120 0x00000000 0x00000000 0x00000000
[193] mmc0: CMD55 RESULT: 0
[193] mmc0: REQUEST: CMD41 arg 0x40ff8000 flags 0x61
[193] rtsx0: rtsx_mmcbr_request(CMD41 arg 0x40ff8000 flags 0x61 dlen 0 dflags 0)
[193] rtsx0: rtsx_send_cmd()
[193] rtsx0: Interrupt handler - enabled: 0x32000000, status: 0xa0080000
[193] rtsx0: cmd_buffer: 0xff003f68 0x00000080 0x7dabffff 0x7dacff80 0x7dadff00
[193] rtsx0: cmd->resp = 0x00ff8000 0x00000000 0x00000000 0x00000000
[193] mmc0: CMD41 RESULT: 0
[193] mmc0: REQUEST: CMD55 arg 0 flags 0x15
[193] rtsx0: rtsx_mmcbr_request(CMD55 arg 0 flags 0x15 dlen 0 dflags 0)
[193] rtsx0: rtsx_send_cmd()
[193] rtsx0: Interrupt handler - enabled: 0x32000000, status: 0xa0080000
[193] rtsx0: cmd_buffer: 0x00003768 0x00002001 0x7dabff00 0x7dacff00 0x7dadff00
[193] rtsx0: cmd->resp = 0x00000120 0x00000000 0x00000000 0x00000000
[193] mmc0: CMD55 RESULT: 0
[193] mmc0: REQUEST: CMD41 arg 0x40ff8000 flags 0x61
[193] rtsx0: rtsx_mmcbr_request(CMD41 arg 0x40ff8000 flags 0x61 dlen 0 dflags 0)
[193] rtsx0: rtsx_send_cmd()
[193] rtsx0: Interrupt handler - enabled: 0x32000000, status: 0xa0080000
[193] rtsx0: cmd_buffer: 0xff403f68 0x00000080 0x7dabffff 0x7dacff80 0x7dadff00
[193] rtsx0: cmd->resp = 0x40ff8000 0x00000000 0x00000000 0x00000000
[193] mmc0: CMD41 RESULT: 0
[193] mmc0: REQUEST: CMD55 arg 0 flags 0x15
[193] rtsx0: rtsx_mmcbr_request(CMD55 arg 0 flags 0x15 dlen 0 dflags 0)
[193] rtsx0: rtsx_send_cmd()
[193] rtsx0: Interrupt handler - enabled: 0x32000000, status: 0xa0080000
[193] rtsx0: cmd_buffer: 0x00003768 0x00002001 0x7dabff00 0x7dacff00 0x7dadff00
[193] rtsx0: cmd->resp = 0x00000120 0x00000000 0x00000000 0x00000000
[193] mmc0: CMD55 RESULT: 0
[193] mmc0: REQUEST: CMD41 arg 0x40ff8000 flags 0x61
[193] rtsx0: rtsx_mmcbr_request(CMD41 arg 0x40ff8000 flags 0x61 dlen 0 dflags 0)
[193] rtsx0: rtsx_send_cmd()
[193] rtsx0: Interrupt handler - enabled: 0x32000000, status: 0xa0080000
[193] rtsx0: cmd_buffer: 0xffc03f68 0x00000080 0x7dabffff 0x7dacff80 0x7dadff00
[193] rtsx0: cmd->resp = 0xc0ff8000 0x00000000 0x00000000 0x00000000
[193] mmc0: CMD41 RESULT: 0
[193] rtsx0: Read ivar #13, value 0x1 / #1
[193] mmc0: Probing cards
[193] mmc0: REQUEST: CMD2 arg 0 flags 0x67
[193] rtsx0: rtsx_mmcbr_request(CMD2 arg 0 flags 0x67 dlen 0 dflags 0)
[193] rtsx0: rtsx_send_cmd()
[193] rtsx0: Interrupt handler - enabled: 0x32000000, status: 0xa0080000
[193] rtsx0: cmd_buffer: 0x53033f68 0x314c5344 0xd8804736 0x01372470 0x00000024
[193] rtsx0: cmd->resp = 0x03534453 0x4c313647 0x80d87024 0x37012401
[193] mmc0: CMD2 RESULT: 0
[193] mmc0: New card detected (CID 035344534c31364780d8702437012401)
[193] rtsx0: rtsx_mmcbr_get_ro()
[193] rtsx0: Read ivar #7, value 0x1 / #1
[193] mmc0: REQUEST: CMD3 arg 0 flags 0x75
[193] rtsx0: rtsx_mmcbr_request(CMD3 arg 0 flags 0x75 dlen 0 dflags 0)
[193] rtsx0: rtsx_send_cmd()
[193] rtsx0: Interrupt handler - enabled: 0x32000000, status: 0xa0080000
[193] rtsx0: cmd_buffer: 0xaaaa0368 0x00002005 0x7dabff00 0x7dacff00 0x7dadff00
[193] rtsx0: cmd->resp = 0xaaaa0520 0x00000000 0x00000000 0x00000000
[193] mmc0: CMD3 RESULT: 0
[193] mmc0: REQUEST: CMD9 arg 0xaaaa0000 flags 0x67
[193] rtsx0: rtsx_mmcbr_request(CMD9 arg 0xaaaa0000 flags 0x67 dlen 0 dflags 0)
[193] rtsx0: rtsx_send_cmd()
[193] rtsx0: Interrupt handler - enabled: 0x32000000, status: 0xa0080000
[193] rtsx0: cmd_buffer: 0x0e403f68 0x595b3200 0xb2760000 0x400a807f 0x00000040
[193] rtsx0: cmd->resp = 0x400e0032 0x5b590000 0x76b27f80 0x0a404001
[193] mmc0: CMD9 RESULT: 0
[193] mmc0: New card detected (CSD 400e00325b59000076b27f800a404001)
[193] mmc0: REQUEST: CMD13 arg 0xaaaa0000 flags 0x15
[193] rtsx0: rtsx_mmcbr_request(CMD13 arg 0xaaaa0000 flags 0x15 dlen 0 dflags 0)
[193] rtsx0: rtsx_send_cmd()
[193] rtsx0: Interrupt handler - enabled: 0x32000000, status: 0xa0080000
[193] rtsx0: cmd_buffer: 0x00000d68 0x00000007 0x7dabffaa 0x7dacff00 0x7dadff00
[193] rtsx0: cmd->resp = 0x00000700 0x00000000 0x00000000 0x00000000
[193] mmc0: CMD13 RESULT: 0
[193] mmc0: REQUEST: CMD7 arg 0xaaaa0000 flags 0x1d
[193] rtsx0: rtsx_mmcbr_request(CMD7 arg 0xaaaa0000 flags 0x1d dlen 0 dflags 0)
[193] rtsx0: rtsx_send_cmd()
[193] rtsx0: Interrupt handler - enabled: 0x32000000, status: 0xa0080000
[193] rtsx0: cmd_buffer: 0x00000768 0x00000007 0x7dabffaa 0x7dacff00 0x7dadff00
[193] rtsx0: cmd->resp = 0x00000700 0x00000000 0x00000000 0x00000000
[193] mmc0: CMD7 RESULT: 0
[193] mmc0: REQUEST: CMD55 arg 0xaaaa0000 flags 0x15
[193] rtsx0: rtsx_mmcbr_request(CMD55 arg 0xaaaa0000 flags 0x15 dlen 0 dflags 0)
[193] rtsx0: rtsx_send_cmd()
[193] rtsx0: Interrupt handler - enabled: 0x32000000, status: 0xa0080000
[193] rtsx0: cmd_buffer: 0x00003768 0x00002009 0x7dabffaa 0x7dacff00 0x7dadff00
[193] rtsx0: cmd->resp = 0x00000920 0x00000000 0x00000000 0x00000000
[193] mmc0: CMD55 RESULT: 0
[193] mmc0: REQUEST: CMD51 arg 0 flags 0x35 data 8
[193] rtsx0: rtsx_mmcbr_request(CMD51 arg 0 flags 0x35 dlen 8 dflags 0x2)
[193] rtsx0: rtsx_xfer_short() - Read xfer: 8 bytes with block size 8
[193] rtsx0: rtsx_send_cmd()
[193] rtsx0: Interrupt handler - enabled: 0x32000000, status: 0xa0080000
[193] rtsx0: rtsx_send_cmd()
[193] rtsx0: Interrupt handler - enabled: 0x32000000, status: 0xa0080000
[193] mmc0: CMD51 RESULT: 0
[193] mmc0: REQUEST: CMD6 arg 0xffffff flags 0x35 data 64
[193] rtsx0: rtsx_mmcbr_request(CMD6 arg 0xffffff flags 0x35 dlen 64 dflags 0x2)
[193] rtsx0: rtsx_xfer_short() - Read xfer: 64 bytes with block size 64
[193] rtsx0: rtsx_send_cmd()
[193] rtsx0: Interrupt handler - enabled: 0x32000000, status: 0xa0080000
[193] rtsx0: rtsx_send_cmd()
[193] rtsx0: Interrupt handler - enabled: 0x32000000, status: 0xa0080000
[193] mmc0: CMD6 RESULT: 0
[193] mmc0: REQUEST: CMD7 arg 0 flags 0
[193] rtsx0: rtsx_mmcbr_request(CMD7 arg 0 flags 0 dlen 0 dflags 0)
[193] rtsx0: rtsx_send_cmd()
[193] rtsx0: Interrupt handler - enabled: 0x32000000, status: 0xa0080000
[193] mmc0: CMD7 RESULT: 0
[193] mmc0: REQUEST: CMD7 arg 0xaaaa0000 flags 0x1d
[193] rtsx0: rtsx_mmcbr_request(CMD7 arg 0xaaaa0000 flags 0x1d dlen 0 dflags 0)
[193] rtsx0: rtsx_send_cmd()
[193] rtsx0: Interrupt handler - enabled: 0x32000000, status: 0xa0080000
[193] rtsx0: cmd_buffer: 0x00000768 0x00000007 0x7dabffaa 0x7dacff00 0x7dadff00
[193] rtsx0: cmd->resp = 0x00000700 0x00000000 0x00000000 0x00000000
[193] mmc0: CMD7 RESULT: 0
[193] mmc0: REQUEST: CMD55 arg 0xaaaa0000 flags 0x15
[193] rtsx0: rtsx_mmcbr_request(CMD55 arg 0xaaaa0000 flags 0x15 dlen 0 dflags 0)
[193] rtsx0: rtsx_send_cmd()
[193] rtsx0: Interrupt handler - enabled: 0x32000000, status: 0xa0080000
[193] rtsx0: cmd_buffer: 0x00003768 0x00002009 0x7dabffaa 0x7dacff00 0x7dadff00
[193] rtsx0: cmd->resp = 0x00000920 0x00000000 0x00000000 0x00000000
[193] mmc0: CMD55 RESULT: 0
[193] mmc0: REQUEST: CMD13 arg 0 flags 0x35 data 64
[193] rtsx0: rtsx_mmcbr_request(CMD13 arg 0 flags 0x35 dlen 64 dflags 0x2)
[193] rtsx0: rtsx_xfer_short() - Read xfer: 64 bytes with block size 64
[193] rtsx0: rtsx_send_cmd()
[193] rtsx0: Interrupt handler - enabled: 0x32000000, status: 0xa0080000
[193] rtsx0: rtsx_send_cmd()
[193] rtsx0: Interrupt handler - enabled: 0x32000000, status: 0xa0080000
[193] mmc0: CMD13 RESULT: 0
[193] mmc0: Card at relative address 0xaaaa added:
[193] mmc0:  card: SDHC SL16G 8.0 SN D8702437 MFG 04/2018 by 3 SD
[193] mmc0:  quirks: 0
[193] mmc0:  bus: 4bit, 50MHz (high speed timing)
[193] mmc0:  memory: 31116288 blocks, erase sector 8192 blocks
[193] mmc0: REQUEST: CMD7 arg 0 flags 0
[193] rtsx0: rtsx_mmcbr_request(CMD7 arg 0 flags 0 dlen 0 dflags 0)
[193] rtsx0: rtsx_send_cmd()
[193] rtsx0: Interrupt handler - enabled: 0x32000000, status: 0xa0080000
[193] mmc0: CMD7 RESULT: 0
[193] rtsx0: Read ivar #7, value 0x1 / #1
[193] mmc0: REQUEST: CMD7 arg 0xaaaa0000 flags 0x1d
[193] rtsx0: rtsx_mmcbr_request(CMD7 arg 0xaaaa0000 flags 0x1d dlen 0 dflags 0)
[193] rtsx0: rtsx_send_cmd()
[193] rtsx0: Interrupt handler - enabled: 0x32000000, status: 0xa0080000
[193] rtsx0: cmd_buffer: 0x00000768 0x00000007 0x7dabffaa 0x7dacff00 0x7dadff00
[193] rtsx0: cmd->resp = 0x00000700 0x00000000 0x00000000 0x00000000
[193] mmc0: CMD7 RESULT: 0
[193] mmc0: REQUEST: CMD7 arg 0 flags 0
[193] rtsx0: rtsx_mmcbr_request(CMD7 arg 0 flags 0 dlen 0 dflags 0)
[193] rtsx0: rtsx_send_cmd()
[193] rtsx0: Interrupt handler - enabled: 0x32000000, status: 0xa0080000
[193] mmc0: CMD7 RESULT: 0
[193] rtsx0: Write ivar #0, value 0x2 / #2
[193] rtsx0: rtsx_mmcbr_update_ios()
[193] rtsx0: Read ivar #5, value 0x61a80 / #400000
[193] rtsx0: Read ivar #13, value 0x1 / #1
[193] rtsx0: Read ivar #13, value 0x1 / #1
[193] rtsx0: Read ivar #13, value 0x1 / #1
[193] mmc0: setting transfer rate to 0.400MHz (normal speed timing)
[193] mmc0: REQUEST: CMD7 arg 0xaaaa0000 flags 0x1d
[193] rtsx0: rtsx_mmcbr_request(CMD7 arg 0xaaaa0000 flags 0x1d dlen 0 dflags 0)
[193] rtsx0: rtsx_send_cmd()
[193] rtsx0: Interrupt handler - enabled: 0x32000000, status: 0xa0080000
[193] rtsx0: cmd_buffer: 0x00000768 0x00000007 0x7dabffaa 0x7dacff00 0x7dadff00
[193] rtsx0: cmd->resp = 0x00000700 0x00000000 0x00000000 0x00000000
[193] mmc0: CMD7 RESULT: 0
[193] rtsx0: Read ivar #7, value 0x1 / #1
[193] mmc0: REQUEST: CMD6 arg 0x80fffff0 flags 0x35 data 64
[193] rtsx0: rtsx_mmcbr_request(CMD6 arg 0x80fffff0 flags 0x35 dlen 64 dflags 0x2)
[193] rtsx0: rtsx_xfer_short() - Read xfer: 64 bytes with block size 64
[193] rtsx0: rtsx_send_cmd()
[193] rtsx0: Interrupt handler - enabled: 0x32000000, status: 0xa0080000
[193] rtsx0: rtsx_send_cmd()
[193] rtsx0: Interrupt handler - enabled: 0x32000000, status: 0xa0080000
[193] mmc0: CMD6 RESULT: 0
[193] rtsx0: Write ivar #14, value 0 / #0
[193] rtsx0: rtsx_mmcbr_update_ios()
[193] rtsx0: Write ivar #3, value 0x61a80 / #400000
[193] rtsx0: rtsx_mmcbr_update_ios()
[193] rtsx0: rtsx_mmcbr_tune()
[193] rtsx0: Read ivar #14, value 0 / #0
[193] rtsx0: Read ivar #7, value 0x1 / #1
[193] mmc0: REQUEST: CMD7 arg 0 flags 0
[193] rtsx0: rtsx_mmcbr_request(CMD7 arg 0 flags 0 dlen 0 dflags 0)
[193] rtsx0: rtsx_send_cmd()
[193] rtsx0: Interrupt handler - enabled: 0x32000000, status: 0xa0080000
[193] mmc0: CMD7 RESULT: 0
[193] rtsx0: rtsx_mmcbr_release_host()
[193] rtsx0: Read ivar #15, value 0x100 / #256
[193] rtsx0: Read ivar #3, value 0x2faf080 / #50000000
[193] GEOM: new disk mmcsd0
[193] mmcsd0: 16GB <SDHC SL16G 8.0 SN D8702437 MFG 04/2018 by 3 SD> at mmc0 50.0MHz/4bit/256-block
[193] rtsx0: rtsx_mmcbr_acquire_host()
[193] mmc0: REQUEST: CMD7 arg 0xaaaa0000 flags 0x1drtsx0: 
[193] Card removed
[193] mmc0: rtsx0: Card at relative address 43690 deleted
[193] rtsx_mmcbr_request(CMD7 arg 0xaaaa0000 flags 0x1d dlen 0 dflags 0)
[193] rtsx0: rtsx_send_cmd()
[193] rtsx0: Interrupt handler - enabled: 0x32000000, status: 0xa0080000
[193] rtsx0: Soft reset
[193] mmc0: CMD7 RESULT: 1
[193] mmc0: REQUEST: CMD7 arg 0xaaaa0000 flags 0x1d
[193] rtsx0: rtsx_mmcbr_request(CMD7 arg 0xaaaa0000 flags 0x1d dlen 0 dflags 0)
[193] rtsx0: rtsx_send_cmd()
[195] rtsx0: Controller timeout
[195] rtsx0: Soft reset
[195] mmc0: CMD7 RESULT: 1
[195] mmc0: REQUEST: CMD7 arg 0xaaaa0000 flags 0x1d
[195] rtsx0: rtsx_mmcbr_request(CMD7 arg 0xaaaa0000 flags 0x1d dlen 0 dflags 0)
[195] rtsx0: rtsx_send_cmd()
[197] rtsx0: Controller timeout
[197] rtsx0: Soft reset
[197] mmc0: CMD7 RESULT: 1
[197] mmc0: REQUEST: CMD7 arg 0xaaaa0000 flags 0x1d
[197] rtsx0: rtsx_mmcbr_request(CMD7 arg 0xaaaa0000 flags 0x1d dlen 0 dflags 0)
[197] rtsx0: rtsx_send_cmd()
[199] rtsx0: Controller timeout
[199] rtsx0: Soft reset
[199] mmc0: CMD7 RESULT: 1
[199] mmc0: CMD7 failed, RESULT: 1
[199] mmc0: Card at relative address 43690 failed to select
[199] mmc0: REQUEST: CMD17 arg 0x1dacbff flags 0x35 data 512
[199] rtsx0: rtsx_mmcbr_request(CMD17 arg 0x1dacbff flags 0x35 dlen 512 dflags 0x2)
[199] rtsx0: rtsx_xfer_short() - Read xfer: 512 bytes with block size 512
[199] rtsx0: rtsx_send_cmd()
[199] rtsx0: Interrupt handler - enabled: 0x32000000, status: 0xa0080000
[199] rtsx0: Soft reset
[199] mmc0: CMD17 RESULT: 1
[199] mmcsd0: Error indicated: 1 Timeout
[199] rtsx0: rtsx_mmcbr_release_host()
[199] rtsx0: Write ivar #0, value 0x1 / #1
[199] rtsx0: Write ivar #2, value 0 / #0
[199] rtsx0: Write ivar #1, value 0 / #0
[199] rtsx0: Write ivar #9, value 0 / #0
[199] rtsx0: Write ivar #3, value 0 / #0
[199] rtsx0: Write ivar #14, value 0 / #0
[199] rtsx0: rtsx_mmcbr_update_ios()
[199] rtsx0: rtsx_set_sd_clock(0)
[199] rtsx0: rtsx_bus_power_off()
[199] mmc0: detached
Comment 110 Henri Hennebert 2020-05-28 13:29:32 UTC
(In reply to jyoung15 from comment #109)

Just to be sure do you preload mmc and mmcsd
Comment 111 jyoung15 2020-05-28 13:36:41 UTC
(In reply to hlh from comment #110)

I am using the standard GENERIC kernel, which has those modules built-in.

$ kldstat | grep mmc
(empty output)

$ sudo kldload mmc       
kldload: can't load mmc: module already loaded or in kernel

$ sudo kldload mmcsd
kldload: can't load mmcsd: module already loaded or in kernel

If you think it will make a difference, I can comment out those devices in /usr/src/sys/amd64/conf/GENERIC and manually rebuild the kernel.
Comment 112 Henri Hennebert 2020-05-28 14:15:04 UTC
(In reply to hlh from comment #110)
(In reply to jyoung15 from comment #111)

No, don't change GENERIC. I ask because I have a specific kernel definition
without mmc and mmcsd and if I don't preload mmcsd, IIRC I encounter the same problem.

I find the following events:

- during the detection there is one interrupt "Card inserted/removed"

- mmc0: Card at relative address 0xaaaa added

- mmcsd0: 16GB <SDHC SL16G 8.0 SN D8702437 MFG 04/2018 by 3 SD> at mmc0

- rtsx0:  Card removed
  mmc0: rtsx0: Card at relative address 43690 deleted

The interrupt and the discrepancy of address is really strange.

Is the card well inserted: contact firmly established?
Comment 113 Jacob Meadows 2020-05-28 14:22:07 UTC
Thank you for all your hard work on this.  I have a Realtek RTS525A in my Dell Latitude E5570.  My FreeBSD version is 12.1-RELEASE-p5, and I am using the GENERIC kernel.  I just downloaded and installed the driver from Git, when loaded it will not show as attached to the device in pciconf, and no access to an SD card is available.  When I load the driver, dmesg outputs this:

pci0: driver added
found->	vendor=0x8086, dev=0x1903, revid=0x07
	domain=0, bus=0, slot=4, func=0
	class=11-80-00, hdrtype=0x00, mfdev=0
	cmdreg=0x0002, statreg=0x0090, cachelnsz=0 (dwords)
	lattimer=0x00 (0 ns), mingnt=0x00 (0 ns), maxlat=0x00 (0 ns)
	intpin=a, irq=255
	powerspec 3  supports D0 D3  current D0
	MSI supports 1 message
pci0:0:4:0: reprobing on driver added
found->	vendor=0x8086, dev=0xa131, revid=0x31
	domain=0, bus=0, slot=20, func=2
	class=11-80-00, hdrtype=0x00, mfdev=0
	cmdreg=0x0002, statreg=0x0010, cachelnsz=0 (dwords)
	lattimer=0x00 (0 ns), mingnt=0x00 (0 ns), maxlat=0x00 (0 ns)
	intpin=c, irq=255
	powerspec 3  supports D0 D3  current D0
	MSI supports 1 message
pci0:0:20:2: reprobing on driver added
found->	vendor=0x8086, dev=0xa13a, revid=0x31
	domain=0, bus=0, slot=22, func=0
	class=07-80-00, hdrtype=0x00, mfdev=1
	cmdreg=0x0002, statreg=0x0010, cachelnsz=0 (dwords)
	lattimer=0x00 (0 ns), mingnt=0x00 (0 ns), maxlat=0x00 (0 ns)
	intpin=a, irq=255
	powerspec 3  supports D0 D3  current D0
	MSI supports 1 message, 64 bit
pci0:0:22:0: reprobing on driver added
found->	vendor=0x8086, dev=0xa121, revid=0x31
	domain=0, bus=0, slot=31, func=2
	class=05-80-00, hdrtype=0x00, mfdev=1
	cmdreg=0x0002, statreg=0x0000, cachelnsz=0 (dwords)
	lattimer=0x00 (0 ns), mingnt=0x00 (0 ns), maxlat=0x00 (0 ns)
pci0:0:31:2: reprobing on driver added
pci1: driver added
pci2: driver added
pci3: driver added
found->	vendor=0x10ec, dev=0x525a, revid=0x01
	domain=0, bus=3, slot=0, func=0
	class=ff-00-00, hdrtype=0x00, mfdev=0
	cmdreg=0x0006, statreg=0x0010, cachelnsz=0 (dwords)
	lattimer=0x00 (0 ns), mingnt=0x00 (0 ns), maxlat=0x00 (0 ns)
	intpin=a, irq=255
	powerspec 3  supports D0 D1 D2 D3  current D0
	MSI supports 1 message, 64 bit
pci0:3:0:0: reprobing on driver added
rtsx0: <Realtek RTS525A PCI MMC/SD Card Reader> mem 0xef000000-0xef000fff at device 0.0 on pci3
rtsx0: Attach - Vendor ID: 0x10ec - Device ID: 0x525a
rtsx0: attempting to allocate 1 MSI vectors (1 supported)
msi: routing MSI IRQ 270 to local APIC 4 vector 52
rtsx0: using IRQ 270 for MSI
rtsx0: Can't allocate memory resource for 16
device_attach: rtsx0 attach returned 6

pciconf -lv shows this for the device:
none4@pci0:3:0:0:	class=0xff0000 card=0x06df1028 chip=0x525a10ec rev=0x01 hdr=0x00
    vendor     = 'Realtek Semiconductor Co., Ltd.'
    device     = 'RTS525A PCI Express Card Reader'
Comment 114 Lutz Bichler 2020-05-28 14:36:57 UTC
Does it change something if you add the following after line 2237: 

if (sc->rtsx_flags & RTSX_F_525A)
    sc->rtsx_res_id = PCIR_BAR(1);
Comment 115 Gary Jennejohn 2020-05-28 14:42:06 UTC
(In reply to Lutz Bichler from comment #114)
That should do it.  There should be a check in the code for the 525A because it must use a different PCI BAR from all the other controllers.
Comment 116 Jesper Schmitz Mouridsen freebsd_committer 2020-05-28 14:44:10 UTC
(In reply to Gary Jennejohn from comment #115)
Yup, all changes from https://github.com/openbsd/src/commit/2c295edd2d779a7f5269c2ae901559edbf040016 are not ported only the power related one
Comment 117 Henri Hennebert 2020-05-28 15:00:29 UTC
(In reply to Jesper Schmitz Mouridsen from comment #116)

pciconf -lb would be helpfull
Comment 118 Henri Hennebert 2020-05-28 15:14:25 UTC
(In reply to Lutz Bichler from comment #114)

I update github.
thanks
Comment 119 Henri Hennebert 2020-05-28 15:40:30 UTC
WARNING - during the style(9) update I mess something in card insert/remove.

A card remove produce a panic :-/
Comment 120 Henri Hennebert 2020-05-28 15:53:37 UTC
A stupid error corrected in rtsx_card_task()

github updated

sorry
Comment 121 Jacob Meadows 2020-05-28 17:14:43 UTC
(In reply to hlh from comment #118)
Thank you.  This (almost) worked.  The driver is now properly attached to the device, but I am having a similar (or same) problem as jyoung15 reported.  If you want, I can post my dmesg, but as it is similar to jyoung15's, and rather long, I will only post if you want it.
Comment 122 Henri Hennebert 2020-05-28 17:42:35 UTC
(In reply to Jacob Meadows from comment #121)
Do you see "Card inserted/removed" during the card detection as in https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=204521#c112

(comment 112)

Post your log as a attachment
Comment 123 Jacob Meadows 2020-05-28 19:08:47 UTC
Created attachment 214987 [details]
Card Inserted dmesg
Comment 124 Jacob Meadows 2020-05-28 19:19:14 UTC
(In reply to hlh from comment #122)
I've added my dmesg as an attachment.  It is recognizing the card installed, but seems to be some errors.  When I remove the card, the driver recognizes that as well:

rtsx0: Interrupt handler - enabled: 0x32000000, status: 0x2000000
rtsx0: Card inserted/removed
rtsx0: No request running
rtsx0: Card removed
rtsx0: Write ivar #0, value 0x1 / #1
rtsx0: Write ivar #2, value 0 / #0
rtsx0: Write ivar #1, value 0 / #0
rtsx0: Write ivar #9, value 0 / #0
rtsx0: Write ivar #3, value 0 / #0
rtsx0: Write ivar #14, value 0 / #0
rtsx0: rtsx_mmcbr_update_ios()
rtsx0: rtsx_set_sd_clock(0)
rtsx0: rtsx_bus_power_off()
mmc0: detached

It's getting close.  I am so thankful for the work you have put in!
Comment 125 jyoung15 2020-05-28 19:38:23 UTC
I've made some progress with the issue I'm having.  I noticed in the dmesg output it seems to incorrectly detect when the card is inserted and removed.  Specifically the issue seems to be in rtsx_is_card_present.  As a very quick and dirty hack, I made this change so it always reports the card as present:

diff --git a/rtsx.c b/rtsx.c
index 0010bc7..a38668e 100644
--- a/rtsx.c
+++ b/rtsx.c
@@ -1122,7 +1122,8 @@ rtsx_is_card_present(struct rtsx_softc *sc)
        uint32_t status;
 
        status = READ4(sc, RTSX_BIPR);
-       return (status & RTSX_SD_EXIST);
+       // return (status & RTSX_SD_EXIST);
+       return RTSX_SD_EXIST;
 }


After making this change I'm able to see the device in /dev and mount the filesystem successfully.  So it seems the issue is related to detecting if the card is present on the BIPR register.
Comment 126 Jesper Schmitz Mouridsen freebsd_committer 2020-05-28 20:54:00 UTC
Created attachment 214992 [details]
do not read BIPR to often..

this could help #comment 125
Comment 127 jyoung15 2020-05-28 21:04:52 UTC
(In reply to Jesper Schmitz Mouridsen from comment #126)

Unfortunately there is no improvement with the patch.  Oddly it seems the card sensing is opposite of what it should be.

With this simple change, it seems to be working perfectly for me:

diff --git a/rtsx.c b/rtsx.c
index 13ce3df..4f5e34f 100644
--- a/rtsx.c
+++ b/rtsx.c
@@ -548,7 +548,7 @@ rtsx_is_card_present(struct rtsx_softc *sc)
        uint32_t status;
 
        status = READ4(sc, RTSX_BIPR);
-       return (status & RTSX_SD_EXIST);
+       return !(status & RTSX_SD_EXIST);
 }
 


With the above change, the /dev/mmcsd0 file appears when inserting the card and disappears when removing the card.  Also, the "rtsx0: Card removed" and "rtsx0: Card inserted" messages appear as expected (previously it would say removed when inserted and vice versa).  I can't explain why, but this works for me.
Comment 128 Lyubomir 2020-05-29 05:48:18 UTC
Thanks for the great work. I can confirm that it compiles and loads on:
Lenovo T450
pciconf -lv

rtsx0@pci0:2:0:0: class=0xff0000 card=0x503417aa chip=0x522710ec rev=0x01 hdr=0x00
    vendor     = 'Realtek Semiconductor Co., Ltd.'
    device     = 'RTS5227 PCI Express Card Reader'

FreeBSD 12.1-RELEASE-p5 GENERIC amd64

I can also copy from and to the inserted card (2GB at the moment available).
dmesg

rtsx0: <Realtek RTS5227 PCI MMC/SD Card Reader> mem 0xf1100000-0xf1100fff irq 16 at device 0.0 on pci1
rtsx0: Card inserted/removed
rtsx0: No request running
mmc0: <MMC/SD bus> on rtsx0
mmcsd0: 32GB <SDHC USD00 0.2 SN 26157BA2 MFG 06/2016 by 156 SO> at mmc0 50.0MHz/4bit/256-block

Here is the output from the compilation:
t450:~/rtsx.git/trunk % make
machine -> /usr/src/sys/amd64/include
x86 -> /usr/src/sys/x86/include
awk -f /usr/src/sys/tools/makeobjops.awk /usr/src/sys/kern/device_if.m -h
awk -f /usr/src/sys/tools/makeobjops.awk /usr/src/sys/kern/bus_if.m -h
awk -f /usr/src/sys/tools/makeobjops.awk /usr/src/sys/dev/pci/pci_if.m -h
awk -f /usr/src/sys/tools/makeobjops.awk /usr/src/sys/dev/mmc/mmcbr_if.m -h
Warning: Object directory not changed from original /usr/home/lyubd/rtsx.git/trunk
cc  "-ferror-limit=0"  -Werror -D_KERNEL -DKLD_MODULE -nostdinc   -I. -I/usr/src/sys -I/usr/src/sys/contrib/ck/include -fno-common  -fno-omit-frame-pointer -mno-omit-leaf-frame-pointer -fdebug-prefix-map=./machine=/usr/src/sys/amd64/include -fdebug-prefix-map=./x86=/usr/src/sys/x86/include   -MD  -MF.depend.rtsx.o -MTrtsx.o -mcmodel=kernel -mno-red-zone -mno-mmx -mno-sse -msoft-float  -fno-asynchronous-unwind-tables -ffreestanding -fwrapv -fstack-protector -Wall -Wredundant-decls -Wnested-externs -Wstrict-prototypes -Wmissing-prototypes -Wpointer-arith -Wcast-qual -Wundef -Wno-pointer-sign -D__printf__=__freebsd_kprintf__ -Wmissing-include-dirs -fdiagnostics-show-option -Wno-unknown-pragmas -Wno-error-tautological-compare -Wno-error-empty-body -Wno-error-parentheses-equality -Wno-error-unused-function -Wno-error-pointer-sign -Wno-error-shift-negative-value -Wno-address-of-packed-member  -mno-aes -mno-avx  -std=iso9899:1999 -c rtsx.c -o rtsx.o
rtsx.c:1180:1: warning: unused function 'rtsx_led_enable' [-Wunused-function]
rtsx_led_enable(struct rtsx_softc *sc)
^
rtsx.c:1199:1: warning: unused function 'rtsx_led_disable' [-Wunused-function]
rtsx_led_disable(struct rtsx_softc *sc)
^
2 warnings generated.
ld -m elf_x86_64_fbsd -d -warn-common --build-id=sha1 -r -d -o rtsx.ko rtsx.o
:> export_syms
awk -f /usr/src/sys/conf/kmod_syms.awk rtsx.ko  export_syms | xargs -J% objcopy % rtsx.ko
objcopy --strip-debug rtsx.ko

Regards,
Lyubomir
Comment 129 Zeus Panchenko 2020-05-29 06:28:57 UTC
when an empty microSD adaptor inserted first and the very microSD card inserted after that, I see CRC errors

kldunload/load doesn't help, only reboot

> uname
FreeBSD 12.1-RELEASE-p3 GENERIC amd64

> git log
commit d02fc0ebfc0983cc1ed9e00657b2316fda1c36fe


here dmesg output for these actions:
1. module unload/load
2. insert/remove microSD adaptor with card
3. insert an empty microSD adaptor
4. insert a card into the microSD adaptor

full dmesg uploaded here: https://pastebin.com/JVGvPKxm

> dmesg

rtsx0: detached
pci2: <unknown> at device 0.0 (no driver attached)
rtsx0: <Realtek RTS5227 PCI MMC/SD Card Reader> mem 0xc1200000-0xc1200fff at device 0.0 on pci2
rtsx0: Interrupt handler - enabled: 0x32000000, status: 0x2010000
rtsx0: Card inserted/removed
rtsx0: No request running
rtsx0: Card inserted
mmc0: <MMC/SD bus> on rtsx0
rtsx0: rtsx_mmcbr_acquire_host()
rtsx0: Read ivar #9, value 0 / #0
rtsx0: Write ivar #7, value 0x1 / #1
rtsx0: Read ivar #6, value 0x3c0000 / #3932160
rtsx0: Write ivar #11, value 0x15 / #21
rtsx0: Write ivar #0, value 0x1 / #1
rtsx0: Write ivar #2, value 0 / #0
rtsx0: Write ivar #1, value 0 / #0
rtsx0: Write ivar #9, value 0x1 / #1
rtsx0: Write ivar #3, value 0 / #0
rtsx0: rtsx_mmcbr_update_ios()
rtsx0: rtsx_bus_power_up()
rtsx0: Write ivar #12, value 0x2 / #2
rtsx0: rtsx_mmcbr_switch_vccq(330)
rtsx0: Write ivar #3, value 0x61a80 / #400000
rtsx0: Write ivar #14, value 0 / #0
rtsx0: Write ivar #9, value 0x2 / #2
rtsx0: rtsx_mmcbr_update_ios()
rtsx0: rtsx_set_sd_clock(400000)
rtsx0: Write ivar #0, value 0x2 / #2
mmc0: Probing bus
rtsx0: Write ivar #2, value 0x1 / #1
rtsx0: rtsx_mmcbr_update_ios()
mmc0: REQUEST: CMD0 arg 0 flags 0x40
rtsx0: rtsx_mmcbr_request(CMD0 arg 0 flags 0x40 dlen 0 dflags 0)
rtsx0: rtsx_send_cmd()
rtsx0: Interrupt handler - enabled: 0x32000000, status: 0xa0090000
mmc0: CMD0 RESULT: 0
rtsx0: Write ivar #2, value 0 / #0
rtsx0: rtsx_mmcbr_update_ios()
mmc0: REQUEST: CMD8 arg 0x1aa flags 0x75
rtsx0: rtsx_mmcbr_request(CMD8 arg 0x1aa flags 0x75 dlen 0 dflags 0)
rtsx0: rtsx_send_cmd()
rtsx0: Interrupt handler - enabled: 0x32000000, status: 0xa0090000
rtsx0: cmd_buffer: 0x00000868 0x0000aa01 0x7dabff00 0x7dacff01 0x7dadffaa
rtsx0: cmd->resp = 0x000001aa 0x00000000 0x00000000 0x00000000
mmc0: CMD8 RESULT: 0
mmc0: SD 2.0 interface conditions: OK
mmc0: REQUEST: CMD55 arg 0 flags 0x15
rtsx0: rtsx_mmcbr_request(CMD55 arg 0 flags 0x15 dlen 0 dflags 0)
rtsx0: rtsx_send_cmd()
rtsx0: Interrupt handler - enabled: 0x32000000, status: 0xa0090000
rtsx0: cmd_buffer: 0x00003768 0x00002001 0x7dabff00 0x7dacff00 0x7dadff00
rtsx0: cmd->resp = 0x00000120 0x00000000 0x00000000 0x00000000
mmc0: CMD55 RESULT: 0
mmc0: REQUEST: CMD41 arg 0 flags 0x61
rtsx0: rtsx_mmcbr_request(CMD41 arg 0 flags 0x61 dlen 0 dflags 0)
rtsx0: rtsx_send_cmd()
rtsx0: Interrupt handler - enabled: 0x32000000, status: 0xa0090000
rtsx0: cmd_buffer: 0xff003f68 0x00000080 0x7dabff00 0x7dacff00 0x7dadff00
rtsx0: cmd->resp = 0x00ff8000 0x00000000 0x00000000 0x00000000
mmc0: CMD41 RESULT: 0
mmc0: SD probe: OK (OCR: 0x00ff8000)
rtsx0: Write ivar #8, value 0xff8000 / #16744448
rtsx0: Read ivar #8, value 0xff8000 / #16744448
rtsx0: Write ivar #2, value 0x1 / #1
rtsx0: rtsx_mmcbr_update_ios()
mmc0: REQUEST: CMD0 arg 0 flags 0x40
rtsx0: rtsx_mmcbr_request(CMD0 arg 0 flags 0x40 dlen 0 dflags 0)
rtsx0: rtsx_send_cmd()
rtsx0: Interrupt handler - enabled: 0x32000000, status: 0xa0090000
mmc0: CMD0 RESULT: 0
rtsx0: Write ivar #2, value 0 / #0
rtsx0: rtsx_mmcbr_update_ios()
rtsx0: Read ivar #8, value 0xff8000 / #16744448
mmc0: Current OCR: 0x00ff8000
rtsx0: Read ivar #8, value 0xff8000 / #16744448
rtsx0: Read ivar #7, value 0x1 / #1
mmc0: REQUEST: CMD8 arg 0x1aa flags 0x75
rtsx0: rtsx_mmcbr_request(CMD8 arg 0x1aa flags 0x75 dlen 0 dflags 0)
rtsx0: rtsx_send_cmd()
...
mmc0: CMD41 RESULT: 0
mmc0: REQUEST: CMD55 arg 0 flags 0x15
rtsx0: rtsx_mmcbr_request(CMD55 arg 0 flags 0x15 dlen 0 dflags 0)
rtsx0: rtsx_send_cmd()
rtsx0: Interrupt handler - enabled: 0x32000000, status: 0xa0090000
rtsx0: cmd_buffer: 0x00003768 0x00002001 0x7dabff00 0x7dacff00 0x7dadff00
rtsx0: cmd->resp = 0x00000120 0x00000000 0x00000000 0x00000000
mmc0: CMD55 RESULT: 0
mmc0: REQUEST: CMD41 arg 0x40ff8000 flags 0x61
rtsx0: rtsx_mmcbr_request(CMD41 arg 0x40ff8000 flags 0x61 dlen 0 dflags 0)
rtsx0: rtsx_send_cmd()
rtsx0: Interrupt handler - enabled: 0x32000000, status: 0xa0090000
rtsx0: cmd_buffer: 0xff003f68 0x00000080 0x7dabffff 0x7dacff80 0x7dadff00
rtsx0: cmd->resp = 0x00ff8000 0x00000000 0x00000000 0x00000000
mmc0: CMD41 RESULT: 0
mmc0: REQUEST: CMD55 arg 0 flags 0x15
rtsx0: rtsx_mmcbr_request(CMD55 arg 0 flags 0x15 dlen 0 dflags 0)
rtsx0: rtsx_send_cmd()
rtsx0: Interrupt handler - enabled: 0x32000000, status: 0xa0090000
rtsx0: cmd_buffer: 0x00003768 0x00002001 0x7dabff00 0x7dacff00 0x7dadff00
rtsx0: cmd->resp = 0x00000120 0x00000000 0x00000000 0x00000000
mmc0: CMD55 RESULT: 0
...
rtsx0: Interrupt handler - enabled: 0x32000000, status: 0xa0090000
mmc0: CMD7 RESULT: 0
rtsx0: rtsx_mmcbr_release_host()
rtsx0: Interrupt handler - enabled: 0x32000000, status: 0x2000000
rtsx0: Card inserted/removed
rtsx0: No request running
rtsx0: Card removed
rtsx0: Write ivar #0, value 0x1 / #1
rtsx0: Write ivar #2, value 0 / #0
rtsx0: Write ivar #1, value 0 / #0
rtsx0: Write ivar #9, value 0 / #0
rtsx0: Write ivar #3, value 0 / #0
rtsx0: Write ivar #14, value 0 / #0
rtsx0: rtsx_mmcbr_update_ios()
rtsx0: rtsx_set_sd_clock(0)
rtsx0: rtsx_bus_power_off()
mmc0: detached
rtsx0: Interrupt handler - enabled: 0x32000000, status: 0x2010000
rtsx0: Card inserted/removed
rtsx0: No request running
rtsx0: Card inserted
mmc0: <MMC/SD bus> on rtsx0
rtsx0: rtsx_mmcbr_acquire_host()
rtsx0: Read ivar #9, value 0 / #0
rtsx0: Write ivar #7, value 0x1 / #1
rtsx0: Read ivar #6, value 0x3c0000 / #3932160
rtsx0: Write ivar #11, value 0x15 / #21
rtsx0: Write ivar #0, value 0x1 / #1
rtsx0: Write ivar #2, value 0 / #0
rtsx0: Write ivar #1, value 0 / #0
rtsx0: Write ivar #9, value 0x1 / #1
rtsx0: Write ivar #3, value 0 / #0
rtsx0: rtsx_mmcbr_update_ios()
rtsx0: rtsx_bus_power_up()
rtsx0: Write ivar #12, value 0x2 / #2
rtsx0: rtsx_mmcbr_switch_vccq(330)
rtsx0: Write ivar #3, value 0x61a80 / #400000
rtsx0: Write ivar #14, value 0 / #0
rtsx0: Write ivar #9, value 0x2 / #2
rtsx0: rtsx_mmcbr_update_ios()
rtsx0: rtsx_set_sd_clock(400000)
rtsx0: Write ivar #0, value 0x2 / #2
mmc0: Probing bus
rtsx0: Write ivar #2, value 0x1 / #1
rtsx0: rtsx_mmcbr_update_ios()
mmc0: REQUEST: CMD0 arg 0 flags 0x40
rtsx0: rtsx_mmcbr_request(CMD0 arg 0 flags 0x40 dlen 0 dflags 0)
rtsx0: rtsx_send_cmd()
...
rtsx0: CRC error
rtsx0: Soft reset
mmc0: CMD17 RESULT: 2
rtsx0: rtsx_mmcbr_release_host()
rtsx0: rtsx_mmcbr_acquire_host()
mmc0: REQUEST: CMD17 arg 0x10 flags 0x35 data 512
rtsx0: rtsx_mmcbr_request(CMD17 arg 0x10 flags 0x35 dlen 512 dflags 0x2)
rtsx0: rtsx_xfer_short() - Read xfer: 512 bytes with block size 512
rtsx0: rtsx_send_cmd()
rtsx0: Interrupt handler - enabled: 0x32000000, status: 0x90090080
rtsx0: CRC error
rtsx0: Soft reset
mmc0: CMD17 RESULT: 2
rtsx0: rtsx_mmcbr_release_host()
rtsx0: rtsx_mmcbr_acquire_host()
mmc0: REQUEST: CMD17 arg 0x80 flags 0x35 data 512
rtsx0: rtsx_mmcbr_request(CMD17 arg 0x80 flags 0x35 dlen 512 dflags 0x2)
rtsx0: rtsx_xfer_short() - Read xfer: 512 bytes with block size 512
rtsx0: rtsx_send_cmd()
rtsx0: Interrupt handler - enabled: 0x32000000, status: 0x90090080
rtsx0: CRC error
rtsx0: Soft reset
mmc0: CMD17 RESULT: 2
rtsx0: rtsx_mmcbr_release_host()
rtsx0: rtsx_mmcbr_acquire_host()
mmc0: REQUEST: CMD17 arg 0 flags 0x35 data 512
rtsx0: rtsx_mmcbr_request(CMD17 arg 0 flags 0x35 dlen 512 dflags 0x2)
rtsx0: rtsx_xfer_short() - Read xfer: 512 bytes with block size 512
rtsx0: rtsx_send_cmd()
rtsx0: Interrupt handler - enabled: 0x32000000, status: 0x90090080
rtsx0: CRC error
rtsx0: Soft reset
mmc0: CMD17 RESULT: 2
rtsx0: rtsx_mmcbr_release_host()
rtsx0: Interrupt handler - enabled: 0x32000000, status: 0x2000000
rtsx0: Card inserted/removed
rtsx0: No request running
rtsx0: Card removed
mmc0: Card at relative address 7 deleted
rtsx0: Write ivar #0, value 0x1 / #1
rtsx0: Write ivar #2, value 0 / #0
rtsx0: Write ivar #1, value 0 / #0
rtsx0: Write ivar #9, value 0 / #0
rtsx0: Write ivar #3, value 0 / #0
rtsx0: Write ivar #14, value 0 / #0
rtsx0: rtsx_mmcbr_update_ios()
rtsx0: rtsx_set_sd_clock(0)
rtsx0: rtsx_bus_power_off()
mmc0: detached
Comment 130 Gary Jennejohn 2020-05-29 06:50:21 UTC
(In reply to jyoung15 from comment #127)
This appears to be a strange bug in your hardware.  The Linux code also depends on the bit being set in BIPR to determine whether a card is inserted.  Since Realtek developed the Linux driver I'd have to say that it's the gold standard.
Comment 131 Henri Hennebert 2020-05-29 06:53:03 UTC
(In reply to jyoung15 from comment #127)
Just to check if ther is a timing problem, can you try:

diff --git a/rtsx.c b/rtsx.c
index 13ce3df..6da1b1c 100644
--- a/rtsx.c
+++ b/rtsx.c
@@ -547,6 +547,7 @@ rtsx_is_card_present(struct rtsx_softc *sc)
 {
        uint32_t status;
 
+       DELAY(200);
        status = READ4(sc, RTSX_BIPR);
        return (status & RTSX_SD_EXIST);
 }
Comment 132 Gary Jennejohn 2020-05-29 06:56:05 UTC
(In reply to hlh from comment #131)
Good idea.
Comment 133 Gary Jennejohn 2020-05-29 06:57:57 UTC
(In reply to Zeus Panchenko from comment #129)
I would suggest putting the SD card into the holder and then inserting it.  Poor contact of the card in the holder will lead to errors.
Comment 134 jyoung15 2020-05-29 12:42:04 UTC
(In reply to hlh from comment #131)

Unfortunately the added DELAY did not make any difference for me.  Regarding the comment about a hardware issue, this is a dual-boot laptop and the card reader works fine on Windows.  Anyway, I have found a workaround for my issue and the rtsx driver works fine otherwise.  Thanks to all involved for your efforts.
Comment 135 Henri Hennebert 2020-05-29 12:53:08 UTC
(In reply to jyoung15 from comment #134)
I can't change the source because it work for other RTS522A.
Really strange...
Comment 136 Henri Hennebert 2020-05-29 14:20:35 UTC
(In reply to jyoung15 from comment #134)

Just to be sure, as I mess up in power control, can you test with the new github.
Thanks.
Comment 137 Henri Hennebert 2020-05-29 14:22:11 UTC
(In reply to Jacob Meadows from comment #124)

As I mess up with power control, can you test again.
Thanks
Comment 138 Jacob Meadows 2020-05-29 15:45:02 UTC
(In reply to hlh from comment #137)
It is still not working, but the error messages are different.  It does detect the card being inserted and removed, but the /dev/mmc0 is not being created.  Here is the current dmesg when I insert a card.

rtsx0: Interrupt handler - enabled: 0x32000000, status: 0x2010000
rtsx0: Interrupt card inserted/removed
rtsx0: No request running
rtsx0: Card inserted
mmc0: <MMC/SD bus> on rtsx0
rtsx0: rtsx_mmcbr_acquire_host()
rtsx0: Read ivar #9, value 0 / #0
rtsx0: Write ivar #7, value 0x1 / #1
rtsx0: Read ivar #6, value 0x3c0000 / #3932160
rtsx0: Write ivar #11, value 0x15 / #21
rtsx0: Write ivar #0, value 0x1 / #1
rtsx0: Write ivar #2, value 0 / #0
rtsx0: Write ivar #1, value 0 / #0
rtsx0: Write ivar #9, value 0x1 / #1
rtsx0: Write ivar #3, value 0 / #0
rtsx0: rtsx_mmcbr_update_ios()
rtsx0: rtsx_bus_power_on()
rtsx0: Write ivar #12, value 0x2 / #2
rtsx0: rtsx_mmcbr_switch_vccq(330)
rtsx0: Write ivar #3, value 0x61a80 / #400000
rtsx0: Write ivar #14, value 0 / #0
rtsx0: Write ivar #9, value 0x2 / #2
rtsx0: rtsx_mmcbr_update_ios()
rtsx0: rtsx_set_sd_clock(400000)
rtsx0: Write ivar #0, value 0x2 / #2
mmc0: Probing bus
rtsx0: Write ivar #2, value 0x1 / #1
rtsx0: rtsx_mmcbr_update_ios()
rtsx0: rtsx_mmcbr_request(CMD0 arg 0 flags 0x40 dlen 0 dflags 0)
rtsx0: rtsx_send_cmd()
rtsx0: Interrupt handler - enabled: 0x32000000, status: 0xa0010000
rtsx0: Write ivar #2, value 0 / #0
rtsx0: rtsx_mmcbr_update_ios()
rtsx0: rtsx_mmcbr_request(CMD8 arg 0x1aa flags 0x75 dlen 0 dflags 0)
rtsx0: rtsx_send_cmd()
rtsx0: Controller timeout
rtsx0: Soft reset
rtsx0: rtsx_mmcbr_request(CMD8 arg 0x1aa flags 0x75 dlen 0 dflags 0)
rtsx0: rtsx_send_cmd()
rtsx0: Controller timeout
rtsx0: Soft reset
rtsx0: rtsx_mmcbr_request(CMD8 arg 0x1aa flags 0x75 dlen 0 dflags 0)
rtsx0: rtsx_send_cmd()
rtsx0: Controller timeout
rtsx0: Soft reset
rtsx0: rtsx_mmcbr_request(CMD8 arg 0x1aa flags 0x75 dlen 0 dflags 0)
rtsx0: rtsx_send_cmd()
rtsx0: Controller timeout
rtsx0: Soft reset
rtsx0: rtsx_mmcbr_request(CMD55 arg 0 flags 0x15 dlen 0 dflags 0)
rtsx0: rtsx_send_cmd()
rtsx0: Controller timeout
rtsx0: Soft reset
rtsx0: rtsx_mmcbr_request(CMD55 arg 0 flags 0x15 dlen 0 dflags 0)
rtsx0: rtsx_send_cmd()
rtsx0: Controller timeout
rtsx0: Soft reset
rtsx0: rtsx_mmcbr_request(CMD55 arg 0 flags 0x15 dlen 0 dflags 0)
rtsx0: rtsx_send_cmd()
rtsx0: Controller timeout
rtsx0: Soft reset
rtsx0: rtsx_mmcbr_request(CMD55 arg 0 flags 0x15 dlen 0 dflags 0)
rtsx0: rtsx_send_cmd()
rtsx0: Controller timeout
rtsx0: Soft reset
mmc0: SD probe: failed
rtsx0: Write ivar #7, value 0 / #0
rtsx0: rtsx_mmcbr_request(CMD1 arg 0 flags 0x61 dlen 0 dflags 0)
rtsx0: rtsx_send_cmd()
wlan0: link state changed to UP
rtsx0: Controller timeout
rtsx0: Soft reset
rtsx0: rtsx_mmcbr_request(CMD1 arg 0 flags 0x61 dlen 0 dflags 0)
rtsx0: rtsx_send_cmd()
rtsx0: Controller timeout
rtsx0: Soft reset
rtsx0: rtsx_mmcbr_request(CMD1 arg 0 flags 0x61 dlen 0 dflags 0)
rtsx0: rtsx_send_cmd()
rtsx0: Controller timeout
rtsx0: Soft reset
rtsx0: rtsx_mmcbr_request(CMD1 arg 0 flags 0x61 dlen 0 dflags 0)
rtsx0: rtsx_send_cmd()
rtsx0: Controller timeout
rtsx0: Soft reset
mmc0: MMC probe: failed
rtsx0: Write ivar #8, value 0 / #0
rtsx0: Read ivar #8, value 0 / #0
rtsx0: Read ivar #8, value 0 / #0
mmc0: Current OCR: 0x00000000
rtsx0: Read ivar #8, value 0 / #0
mmc0: No compatible cards found on bus
rtsx0: Write ivar #0, value 0x1 / #1
rtsx0: Write ivar #2, value 0 / #0
rtsx0: Write ivar #1, value 0 / #0
rtsx0: Write ivar #9, value 0 / #0
rtsx0: Write ivar #3, value 0 / #0
rtsx0: Write ivar #14, value 0 / #0
rtsx0: rtsx_mmcbr_update_ios()
rtsx0: rtsx_set_sd_clock(0)
rtsx0: rtsx_bus_power_off()
rtsx0: rtsx_mmcbr_release_host()
Comment 139 Jesper Schmitz Mouridsen freebsd_committer 2020-05-29 16:01:32 UTC
(In reply to hlh from comment #137)
Hi for 5209 and 5229 you do not want full power in bus power_on, it breaks, and might also break for RTS522A but I have only access to 5209 and 5229. I switched calls in #attachment 214840 [details], and you switched implementation in git instead.  Now it seems you have switched them back, and it breaks at least for 5209 and 5229
Comment 140 Henri Hennebert 2020-05-29 16:22:40 UTC
(In reply to Jesper Schmitz Mouridsen from comment #139)

I forget to put the right message when switching back the content of the power_xx(). So it is just in power_up and not power_on.

I found that rtsx_mmcbr_update_ios() never encounter a power_on.

You was right when you switch power_up and power_on because so you do a power_on when a power_up was requested.

In the github I push now, I change the switch in rtsx_mmcbr_update_ios()

        switch (ios->power_mode) {                                                                                                  
        case power_off:                                                                                                             
                if (sc->rtsx_power_mode != power_off) {                                                                             
                        rtsx_bus_power_off(sc);                                                                                     
                        sc->rtsx_power_mode = power_off;                                                                            
                }                                                                                                                   
                break;                                                                                                              
        case power_up:                                                                                                              
//              if (sc->rtsx_power_mode != power_up) {                                                                              
//                      rtsx_bus_power_up(sc);                                                                                      
//                      sc->rtsx_power_mode = power_up;                                                                             
//              }                                                                                                                   
//              break;                                                                                                              
        case power_on:                                                                                                              
                if (sc->rtsx_power_mode != power_on) {                                                                              
                        rtsx_bus_power_on(sc);                                                                                      
                        sc->rtsx_power_mode = power_on;                                                                             
                }                                                                                                                   
                break;                                                                                                              
        };

To be more coherent (if it is possible ;-)

Can you please test with this last update
thanks
Comment 141 Jesper Schmitz Mouridsen freebsd_committer 2020-05-29 16:40:50 UTC
(In reply to hlh from comment #140)
Yes, it works again.
Comment 142 Gary Jennejohn 2020-05-30 07:04:51 UTC
(In reply to jyoung15 from comment #134)
I wonder whether this strange behavior is generic to the Lenovo P50s or just on your laptop.  Would be good if we could get a report from another user of this laptop.
Considering that Lenovo provides its own Windows driver for the card reader, the fact that it works in Windows doesn't disprove that there's a bug.
Comment 143 Gary Jennejohn 2020-05-30 07:08:29 UTC
@hlh
I read the man page and it looks pretty good for a start.
Thanks for listing me as an author, alhough I didn't contribute much.  Can you use my gj@freebsd.org mail address?  I'm not an active committer any more but I do still have the mail address.
Thanks.
Comment 144 Gary Jennejohn 2020-05-30 07:55:07 UTC
(In reply to Gary Jennejohn from comment #142)
The reason I'm harping on this is, if the card detection inversion is common enough, it might be possible to get a config(8) option like RTSX_CARD_DETECT_INVERTED added which affected users could set in their kernel configuration files.  And then document it in the man page.
Comment 145 Henri Hennebert 2020-05-30 12:00:07 UTC
(In reply to Jacob Meadows from comment #138)

I pull some stuff for RTS525A from Linux

Can you please test with the last github
Comment 146 Jacob Meadows 2020-05-31 00:20:15 UTC
(In reply to hlh from comment #145)
This works!  I was able to successfully insert a 64GB SDXC card formatted in exFat, mount it with FUSE, read and write files about 4GB in size.  Thank you so much!

Dell Latitude E5570, with Realtek RTS525A PCI Express card reader, confirmed to work!
Comment 147 Jesper Schmitz Mouridsen freebsd_committer 2020-05-31 10:28:05 UTC
(In reply to Sergey V. Dyatko from comment #107)
Your dmesg says rtsx0: Card inserted, and you have no card inserted?

Perhaps you are hit by the inverted card detection on RTS522A problem that jyoung15@gmail.com worked around in #comment 127
Comment 148 Henri Hennebert 2020-05-31 16:10:58 UTC
(In reply to Jesper Schmitz Mouridsen from comment #147)

I add some specific code for RTS522A. there is one difference depending on the chip version. Maybe it solve the strange problem of card detection.
Comment 149 Gary Jennejohn 2020-06-01 07:42:11 UTC
(In reply to hlh from comment #148)
I can report that this change did not affect my 522A.
I now see non-fatal lock order reversals when I remove the SD card.  I don't remember seeing them last week.  But the kernel contains lots of this type of lock order reversal, so nothing to worry about.
Comment 150 Henri Hennebert 2020-06-01 08:27:58 UTC
(In reply to Gary Jennejohn from comment #149)

rtsx_handle_card_present() is called in rtsx_intr() with the LOCK active.
This function schedule immediately rtsx_card_task() and this one acquire the LOCK, make some processing and UNLOCK. maybe during this processing the rtsx_intr() complete and UNLOCK.
Comment 151 Henri Hennebert 2020-06-01 08:45:40 UTC
(In reply to hlh from comment #150)

maybe the culprit:

In a commit on May 28 in rtsx_card_task() I put the UNLOCK after
sc->rtsx_mmc_dev = NULL. It seems logical since sc struct is modified.

In your original code, the UNLOCK was before device_delete_child().
Comment 152 Jesper Schmitz Mouridsen freebsd_committer 2020-06-01 09:46:41 UTC
(In reply to hlh from comment #151)
lock order reversal: (Giant after non-sleepable)
 1st 0xfffff80004e0fc00 rtsx0 (rtsx) @ rtsx.c:513
 2nd 0xffffffff81802500 Giant (Giant) @ /usr/src/sys/kern/kern_synch.c:228
stack backtrace:
#0 0xffffffff80c2acc1 at witness_debugger+0x71
#1 0xffffffff80b9c844 at __mtx_lock_flags+0x94
#2 0xffffffff80bca5c6 at _sleep+0x366
#3 0xffffffff8071c341 at mmcsd_detach+0xe1
#4 0xffffffff80bf63ee at device_detach+0x18e
#5 0xffffffff80bf60c5 at device_delete_child+0x15
#6 0xffffffff8071a0c7 at mmc_delete_cards+0x97
#7 0xffffffff80712e43 at mmc_detach+0x23
#8 0xffffffff80bf63ee at device_detach+0x18e
#9 0xffffffff80bf60c5 at device_delete_child+0x15
#10 0xffffffff82322b0f at rtsx_card_task+0x1bf
#11 0xffffffff80c1d5fa at taskqueue_run_locked+0xaa
#12 0xffffffff80c1d50d at taskqueue_run+0x4d
#13 0xffffffff80b7f379 at ithread_loop+0x279
#14 0xffffffff80b7bec0 at fork_exit+0x80
#15 0xffffffff81031aae at fork_trampoline+0xe
uma_zalloc_debug: zone "256" with the following non-sleepable locks held:
exclusive sleep mutex rtsx0 (rtsx) r = 0 (0xfffff80004e0fc00) locked @ rtsx.c:513
unlocking before delete_device_child as you noted fixes it
Comment 153 Gary Jennejohn 2020-06-01 10:14:03 UTC
(In reply to Jesper Schmitz Mouridsen from comment #152)
Right, so unlock before device_delete_child() and re-lock before the write to sc->rtsx_mmc_dev.  It tried this change, it works and eliminates the lock reversal.
Comment 154 Henri Hennebert 2020-06-01 10:26:48 UTC
(In reply to Gary Jennejohn from comment #153)
LOCK / UNLOCK for sc->rtsx_mmc_dev = NULL; seems useless to me.
Comment 155 Gary Jennejohn 2020-06-01 10:42:22 UTC
(In reply to hlh from commsc->rtsx_mmc_devent #154)
This disagress with your comment in 151, which implies you were worried about modifying sc->rtsx_mmc_dev when it's not under the lock.  If that's the case then it should be re-locked beforehand.
IMO reacquiring the lock is no big deal.  It's not like this code is executed millions of time per second.  And it makes it clear exactly why the lock is being used.
Comment 156 Henri Hennebert 2020-06-01 10:58:26 UTC
(In reply to Gary Jennejohn from comment #155)

In this case:

       if (device_delete_child(sc->rtsx_dev, sc->rtsx_mmc_dev))
               device_printf(sc->rtsx_dev, "Detaching MMC bus failed\n");
       sc->rtsx_mmc_dev = NULL;
       RTSX_UNLOCK(sc);

The LOCK try to postpone a new occurrence of rtsx_card_task()
to reconnect a card.

In this case:

       RTSX_UNLOCK(sc);
       if (device_delete_child(sc->rtsx_dev, sc->rtsx_mmc_dev))
               device_printf(sc->rtsx_dev, "Detaching MMC bus failed\n");
       sc->rtsx_mmc_dev = NULL;


A new occurrence of rtsx_card_task() may reconnect a card and then
rtsx_mmc_dev is replace by NULL. And so even if it is bracketed by LOCL/UNLOCK.
Comment 157 Gary Jennejohn 2020-06-01 11:04:39 UTC
(In reply to hlh from comment #156)
OK, I'm convinced.  Now we both know why the original code was correct :)
Comment 158 Gleb Popov freebsd_committer 2020-06-01 13:38:25 UTC
It is awesome to see progress on this.

I wrote a FreeBSD port that pulls sources from GitHub, compiles the kmod and installs it. We can use it to gain wider testing audience.

Henri, once you're confident with the code, add a tag and I will use it to commit the port.

And thanks again everyone involved!
Comment 159 Henri Hennebert 2020-06-01 14:15:25 UTC
(In reply to Gleb Popov from comment #158)

Thank you!

I am waiting for a test by jyoung15@gmail.com about the RTS522A.

Can you test the driver on your notebook and report the
Realtek device and FreeBSD version to complete the README.md

thanks
Comment 160 Gleb Popov freebsd_committer 2020-06-01 20:18:49 UTC
Loading the driver results in

rtsx0: <Realtek RTS5229 PCI MMC/SD Card Reader> mem 0xdf100000-0xdf100fff at device 0.0 on pci4

but nothing else. mmc and mmcsd are already loaded.
Comment 161 Gleb Popov freebsd_committer 2020-06-01 20:24:14 UTC
Oh, I inserted a card and new device appeared. I haven't done extensive testing, though,
Comment 162 Henri Hennebert 2020-06-02 05:37:34 UTC
(In reply to Gleb Popov from comment #161)

It is the first test for RTS5229.

Can you try read and write operation on /dec/mmcsd0

eg:

Create a zpool, populate it and then a zpool scrub.

If the content of the card is precious, at least create a file and check it.

Thanks in advance!
Comment 163 Gleb Popov freebsd_committer 2020-06-03 15:34:37 UTC
(In reply to Henri Hennebert from comment #162)
The test was mostly successfull.

dd'ing from a random file to /dev/mmcsd0 was successfull. However, reading the file back resulted in 

rtsx0: Controller timeout
rtsx0: Soft reset
mmcsd0: Error indicated: 1 Timeout

This happens randomly after starting reading from the card, after reading about 50 Mb.

Playing with block size doesn't seem to change anything.

Creating a FAT filesystem and a file on it also worked.
Comment 164 Jesper Schmitz Mouridsen freebsd_committer 2020-06-03 18:10:57 UTC
Thanks gleb for the test.

Testing rtsx0@pci0:1:0:0: class=0xff0000 card=0x522910ec chip=0x522910ec rev=0x01 hdr=0x00
    vendor     = 'Realtek Semiconductor Co., Ltd.'
    device     = 'RTS5229 PCI Express Card Reader'
    bar   [10] = type Memory, range 32, base rx91200000, size 4096, enabled
my self in my Lenovo ideapad 120S-14IAP I could not replicate. dd random to mmcsd0 as well as reading back did not fail.

I wonder how a bad card behaves...
Comment 165 Henri Hennebert 2020-06-03 18:26:12 UTC
(In reply to Jesper Schmitz Mouridsen from comment #164)

I am lost in the readers you test.

can you give the list of

card reader, freebsd version and make of the computer.
So I will complete the README.md

Thanks in advance.
Comment 166 Jesper Schmitz Mouridsen freebsd_committer 2020-06-03 18:40:51 UTC
(In reply to Henri Hennebert from comment #165)
Yes of course.

I have tested RTS5229 PCI Express Card Reader on Lenovo ideapad 120S-14IAP successfully on FreeBSD 12.1-RELEASE-p1

and on
        Manufacturer: LENOVO
        Product Name: 5017AA5
        Version: ThinkPad L520
rtsx0@pci0:4:0:0:       class=0xff0000 rev=0x01 hdr=0x00 vendor=0x10ec device=0x5209 subvendor=0x17aa subdevice=0x21dd
    vendor     = 'Realtek Semiconductor Co., Ltd.'
    device     = 'RTS5209 PCI Express Card Reader'
    bar   [10] = type Memory, range 32, base rxf0600000, size 4096, enabled
with version FreeBSD 13.0-CURRENT #0 r361019 which also works successfully.