xilinx_ethlite.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /*
  2. * QEMU model of the Xilinx Ethernet Lite MAC.
  3. *
  4. * Copyright (c) 2009 Edgar E. Iglesias.
  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 "sysbus.h"
  25. #include "hw.h"
  26. #include "net/net.h"
  27. #define D(x)
  28. #define R_TX_BUF0 0
  29. #define R_TX_LEN0 (0x07f4 / 4)
  30. #define R_TX_GIE0 (0x07f8 / 4)
  31. #define R_TX_CTRL0 (0x07fc / 4)
  32. #define R_TX_BUF1 (0x0800 / 4)
  33. #define R_TX_LEN1 (0x0ff4 / 4)
  34. #define R_TX_CTRL1 (0x0ffc / 4)
  35. #define R_RX_BUF0 (0x1000 / 4)
  36. #define R_RX_CTRL0 (0x17fc / 4)
  37. #define R_RX_BUF1 (0x1800 / 4)
  38. #define R_RX_CTRL1 (0x1ffc / 4)
  39. #define R_MAX (0x2000 / 4)
  40. #define GIE_GIE 0x80000000
  41. #define CTRL_I 0x8
  42. #define CTRL_P 0x2
  43. #define CTRL_S 0x1
  44. struct xlx_ethlite
  45. {
  46. SysBusDevice busdev;
  47. MemoryRegion mmio;
  48. qemu_irq irq;
  49. NICState *nic;
  50. NICConf conf;
  51. uint32_t c_tx_pingpong;
  52. uint32_t c_rx_pingpong;
  53. unsigned int txbuf;
  54. unsigned int rxbuf;
  55. uint32_t regs[R_MAX];
  56. };
  57. static inline void eth_pulse_irq(struct xlx_ethlite *s)
  58. {
  59. /* Only the first gie reg is active. */
  60. if (s->regs[R_TX_GIE0] & GIE_GIE) {
  61. qemu_irq_pulse(s->irq);
  62. }
  63. }
  64. static uint64_t
  65. eth_read(void *opaque, hwaddr addr, unsigned int size)
  66. {
  67. struct xlx_ethlite *s = opaque;
  68. uint32_t r = 0;
  69. addr >>= 2;
  70. switch (addr)
  71. {
  72. case R_TX_GIE0:
  73. case R_TX_LEN0:
  74. case R_TX_LEN1:
  75. case R_TX_CTRL1:
  76. case R_TX_CTRL0:
  77. case R_RX_CTRL1:
  78. case R_RX_CTRL0:
  79. r = s->regs[addr];
  80. D(qemu_log("%s " TARGET_FMT_plx "=%x\n", __func__, addr * 4, r));
  81. break;
  82. default:
  83. r = tswap32(s->regs[addr]);
  84. break;
  85. }
  86. return r;
  87. }
  88. static void
  89. eth_write(void *opaque, hwaddr addr,
  90. uint64_t val64, unsigned int size)
  91. {
  92. struct xlx_ethlite *s = opaque;
  93. unsigned int base = 0;
  94. uint32_t value = val64;
  95. addr >>= 2;
  96. switch (addr)
  97. {
  98. case R_TX_CTRL0:
  99. case R_TX_CTRL1:
  100. if (addr == R_TX_CTRL1)
  101. base = 0x800 / 4;
  102. D(qemu_log("%s addr=" TARGET_FMT_plx " val=%x\n",
  103. __func__, addr * 4, value));
  104. if ((value & (CTRL_P | CTRL_S)) == CTRL_S) {
  105. qemu_send_packet(qemu_get_queue(s->nic),
  106. (void *) &s->regs[base],
  107. s->regs[base + R_TX_LEN0]);
  108. D(qemu_log("eth_tx %d\n", s->regs[base + R_TX_LEN0]));
  109. if (s->regs[base + R_TX_CTRL0] & CTRL_I)
  110. eth_pulse_irq(s);
  111. } else if ((value & (CTRL_P | CTRL_S)) == (CTRL_P | CTRL_S)) {
  112. memcpy(&s->conf.macaddr.a[0], &s->regs[base], 6);
  113. if (s->regs[base + R_TX_CTRL0] & CTRL_I)
  114. eth_pulse_irq(s);
  115. }
  116. /* We are fast and get ready pretty much immediately so
  117. we actually never flip the S nor P bits to one. */
  118. s->regs[addr] = value & ~(CTRL_P | CTRL_S);
  119. break;
  120. /* Keep these native. */
  121. case R_RX_CTRL0:
  122. case R_RX_CTRL1:
  123. if (!(value & CTRL_S)) {
  124. qemu_flush_queued_packets(qemu_get_queue(s->nic));
  125. }
  126. case R_TX_LEN0:
  127. case R_TX_LEN1:
  128. case R_TX_GIE0:
  129. D(qemu_log("%s addr=" TARGET_FMT_plx " val=%x\n",
  130. __func__, addr * 4, value));
  131. s->regs[addr] = value;
  132. break;
  133. default:
  134. s->regs[addr] = tswap32(value);
  135. break;
  136. }
  137. }
  138. static const MemoryRegionOps eth_ops = {
  139. .read = eth_read,
  140. .write = eth_write,
  141. .endianness = DEVICE_NATIVE_ENDIAN,
  142. .valid = {
  143. .min_access_size = 4,
  144. .max_access_size = 4
  145. }
  146. };
  147. static int eth_can_rx(NetClientState *nc)
  148. {
  149. struct xlx_ethlite *s = qemu_get_nic_opaque(nc);
  150. unsigned int rxbase = s->rxbuf * (0x800 / 4);
  151. return !(s->regs[rxbase + R_RX_CTRL0] & CTRL_S);
  152. }
  153. static ssize_t eth_rx(NetClientState *nc, const uint8_t *buf, size_t size)
  154. {
  155. struct xlx_ethlite *s = qemu_get_nic_opaque(nc);
  156. unsigned int rxbase = s->rxbuf * (0x800 / 4);
  157. /* DA filter. */
  158. if (!(buf[0] & 0x80) && memcmp(&s->conf.macaddr.a[0], buf, 6))
  159. return size;
  160. if (s->regs[rxbase + R_RX_CTRL0] & CTRL_S) {
  161. D(qemu_log("ethlite lost packet %x\n", s->regs[R_RX_CTRL0]));
  162. return -1;
  163. }
  164. D(qemu_log("%s %zd rxbase=%x\n", __func__, size, rxbase));
  165. memcpy(&s->regs[rxbase + R_RX_BUF0], buf, size);
  166. s->regs[rxbase + R_RX_CTRL0] |= CTRL_S;
  167. if (s->regs[rxbase + R_RX_CTRL0] & CTRL_I)
  168. eth_pulse_irq(s);
  169. /* If c_rx_pingpong was set flip buffers. */
  170. s->rxbuf ^= s->c_rx_pingpong;
  171. return size;
  172. }
  173. static void eth_cleanup(NetClientState *nc)
  174. {
  175. struct xlx_ethlite *s = qemu_get_nic_opaque(nc);
  176. s->nic = NULL;
  177. }
  178. static NetClientInfo net_xilinx_ethlite_info = {
  179. .type = NET_CLIENT_OPTIONS_KIND_NIC,
  180. .size = sizeof(NICState),
  181. .can_receive = eth_can_rx,
  182. .receive = eth_rx,
  183. .cleanup = eth_cleanup,
  184. };
  185. static int xilinx_ethlite_init(SysBusDevice *dev)
  186. {
  187. struct xlx_ethlite *s = FROM_SYSBUS(typeof (*s), dev);
  188. sysbus_init_irq(dev, &s->irq);
  189. s->rxbuf = 0;
  190. memory_region_init_io(&s->mmio, &eth_ops, s, "xlnx.xps-ethernetlite",
  191. R_MAX * 4);
  192. sysbus_init_mmio(dev, &s->mmio);
  193. qemu_macaddr_default_if_unset(&s->conf.macaddr);
  194. s->nic = qemu_new_nic(&net_xilinx_ethlite_info, &s->conf,
  195. object_get_typename(OBJECT(dev)), dev->qdev.id, s);
  196. qemu_format_nic_info_str(qemu_get_queue(s->nic), s->conf.macaddr.a);
  197. return 0;
  198. }
  199. static Property xilinx_ethlite_properties[] = {
  200. DEFINE_PROP_UINT32("tx-ping-pong", struct xlx_ethlite, c_tx_pingpong, 1),
  201. DEFINE_PROP_UINT32("rx-ping-pong", struct xlx_ethlite, c_rx_pingpong, 1),
  202. DEFINE_NIC_PROPERTIES(struct xlx_ethlite, conf),
  203. DEFINE_PROP_END_OF_LIST(),
  204. };
  205. static void xilinx_ethlite_class_init(ObjectClass *klass, void *data)
  206. {
  207. DeviceClass *dc = DEVICE_CLASS(klass);
  208. SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass);
  209. k->init = xilinx_ethlite_init;
  210. dc->props = xilinx_ethlite_properties;
  211. }
  212. static const TypeInfo xilinx_ethlite_info = {
  213. .name = "xlnx.xps-ethernetlite",
  214. .parent = TYPE_SYS_BUS_DEVICE,
  215. .instance_size = sizeof(struct xlx_ethlite),
  216. .class_init = xilinx_ethlite_class_init,
  217. };
  218. static void xilinx_ethlite_register_types(void)
  219. {
  220. type_register_static(&xilinx_ethlite_info);
  221. }
  222. type_init(xilinx_ethlite_register_types)