xref: /openbmc/qemu/hw/net/dp8393x.c (revision a9a5c473)
1 /*
2  * QEMU NS SONIC DP8393x netcard
3  *
4  * Copyright (c) 2008-2009 Herve Poussineau
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License as
8  * published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * with this program; if not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #include "qemu/osdep.h"
21 #include "hw/irq.h"
22 #include "hw/qdev-properties.h"
23 #include "hw/sysbus.h"
24 #include "migration/vmstate.h"
25 #include "net/net.h"
26 #include "qapi/error.h"
27 #include "qemu/module.h"
28 #include "qemu/timer.h"
29 #include <zlib.h>
30 
31 //#define DEBUG_SONIC
32 
33 #define SONIC_PROM_SIZE 0x1000
34 
35 #ifdef DEBUG_SONIC
36 #define DPRINTF(fmt, ...) \
37 do { printf("sonic: " fmt , ##  __VA_ARGS__); } while (0)
38 static const char* reg_names[] = {
39     "CR", "DCR", "RCR", "TCR", "IMR", "ISR", "UTDA", "CTDA",
40     "TPS", "TFC", "TSA0", "TSA1", "TFS", "URDA", "CRDA", "CRBA0",
41     "CRBA1", "RBWC0", "RBWC1", "EOBC", "URRA", "RSA", "REA", "RRP",
42     "RWP", "TRBA0", "TRBA1", "0x1b", "0x1c", "0x1d", "0x1e", "LLFA",
43     "TTDA", "CEP", "CAP2", "CAP1", "CAP0", "CE", "CDP", "CDC",
44     "SR", "WT0", "WT1", "RSC", "CRCT", "FAET", "MPT", "MDT",
45     "0x30", "0x31", "0x32", "0x33", "0x34", "0x35", "0x36", "0x37",
46     "0x38", "0x39", "0x3a", "0x3b", "0x3c", "0x3d", "0x3e", "DCR2" };
47 #else
48 #define DPRINTF(fmt, ...) do {} while (0)
49 #endif
50 
51 #define SONIC_ERROR(fmt, ...) \
52 do { printf("sonic ERROR: %s: " fmt, __func__ , ## __VA_ARGS__); } while (0)
53 
54 #define SONIC_CR     0x00
55 #define SONIC_DCR    0x01
56 #define SONIC_RCR    0x02
57 #define SONIC_TCR    0x03
58 #define SONIC_IMR    0x04
59 #define SONIC_ISR    0x05
60 #define SONIC_UTDA   0x06
61 #define SONIC_CTDA   0x07
62 #define SONIC_TPS    0x08
63 #define SONIC_TFC    0x09
64 #define SONIC_TSA0   0x0a
65 #define SONIC_TSA1   0x0b
66 #define SONIC_TFS    0x0c
67 #define SONIC_URDA   0x0d
68 #define SONIC_CRDA   0x0e
69 #define SONIC_CRBA0  0x0f
70 #define SONIC_CRBA1  0x10
71 #define SONIC_RBWC0  0x11
72 #define SONIC_RBWC1  0x12
73 #define SONIC_EOBC   0x13
74 #define SONIC_URRA   0x14
75 #define SONIC_RSA    0x15
76 #define SONIC_REA    0x16
77 #define SONIC_RRP    0x17
78 #define SONIC_RWP    0x18
79 #define SONIC_TRBA0  0x19
80 #define SONIC_TRBA1  0x1a
81 #define SONIC_LLFA   0x1f
82 #define SONIC_TTDA   0x20
83 #define SONIC_CEP    0x21
84 #define SONIC_CAP2   0x22
85 #define SONIC_CAP1   0x23
86 #define SONIC_CAP0   0x24
87 #define SONIC_CE     0x25
88 #define SONIC_CDP    0x26
89 #define SONIC_CDC    0x27
90 #define SONIC_SR     0x28
91 #define SONIC_WT0    0x29
92 #define SONIC_WT1    0x2a
93 #define SONIC_RSC    0x2b
94 #define SONIC_CRCT   0x2c
95 #define SONIC_FAET   0x2d
96 #define SONIC_MPT    0x2e
97 #define SONIC_MDT    0x2f
98 #define SONIC_DCR2   0x3f
99 
100 #define SONIC_CR_HTX     0x0001
101 #define SONIC_CR_TXP     0x0002
102 #define SONIC_CR_RXDIS   0x0004
103 #define SONIC_CR_RXEN    0x0008
104 #define SONIC_CR_STP     0x0010
105 #define SONIC_CR_ST      0x0020
106 #define SONIC_CR_RST     0x0080
107 #define SONIC_CR_RRRA    0x0100
108 #define SONIC_CR_LCAM    0x0200
109 #define SONIC_CR_MASK    0x03bf
110 
111 #define SONIC_DCR_DW     0x0020
112 #define SONIC_DCR_LBR    0x2000
113 #define SONIC_DCR_EXBUS  0x8000
114 
115 #define SONIC_RCR_PRX    0x0001
116 #define SONIC_RCR_LBK    0x0002
117 #define SONIC_RCR_FAER   0x0004
118 #define SONIC_RCR_CRCR   0x0008
119 #define SONIC_RCR_CRS    0x0020
120 #define SONIC_RCR_LPKT   0x0040
121 #define SONIC_RCR_BC     0x0080
122 #define SONIC_RCR_MC     0x0100
123 #define SONIC_RCR_LB0    0x0200
124 #define SONIC_RCR_LB1    0x0400
125 #define SONIC_RCR_AMC    0x0800
126 #define SONIC_RCR_PRO    0x1000
127 #define SONIC_RCR_BRD    0x2000
128 #define SONIC_RCR_RNT    0x4000
129 
130 #define SONIC_TCR_PTX    0x0001
131 #define SONIC_TCR_BCM    0x0002
132 #define SONIC_TCR_FU     0x0004
133 #define SONIC_TCR_EXC    0x0040
134 #define SONIC_TCR_CRSL   0x0080
135 #define SONIC_TCR_NCRS   0x0100
136 #define SONIC_TCR_EXD    0x0400
137 #define SONIC_TCR_CRCI   0x2000
138 #define SONIC_TCR_PINT   0x8000
139 
140 #define SONIC_ISR_RBAE   0x0010
141 #define SONIC_ISR_RBE    0x0020
142 #define SONIC_ISR_RDE    0x0040
143 #define SONIC_ISR_TC     0x0080
144 #define SONIC_ISR_TXDN   0x0200
145 #define SONIC_ISR_PKTRX  0x0400
146 #define SONIC_ISR_PINT   0x0800
147 #define SONIC_ISR_LCD    0x1000
148 
149 #define SONIC_DESC_EOL   0x0001
150 #define SONIC_DESC_ADDR  0xFFFE
151 
152 #define TYPE_DP8393X "dp8393x"
153 #define DP8393X(obj) OBJECT_CHECK(dp8393xState, (obj), TYPE_DP8393X)
154 
155 typedef struct dp8393xState {
156     SysBusDevice parent_obj;
157 
158     /* Hardware */
159     uint8_t it_shift;
160     bool big_endian;
161     bool last_rba_is_full;
162     qemu_irq irq;
163 #ifdef DEBUG_SONIC
164     int irq_level;
165 #endif
166     QEMUTimer *watchdog;
167     int64_t wt_last_update;
168     NICConf conf;
169     NICState *nic;
170     MemoryRegion mmio;
171     MemoryRegion prom;
172 
173     /* Registers */
174     uint8_t cam[16][6];
175     uint16_t regs[0x40];
176 
177     /* Temporaries */
178     uint8_t tx_buffer[0x10000];
179     uint16_t data[12];
180     int loopback_packet;
181 
182     /* Memory access */
183     MemoryRegion *dma_mr;
184     AddressSpace as;
185 } dp8393xState;
186 
187 /* Accessor functions for values which are formed by
188  * concatenating two 16 bit device registers. By putting these
189  * in their own functions with a uint32_t return type we avoid the
190  * pitfall of implicit sign extension where ((x << 16) | y) is a
191  * signed 32 bit integer that might get sign-extended to a 64 bit integer.
192  */
193 static uint32_t dp8393x_cdp(dp8393xState *s)
194 {
195     return (s->regs[SONIC_URRA] << 16) | s->regs[SONIC_CDP];
196 }
197 
198 static uint32_t dp8393x_crba(dp8393xState *s)
199 {
200     return (s->regs[SONIC_CRBA1] << 16) | s->regs[SONIC_CRBA0];
201 }
202 
203 static uint32_t dp8393x_crda(dp8393xState *s)
204 {
205     return (s->regs[SONIC_URDA] << 16) |
206            (s->regs[SONIC_CRDA] & SONIC_DESC_ADDR);
207 }
208 
209 static uint32_t dp8393x_rbwc(dp8393xState *s)
210 {
211     return (s->regs[SONIC_RBWC1] << 16) | s->regs[SONIC_RBWC0];
212 }
213 
214 static uint32_t dp8393x_rrp(dp8393xState *s)
215 {
216     return (s->regs[SONIC_URRA] << 16) | s->regs[SONIC_RRP];
217 }
218 
219 static uint32_t dp8393x_tsa(dp8393xState *s)
220 {
221     return (s->regs[SONIC_TSA1] << 16) | s->regs[SONIC_TSA0];
222 }
223 
224 static uint32_t dp8393x_ttda(dp8393xState *s)
225 {
226     return (s->regs[SONIC_UTDA] << 16) |
227            (s->regs[SONIC_TTDA] & SONIC_DESC_ADDR);
228 }
229 
230 static uint32_t dp8393x_wt(dp8393xState *s)
231 {
232     return s->regs[SONIC_WT1] << 16 | s->regs[SONIC_WT0];
233 }
234 
235 static uint16_t dp8393x_get(dp8393xState *s, int width, int offset)
236 {
237     uint16_t val;
238 
239     if (s->big_endian) {
240         val = be16_to_cpu(s->data[offset * width + width - 1]);
241     } else {
242         val = le16_to_cpu(s->data[offset * width]);
243     }
244     return val;
245 }
246 
247 static void dp8393x_put(dp8393xState *s, int width, int offset,
248                         uint16_t val)
249 {
250     if (s->big_endian) {
251         if (width == 2) {
252             s->data[offset * 2] = 0;
253             s->data[offset * 2 + 1] = cpu_to_be16(val);
254         } else {
255             s->data[offset] = cpu_to_be16(val);
256         }
257     } else {
258         if (width == 2) {
259             s->data[offset * 2] = cpu_to_le16(val);
260             s->data[offset * 2 + 1] = 0;
261         } else {
262             s->data[offset] = cpu_to_le16(val);
263         }
264     }
265 }
266 
267 static void dp8393x_update_irq(dp8393xState *s)
268 {
269     int level = (s->regs[SONIC_IMR] & s->regs[SONIC_ISR]) ? 1 : 0;
270 
271 #ifdef DEBUG_SONIC
272     if (level != s->irq_level) {
273         s->irq_level = level;
274         if (level) {
275             DPRINTF("raise irq, isr is 0x%04x\n", s->regs[SONIC_ISR]);
276         } else {
277             DPRINTF("lower irq\n");
278         }
279     }
280 #endif
281 
282     qemu_set_irq(s->irq, level);
283 }
284 
285 static void dp8393x_do_load_cam(dp8393xState *s)
286 {
287     int width, size;
288     uint16_t index = 0;
289 
290     width = (s->regs[SONIC_DCR] & SONIC_DCR_DW) ? 2 : 1;
291     size = sizeof(uint16_t) * 4 * width;
292 
293     while (s->regs[SONIC_CDC] & 0x1f) {
294         /* Fill current entry */
295         address_space_read(&s->as, dp8393x_cdp(s),
296                            MEMTXATTRS_UNSPECIFIED, s->data, size);
297         s->cam[index][0] = dp8393x_get(s, width, 1) & 0xff;
298         s->cam[index][1] = dp8393x_get(s, width, 1) >> 8;
299         s->cam[index][2] = dp8393x_get(s, width, 2) & 0xff;
300         s->cam[index][3] = dp8393x_get(s, width, 2) >> 8;
301         s->cam[index][4] = dp8393x_get(s, width, 3) & 0xff;
302         s->cam[index][5] = dp8393x_get(s, width, 3) >> 8;
303         DPRINTF("load cam[%d] with %02x%02x%02x%02x%02x%02x\n", index,
304             s->cam[index][0], s->cam[index][1], s->cam[index][2],
305             s->cam[index][3], s->cam[index][4], s->cam[index][5]);
306         /* Move to next entry */
307         s->regs[SONIC_CDC]--;
308         s->regs[SONIC_CDP] += size;
309         index++;
310     }
311 
312     /* Read CAM enable */
313     address_space_read(&s->as, dp8393x_cdp(s),
314                        MEMTXATTRS_UNSPECIFIED, s->data, size);
315     s->regs[SONIC_CE] = dp8393x_get(s, width, 0);
316     DPRINTF("load cam done. cam enable mask 0x%04x\n", s->regs[SONIC_CE]);
317 
318     /* Done */
319     s->regs[SONIC_CR] &= ~SONIC_CR_LCAM;
320     s->regs[SONIC_ISR] |= SONIC_ISR_LCD;
321     dp8393x_update_irq(s);
322 }
323 
324 static void dp8393x_do_read_rra(dp8393xState *s)
325 {
326     int width, size;
327 
328     /* Read memory */
329     width = (s->regs[SONIC_DCR] & SONIC_DCR_DW) ? 2 : 1;
330     size = sizeof(uint16_t) * 4 * width;
331     address_space_read(&s->as, dp8393x_rrp(s),
332                        MEMTXATTRS_UNSPECIFIED, s->data, size);
333 
334     /* Update SONIC registers */
335     s->regs[SONIC_CRBA0] = dp8393x_get(s, width, 0);
336     s->regs[SONIC_CRBA1] = dp8393x_get(s, width, 1);
337     s->regs[SONIC_RBWC0] = dp8393x_get(s, width, 2);
338     s->regs[SONIC_RBWC1] = dp8393x_get(s, width, 3);
339     DPRINTF("CRBA0/1: 0x%04x/0x%04x, RBWC0/1: 0x%04x/0x%04x\n",
340         s->regs[SONIC_CRBA0], s->regs[SONIC_CRBA1],
341         s->regs[SONIC_RBWC0], s->regs[SONIC_RBWC1]);
342 
343     /* Go to next entry */
344     s->regs[SONIC_RRP] += size;
345 
346     /* Handle wrap */
347     if (s->regs[SONIC_RRP] == s->regs[SONIC_REA]) {
348         s->regs[SONIC_RRP] = s->regs[SONIC_RSA];
349     }
350 
351     /* Warn the host if CRBA now has the last available resource */
352     if (s->regs[SONIC_RRP] == s->regs[SONIC_RWP])
353     {
354         s->regs[SONIC_ISR] |= SONIC_ISR_RBE;
355         dp8393x_update_irq(s);
356     }
357 
358     /* Allow packet reception */
359     s->last_rba_is_full = false;
360 }
361 
362 static void dp8393x_do_software_reset(dp8393xState *s)
363 {
364     timer_del(s->watchdog);
365 
366     s->regs[SONIC_CR] &= ~(SONIC_CR_LCAM | SONIC_CR_RRRA | SONIC_CR_TXP | SONIC_CR_HTX);
367     s->regs[SONIC_CR] |= SONIC_CR_RST | SONIC_CR_RXDIS;
368 }
369 
370 static void dp8393x_set_next_tick(dp8393xState *s)
371 {
372     uint32_t ticks;
373     int64_t delay;
374 
375     if (s->regs[SONIC_CR] & SONIC_CR_STP) {
376         timer_del(s->watchdog);
377         return;
378     }
379 
380     ticks = dp8393x_wt(s);
381     s->wt_last_update = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
382     delay = NANOSECONDS_PER_SECOND * ticks / 5000000;
383     timer_mod(s->watchdog, s->wt_last_update + delay);
384 }
385 
386 static void dp8393x_update_wt_regs(dp8393xState *s)
387 {
388     int64_t elapsed;
389     uint32_t val;
390 
391     if (s->regs[SONIC_CR] & SONIC_CR_STP) {
392         timer_del(s->watchdog);
393         return;
394     }
395 
396     elapsed = s->wt_last_update - qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
397     val = dp8393x_wt(s);
398     val -= elapsed / 5000000;
399     s->regs[SONIC_WT1] = (val >> 16) & 0xffff;
400     s->regs[SONIC_WT0] = (val >> 0)  & 0xffff;
401     dp8393x_set_next_tick(s);
402 
403 }
404 
405 static void dp8393x_do_start_timer(dp8393xState *s)
406 {
407     s->regs[SONIC_CR] &= ~SONIC_CR_STP;
408     dp8393x_set_next_tick(s);
409 }
410 
411 static void dp8393x_do_stop_timer(dp8393xState *s)
412 {
413     s->regs[SONIC_CR] &= ~SONIC_CR_ST;
414     dp8393x_update_wt_regs(s);
415 }
416 
417 static bool dp8393x_can_receive(NetClientState *nc);
418 
419 static void dp8393x_do_receiver_enable(dp8393xState *s)
420 {
421     s->regs[SONIC_CR] &= ~SONIC_CR_RXDIS;
422     if (dp8393x_can_receive(s->nic->ncs)) {
423         qemu_flush_queued_packets(qemu_get_queue(s->nic));
424     }
425 }
426 
427 static void dp8393x_do_receiver_disable(dp8393xState *s)
428 {
429     s->regs[SONIC_CR] &= ~SONIC_CR_RXEN;
430 }
431 
432 static void dp8393x_do_transmit_packets(dp8393xState *s)
433 {
434     NetClientState *nc = qemu_get_queue(s->nic);
435     int width, size;
436     int tx_len, len;
437     uint16_t i;
438 
439     width = (s->regs[SONIC_DCR] & SONIC_DCR_DW) ? 2 : 1;
440 
441     while (1) {
442         /* Read memory */
443         size = sizeof(uint16_t) * 6 * width;
444         s->regs[SONIC_TTDA] = s->regs[SONIC_CTDA];
445         DPRINTF("Transmit packet at %08x\n", dp8393x_ttda(s));
446         address_space_read(&s->as, dp8393x_ttda(s) + sizeof(uint16_t) * width,
447                            MEMTXATTRS_UNSPECIFIED, s->data, size);
448         tx_len = 0;
449 
450         /* Update registers */
451         s->regs[SONIC_TCR] = dp8393x_get(s, width, 0) & 0xf000;
452         s->regs[SONIC_TPS] = dp8393x_get(s, width, 1);
453         s->regs[SONIC_TFC] = dp8393x_get(s, width, 2);
454         s->regs[SONIC_TSA0] = dp8393x_get(s, width, 3);
455         s->regs[SONIC_TSA1] = dp8393x_get(s, width, 4);
456         s->regs[SONIC_TFS] = dp8393x_get(s, width, 5);
457 
458         /* Handle programmable interrupt */
459         if (s->regs[SONIC_TCR] & SONIC_TCR_PINT) {
460             s->regs[SONIC_ISR] |= SONIC_ISR_PINT;
461         } else {
462             s->regs[SONIC_ISR] &= ~SONIC_ISR_PINT;
463         }
464 
465         for (i = 0; i < s->regs[SONIC_TFC]; ) {
466             /* Append fragment */
467             len = s->regs[SONIC_TFS];
468             if (tx_len + len > sizeof(s->tx_buffer)) {
469                 len = sizeof(s->tx_buffer) - tx_len;
470             }
471             address_space_read(&s->as, dp8393x_tsa(s), MEMTXATTRS_UNSPECIFIED,
472                                &s->tx_buffer[tx_len], len);
473             tx_len += len;
474 
475             i++;
476             if (i != s->regs[SONIC_TFC]) {
477                 /* Read next fragment details */
478                 size = sizeof(uint16_t) * 3 * width;
479                 address_space_read(&s->as,
480                                    dp8393x_ttda(s)
481                                    + sizeof(uint16_t) * width * (4 + 3 * i),
482                                    MEMTXATTRS_UNSPECIFIED, s->data,
483                                    size);
484                 s->regs[SONIC_TSA0] = dp8393x_get(s, width, 0);
485                 s->regs[SONIC_TSA1] = dp8393x_get(s, width, 1);
486                 s->regs[SONIC_TFS] = dp8393x_get(s, width, 2);
487             }
488         }
489 
490         /* Handle Ethernet checksum */
491         if (!(s->regs[SONIC_TCR] & SONIC_TCR_CRCI)) {
492             /* Don't append FCS there, to look like slirp packets
493              * which don't have one */
494         } else {
495             /* Remove existing FCS */
496             tx_len -= 4;
497         }
498 
499         if (s->regs[SONIC_RCR] & (SONIC_RCR_LB1 | SONIC_RCR_LB0)) {
500             /* Loopback */
501             s->regs[SONIC_TCR] |= SONIC_TCR_CRSL;
502             if (nc->info->can_receive(nc)) {
503                 s->loopback_packet = 1;
504                 nc->info->receive(nc, s->tx_buffer, tx_len);
505             }
506         } else {
507             /* Transmit packet */
508             qemu_send_packet(nc, s->tx_buffer, tx_len);
509         }
510         s->regs[SONIC_TCR] |= SONIC_TCR_PTX;
511 
512         /* Write status */
513         dp8393x_put(s, width, 0,
514                     s->regs[SONIC_TCR] & 0x0fff); /* status */
515         size = sizeof(uint16_t) * width;
516         address_space_write(&s->as, dp8393x_ttda(s),
517                             MEMTXATTRS_UNSPECIFIED, s->data, size);
518 
519         if (!(s->regs[SONIC_CR] & SONIC_CR_HTX)) {
520             /* Read footer of packet */
521             size = sizeof(uint16_t) * width;
522             address_space_read(&s->as,
523                                dp8393x_ttda(s)
524                                + sizeof(uint16_t) * width
525                                  * (4 + 3 * s->regs[SONIC_TFC]),
526                                MEMTXATTRS_UNSPECIFIED, s->data,
527                                size);
528             s->regs[SONIC_CTDA] = dp8393x_get(s, width, 0);
529             if (s->regs[SONIC_CTDA] & SONIC_DESC_EOL) {
530                 /* EOL detected */
531                 break;
532             }
533         }
534     }
535 
536     /* Done */
537     s->regs[SONIC_CR] &= ~SONIC_CR_TXP;
538     s->regs[SONIC_ISR] |= SONIC_ISR_TXDN;
539     dp8393x_update_irq(s);
540 }
541 
542 static void dp8393x_do_halt_transmission(dp8393xState *s)
543 {
544     /* Nothing to do */
545 }
546 
547 static void dp8393x_do_command(dp8393xState *s, uint16_t command)
548 {
549     if ((s->regs[SONIC_CR] & SONIC_CR_RST) && !(command & SONIC_CR_RST)) {
550         s->regs[SONIC_CR] &= ~SONIC_CR_RST;
551         return;
552     }
553 
554     s->regs[SONIC_CR] |= (command & SONIC_CR_MASK);
555 
556     if (command & SONIC_CR_HTX)
557         dp8393x_do_halt_transmission(s);
558     if (command & SONIC_CR_TXP)
559         dp8393x_do_transmit_packets(s);
560     if (command & SONIC_CR_RXDIS)
561         dp8393x_do_receiver_disable(s);
562     if (command & SONIC_CR_RXEN)
563         dp8393x_do_receiver_enable(s);
564     if (command & SONIC_CR_STP)
565         dp8393x_do_stop_timer(s);
566     if (command & SONIC_CR_ST)
567         dp8393x_do_start_timer(s);
568     if (command & SONIC_CR_RST)
569         dp8393x_do_software_reset(s);
570     if (command & SONIC_CR_RRRA) {
571         dp8393x_do_read_rra(s);
572         s->regs[SONIC_CR] &= ~SONIC_CR_RRRA;
573     }
574     if (command & SONIC_CR_LCAM)
575         dp8393x_do_load_cam(s);
576 }
577 
578 static uint64_t dp8393x_read(void *opaque, hwaddr addr, unsigned int size)
579 {
580     dp8393xState *s = opaque;
581     int reg = addr >> s->it_shift;
582     uint16_t val = 0;
583 
584     switch (reg) {
585         /* Update data before reading it */
586         case SONIC_WT0:
587         case SONIC_WT1:
588             dp8393x_update_wt_regs(s);
589             val = s->regs[reg];
590             break;
591         /* Accept read to some registers only when in reset mode */
592         case SONIC_CAP2:
593         case SONIC_CAP1:
594         case SONIC_CAP0:
595             if (s->regs[SONIC_CR] & SONIC_CR_RST) {
596                 val = s->cam[s->regs[SONIC_CEP] & 0xf][2* (SONIC_CAP0 - reg) + 1] << 8;
597                 val |= s->cam[s->regs[SONIC_CEP] & 0xf][2* (SONIC_CAP0 - reg)];
598             }
599             break;
600         /* All other registers have no special contrainst */
601         default:
602             val = s->regs[reg];
603     }
604 
605     DPRINTF("read 0x%04x from reg %s\n", val, reg_names[reg]);
606 
607     return s->big_endian ? val << 16 : val;
608 }
609 
610 static void dp8393x_write(void *opaque, hwaddr addr, uint64_t data,
611                           unsigned int size)
612 {
613     dp8393xState *s = opaque;
614     int reg = addr >> s->it_shift;
615     uint32_t val = s->big_endian ? data >> 16 : data;
616 
617     DPRINTF("write 0x%04x to reg %s\n", (uint16_t)val, reg_names[reg]);
618 
619     switch (reg) {
620         /* Command register */
621         case SONIC_CR:
622             dp8393x_do_command(s, val);
623             break;
624         /* Prevent write to read-only registers */
625         case SONIC_CAP2:
626         case SONIC_CAP1:
627         case SONIC_CAP0:
628         case SONIC_SR:
629         case SONIC_MDT:
630             DPRINTF("writing to reg %d invalid\n", reg);
631             break;
632         /* Accept write to some registers only when in reset mode */
633         case SONIC_DCR:
634             if (s->regs[SONIC_CR] & SONIC_CR_RST) {
635                 s->regs[reg] = val & 0xbfff;
636             } else {
637                 DPRINTF("writing to DCR invalid\n");
638             }
639             break;
640         case SONIC_DCR2:
641             if (s->regs[SONIC_CR] & SONIC_CR_RST) {
642                 s->regs[reg] = val & 0xf017;
643             } else {
644                 DPRINTF("writing to DCR2 invalid\n");
645             }
646             break;
647         /* 12 lower bytes are Read Only */
648         case SONIC_TCR:
649             s->regs[reg] = val & 0xf000;
650             break;
651         /* 9 lower bytes are Read Only */
652         case SONIC_RCR:
653             s->regs[reg] = val & 0xffe0;
654             break;
655         /* Ignore most significant bit */
656         case SONIC_IMR:
657             s->regs[reg] = val & 0x7fff;
658             dp8393x_update_irq(s);
659             break;
660         /* Clear bits by writing 1 to them */
661         case SONIC_ISR:
662             val &= s->regs[reg];
663             s->regs[reg] &= ~val;
664             if (val & SONIC_ISR_RBE) {
665                 dp8393x_do_read_rra(s);
666             }
667             dp8393x_update_irq(s);
668             break;
669         /* The guest is required to store aligned pointers here */
670         case SONIC_RSA:
671         case SONIC_REA:
672         case SONIC_RRP:
673         case SONIC_RWP:
674             if (s->regs[SONIC_DCR] & SONIC_DCR_DW) {
675                 s->regs[reg] = val & 0xfffc;
676             } else {
677                 s->regs[reg] = val & 0xfffe;
678             }
679             break;
680         /* Invert written value for some registers */
681         case SONIC_CRCT:
682         case SONIC_FAET:
683         case SONIC_MPT:
684             s->regs[reg] = val ^ 0xffff;
685             break;
686         /* All other registers have no special contrainst */
687         default:
688             s->regs[reg] = val;
689     }
690 
691     if (reg == SONIC_WT0 || reg == SONIC_WT1) {
692         dp8393x_set_next_tick(s);
693     }
694 }
695 
696 static const MemoryRegionOps dp8393x_ops = {
697     .read = dp8393x_read,
698     .write = dp8393x_write,
699     .impl.min_access_size = 4,
700     .impl.max_access_size = 4,
701     .endianness = DEVICE_NATIVE_ENDIAN,
702 };
703 
704 static void dp8393x_watchdog(void *opaque)
705 {
706     dp8393xState *s = opaque;
707 
708     if (s->regs[SONIC_CR] & SONIC_CR_STP) {
709         return;
710     }
711 
712     s->regs[SONIC_WT1] = 0xffff;
713     s->regs[SONIC_WT0] = 0xffff;
714     dp8393x_set_next_tick(s);
715 
716     /* Signal underflow */
717     s->regs[SONIC_ISR] |= SONIC_ISR_TC;
718     dp8393x_update_irq(s);
719 }
720 
721 static bool dp8393x_can_receive(NetClientState *nc)
722 {
723     dp8393xState *s = qemu_get_nic_opaque(nc);
724 
725     return !!(s->regs[SONIC_CR] & SONIC_CR_RXEN);
726 }
727 
728 static int dp8393x_receive_filter(dp8393xState *s, const uint8_t * buf,
729                                   int size)
730 {
731     static const uint8_t bcast[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
732     int i;
733 
734     /* Check promiscuous mode */
735     if ((s->regs[SONIC_RCR] & SONIC_RCR_PRO) && (buf[0] & 1) == 0) {
736         return 0;
737     }
738 
739     /* Check multicast packets */
740     if ((s->regs[SONIC_RCR] & SONIC_RCR_AMC) && (buf[0] & 1) == 1) {
741         return SONIC_RCR_MC;
742     }
743 
744     /* Check broadcast */
745     if ((s->regs[SONIC_RCR] & SONIC_RCR_BRD) && !memcmp(buf, bcast, sizeof(bcast))) {
746         return SONIC_RCR_BC;
747     }
748 
749     /* Check CAM */
750     for (i = 0; i < 16; i++) {
751         if (s->regs[SONIC_CE] & (1 << i)) {
752              /* Entry enabled */
753              if (!memcmp(buf, s->cam[i], sizeof(s->cam[i]))) {
754                  return 0;
755              }
756         }
757     }
758 
759     return -1;
760 }
761 
762 static ssize_t dp8393x_receive(NetClientState *nc, const uint8_t * buf,
763                                size_t pkt_size)
764 {
765     dp8393xState *s = qemu_get_nic_opaque(nc);
766     int packet_type;
767     uint32_t available, address;
768     int width, rx_len, padded_len;
769     uint32_t checksum;
770     int size;
771 
772     s->regs[SONIC_RCR] &= ~(SONIC_RCR_PRX | SONIC_RCR_LBK | SONIC_RCR_FAER |
773         SONIC_RCR_CRCR | SONIC_RCR_LPKT | SONIC_RCR_BC | SONIC_RCR_MC);
774 
775     if (s->last_rba_is_full) {
776         return pkt_size;
777     }
778 
779     rx_len = pkt_size + sizeof(checksum);
780     if (s->regs[SONIC_DCR] & SONIC_DCR_DW) {
781         width = 2;
782         padded_len = ((rx_len - 1) | 3) + 1;
783     } else {
784         width = 1;
785         padded_len = ((rx_len - 1) | 1) + 1;
786     }
787 
788     if (padded_len > dp8393x_rbwc(s) * 2) {
789         DPRINTF("oversize packet, pkt_size is %d\n", pkt_size);
790         s->regs[SONIC_ISR] |= SONIC_ISR_RBAE;
791         dp8393x_update_irq(s);
792         s->regs[SONIC_RCR] |= SONIC_RCR_LPKT;
793         goto done;
794     }
795 
796     packet_type = dp8393x_receive_filter(s, buf, pkt_size);
797     if (packet_type < 0) {
798         DPRINTF("packet not for netcard\n");
799         return -1;
800     }
801 
802     /* Check for EOL */
803     if (s->regs[SONIC_LLFA] & SONIC_DESC_EOL) {
804         /* Are we still in resource exhaustion? */
805         size = sizeof(uint16_t) * 1 * width;
806         address = dp8393x_crda(s) + sizeof(uint16_t) * 5 * width;
807         address_space_read(&s->as, address, MEMTXATTRS_UNSPECIFIED,
808                            s->data, size);
809         s->regs[SONIC_LLFA] = dp8393x_get(s, width, 0);
810         if (s->regs[SONIC_LLFA] & SONIC_DESC_EOL) {
811             /* Still EOL ; stop reception */
812             return -1;
813         }
814         /* Link has been updated by host */
815 
816         /* Clear in_use */
817         size = sizeof(uint16_t) * width;
818         address = dp8393x_crda(s) + sizeof(uint16_t) * 6 * width;
819         dp8393x_put(s, width, 0, 0);
820         address_space_rw(&s->as, address, MEMTXATTRS_UNSPECIFIED,
821                          (uint8_t *)s->data, size, 1);
822 
823         /* Move to next descriptor */
824         s->regs[SONIC_CRDA] = s->regs[SONIC_LLFA];
825         s->regs[SONIC_ISR] |= SONIC_ISR_PKTRX;
826     }
827 
828     /* Save current position */
829     s->regs[SONIC_TRBA1] = s->regs[SONIC_CRBA1];
830     s->regs[SONIC_TRBA0] = s->regs[SONIC_CRBA0];
831 
832     /* Calculate the ethernet checksum */
833     checksum = cpu_to_le32(crc32(0, buf, pkt_size));
834 
835     /* Put packet into RBA */
836     DPRINTF("Receive packet at %08x\n", dp8393x_crba(s));
837     address = dp8393x_crba(s);
838     address_space_write(&s->as, address, MEMTXATTRS_UNSPECIFIED,
839                         buf, pkt_size);
840     address += pkt_size;
841 
842     /* Put frame checksum into RBA */
843     address_space_write(&s->as, address, MEMTXATTRS_UNSPECIFIED,
844                         &checksum, sizeof(checksum));
845     address += sizeof(checksum);
846 
847     /* Pad short packets to keep pointers aligned */
848     if (rx_len < padded_len) {
849         size = padded_len - rx_len;
850         address_space_rw(&s->as, address, MEMTXATTRS_UNSPECIFIED,
851             (uint8_t *)"\xFF\xFF\xFF", size, 1);
852         address += size;
853     }
854 
855     s->regs[SONIC_CRBA1] = address >> 16;
856     s->regs[SONIC_CRBA0] = address & 0xffff;
857     available = dp8393x_rbwc(s);
858     available -= padded_len >> 1;
859     s->regs[SONIC_RBWC1] = available >> 16;
860     s->regs[SONIC_RBWC0] = available & 0xffff;
861 
862     /* Update status */
863     if (dp8393x_rbwc(s) < s->regs[SONIC_EOBC]) {
864         s->regs[SONIC_RCR] |= SONIC_RCR_LPKT;
865     }
866     s->regs[SONIC_RCR] |= packet_type;
867     s->regs[SONIC_RCR] |= SONIC_RCR_PRX;
868     if (s->loopback_packet) {
869         s->regs[SONIC_RCR] |= SONIC_RCR_LBK;
870         s->loopback_packet = 0;
871     }
872 
873     /* Write status to memory */
874     DPRINTF("Write status at %08x\n", dp8393x_crda(s));
875     dp8393x_put(s, width, 0, s->regs[SONIC_RCR]); /* status */
876     dp8393x_put(s, width, 1, rx_len); /* byte count */
877     dp8393x_put(s, width, 2, s->regs[SONIC_TRBA0]); /* pkt_ptr0 */
878     dp8393x_put(s, width, 3, s->regs[SONIC_TRBA1]); /* pkt_ptr1 */
879     dp8393x_put(s, width, 4, s->regs[SONIC_RSC]); /* seq_no */
880     size = sizeof(uint16_t) * 5 * width;
881     address_space_write(&s->as, dp8393x_crda(s),
882                         MEMTXATTRS_UNSPECIFIED,
883                         s->data, size);
884 
885     /* Check link field */
886     size = sizeof(uint16_t) * width;
887     address_space_read(&s->as,
888                        dp8393x_crda(s) + sizeof(uint16_t) * 5 * width,
889                        MEMTXATTRS_UNSPECIFIED, s->data, size);
890     s->regs[SONIC_LLFA] = dp8393x_get(s, width, 0);
891     if (s->regs[SONIC_LLFA] & SONIC_DESC_EOL) {
892         /* EOL detected */
893         s->regs[SONIC_ISR] |= SONIC_ISR_RDE;
894     } else {
895         /* Clear in_use */
896         size = sizeof(uint16_t) * width;
897         address = dp8393x_crda(s) + sizeof(uint16_t) * 6 * width;
898         dp8393x_put(s, width, 0, 0);
899         address_space_write(&s->as, address, MEMTXATTRS_UNSPECIFIED,
900                             s->data, size);
901 
902         /* Move to next descriptor */
903         s->regs[SONIC_CRDA] = s->regs[SONIC_LLFA];
904         s->regs[SONIC_ISR] |= SONIC_ISR_PKTRX;
905     }
906 
907     dp8393x_update_irq(s);
908 
909     s->regs[SONIC_RSC] = (s->regs[SONIC_RSC] & 0xff00) |
910                          ((s->regs[SONIC_RSC] + 1) & 0x00ff);
911 
912 done:
913 
914     if (s->regs[SONIC_RCR] & SONIC_RCR_LPKT) {
915         if (s->regs[SONIC_RRP] == s->regs[SONIC_RWP]) {
916             /* Stop packet reception */
917             s->last_rba_is_full = true;
918         } else {
919             /* Read next resource */
920             dp8393x_do_read_rra(s);
921         }
922     }
923 
924     return pkt_size;
925 }
926 
927 static void dp8393x_reset(DeviceState *dev)
928 {
929     dp8393xState *s = DP8393X(dev);
930     timer_del(s->watchdog);
931 
932     memset(s->regs, 0, sizeof(s->regs));
933     s->regs[SONIC_SR] = 0x0004; /* only revision recognized by Linux/mips */
934     s->regs[SONIC_CR] = SONIC_CR_RST | SONIC_CR_STP | SONIC_CR_RXDIS;
935     s->regs[SONIC_DCR] &= ~(SONIC_DCR_EXBUS | SONIC_DCR_LBR);
936     s->regs[SONIC_RCR] &= ~(SONIC_RCR_LB0 | SONIC_RCR_LB1 | SONIC_RCR_BRD | SONIC_RCR_RNT);
937     s->regs[SONIC_TCR] |= SONIC_TCR_NCRS | SONIC_TCR_PTX;
938     s->regs[SONIC_TCR] &= ~SONIC_TCR_BCM;
939     s->regs[SONIC_IMR] = 0;
940     s->regs[SONIC_ISR] = 0;
941     s->regs[SONIC_DCR2] = 0;
942     s->regs[SONIC_EOBC] = 0x02F8;
943     s->regs[SONIC_RSC] = 0;
944     s->regs[SONIC_CE] = 0;
945     s->regs[SONIC_RSC] = 0;
946 
947     /* Network cable is connected */
948     s->regs[SONIC_RCR] |= SONIC_RCR_CRS;
949 
950     dp8393x_update_irq(s);
951 }
952 
953 static NetClientInfo net_dp83932_info = {
954     .type = NET_CLIENT_DRIVER_NIC,
955     .size = sizeof(NICState),
956     .can_receive = dp8393x_can_receive,
957     .receive = dp8393x_receive,
958 };
959 
960 static void dp8393x_instance_init(Object *obj)
961 {
962     SysBusDevice *sbd = SYS_BUS_DEVICE(obj);
963     dp8393xState *s = DP8393X(obj);
964 
965     sysbus_init_mmio(sbd, &s->mmio);
966     sysbus_init_mmio(sbd, &s->prom);
967     sysbus_init_irq(sbd, &s->irq);
968 }
969 
970 static void dp8393x_realize(DeviceState *dev, Error **errp)
971 {
972     dp8393xState *s = DP8393X(dev);
973     int i, checksum;
974     uint8_t *prom;
975     Error *local_err = NULL;
976 
977     address_space_init(&s->as, s->dma_mr, "dp8393x");
978     memory_region_init_io(&s->mmio, OBJECT(dev), &dp8393x_ops, s,
979                           "dp8393x-regs", 0x40 << s->it_shift);
980 
981     s->nic = qemu_new_nic(&net_dp83932_info, &s->conf,
982                           object_get_typename(OBJECT(dev)), dev->id, s);
983     qemu_format_nic_info_str(qemu_get_queue(s->nic), s->conf.macaddr.a);
984 
985     s->watchdog = timer_new_ns(QEMU_CLOCK_VIRTUAL, dp8393x_watchdog, s);
986 
987     memory_region_init_rom(&s->prom, OBJECT(dev), "dp8393x-prom",
988                            SONIC_PROM_SIZE, &local_err);
989     if (local_err) {
990         error_propagate(errp, local_err);
991         return;
992     }
993     prom = memory_region_get_ram_ptr(&s->prom);
994     checksum = 0;
995     for (i = 0; i < 6; i++) {
996         prom[i] = s->conf.macaddr.a[i];
997         checksum += prom[i];
998         if (checksum > 0xff) {
999             checksum = (checksum + 1) & 0xff;
1000         }
1001     }
1002     prom[7] = 0xff - checksum;
1003 }
1004 
1005 static const VMStateDescription vmstate_dp8393x = {
1006     .name = "dp8393x",
1007     .version_id = 0,
1008     .minimum_version_id = 0,
1009     .fields = (VMStateField []) {
1010         VMSTATE_BUFFER_UNSAFE(cam, dp8393xState, 0, 16 * 6),
1011         VMSTATE_UINT16_ARRAY(regs, dp8393xState, 0x40),
1012         VMSTATE_END_OF_LIST()
1013     }
1014 };
1015 
1016 static Property dp8393x_properties[] = {
1017     DEFINE_NIC_PROPERTIES(dp8393xState, conf),
1018     DEFINE_PROP_LINK("dma_mr", dp8393xState, dma_mr,
1019                      TYPE_MEMORY_REGION, MemoryRegion *),
1020     DEFINE_PROP_UINT8("it_shift", dp8393xState, it_shift, 0),
1021     DEFINE_PROP_BOOL("big_endian", dp8393xState, big_endian, false),
1022     DEFINE_PROP_END_OF_LIST(),
1023 };
1024 
1025 static void dp8393x_class_init(ObjectClass *klass, void *data)
1026 {
1027     DeviceClass *dc = DEVICE_CLASS(klass);
1028 
1029     set_bit(DEVICE_CATEGORY_NETWORK, dc->categories);
1030     dc->realize = dp8393x_realize;
1031     dc->reset = dp8393x_reset;
1032     dc->vmsd = &vmstate_dp8393x;
1033     device_class_set_props(dc, dp8393x_properties);
1034 }
1035 
1036 static const TypeInfo dp8393x_info = {
1037     .name          = TYPE_DP8393X,
1038     .parent        = TYPE_SYS_BUS_DEVICE,
1039     .instance_size = sizeof(dp8393xState),
1040     .instance_init = dp8393x_instance_init,
1041     .class_init    = dp8393x_class_init,
1042 };
1043 
1044 static void dp8393x_register_types(void)
1045 {
1046     type_register_static(&dp8393x_info);
1047 }
1048 
1049 type_init(dp8393x_register_types)
1050