2 * SMSC 91C111 Ethernet interface emulation
4 * Copyright (c) 2005 CodeSourcery, LLC.
5 * Written by Paul Brook
7 * This code is licensed under the GPL
10 #include "qemu/osdep.h"
11 #include "hw/sysbus.h"
13 #include "hw/devices.h"
18 /* Number of 2k memory pages available. */
21 #define TYPE_SMC91C111 "smc91c111"
22 #define SMC91C111(obj) OBJECT_CHECK(smc91c111_state, (obj), TYPE_SMC91C111)
25 SysBusDevice parent_obj
;
40 /* Bitmask of allocated packets. */
43 int tx_fifo
[NUM_PACKETS
];
45 int rx_fifo
[NUM_PACKETS
];
47 int tx_fifo_done
[NUM_PACKETS
];
48 /* Packet buffer memory. */
49 uint8_t data
[NUM_PACKETS
][2048];
55 static const VMStateDescription vmstate_smc91c111
= {
58 .minimum_version_id
= 1,
59 .fields
= (VMStateField
[]) {
60 VMSTATE_UINT16(tcr
, smc91c111_state
),
61 VMSTATE_UINT16(rcr
, smc91c111_state
),
62 VMSTATE_UINT16(cr
, smc91c111_state
),
63 VMSTATE_UINT16(ctr
, smc91c111_state
),
64 VMSTATE_UINT16(gpr
, smc91c111_state
),
65 VMSTATE_UINT16(ptr
, smc91c111_state
),
66 VMSTATE_UINT16(ercv
, smc91c111_state
),
67 VMSTATE_INT32(bank
, smc91c111_state
),
68 VMSTATE_INT32(packet_num
, smc91c111_state
),
69 VMSTATE_INT32(tx_alloc
, smc91c111_state
),
70 VMSTATE_INT32(allocated
, smc91c111_state
),
71 VMSTATE_INT32(tx_fifo_len
, smc91c111_state
),
72 VMSTATE_INT32_ARRAY(tx_fifo
, smc91c111_state
, NUM_PACKETS
),
73 VMSTATE_INT32(rx_fifo_len
, smc91c111_state
),
74 VMSTATE_INT32_ARRAY(rx_fifo
, smc91c111_state
, NUM_PACKETS
),
75 VMSTATE_INT32(tx_fifo_done_len
, smc91c111_state
),
76 VMSTATE_INT32_ARRAY(tx_fifo_done
, smc91c111_state
, NUM_PACKETS
),
77 VMSTATE_BUFFER_UNSAFE(data
, smc91c111_state
, 0, NUM_PACKETS
* 2048),
78 VMSTATE_UINT8(int_level
, smc91c111_state
),
79 VMSTATE_UINT8(int_mask
, smc91c111_state
),
84 #define RCR_SOFT_RST 0x8000
85 #define RCR_STRIP_CRC 0x0200
86 #define RCR_RXEN 0x0100
88 #define TCR_EPH_LOOP 0x2000
89 #define TCR_NOCRC 0x0100
90 #define TCR_PAD_EN 0x0080
91 #define TCR_FORCOL 0x0004
92 #define TCR_LOOP 0x0002
93 #define TCR_TXEN 0x0001
98 #define INT_RX_OVRN 0x10
99 #define INT_ALLOC 0x08
100 #define INT_TX_EMPTY 0x04
104 #define CTR_AUTO_RELEASE 0x0800
105 #define CTR_RELOAD 0x0002
106 #define CTR_STORE 0x0001
108 #define RS_ALGNERR 0x8000
109 #define RS_BRODCAST 0x4000
110 #define RS_BADCRC 0x2000
111 #define RS_ODDFRAME 0x1000
112 #define RS_TOOLONG 0x0800
113 #define RS_TOOSHORT 0x0400
114 #define RS_MULTICAST 0x0001
116 /* Update interrupt status. */
117 static void smc91c111_update(smc91c111_state
*s
)
121 if (s
->tx_fifo_len
== 0)
122 s
->int_level
|= INT_TX_EMPTY
;
123 if (s
->tx_fifo_done_len
!= 0)
124 s
->int_level
|= INT_TX
;
125 level
= (s
->int_level
& s
->int_mask
) != 0;
126 qemu_set_irq(s
->irq
, level
);
129 static int smc91c111_can_receive(smc91c111_state
*s
)
131 if ((s
->rcr
& RCR_RXEN
) == 0 || (s
->rcr
& RCR_SOFT_RST
)) {
134 if (s
->allocated
== (1 << NUM_PACKETS
) - 1 ||
135 s
->rx_fifo_len
== NUM_PACKETS
) {
141 static inline void smc91c111_flush_queued_packets(smc91c111_state
*s
)
143 if (smc91c111_can_receive(s
)) {
144 qemu_flush_queued_packets(qemu_get_queue(s
->nic
));
148 /* Try to allocate a packet. Returns 0x80 on failure. */
149 static int smc91c111_allocate_packet(smc91c111_state
*s
)
152 if (s
->allocated
== (1 << NUM_PACKETS
) - 1) {
156 for (i
= 0; i
< NUM_PACKETS
; i
++) {
157 if ((s
->allocated
& (1 << i
)) == 0)
160 s
->allocated
|= 1 << i
;
165 /* Process a pending TX allocate. */
166 static void smc91c111_tx_alloc(smc91c111_state
*s
)
168 s
->tx_alloc
= smc91c111_allocate_packet(s
);
169 if (s
->tx_alloc
== 0x80)
171 s
->int_level
|= INT_ALLOC
;
175 /* Remove and item from the RX FIFO. */
176 static void smc91c111_pop_rx_fifo(smc91c111_state
*s
)
181 if (s
->rx_fifo_len
) {
182 for (i
= 0; i
< s
->rx_fifo_len
; i
++)
183 s
->rx_fifo
[i
] = s
->rx_fifo
[i
+ 1];
184 s
->int_level
|= INT_RCV
;
186 s
->int_level
&= ~INT_RCV
;
188 smc91c111_flush_queued_packets(s
);
192 /* Remove an item from the TX completion FIFO. */
193 static void smc91c111_pop_tx_fifo_done(smc91c111_state
*s
)
197 if (s
->tx_fifo_done_len
== 0)
199 s
->tx_fifo_done_len
--;
200 for (i
= 0; i
< s
->tx_fifo_done_len
; i
++)
201 s
->tx_fifo_done
[i
] = s
->tx_fifo_done
[i
+ 1];
204 /* Release the memory allocated to a packet. */
205 static void smc91c111_release_packet(smc91c111_state
*s
, int packet
)
207 s
->allocated
&= ~(1 << packet
);
208 if (s
->tx_alloc
== 0x80)
209 smc91c111_tx_alloc(s
);
210 smc91c111_flush_queued_packets(s
);
213 /* Flush the TX FIFO. */
214 static void smc91c111_do_tx(smc91c111_state
*s
)
222 if ((s
->tcr
& TCR_TXEN
) == 0)
224 if (s
->tx_fifo_len
== 0)
226 for (i
= 0; i
< s
->tx_fifo_len
; i
++) {
227 packetnum
= s
->tx_fifo
[i
];
228 p
= &s
->data
[packetnum
][0];
229 /* Set status word. */
233 len
|= ((int)*(p
++)) << 8;
235 control
= p
[len
+ 1];
238 /* ??? This overwrites the data following the buffer.
239 Don't know what real hardware does. */
240 if (len
< 64 && (s
->tcr
& TCR_PAD_EN
)) {
241 memset(p
+ len
, 0, 64 - len
);
248 /* The card is supposed to append the CRC to the frame.
249 However none of the other network traffic has the CRC
250 appended. Suspect this is low level ethernet detail we
251 don't need to worry about. */
252 add_crc
= (control
& 0x10) || (s
->tcr
& TCR_NOCRC
) == 0;
256 crc
= crc32(~0, p
, len
);
257 memcpy(p
+ len
, &crc
, 4);
262 if (s
->ctr
& CTR_AUTO_RELEASE
)
264 smc91c111_release_packet(s
, packetnum
);
265 else if (s
->tx_fifo_done_len
< NUM_PACKETS
)
266 s
->tx_fifo_done
[s
->tx_fifo_done_len
++] = packetnum
;
267 qemu_send_packet(qemu_get_queue(s
->nic
), p
, len
);
273 /* Add a packet to the TX FIFO. */
274 static void smc91c111_queue_tx(smc91c111_state
*s
, int packet
)
276 if (s
->tx_fifo_len
== NUM_PACKETS
)
278 s
->tx_fifo
[s
->tx_fifo_len
++] = packet
;
282 static void smc91c111_reset(DeviceState
*dev
)
284 smc91c111_state
*s
= SMC91C111(dev
);
288 s
->tx_fifo_done_len
= 0;
299 s
->int_level
= INT_TX_EMPTY
;
304 #define SET_LOW(name, val) s->name = (s->name & 0xff00) | val
305 #define SET_HIGH(name, val) s->name = (s->name & 0xff) | (val << 8)
307 static void smc91c111_writeb(void *opaque
, hwaddr offset
,
310 smc91c111_state
*s
= (smc91c111_state
*)opaque
;
312 offset
= offset
& 0xf;
326 SET_HIGH(tcr
, value
);
332 SET_HIGH(rcr
, value
);
333 if (s
->rcr
& RCR_SOFT_RST
) {
334 smc91c111_reset(DEVICE(s
));
336 smc91c111_flush_queued_packets(s
);
338 case 10: case 11: /* RPCR */
341 case 12: case 13: /* Reserved */
354 case 2: case 3: /* BASE */
355 case 4: case 5: case 6: case 7: case 8: case 9: /* IA */
356 /* Not implemented. */
358 case 10: /* Genral Purpose */
362 SET_HIGH(gpr
, value
);
364 case 12: /* Control */
366 fprintf(stderr
, "smc91c111:EEPROM store not implemented\n");
368 fprintf(stderr
, "smc91c111:EEPROM reload not implemented\n");
373 SET_HIGH(ctr
, value
);
380 case 0: /* MMU Command */
381 switch (value
>> 5) {
384 case 1: /* Allocate for TX. */
386 s
->int_level
&= ~INT_ALLOC
;
388 smc91c111_tx_alloc(s
);
390 case 2: /* Reset MMU. */
393 s
->tx_fifo_done_len
= 0;
397 case 3: /* Remove from RX FIFO. */
398 smc91c111_pop_rx_fifo(s
);
400 case 4: /* Remove from RX FIFO and release. */
401 if (s
->rx_fifo_len
> 0) {
402 smc91c111_release_packet(s
, s
->rx_fifo
[0]);
404 smc91c111_pop_rx_fifo(s
);
406 case 5: /* Release. */
407 smc91c111_release_packet(s
, s
->packet_num
);
409 case 6: /* Add to TX FIFO. */
410 smc91c111_queue_tx(s
, s
->packet_num
);
412 case 7: /* Reset TX FIFO. */
414 s
->tx_fifo_done_len
= 0;
421 case 2: /* Packet Number Register */
422 s
->packet_num
= value
;
424 case 3: case 4: case 5:
425 /* Should be readonly, but linux writes to them anyway. Ignore. */
427 case 6: /* Pointer */
431 SET_HIGH(ptr
, value
);
433 case 8: case 9: case 10: case 11: /* Data */
443 if (s
->ptr
& 0x4000) {
444 s
->ptr
= (s
->ptr
& 0xf800) | ((s
->ptr
+ 1) & 0x7ff);
448 s
->data
[n
][p
] = value
;
451 case 12: /* Interrupt ACK. */
452 s
->int_level
&= ~(value
& 0xd6);
454 smc91c111_pop_tx_fifo_done(s
);
457 case 13: /* Interrupt mask. */
466 case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7:
467 /* Multicast table. */
468 /* Not implemented. */
470 case 8: case 9: /* Management Interface. */
471 /* Not implemented. */
473 case 12: /* Early receive. */
474 s
->ercv
= value
& 0x1f;
482 qemu_log_mask(LOG_GUEST_ERROR
, "smc91c111_write(bank:%d) Illegal register"
483 " 0x%" HWADDR_PRIx
" = 0x%x\n",
484 s
->bank
, offset
, value
);
487 static uint32_t smc91c111_readb(void *opaque
, hwaddr offset
)
489 smc91c111_state
*s
= (smc91c111_state
*)opaque
;
491 offset
= offset
& 0xf;
501 return s
->tcr
& 0xff;
504 case 2: /* EPH Status */
509 return s
->rcr
& 0xff;
512 case 6: /* Counter */
514 /* Not implemented. */
516 case 8: /* Memory size. */
518 case 9: /* Free memory available. */
523 for (i
= 0; i
< NUM_PACKETS
; i
++) {
524 if (s
->allocated
& (1 << i
))
529 case 10: case 11: /* RPCR */
530 /* Not implemented. */
532 case 12: case 13: /* Reserved */
543 case 2: case 3: /* BASE */
544 /* Not implemented. */
546 case 4: case 5: case 6: case 7: case 8: case 9: /* IA */
547 return s
->conf
.macaddr
.a
[offset
- 4];
548 case 10: /* General Purpose */
549 return s
->gpr
& 0xff;
552 case 12: /* Control */
553 return s
->ctr
& 0xff;
561 case 0: case 1: /* MMUCR Busy bit. */
563 case 2: /* Packet Number. */
564 return s
->packet_num
;
565 case 3: /* Allocation Result. */
567 case 4: /* TX FIFO */
568 if (s
->tx_fifo_done_len
== 0)
571 return s
->tx_fifo_done
[0];
572 case 5: /* RX FIFO */
573 if (s
->rx_fifo_len
== 0)
576 return s
->rx_fifo
[0];
577 case 6: /* Pointer */
578 return s
->ptr
& 0xff;
580 return (s
->ptr
>> 8) & 0xf7;
581 case 8: case 9: case 10: case 11: /* Data */
591 if (s
->ptr
& 0x4000) {
592 s
->ptr
= (s
->ptr
& 0xf800) | ((s
->ptr
+ 1) & 0x07ff);
596 return s
->data
[n
][p
];
598 case 12: /* Interrupt status. */
600 case 13: /* Interrupt mask. */
607 case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7:
608 /* Multicast table. */
609 /* Not implemented. */
611 case 8: /* Management Interface. */
612 /* Not implemented. */
616 case 10: /* Revision. */
627 qemu_log_mask(LOG_GUEST_ERROR
, "smc91c111_read(bank:%d) Illegal register"
628 " 0x%" HWADDR_PRIx
"\n",
633 static uint64_t smc91c111_readfn(void *opaque
, hwaddr addr
, unsigned size
)
638 for (i
= 0; i
< size
; i
++) {
639 val
|= smc91c111_readb(opaque
, addr
+ i
) << (i
* 8);
644 static void smc91c111_writefn(void *opaque
, hwaddr addr
,
645 uint64_t value
, unsigned size
)
649 /* 32-bit writes to offset 0xc only actually write to the bank select
650 * register (offset 0xe), so skip the first two bytes we would write.
652 if (addr
== 0xc && size
== 4) {
656 for (; i
< size
; i
++) {
657 smc91c111_writeb(opaque
, addr
+ i
,
658 extract32(value
, i
* 8, 8));
662 static int smc91c111_can_receive_nc(NetClientState
*nc
)
664 smc91c111_state
*s
= qemu_get_nic_opaque(nc
);
666 return smc91c111_can_receive(s
);
669 static ssize_t
smc91c111_receive(NetClientState
*nc
, const uint8_t *buf
, size_t size
)
671 smc91c111_state
*s
= qemu_get_nic_opaque(nc
);
678 if ((s
->rcr
& RCR_RXEN
) == 0 || (s
->rcr
& RCR_SOFT_RST
))
680 /* Short packets are padded with zeros. Receiving a packet
681 < 64 bytes long is considered an error condition. */
685 packetsize
= (size
& ~1);
687 crc
= (s
->rcr
& RCR_STRIP_CRC
) == 0;
690 /* TODO: Flag overrun and receive errors. */
691 if (packetsize
> 2048)
693 packetnum
= smc91c111_allocate_packet(s
);
694 if (packetnum
== 0x80)
696 s
->rx_fifo
[s
->rx_fifo_len
++] = packetnum
;
698 p
= &s
->data
[packetnum
][0];
699 /* ??? Multicast packets? */
702 status
|= RS_TOOLONG
;
704 status
|= RS_ODDFRAME
;
705 *(p
++) = status
& 0xff;
706 *(p
++) = status
>> 8;
707 *(p
++) = packetsize
& 0xff;
708 *(p
++) = packetsize
>> 8;
709 memcpy(p
, buf
, size
& ~1);
711 /* Pad short packets. */
716 *(p
++) = buf
[size
- 1];
722 /* It's not clear if the CRC should go before or after the last byte in
723 odd sized packets. Linux disables the CRC, so that's no help.
724 The pictures in the documentation show the CRC aligned on a 16-bit
725 boundary before the last odd byte, so that's what we do. */
727 crc
= crc32(~0, buf
, size
);
728 *(p
++) = crc
& 0xff; crc
>>= 8;
729 *(p
++) = crc
& 0xff; crc
>>= 8;
730 *(p
++) = crc
& 0xff; crc
>>= 8;
734 *(p
++) = buf
[size
- 1];
740 /* TODO: Raise early RX interrupt? */
741 s
->int_level
|= INT_RCV
;
747 static const MemoryRegionOps smc91c111_mem_ops
= {
748 /* The special case for 32 bit writes to 0xc means we can't just
749 * set .impl.min/max_access_size to 1, unfortunately
751 .read
= smc91c111_readfn
,
752 .write
= smc91c111_writefn
,
753 .valid
.min_access_size
= 1,
754 .valid
.max_access_size
= 4,
755 .endianness
= DEVICE_NATIVE_ENDIAN
,
758 static NetClientInfo net_smc91c111_info
= {
759 .type
= NET_CLIENT_DRIVER_NIC
,
760 .size
= sizeof(NICState
),
761 .can_receive
= smc91c111_can_receive_nc
,
762 .receive
= smc91c111_receive
,
765 static int smc91c111_init1(SysBusDevice
*sbd
)
767 DeviceState
*dev
= DEVICE(sbd
);
768 smc91c111_state
*s
= SMC91C111(dev
);
770 memory_region_init_io(&s
->mmio
, OBJECT(s
), &smc91c111_mem_ops
, s
,
771 "smc91c111-mmio", 16);
772 sysbus_init_mmio(sbd
, &s
->mmio
);
773 sysbus_init_irq(sbd
, &s
->irq
);
774 qemu_macaddr_default_if_unset(&s
->conf
.macaddr
);
775 s
->nic
= qemu_new_nic(&net_smc91c111_info
, &s
->conf
,
776 object_get_typename(OBJECT(dev
)), dev
->id
, s
);
777 qemu_format_nic_info_str(qemu_get_queue(s
->nic
), s
->conf
.macaddr
.a
);
778 /* ??? Save/restore. */
782 static Property smc91c111_properties
[] = {
783 DEFINE_NIC_PROPERTIES(smc91c111_state
, conf
),
784 DEFINE_PROP_END_OF_LIST(),
787 static void smc91c111_class_init(ObjectClass
*klass
, void *data
)
789 DeviceClass
*dc
= DEVICE_CLASS(klass
);
790 SysBusDeviceClass
*k
= SYS_BUS_DEVICE_CLASS(klass
);
792 k
->init
= smc91c111_init1
;
793 dc
->reset
= smc91c111_reset
;
794 dc
->vmsd
= &vmstate_smc91c111
;
795 dc
->props
= smc91c111_properties
;
798 static const TypeInfo smc91c111_info
= {
799 .name
= TYPE_SMC91C111
,
800 .parent
= TYPE_SYS_BUS_DEVICE
,
801 .instance_size
= sizeof(smc91c111_state
),
802 .class_init
= smc91c111_class_init
,
805 static void smc91c111_register_types(void)
807 type_register_static(&smc91c111_info
);
810 /* Legacy helper function. Should go away when machine config files are
812 void smc91c111_init(NICInfo
*nd
, uint32_t base
, qemu_irq irq
)
817 qemu_check_nic_model(nd
, "smc91c111");
818 dev
= qdev_create(NULL
, TYPE_SMC91C111
);
819 qdev_set_nic_properties(dev
, nd
);
820 qdev_init_nofail(dev
);
821 s
= SYS_BUS_DEVICE(dev
);
822 sysbus_mmio_map(s
, 0, base
);
823 sysbus_connect_irq(s
, 0, irq
);
826 type_init(smc91c111_register_types
)