Gerd Hoffmann | 9453c5b | 2009-09-10 11:43:33 +0200 | [diff] [blame] | 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 | */ |
Peter Maydell | e8d4046 | 2016-01-26 18:17:11 +0000 | [diff] [blame] | 24 | #include "qemu/osdep.h" |
Paolo Bonzini | 83c9f4c | 2013-02-04 15:40:22 +0100 | [diff] [blame] | 25 | #include "hw/hw.h" |
Paolo Bonzini | 0d09e41 | 2013-02-05 17:06:20 +0100 | [diff] [blame] | 26 | #include "hw/i386/pc.h" |
| 27 | #include "hw/isa/isa.h" |
Paolo Bonzini | 83c9f4c | 2013-02-04 15:40:22 +0100 | [diff] [blame] | 28 | #include "hw/qdev.h" |
Paolo Bonzini | 1422e32 | 2012-10-24 08:43:34 +0200 | [diff] [blame] | 29 | #include "net/net.h" |
Paolo Bonzini | 47b43a1 | 2013-03-18 17:36:02 +0100 | [diff] [blame] | 30 | #include "ne2000.h" |
Paolo Bonzini | 022c62c | 2012-12-17 18:19:49 +0100 | [diff] [blame] | 31 | #include "exec/address-spaces.h" |
Markus Armbruster | da34e65 | 2016-03-14 09:01:28 +0100 | [diff] [blame] | 32 | #include "qapi/error.h" |
Gonglei | 6cb0851 | 2014-10-07 16:00:15 +0800 | [diff] [blame] | 33 | #include "qapi/visitor.h" |
Gerd Hoffmann | 9453c5b | 2009-09-10 11:43:33 +0200 | [diff] [blame] | 34 | |
Andreas Färber | fe6f5de | 2013-04-27 22:18:44 +0200 | [diff] [blame] | 35 | #define TYPE_ISA_NE2000 "ne2k_isa" |
| 36 | #define ISA_NE2000(obj) OBJECT_CHECK(ISANE2000State, (obj), TYPE_ISA_NE2000) |
| 37 | |
Gerd Hoffmann | 9453c5b | 2009-09-10 11:43:33 +0200 | [diff] [blame] | 38 | typedef struct ISANE2000State { |
Andreas Färber | fe6f5de | 2013-04-27 22:18:44 +0200 | [diff] [blame] | 39 | ISADevice parent_obj; |
| 40 | |
Gerd Hoffmann | 9453c5b | 2009-09-10 11:43:33 +0200 | [diff] [blame] | 41 | uint32_t iobase; |
| 42 | uint32_t isairq; |
| 43 | NE2000State ne2000; |
| 44 | } ISANE2000State; |
| 45 | |
Mark McLoughlin | 1c2045b | 2009-11-25 18:49:14 +0000 | [diff] [blame] | 46 | static NetClientInfo net_ne2000_isa_info = { |
Eric Blake | f394b2e | 2016-07-13 21:50:23 -0600 | [diff] [blame] | 47 | .type = NET_CLIENT_DRIVER_NIC, |
Mark McLoughlin | 1c2045b | 2009-11-25 18:49:14 +0000 | [diff] [blame] | 48 | .size = sizeof(NICState), |
Mark McLoughlin | 1c2045b | 2009-11-25 18:49:14 +0000 | [diff] [blame] | 49 | .receive = ne2000_receive, |
Mark McLoughlin | 1c2045b | 2009-11-25 18:49:14 +0000 | [diff] [blame] | 50 | }; |
| 51 | |
Blue Swirl | d05ac8f | 2009-12-04 20:44:44 +0000 | [diff] [blame] | 52 | static const VMStateDescription vmstate_isa_ne2000 = { |
Juan Quintela | be73cfe | 2009-12-02 12:36:46 +0100 | [diff] [blame] | 53 | .name = "ne2000", |
| 54 | .version_id = 2, |
| 55 | .minimum_version_id = 0, |
Juan Quintela | d49805a | 2014-04-16 15:32:32 +0200 | [diff] [blame] | 56 | .fields = (VMStateField[]) { |
Juan Quintela | be73cfe | 2009-12-02 12:36:46 +0100 | [diff] [blame] | 57 | VMSTATE_STRUCT(ne2000, ISANE2000State, 0, vmstate_ne2000, NE2000State), |
| 58 | VMSTATE_END_OF_LIST() |
| 59 | } |
| 60 | }; |
| 61 | |
Andreas Färber | db895a1 | 2012-11-25 02:37:14 +0100 | [diff] [blame] | 62 | static void isa_ne2000_realizefn(DeviceState *dev, Error **errp) |
Gerd Hoffmann | 9453c5b | 2009-09-10 11:43:33 +0200 | [diff] [blame] | 63 | { |
Andreas Färber | db895a1 | 2012-11-25 02:37:14 +0100 | [diff] [blame] | 64 | ISADevice *isadev = ISA_DEVICE(dev); |
Andreas Färber | fe6f5de | 2013-04-27 22:18:44 +0200 | [diff] [blame] | 65 | ISANE2000State *isa = ISA_NE2000(dev); |
Gerd Hoffmann | 9453c5b | 2009-09-10 11:43:33 +0200 | [diff] [blame] | 66 | NE2000State *s = &isa->ne2000; |
| 67 | |
Paolo Bonzini | dcb117b | 2013-06-25 15:04:35 +0200 | [diff] [blame] | 68 | ne2000_setup_io(s, DEVICE(isadev), 0x20); |
Andreas Färber | db895a1 | 2012-11-25 02:37:14 +0100 | [diff] [blame] | 69 | isa_register_ioport(isadev, &s->io, isa->iobase); |
Gerd Hoffmann | 9453c5b | 2009-09-10 11:43:33 +0200 | [diff] [blame] | 70 | |
Andreas Färber | db895a1 | 2012-11-25 02:37:14 +0100 | [diff] [blame] | 71 | isa_init_irq(isadev, &s->irq, isa->isairq); |
Gerd Hoffmann | 9453c5b | 2009-09-10 11:43:33 +0200 | [diff] [blame] | 72 | |
Gerd Hoffmann | 93db668 | 2009-10-21 15:25:27 +0200 | [diff] [blame] | 73 | qemu_macaddr_default_if_unset(&s->c.macaddr); |
Gerd Hoffmann | 9453c5b | 2009-09-10 11:43:33 +0200 | [diff] [blame] | 74 | ne2000_reset(s); |
| 75 | |
Mark McLoughlin | 1c2045b | 2009-11-25 18:49:14 +0000 | [diff] [blame] | 76 | s->nic = qemu_new_nic(&net_ne2000_isa_info, &s->c, |
Andreas Färber | db895a1 | 2012-11-25 02:37:14 +0100 | [diff] [blame] | 77 | object_get_typename(OBJECT(dev)), dev->id, s); |
Jason Wang | b356f76 | 2013-01-30 19:12:22 +0800 | [diff] [blame] | 78 | qemu_format_nic_info_str(qemu_get_queue(s->nic), s->c.macaddr.a); |
Gerd Hoffmann | 9453c5b | 2009-09-10 11:43:33 +0200 | [diff] [blame] | 79 | } |
| 80 | |
Anthony Liguori | 39bffca | 2011-12-07 21:34:16 -0600 | [diff] [blame] | 81 | static Property ne2000_isa_properties[] = { |
Paolo Bonzini | c7bcc85 | 2014-02-08 11:01:53 +0100 | [diff] [blame] | 82 | DEFINE_PROP_UINT32("iobase", ISANE2000State, iobase, 0x300), |
Anthony Liguori | 39bffca | 2011-12-07 21:34:16 -0600 | [diff] [blame] | 83 | DEFINE_PROP_UINT32("irq", ISANE2000State, isairq, 9), |
| 84 | DEFINE_NIC_PROPERTIES(ISANE2000State, ne2000.c), |
| 85 | DEFINE_PROP_END_OF_LIST(), |
| 86 | }; |
| 87 | |
Anthony Liguori | 8f04ee0 | 2011-12-04 11:52:49 -0600 | [diff] [blame] | 88 | static void isa_ne2000_class_initfn(ObjectClass *klass, void *data) |
| 89 | { |
Anthony Liguori | 39bffca | 2011-12-07 21:34:16 -0600 | [diff] [blame] | 90 | DeviceClass *dc = DEVICE_CLASS(klass); |
Andreas Färber | db895a1 | 2012-11-25 02:37:14 +0100 | [diff] [blame] | 91 | |
| 92 | dc->realize = isa_ne2000_realizefn; |
Anthony Liguori | 39bffca | 2011-12-07 21:34:16 -0600 | [diff] [blame] | 93 | dc->props = ne2000_isa_properties; |
Peter Maydell | 89218c2 | 2014-06-07 17:53:11 +0100 | [diff] [blame] | 94 | dc->vmsd = &vmstate_isa_ne2000; |
Marcel Apfelbaum | 125ee0e | 2013-07-29 17:17:45 +0300 | [diff] [blame] | 95 | set_bit(DEVICE_CATEGORY_NETWORK, dc->categories); |
Anthony Liguori | 8f04ee0 | 2011-12-04 11:52:49 -0600 | [diff] [blame] | 96 | } |
| 97 | |
Eric Blake | d7bce99 | 2016-01-29 06:48:55 -0700 | [diff] [blame] | 98 | static void isa_ne2000_get_bootindex(Object *obj, Visitor *v, |
| 99 | const char *name, void *opaque, |
| 100 | Error **errp) |
Gonglei | 6cb0851 | 2014-10-07 16:00:15 +0800 | [diff] [blame] | 101 | { |
| 102 | ISANE2000State *isa = ISA_NE2000(obj); |
| 103 | NE2000State *s = &isa->ne2000; |
| 104 | |
Eric Blake | 51e72bc | 2016-01-29 06:48:54 -0700 | [diff] [blame] | 105 | visit_type_int32(v, name, &s->c.bootindex, errp); |
Gonglei | 6cb0851 | 2014-10-07 16:00:15 +0800 | [diff] [blame] | 106 | } |
| 107 | |
Eric Blake | d7bce99 | 2016-01-29 06:48:55 -0700 | [diff] [blame] | 108 | static void isa_ne2000_set_bootindex(Object *obj, Visitor *v, |
| 109 | const char *name, void *opaque, |
| 110 | Error **errp) |
Gonglei | 6cb0851 | 2014-10-07 16:00:15 +0800 | [diff] [blame] | 111 | { |
| 112 | ISANE2000State *isa = ISA_NE2000(obj); |
| 113 | NE2000State *s = &isa->ne2000; |
| 114 | int32_t boot_index; |
| 115 | Error *local_err = NULL; |
| 116 | |
Eric Blake | 51e72bc | 2016-01-29 06:48:54 -0700 | [diff] [blame] | 117 | visit_type_int32(v, name, &boot_index, &local_err); |
Gonglei | 6cb0851 | 2014-10-07 16:00:15 +0800 | [diff] [blame] | 118 | if (local_err) { |
| 119 | goto out; |
| 120 | } |
| 121 | /* check whether bootindex is present in fw_boot_order list */ |
| 122 | check_boot_index(boot_index, &local_err); |
| 123 | if (local_err) { |
| 124 | goto out; |
| 125 | } |
| 126 | /* change bootindex to a new one */ |
| 127 | s->c.bootindex = boot_index; |
| 128 | |
| 129 | out: |
Eduardo Habkost | 621ff94 | 2016-06-13 18:57:56 -0300 | [diff] [blame] | 130 | error_propagate(errp, local_err); |
Gonglei | 6cb0851 | 2014-10-07 16:00:15 +0800 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | static void isa_ne2000_instance_init(Object *obj) |
| 134 | { |
| 135 | object_property_add(obj, "bootindex", "int32", |
| 136 | isa_ne2000_get_bootindex, |
| 137 | isa_ne2000_set_bootindex, NULL, NULL, NULL); |
| 138 | object_property_set_int(obj, -1, "bootindex", NULL); |
| 139 | } |
Andreas Färber | 8c43a6f | 2013-01-10 16:19:07 +0100 | [diff] [blame] | 140 | static const TypeInfo ne2000_isa_info = { |
Andreas Färber | fe6f5de | 2013-04-27 22:18:44 +0200 | [diff] [blame] | 141 | .name = TYPE_ISA_NE2000, |
Anthony Liguori | 39bffca | 2011-12-07 21:34:16 -0600 | [diff] [blame] | 142 | .parent = TYPE_ISA_DEVICE, |
| 143 | .instance_size = sizeof(ISANE2000State), |
| 144 | .class_init = isa_ne2000_class_initfn, |
Gonglei | 6cb0851 | 2014-10-07 16:00:15 +0800 | [diff] [blame] | 145 | .instance_init = isa_ne2000_instance_init, |
Gerd Hoffmann | 9453c5b | 2009-09-10 11:43:33 +0200 | [diff] [blame] | 146 | }; |
| 147 | |
Andreas Färber | 83f7d43 | 2012-02-09 15:20:55 +0100 | [diff] [blame] | 148 | static void ne2000_isa_register_types(void) |
Gerd Hoffmann | 9453c5b | 2009-09-10 11:43:33 +0200 | [diff] [blame] | 149 | { |
Anthony Liguori | 39bffca | 2011-12-07 21:34:16 -0600 | [diff] [blame] | 150 | type_register_static(&ne2000_isa_info); |
Gerd Hoffmann | 9453c5b | 2009-09-10 11:43:33 +0200 | [diff] [blame] | 151 | } |
| 152 | |
Andreas Färber | 83f7d43 | 2012-02-09 15:20:55 +0100 | [diff] [blame] | 153 | type_init(ne2000_isa_register_types) |