xref: /openbmc/qemu/hw/virtio/virtio-net-pci.c (revision 5e25c93ccb8ddc8dda6845d6c09334ca44cbef17)
1cad3cd79SJuan Quintela /*
2cad3cd79SJuan Quintela  * Virtio net PCI Bindings
3cad3cd79SJuan Quintela  *
4cad3cd79SJuan Quintela  * Copyright IBM, Corp. 2007
5cad3cd79SJuan Quintela  * Copyright (c) 2009 CodeSourcery
6cad3cd79SJuan Quintela  *
7cad3cd79SJuan Quintela  * Authors:
8cad3cd79SJuan Quintela  *  Anthony Liguori   <aliguori@us.ibm.com>
9cad3cd79SJuan Quintela  *  Paul Brook        <paul@codesourcery.com>
10cad3cd79SJuan Quintela  *
11cad3cd79SJuan Quintela  * This work is licensed under the terms of the GNU GPL, version 2.  See
12cad3cd79SJuan Quintela  * the COPYING file in the top-level directory.
13cad3cd79SJuan Quintela  *
14cad3cd79SJuan Quintela  * Contributions after 2012-01-13 are licensed under the terms of the
15cad3cd79SJuan Quintela  * GNU GPL, version 2 or (at your option) any later version.
16cad3cd79SJuan Quintela  */
17cad3cd79SJuan Quintela 
18cad3cd79SJuan Quintela #include "qemu/osdep.h"
19cad3cd79SJuan Quintela 
20a27bd6c7SMarkus Armbruster #include "hw/qdev-properties.h"
21cad3cd79SJuan Quintela #include "hw/virtio/virtio-net.h"
22*e1b1f534SAlex Bennée #include "hw/virtio/virtio-pci.h"
23cad3cd79SJuan Quintela #include "qapi/error.h"
240b8fa32fSMarkus Armbruster #include "qemu/module.h"
25db1015e9SEduardo Habkost #include "qom/object.h"
26cad3cd79SJuan Quintela 
27cad3cd79SJuan Quintela typedef struct VirtIONetPCI VirtIONetPCI;
28cad3cd79SJuan Quintela 
29cad3cd79SJuan Quintela /*
30cad3cd79SJuan Quintela  * virtio-net-pci: This extends VirtioPCIProxy.
31cad3cd79SJuan Quintela  */
32cad3cd79SJuan Quintela #define TYPE_VIRTIO_NET_PCI "virtio-net-pci-base"
338110fa1dSEduardo Habkost DECLARE_INSTANCE_CHECKER(VirtIONetPCI, VIRTIO_NET_PCI,
348110fa1dSEduardo Habkost                          TYPE_VIRTIO_NET_PCI)
35cad3cd79SJuan Quintela 
36cad3cd79SJuan Quintela struct VirtIONetPCI {
37cad3cd79SJuan Quintela     VirtIOPCIProxy parent_obj;
38cad3cd79SJuan Quintela     VirtIONet vdev;
39cad3cd79SJuan Quintela };
40cad3cd79SJuan Quintela 
41cad3cd79SJuan Quintela static Property virtio_net_properties[] = {
42cad3cd79SJuan Quintela     DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags,
43cad3cd79SJuan Quintela                     VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true),
4451a81a21SJason Wang     DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors,
4551a81a21SJason Wang                        DEV_NVECTORS_UNSPECIFIED),
46cad3cd79SJuan Quintela     DEFINE_PROP_END_OF_LIST(),
47cad3cd79SJuan Quintela };
48cad3cd79SJuan Quintela 
virtio_net_pci_realize(VirtIOPCIProxy * vpci_dev,Error ** errp)49cad3cd79SJuan Quintela static void virtio_net_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
50cad3cd79SJuan Quintela {
51cad3cd79SJuan Quintela     DeviceState *qdev = DEVICE(vpci_dev);
52cad3cd79SJuan Quintela     VirtIONetPCI *dev = VIRTIO_NET_PCI(vpci_dev);
53cad3cd79SJuan Quintela     DeviceState *vdev = DEVICE(&dev->vdev);
5451a81a21SJason Wang     VirtIONet *net = VIRTIO_NET(vdev);
5551a81a21SJason Wang 
5651a81a21SJason Wang     if (vpci_dev->nvectors == DEV_NVECTORS_UNSPECIFIED) {
5751a81a21SJason Wang         vpci_dev->nvectors = 2 * MAX(net->nic_conf.peers.queues, 1)
5851a81a21SJason Wang             + 1 /* Config interrupt */
5951a81a21SJason Wang             + 1 /* Control vq */;
6051a81a21SJason Wang     }
61cad3cd79SJuan Quintela 
62cad3cd79SJuan Quintela     virtio_net_set_netclient_name(&dev->vdev, qdev->id,
63cad3cd79SJuan Quintela                                   object_get_typename(OBJECT(qdev)));
6499ba777eSMarkus Armbruster     qdev_realize(vdev, BUS(&vpci_dev->bus), errp);
65cad3cd79SJuan Quintela }
66cad3cd79SJuan Quintela 
virtio_net_pci_class_init(ObjectClass * klass,void * data)67cad3cd79SJuan Quintela static void virtio_net_pci_class_init(ObjectClass *klass, void *data)
68cad3cd79SJuan Quintela {
69cad3cd79SJuan Quintela     DeviceClass *dc = DEVICE_CLASS(klass);
70cad3cd79SJuan Quintela     PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
71cad3cd79SJuan Quintela     VirtioPCIClass *vpciklass = VIRTIO_PCI_CLASS(klass);
72cad3cd79SJuan Quintela 
73cad3cd79SJuan Quintela     k->romfile = "efi-virtio.rom";
74cad3cd79SJuan Quintela     k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
75cad3cd79SJuan Quintela     k->device_id = PCI_DEVICE_ID_VIRTIO_NET;
76cad3cd79SJuan Quintela     k->revision = VIRTIO_PCI_ABI_VERSION;
77cad3cd79SJuan Quintela     k->class_id = PCI_CLASS_NETWORK_ETHERNET;
78cad3cd79SJuan Quintela     set_bit(DEVICE_CATEGORY_NETWORK, dc->categories);
794f67d30bSMarc-André Lureau     device_class_set_props(dc, virtio_net_properties);
80cad3cd79SJuan Quintela     vpciklass->realize = virtio_net_pci_realize;
81cad3cd79SJuan Quintela }
82cad3cd79SJuan Quintela 
virtio_net_pci_instance_init(Object * obj)83cad3cd79SJuan Quintela static void virtio_net_pci_instance_init(Object *obj)
84cad3cd79SJuan Quintela {
85cad3cd79SJuan Quintela     VirtIONetPCI *dev = VIRTIO_NET_PCI(obj);
86cad3cd79SJuan Quintela 
87cad3cd79SJuan Quintela     virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
88cad3cd79SJuan Quintela                                 TYPE_VIRTIO_NET);
89cad3cd79SJuan Quintela     object_property_add_alias(obj, "bootindex", OBJECT(&dev->vdev),
90d2623129SMarkus Armbruster                               "bootindex");
91cad3cd79SJuan Quintela }
92cad3cd79SJuan Quintela 
93cad3cd79SJuan Quintela static const VirtioPCIDeviceTypeInfo virtio_net_pci_info = {
94cad3cd79SJuan Quintela     .base_name             = TYPE_VIRTIO_NET_PCI,
95cad3cd79SJuan Quintela     .generic_name          = "virtio-net-pci",
96cad3cd79SJuan Quintela     .transitional_name     = "virtio-net-pci-transitional",
97cad3cd79SJuan Quintela     .non_transitional_name = "virtio-net-pci-non-transitional",
98cad3cd79SJuan Quintela     .instance_size = sizeof(VirtIONetPCI),
99cad3cd79SJuan Quintela     .instance_init = virtio_net_pci_instance_init,
100cad3cd79SJuan Quintela     .class_init    = virtio_net_pci_class_init,
101cad3cd79SJuan Quintela };
102cad3cd79SJuan Quintela 
virtio_net_pci_register(void)103cad3cd79SJuan Quintela static void virtio_net_pci_register(void)
104cad3cd79SJuan Quintela {
105cad3cd79SJuan Quintela     virtio_pci_types_register(&virtio_net_pci_info);
106cad3cd79SJuan Quintela }
107cad3cd79SJuan Quintela 
108cad3cd79SJuan Quintela type_init(virtio_net_pci_register)
109