1 /* 2 * QEMU Cadence GEM emulation 3 * 4 * Copyright (c) 2011 Xilinx, Inc. 5 * 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: 12 * 13 * The above copyright notice and this permission notice shall be included in 14 * all copies or substantial portions of the Software. 15 * 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 22 * THE SOFTWARE. 23 */ 24 25 #include "qemu/osdep.h" 26 #include <zlib.h> /* For crc32 */ 27 28 #include "hw/irq.h" 29 #include "hw/net/cadence_gem.h" 30 #include "qapi/error.h" 31 #include "qemu/log.h" 32 #include "qemu/module.h" 33 #include "sysemu/dma.h" 34 #include "net/checksum.h" 35 36 #ifdef CADENCE_GEM_ERR_DEBUG 37 #define DB_PRINT(...) do { \ 38 fprintf(stderr, ": %s: ", __func__); \ 39 fprintf(stderr, ## __VA_ARGS__); \ 40 } while (0) 41 #else 42 #define DB_PRINT(...) 43 #endif 44 45 #define GEM_NWCTRL (0x00000000/4) /* Network Control reg */ 46 #define GEM_NWCFG (0x00000004/4) /* Network Config reg */ 47 #define GEM_NWSTATUS (0x00000008/4) /* Network Status reg */ 48 #define GEM_USERIO (0x0000000C/4) /* User IO reg */ 49 #define GEM_DMACFG (0x00000010/4) /* DMA Control reg */ 50 #define GEM_TXSTATUS (0x00000014/4) /* TX Status reg */ 51 #define GEM_RXQBASE (0x00000018/4) /* RX Q Base address reg */ 52 #define GEM_TXQBASE (0x0000001C/4) /* TX Q Base address reg */ 53 #define GEM_RXSTATUS (0x00000020/4) /* RX Status reg */ 54 #define GEM_ISR (0x00000024/4) /* Interrupt Status reg */ 55 #define GEM_IER (0x00000028/4) /* Interrupt Enable reg */ 56 #define GEM_IDR (0x0000002C/4) /* Interrupt Disable reg */ 57 #define GEM_IMR (0x00000030/4) /* Interrupt Mask reg */ 58 #define GEM_PHYMNTNC (0x00000034/4) /* Phy Maintenance reg */ 59 #define GEM_RXPAUSE (0x00000038/4) /* RX Pause Time reg */ 60 #define GEM_TXPAUSE (0x0000003C/4) /* TX Pause Time reg */ 61 #define GEM_TXPARTIALSF (0x00000040/4) /* TX Partial Store and Forward */ 62 #define GEM_RXPARTIALSF (0x00000044/4) /* RX Partial Store and Forward */ 63 #define GEM_HASHLO (0x00000080/4) /* Hash Low address reg */ 64 #define GEM_HASHHI (0x00000084/4) /* Hash High address reg */ 65 #define GEM_SPADDR1LO (0x00000088/4) /* Specific addr 1 low reg */ 66 #define GEM_SPADDR1HI (0x0000008C/4) /* Specific addr 1 high reg */ 67 #define GEM_SPADDR2LO (0x00000090/4) /* Specific addr 2 low reg */ 68 #define GEM_SPADDR2HI (0x00000094/4) /* Specific addr 2 high reg */ 69 #define GEM_SPADDR3LO (0x00000098/4) /* Specific addr 3 low reg */ 70 #define GEM_SPADDR3HI (0x0000009C/4) /* Specific addr 3 high reg */ 71 #define GEM_SPADDR4LO (0x000000A0/4) /* Specific addr 4 low reg */ 72 #define GEM_SPADDR4HI (0x000000A4/4) /* Specific addr 4 high reg */ 73 #define GEM_TIDMATCH1 (0x000000A8/4) /* Type ID1 Match reg */ 74 #define GEM_TIDMATCH2 (0x000000AC/4) /* Type ID2 Match reg */ 75 #define GEM_TIDMATCH3 (0x000000B0/4) /* Type ID3 Match reg */ 76 #define GEM_TIDMATCH4 (0x000000B4/4) /* Type ID4 Match reg */ 77 #define GEM_WOLAN (0x000000B8/4) /* Wake on LAN reg */ 78 #define GEM_IPGSTRETCH (0x000000BC/4) /* IPG Stretch reg */ 79 #define GEM_SVLAN (0x000000C0/4) /* Stacked VLAN reg */ 80 #define GEM_MODID (0x000000FC/4) /* Module ID reg */ 81 #define GEM_OCTTXLO (0x00000100/4) /* Octects transmitted Low reg */ 82 #define GEM_OCTTXHI (0x00000104/4) /* Octects transmitted High reg */ 83 #define GEM_TXCNT (0x00000108/4) /* Error-free Frames transmitted */ 84 #define GEM_TXBCNT (0x0000010C/4) /* Error-free Broadcast Frames */ 85 #define GEM_TXMCNT (0x00000110/4) /* Error-free Multicast Frame */ 86 #define GEM_TXPAUSECNT (0x00000114/4) /* Pause Frames Transmitted */ 87 #define GEM_TX64CNT (0x00000118/4) /* Error-free 64 TX */ 88 #define GEM_TX65CNT (0x0000011C/4) /* Error-free 65-127 TX */ 89 #define GEM_TX128CNT (0x00000120/4) /* Error-free 128-255 TX */ 90 #define GEM_TX256CNT (0x00000124/4) /* Error-free 256-511 */ 91 #define GEM_TX512CNT (0x00000128/4) /* Error-free 512-1023 TX */ 92 #define GEM_TX1024CNT (0x0000012C/4) /* Error-free 1024-1518 TX */ 93 #define GEM_TX1519CNT (0x00000130/4) /* Error-free larger than 1519 TX */ 94 #define GEM_TXURUNCNT (0x00000134/4) /* TX under run error counter */ 95 #define GEM_SINGLECOLLCNT (0x00000138/4) /* Single Collision Frames */ 96 #define GEM_MULTCOLLCNT (0x0000013C/4) /* Multiple Collision Frames */ 97 #define GEM_EXCESSCOLLCNT (0x00000140/4) /* Excessive Collision Frames */ 98 #define GEM_LATECOLLCNT (0x00000144/4) /* Late Collision Frames */ 99 #define GEM_DEFERTXCNT (0x00000148/4) /* Deferred Transmission Frames */ 100 #define GEM_CSENSECNT (0x0000014C/4) /* Carrier Sense Error Counter */ 101 #define GEM_OCTRXLO (0x00000150/4) /* Octects Received register Low */ 102 #define GEM_OCTRXHI (0x00000154/4) /* Octects Received register High */ 103 #define GEM_RXCNT (0x00000158/4) /* Error-free Frames Received */ 104 #define GEM_RXBROADCNT (0x0000015C/4) /* Error-free Broadcast Frames RX */ 105 #define GEM_RXMULTICNT (0x00000160/4) /* Error-free Multicast Frames RX */ 106 #define GEM_RXPAUSECNT (0x00000164/4) /* Pause Frames Received Counter */ 107 #define GEM_RX64CNT (0x00000168/4) /* Error-free 64 byte Frames RX */ 108 #define GEM_RX65CNT (0x0000016C/4) /* Error-free 65-127B Frames RX */ 109 #define GEM_RX128CNT (0x00000170/4) /* Error-free 128-255B Frames RX */ 110 #define GEM_RX256CNT (0x00000174/4) /* Error-free 256-512B Frames RX */ 111 #define GEM_RX512CNT (0x00000178/4) /* Error-free 512-1023B Frames RX */ 112 #define GEM_RX1024CNT (0x0000017C/4) /* Error-free 1024-1518B Frames RX */ 113 #define GEM_RX1519CNT (0x00000180/4) /* Error-free 1519-max Frames RX */ 114 #define GEM_RXUNDERCNT (0x00000184/4) /* Undersize Frames Received */ 115 #define GEM_RXOVERCNT (0x00000188/4) /* Oversize Frames Received */ 116 #define GEM_RXJABCNT (0x0000018C/4) /* Jabbers Received Counter */ 117 #define GEM_RXFCSCNT (0x00000190/4) /* Frame Check seq. Error Counter */ 118 #define GEM_RXLENERRCNT (0x00000194/4) /* Length Field Error Counter */ 119 #define GEM_RXSYMERRCNT (0x00000198/4) /* Symbol Error Counter */ 120 #define GEM_RXALIGNERRCNT (0x0000019C/4) /* Alignment Error Counter */ 121 #define GEM_RXRSCERRCNT (0x000001A0/4) /* Receive Resource Error Counter */ 122 #define GEM_RXORUNCNT (0x000001A4/4) /* Receive Overrun Counter */ 123 #define GEM_RXIPCSERRCNT (0x000001A8/4) /* IP header Checksum Error Counter */ 124 #define GEM_RXTCPCCNT (0x000001AC/4) /* TCP Checksum Error Counter */ 125 #define GEM_RXUDPCCNT (0x000001B0/4) /* UDP Checksum Error Counter */ 126 127 #define GEM_1588S (0x000001D0/4) /* 1588 Timer Seconds */ 128 #define GEM_1588NS (0x000001D4/4) /* 1588 Timer Nanoseconds */ 129 #define GEM_1588ADJ (0x000001D8/4) /* 1588 Timer Adjust */ 130 #define GEM_1588INC (0x000001DC/4) /* 1588 Timer Increment */ 131 #define GEM_PTPETXS (0x000001E0/4) /* PTP Event Frame Transmitted (s) */ 132 #define GEM_PTPETXNS (0x000001E4/4) /* PTP Event Frame Transmitted (ns) */ 133 #define GEM_PTPERXS (0x000001E8/4) /* PTP Event Frame Received (s) */ 134 #define GEM_PTPERXNS (0x000001EC/4) /* PTP Event Frame Received (ns) */ 135 #define GEM_PTPPTXS (0x000001E0/4) /* PTP Peer Frame Transmitted (s) */ 136 #define GEM_PTPPTXNS (0x000001E4/4) /* PTP Peer Frame Transmitted (ns) */ 137 #define GEM_PTPPRXS (0x000001E8/4) /* PTP Peer Frame Received (s) */ 138 #define GEM_PTPPRXNS (0x000001EC/4) /* PTP Peer Frame Received (ns) */ 139 140 /* Design Configuration Registers */ 141 #define GEM_DESCONF (0x00000280/4) 142 #define GEM_DESCONF2 (0x00000284/4) 143 #define GEM_DESCONF3 (0x00000288/4) 144 #define GEM_DESCONF4 (0x0000028C/4) 145 #define GEM_DESCONF5 (0x00000290/4) 146 #define GEM_DESCONF6 (0x00000294/4) 147 #define GEM_DESCONF6_64B_MASK (1U << 23) 148 #define GEM_DESCONF7 (0x00000298/4) 149 150 #define GEM_INT_Q1_STATUS (0x00000400 / 4) 151 #define GEM_INT_Q1_MASK (0x00000640 / 4) 152 153 #define GEM_TRANSMIT_Q1_PTR (0x00000440 / 4) 154 #define GEM_TRANSMIT_Q7_PTR (GEM_TRANSMIT_Q1_PTR + 6) 155 156 #define GEM_RECEIVE_Q1_PTR (0x00000480 / 4) 157 #define GEM_RECEIVE_Q7_PTR (GEM_RECEIVE_Q1_PTR + 6) 158 159 #define GEM_TBQPH (0x000004C8 / 4) 160 #define GEM_RBQPH (0x000004D4 / 4) 161 162 #define GEM_INT_Q1_ENABLE (0x00000600 / 4) 163 #define GEM_INT_Q7_ENABLE (GEM_INT_Q1_ENABLE + 6) 164 165 #define GEM_INT_Q1_DISABLE (0x00000620 / 4) 166 #define GEM_INT_Q7_DISABLE (GEM_INT_Q1_DISABLE + 6) 167 168 #define GEM_INT_Q1_MASK (0x00000640 / 4) 169 #define GEM_INT_Q7_MASK (GEM_INT_Q1_MASK + 6) 170 171 #define GEM_SCREENING_TYPE1_REGISTER_0 (0x00000500 / 4) 172 173 #define GEM_ST1R_UDP_PORT_MATCH_ENABLE (1 << 29) 174 #define GEM_ST1R_DSTC_ENABLE (1 << 28) 175 #define GEM_ST1R_UDP_PORT_MATCH_SHIFT (12) 176 #define GEM_ST1R_UDP_PORT_MATCH_WIDTH (27 - GEM_ST1R_UDP_PORT_MATCH_SHIFT + 1) 177 #define GEM_ST1R_DSTC_MATCH_SHIFT (4) 178 #define GEM_ST1R_DSTC_MATCH_WIDTH (11 - GEM_ST1R_DSTC_MATCH_SHIFT + 1) 179 #define GEM_ST1R_QUEUE_SHIFT (0) 180 #define GEM_ST1R_QUEUE_WIDTH (3 - GEM_ST1R_QUEUE_SHIFT + 1) 181 182 #define GEM_SCREENING_TYPE2_REGISTER_0 (0x00000540 / 4) 183 184 #define GEM_ST2R_COMPARE_A_ENABLE (1 << 18) 185 #define GEM_ST2R_COMPARE_A_SHIFT (13) 186 #define GEM_ST2R_COMPARE_WIDTH (17 - GEM_ST2R_COMPARE_A_SHIFT + 1) 187 #define GEM_ST2R_ETHERTYPE_ENABLE (1 << 12) 188 #define GEM_ST2R_ETHERTYPE_INDEX_SHIFT (9) 189 #define GEM_ST2R_ETHERTYPE_INDEX_WIDTH (11 - GEM_ST2R_ETHERTYPE_INDEX_SHIFT \ 190 + 1) 191 #define GEM_ST2R_QUEUE_SHIFT (0) 192 #define GEM_ST2R_QUEUE_WIDTH (3 - GEM_ST2R_QUEUE_SHIFT + 1) 193 194 #define GEM_SCREENING_TYPE2_ETHERTYPE_REG_0 (0x000006e0 / 4) 195 #define GEM_TYPE2_COMPARE_0_WORD_0 (0x00000700 / 4) 196 197 #define GEM_T2CW1_COMPARE_OFFSET_SHIFT (7) 198 #define GEM_T2CW1_COMPARE_OFFSET_WIDTH (8 - GEM_T2CW1_COMPARE_OFFSET_SHIFT + 1) 199 #define GEM_T2CW1_OFFSET_VALUE_SHIFT (0) 200 #define GEM_T2CW1_OFFSET_VALUE_WIDTH (6 - GEM_T2CW1_OFFSET_VALUE_SHIFT + 1) 201 202 /*****************************************/ 203 #define GEM_NWCTRL_TXSTART 0x00000200 /* Transmit Enable */ 204 #define GEM_NWCTRL_TXENA 0x00000008 /* Transmit Enable */ 205 #define GEM_NWCTRL_RXENA 0x00000004 /* Receive Enable */ 206 #define GEM_NWCTRL_LOCALLOOP 0x00000002 /* Local Loopback */ 207 208 #define GEM_NWCFG_STRIP_FCS 0x00020000 /* Strip FCS field */ 209 #define GEM_NWCFG_LERR_DISC 0x00010000 /* Discard RX frames with len err */ 210 #define GEM_NWCFG_BUFF_OFST_M 0x0000C000 /* Receive buffer offset mask */ 211 #define GEM_NWCFG_BUFF_OFST_S 14 /* Receive buffer offset shift */ 212 #define GEM_NWCFG_UCAST_HASH 0x00000080 /* accept unicast if hash match */ 213 #define GEM_NWCFG_MCAST_HASH 0x00000040 /* accept multicast if hash match */ 214 #define GEM_NWCFG_BCAST_REJ 0x00000020 /* Reject broadcast packets */ 215 #define GEM_NWCFG_PROMISC 0x00000010 /* Accept all packets */ 216 217 #define GEM_DMACFG_ADDR_64B (1U << 30) 218 #define GEM_DMACFG_TX_BD_EXT (1U << 29) 219 #define GEM_DMACFG_RX_BD_EXT (1U << 28) 220 #define GEM_DMACFG_RBUFSZ_M 0x00FF0000 /* DMA RX Buffer Size mask */ 221 #define GEM_DMACFG_RBUFSZ_S 16 /* DMA RX Buffer Size shift */ 222 #define GEM_DMACFG_RBUFSZ_MUL 64 /* DMA RX Buffer Size multiplier */ 223 #define GEM_DMACFG_TXCSUM_OFFL 0x00000800 /* Transmit checksum offload */ 224 225 #define GEM_TXSTATUS_TXCMPL 0x00000020 /* Transmit Complete */ 226 #define GEM_TXSTATUS_USED 0x00000001 /* sw owned descriptor encountered */ 227 228 #define GEM_RXSTATUS_FRMRCVD 0x00000002 /* Frame received */ 229 #define GEM_RXSTATUS_NOBUF 0x00000001 /* Buffer unavailable */ 230 231 /* GEM_ISR GEM_IER GEM_IDR GEM_IMR */ 232 #define GEM_INT_TXCMPL 0x00000080 /* Transmit Complete */ 233 #define GEM_INT_TXUSED 0x00000008 234 #define GEM_INT_RXUSED 0x00000004 235 #define GEM_INT_RXCMPL 0x00000002 236 237 #define GEM_PHYMNTNC_OP_R 0x20000000 /* read operation */ 238 #define GEM_PHYMNTNC_OP_W 0x10000000 /* write operation */ 239 #define GEM_PHYMNTNC_ADDR 0x0F800000 /* Address bits */ 240 #define GEM_PHYMNTNC_ADDR_SHFT 23 241 #define GEM_PHYMNTNC_REG 0x007C0000 /* register bits */ 242 #define GEM_PHYMNTNC_REG_SHIFT 18 243 244 /* Marvell PHY definitions */ 245 #define BOARD_PHY_ADDRESS 23 /* PHY address we will emulate a device at */ 246 247 #define PHY_REG_CONTROL 0 248 #define PHY_REG_STATUS 1 249 #define PHY_REG_PHYID1 2 250 #define PHY_REG_PHYID2 3 251 #define PHY_REG_ANEGADV 4 252 #define PHY_REG_LINKPABIL 5 253 #define PHY_REG_ANEGEXP 6 254 #define PHY_REG_NEXTP 7 255 #define PHY_REG_LINKPNEXTP 8 256 #define PHY_REG_100BTCTRL 9 257 #define PHY_REG_1000BTSTAT 10 258 #define PHY_REG_EXTSTAT 15 259 #define PHY_REG_PHYSPCFC_CTL 16 260 #define PHY_REG_PHYSPCFC_ST 17 261 #define PHY_REG_INT_EN 18 262 #define PHY_REG_INT_ST 19 263 #define PHY_REG_EXT_PHYSPCFC_CTL 20 264 #define PHY_REG_RXERR 21 265 #define PHY_REG_EACD 22 266 #define PHY_REG_LED 24 267 #define PHY_REG_LED_OVRD 25 268 #define PHY_REG_EXT_PHYSPCFC_CTL2 26 269 #define PHY_REG_EXT_PHYSPCFC_ST 27 270 #define PHY_REG_CABLE_DIAG 28 271 272 #define PHY_REG_CONTROL_RST 0x8000 273 #define PHY_REG_CONTROL_LOOP 0x4000 274 #define PHY_REG_CONTROL_ANEG 0x1000 275 276 #define PHY_REG_STATUS_LINK 0x0004 277 #define PHY_REG_STATUS_ANEGCMPL 0x0020 278 279 #define PHY_REG_INT_ST_ANEGCMPL 0x0800 280 #define PHY_REG_INT_ST_LINKC 0x0400 281 #define PHY_REG_INT_ST_ENERGY 0x0010 282 283 /***********************************************************************/ 284 #define GEM_RX_REJECT (-1) 285 #define GEM_RX_PROMISCUOUS_ACCEPT (-2) 286 #define GEM_RX_BROADCAST_ACCEPT (-3) 287 #define GEM_RX_MULTICAST_HASH_ACCEPT (-4) 288 #define GEM_RX_UNICAST_HASH_ACCEPT (-5) 289 290 #define GEM_RX_SAR_ACCEPT 0 291 292 /***********************************************************************/ 293 294 #define DESC_1_USED 0x80000000 295 #define DESC_1_LENGTH 0x00001FFF 296 297 #define DESC_1_TX_WRAP 0x40000000 298 #define DESC_1_TX_LAST 0x00008000 299 300 #define DESC_0_RX_WRAP 0x00000002 301 #define DESC_0_RX_OWNERSHIP 0x00000001 302 303 #define R_DESC_1_RX_SAR_SHIFT 25 304 #define R_DESC_1_RX_SAR_LENGTH 2 305 #define R_DESC_1_RX_SAR_MATCH (1 << 27) 306 #define R_DESC_1_RX_UNICAST_HASH (1 << 29) 307 #define R_DESC_1_RX_MULTICAST_HASH (1 << 30) 308 #define R_DESC_1_RX_BROADCAST (1 << 31) 309 310 #define DESC_1_RX_SOF 0x00004000 311 #define DESC_1_RX_EOF 0x00008000 312 313 #define GEM_MODID_VALUE 0x00020118 314 315 static inline uint64_t tx_desc_get_buffer(CadenceGEMState *s, uint32_t *desc) 316 { 317 uint64_t ret = desc[0]; 318 319 if (s->regs[GEM_DMACFG] & GEM_DMACFG_ADDR_64B) { 320 ret |= (uint64_t)desc[2] << 32; 321 } 322 return ret; 323 } 324 325 static inline unsigned tx_desc_get_used(uint32_t *desc) 326 { 327 return (desc[1] & DESC_1_USED) ? 1 : 0; 328 } 329 330 static inline void tx_desc_set_used(uint32_t *desc) 331 { 332 desc[1] |= DESC_1_USED; 333 } 334 335 static inline unsigned tx_desc_get_wrap(uint32_t *desc) 336 { 337 return (desc[1] & DESC_1_TX_WRAP) ? 1 : 0; 338 } 339 340 static inline unsigned tx_desc_get_last(uint32_t *desc) 341 { 342 return (desc[1] & DESC_1_TX_LAST) ? 1 : 0; 343 } 344 345 static inline void tx_desc_set_last(uint32_t *desc) 346 { 347 desc[1] |= DESC_1_TX_LAST; 348 } 349 350 static inline unsigned tx_desc_get_length(uint32_t *desc) 351 { 352 return desc[1] & DESC_1_LENGTH; 353 } 354 355 static inline void print_gem_tx_desc(uint32_t *desc, uint8_t queue) 356 { 357 DB_PRINT("TXDESC (queue %" PRId8 "):\n", queue); 358 DB_PRINT("bufaddr: 0x%08x\n", *desc); 359 DB_PRINT("used_hw: %d\n", tx_desc_get_used(desc)); 360 DB_PRINT("wrap: %d\n", tx_desc_get_wrap(desc)); 361 DB_PRINT("last: %d\n", tx_desc_get_last(desc)); 362 DB_PRINT("length: %d\n", tx_desc_get_length(desc)); 363 } 364 365 static inline uint64_t rx_desc_get_buffer(CadenceGEMState *s, uint32_t *desc) 366 { 367 uint64_t ret = desc[0] & ~0x3UL; 368 369 if (s->regs[GEM_DMACFG] & GEM_DMACFG_ADDR_64B) { 370 ret |= (uint64_t)desc[2] << 32; 371 } 372 return ret; 373 } 374 375 static inline int gem_get_desc_len(CadenceGEMState *s, bool rx_n_tx) 376 { 377 int ret = 2; 378 379 if (s->regs[GEM_DMACFG] & GEM_DMACFG_ADDR_64B) { 380 ret += 2; 381 } 382 if (s->regs[GEM_DMACFG] & (rx_n_tx ? GEM_DMACFG_RX_BD_EXT 383 : GEM_DMACFG_TX_BD_EXT)) { 384 ret += 2; 385 } 386 387 assert(ret <= DESC_MAX_NUM_WORDS); 388 return ret; 389 } 390 391 static inline unsigned rx_desc_get_wrap(uint32_t *desc) 392 { 393 return desc[0] & DESC_0_RX_WRAP ? 1 : 0; 394 } 395 396 static inline unsigned rx_desc_get_ownership(uint32_t *desc) 397 { 398 return desc[0] & DESC_0_RX_OWNERSHIP ? 1 : 0; 399 } 400 401 static inline void rx_desc_set_ownership(uint32_t *desc) 402 { 403 desc[0] |= DESC_0_RX_OWNERSHIP; 404 } 405 406 static inline void rx_desc_set_sof(uint32_t *desc) 407 { 408 desc[1] |= DESC_1_RX_SOF; 409 } 410 411 static inline void rx_desc_set_eof(uint32_t *desc) 412 { 413 desc[1] |= DESC_1_RX_EOF; 414 } 415 416 static inline void rx_desc_set_length(uint32_t *desc, unsigned len) 417 { 418 desc[1] &= ~DESC_1_LENGTH; 419 desc[1] |= len; 420 } 421 422 static inline void rx_desc_set_broadcast(uint32_t *desc) 423 { 424 desc[1] |= R_DESC_1_RX_BROADCAST; 425 } 426 427 static inline void rx_desc_set_unicast_hash(uint32_t *desc) 428 { 429 desc[1] |= R_DESC_1_RX_UNICAST_HASH; 430 } 431 432 static inline void rx_desc_set_multicast_hash(uint32_t *desc) 433 { 434 desc[1] |= R_DESC_1_RX_MULTICAST_HASH; 435 } 436 437 static inline void rx_desc_set_sar(uint32_t *desc, int sar_idx) 438 { 439 desc[1] = deposit32(desc[1], R_DESC_1_RX_SAR_SHIFT, R_DESC_1_RX_SAR_LENGTH, 440 sar_idx); 441 desc[1] |= R_DESC_1_RX_SAR_MATCH; 442 } 443 444 /* The broadcast MAC address: 0xFFFFFFFFFFFF */ 445 static const uint8_t broadcast_addr[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }; 446 447 /* 448 * gem_init_register_masks: 449 * One time initialization. 450 * Set masks to identify which register bits have magical clear properties 451 */ 452 static void gem_init_register_masks(CadenceGEMState *s) 453 { 454 /* Mask of register bits which are read only */ 455 memset(&s->regs_ro[0], 0, sizeof(s->regs_ro)); 456 s->regs_ro[GEM_NWCTRL] = 0xFFF80000; 457 s->regs_ro[GEM_NWSTATUS] = 0xFFFFFFFF; 458 s->regs_ro[GEM_DMACFG] = 0x8E00F000; 459 s->regs_ro[GEM_TXSTATUS] = 0xFFFFFE08; 460 s->regs_ro[GEM_RXQBASE] = 0x00000003; 461 s->regs_ro[GEM_TXQBASE] = 0x00000003; 462 s->regs_ro[GEM_RXSTATUS] = 0xFFFFFFF0; 463 s->regs_ro[GEM_ISR] = 0xFFFFFFFF; 464 s->regs_ro[GEM_IMR] = 0xFFFFFFFF; 465 s->regs_ro[GEM_MODID] = 0xFFFFFFFF; 466 467 /* Mask of register bits which are clear on read */ 468 memset(&s->regs_rtc[0], 0, sizeof(s->regs_rtc)); 469 s->regs_rtc[GEM_ISR] = 0xFFFFFFFF; 470 471 /* Mask of register bits which are write 1 to clear */ 472 memset(&s->regs_w1c[0], 0, sizeof(s->regs_w1c)); 473 s->regs_w1c[GEM_TXSTATUS] = 0x000001F7; 474 s->regs_w1c[GEM_RXSTATUS] = 0x0000000F; 475 476 /* Mask of register bits which are write only */ 477 memset(&s->regs_wo[0], 0, sizeof(s->regs_wo)); 478 s->regs_wo[GEM_NWCTRL] = 0x00073E60; 479 s->regs_wo[GEM_IER] = 0x07FFFFFF; 480 s->regs_wo[GEM_IDR] = 0x07FFFFFF; 481 } 482 483 /* 484 * phy_update_link: 485 * Make the emulated PHY link state match the QEMU "interface" state. 486 */ 487 static void phy_update_link(CadenceGEMState *s) 488 { 489 DB_PRINT("down %d\n", qemu_get_queue(s->nic)->link_down); 490 491 /* Autonegotiation status mirrors link status. */ 492 if (qemu_get_queue(s->nic)->link_down) { 493 s->phy_regs[PHY_REG_STATUS] &= ~(PHY_REG_STATUS_ANEGCMPL | 494 PHY_REG_STATUS_LINK); 495 s->phy_regs[PHY_REG_INT_ST] |= PHY_REG_INT_ST_LINKC; 496 } else { 497 s->phy_regs[PHY_REG_STATUS] |= (PHY_REG_STATUS_ANEGCMPL | 498 PHY_REG_STATUS_LINK); 499 s->phy_regs[PHY_REG_INT_ST] |= (PHY_REG_INT_ST_LINKC | 500 PHY_REG_INT_ST_ANEGCMPL | 501 PHY_REG_INT_ST_ENERGY); 502 } 503 } 504 505 static int gem_can_receive(NetClientState *nc) 506 { 507 CadenceGEMState *s; 508 int i; 509 510 s = qemu_get_nic_opaque(nc); 511 512 /* Do nothing if receive is not enabled. */ 513 if (!(s->regs[GEM_NWCTRL] & GEM_NWCTRL_RXENA)) { 514 if (s->can_rx_state != 1) { 515 s->can_rx_state = 1; 516 DB_PRINT("can't receive - no enable\n"); 517 } 518 return 0; 519 } 520 521 for (i = 0; i < s->num_priority_queues; i++) { 522 if (rx_desc_get_ownership(s->rx_desc[i]) != 1) { 523 break; 524 } 525 }; 526 527 if (i == s->num_priority_queues) { 528 if (s->can_rx_state != 2) { 529 s->can_rx_state = 2; 530 DB_PRINT("can't receive - all the buffer descriptors are busy\n"); 531 } 532 return 0; 533 } 534 535 if (s->can_rx_state != 0) { 536 s->can_rx_state = 0; 537 DB_PRINT("can receive\n"); 538 } 539 return 1; 540 } 541 542 /* 543 * gem_update_int_status: 544 * Raise or lower interrupt based on current status. 545 */ 546 static void gem_update_int_status(CadenceGEMState *s) 547 { 548 int i; 549 550 if (!s->regs[GEM_ISR]) { 551 /* ISR isn't set, clear all the interrupts */ 552 for (i = 0; i < s->num_priority_queues; ++i) { 553 qemu_set_irq(s->irq[i], 0); 554 } 555 return; 556 } 557 558 /* If we get here we know s->regs[GEM_ISR] is set, so we don't need to 559 * check it again. 560 */ 561 if (s->num_priority_queues == 1) { 562 /* No priority queues, just trigger the interrupt */ 563 DB_PRINT("asserting int.\n"); 564 qemu_set_irq(s->irq[0], 1); 565 return; 566 } 567 568 for (i = 0; i < s->num_priority_queues; ++i) { 569 if (s->regs[GEM_INT_Q1_STATUS + i]) { 570 DB_PRINT("asserting int. (q=%d)\n", i); 571 qemu_set_irq(s->irq[i], 1); 572 } 573 } 574 } 575 576 /* 577 * gem_receive_updatestats: 578 * Increment receive statistics. 579 */ 580 static void gem_receive_updatestats(CadenceGEMState *s, const uint8_t *packet, 581 unsigned bytes) 582 { 583 uint64_t octets; 584 585 /* Total octets (bytes) received */ 586 octets = ((uint64_t)(s->regs[GEM_OCTRXLO]) << 32) | 587 s->regs[GEM_OCTRXHI]; 588 octets += bytes; 589 s->regs[GEM_OCTRXLO] = octets >> 32; 590 s->regs[GEM_OCTRXHI] = octets; 591 592 /* Error-free Frames received */ 593 s->regs[GEM_RXCNT]++; 594 595 /* Error-free Broadcast Frames counter */ 596 if (!memcmp(packet, broadcast_addr, 6)) { 597 s->regs[GEM_RXBROADCNT]++; 598 } 599 600 /* Error-free Multicast Frames counter */ 601 if (packet[0] == 0x01) { 602 s->regs[GEM_RXMULTICNT]++; 603 } 604 605 if (bytes <= 64) { 606 s->regs[GEM_RX64CNT]++; 607 } else if (bytes <= 127) { 608 s->regs[GEM_RX65CNT]++; 609 } else if (bytes <= 255) { 610 s->regs[GEM_RX128CNT]++; 611 } else if (bytes <= 511) { 612 s->regs[GEM_RX256CNT]++; 613 } else if (bytes <= 1023) { 614 s->regs[GEM_RX512CNT]++; 615 } else if (bytes <= 1518) { 616 s->regs[GEM_RX1024CNT]++; 617 } else { 618 s->regs[GEM_RX1519CNT]++; 619 } 620 } 621 622 /* 623 * Get the MAC Address bit from the specified position 624 */ 625 static unsigned get_bit(const uint8_t *mac, unsigned bit) 626 { 627 unsigned byte; 628 629 byte = mac[bit / 8]; 630 byte >>= (bit & 0x7); 631 byte &= 1; 632 633 return byte; 634 } 635 636 /* 637 * Calculate a GEM MAC Address hash index 638 */ 639 static unsigned calc_mac_hash(const uint8_t *mac) 640 { 641 int index_bit, mac_bit; 642 unsigned hash_index; 643 644 hash_index = 0; 645 mac_bit = 5; 646 for (index_bit = 5; index_bit >= 0; index_bit--) { 647 hash_index |= (get_bit(mac, mac_bit) ^ 648 get_bit(mac, mac_bit + 6) ^ 649 get_bit(mac, mac_bit + 12) ^ 650 get_bit(mac, mac_bit + 18) ^ 651 get_bit(mac, mac_bit + 24) ^ 652 get_bit(mac, mac_bit + 30) ^ 653 get_bit(mac, mac_bit + 36) ^ 654 get_bit(mac, mac_bit + 42)) << index_bit; 655 mac_bit--; 656 } 657 658 return hash_index; 659 } 660 661 /* 662 * gem_mac_address_filter: 663 * Accept or reject this destination address? 664 * Returns: 665 * GEM_RX_REJECT: reject 666 * >= 0: Specific address accept (which matched SAR is returned) 667 * others for various other modes of accept: 668 * GEM_RM_PROMISCUOUS_ACCEPT, GEM_RX_BROADCAST_ACCEPT, 669 * GEM_RX_MULTICAST_HASH_ACCEPT or GEM_RX_UNICAST_HASH_ACCEPT 670 */ 671 static int gem_mac_address_filter(CadenceGEMState *s, const uint8_t *packet) 672 { 673 uint8_t *gem_spaddr; 674 int i; 675 676 /* Promiscuous mode? */ 677 if (s->regs[GEM_NWCFG] & GEM_NWCFG_PROMISC) { 678 return GEM_RX_PROMISCUOUS_ACCEPT; 679 } 680 681 if (!memcmp(packet, broadcast_addr, 6)) { 682 /* Reject broadcast packets? */ 683 if (s->regs[GEM_NWCFG] & GEM_NWCFG_BCAST_REJ) { 684 return GEM_RX_REJECT; 685 } 686 return GEM_RX_BROADCAST_ACCEPT; 687 } 688 689 /* Accept packets -w- hash match? */ 690 if ((packet[0] == 0x01 && (s->regs[GEM_NWCFG] & GEM_NWCFG_MCAST_HASH)) || 691 (packet[0] != 0x01 && (s->regs[GEM_NWCFG] & GEM_NWCFG_UCAST_HASH))) { 692 unsigned hash_index; 693 694 hash_index = calc_mac_hash(packet); 695 if (hash_index < 32) { 696 if (s->regs[GEM_HASHLO] & (1<<hash_index)) { 697 return packet[0] == 0x01 ? GEM_RX_MULTICAST_HASH_ACCEPT : 698 GEM_RX_UNICAST_HASH_ACCEPT; 699 } 700 } else { 701 hash_index -= 32; 702 if (s->regs[GEM_HASHHI] & (1<<hash_index)) { 703 return packet[0] == 0x01 ? GEM_RX_MULTICAST_HASH_ACCEPT : 704 GEM_RX_UNICAST_HASH_ACCEPT; 705 } 706 } 707 } 708 709 /* Check all 4 specific addresses */ 710 gem_spaddr = (uint8_t *)&(s->regs[GEM_SPADDR1LO]); 711 for (i = 3; i >= 0; i--) { 712 if (s->sar_active[i] && !memcmp(packet, gem_spaddr + 8 * i, 6)) { 713 return GEM_RX_SAR_ACCEPT + i; 714 } 715 } 716 717 /* No address match; reject the packet */ 718 return GEM_RX_REJECT; 719 } 720 721 /* Figure out which queue the received data should be sent to */ 722 static int get_queue_from_screen(CadenceGEMState *s, uint8_t *rxbuf_ptr, 723 unsigned rxbufsize) 724 { 725 uint32_t reg; 726 bool matched, mismatched; 727 int i, j; 728 729 for (i = 0; i < s->num_type1_screeners; i++) { 730 reg = s->regs[GEM_SCREENING_TYPE1_REGISTER_0 + i]; 731 matched = false; 732 mismatched = false; 733 734 /* Screening is based on UDP Port */ 735 if (reg & GEM_ST1R_UDP_PORT_MATCH_ENABLE) { 736 uint16_t udp_port = rxbuf_ptr[14 + 22] << 8 | rxbuf_ptr[14 + 23]; 737 if (udp_port == extract32(reg, GEM_ST1R_UDP_PORT_MATCH_SHIFT, 738 GEM_ST1R_UDP_PORT_MATCH_WIDTH)) { 739 matched = true; 740 } else { 741 mismatched = true; 742 } 743 } 744 745 /* Screening is based on DS/TC */ 746 if (reg & GEM_ST1R_DSTC_ENABLE) { 747 uint8_t dscp = rxbuf_ptr[14 + 1]; 748 if (dscp == extract32(reg, GEM_ST1R_DSTC_MATCH_SHIFT, 749 GEM_ST1R_DSTC_MATCH_WIDTH)) { 750 matched = true; 751 } else { 752 mismatched = true; 753 } 754 } 755 756 if (matched && !mismatched) { 757 return extract32(reg, GEM_ST1R_QUEUE_SHIFT, GEM_ST1R_QUEUE_WIDTH); 758 } 759 } 760 761 for (i = 0; i < s->num_type2_screeners; i++) { 762 reg = s->regs[GEM_SCREENING_TYPE2_REGISTER_0 + i]; 763 matched = false; 764 mismatched = false; 765 766 if (reg & GEM_ST2R_ETHERTYPE_ENABLE) { 767 uint16_t type = rxbuf_ptr[12] << 8 | rxbuf_ptr[13]; 768 int et_idx = extract32(reg, GEM_ST2R_ETHERTYPE_INDEX_SHIFT, 769 GEM_ST2R_ETHERTYPE_INDEX_WIDTH); 770 771 if (et_idx > s->num_type2_screeners) { 772 qemu_log_mask(LOG_GUEST_ERROR, "Out of range ethertype " 773 "register index: %d\n", et_idx); 774 } 775 if (type == s->regs[GEM_SCREENING_TYPE2_ETHERTYPE_REG_0 + 776 et_idx]) { 777 matched = true; 778 } else { 779 mismatched = true; 780 } 781 } 782 783 /* Compare A, B, C */ 784 for (j = 0; j < 3; j++) { 785 uint32_t cr0, cr1, mask; 786 uint16_t rx_cmp; 787 int offset; 788 int cr_idx = extract32(reg, GEM_ST2R_COMPARE_A_SHIFT + j * 6, 789 GEM_ST2R_COMPARE_WIDTH); 790 791 if (!(reg & (GEM_ST2R_COMPARE_A_ENABLE << (j * 6)))) { 792 continue; 793 } 794 if (cr_idx > s->num_type2_screeners) { 795 qemu_log_mask(LOG_GUEST_ERROR, "Out of range compare " 796 "register index: %d\n", cr_idx); 797 } 798 799 cr0 = s->regs[GEM_TYPE2_COMPARE_0_WORD_0 + cr_idx * 2]; 800 cr1 = s->regs[GEM_TYPE2_COMPARE_0_WORD_0 + cr_idx * 2 + 1]; 801 offset = extract32(cr1, GEM_T2CW1_OFFSET_VALUE_SHIFT, 802 GEM_T2CW1_OFFSET_VALUE_WIDTH); 803 804 switch (extract32(cr1, GEM_T2CW1_COMPARE_OFFSET_SHIFT, 805 GEM_T2CW1_COMPARE_OFFSET_WIDTH)) { 806 case 3: /* Skip UDP header */ 807 qemu_log_mask(LOG_UNIMP, "TCP compare offsets" 808 "unimplemented - assuming UDP\n"); 809 offset += 8; 810 /* Fallthrough */ 811 case 2: /* skip the IP header */ 812 offset += 20; 813 /* Fallthrough */ 814 case 1: /* Count from after the ethertype */ 815 offset += 14; 816 break; 817 case 0: 818 /* Offset from start of frame */ 819 break; 820 } 821 822 rx_cmp = rxbuf_ptr[offset] << 8 | rxbuf_ptr[offset]; 823 mask = extract32(cr0, 0, 16); 824 825 if ((rx_cmp & mask) == (extract32(cr0, 16, 16) & mask)) { 826 matched = true; 827 } else { 828 mismatched = true; 829 } 830 } 831 832 if (matched && !mismatched) { 833 return extract32(reg, GEM_ST2R_QUEUE_SHIFT, GEM_ST2R_QUEUE_WIDTH); 834 } 835 } 836 837 /* We made it here, assume it's queue 0 */ 838 return 0; 839 } 840 841 static hwaddr gem_get_desc_addr(CadenceGEMState *s, bool tx, int q) 842 { 843 hwaddr desc_addr = 0; 844 845 if (s->regs[GEM_DMACFG] & GEM_DMACFG_ADDR_64B) { 846 desc_addr = s->regs[tx ? GEM_TBQPH : GEM_RBQPH]; 847 } 848 desc_addr <<= 32; 849 desc_addr |= tx ? s->tx_desc_addr[q] : s->rx_desc_addr[q]; 850 return desc_addr; 851 } 852 853 static hwaddr gem_get_tx_desc_addr(CadenceGEMState *s, int q) 854 { 855 return gem_get_desc_addr(s, true, q); 856 } 857 858 static hwaddr gem_get_rx_desc_addr(CadenceGEMState *s, int q) 859 { 860 return gem_get_desc_addr(s, false, q); 861 } 862 863 static void gem_get_rx_desc(CadenceGEMState *s, int q) 864 { 865 hwaddr desc_addr = gem_get_rx_desc_addr(s, q); 866 867 DB_PRINT("read descriptor 0x%" HWADDR_PRIx "\n", desc_addr); 868 869 /* read current descriptor */ 870 address_space_read(&s->dma_as, desc_addr, MEMTXATTRS_UNSPECIFIED, 871 (uint8_t *)s->rx_desc[q], 872 sizeof(uint32_t) * gem_get_desc_len(s, true)); 873 874 /* Descriptor owned by software ? */ 875 if (rx_desc_get_ownership(s->rx_desc[q]) == 1) { 876 DB_PRINT("descriptor 0x%" HWADDR_PRIx " owned by sw.\n", desc_addr); 877 s->regs[GEM_RXSTATUS] |= GEM_RXSTATUS_NOBUF; 878 s->regs[GEM_ISR] |= GEM_INT_RXUSED & ~(s->regs[GEM_IMR]); 879 /* Handle interrupt consequences */ 880 gem_update_int_status(s); 881 } 882 } 883 884 /* 885 * gem_receive: 886 * Fit a packet handed to us by QEMU into the receive descriptor ring. 887 */ 888 static ssize_t gem_receive(NetClientState *nc, const uint8_t *buf, size_t size) 889 { 890 CadenceGEMState *s; 891 unsigned rxbufsize, bytes_to_copy; 892 unsigned rxbuf_offset; 893 uint8_t rxbuf[2048]; 894 uint8_t *rxbuf_ptr; 895 bool first_desc = true; 896 int maf; 897 int q = 0; 898 899 s = qemu_get_nic_opaque(nc); 900 901 /* Is this destination MAC address "for us" ? */ 902 maf = gem_mac_address_filter(s, buf); 903 if (maf == GEM_RX_REJECT) { 904 return -1; 905 } 906 907 /* Discard packets with receive length error enabled ? */ 908 if (s->regs[GEM_NWCFG] & GEM_NWCFG_LERR_DISC) { 909 unsigned type_len; 910 911 /* Fish the ethertype / length field out of the RX packet */ 912 type_len = buf[12] << 8 | buf[13]; 913 /* It is a length field, not an ethertype */ 914 if (type_len < 0x600) { 915 if (size < type_len) { 916 /* discard */ 917 return -1; 918 } 919 } 920 } 921 922 /* 923 * Determine configured receive buffer offset (probably 0) 924 */ 925 rxbuf_offset = (s->regs[GEM_NWCFG] & GEM_NWCFG_BUFF_OFST_M) >> 926 GEM_NWCFG_BUFF_OFST_S; 927 928 /* The configure size of each receive buffer. Determines how many 929 * buffers needed to hold this packet. 930 */ 931 rxbufsize = ((s->regs[GEM_DMACFG] & GEM_DMACFG_RBUFSZ_M) >> 932 GEM_DMACFG_RBUFSZ_S) * GEM_DMACFG_RBUFSZ_MUL; 933 bytes_to_copy = size; 934 935 /* Hardware allows a zero value here but warns against it. To avoid QEMU 936 * indefinite loops we enforce a minimum value here 937 */ 938 if (rxbufsize < GEM_DMACFG_RBUFSZ_MUL) { 939 rxbufsize = GEM_DMACFG_RBUFSZ_MUL; 940 } 941 942 /* Pad to minimum length. Assume FCS field is stripped, logic 943 * below will increment it to the real minimum of 64 when 944 * not FCS stripping 945 */ 946 if (size < 60) { 947 size = 60; 948 } 949 950 /* Strip of FCS field ? (usually yes) */ 951 if (s->regs[GEM_NWCFG] & GEM_NWCFG_STRIP_FCS) { 952 rxbuf_ptr = (void *)buf; 953 } else { 954 unsigned crc_val; 955 956 if (size > sizeof(rxbuf) - sizeof(crc_val)) { 957 size = sizeof(rxbuf) - sizeof(crc_val); 958 } 959 bytes_to_copy = size; 960 /* The application wants the FCS field, which QEMU does not provide. 961 * We must try and calculate one. 962 */ 963 964 memcpy(rxbuf, buf, size); 965 memset(rxbuf + size, 0, sizeof(rxbuf) - size); 966 rxbuf_ptr = rxbuf; 967 crc_val = cpu_to_le32(crc32(0, rxbuf, MAX(size, 60))); 968 memcpy(rxbuf + size, &crc_val, sizeof(crc_val)); 969 970 bytes_to_copy += 4; 971 size += 4; 972 } 973 974 DB_PRINT("config bufsize: %d packet size: %ld\n", rxbufsize, size); 975 976 /* Find which queue we are targeting */ 977 q = get_queue_from_screen(s, rxbuf_ptr, rxbufsize); 978 979 while (bytes_to_copy) { 980 hwaddr desc_addr; 981 982 /* Do nothing if receive is not enabled. */ 983 if (!gem_can_receive(nc)) { 984 return -1; 985 } 986 987 DB_PRINT("copy %d bytes to 0x%x\n", MIN(bytes_to_copy, rxbufsize), 988 rx_desc_get_buffer(s->rx_desc[q])); 989 990 /* Copy packet data to emulated DMA buffer */ 991 address_space_write(&s->dma_as, rx_desc_get_buffer(s, s->rx_desc[q]) + 992 rxbuf_offset, 993 MEMTXATTRS_UNSPECIFIED, rxbuf_ptr, 994 MIN(bytes_to_copy, rxbufsize)); 995 rxbuf_ptr += MIN(bytes_to_copy, rxbufsize); 996 bytes_to_copy -= MIN(bytes_to_copy, rxbufsize); 997 998 /* Update the descriptor. */ 999 if (first_desc) { 1000 rx_desc_set_sof(s->rx_desc[q]); 1001 first_desc = false; 1002 } 1003 if (bytes_to_copy == 0) { 1004 rx_desc_set_eof(s->rx_desc[q]); 1005 rx_desc_set_length(s->rx_desc[q], size); 1006 } 1007 rx_desc_set_ownership(s->rx_desc[q]); 1008 1009 switch (maf) { 1010 case GEM_RX_PROMISCUOUS_ACCEPT: 1011 break; 1012 case GEM_RX_BROADCAST_ACCEPT: 1013 rx_desc_set_broadcast(s->rx_desc[q]); 1014 break; 1015 case GEM_RX_UNICAST_HASH_ACCEPT: 1016 rx_desc_set_unicast_hash(s->rx_desc[q]); 1017 break; 1018 case GEM_RX_MULTICAST_HASH_ACCEPT: 1019 rx_desc_set_multicast_hash(s->rx_desc[q]); 1020 break; 1021 case GEM_RX_REJECT: 1022 abort(); 1023 default: /* SAR */ 1024 rx_desc_set_sar(s->rx_desc[q], maf); 1025 } 1026 1027 /* Descriptor write-back. */ 1028 desc_addr = gem_get_rx_desc_addr(s, q); 1029 address_space_write(&s->dma_as, desc_addr, 1030 MEMTXATTRS_UNSPECIFIED, 1031 (uint8_t *)s->rx_desc[q], 1032 sizeof(uint32_t) * gem_get_desc_len(s, true)); 1033 1034 /* Next descriptor */ 1035 if (rx_desc_get_wrap(s->rx_desc[q])) { 1036 DB_PRINT("wrapping RX descriptor list\n"); 1037 s->rx_desc_addr[q] = s->regs[GEM_RXQBASE]; 1038 } else { 1039 DB_PRINT("incrementing RX descriptor list\n"); 1040 s->rx_desc_addr[q] += 4 * gem_get_desc_len(s, true); 1041 } 1042 1043 gem_get_rx_desc(s, q); 1044 } 1045 1046 /* Count it */ 1047 gem_receive_updatestats(s, buf, size); 1048 1049 s->regs[GEM_RXSTATUS] |= GEM_RXSTATUS_FRMRCVD; 1050 s->regs[GEM_ISR] |= GEM_INT_RXCMPL & ~(s->regs[GEM_IMR]); 1051 1052 /* Handle interrupt consequences */ 1053 gem_update_int_status(s); 1054 1055 return size; 1056 } 1057 1058 /* 1059 * gem_transmit_updatestats: 1060 * Increment transmit statistics. 1061 */ 1062 static void gem_transmit_updatestats(CadenceGEMState *s, const uint8_t *packet, 1063 unsigned bytes) 1064 { 1065 uint64_t octets; 1066 1067 /* Total octets (bytes) transmitted */ 1068 octets = ((uint64_t)(s->regs[GEM_OCTTXLO]) << 32) | 1069 s->regs[GEM_OCTTXHI]; 1070 octets += bytes; 1071 s->regs[GEM_OCTTXLO] = octets >> 32; 1072 s->regs[GEM_OCTTXHI] = octets; 1073 1074 /* Error-free Frames transmitted */ 1075 s->regs[GEM_TXCNT]++; 1076 1077 /* Error-free Broadcast Frames counter */ 1078 if (!memcmp(packet, broadcast_addr, 6)) { 1079 s->regs[GEM_TXBCNT]++; 1080 } 1081 1082 /* Error-free Multicast Frames counter */ 1083 if (packet[0] == 0x01) { 1084 s->regs[GEM_TXMCNT]++; 1085 } 1086 1087 if (bytes <= 64) { 1088 s->regs[GEM_TX64CNT]++; 1089 } else if (bytes <= 127) { 1090 s->regs[GEM_TX65CNT]++; 1091 } else if (bytes <= 255) { 1092 s->regs[GEM_TX128CNT]++; 1093 } else if (bytes <= 511) { 1094 s->regs[GEM_TX256CNT]++; 1095 } else if (bytes <= 1023) { 1096 s->regs[GEM_TX512CNT]++; 1097 } else if (bytes <= 1518) { 1098 s->regs[GEM_TX1024CNT]++; 1099 } else { 1100 s->regs[GEM_TX1519CNT]++; 1101 } 1102 } 1103 1104 /* 1105 * gem_transmit: 1106 * Fish packets out of the descriptor ring and feed them to QEMU 1107 */ 1108 static void gem_transmit(CadenceGEMState *s) 1109 { 1110 uint32_t desc[DESC_MAX_NUM_WORDS]; 1111 hwaddr packet_desc_addr; 1112 uint8_t tx_packet[2048]; 1113 uint8_t *p; 1114 unsigned total_bytes; 1115 int q = 0; 1116 1117 /* Do nothing if transmit is not enabled. */ 1118 if (!(s->regs[GEM_NWCTRL] & GEM_NWCTRL_TXENA)) { 1119 return; 1120 } 1121 1122 DB_PRINT("\n"); 1123 1124 /* The packet we will hand off to QEMU. 1125 * Packets scattered across multiple descriptors are gathered to this 1126 * one contiguous buffer first. 1127 */ 1128 p = tx_packet; 1129 total_bytes = 0; 1130 1131 for (q = s->num_priority_queues - 1; q >= 0; q--) { 1132 /* read current descriptor */ 1133 packet_desc_addr = gem_get_tx_desc_addr(s, q); 1134 1135 DB_PRINT("read descriptor 0x%" HWADDR_PRIx "\n", packet_desc_addr); 1136 address_space_read(&s->dma_as, packet_desc_addr, 1137 MEMTXATTRS_UNSPECIFIED, (uint8_t *)desc, 1138 sizeof(uint32_t) * gem_get_desc_len(s, false)); 1139 /* Handle all descriptors owned by hardware */ 1140 while (tx_desc_get_used(desc) == 0) { 1141 1142 /* Do nothing if transmit is not enabled. */ 1143 if (!(s->regs[GEM_NWCTRL] & GEM_NWCTRL_TXENA)) { 1144 return; 1145 } 1146 print_gem_tx_desc(desc, q); 1147 1148 /* The real hardware would eat this (and possibly crash). 1149 * For QEMU let's lend a helping hand. 1150 */ 1151 if ((tx_desc_get_buffer(s, desc) == 0) || 1152 (tx_desc_get_length(desc) == 0)) { 1153 DB_PRINT("Invalid TX descriptor @ 0x%x\n", 1154 (unsigned)packet_desc_addr); 1155 break; 1156 } 1157 1158 if (tx_desc_get_length(desc) > sizeof(tx_packet) - 1159 (p - tx_packet)) { 1160 DB_PRINT("TX descriptor @ 0x%x too large: size 0x%x space " \ 1161 "0x%x\n", (unsigned)packet_desc_addr, 1162 (unsigned)tx_desc_get_length(desc), 1163 sizeof(tx_packet) - (p - tx_packet)); 1164 break; 1165 } 1166 1167 /* Gather this fragment of the packet from "dma memory" to our 1168 * contig buffer. 1169 */ 1170 address_space_read(&s->dma_as, tx_desc_get_buffer(s, desc), 1171 MEMTXATTRS_UNSPECIFIED, 1172 p, tx_desc_get_length(desc)); 1173 p += tx_desc_get_length(desc); 1174 total_bytes += tx_desc_get_length(desc); 1175 1176 /* Last descriptor for this packet; hand the whole thing off */ 1177 if (tx_desc_get_last(desc)) { 1178 uint32_t desc_first[DESC_MAX_NUM_WORDS]; 1179 hwaddr desc_addr = gem_get_tx_desc_addr(s, q); 1180 1181 /* Modify the 1st descriptor of this packet to be owned by 1182 * the processor. 1183 */ 1184 address_space_read(&s->dma_as, desc_addr, 1185 MEMTXATTRS_UNSPECIFIED, 1186 (uint8_t *)desc_first, 1187 sizeof(desc_first)); 1188 tx_desc_set_used(desc_first); 1189 address_space_write(&s->dma_as, desc_addr, 1190 MEMTXATTRS_UNSPECIFIED, 1191 (uint8_t *)desc_first, 1192 sizeof(desc_first)); 1193 /* Advance the hardware current descriptor past this packet */ 1194 if (tx_desc_get_wrap(desc)) { 1195 s->tx_desc_addr[q] = s->regs[GEM_TXQBASE]; 1196 } else { 1197 s->tx_desc_addr[q] = packet_desc_addr + 1198 4 * gem_get_desc_len(s, false); 1199 } 1200 DB_PRINT("TX descriptor next: 0x%08x\n", s->tx_desc_addr[q]); 1201 1202 s->regs[GEM_TXSTATUS] |= GEM_TXSTATUS_TXCMPL; 1203 s->regs[GEM_ISR] |= GEM_INT_TXCMPL & ~(s->regs[GEM_IMR]); 1204 1205 /* Update queue interrupt status */ 1206 if (s->num_priority_queues > 1) { 1207 s->regs[GEM_INT_Q1_STATUS + q] |= 1208 GEM_INT_TXCMPL & ~(s->regs[GEM_INT_Q1_MASK + q]); 1209 } 1210 1211 /* Handle interrupt consequences */ 1212 gem_update_int_status(s); 1213 1214 /* Is checksum offload enabled? */ 1215 if (s->regs[GEM_DMACFG] & GEM_DMACFG_TXCSUM_OFFL) { 1216 net_checksum_calculate(tx_packet, total_bytes); 1217 } 1218 1219 /* Update MAC statistics */ 1220 gem_transmit_updatestats(s, tx_packet, total_bytes); 1221 1222 /* Send the packet somewhere */ 1223 if (s->phy_loop || (s->regs[GEM_NWCTRL] & 1224 GEM_NWCTRL_LOCALLOOP)) { 1225 gem_receive(qemu_get_queue(s->nic), tx_packet, 1226 total_bytes); 1227 } else { 1228 qemu_send_packet(qemu_get_queue(s->nic), tx_packet, 1229 total_bytes); 1230 } 1231 1232 /* Prepare for next packet */ 1233 p = tx_packet; 1234 total_bytes = 0; 1235 } 1236 1237 /* read next descriptor */ 1238 if (tx_desc_get_wrap(desc)) { 1239 tx_desc_set_last(desc); 1240 packet_desc_addr = s->regs[GEM_TXQBASE]; 1241 } else { 1242 packet_desc_addr += 4 * gem_get_desc_len(s, false); 1243 } 1244 DB_PRINT("read descriptor 0x%" HWADDR_PRIx "\n", packet_desc_addr); 1245 address_space_read(&s->dma_as, packet_desc_addr, 1246 MEMTXATTRS_UNSPECIFIED, (uint8_t *)desc, 1247 sizeof(uint32_t) * gem_get_desc_len(s, false)); 1248 } 1249 1250 if (tx_desc_get_used(desc)) { 1251 s->regs[GEM_TXSTATUS] |= GEM_TXSTATUS_USED; 1252 s->regs[GEM_ISR] |= GEM_INT_TXUSED & ~(s->regs[GEM_IMR]); 1253 gem_update_int_status(s); 1254 } 1255 } 1256 } 1257 1258 static void gem_phy_reset(CadenceGEMState *s) 1259 { 1260 memset(&s->phy_regs[0], 0, sizeof(s->phy_regs)); 1261 s->phy_regs[PHY_REG_CONTROL] = 0x1140; 1262 s->phy_regs[PHY_REG_STATUS] = 0x7969; 1263 s->phy_regs[PHY_REG_PHYID1] = 0x0141; 1264 s->phy_regs[PHY_REG_PHYID2] = 0x0CC2; 1265 s->phy_regs[PHY_REG_ANEGADV] = 0x01E1; 1266 s->phy_regs[PHY_REG_LINKPABIL] = 0xCDE1; 1267 s->phy_regs[PHY_REG_ANEGEXP] = 0x000F; 1268 s->phy_regs[PHY_REG_NEXTP] = 0x2001; 1269 s->phy_regs[PHY_REG_LINKPNEXTP] = 0x40E6; 1270 s->phy_regs[PHY_REG_100BTCTRL] = 0x0300; 1271 s->phy_regs[PHY_REG_1000BTSTAT] = 0x7C00; 1272 s->phy_regs[PHY_REG_EXTSTAT] = 0x3000; 1273 s->phy_regs[PHY_REG_PHYSPCFC_CTL] = 0x0078; 1274 s->phy_regs[PHY_REG_PHYSPCFC_ST] = 0x7C00; 1275 s->phy_regs[PHY_REG_EXT_PHYSPCFC_CTL] = 0x0C60; 1276 s->phy_regs[PHY_REG_LED] = 0x4100; 1277 s->phy_regs[PHY_REG_EXT_PHYSPCFC_CTL2] = 0x000A; 1278 s->phy_regs[PHY_REG_EXT_PHYSPCFC_ST] = 0x848B; 1279 1280 phy_update_link(s); 1281 } 1282 1283 static void gem_reset(DeviceState *d) 1284 { 1285 int i; 1286 CadenceGEMState *s = CADENCE_GEM(d); 1287 const uint8_t *a; 1288 uint32_t queues_mask = 0; 1289 1290 DB_PRINT("\n"); 1291 1292 /* Set post reset register values */ 1293 memset(&s->regs[0], 0, sizeof(s->regs)); 1294 s->regs[GEM_NWCFG] = 0x00080000; 1295 s->regs[GEM_NWSTATUS] = 0x00000006; 1296 s->regs[GEM_DMACFG] = 0x00020784; 1297 s->regs[GEM_IMR] = 0x07ffffff; 1298 s->regs[GEM_TXPAUSE] = 0x0000ffff; 1299 s->regs[GEM_TXPARTIALSF] = 0x000003ff; 1300 s->regs[GEM_RXPARTIALSF] = 0x000003ff; 1301 s->regs[GEM_MODID] = s->revision; 1302 s->regs[GEM_DESCONF] = 0x02500111; 1303 s->regs[GEM_DESCONF2] = 0x2ab13fff; 1304 s->regs[GEM_DESCONF5] = 0x002f2045; 1305 s->regs[GEM_DESCONF6] = GEM_DESCONF6_64B_MASK; 1306 1307 if (s->num_priority_queues > 1) { 1308 queues_mask = MAKE_64BIT_MASK(1, s->num_priority_queues - 1); 1309 s->regs[GEM_DESCONF6] |= queues_mask; 1310 } 1311 1312 /* Set MAC address */ 1313 a = &s->conf.macaddr.a[0]; 1314 s->regs[GEM_SPADDR1LO] = a[0] | (a[1] << 8) | (a[2] << 16) | (a[3] << 24); 1315 s->regs[GEM_SPADDR1HI] = a[4] | (a[5] << 8); 1316 1317 for (i = 0; i < 4; i++) { 1318 s->sar_active[i] = false; 1319 } 1320 1321 gem_phy_reset(s); 1322 1323 gem_update_int_status(s); 1324 } 1325 1326 static uint16_t gem_phy_read(CadenceGEMState *s, unsigned reg_num) 1327 { 1328 DB_PRINT("reg: %d value: 0x%04x\n", reg_num, s->phy_regs[reg_num]); 1329 return s->phy_regs[reg_num]; 1330 } 1331 1332 static void gem_phy_write(CadenceGEMState *s, unsigned reg_num, uint16_t val) 1333 { 1334 DB_PRINT("reg: %d value: 0x%04x\n", reg_num, val); 1335 1336 switch (reg_num) { 1337 case PHY_REG_CONTROL: 1338 if (val & PHY_REG_CONTROL_RST) { 1339 /* Phy reset */ 1340 gem_phy_reset(s); 1341 val &= ~(PHY_REG_CONTROL_RST | PHY_REG_CONTROL_LOOP); 1342 s->phy_loop = 0; 1343 } 1344 if (val & PHY_REG_CONTROL_ANEG) { 1345 /* Complete autonegotiation immediately */ 1346 val &= ~PHY_REG_CONTROL_ANEG; 1347 s->phy_regs[PHY_REG_STATUS] |= PHY_REG_STATUS_ANEGCMPL; 1348 } 1349 if (val & PHY_REG_CONTROL_LOOP) { 1350 DB_PRINT("PHY placed in loopback\n"); 1351 s->phy_loop = 1; 1352 } else { 1353 s->phy_loop = 0; 1354 } 1355 break; 1356 } 1357 s->phy_regs[reg_num] = val; 1358 } 1359 1360 /* 1361 * gem_read32: 1362 * Read a GEM register. 1363 */ 1364 static uint64_t gem_read(void *opaque, hwaddr offset, unsigned size) 1365 { 1366 CadenceGEMState *s; 1367 uint32_t retval; 1368 s = (CadenceGEMState *)opaque; 1369 1370 offset >>= 2; 1371 retval = s->regs[offset]; 1372 1373 DB_PRINT("offset: 0x%04x read: 0x%08x\n", (unsigned)offset*4, retval); 1374 1375 switch (offset) { 1376 case GEM_ISR: 1377 DB_PRINT("lowering irqs on ISR read\n"); 1378 /* The interrupts get updated at the end of the function. */ 1379 break; 1380 case GEM_PHYMNTNC: 1381 if (retval & GEM_PHYMNTNC_OP_R) { 1382 uint32_t phy_addr, reg_num; 1383 1384 phy_addr = (retval & GEM_PHYMNTNC_ADDR) >> GEM_PHYMNTNC_ADDR_SHFT; 1385 if (phy_addr == BOARD_PHY_ADDRESS || phy_addr == 0) { 1386 reg_num = (retval & GEM_PHYMNTNC_REG) >> GEM_PHYMNTNC_REG_SHIFT; 1387 retval &= 0xFFFF0000; 1388 retval |= gem_phy_read(s, reg_num); 1389 } else { 1390 retval |= 0xFFFF; /* No device at this address */ 1391 } 1392 } 1393 break; 1394 } 1395 1396 /* Squash read to clear bits */ 1397 s->regs[offset] &= ~(s->regs_rtc[offset]); 1398 1399 /* Do not provide write only bits */ 1400 retval &= ~(s->regs_wo[offset]); 1401 1402 DB_PRINT("0x%08x\n", retval); 1403 gem_update_int_status(s); 1404 return retval; 1405 } 1406 1407 /* 1408 * gem_write32: 1409 * Write a GEM register. 1410 */ 1411 static void gem_write(void *opaque, hwaddr offset, uint64_t val, 1412 unsigned size) 1413 { 1414 CadenceGEMState *s = (CadenceGEMState *)opaque; 1415 uint32_t readonly; 1416 int i; 1417 1418 DB_PRINT("offset: 0x%04x write: 0x%08x ", (unsigned)offset, (unsigned)val); 1419 offset >>= 2; 1420 1421 /* Squash bits which are read only in write value */ 1422 val &= ~(s->regs_ro[offset]); 1423 /* Preserve (only) bits which are read only and wtc in register */ 1424 readonly = s->regs[offset] & (s->regs_ro[offset] | s->regs_w1c[offset]); 1425 1426 /* Copy register write to backing store */ 1427 s->regs[offset] = (val & ~s->regs_w1c[offset]) | readonly; 1428 1429 /* do w1c */ 1430 s->regs[offset] &= ~(s->regs_w1c[offset] & val); 1431 1432 /* Handle register write side effects */ 1433 switch (offset) { 1434 case GEM_NWCTRL: 1435 if (val & GEM_NWCTRL_RXENA) { 1436 for (i = 0; i < s->num_priority_queues; ++i) { 1437 gem_get_rx_desc(s, i); 1438 } 1439 } 1440 if (val & GEM_NWCTRL_TXSTART) { 1441 gem_transmit(s); 1442 } 1443 if (!(val & GEM_NWCTRL_TXENA)) { 1444 /* Reset to start of Q when transmit disabled. */ 1445 for (i = 0; i < s->num_priority_queues; i++) { 1446 s->tx_desc_addr[i] = s->regs[GEM_TXQBASE]; 1447 } 1448 } 1449 if (gem_can_receive(qemu_get_queue(s->nic))) { 1450 qemu_flush_queued_packets(qemu_get_queue(s->nic)); 1451 } 1452 break; 1453 1454 case GEM_TXSTATUS: 1455 gem_update_int_status(s); 1456 break; 1457 case GEM_RXQBASE: 1458 s->rx_desc_addr[0] = val; 1459 break; 1460 case GEM_RECEIVE_Q1_PTR ... GEM_RECEIVE_Q7_PTR: 1461 s->rx_desc_addr[offset - GEM_RECEIVE_Q1_PTR + 1] = val; 1462 break; 1463 case GEM_TXQBASE: 1464 s->tx_desc_addr[0] = val; 1465 break; 1466 case GEM_TRANSMIT_Q1_PTR ... GEM_TRANSMIT_Q7_PTR: 1467 s->tx_desc_addr[offset - GEM_TRANSMIT_Q1_PTR + 1] = val; 1468 break; 1469 case GEM_RXSTATUS: 1470 gem_update_int_status(s); 1471 break; 1472 case GEM_IER: 1473 s->regs[GEM_IMR] &= ~val; 1474 gem_update_int_status(s); 1475 break; 1476 case GEM_INT_Q1_ENABLE ... GEM_INT_Q7_ENABLE: 1477 s->regs[GEM_INT_Q1_MASK + offset - GEM_INT_Q1_ENABLE] &= ~val; 1478 gem_update_int_status(s); 1479 break; 1480 case GEM_IDR: 1481 s->regs[GEM_IMR] |= val; 1482 gem_update_int_status(s); 1483 break; 1484 case GEM_INT_Q1_DISABLE ... GEM_INT_Q7_DISABLE: 1485 s->regs[GEM_INT_Q1_MASK + offset - GEM_INT_Q1_DISABLE] |= val; 1486 gem_update_int_status(s); 1487 break; 1488 case GEM_SPADDR1LO: 1489 case GEM_SPADDR2LO: 1490 case GEM_SPADDR3LO: 1491 case GEM_SPADDR4LO: 1492 s->sar_active[(offset - GEM_SPADDR1LO) / 2] = false; 1493 break; 1494 case GEM_SPADDR1HI: 1495 case GEM_SPADDR2HI: 1496 case GEM_SPADDR3HI: 1497 case GEM_SPADDR4HI: 1498 s->sar_active[(offset - GEM_SPADDR1HI) / 2] = true; 1499 break; 1500 case GEM_PHYMNTNC: 1501 if (val & GEM_PHYMNTNC_OP_W) { 1502 uint32_t phy_addr, reg_num; 1503 1504 phy_addr = (val & GEM_PHYMNTNC_ADDR) >> GEM_PHYMNTNC_ADDR_SHFT; 1505 if (phy_addr == BOARD_PHY_ADDRESS || phy_addr == 0) { 1506 reg_num = (val & GEM_PHYMNTNC_REG) >> GEM_PHYMNTNC_REG_SHIFT; 1507 gem_phy_write(s, reg_num, val); 1508 } 1509 } 1510 break; 1511 } 1512 1513 DB_PRINT("newval: 0x%08x\n", s->regs[offset]); 1514 } 1515 1516 static const MemoryRegionOps gem_ops = { 1517 .read = gem_read, 1518 .write = gem_write, 1519 .endianness = DEVICE_LITTLE_ENDIAN, 1520 }; 1521 1522 static void gem_set_link(NetClientState *nc) 1523 { 1524 CadenceGEMState *s = qemu_get_nic_opaque(nc); 1525 1526 DB_PRINT("\n"); 1527 phy_update_link(s); 1528 gem_update_int_status(s); 1529 } 1530 1531 static NetClientInfo net_gem_info = { 1532 .type = NET_CLIENT_DRIVER_NIC, 1533 .size = sizeof(NICState), 1534 .can_receive = gem_can_receive, 1535 .receive = gem_receive, 1536 .link_status_changed = gem_set_link, 1537 }; 1538 1539 static void gem_realize(DeviceState *dev, Error **errp) 1540 { 1541 CadenceGEMState *s = CADENCE_GEM(dev); 1542 int i; 1543 1544 address_space_init(&s->dma_as, 1545 s->dma_mr ? s->dma_mr : get_system_memory(), "dma"); 1546 1547 if (s->num_priority_queues == 0 || 1548 s->num_priority_queues > MAX_PRIORITY_QUEUES) { 1549 error_setg(errp, "Invalid num-priority-queues value: %" PRIx8, 1550 s->num_priority_queues); 1551 return; 1552 } else if (s->num_type1_screeners > MAX_TYPE1_SCREENERS) { 1553 error_setg(errp, "Invalid num-type1-screeners value: %" PRIx8, 1554 s->num_type1_screeners); 1555 return; 1556 } else if (s->num_type2_screeners > MAX_TYPE2_SCREENERS) { 1557 error_setg(errp, "Invalid num-type2-screeners value: %" PRIx8, 1558 s->num_type2_screeners); 1559 return; 1560 } 1561 1562 for (i = 0; i < s->num_priority_queues; ++i) { 1563 sysbus_init_irq(SYS_BUS_DEVICE(dev), &s->irq[i]); 1564 } 1565 1566 qemu_macaddr_default_if_unset(&s->conf.macaddr); 1567 1568 s->nic = qemu_new_nic(&net_gem_info, &s->conf, 1569 object_get_typename(OBJECT(dev)), dev->id, s); 1570 } 1571 1572 static void gem_init(Object *obj) 1573 { 1574 CadenceGEMState *s = CADENCE_GEM(obj); 1575 DeviceState *dev = DEVICE(obj); 1576 1577 DB_PRINT("\n"); 1578 1579 gem_init_register_masks(s); 1580 memory_region_init_io(&s->iomem, OBJECT(s), &gem_ops, s, 1581 "enet", sizeof(s->regs)); 1582 1583 sysbus_init_mmio(SYS_BUS_DEVICE(dev), &s->iomem); 1584 1585 object_property_add_link(obj, "dma", TYPE_MEMORY_REGION, 1586 (Object **)&s->dma_mr, 1587 qdev_prop_allow_set_link_before_realize, 1588 OBJ_PROP_LINK_STRONG, 1589 &error_abort); 1590 } 1591 1592 static const VMStateDescription vmstate_cadence_gem = { 1593 .name = "cadence_gem", 1594 .version_id = 4, 1595 .minimum_version_id = 4, 1596 .fields = (VMStateField[]) { 1597 VMSTATE_UINT32_ARRAY(regs, CadenceGEMState, CADENCE_GEM_MAXREG), 1598 VMSTATE_UINT16_ARRAY(phy_regs, CadenceGEMState, 32), 1599 VMSTATE_UINT8(phy_loop, CadenceGEMState), 1600 VMSTATE_UINT32_ARRAY(rx_desc_addr, CadenceGEMState, 1601 MAX_PRIORITY_QUEUES), 1602 VMSTATE_UINT32_ARRAY(tx_desc_addr, CadenceGEMState, 1603 MAX_PRIORITY_QUEUES), 1604 VMSTATE_BOOL_ARRAY(sar_active, CadenceGEMState, 4), 1605 VMSTATE_END_OF_LIST(), 1606 } 1607 }; 1608 1609 static Property gem_properties[] = { 1610 DEFINE_NIC_PROPERTIES(CadenceGEMState, conf), 1611 DEFINE_PROP_UINT32("revision", CadenceGEMState, revision, 1612 GEM_MODID_VALUE), 1613 DEFINE_PROP_UINT8("num-priority-queues", CadenceGEMState, 1614 num_priority_queues, 1), 1615 DEFINE_PROP_UINT8("num-type1-screeners", CadenceGEMState, 1616 num_type1_screeners, 4), 1617 DEFINE_PROP_UINT8("num-type2-screeners", CadenceGEMState, 1618 num_type2_screeners, 4), 1619 DEFINE_PROP_END_OF_LIST(), 1620 }; 1621 1622 static void gem_class_init(ObjectClass *klass, void *data) 1623 { 1624 DeviceClass *dc = DEVICE_CLASS(klass); 1625 1626 dc->realize = gem_realize; 1627 dc->props = gem_properties; 1628 dc->vmsd = &vmstate_cadence_gem; 1629 dc->reset = gem_reset; 1630 } 1631 1632 static const TypeInfo gem_info = { 1633 .name = TYPE_CADENCE_GEM, 1634 .parent = TYPE_SYS_BUS_DEVICE, 1635 .instance_size = sizeof(CadenceGEMState), 1636 .instance_init = gem_init, 1637 .class_init = gem_class_init, 1638 }; 1639 1640 static void gem_register_types(void) 1641 { 1642 type_register_static(&gem_info); 1643 } 1644 1645 type_init(gem_register_types) 1646