blob: 18b064463afec01c2677c8a10dfd47537fdb4cf7 [file] [log] [blame]
Gerd Hoffmann9453c5b2009-09-10 11:43:33 +02001/*
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 */
Paolo Bonzini83c9f4c2013-02-04 15:40:22 +010024#include "hw/hw.h"
Paolo Bonzini0d09e412013-02-05 17:06:20 +010025#include "hw/i386/pc.h"
26#include "hw/isa/isa.h"
Paolo Bonzini83c9f4c2013-02-04 15:40:22 +010027#include "hw/qdev.h"
Paolo Bonzini1422e322012-10-24 08:43:34 +020028#include "net/net.h"
Paolo Bonzini47b43a12013-03-18 17:36:02 +010029#include "ne2000.h"
Paolo Bonzini022c62c2012-12-17 18:19:49 +010030#include "exec/address-spaces.h"
Gonglei6cb08512014-10-07 16:00:15 +080031#include "qapi/visitor.h"
Gerd Hoffmann9453c5b2009-09-10 11:43:33 +020032
Andreas Färberfe6f5de2013-04-27 22:18:44 +020033#define TYPE_ISA_NE2000 "ne2k_isa"
34#define ISA_NE2000(obj) OBJECT_CHECK(ISANE2000State, (obj), TYPE_ISA_NE2000)
35
Gerd Hoffmann9453c5b2009-09-10 11:43:33 +020036typedef struct ISANE2000State {
Andreas Färberfe6f5de2013-04-27 22:18:44 +020037 ISADevice parent_obj;
38
Gerd Hoffmann9453c5b2009-09-10 11:43:33 +020039 uint32_t iobase;
40 uint32_t isairq;
41 NE2000State ne2000;
42} ISANE2000State;
43
Mark McLoughlin1c2045b2009-11-25 18:49:14 +000044static NetClientInfo net_ne2000_isa_info = {
Laszlo Ersek2be64a62012-07-17 16:17:12 +020045 .type = NET_CLIENT_OPTIONS_KIND_NIC,
Mark McLoughlin1c2045b2009-11-25 18:49:14 +000046 .size = sizeof(NICState),
Mark McLoughlin1c2045b2009-11-25 18:49:14 +000047 .receive = ne2000_receive,
Mark McLoughlin1c2045b2009-11-25 18:49:14 +000048};
49
Blue Swirld05ac8f2009-12-04 20:44:44 +000050static const VMStateDescription vmstate_isa_ne2000 = {
Juan Quintelabe73cfe2009-12-02 12:36:46 +010051 .name = "ne2000",
52 .version_id = 2,
53 .minimum_version_id = 0,
Juan Quintelad49805a2014-04-16 15:32:32 +020054 .fields = (VMStateField[]) {
Juan Quintelabe73cfe2009-12-02 12:36:46 +010055 VMSTATE_STRUCT(ne2000, ISANE2000State, 0, vmstate_ne2000, NE2000State),
56 VMSTATE_END_OF_LIST()
57 }
58};
59
Andreas Färberdb895a12012-11-25 02:37:14 +010060static void isa_ne2000_realizefn(DeviceState *dev, Error **errp)
Gerd Hoffmann9453c5b2009-09-10 11:43:33 +020061{
Andreas Färberdb895a12012-11-25 02:37:14 +010062 ISADevice *isadev = ISA_DEVICE(dev);
Andreas Färberfe6f5de2013-04-27 22:18:44 +020063 ISANE2000State *isa = ISA_NE2000(dev);
Gerd Hoffmann9453c5b2009-09-10 11:43:33 +020064 NE2000State *s = &isa->ne2000;
65
Paolo Bonzinidcb117b2013-06-25 15:04:35 +020066 ne2000_setup_io(s, DEVICE(isadev), 0x20);
Andreas Färberdb895a12012-11-25 02:37:14 +010067 isa_register_ioport(isadev, &s->io, isa->iobase);
Gerd Hoffmann9453c5b2009-09-10 11:43:33 +020068
Andreas Färberdb895a12012-11-25 02:37:14 +010069 isa_init_irq(isadev, &s->irq, isa->isairq);
Gerd Hoffmann9453c5b2009-09-10 11:43:33 +020070
Gerd Hoffmann93db6682009-10-21 15:25:27 +020071 qemu_macaddr_default_if_unset(&s->c.macaddr);
Gerd Hoffmann9453c5b2009-09-10 11:43:33 +020072 ne2000_reset(s);
73
Mark McLoughlin1c2045b2009-11-25 18:49:14 +000074 s->nic = qemu_new_nic(&net_ne2000_isa_info, &s->c,
Andreas Färberdb895a12012-11-25 02:37:14 +010075 object_get_typename(OBJECT(dev)), dev->id, s);
Jason Wangb356f762013-01-30 19:12:22 +080076 qemu_format_nic_info_str(qemu_get_queue(s->nic), s->c.macaddr.a);
Gerd Hoffmann9453c5b2009-09-10 11:43:33 +020077}
78
Anthony Liguori39bffca2011-12-07 21:34:16 -060079static Property ne2000_isa_properties[] = {
Paolo Bonzinic7bcc852014-02-08 11:01:53 +010080 DEFINE_PROP_UINT32("iobase", ISANE2000State, iobase, 0x300),
Anthony Liguori39bffca2011-12-07 21:34:16 -060081 DEFINE_PROP_UINT32("irq", ISANE2000State, isairq, 9),
82 DEFINE_NIC_PROPERTIES(ISANE2000State, ne2000.c),
83 DEFINE_PROP_END_OF_LIST(),
84};
85
Anthony Liguori8f04ee02011-12-04 11:52:49 -060086static void isa_ne2000_class_initfn(ObjectClass *klass, void *data)
87{
Anthony Liguori39bffca2011-12-07 21:34:16 -060088 DeviceClass *dc = DEVICE_CLASS(klass);
Andreas Färberdb895a12012-11-25 02:37:14 +010089
90 dc->realize = isa_ne2000_realizefn;
Anthony Liguori39bffca2011-12-07 21:34:16 -060091 dc->props = ne2000_isa_properties;
Peter Maydell89218c22014-06-07 17:53:11 +010092 dc->vmsd = &vmstate_isa_ne2000;
Marcel Apfelbaum125ee0e2013-07-29 17:17:45 +030093 set_bit(DEVICE_CATEGORY_NETWORK, dc->categories);
Anthony Liguori8f04ee02011-12-04 11:52:49 -060094}
95
Gonglei6cb08512014-10-07 16:00:15 +080096static void isa_ne2000_get_bootindex(Object *obj, Visitor *v, void *opaque,
97 const char *name, Error **errp)
98{
99 ISANE2000State *isa = ISA_NE2000(obj);
100 NE2000State *s = &isa->ne2000;
101
102 visit_type_int32(v, &s->c.bootindex, name, errp);
103}
104
105static void isa_ne2000_set_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 int32_t boot_index;
111 Error *local_err = NULL;
112
113 visit_type_int32(v, &boot_index, name, &local_err);
114 if (local_err) {
115 goto out;
116 }
117 /* check whether bootindex is present in fw_boot_order list */
118 check_boot_index(boot_index, &local_err);
119 if (local_err) {
120 goto out;
121 }
122 /* change bootindex to a new one */
123 s->c.bootindex = boot_index;
124
125out:
126 if (local_err) {
127 error_propagate(errp, local_err);
128 }
129}
130
131static void isa_ne2000_instance_init(Object *obj)
132{
133 object_property_add(obj, "bootindex", "int32",
134 isa_ne2000_get_bootindex,
135 isa_ne2000_set_bootindex, NULL, NULL, NULL);
136 object_property_set_int(obj, -1, "bootindex", NULL);
137}
Andreas Färber8c43a6f2013-01-10 16:19:07 +0100138static const TypeInfo ne2000_isa_info = {
Andreas Färberfe6f5de2013-04-27 22:18:44 +0200139 .name = TYPE_ISA_NE2000,
Anthony Liguori39bffca2011-12-07 21:34:16 -0600140 .parent = TYPE_ISA_DEVICE,
141 .instance_size = sizeof(ISANE2000State),
142 .class_init = isa_ne2000_class_initfn,
Gonglei6cb08512014-10-07 16:00:15 +0800143 .instance_init = isa_ne2000_instance_init,
Gerd Hoffmann9453c5b2009-09-10 11:43:33 +0200144};
145
Andreas Färber83f7d432012-02-09 15:20:55 +0100146static void ne2000_isa_register_types(void)
Gerd Hoffmann9453c5b2009-09-10 11:43:33 +0200147{
Anthony Liguori39bffca2011-12-07 21:34:16 -0600148 type_register_static(&ne2000_isa_info);
Gerd Hoffmann9453c5b2009-09-10 11:43:33 +0200149}
150
Andreas Färber83f7d432012-02-09 15:20:55 +0100151type_init(ne2000_isa_register_types)