net: checksum: Introduce fine control over checksum type
[qemu/ar7.git] / hw / net / fsl_etsec / rings.c
blob121415abfead7f9adc17aa4e701b594f75394ce1
1 /*
2 * QEMU Freescale eTSEC Emulator
4 * Copyright (c) 2011-2013 AdaCore
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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.
24 #include "qemu/osdep.h"
25 #include "net/checksum.h"
26 #include "qemu/log.h"
27 #include "etsec.h"
28 #include "registers.h"
30 /* #define ETSEC_RING_DEBUG */
31 /* #define HEX_DUMP */
32 /* #define DEBUG_BD */
34 #ifdef ETSEC_RING_DEBUG
35 static const int debug_etsec = 1;
36 #else
37 static const int debug_etsec;
38 #endif
40 #define RING_DEBUG(fmt, ...) do { \
41 if (debug_etsec) { \
42 qemu_log(fmt , ## __VA_ARGS__); \
43 } \
44 } while (0)
46 #ifdef DEBUG_BD
48 static void print_tx_bd_flags(uint16_t flags)
50 qemu_log(" Ready: %d\n", !!(flags & BD_TX_READY));
51 qemu_log(" PAD/CRC: %d\n", !!(flags & BD_TX_PADCRC));
52 qemu_log(" Wrap: %d\n", !!(flags & BD_WRAP));
53 qemu_log(" Interrupt: %d\n", !!(flags & BD_INTERRUPT));
54 qemu_log(" Last in frame: %d\n", !!(flags & BD_LAST));
55 qemu_log(" Tx CRC: %d\n", !!(flags & BD_TX_TC));
56 qemu_log(" User-defined preamble / defer: %d\n",
57 !!(flags & BD_TX_PREDEF));
58 qemu_log(" Huge frame enable / Late collision: %d\n",
59 !!(flags & BD_TX_HFELC));
60 qemu_log(" Control frame / Retransmission Limit: %d\n",
61 !!(flags & BD_TX_CFRL));
62 qemu_log(" Retry count: %d\n",
63 (flags >> BD_TX_RC_OFFSET) & BD_TX_RC_MASK);
64 qemu_log(" Underrun / TCP/IP off-load enable: %d\n",
65 !!(flags & BD_TX_TOEUN));
66 qemu_log(" Truncation: %d\n", !!(flags & BD_TX_TR));
69 static void print_rx_bd_flags(uint16_t flags)
71 qemu_log(" Empty: %d\n", !!(flags & BD_RX_EMPTY));
72 qemu_log(" Receive software ownership: %d\n", !!(flags & BD_RX_RO1));
73 qemu_log(" Wrap: %d\n", !!(flags & BD_WRAP));
74 qemu_log(" Interrupt: %d\n", !!(flags & BD_INTERRUPT));
75 qemu_log(" Last in frame: %d\n", !!(flags & BD_LAST));
76 qemu_log(" First in frame: %d\n", !!(flags & BD_RX_FIRST));
77 qemu_log(" Miss: %d\n", !!(flags & BD_RX_MISS));
78 qemu_log(" Broadcast: %d\n", !!(flags & BD_RX_BROADCAST));
79 qemu_log(" Multicast: %d\n", !!(flags & BD_RX_MULTICAST));
80 qemu_log(" Rx frame length violation: %d\n", !!(flags & BD_RX_LG));
81 qemu_log(" Rx non-octet aligned frame: %d\n", !!(flags & BD_RX_NO));
82 qemu_log(" Short frame: %d\n", !!(flags & BD_RX_SH));
83 qemu_log(" Rx CRC Error: %d\n", !!(flags & BD_RX_CR));
84 qemu_log(" Overrun: %d\n", !!(flags & BD_RX_OV));
85 qemu_log(" Truncation: %d\n", !!(flags & BD_RX_TR));
89 static void print_bd(eTSEC_rxtx_bd bd, int mode, uint32_t index)
91 qemu_log("eTSEC %s Data Buffer Descriptor (%u)\n",
92 mode == eTSEC_TRANSMIT ? "Transmit" : "Receive",
93 index);
94 qemu_log(" Flags : 0x%04x\n", bd.flags);
95 if (mode == eTSEC_TRANSMIT) {
96 print_tx_bd_flags(bd.flags);
97 } else {
98 print_rx_bd_flags(bd.flags);
100 qemu_log(" Length : 0x%04x\n", bd.length);
101 qemu_log(" Pointer : 0x%08x\n", bd.bufptr);
104 #endif /* DEBUG_BD */
106 static void read_buffer_descriptor(eTSEC *etsec,
107 hwaddr addr,
108 eTSEC_rxtx_bd *bd)
110 assert(bd != NULL);
112 RING_DEBUG("READ Buffer Descriptor @ 0x" TARGET_FMT_plx"\n", addr);
113 cpu_physical_memory_read(addr,
115 sizeof(eTSEC_rxtx_bd));
117 if (etsec->regs[DMACTRL].value & DMACTRL_LE) {
118 bd->flags = lduw_le_p(&bd->flags);
119 bd->length = lduw_le_p(&bd->length);
120 bd->bufptr = ldl_le_p(&bd->bufptr);
121 } else {
122 bd->flags = lduw_be_p(&bd->flags);
123 bd->length = lduw_be_p(&bd->length);
124 bd->bufptr = ldl_be_p(&bd->bufptr);
128 static void write_buffer_descriptor(eTSEC *etsec,
129 hwaddr addr,
130 eTSEC_rxtx_bd *bd)
132 assert(bd != NULL);
134 if (etsec->regs[DMACTRL].value & DMACTRL_LE) {
135 stw_le_p(&bd->flags, bd->flags);
136 stw_le_p(&bd->length, bd->length);
137 stl_le_p(&bd->bufptr, bd->bufptr);
138 } else {
139 stw_be_p(&bd->flags, bd->flags);
140 stw_be_p(&bd->length, bd->length);
141 stl_be_p(&bd->bufptr, bd->bufptr);
144 RING_DEBUG("Write Buffer Descriptor @ 0x" TARGET_FMT_plx"\n", addr);
145 cpu_physical_memory_write(addr,
147 sizeof(eTSEC_rxtx_bd));
150 static void ievent_set(eTSEC *etsec,
151 uint32_t flags)
153 etsec->regs[IEVENT].value |= flags;
155 etsec_update_irq(etsec);
158 static void tx_padding_and_crc(eTSEC *etsec, uint32_t min_frame_len)
160 int add = min_frame_len - etsec->tx_buffer_len;
162 /* Padding */
163 if (add > 0) {
164 RING_DEBUG("pad:%u\n", add);
165 etsec->tx_buffer = g_realloc(etsec->tx_buffer,
166 etsec->tx_buffer_len + add);
168 memset(etsec->tx_buffer + etsec->tx_buffer_len, 0x0, add);
169 etsec->tx_buffer_len += add;
172 /* Never add CRC in QEMU */
175 static void process_tx_fcb(eTSEC *etsec)
177 uint8_t flags = (uint8_t)(*etsec->tx_buffer);
178 /* L3 header offset from start of frame */
179 uint8_t l3_header_offset = (uint8_t)*(etsec->tx_buffer + 3);
180 /* L4 header offset from start of L3 header */
181 uint8_t l4_header_offset = (uint8_t)*(etsec->tx_buffer + 2);
182 /* L3 header */
183 uint8_t *l3_header = etsec->tx_buffer + 8 + l3_header_offset;
184 /* L4 header */
185 uint8_t *l4_header = l3_header + l4_header_offset;
186 int csum = 0;
188 /* if packet is IP4 and IP checksum is requested */
189 if (flags & FCB_TX_IP && flags & FCB_TX_CIP) {
190 csum |= CSUM_IP;
192 /* TODO Check the correct usage of the PHCS field of the FCB in case the NPH
193 * flag is on */
195 /* if packet is IP4 and TCP or UDP */
196 if (flags & FCB_TX_IP && flags & FCB_TX_TUP) {
197 /* if UDP */
198 if (flags & FCB_TX_UDP) {
199 /* if checksum is requested */
200 if (flags & FCB_TX_CTU) {
201 /* do UDP checksum */
202 csum |= CSUM_UDP;
203 } else {
204 /* set checksum field to 0 */
205 l4_header[6] = 0;
206 l4_header[7] = 0;
208 } else if (flags & FCB_TX_CTU) { /* if TCP and checksum is requested */
209 /* do TCP checksum */
210 csum |= CSUM_TCP;
214 if (csum) {
215 net_checksum_calculate(etsec->tx_buffer + 8,
216 etsec->tx_buffer_len - 8, csum);
220 static void process_tx_bd(eTSEC *etsec,
221 eTSEC_rxtx_bd *bd)
223 uint8_t *tmp_buff = NULL;
224 hwaddr tbdbth = (hwaddr)(etsec->regs[TBDBPH].value & 0xF) << 32;
226 if (bd->length == 0) {
227 /* ERROR */
228 return;
231 if (etsec->tx_buffer_len == 0) {
232 /* It's the first BD */
233 etsec->first_bd = *bd;
236 /* TODO: if TxBD[TOE/UN] skip the Tx Frame Control Block*/
238 /* Load this Data Buffer */
239 etsec->tx_buffer = g_realloc(etsec->tx_buffer,
240 etsec->tx_buffer_len + bd->length);
241 tmp_buff = etsec->tx_buffer + etsec->tx_buffer_len;
242 cpu_physical_memory_read(bd->bufptr + tbdbth, tmp_buff, bd->length);
244 /* Update buffer length */
245 etsec->tx_buffer_len += bd->length;
248 if (etsec->tx_buffer_len != 0 && (bd->flags & BD_LAST)) {
249 if (etsec->regs[MACCFG1].value & MACCFG1_TX_EN) {
250 /* MAC Transmit enabled */
252 /* Process offload Tx FCB */
253 if (etsec->first_bd.flags & BD_TX_TOEUN) {
254 process_tx_fcb(etsec);
257 if (etsec->first_bd.flags & BD_TX_PADCRC
258 || etsec->regs[MACCFG2].value & MACCFG2_PADCRC) {
260 /* Padding and CRC (Padding implies CRC) */
261 tx_padding_and_crc(etsec, 64);
263 } else if (etsec->first_bd.flags & BD_TX_TC
264 || etsec->regs[MACCFG2].value & MACCFG2_CRC_EN) {
266 /* Only CRC */
267 /* Never add CRC in QEMU */
270 #if defined(HEX_DUMP)
271 qemu_log("eTSEC Send packet size:%d\n", etsec->tx_buffer_len);
272 qemu_hexdump(stderr, "", etsec->tx_buffer, etsec->tx_buffer_len);
273 #endif /* ETSEC_RING_DEBUG */
275 if (etsec->first_bd.flags & BD_TX_TOEUN) {
276 qemu_send_packet(qemu_get_queue(etsec->nic),
277 etsec->tx_buffer + 8,
278 etsec->tx_buffer_len - 8);
279 } else {
280 qemu_send_packet(qemu_get_queue(etsec->nic),
281 etsec->tx_buffer,
282 etsec->tx_buffer_len);
287 etsec->tx_buffer_len = 0;
289 if (bd->flags & BD_INTERRUPT) {
290 ievent_set(etsec, IEVENT_TXF);
292 } else {
293 if (bd->flags & BD_INTERRUPT) {
294 ievent_set(etsec, IEVENT_TXB);
298 /* Update DB flags */
300 /* Clear Ready */
301 bd->flags &= ~BD_TX_READY;
303 /* Clear Defer */
304 bd->flags &= ~BD_TX_PREDEF;
306 /* Clear Late Collision */
307 bd->flags &= ~BD_TX_HFELC;
309 /* Clear Retransmission Limit */
310 bd->flags &= ~BD_TX_CFRL;
312 /* Clear Retry Count */
313 bd->flags &= ~(BD_TX_RC_MASK << BD_TX_RC_OFFSET);
315 /* Clear Underrun */
316 bd->flags &= ~BD_TX_TOEUN;
318 /* Clear Truncation */
319 bd->flags &= ~BD_TX_TR;
322 void etsec_walk_tx_ring(eTSEC *etsec, int ring_nbr)
324 hwaddr ring_base = 0;
325 hwaddr bd_addr = 0;
326 eTSEC_rxtx_bd bd;
327 uint16_t bd_flags;
329 if (!(etsec->regs[MACCFG1].value & MACCFG1_TX_EN)) {
330 RING_DEBUG("%s: MAC Transmit not enabled\n", __func__);
331 return;
334 ring_base = (hwaddr)(etsec->regs[TBASEH].value & 0xF) << 32;
335 ring_base += etsec->regs[TBASE0 + ring_nbr].value & ~0x7;
336 bd_addr = etsec->regs[TBPTR0 + ring_nbr].value & ~0x7;
338 do {
339 read_buffer_descriptor(etsec, bd_addr, &bd);
341 #ifdef DEBUG_BD
342 print_bd(bd,
343 eTSEC_TRANSMIT,
344 (bd_addr - ring_base) / sizeof(eTSEC_rxtx_bd));
346 #endif /* DEBUG_BD */
348 /* Save flags before BD update */
349 bd_flags = bd.flags;
351 if (!(bd_flags & BD_TX_READY)) {
352 break;
355 process_tx_bd(etsec, &bd);
356 /* Write back BD after update */
357 write_buffer_descriptor(etsec, bd_addr, &bd);
359 /* Wrap or next BD */
360 if (bd_flags & BD_WRAP) {
361 bd_addr = ring_base;
362 } else {
363 bd_addr += sizeof(eTSEC_rxtx_bd);
365 } while (TRUE);
367 /* Save the Buffer Descriptor Pointers to last bd that was not
368 * succesfully closed */
369 etsec->regs[TBPTR0 + ring_nbr].value = bd_addr;
371 /* Set transmit halt THLTx */
372 etsec->regs[TSTAT].value |= 1 << (31 - ring_nbr);
375 static void fill_rx_bd(eTSEC *etsec,
376 eTSEC_rxtx_bd *bd,
377 const uint8_t **buf,
378 size_t *size)
380 uint16_t to_write;
381 hwaddr bufptr = bd->bufptr +
382 ((hwaddr)(etsec->regs[TBDBPH].value & 0xF) << 32);
383 uint8_t padd[etsec->rx_padding];
384 uint8_t rem;
386 RING_DEBUG("eTSEC fill Rx buffer @ 0x%016" HWADDR_PRIx
387 " size:%zu(padding + crc:%u) + fcb:%u\n",
388 bufptr, *size, etsec->rx_padding, etsec->rx_fcb_size);
390 bd->length = 0;
392 /* This operation will only write FCB */
393 if (etsec->rx_fcb_size != 0) {
395 cpu_physical_memory_write(bufptr, etsec->rx_fcb, etsec->rx_fcb_size);
397 bufptr += etsec->rx_fcb_size;
398 bd->length += etsec->rx_fcb_size;
399 etsec->rx_fcb_size = 0;
403 /* We remove padding from the computation of to_write because it is not
404 * allocated in the buffer.
406 to_write = MIN(*size - etsec->rx_padding,
407 etsec->regs[MRBLR].value - etsec->rx_fcb_size);
409 /* This operation can only write packet data and no padding */
410 if (to_write > 0) {
411 cpu_physical_memory_write(bufptr, *buf, to_write);
413 *buf += to_write;
414 bufptr += to_write;
415 *size -= to_write;
417 bd->flags &= ~BD_RX_EMPTY;
418 bd->length += to_write;
421 if (*size == etsec->rx_padding) {
422 /* The remaining bytes are only for padding which is not actually
423 * allocated in the data buffer.
426 rem = MIN(etsec->regs[MRBLR].value - bd->length, etsec->rx_padding);
428 if (rem > 0) {
429 memset(padd, 0x0, sizeof(padd));
430 etsec->rx_padding -= rem;
431 *size -= rem;
432 bd->length += rem;
433 cpu_physical_memory_write(bufptr, padd, rem);
438 static void rx_init_frame(eTSEC *etsec, const uint8_t *buf, size_t size)
440 uint32_t fcb_size = 0;
441 uint8_t prsdep = (etsec->regs[RCTRL].value >> RCTRL_PRSDEP_OFFSET)
442 & RCTRL_PRSDEP_MASK;
444 if (prsdep != 0) {
445 /* Prepend FCB (FCB size + RCTRL[PAL]) */
446 fcb_size = 8 + ((etsec->regs[RCTRL].value >> 16) & 0x1F);
448 etsec->rx_fcb_size = fcb_size;
450 /* TODO: fill_FCB(etsec); */
451 memset(etsec->rx_fcb, 0x0, sizeof(etsec->rx_fcb));
453 } else {
454 etsec->rx_fcb_size = 0;
457 g_free(etsec->rx_buffer);
459 /* Do not copy the frame for now */
460 etsec->rx_buffer = (uint8_t *)buf;
461 etsec->rx_buffer_len = size;
463 /* CRC padding (We don't have to compute the CRC) */
464 etsec->rx_padding = 4;
467 * Ensure that payload length + CRC length is at least 802.3
468 * minimum MTU size bytes long (64)
470 if (etsec->rx_buffer_len < 60) {
471 etsec->rx_padding += 60 - etsec->rx_buffer_len;
474 etsec->rx_first_in_frame = 1;
475 etsec->rx_remaining_data = etsec->rx_buffer_len;
476 RING_DEBUG("%s: rx_buffer_len:%u rx_padding+crc:%u\n", __func__,
477 etsec->rx_buffer_len, etsec->rx_padding);
480 ssize_t etsec_rx_ring_write(eTSEC *etsec, const uint8_t *buf, size_t size)
482 int ring_nbr = 0; /* Always use ring0 (no filer) */
484 if (etsec->rx_buffer_len != 0) {
485 RING_DEBUG("%s: We can't receive now,"
486 " a buffer is already in the pipe\n", __func__);
487 return 0;
490 if (etsec->regs[RSTAT].value & 1 << (23 - ring_nbr)) {
491 RING_DEBUG("%s: The ring is halted\n", __func__);
492 return -1;
495 if (etsec->regs[DMACTRL].value & DMACTRL_GRS) {
496 RING_DEBUG("%s: Graceful receive stop\n", __func__);
497 return -1;
500 if (!(etsec->regs[MACCFG1].value & MACCFG1_RX_EN)) {
501 RING_DEBUG("%s: MAC Receive not enabled\n", __func__);
502 return -1;
505 if ((etsec->regs[RCTRL].value & RCTRL_RSF) && (size < 60)) {
506 /* CRC is not in the packet yet, so short frame is below 60 bytes */
507 RING_DEBUG("%s: Drop short frame\n", __func__);
508 return -1;
511 rx_init_frame(etsec, buf, size);
513 etsec_walk_rx_ring(etsec, ring_nbr);
515 return size;
518 void etsec_walk_rx_ring(eTSEC *etsec, int ring_nbr)
520 hwaddr ring_base = 0;
521 hwaddr bd_addr = 0;
522 hwaddr start_bd_addr = 0;
523 eTSEC_rxtx_bd bd;
524 uint16_t bd_flags;
525 size_t remaining_data;
526 const uint8_t *buf;
527 uint8_t *tmp_buf;
528 size_t size;
530 if (etsec->rx_buffer_len == 0) {
531 /* No frame to send */
532 RING_DEBUG("No frame to send\n");
533 return;
536 remaining_data = etsec->rx_remaining_data + etsec->rx_padding;
537 buf = etsec->rx_buffer
538 + (etsec->rx_buffer_len - etsec->rx_remaining_data);
539 size = etsec->rx_buffer_len + etsec->rx_padding;
541 ring_base = (hwaddr)(etsec->regs[RBASEH].value & 0xF) << 32;
542 ring_base += etsec->regs[RBASE0 + ring_nbr].value & ~0x7;
543 start_bd_addr = bd_addr = etsec->regs[RBPTR0 + ring_nbr].value & ~0x7;
545 do {
546 read_buffer_descriptor(etsec, bd_addr, &bd);
548 #ifdef DEBUG_BD
549 print_bd(bd,
550 eTSEC_RECEIVE,
551 (bd_addr - ring_base) / sizeof(eTSEC_rxtx_bd));
553 #endif /* DEBUG_BD */
555 /* Save flags before BD update */
556 bd_flags = bd.flags;
558 if (bd_flags & BD_RX_EMPTY) {
559 fill_rx_bd(etsec, &bd, &buf, &remaining_data);
561 if (etsec->rx_first_in_frame) {
562 bd.flags |= BD_RX_FIRST;
563 etsec->rx_first_in_frame = 0;
564 etsec->rx_first_bd = bd;
567 /* Last in frame */
568 if (remaining_data == 0) {
570 /* Clear flags */
572 bd.flags &= ~0x7ff;
574 bd.flags |= BD_LAST;
576 /* NOTE: non-octet aligned frame is impossible in qemu */
578 if (size >= etsec->regs[MAXFRM].value) {
579 /* frame length violation */
580 qemu_log("%s frame length violation: size:%zu MAXFRM:%d\n",
581 __func__, size, etsec->regs[MAXFRM].value);
583 bd.flags |= BD_RX_LG;
586 if (size < 64) {
587 /* Short frame */
588 bd.flags |= BD_RX_SH;
591 /* TODO: Broadcast and Multicast */
593 if (bd.flags & BD_INTERRUPT) {
594 /* Set RXFx */
595 etsec->regs[RSTAT].value |= 1 << (7 - ring_nbr);
597 /* Set IEVENT */
598 ievent_set(etsec, IEVENT_RXF);
601 } else {
602 if (bd.flags & BD_INTERRUPT) {
603 /* Set IEVENT */
604 ievent_set(etsec, IEVENT_RXB);
608 /* Write back BD after update */
609 write_buffer_descriptor(etsec, bd_addr, &bd);
612 /* Wrap or next BD */
613 if (bd_flags & BD_WRAP) {
614 bd_addr = ring_base;
615 } else {
616 bd_addr += sizeof(eTSEC_rxtx_bd);
618 } while (remaining_data != 0
619 && (bd_flags & BD_RX_EMPTY)
620 && bd_addr != start_bd_addr);
622 /* Reset ring ptr */
623 etsec->regs[RBPTR0 + ring_nbr].value = bd_addr;
625 /* The frame is too large to fit in the Rx ring */
626 if (remaining_data > 0) {
628 /* Set RSTAT[QHLTx] */
629 etsec->regs[RSTAT].value |= 1 << (23 - ring_nbr);
631 /* Save remaining data to send the end of the frame when the ring will
632 * be restarted
634 etsec->rx_remaining_data = remaining_data;
636 /* Copy the frame */
637 tmp_buf = g_malloc(size);
638 memcpy(tmp_buf, etsec->rx_buffer, size);
639 etsec->rx_buffer = tmp_buf;
641 RING_DEBUG("no empty RxBD available any more\n");
642 } else {
643 etsec->rx_buffer_len = 0;
644 etsec->rx_buffer = NULL;
645 if (etsec->need_flush) {
646 qemu_flush_queued_packets(qemu_get_queue(etsec->nic));
650 RING_DEBUG("eTSEC End of ring_write: remaining_data:%zu\n", remaining_data);