# RealTek 8176 + ndis



## pkc (Nov 26, 2012)

Hello, 

I am trying to get my on-board Realtek 8176 wireless adapter working. 

```
none1@pci0:4:0:0:       class=0x028000 card=0x819510ec chip=0x817610ec rev=0x01 hdr=0x00
    vendor     = 'Realtek Semiconductor'
    class      = network
```

I searched fruitlessly for a 'Realtek 8176 driver', and came across a post on a forum declaring that this chipset actually used the driver for 8188CE. (Which actually exists)
Apparently this driver is actually the same as for 8192CE, also. (Possible problem here?)

Anyway, I installed the 8188CE driver for several of the variants, and nothing happened (no dmesg, no ifconfig, etc; ndis and if_ndis are loaded). Then I tried the WinX64 variant, and this seems to have crashed the system:


```
no match for IoWMIQueryAllData
no match for IoWMIOpenBlock
ndis0: <1x1 11b/g/n Wireless LAN PCI Express Half Mini Card Adapter> port 0x2000-0x20ff mem 0xf0100000-0xf0103fff irq 17 at device 0.0 on pci4
ndis0: [ITHREAD]
ndis0: NDIS API version: 5.1


Fatal trap 12: page fault while in kernel mode
cpuid = 0; apic id = 00
fault virtual address   = 0xffffff8100a00000
fault code              = supervisor read data, page not present
instruction pointer     = 0x20:0xffffffff806ae172
stack pointer           = 0x28:0xffffff81083f6e10
frame pointer           = 0x28:0xffffff81083f6f60
code segment            = base 0x0, limit 0xfffff, type 0x1b
                        = DPL 0, pres 1, long 1, def32 0, gran 1
processor eflags        = interrupt enabled, resume, IOPL = 0
current process         = 56795 (kldload)
trap number             = 12
panic: page fault
cpuid = 0
KDB: stack backtrace:
#0 0xffffffff8063dcbe at kdb_backtrace+0x5e
#1 0xffffffff8060aed7 at panic+0x187
#2 0xffffffff80900a10 at trap_fatal+0x290
#3 0xffffffff80900d61 at trap_pfault+0x201
#4 0xffffffff8090121f at trap+0x3df
#5 0xffffffff808e8774 at calltrap+0x8
#6 0xffffffff80640fae at vsnprintf+0x2e
#7 0xffffff0025472b4e at dmapbase+0x25472b4e
```

I have no experience with this sort of situation, but I'd guess that resolving this problem would improve FreeBSD somehow, or something. So I am at your disposal, and thanks in advance for any help.

If it's relevant, I'm running a largely unpatched 8.3-RELEASE, amd64 edition. Let me know if any other info is needed.


----------



## ORTO-DOX (Nov 26, 2012)

Hi! Try to work with *NDISulator* project. His maintainers maybe also could help you.


----------



## pkc (Nov 26, 2012)

Cool, I submitted an issue on the Github page. I'll post any positive results here.


----------



## pkc (Dec 11, 2012)

No luck yet. Might someone point me in the direction where I could learn about how to diagnose this issue / send in a patch / add some code to the driver? I'm willing to help but don't know where to start (And I understand that little drivers like this are not exactly top of the priority list)


----------



## temny (Dec 25, 2012)

I'm sorry for delay with the answer.

Original report gives almost no information about the reason of the problem because backtrack contains no even function names related to ndis.

If you are still interested in discovering this problem lets try to do this step by step.

First lets use ndisulator from github:
1. Go to https://github.com/NDISulator/ndisulator page
2. Select "stable8" branch
3. Download and extract sources (button with cloud and word "Zip")
4. cd to directory where ndisulator sources have been saved and extracted
5. Run "make attach"
6. Run "make build"
7. Run "make install"
Please make sure you got no errors on any of this steps.

At this moment you replaced ndis modules and binaries with non-default implementation from github

8. At this step you have to generate miniport module based on windows driver. 
Windows driver should have the same arch as your freebsd installation (64 bit driver for amd64 case) + it have to be implemented with NDIS 5.X API (most of winXP drivers
implemented using NDIS 5.X API)

For my test case I run command like this:
ndisgen ./windrv/e1G5132e.INF ./windrv/e1G5132e.sys
and after answering all questions of ndisgen I have miniport module named "e1G5132e_sys.ko"

At this moment it is safe to run "make detach" and start testing.

9. Try kldload ./path/to/miniport_module.so and check if new interface appears, if ndis driver attached to your device in pciconf output


Lets try to start from steps above and check how it works or where it fails.

Waiting for you feedback



PS. I've just tested steps 1 to 7 on 8.3-RELEASE (amd64) - it should work fine for you too.

Success of step 8 depends mostly on .inf file. I've seen several .inf files which ndisgen were unable to parse and all this cases were easy to fix. So if it fails on this step please provide us with link to windows driver you are trying to use.

Step 9 significantly depends on windows driver. If you see panic on this step it would be nice to see panic message.


----------



## G_Nerc (Dec 26, 2012)

*temny* thank for detailed how-to! Can you tell, for 9-, and current steps identical?


----------



## temny (Dec 27, 2012)

G_Nerc said:
			
		

> Can you tell, for 9-, and current steps identical?



It took some time to setup test environment on freebsd 9 to check "stable9" branch of ndisulator. I can confirm there was a problem and now it is fixed. I mean now it is possible to use "stable9" on freebsd 9 with steps from above.

What about to freebsd current - I need some time to setup and test. At least for master branch step by step will be different. I'll get back to you as soon as I have more information to share.


----------



## G_Nerc (Dec 27, 2012)

Many thanks to all peoples like you *temny*, who do his own work for many other peoples!


----------



## temny (Jan 5, 2013)

*G_Nerc*, thank you for the kind words.

I've finished testing recent FreeBSD current with ndisulator. Here is what I got:

In case of FreeBSD current there are two options:
First is to use "stable" branch of ndisulator. In this case it is possible to use step-by-step provided few messages above.

Second option is to use "master" branch of ndisulator with the following steps:
1. Go to https://github.com/NDISulator/ndisulator page
2. Select "master" branch (it should be already selected by default)
3. Download and extract sources (Button with cloud and word "Zip"; no need to install git)
4. cd to directory where ndisulator sources have been saved and extracted
5. Run "make"
6. Run "make install"
Please make sure you got no errors in ouput of previous two steps
7. Run "kldload ndis"
8. Load windows miniport driver (.sys file) using "ndisload" (there is no need to generate kernel module based on .inf + .sys files like it was necessary with stable, stable8 and stable9 branches)

I'm going to use ndisgen for the following device as example:
none@pci0:0:3:0:       class=0x020000 card=0x11001af4 chip=0x813910ec rev=0x20 hdr=0x00
    vendor     = 'Realtek Semiconductor Co., Ltd.'
    device     = 'RTL-8139/8139C/8139C+'
    class      = network
    subclass   = ethernet

ndisload -p -s /root/rl8139_3/Rtnic64.sys -n test_dev -v 0x10ec -d 0x8139

ndisload flags:
-p = PCI device
-u = USB device
-P = PCMCIA device
-s PATH = path to windows miniport driver (.sys file)
-n NAME = device name (any name you like)
-v NUMBER = vendor id. Last 4 hex digits of "chip" value in pciconf output. Don't forget to use "0x" prefix (like in "0x10ec") if you are just copying this value from pciconf output
-d NUMBER = device id. First 4 hex digits of "chip" value in pciconf output. Don't forget to use "0x" prefix (like in "0x8139") if you are just copying this value from pciconf output

Same as before windows driver must have the same arch as your freebsd installation (64 bit driver for amd64 case) + it have to be implemented with NDIS 5.X API

9. Check if ndis0 interface appears, if ndis driver attached to your device in pciconf output


----------



## ORTO-DOX (Jan 5, 2013)

Thank for deep investigations and detailed instructions!
I'm do all steps which you provide.
From step 1 to 6 all be without any error or warning.
But when I'm do:
*kldload ndis*

```
kldload: can't load ndis: Exec format error
```
I do:
*kldload /boot/kernel/ndis.ko*
and it loads without errors, but in /var/log/messages i have such messages:

```
Jan  5 22:19:36 BSD kernel: KLD if_ndis.ko: depends on ndisapi - not available or version mismatch
Jan  5 22:19:36 BSD kernel: linker_load_file: Unsupported file type
```
After that I do :
*ndisload -p -s /path/WinX64/rtwlane.sys -n nd_test -v 0x10ec -d 0x8723*
and got in /var/log/messages

```
Jan  5 22:24:48 BSD kernel: NDIS: no match for ExNotifyCallback
Jan  5 22:24:48 BSD kernel: NDIS: no match for ExCreateCallback
Jan  5 22:24:48 BSD kernel: ndis0: <nd_test> port 0xd000-0xd0ff mem 0xf7900000-0xf7903fff irq 18 at device 0.0 on pci4
```
ifconfig -a not shows ndis0 or any new interface
*pciconf -lv* shows that driver attached to device:

```
ndis0@pci0:4:0:0:       class=0x028000 card=0x072610ec chip=0x872310ec rev=0x00 hdr=0x00
    vendor     = 'Realtek Semiconductor Co., Ltd.'
    class      = network
```
Can I try to do something to get WiFi working?

Thanks!


----------



## temny (Jan 5, 2013)

I suppose to make ndis0 interface appear in ifconfig output and make it usable we have to find out what is the reason of kldload error.

At this moment I have several ideas:
First: please make sure there are no ndis or if_ndis modules loaded after system reboot - I want to make sure there were no other versions of ndis modules loaded before we started playing with github version. (Also please make sure you are not trying to load ndis / if_ndis from /boot/loader.conf)

Second: please make sure kernel you are currently running is in sync with the sources in /usr/src

Third: please make sure you repeated all steps from the previous message after you reinstalled kernel (if you reinstalled kernel after installed ndisulator from github)

Also could you please show output of the following commands:
ls -al /boot/*/*ndis*
kldstat -v (after you tried to kldload ndis)
uname -a


----------



## ORTO-DOX (Jan 6, 2013)

Ok! I will do anything for help you *Temny* with that deal.
*uname -a*

```
FreeBSD BSD 10.0-CURRENT FreeBSD 10.0-CURRENT #0: Wed Jan  2 21:40:45 YEKT 2013     user@BSD:/usr/obj/usr/src/sys/BSD  amd64
```
System rebuild from sources getting with csup -g -L 2 /root/my-supfile with **default release=cvs tag=.* sources from date *02.01.2013*
System rebuilds with standart way from /usr/src/Makefile
*/etc/src.conf*

```
WITHOUT_ACCT=YES
WITHOUT_ASSERT_DEBUG=YES
WITHOUT_ATM=YES
WITHOUT_AUDIT=YES
WITHOUT_AUTHPF=YES
WITHOUT_BIND=YES
WITHOUT_BSNMP=YES
WITHOUT_CLANG=YES
WITHOUT_CTM=YES
WITHOUT_FDT=YES
WITHOUT_FLOPPY=YES
WITHOUT_GDB=YES
WITHOUT_HTML=YES
WITHOUT_INET6=YES
WITHOUT_INFO=YES
WITHOUT_IPFILTER=YES
WITHOUT_IPX=YES
WITHOUT_KERNEL_SYMBOLS=YES
WITHOUT_KVM=YES
WITHOUT_LPR=YES
WITHOUT_MAIL=YES
WITHOUT_NCP=YES
WITHOUT_OFED=YES
WITHOUT_PF=YES
WITHOUT_PMC=YES
WITHOUT_QUOTAS=YES
WITHOUT_RCMDS=YES
WITHOUT_RCS=YES
WITHOUT_SHAREDOCS=YES
WITHOUT_ZFS=YES
```
*kldstat* (after reboot)

```
Id Refs Address            Size     Name
 1   44 0xffffffff80200000 9d38e8   kernel
 2    1 0xffffffff80bd4000 6588     cuse4bsd.ko
 3    1 0xffffffff80bdb000 15378    fuse.ko
 4    1 0xffffffff80e12000 32c5     ng_ubt.ko
 5    1 0xffffffff80e16000 8b25     ng_hci.ko
 6    3 0xffffffff80e1f000 a5d      ng_bluetooth.ko
 7    5 0xffffffff80e20000 87da     netgraph.ko
 8    1 0xffffffff80e29000 b4ca     ng_l2cap.ko
 9    1 0xffffffff80e35000 1744e    ng_btsocket.ko
10    1 0xffffffff80e4d000 1e3d     ng_socket.ko
11    1 0xffffffff80e4f000 5fe7d    i915kms.ko
12    1 0xffffffff80eaf000 13df     iicbb.ko
13    4 0xffffffff80eb1000 135b     iicbus.ko
14    1 0xffffffff80eb3000 dc5      iic.ko
15    2 0xffffffff80eb4000 ab68     agp.ko
16    1 0xffffffff80ebf000 2e099    drm2.ko
```
*/boot/loader.conf*

```
autoboot_delay="1"
cuse4bsd_load="YES"
fuse_load="YES"
```
Kernel and system I reinstalled in 2 Jan, as i write above with all steps from /usr/src/Makefile, if it strongly needed I can do it again, but it is deal for 3-4 hours 
*ls -al /boot/*/*ndis**
dl
	
	



```
-r-xr-xr-x  1 root  wheel   72432  8 Ð´ÐµÐº 23:38 /boot/kernel.old/if_ndis.ko
-r-xr-xr-x  1 root  wheel  166832  8 Ð´ÐµÐº 23:38 /boot/kernel.old/ndis.ko
-r-xr-xr-x  1 root  wheel   72432  2 ÑÐ½Ð² 21:45 /boot/kernel/if_ndis.ko
-r-xr-xr-x  1 root  wheel  267048  5 ÑÐ½Ð² 22:19 /boot/kernel/ndis.ko
```
*kldload ndis*

```
kldload: can't load ndis: Exec format error
```
*kldstat -v* (after kldload ndis)
http://pastebin.com/MPrA9ur2
*/var/log/messages* (after kldload ndis)

```
Jan  6 18:01:51 BSD kernel: KLD if_ndis.ko: depends on ndisapi - not available or version mismatch
Jan  6 18:01:51 BSD kernel: linker_load_file: Unsupported file type
```
*kldstat -v* (after kldload /boot/kernel/ndis.ko)
http://pastebin.com/kPgeBtSy
*/var/log/messages* (after kldload /boot/kernel/ndis.ko)

```
Jan  6 18:03:13 BSD root: Unknown USB device: vendor 0x5986 product 0x0315 bus uhub4
Jan  6 18:03:13 BSD last message repeated 3 times
```


----------



## ORTO-DOX (Jan 6, 2013)

Also some additional information which maybe can be useful:
*kernel config*
http://pastebin.com/2Kkx3p9T
When I do 
*ndisload -p -s /path/WinX64/rtwlane.sys -n nd_test -v 0x10ec -d 0x8723*
command not ends and I can't kill it anyhow (not killall ndisload, not kill -9 ${PID})
And info about output in /var/log/messages after *kldload /boot/kernel/ndis.ko*

```
Unknown USB device: vendor 0x5986 product 0x0315 bus uhub4
```
described in mailing list *HERE*


----------



## temny (Jan 6, 2013)

Thanks for detailed report. I think I found and fixed the reason of [CMD=""]kldload: can't load ndis: Exec format error"[/CMD]. Please download fresh version of ndisulator from github and retry. 

What about to [CMD=""]Unknown USB device: vendor 0x5986 product 0x0315 bus uhub4[/CMD]. I think this message could be just ignored. It is about webcam and look like it is caused by re-probe attempt triggered by ndisload.

If with the fresh version of ndisulator-master you obtain no progress please give me a link to windows drivers which you are using. Also please let me know if you tried to use stable branch of ndisulator with FreeBSD current and what was the result.


----------



## ORTO-DOX (Jan 7, 2013)

Hi! There is a new info with updated *ndisulator*
*ls -al /boot/*/*ndis**

```
-r-xr-xr-x  1 root  wheel   72432  8 Ð´ÐµÐº 23:38 /boot/kernel.old/if_ndis.ko
-r-xr-xr-x  1 root  wheel  166832  8 Ð´ÐµÐº 23:38 /boot/kernel.old/ndis.ko
-r-xr-xr-x  1 root  wheel  267048  7 ÑÐ½Ð² 15:51 /boot/kernel/ndis.ko
```
There is no error after *kldload ndis*, and in /var/log/messages

```
Jan  7 20:13:11 BSD root: Unknown USB device: vendor 0x5986 product 0x0315 bus uhub4
```
and *kldstat -v*
http://pastebin.com/A3Wsc80u
After *ndisload -p -s /path/WinX64/rtwlane.sys -n nd_test -v 0x10ec -d 0x8723* there is a messages in /var/log/messages

```
Jan  7 20:17:16 BSD kernel: NDIS: no match for ExNotifyCallback
Jan  7 20:17:16 BSD kernel: NDIS: no match for ExCreateCallback
Jan  7 20:17:16 BSD kernel: ndis0: <nd_test> port 0xd000-0xd0ff mem 0xf7900000-0xf7903fff irq 18 at device 0.0 on pci4
```
*ifconfig -a* not shows ndis0 interface and ndisload command againg not detach from console and not kills.

There is a LINK to driver from which I get WinX64 folder:
Also there is LINK to archive with .sys and .inf files which I use (for except downloading providing .exe file)

*========= Ndisulator-stable with -current sources*
I'm download ndisulator stable, and do:
1. cd /path/ndisulator-stable/src
2. su root
3. make attach
4. make build
5. make install
That steps proceeds without error.
After that I'm reboot and 
1. cd /path/to/WinX64/driver
2. ndisgen
And when I'm choose *.inf* and *.sys* driver and on latest step i got the error:

```
Kernel module generation


        The script will now try to generate the kernel driver module.
        This is the last step. Once this module is generated, you should
        be able to load it just like any other FreeBSD driver module.

        Press enter to compile the stub module and generate the driver
        module now:

        Generating Makefile... done.
        Building kernel module... /usr/src/usr.sbin/ndiscvt/windrv_stub.c:136: warning: redundant redeclaration of 'device_probe_desc' [-Wredundant-decls]
./device_if.h:27: warning: previous declaration of 'device_probe_desc' was here
/usr/src/usr.sbin/ndiscvt/windrv_stub.c:137: error: redefinition of typedef 'device_probe_t'
./device_if.h:29: error: previous declaration of 'device_probe_t' was here
build failed. Exiting.
```


----------



## temny (Jan 7, 2013)

About *ndisulator-stable*:
Please retry the same but without reboot after step 5. Or (if reboot is necessary) execute following commands as root before running ndisgen:
[CMD=""]cd /path/ndisulator-stable
make attach
cd -[/CMD]

I've just tested ndisgen step in my test environment and it went smooth with *.inf* and *.sys* files you are working with.


----------



## ORTO-DOX (Jan 7, 2013)

You are right!
Without reboot, and follow to your instructions I successfully generate *rtwlane_sys.ko*, but after tryin to *kldload ./rtwlane_sys.ko*, system immediately reboots.
In /var/log/messages I have not any entries, maybe because HDD not sync on reboots.


----------



## temny (Jan 7, 2013)

I think this reboot is caused by kernel panic and to dig it any deeper it is necessary to rebuild kernel with at least basic debugging support to see panic message and backtrace.

If you are interested - it is necessary to enable following kernel options:
[CMD=""]options        KDB
options        KDB_TRACE 
options        DDB
[/CMD]

You mentioned before that is take 3-4 hours to rebuild kernel. I think it can be done several times faster if you add -DNO_CLEAN to make command. Something like this:
[CMD=""]cd /usr/src
make kernel KERNCONF=... -DNO_CLEAN
[/CMD]

After you reinstall kernel it is necessary to repeat ndisulator install procedure (because ndis modules got overwritten during kernel reinstall).


----------



## ORTO-DOX (Jan 7, 2013)

Ok. Many thanks again for your hard work!
I do all what You say, and paste here results.
What I will need to do after that procedure and when kernel panic appears?
Can you please get steps which I need to do to get something to paste here.


----------



## ORTO-DOX (Jan 7, 2013)

I do that steps:
1. Add to kernel config (Previous kernel config as pasted some messages above: LINK) three lines:

```
options KDB
 options KDB_TRACE 
 options DDB
```
2. *rm -rf /usr/obj && cd /usr/src && make kernel* (KERNCONF defined in /etc/make.conf)
3. My /etc/sysctl.conf

```
hw.syscons.bell=0
kern.module_path=/boot/kernel;/boot/modules;/usr/local/modules
vfs.usermount=1
kern.coredump=0
kern.nodump_coredump=1
kern.sync_on_panic=1
```
4. reboot
5. All steps (1..5) for *ndisulator-stable*
6. ndisgen kernel module from windows drivers
7. *kldload ./rtwlane_sys.ko*
System freezes. Not reboots, just freezes, and I don't know how to get any interested information.
After reset, in /var/log/messages have not any information related to ndis actions.


----------



## temny (Jan 7, 2013)

Minimum possible information is just a panic message with a stack trace which consists of list of functions which got called before the panic occurs. This kind of information you will get if you just rebuild the kernel with the options mentioned above and try to *kldload* the module generated with ndisgen. In general it is important to trigger a panic while you are in the console (in text mode) and not in X. If you are in X (in "graphic mode") you see nothing - everything just freezes (because the kernel stopped execution and all kdb/ddb output is sent to the text console and for you there is no way to see it or switch into it).

By default when you see a panic message there is no way to "save it" so you have to take a picture using a camera or copy it by hand, which is annoying. It is possible to define the device which will be used as "temporary storage" or "device for dump" by setting the dumpdev variable in rc.conf:

```
dumpdev="/dev/ada0s1b"
```

The swap partition is commonly used for this purpose.

Please make sure you are not pointing dumpdev to a partition which has a valid filesystem and data - everything will be destroyed if ddb tries to write any data to such a device.

When you have a dumpdev you can extract and save much more information (including kmem) about the current state of the system when you triggered a panic. This could be done automatically (by altering /etc/ddb.conf and setting sysctl debug.debugger_on_panic to zero) or manually.

The list of commands I have in ddb.conf is the following:
[CMD=""]script kdb.enter.panic=capture on; show allpcpu; bt; ps; alltrace; show alllocks; show locks; show msgbuf; capture off; call doadump[/CMD]

Also I added the following lines to /etc/sysctl.conf:

```
#debug.debugger_on_panic=0
debug.ddb.capture.bufsize=5242880
```

Uncomment the first line if you like to execute commands defined in *script kdb.enter.panic* automatically in case of a panic.

If everything went smoothly then after the reboot it will be possible to find the ddb output in the /var/crash directory.

You can try to find more information in the "Kernel Debugging" chapter in "FreeBSD Developers' Handbook" and ddb(4) and ddb(8).


----------



## ORTO-DOX (Jan 7, 2013)

Thank for necessary info about system debugging!
I am until wait for that info, search myself that in internet and there is my results:
1. I am add sysctl variable for buffer increasing
2. Set dumpdev="AUTO"
3. /etc/rc.d/dumpon start
4. exit from X
5. proceed all ndis steps to panic appears
6. In DB console type textdump set; and get dump (forget command)
And attach zippped /var/crash/textdump.crash.0
After that I will do your procedure and if my result is incorrect, I will send your results.


----------



## temny (Jan 7, 2013)

Your steps seems to be correct. 
Now lets extend amount data you are gathering from ddb. Please trigger panic one more time and type following commands in ddb prompt:
`textdump set
capture on
show allpcpu
bt
ps
alltrace
show msgbuf
capture off
call doadump`

Or do the same via ddb.conf as I mentioned in previous message.


----------



## ORTO-DOX (Jan 7, 2013)

There is a result in attached file.


----------



## temny (Jan 7, 2013)

As I can see only part of ddb commands output has been saved. I suppose some commands were interrupted with "q" key or in some other way. Please try to repeat same steps, but scroll down every command output with space key (just hold it and ddb will scroll until the end).


----------



## ORTO-DOX (Jan 7, 2013)

Yes  Sorry, I don't know such thing.
Now full output, thank you one more time and now I'm go sleep.
Thanks!


----------



## temny (Jan 8, 2013)

Please try to do the same with "*stable_tmp*" branch available using this link: https://github.com/vladmovchan/ndisulator/commits/stable_tmp


----------



## ORTO-DOX (Jan 9, 2013)

With *ndisulator-stable_tmp* branch I have a panic, and there is a crash dump in attach.


----------



## ORTO-DOX (Jan 26, 2013)

Sorry for my annoying, I just don't know where to find help with that really terrible problem  My laptop like PC, it has internet only through LAN, while everywhere internet work through Wi-Fi.


----------

