19453c5bcSGerd Hoffmann /* 29453c5bcSGerd Hoffmann * QEMU NE2000 emulation -- isa bus windup 39453c5bcSGerd Hoffmann * 49453c5bcSGerd Hoffmann * Copyright (c) 2003-2004 Fabrice Bellard 59453c5bcSGerd Hoffmann * 69453c5bcSGerd Hoffmann * Permission is hereby granted, free of charge, to any person obtaining a copy 79453c5bcSGerd Hoffmann * of this software and associated documentation files (the "Software"), to deal 89453c5bcSGerd Hoffmann * in the Software without restriction, including without limitation the rights 99453c5bcSGerd Hoffmann * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 109453c5bcSGerd Hoffmann * copies of the Software, and to permit persons to whom the Software is 119453c5bcSGerd Hoffmann * furnished to do so, subject to the following conditions: 129453c5bcSGerd Hoffmann * 139453c5bcSGerd Hoffmann * The above copyright notice and this permission notice shall be included in 149453c5bcSGerd Hoffmann * all copies or substantial portions of the Software. 159453c5bcSGerd Hoffmann * 169453c5bcSGerd Hoffmann * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 179453c5bcSGerd Hoffmann * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 189453c5bcSGerd Hoffmann * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 199453c5bcSGerd Hoffmann * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 209453c5bcSGerd Hoffmann * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 219453c5bcSGerd Hoffmann * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 229453c5bcSGerd Hoffmann * THE SOFTWARE. 239453c5bcSGerd Hoffmann */ 24e8d40465SPeter Maydell #include "qemu/osdep.h" 250d09e41aSPaolo Bonzini #include "hw/isa/isa.h" 26*489983d6SPhilippe Mathieu-Daudé #include "hw/net/ne2000-isa.h" 2783c9f4caSPaolo Bonzini #include "hw/qdev.h" 2847b43a1fSPaolo Bonzini #include "ne2000.h" 29*489983d6SPhilippe Mathieu-Daudé #include "sysemu/sysemu.h" 30022c62cbSPaolo Bonzini #include "exec/address-spaces.h" 31da34e65cSMarkus Armbruster #include "qapi/error.h" 326cb0851dSGonglei #include "qapi/visitor.h" 339453c5bcSGerd Hoffmann 34fe6f5debSAndreas Färber #define ISA_NE2000(obj) OBJECT_CHECK(ISANE2000State, (obj), TYPE_ISA_NE2000) 35fe6f5debSAndreas Färber 369453c5bcSGerd Hoffmann typedef struct ISANE2000State { 37fe6f5debSAndreas Färber ISADevice parent_obj; 38fe6f5debSAndreas Färber 399453c5bcSGerd Hoffmann uint32_t iobase; 409453c5bcSGerd Hoffmann uint32_t isairq; 419453c5bcSGerd Hoffmann NE2000State ne2000; 429453c5bcSGerd Hoffmann } ISANE2000State; 439453c5bcSGerd Hoffmann 441c2045b5SMark McLoughlin static NetClientInfo net_ne2000_isa_info = { 45f394b2e2SEric Blake .type = NET_CLIENT_DRIVER_NIC, 461c2045b5SMark McLoughlin .size = sizeof(NICState), 471c2045b5SMark McLoughlin .receive = ne2000_receive, 481c2045b5SMark McLoughlin }; 491c2045b5SMark McLoughlin 50d05ac8faSBlue Swirl static const VMStateDescription vmstate_isa_ne2000 = { 51be73cfe2SJuan Quintela .name = "ne2000", 52be73cfe2SJuan Quintela .version_id = 2, 53be73cfe2SJuan Quintela .minimum_version_id = 0, 54be73cfe2SJuan Quintela .fields = (VMStateField[]) { 55be73cfe2SJuan Quintela VMSTATE_STRUCT(ne2000, ISANE2000State, 0, vmstate_ne2000, NE2000State), 56be73cfe2SJuan Quintela VMSTATE_END_OF_LIST() 57be73cfe2SJuan Quintela } 58be73cfe2SJuan Quintela }; 59be73cfe2SJuan Quintela 60db895a1eSAndreas Färber static void isa_ne2000_realizefn(DeviceState *dev, Error **errp) 619453c5bcSGerd Hoffmann { 62db895a1eSAndreas Färber ISADevice *isadev = ISA_DEVICE(dev); 63fe6f5debSAndreas Färber ISANE2000State *isa = ISA_NE2000(dev); 649453c5bcSGerd Hoffmann NE2000State *s = &isa->ne2000; 659453c5bcSGerd Hoffmann 66dcb117bfSPaolo Bonzini ne2000_setup_io(s, DEVICE(isadev), 0x20); 67db895a1eSAndreas Färber isa_register_ioport(isadev, &s->io, isa->iobase); 689453c5bcSGerd Hoffmann 69db895a1eSAndreas Färber isa_init_irq(isadev, &s->irq, isa->isairq); 709453c5bcSGerd Hoffmann 7193db6685SGerd Hoffmann qemu_macaddr_default_if_unset(&s->c.macaddr); 729453c5bcSGerd Hoffmann ne2000_reset(s); 739453c5bcSGerd Hoffmann 741c2045b5SMark McLoughlin s->nic = qemu_new_nic(&net_ne2000_isa_info, &s->c, 75db895a1eSAndreas Färber object_get_typename(OBJECT(dev)), dev->id, s); 76b356f76dSJason Wang qemu_format_nic_info_str(qemu_get_queue(s->nic), s->c.macaddr.a); 779453c5bcSGerd Hoffmann } 789453c5bcSGerd Hoffmann 7939bffca2SAnthony Liguori static Property ne2000_isa_properties[] = { 80c7bcc85dSPaolo Bonzini DEFINE_PROP_UINT32("iobase", ISANE2000State, iobase, 0x300), 819453c5bcSGerd Hoffmann DEFINE_PROP_UINT32("irq", ISANE2000State, isairq, 9), 8293db6685SGerd Hoffmann DEFINE_NIC_PROPERTIES(ISANE2000State, ne2000.c), 839453c5bcSGerd Hoffmann DEFINE_PROP_END_OF_LIST(), 8439bffca2SAnthony Liguori }; 8539bffca2SAnthony Liguori 8639bffca2SAnthony Liguori static void isa_ne2000_class_initfn(ObjectClass *klass, void *data) 8739bffca2SAnthony Liguori { 8839bffca2SAnthony Liguori DeviceClass *dc = DEVICE_CLASS(klass); 89db895a1eSAndreas Färber 90db895a1eSAndreas Färber dc->realize = isa_ne2000_realizefn; 9139bffca2SAnthony Liguori dc->props = ne2000_isa_properties; 9289218c21SPeter Maydell dc->vmsd = &vmstate_isa_ne2000; 93125ee0edSMarcel Apfelbaum set_bit(DEVICE_CATEGORY_NETWORK, dc->categories); 9439bffca2SAnthony Liguori } 9539bffca2SAnthony Liguori 96d7bce999SEric Blake static void isa_ne2000_get_bootindex(Object *obj, Visitor *v, 97d7bce999SEric Blake const char *name, void *opaque, 98d7bce999SEric Blake Error **errp) 996cb0851dSGonglei { 1006cb0851dSGonglei ISANE2000State *isa = ISA_NE2000(obj); 1016cb0851dSGonglei NE2000State *s = &isa->ne2000; 1026cb0851dSGonglei 10351e72bc1SEric Blake visit_type_int32(v, name, &s->c.bootindex, errp); 1046cb0851dSGonglei } 1056cb0851dSGonglei 106d7bce999SEric Blake static void isa_ne2000_set_bootindex(Object *obj, Visitor *v, 107d7bce999SEric Blake const char *name, void *opaque, 108d7bce999SEric Blake Error **errp) 1096cb0851dSGonglei { 1106cb0851dSGonglei ISANE2000State *isa = ISA_NE2000(obj); 1116cb0851dSGonglei NE2000State *s = &isa->ne2000; 1126cb0851dSGonglei int32_t boot_index; 1136cb0851dSGonglei Error *local_err = NULL; 1146cb0851dSGonglei 11551e72bc1SEric Blake visit_type_int32(v, name, &boot_index, &local_err); 1166cb0851dSGonglei if (local_err) { 1176cb0851dSGonglei goto out; 1186cb0851dSGonglei } 1196cb0851dSGonglei /* check whether bootindex is present in fw_boot_order list */ 1206cb0851dSGonglei check_boot_index(boot_index, &local_err); 1216cb0851dSGonglei if (local_err) { 1226cb0851dSGonglei goto out; 1236cb0851dSGonglei } 1246cb0851dSGonglei /* change bootindex to a new one */ 1256cb0851dSGonglei s->c.bootindex = boot_index; 1266cb0851dSGonglei 1276cb0851dSGonglei out: 1286cb0851dSGonglei error_propagate(errp, local_err); 1296cb0851dSGonglei } 1306cb0851dSGonglei 1316cb0851dSGonglei static void isa_ne2000_instance_init(Object *obj) 1326cb0851dSGonglei { 1336cb0851dSGonglei object_property_add(obj, "bootindex", "int32", 1346cb0851dSGonglei isa_ne2000_get_bootindex, 1356cb0851dSGonglei isa_ne2000_set_bootindex, NULL, NULL, NULL); 1366cb0851dSGonglei object_property_set_int(obj, -1, "bootindex", NULL); 1376cb0851dSGonglei } 1388c43a6f0SAndreas Färber static const TypeInfo ne2000_isa_info = { 139fe6f5debSAndreas Färber .name = TYPE_ISA_NE2000, 14039bffca2SAnthony Liguori .parent = TYPE_ISA_DEVICE, 14139bffca2SAnthony Liguori .instance_size = sizeof(ISANE2000State), 14239bffca2SAnthony Liguori .class_init = isa_ne2000_class_initfn, 1436cb0851dSGonglei .instance_init = isa_ne2000_instance_init, 1449453c5bcSGerd Hoffmann }; 1459453c5bcSGerd Hoffmann 14683f7d43aSAndreas Färber static void ne2000_isa_register_types(void) 1479453c5bcSGerd Hoffmann { 14839bffca2SAnthony Liguori type_register_static(&ne2000_isa_info); 1499453c5bcSGerd Hoffmann } 1509453c5bcSGerd Hoffmann 15183f7d43aSAndreas Färber type_init(ne2000_isa_register_types) 152