accessing the network card

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
aloktiagi
Member
Member
Posts: 52
Joined: Fri Apr 23, 2010 11:03 am

Re: accessing the network card

Post by aloktiagi »

OK found it in the dev guide for broadcom, we have to set command register to enable i/o or memory access

Thanks
mkruk
Posts: 11
Joined: Thu Apr 15, 2010 9:34 am

Re: accessing the network card

Post by mkruk »

aloktiagi wrote:OK found it in the dev guide for broadcom, we have to set command register to enable i/o or memory access

Thanks
yep, that's what I mean with bit 0 and bit 1 of the command register.
You should also note that, when trying to figure out the io base and memory base, you will first have to distinguish whether the address in BARn is the io base or memory base. In my OS, im looping through BAR0 - BAR5 and check the first bit of the BAR. If it is set to 1 its an io base, otherwise its a memory base.

It's actually described quite good in the wiki, but here's still a code snipped how I'm discovering the bases in my os:

Code: Select all

u32_t bar[6];
u32_t iobase, membase;
u8_t i;

/* I assume that all base address registers are stored in bar */

iobase = 0;
membase = 0;

for(i = 0; i < 6; i++) {
	if(bar[i] & 0x1) {
		/* bit 0 is set, hence its an io base */
		if(!iobase) {
			iobase = bar[i] & ~0x3;
		}
	} else {
		/* bit 0 is clear; its a memory base */
		if(!membase) {
			membase = bar[i] & ~0xf;
		}
	}
}
Note that the code snipped above does not check if the memory base is 32-bit or 64-bit. Neither does it work very well when a device has more than one io base or memory base. So you should also check for that if you want a proper implementation.
aloktiagi
Member
Member
Posts: 52
Joined: Fri Apr 23, 2010 11:03 am

Re: accessing the network card

Post by aloktiagi »

Hi

I got the BAR0 as F8000004. So my memory base address is F8000000. So using this address can i now access NICs registers by adding offsets to this memory base address, for eg

Code: Select all

inb(F8000000+offset)
mkruk
Posts: 11
Joined: Thu Apr 15, 2010 9:34 am

Re: accessing the network card

Post by mkruk »

not quite.. you have to use the io base instead of the memory base
but aside of that its correct to use inb(iobase+offset)
aloktiagi
Member
Member
Posts: 52
Joined: Fri Apr 23, 2010 11:03 am

Re: accessing the network card

Post by aloktiagi »

Hi,

Ok but i found only a memory base address i.e BAR[0] there was no other address in the other BAR registers. So where is the I/O base register value?
mkruk
Posts: 11
Joined: Thu Apr 15, 2010 9:34 am

Re: accessing the network card

Post by mkruk »

oops, sorry I thought we're talking about a realtek nic here.
According to broadcom's specs, the registers are located in the memory address space (offset 0x400 - 0x8000 [see page 32 of the manual]), so membase+offset is correct.
aloktiagi
Member
Member
Posts: 52
Joined: Fri Apr 23, 2010 11:03 am

Re: accessing the network card

Post by aloktiagi »

I got the BAR0 as F8000004. So my memory base address is F8000000.

Now when i do something like this

Code: Select all

add=0x410;
    for (j = 0; j < 10; j++)
    {
        addr = F8000000;
        reg_value = inl(addr+add);
        printf("REG VALUE = %X \n",reg_value);
        add = add + 0x04;
    }
Here all that i get for register vlaues are FFFF and don't see any MAC addresses which should be present at offset ox410!!
mkruk
Posts: 11
Joined: Thu Apr 15, 2010 9:34 am

Re: accessing the network card

Post by mkruk »

aloktiagi wrote:

Code: Select all

        addr = F8000000;
Doesn't this cause a compiler error?
Try 0xf8000000 instead.

I've just had a look at the manual and the offset you're using isn't correct. According to the manual the device id registers are located at offset 0x2 (see page 152, NetXtreme II Programmer's Reference Guide)
Selenic
Member
Member
Posts: 123
Joined: Sat Jan 23, 2010 2:56 pm

Re: accessing the network card

Post by Selenic »

@aloktiagi
You can't use IO instructions on memory. They're two completely independent address spaces; you just use a normal pointer at (membase + offset). So you can dereference it at whatever address you want, use memcpy or whatever to transfer buffers, etc. etc. That ease of use, along with the fact that memory accesses are faster and have a larger address space, is why you're unlikely to find IO ports used for non-legacy purposes.
aloktiagi
Member
Member
Posts: 52
Joined: Fri Apr 23, 2010 11:03 am

Re: accessing the network card

Post by aloktiagi »

@selenic
Yes thats what i thought,but then mkruk told using inb for memory addresses was ok :(.
So i'll use something like void *memcpy(void *dest, const void *src, size_t n); to transfer register data at F8000000
eddyb
Member
Member
Posts: 248
Joined: Fri Aug 01, 2008 7:52 am

Re: accessing the network card

Post by eddyb »

aloktiagi wrote:@selenic
Yes thats what i thought,but then mkruk told using inb for memory addresses was ok :(.
So i'll use something like void *memcpy(void *dest, const void *src, size_t n); to transfer register data at F8000000
This is enough:

Code: Select all

char reg_value = *((char*)addr);
If you replace char with short or int, you get all register sizes ;).
aloktiagi
Member
Member
Posts: 52
Joined: Fri Apr 23, 2010 11:03 am

Re: accessing the network card

Post by aloktiagi »

@eddyb

I used your method
char reg_value = *((char*)0xF8000000);
So i should get the value of the register at 0xF8000000 in reg_value?

But i'm getting a seg fault :(. Any idea why?
aloktiagi
Member
Member
Posts: 52
Joined: Fri Apr 23, 2010 11:03 am

Re: accessing the network card

Post by aloktiagi »

Could someone comment on the previous post!!
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: accessing the network card

Post by Combuster »

Of course nobody has a clue. The intel processor doesn't do segfaults. It's a unix thing.

So if I have to trust your description, you're trying to access hardware from an existing OS. Which is just stupid because the kernel won't allow it.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
aloktiagi
Member
Member
Posts: 52
Joined: Fri Apr 23, 2010 11:03 am

Re: accessing the network card

Post by aloktiagi »

And it is stupid to not have anticipated this from my previous posts :)
Post Reply