pcnet-pci.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. /*
  2. * QEMU AMD PC-Net II (Am79C970A) PCI emulation
  3. *
  4. * Copyright (c) 2004 Antony T Curtis
  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. /* This software was written to be compatible with the specification:
  25. * AMD Am79C970A PCnet-PCI II Ethernet Controller Data-Sheet
  26. * AMD Publication# 19436 Rev:E Amendment/0 Issue Date: June 2000
  27. */
  28. #include "qemu/osdep.h"
  29. #include "hw/irq.h"
  30. #include "hw/pci/pci.h"
  31. #include "hw/qdev-properties.h"
  32. #include "migration/vmstate.h"
  33. #include "net/net.h"
  34. #include "qemu/module.h"
  35. #include "qemu/timer.h"
  36. #include "sysemu/dma.h"
  37. #include "sysemu/sysemu.h"
  38. #include "trace.h"
  39. #include "pcnet.h"
  40. #include "qom/object.h"
  41. //#define PCNET_DEBUG
  42. //#define PCNET_DEBUG_IO
  43. //#define PCNET_DEBUG_BCR
  44. //#define PCNET_DEBUG_CSR
  45. //#define PCNET_DEBUG_RMD
  46. //#define PCNET_DEBUG_TMD
  47. //#define PCNET_DEBUG_MATCH
  48. #define TYPE_PCI_PCNET "pcnet"
  49. OBJECT_DECLARE_SIMPLE_TYPE(PCIPCNetState, PCI_PCNET)
  50. struct PCIPCNetState {
  51. /*< private >*/
  52. PCIDevice parent_obj;
  53. /*< public >*/
  54. PCNetState state;
  55. MemoryRegion io_bar;
  56. };
  57. static void pcnet_aprom_writeb(void *opaque, uint32_t addr, uint32_t val)
  58. {
  59. PCNetState *s = opaque;
  60. trace_pcnet_aprom_writeb(opaque, addr, val);
  61. if (BCR_APROMWE(s)) {
  62. s->prom[addr & 15] = val;
  63. }
  64. }
  65. static uint32_t pcnet_aprom_readb(void *opaque, uint32_t addr)
  66. {
  67. PCNetState *s = opaque;
  68. uint32_t val = s->prom[addr & 15];
  69. trace_pcnet_aprom_readb(opaque, addr, val);
  70. return val;
  71. }
  72. static uint64_t pcnet_ioport_read(void *opaque, hwaddr addr,
  73. unsigned size)
  74. {
  75. PCNetState *d = opaque;
  76. trace_pcnet_ioport_read(opaque, addr, size);
  77. if (addr < 0x10) {
  78. if (!BCR_DWIO(d) && size == 1) {
  79. return pcnet_aprom_readb(d, addr);
  80. } else if (!BCR_DWIO(d) && (addr & 1) == 0 && size == 2) {
  81. return pcnet_aprom_readb(d, addr) |
  82. (pcnet_aprom_readb(d, addr + 1) << 8);
  83. } else if (BCR_DWIO(d) && (addr & 3) == 0 && size == 4) {
  84. return pcnet_aprom_readb(d, addr) |
  85. (pcnet_aprom_readb(d, addr + 1) << 8) |
  86. (pcnet_aprom_readb(d, addr + 2) << 16) |
  87. (pcnet_aprom_readb(d, addr + 3) << 24);
  88. }
  89. } else {
  90. if (size == 2) {
  91. return pcnet_ioport_readw(d, addr);
  92. } else if (size == 4) {
  93. return pcnet_ioport_readl(d, addr);
  94. }
  95. }
  96. return ((uint64_t)1 << (size * 8)) - 1;
  97. }
  98. static void pcnet_ioport_write(void *opaque, hwaddr addr,
  99. uint64_t data, unsigned size)
  100. {
  101. PCNetState *d = opaque;
  102. trace_pcnet_ioport_write(opaque, addr, data, size);
  103. if (addr < 0x10) {
  104. if (!BCR_DWIO(d) && size == 1) {
  105. pcnet_aprom_writeb(d, addr, data);
  106. } else if (!BCR_DWIO(d) && (addr & 1) == 0 && size == 2) {
  107. pcnet_aprom_writeb(d, addr, data & 0xff);
  108. pcnet_aprom_writeb(d, addr + 1, data >> 8);
  109. } else if (BCR_DWIO(d) && (addr & 3) == 0 && size == 4) {
  110. pcnet_aprom_writeb(d, addr, data & 0xff);
  111. pcnet_aprom_writeb(d, addr + 1, (data >> 8) & 0xff);
  112. pcnet_aprom_writeb(d, addr + 2, (data >> 16) & 0xff);
  113. pcnet_aprom_writeb(d, addr + 3, data >> 24);
  114. }
  115. } else {
  116. if (size == 2) {
  117. pcnet_ioport_writew(d, addr, data);
  118. } else if (size == 4) {
  119. pcnet_ioport_writel(d, addr, data);
  120. }
  121. }
  122. }
  123. static const MemoryRegionOps pcnet_io_ops = {
  124. .read = pcnet_ioport_read,
  125. .write = pcnet_ioport_write,
  126. .endianness = DEVICE_LITTLE_ENDIAN,
  127. };
  128. static const VMStateDescription vmstate_pci_pcnet = {
  129. .name = "pcnet",
  130. .version_id = 3,
  131. .minimum_version_id = 2,
  132. .fields = (VMStateField[]) {
  133. VMSTATE_PCI_DEVICE(parent_obj, PCIPCNetState),
  134. VMSTATE_STRUCT(state, PCIPCNetState, 0, vmstate_pcnet, PCNetState),
  135. VMSTATE_END_OF_LIST()
  136. }
  137. };
  138. /* PCI interface */
  139. static const MemoryRegionOps pcnet_mmio_ops = {
  140. .read = pcnet_ioport_read,
  141. .write = pcnet_ioport_write,
  142. .valid.min_access_size = 1,
  143. .valid.max_access_size = 4,
  144. .impl.min_access_size = 1,
  145. .impl.max_access_size = 4,
  146. .endianness = DEVICE_LITTLE_ENDIAN,
  147. };
  148. static void pci_physical_memory_write(void *dma_opaque, hwaddr addr,
  149. uint8_t *buf, int len, int do_bswap)
  150. {
  151. pci_dma_write(dma_opaque, addr, buf, len);
  152. }
  153. static void pci_physical_memory_read(void *dma_opaque, hwaddr addr,
  154. uint8_t *buf, int len, int do_bswap)
  155. {
  156. pci_dma_read(dma_opaque, addr, buf, len);
  157. }
  158. static void pci_pcnet_uninit(PCIDevice *dev)
  159. {
  160. PCIPCNetState *d = PCI_PCNET(dev);
  161. qemu_free_irq(d->state.irq);
  162. timer_del(d->state.poll_timer);
  163. timer_free(d->state.poll_timer);
  164. qemu_del_nic(d->state.nic);
  165. }
  166. static NetClientInfo net_pci_pcnet_info = {
  167. .type = NET_CLIENT_DRIVER_NIC,
  168. .size = sizeof(NICState),
  169. .receive = pcnet_receive,
  170. .link_status_changed = pcnet_set_link_status,
  171. };
  172. static void pci_pcnet_realize(PCIDevice *pci_dev, Error **errp)
  173. {
  174. PCIPCNetState *d = PCI_PCNET(pci_dev);
  175. PCNetState *s = &d->state;
  176. uint8_t *pci_conf;
  177. #if 0
  178. printf("sizeof(RMD)=%d, sizeof(TMD)=%d\n",
  179. sizeof(struct pcnet_RMD), sizeof(struct pcnet_TMD));
  180. #endif
  181. pci_conf = pci_dev->config;
  182. pci_set_word(pci_conf + PCI_STATUS,
  183. PCI_STATUS_FAST_BACK | PCI_STATUS_DEVSEL_MEDIUM);
  184. pci_set_word(pci_conf + PCI_SUBSYSTEM_VENDOR_ID, 0x0);
  185. pci_set_word(pci_conf + PCI_SUBSYSTEM_ID, 0x0);
  186. pci_conf[PCI_INTERRUPT_PIN] = 1; /* interrupt pin A */
  187. pci_conf[PCI_MIN_GNT] = 0x06;
  188. pci_conf[PCI_MAX_LAT] = 0xff;
  189. /* Handler for memory-mapped I/O */
  190. memory_region_init_io(&d->state.mmio, OBJECT(d), &pcnet_mmio_ops, s,
  191. "pcnet-mmio", PCNET_PNPMMIO_SIZE);
  192. memory_region_init_io(&d->io_bar, OBJECT(d), &pcnet_io_ops, s, "pcnet-io",
  193. PCNET_IOPORT_SIZE);
  194. pci_register_bar(pci_dev, 0, PCI_BASE_ADDRESS_SPACE_IO, &d->io_bar);
  195. pci_register_bar(pci_dev, 1, 0, &s->mmio);
  196. s->irq = pci_allocate_irq(pci_dev);
  197. s->phys_mem_read = pci_physical_memory_read;
  198. s->phys_mem_write = pci_physical_memory_write;
  199. s->dma_opaque = DEVICE(pci_dev);
  200. pcnet_common_init(DEVICE(pci_dev), s, &net_pci_pcnet_info);
  201. }
  202. static void pci_reset(DeviceState *dev)
  203. {
  204. PCIPCNetState *d = PCI_PCNET(dev);
  205. pcnet_h_reset(&d->state);
  206. }
  207. static void pcnet_instance_init(Object *obj)
  208. {
  209. PCIPCNetState *d = PCI_PCNET(obj);
  210. PCNetState *s = &d->state;
  211. device_add_bootindex_property(obj, &s->conf.bootindex,
  212. "bootindex", "/ethernet-phy@0",
  213. DEVICE(obj));
  214. }
  215. static Property pcnet_properties[] = {
  216. DEFINE_NIC_PROPERTIES(PCIPCNetState, state.conf),
  217. DEFINE_PROP_END_OF_LIST(),
  218. };
  219. static void pcnet_class_init(ObjectClass *klass, void *data)
  220. {
  221. DeviceClass *dc = DEVICE_CLASS(klass);
  222. PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
  223. k->realize = pci_pcnet_realize;
  224. k->exit = pci_pcnet_uninit;
  225. k->romfile = "efi-pcnet.rom",
  226. k->vendor_id = PCI_VENDOR_ID_AMD;
  227. k->device_id = PCI_DEVICE_ID_AMD_LANCE;
  228. k->revision = 0x10;
  229. k->class_id = PCI_CLASS_NETWORK_ETHERNET;
  230. dc->reset = pci_reset;
  231. dc->vmsd = &vmstate_pci_pcnet;
  232. device_class_set_props(dc, pcnet_properties);
  233. set_bit(DEVICE_CATEGORY_NETWORK, dc->categories);
  234. }
  235. static const TypeInfo pcnet_info = {
  236. .name = TYPE_PCI_PCNET,
  237. .parent = TYPE_PCI_DEVICE,
  238. .instance_size = sizeof(PCIPCNetState),
  239. .class_init = pcnet_class_init,
  240. .instance_init = pcnet_instance_init,
  241. .interfaces = (InterfaceInfo[]) {
  242. { INTERFACE_CONVENTIONAL_PCI_DEVICE },
  243. { },
  244. },
  245. };
  246. static void pci_pcnet_register_types(void)
  247. {
  248. type_register_static(&pcnet_info);
  249. }
  250. type_init(pci_pcnet_register_types)