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"
17 /* Number of 2k memory pages available. */
20 #define TYPE_SMC91C111 "smc91c111"
21 #define SMC91C111(obj) OBJECT_CHECK(smc91c111_state, (obj), TYPE_SMC91C111)
24 SysBusDevice parent_obj
;
39 /* Bitmask of allocated packets. */
42 int tx_fifo
[NUM_PACKETS
];
44 int rx_fifo
[NUM_PACKETS
];
46 int tx_fifo_done
[NUM_PACKETS
];
47 /* Packet buffer memory. */
48 uint8_t data
[NUM_PACKETS
][2048];
54 static const VMStateDescription vmstate_smc91c111
= {
57 .minimum_version_id
= 1,
58 .fields
= (VMStateField
[]) {
59 VMSTATE_UINT16(tcr
, smc91c111_state
),
60 VMSTATE_UINT16(rcr
, smc91c111_state
),
61 VMSTATE_UINT16(cr
, smc91c111_state
),
62 VMSTATE_UINT16(ctr
, smc91c111_state
),
63 VMSTATE_UINT16(gpr
, smc91c111_state
),
64 VMSTATE_UINT16(ptr
, smc91c111_state
),
65 VMSTATE_UINT16(ercv
, smc91c111_state
),
66 VMSTATE_INT32(bank
, smc91c111_state
),
67 VMSTATE_INT32(packet_num
, smc91c111_state
),
68 VMSTATE_INT32(tx_alloc
, smc91c111_state
),
69 VMSTATE_INT32(allocated
, smc91c111_state
),
70 VMSTATE_INT32(tx_fifo_len
, smc91c111_state
),
71 VMSTATE_INT32_ARRAY(tx_fifo
, smc91c111_state
, NUM_PACKETS
),
72 VMSTATE_INT32(rx_fifo_len
, smc91c111_state
),
73 VMSTATE_INT32_ARRAY(rx_fifo
, smc91c111_state
, NUM_PACKETS
),
74 VMSTATE_INT32(tx_fifo_done_len
, smc91c111_state
),
75 VMSTATE_INT32_ARRAY(tx_fifo_done
, smc91c111_state
, NUM_PACKETS
),
76 VMSTATE_BUFFER_UNSAFE(data
, smc91c111_state
, 0, NUM_PACKETS
* 2048),
77 VMSTATE_UINT8(int_level
, smc91c111_state
),
78 VMSTATE_UINT8(int_mask
, smc91c111_state
),
83 #define RCR_SOFT_RST 0x8000
84 #define RCR_STRIP_CRC 0x0200
85 #define RCR_RXEN 0x0100
87 #define TCR_EPH_LOOP 0x2000
88 #define TCR_NOCRC 0x0100
89 #define TCR_PAD_EN 0x0080
90 #define TCR_FORCOL 0x0004
91 #define TCR_LOOP 0x0002
92 #define TCR_TXEN 0x0001
97 #define INT_RX_OVRN 0x10
98 #define INT_ALLOC 0x08
99 #define INT_TX_EMPTY 0x04
103 #define CTR_AUTO_RELEASE 0x0800
104 #define CTR_RELOAD 0x0002
105 #define CTR_STORE 0x0001
107 #define RS_ALGNERR 0x8000
108 #define RS_BRODCAST 0x4000
109 #define RS_BADCRC 0x2000
110 #define RS_ODDFRAME 0x1000
111 #define RS_TOOLONG 0x0800
112 #define RS_TOOSHORT 0x0400
113 #define RS_MULTICAST 0x0001
115 /* Update interrupt status. */
116 static void smc91c111_update(smc91c111_state
*s
)
120 if (s
->tx_fifo_len
== 0)
121 s
->int_level
|= INT_TX_EMPTY
;
122 if (s
->tx_fifo_done_len
!= 0)
123 s
->int_level
|= INT_TX
;
124 level
= (s
->int_level
& s
->int_mask
) != 0;
125 qemu_set_irq(s
->irq
, level
);
128 static int smc91c111_can_receive(smc91c111_state
*s
)
130 if ((s
->rcr
& RCR_RXEN
) == 0 || (s
->rcr
& RCR_SOFT_RST
)) {
133 if (s
->allocated
== (1 << NUM_PACKETS
) - 1 ||
134 s
->rx_fifo_len
== NUM_PACKETS
) {
140 static inline void smc91c111_flush_queued_packets(smc91c111_state
*s
)
142 if (smc91c111_can_receive(s
)) {
143 qemu_flush_queued_packets(qemu_get_queue(s
->nic
));
147 /* Try to allocate a packet. Returns 0x80 on failure. */
148 static int smc91c111_allocate_packet(smc91c111_state
*s
)
151 if (s
->allocated
== (1 << NUM_PACKETS
) - 1) {
155 for (i
= 0; i
< NUM_PACKETS
; i
++) {
156 if ((s
->allocated
& (1 << i
)) == 0)
159 s
->allocated
|= 1 << i
;
164 /* Process a pending TX allocate. */
165 static void smc91c111_tx_alloc(smc91c111_state
*s
)
167 s
->tx_alloc
= smc91c111_allocate_packet(s
);
168 if (s
->tx_alloc
== 0x80)
170 s
->int_level
|= INT_ALLOC
;
174 /* Remove and item from the RX FIFO. */
175 static void smc91c111_pop_rx_fifo(smc91c111_state
*s
)
180 if (s
->rx_fifo_len
) {
181 for (i
= 0; i
< s
->rx_fifo_len
; i
++)
182 s
->rx_fifo
[i
] = s
->rx_fifo
[i
+ 1];
183 s
->int_level
|= INT_RCV
;
185 s
->int_level
&= ~INT_RCV
;
187 smc91c111_flush_queued_packets(s
);
191 /* Remove an item from the TX completion FIFO. */
192 static void smc91c111_pop_tx_fifo_done(smc91c111_state
*s
)
196 if (s
->tx_fifo_done_len
== 0)
198 s
->tx_fifo_done_len
--;
199 for (i
= 0; i
< s
->tx_fifo_done_len
; i
++)
200 s
->tx_fifo_done
[i
] = s
->tx_fifo_done
[i
+ 1];
203 /* Release the memory allocated to a packet. */
204 static void smc91c111_release_packet(smc91c111_state
*s
, int packet
)
206 s
->allocated
&= ~(1 << packet
);
207 if (s
->tx_alloc
== 0x80)
208 smc91c111_tx_alloc(s
);
209 smc91c111_flush_queued_packets(s
);
212 /* Flush the TX FIFO. */
213 static void smc91c111_do_tx(smc91c111_state
*s
)
221 if ((s
->tcr
& TCR_TXEN
) == 0)
223 if (s
->tx_fifo_len
== 0)
225 for (i
= 0; i
< s
->tx_fifo_len
; i
++) {
226 packetnum
= s
->tx_fifo
[i
];
227 p
= &s
->data
[packetnum
][0];
228 /* Set status word. */
232 len
|= ((int)*(p
++)) << 8;
234 control
= p
[len
+ 1];
237 /* ??? This overwrites the data following the buffer.
238 Don't know what real hardware does. */
239 if (len
< 64 && (s
->tcr
& TCR_PAD_EN
)) {
240 memset(p
+ len
, 0, 64 - len
);
247 /* The card is supposed to append the CRC to the frame.
248 However none of the other network traffic has the CRC
249 appended. Suspect this is low level ethernet detail we
250 don't need to worry about. */
251 add_crc
= (control
& 0x10) || (s
->tcr
& TCR_NOCRC
) == 0;
255 crc
= crc32(~0, p
, len
);
256 memcpy(p
+ len
, &crc
, 4);
261 if (s
->ctr
& CTR_AUTO_RELEASE
)
263 smc91c111_release_packet(s
, packetnum
);
264 else if (s
->tx_fifo_done_len
< NUM_PACKETS
)
265 s
->tx_fifo_done
[s
->tx_fifo_done_len
++] = packetnum
;
266 qemu_send_packet(qemu_get_queue(s
->nic
), p
, len
);
272 /* Add a packet to the TX FIFO. */
273 static void smc91c111_queue_tx(smc91c111_state
*s
, int packet
)
275 if (s
->tx_fifo_len
== NUM_PACKETS
)
277 s
->tx_fifo
[s
->tx_fifo_len
++] = packet
;
281 static void smc91c111_reset(DeviceState
*dev
)
283 smc91c111_state
*s
= SMC91C111(dev
);
287 s
->tx_fifo_done_len
= 0;
298 s
->int_level
= INT_TX_EMPTY
;
303 #define SET_LOW(name, val) s->name = (s->name & 0xff00) | val
304 #define SET_HIGH(name, val) s->name = (s->name & 0xff) | (val << 8)
306 static void smc91c111_writeb(void *opaque
, hwaddr offset
,
309 smc91c111_state
*s
= (smc91c111_state
*)opaque
;
311 offset
= offset
& 0xf;
325 SET_HIGH(tcr
, value
);
331 SET_HIGH(rcr
, value
);
332 if (s
->rcr
& RCR_SOFT_RST
) {
333 smc91c111_reset(DEVICE(s
));
335 smc91c111_flush_queued_packets(s
);
337 case 10: case 11: /* RPCR */
340 case 12: case 13: /* Reserved */
353 case 2: case 3: /* BASE */
354 case 4: case 5: case 6: case 7: case 8: case 9: /* IA */
355 /* Not implemented. */
357 case 10: /* Genral Purpose */
361 SET_HIGH(gpr
, value
);
363 case 12: /* Control */
365 fprintf(stderr
, "smc91c111:EEPROM store not implemented\n");
367 fprintf(stderr
, "smc91c111:EEPROM reload not implemented\n");
372 SET_HIGH(ctr
, value
);
379 case 0: /* MMU Command */
380 switch (value
>> 5) {
383 case 1: /* Allocate for TX. */
385 s
->int_level
&= ~INT_ALLOC
;
387 smc91c111_tx_alloc(s
);
389 case 2: /* Reset MMU. */
392 s
->tx_fifo_done_len
= 0;
396 case 3: /* Remove from RX FIFO. */
397 smc91c111_pop_rx_fifo(s
);
399 case 4: /* Remove from RX FIFO and release. */
400 if (s
->rx_fifo_len
> 0) {
401 smc91c111_release_packet(s
, s
->rx_fifo
[0]);
403 smc91c111_pop_rx_fifo(s
);
405 case 5: /* Release. */
406 smc91c111_release_packet(s
, s
->packet_num
);
408 case 6: /* Add to TX FIFO. */
409 smc91c111_queue_tx(s
, s
->packet_num
);
411 case 7: /* Reset TX FIFO. */
413 s
->tx_fifo_done_len
= 0;
420 case 2: /* Packet Number Register */
421 s
->packet_num
= value
;
423 case 3: case 4: case 5:
424 /* Should be readonly, but linux writes to them anyway. Ignore. */
426 case 6: /* Pointer */
430 SET_HIGH(ptr
, value
);
432 case 8: case 9: case 10: case 11: /* Data */
442 if (s
->ptr
& 0x4000) {
443 s
->ptr
= (s
->ptr
& 0xf800) | ((s
->ptr
+ 1) & 0x7ff);
447 s
->data
[n
][p
] = value
;
450 case 12: /* Interrupt ACK. */
451 s
->int_level
&= ~(value
& 0xd6);
453 smc91c111_pop_tx_fifo_done(s
);
456 case 13: /* Interrupt mask. */
465 case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7:
466 /* Multicast table. */
467 /* Not implemented. */
469 case 8: case 9: /* Management Interface. */
470 /* Not implemented. */
472 case 12: /* Early receive. */
473 s
->ercv
= value
& 0x1f;
481 hw_error("smc91c111_write: Bad reg %d:%x\n", s
->bank
, (int)offset
);
484 static uint32_t smc91c111_readb(void *opaque
, hwaddr offset
)
486 smc91c111_state
*s
= (smc91c111_state
*)opaque
;
488 offset
= offset
& 0xf;
498 return s
->tcr
& 0xff;
501 case 2: /* EPH Status */
506 return s
->rcr
& 0xff;
509 case 6: /* Counter */
511 /* Not implemented. */
513 case 8: /* Memory size. */
515 case 9: /* Free memory available. */
520 for (i
= 0; i
< NUM_PACKETS
; i
++) {
521 if (s
->allocated
& (1 << i
))
526 case 10: case 11: /* RPCR */
527 /* Not implemented. */
529 case 12: case 13: /* Reserved */
540 case 2: case 3: /* BASE */
541 /* Not implemented. */
543 case 4: case 5: case 6: case 7: case 8: case 9: /* IA */
544 return s
->conf
.macaddr
.a
[offset
- 4];
545 case 10: /* General Purpose */
546 return s
->gpr
& 0xff;
549 case 12: /* Control */
550 return s
->ctr
& 0xff;
558 case 0: case 1: /* MMUCR Busy bit. */
560 case 2: /* Packet Number. */
561 return s
->packet_num
;
562 case 3: /* Allocation Result. */
564 case 4: /* TX FIFO */
565 if (s
->tx_fifo_done_len
== 0)
568 return s
->tx_fifo_done
[0];
569 case 5: /* RX FIFO */
570 if (s
->rx_fifo_len
== 0)
573 return s
->rx_fifo
[0];
574 case 6: /* Pointer */
575 return s
->ptr
& 0xff;
577 return (s
->ptr
>> 8) & 0xf7;
578 case 8: case 9: case 10: case 11: /* Data */
588 if (s
->ptr
& 0x4000) {
589 s
->ptr
= (s
->ptr
& 0xf800) | ((s
->ptr
+ 1) & 0x07ff);
593 return s
->data
[n
][p
];
595 case 12: /* Interrupt status. */
597 case 13: /* Interrupt mask. */
604 case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7:
605 /* Multicast table. */
606 /* Not implemented. */
608 case 8: /* Management Interface. */
609 /* Not implemented. */
613 case 10: /* Revision. */
624 hw_error("smc91c111_read: Bad reg %d:%x\n", s
->bank
, (int)offset
);
628 static uint64_t smc91c111_readfn(void *opaque
, hwaddr addr
, unsigned size
)
633 for (i
= 0; i
< size
; i
++) {
634 val
|= smc91c111_readb(opaque
, addr
+ i
) << (i
* 8);
639 static void smc91c111_writefn(void *opaque
, hwaddr addr
,
640 uint64_t value
, unsigned size
)
644 /* 32-bit writes to offset 0xc only actually write to the bank select
645 * register (offset 0xe), so skip the first two bytes we would write.
647 if (addr
== 0xc && size
== 4) {
651 for (; i
< size
; i
++) {
652 smc91c111_writeb(opaque
, addr
+ i
,
653 extract32(value
, i
* 8, 8));
657 static int smc91c111_can_receive_nc(NetClientState
*nc
)
659 smc91c111_state
*s
= qemu_get_nic_opaque(nc
);
661 return smc91c111_can_receive(s
);
664 static ssize_t
smc91c111_receive(NetClientState
*nc
, const uint8_t *buf
, size_t size
)
666 smc91c111_state
*s
= qemu_get_nic_opaque(nc
);
673 if ((s
->rcr
& RCR_RXEN
) == 0 || (s
->rcr
& RCR_SOFT_RST
))
675 /* Short packets are padded with zeros. Receiving a packet
676 < 64 bytes long is considered an error condition. */
680 packetsize
= (size
& ~1);
682 crc
= (s
->rcr
& RCR_STRIP_CRC
) == 0;
685 /* TODO: Flag overrun and receive errors. */
686 if (packetsize
> 2048)
688 packetnum
= smc91c111_allocate_packet(s
);
689 if (packetnum
== 0x80)
691 s
->rx_fifo
[s
->rx_fifo_len
++] = packetnum
;
693 p
= &s
->data
[packetnum
][0];
694 /* ??? Multicast packets? */
697 status
|= RS_TOOLONG
;
699 status
|= RS_ODDFRAME
;
700 *(p
++) = status
& 0xff;
701 *(p
++) = status
>> 8;
702 *(p
++) = packetsize
& 0xff;
703 *(p
++) = packetsize
>> 8;
704 memcpy(p
, buf
, size
& ~1);
706 /* Pad short packets. */
711 *(p
++) = buf
[size
- 1];
717 /* It's not clear if the CRC should go before or after the last byte in
718 odd sized packets. Linux disables the CRC, so that's no help.
719 The pictures in the documentation show the CRC aligned on a 16-bit
720 boundary before the last odd byte, so that's what we do. */
722 crc
= crc32(~0, buf
, size
);
723 *(p
++) = crc
& 0xff; crc
>>= 8;
724 *(p
++) = crc
& 0xff; crc
>>= 8;
725 *(p
++) = crc
& 0xff; crc
>>= 8;
729 *(p
++) = buf
[size
- 1];
735 /* TODO: Raise early RX interrupt? */
736 s
->int_level
|= INT_RCV
;
742 static const MemoryRegionOps smc91c111_mem_ops
= {
743 /* The special case for 32 bit writes to 0xc means we can't just
744 * set .impl.min/max_access_size to 1, unfortunately
746 .read
= smc91c111_readfn
,
747 .write
= smc91c111_writefn
,
748 .valid
.min_access_size
= 1,
749 .valid
.max_access_size
= 4,
750 .endianness
= DEVICE_NATIVE_ENDIAN
,
753 static NetClientInfo net_smc91c111_info
= {
754 .type
= NET_CLIENT_DRIVER_NIC
,
755 .size
= sizeof(NICState
),
756 .can_receive
= smc91c111_can_receive_nc
,
757 .receive
= smc91c111_receive
,
760 static int smc91c111_init1(SysBusDevice
*sbd
)
762 DeviceState
*dev
= DEVICE(sbd
);
763 smc91c111_state
*s
= SMC91C111(dev
);
765 memory_region_init_io(&s
->mmio
, OBJECT(s
), &smc91c111_mem_ops
, s
,
766 "smc91c111-mmio", 16);
767 sysbus_init_mmio(sbd
, &s
->mmio
);
768 sysbus_init_irq(sbd
, &s
->irq
);
769 qemu_macaddr_default_if_unset(&s
->conf
.macaddr
);
770 s
->nic
= qemu_new_nic(&net_smc91c111_info
, &s
->conf
,
771 object_get_typename(OBJECT(dev
)), dev
->id
, s
);
772 qemu_format_nic_info_str(qemu_get_queue(s
->nic
), s
->conf
.macaddr
.a
);
773 /* ??? Save/restore. */
777 static Property smc91c111_properties
[] = {
778 DEFINE_NIC_PROPERTIES(smc91c111_state
, conf
),
779 DEFINE_PROP_END_OF_LIST(),
782 static void smc91c111_class_init(ObjectClass
*klass
, void *data
)
784 DeviceClass
*dc
= DEVICE_CLASS(klass
);
785 SysBusDeviceClass
*k
= SYS_BUS_DEVICE_CLASS(klass
);
787 k
->init
= smc91c111_init1
;
788 dc
->reset
= smc91c111_reset
;
789 dc
->vmsd
= &vmstate_smc91c111
;
790 dc
->props
= smc91c111_properties
;
793 static const TypeInfo smc91c111_info
= {
794 .name
= TYPE_SMC91C111
,
795 .parent
= TYPE_SYS_BUS_DEVICE
,
796 .instance_size
= sizeof(smc91c111_state
),
797 .class_init
= smc91c111_class_init
,
800 static void smc91c111_register_types(void)
802 type_register_static(&smc91c111_info
);
805 /* Legacy helper function. Should go away when machine config files are
807 void smc91c111_init(NICInfo
*nd
, uint32_t base
, qemu_irq irq
)
812 qemu_check_nic_model(nd
, "smc91c111");
813 dev
= qdev_create(NULL
, TYPE_SMC91C111
);
814 qdev_set_nic_properties(dev
, nd
);
815 qdev_init_nofail(dev
);
816 s
= SYS_BUS_DEVICE(dev
);
817 sysbus_mmio_map(s
, 0, base
);
818 sysbus_connect_irq(s
, 0, irq
);
821 type_init(smc91c111_register_types
)