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