1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25#include <zlib.h>
26
27#include "sysbus.h"
28#include "net/net.h"
29#include "net/checksum.h"
30
31#ifdef CADENCE_GEM_ERR_DEBUG
32#define DB_PRINT(...) do { \
33 fprintf(stderr, ": %s: ", __func__); \
34 fprintf(stderr, ## __VA_ARGS__); \
35 } while (0);
36#else
37 #define DB_PRINT(...)
38#endif
39
40#define GEM_NWCTRL (0x00000000/4)
41#define GEM_NWCFG (0x00000004/4)
42#define GEM_NWSTATUS (0x00000008/4)
43#define GEM_USERIO (0x0000000C/4)
44#define GEM_DMACFG (0x00000010/4)
45#define GEM_TXSTATUS (0x00000014/4)
46#define GEM_RXQBASE (0x00000018/4)
47#define GEM_TXQBASE (0x0000001C/4)
48#define GEM_RXSTATUS (0x00000020/4)
49#define GEM_ISR (0x00000024/4)
50#define GEM_IER (0x00000028/4)
51#define GEM_IDR (0x0000002C/4)
52#define GEM_IMR (0x00000030/4)
53#define GEM_PHYMNTNC (0x00000034/4)
54#define GEM_RXPAUSE (0x00000038/4)
55#define GEM_TXPAUSE (0x0000003C/4)
56#define GEM_TXPARTIALSF (0x00000040/4)
57#define GEM_RXPARTIALSF (0x00000044/4)
58#define GEM_HASHLO (0x00000080/4)
59#define GEM_HASHHI (0x00000084/4)
60#define GEM_SPADDR1LO (0x00000088/4)
61#define GEM_SPADDR1HI (0x0000008C/4)
62#define GEM_SPADDR2LO (0x00000090/4)
63#define GEM_SPADDR2HI (0x00000094/4)
64#define GEM_SPADDR3LO (0x00000098/4)
65#define GEM_SPADDR3HI (0x0000009C/4)
66#define GEM_SPADDR4LO (0x000000A0/4)
67#define GEM_SPADDR4HI (0x000000A4/4)
68#define GEM_TIDMATCH1 (0x000000A8/4)
69#define GEM_TIDMATCH2 (0x000000AC/4)
70#define GEM_TIDMATCH3 (0x000000B0/4)
71#define GEM_TIDMATCH4 (0x000000B4/4)
72#define GEM_WOLAN (0x000000B8/4)
73#define GEM_IPGSTRETCH (0x000000BC/4)
74#define GEM_SVLAN (0x000000C0/4)
75#define GEM_MODID (0x000000FC/4)
76#define GEM_OCTTXLO (0x00000100/4)
77#define GEM_OCTTXHI (0x00000104/4)
78#define GEM_TXCNT (0x00000108/4)
79#define GEM_TXBCNT (0x0000010C/4)
80#define GEM_TXMCNT (0x00000110/4)
81#define GEM_TXPAUSECNT (0x00000114/4)
82#define GEM_TX64CNT (0x00000118/4)
83#define GEM_TX65CNT (0x0000011C/4)
84#define GEM_TX128CNT (0x00000120/4)
85#define GEM_TX256CNT (0x00000124/4)
86#define GEM_TX512CNT (0x00000128/4)
87#define GEM_TX1024CNT (0x0000012C/4)
88#define GEM_TX1519CNT (0x00000130/4)
89#define GEM_TXURUNCNT (0x00000134/4)
90#define GEM_SINGLECOLLCNT (0x00000138/4)
91#define GEM_MULTCOLLCNT (0x0000013C/4)
92#define GEM_EXCESSCOLLCNT (0x00000140/4)
93#define GEM_LATECOLLCNT (0x00000144/4)
94#define GEM_DEFERTXCNT (0x00000148/4)
95#define GEM_CSENSECNT (0x0000014C/4)
96#define GEM_OCTRXLO (0x00000150/4)
97#define GEM_OCTRXHI (0x00000154/4)
98#define GEM_RXCNT (0x00000158/4)
99#define GEM_RXBROADCNT (0x0000015C/4)
100#define GEM_RXMULTICNT (0x00000160/4)
101#define GEM_RXPAUSECNT (0x00000164/4)
102#define GEM_RX64CNT (0x00000168/4)
103#define GEM_RX65CNT (0x0000016C/4)
104#define GEM_RX128CNT (0x00000170/4)
105#define GEM_RX256CNT (0x00000174/4)
106#define GEM_RX512CNT (0x00000178/4)
107#define GEM_RX1024CNT (0x0000017C/4)
108#define GEM_RX1519CNT (0x00000180/4)
109#define GEM_RXUNDERCNT (0x00000184/4)
110#define GEM_RXOVERCNT (0x00000188/4)
111#define GEM_RXJABCNT (0x0000018C/4)
112#define GEM_RXFCSCNT (0x00000190/4)
113#define GEM_RXLENERRCNT (0x00000194/4)
114#define GEM_RXSYMERRCNT (0x00000198/4)
115#define GEM_RXALIGNERRCNT (0x0000019C/4)
116#define GEM_RXRSCERRCNT (0x000001A0/4)
117#define GEM_RXORUNCNT (0x000001A4/4)
118#define GEM_RXIPCSERRCNT (0x000001A8/4)
119#define GEM_RXTCPCCNT (0x000001AC/4)
120#define GEM_RXUDPCCNT (0x000001B0/4)
121
122#define GEM_1588S (0x000001D0/4)
123#define GEM_1588NS (0x000001D4/4)
124#define GEM_1588ADJ (0x000001D8/4)
125#define GEM_1588INC (0x000001DC/4)
126#define GEM_PTPETXS (0x000001E0/4)
127#define GEM_PTPETXNS (0x000001E4/4)
128#define GEM_PTPERXS (0x000001E8/4)
129#define GEM_PTPERXNS (0x000001EC/4)
130#define GEM_PTPPTXS (0x000001E0/4)
131#define GEM_PTPPTXNS (0x000001E4/4)
132#define GEM_PTPPRXS (0x000001E8/4)
133#define GEM_PTPPRXNS (0x000001EC/4)
134
135
136#define GEM_DESCONF (0x00000280/4)
137#define GEM_DESCONF2 (0x00000284/4)
138#define GEM_DESCONF3 (0x00000288/4)
139#define GEM_DESCONF4 (0x0000028C/4)
140#define GEM_DESCONF5 (0x00000290/4)
141#define GEM_DESCONF6 (0x00000294/4)
142#define GEM_DESCONF7 (0x00000298/4)
143
144#define GEM_MAXREG (0x00000640/4)
145
146
147#define GEM_NWCTRL_TXSTART 0x00000200
148#define GEM_NWCTRL_TXENA 0x00000008
149#define GEM_NWCTRL_RXENA 0x00000004
150#define GEM_NWCTRL_LOCALLOOP 0x00000002
151
152#define GEM_NWCFG_STRIP_FCS 0x00020000
153#define GEM_NWCFG_LERR_DISC 0x00010000
154#define GEM_NWCFG_BUFF_OFST_M 0x0000C000
155#define GEM_NWCFG_BUFF_OFST_S 14
156#define GEM_NWCFG_UCAST_HASH 0x00000080
157#define GEM_NWCFG_MCAST_HASH 0x00000040
158#define GEM_NWCFG_BCAST_REJ 0x00000020
159#define GEM_NWCFG_PROMISC 0x00000010
160
161#define GEM_DMACFG_RBUFSZ_M 0x007F0000
162#define GEM_DMACFG_RBUFSZ_S 16
163#define GEM_DMACFG_RBUFSZ_MUL 64
164#define GEM_DMACFG_TXCSUM_OFFL 0x00000800
165
166#define GEM_TXSTATUS_TXCMPL 0x00000020
167#define GEM_TXSTATUS_USED 0x00000001
168
169#define GEM_RXSTATUS_FRMRCVD 0x00000002
170#define GEM_RXSTATUS_NOBUF 0x00000001
171
172
173#define GEM_INT_TXCMPL 0x00000080
174#define GEM_INT_TXUSED 0x00000008
175#define GEM_INT_RXUSED 0x00000004
176#define GEM_INT_RXCMPL 0x00000002
177
178#define GEM_PHYMNTNC_OP_R 0x20000000
179#define GEM_PHYMNTNC_OP_W 0x10000000
180#define GEM_PHYMNTNC_ADDR 0x0F800000
181#define GEM_PHYMNTNC_ADDR_SHFT 23
182#define GEM_PHYMNTNC_REG 0x007C0000
183#define GEM_PHYMNTNC_REG_SHIFT 18
184
185
186#define BOARD_PHY_ADDRESS 23
187
188#define PHY_REG_CONTROL 0
189#define PHY_REG_STATUS 1
190#define PHY_REG_PHYID1 2
191#define PHY_REG_PHYID2 3
192#define PHY_REG_ANEGADV 4
193#define PHY_REG_LINKPABIL 5
194#define PHY_REG_ANEGEXP 6
195#define PHY_REG_NEXTP 7
196#define PHY_REG_LINKPNEXTP 8
197#define PHY_REG_100BTCTRL 9
198#define PHY_REG_1000BTSTAT 10
199#define PHY_REG_EXTSTAT 15
200#define PHY_REG_PHYSPCFC_CTL 16
201#define PHY_REG_PHYSPCFC_ST 17
202#define PHY_REG_INT_EN 18
203#define PHY_REG_INT_ST 19
204#define PHY_REG_EXT_PHYSPCFC_CTL 20
205#define PHY_REG_RXERR 21
206#define PHY_REG_EACD 22
207#define PHY_REG_LED 24
208#define PHY_REG_LED_OVRD 25
209#define PHY_REG_EXT_PHYSPCFC_CTL2 26
210#define PHY_REG_EXT_PHYSPCFC_ST 27
211#define PHY_REG_CABLE_DIAG 28
212
213#define PHY_REG_CONTROL_RST 0x8000
214#define PHY_REG_CONTROL_LOOP 0x4000
215#define PHY_REG_CONTROL_ANEG 0x1000
216
217#define PHY_REG_STATUS_LINK 0x0004
218#define PHY_REG_STATUS_ANEGCMPL 0x0020
219
220#define PHY_REG_INT_ST_ANEGCMPL 0x0800
221#define PHY_REG_INT_ST_LINKC 0x0400
222#define PHY_REG_INT_ST_ENERGY 0x0010
223
224
225#define GEM_RX_REJECT 1
226#define GEM_RX_ACCEPT 0
227
228
229
230#define DESC_1_USED 0x80000000
231#define DESC_1_LENGTH 0x00001FFF
232
233#define DESC_1_TX_WRAP 0x40000000
234#define DESC_1_TX_LAST 0x00008000
235
236#define DESC_0_RX_WRAP 0x00000002
237#define DESC_0_RX_OWNERSHIP 0x00000001
238
239#define DESC_1_RX_SOF 0x00004000
240#define DESC_1_RX_EOF 0x00008000
241
242static inline unsigned tx_desc_get_buffer(unsigned *desc)
243{
244 return desc[0];
245}
246
247static inline unsigned tx_desc_get_used(unsigned *desc)
248{
249 return (desc[1] & DESC_1_USED) ? 1 : 0;
250}
251
252static inline void tx_desc_set_used(unsigned *desc)
253{
254 desc[1] |= DESC_1_USED;
255}
256
257static inline unsigned tx_desc_get_wrap(unsigned *desc)
258{
259 return (desc[1] & DESC_1_TX_WRAP) ? 1 : 0;
260}
261
262static inline unsigned tx_desc_get_last(unsigned *desc)
263{
264 return (desc[1] & DESC_1_TX_LAST) ? 1 : 0;
265}
266
267static inline unsigned tx_desc_get_length(unsigned *desc)
268{
269 return desc[1] & DESC_1_LENGTH;
270}
271
272static inline void print_gem_tx_desc(unsigned *desc)
273{
274 DB_PRINT("TXDESC:\n");
275 DB_PRINT("bufaddr: 0x%08x\n", *desc);
276 DB_PRINT("used_hw: %d\n", tx_desc_get_used(desc));
277 DB_PRINT("wrap: %d\n", tx_desc_get_wrap(desc));
278 DB_PRINT("last: %d\n", tx_desc_get_last(desc));
279 DB_PRINT("length: %d\n", tx_desc_get_length(desc));
280}
281
282static inline unsigned rx_desc_get_buffer(unsigned *desc)
283{
284 return desc[0] & ~0x3UL;
285}
286
287static inline unsigned rx_desc_get_wrap(unsigned *desc)
288{
289 return desc[0] & DESC_0_RX_WRAP ? 1 : 0;
290}
291
292static inline unsigned rx_desc_get_ownership(unsigned *desc)
293{
294 return desc[0] & DESC_0_RX_OWNERSHIP ? 1 : 0;
295}
296
297static inline void rx_desc_set_ownership(unsigned *desc)
298{
299 desc[0] |= DESC_0_RX_OWNERSHIP;
300}
301
302static inline void rx_desc_set_sof(unsigned *desc)
303{
304 desc[1] |= DESC_1_RX_SOF;
305}
306
307static inline void rx_desc_set_eof(unsigned *desc)
308{
309 desc[1] |= DESC_1_RX_EOF;
310}
311
312static inline void rx_desc_set_length(unsigned *desc, unsigned len)
313{
314 desc[1] &= ~DESC_1_LENGTH;
315 desc[1] |= len;
316}
317
318typedef struct {
319 SysBusDevice busdev;
320 MemoryRegion iomem;
321 NICState *nic;
322 NICConf conf;
323 qemu_irq irq;
324
325
326 uint32_t regs[GEM_MAXREG];
327
328 uint32_t regs_wo[GEM_MAXREG];
329
330 uint32_t regs_ro[GEM_MAXREG];
331
332 uint32_t regs_rtc[GEM_MAXREG];
333
334 uint32_t regs_w1c[GEM_MAXREG];
335
336
337 uint16_t phy_regs[32];
338
339 uint8_t phy_loop;
340
341
342 uint32_t rx_desc_addr;
343 uint32_t tx_desc_addr;
344
345} GemState;
346
347
348const uint8_t broadcast_addr[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
349
350
351
352
353
354
355static void gem_init_register_masks(GemState *s)
356{
357
358 memset(&s->regs_ro[0], 0, sizeof(s->regs_ro));
359 s->regs_ro[GEM_NWCTRL] = 0xFFF80000;
360 s->regs_ro[GEM_NWSTATUS] = 0xFFFFFFFF;
361 s->regs_ro[GEM_DMACFG] = 0xFE00F000;
362 s->regs_ro[GEM_TXSTATUS] = 0xFFFFFE08;
363 s->regs_ro[GEM_RXQBASE] = 0x00000003;
364 s->regs_ro[GEM_TXQBASE] = 0x00000003;
365 s->regs_ro[GEM_RXSTATUS] = 0xFFFFFFF0;
366 s->regs_ro[GEM_ISR] = 0xFFFFFFFF;
367 s->regs_ro[GEM_IMR] = 0xFFFFFFFF;
368 s->regs_ro[GEM_MODID] = 0xFFFFFFFF;
369
370
371 memset(&s->regs_rtc[0], 0, sizeof(s->regs_rtc));
372 s->regs_rtc[GEM_ISR] = 0xFFFFFFFF;
373
374
375 memset(&s->regs_w1c[0], 0, sizeof(s->regs_w1c));
376 s->regs_w1c[GEM_TXSTATUS] = 0x000001F7;
377 s->regs_w1c[GEM_RXSTATUS] = 0x0000000F;
378
379
380 memset(&s->regs_wo[0], 0, sizeof(s->regs_wo));
381 s->regs_wo[GEM_NWCTRL] = 0x00073E60;
382 s->regs_wo[GEM_IER] = 0x07FFFFFF;
383 s->regs_wo[GEM_IDR] = 0x07FFFFFF;
384}
385
386
387
388
389
390static void phy_update_link(GemState *s)
391{
392 DB_PRINT("down %d\n", qemu_get_queue(s->nic)->link_down);
393
394
395 if (qemu_get_queue(s->nic)->link_down) {
396 s->phy_regs[PHY_REG_STATUS] &= ~(PHY_REG_STATUS_ANEGCMPL |
397 PHY_REG_STATUS_LINK);
398 s->phy_regs[PHY_REG_INT_ST] |= PHY_REG_INT_ST_LINKC;
399 } else {
400 s->phy_regs[PHY_REG_STATUS] |= (PHY_REG_STATUS_ANEGCMPL |
401 PHY_REG_STATUS_LINK);
402 s->phy_regs[PHY_REG_INT_ST] |= (PHY_REG_INT_ST_LINKC |
403 PHY_REG_INT_ST_ANEGCMPL |
404 PHY_REG_INT_ST_ENERGY);
405 }
406}
407
408static int gem_can_receive(NetClientState *nc)
409{
410 GemState *s;
411
412 s = qemu_get_nic_opaque(nc);
413
414 DB_PRINT("\n");
415
416
417 if (!(s->regs[GEM_NWCTRL] & GEM_NWCTRL_RXENA)) {
418 return 0;
419 }
420
421 return 1;
422}
423
424
425
426
427
428static void gem_update_int_status(GemState *s)
429{
430 uint32_t new_interrupts = 0;
431
432 if (s->regs[GEM_TXSTATUS] & GEM_TXSTATUS_TXCMPL) {
433 new_interrupts |= GEM_INT_TXCMPL;
434 }
435
436 if (s->regs[GEM_TXSTATUS] & GEM_TXSTATUS_USED) {
437 new_interrupts |= GEM_INT_TXUSED;
438 }
439
440
441 if (s->regs[GEM_RXSTATUS] & GEM_RXSTATUS_FRMRCVD) {
442 new_interrupts |= GEM_INT_RXCMPL;
443 }
444
445 if (s->regs[GEM_RXSTATUS] & GEM_RXSTATUS_NOBUF) {
446 new_interrupts |= GEM_INT_RXUSED;
447 }
448
449 s->regs[GEM_ISR] |= new_interrupts & ~(s->regs[GEM_IMR]);
450
451 if (s->regs[GEM_ISR]) {
452 DB_PRINT("asserting int. (0x%08x)\n", s->regs[GEM_ISR]);
453 qemu_set_irq(s->irq, 1);
454 } else {
455 qemu_set_irq(s->irq, 0);
456 }
457}
458
459
460
461
462
463static void gem_receive_updatestats(GemState *s, const uint8_t *packet,
464 unsigned bytes)
465{
466 uint64_t octets;
467
468
469 octets = ((uint64_t)(s->regs[GEM_OCTRXLO]) << 32) |
470 s->regs[GEM_OCTRXHI];
471 octets += bytes;
472 s->regs[GEM_OCTRXLO] = octets >> 32;
473 s->regs[GEM_OCTRXHI] = octets;
474
475
476 s->regs[GEM_RXCNT]++;
477
478
479 if (!memcmp(packet, broadcast_addr, 6)) {
480 s->regs[GEM_RXBROADCNT]++;
481 }
482
483
484 if (packet[0] == 0x01) {
485 s->regs[GEM_RXMULTICNT]++;
486 }
487
488 if (bytes <= 64) {
489 s->regs[GEM_RX64CNT]++;
490 } else if (bytes <= 127) {
491 s->regs[GEM_RX65CNT]++;
492 } else if (bytes <= 255) {
493 s->regs[GEM_RX128CNT]++;
494 } else if (bytes <= 511) {
495 s->regs[GEM_RX256CNT]++;
496 } else if (bytes <= 1023) {
497 s->regs[GEM_RX512CNT]++;
498 } else if (bytes <= 1518) {
499 s->regs[GEM_RX1024CNT]++;
500 } else {
501 s->regs[GEM_RX1519CNT]++;
502 }
503}
504
505
506
507
508static unsigned get_bit(const uint8_t *mac, unsigned bit)
509{
510 unsigned byte;
511
512 byte = mac[bit / 8];
513 byte >>= (bit & 0x7);
514 byte &= 1;
515
516 return byte;
517}
518
519
520
521
522static unsigned calc_mac_hash(const uint8_t *mac)
523{
524 int index_bit, mac_bit;
525 unsigned hash_index;
526
527 hash_index = 0;
528 mac_bit = 5;
529 for (index_bit = 5; index_bit >= 0; index_bit--) {
530 hash_index |= (get_bit(mac, mac_bit) ^
531 get_bit(mac, mac_bit + 6) ^
532 get_bit(mac, mac_bit + 12) ^
533 get_bit(mac, mac_bit + 18) ^
534 get_bit(mac, mac_bit + 24) ^
535 get_bit(mac, mac_bit + 30) ^
536 get_bit(mac, mac_bit + 36) ^
537 get_bit(mac, mac_bit + 42)) << index_bit;
538 mac_bit--;
539 }
540
541 return hash_index;
542}
543
544
545
546
547
548
549
550
551static int gem_mac_address_filter(GemState *s, const uint8_t *packet)
552{
553 uint8_t *gem_spaddr;
554 int i;
555
556
557 if (s->regs[GEM_NWCFG] & GEM_NWCFG_PROMISC) {
558 return GEM_RX_ACCEPT;
559 }
560
561 if (!memcmp(packet, broadcast_addr, 6)) {
562
563 if (s->regs[GEM_NWCFG] & GEM_NWCFG_BCAST_REJ) {
564 return GEM_RX_REJECT;
565 }
566 return GEM_RX_ACCEPT;
567 }
568
569
570 if ((packet[0] == 0x01 && (s->regs[GEM_NWCFG] & GEM_NWCFG_MCAST_HASH)) ||
571 (packet[0] != 0x01 && (s->regs[GEM_NWCFG] & GEM_NWCFG_UCAST_HASH))) {
572 unsigned hash_index;
573
574 hash_index = calc_mac_hash(packet);
575 if (hash_index < 32) {
576 if (s->regs[GEM_HASHLO] & (1<<hash_index)) {
577 return GEM_RX_ACCEPT;
578 }
579 } else {
580 hash_index -= 32;
581 if (s->regs[GEM_HASHHI] & (1<<hash_index)) {
582 return GEM_RX_ACCEPT;
583 }
584 }
585 }
586
587
588 gem_spaddr = (uint8_t *)&(s->regs[GEM_SPADDR1LO]);
589 for (i = 0; i < 4; i++) {
590 if (!memcmp(packet, gem_spaddr, 6)) {
591 return GEM_RX_ACCEPT;
592 }
593
594 gem_spaddr += 8;
595 }
596
597
598 return GEM_RX_REJECT;
599}
600
601
602
603
604
605static ssize_t gem_receive(NetClientState *nc, const uint8_t *buf, size_t size)
606{
607 unsigned desc[2];
608 hwaddr packet_desc_addr, last_desc_addr;
609 GemState *s;
610 unsigned rxbufsize, bytes_to_copy;
611 unsigned rxbuf_offset;
612 uint8_t rxbuf[2048];
613 uint8_t *rxbuf_ptr;
614
615 s = qemu_get_nic_opaque(nc);
616
617
618 if (!(s->regs[GEM_NWCTRL] & GEM_NWCTRL_RXENA)) {
619 return -1;
620 }
621
622
623 if (gem_mac_address_filter(s, buf) == GEM_RX_REJECT) {
624 return -1;
625 }
626
627
628 if (s->regs[GEM_NWCFG] & GEM_NWCFG_LERR_DISC) {
629 unsigned type_len;
630
631
632 type_len = buf[12] << 8 | buf[13];
633
634 if (type_len < 0x600) {
635 if (size < type_len) {
636
637 return -1;
638 }
639 }
640 }
641
642
643
644
645 rxbuf_offset = (s->regs[GEM_NWCFG] & GEM_NWCFG_BUFF_OFST_M) >>
646 GEM_NWCFG_BUFF_OFST_S;
647
648
649
650
651 rxbufsize = ((s->regs[GEM_DMACFG] & GEM_DMACFG_RBUFSZ_M) >>
652 GEM_DMACFG_RBUFSZ_S) * GEM_DMACFG_RBUFSZ_MUL;
653 bytes_to_copy = size;
654
655
656 if (s->regs[GEM_NWCFG] & GEM_NWCFG_STRIP_FCS) {
657 rxbuf_ptr = (void *)buf;
658 } else {
659 unsigned crc_val;
660 int crc_offset;
661
662
663
664
665
666 memcpy(rxbuf, buf, size);
667 memset(rxbuf + size, 0, sizeof(rxbuf) - size);
668 rxbuf_ptr = rxbuf;
669 crc_val = cpu_to_le32(crc32(0, rxbuf, MAX(size, 60)));
670 if (size < 60) {
671 crc_offset = 60;
672 } else {
673 crc_offset = size;
674 }
675 memcpy(rxbuf + crc_offset, &crc_val, sizeof(crc_val));
676
677 bytes_to_copy += 4;
678 size += 4;
679 }
680
681
682 if (size < 64) {
683 size = 64;
684 }
685
686 DB_PRINT("config bufsize: %d packet size: %ld\n", rxbufsize, size);
687
688 packet_desc_addr = s->rx_desc_addr;
689 while (1) {
690 DB_PRINT("read descriptor 0x%x\n", (unsigned)packet_desc_addr);
691
692 cpu_physical_memory_read(packet_desc_addr,
693 (uint8_t *)&desc[0], sizeof(desc));
694
695
696 if (rx_desc_get_ownership(desc) == 1) {
697 DB_PRINT("descriptor 0x%x owned by sw.\n",
698 (unsigned)packet_desc_addr);
699 s->regs[GEM_RXSTATUS] |= GEM_RXSTATUS_NOBUF;
700
701 gem_update_int_status(s);
702 return -1;
703 }
704
705 DB_PRINT("copy %d bytes to 0x%x\n", MIN(bytes_to_copy, rxbufsize),
706 rx_desc_get_buffer(desc));
707
708
709
710
711 if (rx_desc_get_buffer(desc) == 0) {
712 DB_PRINT("Invalid RX buffer (NULL) for descriptor 0x%x\n",
713 (unsigned)packet_desc_addr);
714 break;
715 }
716
717
718 cpu_physical_memory_write(rx_desc_get_buffer(desc) + rxbuf_offset,
719 rxbuf_ptr, MIN(bytes_to_copy, rxbufsize));
720 bytes_to_copy -= MIN(bytes_to_copy, rxbufsize);
721 rxbuf_ptr += MIN(bytes_to_copy, rxbufsize);
722 if (bytes_to_copy == 0) {
723 break;
724 }
725
726
727 if (rx_desc_get_wrap(desc)) {
728 packet_desc_addr = s->regs[GEM_RXQBASE];
729 } else {
730 packet_desc_addr += 8;
731 }
732 }
733
734 DB_PRINT("set length: %ld, EOF on descriptor 0x%x\n", size,
735 (unsigned)packet_desc_addr);
736
737
738 rx_desc_set_eof(desc);
739 rx_desc_set_length(desc, size);
740 cpu_physical_memory_write(packet_desc_addr,
741 (uint8_t *)&desc[0], sizeof(desc));
742
743
744 last_desc_addr = packet_desc_addr;
745 packet_desc_addr = s->rx_desc_addr;
746 s->rx_desc_addr = last_desc_addr;
747 if (rx_desc_get_wrap(desc)) {
748 s->rx_desc_addr = s->regs[GEM_RXQBASE];
749 } else {
750 s->rx_desc_addr += 8;
751 }
752
753 DB_PRINT("set SOF, OWN on descriptor 0x%08x\n", (unsigned)packet_desc_addr);
754
755
756 gem_receive_updatestats(s, buf, size);
757
758
759
760 cpu_physical_memory_read(packet_desc_addr,
761 (uint8_t *)&desc[0], sizeof(desc));
762 rx_desc_set_sof(desc);
763 rx_desc_set_ownership(desc);
764 cpu_physical_memory_write(packet_desc_addr,
765 (uint8_t *)&desc[0], sizeof(desc));
766
767 s->regs[GEM_RXSTATUS] |= GEM_RXSTATUS_FRMRCVD;
768
769
770 gem_update_int_status(s);
771
772 return size;
773}
774
775
776
777
778
779static void gem_transmit_updatestats(GemState *s, const uint8_t *packet,
780 unsigned bytes)
781{
782 uint64_t octets;
783
784
785 octets = ((uint64_t)(s->regs[GEM_OCTTXLO]) << 32) |
786 s->regs[GEM_OCTTXHI];
787 octets += bytes;
788 s->regs[GEM_OCTTXLO] = octets >> 32;
789 s->regs[GEM_OCTTXHI] = octets;
790
791
792 s->regs[GEM_TXCNT]++;
793
794
795 if (!memcmp(packet, broadcast_addr, 6)) {
796 s->regs[GEM_TXBCNT]++;
797 }
798
799
800 if (packet[0] == 0x01) {
801 s->regs[GEM_TXMCNT]++;
802 }
803
804 if (bytes <= 64) {
805 s->regs[GEM_TX64CNT]++;
806 } else if (bytes <= 127) {
807 s->regs[GEM_TX65CNT]++;
808 } else if (bytes <= 255) {
809 s->regs[GEM_TX128CNT]++;
810 } else if (bytes <= 511) {
811 s->regs[GEM_TX256CNT]++;
812 } else if (bytes <= 1023) {
813 s->regs[GEM_TX512CNT]++;
814 } else if (bytes <= 1518) {
815 s->regs[GEM_TX1024CNT]++;
816 } else {
817 s->regs[GEM_TX1519CNT]++;
818 }
819}
820
821
822
823
824
825static void gem_transmit(GemState *s)
826{
827 unsigned desc[2];
828 hwaddr packet_desc_addr;
829 uint8_t tx_packet[2048];
830 uint8_t *p;
831 unsigned total_bytes;
832
833
834 if (!(s->regs[GEM_NWCTRL] & GEM_NWCTRL_TXENA)) {
835 return;
836 }
837
838 DB_PRINT("\n");
839
840
841
842
843
844 p = tx_packet;
845 total_bytes = 0;
846
847
848 packet_desc_addr = s->tx_desc_addr;
849 cpu_physical_memory_read(packet_desc_addr,
850 (uint8_t *)&desc[0], sizeof(desc));
851
852 while (tx_desc_get_used(desc) == 0) {
853
854
855 if (!(s->regs[GEM_NWCTRL] & GEM_NWCTRL_TXENA)) {
856 return;
857 }
858 print_gem_tx_desc(desc);
859
860
861
862
863 if ((tx_desc_get_buffer(desc) == 0) ||
864 (tx_desc_get_length(desc) == 0)) {
865 DB_PRINT("Invalid TX descriptor @ 0x%x\n",
866 (unsigned)packet_desc_addr);
867 break;
868 }
869
870
871
872
873 cpu_physical_memory_read(tx_desc_get_buffer(desc), p,
874 tx_desc_get_length(desc));
875 p += tx_desc_get_length(desc);
876 total_bytes += tx_desc_get_length(desc);
877
878
879 if (tx_desc_get_last(desc)) {
880
881
882
883 cpu_physical_memory_read(s->tx_desc_addr,
884 (uint8_t *)&desc[0], sizeof(desc));
885 tx_desc_set_used(desc);
886 cpu_physical_memory_write(s->tx_desc_addr,
887 (uint8_t *)&desc[0], sizeof(desc));
888
889 if (tx_desc_get_wrap(desc)) {
890 s->tx_desc_addr = s->regs[GEM_TXQBASE];
891 } else {
892 s->tx_desc_addr = packet_desc_addr + 8;
893 }
894 DB_PRINT("TX descriptor next: 0x%08x\n", s->tx_desc_addr);
895
896 s->regs[GEM_TXSTATUS] |= GEM_TXSTATUS_TXCMPL;
897
898
899 gem_update_int_status(s);
900
901
902 if (s->regs[GEM_DMACFG] & GEM_DMACFG_TXCSUM_OFFL) {
903 net_checksum_calculate(tx_packet, total_bytes);
904 }
905
906
907 gem_transmit_updatestats(s, tx_packet, total_bytes);
908
909
910 if (s->phy_loop) {
911 gem_receive(qemu_get_queue(s->nic), tx_packet, total_bytes);
912 } else {
913 qemu_send_packet(qemu_get_queue(s->nic), tx_packet,
914 total_bytes);
915 }
916
917
918 p = tx_packet;
919 total_bytes = 0;
920 }
921
922
923 if (tx_desc_get_wrap(desc)) {
924 packet_desc_addr = s->regs[GEM_TXQBASE];
925 } else {
926 packet_desc_addr += 8;
927 }
928 cpu_physical_memory_read(packet_desc_addr,
929 (uint8_t *)&desc[0], sizeof(desc));
930 }
931
932 if (tx_desc_get_used(desc)) {
933 s->regs[GEM_TXSTATUS] |= GEM_TXSTATUS_USED;
934 gem_update_int_status(s);
935 }
936}
937
938static void gem_phy_reset(GemState *s)
939{
940 memset(&s->phy_regs[0], 0, sizeof(s->phy_regs));
941 s->phy_regs[PHY_REG_CONTROL] = 0x1140;
942 s->phy_regs[PHY_REG_STATUS] = 0x7969;
943 s->phy_regs[PHY_REG_PHYID1] = 0x0141;
944 s->phy_regs[PHY_REG_PHYID2] = 0x0CC2;
945 s->phy_regs[PHY_REG_ANEGADV] = 0x01E1;
946 s->phy_regs[PHY_REG_LINKPABIL] = 0xCDE1;
947 s->phy_regs[PHY_REG_ANEGEXP] = 0x000F;
948 s->phy_regs[PHY_REG_NEXTP] = 0x2001;
949 s->phy_regs[PHY_REG_LINKPNEXTP] = 0x40E6;
950 s->phy_regs[PHY_REG_100BTCTRL] = 0x0300;
951 s->phy_regs[PHY_REG_1000BTSTAT] = 0x7C00;
952 s->phy_regs[PHY_REG_EXTSTAT] = 0x3000;
953 s->phy_regs[PHY_REG_PHYSPCFC_CTL] = 0x0078;
954 s->phy_regs[PHY_REG_PHYSPCFC_ST] = 0xBC00;
955 s->phy_regs[PHY_REG_EXT_PHYSPCFC_CTL] = 0x0C60;
956 s->phy_regs[PHY_REG_LED] = 0x4100;
957 s->phy_regs[PHY_REG_EXT_PHYSPCFC_CTL2] = 0x000A;
958 s->phy_regs[PHY_REG_EXT_PHYSPCFC_ST] = 0x848B;
959
960 phy_update_link(s);
961}
962
963static void gem_reset(DeviceState *d)
964{
965 GemState *s = FROM_SYSBUS(GemState, SYS_BUS_DEVICE(d));
966
967 DB_PRINT("\n");
968
969
970 memset(&s->regs[0], 0, sizeof(s->regs));
971 s->regs[GEM_NWCFG] = 0x00080000;
972 s->regs[GEM_NWSTATUS] = 0x00000006;
973 s->regs[GEM_DMACFG] = 0x00020784;
974 s->regs[GEM_IMR] = 0x07ffffff;
975 s->regs[GEM_TXPAUSE] = 0x0000ffff;
976 s->regs[GEM_TXPARTIALSF] = 0x000003ff;
977 s->regs[GEM_RXPARTIALSF] = 0x000003ff;
978 s->regs[GEM_MODID] = 0x00020118;
979 s->regs[GEM_DESCONF] = 0x02500111;
980 s->regs[GEM_DESCONF2] = 0x2ab13fff;
981 s->regs[GEM_DESCONF5] = 0x002f2145;
982 s->regs[GEM_DESCONF6] = 0x00000200;
983
984 gem_phy_reset(s);
985
986 gem_update_int_status(s);
987}
988
989static uint16_t gem_phy_read(GemState *s, unsigned reg_num)
990{
991 DB_PRINT("reg: %d value: 0x%04x\n", reg_num, s->phy_regs[reg_num]);
992 return s->phy_regs[reg_num];
993}
994
995static void gem_phy_write(GemState *s, unsigned reg_num, uint16_t val)
996{
997 DB_PRINT("reg: %d value: 0x%04x\n", reg_num, val);
998
999 switch (reg_num) {
1000 case PHY_REG_CONTROL:
1001 if (val & PHY_REG_CONTROL_RST) {
1002
1003 gem_phy_reset(s);
1004 val &= ~(PHY_REG_CONTROL_RST | PHY_REG_CONTROL_LOOP);
1005 s->phy_loop = 0;
1006 }
1007 if (val & PHY_REG_CONTROL_ANEG) {
1008
1009 val &= ~PHY_REG_CONTROL_ANEG;
1010 s->phy_regs[PHY_REG_STATUS] |= PHY_REG_STATUS_ANEGCMPL;
1011 }
1012 if (val & PHY_REG_CONTROL_LOOP) {
1013 DB_PRINT("PHY placed in loopback\n");
1014 s->phy_loop = 1;
1015 } else {
1016 s->phy_loop = 0;
1017 }
1018 break;
1019 }
1020 s->phy_regs[reg_num] = val;
1021}
1022
1023
1024
1025
1026
1027static uint64_t gem_read(void *opaque, hwaddr offset, unsigned size)
1028{
1029 GemState *s;
1030 uint32_t retval;
1031
1032 s = (GemState *)opaque;
1033
1034 offset >>= 2;
1035 retval = s->regs[offset];
1036
1037 DB_PRINT("offset: 0x%04x read: 0x%08x\n", (unsigned)offset*4, retval);
1038
1039 switch (offset) {
1040 case GEM_ISR:
1041 DB_PRINT("lowering irq on ISR read\n");
1042 qemu_set_irq(s->irq, 0);
1043 break;
1044 case GEM_PHYMNTNC:
1045 if (retval & GEM_PHYMNTNC_OP_R) {
1046 uint32_t phy_addr, reg_num;
1047
1048 phy_addr = (retval & GEM_PHYMNTNC_ADDR) >> GEM_PHYMNTNC_ADDR_SHFT;
1049 if (phy_addr == BOARD_PHY_ADDRESS) {
1050 reg_num = (retval & GEM_PHYMNTNC_REG) >> GEM_PHYMNTNC_REG_SHIFT;
1051 retval &= 0xFFFF0000;
1052 retval |= gem_phy_read(s, reg_num);
1053 } else {
1054 retval |= 0xFFFF;
1055 }
1056 }
1057 break;
1058 }
1059
1060
1061 s->regs[offset] &= ~(s->regs_rtc[offset]);
1062
1063
1064 retval &= ~(s->regs_wo[offset]);
1065
1066 DB_PRINT("0x%08x\n", retval);
1067 return retval;
1068}
1069
1070
1071
1072
1073
1074static void gem_write(void *opaque, hwaddr offset, uint64_t val,
1075 unsigned size)
1076{
1077 GemState *s = (GemState *)opaque;
1078 uint32_t readonly;
1079
1080 DB_PRINT("offset: 0x%04x write: 0x%08x ", (unsigned)offset, (unsigned)val);
1081 offset >>= 2;
1082
1083
1084 val &= ~(s->regs_ro[offset]);
1085
1086 readonly = s->regs[offset];
1087 readonly &= s->regs_ro[offset];
1088
1089
1090 val &= ~(s->regs_w1c[offset] & val);
1091
1092
1093 s->regs[offset] = val | readonly;
1094
1095
1096 switch (offset) {
1097 case GEM_NWCTRL:
1098 if (val & GEM_NWCTRL_TXSTART) {
1099 gem_transmit(s);
1100 }
1101 if (!(val & GEM_NWCTRL_TXENA)) {
1102
1103 s->tx_desc_addr = s->regs[GEM_TXQBASE];
1104 }
1105 if (!(val & GEM_NWCTRL_RXENA)) {
1106
1107 s->rx_desc_addr = s->regs[GEM_RXQBASE];
1108 }
1109 break;
1110
1111 case GEM_TXSTATUS:
1112 gem_update_int_status(s);
1113 break;
1114 case GEM_RXQBASE:
1115 s->rx_desc_addr = val;
1116 break;
1117 case GEM_TXQBASE:
1118 s->tx_desc_addr = val;
1119 break;
1120 case GEM_RXSTATUS:
1121 gem_update_int_status(s);
1122 break;
1123 case GEM_IER:
1124 s->regs[GEM_IMR] &= ~val;
1125 gem_update_int_status(s);
1126 break;
1127 case GEM_IDR:
1128 s->regs[GEM_IMR] |= val;
1129 gem_update_int_status(s);
1130 break;
1131 case GEM_PHYMNTNC:
1132 if (val & GEM_PHYMNTNC_OP_W) {
1133 uint32_t phy_addr, reg_num;
1134
1135 phy_addr = (val & GEM_PHYMNTNC_ADDR) >> GEM_PHYMNTNC_ADDR_SHFT;
1136 if (phy_addr == BOARD_PHY_ADDRESS) {
1137 reg_num = (val & GEM_PHYMNTNC_REG) >> GEM_PHYMNTNC_REG_SHIFT;
1138 gem_phy_write(s, reg_num, val);
1139 }
1140 }
1141 break;
1142 }
1143
1144 DB_PRINT("newval: 0x%08x\n", s->regs[offset]);
1145}
1146
1147static const MemoryRegionOps gem_ops = {
1148 .read = gem_read,
1149 .write = gem_write,
1150 .endianness = DEVICE_LITTLE_ENDIAN,
1151};
1152
1153static void gem_cleanup(NetClientState *nc)
1154{
1155 GemState *s = qemu_get_nic_opaque(nc);
1156
1157 DB_PRINT("\n");
1158 s->nic = NULL;
1159}
1160
1161static void gem_set_link(NetClientState *nc)
1162{
1163 DB_PRINT("\n");
1164 phy_update_link(qemu_get_nic_opaque(nc));
1165}
1166
1167static NetClientInfo net_gem_info = {
1168 .type = NET_CLIENT_OPTIONS_KIND_NIC,
1169 .size = sizeof(NICState),
1170 .can_receive = gem_can_receive,
1171 .receive = gem_receive,
1172 .cleanup = gem_cleanup,
1173 .link_status_changed = gem_set_link,
1174};
1175
1176static int gem_init(SysBusDevice *dev)
1177{
1178 GemState *s;
1179
1180 DB_PRINT("\n");
1181
1182 s = FROM_SYSBUS(GemState, dev);
1183 gem_init_register_masks(s);
1184 memory_region_init_io(&s->iomem, &gem_ops, s, "enet", sizeof(s->regs));
1185 sysbus_init_mmio(dev, &s->iomem);
1186 sysbus_init_irq(dev, &s->irq);
1187 qemu_macaddr_default_if_unset(&s->conf.macaddr);
1188
1189 s->nic = qemu_new_nic(&net_gem_info, &s->conf,
1190 object_get_typename(OBJECT(dev)), dev->qdev.id, s);
1191
1192 return 0;
1193}
1194
1195static const VMStateDescription vmstate_cadence_gem = {
1196 .name = "cadence_gem",
1197 .version_id = 1,
1198 .minimum_version_id = 1,
1199 .minimum_version_id_old = 1,
1200 .fields = (VMStateField[]) {
1201 VMSTATE_UINT32_ARRAY(regs, GemState, GEM_MAXREG),
1202 VMSTATE_UINT16_ARRAY(phy_regs, GemState, 32),
1203 VMSTATE_UINT8(phy_loop, GemState),
1204 VMSTATE_UINT32(rx_desc_addr, GemState),
1205 VMSTATE_UINT32(tx_desc_addr, GemState),
1206 }
1207};
1208
1209static Property gem_properties[] = {
1210 DEFINE_NIC_PROPERTIES(GemState, conf),
1211 DEFINE_PROP_END_OF_LIST(),
1212};
1213
1214static void gem_class_init(ObjectClass *klass, void *data)
1215{
1216 DeviceClass *dc = DEVICE_CLASS(klass);
1217 SysBusDeviceClass *sdc = SYS_BUS_DEVICE_CLASS(klass);
1218
1219 sdc->init = gem_init;
1220 dc->props = gem_properties;
1221 dc->vmsd = &vmstate_cadence_gem;
1222 dc->reset = gem_reset;
1223}
1224
1225static const TypeInfo gem_info = {
1226 .class_init = gem_class_init,
1227 .name = "cadence_gem",
1228 .parent = TYPE_SYS_BUS_DEVICE,
1229 .instance_size = sizeof(GemState),
1230};
1231
1232static void gem_register_types(void)
1233{
1234 type_register_static(&gem_info);
1235}
1236
1237type_init(gem_register_types)
1238