Date: Thu, 20 Aug 2015 13:54:32 GMT From: iateaca@FreeBSD.org To: svn-soc-all@FreeBSD.org Subject: socsvn commit: r289965 - soc2015/iateaca/bhyve-ne2000-head/usr.sbin/bhyve Message-ID: <201508201354.t7KDsWwp040437@socsvn.freebsd.org>
next in thread | raw e-mail | index | archive | help
Author: iateaca Date: Thu Aug 20 13:54:32 2015 New Revision: 289965 URL: http://svnweb.FreeBSD.org/socsvn/?view=rev&rev=289965 Log: redesign: implement ne2000 init function Modified: soc2015/iateaca/bhyve-ne2000-head/usr.sbin/bhyve/pci_ne2000.c Modified: soc2015/iateaca/bhyve-ne2000-head/usr.sbin/bhyve/pci_ne2000.c ============================================================================== --- soc2015/iateaca/bhyve-ne2000-head/usr.sbin/bhyve/pci_ne2000.c Thu Aug 20 12:49:56 2015 (r289964) +++ soc2015/iateaca/bhyve-ne2000-head/usr.sbin/bhyve/pci_ne2000.c Thu Aug 20 13:54:32 2015 (r289965) @@ -62,7 +62,7 @@ * NE2000 data structures */ struct ne2000_softc { - struct pci_devinst *asc_pi; + struct pci_devinst *pci_inst; /* * one single mutex used to lock the reception flow with @@ -101,6 +101,9 @@ ne2000_set_field_by_offset(struct ne2000_softc *sc, uint8_t page, uint8_t offset, uint8_t mask, uint8_t value); +static int +ne2000_init(struct ne2000_softc *sc, char *opts); + static uint8_t ne2000_read_nic(struct ne2000_softc *sc, uint8_t offset); static uint16_t @@ -147,6 +150,18 @@ ne2000_parse_input(char *opts, char *tap_name, uint8_t *mac); /* + * PCI NE2000 function declarations + */ +static int +pci_ne2000_init(struct vmctx *ctx, struct pci_devinst *pi, char *opts); +static void +pci_ne2000_write(struct vmctx *ctx, int vcpu, struct pci_devinst *pi, + int baridx, uint64_t offset, int size, uint64_t value); +static uint64_t +pci_ne2000_read(struct vmctx *ctx, int vcpu, struct pci_devinst *pi, + int baridx, uint64_t offset, int size); + +/* * NE2000 module function definitions */ static void @@ -194,12 +209,12 @@ if (imr & isr) { if (!sc->lintr) { - pci_lintr_assert(sc->asc_pi); + pci_lintr_assert(sc->pci_inst); sc->lintr = 1; } } else { if (sc->lintr) { - pci_lintr_deassert(sc->asc_pi); + pci_lintr_deassert(sc->pci_inst); sc->lintr = 0; } } @@ -555,103 +570,6 @@ } static int -pci_ne2000_init(struct vmctx *ctx, struct pci_devinst *pi, char *opts) -{ - struct ne2000_softc *sc = NULL; - int err; - - /* the default mac address is 00:a0:98:4a:0e:ee */ - uint8_t mac[ETHER_ADDR_LEN] = {0x00, 0xa0, 0x98, 0x4a, 0x0e, 0xee}; - char tap_name[MAX_INPUT_LEN]; - -#if DEBUG_NE2000 == 1 - dbg = fopen("/tmp/bhyve_ne2000.log", "w+"); -#endif - - assert(ctx != NULL); - assert(pi != NULL); - - sc = calloc(1, sizeof(struct ne2000_softc)); - pi->pi_arg = sc; - sc->asc_pi = pi; - - err = ne2000_parse_input(opts, tap_name, mac); - if (err != 0) { - printf("Use input param like: -s x:y,ne2000-net,tap_name[,mac address]"); - free(sc); - return 1; - } - - err = pthread_mutex_init(&sc->mtx, NULL); - assert(err == 0); - - err = ne2000_tap_init(sc, tap_name); - assert(err == 0); - - /* probe a RTL8029 PCI card as a generic NE2000 device */ - pci_set_cfgdata16(pi, PCIR_DEVICE, 0x8029); - pci_set_cfgdata16(pi, PCIR_VENDOR, 0x10ec); - - /* allocate two BAR registers for both NIC and ASIC I/O bus address offsets */ - pci_emul_alloc_bar(pi, 0, PCIBAR_IO, 16); - pci_emul_alloc_bar(pi, 1, PCIBAR_IO, 16); - - /* allocate an IRQ pin for our slot */ - pci_lintr_request(pi); - - /* set network medium type as 10BaseT and full-duplex */ - ne2000_set_reg_by_offset(sc, NE2000_P3, - ED_RTL80X9_CONFIG2, ED_RTL80X9_CF2_10_T); - ne2000_set_reg_by_offset(sc, NE2000_P3, - ED_RTL80X9_CONFIG3, ED_RTL80X9_CF3_FUDUP); - - /* the NE2000 card has his MAC address located in the first 6 words of the RAM memory */ - sc->ram[0] = mac[0]; - sc->ram[2] = mac[1]; - sc->ram[4] = mac[2]; - sc->ram[6] = mac[3]; - sc->ram[8] = mac[4]; - sc->ram[10] = mac[5]; - - return 0; -} - -static void -pci_ne2000_write(struct vmctx *ctx, int vcpu, struct pci_devinst *pi, - int baridx, uint64_t offset, int size, uint64_t value) -{ - struct ne2000_softc *sc = pi->pi_arg; - int err; - - assert(sc != NULL); - assert(offset <= 0x0f); - - err = pthread_mutex_lock(&sc->mtx); - assert(err == 0); - - switch (baridx) { - case NE2000_BAR_NIC: - assert(size == 1); - assert(value <= 0xff); - err = ne2000_write_nic(sc, offset, value); - break; - case NE2000_BAR_ASIC: - assert(size <= 2); - err = ne2000_write_asic(sc, offset, value); - break; - default: - assert(0); - } - - assert(err == 0); - - err = pthread_mutex_unlock(&sc->mtx); - assert(err == 0); - - return; -} - -static int ne2000_write_nic(struct ne2000_softc *sc, uint8_t offset, uint8_t value) { int err; @@ -935,37 +853,46 @@ return 0; } -static uint64_t -pci_ne2000_read(struct vmctx *ctx, int vcpu, struct pci_devinst *pi, - int baridx, uint64_t offset, int size) +static int +ne2000_init(struct ne2000_softc *sc, char *opts) { - struct ne2000_softc *sc = pi->pi_arg; - uint64_t value = 0; + /* the default mac address is 00:a0:98:4a:0e:ee */ + uint8_t mac[ETHER_ADDR_LEN] = {0x00, 0xa0, 0x98, 0x4a, 0x0e, 0xee}; + char tap_name[MAX_INPUT_LEN]; int err; - assert(sc != NULL); - assert(offset <= 0x0f); - - err = pthread_mutex_lock(&sc->mtx); - assert(err == 0); +#if DEBUG_NE2000 == 1 + dbg = fopen("/tmp/bhyve_ne2000.log", "w+"); +#endif - switch (baridx) { - case NE2000_BAR_NIC: - assert(size == 1); - value = ne2000_read_nic(sc, offset); - break; - case NE2000_BAR_ASIC: - assert(size <= 2); - value = ne2000_read_asic(sc, offset); - break; - default: - assert(0); + err = ne2000_parse_input(opts, tap_name, mac); + if (err != 0) { + printf("Use input param like: -s x:y,ne2000-net,tap_name[,mac address]"); + free(sc); + return -1; } - err = pthread_mutex_unlock(&sc->mtx); + err = pthread_mutex_init(&sc->mtx, NULL); assert(err == 0); - return value; + err = ne2000_tap_init(sc, tap_name); + assert(err == 0); + + /* set network medium type as 10BaseT and full-duplex */ + ne2000_set_reg_by_offset(sc, NE2000_P3, + ED_RTL80X9_CONFIG2, ED_RTL80X9_CF2_10_T); + ne2000_set_reg_by_offset(sc, NE2000_P3, + ED_RTL80X9_CONFIG3, ED_RTL80X9_CF3_FUDUP); + + /* the NE2000 card has his MAC address located in the first 6 words of the RAM memory */ + sc->ram[0] = mac[0]; + sc->ram[2] = mac[1]; + sc->ram[4] = mac[2]; + sc->ram[6] = mac[3]; + sc->ram[8] = mac[4]; + sc->ram[10] = mac[5]; + + return 0; } static uint8_t @@ -1101,6 +1028,109 @@ return 0; } +/* + * PCI NE2000 function definitions + */ +static int +pci_ne2000_init(struct vmctx *ctx, struct pci_devinst *pi, char *opts) +{ + struct ne2000_softc *sc = NULL; + + assert(ctx != NULL); + assert(pi != NULL); + + sc = calloc(1, sizeof(struct ne2000_softc)); + + /* initialize the ne2000 data structure */ + if (ne2000_init(sc, opts) != 0) + return 1; + + /* save the pci instance into the ne2000 structure */ + sc->pci_inst = pi; + pi->pi_arg = sc; + + /* probe a RTL8029 PCI card as a generic NE2000 device */ + pci_set_cfgdata16(pi, PCIR_DEVICE, 0x8029); + pci_set_cfgdata16(pi, PCIR_VENDOR, 0x10ec); + + /* allocate two BAR registers for both NIC and ASIC I/O bus address offsets */ + pci_emul_alloc_bar(pi, 0, PCIBAR_IO, 16); + pci_emul_alloc_bar(pi, 1, PCIBAR_IO, 16); + + /* allocate an IRQ pin for our slot */ + pci_lintr_request(pi); + + return 0; +} + +static void +pci_ne2000_write(struct vmctx *ctx, int vcpu, struct pci_devinst *pi, + int baridx, uint64_t offset, int size, uint64_t value) +{ + struct ne2000_softc *sc = pi->pi_arg; + int err; + + assert(sc != NULL); + assert(offset <= 0x0f); + + err = pthread_mutex_lock(&sc->mtx); + assert(err == 0); + + switch (baridx) { + case NE2000_BAR_NIC: + assert(size == 1); + assert(value <= 0xff); + err = ne2000_write_nic(sc, offset, value); + break; + case NE2000_BAR_ASIC: + assert(size <= 2); + err = ne2000_write_asic(sc, offset, value); + break; + default: + assert(0); + } + + assert(err == 0); + + err = pthread_mutex_unlock(&sc->mtx); + assert(err == 0); + + return; +} + +static uint64_t +pci_ne2000_read(struct vmctx *ctx, int vcpu, struct pci_devinst *pi, + int baridx, uint64_t offset, int size) +{ + struct ne2000_softc *sc = pi->pi_arg; + uint64_t value = 0; + int err; + + assert(sc != NULL); + assert(offset <= 0x0f); + + err = pthread_mutex_lock(&sc->mtx); + assert(err == 0); + + switch (baridx) { + case NE2000_BAR_NIC: + assert(size == 1); + value = ne2000_read_nic(sc, offset); + break; + case NE2000_BAR_ASIC: + assert(size <= 2); + value = ne2000_read_asic(sc, offset); + break; + default: + assert(0); + } + + err = pthread_mutex_unlock(&sc->mtx); + assert(err == 0); + + return value; +} + struct pci_devemu pci_de_ne2000_net = { .pe_emu = "ne2000-net", .pe_init = pci_ne2000_init,
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201508201354.t7KDsWwp040437>