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