2 * This file is part of the Palacios Virtual Machine Monitor developed
3 * by the V3VEE Project with funding from the United States National
4 * Science Foundation and the Department of Energy.
6 * The V3VEE Project is a joint project between Northwestern University
7 * and the University of New Mexico. You can find out more at
10 * Copyright (c) 2009, Lei Xia <lxia@northwestern.edu>
11 * Copyright (c) 2009, The V3VEE Project <http://www.v3vee.org>
12 * All rights reserved.
14 * Author: Lei Xia <lxia@northwestern.edu>
16 * This is free software. You are permitted to use,
17 * redistribute, and modify it as specified in the file "V3VEE_LICENSE".
21 * Virtual NE2K Network Card
24 #include <devices/pci.h>
25 #include <devices/ne2k.h>
26 #include <palacios/vmm.h>
27 #include <palacios/vmm_types.h>
28 #include <palacios/vmm_io.h>
29 #include <palacios/vmm_debug.h>
30 #include <palacios/vmm_string.h>
34 #define PrintDebug(fmts, args...)
37 //#define TEST_PERFORMANCE 0
39 typedef enum {NIC_READY, NIC_REG_POSTED} nic_state_t;
42 struct guest_info *vm;
43 nic_state_t dev_state;
45 struct ne2k_regs regs;
47 uchar_t mem[NE2K_MEM_SIZE];
48 struct pci_device *pci_dev;
49 struct vm_device *pci;
54 struct vm_device *ne2ks[NUM_NE2K]; //the array of virtual network cards
56 static int nic_no = 0;
59 static uint32_t exit_num = 0;
60 static uint32_t int_num = 0;
63 #define compare_mac(src, dst) ({ \
64 ((src[0] == dst[0]) && \
65 (src[1] == dst[1]) && \
66 (src[2] == dst[2]) && \
67 (src[3] == dst[3]) && \
68 (src[4] == dst[4]) && \
69 (src[5] == dst[5]))? 1:0; \
72 extern int V3_Send_pkt(uchar_t *buf, int length);
73 extern int V3_Register_pkt_event(int (*netif_input)(uchar_t * pkt, uint_t size));
76 static void dump_state(struct vm_device *dev)
80 struct ne2k_context *nic_state = (struct ne2k_context *)dev->private_data;
82 PrintDebug("====NE2000: Dumping state Begin ==========\n");
83 PrintDebug("Registers:\n");
85 p = (uchar_t *)&nic_state->regs;
86 for(i = 0; i < sizeof(struct ne2k_regs); i++)
87 PrintDebug("Regs[%d] = 0x%2x\n", i, (int)p[i]);
89 PrintDebug("Memory:\n");
90 for(i = 0; i < 32; i++)
91 PrintDebug("0x%02x ", nic_state->mem[i]);
93 PrintDebug("====NE2000: Dumping state End==========\n");
99 static void ne2k_update_irq(struct vm_device *dev)
102 struct ne2k_context *nic_state = (struct ne2k_context *)dev->private_data;
103 struct guest_info *guest = dev->vm;
105 isr = ((nic_state->regs.isr & nic_state->regs.imr) & 0x7f);
107 if ((isr & 0x7f) != 0x0) {
108 v3_raise_irq(guest, NIC_DEF_IRQ);
109 PrintDebug("Ne2k: RaiseIrq: isr: 0x%02x imr: 0x%02x\n", nic_state->regs.isr, nic_state->regs.imr);
117 static void ne2k_update_irq(struct vm_device *dev)
120 struct ne2k_context *nic_state = (struct ne2k_context *)dev->private_data;
121 struct pci_device *pdev = nic_state->pci_dev;
125 PrintDebug("Ne2k: Device %p is not attached to any PCI Bus\n", nic_state);
126 irqline = NE2K_DEF_IRQ;
128 irqline = pdev->config_header.intr_line;
131 isr = ((nic_state->regs.isr & nic_state->regs.imr) & 0x7f);
134 PrintError("Ne2k: IRQ_LINE: %d\n", irqline);
138 PrintDebug("Ne2k: RaiseIrq: isr: 0x%02x imr: 0x%02x\n", nic_state->regs.isr, nic_state->regs.imr);
139 PrintDebug("ne2k_update_irq: irq_line: %d\n", irqline);
141 if ((isr & 0x7f) != 0x0) {
142 v3_raise_irq(nic_state->vm, irqline);
143 PrintDebug("Ne2k: RaiseIrq: isr: 0x%02x imr: 0x%02x\n", nic_state->regs.isr, nic_state->regs.imr);
146 if ((++int_num) % 50 == 0)
147 PrintError("Ne2k: Total Exit: %d, INT: %d\n", (int)exit_num, int_num);
154 static void init_ne2k_context(struct vm_device *dev)
156 struct ne2k_context *nic_state = (struct ne2k_context *)dev->private_data;
158 uchar_t mac[6] = {0x52, 0x54, 0x0, 0x12, 0x34, (0x60 + nic_no)};
160 nic_state->vm = dev->vm;
162 nic_state->regs.isr = ENISR_RESET;
163 nic_state->regs.imr = 0x00;
164 nic_state->regs.cmd = 0x22;
166 for (i = 0; i < 5; i++)
167 nic_state->regs.macaddr[i] = nic_state->mac[i] = mac[i];
169 nic_state->regs.macaddr[5] = nic_state->mac[5] = mac[5] + nic_no;
171 for (i = 0; i < 8; i++)
172 nic_state->regs.mult[i] = 0xff;
174 for(i = 0; i < 32; i++) {
175 nic_state->mem[i] = 0xff;
178 memcpy(nic_state->mem, nic_state->mac, 6);
179 nic_state->mem[14] = 0x57;
180 nic_state->mem[15] = 0x57;
188 static int ne2k_send_packet(struct vm_device *dev, uchar_t *pkt, int length)
192 PrintDebug("\nNe2k: Sending Packet\n");
194 for (i = 0; i<length; i++)
195 PrintDebug("%x ",pkt[i]);
198 return V3_Send_pkt(pkt, length);
201 //TODO: Support Broadcast and Multicast
202 static struct vm_device * get_rx_dev(uchar_t *dst_mac)
204 struct ne2k_context *nic_state;
205 struct ne2k_regs *nregs;
208 static const uchar_t brocast_mac[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
210 for (i = 0; i < NUM_NE2K; i++){
211 if (ne2ks[i] != NULL) {
212 nic_state = (struct ne2k_context *)ne2ks[i]->private_data;
213 nregs = &(nic_state->regs);
215 if (nregs->rcr & 0x10) {//broadcast packets, not correct here
218 if (compare_mac(dst_mac, brocast_mac) && (nregs->rcr & 0x04)) //broadcast address
220 if (dst_mac[0] & 0x01) {
221 // multicast packet, not done here
223 if (nregs->rcr & 0x08)
226 if (compare_mac(dst_mac, nic_state->mac))
235 static int ne2k_rxbuf_full(struct vm_device *dev)
237 int empty, index, boundary;
238 struct ne2k_context *nic_state = (struct ne2k_context *)dev->private_data;
240 index = nic_state->regs.curpag << 8;
241 boundary = nic_state->regs.boundary << 8;
242 if (index < boundary)
243 empty = boundary - index;
245 empty = ((nic_state->regs.pgstop - nic_state->regs.pgstart) << 8) - (index - boundary);
247 if (empty < (MAX_ETH_FRAME_SIZE + 4))
253 #define MIN_BUF_SIZE 60
255 static void ne2k_receive(struct vm_device *dev, const uchar_t *pkt, int length)
257 struct ne2k_context *nic_state = (struct ne2k_context *)dev->private_data;
258 struct ne2k_regs *nregs = &(nic_state->regs);
260 uint32_t total_len, next, len, index, empty;
262 uint32_t start, stop;
264 start = nregs->pgstart << 8;
265 stop = nregs->pgstop << 8;
267 if (nregs->cmd & NE2K_STOP)
270 if (ne2k_rxbuf_full(dev)){
271 PrintError("Ne2k: received buffer overflow\n");
275 //packet too small, expand it
276 if (length < MIN_BUF_SIZE) {
277 memcpy(buf, pkt, length);
278 memset(buf + length, 0, MIN_BUF_SIZE - length);
280 length = MIN_BUF_SIZE;
283 index = nregs->curpag << 8;
285 total_len = length + 4;
286 //address for next packet (4 bytes for CRC)
287 next = index + ((total_len + 4 + 255) & ~0xff);
289 next -= stop - start;
291 p = nic_state->mem + index;
292 nregs->rsr = ENRSR_RXOK;
295 nregs->rsr |= ENRSR_PHY;
300 p[3] = total_len >> 8;
305 empty = stop - index;
311 memcpy(nic_state->mem + index, pkt, len);
318 nregs->curpag = next >> 8;
320 nregs->isr |= ENISR_RX;
321 ne2k_update_irq(dev);
324 static int ne2k_hook_iospace(struct vm_device *vmdev, addr_t base_addr, int size, int type, void *data);
326 static struct pci_device * pci_ne2k_init(struct vm_device *vmdev,
327 struct vm_device *pci,
331 int (*io_read)(ushort_t port, void * dst, uint_t length, struct vm_device * dev),
332 int (*io_write)(ushort_t port, void * src, uint_t length, struct vm_device * dev))
335 struct pci_device *pdev;
336 struct v3_pci_bar ne2k_bar;
338 ne2k_bar.type = PCI_BAR_IO;
339 ne2k_bar.num_ports = 0x100;
340 ne2k_bar.default_base_port = 0xc100;
341 ne2k_bar.io_read = io_read;
342 ne2k_bar.io_write = io_write;
344 pdev = v3_pci_register_device(vmdev,
351 NULL, NULL, NULL, vmdev);
354 PrintError("NIC: Register to PCI bus failed\n");
358 pci_conf = pdev->config_space;
360 pci_conf[0x00] = 0xec; // Realtek 8029
361 pci_conf[0x01] = 0x10;
362 pci_conf[0x02] = 0x29;
363 pci_conf[0x03] = 0x80;
364 pci_conf[0x0a] = 0x00; // ethernet network controller
365 pci_conf[0x0b] = 0x02;
366 pci_conf[0x0e] = 0x00; // header_type
367 pci_conf[0x3d] = 1; // interrupt pin 0
370 pdev->vm_dev = vmdev;
375 static int netif_input(uchar_t * pkt, uint_t size)
378 struct vm_device *dev;
380 PrintDebug("\nNe2k: Packet Received:\nSource:");
381 for (i = 6; i < 12; i++) {
382 PrintDebug("%x ", pkt[i]);
385 dev = get_rx_dev(pkt); //TODO: handle multiple destination packets
391 for(i= 0; i<size; i++)
392 PrintDebug("%x ", pkt[i]);
394 ne2k_receive(dev, pkt, size);
400 static inline uint16_t cpu2le16(uint16_t val)
403 uchar_t *p1 = (uchar_t *)&p;
412 static inline uint32_t cpu2le32(uint32_t val)
415 uchar_t *p1 = (uchar_t *)&p;
425 static inline uint16_t le16_to_cpu(const uint16_t *p)
427 const uchar_t *p1 = (const uchar_t *)p;
428 return p1[0] | (p1[1] << 8);
431 static inline uint32_t le32_to_cpu(const uint32_t *p)
433 const uchar_t *p1 = (const uchar_t *)p;
434 return p1[0] | (p1[1] << 8) | (p1[2] << 16) | (p1[3] << 24);
438 ne2k_mem_writeb(struct ne2k_context *nic_state,
444 tmp = (uchar_t) (val & 0x000000ff);
445 if (addr < 32 || (addr >= NE2K_PMEM_START && addr < NE2K_MEM_SIZE)) {
446 nic_state->mem[addr] = tmp;
449 PrintDebug("wmem addr: %x val: %x\n", addr, val);
453 ne2k_mem_writew(struct ne2k_context *nic_state,
457 addr &= ~1; //XXX: check exact behaviour if not even
459 (addr >= NE2K_PMEM_START && addr < NE2K_MEM_SIZE)) {
460 *(ushort_t *)(nic_state->mem + addr) = cpu2le16(val);
463 PrintDebug("wmem addr: %x val: %x\n", addr, val);
467 ne2k_mem_writel(struct ne2k_context *nic_state,
471 addr &= ~1; // XXX: check exact behaviour if not even
473 (addr >= NE2K_PMEM_START && addr < NE2K_MEM_SIZE)) {
474 *(uint32_t *)(nic_state->mem + addr) = cpu2le32(val);
477 PrintDebug("wmem addr: %x val: %x\n", addr, val);
481 ne2k_mem_readb(struct ne2k_context *nic_state, uint32_t addr)
483 PrintDebug("rmem addr: %x\n", addr);
486 (addr >= NE2K_PMEM_START && addr < NE2K_MEM_SIZE)) {
487 return nic_state->mem[addr];
494 ne2k_mem_readw(struct ne2k_context *nic_state, uint32_t addr)
496 PrintDebug("rmem addr: %x\n", addr);
498 addr &= ~1; //XXX: check exact behaviour if not even
500 (addr >= NE2K_PMEM_START && addr < NE2K_MEM_SIZE)) {
501 return (ushort_t)le16_to_cpu((ushort_t *)(nic_state->mem + addr));
508 ne2k_mem_readl(struct ne2k_context *nic_state, uint32_t addr)
510 PrintDebug("rmem addr: %x\n", addr);
512 addr &= ~1; //XXX: check exact behaviour if not even
514 (addr >= NE2K_PMEM_START && addr < NE2K_MEM_SIZE)) {
515 return (uint32_t)le32_to_cpu((uint32_t *)(nic_state->mem + addr));
522 ne2k_dma_update(struct vm_device *dev, int len)
524 struct ne2k_context *nic_state = (struct ne2k_context *)dev->private_data;
526 nic_state->regs.rsar += len;
528 if (nic_state->regs.rsar == nic_state->regs.pgstop)
529 nic_state->regs.rsar = nic_state->regs.pgstart;
531 if (nic_state->regs.rbcr <= len) {
532 nic_state->regs.rbcr = 0;
533 nic_state->regs.isr |= ENISR_RDC;
534 ne2k_update_irq(dev);
536 nic_state->regs.rbcr -= len;
541 //for data port read/write
542 static int ne2k_data_read(ushort_t port,
545 struct vm_device *dev)
548 struct ne2k_context *nic_state = (struct ne2k_context *)dev->private_data;
550 // current dma address
551 uint32_t addr = nic_state->regs.rsar;
555 val = ne2k_mem_readb(nic_state, addr);
558 val = ne2k_mem_readw(nic_state, addr);
561 val = ne2k_mem_readl(nic_state, addr);
564 PrintError("ne2k_data_read error: invalid length %d\n", length);
568 ne2k_dma_update(dev, length);
570 memcpy(dst, &val, length);
572 PrintDebug("ne2k_read: port:0x%x (%u bytes): 0x%x", port & 0x1f,length, val);
575 if ((++exit_num) % 50 == 0)
576 PrintError("Ne2k-Ne2k: Total Exit: %d, INT: %d\n", (int)exit_num, int_num);
582 static int ne2k_data_write(ushort_t port,
585 struct vm_device *dev)
588 struct ne2k_context *nic_state = (struct ne2k_context *)dev->private_data;
590 if (nic_state->regs.rbcr == 0)
593 memcpy(&val, src, length);
595 uint32_t addr = nic_state->regs.rsar;
599 ne2k_mem_writeb(nic_state, addr, val);
602 ne2k_mem_writew(nic_state, addr, val);
605 ne2k_mem_writel(nic_state, addr, val);
608 PrintError("nic_data_write error: invalid length %d\n", length);
611 ne2k_dma_update(dev, length);
613 PrintDebug("ne2k_write: port:0x%x (%u bytes): 0x%x\n", port & 0x1f,length, val);
616 if ((++exit_num) % 50 == 0)
617 PrintError("Ne2k-Ne2k: Total Exit: %d, INT: %d\n", (int)exit_num, int_num);
623 static int ne2k_reset_device(struct vm_device * dev)
626 PrintDebug("vnic: reset device\n");
628 init_ne2k_context(dev);
635 static int ne2k_reset_port_read(ushort_t port,
638 struct vm_device *dev)
642 memcpy(dst, &val, length);
644 PrintDebug("ne2k_read: port:0x%x (%u bytes): 0x%x\n", port,length, val);
646 ne2k_reset_device(dev);
649 if ((++exit_num) % 50 == 0)
650 PrintError("Ne2k-Ne2k: Total Exit: %d, INT: %d\n", (int)exit_num, int_num);
656 static int ne2k_reset_port_write(ushort_t port,
659 struct vm_device *dev)
663 memcpy(&val, src, length);
665 PrintDebug("ne2k_write: port:0x%x (%u bytes): 0x%x\n", port,length, val);
668 if ((++exit_num) % 50 == 0)
669 PrintError("Ne2k-Ne2k: Total Exit: %d, INT: %d\n", (int)exit_num, int_num);
675 static int ne2k_ioport_write(ushort_t port,
678 struct vm_device *dev)
681 struct ne2k_context *nic_state = (struct ne2k_context* )dev->private_data;
686 memcpy(&val, src, 1);
688 PrintError("ne2k_write error: length %d\n", length);
692 PrintDebug("ne2k_write: port:0x%x val: 0x%x\n", port, (int)val);
697 return ne2k_data_write(port, src, length, dev);
700 return ne2k_reset_port_write(port, src, length, dev);
702 if (port == EN0_COMMAND) {
703 nic_state->regs.cmd = val;
704 if (!(val & NE2K_STOP)) {
705 nic_state->regs.isr &= ~ENISR_RESET;
706 if ((val & (NE2K_DMAREAD | NE2K_DMAWRITE)) &&
707 nic_state->regs.rbcr == 0) {
708 nic_state->regs.isr |= ENISR_RDC;
709 ne2k_update_irq(dev);
711 if (val & NE2K_TRANSMIT) {
712 index = (nic_state->regs.tpsr << 8);
713 if (index >= NE2K_PMEM_END)
714 index -= NE2K_PMEM_SIZE;
715 if (index + nic_state->regs.tbcr <= NE2K_PMEM_END) {
716 ne2k_send_packet(dev, nic_state->mem + index, nic_state->regs.tbcr);
718 nic_state->regs.tsr = ENTSR_PTX;
719 nic_state->regs.isr |= ENISR_TX;
720 nic_state->regs.cmd &= ~NE2K_TRANSMIT;
721 ne2k_update_irq(dev);
725 page = nic_state->regs.cmd >> 6;
729 nic_state->regs.pgstart = val;
732 nic_state->regs.pgstop = val;
735 nic_state->regs.boundary = val;
738 nic_state->regs.tpsr = val;
741 nic_state->regs.tbcr = (nic_state->regs.tbcr & 0xff00) | val;
744 nic_state->regs.tbcr = (nic_state->regs.tbcr & 0x00ff) | (val << 8);
747 nic_state->regs.isr &= ~(val & 0x7f);
748 ne2k_update_irq(dev);
751 nic_state->regs.rsar = (nic_state->regs.rsar & 0xff00) | val;
754 nic_state->regs.rsar = (nic_state->regs.rsar & 0x00ff) | (val << 8);
757 nic_state->regs.rbcr = (nic_state->regs.rbcr & 0xff00) | val;
760 nic_state->regs.rbcr = (nic_state->regs.rbcr & 0x00ff) | (val << 8);
763 nic_state->regs.rcr = val;
766 nic_state->regs.tcr = val;
768 nic_state->regs.dcr = val;
771 nic_state->regs.imr = val;
772 //PrintError("ne2k_write error: write IMR:0x%x\n", (int)val);
773 ne2k_update_irq(dev);
776 PrintError("ne2k_write error: invalid port:0x%x\n", port);
782 case EN1_PHYS ... EN1_PHYS + 5:
783 nic_state->regs.phys[port - EN1_PHYS] = val;
786 nic_state->regs.curpag = val;
788 case EN1_MULT ... EN1_MULT + 7:
789 // PrintError("ne2k_write error: write EN_MULT:0x%x\n", (int)val);
790 nic_state->regs.mult[port - EN1_MULT] = val;
793 PrintError("ne2k_write error: invalid port:0x%x\n", port);
800 nic_state->regs.clda = (nic_state->regs.clda & 0xff00) | val;
803 nic_state->regs.clda = (nic_state->regs.clda & 0x00ff) | (val << 8);
806 nic_state->regs.rnpp = val;
809 nic_state->regs.lnpp = val;
812 nic_state->regs.addcnt = (nic_state->regs.addcnt & 0xff00) | val;
815 nic_state->regs.addcnt = (nic_state->regs.addcnt & 0x00ff) | (val << 8);
818 PrintError("ne2k_write error: invalid port:0x%x\n", port);
825 if ((++exit_num) % 50 == 0)
826 PrintError("Ne2k-Ne2k: Total Exit: %d, INT: %d\n", (int)exit_num, int_num);
833 static int ne2k_ioport_read(ushort_t port,
836 struct vm_device *dev)
838 uchar_t page, ret, offset;
840 struct ne2k_context *nic_state = (struct ne2k_context* )dev->private_data;
843 PrintError("ne2k_read error: length %d\n", length);
851 return ne2k_data_read(port, dst, length, dev);
854 return ne2k_reset_port_read(port, dst, length, dev);
856 if (port == EN0_COMMAND) {
857 ret = nic_state->regs.cmd;
859 page = nic_state->regs.cmd >> 6;
863 ret = nic_state->regs.clda & 0x00ff;
866 ret = (nic_state->regs.clda & 0xff00) >> 8;
869 ret = nic_state->regs.boundary;
872 ret = nic_state->regs.tsr;
875 ret = nic_state->regs.ncr;
878 ret = nic_state->regs.fifo;
881 ret = nic_state->regs.isr;
882 ne2k_update_irq(dev);
885 ret = nic_state->regs.crda & 0x00ff;
888 ret = (nic_state->regs.crda & 0xff00) >> 8;
891 ret = nic_state->regs.rsr;
894 ret = nic_state->regs.cntr & 0x000000ff;
897 ret = (nic_state->regs.cntr & 0x0000ff00) >> 8;
900 ret = (nic_state->regs.cntr & 0x00ff0000) >> 16;
903 PrintError("ne2k_read error: invalid port:0x%x\n", port);
910 case EN1_PHYS ... EN1_PHYS + 5:
911 ret = nic_state->regs.phys[port - EN1_PHYS];
914 ret = nic_state->regs.curpag;
916 case EN1_MULT ... EN1_MULT + 7:
917 ret = nic_state->regs.mult[port - EN1_MULT];
920 PrintError("ne2k_read error: invalid port:0x%x\n", port);
928 ret = nic_state->regs.pgstart;
931 ret = nic_state->regs.pgstop;
934 ret = nic_state->regs.rnpp;
937 ret = nic_state->regs.lnpp;
940 ret = nic_state->regs.tpsr;
943 ret = nic_state->regs.addcnt & 0x00ff;
946 ret = (nic_state->regs.addcnt & 0xff00) >> 8;
949 ret = nic_state->regs.rcr;
952 ret = nic_state->regs.tcr;
955 ret = nic_state->regs.dcr;
958 ret = nic_state->regs.imr;
961 PrintError("ne2k_read error: invalid port:0x%x\n", port);
968 memcpy(dst, &ret, 1);
970 PrintDebug("ne2k_read: port:0x%x val: 0x%x\n", offset, (int)ret);
973 if ((++exit_num) % 50 == 0)
974 PrintError("Ne2k-Ne2k: Total Exit: %d, INT: %d\n", (int)exit_num, int_num);
982 static int ne2k_start_device(struct vm_device *dev)
984 PrintDebug("vnic: start device\n");
990 static int ne2k_stop_device(struct vm_device *dev)
992 PrintDebug("vnic: stop device\n");
997 static void init_global_setting()
1001 V3_Register_pkt_event(&netif_input);
1003 for (i = 0; i < NUM_NE2K; i++)
1008 static int ne2k_hook_iospace(struct vm_device *vmdev, addr_t base_addr, int size, int type, void *data)
1015 PrintDebug("In NIC%d: Hook IO space starting from %x\n", nic_no, (int) base_addr);
1017 for (i = 0; i < 16; i++){
1018 v3_dev_hook_io(vmdev, base_addr + i, &ne2k_ioport_read, &ne2k_ioport_write);
1020 v3_dev_hook_io(vmdev, base_addr + NIC_DATA_PORT, &ne2k_data_read, &ne2k_data_write);
1021 v3_dev_hook_io(vmdev, base_addr + NIC_RESET_PORT, &ne2k_reset_port_read, &ne2k_reset_port_write);
1027 static int ne2k_init_device(struct vm_device * vmdev)
1029 struct vm_device *pci = NULL;
1030 struct pci_device *pdev = NULL;
1031 struct ne2k_context *nic_state = (struct ne2k_context *)vmdev->private_data;
1033 PrintDebug("ne2k%d: init_device\n", nic_no);
1035 if (nic_no == 0) // only initiate once
1036 init_global_setting();
1038 init_ne2k_context(vmdev);
1040 pci = nic_state->pci;
1043 PrintDebug("Ne2k: attach ne2k to the pci %p\n", pci);
1044 pdev = pci_ne2k_init(vmdev, pci, 0, -1, 0, &ne2k_ioport_read, &ne2k_ioport_write);
1046 PrintError("Ne2k: initiate failure, failure to attach ne2k to the bus %p\n", pci);
1048 PrintError("Ne2k: Failure to initiate Ne2k , no pci\n");
1049 ne2k_hook_iospace(vmdev, NE2K_DEF_BASE_ADDR, 100, 0, NULL);
1052 nic_state->pci_dev = pdev;
1053 ne2ks[nic_no ++] = vmdev;
1064 static int ne2k_deinit_device(struct vm_device *vmdev)
1068 for (i = 0; i<16; i++){
1069 v3_dev_unhook_io(vmdev, NE2K_DEF_BASE_ADDR + i);
1072 v3_dev_unhook_io(vmdev, NE2K_DEF_BASE_ADDR + NIC_DATA_PORT);
1073 v3_dev_unhook_io(vmdev, NE2K_DEF_BASE_ADDR + NIC_RESET_PORT);
1079 static struct vm_device_ops dev_ops = {
1080 .init = ne2k_init_device,
1081 .deinit = ne2k_deinit_device,
1082 .reset = ne2k_reset_device,
1083 .start = ne2k_start_device,
1084 .stop = ne2k_stop_device,
1088 struct vm_device *v3_create_ne2k(struct vm_device *pci)
1090 struct ne2k_context * nic_state = V3_Malloc(sizeof(struct ne2k_context));
1092 memset(nic_state, 0, sizeof(struct ne2k_context));
1094 PrintDebug("NE2K internal at %p\n", (void *)nic_state);
1096 nic_state->pci = pci;
1098 struct vm_device *device = v3_create_device("NE2K", &dev_ops, nic_state);