pcnet-pci.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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 "pci.h"
  29. #include "net.h"
  30. #include "loader.h"
  31. #include "qemu-timer.h"
  32. #include "dma.h"
  33. #include "pcnet.h"
  34. //#define PCNET_DEBUG
  35. //#define PCNET_DEBUG_IO
  36. //#define PCNET_DEBUG_BCR
  37. //#define PCNET_DEBUG_CSR
  38. //#define PCNET_DEBUG_RMD
  39. //#define PCNET_DEBUG_TMD
  40. //#define PCNET_DEBUG_MATCH
  41. typedef struct {
  42. PCIDevice pci_dev;
  43. PCNetState state;
  44. MemoryRegion io_bar;
  45. } PCIPCNetState;
  46. static void pcnet_aprom_writeb(void *opaque, uint32_t addr, uint32_t val)
  47. {
  48. PCNetState *s = opaque;
  49. #ifdef PCNET_DEBUG
  50. printf("pcnet_aprom_writeb addr=0x%08x val=0x%02x\n", addr, val);
  51. #endif
  52. if (BCR_APROMWE(s)) {
  53. s->prom[addr & 15] = val;
  54. }
  55. }
  56. static uint32_t pcnet_aprom_readb(void *opaque, uint32_t addr)
  57. {
  58. PCNetState *s = opaque;
  59. uint32_t val = s->prom[addr & 15];
  60. #ifdef PCNET_DEBUG
  61. printf("pcnet_aprom_readb addr=0x%08x val=0x%02x\n", addr, val);
  62. #endif
  63. return val;
  64. }
  65. static uint64_t pcnet_ioport_read(void *opaque, target_phys_addr_t addr,
  66. unsigned size)
  67. {
  68. PCNetState *d = opaque;
  69. if (addr < 0x10) {
  70. if (!BCR_DWIO(d) && size == 1) {
  71. return pcnet_aprom_readb(d, addr);
  72. } else if (!BCR_DWIO(d) && (addr & 1) == 0 && size == 2) {
  73. return pcnet_aprom_readb(d, addr) |
  74. (pcnet_aprom_readb(d, addr + 1) << 8);
  75. } else if (BCR_DWIO(d) && (addr & 3) == 0 && size == 4) {
  76. return pcnet_aprom_readb(d, addr) |
  77. (pcnet_aprom_readb(d, addr + 1) << 8) |
  78. (pcnet_aprom_readb(d, addr + 2) << 16) |
  79. (pcnet_aprom_readb(d, addr + 3) << 24);
  80. }
  81. } else {
  82. if (size == 2) {
  83. return pcnet_ioport_readw(d, addr);
  84. } else if (size == 4) {
  85. return pcnet_ioport_readl(d, addr);
  86. }
  87. }
  88. return ((uint64_t)1 << (size * 8)) - 1;
  89. }
  90. static void pcnet_ioport_write(void *opaque, target_phys_addr_t addr,
  91. uint64_t data, unsigned size)
  92. {
  93. PCNetState *d = opaque;
  94. if (addr < 0x10) {
  95. if (!BCR_DWIO(d) && size == 1) {
  96. pcnet_aprom_writeb(d, addr, data);
  97. } else if (!BCR_DWIO(d) && (addr & 1) == 0 && size == 2) {
  98. pcnet_aprom_writeb(d, addr, data & 0xff);
  99. pcnet_aprom_writeb(d, addr + 1, data >> 8);
  100. } else if (BCR_DWIO(d) && (addr & 3) == 0 && size == 4) {
  101. pcnet_aprom_writeb(d, addr, data & 0xff);
  102. pcnet_aprom_writeb(d, addr + 1, (data >> 8) & 0xff);
  103. pcnet_aprom_writeb(d, addr + 2, (data >> 16) & 0xff);
  104. pcnet_aprom_writeb(d, addr + 3, data >> 24);
  105. }
  106. } else {
  107. if (size == 2) {
  108. pcnet_ioport_writew(d, addr, data);
  109. } else if (size == 4) {
  110. pcnet_ioport_writel(d, addr, data);
  111. }
  112. }
  113. }
  114. static const MemoryRegionOps pcnet_io_ops = {
  115. .read = pcnet_ioport_read,
  116. .write = pcnet_ioport_write,
  117. .endianness = DEVICE_NATIVE_ENDIAN,
  118. };
  119. static void pcnet_mmio_writeb(void *opaque, target_phys_addr_t addr, uint32_t val)
  120. {
  121. PCNetState *d = opaque;
  122. #ifdef PCNET_DEBUG_IO
  123. printf("pcnet_mmio_writeb addr=0x" TARGET_FMT_plx" val=0x%02x\n", addr,
  124. val);
  125. #endif
  126. if (!(addr & 0x10))
  127. pcnet_aprom_writeb(d, addr & 0x0f, val);
  128. }
  129. static uint32_t pcnet_mmio_readb(void *opaque, target_phys_addr_t addr)
  130. {
  131. PCNetState *d = opaque;
  132. uint32_t val = -1;
  133. if (!(addr & 0x10))
  134. val = pcnet_aprom_readb(d, addr & 0x0f);
  135. #ifdef PCNET_DEBUG_IO
  136. printf("pcnet_mmio_readb addr=0x" TARGET_FMT_plx " val=0x%02x\n", addr,
  137. val & 0xff);
  138. #endif
  139. return val;
  140. }
  141. static void pcnet_mmio_writew(void *opaque, target_phys_addr_t addr, uint32_t val)
  142. {
  143. PCNetState *d = opaque;
  144. #ifdef PCNET_DEBUG_IO
  145. printf("pcnet_mmio_writew addr=0x" TARGET_FMT_plx " val=0x%04x\n", addr,
  146. val);
  147. #endif
  148. if (addr & 0x10)
  149. pcnet_ioport_writew(d, addr & 0x0f, val);
  150. else {
  151. addr &= 0x0f;
  152. pcnet_aprom_writeb(d, addr, val & 0xff);
  153. pcnet_aprom_writeb(d, addr+1, (val & 0xff00) >> 8);
  154. }
  155. }
  156. static uint32_t pcnet_mmio_readw(void *opaque, target_phys_addr_t addr)
  157. {
  158. PCNetState *d = opaque;
  159. uint32_t val = -1;
  160. if (addr & 0x10)
  161. val = pcnet_ioport_readw(d, addr & 0x0f);
  162. else {
  163. addr &= 0x0f;
  164. val = pcnet_aprom_readb(d, addr+1);
  165. val <<= 8;
  166. val |= pcnet_aprom_readb(d, addr);
  167. }
  168. #ifdef PCNET_DEBUG_IO
  169. printf("pcnet_mmio_readw addr=0x" TARGET_FMT_plx" val = 0x%04x\n", addr,
  170. val & 0xffff);
  171. #endif
  172. return val;
  173. }
  174. static void pcnet_mmio_writel(void *opaque, target_phys_addr_t addr, uint32_t val)
  175. {
  176. PCNetState *d = opaque;
  177. #ifdef PCNET_DEBUG_IO
  178. printf("pcnet_mmio_writel addr=0x" TARGET_FMT_plx" val=0x%08x\n", addr,
  179. val);
  180. #endif
  181. if (addr & 0x10)
  182. pcnet_ioport_writel(d, addr & 0x0f, val);
  183. else {
  184. addr &= 0x0f;
  185. pcnet_aprom_writeb(d, addr, val & 0xff);
  186. pcnet_aprom_writeb(d, addr+1, (val & 0xff00) >> 8);
  187. pcnet_aprom_writeb(d, addr+2, (val & 0xff0000) >> 16);
  188. pcnet_aprom_writeb(d, addr+3, (val & 0xff000000) >> 24);
  189. }
  190. }
  191. static uint32_t pcnet_mmio_readl(void *opaque, target_phys_addr_t addr)
  192. {
  193. PCNetState *d = opaque;
  194. uint32_t val;
  195. if (addr & 0x10)
  196. val = pcnet_ioport_readl(d, addr & 0x0f);
  197. else {
  198. addr &= 0x0f;
  199. val = pcnet_aprom_readb(d, addr+3);
  200. val <<= 8;
  201. val |= pcnet_aprom_readb(d, addr+2);
  202. val <<= 8;
  203. val |= pcnet_aprom_readb(d, addr+1);
  204. val <<= 8;
  205. val |= pcnet_aprom_readb(d, addr);
  206. }
  207. #ifdef PCNET_DEBUG_IO
  208. printf("pcnet_mmio_readl addr=0x" TARGET_FMT_plx " val=0x%08x\n", addr,
  209. val);
  210. #endif
  211. return val;
  212. }
  213. static const VMStateDescription vmstate_pci_pcnet = {
  214. .name = "pcnet",
  215. .version_id = 3,
  216. .minimum_version_id = 2,
  217. .minimum_version_id_old = 2,
  218. .fields = (VMStateField []) {
  219. VMSTATE_PCI_DEVICE(pci_dev, PCIPCNetState),
  220. VMSTATE_STRUCT(state, PCIPCNetState, 0, vmstate_pcnet, PCNetState),
  221. VMSTATE_END_OF_LIST()
  222. }
  223. };
  224. /* PCI interface */
  225. static const MemoryRegionOps pcnet_mmio_ops = {
  226. .old_mmio = {
  227. .read = { pcnet_mmio_readb, pcnet_mmio_readw, pcnet_mmio_readl },
  228. .write = { pcnet_mmio_writeb, pcnet_mmio_writew, pcnet_mmio_writel },
  229. },
  230. .endianness = DEVICE_NATIVE_ENDIAN,
  231. };
  232. static void pci_physical_memory_write(void *dma_opaque, target_phys_addr_t addr,
  233. uint8_t *buf, int len, int do_bswap)
  234. {
  235. pci_dma_write(dma_opaque, addr, buf, len);
  236. }
  237. static void pci_physical_memory_read(void *dma_opaque, target_phys_addr_t addr,
  238. uint8_t *buf, int len, int do_bswap)
  239. {
  240. pci_dma_read(dma_opaque, addr, buf, len);
  241. }
  242. static void pci_pcnet_cleanup(NetClientState *nc)
  243. {
  244. PCNetState *d = DO_UPCAST(NICState, nc, nc)->opaque;
  245. pcnet_common_cleanup(d);
  246. }
  247. static void pci_pcnet_uninit(PCIDevice *dev)
  248. {
  249. PCIPCNetState *d = DO_UPCAST(PCIPCNetState, pci_dev, dev);
  250. memory_region_destroy(&d->state.mmio);
  251. memory_region_destroy(&d->io_bar);
  252. qemu_del_timer(d->state.poll_timer);
  253. qemu_free_timer(d->state.poll_timer);
  254. qemu_del_net_client(&d->state.nic->nc);
  255. }
  256. static NetClientInfo net_pci_pcnet_info = {
  257. .type = NET_CLIENT_OPTIONS_KIND_NIC,
  258. .size = sizeof(NICState),
  259. .can_receive = pcnet_can_receive,
  260. .receive = pcnet_receive,
  261. .link_status_changed = pcnet_set_link_status,
  262. .cleanup = pci_pcnet_cleanup,
  263. };
  264. static int pci_pcnet_init(PCIDevice *pci_dev)
  265. {
  266. PCIPCNetState *d = DO_UPCAST(PCIPCNetState, pci_dev, pci_dev);
  267. PCNetState *s = &d->state;
  268. uint8_t *pci_conf;
  269. #if 0
  270. printf("sizeof(RMD)=%d, sizeof(TMD)=%d\n",
  271. sizeof(struct pcnet_RMD), sizeof(struct pcnet_TMD));
  272. #endif
  273. pci_conf = pci_dev->config;
  274. pci_set_word(pci_conf + PCI_STATUS,
  275. PCI_STATUS_FAST_BACK | PCI_STATUS_DEVSEL_MEDIUM);
  276. pci_set_word(pci_conf + PCI_SUBSYSTEM_VENDOR_ID, 0x0);
  277. pci_set_word(pci_conf + PCI_SUBSYSTEM_ID, 0x0);
  278. pci_conf[PCI_INTERRUPT_PIN] = 1; /* interrupt pin A */
  279. pci_conf[PCI_MIN_GNT] = 0x06;
  280. pci_conf[PCI_MAX_LAT] = 0xff;
  281. /* Handler for memory-mapped I/O */
  282. memory_region_init_io(&d->state.mmio, &pcnet_mmio_ops, s, "pcnet-mmio",
  283. PCNET_PNPMMIO_SIZE);
  284. memory_region_init_io(&d->io_bar, &pcnet_io_ops, s, "pcnet-io",
  285. PCNET_IOPORT_SIZE);
  286. pci_register_bar(pci_dev, 0, PCI_BASE_ADDRESS_SPACE_IO, &d->io_bar);
  287. pci_register_bar(pci_dev, 1, 0, &s->mmio);
  288. s->irq = pci_dev->irq[0];
  289. s->phys_mem_read = pci_physical_memory_read;
  290. s->phys_mem_write = pci_physical_memory_write;
  291. s->dma_opaque = pci_dev;
  292. return pcnet_common_init(&pci_dev->qdev, s, &net_pci_pcnet_info);
  293. }
  294. static void pci_reset(DeviceState *dev)
  295. {
  296. PCIPCNetState *d = DO_UPCAST(PCIPCNetState, pci_dev.qdev, dev);
  297. pcnet_h_reset(&d->state);
  298. }
  299. static Property pcnet_properties[] = {
  300. DEFINE_NIC_PROPERTIES(PCIPCNetState, state.conf),
  301. DEFINE_PROP_END_OF_LIST(),
  302. };
  303. static void pcnet_class_init(ObjectClass *klass, void *data)
  304. {
  305. DeviceClass *dc = DEVICE_CLASS(klass);
  306. PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
  307. k->init = pci_pcnet_init;
  308. k->exit = pci_pcnet_uninit;
  309. k->romfile = "pxe-pcnet.rom",
  310. k->vendor_id = PCI_VENDOR_ID_AMD;
  311. k->device_id = PCI_DEVICE_ID_AMD_LANCE;
  312. k->revision = 0x10;
  313. k->class_id = PCI_CLASS_NETWORK_ETHERNET;
  314. dc->reset = pci_reset;
  315. dc->vmsd = &vmstate_pci_pcnet;
  316. dc->props = pcnet_properties;
  317. }
  318. static TypeInfo pcnet_info = {
  319. .name = "pcnet",
  320. .parent = TYPE_PCI_DEVICE,
  321. .instance_size = sizeof(PCIPCNetState),
  322. .class_init = pcnet_class_init,
  323. };
  324. static void pci_pcnet_register_types(void)
  325. {
  326. type_register_static(&pcnet_info);
  327. }
  328. type_init(pci_pcnet_register_types)