ne2000-pci.c 4.3 KB

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