xref: /openbmc/qemu/hw/net/ne2000-isa.c (revision 8fa3b702)
1 /*
2  * QEMU NE2000 emulation -- isa bus windup
3  *
4  * Copyright (c) 2003-2004 Fabrice Bellard
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  */
24 
25 #include "qemu/osdep.h"
26 #include "hw/isa/isa.h"
27 #include "hw/net/ne2000-isa.h"
28 #include "migration/vmstate.h"
29 #include "ne2000.h"
30 #include "sysemu/sysemu.h"
31 #include "qapi/error.h"
32 #include "qapi/visitor.h"
33 #include "qemu/module.h"
34 #include "qom/object.h"
35 
36 typedef struct ISANE2000State ISANE2000State;
37 DECLARE_INSTANCE_CHECKER(ISANE2000State, ISA_NE2000,
38                          TYPE_ISA_NE2000)
39 
40 struct ISANE2000State {
41     ISADevice parent_obj;
42 
43     uint32_t iobase;
44     uint32_t isairq;
45     NE2000State ne2000;
46 };
47 
48 static NetClientInfo net_ne2000_isa_info = {
49     .type = NET_CLIENT_DRIVER_NIC,
50     .size = sizeof(NICState),
51     .receive = ne2000_receive,
52 };
53 
54 static const VMStateDescription vmstate_isa_ne2000 = {
55     .name = "ne2000",
56     .version_id = 2,
57     .minimum_version_id = 0,
58     .fields = (VMStateField[]) {
59         VMSTATE_STRUCT(ne2000, ISANE2000State, 0, vmstate_ne2000, NE2000State),
60         VMSTATE_END_OF_LIST()
61     }
62 };
63 
64 static void isa_ne2000_realizefn(DeviceState *dev, Error **errp)
65 {
66     ISADevice *isadev = ISA_DEVICE(dev);
67     ISANE2000State *isa = ISA_NE2000(dev);
68     NE2000State *s = &isa->ne2000;
69 
70     ne2000_setup_io(s, DEVICE(isadev), 0x20);
71     isa_register_ioport(isadev, &s->io, isa->iobase);
72 
73     isa_init_irq(isadev, &s->irq, isa->isairq);
74 
75     qemu_macaddr_default_if_unset(&s->c.macaddr);
76     ne2000_reset(s);
77 
78     s->nic = qemu_new_nic(&net_ne2000_isa_info, &s->c,
79                           object_get_typename(OBJECT(dev)), dev->id, s);
80     qemu_format_nic_info_str(qemu_get_queue(s->nic), s->c.macaddr.a);
81 }
82 
83 static Property ne2000_isa_properties[] = {
84     DEFINE_PROP_UINT32("iobase", ISANE2000State, iobase, 0x300),
85     DEFINE_PROP_UINT32("irq",   ISANE2000State, isairq, 9),
86     DEFINE_NIC_PROPERTIES(ISANE2000State, ne2000.c),
87     DEFINE_PROP_END_OF_LIST(),
88 };
89 
90 static void isa_ne2000_class_initfn(ObjectClass *klass, void *data)
91 {
92     DeviceClass *dc = DEVICE_CLASS(klass);
93 
94     dc->realize = isa_ne2000_realizefn;
95     device_class_set_props(dc, ne2000_isa_properties);
96     dc->vmsd = &vmstate_isa_ne2000;
97     set_bit(DEVICE_CATEGORY_NETWORK, dc->categories);
98 }
99 
100 static void isa_ne2000_get_bootindex(Object *obj, Visitor *v,
101                                      const char *name, void *opaque,
102                                      Error **errp)
103 {
104     ISANE2000State *isa = ISA_NE2000(obj);
105     NE2000State *s = &isa->ne2000;
106 
107     visit_type_int32(v, name, &s->c.bootindex, errp);
108 }
109 
110 static void isa_ne2000_set_bootindex(Object *obj, Visitor *v,
111                                      const char *name, void *opaque,
112                                      Error **errp)
113 {
114     ISANE2000State *isa = ISA_NE2000(obj);
115     NE2000State *s = &isa->ne2000;
116     int32_t boot_index;
117     Error *local_err = NULL;
118 
119     if (!visit_type_int32(v, name, &boot_index, errp)) {
120         return;
121     }
122     /* check whether bootindex is present in fw_boot_order list  */
123     check_boot_index(boot_index, &local_err);
124     if (local_err) {
125         goto out;
126     }
127     /* change bootindex to a new one */
128     s->c.bootindex = boot_index;
129 
130 out:
131     error_propagate(errp, local_err);
132 }
133 
134 static void isa_ne2000_instance_init(Object *obj)
135 {
136     object_property_add(obj, "bootindex", "int32",
137                         isa_ne2000_get_bootindex,
138                         isa_ne2000_set_bootindex, NULL, NULL);
139     object_property_set_int(obj, "bootindex", -1, NULL);
140 }
141 static const TypeInfo ne2000_isa_info = {
142     .name          = TYPE_ISA_NE2000,
143     .parent        = TYPE_ISA_DEVICE,
144     .instance_size = sizeof(ISANE2000State),
145     .class_init    = isa_ne2000_class_initfn,
146     .instance_init = isa_ne2000_instance_init,
147 };
148 
149 static void ne2000_isa_register_types(void)
150 {
151     type_register_static(&ne2000_isa_info);
152 }
153 
154 type_init(ne2000_isa_register_types)
155