-
Notifications
You must be signed in to change notification settings - Fork 4
Implement RTL8139 Driver #81
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
KieranMusser
wants to merge
4
commits into
UMN-Kernel-Object:trunk
Choose a base branch
from
KieranMusser:kieran/rtl8139
base: trunk
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,154 @@ | ||
| #include <arch/riscv64/insns.h> | ||
|
|
||
| #include <drivers/rtl8139.h> | ||
|
|
||
| #include <mm/paging.h> | ||
| #include <mm/virtual_alloc.h> | ||
| #include <physical.h> | ||
| #include <print.h> | ||
|
|
||
|
|
||
| constexpr usize TX_BUFF_SIZE = 0x1700; | ||
|
|
||
| enum RTL8139_REGS { | ||
| REG_COM = 0x37, | ||
| REG_TCR = 0x40, | ||
| REG_CONFIG1 = 0x52 | ||
| }; | ||
|
|
||
| enum RTL8139_CONST { | ||
| TSD_TOK = (1 << 15), | ||
| TSD_OWN = (1 << 13), | ||
|
|
||
| COM_RST = (1 << 4), | ||
| COM_RE = (1 << 3), | ||
| COM_TE = (1 << 2) | ||
| }; | ||
|
|
||
| u8 rtl8139_tbuff[4][TX_BUFF_SIZE] __attribute__((aligned(256))); | ||
|
|
||
| struct rtl8139_regs { | ||
| u8 mac[6]; | ||
| u8 pad0[10]; | ||
| u32 tsd[4]; | ||
| u32 tsad[4]; | ||
| u8 pad1[7]; | ||
| u8 com; | ||
| u8 pad2[26]; | ||
| u8 config1; | ||
| }; | ||
|
|
||
| static_assert( offsetof(struct rtl8139_regs, tsad) == 0x20); | ||
| static_assert( offsetof(struct rtl8139_regs, tsd) == 0x10); | ||
| static_assert( offsetof(struct rtl8139_regs, com) == 0x37); | ||
| static_assert( offsetof(struct rtl8139_regs, config1) == 0x52); | ||
|
|
||
| volatile struct rtl8139_regs *rtl_regs; | ||
|
|
||
| u64 g_regs_addr; | ||
| u64 g_cur_buf = 0; | ||
|
|
||
| bool mm_paging_walk(uaddr va, paddr *pte, bool alloc); | ||
|
|
||
| u32 walkaddr(uaddr addr) { | ||
| paddr pte_addr; | ||
| u64 pte; | ||
| u32 off; | ||
|
|
||
| off = addr & 0xfff; | ||
| addr &= (~0xfffUL); | ||
|
|
||
| assert(mm_paging_walk(addr, &pte_addr, false)); | ||
|
|
||
| copy_from_physical(&pte, pte_addr, sizeof pte); | ||
|
|
||
| pte = (pte >> 10) << 12; | ||
| assert(pte < U32_MAX); | ||
| return (u32) pte | off; | ||
| } | ||
|
|
||
| bool rtl8139_send_packet(u8 *buffer, u32 len) { | ||
| u32 phys_buffer = walkaddr((uaddr) (&rtl8139_tbuff[0])); | ||
| u64 i; | ||
|
|
||
| print("{u32:x}", phys_buffer); | ||
|
|
||
| for (i=0; i<len; ++i) { | ||
| rtl8139_tbuff[0][i] = buffer[i]; | ||
| } | ||
|
|
||
| if ((rtl_regs->tsd[0] & TSD_OWN) == 0) { | ||
| return false; | ||
| } | ||
|
|
||
| rtl_regs->tsad[0] = phys_buffer; | ||
| rtl_regs->tsd[0] = len; | ||
|
|
||
| ++g_cur_buf; | ||
| return true; | ||
| } | ||
|
|
||
| bool eth_send_packet(u8 *buffer, u32 len) { | ||
| u8 buf[0x100]; | ||
| u64 i; | ||
|
|
||
| for (i=0; i<6; ++i) { | ||
| buf[i] = 0xff; | ||
| buf[i + 6] = rtl_regs->mac[i]; | ||
| } | ||
| buf[12] = 0x08; | ||
| buf[13] = 0x00; | ||
|
|
||
| for (i=0; i<len; ++i) { | ||
| buf[i + 14] = buffer[i]; | ||
| } | ||
| for (i=len; i<64; ++i) { | ||
| buf[i + 14] = 0; | ||
| } | ||
| if (len < 64) len = 64; | ||
|
|
||
| return rtl8139_send_packet(buf, len + 13); | ||
| } | ||
|
|
||
| void rtl8139_test() { | ||
| eth_send_packet((u8*) "yellow submarine", 16); | ||
|
|
||
| assert(rtl_regs->tsad[0] != 0); | ||
| assert((rtl_regs->tsd[0] & TSD_TOK) != 0); | ||
| } | ||
|
|
||
| void rtl8139_init(struct pci_device *device) { | ||
| print("initializing rtl8139"); | ||
|
|
||
| u32 reg_addr = 0x40000000; | ||
| g_regs_addr = reg_addr; | ||
|
|
||
| uaddr lo, hi; | ||
| struct vma *vma; | ||
| vma = vma_alloc(&kernel_virtual_allocator, 1); | ||
| assert(vma); | ||
|
|
||
| vma_bounds(vma, &lo, &hi); | ||
|
|
||
| mm_paging_map(lo, paddr_of_bits(reg_addr), PGPERM_KRW); | ||
|
|
||
| rtl_regs = (struct rtl8139_regs*) lo; | ||
|
|
||
|
|
||
|
|
||
| /* TODO: Maybe move PCI init to pci.c? */ | ||
| /* Setup BAR for MMIO - we just pick a nice number for now */ | ||
| device->memar = reg_addr; | ||
| device->cmd |= 0x6; | ||
|
|
||
| rtl_regs->config1 = 0; | ||
|
|
||
| rtl_regs->com = COM_RST; | ||
| while (rtl_regs->com & COM_RST) ; | ||
| rtl_regs->com = COM_RE | COM_TE; | ||
|
|
||
| rtl_regs->tsad[0] = 0x12; | ||
| assert(rtl_regs->tsad[0] == 0x12); | ||
|
|
||
| rtl8139_test(); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| #ifndef UKO_OS_KERNEL__RTL8139_H | ||
| #define UKO_OS_KERNEL__RTL8139_H 1 | ||
|
|
||
| #include <types.h> | ||
| #include <pci.h> | ||
|
|
||
| void rtl8139_init(struct pci_device *device); | ||
| void rtl8139_test(); | ||
| bool eth_send_packet(u8 *buffer, u32 len); | ||
|
|
||
| #endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| #ifndef UKO_OS_KERNEL__PCI_H | ||
| #define UKO_OS_KERNEL__PCI_H 1 | ||
|
|
||
| #include <types.h> | ||
|
|
||
|
|
||
| void pci_enumerate(); | ||
|
|
||
| struct pci_device { | ||
| u16 vid; | ||
| u16 did; | ||
| u8 cmd; | ||
| u8 pad[15]; | ||
| u32 memar; | ||
| }; | ||
| static_assert( offsetof(struct pci_device, memar) == 0x14); | ||
|
|
||
|
|
||
| #endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| #include <pci.h> | ||
| #include <print.h> | ||
| #include <mm/paging.h> | ||
| #include <mm/virtual_alloc.h> | ||
| #include <physical.h> | ||
|
|
||
| #include <drivers/rtl8139.h> | ||
|
|
||
| constexpr u64 pci_config = 0x30000000L; | ||
|
|
||
| void pci_enumerate() { | ||
| uaddr lo, hi; | ||
| struct vma *vma; | ||
|
|
||
| vma = vma_alloc(&kernel_virtual_allocator, 32); | ||
| assert(vma); | ||
|
|
||
| vma_bounds(vma, &lo, &hi); | ||
|
|
||
| for (u64 i=0; i<32; ++i) { | ||
| mm_paging_map(lo + i * 0x1000, paddr_of_bits(pci_config + i * 0x1000), PGPERM_KRW); | ||
| } | ||
| for (u64 dev=0; dev<32; ++dev) { | ||
| u64 bus = 0; | ||
| u64 func = 0; | ||
| u64 offset = 0; | ||
| offset = (bus << 16) | (dev << 11) | (func << 8) | (offset); | ||
| struct pci_device *device = (struct pci_device *)(lo + offset); | ||
|
|
||
| u16 did = device->did; | ||
| u16 vid = device->vid; | ||
|
|
||
| if (vid == 0x10ec && did == 0x8139){ //0xedf11efd) { | ||
| rtl8139_init(device); | ||
| break; | ||
| } else if (did != 0xffff && vid != 0xffff && vid) { | ||
| print("unknown pci device: {u16:x} vender: {u16:x}", did, vid); | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh hm, my first instinct would be to say "make this a selftest," but we don't want selftests that depend on the user having certain hardware either...
This probable deserves some thought.
Should this be exposed to userspace, so we can hit it in CI (maybe this would be a build time option, see #62)? Should we accumulate a list of selftests for devices (not drivers) and allow this to be run later in boot?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah, I tried a selftest and realized that myself, I had thought of some sort of devices selftest, but I don't want to create too many different types of selftest, so I would be in favor of exposing to userspace and having some userspace testing