2 * QEMU NS SONIC DP8393x netcard
4 * Copyright (c) 2008-2009 Herve Poussineau
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.
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.
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/>.
20 #include "qemu/osdep.h"
22 #include "hw/sysbus.h"
23 #include "migration/vmstate.h"
25 #include "qapi/error.h"
26 #include "qemu/module.h"
27 #include "qemu/timer.h"
32 #define SONIC_PROM_SIZE 0x1000
35 #define DPRINTF(fmt, ...) \
36 do { printf("sonic: " fmt , ## __VA_ARGS__); } while (0)
37 static const char* reg_names
[] = {
38 "CR", "DCR", "RCR", "TCR", "IMR", "ISR", "UTDA", "CTDA",
39 "TPS", "TFC", "TSA0", "TSA1", "TFS", "URDA", "CRDA", "CRBA0",
40 "CRBA1", "RBWC0", "RBWC1", "EOBC", "URRA", "RSA", "REA", "RRP",
41 "RWP", "TRBA0", "TRBA1", "0x1b", "0x1c", "0x1d", "0x1e", "LLFA",
42 "TTDA", "CEP", "CAP2", "CAP1", "CAP0", "CE", "CDP", "CDC",
43 "SR", "WT0", "WT1", "RSC", "CRCT", "FAET", "MPT", "MDT",
44 "0x30", "0x31", "0x32", "0x33", "0x34", "0x35", "0x36", "0x37",
45 "0x38", "0x39", "0x3a", "0x3b", "0x3c", "0x3d", "0x3e", "DCR2" };
47 #define DPRINTF(fmt, ...) do {} while (0)
50 #define SONIC_ERROR(fmt, ...) \
51 do { printf("sonic ERROR: %s: " fmt, __func__ , ## __VA_ARGS__); } while (0)
54 #define SONIC_DCR 0x01
55 #define SONIC_RCR 0x02
56 #define SONIC_TCR 0x03
57 #define SONIC_IMR 0x04
58 #define SONIC_ISR 0x05
59 #define SONIC_UTDA 0x06
60 #define SONIC_CTDA 0x07
61 #define SONIC_TPS 0x08
62 #define SONIC_TFC 0x09
63 #define SONIC_TSA0 0x0a
64 #define SONIC_TSA1 0x0b
65 #define SONIC_TFS 0x0c
66 #define SONIC_URDA 0x0d
67 #define SONIC_CRDA 0x0e
68 #define SONIC_CRBA0 0x0f
69 #define SONIC_CRBA1 0x10
70 #define SONIC_RBWC0 0x11
71 #define SONIC_RBWC1 0x12
72 #define SONIC_EOBC 0x13
73 #define SONIC_URRA 0x14
74 #define SONIC_RSA 0x15
75 #define SONIC_REA 0x16
76 #define SONIC_RRP 0x17
77 #define SONIC_RWP 0x18
78 #define SONIC_TRBA0 0x19
79 #define SONIC_TRBA1 0x1a
80 #define SONIC_LLFA 0x1f
81 #define SONIC_TTDA 0x20
82 #define SONIC_CEP 0x21
83 #define SONIC_CAP2 0x22
84 #define SONIC_CAP1 0x23
85 #define SONIC_CAP0 0x24
87 #define SONIC_CDP 0x26
88 #define SONIC_CDC 0x27
90 #define SONIC_WT0 0x29
91 #define SONIC_WT1 0x2a
92 #define SONIC_RSC 0x2b
93 #define SONIC_CRCT 0x2c
94 #define SONIC_FAET 0x2d
95 #define SONIC_MPT 0x2e
96 #define SONIC_MDT 0x2f
97 #define SONIC_DCR2 0x3f
99 #define SONIC_CR_HTX 0x0001
100 #define SONIC_CR_TXP 0x0002
101 #define SONIC_CR_RXDIS 0x0004
102 #define SONIC_CR_RXEN 0x0008
103 #define SONIC_CR_STP 0x0010
104 #define SONIC_CR_ST 0x0020
105 #define SONIC_CR_RST 0x0080
106 #define SONIC_CR_RRRA 0x0100
107 #define SONIC_CR_LCAM 0x0200
108 #define SONIC_CR_MASK 0x03bf
110 #define SONIC_DCR_DW 0x0020
111 #define SONIC_DCR_LBR 0x2000
112 #define SONIC_DCR_EXBUS 0x8000
114 #define SONIC_RCR_PRX 0x0001
115 #define SONIC_RCR_LBK 0x0002
116 #define SONIC_RCR_FAER 0x0004
117 #define SONIC_RCR_CRCR 0x0008
118 #define SONIC_RCR_CRS 0x0020
119 #define SONIC_RCR_LPKT 0x0040
120 #define SONIC_RCR_BC 0x0080
121 #define SONIC_RCR_MC 0x0100
122 #define SONIC_RCR_LB0 0x0200
123 #define SONIC_RCR_LB1 0x0400
124 #define SONIC_RCR_AMC 0x0800
125 #define SONIC_RCR_PRO 0x1000
126 #define SONIC_RCR_BRD 0x2000
127 #define SONIC_RCR_RNT 0x4000
129 #define SONIC_TCR_PTX 0x0001
130 #define SONIC_TCR_BCM 0x0002
131 #define SONIC_TCR_FU 0x0004
132 #define SONIC_TCR_EXC 0x0040
133 #define SONIC_TCR_CRSL 0x0080
134 #define SONIC_TCR_NCRS 0x0100
135 #define SONIC_TCR_EXD 0x0400
136 #define SONIC_TCR_CRCI 0x2000
137 #define SONIC_TCR_PINT 0x8000
139 #define SONIC_ISR_RBE 0x0020
140 #define SONIC_ISR_RDE 0x0040
141 #define SONIC_ISR_TC 0x0080
142 #define SONIC_ISR_TXDN 0x0200
143 #define SONIC_ISR_PKTRX 0x0400
144 #define SONIC_ISR_PINT 0x0800
145 #define SONIC_ISR_LCD 0x1000
147 #define TYPE_DP8393X "dp8393x"
148 #define DP8393X(obj) OBJECT_CHECK(dp8393xState, (obj), TYPE_DP8393X)
150 typedef struct dp8393xState
{
151 SysBusDevice parent_obj
;
160 int64_t wt_last_update
;
171 uint8_t tx_buffer
[0x10000];
179 /* Accessor functions for values which are formed by
180 * concatenating two 16 bit device registers. By putting these
181 * in their own functions with a uint32_t return type we avoid the
182 * pitfall of implicit sign extension where ((x << 16) | y) is a
183 * signed 32 bit integer that might get sign-extended to a 64 bit integer.
185 static uint32_t dp8393x_cdp(dp8393xState
*s
)
187 return (s
->regs
[SONIC_URRA
] << 16) | s
->regs
[SONIC_CDP
];
190 static uint32_t dp8393x_crba(dp8393xState
*s
)
192 return (s
->regs
[SONIC_CRBA1
] << 16) | s
->regs
[SONIC_CRBA0
];
195 static uint32_t dp8393x_crda(dp8393xState
*s
)
197 return (s
->regs
[SONIC_URDA
] << 16) | s
->regs
[SONIC_CRDA
];
200 static uint32_t dp8393x_rbwc(dp8393xState
*s
)
202 return (s
->regs
[SONIC_RBWC1
] << 16) | s
->regs
[SONIC_RBWC0
];
205 static uint32_t dp8393x_rrp(dp8393xState
*s
)
207 return (s
->regs
[SONIC_URRA
] << 16) | s
->regs
[SONIC_RRP
];
210 static uint32_t dp8393x_tsa(dp8393xState
*s
)
212 return (s
->regs
[SONIC_TSA1
] << 16) | s
->regs
[SONIC_TSA0
];
215 static uint32_t dp8393x_ttda(dp8393xState
*s
)
217 return (s
->regs
[SONIC_UTDA
] << 16) | s
->regs
[SONIC_TTDA
];
220 static uint32_t dp8393x_wt(dp8393xState
*s
)
222 return s
->regs
[SONIC_WT1
] << 16 | s
->regs
[SONIC_WT0
];
225 static void dp8393x_update_irq(dp8393xState
*s
)
227 int level
= (s
->regs
[SONIC_IMR
] & s
->regs
[SONIC_ISR
]) ? 1 : 0;
230 if (level
!= s
->irq_level
) {
231 s
->irq_level
= level
;
233 DPRINTF("raise irq, isr is 0x%04x\n", s
->regs
[SONIC_ISR
]);
235 DPRINTF("lower irq\n");
240 qemu_set_irq(s
->irq
, level
);
243 static void dp8393x_do_load_cam(dp8393xState
*s
)
249 width
= (s
->regs
[SONIC_DCR
] & SONIC_DCR_DW
) ? 2 : 1;
250 size
= sizeof(uint16_t) * 4 * width
;
252 while (s
->regs
[SONIC_CDC
] & 0x1f) {
253 /* Fill current entry */
254 address_space_rw(&s
->as
, dp8393x_cdp(s
),
255 MEMTXATTRS_UNSPECIFIED
, (uint8_t *)data
, size
, 0);
256 s
->cam
[index
][0] = data
[1 * width
] & 0xff;
257 s
->cam
[index
][1] = data
[1 * width
] >> 8;
258 s
->cam
[index
][2] = data
[2 * width
] & 0xff;
259 s
->cam
[index
][3] = data
[2 * width
] >> 8;
260 s
->cam
[index
][4] = data
[3 * width
] & 0xff;
261 s
->cam
[index
][5] = data
[3 * width
] >> 8;
262 DPRINTF("load cam[%d] with %02x%02x%02x%02x%02x%02x\n", index
,
263 s
->cam
[index
][0], s
->cam
[index
][1], s
->cam
[index
][2],
264 s
->cam
[index
][3], s
->cam
[index
][4], s
->cam
[index
][5]);
265 /* Move to next entry */
266 s
->regs
[SONIC_CDC
]--;
267 s
->regs
[SONIC_CDP
] += size
;
271 /* Read CAM enable */
272 address_space_rw(&s
->as
, dp8393x_cdp(s
),
273 MEMTXATTRS_UNSPECIFIED
, (uint8_t *)data
, size
, 0);
274 s
->regs
[SONIC_CE
] = data
[0 * width
];
275 DPRINTF("load cam done. cam enable mask 0x%04x\n", s
->regs
[SONIC_CE
]);
278 s
->regs
[SONIC_CR
] &= ~SONIC_CR_LCAM
;
279 s
->regs
[SONIC_ISR
] |= SONIC_ISR_LCD
;
280 dp8393x_update_irq(s
);
283 static void dp8393x_do_read_rra(dp8393xState
*s
)
289 width
= (s
->regs
[SONIC_DCR
] & SONIC_DCR_DW
) ? 2 : 1;
290 size
= sizeof(uint16_t) * 4 * width
;
291 address_space_rw(&s
->as
, dp8393x_rrp(s
),
292 MEMTXATTRS_UNSPECIFIED
, (uint8_t *)data
, size
, 0);
294 /* Update SONIC registers */
295 s
->regs
[SONIC_CRBA0
] = data
[0 * width
];
296 s
->regs
[SONIC_CRBA1
] = data
[1 * width
];
297 s
->regs
[SONIC_RBWC0
] = data
[2 * width
];
298 s
->regs
[SONIC_RBWC1
] = data
[3 * width
];
299 DPRINTF("CRBA0/1: 0x%04x/0x%04x, RBWC0/1: 0x%04x/0x%04x\n",
300 s
->regs
[SONIC_CRBA0
], s
->regs
[SONIC_CRBA1
],
301 s
->regs
[SONIC_RBWC0
], s
->regs
[SONIC_RBWC1
]);
303 /* Go to next entry */
304 s
->regs
[SONIC_RRP
] += size
;
307 if (s
->regs
[SONIC_RRP
] == s
->regs
[SONIC_REA
]) {
308 s
->regs
[SONIC_RRP
] = s
->regs
[SONIC_RSA
];
311 /* Check resource exhaustion */
312 if (s
->regs
[SONIC_RRP
] == s
->regs
[SONIC_RWP
])
314 s
->regs
[SONIC_ISR
] |= SONIC_ISR_RBE
;
315 dp8393x_update_irq(s
);
319 s
->regs
[SONIC_CR
] &= ~SONIC_CR_RRRA
;
322 static void dp8393x_do_software_reset(dp8393xState
*s
)
324 timer_del(s
->watchdog
);
326 s
->regs
[SONIC_CR
] &= ~(SONIC_CR_LCAM
| SONIC_CR_RRRA
| SONIC_CR_TXP
| SONIC_CR_HTX
);
327 s
->regs
[SONIC_CR
] |= SONIC_CR_RST
| SONIC_CR_RXDIS
;
330 static void dp8393x_set_next_tick(dp8393xState
*s
)
335 if (s
->regs
[SONIC_CR
] & SONIC_CR_STP
) {
336 timer_del(s
->watchdog
);
340 ticks
= dp8393x_wt(s
);
341 s
->wt_last_update
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
342 delay
= NANOSECONDS_PER_SECOND
* ticks
/ 5000000;
343 timer_mod(s
->watchdog
, s
->wt_last_update
+ delay
);
346 static void dp8393x_update_wt_regs(dp8393xState
*s
)
351 if (s
->regs
[SONIC_CR
] & SONIC_CR_STP
) {
352 timer_del(s
->watchdog
);
356 elapsed
= s
->wt_last_update
- qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
358 val
-= elapsed
/ 5000000;
359 s
->regs
[SONIC_WT1
] = (val
>> 16) & 0xffff;
360 s
->regs
[SONIC_WT0
] = (val
>> 0) & 0xffff;
361 dp8393x_set_next_tick(s
);
365 static void dp8393x_do_start_timer(dp8393xState
*s
)
367 s
->regs
[SONIC_CR
] &= ~SONIC_CR_STP
;
368 dp8393x_set_next_tick(s
);
371 static void dp8393x_do_stop_timer(dp8393xState
*s
)
373 s
->regs
[SONIC_CR
] &= ~SONIC_CR_ST
;
374 dp8393x_update_wt_regs(s
);
377 static int dp8393x_can_receive(NetClientState
*nc
);
379 static void dp8393x_do_receiver_enable(dp8393xState
*s
)
381 s
->regs
[SONIC_CR
] &= ~SONIC_CR_RXDIS
;
382 if (dp8393x_can_receive(s
->nic
->ncs
)) {
383 qemu_flush_queued_packets(qemu_get_queue(s
->nic
));
387 static void dp8393x_do_receiver_disable(dp8393xState
*s
)
389 s
->regs
[SONIC_CR
] &= ~SONIC_CR_RXEN
;
392 static void dp8393x_do_transmit_packets(dp8393xState
*s
)
394 NetClientState
*nc
= qemu_get_queue(s
->nic
);
400 width
= (s
->regs
[SONIC_DCR
] & SONIC_DCR_DW
) ? 2 : 1;
404 size
= sizeof(uint16_t) * 6 * width
;
405 s
->regs
[SONIC_TTDA
] = s
->regs
[SONIC_CTDA
];
406 DPRINTF("Transmit packet at %08x\n", dp8393x_ttda(s
));
407 address_space_rw(&s
->as
,
408 dp8393x_ttda(s
) + sizeof(uint16_t) * width
,
409 MEMTXATTRS_UNSPECIFIED
, (uint8_t *)data
, size
, 0);
412 /* Update registers */
413 s
->regs
[SONIC_TCR
] = data
[0 * width
] & 0xf000;
414 s
->regs
[SONIC_TPS
] = data
[1 * width
];
415 s
->regs
[SONIC_TFC
] = data
[2 * width
];
416 s
->regs
[SONIC_TSA0
] = data
[3 * width
];
417 s
->regs
[SONIC_TSA1
] = data
[4 * width
];
418 s
->regs
[SONIC_TFS
] = data
[5 * width
];
420 /* Handle programmable interrupt */
421 if (s
->regs
[SONIC_TCR
] & SONIC_TCR_PINT
) {
422 s
->regs
[SONIC_ISR
] |= SONIC_ISR_PINT
;
424 s
->regs
[SONIC_ISR
] &= ~SONIC_ISR_PINT
;
427 for (i
= 0; i
< s
->regs
[SONIC_TFC
]; ) {
428 /* Append fragment */
429 len
= s
->regs
[SONIC_TFS
];
430 if (tx_len
+ len
> sizeof(s
->tx_buffer
)) {
431 len
= sizeof(s
->tx_buffer
) - tx_len
;
433 address_space_rw(&s
->as
, dp8393x_tsa(s
),
434 MEMTXATTRS_UNSPECIFIED
, &s
->tx_buffer
[tx_len
], len
, 0);
438 if (i
!= s
->regs
[SONIC_TFC
]) {
439 /* Read next fragment details */
440 size
= sizeof(uint16_t) * 3 * width
;
441 address_space_rw(&s
->as
,
442 dp8393x_ttda(s
) + sizeof(uint16_t) * (4 + 3 * i
) * width
,
443 MEMTXATTRS_UNSPECIFIED
, (uint8_t *)data
, size
, 0);
444 s
->regs
[SONIC_TSA0
] = data
[0 * width
];
445 s
->regs
[SONIC_TSA1
] = data
[1 * width
];
446 s
->regs
[SONIC_TFS
] = data
[2 * width
];
450 /* Handle Ethernet checksum */
451 if (!(s
->regs
[SONIC_TCR
] & SONIC_TCR_CRCI
)) {
452 /* Don't append FCS there, to look like slirp packets
453 * which don't have one */
455 /* Remove existing FCS */
459 if (s
->regs
[SONIC_RCR
] & (SONIC_RCR_LB1
| SONIC_RCR_LB0
)) {
461 s
->regs
[SONIC_TCR
] |= SONIC_TCR_CRSL
;
462 if (nc
->info
->can_receive(nc
)) {
463 s
->loopback_packet
= 1;
464 nc
->info
->receive(nc
, s
->tx_buffer
, tx_len
);
467 /* Transmit packet */
468 qemu_send_packet(nc
, s
->tx_buffer
, tx_len
);
470 s
->regs
[SONIC_TCR
] |= SONIC_TCR_PTX
;
473 data
[0 * width
] = s
->regs
[SONIC_TCR
] & 0x0fff; /* status */
474 size
= sizeof(uint16_t) * width
;
475 address_space_rw(&s
->as
,
477 MEMTXATTRS_UNSPECIFIED
, (uint8_t *)data
, size
, 1);
479 if (!(s
->regs
[SONIC_CR
] & SONIC_CR_HTX
)) {
480 /* Read footer of packet */
481 size
= sizeof(uint16_t) * width
;
482 address_space_rw(&s
->as
,
485 (4 + 3 * s
->regs
[SONIC_TFC
]) * width
,
486 MEMTXATTRS_UNSPECIFIED
, (uint8_t *)data
, size
, 0);
487 s
->regs
[SONIC_CTDA
] = data
[0 * width
] & ~0x1;
488 if (data
[0 * width
] & 0x1) {
496 s
->regs
[SONIC_CR
] &= ~SONIC_CR_TXP
;
497 s
->regs
[SONIC_ISR
] |= SONIC_ISR_TXDN
;
498 dp8393x_update_irq(s
);
501 static void dp8393x_do_halt_transmission(dp8393xState
*s
)
506 static void dp8393x_do_command(dp8393xState
*s
, uint16_t command
)
508 if ((s
->regs
[SONIC_CR
] & SONIC_CR_RST
) && !(command
& SONIC_CR_RST
)) {
509 s
->regs
[SONIC_CR
] &= ~SONIC_CR_RST
;
513 s
->regs
[SONIC_CR
] |= (command
& SONIC_CR_MASK
);
515 if (command
& SONIC_CR_HTX
)
516 dp8393x_do_halt_transmission(s
);
517 if (command
& SONIC_CR_TXP
)
518 dp8393x_do_transmit_packets(s
);
519 if (command
& SONIC_CR_RXDIS
)
520 dp8393x_do_receiver_disable(s
);
521 if (command
& SONIC_CR_RXEN
)
522 dp8393x_do_receiver_enable(s
);
523 if (command
& SONIC_CR_STP
)
524 dp8393x_do_stop_timer(s
);
525 if (command
& SONIC_CR_ST
)
526 dp8393x_do_start_timer(s
);
527 if (command
& SONIC_CR_RST
)
528 dp8393x_do_software_reset(s
);
529 if (command
& SONIC_CR_RRRA
)
530 dp8393x_do_read_rra(s
);
531 if (command
& SONIC_CR_LCAM
)
532 dp8393x_do_load_cam(s
);
535 static uint64_t dp8393x_read(void *opaque
, hwaddr addr
, unsigned int size
)
537 dp8393xState
*s
= opaque
;
538 int reg
= addr
>> s
->it_shift
;
542 /* Update data before reading it */
545 dp8393x_update_wt_regs(s
);
548 /* Accept read to some registers only when in reset mode */
552 if (s
->regs
[SONIC_CR
] & SONIC_CR_RST
) {
553 val
= s
->cam
[s
->regs
[SONIC_CEP
] & 0xf][2* (SONIC_CAP0
- reg
) + 1] << 8;
554 val
|= s
->cam
[s
->regs
[SONIC_CEP
] & 0xf][2* (SONIC_CAP0
- reg
)];
557 /* All other registers have no special contrainst */
562 DPRINTF("read 0x%04x from reg %s\n", val
, reg_names
[reg
]);
567 static void dp8393x_write(void *opaque
, hwaddr addr
, uint64_t data
,
570 dp8393xState
*s
= opaque
;
571 int reg
= addr
>> s
->it_shift
;
573 DPRINTF("write 0x%04x to reg %s\n", (uint16_t)data
, reg_names
[reg
]);
576 /* Command register */
578 dp8393x_do_command(s
, data
);
580 /* Prevent write to read-only registers */
586 DPRINTF("writing to reg %d invalid\n", reg
);
588 /* Accept write to some registers only when in reset mode */
590 if (s
->regs
[SONIC_CR
] & SONIC_CR_RST
) {
591 s
->regs
[reg
] = data
& 0xbfff;
593 DPRINTF("writing to DCR invalid\n");
597 if (s
->regs
[SONIC_CR
] & SONIC_CR_RST
) {
598 s
->regs
[reg
] = data
& 0xf017;
600 DPRINTF("writing to DCR2 invalid\n");
603 /* 12 lower bytes are Read Only */
605 s
->regs
[reg
] = data
& 0xf000;
607 /* 9 lower bytes are Read Only */
609 s
->regs
[reg
] = data
& 0xffe0;
611 /* Ignore most significant bit */
613 s
->regs
[reg
] = data
& 0x7fff;
614 dp8393x_update_irq(s
);
616 /* Clear bits by writing 1 to them */
618 data
&= s
->regs
[reg
];
619 s
->regs
[reg
] &= ~data
;
620 if (data
& SONIC_ISR_RBE
) {
621 dp8393x_do_read_rra(s
);
623 dp8393x_update_irq(s
);
624 if (dp8393x_can_receive(s
->nic
->ncs
)) {
625 qemu_flush_queued_packets(qemu_get_queue(s
->nic
));
628 /* Ignore least significant bit */
633 s
->regs
[reg
] = data
& 0xfffe;
635 /* Invert written value for some registers */
639 s
->regs
[reg
] = data
^ 0xffff;
641 /* All other registers have no special contrainst */
646 if (reg
== SONIC_WT0
|| reg
== SONIC_WT1
) {
647 dp8393x_set_next_tick(s
);
651 static const MemoryRegionOps dp8393x_ops
= {
652 .read
= dp8393x_read
,
653 .write
= dp8393x_write
,
654 .impl
.min_access_size
= 2,
655 .impl
.max_access_size
= 2,
656 .endianness
= DEVICE_NATIVE_ENDIAN
,
659 static void dp8393x_watchdog(void *opaque
)
661 dp8393xState
*s
= opaque
;
663 if (s
->regs
[SONIC_CR
] & SONIC_CR_STP
) {
667 s
->regs
[SONIC_WT1
] = 0xffff;
668 s
->regs
[SONIC_WT0
] = 0xffff;
669 dp8393x_set_next_tick(s
);
671 /* Signal underflow */
672 s
->regs
[SONIC_ISR
] |= SONIC_ISR_TC
;
673 dp8393x_update_irq(s
);
676 static int dp8393x_can_receive(NetClientState
*nc
)
678 dp8393xState
*s
= qemu_get_nic_opaque(nc
);
680 if (!(s
->regs
[SONIC_CR
] & SONIC_CR_RXEN
))
682 if (s
->regs
[SONIC_ISR
] & SONIC_ISR_RBE
)
687 static int dp8393x_receive_filter(dp8393xState
*s
, const uint8_t * buf
,
690 static const uint8_t bcast
[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
693 /* Check promiscuous mode */
694 if ((s
->regs
[SONIC_RCR
] & SONIC_RCR_PRO
) && (buf
[0] & 1) == 0) {
698 /* Check multicast packets */
699 if ((s
->regs
[SONIC_RCR
] & SONIC_RCR_AMC
) && (buf
[0] & 1) == 1) {
703 /* Check broadcast */
704 if ((s
->regs
[SONIC_RCR
] & SONIC_RCR_BRD
) && !memcmp(buf
, bcast
, sizeof(bcast
))) {
709 for (i
= 0; i
< 16; i
++) {
710 if (s
->regs
[SONIC_CE
] & (1 << i
)) {
712 if (!memcmp(buf
, s
->cam
[i
], sizeof(s
->cam
[i
]))) {
721 static ssize_t
dp8393x_receive(NetClientState
*nc
, const uint8_t * buf
,
724 dp8393xState
*s
= qemu_get_nic_opaque(nc
);
727 uint32_t available
, address
;
728 int width
, rx_len
= size
;
731 width
= (s
->regs
[SONIC_DCR
] & SONIC_DCR_DW
) ? 2 : 1;
733 s
->regs
[SONIC_RCR
] &= ~(SONIC_RCR_PRX
| SONIC_RCR_LBK
| SONIC_RCR_FAER
|
734 SONIC_RCR_CRCR
| SONIC_RCR_LPKT
| SONIC_RCR_BC
| SONIC_RCR_MC
);
736 packet_type
= dp8393x_receive_filter(s
, buf
, size
);
737 if (packet_type
< 0) {
738 DPRINTF("packet not for netcard\n");
742 /* XXX: Check byte ordering */
745 if (s
->regs
[SONIC_LLFA
] & 0x1) {
746 /* Are we still in resource exhaustion? */
747 size
= sizeof(uint16_t) * 1 * width
;
748 address
= dp8393x_crda(s
) + sizeof(uint16_t) * 5 * width
;
749 address_space_rw(&s
->as
, address
, MEMTXATTRS_UNSPECIFIED
,
750 (uint8_t *)data
, size
, 0);
751 if (data
[0 * width
] & 0x1) {
752 /* Still EOL ; stop reception */
755 s
->regs
[SONIC_CRDA
] = s
->regs
[SONIC_LLFA
];
759 /* Save current position */
760 s
->regs
[SONIC_TRBA1
] = s
->regs
[SONIC_CRBA1
];
761 s
->regs
[SONIC_TRBA0
] = s
->regs
[SONIC_CRBA0
];
763 /* Calculate the ethernet checksum */
764 checksum
= cpu_to_le32(crc32(0, buf
, rx_len
));
766 /* Put packet into RBA */
767 DPRINTF("Receive packet at %08x\n", dp8393x_crba(s
));
768 address
= dp8393x_crba(s
);
769 address_space_rw(&s
->as
, address
,
770 MEMTXATTRS_UNSPECIFIED
, (uint8_t *)buf
, rx_len
, 1);
772 address_space_rw(&s
->as
, address
,
773 MEMTXATTRS_UNSPECIFIED
, (uint8_t *)&checksum
, 4, 1);
775 s
->regs
[SONIC_CRBA1
] = address
>> 16;
776 s
->regs
[SONIC_CRBA0
] = address
& 0xffff;
777 available
= dp8393x_rbwc(s
);
778 available
-= rx_len
/ 2;
779 s
->regs
[SONIC_RBWC1
] = available
>> 16;
780 s
->regs
[SONIC_RBWC0
] = available
& 0xffff;
783 if (dp8393x_rbwc(s
) < s
->regs
[SONIC_EOBC
]) {
784 s
->regs
[SONIC_RCR
] |= SONIC_RCR_LPKT
;
786 s
->regs
[SONIC_RCR
] |= packet_type
;
787 s
->regs
[SONIC_RCR
] |= SONIC_RCR_PRX
;
788 if (s
->loopback_packet
) {
789 s
->regs
[SONIC_RCR
] |= SONIC_RCR_LBK
;
790 s
->loopback_packet
= 0;
793 /* Write status to memory */
794 DPRINTF("Write status at %08x\n", dp8393x_crda(s
));
795 data
[0 * width
] = s
->regs
[SONIC_RCR
]; /* status */
796 data
[1 * width
] = rx_len
; /* byte count */
797 data
[2 * width
] = s
->regs
[SONIC_TRBA0
]; /* pkt_ptr0 */
798 data
[3 * width
] = s
->regs
[SONIC_TRBA1
]; /* pkt_ptr1 */
799 data
[4 * width
] = s
->regs
[SONIC_RSC
]; /* seq_no */
800 size
= sizeof(uint16_t) * 5 * width
;
801 address_space_rw(&s
->as
, dp8393x_crda(s
),
802 MEMTXATTRS_UNSPECIFIED
, (uint8_t *)data
, size
, 1);
804 /* Move to next descriptor */
805 size
= sizeof(uint16_t) * width
;
806 address_space_rw(&s
->as
, dp8393x_crda(s
) + sizeof(uint16_t) * 5 * width
,
807 MEMTXATTRS_UNSPECIFIED
, (uint8_t *)data
, size
, 0);
808 s
->regs
[SONIC_LLFA
] = data
[0 * width
];
809 if (s
->regs
[SONIC_LLFA
] & 0x1) {
811 s
->regs
[SONIC_ISR
] |= SONIC_ISR_RDE
;
813 data
[0 * width
] = 0; /* in_use */
814 address_space_rw(&s
->as
, dp8393x_crda(s
) + sizeof(uint16_t) * 6 * width
,
815 MEMTXATTRS_UNSPECIFIED
, (uint8_t *)data
, sizeof(uint16_t), 1);
816 s
->regs
[SONIC_CRDA
] = s
->regs
[SONIC_LLFA
];
817 s
->regs
[SONIC_ISR
] |= SONIC_ISR_PKTRX
;
818 s
->regs
[SONIC_RSC
] = (s
->regs
[SONIC_RSC
] & 0xff00) | (((s
->regs
[SONIC_RSC
] & 0x00ff) + 1) & 0x00ff);
820 if (s
->regs
[SONIC_RCR
] & SONIC_RCR_LPKT
) {
822 dp8393x_do_read_rra(s
);
827 dp8393x_update_irq(s
);
832 static void dp8393x_reset(DeviceState
*dev
)
834 dp8393xState
*s
= DP8393X(dev
);
835 timer_del(s
->watchdog
);
837 memset(s
->regs
, 0, sizeof(s
->regs
));
838 s
->regs
[SONIC_CR
] = SONIC_CR_RST
| SONIC_CR_STP
| SONIC_CR_RXDIS
;
839 s
->regs
[SONIC_DCR
] &= ~(SONIC_DCR_EXBUS
| SONIC_DCR_LBR
);
840 s
->regs
[SONIC_RCR
] &= ~(SONIC_RCR_LB0
| SONIC_RCR_LB1
| SONIC_RCR_BRD
| SONIC_RCR_RNT
);
841 s
->regs
[SONIC_TCR
] |= SONIC_TCR_NCRS
| SONIC_TCR_PTX
;
842 s
->regs
[SONIC_TCR
] &= ~SONIC_TCR_BCM
;
843 s
->regs
[SONIC_IMR
] = 0;
844 s
->regs
[SONIC_ISR
] = 0;
845 s
->regs
[SONIC_DCR2
] = 0;
846 s
->regs
[SONIC_EOBC
] = 0x02F8;
847 s
->regs
[SONIC_RSC
] = 0;
848 s
->regs
[SONIC_CE
] = 0;
849 s
->regs
[SONIC_RSC
] = 0;
851 /* Network cable is connected */
852 s
->regs
[SONIC_RCR
] |= SONIC_RCR_CRS
;
854 dp8393x_update_irq(s
);
857 static NetClientInfo net_dp83932_info
= {
858 .type
= NET_CLIENT_DRIVER_NIC
,
859 .size
= sizeof(NICState
),
860 .can_receive
= dp8393x_can_receive
,
861 .receive
= dp8393x_receive
,
864 static void dp8393x_instance_init(Object
*obj
)
866 SysBusDevice
*sbd
= SYS_BUS_DEVICE(obj
);
867 dp8393xState
*s
= DP8393X(obj
);
869 sysbus_init_mmio(sbd
, &s
->mmio
);
870 sysbus_init_mmio(sbd
, &s
->prom
);
871 sysbus_init_irq(sbd
, &s
->irq
);
874 static void dp8393x_realize(DeviceState
*dev
, Error
**errp
)
876 dp8393xState
*s
= DP8393X(dev
);
879 Error
*local_err
= NULL
;
881 address_space_init(&s
->as
, s
->dma_mr
, "dp8393x");
882 memory_region_init_io(&s
->mmio
, OBJECT(dev
), &dp8393x_ops
, s
,
883 "dp8393x-regs", 0x40 << s
->it_shift
);
885 s
->nic
= qemu_new_nic(&net_dp83932_info
, &s
->conf
,
886 object_get_typename(OBJECT(dev
)), dev
->id
, s
);
887 qemu_format_nic_info_str(qemu_get_queue(s
->nic
), s
->conf
.macaddr
.a
);
889 s
->watchdog
= timer_new_ns(QEMU_CLOCK_VIRTUAL
, dp8393x_watchdog
, s
);
890 s
->regs
[SONIC_SR
] = 0x0004; /* only revision recognized by Linux */
892 memory_region_init_ram(&s
->prom
, OBJECT(dev
),
893 "dp8393x-prom", SONIC_PROM_SIZE
, &local_err
);
895 error_propagate(errp
, local_err
);
898 memory_region_set_readonly(&s
->prom
, true);
899 prom
= memory_region_get_ram_ptr(&s
->prom
);
901 for (i
= 0; i
< 6; i
++) {
902 prom
[i
] = s
->conf
.macaddr
.a
[i
];
904 if (checksum
> 0xff) {
905 checksum
= (checksum
+ 1) & 0xff;
908 prom
[7] = 0xff - checksum
;
911 static const VMStateDescription vmstate_dp8393x
= {
914 .minimum_version_id
= 0,
915 .fields
= (VMStateField
[]) {
916 VMSTATE_BUFFER_UNSAFE(cam
, dp8393xState
, 0, 16 * 6),
917 VMSTATE_UINT16_ARRAY(regs
, dp8393xState
, 0x40),
918 VMSTATE_END_OF_LIST()
922 static Property dp8393x_properties
[] = {
923 DEFINE_NIC_PROPERTIES(dp8393xState
, conf
),
924 DEFINE_PROP_PTR("dma_mr", dp8393xState
, dma_mr
),
925 DEFINE_PROP_UINT8("it_shift", dp8393xState
, it_shift
, 0),
926 DEFINE_PROP_END_OF_LIST(),
929 static void dp8393x_class_init(ObjectClass
*klass
, void *data
)
931 DeviceClass
*dc
= DEVICE_CLASS(klass
);
933 set_bit(DEVICE_CATEGORY_NETWORK
, dc
->categories
);
934 dc
->realize
= dp8393x_realize
;
935 dc
->reset
= dp8393x_reset
;
936 dc
->vmsd
= &vmstate_dp8393x
;
937 dc
->props
= dp8393x_properties
;
938 /* Reason: dma_mr property can't be set */
939 dc
->user_creatable
= false;
942 static const TypeInfo dp8393x_info
= {
943 .name
= TYPE_DP8393X
,
944 .parent
= TYPE_SYS_BUS_DEVICE
,
945 .instance_size
= sizeof(dp8393xState
),
946 .instance_init
= dp8393x_instance_init
,
947 .class_init
= dp8393x_class_init
,
950 static void dp8393x_register_types(void)
952 type_register_static(&dp8393x_info
);
955 type_init(dp8393x_register_types
)