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