xref: /openbmc/qemu/hw/net/ne2000-pci.c (revision b14df228)
1 /*
2  * QEMU NE2000 emulation (PCI bus)
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/irq.h"
27 #include "hw/pci/pci.h"
28 #include "hw/qdev-properties.h"
29 #include "migration/vmstate.h"
30 #include "ne2000.h"
31 #include "sysemu/sysemu.h"
32 
33 typedef struct PCINE2000State {
34     PCIDevice dev;
35     NE2000State ne2000;
36 } PCINE2000State;
37 
38 static const VMStateDescription vmstate_pci_ne2000 = {
39     .name = "ne2000",
40     .version_id = 3,
41     .minimum_version_id = 3,
42     .fields = (VMStateField[]) {
43         VMSTATE_PCI_DEVICE(dev, PCINE2000State),
44         VMSTATE_STRUCT(ne2000, PCINE2000State, 0, vmstate_ne2000, NE2000State),
45         VMSTATE_END_OF_LIST()
46     }
47 };
48 
49 static NetClientInfo net_ne2000_info = {
50     .type = NET_CLIENT_DRIVER_NIC,
51     .size = sizeof(NICState),
52     .receive = ne2000_receive,
53 };
54 
55 static void pci_ne2000_realize(PCIDevice *pci_dev, Error **errp)
56 {
57     PCINE2000State *d = DO_UPCAST(PCINE2000State, dev, pci_dev);
58     NE2000State *s;
59     uint8_t *pci_conf;
60 
61     pci_conf = d->dev.config;
62     pci_conf[PCI_INTERRUPT_PIN] = 1; /* interrupt pin A */
63 
64     s = &d->ne2000;
65     ne2000_setup_io(s, DEVICE(pci_dev), 0x100);
66     pci_register_bar(&d->dev, 0, PCI_BASE_ADDRESS_SPACE_IO, &s->io);
67     s->irq = pci_allocate_irq(&d->dev);
68 
69     qemu_macaddr_default_if_unset(&s->c.macaddr);
70     ne2000_reset(s);
71 
72     s->nic = qemu_new_nic(&net_ne2000_info, &s->c,
73                           object_get_typename(OBJECT(pci_dev)),
74                           pci_dev->qdev.id, s);
75     qemu_format_nic_info_str(qemu_get_queue(s->nic), s->c.macaddr.a);
76 }
77 
78 static void pci_ne2000_exit(PCIDevice *pci_dev)
79 {
80     PCINE2000State *d = DO_UPCAST(PCINE2000State, dev, pci_dev);
81     NE2000State *s = &d->ne2000;
82 
83     qemu_del_nic(s->nic);
84     qemu_free_irq(s->irq);
85 }
86 
87 static void ne2000_instance_init(Object *obj)
88 {
89     PCIDevice *pci_dev = PCI_DEVICE(obj);
90     PCINE2000State *d = DO_UPCAST(PCINE2000State, dev, pci_dev);
91     NE2000State *s = &d->ne2000;
92 
93     device_add_bootindex_property(obj, &s->c.bootindex,
94                                   "bootindex", "/ethernet-phy@0",
95                                   &pci_dev->qdev);
96 }
97 
98 static Property ne2000_properties[] = {
99     DEFINE_NIC_PROPERTIES(PCINE2000State, ne2000.c),
100     DEFINE_PROP_END_OF_LIST(),
101 };
102 
103 static void ne2000_class_init(ObjectClass *klass, void *data)
104 {
105     DeviceClass *dc = DEVICE_CLASS(klass);
106     PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
107 
108     k->realize = pci_ne2000_realize;
109     k->exit = pci_ne2000_exit;
110     k->romfile = "efi-ne2k_pci.rom",
111     k->vendor_id = PCI_VENDOR_ID_REALTEK;
112     k->device_id = PCI_DEVICE_ID_REALTEK_8029;
113     k->class_id = PCI_CLASS_NETWORK_ETHERNET;
114     dc->vmsd = &vmstate_pci_ne2000;
115     device_class_set_props(dc, ne2000_properties);
116     set_bit(DEVICE_CATEGORY_NETWORK, dc->categories);
117 }
118 
119 static const TypeInfo ne2000_info = {
120     .name          = "ne2k_pci",
121     .parent        = TYPE_PCI_DEVICE,
122     .instance_size = sizeof(PCINE2000State),
123     .class_init    = ne2000_class_init,
124     .instance_init = ne2000_instance_init,
125     .interfaces = (InterfaceInfo[]) {
126         { INTERFACE_CONVENTIONAL_PCI_DEVICE },
127         { },
128     },
129 };
130 
131 static void ne2000_register_types(void)
132 {
133     type_register_static(&ne2000_info);
134 }
135 
136 type_init(ne2000_register_types)
137