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