xref: /qemu/hw/net/ne2000-isa.c (revision e8d40465592716cb209f0ae5de6b4cbe9ea2f8ba)
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  */
24*e8d40465SPeter Maydell #include "qemu/osdep.h"
2583c9f4caSPaolo Bonzini #include "hw/hw.h"
260d09e41aSPaolo Bonzini #include "hw/i386/pc.h"
270d09e41aSPaolo Bonzini #include "hw/isa/isa.h"
2883c9f4caSPaolo Bonzini #include "hw/qdev.h"
291422e32dSPaolo Bonzini #include "net/net.h"
3047b43a1fSPaolo Bonzini #include "ne2000.h"
31022c62cbSPaolo Bonzini #include "exec/address-spaces.h"
326cb0851dSGonglei #include "qapi/visitor.h"
339453c5bcSGerd Hoffmann 
34fe6f5debSAndreas Färber #define TYPE_ISA_NE2000 "ne2k_isa"
35fe6f5debSAndreas Färber #define ISA_NE2000(obj) OBJECT_CHECK(ISANE2000State, (obj), TYPE_ISA_NE2000)
36fe6f5debSAndreas Färber 
379453c5bcSGerd Hoffmann typedef struct ISANE2000State {
38fe6f5debSAndreas Färber     ISADevice parent_obj;
39fe6f5debSAndreas Färber 
409453c5bcSGerd Hoffmann     uint32_t iobase;
419453c5bcSGerd Hoffmann     uint32_t isairq;
429453c5bcSGerd Hoffmann     NE2000State ne2000;
439453c5bcSGerd Hoffmann } ISANE2000State;
449453c5bcSGerd Hoffmann 
451c2045b5SMark McLoughlin static NetClientInfo net_ne2000_isa_info = {
462be64a68SLaszlo Ersek     .type = NET_CLIENT_OPTIONS_KIND_NIC,
471c2045b5SMark McLoughlin     .size = sizeof(NICState),
481c2045b5SMark McLoughlin     .receive = ne2000_receive,
491c2045b5SMark McLoughlin };
501c2045b5SMark McLoughlin 
51d05ac8faSBlue Swirl static const VMStateDescription vmstate_isa_ne2000 = {
52be73cfe2SJuan Quintela     .name = "ne2000",
53be73cfe2SJuan Quintela     .version_id = 2,
54be73cfe2SJuan Quintela     .minimum_version_id = 0,
55be73cfe2SJuan Quintela     .fields = (VMStateField[]) {
56be73cfe2SJuan Quintela         VMSTATE_STRUCT(ne2000, ISANE2000State, 0, vmstate_ne2000, NE2000State),
57be73cfe2SJuan Quintela         VMSTATE_END_OF_LIST()
58be73cfe2SJuan Quintela     }
59be73cfe2SJuan Quintela };
60be73cfe2SJuan Quintela 
61db895a1eSAndreas Färber static void isa_ne2000_realizefn(DeviceState *dev, Error **errp)
629453c5bcSGerd Hoffmann {
63db895a1eSAndreas Färber     ISADevice *isadev = ISA_DEVICE(dev);
64fe6f5debSAndreas Färber     ISANE2000State *isa = ISA_NE2000(dev);
659453c5bcSGerd Hoffmann     NE2000State *s = &isa->ne2000;
669453c5bcSGerd Hoffmann 
67dcb117bfSPaolo Bonzini     ne2000_setup_io(s, DEVICE(isadev), 0x20);
68db895a1eSAndreas Färber     isa_register_ioport(isadev, &s->io, isa->iobase);
699453c5bcSGerd Hoffmann 
70db895a1eSAndreas Färber     isa_init_irq(isadev, &s->irq, isa->isairq);
719453c5bcSGerd Hoffmann 
7293db6685SGerd Hoffmann     qemu_macaddr_default_if_unset(&s->c.macaddr);
739453c5bcSGerd Hoffmann     ne2000_reset(s);
749453c5bcSGerd Hoffmann 
751c2045b5SMark McLoughlin     s->nic = qemu_new_nic(&net_ne2000_isa_info, &s->c,
76db895a1eSAndreas Färber                           object_get_typename(OBJECT(dev)), dev->id, s);
77b356f76dSJason Wang     qemu_format_nic_info_str(qemu_get_queue(s->nic), s->c.macaddr.a);
789453c5bcSGerd Hoffmann }
799453c5bcSGerd Hoffmann 
8039bffca2SAnthony Liguori static Property ne2000_isa_properties[] = {
81c7bcc85dSPaolo Bonzini     DEFINE_PROP_UINT32("iobase", ISANE2000State, iobase, 0x300),
829453c5bcSGerd Hoffmann     DEFINE_PROP_UINT32("irq",   ISANE2000State, isairq, 9),
8393db6685SGerd Hoffmann     DEFINE_NIC_PROPERTIES(ISANE2000State, ne2000.c),
849453c5bcSGerd Hoffmann     DEFINE_PROP_END_OF_LIST(),
8539bffca2SAnthony Liguori };
8639bffca2SAnthony Liguori 
8739bffca2SAnthony Liguori static void isa_ne2000_class_initfn(ObjectClass *klass, void *data)
8839bffca2SAnthony Liguori {
8939bffca2SAnthony Liguori     DeviceClass *dc = DEVICE_CLASS(klass);
90db895a1eSAndreas Färber 
91db895a1eSAndreas Färber     dc->realize = isa_ne2000_realizefn;
9239bffca2SAnthony Liguori     dc->props = ne2000_isa_properties;
9389218c21SPeter Maydell     dc->vmsd = &vmstate_isa_ne2000;
94125ee0edSMarcel Apfelbaum     set_bit(DEVICE_CATEGORY_NETWORK, dc->categories);
9539bffca2SAnthony Liguori }
9639bffca2SAnthony Liguori 
976cb0851dSGonglei static void isa_ne2000_get_bootindex(Object *obj, Visitor *v, void *opaque,
986cb0851dSGonglei                                      const char *name, Error **errp)
996cb0851dSGonglei {
1006cb0851dSGonglei     ISANE2000State *isa = ISA_NE2000(obj);
1016cb0851dSGonglei     NE2000State *s = &isa->ne2000;
1026cb0851dSGonglei 
1036cb0851dSGonglei     visit_type_int32(v, &s->c.bootindex, name, errp);
1046cb0851dSGonglei }
1056cb0851dSGonglei 
1066cb0851dSGonglei static void isa_ne2000_set_bootindex(Object *obj, Visitor *v, void *opaque,
1076cb0851dSGonglei                                      const char *name, Error **errp)
1086cb0851dSGonglei {
1096cb0851dSGonglei     ISANE2000State *isa = ISA_NE2000(obj);
1106cb0851dSGonglei     NE2000State *s = &isa->ne2000;
1116cb0851dSGonglei     int32_t boot_index;
1126cb0851dSGonglei     Error *local_err = NULL;
1136cb0851dSGonglei 
1146cb0851dSGonglei     visit_type_int32(v, &boot_index, name, &local_err);
1156cb0851dSGonglei     if (local_err) {
1166cb0851dSGonglei         goto out;
1176cb0851dSGonglei     }
1186cb0851dSGonglei     /* check whether bootindex is present in fw_boot_order list  */
1196cb0851dSGonglei     check_boot_index(boot_index, &local_err);
1206cb0851dSGonglei     if (local_err) {
1216cb0851dSGonglei         goto out;
1226cb0851dSGonglei     }
1236cb0851dSGonglei     /* change bootindex to a new one */
1246cb0851dSGonglei     s->c.bootindex = boot_index;
1256cb0851dSGonglei 
1266cb0851dSGonglei out:
1276cb0851dSGonglei     if (local_err) {
1286cb0851dSGonglei         error_propagate(errp, local_err);
1296cb0851dSGonglei     }
1306cb0851dSGonglei }
1316cb0851dSGonglei 
1326cb0851dSGonglei static void isa_ne2000_instance_init(Object *obj)
1336cb0851dSGonglei {
1346cb0851dSGonglei     object_property_add(obj, "bootindex", "int32",
1356cb0851dSGonglei                         isa_ne2000_get_bootindex,
1366cb0851dSGonglei                         isa_ne2000_set_bootindex, NULL, NULL, NULL);
1376cb0851dSGonglei     object_property_set_int(obj, -1, "bootindex", NULL);
1386cb0851dSGonglei }
1398c43a6f0SAndreas Färber static const TypeInfo ne2000_isa_info = {
140fe6f5debSAndreas Färber     .name          = TYPE_ISA_NE2000,
14139bffca2SAnthony Liguori     .parent        = TYPE_ISA_DEVICE,
14239bffca2SAnthony Liguori     .instance_size = sizeof(ISANE2000State),
14339bffca2SAnthony Liguori     .class_init    = isa_ne2000_class_initfn,
1446cb0851dSGonglei     .instance_init = isa_ne2000_instance_init,
1459453c5bcSGerd Hoffmann };
1469453c5bcSGerd Hoffmann 
14783f7d43aSAndreas Färber static void ne2000_isa_register_types(void)
1489453c5bcSGerd Hoffmann {
14939bffca2SAnthony Liguori     type_register_static(&ne2000_isa_info);
1509453c5bcSGerd Hoffmann }
1519453c5bcSGerd Hoffmann 
15283f7d43aSAndreas Färber type_init(ne2000_isa_register_types)
153