rings.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651
  1. /*
  2. * QEMU Freescale eTSEC Emulator
  3. *
  4. * Copyright (c) 2011-2013 AdaCore
  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 "net/checksum.h"
  26. #include "qemu/log.h"
  27. #include "etsec.h"
  28. #include "registers.h"
  29. /* #define ETSEC_RING_DEBUG */
  30. /* #define HEX_DUMP */
  31. /* #define DEBUG_BD */
  32. #ifdef ETSEC_RING_DEBUG
  33. static const int debug_etsec = 1;
  34. #else
  35. static const int debug_etsec;
  36. #endif
  37. #define RING_DEBUG(fmt, ...) do { \
  38. if (debug_etsec) { \
  39. qemu_log(fmt , ## __VA_ARGS__); \
  40. } \
  41. } while (0)
  42. #ifdef DEBUG_BD
  43. static void print_tx_bd_flags(uint16_t flags)
  44. {
  45. qemu_log(" Ready: %d\n", !!(flags & BD_TX_READY));
  46. qemu_log(" PAD/CRC: %d\n", !!(flags & BD_TX_PADCRC));
  47. qemu_log(" Wrap: %d\n", !!(flags & BD_WRAP));
  48. qemu_log(" Interrupt: %d\n", !!(flags & BD_INTERRUPT));
  49. qemu_log(" Last in frame: %d\n", !!(flags & BD_LAST));
  50. qemu_log(" Tx CRC: %d\n", !!(flags & BD_TX_TC));
  51. qemu_log(" User-defined preamble / defer: %d\n",
  52. !!(flags & BD_TX_PREDEF));
  53. qemu_log(" Huge frame enable / Late collision: %d\n",
  54. !!(flags & BD_TX_HFELC));
  55. qemu_log(" Control frame / Retransmission Limit: %d\n",
  56. !!(flags & BD_TX_CFRL));
  57. qemu_log(" Retry count: %d\n",
  58. (flags >> BD_TX_RC_OFFSET) & BD_TX_RC_MASK);
  59. qemu_log(" Underrun / TCP/IP off-load enable: %d\n",
  60. !!(flags & BD_TX_TOEUN));
  61. qemu_log(" Truncation: %d\n", !!(flags & BD_TX_TR));
  62. }
  63. static void print_rx_bd_flags(uint16_t flags)
  64. {
  65. qemu_log(" Empty: %d\n", !!(flags & BD_RX_EMPTY));
  66. qemu_log(" Receive software ownership: %d\n", !!(flags & BD_RX_RO1));
  67. qemu_log(" Wrap: %d\n", !!(flags & BD_WRAP));
  68. qemu_log(" Interrupt: %d\n", !!(flags & BD_INTERRUPT));
  69. qemu_log(" Last in frame: %d\n", !!(flags & BD_LAST));
  70. qemu_log(" First in frame: %d\n", !!(flags & BD_RX_FIRST));
  71. qemu_log(" Miss: %d\n", !!(flags & BD_RX_MISS));
  72. qemu_log(" Broadcast: %d\n", !!(flags & BD_RX_BROADCAST));
  73. qemu_log(" Multicast: %d\n", !!(flags & BD_RX_MULTICAST));
  74. qemu_log(" Rx frame length violation: %d\n", !!(flags & BD_RX_LG));
  75. qemu_log(" Rx non-octet aligned frame: %d\n", !!(flags & BD_RX_NO));
  76. qemu_log(" Short frame: %d\n", !!(flags & BD_RX_SH));
  77. qemu_log(" Rx CRC Error: %d\n", !!(flags & BD_RX_CR));
  78. qemu_log(" Overrun: %d\n", !!(flags & BD_RX_OV));
  79. qemu_log(" Truncation: %d\n", !!(flags & BD_RX_TR));
  80. }
  81. static void print_bd(eTSEC_rxtx_bd bd, int mode, uint32_t index)
  82. {
  83. qemu_log("eTSEC %s Data Buffer Descriptor (%u)\n",
  84. mode == eTSEC_TRANSMIT ? "Transmit" : "Receive",
  85. index);
  86. qemu_log(" Flags : 0x%04x\n", bd.flags);
  87. if (mode == eTSEC_TRANSMIT) {
  88. print_tx_bd_flags(bd.flags);
  89. } else {
  90. print_rx_bd_flags(bd.flags);
  91. }
  92. qemu_log(" Length : 0x%04x\n", bd.length);
  93. qemu_log(" Pointer : 0x%08x\n", bd.bufptr);
  94. }
  95. #endif /* DEBUG_BD */
  96. static void read_buffer_descriptor(eTSEC *etsec,
  97. hwaddr addr,
  98. eTSEC_rxtx_bd *bd)
  99. {
  100. assert(bd != NULL);
  101. RING_DEBUG("READ Buffer Descriptor @ 0x" TARGET_FMT_plx"\n", addr);
  102. cpu_physical_memory_read(addr,
  103. bd,
  104. sizeof(eTSEC_rxtx_bd));
  105. if (etsec->regs[DMACTRL].value & DMACTRL_LE) {
  106. bd->flags = lduw_le_p(&bd->flags);
  107. bd->length = lduw_le_p(&bd->length);
  108. bd->bufptr = ldl_le_p(&bd->bufptr);
  109. } else {
  110. bd->flags = lduw_be_p(&bd->flags);
  111. bd->length = lduw_be_p(&bd->length);
  112. bd->bufptr = ldl_be_p(&bd->bufptr);
  113. }
  114. }
  115. static void write_buffer_descriptor(eTSEC *etsec,
  116. hwaddr addr,
  117. eTSEC_rxtx_bd *bd)
  118. {
  119. assert(bd != NULL);
  120. if (etsec->regs[DMACTRL].value & DMACTRL_LE) {
  121. stw_le_p(&bd->flags, bd->flags);
  122. stw_le_p(&bd->length, bd->length);
  123. stl_le_p(&bd->bufptr, bd->bufptr);
  124. } else {
  125. stw_be_p(&bd->flags, bd->flags);
  126. stw_be_p(&bd->length, bd->length);
  127. stl_be_p(&bd->bufptr, bd->bufptr);
  128. }
  129. RING_DEBUG("Write Buffer Descriptor @ 0x" TARGET_FMT_plx"\n", addr);
  130. cpu_physical_memory_write(addr,
  131. bd,
  132. sizeof(eTSEC_rxtx_bd));
  133. }
  134. static void ievent_set(eTSEC *etsec,
  135. uint32_t flags)
  136. {
  137. etsec->regs[IEVENT].value |= flags;
  138. etsec_update_irq(etsec);
  139. }
  140. static void tx_padding_and_crc(eTSEC *etsec, uint32_t min_frame_len)
  141. {
  142. int add = min_frame_len - etsec->tx_buffer_len;
  143. /* Padding */
  144. if (add > 0) {
  145. RING_DEBUG("pad:%u\n", add);
  146. etsec->tx_buffer = g_realloc(etsec->tx_buffer,
  147. etsec->tx_buffer_len + add);
  148. memset(etsec->tx_buffer + etsec->tx_buffer_len, 0x0, add);
  149. etsec->tx_buffer_len += add;
  150. }
  151. /* Never add CRC in QEMU */
  152. }
  153. static void process_tx_fcb(eTSEC *etsec)
  154. {
  155. uint8_t flags = (uint8_t)(*etsec->tx_buffer);
  156. /* L3 header offset from start of frame */
  157. uint8_t l3_header_offset = (uint8_t)*(etsec->tx_buffer + 3);
  158. /* L4 header offset from start of L3 header */
  159. uint8_t l4_header_offset = (uint8_t)*(etsec->tx_buffer + 2);
  160. /* L3 header */
  161. uint8_t *l3_header = etsec->tx_buffer + 8 + l3_header_offset;
  162. /* L4 header */
  163. uint8_t *l4_header = l3_header + l4_header_offset;
  164. int csum = 0;
  165. /* if packet is IP4 and IP checksum is requested */
  166. if (flags & FCB_TX_IP && flags & FCB_TX_CIP) {
  167. csum |= CSUM_IP;
  168. }
  169. /* TODO Check the correct usage of the PHCS field of the FCB in case the NPH
  170. * flag is on */
  171. /* if packet is IP4 and TCP or UDP */
  172. if (flags & FCB_TX_IP && flags & FCB_TX_TUP) {
  173. /* if UDP */
  174. if (flags & FCB_TX_UDP) {
  175. /* if checksum is requested */
  176. if (flags & FCB_TX_CTU) {
  177. /* do UDP checksum */
  178. csum |= CSUM_UDP;
  179. } else {
  180. /* set checksum field to 0 */
  181. l4_header[6] = 0;
  182. l4_header[7] = 0;
  183. }
  184. } else if (flags & FCB_TX_CTU) { /* if TCP and checksum is requested */
  185. /* do TCP checksum */
  186. csum |= CSUM_TCP;
  187. }
  188. }
  189. if (csum) {
  190. net_checksum_calculate(etsec->tx_buffer + 8,
  191. etsec->tx_buffer_len - 8, csum);
  192. }
  193. }
  194. static void process_tx_bd(eTSEC *etsec,
  195. eTSEC_rxtx_bd *bd)
  196. {
  197. uint8_t *tmp_buff = NULL;
  198. hwaddr tbdbth = (hwaddr)(etsec->regs[TBDBPH].value & 0xF) << 32;
  199. if (bd->length == 0) {
  200. /* ERROR */
  201. return;
  202. }
  203. if (etsec->tx_buffer_len == 0) {
  204. /* It's the first BD */
  205. etsec->first_bd = *bd;
  206. }
  207. /* TODO: if TxBD[TOE/UN] skip the Tx Frame Control Block*/
  208. /* Load this Data Buffer */
  209. etsec->tx_buffer = g_realloc(etsec->tx_buffer,
  210. etsec->tx_buffer_len + bd->length);
  211. tmp_buff = etsec->tx_buffer + etsec->tx_buffer_len;
  212. cpu_physical_memory_read(bd->bufptr + tbdbth, tmp_buff, bd->length);
  213. /* Update buffer length */
  214. etsec->tx_buffer_len += bd->length;
  215. if (etsec->tx_buffer_len != 0 && (bd->flags & BD_LAST)) {
  216. if (etsec->regs[MACCFG1].value & MACCFG1_TX_EN) {
  217. /* MAC Transmit enabled */
  218. /* Process offload Tx FCB */
  219. if (etsec->first_bd.flags & BD_TX_TOEUN) {
  220. process_tx_fcb(etsec);
  221. }
  222. if (etsec->first_bd.flags & BD_TX_PADCRC
  223. || etsec->regs[MACCFG2].value & MACCFG2_PADCRC) {
  224. /* Padding and CRC (Padding implies CRC) */
  225. tx_padding_and_crc(etsec, 60);
  226. } else if (etsec->first_bd.flags & BD_TX_TC
  227. || etsec->regs[MACCFG2].value & MACCFG2_CRC_EN) {
  228. /* Only CRC */
  229. /* Never add CRC in QEMU */
  230. }
  231. #if defined(HEX_DUMP)
  232. qemu_log("eTSEC Send packet size:%d\n", etsec->tx_buffer_len);
  233. qemu_hexdump(stderr, "", etsec->tx_buffer, etsec->tx_buffer_len);
  234. #endif /* ETSEC_RING_DEBUG */
  235. if (etsec->first_bd.flags & BD_TX_TOEUN) {
  236. qemu_send_packet(qemu_get_queue(etsec->nic),
  237. etsec->tx_buffer + 8,
  238. etsec->tx_buffer_len - 8);
  239. } else {
  240. qemu_send_packet(qemu_get_queue(etsec->nic),
  241. etsec->tx_buffer,
  242. etsec->tx_buffer_len);
  243. }
  244. }
  245. etsec->tx_buffer_len = 0;
  246. if (bd->flags & BD_INTERRUPT) {
  247. ievent_set(etsec, IEVENT_TXF);
  248. }
  249. } else {
  250. if (bd->flags & BD_INTERRUPT) {
  251. ievent_set(etsec, IEVENT_TXB);
  252. }
  253. }
  254. /* Update DB flags */
  255. /* Clear Ready */
  256. bd->flags &= ~BD_TX_READY;
  257. /* Clear Defer */
  258. bd->flags &= ~BD_TX_PREDEF;
  259. /* Clear Late Collision */
  260. bd->flags &= ~BD_TX_HFELC;
  261. /* Clear Retransmission Limit */
  262. bd->flags &= ~BD_TX_CFRL;
  263. /* Clear Retry Count */
  264. bd->flags &= ~(BD_TX_RC_MASK << BD_TX_RC_OFFSET);
  265. /* Clear Underrun */
  266. bd->flags &= ~BD_TX_TOEUN;
  267. /* Clear Truncation */
  268. bd->flags &= ~BD_TX_TR;
  269. }
  270. void etsec_walk_tx_ring(eTSEC *etsec, int ring_nbr)
  271. {
  272. hwaddr ring_base = 0;
  273. hwaddr bd_addr = 0;
  274. eTSEC_rxtx_bd bd;
  275. uint16_t bd_flags;
  276. if (!(etsec->regs[MACCFG1].value & MACCFG1_TX_EN)) {
  277. RING_DEBUG("%s: MAC Transmit not enabled\n", __func__);
  278. return;
  279. }
  280. ring_base = (hwaddr)(etsec->regs[TBASEH].value & 0xF) << 32;
  281. ring_base += etsec->regs[TBASE0 + ring_nbr].value & ~0x7;
  282. bd_addr = etsec->regs[TBPTR0 + ring_nbr].value & ~0x7;
  283. do {
  284. read_buffer_descriptor(etsec, bd_addr, &bd);
  285. #ifdef DEBUG_BD
  286. print_bd(bd,
  287. eTSEC_TRANSMIT,
  288. (bd_addr - ring_base) / sizeof(eTSEC_rxtx_bd));
  289. #endif /* DEBUG_BD */
  290. /* Save flags before BD update */
  291. bd_flags = bd.flags;
  292. if (!(bd_flags & BD_TX_READY)) {
  293. break;
  294. }
  295. process_tx_bd(etsec, &bd);
  296. /* Write back BD after update */
  297. write_buffer_descriptor(etsec, bd_addr, &bd);
  298. /* Wrap or next BD */
  299. if (bd_flags & BD_WRAP) {
  300. bd_addr = ring_base;
  301. } else {
  302. bd_addr += sizeof(eTSEC_rxtx_bd);
  303. }
  304. } while (TRUE);
  305. /* Save the Buffer Descriptor Pointers to last bd that was not
  306. * succesfully closed */
  307. etsec->regs[TBPTR0 + ring_nbr].value = bd_addr;
  308. /* Set transmit halt THLTx */
  309. etsec->regs[TSTAT].value |= 1 << (31 - ring_nbr);
  310. }
  311. static void fill_rx_bd(eTSEC *etsec,
  312. eTSEC_rxtx_bd *bd,
  313. const uint8_t **buf,
  314. size_t *size)
  315. {
  316. uint16_t to_write;
  317. hwaddr bufptr = bd->bufptr +
  318. ((hwaddr)(etsec->regs[TBDBPH].value & 0xF) << 32);
  319. uint8_t padd[etsec->rx_padding];
  320. uint8_t rem;
  321. RING_DEBUG("eTSEC fill Rx buffer @ 0x%016" HWADDR_PRIx
  322. " size:%zu(padding + crc:%u) + fcb:%u\n",
  323. bufptr, *size, etsec->rx_padding, etsec->rx_fcb_size);
  324. bd->length = 0;
  325. /* This operation will only write FCB */
  326. if (etsec->rx_fcb_size != 0) {
  327. cpu_physical_memory_write(bufptr, etsec->rx_fcb, etsec->rx_fcb_size);
  328. bufptr += etsec->rx_fcb_size;
  329. bd->length += etsec->rx_fcb_size;
  330. etsec->rx_fcb_size = 0;
  331. }
  332. /* We remove padding from the computation of to_write because it is not
  333. * allocated in the buffer.
  334. */
  335. to_write = MIN(*size - etsec->rx_padding,
  336. etsec->regs[MRBLR].value - etsec->rx_fcb_size);
  337. /* This operation can only write packet data and no padding */
  338. if (to_write > 0) {
  339. cpu_physical_memory_write(bufptr, *buf, to_write);
  340. *buf += to_write;
  341. bufptr += to_write;
  342. *size -= to_write;
  343. bd->flags &= ~BD_RX_EMPTY;
  344. bd->length += to_write;
  345. }
  346. if (*size == etsec->rx_padding) {
  347. /* The remaining bytes are only for padding which is not actually
  348. * allocated in the data buffer.
  349. */
  350. rem = MIN(etsec->regs[MRBLR].value - bd->length, etsec->rx_padding);
  351. if (rem > 0) {
  352. memset(padd, 0x0, sizeof(padd));
  353. etsec->rx_padding -= rem;
  354. *size -= rem;
  355. bd->length += rem;
  356. cpu_physical_memory_write(bufptr, padd, rem);
  357. }
  358. }
  359. }
  360. static void rx_init_frame(eTSEC *etsec, const uint8_t *buf, size_t size)
  361. {
  362. uint32_t fcb_size = 0;
  363. uint8_t prsdep = (etsec->regs[RCTRL].value >> RCTRL_PRSDEP_OFFSET)
  364. & RCTRL_PRSDEP_MASK;
  365. if (prsdep != 0) {
  366. /* Prepend FCB (FCB size + RCTRL[PAL]) */
  367. fcb_size = 8 + ((etsec->regs[RCTRL].value >> 16) & 0x1F);
  368. etsec->rx_fcb_size = fcb_size;
  369. /* TODO: fill_FCB(etsec); */
  370. memset(etsec->rx_fcb, 0x0, sizeof(etsec->rx_fcb));
  371. } else {
  372. etsec->rx_fcb_size = 0;
  373. }
  374. g_free(etsec->rx_buffer);
  375. /* Do not copy the frame for now */
  376. etsec->rx_buffer = (uint8_t *)buf;
  377. etsec->rx_buffer_len = size;
  378. /* CRC padding (We don't have to compute the CRC) */
  379. etsec->rx_padding = 4;
  380. /*
  381. * Ensure that payload length + CRC length is at least 802.3
  382. * minimum MTU size bytes long (64)
  383. */
  384. if (etsec->rx_buffer_len < 60) {
  385. etsec->rx_padding += 60 - etsec->rx_buffer_len;
  386. }
  387. etsec->rx_first_in_frame = 1;
  388. etsec->rx_remaining_data = etsec->rx_buffer_len;
  389. RING_DEBUG("%s: rx_buffer_len:%u rx_padding+crc:%u\n", __func__,
  390. etsec->rx_buffer_len, etsec->rx_padding);
  391. }
  392. ssize_t etsec_rx_ring_write(eTSEC *etsec, const uint8_t *buf, size_t size)
  393. {
  394. int ring_nbr = 0; /* Always use ring0 (no filer) */
  395. if (etsec->rx_buffer_len != 0) {
  396. RING_DEBUG("%s: We can't receive now,"
  397. " a buffer is already in the pipe\n", __func__);
  398. return 0;
  399. }
  400. if (etsec->regs[RSTAT].value & 1 << (23 - ring_nbr)) {
  401. RING_DEBUG("%s: The ring is halted\n", __func__);
  402. return -1;
  403. }
  404. if (etsec->regs[DMACTRL].value & DMACTRL_GRS) {
  405. RING_DEBUG("%s: Graceful receive stop\n", __func__);
  406. return -1;
  407. }
  408. if (!(etsec->regs[MACCFG1].value & MACCFG1_RX_EN)) {
  409. RING_DEBUG("%s: MAC Receive not enabled\n", __func__);
  410. return -1;
  411. }
  412. if (!(etsec->regs[RCTRL].value & RCTRL_RSF) && (size < 60)) {
  413. /* CRC is not in the packet yet, so short frame is below 60 bytes */
  414. RING_DEBUG("%s: Drop short frame\n", __func__);
  415. return -1;
  416. }
  417. rx_init_frame(etsec, buf, size);
  418. etsec_walk_rx_ring(etsec, ring_nbr);
  419. return size;
  420. }
  421. void etsec_walk_rx_ring(eTSEC *etsec, int ring_nbr)
  422. {
  423. hwaddr ring_base = 0;
  424. hwaddr bd_addr = 0;
  425. hwaddr start_bd_addr = 0;
  426. eTSEC_rxtx_bd bd;
  427. uint16_t bd_flags;
  428. size_t remaining_data;
  429. const uint8_t *buf;
  430. uint8_t *tmp_buf;
  431. size_t size;
  432. if (etsec->rx_buffer_len == 0) {
  433. /* No frame to send */
  434. RING_DEBUG("No frame to send\n");
  435. return;
  436. }
  437. remaining_data = etsec->rx_remaining_data + etsec->rx_padding;
  438. buf = etsec->rx_buffer
  439. + (etsec->rx_buffer_len - etsec->rx_remaining_data);
  440. size = etsec->rx_buffer_len + etsec->rx_padding;
  441. ring_base = (hwaddr)(etsec->regs[RBASEH].value & 0xF) << 32;
  442. ring_base += etsec->regs[RBASE0 + ring_nbr].value & ~0x7;
  443. start_bd_addr = bd_addr = etsec->regs[RBPTR0 + ring_nbr].value & ~0x7;
  444. do {
  445. read_buffer_descriptor(etsec, bd_addr, &bd);
  446. #ifdef DEBUG_BD
  447. print_bd(bd,
  448. eTSEC_RECEIVE,
  449. (bd_addr - ring_base) / sizeof(eTSEC_rxtx_bd));
  450. #endif /* DEBUG_BD */
  451. /* Save flags before BD update */
  452. bd_flags = bd.flags;
  453. if (bd_flags & BD_RX_EMPTY) {
  454. fill_rx_bd(etsec, &bd, &buf, &remaining_data);
  455. if (etsec->rx_first_in_frame) {
  456. bd.flags |= BD_RX_FIRST;
  457. etsec->rx_first_in_frame = 0;
  458. etsec->rx_first_bd = bd;
  459. }
  460. /* Last in frame */
  461. if (remaining_data == 0) {
  462. /* Clear flags */
  463. bd.flags &= ~0x7ff;
  464. bd.flags |= BD_LAST;
  465. /* NOTE: non-octet aligned frame is impossible in qemu */
  466. if (size >= etsec->regs[MAXFRM].value) {
  467. /* frame length violation */
  468. qemu_log("%s frame length violation: size:%zu MAXFRM:%d\n",
  469. __func__, size, etsec->regs[MAXFRM].value);
  470. bd.flags |= BD_RX_LG;
  471. }
  472. if (size < 64) {
  473. /* Short frame */
  474. bd.flags |= BD_RX_SH;
  475. }
  476. /* TODO: Broadcast and Multicast */
  477. if (bd.flags & BD_INTERRUPT) {
  478. /* Set RXFx */
  479. etsec->regs[RSTAT].value |= 1 << (7 - ring_nbr);
  480. /* Set IEVENT */
  481. ievent_set(etsec, IEVENT_RXF);
  482. }
  483. } else {
  484. if (bd.flags & BD_INTERRUPT) {
  485. /* Set IEVENT */
  486. ievent_set(etsec, IEVENT_RXB);
  487. }
  488. }
  489. /* Write back BD after update */
  490. write_buffer_descriptor(etsec, bd_addr, &bd);
  491. }
  492. /* Wrap or next BD */
  493. if (bd_flags & BD_WRAP) {
  494. bd_addr = ring_base;
  495. } else {
  496. bd_addr += sizeof(eTSEC_rxtx_bd);
  497. }
  498. } while (remaining_data != 0
  499. && (bd_flags & BD_RX_EMPTY)
  500. && bd_addr != start_bd_addr);
  501. /* Reset ring ptr */
  502. etsec->regs[RBPTR0 + ring_nbr].value = bd_addr;
  503. /* The frame is too large to fit in the Rx ring */
  504. if (remaining_data > 0) {
  505. /* Set RSTAT[QHLTx] */
  506. etsec->regs[RSTAT].value |= 1 << (23 - ring_nbr);
  507. /* Save remaining data to send the end of the frame when the ring will
  508. * be restarted
  509. */
  510. etsec->rx_remaining_data = remaining_data;
  511. /* Copy the frame */
  512. tmp_buf = g_malloc(size);
  513. memcpy(tmp_buf, etsec->rx_buffer, size);
  514. etsec->rx_buffer = tmp_buf;
  515. RING_DEBUG("no empty RxBD available any more\n");
  516. } else {
  517. etsec->rx_buffer_len = 0;
  518. etsec->rx_buffer = NULL;
  519. if (etsec->need_flush) {
  520. qemu_flush_queued_packets(qemu_get_queue(etsec->nic));
  521. }
  522. }
  523. RING_DEBUG("eTSEC End of ring_write: remaining_data:%zu\n", remaining_data);
  524. }