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 "qemu/osdep.h"
26#include <zlib.h>
27
28#include "hw/irq.h"
29#include "hw/net/cadence_gem.h"
30#include "hw/qdev-properties.h"
31#include "migration/vmstate.h"
32#include "qapi/error.h"
33#include "qemu/log.h"
34#include "qemu/module.h"
35#include "sysemu/dma.h"
36#include "net/checksum.h"
37#include "net/eth.h"
38#include "exec/address-spaces.h"
39
40#define CADENCE_GEM_ERR_DEBUG 0
41#define DB_PRINT(...) do {\
42 if (CADENCE_GEM_ERR_DEBUG) { \
43 qemu_log(": %s: ", __func__); \
44 qemu_log(__VA_ARGS__); \
45 } \
46} while (0);
47
48#define GEM_NWCTRL (0x00000000/4)
49#define GEM_NWCFG (0x00000004/4)
50#define GEM_NWSTATUS (0x00000008/4)
51#define GEM_USERIO (0x0000000C/4)
52#define GEM_DMACFG (0x00000010/4)
53#define GEM_TXSTATUS (0x00000014/4)
54#define GEM_RXQBASE (0x00000018/4)
55#define GEM_TXQBASE (0x0000001C/4)
56#define GEM_RXSTATUS (0x00000020/4)
57#define GEM_ISR (0x00000024/4)
58#define GEM_IER (0x00000028/4)
59#define GEM_IDR (0x0000002C/4)
60#define GEM_IMR (0x00000030/4)
61#define GEM_PHYMNTNC (0x00000034/4)
62#define GEM_RXPAUSE (0x00000038/4)
63#define GEM_TXPAUSE (0x0000003C/4)
64#define GEM_TXPARTIALSF (0x00000040/4)
65#define GEM_RXPARTIALSF (0x00000044/4)
66#define GEM_HASHLO (0x00000080/4)
67#define GEM_HASHHI (0x00000084/4)
68#define GEM_SPADDR1LO (0x00000088/4)
69#define GEM_SPADDR1HI (0x0000008C/4)
70#define GEM_SPADDR2LO (0x00000090/4)
71#define GEM_SPADDR2HI (0x00000094/4)
72#define GEM_SPADDR3LO (0x00000098/4)
73#define GEM_SPADDR3HI (0x0000009C/4)
74#define GEM_SPADDR4LO (0x000000A0/4)
75#define GEM_SPADDR4HI (0x000000A4/4)
76#define GEM_TIDMATCH1 (0x000000A8/4)
77#define GEM_TIDMATCH2 (0x000000AC/4)
78#define GEM_TIDMATCH3 (0x000000B0/4)
79#define GEM_TIDMATCH4 (0x000000B4/4)
80#define GEM_WOLAN (0x000000B8/4)
81#define GEM_IPGSTRETCH (0x000000BC/4)
82#define GEM_SVLAN (0x000000C0/4)
83#define GEM_MODID (0x000000FC/4)
84#define GEM_OCTTXLO (0x00000100/4)
85#define GEM_OCTTXHI (0x00000104/4)
86#define GEM_TXCNT (0x00000108/4)
87#define GEM_TXBCNT (0x0000010C/4)
88#define GEM_TXMCNT (0x00000110/4)
89#define GEM_TXPAUSECNT (0x00000114/4)
90#define GEM_TX64CNT (0x00000118/4)
91#define GEM_TX65CNT (0x0000011C/4)
92#define GEM_TX128CNT (0x00000120/4)
93#define GEM_TX256CNT (0x00000124/4)
94#define GEM_TX512CNT (0x00000128/4)
95#define GEM_TX1024CNT (0x0000012C/4)
96#define GEM_TX1519CNT (0x00000130/4)
97#define GEM_TXURUNCNT (0x00000134/4)
98#define GEM_SINGLECOLLCNT (0x00000138/4)
99#define GEM_MULTCOLLCNT (0x0000013C/4)
100#define GEM_EXCESSCOLLCNT (0x00000140/4)
101#define GEM_LATECOLLCNT (0x00000144/4)
102#define GEM_DEFERTXCNT (0x00000148/4)
103#define GEM_CSENSECNT (0x0000014C/4)
104#define GEM_OCTRXLO (0x00000150/4)
105#define GEM_OCTRXHI (0x00000154/4)
106#define GEM_RXCNT (0x00000158/4)
107#define GEM_RXBROADCNT (0x0000015C/4)
108#define GEM_RXMULTICNT (0x00000160/4)
109#define GEM_RXPAUSECNT (0x00000164/4)
110#define GEM_RX64CNT (0x00000168/4)
111#define GEM_RX65CNT (0x0000016C/4)
112#define GEM_RX128CNT (0x00000170/4)
113#define GEM_RX256CNT (0x00000174/4)
114#define GEM_RX512CNT (0x00000178/4)
115#define GEM_RX1024CNT (0x0000017C/4)
116#define GEM_RX1519CNT (0x00000180/4)
117#define GEM_RXUNDERCNT (0x00000184/4)
118#define GEM_RXOVERCNT (0x00000188/4)
119#define GEM_RXJABCNT (0x0000018C/4)
120#define GEM_RXFCSCNT (0x00000190/4)
121#define GEM_RXLENERRCNT (0x00000194/4)
122#define GEM_RXSYMERRCNT (0x00000198/4)
123#define GEM_RXALIGNERRCNT (0x0000019C/4)
124#define GEM_RXRSCERRCNT (0x000001A0/4)
125#define GEM_RXORUNCNT (0x000001A4/4)
126#define GEM_RXIPCSERRCNT (0x000001A8/4)
127#define GEM_RXTCPCCNT (0x000001AC/4)
128#define GEM_RXUDPCCNT (0x000001B0/4)
129
130#define GEM_1588S (0x000001D0/4)
131#define GEM_1588NS (0x000001D4/4)
132#define GEM_1588ADJ (0x000001D8/4)
133#define GEM_1588INC (0x000001DC/4)
134#define GEM_PTPETXS (0x000001E0/4)
135#define GEM_PTPETXNS (0x000001E4/4)
136#define GEM_PTPERXS (0x000001E8/4)
137#define GEM_PTPERXNS (0x000001EC/4)
138#define GEM_PTPPTXS (0x000001E0/4)
139#define GEM_PTPPTXNS (0x000001E4/4)
140#define GEM_PTPPRXS (0x000001E8/4)
141#define GEM_PTPPRXNS (0x000001EC/4)
142
143
144#define GEM_DESCONF (0x00000280/4)
145#define GEM_DESCONF2 (0x00000284/4)
146#define GEM_DESCONF3 (0x00000288/4)
147#define GEM_DESCONF4 (0x0000028C/4)
148#define GEM_DESCONF5 (0x00000290/4)
149#define GEM_DESCONF6 (0x00000294/4)
150#define GEM_DESCONF6_64B_MASK (1U << 23)
151#define GEM_DESCONF7 (0x00000298/4)
152
153#define GEM_INT_Q1_STATUS (0x00000400 / 4)
154#define GEM_INT_Q1_MASK (0x00000640 / 4)
155
156#define GEM_TRANSMIT_Q1_PTR (0x00000440 / 4)
157#define GEM_TRANSMIT_Q7_PTR (GEM_TRANSMIT_Q1_PTR + 6)
158
159#define GEM_RECEIVE_Q1_PTR (0x00000480 / 4)
160#define GEM_RECEIVE_Q7_PTR (GEM_RECEIVE_Q1_PTR + 6)
161
162#define GEM_TBQPH (0x000004C8 / 4)
163#define GEM_RBQPH (0x000004D4 / 4)
164
165#define GEM_INT_Q1_ENABLE (0x00000600 / 4)
166#define GEM_INT_Q7_ENABLE (GEM_INT_Q1_ENABLE + 6)
167
168#define GEM_INT_Q1_DISABLE (0x00000620 / 4)
169#define GEM_INT_Q7_DISABLE (GEM_INT_Q1_DISABLE + 6)
170
171#define GEM_INT_Q1_MASK (0x00000640 / 4)
172#define GEM_INT_Q7_MASK (GEM_INT_Q1_MASK + 6)
173
174#define GEM_SCREENING_TYPE1_REGISTER_0 (0x00000500 / 4)
175
176#define GEM_ST1R_UDP_PORT_MATCH_ENABLE (1 << 29)
177#define GEM_ST1R_DSTC_ENABLE (1 << 28)
178#define GEM_ST1R_UDP_PORT_MATCH_SHIFT (12)
179#define GEM_ST1R_UDP_PORT_MATCH_WIDTH (27 - GEM_ST1R_UDP_PORT_MATCH_SHIFT + 1)
180#define GEM_ST1R_DSTC_MATCH_SHIFT (4)
181#define GEM_ST1R_DSTC_MATCH_WIDTH (11 - GEM_ST1R_DSTC_MATCH_SHIFT + 1)
182#define GEM_ST1R_QUEUE_SHIFT (0)
183#define GEM_ST1R_QUEUE_WIDTH (3 - GEM_ST1R_QUEUE_SHIFT + 1)
184
185#define GEM_SCREENING_TYPE2_REGISTER_0 (0x00000540 / 4)
186
187#define GEM_ST2R_COMPARE_A_ENABLE (1 << 18)
188#define GEM_ST2R_COMPARE_A_SHIFT (13)
189#define GEM_ST2R_COMPARE_WIDTH (17 - GEM_ST2R_COMPARE_A_SHIFT + 1)
190#define GEM_ST2R_ETHERTYPE_ENABLE (1 << 12)
191#define GEM_ST2R_ETHERTYPE_INDEX_SHIFT (9)
192#define GEM_ST2R_ETHERTYPE_INDEX_WIDTH (11 - GEM_ST2R_ETHERTYPE_INDEX_SHIFT \
193 + 1)
194#define GEM_ST2R_QUEUE_SHIFT (0)
195#define GEM_ST2R_QUEUE_WIDTH (3 - GEM_ST2R_QUEUE_SHIFT + 1)
196
197#define GEM_SCREENING_TYPE2_ETHERTYPE_REG_0 (0x000006e0 / 4)
198#define GEM_TYPE2_COMPARE_0_WORD_0 (0x00000700 / 4)
199
200#define GEM_T2CW1_COMPARE_OFFSET_SHIFT (7)
201#define GEM_T2CW1_COMPARE_OFFSET_WIDTH (8 - GEM_T2CW1_COMPARE_OFFSET_SHIFT + 1)
202#define GEM_T2CW1_OFFSET_VALUE_SHIFT (0)
203#define GEM_T2CW1_OFFSET_VALUE_WIDTH (6 - GEM_T2CW1_OFFSET_VALUE_SHIFT + 1)
204
205
206#define GEM_NWCTRL_TXSTART 0x00000200
207#define GEM_NWCTRL_TXENA 0x00000008
208#define GEM_NWCTRL_RXENA 0x00000004
209#define GEM_NWCTRL_LOCALLOOP 0x00000002
210
211#define GEM_NWCFG_STRIP_FCS 0x00020000
212#define GEM_NWCFG_LERR_DISC 0x00010000
213#define GEM_NWCFG_BUFF_OFST_M 0x0000C000
214#define GEM_NWCFG_BUFF_OFST_S 14
215#define GEM_NWCFG_UCAST_HASH 0x00000080
216#define GEM_NWCFG_MCAST_HASH 0x00000040
217#define GEM_NWCFG_BCAST_REJ 0x00000020
218#define GEM_NWCFG_PROMISC 0x00000010
219
220#define GEM_DMACFG_ADDR_64B (1U << 30)
221#define GEM_DMACFG_TX_BD_EXT (1U << 29)
222#define GEM_DMACFG_RX_BD_EXT (1U << 28)
223#define GEM_DMACFG_RBUFSZ_M 0x00FF0000
224#define GEM_DMACFG_RBUFSZ_S 16
225#define GEM_DMACFG_RBUFSZ_MUL 64
226#define GEM_DMACFG_TXCSUM_OFFL 0x00000800
227
228#define GEM_TXSTATUS_TXCMPL 0x00000020
229#define GEM_TXSTATUS_USED 0x00000001
230
231#define GEM_RXSTATUS_FRMRCVD 0x00000002
232#define GEM_RXSTATUS_NOBUF 0x00000001
233
234
235#define GEM_INT_TXCMPL 0x00000080
236#define GEM_INT_TXUSED 0x00000008
237#define GEM_INT_RXUSED 0x00000004
238#define GEM_INT_RXCMPL 0x00000002
239
240#define GEM_PHYMNTNC_OP_R 0x20000000
241#define GEM_PHYMNTNC_OP_W 0x10000000
242#define GEM_PHYMNTNC_ADDR 0x0F800000
243#define GEM_PHYMNTNC_ADDR_SHFT 23
244#define GEM_PHYMNTNC_REG 0x007C0000
245#define GEM_PHYMNTNC_REG_SHIFT 18
246
247
248#define BOARD_PHY_ADDRESS 7
249
250#define PHY_REG_CONTROL 0
251#define PHY_REG_STATUS 1
252#define PHY_REG_PHYID1 2
253#define PHY_REG_PHYID2 3
254#define PHY_REG_ANEGADV 4
255#define PHY_REG_LINKPABIL 5
256#define PHY_REG_ANEGEXP 6
257#define PHY_REG_NEXTP 7
258#define PHY_REG_LINKPNEXTP 8
259#define PHY_REG_100BTCTRL 9
260#define PHY_REG_1000BTSTAT 10
261#define PHY_REG_EXTSTAT 15
262#define PHY_REG_PHYSPCFC_CTL 16
263#define PHY_REG_PHYSPCFC_ST 17
264#define PHY_REG_INT_EN 18
265#define PHY_REG_INT_ST 19
266#define PHY_REG_EXT_PHYSPCFC_CTL 20
267#define PHY_REG_RXERR 21
268#define PHY_REG_EACD 22
269#define PHY_REG_LED 24
270#define PHY_REG_LED_OVRD 25
271#define PHY_REG_EXT_PHYSPCFC_CTL2 26
272#define PHY_REG_EXT_PHYSPCFC_ST 27
273#define PHY_REG_CABLE_DIAG 28
274
275#define PHY_REG_CONTROL_RST 0x8000
276#define PHY_REG_CONTROL_LOOP 0x4000
277#define PHY_REG_CONTROL_ANEG 0x1000
278#define PHY_REG_CONTROL_ANRESTART 0x0200
279
280#define PHY_REG_STATUS_LINK 0x0004
281#define PHY_REG_STATUS_ANEGCMPL 0x0020
282
283#define PHY_REG_INT_ST_ANEGCMPL 0x0800
284#define PHY_REG_INT_ST_LINKC 0x0400
285#define PHY_REG_INT_ST_ENERGY 0x0010
286
287
288#define GEM_RX_REJECT (-1)
289#define GEM_RX_PROMISCUOUS_ACCEPT (-2)
290#define GEM_RX_BROADCAST_ACCEPT (-3)
291#define GEM_RX_MULTICAST_HASH_ACCEPT (-4)
292#define GEM_RX_UNICAST_HASH_ACCEPT (-5)
293
294#define GEM_RX_SAR_ACCEPT 0
295
296
297
298#define DESC_1_USED 0x80000000
299#define DESC_1_LENGTH 0x00003FFF
300
301#define DESC_1_TX_WRAP 0x40000000
302#define DESC_1_TX_LAST 0x00008000
303
304#define DESC_0_RX_WRAP 0x00000002
305#define DESC_0_RX_OWNERSHIP 0x00000001
306
307#define R_DESC_1_RX_SAR_SHIFT 25
308#define R_DESC_1_RX_SAR_LENGTH 2
309#define R_DESC_1_RX_SAR_MATCH (1 << 27)
310#define R_DESC_1_RX_UNICAST_HASH (1 << 29)
311#define R_DESC_1_RX_MULTICAST_HASH (1 << 30)
312#define R_DESC_1_RX_BROADCAST (1 << 31)
313
314#define DESC_1_RX_SOF 0x00004000
315#define DESC_1_RX_EOF 0x00008000
316
317#define GEM_MODID_VALUE 0x00020118
318
319static inline uint64_t tx_desc_get_buffer(CadenceGEMState *s, uint32_t *desc)
320{
321 uint64_t ret = desc[0];
322
323 if (s->regs[GEM_DMACFG] & GEM_DMACFG_ADDR_64B) {
324 ret |= (uint64_t)desc[2] << 32;
325 }
326 return ret;
327}
328
329static inline unsigned tx_desc_get_used(uint32_t *desc)
330{
331 return (desc[1] & DESC_1_USED) ? 1 : 0;
332}
333
334static inline void tx_desc_set_used(uint32_t *desc)
335{
336 desc[1] |= DESC_1_USED;
337}
338
339static inline unsigned tx_desc_get_wrap(uint32_t *desc)
340{
341 return (desc[1] & DESC_1_TX_WRAP) ? 1 : 0;
342}
343
344static inline unsigned tx_desc_get_last(uint32_t *desc)
345{
346 return (desc[1] & DESC_1_TX_LAST) ? 1 : 0;
347}
348
349static inline unsigned tx_desc_get_length(uint32_t *desc)
350{
351 return desc[1] & DESC_1_LENGTH;
352}
353
354static inline void print_gem_tx_desc(uint32_t *desc, uint8_t queue)
355{
356 DB_PRINT("TXDESC (queue %" PRId8 "):\n", queue);
357 DB_PRINT("bufaddr: 0x%08x\n", *desc);
358 DB_PRINT("used_hw: %d\n", tx_desc_get_used(desc));
359 DB_PRINT("wrap: %d\n", tx_desc_get_wrap(desc));
360 DB_PRINT("last: %d\n", tx_desc_get_last(desc));
361 DB_PRINT("length: %d\n", tx_desc_get_length(desc));
362}
363
364static inline uint64_t rx_desc_get_buffer(CadenceGEMState *s, uint32_t *desc)
365{
366 uint64_t ret = desc[0] & ~0x3UL;
367
368 if (s->regs[GEM_DMACFG] & GEM_DMACFG_ADDR_64B) {
369 ret |= (uint64_t)desc[2] << 32;
370 }
371 return ret;
372}
373
374static inline int gem_get_desc_len(CadenceGEMState *s, bool rx_n_tx)
375{
376 int ret = 2;
377
378 if (s->regs[GEM_DMACFG] & GEM_DMACFG_ADDR_64B) {
379 ret += 2;
380 }
381 if (s->regs[GEM_DMACFG] & (rx_n_tx ? GEM_DMACFG_RX_BD_EXT
382 : GEM_DMACFG_TX_BD_EXT)) {
383 ret += 2;
384 }
385
386 assert(ret <= DESC_MAX_NUM_WORDS);
387 return ret;
388}
389
390static inline unsigned rx_desc_get_wrap(uint32_t *desc)
391{
392 return desc[0] & DESC_0_RX_WRAP ? 1 : 0;
393}
394
395static inline unsigned rx_desc_get_ownership(uint32_t *desc)
396{
397 return desc[0] & DESC_0_RX_OWNERSHIP ? 1 : 0;
398}
399
400static inline void rx_desc_set_ownership(uint32_t *desc)
401{
402 desc[0] |= DESC_0_RX_OWNERSHIP;
403}
404
405static inline void rx_desc_set_sof(uint32_t *desc)
406{
407 desc[1] |= DESC_1_RX_SOF;
408}
409
410static inline void rx_desc_set_eof(uint32_t *desc)
411{
412 desc[1] |= DESC_1_RX_EOF;
413}
414
415static inline void rx_desc_set_length(uint32_t *desc, unsigned len)
416{
417 desc[1] &= ~DESC_1_LENGTH;
418 desc[1] |= len;
419}
420
421static inline void rx_desc_set_broadcast(uint32_t *desc)
422{
423 desc[1] |= R_DESC_1_RX_BROADCAST;
424}
425
426static inline void rx_desc_set_unicast_hash(uint32_t *desc)
427{
428 desc[1] |= R_DESC_1_RX_UNICAST_HASH;
429}
430
431static inline void rx_desc_set_multicast_hash(uint32_t *desc)
432{
433 desc[1] |= R_DESC_1_RX_MULTICAST_HASH;
434}
435
436static inline void rx_desc_set_sar(uint32_t *desc, int sar_idx)
437{
438 desc[1] = deposit32(desc[1], R_DESC_1_RX_SAR_SHIFT, R_DESC_1_RX_SAR_LENGTH,
439 sar_idx);
440 desc[1] |= R_DESC_1_RX_SAR_MATCH;
441}
442
443
444static const uint8_t broadcast_addr[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
445
446
447
448
449
450
451static void gem_init_register_masks(CadenceGEMState *s)
452{
453 unsigned int i;
454
455 memset(&s->regs_ro[0], 0, sizeof(s->regs_ro));
456 s->regs_ro[GEM_NWCTRL] = 0xFFF80000;
457 s->regs_ro[GEM_NWSTATUS] = 0xFFFFFFFF;
458 s->regs_ro[GEM_DMACFG] = 0x8E00F000;
459 s->regs_ro[GEM_TXSTATUS] = 0xFFFFFE08;
460 s->regs_ro[GEM_RXQBASE] = 0x00000003;
461 s->regs_ro[GEM_TXQBASE] = 0x00000003;
462 s->regs_ro[GEM_RXSTATUS] = 0xFFFFFFF0;
463 s->regs_ro[GEM_ISR] = 0xFFFFFFFF;
464 s->regs_ro[GEM_IMR] = 0xFFFFFFFF;
465 s->regs_ro[GEM_MODID] = 0xFFFFFFFF;
466 for (i = 0; i < s->num_priority_queues; i++) {
467 s->regs_ro[GEM_INT_Q1_STATUS + i] = 0xFFFFFFFF;
468 s->regs_ro[GEM_INT_Q1_ENABLE + i] = 0xFFFFE319;
469 s->regs_ro[GEM_INT_Q1_DISABLE + i] = 0xFFFFE319;
470 s->regs_ro[GEM_INT_Q1_MASK + i] = 0xFFFFFFFF;
471 }
472
473
474 memset(&s->regs_rtc[0], 0, sizeof(s->regs_rtc));
475 s->regs_rtc[GEM_ISR] = 0xFFFFFFFF;
476 for (i = 0; i < s->num_priority_queues; i++) {
477 s->regs_rtc[GEM_INT_Q1_STATUS + i] = 0x00000CE6;
478 }
479
480
481 memset(&s->regs_w1c[0], 0, sizeof(s->regs_w1c));
482 s->regs_w1c[GEM_TXSTATUS] = 0x000001F7;
483 s->regs_w1c[GEM_RXSTATUS] = 0x0000000F;
484
485
486 memset(&s->regs_wo[0], 0, sizeof(s->regs_wo));
487 s->regs_wo[GEM_NWCTRL] = 0x00073E60;
488 s->regs_wo[GEM_IER] = 0x07FFFFFF;
489 s->regs_wo[GEM_IDR] = 0x07FFFFFF;
490 for (i = 0; i < s->num_priority_queues; i++) {
491 s->regs_wo[GEM_INT_Q1_ENABLE + i] = 0x00000CE6;
492 s->regs_wo[GEM_INT_Q1_DISABLE + i] = 0x00000CE6;
493 }
494}
495
496
497
498
499
500static void phy_update_link(CadenceGEMState *s)
501{
502 DB_PRINT("down %d\n", qemu_get_queue(s->nic)->link_down);
503
504
505 if (qemu_get_queue(s->nic)->link_down) {
506 s->phy_regs[PHY_REG_STATUS] &= ~(PHY_REG_STATUS_ANEGCMPL |
507 PHY_REG_STATUS_LINK);
508 s->phy_regs[PHY_REG_INT_ST] |= PHY_REG_INT_ST_LINKC;
509 } else {
510 s->phy_regs[PHY_REG_STATUS] |= (PHY_REG_STATUS_ANEGCMPL |
511 PHY_REG_STATUS_LINK);
512 s->phy_regs[PHY_REG_INT_ST] |= (PHY_REG_INT_ST_LINKC |
513 PHY_REG_INT_ST_ANEGCMPL |
514 PHY_REG_INT_ST_ENERGY);
515 }
516}
517
518static int gem_can_receive(NetClientState *nc)
519{
520 CadenceGEMState *s;
521 int i;
522
523 s = qemu_get_nic_opaque(nc);
524
525
526 if (!(s->regs[GEM_NWCTRL] & GEM_NWCTRL_RXENA)) {
527 if (s->can_rx_state != 1) {
528 s->can_rx_state = 1;
529 DB_PRINT("can't receive - no enable\n");
530 }
531 return 0;
532 }
533
534 for (i = 0; i < s->num_priority_queues; i++) {
535 if (rx_desc_get_ownership(s->rx_desc[i]) != 1) {
536 break;
537 }
538 };
539
540 if (i == s->num_priority_queues) {
541 if (s->can_rx_state != 2) {
542 s->can_rx_state = 2;
543 DB_PRINT("can't receive - all the buffer descriptors are busy\n");
544 }
545 return 0;
546 }
547
548 if (s->can_rx_state != 0) {
549 s->can_rx_state = 0;
550 DB_PRINT("can receive\n");
551 }
552 return 1;
553}
554
555
556
557
558
559static void gem_update_int_status(CadenceGEMState *s)
560{
561 int i;
562
563 qemu_set_irq(s->irq[0], !!s->regs[GEM_ISR]);
564
565 for (i = 1; i < s->num_priority_queues; ++i) {
566 qemu_set_irq(s->irq[i], !!s->regs[GEM_INT_Q1_STATUS + i - 1]);
567 }
568}
569
570
571
572
573
574static void gem_receive_updatestats(CadenceGEMState *s, const uint8_t *packet,
575 unsigned bytes)
576{
577 uint64_t octets;
578
579
580 octets = ((uint64_t)(s->regs[GEM_OCTRXLO]) << 32) |
581 s->regs[GEM_OCTRXHI];
582 octets += bytes;
583 s->regs[GEM_OCTRXLO] = octets >> 32;
584 s->regs[GEM_OCTRXHI] = octets;
585
586
587 s->regs[GEM_RXCNT]++;
588
589
590 if (!memcmp(packet, broadcast_addr, 6)) {
591 s->regs[GEM_RXBROADCNT]++;
592 }
593
594
595 if (packet[0] == 0x01) {
596 s->regs[GEM_RXMULTICNT]++;
597 }
598
599 if (bytes <= 64) {
600 s->regs[GEM_RX64CNT]++;
601 } else if (bytes <= 127) {
602 s->regs[GEM_RX65CNT]++;
603 } else if (bytes <= 255) {
604 s->regs[GEM_RX128CNT]++;
605 } else if (bytes <= 511) {
606 s->regs[GEM_RX256CNT]++;
607 } else if (bytes <= 1023) {
608 s->regs[GEM_RX512CNT]++;
609 } else if (bytes <= 1518) {
610 s->regs[GEM_RX1024CNT]++;
611 } else {
612 s->regs[GEM_RX1519CNT]++;
613 }
614}
615
616
617
618
619static unsigned get_bit(const uint8_t *mac, unsigned bit)
620{
621 unsigned byte;
622
623 byte = mac[bit / 8];
624 byte >>= (bit & 0x7);
625 byte &= 1;
626
627 return byte;
628}
629
630
631
632
633static unsigned calc_mac_hash(const uint8_t *mac)
634{
635 int index_bit, mac_bit;
636 unsigned hash_index;
637
638 hash_index = 0;
639 mac_bit = 5;
640 for (index_bit = 5; index_bit >= 0; index_bit--) {
641 hash_index |= (get_bit(mac, mac_bit) ^
642 get_bit(mac, mac_bit + 6) ^
643 get_bit(mac, mac_bit + 12) ^
644 get_bit(mac, mac_bit + 18) ^
645 get_bit(mac, mac_bit + 24) ^
646 get_bit(mac, mac_bit + 30) ^
647 get_bit(mac, mac_bit + 36) ^
648 get_bit(mac, mac_bit + 42)) << index_bit;
649 mac_bit--;
650 }
651
652 return hash_index;
653}
654
655
656
657
658
659
660
661
662
663
664
665static int gem_mac_address_filter(CadenceGEMState *s, const uint8_t *packet)
666{
667 uint8_t *gem_spaddr;
668 int i, is_mc;
669
670
671 if (s->regs[GEM_NWCFG] & GEM_NWCFG_PROMISC) {
672 return GEM_RX_PROMISCUOUS_ACCEPT;
673 }
674
675 if (!memcmp(packet, broadcast_addr, 6)) {
676
677 if (s->regs[GEM_NWCFG] & GEM_NWCFG_BCAST_REJ) {
678 return GEM_RX_REJECT;
679 }
680 return GEM_RX_BROADCAST_ACCEPT;
681 }
682
683
684 is_mc = is_multicast_ether_addr(packet);
685 if ((is_mc && (s->regs[GEM_NWCFG] & GEM_NWCFG_MCAST_HASH)) ||
686 (!is_mc && (s->regs[GEM_NWCFG] & GEM_NWCFG_UCAST_HASH))) {
687 uint64_t buckets;
688 unsigned hash_index;
689
690 hash_index = calc_mac_hash(packet);
691 buckets = ((uint64_t)s->regs[GEM_HASHHI] << 32) | s->regs[GEM_HASHLO];
692 if ((buckets >> hash_index) & 1) {
693 return is_mc ? GEM_RX_MULTICAST_HASH_ACCEPT
694 : GEM_RX_UNICAST_HASH_ACCEPT;
695 }
696 }
697
698
699 gem_spaddr = (uint8_t *)&(s->regs[GEM_SPADDR1LO]);
700 for (i = 3; i >= 0; i--) {
701 if (s->sar_active[i] && !memcmp(packet, gem_spaddr + 8 * i, 6)) {
702 return GEM_RX_SAR_ACCEPT + i;
703 }
704 }
705
706
707 return GEM_RX_REJECT;
708}
709
710
711static int get_queue_from_screen(CadenceGEMState *s, uint8_t *rxbuf_ptr,
712 unsigned rxbufsize)
713{
714 uint32_t reg;
715 bool matched, mismatched;
716 int i, j;
717
718 for (i = 0; i < s->num_type1_screeners; i++) {
719 reg = s->regs[GEM_SCREENING_TYPE1_REGISTER_0 + i];
720 matched = false;
721 mismatched = false;
722
723
724 if (reg & GEM_ST1R_UDP_PORT_MATCH_ENABLE) {
725 uint16_t udp_port = rxbuf_ptr[14 + 22] << 8 | rxbuf_ptr[14 + 23];
726 if (udp_port == extract32(reg, GEM_ST1R_UDP_PORT_MATCH_SHIFT,
727 GEM_ST1R_UDP_PORT_MATCH_WIDTH)) {
728 matched = true;
729 } else {
730 mismatched = true;
731 }
732 }
733
734
735 if (reg & GEM_ST1R_DSTC_ENABLE) {
736 uint8_t dscp = rxbuf_ptr[14 + 1];
737 if (dscp == extract32(reg, GEM_ST1R_DSTC_MATCH_SHIFT,
738 GEM_ST1R_DSTC_MATCH_WIDTH)) {
739 matched = true;
740 } else {
741 mismatched = true;
742 }
743 }
744
745 if (matched && !mismatched) {
746 return extract32(reg, GEM_ST1R_QUEUE_SHIFT, GEM_ST1R_QUEUE_WIDTH);
747 }
748 }
749
750 for (i = 0; i < s->num_type2_screeners; i++) {
751 reg = s->regs[GEM_SCREENING_TYPE2_REGISTER_0 + i];
752 matched = false;
753 mismatched = false;
754
755 if (reg & GEM_ST2R_ETHERTYPE_ENABLE) {
756 uint16_t type = rxbuf_ptr[12] << 8 | rxbuf_ptr[13];
757 int et_idx = extract32(reg, GEM_ST2R_ETHERTYPE_INDEX_SHIFT,
758 GEM_ST2R_ETHERTYPE_INDEX_WIDTH);
759
760 if (et_idx > s->num_type2_screeners) {
761 qemu_log_mask(LOG_GUEST_ERROR, "Out of range ethertype "
762 "register index: %d\n", et_idx);
763 }
764 if (type == s->regs[GEM_SCREENING_TYPE2_ETHERTYPE_REG_0 +
765 et_idx]) {
766 matched = true;
767 } else {
768 mismatched = true;
769 }
770 }
771
772
773 for (j = 0; j < 3; j++) {
774 uint32_t cr0, cr1, mask;
775 uint16_t rx_cmp;
776 int offset;
777 int cr_idx = extract32(reg, GEM_ST2R_COMPARE_A_SHIFT + j * 6,
778 GEM_ST2R_COMPARE_WIDTH);
779
780 if (!(reg & (GEM_ST2R_COMPARE_A_ENABLE << (j * 6)))) {
781 continue;
782 }
783 if (cr_idx > s->num_type2_screeners) {
784 qemu_log_mask(LOG_GUEST_ERROR, "Out of range compare "
785 "register index: %d\n", cr_idx);
786 }
787
788 cr0 = s->regs[GEM_TYPE2_COMPARE_0_WORD_0 + cr_idx * 2];
789 cr1 = s->regs[GEM_TYPE2_COMPARE_0_WORD_0 + cr_idx * 2 + 1];
790 offset = extract32(cr1, GEM_T2CW1_OFFSET_VALUE_SHIFT,
791 GEM_T2CW1_OFFSET_VALUE_WIDTH);
792
793 switch (extract32(cr1, GEM_T2CW1_COMPARE_OFFSET_SHIFT,
794 GEM_T2CW1_COMPARE_OFFSET_WIDTH)) {
795 case 3:
796 qemu_log_mask(LOG_UNIMP, "TCP compare offsets"
797 "unimplemented - assuming UDP\n");
798 offset += 8;
799
800 case 2:
801 offset += 20;
802
803 case 1:
804 offset += 14;
805 break;
806 case 0:
807
808 break;
809 }
810
811 rx_cmp = rxbuf_ptr[offset] << 8 | rxbuf_ptr[offset];
812 mask = extract32(cr0, 0, 16);
813
814 if ((rx_cmp & mask) == (extract32(cr0, 16, 16) & mask)) {
815 matched = true;
816 } else {
817 mismatched = true;
818 }
819 }
820
821 if (matched && !mismatched) {
822 return extract32(reg, GEM_ST2R_QUEUE_SHIFT, GEM_ST2R_QUEUE_WIDTH);
823 }
824 }
825
826
827 return 0;
828}
829
830static uint32_t gem_get_queue_base_addr(CadenceGEMState *s, bool tx, int q)
831{
832 uint32_t base_addr = 0;
833
834 switch (q) {
835 case 0:
836 base_addr = s->regs[tx ? GEM_TXQBASE : GEM_RXQBASE];
837 break;
838 case 1 ... (MAX_PRIORITY_QUEUES - 1):
839 base_addr = s->regs[(tx ? GEM_TRANSMIT_Q1_PTR :
840 GEM_RECEIVE_Q1_PTR) + q - 1];
841 break;
842 default:
843 g_assert_not_reached();
844 };
845
846 return base_addr;
847}
848
849static hwaddr gem_get_desc_addr(CadenceGEMState *s, bool tx, int q)
850{
851 hwaddr desc_addr = 0;
852
853 if (s->regs[GEM_DMACFG] & GEM_DMACFG_ADDR_64B) {
854 desc_addr = s->regs[tx ? GEM_TBQPH : GEM_RBQPH];
855 }
856 desc_addr <<= 32;
857 desc_addr |= tx ? s->tx_desc_addr[q] : s->rx_desc_addr[q];
858 return desc_addr;
859}
860
861static hwaddr gem_get_tx_desc_addr(CadenceGEMState *s, int q)
862{
863 return gem_get_desc_addr(s, true, q);
864}
865
866static hwaddr gem_get_rx_desc_addr(CadenceGEMState *s, int q)
867{
868 return gem_get_desc_addr(s, false, q);
869}
870
871static void gem_get_rx_desc(CadenceGEMState *s, int q)
872{
873 hwaddr desc_addr = gem_get_rx_desc_addr(s, q);
874
875 DB_PRINT("read descriptor 0x%" HWADDR_PRIx "\n", desc_addr);
876
877
878 address_space_read(&s->dma_as, desc_addr, *s->attr,
879 (uint8_t *)s->rx_desc[q],
880 sizeof(uint32_t) * gem_get_desc_len(s, true));
881
882
883 if (rx_desc_get_ownership(s->rx_desc[q]) == 1) {
884 DB_PRINT("descriptor 0x%" HWADDR_PRIx " owned by sw.\n", desc_addr);
885 s->regs[GEM_RXSTATUS] |= GEM_RXSTATUS_NOBUF;
886 if (q == 0) {
887 s->regs[GEM_ISR] |= GEM_INT_RXUSED & ~(s->regs[GEM_IMR]);
888 } else {
889 s->regs[GEM_INT_Q1_STATUS + q - 1] |= GEM_INT_RXUSED &
890 ~(s->regs[GEM_INT_Q1_MASK + q - 1]);
891 }
892
893
894 gem_update_int_status(s);
895 }
896}
897
898
899
900
901
902static ssize_t gem_receive(NetClientState *nc, const uint8_t *buf, size_t size)
903{
904 CadenceGEMState *s;
905 unsigned rxbufsize, bytes_to_copy;
906 unsigned rxbuf_offset;
907 uint8_t rxbuf[2048];
908 uint8_t *rxbuf_ptr;
909 bool first_desc = true;
910 int maf;
911 int q = 0;
912
913 s = qemu_get_nic_opaque(nc);
914
915
916 maf = gem_mac_address_filter(s, buf);
917 if (maf == GEM_RX_REJECT) {
918 return size;
919 }
920
921
922 if (s->regs[GEM_NWCFG] & GEM_NWCFG_LERR_DISC) {
923 unsigned type_len;
924
925
926 type_len = buf[12] << 8 | buf[13];
927
928 if (type_len < 0x600) {
929 if (size < type_len) {
930
931 return -1;
932 }
933 }
934 }
935
936
937
938
939 rxbuf_offset = (s->regs[GEM_NWCFG] & GEM_NWCFG_BUFF_OFST_M) >>
940 GEM_NWCFG_BUFF_OFST_S;
941
942
943
944
945 rxbufsize = ((s->regs[GEM_DMACFG] & GEM_DMACFG_RBUFSZ_M) >>
946 GEM_DMACFG_RBUFSZ_S) * GEM_DMACFG_RBUFSZ_MUL;
947 bytes_to_copy = size;
948
949
950
951
952 if (rxbufsize < GEM_DMACFG_RBUFSZ_MUL) {
953 rxbufsize = GEM_DMACFG_RBUFSZ_MUL;
954 }
955
956
957
958
959
960 if (size < 60) {
961 size = 60;
962 }
963
964
965 if (s->regs[GEM_NWCFG] & GEM_NWCFG_STRIP_FCS) {
966 rxbuf_ptr = (void *)buf;
967 } else {
968 unsigned crc_val;
969
970 if (size > sizeof(rxbuf) - sizeof(crc_val)) {
971 size = sizeof(rxbuf) - sizeof(crc_val);
972 }
973 bytes_to_copy = size;
974
975
976
977
978 memcpy(rxbuf, buf, size);
979 memset(rxbuf + size, 0, sizeof(rxbuf) - size);
980 rxbuf_ptr = rxbuf;
981 crc_val = cpu_to_le32(crc32(0, rxbuf, MAX(size, 60)));
982 memcpy(rxbuf + size, &crc_val, sizeof(crc_val));
983
984 bytes_to_copy += 4;
985 size += 4;
986 }
987
988 DB_PRINT("config bufsize: %d packet size: %ld\n", rxbufsize, size);
989
990
991 q = get_queue_from_screen(s, rxbuf_ptr, rxbufsize);
992
993 while (bytes_to_copy) {
994 hwaddr desc_addr;
995
996
997 if (!gem_can_receive(nc)) {
998 return -1;
999 }
1000
1001 DB_PRINT("copy %" PRIu32 " bytes to 0x%" PRIx64 "\n",
1002 MIN(bytes_to_copy, rxbufsize),
1003 rx_desc_get_buffer(s, s->rx_desc[q]) + rxbuf_offset);
1004
1005
1006 address_space_write(&s->dma_as, rx_desc_get_buffer(s, s->rx_desc[q]) +
1007 rxbuf_offset,
1008 *s->attr, rxbuf_ptr,
1009 MIN(bytes_to_copy, rxbufsize));
1010 rxbuf_ptr += MIN(bytes_to_copy, rxbufsize);
1011 bytes_to_copy -= MIN(bytes_to_copy, rxbufsize);
1012
1013
1014 if (first_desc) {
1015 rx_desc_set_sof(s->rx_desc[q]);
1016 first_desc = false;
1017 }
1018 if (bytes_to_copy == 0) {
1019 rx_desc_set_eof(s->rx_desc[q]);
1020 rx_desc_set_length(s->rx_desc[q], size);
1021 }
1022 rx_desc_set_ownership(s->rx_desc[q]);
1023
1024 switch (maf) {
1025 case GEM_RX_PROMISCUOUS_ACCEPT:
1026 break;
1027 case GEM_RX_BROADCAST_ACCEPT:
1028 rx_desc_set_broadcast(s->rx_desc[q]);
1029 break;
1030 case GEM_RX_UNICAST_HASH_ACCEPT:
1031 rx_desc_set_unicast_hash(s->rx_desc[q]);
1032 break;
1033 case GEM_RX_MULTICAST_HASH_ACCEPT:
1034 rx_desc_set_multicast_hash(s->rx_desc[q]);
1035 break;
1036 case GEM_RX_REJECT:
1037 abort();
1038 default:
1039 rx_desc_set_sar(s->rx_desc[q], maf);
1040 }
1041
1042
1043 desc_addr = gem_get_rx_desc_addr(s, q);
1044 address_space_write(&s->dma_as, desc_addr,
1045 *s->attr,
1046 (uint8_t *)s->rx_desc[q],
1047 sizeof(uint32_t) * gem_get_desc_len(s, true));
1048
1049
1050 if (rx_desc_get_wrap(s->rx_desc[q])) {
1051 DB_PRINT("wrapping RX descriptor list\n");
1052 s->rx_desc_addr[q] = gem_get_queue_base_addr(s, false, q);
1053 } else {
1054 DB_PRINT("incrementing RX descriptor list\n");
1055 s->rx_desc_addr[q] += 4 * gem_get_desc_len(s, true);
1056 }
1057
1058 gem_get_rx_desc(s, q);
1059 }
1060
1061
1062 gem_receive_updatestats(s, buf, size);
1063
1064 s->regs[GEM_RXSTATUS] |= GEM_RXSTATUS_FRMRCVD;
1065 if (q == 0) {
1066 s->regs[GEM_ISR] |= GEM_INT_RXCMPL & ~(s->regs[GEM_IMR]);
1067 } else {
1068 s->regs[GEM_INT_Q1_STATUS + q - 1] |= GEM_INT_RXCMPL &
1069 ~(s->regs[GEM_INT_Q1_MASK + q - 1]);
1070 }
1071
1072 gem_update_int_status(s);
1073
1074 return size;
1075}
1076
1077
1078
1079
1080
1081static void gem_transmit_updatestats(CadenceGEMState *s, const uint8_t *packet,
1082 unsigned bytes)
1083{
1084 uint64_t octets;
1085
1086
1087 octets = ((uint64_t)(s->regs[GEM_OCTTXLO]) << 32) |
1088 s->regs[GEM_OCTTXHI];
1089 octets += bytes;
1090 s->regs[GEM_OCTTXLO] = octets >> 32;
1091 s->regs[GEM_OCTTXHI] = octets;
1092
1093
1094 s->regs[GEM_TXCNT]++;
1095
1096
1097 if (!memcmp(packet, broadcast_addr, 6)) {
1098 s->regs[GEM_TXBCNT]++;
1099 }
1100
1101
1102 if (packet[0] == 0x01) {
1103 s->regs[GEM_TXMCNT]++;
1104 }
1105
1106 if (bytes <= 64) {
1107 s->regs[GEM_TX64CNT]++;
1108 } else if (bytes <= 127) {
1109 s->regs[GEM_TX65CNT]++;
1110 } else if (bytes <= 255) {
1111 s->regs[GEM_TX128CNT]++;
1112 } else if (bytes <= 511) {
1113 s->regs[GEM_TX256CNT]++;
1114 } else if (bytes <= 1023) {
1115 s->regs[GEM_TX512CNT]++;
1116 } else if (bytes <= 1518) {
1117 s->regs[GEM_TX1024CNT]++;
1118 } else {
1119 s->regs[GEM_TX1519CNT]++;
1120 }
1121}
1122
1123
1124
1125
1126
1127static void gem_transmit(CadenceGEMState *s)
1128{
1129 uint32_t desc[DESC_MAX_NUM_WORDS];
1130 hwaddr packet_desc_addr;
1131 uint8_t tx_packet[10240];
1132 uint8_t *p;
1133 unsigned total_bytes;
1134 int q = 0;
1135
1136
1137 if (!(s->regs[GEM_NWCTRL] & GEM_NWCTRL_TXENA)) {
1138 return;
1139 }
1140
1141 DB_PRINT("\n");
1142
1143
1144
1145
1146
1147 p = tx_packet;
1148 total_bytes = 0;
1149
1150 for (q = s->num_priority_queues - 1; q >= 0; q--) {
1151
1152 packet_desc_addr = gem_get_tx_desc_addr(s, q);
1153
1154 DB_PRINT("read descriptor 0x%" HWADDR_PRIx "\n", packet_desc_addr);
1155 address_space_read(&s->dma_as, packet_desc_addr,
1156 *s->attr, (uint8_t *)desc,
1157 sizeof(uint32_t) * gem_get_desc_len(s, false));
1158
1159 while (tx_desc_get_used(desc) == 0) {
1160
1161
1162 if (!(s->regs[GEM_NWCTRL] & GEM_NWCTRL_TXENA)) {
1163 return;
1164 }
1165 print_gem_tx_desc(desc, q);
1166
1167
1168
1169
1170 if ((tx_desc_get_buffer(s, desc) == 0) ||
1171 (tx_desc_get_length(desc) == 0)) {
1172 DB_PRINT("Invalid TX descriptor @ 0x%" HWADDR_PRIx "\n",
1173 packet_desc_addr);
1174 break;
1175 }
1176
1177 if (tx_desc_get_length(desc) > sizeof(tx_packet) -
1178 (p - tx_packet)) {
1179 DB_PRINT("TX descriptor @ 0x%" HWADDR_PRIx " \
1180 too large: size 0x%"PRIx32" space 0x%"PRIx64"\n",
1181 packet_desc_addr,
1182 tx_desc_get_length(desc),
1183 sizeof(tx_packet) - (p - tx_packet));
1184 break;
1185 }
1186
1187
1188
1189
1190 address_space_read(&s->dma_as, tx_desc_get_buffer(s, desc),
1191 *s->attr,
1192 p, tx_desc_get_length(desc));
1193 p += tx_desc_get_length(desc);
1194 total_bytes += tx_desc_get_length(desc);
1195
1196
1197 if (tx_desc_get_last(desc)) {
1198 uint32_t desc_first[DESC_MAX_NUM_WORDS];
1199 hwaddr desc_addr = gem_get_tx_desc_addr(s, q);
1200
1201
1202
1203
1204 address_space_read(&s->dma_as, desc_addr,
1205 *s->attr,
1206 (uint8_t *)desc_first,
1207 sizeof(desc_first));
1208 tx_desc_set_used(desc_first);
1209 address_space_write(&s->dma_as, desc_addr,
1210 *s->attr,
1211 (uint8_t *)desc_first,
1212 sizeof(desc_first));
1213
1214 if (tx_desc_get_wrap(desc)) {
1215 s->tx_desc_addr[q] = gem_get_queue_base_addr(s,
1216 true, q);
1217 } else {
1218 s->tx_desc_addr[q] = (uint32_t)packet_desc_addr +
1219 4 * gem_get_desc_len(s, false);
1220 }
1221 DB_PRINT("TX descriptor next: 0x%08x\n", s->tx_desc_addr[q]);
1222
1223 s->regs[GEM_TXSTATUS] |= GEM_TXSTATUS_TXCMPL;
1224 if (q == 0) {
1225 s->regs[GEM_ISR] |= GEM_INT_TXCMPL & ~(s->regs[GEM_IMR]);
1226 } else {
1227
1228 s->regs[GEM_INT_Q1_STATUS + q - 1] |=
1229 GEM_INT_TXCMPL & ~s->regs[GEM_INT_Q1_MASK + q - 1];
1230 }
1231
1232
1233 gem_update_int_status(s);
1234
1235
1236 if (s->regs[GEM_DMACFG] & GEM_DMACFG_TXCSUM_OFFL) {
1237 net_checksum_calculate(tx_packet, total_bytes);
1238 }
1239
1240
1241 gem_transmit_updatestats(s, tx_packet, total_bytes);
1242
1243
1244 if (s->phy_loop || (s->regs[GEM_NWCTRL] &
1245 GEM_NWCTRL_LOCALLOOP)) {
1246 gem_receive(qemu_get_queue(s->nic), tx_packet,
1247 total_bytes);
1248 } else {
1249 qemu_send_packet(qemu_get_queue(s->nic), tx_packet,
1250 total_bytes);
1251 }
1252
1253
1254 p = tx_packet;
1255 total_bytes = 0;
1256 }
1257
1258
1259 if (tx_desc_get_wrap(desc)) {
1260 packet_desc_addr = 0;
1261 if (s->regs[GEM_DMACFG] & GEM_DMACFG_ADDR_64B) {
1262 packet_desc_addr = s->regs[GEM_TBQPH];
1263 packet_desc_addr <<= 32;
1264 }
1265 packet_desc_addr |= gem_get_queue_base_addr(s, true, q);
1266 } else {
1267 packet_desc_addr += 4 * gem_get_desc_len(s, false);
1268 }
1269 DB_PRINT("read descriptor 0x%" HWADDR_PRIx "\n", packet_desc_addr);
1270 address_space_read(&s->dma_as, packet_desc_addr,
1271 *s->attr, (uint8_t *)desc,
1272 sizeof(uint32_t) * gem_get_desc_len(s, false));
1273 }
1274
1275 if (tx_desc_get_used(desc)) {
1276 s->regs[GEM_TXSTATUS] |= GEM_TXSTATUS_USED;
1277
1278 if (q == 0) {
1279 s->regs[GEM_ISR] |= GEM_INT_TXUSED & ~(s->regs[GEM_IMR]);
1280 }
1281 gem_update_int_status(s);
1282 }
1283 }
1284}
1285
1286static void gem_phy_reset(CadenceGEMState *s)
1287{
1288 memset(&s->phy_regs[0], 0, sizeof(s->phy_regs));
1289 s->phy_regs[PHY_REG_CONTROL] = 0x1140;
1290 s->phy_regs[PHY_REG_STATUS] = 0x7969;
1291 s->phy_regs[PHY_REG_PHYID1] = 0x0141;
1292 s->phy_regs[PHY_REG_PHYID2] = 0x0CC2;
1293 s->phy_regs[PHY_REG_ANEGADV] = 0x01E1;
1294 s->phy_regs[PHY_REG_LINKPABIL] = 0xCDE1;
1295 s->phy_regs[PHY_REG_ANEGEXP] = 0x000F;
1296 s->phy_regs[PHY_REG_NEXTP] = 0x2001;
1297 s->phy_regs[PHY_REG_LINKPNEXTP] = 0x40E6;
1298 s->phy_regs[PHY_REG_100BTCTRL] = 0x0300;
1299 s->phy_regs[PHY_REG_1000BTSTAT] = 0x7C00;
1300 s->phy_regs[PHY_REG_EXTSTAT] = 0x3000;
1301 s->phy_regs[PHY_REG_PHYSPCFC_CTL] = 0x0078;
1302 s->phy_regs[PHY_REG_PHYSPCFC_ST] = 0x7C00;
1303 s->phy_regs[PHY_REG_EXT_PHYSPCFC_CTL] = 0x0C60;
1304 s->phy_regs[PHY_REG_LED] = 0x4100;
1305 s->phy_regs[PHY_REG_EXT_PHYSPCFC_CTL2] = 0x000A;
1306 s->phy_regs[PHY_REG_EXT_PHYSPCFC_ST] = 0x848B;
1307
1308 phy_update_link(s);
1309}
1310
1311static void gem_reset(DeviceState *d)
1312{
1313 int i;
1314 CadenceGEMState *s = CADENCE_GEM(d);
1315 const uint8_t *a;
1316 uint32_t queues_mask = 0;
1317
1318 DB_PRINT("\n");
1319
1320
1321 memset(&s->regs[0], 0, sizeof(s->regs));
1322 s->regs[GEM_NWCFG] = 0x00080000;
1323 s->regs[GEM_NWSTATUS] = 0x00000006;
1324 s->regs[GEM_DMACFG] = 0x00020784;
1325 s->regs[GEM_IMR] = 0x07ffffff;
1326 s->regs[GEM_TXPAUSE] = 0x0000ffff;
1327 s->regs[GEM_TXPARTIALSF] = 0x000003ff;
1328 s->regs[GEM_RXPARTIALSF] = 0x000003ff;
1329 s->regs[GEM_MODID] = s->revision;
1330 s->regs[GEM_DESCONF] = 0x02D00111;
1331 s->regs[GEM_DESCONF2] = 0x2ab12800;
1332 s->regs[GEM_DESCONF5] = 0x002f2045;
1333 s->regs[GEM_DESCONF6] = GEM_DESCONF6_64B_MASK;
1334 s->regs[GEM_INT_Q1_MASK] = 0x00000CE6;
1335
1336 if (s->num_priority_queues > 1) {
1337 queues_mask = MAKE_64BIT_MASK(1, s->num_priority_queues - 1);
1338 s->regs[GEM_DESCONF6] |= queues_mask;
1339 }
1340
1341
1342 a = &s->conf.macaddr.a[0];
1343 s->regs[GEM_SPADDR1LO] = a[0] | (a[1] << 8) | (a[2] << 16) | (a[3] << 24);
1344 s->regs[GEM_SPADDR1HI] = a[4] | (a[5] << 8);
1345
1346 for (i = 0; i < 4; i++) {
1347 s->sar_active[i] = false;
1348 }
1349
1350 if (s->mdio) {
1351 phy_update_link(s);
1352 } else {
1353 gem_phy_reset(s);
1354 }
1355
1356 gem_update_int_status(s);
1357}
1358
1359static uint16_t gem_phy_read(CadenceGEMState *s, unsigned reg_num)
1360{
1361 DB_PRINT("reg: %d value: 0x%04x\n", reg_num, s->phy_regs[reg_num]);
1362
1363 assert(!s->mdio);
1364
1365 return s->phy_regs[reg_num];
1366}
1367
1368static void gem_phy_write(CadenceGEMState *s, unsigned reg_num, uint16_t val)
1369{
1370 DB_PRINT("reg: %d value: 0x%04x\n", reg_num, val);
1371
1372 assert(!s->mdio);
1373
1374 switch (reg_num) {
1375 case PHY_REG_CONTROL:
1376 if (val & PHY_REG_CONTROL_RST) {
1377
1378 gem_phy_reset(s);
1379 val &= ~(PHY_REG_CONTROL_RST | PHY_REG_CONTROL_LOOP);
1380 s->phy_loop = 0;
1381 }
1382 if (val & PHY_REG_CONTROL_ANEG) {
1383
1384 val &= ~(PHY_REG_CONTROL_ANEG | PHY_REG_CONTROL_ANRESTART);
1385 s->phy_regs[PHY_REG_STATUS] |= PHY_REG_STATUS_ANEGCMPL;
1386 }
1387 if (val & PHY_REG_CONTROL_LOOP) {
1388 DB_PRINT("PHY placed in loopback\n");
1389 s->phy_loop = 1;
1390 } else {
1391 s->phy_loop = 0;
1392 }
1393 break;
1394 }
1395 s->phy_regs[reg_num] = val;
1396}
1397
1398static void gem_phy_loopback_setup(CadenceGEMState *s, unsigned reg_num,
1399 uint16_t val)
1400{
1401 assert(s->mdio);
1402
1403 switch (reg_num) {
1404 case PHY_REG_CONTROL:
1405 if (val & PHY_REG_CONTROL_RST) {
1406
1407 s->phy_loop = 0;
1408 }
1409 if (val & PHY_REG_CONTROL_LOOP) {
1410 DB_PRINT("PHY placed in loopback\n");
1411 s->phy_loop = 1;
1412 } else {
1413 s->phy_loop = 0;
1414 }
1415 break;
1416 }
1417}
1418
1419
1420
1421
1422
1423static uint64_t gem_read(void *opaque, hwaddr offset, unsigned size)
1424{
1425 CadenceGEMState *s;
1426 uint32_t retval;
1427 s = (CadenceGEMState *)opaque;
1428
1429 offset >>= 2;
1430 retval = s->regs[offset];
1431
1432 DB_PRINT("offset: 0x%04x read: 0x%08x\n", (unsigned)offset*4, retval);
1433
1434 switch (offset) {
1435 case GEM_ISR:
1436 DB_PRINT("lowering irqs on ISR read\n");
1437
1438 break;
1439 case GEM_PHYMNTNC:
1440 if (retval & GEM_PHYMNTNC_OP_R) {
1441 uint32_t phy_addr, reg_num;
1442
1443 phy_addr = (retval & GEM_PHYMNTNC_ADDR) >> GEM_PHYMNTNC_ADDR_SHFT;
1444 if (s->mdio) {
1445 reg_num = (retval & GEM_PHYMNTNC_REG) >> GEM_PHYMNTNC_REG_SHIFT;
1446 retval &= 0xFFFF0000;
1447 retval |= s->mdio->read(s->mdio, phy_addr, reg_num);
1448 } else if (phy_addr == BOARD_PHY_ADDRESS || phy_addr == 0) {
1449 reg_num = (retval & GEM_PHYMNTNC_REG) >> GEM_PHYMNTNC_REG_SHIFT;
1450 retval &= 0xFFFF0000;
1451 retval |= gem_phy_read(s, reg_num);
1452 } else {
1453 retval |= 0xFFFF;
1454 }
1455 }
1456 break;
1457 }
1458
1459
1460 s->regs[offset] &= ~(s->regs_rtc[offset]);
1461
1462
1463 retval &= ~(s->regs_wo[offset]);
1464
1465 DB_PRINT("0x%08x\n", retval);
1466 gem_update_int_status(s);
1467 return retval;
1468}
1469
1470
1471
1472
1473
1474static void gem_write(void *opaque, hwaddr offset, uint64_t val,
1475 unsigned size)
1476{
1477 CadenceGEMState *s = (CadenceGEMState *)opaque;
1478 uint32_t readonly;
1479 int i;
1480
1481 DB_PRINT("offset: 0x%04x write: 0x%08x ", (unsigned)offset, (unsigned)val);
1482 offset >>= 2;
1483
1484
1485 val &= ~(s->regs_ro[offset]);
1486
1487 readonly = s->regs[offset] & (s->regs_ro[offset] | s->regs_w1c[offset]);
1488
1489
1490 s->regs[offset] = (val & ~s->regs_w1c[offset]) | readonly;
1491
1492
1493 s->regs[offset] &= ~(s->regs_w1c[offset] & val);
1494
1495
1496 switch (offset) {
1497 case GEM_NWCTRL:
1498 if (val & GEM_NWCTRL_RXENA) {
1499 for (i = 0; i < s->num_priority_queues; ++i) {
1500 gem_get_rx_desc(s, i);
1501 }
1502 }
1503 if (val & GEM_NWCTRL_TXSTART) {
1504 gem_transmit(s);
1505 }
1506 if (!(val & GEM_NWCTRL_TXENA)) {
1507
1508 for (i = 0; i < s->num_priority_queues; i++) {
1509 s->tx_desc_addr[i] = gem_get_queue_base_addr(s, true, i);
1510 }
1511 }
1512 if (gem_can_receive(qemu_get_queue(s->nic))) {
1513 qemu_flush_queued_packets(qemu_get_queue(s->nic));
1514 }
1515 break;
1516
1517 case GEM_TXSTATUS:
1518 gem_update_int_status(s);
1519 break;
1520 case GEM_RXQBASE:
1521 s->rx_desc_addr[0] = val;
1522 break;
1523 case GEM_RECEIVE_Q1_PTR ... GEM_RECEIVE_Q7_PTR:
1524 s->rx_desc_addr[offset - GEM_RECEIVE_Q1_PTR + 1] = val;
1525 break;
1526 case GEM_TXQBASE:
1527 s->tx_desc_addr[0] = val;
1528 break;
1529 case GEM_TRANSMIT_Q1_PTR ... GEM_TRANSMIT_Q7_PTR:
1530 s->tx_desc_addr[offset - GEM_TRANSMIT_Q1_PTR + 1] = val;
1531 break;
1532 case GEM_RXSTATUS:
1533 gem_update_int_status(s);
1534 break;
1535 case GEM_IER:
1536 s->regs[GEM_IMR] &= ~val;
1537 gem_update_int_status(s);
1538 break;
1539 case GEM_INT_Q1_ENABLE ... GEM_INT_Q7_ENABLE:
1540 s->regs[GEM_INT_Q1_MASK + offset - GEM_INT_Q1_ENABLE] &= ~val;
1541 gem_update_int_status(s);
1542 break;
1543 case GEM_IDR:
1544 s->regs[GEM_IMR] |= val;
1545 gem_update_int_status(s);
1546 break;
1547 case GEM_INT_Q1_DISABLE ... GEM_INT_Q7_DISABLE:
1548 s->regs[GEM_INT_Q1_MASK + offset - GEM_INT_Q1_DISABLE] |= val;
1549 gem_update_int_status(s);
1550 break;
1551 case GEM_SPADDR1LO:
1552 case GEM_SPADDR2LO:
1553 case GEM_SPADDR3LO:
1554 case GEM_SPADDR4LO:
1555 s->sar_active[(offset - GEM_SPADDR1LO) / 2] = false;
1556 break;
1557 case GEM_SPADDR1HI:
1558 case GEM_SPADDR2HI:
1559 case GEM_SPADDR3HI:
1560 case GEM_SPADDR4HI:
1561 s->sar_active[(offset - GEM_SPADDR1HI) / 2] = true;
1562 break;
1563 case GEM_PHYMNTNC:
1564 if (val & GEM_PHYMNTNC_OP_W) {
1565 uint32_t phy_addr, reg_num;
1566
1567 phy_addr = (val & GEM_PHYMNTNC_ADDR) >> GEM_PHYMNTNC_ADDR_SHFT;
1568 if (s->mdio) {
1569 reg_num = (val & GEM_PHYMNTNC_REG) >> GEM_PHYMNTNC_REG_SHIFT;
1570 gem_phy_loopback_setup(s, reg_num, val);
1571 s->mdio->write(s->mdio, phy_addr, reg_num, val);
1572 } else if (phy_addr == BOARD_PHY_ADDRESS || phy_addr == 0) {
1573 reg_num = (val & GEM_PHYMNTNC_REG) >> GEM_PHYMNTNC_REG_SHIFT;
1574 gem_phy_write(s, reg_num, val);
1575 }
1576 }
1577 break;
1578 }
1579
1580 DB_PRINT("newval: 0x%08x\n", s->regs[offset]);
1581}
1582
1583static const MemoryRegionOps gem_ops = {
1584 .read = gem_read,
1585 .write = gem_write,
1586 .endianness = DEVICE_LITTLE_ENDIAN,
1587};
1588
1589static void gem_set_link(NetClientState *nc)
1590{
1591 CadenceGEMState *s = qemu_get_nic_opaque(nc);
1592
1593 DB_PRINT("\n");
1594 phy_update_link(s);
1595 gem_update_int_status(s);
1596}
1597
1598static NetClientInfo net_gem_info = {
1599 .type = NET_CLIENT_DRIVER_NIC,
1600 .size = sizeof(NICState),
1601 .can_receive = gem_can_receive,
1602 .receive = gem_receive,
1603 .link_status_changed = gem_set_link,
1604};
1605
1606static void gem_realize(DeviceState *dev, Error **errp)
1607{
1608 CadenceGEMState *s = CADENCE_GEM(dev);
1609 int i;
1610
1611 address_space_init(&s->dma_as,
1612 s->dma_mr ? s->dma_mr : get_system_memory(), "dma");
1613
1614 if (s->num_priority_queues == 0 ||
1615 s->num_priority_queues > MAX_PRIORITY_QUEUES) {
1616 error_setg(errp, "Invalid num-priority-queues value: %" PRIx8,
1617 s->num_priority_queues);
1618 return;
1619 } else if (s->num_type1_screeners > MAX_TYPE1_SCREENERS) {
1620 error_setg(errp, "Invalid num-type1-screeners value: %" PRIx8,
1621 s->num_type1_screeners);
1622 return;
1623 } else if (s->num_type2_screeners > MAX_TYPE2_SCREENERS) {
1624 error_setg(errp, "Invalid num-type2-screeners value: %" PRIx8,
1625 s->num_type2_screeners);
1626 return;
1627 }
1628
1629 for (i = 0; i < s->num_priority_queues; ++i) {
1630 sysbus_init_irq(SYS_BUS_DEVICE(dev), &s->irq[i]);
1631 }
1632
1633 if (!s->attr) {
1634 s->attr = MEMORY_TRANSACTION_ATTR(
1635 object_new(TYPE_MEMORY_TRANSACTION_ATTR));
1636 }
1637
1638 qemu_macaddr_default_if_unset(&s->conf.macaddr);
1639
1640 s->nic = qemu_new_nic(&net_gem_info, &s->conf,
1641 object_get_typename(OBJECT(dev)), dev->id, s);
1642}
1643
1644static void gem_init(Object *obj)
1645{
1646 CadenceGEMState *s = CADENCE_GEM(obj);
1647 DeviceState *dev = DEVICE(obj);
1648
1649 DB_PRINT("\n");
1650
1651 gem_init_register_masks(s);
1652 memory_region_init_io(&s->iomem, OBJECT(s), &gem_ops, s,
1653 "enet", sizeof(s->regs));
1654
1655 sysbus_init_mmio(SYS_BUS_DEVICE(dev), &s->iomem);
1656
1657 object_property_add_link(obj, "dma", TYPE_MEMORY_REGION,
1658 (Object **)&s->dma_mr,
1659 qdev_prop_allow_set_link_before_realize,
1660 OBJ_PROP_LINK_STRONG,
1661 &error_abort);
1662 object_property_add_link(obj, "memattr", TYPE_MEMORY_TRANSACTION_ATTR,
1663 (Object **)&s->attr,
1664 qdev_prop_allow_set_link_before_realize,
1665 OBJ_PROP_LINK_STRONG,
1666 &error_abort);
1667 object_property_add_link(obj, "mdio", TYPE_MDIO, (Object **)&s->mdio,
1668 qdev_prop_allow_set_link,
1669 OBJ_PROP_LINK_STRONG,
1670 &error_abort);
1671}
1672
1673static const VMStateDescription vmstate_cadence_gem = {
1674 .name = "cadence_gem",
1675 .version_id = 4,
1676 .minimum_version_id = 4,
1677 .fields = (VMStateField[]) {
1678 VMSTATE_UINT32_ARRAY(regs, CadenceGEMState, CADENCE_GEM_MAXREG),
1679 VMSTATE_UINT8(phy_loop, CadenceGEMState),
1680 VMSTATE_UINT32_ARRAY(rx_desc_addr, CadenceGEMState,
1681 MAX_PRIORITY_QUEUES),
1682 VMSTATE_UINT32_ARRAY(tx_desc_addr, CadenceGEMState,
1683 MAX_PRIORITY_QUEUES),
1684 VMSTATE_BOOL_ARRAY(sar_active, CadenceGEMState, 4),
1685 VMSTATE_END_OF_LIST(),
1686 }
1687};
1688
1689static Property gem_properties[] = {
1690 DEFINE_NIC_PROPERTIES(CadenceGEMState, conf),
1691 DEFINE_PROP_UINT32("revision", CadenceGEMState, revision,
1692 GEM_MODID_VALUE),
1693 DEFINE_PROP_UINT8("num-priority-queues", CadenceGEMState,
1694 num_priority_queues, 1),
1695 DEFINE_PROP_UINT8("num-type1-screeners", CadenceGEMState,
1696 num_type1_screeners, 4),
1697 DEFINE_PROP_UINT8("num-type2-screeners", CadenceGEMState,
1698 num_type2_screeners, 4),
1699 DEFINE_PROP_END_OF_LIST(),
1700};
1701
1702static void gem_class_init(ObjectClass *klass, void *data)
1703{
1704 DeviceClass *dc = DEVICE_CLASS(klass);
1705
1706 dc->realize = gem_realize;
1707 dc->props = gem_properties;
1708 dc->vmsd = &vmstate_cadence_gem;
1709 dc->reset = gem_reset;
1710}
1711
1712static const TypeInfo gem_info = {
1713 .name = TYPE_CADENCE_GEM,
1714 .parent = TYPE_SYS_BUS_DEVICE,
1715 .instance_size = sizeof(CadenceGEMState),
1716 .instance_init = gem_init,
1717 .class_init = gem_class_init,
1718};
1719
1720static void gem_register_types(void)
1721{
1722 type_register_static(&gem_info);
1723}
1724
1725type_init(gem_register_types)
1726