xref: /openbmc/qemu/hw/net/ne2000-isa.c (revision 228aa992)
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 #include "hw/hw.h"
25 #include "hw/i386/pc.h"
26 #include "hw/isa/isa.h"
27 #include "hw/qdev.h"
28 #include "net/net.h"
29 #include "ne2000.h"
30 #include "exec/address-spaces.h"
31 #include "qapi/visitor.h"
32 
33 #define TYPE_ISA_NE2000 "ne2k_isa"
34 #define ISA_NE2000(obj) OBJECT_CHECK(ISANE2000State, (obj), TYPE_ISA_NE2000)
35 
36 typedef struct ISANE2000State {
37     ISADevice parent_obj;
38 
39     uint32_t iobase;
40     uint32_t isairq;
41     NE2000State ne2000;
42 } ISANE2000State;
43 
44 static void isa_ne2000_cleanup(NetClientState *nc)
45 {
46     NE2000State *s = qemu_get_nic_opaque(nc);
47 
48     s->nic = NULL;
49 }
50 
51 static NetClientInfo net_ne2000_isa_info = {
52     .type = NET_CLIENT_OPTIONS_KIND_NIC,
53     .size = sizeof(NICState),
54     .can_receive = ne2000_can_receive,
55     .receive = ne2000_receive,
56     .cleanup = isa_ne2000_cleanup,
57 };
58 
59 static const VMStateDescription vmstate_isa_ne2000 = {
60     .name = "ne2000",
61     .version_id = 2,
62     .minimum_version_id = 0,
63     .fields = (VMStateField[]) {
64         VMSTATE_STRUCT(ne2000, ISANE2000State, 0, vmstate_ne2000, NE2000State),
65         VMSTATE_END_OF_LIST()
66     }
67 };
68 
69 static void isa_ne2000_realizefn(DeviceState *dev, Error **errp)
70 {
71     ISADevice *isadev = ISA_DEVICE(dev);
72     ISANE2000State *isa = ISA_NE2000(dev);
73     NE2000State *s = &isa->ne2000;
74 
75     ne2000_setup_io(s, DEVICE(isadev), 0x20);
76     isa_register_ioport(isadev, &s->io, isa->iobase);
77 
78     isa_init_irq(isadev, &s->irq, isa->isairq);
79 
80     qemu_macaddr_default_if_unset(&s->c.macaddr);
81     ne2000_reset(s);
82 
83     s->nic = qemu_new_nic(&net_ne2000_isa_info, &s->c,
84                           object_get_typename(OBJECT(dev)), dev->id, s);
85     qemu_format_nic_info_str(qemu_get_queue(s->nic), s->c.macaddr.a);
86 }
87 
88 static Property ne2000_isa_properties[] = {
89     DEFINE_PROP_UINT32("iobase", ISANE2000State, iobase, 0x300),
90     DEFINE_PROP_UINT32("irq",   ISANE2000State, isairq, 9),
91     DEFINE_NIC_PROPERTIES(ISANE2000State, ne2000.c),
92     DEFINE_PROP_END_OF_LIST(),
93 };
94 
95 static void isa_ne2000_class_initfn(ObjectClass *klass, void *data)
96 {
97     DeviceClass *dc = DEVICE_CLASS(klass);
98 
99     dc->realize = isa_ne2000_realizefn;
100     dc->props = ne2000_isa_properties;
101     dc->vmsd = &vmstate_isa_ne2000;
102     set_bit(DEVICE_CATEGORY_NETWORK, dc->categories);
103 }
104 
105 static void isa_ne2000_get_bootindex(Object *obj, Visitor *v, void *opaque,
106                                      const char *name, Error **errp)
107 {
108     ISANE2000State *isa = ISA_NE2000(obj);
109     NE2000State *s = &isa->ne2000;
110 
111     visit_type_int32(v, &s->c.bootindex, name, errp);
112 }
113 
114 static void isa_ne2000_set_bootindex(Object *obj, Visitor *v, void *opaque,
115                                      const char *name, Error **errp)
116 {
117     ISANE2000State *isa = ISA_NE2000(obj);
118     NE2000State *s = &isa->ne2000;
119     int32_t boot_index;
120     Error *local_err = NULL;
121 
122     visit_type_int32(v, &boot_index, name, &local_err);
123     if (local_err) {
124         goto out;
125     }
126     /* check whether bootindex is present in fw_boot_order list  */
127     check_boot_index(boot_index, &local_err);
128     if (local_err) {
129         goto out;
130     }
131     /* change bootindex to a new one */
132     s->c.bootindex = boot_index;
133 
134 out:
135     if (local_err) {
136         error_propagate(errp, local_err);
137     }
138 }
139 
140 static void isa_ne2000_instance_init(Object *obj)
141 {
142     object_property_add(obj, "bootindex", "int32",
143                         isa_ne2000_get_bootindex,
144                         isa_ne2000_set_bootindex, NULL, NULL, NULL);
145     object_property_set_int(obj, -1, "bootindex", NULL);
146 }
147 static const TypeInfo ne2000_isa_info = {
148     .name          = TYPE_ISA_NE2000,
149     .parent        = TYPE_ISA_DEVICE,
150     .instance_size = sizeof(ISANE2000State),
151     .class_init    = isa_ne2000_class_initfn,
152     .instance_init = isa_ne2000_instance_init,
153 };
154 
155 static void ne2000_isa_register_types(void)
156 {
157     type_register_static(&ne2000_isa_info);
158 }
159 
160 type_init(ne2000_isa_register_types)
161