]> Git Repo - qemu.git/blob - hw/net/cadence_gem.c
net/cadence_gem: Prefetch rx descriptors ASAP
[qemu.git] / hw / net / cadence_gem.c
1 /*
2  * QEMU Xilinx GEM emulation
3  *
4  * Copyright (c) 2011 Xilinx, Inc.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  */
24
25 #include <zlib.h> /* For crc32 */
26
27 #include "hw/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) /* Network Control reg */
41 #define GEM_NWCFG         (0x00000004/4) /* Network Config reg */
42 #define GEM_NWSTATUS      (0x00000008/4) /* Network Status reg */
43 #define GEM_USERIO        (0x0000000C/4) /* User IO reg */
44 #define GEM_DMACFG        (0x00000010/4) /* DMA Control reg */
45 #define GEM_TXSTATUS      (0x00000014/4) /* TX Status reg */
46 #define GEM_RXQBASE       (0x00000018/4) /* RX Q Base address reg */
47 #define GEM_TXQBASE       (0x0000001C/4) /* TX Q Base address reg */
48 #define GEM_RXSTATUS      (0x00000020/4) /* RX Status reg */
49 #define GEM_ISR           (0x00000024/4) /* Interrupt Status reg */
50 #define GEM_IER           (0x00000028/4) /* Interrupt Enable reg */
51 #define GEM_IDR           (0x0000002C/4) /* Interrupt Disable reg */
52 #define GEM_IMR           (0x00000030/4) /* Interrupt Mask reg */
53 #define GEM_PHYMNTNC      (0x00000034/4) /* Phy Maintaince reg */
54 #define GEM_RXPAUSE       (0x00000038/4) /* RX Pause Time reg */
55 #define GEM_TXPAUSE       (0x0000003C/4) /* TX Pause Time reg */
56 #define GEM_TXPARTIALSF   (0x00000040/4) /* TX Partial Store and Forward */
57 #define GEM_RXPARTIALSF   (0x00000044/4) /* RX Partial Store and Forward */
58 #define GEM_HASHLO        (0x00000080/4) /* Hash Low address reg */
59 #define GEM_HASHHI        (0x00000084/4) /* Hash High address reg */
60 #define GEM_SPADDR1LO     (0x00000088/4) /* Specific addr 1 low reg */
61 #define GEM_SPADDR1HI     (0x0000008C/4) /* Specific addr 1 high reg */
62 #define GEM_SPADDR2LO     (0x00000090/4) /* Specific addr 2 low reg */
63 #define GEM_SPADDR2HI     (0x00000094/4) /* Specific addr 2 high reg */
64 #define GEM_SPADDR3LO     (0x00000098/4) /* Specific addr 3 low reg */
65 #define GEM_SPADDR3HI     (0x0000009C/4) /* Specific addr 3 high reg */
66 #define GEM_SPADDR4LO     (0x000000A0/4) /* Specific addr 4 low reg */
67 #define GEM_SPADDR4HI     (0x000000A4/4) /* Specific addr 4 high reg */
68 #define GEM_TIDMATCH1     (0x000000A8/4) /* Type ID1 Match reg */
69 #define GEM_TIDMATCH2     (0x000000AC/4) /* Type ID2 Match reg */
70 #define GEM_TIDMATCH3     (0x000000B0/4) /* Type ID3 Match reg */
71 #define GEM_TIDMATCH4     (0x000000B4/4) /* Type ID4 Match reg */
72 #define GEM_WOLAN         (0x000000B8/4) /* Wake on LAN reg */
73 #define GEM_IPGSTRETCH    (0x000000BC/4) /* IPG Stretch reg */
74 #define GEM_SVLAN         (0x000000C0/4) /* Stacked VLAN reg */
75 #define GEM_MODID         (0x000000FC/4) /* Module ID reg */
76 #define GEM_OCTTXLO       (0x00000100/4) /* Octects transmitted Low reg */
77 #define GEM_OCTTXHI       (0x00000104/4) /* Octects transmitted High reg */
78 #define GEM_TXCNT         (0x00000108/4) /* Error-free Frames transmitted */
79 #define GEM_TXBCNT        (0x0000010C/4) /* Error-free Broadcast Frames */
80 #define GEM_TXMCNT        (0x00000110/4) /* Error-free Multicast Frame */
81 #define GEM_TXPAUSECNT    (0x00000114/4) /* Pause Frames Transmitted */
82 #define GEM_TX64CNT       (0x00000118/4) /* Error-free 64 TX */
83 #define GEM_TX65CNT       (0x0000011C/4) /* Error-free 65-127 TX */
84 #define GEM_TX128CNT      (0x00000120/4) /* Error-free 128-255 TX */
85 #define GEM_TX256CNT      (0x00000124/4) /* Error-free 256-511 */
86 #define GEM_TX512CNT      (0x00000128/4) /* Error-free 512-1023 TX */
87 #define GEM_TX1024CNT     (0x0000012C/4) /* Error-free 1024-1518 TX */
88 #define GEM_TX1519CNT     (0x00000130/4) /* Error-free larger than 1519 TX */
89 #define GEM_TXURUNCNT     (0x00000134/4) /* TX under run error counter */
90 #define GEM_SINGLECOLLCNT (0x00000138/4) /* Single Collision Frames */
91 #define GEM_MULTCOLLCNT   (0x0000013C/4) /* Multiple Collision Frames */
92 #define GEM_EXCESSCOLLCNT (0x00000140/4) /* Excessive Collision Frames */
93 #define GEM_LATECOLLCNT   (0x00000144/4) /* Late Collision Frames */
94 #define GEM_DEFERTXCNT    (0x00000148/4) /* Deferred Transmission Frames */
95 #define GEM_CSENSECNT     (0x0000014C/4) /* Carrier Sense Error Counter */
96 #define GEM_OCTRXLO       (0x00000150/4) /* Octects Received register Low */
97 #define GEM_OCTRXHI       (0x00000154/4) /* Octects Received register High */
98 #define GEM_RXCNT         (0x00000158/4) /* Error-free Frames Received */
99 #define GEM_RXBROADCNT    (0x0000015C/4) /* Error-free Broadcast Frames RX */
100 #define GEM_RXMULTICNT    (0x00000160/4) /* Error-free Multicast Frames RX */
101 #define GEM_RXPAUSECNT    (0x00000164/4) /* Pause Frames Received Counter */
102 #define GEM_RX64CNT       (0x00000168/4) /* Error-free 64 byte Frames RX */
103 #define GEM_RX65CNT       (0x0000016C/4) /* Error-free 65-127B Frames RX */
104 #define GEM_RX128CNT      (0x00000170/4) /* Error-free 128-255B Frames RX */
105 #define GEM_RX256CNT      (0x00000174/4) /* Error-free 256-512B Frames RX */
106 #define GEM_RX512CNT      (0x00000178/4) /* Error-free 512-1023B Frames RX */
107 #define GEM_RX1024CNT     (0x0000017C/4) /* Error-free 1024-1518B Frames RX */
108 #define GEM_RX1519CNT     (0x00000180/4) /* Error-free 1519-max Frames RX */
109 #define GEM_RXUNDERCNT    (0x00000184/4) /* Undersize Frames Received */
110 #define GEM_RXOVERCNT     (0x00000188/4) /* Oversize Frames Received */
111 #define GEM_RXJABCNT      (0x0000018C/4) /* Jabbers Received Counter */
112 #define GEM_RXFCSCNT      (0x00000190/4) /* Frame Check seq. Error Counter */
113 #define GEM_RXLENERRCNT   (0x00000194/4) /* Length Field Error Counter */
114 #define GEM_RXSYMERRCNT   (0x00000198/4) /* Symbol Error Counter */
115 #define GEM_RXALIGNERRCNT (0x0000019C/4) /* Alignment Error Counter */
116 #define GEM_RXRSCERRCNT   (0x000001A0/4) /* Receive Resource Error Counter */
117 #define GEM_RXORUNCNT     (0x000001A4/4) /* Receive Overrun Counter */
118 #define GEM_RXIPCSERRCNT  (0x000001A8/4) /* IP header Checksum Error Counter */
119 #define GEM_RXTCPCCNT     (0x000001AC/4) /* TCP Checksum Error Counter */
120 #define GEM_RXUDPCCNT     (0x000001B0/4) /* UDP Checksum Error Counter */
121
122 #define GEM_1588S         (0x000001D0/4) /* 1588 Timer Seconds */
123 #define GEM_1588NS        (0x000001D4/4) /* 1588 Timer Nanoseconds */
124 #define GEM_1588ADJ       (0x000001D8/4) /* 1588 Timer Adjust */
125 #define GEM_1588INC       (0x000001DC/4) /* 1588 Timer Increment */
126 #define GEM_PTPETXS       (0x000001E0/4) /* PTP Event Frame Transmitted (s) */
127 #define GEM_PTPETXNS      (0x000001E4/4) /* PTP Event Frame Transmitted (ns) */
128 #define GEM_PTPERXS       (0x000001E8/4) /* PTP Event Frame Received (s) */
129 #define GEM_PTPERXNS      (0x000001EC/4) /* PTP Event Frame Received (ns) */
130 #define GEM_PTPPTXS       (0x000001E0/4) /* PTP Peer Frame Transmitted (s) */
131 #define GEM_PTPPTXNS      (0x000001E4/4) /* PTP Peer Frame Transmitted (ns) */
132 #define GEM_PTPPRXS       (0x000001E8/4) /* PTP Peer Frame Received (s) */
133 #define GEM_PTPPRXNS      (0x000001EC/4) /* PTP Peer Frame Received (ns) */
134
135 /* Design Configuration Registers */
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) /* Last valid GEM address */
145
146 /*****************************************/
147 #define GEM_NWCTRL_TXSTART     0x00000200 /* Transmit Enable */
148 #define GEM_NWCTRL_TXENA       0x00000008 /* Transmit Enable */
149 #define GEM_NWCTRL_RXENA       0x00000004 /* Receive Enable */
150 #define GEM_NWCTRL_LOCALLOOP   0x00000002 /* Local Loopback */
151
152 #define GEM_NWCFG_STRIP_FCS    0x00020000 /* Strip FCS field */
153 #define GEM_NWCFG_LERR_DISC    0x00010000 /* Discard RX frames with lenth err */
154 #define GEM_NWCFG_BUFF_OFST_M  0x0000C000 /* Receive buffer offset mask */
155 #define GEM_NWCFG_BUFF_OFST_S  14         /* Receive buffer offset shift */
156 #define GEM_NWCFG_UCAST_HASH   0x00000080 /* accept unicast if hash match */
157 #define GEM_NWCFG_MCAST_HASH   0x00000040 /* accept multicast if hash match */
158 #define GEM_NWCFG_BCAST_REJ    0x00000020 /* Reject broadcast packets */
159 #define GEM_NWCFG_PROMISC      0x00000010 /* Accept all packets */
160
161 #define GEM_DMACFG_RBUFSZ_M    0x007F0000 /* DMA RX Buffer Size mask */
162 #define GEM_DMACFG_RBUFSZ_S    16         /* DMA RX Buffer Size shift */
163 #define GEM_DMACFG_RBUFSZ_MUL  64         /* DMA RX Buffer Size multiplier */
164 #define GEM_DMACFG_TXCSUM_OFFL 0x00000800 /* Transmit checksum offload */
165
166 #define GEM_TXSTATUS_TXCMPL    0x00000020 /* Transmit Complete */
167 #define GEM_TXSTATUS_USED      0x00000001 /* sw owned descriptor encountered */
168
169 #define GEM_RXSTATUS_FRMRCVD   0x00000002 /* Frame received */
170 #define GEM_RXSTATUS_NOBUF     0x00000001 /* Buffer unavailable */
171
172 /* GEM_ISR GEM_IER GEM_IDR GEM_IMR */
173 #define GEM_INT_TXCMPL        0x00000080 /* Transmit Complete */
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 /* read operation */
179 #define GEM_PHYMNTNC_OP_W      0x10000000 /* write operation */
180 #define GEM_PHYMNTNC_ADDR      0x0F800000 /* Address bits */
181 #define GEM_PHYMNTNC_ADDR_SHFT 23
182 #define GEM_PHYMNTNC_REG       0x007C0000 /* register bits */
183 #define GEM_PHYMNTNC_REG_SHIFT 18
184
185 /* Marvell PHY definitions */
186 #define BOARD_PHY_ADDRESS    23 /* PHY address we will emulate a device at */
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
242 static inline unsigned tx_desc_get_buffer(unsigned *desc)
243 {
244     return desc[0];
245 }
246
247 static inline unsigned tx_desc_get_used(unsigned *desc)
248 {
249     return (desc[1] & DESC_1_USED) ? 1 : 0;
250 }
251
252 static inline void tx_desc_set_used(unsigned *desc)
253 {
254     desc[1] |= DESC_1_USED;
255 }
256
257 static inline unsigned tx_desc_get_wrap(unsigned *desc)
258 {
259     return (desc[1] & DESC_1_TX_WRAP) ? 1 : 0;
260 }
261
262 static inline unsigned tx_desc_get_last(unsigned *desc)
263 {
264     return (desc[1] & DESC_1_TX_LAST) ? 1 : 0;
265 }
266
267 static inline unsigned tx_desc_get_length(unsigned *desc)
268 {
269     return desc[1] & DESC_1_LENGTH;
270 }
271
272 static 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
282 static inline unsigned rx_desc_get_buffer(unsigned *desc)
283 {
284     return desc[0] & ~0x3UL;
285 }
286
287 static inline unsigned rx_desc_get_wrap(unsigned *desc)
288 {
289     return desc[0] & DESC_0_RX_WRAP ? 1 : 0;
290 }
291
292 static inline unsigned rx_desc_get_ownership(unsigned *desc)
293 {
294     return desc[0] & DESC_0_RX_OWNERSHIP ? 1 : 0;
295 }
296
297 static inline void rx_desc_set_ownership(unsigned *desc)
298 {
299     desc[0] |= DESC_0_RX_OWNERSHIP;
300 }
301
302 static inline void rx_desc_set_sof(unsigned *desc)
303 {
304     desc[1] |= DESC_1_RX_SOF;
305 }
306
307 static inline void rx_desc_set_eof(unsigned *desc)
308 {
309     desc[1] |= DESC_1_RX_EOF;
310 }
311
312 static inline void rx_desc_set_length(unsigned *desc, unsigned len)
313 {
314     desc[1] &= ~DESC_1_LENGTH;
315     desc[1] |= len;
316 }
317
318 #define TYPE_CADENCE_GEM "cadence_gem"
319 #define GEM(obj) OBJECT_CHECK(GemState, (obj), TYPE_CADENCE_GEM)
320
321 typedef struct GemState {
322     SysBusDevice parent_obj;
323
324     MemoryRegion iomem;
325     NICState *nic;
326     NICConf conf;
327     qemu_irq irq;
328
329     /* GEM registers backing store */
330     uint32_t regs[GEM_MAXREG];
331     /* Mask of register bits which are write only */
332     uint32_t regs_wo[GEM_MAXREG];
333     /* Mask of register bits which are read only */
334     uint32_t regs_ro[GEM_MAXREG];
335     /* Mask of register bits which are clear on read */
336     uint32_t regs_rtc[GEM_MAXREG];
337     /* Mask of register bits which are write 1 to clear */
338     uint32_t regs_w1c[GEM_MAXREG];
339
340     /* PHY registers backing store */
341     uint16_t phy_regs[32];
342
343     uint8_t phy_loop; /* Are we in phy loopback? */
344
345     /* The current DMA descriptor pointers */
346     uint32_t rx_desc_addr;
347     uint32_t tx_desc_addr;
348
349     unsigned rx_desc[2];
350
351 } GemState;
352
353 /* The broadcast MAC address: 0xFFFFFFFFFFFF */
354 const uint8_t broadcast_addr[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
355
356 /*
357  * gem_init_register_masks:
358  * One time initialization.
359  * Set masks to identify which register bits have magical clear properties
360  */
361 static void gem_init_register_masks(GemState *s)
362 {
363     /* Mask of register bits which are read only*/
364     memset(&s->regs_ro[0], 0, sizeof(s->regs_ro));
365     s->regs_ro[GEM_NWCTRL]   = 0xFFF80000;
366     s->regs_ro[GEM_NWSTATUS] = 0xFFFFFFFF;
367     s->regs_ro[GEM_DMACFG]   = 0xFE00F000;
368     s->regs_ro[GEM_TXSTATUS] = 0xFFFFFE08;
369     s->regs_ro[GEM_RXQBASE]  = 0x00000003;
370     s->regs_ro[GEM_TXQBASE]  = 0x00000003;
371     s->regs_ro[GEM_RXSTATUS] = 0xFFFFFFF0;
372     s->regs_ro[GEM_ISR]      = 0xFFFFFFFF;
373     s->regs_ro[GEM_IMR]      = 0xFFFFFFFF;
374     s->regs_ro[GEM_MODID]    = 0xFFFFFFFF;
375
376     /* Mask of register bits which are clear on read */
377     memset(&s->regs_rtc[0], 0, sizeof(s->regs_rtc));
378     s->regs_rtc[GEM_ISR]      = 0xFFFFFFFF;
379
380     /* Mask of register bits which are write 1 to clear */
381     memset(&s->regs_w1c[0], 0, sizeof(s->regs_w1c));
382     s->regs_w1c[GEM_TXSTATUS] = 0x000001F7;
383     s->regs_w1c[GEM_RXSTATUS] = 0x0000000F;
384
385     /* Mask of register bits which are write only */
386     memset(&s->regs_wo[0], 0, sizeof(s->regs_wo));
387     s->regs_wo[GEM_NWCTRL]   = 0x00073E60;
388     s->regs_wo[GEM_IER]      = 0x07FFFFFF;
389     s->regs_wo[GEM_IDR]      = 0x07FFFFFF;
390 }
391
392 /*
393  * phy_update_link:
394  * Make the emulated PHY link state match the QEMU "interface" state.
395  */
396 static void phy_update_link(GemState *s)
397 {
398     DB_PRINT("down %d\n", qemu_get_queue(s->nic)->link_down);
399
400     /* Autonegotiation status mirrors link status.  */
401     if (qemu_get_queue(s->nic)->link_down) {
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     } else {
406         s->phy_regs[PHY_REG_STATUS] |= (PHY_REG_STATUS_ANEGCMPL |
407                                          PHY_REG_STATUS_LINK);
408         s->phy_regs[PHY_REG_INT_ST] |= (PHY_REG_INT_ST_LINKC |
409                                         PHY_REG_INT_ST_ANEGCMPL |
410                                         PHY_REG_INT_ST_ENERGY);
411     }
412 }
413
414 static int gem_can_receive(NetClientState *nc)
415 {
416     GemState *s;
417
418     s = qemu_get_nic_opaque(nc);
419
420     DB_PRINT("\n");
421
422     /* Do nothing if receive is not enabled. */
423     if (!(s->regs[GEM_NWCTRL] & GEM_NWCTRL_RXENA)) {
424         return 0;
425     }
426
427     return 1;
428 }
429
430 /*
431  * gem_update_int_status:
432  * Raise or lower interrupt based on current status.
433  */
434 static void gem_update_int_status(GemState *s)
435 {
436     if (s->regs[GEM_ISR]) {
437         DB_PRINT("asserting int. (0x%08x)\n", s->regs[GEM_ISR]);
438         qemu_set_irq(s->irq, 1);
439     }
440 }
441
442 /*
443  * gem_receive_updatestats:
444  * Increment receive statistics.
445  */
446 static void gem_receive_updatestats(GemState *s, const uint8_t *packet,
447                                     unsigned bytes)
448 {
449     uint64_t octets;
450
451     /* Total octets (bytes) received */
452     octets = ((uint64_t)(s->regs[GEM_OCTRXLO]) << 32) |
453              s->regs[GEM_OCTRXHI];
454     octets += bytes;
455     s->regs[GEM_OCTRXLO] = octets >> 32;
456     s->regs[GEM_OCTRXHI] = octets;
457
458     /* Error-free Frames received */
459     s->regs[GEM_RXCNT]++;
460
461     /* Error-free Broadcast Frames counter */
462     if (!memcmp(packet, broadcast_addr, 6)) {
463         s->regs[GEM_RXBROADCNT]++;
464     }
465
466     /* Error-free Multicast Frames counter */
467     if (packet[0] == 0x01) {
468         s->regs[GEM_RXMULTICNT]++;
469     }
470
471     if (bytes <= 64) {
472         s->regs[GEM_RX64CNT]++;
473     } else if (bytes <= 127) {
474         s->regs[GEM_RX65CNT]++;
475     } else if (bytes <= 255) {
476         s->regs[GEM_RX128CNT]++;
477     } else if (bytes <= 511) {
478         s->regs[GEM_RX256CNT]++;
479     } else if (bytes <= 1023) {
480         s->regs[GEM_RX512CNT]++;
481     } else if (bytes <= 1518) {
482         s->regs[GEM_RX1024CNT]++;
483     } else {
484         s->regs[GEM_RX1519CNT]++;
485     }
486 }
487
488 /*
489  * Get the MAC Address bit from the specified position
490  */
491 static unsigned get_bit(const uint8_t *mac, unsigned bit)
492 {
493     unsigned byte;
494
495     byte = mac[bit / 8];
496     byte >>= (bit & 0x7);
497     byte &= 1;
498
499     return byte;
500 }
501
502 /*
503  * Calculate a GEM MAC Address hash index
504  */
505 static unsigned calc_mac_hash(const uint8_t *mac)
506 {
507     int index_bit, mac_bit;
508     unsigned hash_index;
509
510     hash_index = 0;
511     mac_bit = 5;
512     for (index_bit = 5; index_bit >= 0; index_bit--) {
513         hash_index |= (get_bit(mac,  mac_bit) ^
514                                get_bit(mac, mac_bit + 6) ^
515                                get_bit(mac, mac_bit + 12) ^
516                                get_bit(mac, mac_bit + 18) ^
517                                get_bit(mac, mac_bit + 24) ^
518                                get_bit(mac, mac_bit + 30) ^
519                                get_bit(mac, mac_bit + 36) ^
520                                get_bit(mac, mac_bit + 42)) << index_bit;
521         mac_bit--;
522     }
523
524     return hash_index;
525 }
526
527 /*
528  * gem_mac_address_filter:
529  * Accept or reject this destination address?
530  * Returns:
531  * GEM_RX_REJECT: reject
532  * GEM_RX_ACCEPT: accept
533  */
534 static int gem_mac_address_filter(GemState *s, const uint8_t *packet)
535 {
536     uint8_t *gem_spaddr;
537     int i;
538
539     /* Promiscuous mode? */
540     if (s->regs[GEM_NWCFG] & GEM_NWCFG_PROMISC) {
541         return GEM_RX_ACCEPT;
542     }
543
544     if (!memcmp(packet, broadcast_addr, 6)) {
545         /* Reject broadcast packets? */
546         if (s->regs[GEM_NWCFG] & GEM_NWCFG_BCAST_REJ) {
547             return GEM_RX_REJECT;
548         }
549         return GEM_RX_ACCEPT;
550     }
551
552     /* Accept packets -w- hash match? */
553     if ((packet[0] == 0x01 && (s->regs[GEM_NWCFG] & GEM_NWCFG_MCAST_HASH)) ||
554         (packet[0] != 0x01 && (s->regs[GEM_NWCFG] & GEM_NWCFG_UCAST_HASH))) {
555         unsigned hash_index;
556
557         hash_index = calc_mac_hash(packet);
558         if (hash_index < 32) {
559             if (s->regs[GEM_HASHLO] & (1<<hash_index)) {
560                 return GEM_RX_ACCEPT;
561             }
562         } else {
563             hash_index -= 32;
564             if (s->regs[GEM_HASHHI] & (1<<hash_index)) {
565                 return GEM_RX_ACCEPT;
566             }
567         }
568     }
569
570     /* Check all 4 specific addresses */
571     gem_spaddr = (uint8_t *)&(s->regs[GEM_SPADDR1LO]);
572     for (i = 0; i < 4; i++) {
573         if (!memcmp(packet, gem_spaddr, 6)) {
574             return GEM_RX_ACCEPT;
575         }
576
577         gem_spaddr += 8;
578     }
579
580     /* No address match; reject the packet */
581     return GEM_RX_REJECT;
582 }
583
584 static void gem_get_rx_desc(GemState *s)
585 {
586     DB_PRINT("read descriptor 0x%x\n", (unsigned)s->rx_desc_addr);
587     /* read current descriptor */
588     cpu_physical_memory_read(s->rx_desc_addr,
589                              (uint8_t *)s->rx_desc, sizeof(s->rx_desc));
590
591     /* Descriptor owned by software ? */
592     if (rx_desc_get_ownership(s->rx_desc) == 1) {
593         DB_PRINT("descriptor 0x%x owned by sw.\n",
594                  (unsigned)s->rx_desc_addr);
595         s->regs[GEM_RXSTATUS] |= GEM_RXSTATUS_NOBUF;
596         s->regs[GEM_ISR] |= GEM_INT_RXUSED & ~(s->regs[GEM_IMR]);
597         /* Handle interrupt consequences */
598         gem_update_int_status(s);
599     }
600 }
601
602 /*
603  * gem_receive:
604  * Fit a packet handed to us by QEMU into the receive descriptor ring.
605  */
606 static ssize_t gem_receive(NetClientState *nc, const uint8_t *buf, size_t size)
607 {
608     GemState *s;
609     unsigned   rxbufsize, bytes_to_copy;
610     unsigned   rxbuf_offset;
611     uint8_t    rxbuf[2048];
612     uint8_t   *rxbuf_ptr;
613     bool first_desc = true;
614
615     s = qemu_get_nic_opaque(nc);
616
617     /* Is this destination MAC address "for us" ? */
618     if (gem_mac_address_filter(s, buf) == GEM_RX_REJECT) {
619         return -1;
620     }
621
622     /* Discard packets with receive length error enabled ? */
623     if (s->regs[GEM_NWCFG] & GEM_NWCFG_LERR_DISC) {
624         unsigned type_len;
625
626         /* Fish the ethertype / length field out of the RX packet */
627         type_len = buf[12] << 8 | buf[13];
628         /* It is a length field, not an ethertype */
629         if (type_len < 0x600) {
630             if (size < type_len) {
631                 /* discard */
632                 return -1;
633             }
634         }
635     }
636
637     /*
638      * Determine configured receive buffer offset (probably 0)
639      */
640     rxbuf_offset = (s->regs[GEM_NWCFG] & GEM_NWCFG_BUFF_OFST_M) >>
641                    GEM_NWCFG_BUFF_OFST_S;
642
643     /* The configure size of each receive buffer.  Determines how many
644      * buffers needed to hold this packet.
645      */
646     rxbufsize = ((s->regs[GEM_DMACFG] & GEM_DMACFG_RBUFSZ_M) >>
647                  GEM_DMACFG_RBUFSZ_S) * GEM_DMACFG_RBUFSZ_MUL;
648     bytes_to_copy = size;
649
650     /* Strip of FCS field ? (usually yes) */
651     if (s->regs[GEM_NWCFG] & GEM_NWCFG_STRIP_FCS) {
652         rxbuf_ptr = (void *)buf;
653     } else {
654         unsigned crc_val;
655         int      crc_offset;
656
657         /* The application wants the FCS field, which QEMU does not provide.
658          * We must try and caclculate one.
659          */
660
661         memcpy(rxbuf, buf, size);
662         memset(rxbuf + size, 0, sizeof(rxbuf) - size);
663         rxbuf_ptr = rxbuf;
664         crc_val = cpu_to_le32(crc32(0, rxbuf, MAX(size, 60)));
665         if (size < 60) {
666             crc_offset = 60;
667         } else {
668             crc_offset = size;
669         }
670         memcpy(rxbuf + crc_offset, &crc_val, sizeof(crc_val));
671
672         bytes_to_copy += 4;
673         size += 4;
674     }
675
676     /* Pad to minimum length */
677     if (size < 64) {
678         size = 64;
679     }
680
681     DB_PRINT("config bufsize: %d packet size: %ld\n", rxbufsize, size);
682
683     while (bytes_to_copy) {
684         /* Do nothing if receive is not enabled. */
685         if (!gem_can_receive(nc)) {
686             assert(!first_desc);
687             return -1;
688         }
689
690         DB_PRINT("copy %d bytes to 0x%x\n", MIN(bytes_to_copy, rxbufsize),
691                 rx_desc_get_buffer(s->rx_desc));
692
693         /* Copy packet data to emulated DMA buffer */
694         cpu_physical_memory_write(rx_desc_get_buffer(s->rx_desc) + rxbuf_offset,
695                                   rxbuf_ptr, MIN(bytes_to_copy, rxbufsize));
696         bytes_to_copy -= MIN(bytes_to_copy, rxbufsize);
697         rxbuf_ptr += MIN(bytes_to_copy, rxbufsize);
698
699         /* Update the descriptor.  */
700         if (first_desc) {
701             rx_desc_set_sof(s->rx_desc);
702             first_desc = false;
703         }
704         if (bytes_to_copy == 0) {
705             rx_desc_set_eof(s->rx_desc);
706             rx_desc_set_length(s->rx_desc, size);
707         }
708         rx_desc_set_ownership(s->rx_desc);
709         /* Descriptor write-back.  */
710         cpu_physical_memory_write(s->rx_desc_addr,
711                                   (uint8_t *)s->rx_desc, sizeof(s->rx_desc));
712
713         /* Next descriptor */
714         if (rx_desc_get_wrap(s->rx_desc)) {
715             DB_PRINT("wrapping RX descriptor list\n");
716             s->rx_desc_addr = s->regs[GEM_RXQBASE];
717         } else {
718             DB_PRINT("incrementing RX descriptor list\n");
719             s->rx_desc_addr += 8;
720         }
721         gem_get_rx_desc(s);
722     }
723
724     /* Count it */
725     gem_receive_updatestats(s, buf, size);
726
727     s->regs[GEM_RXSTATUS] |= GEM_RXSTATUS_FRMRCVD;
728     s->regs[GEM_ISR] |= GEM_INT_RXCMPL & ~(s->regs[GEM_IMR]);
729
730     /* Handle interrupt consequences */
731     gem_update_int_status(s);
732
733     return size;
734 }
735
736 /*
737  * gem_transmit_updatestats:
738  * Increment transmit statistics.
739  */
740 static void gem_transmit_updatestats(GemState *s, const uint8_t *packet,
741                                      unsigned bytes)
742 {
743     uint64_t octets;
744
745     /* Total octets (bytes) transmitted */
746     octets = ((uint64_t)(s->regs[GEM_OCTTXLO]) << 32) |
747              s->regs[GEM_OCTTXHI];
748     octets += bytes;
749     s->regs[GEM_OCTTXLO] = octets >> 32;
750     s->regs[GEM_OCTTXHI] = octets;
751
752     /* Error-free Frames transmitted */
753     s->regs[GEM_TXCNT]++;
754
755     /* Error-free Broadcast Frames counter */
756     if (!memcmp(packet, broadcast_addr, 6)) {
757         s->regs[GEM_TXBCNT]++;
758     }
759
760     /* Error-free Multicast Frames counter */
761     if (packet[0] == 0x01) {
762         s->regs[GEM_TXMCNT]++;
763     }
764
765     if (bytes <= 64) {
766         s->regs[GEM_TX64CNT]++;
767     } else if (bytes <= 127) {
768         s->regs[GEM_TX65CNT]++;
769     } else if (bytes <= 255) {
770         s->regs[GEM_TX128CNT]++;
771     } else if (bytes <= 511) {
772         s->regs[GEM_TX256CNT]++;
773     } else if (bytes <= 1023) {
774         s->regs[GEM_TX512CNT]++;
775     } else if (bytes <= 1518) {
776         s->regs[GEM_TX1024CNT]++;
777     } else {
778         s->regs[GEM_TX1519CNT]++;
779     }
780 }
781
782 /*
783  * gem_transmit:
784  * Fish packets out of the descriptor ring and feed them to QEMU
785  */
786 static void gem_transmit(GemState *s)
787 {
788     unsigned    desc[2];
789     hwaddr packet_desc_addr;
790     uint8_t     tx_packet[2048];
791     uint8_t     *p;
792     unsigned    total_bytes;
793
794     /* Do nothing if transmit is not enabled. */
795     if (!(s->regs[GEM_NWCTRL] & GEM_NWCTRL_TXENA)) {
796         return;
797     }
798
799     DB_PRINT("\n");
800
801     /* The packet we will hand off to qemu.
802      * Packets scattered across multiple descriptors are gathered to this
803      * one contiguous buffer first.
804      */
805     p = tx_packet;
806     total_bytes = 0;
807
808     /* read current descriptor */
809     packet_desc_addr = s->tx_desc_addr;
810     cpu_physical_memory_read(packet_desc_addr,
811                              (uint8_t *)&desc[0], sizeof(desc));
812     /* Handle all descriptors owned by hardware */
813     while (tx_desc_get_used(desc) == 0) {
814
815         /* Do nothing if transmit is not enabled. */
816         if (!(s->regs[GEM_NWCTRL] & GEM_NWCTRL_TXENA)) {
817             return;
818         }
819         print_gem_tx_desc(desc);
820
821         /* The real hardware would eat this (and possibly crash).
822          * For QEMU let's lend a helping hand.
823          */
824         if ((tx_desc_get_buffer(desc) == 0) ||
825             (tx_desc_get_length(desc) == 0)) {
826             DB_PRINT("Invalid TX descriptor @ 0x%x\n",
827                      (unsigned)packet_desc_addr);
828             break;
829         }
830
831         /* Gather this fragment of the packet from "dma memory" to our contig.
832          * buffer.
833          */
834         cpu_physical_memory_read(tx_desc_get_buffer(desc), p,
835                                  tx_desc_get_length(desc));
836         p += tx_desc_get_length(desc);
837         total_bytes += tx_desc_get_length(desc);
838
839         /* Last descriptor for this packet; hand the whole thing off */
840         if (tx_desc_get_last(desc)) {
841             /* Modify the 1st descriptor of this packet to be owned by
842              * the processor.
843              */
844             cpu_physical_memory_read(s->tx_desc_addr,
845                                      (uint8_t *)&desc[0], sizeof(desc));
846             tx_desc_set_used(desc);
847             cpu_physical_memory_write(s->tx_desc_addr,
848                                       (uint8_t *)&desc[0], sizeof(desc));
849             /* Advance the hardare current descriptor past this packet */
850             if (tx_desc_get_wrap(desc)) {
851                 s->tx_desc_addr = s->regs[GEM_TXQBASE];
852             } else {
853                 s->tx_desc_addr = packet_desc_addr + 8;
854             }
855             DB_PRINT("TX descriptor next: 0x%08x\n", s->tx_desc_addr);
856
857             s->regs[GEM_TXSTATUS] |= GEM_TXSTATUS_TXCMPL;
858             s->regs[GEM_ISR] |= GEM_INT_TXCMPL & ~(s->regs[GEM_IMR]);
859
860             /* Handle interrupt consequences */
861             gem_update_int_status(s);
862
863             /* Is checksum offload enabled? */
864             if (s->regs[GEM_DMACFG] & GEM_DMACFG_TXCSUM_OFFL) {
865                 net_checksum_calculate(tx_packet, total_bytes);
866             }
867
868             /* Update MAC statistics */
869             gem_transmit_updatestats(s, tx_packet, total_bytes);
870
871             /* Send the packet somewhere */
872             if (s->phy_loop || (s->regs[GEM_NWCTRL] & GEM_NWCTRL_LOCALLOOP)) {
873                 gem_receive(qemu_get_queue(s->nic), tx_packet, total_bytes);
874             } else {
875                 qemu_send_packet(qemu_get_queue(s->nic), tx_packet,
876                                  total_bytes);
877             }
878
879             /* Prepare for next packet */
880             p = tx_packet;
881             total_bytes = 0;
882         }
883
884         /* read next descriptor */
885         if (tx_desc_get_wrap(desc)) {
886             packet_desc_addr = s->regs[GEM_TXQBASE];
887         } else {
888             packet_desc_addr += 8;
889         }
890         cpu_physical_memory_read(packet_desc_addr,
891                                  (uint8_t *)&desc[0], sizeof(desc));
892     }
893
894     if (tx_desc_get_used(desc)) {
895         s->regs[GEM_TXSTATUS] |= GEM_TXSTATUS_USED;
896         s->regs[GEM_ISR] |= GEM_INT_TXUSED & ~(s->regs[GEM_IMR]);
897         gem_update_int_status(s);
898     }
899 }
900
901 static void gem_phy_reset(GemState *s)
902 {
903     memset(&s->phy_regs[0], 0, sizeof(s->phy_regs));
904     s->phy_regs[PHY_REG_CONTROL] = 0x1140;
905     s->phy_regs[PHY_REG_STATUS] = 0x7969;
906     s->phy_regs[PHY_REG_PHYID1] = 0x0141;
907     s->phy_regs[PHY_REG_PHYID2] = 0x0CC2;
908     s->phy_regs[PHY_REG_ANEGADV] = 0x01E1;
909     s->phy_regs[PHY_REG_LINKPABIL] = 0xCDE1;
910     s->phy_regs[PHY_REG_ANEGEXP] = 0x000F;
911     s->phy_regs[PHY_REG_NEXTP] = 0x2001;
912     s->phy_regs[PHY_REG_LINKPNEXTP] = 0x40E6;
913     s->phy_regs[PHY_REG_100BTCTRL] = 0x0300;
914     s->phy_regs[PHY_REG_1000BTSTAT] = 0x7C00;
915     s->phy_regs[PHY_REG_EXTSTAT] = 0x3000;
916     s->phy_regs[PHY_REG_PHYSPCFC_CTL] = 0x0078;
917     s->phy_regs[PHY_REG_PHYSPCFC_ST] = 0xBC00;
918     s->phy_regs[PHY_REG_EXT_PHYSPCFC_CTL] = 0x0C60;
919     s->phy_regs[PHY_REG_LED] = 0x4100;
920     s->phy_regs[PHY_REG_EXT_PHYSPCFC_CTL2] = 0x000A;
921     s->phy_regs[PHY_REG_EXT_PHYSPCFC_ST] = 0x848B;
922
923     phy_update_link(s);
924 }
925
926 static void gem_reset(DeviceState *d)
927 {
928     GemState *s = GEM(d);
929
930     DB_PRINT("\n");
931
932     /* Set post reset register values */
933     memset(&s->regs[0], 0, sizeof(s->regs));
934     s->regs[GEM_NWCFG] = 0x00080000;
935     s->regs[GEM_NWSTATUS] = 0x00000006;
936     s->regs[GEM_DMACFG] = 0x00020784;
937     s->regs[GEM_IMR] = 0x07ffffff;
938     s->regs[GEM_TXPAUSE] = 0x0000ffff;
939     s->regs[GEM_TXPARTIALSF] = 0x000003ff;
940     s->regs[GEM_RXPARTIALSF] = 0x000003ff;
941     s->regs[GEM_MODID] = 0x00020118;
942     s->regs[GEM_DESCONF] = 0x02500111;
943     s->regs[GEM_DESCONF2] = 0x2ab13fff;
944     s->regs[GEM_DESCONF5] = 0x002f2145;
945     s->regs[GEM_DESCONF6] = 0x00000200;
946
947     gem_phy_reset(s);
948
949     gem_update_int_status(s);
950 }
951
952 static uint16_t gem_phy_read(GemState *s, unsigned reg_num)
953 {
954     DB_PRINT("reg: %d value: 0x%04x\n", reg_num, s->phy_regs[reg_num]);
955     return s->phy_regs[reg_num];
956 }
957
958 static void gem_phy_write(GemState *s, unsigned reg_num, uint16_t val)
959 {
960     DB_PRINT("reg: %d value: 0x%04x\n", reg_num, val);
961
962     switch (reg_num) {
963     case PHY_REG_CONTROL:
964         if (val & PHY_REG_CONTROL_RST) {
965             /* Phy reset */
966             gem_phy_reset(s);
967             val &= ~(PHY_REG_CONTROL_RST | PHY_REG_CONTROL_LOOP);
968             s->phy_loop = 0;
969         }
970         if (val & PHY_REG_CONTROL_ANEG) {
971             /* Complete autonegotiation immediately */
972             val &= ~PHY_REG_CONTROL_ANEG;
973             s->phy_regs[PHY_REG_STATUS] |= PHY_REG_STATUS_ANEGCMPL;
974         }
975         if (val & PHY_REG_CONTROL_LOOP) {
976             DB_PRINT("PHY placed in loopback\n");
977             s->phy_loop = 1;
978         } else {
979             s->phy_loop = 0;
980         }
981         break;
982     }
983     s->phy_regs[reg_num] = val;
984 }
985
986 /*
987  * gem_read32:
988  * Read a GEM register.
989  */
990 static uint64_t gem_read(void *opaque, hwaddr offset, unsigned size)
991 {
992     GemState *s;
993     uint32_t retval;
994
995     s = (GemState *)opaque;
996
997     offset >>= 2;
998     retval = s->regs[offset];
999
1000     DB_PRINT("offset: 0x%04x read: 0x%08x\n", (unsigned)offset*4, retval);
1001
1002     switch (offset) {
1003     case GEM_ISR:
1004         DB_PRINT("lowering irq on ISR read\n");
1005         qemu_set_irq(s->irq, 0);
1006         break;
1007     case GEM_PHYMNTNC:
1008         if (retval & GEM_PHYMNTNC_OP_R) {
1009             uint32_t phy_addr, reg_num;
1010
1011             phy_addr = (retval & GEM_PHYMNTNC_ADDR) >> GEM_PHYMNTNC_ADDR_SHFT;
1012             if (phy_addr == BOARD_PHY_ADDRESS) {
1013                 reg_num = (retval & GEM_PHYMNTNC_REG) >> GEM_PHYMNTNC_REG_SHIFT;
1014                 retval &= 0xFFFF0000;
1015                 retval |= gem_phy_read(s, reg_num);
1016             } else {
1017                 retval |= 0xFFFF; /* No device at this address */
1018             }
1019         }
1020         break;
1021     }
1022
1023     /* Squash read to clear bits */
1024     s->regs[offset] &= ~(s->regs_rtc[offset]);
1025
1026     /* Do not provide write only bits */
1027     retval &= ~(s->regs_wo[offset]);
1028
1029     DB_PRINT("0x%08x\n", retval);
1030     return retval;
1031 }
1032
1033 /*
1034  * gem_write32:
1035  * Write a GEM register.
1036  */
1037 static void gem_write(void *opaque, hwaddr offset, uint64_t val,
1038         unsigned size)
1039 {
1040     GemState *s = (GemState *)opaque;
1041     uint32_t readonly;
1042
1043     DB_PRINT("offset: 0x%04x write: 0x%08x ", (unsigned)offset, (unsigned)val);
1044     offset >>= 2;
1045
1046     /* Squash bits which are read only in write value */
1047     val &= ~(s->regs_ro[offset]);
1048     /* Preserve (only) bits which are read only in register */
1049     readonly = s->regs[offset];
1050     readonly &= s->regs_ro[offset];
1051
1052     /* Squash bits which are write 1 to clear */
1053     val &= ~(s->regs_w1c[offset] & val);
1054
1055     /* Copy register write to backing store */
1056     s->regs[offset] = val | readonly;
1057
1058     /* Handle register write side effects */
1059     switch (offset) {
1060     case GEM_NWCTRL:
1061         if (val & GEM_NWCTRL_RXENA) {
1062             gem_get_rx_desc(s);
1063         }
1064         if (val & GEM_NWCTRL_TXSTART) {
1065             gem_transmit(s);
1066         }
1067         if (!(val & GEM_NWCTRL_TXENA)) {
1068             /* Reset to start of Q when transmit disabled. */
1069             s->tx_desc_addr = s->regs[GEM_TXQBASE];
1070         }
1071         if (val & GEM_NWCTRL_RXENA) {
1072             qemu_flush_queued_packets(qemu_get_queue(s->nic));
1073         }
1074         break;
1075
1076     case GEM_TXSTATUS:
1077         gem_update_int_status(s);
1078         break;
1079     case GEM_RXQBASE:
1080         s->rx_desc_addr = val;
1081         break;
1082     case GEM_TXQBASE:
1083         s->tx_desc_addr = val;
1084         break;
1085     case GEM_RXSTATUS:
1086         gem_update_int_status(s);
1087         break;
1088     case GEM_IER:
1089         s->regs[GEM_IMR] &= ~val;
1090         gem_update_int_status(s);
1091         break;
1092     case GEM_IDR:
1093         s->regs[GEM_IMR] |= val;
1094         gem_update_int_status(s);
1095         break;
1096     case GEM_PHYMNTNC:
1097         if (val & GEM_PHYMNTNC_OP_W) {
1098             uint32_t phy_addr, reg_num;
1099
1100             phy_addr = (val & GEM_PHYMNTNC_ADDR) >> GEM_PHYMNTNC_ADDR_SHFT;
1101             if (phy_addr == BOARD_PHY_ADDRESS) {
1102                 reg_num = (val & GEM_PHYMNTNC_REG) >> GEM_PHYMNTNC_REG_SHIFT;
1103                 gem_phy_write(s, reg_num, val);
1104             }
1105         }
1106         break;
1107     }
1108
1109     DB_PRINT("newval: 0x%08x\n", s->regs[offset]);
1110 }
1111
1112 static const MemoryRegionOps gem_ops = {
1113     .read = gem_read,
1114     .write = gem_write,
1115     .endianness = DEVICE_LITTLE_ENDIAN,
1116 };
1117
1118 static void gem_cleanup(NetClientState *nc)
1119 {
1120     GemState *s = qemu_get_nic_opaque(nc);
1121
1122     DB_PRINT("\n");
1123     s->nic = NULL;
1124 }
1125
1126 static void gem_set_link(NetClientState *nc)
1127 {
1128     DB_PRINT("\n");
1129     phy_update_link(qemu_get_nic_opaque(nc));
1130 }
1131
1132 static NetClientInfo net_gem_info = {
1133     .type = NET_CLIENT_OPTIONS_KIND_NIC,
1134     .size = sizeof(NICState),
1135     .can_receive = gem_can_receive,
1136     .receive = gem_receive,
1137     .cleanup = gem_cleanup,
1138     .link_status_changed = gem_set_link,
1139 };
1140
1141 static int gem_init(SysBusDevice *sbd)
1142 {
1143     DeviceState *dev = DEVICE(sbd);
1144     GemState *s = GEM(dev);
1145
1146     DB_PRINT("\n");
1147
1148     gem_init_register_masks(s);
1149     memory_region_init_io(&s->iomem, OBJECT(s), &gem_ops, s,
1150                           "enet", sizeof(s->regs));
1151     sysbus_init_mmio(sbd, &s->iomem);
1152     sysbus_init_irq(sbd, &s->irq);
1153     qemu_macaddr_default_if_unset(&s->conf.macaddr);
1154
1155     s->nic = qemu_new_nic(&net_gem_info, &s->conf,
1156             object_get_typename(OBJECT(dev)), dev->id, s);
1157
1158     return 0;
1159 }
1160
1161 static const VMStateDescription vmstate_cadence_gem = {
1162     .name = "cadence_gem",
1163     .version_id = 1,
1164     .minimum_version_id = 1,
1165     .minimum_version_id_old = 1,
1166     .fields      = (VMStateField[]) {
1167         VMSTATE_UINT32_ARRAY(regs, GemState, GEM_MAXREG),
1168         VMSTATE_UINT16_ARRAY(phy_regs, GemState, 32),
1169         VMSTATE_UINT8(phy_loop, GemState),
1170         VMSTATE_UINT32(rx_desc_addr, GemState),
1171         VMSTATE_UINT32(tx_desc_addr, GemState),
1172     }
1173 };
1174
1175 static Property gem_properties[] = {
1176     DEFINE_NIC_PROPERTIES(GemState, conf),
1177     DEFINE_PROP_END_OF_LIST(),
1178 };
1179
1180 static void gem_class_init(ObjectClass *klass, void *data)
1181 {
1182     DeviceClass *dc = DEVICE_CLASS(klass);
1183     SysBusDeviceClass *sdc = SYS_BUS_DEVICE_CLASS(klass);
1184
1185     sdc->init = gem_init;
1186     dc->props = gem_properties;
1187     dc->vmsd = &vmstate_cadence_gem;
1188     dc->reset = gem_reset;
1189 }
1190
1191 static const TypeInfo gem_info = {
1192     .name  = TYPE_CADENCE_GEM,
1193     .parent = TYPE_SYS_BUS_DEVICE,
1194     .instance_size  = sizeof(GemState),
1195     .class_init = gem_class_init,
1196 };
1197
1198 static void gem_register_types(void)
1199 {
1200     type_register_static(&gem_info);
1201 }
1202
1203 type_init(gem_register_types)
This page took 0.090742 seconds and 4 git commands to generate.