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/qdev-properties.h"
23 #include "hw/sysbus.h"
24 #include "migration/vmstate.h"
26 #include "qapi/error.h"
27 #include "qemu/module.h"
28 #include "qemu/timer.h"
33 #define SONIC_PROM_SIZE 0x1000
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" };
48 #define DPRINTF(fmt, ...) do {} while (0)
51 #define SONIC_ERROR(fmt, ...) \
52 do { printf("sonic ERROR: %s: " fmt, __func__ , ## __VA_ARGS__); } while (0)
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
88 #define SONIC_CDP 0x26
89 #define SONIC_CDC 0x27
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
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
111 #define SONIC_DCR_DW 0x0020
112 #define SONIC_DCR_LBR 0x2000
113 #define SONIC_DCR_EXBUS 0x8000
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
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
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
149 #define SONIC_DESC_EOL 0x0001
150 #define SONIC_DESC_ADDR 0xFFFE
152 #define TYPE_DP8393X "dp8393x"
153 #define DP8393X(obj) OBJECT_CHECK(dp8393xState, (obj), TYPE_DP8393X)
155 typedef struct dp8393xState
{
156 SysBusDevice parent_obj
;
161 bool last_rba_is_full
;
167 int64_t wt_last_update
;
178 uint8_t tx_buffer
[0x10000];
183 MemoryRegion
*dma_mr
;
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.
193 static uint32_t dp8393x_cdp(dp8393xState
*s
)
195 return (s
->regs
[SONIC_URRA
] << 16) | s
->regs
[SONIC_CDP
];
198 static uint32_t dp8393x_crba(dp8393xState
*s
)
200 return (s
->regs
[SONIC_CRBA1
] << 16) | s
->regs
[SONIC_CRBA0
];
203 static uint32_t dp8393x_crda(dp8393xState
*s
)
205 return (s
->regs
[SONIC_URDA
] << 16) |
206 (s
->regs
[SONIC_CRDA
] & SONIC_DESC_ADDR
);
209 static uint32_t dp8393x_rbwc(dp8393xState
*s
)
211 return (s
->regs
[SONIC_RBWC1
] << 16) | s
->regs
[SONIC_RBWC0
];
214 static uint32_t dp8393x_rrp(dp8393xState
*s
)
216 return (s
->regs
[SONIC_URRA
] << 16) | s
->regs
[SONIC_RRP
];
219 static uint32_t dp8393x_tsa(dp8393xState
*s
)
221 return (s
->regs
[SONIC_TSA1
] << 16) | s
->regs
[SONIC_TSA0
];
224 static uint32_t dp8393x_ttda(dp8393xState
*s
)
226 return (s
->regs
[SONIC_UTDA
] << 16) |
227 (s
->regs
[SONIC_TTDA
] & SONIC_DESC_ADDR
);
230 static uint32_t dp8393x_wt(dp8393xState
*s
)
232 return s
->regs
[SONIC_WT1
] << 16 | s
->regs
[SONIC_WT0
];
235 static uint16_t dp8393x_get(dp8393xState
*s
, int width
, int offset
)
240 val
= be16_to_cpu(s
->data
[offset
* width
+ width
- 1]);
242 val
= le16_to_cpu(s
->data
[offset
* width
]);
247 static void dp8393x_put(dp8393xState
*s
, int width
, int offset
,
252 s
->data
[offset
* 2] = 0;
253 s
->data
[offset
* 2 + 1] = cpu_to_be16(val
);
255 s
->data
[offset
] = cpu_to_be16(val
);
259 s
->data
[offset
* 2] = cpu_to_le16(val
);
260 s
->data
[offset
* 2 + 1] = 0;
262 s
->data
[offset
] = cpu_to_le16(val
);
267 static void dp8393x_update_irq(dp8393xState
*s
)
269 int level
= (s
->regs
[SONIC_IMR
] & s
->regs
[SONIC_ISR
]) ? 1 : 0;
272 if (level
!= s
->irq_level
) {
273 s
->irq_level
= level
;
275 DPRINTF("raise irq, isr is 0x%04x\n", s
->regs
[SONIC_ISR
]);
277 DPRINTF("lower irq\n");
282 qemu_set_irq(s
->irq
, level
);
285 static void dp8393x_do_load_cam(dp8393xState
*s
)
290 width
= (s
->regs
[SONIC_DCR
] & SONIC_DCR_DW
) ? 2 : 1;
291 size
= sizeof(uint16_t) * 4 * width
;
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
;
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
]);
319 s
->regs
[SONIC_CR
] &= ~SONIC_CR_LCAM
;
320 s
->regs
[SONIC_ISR
] |= SONIC_ISR_LCD
;
321 dp8393x_update_irq(s
);
324 static void dp8393x_do_read_rra(dp8393xState
*s
)
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
);
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
]);
343 /* Go to next entry */
344 s
->regs
[SONIC_RRP
] += size
;
347 if (s
->regs
[SONIC_RRP
] == s
->regs
[SONIC_REA
]) {
348 s
->regs
[SONIC_RRP
] = s
->regs
[SONIC_RSA
];
351 /* Warn the host if CRBA now has the last available resource */
352 if (s
->regs
[SONIC_RRP
] == s
->regs
[SONIC_RWP
])
354 s
->regs
[SONIC_ISR
] |= SONIC_ISR_RBE
;
355 dp8393x_update_irq(s
);
358 /* Allow packet reception */
359 s
->last_rba_is_full
= false;
362 static void dp8393x_do_software_reset(dp8393xState
*s
)
364 timer_del(s
->watchdog
);
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
;
370 static void dp8393x_set_next_tick(dp8393xState
*s
)
375 if (s
->regs
[SONIC_CR
] & SONIC_CR_STP
) {
376 timer_del(s
->watchdog
);
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
);
386 static void dp8393x_update_wt_regs(dp8393xState
*s
)
391 if (s
->regs
[SONIC_CR
] & SONIC_CR_STP
) {
392 timer_del(s
->watchdog
);
396 elapsed
= s
->wt_last_update
- qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
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
);
405 static void dp8393x_do_start_timer(dp8393xState
*s
)
407 s
->regs
[SONIC_CR
] &= ~SONIC_CR_STP
;
408 dp8393x_set_next_tick(s
);
411 static void dp8393x_do_stop_timer(dp8393xState
*s
)
413 s
->regs
[SONIC_CR
] &= ~SONIC_CR_ST
;
414 dp8393x_update_wt_regs(s
);
417 static bool dp8393x_can_receive(NetClientState
*nc
);
419 static void dp8393x_do_receiver_enable(dp8393xState
*s
)
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
));
427 static void dp8393x_do_receiver_disable(dp8393xState
*s
)
429 s
->regs
[SONIC_CR
] &= ~SONIC_CR_RXEN
;
432 static void dp8393x_do_transmit_packets(dp8393xState
*s
)
434 NetClientState
*nc
= qemu_get_queue(s
->nic
);
439 width
= (s
->regs
[SONIC_DCR
] & SONIC_DCR_DW
) ? 2 : 1;
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
);
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);
458 /* Handle programmable interrupt */
459 if (s
->regs
[SONIC_TCR
] & SONIC_TCR_PINT
) {
460 s
->regs
[SONIC_ISR
] |= SONIC_ISR_PINT
;
462 s
->regs
[SONIC_ISR
] &= ~SONIC_ISR_PINT
;
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
;
471 address_space_read(&s
->as
, dp8393x_tsa(s
), MEMTXATTRS_UNSPECIFIED
,
472 &s
->tx_buffer
[tx_len
], len
);
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
,
481 + sizeof(uint16_t) * width
* (4 + 3 * i
),
482 MEMTXATTRS_UNSPECIFIED
, s
->data
,
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);
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 */
495 /* Remove existing FCS */
499 if (s
->regs
[SONIC_RCR
] & (SONIC_RCR_LB1
| SONIC_RCR_LB0
)) {
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
);
507 /* Transmit packet */
508 qemu_send_packet(nc
, s
->tx_buffer
, tx_len
);
510 s
->regs
[SONIC_TCR
] |= SONIC_TCR_PTX
;
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
);
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
,
524 + sizeof(uint16_t) * width
525 * (4 + 3 * s
->regs
[SONIC_TFC
]),
526 MEMTXATTRS_UNSPECIFIED
, s
->data
,
528 s
->regs
[SONIC_CTDA
] = dp8393x_get(s
, width
, 0);
529 if (s
->regs
[SONIC_CTDA
] & SONIC_DESC_EOL
) {
537 s
->regs
[SONIC_CR
] &= ~SONIC_CR_TXP
;
538 s
->regs
[SONIC_ISR
] |= SONIC_ISR_TXDN
;
539 dp8393x_update_irq(s
);
542 static void dp8393x_do_halt_transmission(dp8393xState
*s
)
547 static void dp8393x_do_command(dp8393xState
*s
, uint16_t command
)
549 if ((s
->regs
[SONIC_CR
] & SONIC_CR_RST
) && !(command
& SONIC_CR_RST
)) {
550 s
->regs
[SONIC_CR
] &= ~SONIC_CR_RST
;
554 s
->regs
[SONIC_CR
] |= (command
& SONIC_CR_MASK
);
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
;
574 if (command
& SONIC_CR_LCAM
)
575 dp8393x_do_load_cam(s
);
578 static uint64_t dp8393x_read(void *opaque
, hwaddr addr
, unsigned int size
)
580 dp8393xState
*s
= opaque
;
581 int reg
= addr
>> s
->it_shift
;
585 /* Update data before reading it */
588 dp8393x_update_wt_regs(s
);
591 /* Accept read to some registers only when in reset mode */
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
)];
600 /* All other registers have no special contrainst */
605 DPRINTF("read 0x%04x from reg %s\n", val
, reg_names
[reg
]);
607 return s
->big_endian
? val
<< 16 : val
;
610 static void dp8393x_write(void *opaque
, hwaddr addr
, uint64_t data
,
613 dp8393xState
*s
= opaque
;
614 int reg
= addr
>> s
->it_shift
;
615 uint32_t val
= s
->big_endian
? data
>> 16 : data
;
617 DPRINTF("write 0x%04x to reg %s\n", (uint16_t)val
, reg_names
[reg
]);
620 /* Command register */
622 dp8393x_do_command(s
, val
);
624 /* Prevent write to read-only registers */
630 DPRINTF("writing to reg %d invalid\n", reg
);
632 /* Accept write to some registers only when in reset mode */
634 if (s
->regs
[SONIC_CR
] & SONIC_CR_RST
) {
635 s
->regs
[reg
] = val
& 0xbfff;
637 DPRINTF("writing to DCR invalid\n");
641 if (s
->regs
[SONIC_CR
] & SONIC_CR_RST
) {
642 s
->regs
[reg
] = val
& 0xf017;
644 DPRINTF("writing to DCR2 invalid\n");
647 /* 12 lower bytes are Read Only */
649 s
->regs
[reg
] = val
& 0xf000;
651 /* 9 lower bytes are Read Only */
653 s
->regs
[reg
] = val
& 0xffe0;
655 /* Ignore most significant bit */
657 s
->regs
[reg
] = val
& 0x7fff;
658 dp8393x_update_irq(s
);
660 /* Clear bits by writing 1 to them */
663 s
->regs
[reg
] &= ~val
;
664 if (val
& SONIC_ISR_RBE
) {
665 dp8393x_do_read_rra(s
);
667 dp8393x_update_irq(s
);
669 /* The guest is required to store aligned pointers here */
674 if (s
->regs
[SONIC_DCR
] & SONIC_DCR_DW
) {
675 s
->regs
[reg
] = val
& 0xfffc;
677 s
->regs
[reg
] = val
& 0xfffe;
680 /* Invert written value for some registers */
684 s
->regs
[reg
] = val
^ 0xffff;
686 /* All other registers have no special contrainst */
691 if (reg
== SONIC_WT0
|| reg
== SONIC_WT1
) {
692 dp8393x_set_next_tick(s
);
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
,
704 static void dp8393x_watchdog(void *opaque
)
706 dp8393xState
*s
= opaque
;
708 if (s
->regs
[SONIC_CR
] & SONIC_CR_STP
) {
712 s
->regs
[SONIC_WT1
] = 0xffff;
713 s
->regs
[SONIC_WT0
] = 0xffff;
714 dp8393x_set_next_tick(s
);
716 /* Signal underflow */
717 s
->regs
[SONIC_ISR
] |= SONIC_ISR_TC
;
718 dp8393x_update_irq(s
);
721 static bool dp8393x_can_receive(NetClientState
*nc
)
723 dp8393xState
*s
= qemu_get_nic_opaque(nc
);
725 return !!(s
->regs
[SONIC_CR
] & SONIC_CR_RXEN
);
728 static int dp8393x_receive_filter(dp8393xState
*s
, const uint8_t * buf
,
731 static const uint8_t bcast
[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
734 /* Check promiscuous mode */
735 if ((s
->regs
[SONIC_RCR
] & SONIC_RCR_PRO
) && (buf
[0] & 1) == 0) {
739 /* Check multicast packets */
740 if ((s
->regs
[SONIC_RCR
] & SONIC_RCR_AMC
) && (buf
[0] & 1) == 1) {
744 /* Check broadcast */
745 if ((s
->regs
[SONIC_RCR
] & SONIC_RCR_BRD
) && !memcmp(buf
, bcast
, sizeof(bcast
))) {
750 for (i
= 0; i
< 16; i
++) {
751 if (s
->regs
[SONIC_CE
] & (1 << i
)) {
753 if (!memcmp(buf
, s
->cam
[i
], sizeof(s
->cam
[i
]))) {
762 static ssize_t
dp8393x_receive(NetClientState
*nc
, const uint8_t * buf
,
765 dp8393xState
*s
= qemu_get_nic_opaque(nc
);
767 uint32_t available
, address
;
768 int width
, rx_len
, padded_len
;
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
);
775 if (s
->last_rba_is_full
) {
779 rx_len
= pkt_size
+ sizeof(checksum
);
780 if (s
->regs
[SONIC_DCR
] & SONIC_DCR_DW
) {
782 padded_len
= ((rx_len
- 1) | 3) + 1;
785 padded_len
= ((rx_len
- 1) | 1) + 1;
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
;
796 packet_type
= dp8393x_receive_filter(s
, buf
, pkt_size
);
797 if (packet_type
< 0) {
798 DPRINTF("packet not for netcard\n");
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
,
809 s
->regs
[SONIC_LLFA
] = dp8393x_get(s
, width
, 0);
810 if (s
->regs
[SONIC_LLFA
] & SONIC_DESC_EOL
) {
811 /* Still EOL ; stop reception */
814 /* Link has been updated by host */
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);
823 /* Move to next descriptor */
824 s
->regs
[SONIC_CRDA
] = s
->regs
[SONIC_LLFA
];
825 s
->regs
[SONIC_ISR
] |= SONIC_ISR_PKTRX
;
828 /* Save current position */
829 s
->regs
[SONIC_TRBA1
] = s
->regs
[SONIC_CRBA1
];
830 s
->regs
[SONIC_TRBA0
] = s
->regs
[SONIC_CRBA0
];
832 /* Calculate the ethernet checksum */
833 checksum
= cpu_to_le32(crc32(0, buf
, pkt_size
));
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
,
842 /* Put frame checksum into RBA */
843 address_space_write(&s
->as
, address
, MEMTXATTRS_UNSPECIFIED
,
844 &checksum
, sizeof(checksum
));
845 address
+= sizeof(checksum
);
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);
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;
863 if (dp8393x_rbwc(s
) < s
->regs
[SONIC_EOBC
]) {
864 s
->regs
[SONIC_RCR
] |= SONIC_RCR_LPKT
;
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;
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
,
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
) {
893 s
->regs
[SONIC_ISR
] |= SONIC_ISR_RDE
;
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
,
902 /* Move to next descriptor */
903 s
->regs
[SONIC_CRDA
] = s
->regs
[SONIC_LLFA
];
904 s
->regs
[SONIC_ISR
] |= SONIC_ISR_PKTRX
;
907 dp8393x_update_irq(s
);
909 s
->regs
[SONIC_RSC
] = (s
->regs
[SONIC_RSC
] & 0xff00) |
910 ((s
->regs
[SONIC_RSC
] + 1) & 0x00ff);
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;
919 /* Read next resource */
920 dp8393x_do_read_rra(s
);
927 static void dp8393x_reset(DeviceState
*dev
)
929 dp8393xState
*s
= DP8393X(dev
);
930 timer_del(s
->watchdog
);
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;
947 /* Network cable is connected */
948 s
->regs
[SONIC_RCR
] |= SONIC_RCR_CRS
;
950 dp8393x_update_irq(s
);
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
,
960 static void dp8393x_instance_init(Object
*obj
)
962 SysBusDevice
*sbd
= SYS_BUS_DEVICE(obj
);
963 dp8393xState
*s
= DP8393X(obj
);
965 sysbus_init_mmio(sbd
, &s
->mmio
);
966 sysbus_init_mmio(sbd
, &s
->prom
);
967 sysbus_init_irq(sbd
, &s
->irq
);
970 static void dp8393x_realize(DeviceState
*dev
, Error
**errp
)
972 dp8393xState
*s
= DP8393X(dev
);
975 Error
*local_err
= NULL
;
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
);
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
);
985 s
->watchdog
= timer_new_ns(QEMU_CLOCK_VIRTUAL
, dp8393x_watchdog
, s
);
987 memory_region_init_rom(&s
->prom
, OBJECT(dev
), "dp8393x-prom",
988 SONIC_PROM_SIZE
, &local_err
);
990 error_propagate(errp
, local_err
);
993 prom
= memory_region_get_ram_ptr(&s
->prom
);
995 for (i
= 0; i
< 6; i
++) {
996 prom
[i
] = s
->conf
.macaddr
.a
[i
];
998 if (checksum
> 0xff) {
999 checksum
= (checksum
+ 1) & 0xff;
1002 prom
[7] = 0xff - checksum
;
1005 static const VMStateDescription vmstate_dp8393x
= {
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()
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(),
1025 static void dp8393x_class_init(ObjectClass
*klass
, void *data
)
1027 DeviceClass
*dc
= DEVICE_CLASS(klass
);
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
);
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
,
1044 static void dp8393x_register_types(void)
1046 type_register_static(&dp8393x_info
);
1049 type_init(dp8393x_register_types
)