npcm7xx_emc.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877
  1. /*
  2. * Nuvoton NPCM7xx EMC Module
  3. *
  4. * Copyright 2020 Google LLC
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the
  8. * Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful, but WITHOUT
  12. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  14. * for more details.
  15. *
  16. * Unsupported/unimplemented features:
  17. * - MCMDR.FDUP (full duplex) is ignored, half duplex is not supported
  18. * - Only CAM0 is supported, CAM[1-15] are not
  19. * - writes to CAMEN.[1-15] are ignored, these bits always read as zeroes
  20. * - MII is not implemented, MIIDA.BUSY and MIID always return zero
  21. * - MCMDR.LBK is not implemented
  22. * - MCMDR.{OPMOD,ENSQE,AEP,ARP} are not supported
  23. * - H/W FIFOs are not supported, MCMDR.FFTCR is ignored
  24. * - MGSTA.SQE is not supported
  25. * - pause and control frames are not implemented
  26. * - MGSTA.CCNT is not supported
  27. * - MPCNT, DMARFS are not implemented
  28. */
  29. #include "qemu/osdep.h"
  30. #include <zlib.h> /* for crc32 */
  31. #include "hw/irq.h"
  32. #include "hw/qdev-clock.h"
  33. #include "hw/qdev-properties.h"
  34. #include "hw/net/npcm7xx_emc.h"
  35. #include "net/eth.h"
  36. #include "migration/vmstate.h"
  37. #include "qemu/bitops.h"
  38. #include "qemu/error-report.h"
  39. #include "qemu/log.h"
  40. #include "qemu/module.h"
  41. #include "qemu/units.h"
  42. #include "system/dma.h"
  43. #include "trace.h"
  44. #define CRC_LENGTH 4
  45. /*
  46. * The maximum size of a (layer 2) ethernet frame as defined by 802.3.
  47. * 1518 = 6(dest macaddr) + 6(src macaddr) + 2(proto) + 4(crc) + 1500(payload)
  48. * This does not include an additional 4 for the vlan field (802.1q).
  49. */
  50. #define MAX_ETH_FRAME_SIZE 1518
  51. static const char *emc_reg_name(int regno)
  52. {
  53. #define REG(name) case REG_ ## name: return #name;
  54. switch (regno) {
  55. REG(CAMCMR)
  56. REG(CAMEN)
  57. REG(TXDLSA)
  58. REG(RXDLSA)
  59. REG(MCMDR)
  60. REG(MIID)
  61. REG(MIIDA)
  62. REG(FFTCR)
  63. REG(TSDR)
  64. REG(RSDR)
  65. REG(DMARFC)
  66. REG(MIEN)
  67. REG(MISTA)
  68. REG(MGSTA)
  69. REG(MPCNT)
  70. REG(MRPC)
  71. REG(MRPCC)
  72. REG(MREPC)
  73. REG(DMARFS)
  74. REG(CTXDSA)
  75. REG(CTXBSA)
  76. REG(CRXDSA)
  77. REG(CRXBSA)
  78. case REG_CAMM_BASE + 0: return "CAM0M";
  79. case REG_CAML_BASE + 0: return "CAM0L";
  80. case REG_CAMM_BASE + 2 ... REG_CAMML_LAST:
  81. /* Only CAM0 is supported, fold the others into something simple. */
  82. if (regno & 1) {
  83. return "CAM<n>L";
  84. } else {
  85. return "CAM<n>M";
  86. }
  87. default: return "UNKNOWN";
  88. }
  89. #undef REG
  90. }
  91. static void emc_reset(NPCM7xxEMCState *emc)
  92. {
  93. uint32_t value;
  94. trace_npcm7xx_emc_reset(emc->emc_num);
  95. memset(&emc->regs[0], 0, sizeof(emc->regs));
  96. /* These regs have non-zero reset values. */
  97. emc->regs[REG_TXDLSA] = 0xfffffffc;
  98. emc->regs[REG_RXDLSA] = 0xfffffffc;
  99. emc->regs[REG_MIIDA] = 0x00900000;
  100. emc->regs[REG_FFTCR] = 0x0101;
  101. emc->regs[REG_DMARFC] = 0x0800;
  102. emc->regs[REG_MPCNT] = 0x7fff;
  103. emc->tx_active = false;
  104. emc->rx_active = false;
  105. /* Set the MAC address in the register space. */
  106. value = (emc->conf.macaddr.a[0] << 24) |
  107. (emc->conf.macaddr.a[1] << 16) |
  108. (emc->conf.macaddr.a[2] << 8) |
  109. emc->conf.macaddr.a[3];
  110. emc->regs[REG_CAMM_BASE] = value;
  111. value = (emc->conf.macaddr.a[4] << 24) | (emc->conf.macaddr.a[5] << 16);
  112. emc->regs[REG_CAML_BASE] = value;
  113. }
  114. static void npcm7xx_emc_reset(DeviceState *dev)
  115. {
  116. NPCM7xxEMCState *emc = NPCM7XX_EMC(dev);
  117. emc_reset(emc);
  118. }
  119. static void emc_soft_reset(NPCM7xxEMCState *emc)
  120. {
  121. /*
  122. * The docs say at least MCMDR.{LBK,OPMOD} bits are not changed during a
  123. * soft reset, but does not go into further detail. For now, KISS.
  124. */
  125. uint32_t mcmdr = emc->regs[REG_MCMDR];
  126. emc_reset(emc);
  127. emc->regs[REG_MCMDR] = mcmdr & (REG_MCMDR_LBK | REG_MCMDR_OPMOD);
  128. qemu_set_irq(emc->tx_irq, 0);
  129. qemu_set_irq(emc->rx_irq, 0);
  130. }
  131. static void emc_set_link(NetClientState *nc)
  132. {
  133. /* Nothing to do yet. */
  134. }
  135. /* MISTA.TXINTR is the union of the individual bits with their enables. */
  136. static void emc_update_mista_txintr(NPCM7xxEMCState *emc)
  137. {
  138. /* Only look at the bits we support. */
  139. uint32_t mask = (REG_MISTA_TXBERR |
  140. REG_MISTA_TDU |
  141. REG_MISTA_TXCP);
  142. if (emc->regs[REG_MISTA] & emc->regs[REG_MIEN] & mask) {
  143. emc->regs[REG_MISTA] |= REG_MISTA_TXINTR;
  144. } else {
  145. emc->regs[REG_MISTA] &= ~REG_MISTA_TXINTR;
  146. }
  147. }
  148. /* MISTA.RXINTR is the union of the individual bits with their enables. */
  149. static void emc_update_mista_rxintr(NPCM7xxEMCState *emc)
  150. {
  151. /* Only look at the bits we support. */
  152. uint32_t mask = (REG_MISTA_RXBERR |
  153. REG_MISTA_RDU |
  154. REG_MISTA_RXGD);
  155. if (emc->regs[REG_MISTA] & emc->regs[REG_MIEN] & mask) {
  156. emc->regs[REG_MISTA] |= REG_MISTA_RXINTR;
  157. } else {
  158. emc->regs[REG_MISTA] &= ~REG_MISTA_RXINTR;
  159. }
  160. }
  161. /* N.B. emc_update_mista_txintr must have already been called. */
  162. static void emc_update_tx_irq(NPCM7xxEMCState *emc)
  163. {
  164. int level = !!(emc->regs[REG_MISTA] &
  165. emc->regs[REG_MIEN] &
  166. REG_MISTA_TXINTR);
  167. trace_npcm7xx_emc_update_tx_irq(level);
  168. qemu_set_irq(emc->tx_irq, level);
  169. }
  170. /* N.B. emc_update_mista_rxintr must have already been called. */
  171. static void emc_update_rx_irq(NPCM7xxEMCState *emc)
  172. {
  173. int level = !!(emc->regs[REG_MISTA] &
  174. emc->regs[REG_MIEN] &
  175. REG_MISTA_RXINTR);
  176. trace_npcm7xx_emc_update_rx_irq(level);
  177. qemu_set_irq(emc->rx_irq, level);
  178. }
  179. /* Update IRQ states due to changes in MIEN,MISTA. */
  180. static void emc_update_irq_from_reg_change(NPCM7xxEMCState *emc)
  181. {
  182. emc_update_mista_txintr(emc);
  183. emc_update_tx_irq(emc);
  184. emc_update_mista_rxintr(emc);
  185. emc_update_rx_irq(emc);
  186. }
  187. static int emc_read_tx_desc(dma_addr_t addr, NPCM7xxEMCTxDesc *desc)
  188. {
  189. if (dma_memory_read(&address_space_memory, addr, desc,
  190. sizeof(*desc), MEMTXATTRS_UNSPECIFIED)) {
  191. qemu_log_mask(LOG_GUEST_ERROR, "%s: Failed to read descriptor @ 0x%"
  192. HWADDR_PRIx "\n", __func__, addr);
  193. return -1;
  194. }
  195. desc->flags = le32_to_cpu(desc->flags);
  196. desc->txbsa = le32_to_cpu(desc->txbsa);
  197. desc->status_and_length = le32_to_cpu(desc->status_and_length);
  198. desc->ntxdsa = le32_to_cpu(desc->ntxdsa);
  199. return 0;
  200. }
  201. static int emc_write_tx_desc(const NPCM7xxEMCTxDesc *desc, dma_addr_t addr)
  202. {
  203. NPCM7xxEMCTxDesc le_desc;
  204. le_desc.flags = cpu_to_le32(desc->flags);
  205. le_desc.txbsa = cpu_to_le32(desc->txbsa);
  206. le_desc.status_and_length = cpu_to_le32(desc->status_and_length);
  207. le_desc.ntxdsa = cpu_to_le32(desc->ntxdsa);
  208. if (dma_memory_write(&address_space_memory, addr, &le_desc,
  209. sizeof(le_desc), MEMTXATTRS_UNSPECIFIED)) {
  210. qemu_log_mask(LOG_GUEST_ERROR, "%s: Failed to write descriptor @ 0x%"
  211. HWADDR_PRIx "\n", __func__, addr);
  212. return -1;
  213. }
  214. return 0;
  215. }
  216. static int emc_read_rx_desc(dma_addr_t addr, NPCM7xxEMCRxDesc *desc)
  217. {
  218. if (dma_memory_read(&address_space_memory, addr, desc,
  219. sizeof(*desc), MEMTXATTRS_UNSPECIFIED)) {
  220. qemu_log_mask(LOG_GUEST_ERROR, "%s: Failed to read descriptor @ 0x%"
  221. HWADDR_PRIx "\n", __func__, addr);
  222. return -1;
  223. }
  224. desc->status_and_length = le32_to_cpu(desc->status_and_length);
  225. desc->rxbsa = le32_to_cpu(desc->rxbsa);
  226. desc->reserved = le32_to_cpu(desc->reserved);
  227. desc->nrxdsa = le32_to_cpu(desc->nrxdsa);
  228. return 0;
  229. }
  230. static int emc_write_rx_desc(const NPCM7xxEMCRxDesc *desc, dma_addr_t addr)
  231. {
  232. NPCM7xxEMCRxDesc le_desc;
  233. le_desc.status_and_length = cpu_to_le32(desc->status_and_length);
  234. le_desc.rxbsa = cpu_to_le32(desc->rxbsa);
  235. le_desc.reserved = cpu_to_le32(desc->reserved);
  236. le_desc.nrxdsa = cpu_to_le32(desc->nrxdsa);
  237. if (dma_memory_write(&address_space_memory, addr, &le_desc,
  238. sizeof(le_desc), MEMTXATTRS_UNSPECIFIED)) {
  239. qemu_log_mask(LOG_GUEST_ERROR, "%s: Failed to write descriptor @ 0x%"
  240. HWADDR_PRIx "\n", __func__, addr);
  241. return -1;
  242. }
  243. return 0;
  244. }
  245. static void emc_set_mista(NPCM7xxEMCState *emc, uint32_t flags)
  246. {
  247. trace_npcm7xx_emc_set_mista(flags);
  248. emc->regs[REG_MISTA] |= flags;
  249. if (extract32(flags, 16, 16)) {
  250. emc_update_mista_txintr(emc);
  251. }
  252. if (extract32(flags, 0, 16)) {
  253. emc_update_mista_rxintr(emc);
  254. }
  255. }
  256. static void emc_halt_tx(NPCM7xxEMCState *emc, uint32_t mista_flag)
  257. {
  258. emc->tx_active = false;
  259. emc_set_mista(emc, mista_flag);
  260. }
  261. static void emc_halt_rx(NPCM7xxEMCState *emc, uint32_t mista_flag)
  262. {
  263. emc->rx_active = false;
  264. emc_set_mista(emc, mista_flag);
  265. }
  266. static void emc_enable_rx_and_flush(NPCM7xxEMCState *emc)
  267. {
  268. emc->rx_active = true;
  269. qemu_flush_queued_packets(qemu_get_queue(emc->nic));
  270. }
  271. static void emc_set_next_tx_descriptor(NPCM7xxEMCState *emc,
  272. const NPCM7xxEMCTxDesc *tx_desc,
  273. uint32_t desc_addr)
  274. {
  275. /* Update the current descriptor, if only to reset the owner flag. */
  276. if (emc_write_tx_desc(tx_desc, desc_addr)) {
  277. /*
  278. * We just read it so this shouldn't generally happen.
  279. * Error already reported.
  280. */
  281. emc_set_mista(emc, REG_MISTA_TXBERR);
  282. }
  283. emc->regs[REG_CTXDSA] = TX_DESC_NTXDSA(tx_desc->ntxdsa);
  284. }
  285. static void emc_set_next_rx_descriptor(NPCM7xxEMCState *emc,
  286. const NPCM7xxEMCRxDesc *rx_desc,
  287. uint32_t desc_addr)
  288. {
  289. /* Update the current descriptor, if only to reset the owner flag. */
  290. if (emc_write_rx_desc(rx_desc, desc_addr)) {
  291. /*
  292. * We just read it so this shouldn't generally happen.
  293. * Error already reported.
  294. */
  295. emc_set_mista(emc, REG_MISTA_RXBERR);
  296. }
  297. emc->regs[REG_CRXDSA] = RX_DESC_NRXDSA(rx_desc->nrxdsa);
  298. }
  299. static void emc_try_send_next_packet(NPCM7xxEMCState *emc)
  300. {
  301. /* Working buffer for sending out packets. Most packets fit in this. */
  302. #define TX_BUFFER_SIZE 2048
  303. uint8_t tx_send_buffer[TX_BUFFER_SIZE];
  304. uint32_t desc_addr = TX_DESC_NTXDSA(emc->regs[REG_CTXDSA]);
  305. NPCM7xxEMCTxDesc tx_desc;
  306. uint32_t next_buf_addr, length;
  307. uint8_t *buf;
  308. g_autofree uint8_t *malloced_buf = NULL;
  309. if (emc_read_tx_desc(desc_addr, &tx_desc)) {
  310. /* Error reading descriptor, already reported. */
  311. emc_halt_tx(emc, REG_MISTA_TXBERR);
  312. emc_update_tx_irq(emc);
  313. return;
  314. }
  315. /* Nothing we can do if we don't own the descriptor. */
  316. if (!(tx_desc.flags & TX_DESC_FLAG_OWNER_MASK)) {
  317. trace_npcm7xx_emc_cpu_owned_desc(desc_addr);
  318. emc_halt_tx(emc, REG_MISTA_TDU);
  319. emc_update_tx_irq(emc);
  320. return;
  321. }
  322. /* Give the descriptor back regardless of what happens. */
  323. tx_desc.flags &= ~TX_DESC_FLAG_OWNER_MASK;
  324. tx_desc.status_and_length &= 0xffff;
  325. /*
  326. * Despite the h/w documentation saying the tx buffer is word aligned,
  327. * the linux driver does not word align the buffer. There is value in not
  328. * aligning the buffer: See the description of NET_IP_ALIGN in linux
  329. * kernel sources.
  330. */
  331. next_buf_addr = tx_desc.txbsa;
  332. emc->regs[REG_CTXBSA] = next_buf_addr;
  333. length = TX_DESC_PKT_LEN(tx_desc.status_and_length);
  334. buf = &tx_send_buffer[0];
  335. if (length > sizeof(tx_send_buffer)) {
  336. malloced_buf = g_malloc(length);
  337. buf = malloced_buf;
  338. }
  339. if (dma_memory_read(&address_space_memory, next_buf_addr, buf,
  340. length, MEMTXATTRS_UNSPECIFIED)) {
  341. qemu_log_mask(LOG_GUEST_ERROR, "%s: Failed to read packet @ 0x%x\n",
  342. __func__, next_buf_addr);
  343. emc_set_mista(emc, REG_MISTA_TXBERR);
  344. emc_set_next_tx_descriptor(emc, &tx_desc, desc_addr);
  345. emc_update_tx_irq(emc);
  346. trace_npcm7xx_emc_tx_done(emc->regs[REG_CTXDSA]);
  347. return;
  348. }
  349. if ((tx_desc.flags & TX_DESC_FLAG_PADEN) && (length < MIN_PACKET_LENGTH)) {
  350. memset(buf + length, 0, MIN_PACKET_LENGTH - length);
  351. length = MIN_PACKET_LENGTH;
  352. }
  353. /* N.B. emc_receive can get called here. */
  354. qemu_send_packet(qemu_get_queue(emc->nic), buf, length);
  355. trace_npcm7xx_emc_sent_packet(length);
  356. tx_desc.status_and_length |= TX_DESC_STATUS_TXCP;
  357. if (tx_desc.flags & TX_DESC_FLAG_INTEN) {
  358. emc_set_mista(emc, REG_MISTA_TXCP);
  359. }
  360. if (emc->regs[REG_MISTA] & emc->regs[REG_MIEN] & REG_MISTA_TXINTR) {
  361. tx_desc.status_and_length |= TX_DESC_STATUS_TXINTR;
  362. }
  363. emc_set_next_tx_descriptor(emc, &tx_desc, desc_addr);
  364. emc_update_tx_irq(emc);
  365. trace_npcm7xx_emc_tx_done(emc->regs[REG_CTXDSA]);
  366. }
  367. static bool emc_can_receive(NetClientState *nc)
  368. {
  369. NPCM7xxEMCState *emc = NPCM7XX_EMC(qemu_get_nic_opaque(nc));
  370. bool can_receive = emc->rx_active;
  371. trace_npcm7xx_emc_can_receive(can_receive);
  372. return can_receive;
  373. }
  374. /* If result is false then *fail_reason contains the reason. */
  375. static bool emc_receive_filter1(NPCM7xxEMCState *emc, const uint8_t *buf,
  376. size_t len, const char **fail_reason)
  377. {
  378. eth_pkt_types_e pkt_type = get_eth_packet_type(PKT_GET_ETH_HDR(buf));
  379. switch (pkt_type) {
  380. case ETH_PKT_BCAST:
  381. if (emc->regs[REG_CAMCMR] & REG_CAMCMR_CCAM) {
  382. return true;
  383. } else {
  384. *fail_reason = "Broadcast packet disabled";
  385. return !!(emc->regs[REG_CAMCMR] & REG_CAMCMR_ABP);
  386. }
  387. case ETH_PKT_MCAST:
  388. if (emc->regs[REG_CAMCMR] & REG_CAMCMR_CCAM) {
  389. return true;
  390. } else {
  391. *fail_reason = "Multicast packet disabled";
  392. return !!(emc->regs[REG_CAMCMR] & REG_CAMCMR_AMP);
  393. }
  394. case ETH_PKT_UCAST: {
  395. bool matches;
  396. uint32_t value;
  397. struct MACAddr mac;
  398. if (emc->regs[REG_CAMCMR] & REG_CAMCMR_AUP) {
  399. return true;
  400. }
  401. value = emc->regs[REG_CAMM_BASE];
  402. mac.a[0] = value >> 24;
  403. mac.a[1] = value >> 16;
  404. mac.a[2] = value >> 8;
  405. mac.a[3] = value >> 0;
  406. value = emc->regs[REG_CAML_BASE];
  407. mac.a[4] = value >> 24;
  408. mac.a[5] = value >> 16;
  409. matches = ((emc->regs[REG_CAMCMR] & REG_CAMCMR_ECMP) &&
  410. /* We only support one CAM register, CAM0. */
  411. (emc->regs[REG_CAMEN] & (1 << 0)) &&
  412. memcmp(buf, mac.a, ETH_ALEN) == 0);
  413. if (emc->regs[REG_CAMCMR] & REG_CAMCMR_CCAM) {
  414. *fail_reason = "MACADDR matched, comparison complemented";
  415. return !matches;
  416. } else {
  417. *fail_reason = "MACADDR didn't match";
  418. return matches;
  419. }
  420. }
  421. default:
  422. g_assert_not_reached();
  423. }
  424. }
  425. static bool emc_receive_filter(NPCM7xxEMCState *emc, const uint8_t *buf,
  426. size_t len)
  427. {
  428. const char *fail_reason = NULL;
  429. bool ok = emc_receive_filter1(emc, buf, len, &fail_reason);
  430. if (!ok) {
  431. trace_npcm7xx_emc_packet_filtered_out(fail_reason);
  432. }
  433. return ok;
  434. }
  435. static ssize_t emc_receive(NetClientState *nc, const uint8_t *buf, size_t len1)
  436. {
  437. NPCM7xxEMCState *emc = NPCM7XX_EMC(qemu_get_nic_opaque(nc));
  438. const uint32_t len = len1;
  439. size_t max_frame_len;
  440. bool long_frame;
  441. uint32_t desc_addr;
  442. NPCM7xxEMCRxDesc rx_desc;
  443. uint32_t crc;
  444. uint8_t *crc_ptr;
  445. uint32_t buf_addr;
  446. trace_npcm7xx_emc_receiving_packet(len);
  447. if (!emc_can_receive(nc)) {
  448. qemu_log_mask(LOG_GUEST_ERROR, "%s: Unexpected packet\n", __func__);
  449. return -1;
  450. }
  451. if (len < ETH_HLEN ||
  452. /* Defensive programming: drop unsupportable large packets. */
  453. len > 0xffff - CRC_LENGTH) {
  454. qemu_log_mask(LOG_GUEST_ERROR, "%s: Dropped frame of %u bytes\n",
  455. __func__, len);
  456. return len;
  457. }
  458. /*
  459. * DENI is set if EMC received the Length/Type field of the incoming
  460. * packet, so it will be set regardless of what happens next.
  461. */
  462. emc_set_mista(emc, REG_MISTA_DENI);
  463. if (!emc_receive_filter(emc, buf, len)) {
  464. emc_update_rx_irq(emc);
  465. return len;
  466. }
  467. /* Huge frames (> DMARFC) are dropped. */
  468. max_frame_len = REG_DMARFC_RXMS(emc->regs[REG_DMARFC]);
  469. if (len + CRC_LENGTH > max_frame_len) {
  470. trace_npcm7xx_emc_packet_dropped(len);
  471. emc_set_mista(emc, REG_MISTA_DFOI);
  472. emc_update_rx_irq(emc);
  473. return len;
  474. }
  475. /*
  476. * Long Frames (> MAX_ETH_FRAME_SIZE) are also dropped, unless MCMDR.ALP
  477. * is set.
  478. */
  479. long_frame = false;
  480. if (len + CRC_LENGTH > MAX_ETH_FRAME_SIZE) {
  481. if (emc->regs[REG_MCMDR] & REG_MCMDR_ALP) {
  482. long_frame = true;
  483. } else {
  484. trace_npcm7xx_emc_packet_dropped(len);
  485. emc_set_mista(emc, REG_MISTA_PTLE);
  486. emc_update_rx_irq(emc);
  487. return len;
  488. }
  489. }
  490. desc_addr = RX_DESC_NRXDSA(emc->regs[REG_CRXDSA]);
  491. if (emc_read_rx_desc(desc_addr, &rx_desc)) {
  492. /* Error reading descriptor, already reported. */
  493. emc_halt_rx(emc, REG_MISTA_RXBERR);
  494. emc_update_rx_irq(emc);
  495. return len;
  496. }
  497. /* Nothing we can do if we don't own the descriptor. */
  498. if (!(rx_desc.status_and_length & RX_DESC_STATUS_OWNER_MASK)) {
  499. trace_npcm7xx_emc_cpu_owned_desc(desc_addr);
  500. emc_halt_rx(emc, REG_MISTA_RDU);
  501. emc_update_rx_irq(emc);
  502. return len;
  503. }
  504. crc = 0;
  505. crc_ptr = (uint8_t *) &crc;
  506. if (!(emc->regs[REG_MCMDR] & REG_MCMDR_SPCRC)) {
  507. crc = cpu_to_be32(crc32(~0, buf, len));
  508. }
  509. /* Give the descriptor back regardless of what happens. */
  510. rx_desc.status_and_length &= ~RX_DESC_STATUS_OWNER_MASK;
  511. buf_addr = rx_desc.rxbsa;
  512. emc->regs[REG_CRXBSA] = buf_addr;
  513. if (dma_memory_write(&address_space_memory, buf_addr, buf,
  514. len, MEMTXATTRS_UNSPECIFIED) ||
  515. (!(emc->regs[REG_MCMDR] & REG_MCMDR_SPCRC) &&
  516. dma_memory_write(&address_space_memory, buf_addr + len,
  517. crc_ptr, 4, MEMTXATTRS_UNSPECIFIED))) {
  518. qemu_log_mask(LOG_GUEST_ERROR, "%s: Bus error writing packet\n",
  519. __func__);
  520. emc_set_mista(emc, REG_MISTA_RXBERR);
  521. emc_set_next_rx_descriptor(emc, &rx_desc, desc_addr);
  522. emc_update_rx_irq(emc);
  523. trace_npcm7xx_emc_rx_done(emc->regs[REG_CRXDSA]);
  524. return len;
  525. }
  526. trace_npcm7xx_emc_received_packet(len);
  527. /* Note: We've already verified len+4 <= 0xffff. */
  528. rx_desc.status_and_length = len;
  529. if (!(emc->regs[REG_MCMDR] & REG_MCMDR_SPCRC)) {
  530. rx_desc.status_and_length += 4;
  531. }
  532. rx_desc.status_and_length |= RX_DESC_STATUS_RXGD;
  533. emc_set_mista(emc, REG_MISTA_RXGD);
  534. if (emc->regs[REG_MISTA] & emc->regs[REG_MIEN] & REG_MISTA_RXINTR) {
  535. rx_desc.status_and_length |= RX_DESC_STATUS_RXINTR;
  536. }
  537. if (long_frame) {
  538. rx_desc.status_and_length |= RX_DESC_STATUS_PTLE;
  539. }
  540. emc_set_next_rx_descriptor(emc, &rx_desc, desc_addr);
  541. emc_update_rx_irq(emc);
  542. trace_npcm7xx_emc_rx_done(emc->regs[REG_CRXDSA]);
  543. return len;
  544. }
  545. static uint64_t npcm7xx_emc_read(void *opaque, hwaddr offset, unsigned size)
  546. {
  547. NPCM7xxEMCState *emc = opaque;
  548. uint32_t reg = offset / sizeof(uint32_t);
  549. uint32_t result;
  550. if (reg >= NPCM7XX_NUM_EMC_REGS) {
  551. qemu_log_mask(LOG_GUEST_ERROR,
  552. "%s: Invalid offset 0x%04" HWADDR_PRIx "\n",
  553. __func__, offset);
  554. return 0;
  555. }
  556. switch (reg) {
  557. case REG_MIID:
  558. /*
  559. * We don't implement MII. For determinism, always return zero as
  560. * writes record the last value written for debugging purposes.
  561. */
  562. qemu_log_mask(LOG_UNIMP, "%s: Read of MIID, returning 0\n", __func__);
  563. result = 0;
  564. break;
  565. case REG_TSDR:
  566. case REG_RSDR:
  567. qemu_log_mask(LOG_GUEST_ERROR,
  568. "%s: Read of write-only reg, %s/%d\n",
  569. __func__, emc_reg_name(reg), reg);
  570. return 0;
  571. default:
  572. result = emc->regs[reg];
  573. break;
  574. }
  575. trace_npcm7xx_emc_reg_read(emc->emc_num, result, emc_reg_name(reg), reg);
  576. return result;
  577. }
  578. static void npcm7xx_emc_write(void *opaque, hwaddr offset,
  579. uint64_t v, unsigned size)
  580. {
  581. NPCM7xxEMCState *emc = opaque;
  582. uint32_t reg = offset / sizeof(uint32_t);
  583. uint32_t value = v;
  584. g_assert(size == sizeof(uint32_t));
  585. if (reg >= NPCM7XX_NUM_EMC_REGS) {
  586. qemu_log_mask(LOG_GUEST_ERROR,
  587. "%s: Invalid offset 0x%04" HWADDR_PRIx "\n",
  588. __func__, offset);
  589. return;
  590. }
  591. trace_npcm7xx_emc_reg_write(emc->emc_num, emc_reg_name(reg), reg, value);
  592. switch (reg) {
  593. case REG_CAMCMR:
  594. emc->regs[reg] = value;
  595. break;
  596. case REG_CAMEN:
  597. /* Only CAM0 is supported, don't pretend otherwise. */
  598. if (value & ~1) {
  599. qemu_log_mask(LOG_GUEST_ERROR,
  600. "%s: Only CAM0 is supported, cannot enable others"
  601. ": 0x%x\n",
  602. __func__, value);
  603. }
  604. emc->regs[reg] = value & 1;
  605. break;
  606. case REG_CAMM_BASE + 0:
  607. emc->regs[reg] = value;
  608. break;
  609. case REG_CAML_BASE + 0:
  610. emc->regs[reg] = value;
  611. break;
  612. case REG_MCMDR: {
  613. uint32_t prev;
  614. if (value & REG_MCMDR_SWR) {
  615. emc_soft_reset(emc);
  616. /* On h/w the reset happens over multiple cycles. For now KISS. */
  617. break;
  618. }
  619. prev = emc->regs[reg];
  620. emc->regs[reg] = value;
  621. /* Update tx state. */
  622. if (!(prev & REG_MCMDR_TXON) &&
  623. (value & REG_MCMDR_TXON)) {
  624. emc->regs[REG_CTXDSA] = emc->regs[REG_TXDLSA];
  625. /*
  626. * Linux kernel turns TX on with CPU still holding descriptor,
  627. * which suggests we should wait for a write to TSDR before trying
  628. * to send a packet: so we don't send one here.
  629. */
  630. } else if ((prev & REG_MCMDR_TXON) &&
  631. !(value & REG_MCMDR_TXON)) {
  632. emc->regs[REG_MGSTA] |= REG_MGSTA_TXHA;
  633. }
  634. if (!(value & REG_MCMDR_TXON)) {
  635. emc_halt_tx(emc, 0);
  636. }
  637. /* Update rx state. */
  638. if (!(prev & REG_MCMDR_RXON) &&
  639. (value & REG_MCMDR_RXON)) {
  640. emc->regs[REG_CRXDSA] = emc->regs[REG_RXDLSA];
  641. } else if ((prev & REG_MCMDR_RXON) &&
  642. !(value & REG_MCMDR_RXON)) {
  643. emc->regs[REG_MGSTA] |= REG_MGSTA_RXHA;
  644. }
  645. if (value & REG_MCMDR_RXON) {
  646. emc_enable_rx_and_flush(emc);
  647. } else {
  648. emc_halt_rx(emc, 0);
  649. }
  650. break;
  651. }
  652. case REG_TXDLSA:
  653. case REG_RXDLSA:
  654. case REG_DMARFC:
  655. case REG_MIID:
  656. emc->regs[reg] = value;
  657. break;
  658. case REG_MIEN:
  659. emc->regs[reg] = value;
  660. emc_update_irq_from_reg_change(emc);
  661. break;
  662. case REG_MISTA:
  663. /* Clear the bits that have 1 in "value". */
  664. emc->regs[reg] &= ~value;
  665. emc_update_irq_from_reg_change(emc);
  666. break;
  667. case REG_MGSTA:
  668. /* Clear the bits that have 1 in "value". */
  669. emc->regs[reg] &= ~value;
  670. break;
  671. case REG_TSDR:
  672. if (emc->regs[REG_MCMDR] & REG_MCMDR_TXON) {
  673. emc->tx_active = true;
  674. /* Keep trying to send packets until we run out. */
  675. while (emc->tx_active) {
  676. emc_try_send_next_packet(emc);
  677. }
  678. }
  679. break;
  680. case REG_RSDR:
  681. if (emc->regs[REG_MCMDR] & REG_MCMDR_RXON) {
  682. emc_enable_rx_and_flush(emc);
  683. }
  684. break;
  685. case REG_MIIDA:
  686. emc->regs[reg] = value & ~REG_MIIDA_BUSY;
  687. break;
  688. case REG_MRPC:
  689. case REG_MRPCC:
  690. case REG_MREPC:
  691. case REG_CTXDSA:
  692. case REG_CTXBSA:
  693. case REG_CRXDSA:
  694. case REG_CRXBSA:
  695. qemu_log_mask(LOG_GUEST_ERROR,
  696. "%s: Write to read-only reg %s/%d\n",
  697. __func__, emc_reg_name(reg), reg);
  698. break;
  699. default:
  700. qemu_log_mask(LOG_UNIMP, "%s: Write to unimplemented reg %s/%d\n",
  701. __func__, emc_reg_name(reg), reg);
  702. break;
  703. }
  704. }
  705. static const struct MemoryRegionOps npcm7xx_emc_ops = {
  706. .read = npcm7xx_emc_read,
  707. .write = npcm7xx_emc_write,
  708. .endianness = DEVICE_LITTLE_ENDIAN,
  709. .valid = {
  710. .min_access_size = 4,
  711. .max_access_size = 4,
  712. .unaligned = false,
  713. },
  714. };
  715. static void emc_cleanup(NetClientState *nc)
  716. {
  717. /* Nothing to do yet. */
  718. }
  719. static NetClientInfo net_npcm7xx_emc_info = {
  720. .type = NET_CLIENT_DRIVER_NIC,
  721. .size = sizeof(NICState),
  722. .can_receive = emc_can_receive,
  723. .receive = emc_receive,
  724. .cleanup = emc_cleanup,
  725. .link_status_changed = emc_set_link,
  726. };
  727. static void npcm7xx_emc_realize(DeviceState *dev, Error **errp)
  728. {
  729. NPCM7xxEMCState *emc = NPCM7XX_EMC(dev);
  730. SysBusDevice *sbd = SYS_BUS_DEVICE(emc);
  731. memory_region_init_io(&emc->iomem, OBJECT(emc), &npcm7xx_emc_ops, emc,
  732. TYPE_NPCM7XX_EMC, 4 * KiB);
  733. sysbus_init_mmio(sbd, &emc->iomem);
  734. sysbus_init_irq(sbd, &emc->tx_irq);
  735. sysbus_init_irq(sbd, &emc->rx_irq);
  736. qemu_macaddr_default_if_unset(&emc->conf.macaddr);
  737. emc->nic = qemu_new_nic(&net_npcm7xx_emc_info, &emc->conf,
  738. object_get_typename(OBJECT(dev)), dev->id,
  739. &dev->mem_reentrancy_guard, emc);
  740. qemu_format_nic_info_str(qemu_get_queue(emc->nic), emc->conf.macaddr.a);
  741. }
  742. static void npcm7xx_emc_unrealize(DeviceState *dev)
  743. {
  744. NPCM7xxEMCState *emc = NPCM7XX_EMC(dev);
  745. qemu_del_nic(emc->nic);
  746. }
  747. static const VMStateDescription vmstate_npcm7xx_emc = {
  748. .name = TYPE_NPCM7XX_EMC,
  749. .version_id = 0,
  750. .minimum_version_id = 0,
  751. .fields = (const VMStateField[]) {
  752. VMSTATE_UINT8(emc_num, NPCM7xxEMCState),
  753. VMSTATE_UINT32_ARRAY(regs, NPCM7xxEMCState, NPCM7XX_NUM_EMC_REGS),
  754. VMSTATE_BOOL(tx_active, NPCM7xxEMCState),
  755. VMSTATE_BOOL(rx_active, NPCM7xxEMCState),
  756. VMSTATE_END_OF_LIST(),
  757. },
  758. };
  759. static const Property npcm7xx_emc_properties[] = {
  760. DEFINE_NIC_PROPERTIES(NPCM7xxEMCState, conf),
  761. };
  762. static void npcm7xx_emc_class_init(ObjectClass *klass, void *data)
  763. {
  764. DeviceClass *dc = DEVICE_CLASS(klass);
  765. set_bit(DEVICE_CATEGORY_NETWORK, dc->categories);
  766. dc->desc = "NPCM7xx EMC Controller";
  767. dc->realize = npcm7xx_emc_realize;
  768. dc->unrealize = npcm7xx_emc_unrealize;
  769. device_class_set_legacy_reset(dc, npcm7xx_emc_reset);
  770. dc->vmsd = &vmstate_npcm7xx_emc;
  771. device_class_set_props(dc, npcm7xx_emc_properties);
  772. }
  773. static const TypeInfo npcm7xx_emc_info = {
  774. .name = TYPE_NPCM7XX_EMC,
  775. .parent = TYPE_SYS_BUS_DEVICE,
  776. .instance_size = sizeof(NPCM7xxEMCState),
  777. .class_init = npcm7xx_emc_class_init,
  778. };
  779. static void npcm7xx_emc_register_type(void)
  780. {
  781. type_register_static(&npcm7xx_emc_info);
  782. }
  783. type_init(npcm7xx_emc_register_type)