Basic NIC enumeration and wrapping

This commit is contained in:
Ali Mashtizadeh 2014-12-31 19:31:25 -08:00
parent 907baa75a7
commit 9b4e8af58c
5 changed files with 115 additions and 1 deletions

View File

@ -45,6 +45,7 @@ src_common = [
"kern/ktime.c",
"kern/libc.c",
"kern/loader.c",
"kern/nic.c",
"kern/palloc.c",
"kern/printf.c",
"kern/sga.c",

View File

@ -8,7 +8,8 @@
#include <sys/kmem.h>
#include <sys/pci.h>
#include <sys/irq.h>
#include <sys/sga.h>
#include <sys/mbuf.h>
#include <sys/nic.h>
#include <machine/pmap.h>
@ -112,6 +113,7 @@ typedef struct PACKED E1000TXDesc {
typedef struct E1000Dev {
PCIDevice dev;
NIC nic;
IRQHandler irqHandle;
uint8_t *mmiobase;
E1000RXDesc *rxDesc;
@ -398,5 +400,9 @@ E1000_Configure(PCIDevice dev)
E1000_RXInit(ethDev);
E1000_TXInit(ethDev);
ethDev->nic.handle = ethDev;
// XXX: Fill in callbacks
NIC_AddNIC(&ethDev->nic);
}

24
sys/include/mbuf.h Normal file
View File

@ -0,0 +1,24 @@
#ifndef __SYS_MBUF_H__
#define __SYS_MBUF_H__
#define MBUF_TYPE_NULL 0
#define MBUF_TYPE_PACKETSTART 1
#define MBUF_TYPE_PACKETCONT 2
#define MBUF_TYPE_PACKETEND 3
#define MBUF_STATUS_NULL 0
#define MBUF_STATUS_READY 1
#define MBUF_STATUS_FAILED 2
typedef struct MBuf {
uint64_t vaddr;
uint64_t maddr;
uint32_t len;
uint32_t type;
uint32_t flags;
uint32_t status;
} MBuf;
#endif /* __SYS_MBUF_H__ */

30
sys/include/nic.h Normal file
View File

@ -0,0 +1,30 @@
#ifndef __SYS_NIC_H__
#define __SYS_NIC_H__
#include <sys/queue.h>
#include <sys/mbuf.h>
typedef void (*NICCB)(int, void *);
typedef struct NIC NIC;
typedef struct NIC {
void *handle; // Driver handle
uint64_t nicNo; // NIC number
uint8_t mac[6];
int (*tx)(NIC *, MBuf *, NICCB, void *); // TX
int (*rx)(NIC *, MBuf *, NICCB, void *); // RX
int (*poll)();
LIST_ENTRY(NIC) entries;
} NIC;
void NIC_AddNIC(NIC *nic);
void NIC_RemoveNIC(NIC *nic);
NIC *NIC_GetByID(uint64_t nicNo);
int NIC_GetMAC(NIC *nic, void *mac);
int NIC_TX(NIC *nic, MBuf *mbuf, NICCB cb, void *arg);
int NIC_RX(NIC *nic, MBuf *mbuf, NICCB cb, void *arg);
int NIC_Poll();
#endif /* __SYS_NIC_H__ */

53
sys/kern/nic.c Normal file
View File

@ -0,0 +1,53 @@
#include <stdbool.h>
#include <stdint.h>
#include <sys/kassert.h>
#include <sys/kdebug.h>
#include <sys/queue.h>
#include <sys/mbuf.h>
#include <sys/nic.h>
#include <sys/spinlock.h>
LIST_HEAD(NICList, NIC) nicList = LIST_HEAD_INITIALIZER(nicList);
uint64_t nextNICNo = 0;
void
NIC_AddNIC(NIC *nic)
{
nic->nicNo = nextNICNo++;
LIST_INSERT_HEAD(&nicList, nic, entries);
}
void
NIC_RemoveNIC(NIC *nic)
{
LIST_REMOVE(nic, entries);
}
NIC *
NIC_GetByID(uint64_t nicNo)
{
NIC *n;
LIST_FOREACH(n, &nicList, entries) {
if (n->nicNo == nicNo)
return n;
}
return NULL;
}
void
Debug_NICs(int argc, const char *argv[])
{
NIC *n;
LIST_FOREACH(n, &nicList, entries) {
kprintf("nic%lld: %02x:%02x:%02x:%02x:%02x:%02x\n", n->nicNo,
n->mac[0], n->mac[1], n->mac[2], n->mac[3], n->mac[4], n->mac[5]);
}
}
REGISTER_DBGCMD(nics, "List NICs", Debug_NICs);