Added extremely basic code for the PCI NE2K NIC which is emulated in Bochs/KVM. Reorg...
authorPaul Pearce <pearce@eecs.berkeley.edu>
Fri, 7 Aug 2009 03:42:04 +0000 (23:42 -0400)
committerPaul Pearce <pearce@eecs.berkeley.edu>
Fri, 7 Aug 2009 21:27:54 +0000 (17:27 -0400)
Went through and added a basic interrupt sanity check for the NE2K Nic. This is so we can determine the functionality
of PCI based interrupts in a virtual (Bochs/KVM) enviroment. If the device is detected it starts the device, generates
two interrupts, then falls back out.

Code was also reoganized to roughly support two nic drivers existing in the same space. This doesn't allow them
to function at the same time, just coexist.

There is a really nasty Bochs bug that causes PCI interrupts to show up at 2 locations in the IOAPIC. The source
of the bug has been determined, and a bug report filed with Bochs. There are two ways to fix it, if they choose
way 1 the code in this commit will allow PCI interrupts to function properly through the IOAPIC. If they choose
way 2 (or some other way I haven't though of) some ioapic_init() code will need to be rewritten to support the
broken mptable case.

Note: There was a line in .gitignore that caused the two new ne2k files to be ignored. This change to gitignore
is critical to correct functionality!

.gitignore
kern/arch/i386/trap.c
kern/include/ne2k.h [new file with mode: 0644]
kern/include/rl8168.h
kern/src/Makefrag
kern/src/init.c
kern/src/mptables.c
kern/src/ne2k.c [new file with mode: 0644]
kern/src/rl8168.c

index 513b1e5..c49f8b7 100644 (file)
@@ -11,7 +11,6 @@ cscope.out
 hdd.img
 *.*~
 Makelocal
-ne2k*
 .textmate*
 .DS_*
 ros-project.tmproj
index 0efd4ed..383afb4 100644 (file)
@@ -270,13 +270,14 @@ void
        
        lapic_send_eoi();
        
-/*     Old PIC relatd code. Should be gone for good, but leaving it just incase.
+       /*
+       //Old PIC relatd code. Should be gone for good, but leaving it just incase.
        if (tf->tf_trapno < 48)
-               //pic_send_eoi(tf->tf_trapno - PIC1_OFFSET);
-               ioapic_send_eoi(tf->tf_trapno); // Quick hack. Fix.
+               pic_send_eoi(tf->tf_trapno - PIC1_OFFSET);
        else
                lapic_send_eoi();
-*/
+       */
+
 }
 
 void
diff --git a/kern/include/ne2k.h b/kern/include/ne2k.h
new file mode 100644 (file)
index 0000000..f64f1c7
--- /dev/null
@@ -0,0 +1,29 @@
+#ifndef ROS_INC_NE2K_H
+#define ROS_INC_NE2K_H
+
+#include <arch/types.h>
+#include <trap.h>
+#include <pmap.h>
+
+#define ne2k_debug(...)  cprintf(__VA_ARGS__)  
+#define ne2k_interrupt_debug(...) //cprintf(__VA_ARGS__)  
+#define ne2k_frame_debug(...)  cprintf(__VA_ARGS__)  
+
+#define NIC_IRQ_CPU                    5
+
+// Macro for formatting PCI Configuration Address queries
+#define MK_CONFIG_ADDR(BUS, DEV, FUNC, REG) (unsigned long)( (BUS << 16) | (DEV << 11) | \
+                                                             (FUNC << 8) | REG  | \
+                                                             ((uint32_t)0x80000000))
+#define NE2K_VENDOR_ID 0x10EC
+#define NE2K_DEV_ID 0x8029
+
+void ne2k_init();
+int ne2k_scan_pci();
+void ne2k_configure_nic();
+void ne2k_setup_interrupts();
+void ne2k_interrupt_handler(trapframe_t *tf, void* data);
+
+
+
+#endif /* !ROS_INC_NE2K_H */
index 2bf3c75..b536d5e 100644 (file)
@@ -5,11 +5,16 @@
 #include <trap.h>
 #include <pmap.h>
 
-#define nic_debug(...)  //cprintf(__VA_ARGS__)  
-#define nic_interrupt_debug(...) //cprintf(__VA_ARGS__)  
-#define nic_frame_debug(...)  //cprintf(__VA_ARGS__)  
+#define rl8168_debug(...)  //cprintf(__VA_ARGS__)  
+#define rl8168_interrupt_debug(...) //cprintf(__VA_ARGS__)  
+#define rl8168_frame_debug(...)  //cprintf(__VA_ARGS__)  
 
-#define NIC_IRQ_CPU                    5
+// We need to provide some global interface for sending and receiving packets to a generic interface
+//  for now, that is this set of Macros! They aren't in caps since the final inteface shoudn't be.
+#define packet_wrap rl8168_packet_wrap
+#define send_frame rl8168_send_frame
+
+#define NE2K_IRQ_CPU                   5
 
 // Macro for formatting PCI Configuration Address queries
 #define MK_CONFIG_ADDR(BUS, DEV, FUNC, REG) (unsigned long)( (BUS << 16) | (DEV << 11) | \
 #define MAX_PACKET_DATA                MAX_FRAME_SIZE - PACKET_HEADER_SIZE
 // This number needs verification! Also, this is a huge hack, as the driver shouldnt care about UDP/IP etc.
 
+const char *rl8168_packet_wrap(const char* data, size_t len);
+
+
 // ^----- Evil line ------^
 
 // v----- Good line ------v
 
 
-void nic_init(void);
-void reset_nic(void);
-void nic_interrupt_handler(trapframe_t *tf, void* data);
-int scan_pci(void);
-void read_mac(void);
-void setup_interrupts(void);
-void setup_descriptors(void);
-void configure_nic(void);
-void nic_handle_rx_packet(void);
-void set_rx_descriptor(uint32_t des_num, uint8_t reset_buffer);
-void set_tx_descriptor(uint32_t des_num);
-void process_frame(char *frame_buffer, uint32_t frame_size, uint32_t command);
-int send_frame(const char *data, size_t len);
-const char *packet_wrap(const char* data, size_t len);
-
+void rl8168_init(void);
+void rl8168_reset(void);
+void rl8168_interrupt_handler(trapframe_t *tf, void* data);
+int rl8168_scan_pci(void);
+void rl8168_read_mac(void);
+void rl8168_setup_interrupts(void);
+void rl8168_setup_descriptors(void);
+void rl8168_configure(void);
+void rl8168_handle_rx_packet(void);
+void rl8168_set_rx_descriptor(uint32_t des_num, uint8_t reset_buffer);
+void rl8168_set_tx_descriptor(uint32_t des_num);
+void rl8168_process_frame(char *frame_buffer, uint32_t frame_size, uint32_t command);
+int rl8168_send_frame(const char *data, size_t len);
 
 #endif /* !ROS_INC_REALTEK_H */
index 4b8f8b4..0a8407f 100644 (file)
@@ -37,6 +37,7 @@ KERN_SRCFILES := $(KERN_ARCH_SRCFILES) \
                  $(KERN_SRC_DIR)/timer.c \
                  $(KERN_SRC_DIR)/kmalloc.c \
                  $(KERN_SRC_DIR)/rl8168.c \
+                 $(KERN_SRC_DIR)/ne2k.c \
                  $(KERN_SRC_DIR)/testing.c
 
 # Only build files if they exist.
index 13d1ef1..7548d6a 100644 (file)
@@ -27,6 +27,7 @@
 #include <manager.h>
 
 #include <rl8168.h>
+#include <ne2k.h>
 #include <mptables.h>
 #include <pci.h>
 #include <arch/ioapic.h>
@@ -65,7 +66,8 @@ void kernel_init(multiboot_info_t *mboot_info)
        // this returns when all other cores are done and ready to receive IPIs
        smp_boot();
        
-       nic_init();             
+       rl8168_init();          
+       ne2k_init();
        /*
 
        // this returns when all other cores are done and ready to receive IPIs
index fce3005..391e3e9 100644 (file)
@@ -355,6 +355,7 @@ void int_parse(int_entry* entries, uint32_t count) {
                mptables_dump("Interrupt entry %u\n", i);
                mptables_dump("-->type: %x\n", entries[i].type);
                mptables_dump("-->intType: %x\n", entries[i].intType);
+               mptables_dump("-->intFlags: %x\n", entries[i].intFlags);
                mptables_dump("-->srcBusID: %u\n", entries[i].srcBusID);
                mptables_dump("-->srcDevice: %u (PCI ONLY)\n", (entries[i].srcBusIRQ >> 2) & 0x1F);
                mptables_dump("-->srcBusIRQ: %x\n", entries[i].srcBusIRQ);
@@ -699,4 +700,4 @@ void lint_parse(int_entry* entries, uint32_t count) {
 // //  panic("WERE ALL GONNA DIE!");
 // 
 //     return;
-// }
\ No newline at end of file
+// }
diff --git a/kern/src/ne2k.c b/kern/src/ne2k.c
new file mode 100644 (file)
index 0000000..6d334fc
--- /dev/null
@@ -0,0 +1,163 @@
+#ifdef __DEPUTY__
+#pragma nodeputy
+#endif
+
+#include <arch/mmu.h>
+#include <arch/x86.h>
+#include <arch/smp.h>
+#include <arch/apic.h>
+
+#include <ros/memlayout.h>
+
+#include <atomic.h>
+#include <stdio.h>
+#include <string.h>
+#include <trap.h>
+#include <kmalloc.h>
+
+#include <pmap.h>
+#include <pci.h>
+#include <ne2k.h>
+
+/* NE2000 NIC Driver Sketch
+ *
+ * Written by Paul Pearce.
+ *
+ */
+
+extern uint32_t eth_up; // Fix this                               
+uint32_t ne2k_irq;      // And this
+uint32_t ne2k_io_base_addr;
+
+
+void ne2k_init() {
+       
+       if (ne2k_scan_pci() < 0) return;
+       ne2k_setup_interrupts();
+       ne2k_configure_nic();
+       eth_up = 1;
+       
+       return;
+}
+
+
+int ne2k_scan_pci() {
+       
+       extern pci_dev_entry pci_dev_map[PCI_MAX_BUS][PCI_MAX_DEV][PCI_MAX_FUNC];
+       extern uint16_t pci_irq_map[PCI_MAX_BUS][PCI_MAX_DEV][PCI_MAX_FUNC];
+
+       cprintf("Searching for NE2000 Network device......");
+
+       for (int i = 0; i < PCI_MAX_BUS; i++)
+               for (int j = 0; j < PCI_MAX_DEV; j++)
+                       for (int k = 0; k < PCI_MAX_FUNC; k++) {
+                               uint32_t address;
+                               uint32_t lbus = i;
+                               uint32_t ldev = j;
+                               uint32_t lfunc = k;
+                               uint32_t lreg = 0; 
+                               uint32_t result  = 0;
+       
+                               uint16_t dev_id = pci_dev_map[i][j][k].dev_id;
+                               uint16_t ven_id = pci_dev_map[i][j][k].ven_id;
+
+                               // Vender DNE
+                               if (ven_id == INVALID_VENDOR_ID) 
+                                       continue;
+
+                               // Ignore non RealTek 8168 Devices
+                               if (ven_id != NE2K_VENDOR_ID || dev_id != NE2K_DEV_ID)
+                                       continue;
+                               cprintf(" found on BUS %x DEV %x\n", i, j);
+
+                               // Find the IRQ
+                               ne2k_irq = pci_irq_map[i][j][k];
+                               ne2k_debug("-->IRQ: %u\n", ne2k_irq);
+
+                               // Loop over the BARs
+                               for (int k = 0; k <= 5; k++) {
+                                       lreg = 4 + k;
+                                       address = MK_CONFIG_ADDR(lbus, ldev, lfunc, lreg << 2); 
+                               outl(PCI_CONFIG_ADDR, address);
+                               result = inl(PCI_CONFIG_DATA);
+                                       
+                                       if (result == 0) // (0 denotes no valid data)
+                                               continue;
+
+                                       // Read the bottom bit of the BAR. 
+                                       if (result & PCI_BAR_IO_MASK) {
+                                               result = result & PCI_IO_MASK;
+                                               ne2k_debug("-->BAR%u: %s --> %x\n", k, "IO", result);
+                                       } else {
+                                               result = result & PCI_MEM_MASK;
+                                               ne2k_debug("-->BAR%u: %s --> %x\n", k, "MEM", result);
+                                       }
+                       
+                                       // TODO Switch to memory mapped instead of IO?
+                                       if (k == 0) // BAR0 denotes the IO Addr for the device
+                                               ne2k_io_base_addr = result;                                             
+                               }
+                               
+               return 0;
+       }
+       cprintf(" not found. No device configured.\n");
+       
+       return -1;
+}
+
+void ne2k_configure_nic() {
+       
+       ne2k_debug("-->Configuring Device.\n");
+
+        // Reset
+       inb(ne2k_io_base_addr + 0x1f);
+
+       // Configure
+        outb(ne2k_io_base_addr + 0x00, 0x22);
+        outb(ne2k_io_base_addr + 0x07, 0xFF);
+       outb(ne2k_io_base_addr + 0x0F, 0xFF);
+
+        uint8_t isr = inb(ne2k_io_base_addr + 0x07);
+        cprintf("isr: %x\n", isr);
+
+
+       cprintf("Generating Interrupt...\n");
+       outb(ne2k_io_base_addr + 0x0A, 0x00);
+       outb(ne2k_io_base_addr + 0x0B, 0x00);
+       outb(ne2k_io_base_addr + 0x00, 0x0A);
+       udelay(10000000);
+
+        cprintf("Generating Interrupt again...\n");
+        outb(ne2k_io_base_addr + 0x0A, 0x00);
+        outb(ne2k_io_base_addr + 0x0B, 0x00);
+        outb(ne2k_io_base_addr + 0x00, 0x0A);
+        udelay(10000000);
+       
+       return;
+}
+
+void ne2k_setup_interrupts() {
+       
+       extern handler_t interrupt_handlers[];
+       
+       ne2k_debug("-->Setting interrupts.\n");
+       
+       // Kernel based interrupt stuff
+       register_interrupt_handler(interrupt_handlers, KERNEL_IRQ_OFFSET + ne2k_irq, ne2k_interrupt_handler, 0);
+       
+       ioapic_route_irq(ne2k_irq, 6);  
+       
+       return;
+}
+
+// We need to evaluate this routine in terms of concurrency.
+// We also need to figure out whats up with different core interrupts
+void ne2k_interrupt_handler(trapframe_t *tf, void* data) {
+       
+       cprintf("\nNE2K interrupt on core %u!\n", lapic_get_id());
+       uint8_t isr= inb(ne2k_io_base_addr + 0x07);
+       cprintf("isr: %x\n", isr);
+       outb(ne2k_io_base_addr + 0x07, isr);
+
+       return;                         
+}
index 84f49fb..a302436 100644 (file)
@@ -44,8 +44,6 @@
  * TODO: CONCURRENCY!
  */
 
-void test_pit_handler(trapframe_t *tf, void* data);
-
 struct Descriptor
 {
     unsigned int command,  /* command/status dword */
@@ -55,8 +53,8 @@ struct Descriptor
 };
 
 
-uint32_t io_base_addr = 0;
-uint32_t irq = 0;
+uint32_t rl8168_io_base_addr = 0;
+uint32_t rl8168_irq = 0;
 char device_mac[6];
 
 struct Descriptor *rx_des_kva;
@@ -68,7 +66,7 @@ struct Descriptor *tx_des_pa;
 uint32_t rx_des_cur = 0;
 uint32_t tx_des_cur = 0;
 
-uint8_t eth_up = 0;
+uint8_t eth_up = 0; // TODO: This needs to be somewhere global.
 
 // Hacky stuff for syscall hack. Go away.
 int packet_waiting;
@@ -78,18 +76,18 @@ char* packet_buffer_orig;
 int packet_buffer_pos = 0;
 // End hacky stuff
 
-void nic_init() {
+void rl8168_init() {
        
-       if (scan_pci() < 0) return;
-       read_mac();
-       setup_descriptors();
-       configure_nic();
-       setup_interrupts();
+       if (rl8168_scan_pci() < 0) return;
+       rl8168_read_mac();
+       rl8168_setup_descriptors();
+       rl8168_configure();
+       rl8168_setup_interrupts();
        eth_up = 1;
        
        //Trigger sw based nic interrupt
 /*     cprintf("Generating interrupt...\n");
-       outb(io_base_addr + 0x38, 0x1);
+       outb(rl8168_io_base_addr + 0x38, 0x1);
        cprintf("sleeping\n");
        udelay(3000000);
        cprintf("done\n");
@@ -98,7 +96,7 @@ void nic_init() {
 }
 
 
-int scan_pci() {
+int rl8168_scan_pci() {
        
        extern pci_dev_entry pci_dev_map[PCI_MAX_BUS][PCI_MAX_DEV][PCI_MAX_FUNC];
        extern uint16_t pci_irq_map[PCI_MAX_BUS][PCI_MAX_DEV][PCI_MAX_FUNC];
@@ -128,8 +126,8 @@ int scan_pci() {
                                cprintf(" found on BUS %x DEV %x\n", i, j);
 
                                // Find the IRQ
-                               irq = pci_irq_map[i][j][k];
-                               nic_debug("-->IRQ: %u\n", irq);
+                               rl8168_irq = pci_irq_map[i][j][k];
+                               rl8168_debug("-->IRQ: %u\n", rl8168_irq);
 
                                // Loop over the BARs
                                for (int k = 0; k <= 5; k++) {
@@ -144,18 +142,18 @@ int scan_pci() {
                                        // Read the bottom bit of the BAR. 
                                        if (result & PCI_BAR_IO_MASK) {
                                                result = result & PCI_IO_MASK;
-                                               nic_debug("-->BAR%u: %s --> %x\n", k, "IO", result);
+                                               rl8168_debug("-->BAR%u: %s --> %x\n", k, "IO", result);
                                        } else {
                                                result = result & PCI_MEM_MASK;
-                                               nic_debug("-->BAR%u: %s --> %x\n", k, "MEM", result);
+                                               rl8168_debug("-->BAR%u: %s --> %x\n", k, "MEM", result);
                                        }
                        
                                        // TODO Switch to memory mapped instead of IO?
                                        if (k == 0) // BAR0 denotes the IO Addr for the device
-                                               io_base_addr = result;                                          
+                                               rl8168_io_base_addr = result;                                           
                                }
                
-               nic_debug("-->hwrev: %x\n", inl(io_base_addr + RL_HWREV_REG) & RL_HWREV_MASK);
+               rl8168_debug("-->hwrev: %x\n", inl(rl8168_io_base_addr + RL_HWREV_REG) & RL_HWREV_MASK);
                
                return 0;
        }
@@ -164,54 +162,20 @@ int scan_pci() {
        return -1;
 }
 
-void read_mac() {
+void rl8168_read_mac() {
        
        for (int i = 0; i < 6; i++)
-          device_mac[i] = inb(io_base_addr + RL_MAC_OFFSET + i); 
+          device_mac[i] = inb(rl8168_io_base_addr + RL_MAC_OFFSET + i); 
        
-       nic_debug("-->DEVICE MAC: %02x:%02x:%02x:%02x:%02x:%02x\n", 0xFF & device_mac[0], 0xFF & device_mac[1], 
+       rl8168_debug("-->DEVICE MAC: %02x:%02x:%02x:%02x:%02x:%02x\n", 0xFF & device_mac[0], 0xFF & device_mac[1],      
                                                                    0xFF & device_mac[2], 0xFF & device_mac[3], 
                                                                 0xFF & device_mac[4], 0xFF & device_mac[5]);
        return;
 }
 
-/*
-void setup_descriptors() {
-       
-       nic_debug("-->Setting up tx/rx descriptors.\n");
-       
-       page_t *rx_des_page = NULL, *tx_des_page = NULL;
-                       
-       if (page_alloc(&rx_des_page) < 0) panic("Can't allocate page for RX Ring");
-       
-       if (page_alloc(&tx_des_page) < 0) panic("Can't allocate page for TX Ring");
-       
-       if (page2pa(tx_des_page) == 0x1000)
-               if (page_alloc(&tx_des_page) < 0) panic("Can't allocate page for TX Ring");
-       
-       // extra page_alloc needed because of the strange page_alloc thing
-       if (page_alloc(&tx_des_page) < 0) panic("Can't allocate page for TX Ring");
-       
-       rx_des_kva = page2kva(rx_des_page);
-       tx_des_kva = page2kva(tx_des_page);
-       
-       rx_des_pa = page2pa(rx_des_page);
-       tx_des_pa = page2pa(tx_des_page);
-
-       cprintf("rx_des_page: %x\n", rx_des_pa);
-       cprintf("tx_des_page: %x\n", tx_des_pa);
-       
-    for (int i = 0; i < num_of_rx_descriptors; i++) 
-               set_rx_descriptor(i);
-               
-       for (int i = 0; i < num_of_tx_descriptors; i++) 
-               set_tx_descriptor(i);
-}
-*/
-
-void setup_descriptors() {
+void rl8168_setup_descriptors() {
        
-       nic_debug("-->Setting up tx/rx descriptors.\n");
+       rl8168_debug("-->Setting up tx/rx descriptors.\n");
                        
        // Allocate room for the buffers. Include an extra ALIGN space.
        // Buffers need to be on 256 byte boundries.
@@ -227,16 +191,16 @@ void setup_descriptors() {
        tx_des_pa = (struct Descriptor *)PADDR(tx_des_kva);
        
     for (int i = 0; i < NUM_RX_DESCRIPTORS; i++) 
-               set_rx_descriptor(i, TRUE); // Allocate memory for the descriptor
+               rl8168_set_rx_descriptor(i, TRUE); // Allocate memory for the descriptor
                
        for (int i = 0; i < NUM_TX_DESCRIPTORS; i++) 
-               set_tx_descriptor(i);
+               rl8168_set_tx_descriptor(i);
                
        return;
 }
 
 
-void set_rx_descriptor(uint32_t des_num, uint8_t reset_buffer) {
+void rl8168_set_rx_descriptor(uint32_t des_num, uint8_t reset_buffer) {
        
        // Set the OWN bit on all descriptors. Also set the buffer size.
        rx_des_kva[des_num].command = (DES_OWN_MASK | (RL_RX_MAX_BUFFER_SIZE & DES_RX_SIZE_MASK));
@@ -257,7 +221,7 @@ void set_rx_descriptor(uint32_t des_num, uint8_t reset_buffer) {
        return;
 }
 
-void set_tx_descriptor(uint32_t des_num) {
+void rl8168_set_tx_descriptor(uint32_t des_num) {
        
        // Clear the command bits.
        tx_des_kva[des_num].command = 0;
@@ -276,170 +240,170 @@ void set_tx_descriptor(uint32_t des_num) {
        return;
 }
 
-void configure_nic() {
+void rl8168_configure() {
        
        // TODO: Weigh resetting the nic. Not really needed. Remove?
        // TODO Check ordering of what we set.
        // TODO Remove C+ register setting?
        
-       nic_debug("-->Configuring Device.\n");
-       reset_nic();
+       rl8168_debug("-->Configuring Device.\n");
+       rl8168_reset();
 
        // Magic to handle the C+ register. Completely undocumented, ripped from the BSE RE driver.
-       outl(io_base_addr + RL_CP_CTRL_REG, RL_CP_MAGIC_MASK);
+       outl(rl8168_io_base_addr + RL_CP_CTRL_REG, RL_CP_MAGIC_MASK);
 
        // Unlock EPPROM CTRL REG
-       outb(io_base_addr + RL_EP_CTRL_REG, RL_EP_CTRL_UL_MASK);        
+       outb(rl8168_io_base_addr + RL_EP_CTRL_REG, RL_EP_CTRL_UL_MASK);         
        
        // Set max RX Packet Size
-    outw(io_base_addr + RL_RX_MXPKT_REG, RL_RX_MAX_SIZE);      
+    outw(rl8168_io_base_addr + RL_RX_MXPKT_REG, RL_RX_MAX_SIZE);       
                
        // Set max TX Packet Size
-    outb(io_base_addr + RL_TX_MXPKT_REG, RL_TX_MAX_SIZE);                      
+    outb(rl8168_io_base_addr + RL_TX_MXPKT_REG, RL_TX_MAX_SIZE);                       
 
        // Set TX Des Ring Start Addr
-    outl(io_base_addr + RL_TX_DES_REG, (unsigned long)tx_des_pa); 
+    outl(rl8168_io_base_addr + RL_TX_DES_REG, (unsigned long)tx_des_pa); 
        
        // Set RX Des Ring Start Addr
-    outl(io_base_addr + RL_RX_DES_REG, (unsigned long)rx_des_pa);      
+    outl(rl8168_io_base_addr + RL_RX_DES_REG, (unsigned long)rx_des_pa);       
 
        // Configure TX
-       outl(io_base_addr + RL_TX_CFG_REG, RL_TX_CFG_MASK); 
+       outl(rl8168_io_base_addr + RL_TX_CFG_REG, RL_TX_CFG_MASK); 
        
        // Configure RX
-       outl(io_base_addr + RL_TX_CFG_REG, RL_RX_CFG_MASK);                     
+       outl(rl8168_io_base_addr + RL_TX_CFG_REG, RL_RX_CFG_MASK);                      
 
        // Enable RX and TX in the CTRL Reg
-       outb(io_base_addr + RL_CTRL_REG, RL_CTRL_RXTX_MASK);                    
+       outb(rl8168_io_base_addr + RL_CTRL_REG, RL_CTRL_RXTX_MASK);                     
 
        // Lock the EPPROM Ctrl REG
-    outl(io_base_addr + RL_EP_CTRL_REG, RL_EP_CTRL_L_MASK);            
+    outl(rl8168_io_base_addr + RL_EP_CTRL_REG, RL_EP_CTRL_L_MASK);             
        
        return;
 }
 
-void reset_nic() {
+void rl8168_reset() {
        
-       nic_debug("-->Resetting device..... ");
-       outb(io_base_addr + RL_CTRL_REG, RL_CTRL_RESET_MASK);
+       rl8168_debug("-->Resetting device..... ");
+       outb(rl8168_io_base_addr + RL_CTRL_REG, RL_CTRL_RESET_MASK);
        
        // Wait for NIC to answer "done resetting" before continuing on
-       while (inb(io_base_addr + RL_CTRL_REG) & RL_CTRL_RESET_MASK);
-       nic_debug(" done.\n");
+       while (inb(rl8168_io_base_addr + RL_CTRL_REG) & RL_CTRL_RESET_MASK);
+       rl8168_debug(" done.\n");
        
        return;
 }
 
-void setup_interrupts() {
+void rl8168_setup_interrupts() {
        
        extern handler_t interrupt_handlers[];
        
-       nic_debug("-->Setting interrupts.\n");
+       rl8168_debug("-->Setting interrupts.\n");
        
        // Enable NIC interrupts
-       outw(io_base_addr + RL_IM_REG, RL_INTERRUPT_MASK);
+       outw(rl8168_io_base_addr + RL_IM_REG, RL_INTERRUPT_MASK);
        
        //Clear the current interrupts.
-       outw(io_base_addr + RL_IS_REG, RL_INTRRUPT_CLEAR);
+       outw(rl8168_io_base_addr + RL_IS_REG, RL_INTRRUPT_CLEAR);
        
        // Kernel based interrupt stuff
-       register_interrupt_handler(interrupt_handlers, KERNEL_IRQ_OFFSET + irq, nic_interrupt_handler, 0);
-       ioapic_route_irq(irq, NIC_IRQ_CPU);     
+       register_interrupt_handler(interrupt_handlers, KERNEL_IRQ_OFFSET + rl8168_irq, rl8168_interrupt_handler, 0);
+       ioapic_route_irq(rl8168_irq, NE2K_IRQ_CPU);     
        
        return;
 }
 
 // We need to evaluate this routine in terms of concurrency.
 // We also need to figure out whats up with different core interrupts
-void nic_interrupt_handler(trapframe_t *tf, void* data) {
+void rl8168_interrupt_handler(trapframe_t *tf, void* data) {
        
-       nic_interrupt_debug("\nNic interrupt on core %u!\n", lapic_get_id());
+       rl8168_interrupt_debug("\nNic interrupt on core %u!\n", lapic_get_id());
                                
        // Read the offending interrupt(s)
-       uint16_t interrupt_status = inw(io_base_addr + RL_IS_REG);
+       uint16_t interrupt_status = inw(rl8168_io_base_addr + RL_IS_REG);
 
        // Clear interrupts immediately so we can get the flag raised again.
-       outw(io_base_addr + RL_IS_REG, interrupt_status);
+       outw(rl8168_io_base_addr + RL_IS_REG, interrupt_status);
        
        // Loop to deal with TOCTOU 
        while (interrupt_status != 0x0000) {
                // We can have multiple interrupts fire at once. I've personally seen this.
                // This means we need to handle this as a series of independent if's
                if (interrupt_status & RL_INT_ROK) {
-                       nic_interrupt_debug("-->RX OK\n");
-                       nic_handle_rx_packet();
+                       rl8168_interrupt_debug("-->RX OK\n");
+                       rl8168_handle_rx_packet();
                }       
        
                if (interrupt_status & RL_INT_RERR) {
-                       nic_interrupt_debug("-->RX ERR\n");                     
+                       rl8168_interrupt_debug("-->RX ERR\n");                  
                }
        
                if (interrupt_status & RL_INT_TOK) {
-                       nic_interrupt_debug("-->TX OK\n");
+                       rl8168_interrupt_debug("-->TX OK\n");
                }
        
                if (interrupt_status & RL_INT_TERR) {
-                       nic_interrupt_debug("-->TX ERR\n");                     
+                       rl8168_interrupt_debug("-->TX ERR\n");                  
                }
        
                if (interrupt_status & RL_INT_RDU) {
-                       nic_interrupt_debug("-->RX Descriptor Unavailable\n");                  
+                       rl8168_interrupt_debug("-->RX Descriptor Unavailable\n");                       
                }
        
                if (interrupt_status & RL_INT_LINKCHG) {
-                       nic_interrupt_debug("-->Link Status Changed\n");                        
+                       rl8168_interrupt_debug("-->Link Status Changed\n");                     
                }
        
                if (interrupt_status & RL_INT_FOVW) {
-                       nic_interrupt_debug("-->RX Fifo Overflow\n");                   
+                       rl8168_interrupt_debug("-->RX Fifo Overflow\n");                        
                }
        
                if (interrupt_status & RL_INT_TDU) {
-                       nic_interrupt_debug("-->TX Descriptor Unavailable\n");                  
+                       rl8168_interrupt_debug("-->TX Descriptor Unavailable\n");                       
                }
        
                if (interrupt_status & RL_INT_SWINT) {
-                       nic_interrupt_debug("-->Software Generated Interrupt\n");
+                       rl8168_interrupt_debug("-->Software Generated Interrupt\n");
                }
        
                if (interrupt_status & RL_INT_TIMEOUT) {
-                       nic_interrupt_debug("-->Timer Expired\n");
+                       rl8168_interrupt_debug("-->Timer Expired\n");
                }
        
                if (interrupt_status & RL_INT_SERR) {
-                       nic_interrupt_debug("-->PCI Bus System Error\n");                       
+                       rl8168_interrupt_debug("-->PCI Bus System Error\n");                    
                }
        
-               nic_interrupt_debug("\n");
+               rl8168_interrupt_debug("\n");
                
                // Clear interrupts     
-               interrupt_status = inw(io_base_addr + RL_IS_REG);
-               outw(io_base_addr + RL_IS_REG, interrupt_status);
+               interrupt_status = inw(rl8168_io_base_addr + RL_IS_REG);
+               outw(rl8168_io_base_addr + RL_IS_REG, interrupt_status);
        }
        
        // In the event that we got really unlucky and more data arrived after we set 
        //  set the bit last, try one more check
-       nic_handle_rx_packet();
+       rl8168_handle_rx_packet();
        return;
 }
 
 // TODO: Does a packet too large get dropped or just set the error bits in the descriptor? Find out.
 // TODO: Should we move on to look for the next descriptor? is it safe? TOCTOU
-void nic_handle_rx_packet() {
+void rl8168_handle_rx_packet() {
        
        uint32_t current_command = rx_des_kva[rx_des_cur].command;
        uint16_t packet_size;
        
        if (current_command & DES_OWN_MASK) {
-               nic_frame_debug("-->Nothing to process. Returning.");
+               rl8168_frame_debug("-->Nothing to process. Returning.");
                return;
        }
                
-       nic_frame_debug("-->RX Des: %u\n", rx_des_cur);
+       rl8168_frame_debug("-->RX Des: %u\n", rx_des_cur);
        
        // Make sure we are processing from the start of a packet segment
        if (!(current_command & DES_FS_MASK)) {
-               nic_frame_debug("-->ERR: Current RX descriptor not marked with FS mask. Panic!");
+               rl8168_frame_debug("-->ERR: Current RX descriptor not marked with FS mask. Panic!");
                panic("RX Descriptor Ring FS out of sync");
        }
        
@@ -468,7 +432,7 @@ void nic_handle_rx_packet() {
                        //      set_rx_descriptor(i, FALSE); // Dont reallocate memory for the descriptor
                        // rx_des_cur = 0;
                        // return;
-                       nic_frame_debug("-->ERR: No ending segment found in RX buffer.\n");
+                       rl8168_frame_debug("-->ERR: No ending segment found in RX buffer.\n");
                        panic("RX Descriptor Ring out of sync.");
                }
                
@@ -477,13 +441,13 @@ void nic_handle_rx_packet() {
                
                // Make sure we own the current packet. Kernel ownership is denoted by a 0. Nic by a 1.
                if (current_command & DES_OWN_MASK) {
-                       nic_frame_debug("-->ERR: Current RX descriptor not owned by kernel. Panic!");
+                       rl8168_frame_debug("-->ERR: Current RX descriptor not owned by kernel. Panic!");
                        panic("RX Descriptor Ring OWN out of sync");
                }
                
                // Make sure if we are at the end of the buffer, the des is marked as end
                if ((rx_des_loop_cur == (NUM_RX_DESCRIPTORS - 1)) && !(current_command & DES_EOR_MASK)) {
-                       nic_frame_debug("-->ERR: Last RX descriptor not marked with EOR mask. Panic!\n");
+                       rl8168_frame_debug("-->ERR: Last RX descriptor not marked with EOR mask. Panic!\n");
                        panic("RX Descriptor Ring EOR Missing");
                }
                
@@ -494,7 +458,7 @@ void nic_handle_rx_packet() {
                        //      set_rx_descriptor(i, FALSE); // Dont reallocate memory for the descriptor
                        // rx_des_cur = 0;
                        // return;
-                       nic_frame_debug("-->ERR: Nic sent %u byte packet. Max is %u\n", frame_size, MAX_FRAME_SIZE);
+                       rl8168_frame_debug("-->ERR: Nic sent %u byte packet. Max is %u\n", frame_size, MAX_FRAME_SIZE);
                        panic("NIC Sent packets larger than configured.");
                }
                
@@ -502,7 +466,7 @@ void nic_handle_rx_packet() {
                memcpy(rx_buffer + frame_size, KADDR(rx_des_kva[rx_des_loop_cur].low_buf), fragment_size);
                
                // Reset the descriptor. No reuse buffer.
-               set_rx_descriptor(rx_des_loop_cur, FALSE);
+               rl8168_set_rx_descriptor(rx_des_loop_cur, FALSE);
                
                // Note: We mask out fragment sizes at 0x3FFFF. There can be at most 1024 of them.
                // This can not overflow the uint32_t we allocated for frame size, so
@@ -539,7 +503,7 @@ void nic_handle_rx_packet() {
                
                packet_waiting = 1;
                
-               process_frame(rx_buffer, frame_size, current_command);
+               rl8168_process_frame(rx_buffer, frame_size, current_command);
                
                rx_des_cur = rx_des_loop_cur;
                
@@ -549,7 +513,7 @@ void nic_handle_rx_packet() {
        // END HACKY STUFF
        
        // Chew on the frame data. Command bits should be the same for all frags.
-       process_frame(rx_buffer, frame_size, current_command);
+       rl8168_process_frame(rx_buffer, frame_size, current_command);
 
        rx_des_cur = rx_des_loop_cur;
        
@@ -559,10 +523,10 @@ void nic_handle_rx_packet() {
 }
 
 // This is really more of a debug level function. Will probably go away once we get a stack going.
-void process_frame(char *frame_buffer, uint32_t frame_size, uint32_t command) {
+void rl8168_process_frame(char *frame_buffer, uint32_t frame_size, uint32_t command) {
                
-       nic_frame_debug("-->Command: %x\n", command);
-       nic_frame_debug("-->Size: %u\n", frame_size);
+       rl8168_frame_debug("-->Command: %x\n", command);
+       rl8168_frame_debug("-->Size: %u\n", frame_size);
        
        if (frame_buffer == NULL)
                return;
@@ -570,7 +534,7 @@ void process_frame(char *frame_buffer, uint32_t frame_size, uint32_t command) {
        // This is hacky. Once we know what our stack will look like, change this.
        // Once remove check for 0 size.
        if (frame_size < MINIMUM_PACKET_SIZE) {
-               nic_frame_debug("-->Packet too small. Discarding.\n");
+               rl8168_frame_debug("-->Packet too small. Discarding.\n");
                return;
        }
        
@@ -590,28 +554,28 @@ void process_frame(char *frame_buffer, uint32_t frame_size, uint32_t command) {
        eth_type[1] = frame_buffer[13];
        
        if (command & DES_MAR_MASK) {
-               nic_frame_debug("-->Multicast Packet.\n");
+               rl8168_frame_debug("-->Multicast Packet.\n");
        }
        
        if (command & DES_PAM_MASK) {
-               nic_frame_debug("-->Physical Address Matched.\n");
+               rl8168_frame_debug("-->Physical Address Matched.\n");
        }
        
        if (command & DES_BAR_MASK) {
-               nic_frame_debug("-->Broadcast Packet.\n");
+               rl8168_frame_debug("-->Broadcast Packet.\n");
        }
        
        // Note: DEST comes before SRC in the ethernet frame, but that 
        
-       nic_frame_debug("-->DEST   MAC: %02x:%02x:%02x:%02x:%02x:%02x\n", 0xFF & dest_mac[0], 0xFF & dest_mac[1],       
-                                                                          0xFF & dest_mac[2], 0xFF & dest_mac[3],      
-                                                                       0xFF & dest_mac[4], 0xFF & dest_mac[5]);
+       rl8168_frame_debug("-->DEST   MAC: %02x:%02x:%02x:%02x:%02x:%02x\n", 0xFF & dest_mac[0], 0xFF & dest_mac[1],    
+                                                                            0xFF & dest_mac[2], 0xFF & dest_mac[3],    
+                                                                             0xFF & dest_mac[4], 0xFF & dest_mac[5]);
        
-       nic_frame_debug("-->SOURCE MAC: %02x:%02x:%02x:%02x:%02x:%02x\n", 0xFF & source_mac[0], 0xFF & source_mac[1],   
-                                                                          0xFF & source_mac[2], 0xFF & source_mac[3],  
-                                                                       0xFF & source_mac[4], 0xFF & source_mac[5]);
+       rl8168_frame_debug("-->SOURCE MAC: %02x:%02x:%02x:%02x:%02x:%02x\n", 0xFF & source_mac[0], 0xFF & source_mac[1],        
+                                                                            0xFF & source_mac[2], 0xFF & source_mac[3],        
+                                                                             0xFF & source_mac[4], 0xFF & source_mac[5]);
 
-       nic_frame_debug("-->ETHR MODE: %02x%02x\n", 0xFF & eth_type[0], 0xFF & eth_type[1]);
+       rl8168_frame_debug("-->ETHR MODE: %02x%02x\n", 0xFF & eth_type[0], 0xFF & eth_type[1]);
                
        return;
 }
@@ -621,7 +585,7 @@ void process_frame(char *frame_buffer, uint32_t frame_size, uint32_t command) {
 // Would we want to write a function that takes a larger packet and generates fragments?
 // This seems like the stacks responsibility. Leave this for now. may in future
 // Remove the max size cap and generate multiple packets.
-int send_frame(const char *data, size_t len) {
+int rl8168_send_frame(const char *data, size_t len) {
 
        if (data == NULL)
                return -1;
@@ -629,12 +593,12 @@ int send_frame(const char *data, size_t len) {
                return 0;
 
        if (tx_des_kva[tx_des_cur].command & DES_OWN_MASK) {
-               nic_frame_debug("-->TX Ring Buffer Full!\n");
+               rl8168_frame_debug("-->TX Ring Buffer Full!\n");
                return -1;
        }
        
        if (len > MAX_FRAME_SIZE) {
-               nic_frame_debug("-->Frame Too Large!\n");
+               rl8168_frame_debug("-->Frame Too Large!\n");
                return -1;
        }
        
@@ -650,16 +614,16 @@ int send_frame(const char *data, size_t len) {
        
        tx_des_cur = (tx_des_cur + 1) % NUM_TX_DESCRIPTORS;
        
-       //nic_frame_debug("-->Sent packet.\n");
+       //rl8168_frame_debug("-->Sent packet.\n");
        
-       outb(io_base_addr + RL_TX_CTRL_REG, RL_TX_SEND_MASK);
+       outb(rl8168_io_base_addr + RL_TX_CTRL_REG, RL_TX_SEND_MASK);
        
        return len;
 }
 
 // This function is a complete hack for syscalls until we get a stack.
 // the day I delete this monstrosity of code I will be a happy man --Paul
-const char *packet_wrap(const char* data, size_t len) {
+const char *rl8168_packet_wrap(const char* data, size_t len) {
        
        #define htons(A) ((((uint16_t)(A) & 0xff00) >> 8) | \
                            (((uint16_t)(A) & 0x00ff) << 8))
@@ -709,14 +673,14 @@ const char *packet_wrap(const char* data, size_t len) {
  
        
        if (len > MAX_PACKET_DATA) {
-               nic_frame_debug("Bad packet size for packet wrapping");
+               rl8168_frame_debug("Bad packet size for packet wrapping");
                return NULL;
        }
        
        char* wrap_buffer = kmalloc(MAX_PACKET_SIZE, 0);
        
        if (wrap_buffer == NULL) {
-               nic_frame_debug("Can't allocate page for packet wrapping");
+               rl8168_frame_debug("Can't allocate page for packet wrapping");
                return NULL;
        }