2 * QEMU model of Xilinx AXI-Ethernet.
4 * Copyright (c) 2011 Edgar E. Iglesias.
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 #include "hw/sysbus.h"
28 #include "net/checksum.h"
29 #include "qapi/qmp/qerror.h"
31 #include "hw/stream.h"
35 #define TYPE_XILINX_AXI_ENET "xlnx.axi-ethernet"
37 #define XILINX_AXI_ENET(obj) \
38 OBJECT_CHECK(XilinxAXIEnet, (obj), TYPE_XILINX_AXI_ENET)
40 /* Advertisement control register. */
41 #define ADVERTISE_10HALF 0x0020 /* Try for 10mbps half-duplex */
42 #define ADVERTISE_10FULL 0x0040 /* Try for 10mbps full-duplex */
43 #define ADVERTISE_100HALF 0x0080 /* Try for 100mbps half-duplex */
44 #define ADVERTISE_100FULL 0x0100 /* Try for 100mbps full-duplex */
51 unsigned int (*read
)(struct PHY
*phy
, unsigned int req
);
52 void (*write
)(struct PHY
*phy
, unsigned int req
,
56 static unsigned int tdk_read(struct PHY
*phy
, unsigned int req
)
69 /* Speeds and modes. */
70 r
|= (1 << 13) | (1 << 14);
71 r
|= (1 << 11) | (1 << 12);
72 r
|= (1 << 5); /* Autoneg complete. */
73 r
|= (1 << 3); /* Autoneg able. */
74 r
|= (1 << 2); /* link. */
75 r
|= (1 << 1); /* link. */
78 /* Link partner ability.
79 We are kind; always agree with whatever best mode
80 the guest advertises. */
81 r
= 1 << 14; /* Success. */
82 /* Copy advertised modes. */
83 r
|= phy
->regs
[4] & (15 << 5);
84 /* Autoneg support. */
88 /* Marvel PHY on many xilinx boards. */
89 r
= 0x8000; /* 1000Mb */
93 /* Diagnostics reg. */
101 /* Are we advertising 100 half or 100 duplex ? */
102 speed_100
= !!(phy
->regs
[4] & ADVERTISE_100HALF
);
103 speed_100
|= !!(phy
->regs
[4] & ADVERTISE_100FULL
);
105 /* Are we advertising 10 duplex or 100 duplex ? */
106 duplex
= !!(phy
->regs
[4] & ADVERTISE_100FULL
);
107 duplex
|= !!(phy
->regs
[4] & ADVERTISE_10FULL
);
108 r
= (speed_100
<< 10) | (duplex
<< 11);
113 r
= phy
->regs
[regnum
];
116 DPHY(qemu_log("\n%s %x = reg[%d]\n", __func__
, r
, regnum
));
121 tdk_write(struct PHY
*phy
, unsigned int req
, unsigned int data
)
126 DPHY(qemu_log("%s reg[%d] = %x\n", __func__
, regnum
, data
));
129 phy
->regs
[regnum
] = data
;
135 tdk_init(struct PHY
*phy
)
137 phy
->regs
[0] = 0x3100;
139 phy
->regs
[2] = 0x0300;
140 phy
->regs
[3] = 0xe400;
141 /* Autonegotiation advertisement reg. */
142 phy
->regs
[4] = 0x01E1;
145 phy
->read
= tdk_read
;
146 phy
->write
= tdk_write
;
172 struct PHY
*devs
[32];
176 mdio_attach(struct MDIOBus
*bus
, struct PHY
*phy
, unsigned int addr
)
178 bus
->devs
[addr
& 0x1f] = phy
;
181 #ifdef USE_THIS_DEAD_CODE
183 mdio_detach(struct MDIOBus
*bus
, struct PHY
*phy
, unsigned int addr
)
185 bus
->devs
[addr
& 0x1f] = NULL
;
189 static uint16_t mdio_read_req(struct MDIOBus
*bus
, unsigned int addr
,
195 phy
= bus
->devs
[addr
];
196 if (phy
&& phy
->read
) {
197 data
= phy
->read(phy
, reg
);
201 DPHY(qemu_log("%s addr=%d reg=%d data=%x\n", __func__
, addr
, reg
, data
));
205 static void mdio_write_req(struct MDIOBus
*bus
, unsigned int addr
,
206 unsigned int reg
, uint16_t data
)
210 DPHY(qemu_log("%s addr=%d reg=%d data=%x\n", __func__
, addr
, reg
, data
));
211 phy
= bus
->devs
[addr
];
212 if (phy
&& phy
->write
) {
213 phy
->write(phy
, reg
, data
);
219 #define R_RAF (0x000 / 4)
221 RAF_MCAST_REJ
= (1 << 1),
222 RAF_BCAST_REJ
= (1 << 2),
223 RAF_EMCF_EN
= (1 << 12),
224 RAF_NEWFUNC_EN
= (1 << 11)
227 #define R_IS (0x00C / 4)
229 IS_HARD_ACCESS_COMPLETE
= 1,
230 IS_AUTONEG
= (1 << 1),
231 IS_RX_COMPLETE
= (1 << 2),
232 IS_RX_REJECT
= (1 << 3),
233 IS_TX_COMPLETE
= (1 << 5),
234 IS_RX_DCM_LOCK
= (1 << 6),
235 IS_MGM_RDY
= (1 << 7),
236 IS_PHY_RST_DONE
= (1 << 8),
239 #define R_IP (0x010 / 4)
240 #define R_IE (0x014 / 4)
241 #define R_UAWL (0x020 / 4)
242 #define R_UAWU (0x024 / 4)
243 #define R_PPST (0x030 / 4)
245 PPST_LINKSTATUS
= (1 << 0),
246 PPST_PHY_LINKSTATUS
= (1 << 7),
249 #define R_STATS_RX_BYTESL (0x200 / 4)
250 #define R_STATS_RX_BYTESH (0x204 / 4)
251 #define R_STATS_TX_BYTESL (0x208 / 4)
252 #define R_STATS_TX_BYTESH (0x20C / 4)
253 #define R_STATS_RXL (0x290 / 4)
254 #define R_STATS_RXH (0x294 / 4)
255 #define R_STATS_RX_BCASTL (0x2a0 / 4)
256 #define R_STATS_RX_BCASTH (0x2a4 / 4)
257 #define R_STATS_RX_MCASTL (0x2a8 / 4)
258 #define R_STATS_RX_MCASTH (0x2ac / 4)
260 #define R_RCW0 (0x400 / 4)
261 #define R_RCW1 (0x404 / 4)
263 RCW1_VLAN
= (1 << 27),
265 RCW1_FCS
= (1 << 29),
266 RCW1_JUM
= (1 << 30),
267 RCW1_RST
= (1 << 31),
270 #define R_TC (0x408 / 4)
279 #define R_EMMC (0x410 / 4)
281 EMMC_LINKSPEED_10MB
= (0 << 30),
282 EMMC_LINKSPEED_100MB
= (1 << 30),
283 EMMC_LINKSPEED_1000MB
= (2 << 30),
286 #define R_PHYC (0x414 / 4)
288 #define R_MC (0x500 / 4)
289 #define MC_EN (1 << 6)
291 #define R_MCR (0x504 / 4)
292 #define R_MWD (0x508 / 4)
293 #define R_MRD (0x50c / 4)
294 #define R_MIS (0x600 / 4)
295 #define R_MIP (0x620 / 4)
296 #define R_MIE (0x640 / 4)
297 #define R_MIC (0x640 / 4)
299 #define R_UAW0 (0x700 / 4)
300 #define R_UAW1 (0x704 / 4)
301 #define R_FMI (0x708 / 4)
302 #define R_AF0 (0x710 / 4)
303 #define R_AF1 (0x714 / 4)
304 #define R_MAX (0x34 / 4)
306 /* Indirect registers. */
308 struct MDIOBus mdio_bus
;
314 typedef struct XilinxAXIEnet XilinxAXIEnet
;
316 struct XilinxAXIEnet
{
351 /* Receive configuration words. */
353 /* Transmit config. */
358 /* Unicast Address Word. */
360 /* Unicast address filter used with extended mcast. */
364 uint32_t regs
[R_MAX
];
366 /* Multicast filter addrs. */
367 uint32_t maddr
[4][2];
368 /* 32K x 1 lookup filter. */
369 uint32_t ext_mtable
[1024];
375 static void axienet_rx_reset(XilinxAXIEnet
*s
)
377 s
->rcw
[1] = RCW1_JUM
| RCW1_FCS
| RCW1_RX
| RCW1_VLAN
;
380 static void axienet_tx_reset(XilinxAXIEnet
*s
)
382 s
->tc
= TC_JUM
| TC_TX
| TC_VLAN
;
385 static inline int axienet_rx_resetting(XilinxAXIEnet
*s
)
387 return s
->rcw
[1] & RCW1_RST
;
390 static inline int axienet_rx_enabled(XilinxAXIEnet
*s
)
392 return s
->rcw
[1] & RCW1_RX
;
395 static inline int axienet_extmcf_enabled(XilinxAXIEnet
*s
)
397 return !!(s
->regs
[R_RAF
] & RAF_EMCF_EN
);
400 static inline int axienet_newfunc_enabled(XilinxAXIEnet
*s
)
402 return !!(s
->regs
[R_RAF
] & RAF_NEWFUNC_EN
);
405 static void axienet_reset(XilinxAXIEnet
*s
)
410 s
->regs
[R_PPST
] = PPST_LINKSTATUS
| PPST_PHY_LINKSTATUS
;
411 s
->regs
[R_IS
] = IS_AUTONEG
| IS_RX_DCM_LOCK
| IS_MGM_RDY
| IS_PHY_RST_DONE
;
413 s
->emmc
= EMMC_LINKSPEED_100MB
;
416 static void enet_update_irq(XilinxAXIEnet
*s
)
418 s
->regs
[R_IP
] = s
->regs
[R_IS
] & s
->regs
[R_IE
];
419 qemu_set_irq(s
->irq
, !!s
->regs
[R_IP
]);
422 static uint64_t enet_read(void *opaque
, hwaddr addr
, unsigned size
)
424 XilinxAXIEnet
*s
= opaque
;
431 r
= s
->rcw
[addr
& 1];
447 r
= s
->mii
.regs
[addr
& 3] | (1 << 7); /* Always ready. */
450 case R_STATS_RX_BYTESL
:
451 case R_STATS_RX_BYTESH
:
452 r
= s
->stats
.rx_bytes
>> (32 * (addr
& 1));
455 case R_STATS_TX_BYTESL
:
456 case R_STATS_TX_BYTESH
:
457 r
= s
->stats
.tx_bytes
>> (32 * (addr
& 1));
462 r
= s
->stats
.rx
>> (32 * (addr
& 1));
464 case R_STATS_RX_BCASTL
:
465 case R_STATS_RX_BCASTH
:
466 r
= s
->stats
.rx_bcast
>> (32 * (addr
& 1));
468 case R_STATS_RX_MCASTL
:
469 case R_STATS_RX_MCASTH
:
470 r
= s
->stats
.rx_mcast
>> (32 * (addr
& 1));
476 r
= s
->mii
.regs
[addr
& 3];
481 r
= s
->uaw
[addr
& 1];
486 r
= s
->ext_uaw
[addr
& 1];
495 r
= s
->maddr
[s
->fmi
& 3][addr
& 1];
498 case 0x8000 ... 0x83ff:
499 r
= s
->ext_mtable
[addr
- 0x8000];
503 if (addr
< ARRAY_SIZE(s
->regs
)) {
506 DENET(qemu_log("%s addr=" TARGET_FMT_plx
" v=%x\n",
507 __func__
, addr
* 4, r
));
513 static void enet_write(void *opaque
, hwaddr addr
,
514 uint64_t value
, unsigned size
)
516 XilinxAXIEnet
*s
= opaque
;
517 struct TEMAC
*t
= &s
->TEMAC
;
523 s
->rcw
[addr
& 1] = value
;
524 if ((addr
& 1) && value
& RCW1_RST
) {
527 qemu_flush_queued_packets(qemu_get_queue(s
->nic
));
533 if (value
& TC_RST
) {
547 value
&= ((1 < 7) - 1);
549 /* Enable the MII. */
551 unsigned int miiclkdiv
= value
& ((1 << 6) - 1);
553 qemu_log("AXIENET: MDIO enabled but MDIOCLK is zero!\n");
560 unsigned int phyaddr
= (value
>> 24) & 0x1f;
561 unsigned int regaddr
= (value
>> 16) & 0x1f;
562 unsigned int op
= (value
>> 14) & 3;
563 unsigned int initiate
= (value
>> 11) & 1;
567 mdio_write_req(&t
->mdio_bus
, phyaddr
, regaddr
, s
->mii
.mwd
);
568 } else if (op
== 2) {
569 s
->mii
.mrd
= mdio_read_req(&t
->mdio_bus
, phyaddr
, regaddr
);
571 qemu_log("AXIENET: invalid MDIOBus OP=%d\n", op
);
580 s
->mii
.regs
[addr
& 3] = value
;
586 s
->uaw
[addr
& 1] = value
;
591 s
->ext_uaw
[addr
& 1] = value
;
600 s
->maddr
[s
->fmi
& 3][addr
& 1] = value
;
604 s
->regs
[addr
] &= ~value
;
607 case 0x8000 ... 0x83ff:
608 s
->ext_mtable
[addr
- 0x8000] = value
;
612 DENET(qemu_log("%s addr=" TARGET_FMT_plx
" v=%x\n",
613 __func__
, addr
* 4, (unsigned)value
));
614 if (addr
< ARRAY_SIZE(s
->regs
)) {
615 s
->regs
[addr
] = value
;
622 static const MemoryRegionOps enet_ops
= {
625 .endianness
= DEVICE_LITTLE_ENDIAN
,
628 static int eth_can_rx(NetClientState
*nc
)
630 XilinxAXIEnet
*s
= qemu_get_nic_opaque(nc
);
633 return !axienet_rx_resetting(s
) && axienet_rx_enabled(s
);
636 static int enet_match_addr(const uint8_t *buf
, uint32_t f0
, uint32_t f1
)
640 if (memcmp(buf
, &f0
, 4)) {
644 if (buf
[4] != (f1
& 0xff) || buf
[5] != ((f1
>> 8) & 0xff)) {
651 static ssize_t
eth_rx(NetClientState
*nc
, const uint8_t *buf
, size_t size
)
653 XilinxAXIEnet
*s
= qemu_get_nic_opaque(nc
);
654 static const unsigned char sa_bcast
[6] = {0xff, 0xff, 0xff,
656 static const unsigned char sa_ipmcast
[3] = {0x01, 0x00, 0x52};
657 uint32_t app
[6] = {0};
658 int promisc
= s
->fmi
& (1 << 31);
659 int unicast
, broadcast
, multicast
, ip_multicast
= 0;
664 DENET(qemu_log("%s: %zd bytes\n", __func__
, size
));
666 unicast
= ~buf
[0] & 0x1;
667 broadcast
= memcmp(buf
, sa_bcast
, 6) == 0;
668 multicast
= !unicast
&& !broadcast
;
669 if (multicast
&& (memcmp(sa_ipmcast
, buf
, sizeof sa_ipmcast
) == 0)) {
673 /* Jumbo or vlan sizes ? */
674 if (!(s
->rcw
[1] & RCW1_JUM
)) {
675 if (size
> 1518 && size
<= 1522 && !(s
->rcw
[1] & RCW1_VLAN
)) {
680 /* Basic Address filters. If you want to use the extended filters
681 you'll generally have to place the ethernet mac into promiscuous mode
682 to avoid the basic filtering from dropping most frames. */
685 if (!enet_match_addr(buf
, s
->uaw
[0], s
->uaw
[1])) {
691 if (s
->regs
[R_RAF
] & RAF_BCAST_REJ
) {
698 if (s
->regs
[R_RAF
] & RAF_MCAST_REJ
) {
702 for (i
= 0; i
< 4; i
++) {
703 if (enet_match_addr(buf
, s
->maddr
[i
][0], s
->maddr
[i
][1])) {
716 /* Extended mcast filtering enabled? */
717 if (axienet_newfunc_enabled(s
) && axienet_extmcf_enabled(s
)) {
719 if (!enet_match_addr(buf
, s
->ext_uaw
[0], s
->ext_uaw
[1])) {
725 if (s
->regs
[R_RAF
] & RAF_BCAST_REJ
) {
732 if (!memcmp(buf
, sa_ipmcast
, 3)) {
736 idx
= (buf
[4] & 0x7f) << 8;
739 bit
= 1 << (idx
& 0x1f);
742 if (!(s
->ext_mtable
[idx
] & bit
)) {
750 s
->regs
[R_IS
] |= IS_RX_REJECT
;
755 if (size
> (s
->c_rxmem
- 4)) {
756 size
= s
->c_rxmem
- 4;
759 memcpy(s
->rxmem
, buf
, size
);
760 memset(s
->rxmem
+ size
, 0, 4); /* Clear the FCS. */
762 if (s
->rcw
[1] & RCW1_FCS
) {
763 size
+= 4; /* fcs is inband. */
767 csum32
= net_checksum_add(size
- 14, (uint8_t *)s
->rxmem
+ 14);
769 csum32
= (csum32
& 0xffff) + (csum32
>> 16);
770 /* And twice to get rid of possible carries. */
771 csum16
= (csum32
& 0xffff) + (csum32
>> 16);
773 app
[4] = size
& 0xffff;
775 s
->stats
.rx_bytes
+= size
;
779 app
[2] |= 1 | (ip_multicast
<< 1);
780 } else if (broadcast
) {
788 stream_push(s
->tx_dev
, (void *)s
->rxmem
, size
, app
);
790 s
->regs
[R_IS
] |= IS_RX_COMPLETE
;
795 static void eth_cleanup(NetClientState
*nc
)
798 XilinxAXIEnet
*s
= qemu_get_nic_opaque(nc
);
804 axienet_stream_push(StreamSlave
*obj
, uint8_t *buf
, size_t size
, uint32_t *hdr
)
806 XilinxAXIEnet
*s
= FROM_SYSBUS(typeof(*s
), SYS_BUS_DEVICE(obj
));
809 if (!(s
->tc
& TC_TX
)) {
813 /* Jumbo or vlan sizes ? */
814 if (!(s
->tc
& TC_JUM
)) {
815 if (size
> 1518 && size
<= 1522 && !(s
->tc
& TC_VLAN
)) {
821 unsigned int start_off
= hdr
[1] >> 16;
822 unsigned int write_off
= hdr
[1] & 0xffff;
826 tmp_csum
= net_checksum_add(size
- start_off
,
827 (uint8_t *)buf
+ start_off
);
828 /* Accumulate the seed. */
829 tmp_csum
+= hdr
[2] & 0xffff;
831 /* Fold the 32bit partial checksum. */
832 csum
= net_checksum_finish(tmp_csum
);
835 buf
[write_off
] = csum
>> 8;
836 buf
[write_off
+ 1] = csum
& 0xff;
839 qemu_send_packet(qemu_get_queue(s
->nic
), buf
, size
);
841 s
->stats
.tx_bytes
+= size
;
842 s
->regs
[R_IS
] |= IS_TX_COMPLETE
;
846 static NetClientInfo net_xilinx_enet_info
= {
847 .type
= NET_CLIENT_OPTIONS_KIND_NIC
,
848 .size
= sizeof(NICState
),
849 .can_receive
= eth_can_rx
,
851 .cleanup
= eth_cleanup
,
854 static int xilinx_enet_init(SysBusDevice
*dev
)
856 XilinxAXIEnet
*s
= XILINX_AXI_ENET(dev
);
858 sysbus_init_irq(dev
, &s
->irq
);
860 memory_region_init_io(&s
->iomem
, &enet_ops
, s
, "enet", 0x40000);
861 sysbus_init_mmio(dev
, &s
->iomem
);
863 qemu_macaddr_default_if_unset(&s
->conf
.macaddr
);
864 s
->nic
= qemu_new_nic(&net_xilinx_enet_info
, &s
->conf
,
865 object_get_typename(OBJECT(dev
)), dev
->qdev
.id
, s
);
866 qemu_format_nic_info_str(qemu_get_queue(s
->nic
), s
->conf
.macaddr
.a
);
868 tdk_init(&s
->TEMAC
.phy
);
869 mdio_attach(&s
->TEMAC
.mdio_bus
, &s
->TEMAC
.phy
, s
->c_phyaddr
);
873 s
->rxmem
= g_malloc(s
->c_rxmem
);
879 static void xilinx_enet_initfn(Object
*obj
)
881 XilinxAXIEnet
*s
= XILINX_AXI_ENET(obj
);
884 object_property_add_link(obj
, "axistream-connected", TYPE_STREAM_SLAVE
,
885 (Object
**) &s
->tx_dev
, &errp
);
886 assert_no_error(errp
);
889 static Property xilinx_enet_properties
[] = {
890 DEFINE_PROP_UINT32("phyaddr", XilinxAXIEnet
, c_phyaddr
, 7),
891 DEFINE_PROP_UINT32("rxmem", XilinxAXIEnet
, c_rxmem
, 0x1000),
892 DEFINE_PROP_UINT32("txmem", XilinxAXIEnet
, c_txmem
, 0x1000),
893 DEFINE_NIC_PROPERTIES(XilinxAXIEnet
, conf
),
894 DEFINE_PROP_END_OF_LIST(),
897 static void xilinx_enet_class_init(ObjectClass
*klass
, void *data
)
899 DeviceClass
*dc
= DEVICE_CLASS(klass
);
900 SysBusDeviceClass
*k
= SYS_BUS_DEVICE_CLASS(klass
);
901 StreamSlaveClass
*ssc
= STREAM_SLAVE_CLASS(klass
);
903 k
->init
= xilinx_enet_init
;
904 dc
->props
= xilinx_enet_properties
;
905 ssc
->push
= axienet_stream_push
;
908 static const TypeInfo xilinx_enet_info
= {
909 .name
= TYPE_XILINX_AXI_ENET
,
910 .parent
= TYPE_SYS_BUS_DEVICE
,
911 .instance_size
= sizeof(XilinxAXIEnet
),
912 .class_init
= xilinx_enet_class_init
,
913 .instance_init
= xilinx_enet_initfn
,
914 .interfaces
= (InterfaceInfo
[]) {
915 { TYPE_STREAM_SLAVE
},
920 static void xilinx_enet_register_types(void)
922 type_register_static(&xilinx_enet_info
);
925 type_init(xilinx_enet_register_types
)