简体   繁体   中英

reading Memory-Mapped IO registers How to (from datasheet) and using them in mmap

I have intel system with Ethernet controller: Intel Corporation 82579LM Gigabit Network Connection (Lewisville) (rev 04) . I have downloaded the datasheet like datasheet for Intel ® 82579 Gigabit Ethernet PHY

Now I am reading resource0 of pci ethernet device like

    if((fd = open("/sys/bus/pci/devices/0000:00:19.0/resource1", O_RDWR | O_SYNC)) == -1) {
        perror("Error: open error");
    }

    int map_size = 4096UL;
    ...
    map_base = mmap(0,4096UL , PROT_READ, MAP_SHARED, fd, Register_Values_From_Datasheet);

I have x86-64 bit system. kali linux 5.7. so I am substituting Register_Values_From_Datasheet from above with register offset values from datasheet like 0x00008 for STATUS:Device Status Register =from datasheet

but errno of mmap() is having value 22:EINVAL after mmap call. Means invalid argument. Maybe the offset values that I am reading from datasheet may have to be interpreted some other way.

My resource file in sys/bus/pci/devices/0000:00:19.0 of Ethernet device is something like this

        0x00000000fe400000 0x00000000fe41ffff 0x0000000000040200
        0x00000000fe427000 0x00000000fe427fff 0x0000000000040200
        0x000000000000f060 0x000000000000f07f 0x0000000000040101
        0x0000000000000000 0x0000000000000000 0x0000000000000000
        0x0000000000000000 0x0000000000000000 0x0000000000000000
        0x0000000000000000 0x0000000000000000 0x0000000000000000
        0x0000000000000000 0x0000000000000000 0x0000000000000000
        0x0000000000000000 0x0000000000000000 0x0000000000000000
        0x0000000000000000 0x0000000000000000 0x0000000000000000
        0x0000000000000000 0x0000000000000000 0x0000000000000000
        0x0000000000000000 0x0000000000000000 0x0000000000000000
        0x0000000000000000 0x0000000000000000 0x0000000000000000
        0x0000000000000000 0x0000000000000000 0x0000000000000000

As from above dump of resource text file (resource0) shows my Memory-Mapped IO starts at 0x00000000fe400000. So that I think should be the return address of mmap (return (void*)=&0x00000000fe400000)= but I am getting something like 0xffffffff and errno is 22. Can any one guide me in the right direction as to how the offset needs to be interpreted from datasheet. Also which registers normally need to be accessed for getting packets for pci Ethernet device. Since I am new to device programming.

Resource0 is the list of resources, the remaining Resource1.. ResourceN are the actual memory regions where the registers are. (Ref: https://techpubs.jurassic.nl/manuals/linux/developer/REACTLINUX_PG/sgi_html/ch07.html )

The offsets are offsets INTO a region, not offsets of the region.

So you would typically map the entire space of region1 - in your case 128KB because: 0x00000000fe41ffff - 0x00000000fe400000 . (you could also check /proc/iomem for confirmation)

map_base = mmap(0,32*4096UL , PROT_READ, MAP_SHARED, fd, 0); 

EDIT: mmap's signature is:

void *mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset);

If you specify a length or offset that exceeds the filesize, you get EINVAL: From Linux man page:

EINVAL We don't like addr, length, or offset (e.g., they are too large, or not aligned on a page boundary).

You'd probably end up casting map_base into an array of uint32_t (if all registers are 32 bit) and using: map_base_as_int[8/4] to index into the register space.

The first hurdle is getting read-only data (eg, MAC addresses, and the like).

Once you actually want to send and receive packets, you'll need physical addresses - DPDK code handles this (in a rather complex way), but you can slice out physical memory with GRUB parameters and safely use it....


An alternate approach is to use mmap on '/dev/mem' using the offset from above fe400000 ) You'll need this for the physical memory accesses later anyway (and you need to ensure your kernel is compiled for access - some locked down kernels aren't)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM