1 /* 2 * Core code for QEMU e1000e emulation 3 * 4 * Software developer's manuals: 5 * http://www.intel.com/content/dam/doc/datasheet/82574l-gbe-controller-datasheet.pdf 6 * 7 * Copyright (c) 2015 Ravello Systems LTD (http://ravellosystems.com) 8 * Developed by Daynix Computing LTD (http://www.daynix.com) 9 * 10 * Authors: 11 * Dmitry Fleytman <dmitry@daynix.com> 12 * Leonid Bloch <leonid@daynix.com> 13 * Yan Vugenfirer <yan@daynix.com> 14 * 15 * Based on work done by: 16 * Nir Peleg, Tutis Systems Ltd. for Qumranet Inc. 17 * Copyright (c) 2008 Qumranet 18 * Based on work done by: 19 * Copyright (c) 2007 Dan Aloni 20 * Copyright (c) 2004 Antony T Curtis 21 * 22 * This library is free software; you can redistribute it and/or 23 * modify it under the terms of the GNU Lesser General Public 24 * License as published by the Free Software Foundation; either 25 * version 2.1 of the License, or (at your option) any later version. 26 * 27 * This library is distributed in the hope that it will be useful, 28 * but WITHOUT ANY WARRANTY; without even the implied warranty of 29 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 30 * Lesser General Public License for more details. 31 * 32 * You should have received a copy of the GNU Lesser General Public 33 * License along with this library; if not, see <http://www.gnu.org/licenses/>. 34 */ 35 36 #include "qemu/osdep.h" 37 #include "qemu/log.h" 38 #include "net/net.h" 39 #include "net/tap.h" 40 #include "hw/net/mii.h" 41 #include "hw/pci/msi.h" 42 #include "hw/pci/msix.h" 43 #include "sysemu/runstate.h" 44 45 #include "net_tx_pkt.h" 46 #include "net_rx_pkt.h" 47 48 #include "e1000_common.h" 49 #include "e1000x_common.h" 50 #include "e1000e_core.h" 51 52 #include "trace.h" 53 54 /* No more then 7813 interrupts per second according to spec 10.2.4.2 */ 55 #define E1000E_MIN_XITR (500) 56 57 #define E1000E_MAX_TX_FRAGS (64) 58 59 union e1000_rx_desc_union { 60 struct e1000_rx_desc legacy; 61 union e1000_rx_desc_extended extended; 62 union e1000_rx_desc_packet_split packet_split; 63 }; 64 65 static ssize_t 66 e1000e_receive_internal(E1000ECore *core, const struct iovec *iov, int iovcnt, 67 bool has_vnet); 68 69 static inline void 70 e1000e_set_interrupt_cause(E1000ECore *core, uint32_t val); 71 72 static void e1000e_reset(E1000ECore *core, bool sw); 73 74 static inline void 75 e1000e_process_ts_option(E1000ECore *core, struct e1000_tx_desc *dp) 76 { 77 if (le32_to_cpu(dp->upper.data) & E1000_TXD_EXTCMD_TSTAMP) { 78 trace_e1000e_wrn_no_ts_support(); 79 } 80 } 81 82 static inline void 83 e1000e_process_snap_option(E1000ECore *core, uint32_t cmd_and_length) 84 { 85 if (cmd_and_length & E1000_TXD_CMD_SNAP) { 86 trace_e1000e_wrn_no_snap_support(); 87 } 88 } 89 90 static inline void 91 e1000e_raise_legacy_irq(E1000ECore *core) 92 { 93 trace_e1000e_irq_legacy_notify(true); 94 e1000x_inc_reg_if_not_full(core->mac, IAC); 95 pci_set_irq(core->owner, 1); 96 } 97 98 static inline void 99 e1000e_lower_legacy_irq(E1000ECore *core) 100 { 101 trace_e1000e_irq_legacy_notify(false); 102 pci_set_irq(core->owner, 0); 103 } 104 105 static inline void 106 e1000e_intrmgr_rearm_timer(E1000IntrDelayTimer *timer) 107 { 108 int64_t delay_ns = (int64_t) timer->core->mac[timer->delay_reg] * 109 timer->delay_resolution_ns; 110 111 trace_e1000e_irq_rearm_timer(timer->delay_reg << 2, delay_ns); 112 113 timer_mod(timer->timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + delay_ns); 114 115 timer->running = true; 116 } 117 118 static void 119 e1000e_intmgr_timer_resume(E1000IntrDelayTimer *timer) 120 { 121 if (timer->running) { 122 e1000e_intrmgr_rearm_timer(timer); 123 } 124 } 125 126 static void 127 e1000e_intmgr_timer_pause(E1000IntrDelayTimer *timer) 128 { 129 if (timer->running) { 130 timer_del(timer->timer); 131 } 132 } 133 134 static inline void 135 e1000e_intrmgr_stop_timer(E1000IntrDelayTimer *timer) 136 { 137 if (timer->running) { 138 timer_del(timer->timer); 139 timer->running = false; 140 } 141 } 142 143 static inline void 144 e1000e_intrmgr_fire_delayed_interrupts(E1000ECore *core) 145 { 146 trace_e1000e_irq_fire_delayed_interrupts(); 147 e1000e_set_interrupt_cause(core, 0); 148 } 149 150 static void 151 e1000e_intrmgr_on_timer(void *opaque) 152 { 153 E1000IntrDelayTimer *timer = opaque; 154 155 trace_e1000e_irq_throttling_timer(timer->delay_reg << 2); 156 157 timer->running = false; 158 e1000e_intrmgr_fire_delayed_interrupts(timer->core); 159 } 160 161 static void 162 e1000e_intrmgr_on_throttling_timer(void *opaque) 163 { 164 E1000IntrDelayTimer *timer = opaque; 165 166 timer->running = false; 167 168 if (msi_enabled(timer->core->owner)) { 169 trace_e1000e_irq_msi_notify_postponed(); 170 /* Clear msi_causes_pending to fire MSI eventually */ 171 timer->core->msi_causes_pending = 0; 172 e1000e_set_interrupt_cause(timer->core, 0); 173 } else { 174 trace_e1000e_irq_legacy_notify_postponed(); 175 e1000e_set_interrupt_cause(timer->core, 0); 176 } 177 } 178 179 static void 180 e1000e_intrmgr_on_msix_throttling_timer(void *opaque) 181 { 182 E1000IntrDelayTimer *timer = opaque; 183 int idx = timer - &timer->core->eitr[0]; 184 185 timer->running = false; 186 187 trace_e1000e_irq_msix_notify_postponed_vec(idx); 188 msix_notify(timer->core->owner, idx); 189 } 190 191 static void 192 e1000e_intrmgr_initialize_all_timers(E1000ECore *core, bool create) 193 { 194 int i; 195 196 core->radv.delay_reg = RADV; 197 core->rdtr.delay_reg = RDTR; 198 core->raid.delay_reg = RAID; 199 core->tadv.delay_reg = TADV; 200 core->tidv.delay_reg = TIDV; 201 202 core->radv.delay_resolution_ns = E1000_INTR_DELAY_NS_RES; 203 core->rdtr.delay_resolution_ns = E1000_INTR_DELAY_NS_RES; 204 core->raid.delay_resolution_ns = E1000_INTR_DELAY_NS_RES; 205 core->tadv.delay_resolution_ns = E1000_INTR_DELAY_NS_RES; 206 core->tidv.delay_resolution_ns = E1000_INTR_DELAY_NS_RES; 207 208 core->radv.core = core; 209 core->rdtr.core = core; 210 core->raid.core = core; 211 core->tadv.core = core; 212 core->tidv.core = core; 213 214 core->itr.core = core; 215 core->itr.delay_reg = ITR; 216 core->itr.delay_resolution_ns = E1000_INTR_THROTTLING_NS_RES; 217 218 for (i = 0; i < E1000E_MSIX_VEC_NUM; i++) { 219 core->eitr[i].core = core; 220 core->eitr[i].delay_reg = EITR + i; 221 core->eitr[i].delay_resolution_ns = E1000_INTR_THROTTLING_NS_RES; 222 } 223 224 if (!create) { 225 return; 226 } 227 228 core->radv.timer = 229 timer_new_ns(QEMU_CLOCK_VIRTUAL, e1000e_intrmgr_on_timer, &core->radv); 230 core->rdtr.timer = 231 timer_new_ns(QEMU_CLOCK_VIRTUAL, e1000e_intrmgr_on_timer, &core->rdtr); 232 core->raid.timer = 233 timer_new_ns(QEMU_CLOCK_VIRTUAL, e1000e_intrmgr_on_timer, &core->raid); 234 235 core->tadv.timer = 236 timer_new_ns(QEMU_CLOCK_VIRTUAL, e1000e_intrmgr_on_timer, &core->tadv); 237 core->tidv.timer = 238 timer_new_ns(QEMU_CLOCK_VIRTUAL, e1000e_intrmgr_on_timer, &core->tidv); 239 240 core->itr.timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, 241 e1000e_intrmgr_on_throttling_timer, 242 &core->itr); 243 244 for (i = 0; i < E1000E_MSIX_VEC_NUM; i++) { 245 core->eitr[i].timer = 246 timer_new_ns(QEMU_CLOCK_VIRTUAL, 247 e1000e_intrmgr_on_msix_throttling_timer, 248 &core->eitr[i]); 249 } 250 } 251 252 static inline void 253 e1000e_intrmgr_stop_delay_timers(E1000ECore *core) 254 { 255 e1000e_intrmgr_stop_timer(&core->radv); 256 e1000e_intrmgr_stop_timer(&core->rdtr); 257 e1000e_intrmgr_stop_timer(&core->raid); 258 e1000e_intrmgr_stop_timer(&core->tidv); 259 e1000e_intrmgr_stop_timer(&core->tadv); 260 } 261 262 static bool 263 e1000e_intrmgr_delay_rx_causes(E1000ECore *core, uint32_t *causes) 264 { 265 uint32_t delayable_causes; 266 uint32_t rdtr = core->mac[RDTR]; 267 uint32_t radv = core->mac[RADV]; 268 uint32_t raid = core->mac[RAID]; 269 270 if (msix_enabled(core->owner)) { 271 return false; 272 } 273 274 delayable_causes = E1000_ICR_RXQ0 | 275 E1000_ICR_RXQ1 | 276 E1000_ICR_RXT0; 277 278 if (!(core->mac[RFCTL] & E1000_RFCTL_ACK_DIS)) { 279 delayable_causes |= E1000_ICR_ACK; 280 } 281 282 /* Clean up all causes that may be delayed */ 283 core->delayed_causes |= *causes & delayable_causes; 284 *causes &= ~delayable_causes; 285 286 /* 287 * Check if delayed RX interrupts disabled by client 288 * or if there are causes that cannot be delayed 289 */ 290 if ((rdtr == 0) || (*causes != 0)) { 291 return false; 292 } 293 294 /* 295 * Check if delayed RX ACK interrupts disabled by client 296 * and there is an ACK packet received 297 */ 298 if ((raid == 0) && (core->delayed_causes & E1000_ICR_ACK)) { 299 return false; 300 } 301 302 /* All causes delayed */ 303 e1000e_intrmgr_rearm_timer(&core->rdtr); 304 305 if (!core->radv.running && (radv != 0)) { 306 e1000e_intrmgr_rearm_timer(&core->radv); 307 } 308 309 if (!core->raid.running && (core->delayed_causes & E1000_ICR_ACK)) { 310 e1000e_intrmgr_rearm_timer(&core->raid); 311 } 312 313 return true; 314 } 315 316 static bool 317 e1000e_intrmgr_delay_tx_causes(E1000ECore *core, uint32_t *causes) 318 { 319 static const uint32_t delayable_causes = E1000_ICR_TXQ0 | 320 E1000_ICR_TXQ1 | 321 E1000_ICR_TXQE | 322 E1000_ICR_TXDW; 323 324 if (msix_enabled(core->owner)) { 325 return false; 326 } 327 328 /* Clean up all causes that may be delayed */ 329 core->delayed_causes |= *causes & delayable_causes; 330 *causes &= ~delayable_causes; 331 332 /* If there are causes that cannot be delayed */ 333 if (*causes != 0) { 334 return false; 335 } 336 337 /* All causes delayed */ 338 e1000e_intrmgr_rearm_timer(&core->tidv); 339 340 if (!core->tadv.running && (core->mac[TADV] != 0)) { 341 e1000e_intrmgr_rearm_timer(&core->tadv); 342 } 343 344 return true; 345 } 346 347 static uint32_t 348 e1000e_intmgr_collect_delayed_causes(E1000ECore *core) 349 { 350 uint32_t res; 351 352 if (msix_enabled(core->owner)) { 353 assert(core->delayed_causes == 0); 354 return 0; 355 } 356 357 res = core->delayed_causes; 358 core->delayed_causes = 0; 359 360 e1000e_intrmgr_stop_delay_timers(core); 361 362 return res; 363 } 364 365 static void 366 e1000e_intrmgr_fire_all_timers(E1000ECore *core) 367 { 368 int i; 369 uint32_t val = e1000e_intmgr_collect_delayed_causes(core); 370 371 trace_e1000e_irq_adding_delayed_causes(val, core->mac[ICR]); 372 core->mac[ICR] |= val; 373 374 if (core->itr.running) { 375 timer_del(core->itr.timer); 376 e1000e_intrmgr_on_throttling_timer(&core->itr); 377 } 378 379 for (i = 0; i < E1000E_MSIX_VEC_NUM; i++) { 380 if (core->eitr[i].running) { 381 timer_del(core->eitr[i].timer); 382 e1000e_intrmgr_on_msix_throttling_timer(&core->eitr[i]); 383 } 384 } 385 } 386 387 static void 388 e1000e_intrmgr_resume(E1000ECore *core) 389 { 390 int i; 391 392 e1000e_intmgr_timer_resume(&core->radv); 393 e1000e_intmgr_timer_resume(&core->rdtr); 394 e1000e_intmgr_timer_resume(&core->raid); 395 e1000e_intmgr_timer_resume(&core->tidv); 396 e1000e_intmgr_timer_resume(&core->tadv); 397 398 e1000e_intmgr_timer_resume(&core->itr); 399 400 for (i = 0; i < E1000E_MSIX_VEC_NUM; i++) { 401 e1000e_intmgr_timer_resume(&core->eitr[i]); 402 } 403 } 404 405 static void 406 e1000e_intrmgr_pause(E1000ECore *core) 407 { 408 int i; 409 410 e1000e_intmgr_timer_pause(&core->radv); 411 e1000e_intmgr_timer_pause(&core->rdtr); 412 e1000e_intmgr_timer_pause(&core->raid); 413 e1000e_intmgr_timer_pause(&core->tidv); 414 e1000e_intmgr_timer_pause(&core->tadv); 415 416 e1000e_intmgr_timer_pause(&core->itr); 417 418 for (i = 0; i < E1000E_MSIX_VEC_NUM; i++) { 419 e1000e_intmgr_timer_pause(&core->eitr[i]); 420 } 421 } 422 423 static void 424 e1000e_intrmgr_reset(E1000ECore *core) 425 { 426 int i; 427 428 core->delayed_causes = 0; 429 430 e1000e_intrmgr_stop_delay_timers(core); 431 432 e1000e_intrmgr_stop_timer(&core->itr); 433 434 for (i = 0; i < E1000E_MSIX_VEC_NUM; i++) { 435 e1000e_intrmgr_stop_timer(&core->eitr[i]); 436 } 437 } 438 439 static void 440 e1000e_intrmgr_pci_unint(E1000ECore *core) 441 { 442 int i; 443 444 timer_free(core->radv.timer); 445 timer_free(core->rdtr.timer); 446 timer_free(core->raid.timer); 447 448 timer_free(core->tadv.timer); 449 timer_free(core->tidv.timer); 450 451 timer_free(core->itr.timer); 452 453 for (i = 0; i < E1000E_MSIX_VEC_NUM; i++) { 454 timer_free(core->eitr[i].timer); 455 } 456 } 457 458 static void 459 e1000e_intrmgr_pci_realize(E1000ECore *core) 460 { 461 e1000e_intrmgr_initialize_all_timers(core, true); 462 } 463 464 static inline bool 465 e1000e_rx_csum_enabled(E1000ECore *core) 466 { 467 return (core->mac[RXCSUM] & E1000_RXCSUM_PCSD) ? false : true; 468 } 469 470 static inline bool 471 e1000e_rx_use_legacy_descriptor(E1000ECore *core) 472 { 473 return (core->mac[RFCTL] & E1000_RFCTL_EXTEN) ? false : true; 474 } 475 476 static inline bool 477 e1000e_rx_use_ps_descriptor(E1000ECore *core) 478 { 479 return !e1000e_rx_use_legacy_descriptor(core) && 480 (core->mac[RCTL] & E1000_RCTL_DTYP_PS); 481 } 482 483 static inline bool 484 e1000e_rss_enabled(E1000ECore *core) 485 { 486 return E1000_MRQC_ENABLED(core->mac[MRQC]) && 487 !e1000e_rx_csum_enabled(core) && 488 !e1000e_rx_use_legacy_descriptor(core); 489 } 490 491 typedef struct E1000E_RSSInfo_st { 492 bool enabled; 493 uint32_t hash; 494 uint32_t queue; 495 uint32_t type; 496 } E1000E_RSSInfo; 497 498 static uint32_t 499 e1000e_rss_get_hash_type(E1000ECore *core, struct NetRxPkt *pkt) 500 { 501 bool hasip4, hasip6; 502 EthL4HdrProto l4hdr_proto; 503 504 assert(e1000e_rss_enabled(core)); 505 506 net_rx_pkt_get_protocols(pkt, &hasip4, &hasip6, &l4hdr_proto); 507 508 if (hasip4) { 509 trace_e1000e_rx_rss_ip4(l4hdr_proto, core->mac[MRQC], 510 E1000_MRQC_EN_TCPIPV4(core->mac[MRQC]), 511 E1000_MRQC_EN_IPV4(core->mac[MRQC])); 512 513 if (l4hdr_proto == ETH_L4_HDR_PROTO_TCP && 514 E1000_MRQC_EN_TCPIPV4(core->mac[MRQC])) { 515 return E1000_MRQ_RSS_TYPE_IPV4TCP; 516 } 517 518 if (E1000_MRQC_EN_IPV4(core->mac[MRQC])) { 519 return E1000_MRQ_RSS_TYPE_IPV4; 520 } 521 } else if (hasip6) { 522 eth_ip6_hdr_info *ip6info = net_rx_pkt_get_ip6_info(pkt); 523 524 bool ex_dis = core->mac[RFCTL] & E1000_RFCTL_IPV6_EX_DIS; 525 bool new_ex_dis = core->mac[RFCTL] & E1000_RFCTL_NEW_IPV6_EXT_DIS; 526 527 /* 528 * Following two traces must not be combined because resulting 529 * event will have 11 arguments totally and some trace backends 530 * (at least "ust") have limitation of maximum 10 arguments per 531 * event. Events with more arguments fail to compile for 532 * backends like these. 533 */ 534 trace_e1000e_rx_rss_ip6_rfctl(core->mac[RFCTL]); 535 trace_e1000e_rx_rss_ip6(ex_dis, new_ex_dis, l4hdr_proto, 536 ip6info->has_ext_hdrs, 537 ip6info->rss_ex_dst_valid, 538 ip6info->rss_ex_src_valid, 539 core->mac[MRQC], 540 E1000_MRQC_EN_TCPIPV6(core->mac[MRQC]), 541 E1000_MRQC_EN_IPV6EX(core->mac[MRQC]), 542 E1000_MRQC_EN_IPV6(core->mac[MRQC])); 543 544 if ((!ex_dis || !ip6info->has_ext_hdrs) && 545 (!new_ex_dis || !(ip6info->rss_ex_dst_valid || 546 ip6info->rss_ex_src_valid))) { 547 548 if (l4hdr_proto == ETH_L4_HDR_PROTO_TCP && 549 E1000_MRQC_EN_TCPIPV6(core->mac[MRQC])) { 550 return E1000_MRQ_RSS_TYPE_IPV6TCP; 551 } 552 553 if (E1000_MRQC_EN_IPV6EX(core->mac[MRQC])) { 554 return E1000_MRQ_RSS_TYPE_IPV6EX; 555 } 556 557 } 558 559 if (E1000_MRQC_EN_IPV6(core->mac[MRQC])) { 560 return E1000_MRQ_RSS_TYPE_IPV6; 561 } 562 563 } 564 565 return E1000_MRQ_RSS_TYPE_NONE; 566 } 567 568 static uint32_t 569 e1000e_rss_calc_hash(E1000ECore *core, 570 struct NetRxPkt *pkt, 571 E1000E_RSSInfo *info) 572 { 573 NetRxPktRssType type; 574 575 assert(e1000e_rss_enabled(core)); 576 577 switch (info->type) { 578 case E1000_MRQ_RSS_TYPE_IPV4: 579 type = NetPktRssIpV4; 580 break; 581 case E1000_MRQ_RSS_TYPE_IPV4TCP: 582 type = NetPktRssIpV4Tcp; 583 break; 584 case E1000_MRQ_RSS_TYPE_IPV6TCP: 585 type = NetPktRssIpV6TcpEx; 586 break; 587 case E1000_MRQ_RSS_TYPE_IPV6: 588 type = NetPktRssIpV6; 589 break; 590 case E1000_MRQ_RSS_TYPE_IPV6EX: 591 type = NetPktRssIpV6Ex; 592 break; 593 default: 594 assert(false); 595 return 0; 596 } 597 598 return net_rx_pkt_calc_rss_hash(pkt, type, (uint8_t *) &core->mac[RSSRK]); 599 } 600 601 static void 602 e1000e_rss_parse_packet(E1000ECore *core, 603 struct NetRxPkt *pkt, 604 E1000E_RSSInfo *info) 605 { 606 trace_e1000e_rx_rss_started(); 607 608 if (!e1000e_rss_enabled(core)) { 609 info->enabled = false; 610 info->hash = 0; 611 info->queue = 0; 612 info->type = 0; 613 trace_e1000e_rx_rss_disabled(); 614 return; 615 } 616 617 info->enabled = true; 618 619 info->type = e1000e_rss_get_hash_type(core, pkt); 620 621 trace_e1000e_rx_rss_type(info->type); 622 623 if (info->type == E1000_MRQ_RSS_TYPE_NONE) { 624 info->hash = 0; 625 info->queue = 0; 626 return; 627 } 628 629 info->hash = e1000e_rss_calc_hash(core, pkt, info); 630 info->queue = E1000_RSS_QUEUE(&core->mac[RETA], info->hash); 631 } 632 633 static bool 634 e1000e_setup_tx_offloads(E1000ECore *core, struct e1000e_tx *tx) 635 { 636 if (tx->props.tse && tx->cptse) { 637 if (!net_tx_pkt_build_vheader(tx->tx_pkt, true, true, tx->props.mss)) { 638 return false; 639 } 640 641 net_tx_pkt_update_ip_checksums(tx->tx_pkt); 642 e1000x_inc_reg_if_not_full(core->mac, TSCTC); 643 return true; 644 } 645 646 if (tx->sum_needed & E1000_TXD_POPTS_TXSM) { 647 if (!net_tx_pkt_build_vheader(tx->tx_pkt, false, true, 0)) { 648 return false; 649 } 650 } 651 652 if (tx->sum_needed & E1000_TXD_POPTS_IXSM) { 653 net_tx_pkt_update_ip_hdr_checksum(tx->tx_pkt); 654 } 655 656 return true; 657 } 658 659 static void e1000e_tx_pkt_callback(void *core, 660 const struct iovec *iov, 661 int iovcnt, 662 const struct iovec *virt_iov, 663 int virt_iovcnt) 664 { 665 e1000e_receive_internal(core, virt_iov, virt_iovcnt, true); 666 } 667 668 static bool 669 e1000e_tx_pkt_send(E1000ECore *core, struct e1000e_tx *tx, int queue_index) 670 { 671 int target_queue = MIN(core->max_queue_num, queue_index); 672 NetClientState *queue = qemu_get_subqueue(core->owner_nic, target_queue); 673 674 if (!e1000e_setup_tx_offloads(core, tx)) { 675 return false; 676 } 677 678 net_tx_pkt_dump(tx->tx_pkt); 679 680 if ((core->phy[0][MII_BMCR] & MII_BMCR_LOOPBACK) || 681 ((core->mac[RCTL] & E1000_RCTL_LBM_MAC) == E1000_RCTL_LBM_MAC)) { 682 return net_tx_pkt_send_custom(tx->tx_pkt, false, 683 e1000e_tx_pkt_callback, core); 684 } else { 685 return net_tx_pkt_send(tx->tx_pkt, queue); 686 } 687 } 688 689 static void 690 e1000e_on_tx_done_update_stats(E1000ECore *core, struct NetTxPkt *tx_pkt) 691 { 692 static const int PTCregs[6] = { PTC64, PTC127, PTC255, PTC511, 693 PTC1023, PTC1522 }; 694 695 size_t tot_len = net_tx_pkt_get_total_len(tx_pkt) + 4; 696 697 e1000x_increase_size_stats(core->mac, PTCregs, tot_len); 698 e1000x_inc_reg_if_not_full(core->mac, TPT); 699 e1000x_grow_8reg_if_not_full(core->mac, TOTL, tot_len); 700 701 switch (net_tx_pkt_get_packet_type(tx_pkt)) { 702 case ETH_PKT_BCAST: 703 e1000x_inc_reg_if_not_full(core->mac, BPTC); 704 break; 705 case ETH_PKT_MCAST: 706 e1000x_inc_reg_if_not_full(core->mac, MPTC); 707 break; 708 case ETH_PKT_UCAST: 709 break; 710 default: 711 g_assert_not_reached(); 712 } 713 714 e1000x_inc_reg_if_not_full(core->mac, GPTC); 715 e1000x_grow_8reg_if_not_full(core->mac, GOTCL, tot_len); 716 } 717 718 static void 719 e1000e_process_tx_desc(E1000ECore *core, 720 struct e1000e_tx *tx, 721 struct e1000_tx_desc *dp, 722 int queue_index) 723 { 724 uint32_t txd_lower = le32_to_cpu(dp->lower.data); 725 uint32_t dtype = txd_lower & (E1000_TXD_CMD_DEXT | E1000_TXD_DTYP_D); 726 unsigned int split_size = txd_lower & 0xffff; 727 uint64_t addr; 728 struct e1000_context_desc *xp = (struct e1000_context_desc *)dp; 729 bool eop = txd_lower & E1000_TXD_CMD_EOP; 730 731 if (dtype == E1000_TXD_CMD_DEXT) { /* context descriptor */ 732 e1000x_read_tx_ctx_descr(xp, &tx->props); 733 e1000e_process_snap_option(core, le32_to_cpu(xp->cmd_and_length)); 734 return; 735 } else if (dtype == (E1000_TXD_CMD_DEXT | E1000_TXD_DTYP_D)) { 736 /* data descriptor */ 737 tx->sum_needed = le32_to_cpu(dp->upper.data) >> 8; 738 tx->cptse = (txd_lower & E1000_TXD_CMD_TSE) ? 1 : 0; 739 e1000e_process_ts_option(core, dp); 740 } else { 741 /* legacy descriptor */ 742 e1000e_process_ts_option(core, dp); 743 tx->cptse = 0; 744 } 745 746 addr = le64_to_cpu(dp->buffer_addr); 747 748 if (!tx->skip_cp) { 749 if (!net_tx_pkt_add_raw_fragment_pci(tx->tx_pkt, core->owner, 750 addr, split_size)) { 751 tx->skip_cp = true; 752 } 753 } 754 755 if (eop) { 756 if (!tx->skip_cp && net_tx_pkt_parse(tx->tx_pkt)) { 757 if (e1000x_vlan_enabled(core->mac) && 758 e1000x_is_vlan_txd(txd_lower)) { 759 net_tx_pkt_setup_vlan_header_ex(tx->tx_pkt, 760 le16_to_cpu(dp->upper.fields.special), core->mac[VET]); 761 } 762 if (e1000e_tx_pkt_send(core, tx, queue_index)) { 763 e1000e_on_tx_done_update_stats(core, tx->tx_pkt); 764 } 765 } 766 767 tx->skip_cp = false; 768 net_tx_pkt_reset(tx->tx_pkt, net_tx_pkt_unmap_frag_pci, core->owner); 769 770 tx->sum_needed = 0; 771 tx->cptse = 0; 772 } 773 } 774 775 static inline uint32_t 776 e1000e_tx_wb_interrupt_cause(E1000ECore *core, int queue_idx) 777 { 778 if (!msix_enabled(core->owner)) { 779 return E1000_ICR_TXDW; 780 } 781 782 return (queue_idx == 0) ? E1000_ICR_TXQ0 : E1000_ICR_TXQ1; 783 } 784 785 static inline uint32_t 786 e1000e_rx_wb_interrupt_cause(E1000ECore *core, int queue_idx, 787 bool min_threshold_hit) 788 { 789 if (!msix_enabled(core->owner)) { 790 return E1000_ICS_RXT0 | (min_threshold_hit ? E1000_ICS_RXDMT0 : 0); 791 } 792 793 return (queue_idx == 0) ? E1000_ICR_RXQ0 : E1000_ICR_RXQ1; 794 } 795 796 static uint32_t 797 e1000e_txdesc_writeback(E1000ECore *core, dma_addr_t base, 798 struct e1000_tx_desc *dp, bool *ide, int queue_idx) 799 { 800 uint32_t txd_upper, txd_lower = le32_to_cpu(dp->lower.data); 801 802 if (!(txd_lower & E1000_TXD_CMD_RS) && 803 !(core->mac[IVAR] & E1000_IVAR_TX_INT_EVERY_WB)) { 804 return 0; 805 } 806 807 *ide = (txd_lower & E1000_TXD_CMD_IDE) ? true : false; 808 809 txd_upper = le32_to_cpu(dp->upper.data) | E1000_TXD_STAT_DD; 810 811 dp->upper.data = cpu_to_le32(txd_upper); 812 pci_dma_write(core->owner, base + ((char *)&dp->upper - (char *)dp), 813 &dp->upper, sizeof(dp->upper)); 814 return e1000e_tx_wb_interrupt_cause(core, queue_idx); 815 } 816 817 typedef struct E1000E_RingInfo_st { 818 int dbah; 819 int dbal; 820 int dlen; 821 int dh; 822 int dt; 823 int idx; 824 } E1000E_RingInfo; 825 826 static inline bool 827 e1000e_ring_empty(E1000ECore *core, const E1000E_RingInfo *r) 828 { 829 return core->mac[r->dh] == core->mac[r->dt] || 830 core->mac[r->dt] >= core->mac[r->dlen] / E1000_RING_DESC_LEN; 831 } 832 833 static inline uint64_t 834 e1000e_ring_base(E1000ECore *core, const E1000E_RingInfo *r) 835 { 836 uint64_t bah = core->mac[r->dbah]; 837 uint64_t bal = core->mac[r->dbal]; 838 839 return (bah << 32) + bal; 840 } 841 842 static inline uint64_t 843 e1000e_ring_head_descr(E1000ECore *core, const E1000E_RingInfo *r) 844 { 845 return e1000e_ring_base(core, r) + E1000_RING_DESC_LEN * core->mac[r->dh]; 846 } 847 848 static inline void 849 e1000e_ring_advance(E1000ECore *core, const E1000E_RingInfo *r, uint32_t count) 850 { 851 core->mac[r->dh] += count; 852 853 if (core->mac[r->dh] * E1000_RING_DESC_LEN >= core->mac[r->dlen]) { 854 core->mac[r->dh] = 0; 855 } 856 } 857 858 static inline uint32_t 859 e1000e_ring_free_descr_num(E1000ECore *core, const E1000E_RingInfo *r) 860 { 861 trace_e1000e_ring_free_space(r->idx, core->mac[r->dlen], 862 core->mac[r->dh], core->mac[r->dt]); 863 864 if (core->mac[r->dh] <= core->mac[r->dt]) { 865 return core->mac[r->dt] - core->mac[r->dh]; 866 } 867 868 if (core->mac[r->dh] > core->mac[r->dt]) { 869 return core->mac[r->dlen] / E1000_RING_DESC_LEN + 870 core->mac[r->dt] - core->mac[r->dh]; 871 } 872 873 g_assert_not_reached(); 874 return 0; 875 } 876 877 static inline bool 878 e1000e_ring_enabled(E1000ECore *core, const E1000E_RingInfo *r) 879 { 880 return core->mac[r->dlen] > 0; 881 } 882 883 static inline uint32_t 884 e1000e_ring_len(E1000ECore *core, const E1000E_RingInfo *r) 885 { 886 return core->mac[r->dlen]; 887 } 888 889 typedef struct E1000E_TxRing_st { 890 const E1000E_RingInfo *i; 891 struct e1000e_tx *tx; 892 } E1000E_TxRing; 893 894 static inline int 895 e1000e_mq_queue_idx(int base_reg_idx, int reg_idx) 896 { 897 return (reg_idx - base_reg_idx) / (0x100 >> 2); 898 } 899 900 static inline void 901 e1000e_tx_ring_init(E1000ECore *core, E1000E_TxRing *txr, int idx) 902 { 903 static const E1000E_RingInfo i[E1000E_NUM_QUEUES] = { 904 { TDBAH, TDBAL, TDLEN, TDH, TDT, 0 }, 905 { TDBAH1, TDBAL1, TDLEN1, TDH1, TDT1, 1 } 906 }; 907 908 assert(idx < ARRAY_SIZE(i)); 909 910 txr->i = &i[idx]; 911 txr->tx = &core->tx[idx]; 912 } 913 914 typedef struct E1000E_RxRing_st { 915 const E1000E_RingInfo *i; 916 } E1000E_RxRing; 917 918 static inline void 919 e1000e_rx_ring_init(E1000ECore *core, E1000E_RxRing *rxr, int idx) 920 { 921 static const E1000E_RingInfo i[E1000E_NUM_QUEUES] = { 922 { RDBAH0, RDBAL0, RDLEN0, RDH0, RDT0, 0 }, 923 { RDBAH1, RDBAL1, RDLEN1, RDH1, RDT1, 1 } 924 }; 925 926 assert(idx < ARRAY_SIZE(i)); 927 928 rxr->i = &i[idx]; 929 } 930 931 static void 932 e1000e_start_xmit(E1000ECore *core, const E1000E_TxRing *txr) 933 { 934 dma_addr_t base; 935 struct e1000_tx_desc desc; 936 bool ide = false; 937 const E1000E_RingInfo *txi = txr->i; 938 uint32_t cause = E1000_ICS_TXQE; 939 940 if (!(core->mac[TCTL] & E1000_TCTL_EN)) { 941 trace_e1000e_tx_disabled(); 942 return; 943 } 944 945 while (!e1000e_ring_empty(core, txi)) { 946 base = e1000e_ring_head_descr(core, txi); 947 948 pci_dma_read(core->owner, base, &desc, sizeof(desc)); 949 950 trace_e1000e_tx_descr((void *)(intptr_t)desc.buffer_addr, 951 desc.lower.data, desc.upper.data); 952 953 e1000e_process_tx_desc(core, txr->tx, &desc, txi->idx); 954 cause |= e1000e_txdesc_writeback(core, base, &desc, &ide, txi->idx); 955 956 e1000e_ring_advance(core, txi, 1); 957 } 958 959 if (!ide || !e1000e_intrmgr_delay_tx_causes(core, &cause)) { 960 e1000e_set_interrupt_cause(core, cause); 961 } 962 } 963 964 static bool 965 e1000e_has_rxbufs(E1000ECore *core, const E1000E_RingInfo *r, 966 size_t total_size) 967 { 968 uint32_t bufs = e1000e_ring_free_descr_num(core, r); 969 970 trace_e1000e_rx_has_buffers(r->idx, bufs, total_size, 971 core->rx_desc_buf_size); 972 973 return total_size <= bufs / (core->rx_desc_len / E1000_MIN_RX_DESC_LEN) * 974 core->rx_desc_buf_size; 975 } 976 977 void 978 e1000e_start_recv(E1000ECore *core) 979 { 980 int i; 981 982 trace_e1000e_rx_start_recv(); 983 984 for (i = 0; i <= core->max_queue_num; i++) { 985 qemu_flush_queued_packets(qemu_get_subqueue(core->owner_nic, i)); 986 } 987 } 988 989 bool 990 e1000e_can_receive(E1000ECore *core) 991 { 992 int i; 993 994 if (!e1000x_rx_ready(core->owner, core->mac)) { 995 return false; 996 } 997 998 for (i = 0; i < E1000E_NUM_QUEUES; i++) { 999 E1000E_RxRing rxr; 1000 1001 e1000e_rx_ring_init(core, &rxr, i); 1002 if (e1000e_ring_enabled(core, rxr.i) && 1003 e1000e_has_rxbufs(core, rxr.i, 1)) { 1004 trace_e1000e_rx_can_recv(); 1005 return true; 1006 } 1007 } 1008 1009 trace_e1000e_rx_can_recv_rings_full(); 1010 return false; 1011 } 1012 1013 ssize_t 1014 e1000e_receive(E1000ECore *core, const uint8_t *buf, size_t size) 1015 { 1016 const struct iovec iov = { 1017 .iov_base = (uint8_t *)buf, 1018 .iov_len = size 1019 }; 1020 1021 return e1000e_receive_iov(core, &iov, 1); 1022 } 1023 1024 static inline bool 1025 e1000e_rx_l3_cso_enabled(E1000ECore *core) 1026 { 1027 return !!(core->mac[RXCSUM] & E1000_RXCSUM_IPOFLD); 1028 } 1029 1030 static inline bool 1031 e1000e_rx_l4_cso_enabled(E1000ECore *core) 1032 { 1033 return !!(core->mac[RXCSUM] & E1000_RXCSUM_TUOFLD); 1034 } 1035 1036 static bool 1037 e1000e_receive_filter(E1000ECore *core, const uint8_t *buf, int size) 1038 { 1039 uint32_t rctl = core->mac[RCTL]; 1040 1041 if (e1000x_is_vlan_packet(buf, core->mac[VET]) && 1042 e1000x_vlan_rx_filter_enabled(core->mac)) { 1043 uint16_t vid = lduw_be_p(&PKT_GET_VLAN_HDR(buf)->h_tci); 1044 uint32_t vfta = 1045 ldl_le_p((uint32_t *)(core->mac + VFTA) + 1046 ((vid >> E1000_VFTA_ENTRY_SHIFT) & E1000_VFTA_ENTRY_MASK)); 1047 if ((vfta & (1 << (vid & E1000_VFTA_ENTRY_BIT_SHIFT_MASK))) == 0) { 1048 trace_e1000e_rx_flt_vlan_mismatch(vid); 1049 return false; 1050 } else { 1051 trace_e1000e_rx_flt_vlan_match(vid); 1052 } 1053 } 1054 1055 switch (net_rx_pkt_get_packet_type(core->rx_pkt)) { 1056 case ETH_PKT_UCAST: 1057 if (rctl & E1000_RCTL_UPE) { 1058 return true; /* promiscuous ucast */ 1059 } 1060 break; 1061 1062 case ETH_PKT_BCAST: 1063 if (rctl & E1000_RCTL_BAM) { 1064 return true; /* broadcast enabled */ 1065 } 1066 break; 1067 1068 case ETH_PKT_MCAST: 1069 if (rctl & E1000_RCTL_MPE) { 1070 return true; /* promiscuous mcast */ 1071 } 1072 break; 1073 1074 default: 1075 g_assert_not_reached(); 1076 } 1077 1078 return e1000x_rx_group_filter(core->mac, buf); 1079 } 1080 1081 static inline void 1082 e1000e_read_lgcy_rx_descr(E1000ECore *core, struct e1000_rx_desc *desc, 1083 hwaddr *buff_addr) 1084 { 1085 *buff_addr = le64_to_cpu(desc->buffer_addr); 1086 } 1087 1088 static inline void 1089 e1000e_read_ext_rx_descr(E1000ECore *core, union e1000_rx_desc_extended *desc, 1090 hwaddr *buff_addr) 1091 { 1092 *buff_addr = le64_to_cpu(desc->read.buffer_addr); 1093 } 1094 1095 static inline void 1096 e1000e_read_ps_rx_descr(E1000ECore *core, 1097 union e1000_rx_desc_packet_split *desc, 1098 hwaddr buff_addr[MAX_PS_BUFFERS]) 1099 { 1100 int i; 1101 1102 for (i = 0; i < MAX_PS_BUFFERS; i++) { 1103 buff_addr[i] = le64_to_cpu(desc->read.buffer_addr[i]); 1104 } 1105 1106 trace_e1000e_rx_desc_ps_read(buff_addr[0], buff_addr[1], 1107 buff_addr[2], buff_addr[3]); 1108 } 1109 1110 static inline void 1111 e1000e_read_rx_descr(E1000ECore *core, union e1000_rx_desc_union *desc, 1112 hwaddr buff_addr[MAX_PS_BUFFERS]) 1113 { 1114 if (e1000e_rx_use_legacy_descriptor(core)) { 1115 e1000e_read_lgcy_rx_descr(core, &desc->legacy, &buff_addr[0]); 1116 buff_addr[1] = buff_addr[2] = buff_addr[3] = 0; 1117 } else { 1118 if (core->mac[RCTL] & E1000_RCTL_DTYP_PS) { 1119 e1000e_read_ps_rx_descr(core, &desc->packet_split, buff_addr); 1120 } else { 1121 e1000e_read_ext_rx_descr(core, &desc->extended, &buff_addr[0]); 1122 buff_addr[1] = buff_addr[2] = buff_addr[3] = 0; 1123 } 1124 } 1125 } 1126 1127 static void 1128 e1000e_verify_csum_in_sw(E1000ECore *core, 1129 struct NetRxPkt *pkt, 1130 uint32_t *status_flags, 1131 EthL4HdrProto l4hdr_proto) 1132 { 1133 bool csum_valid; 1134 uint32_t csum_error; 1135 1136 if (e1000e_rx_l3_cso_enabled(core)) { 1137 if (!net_rx_pkt_validate_l3_csum(pkt, &csum_valid)) { 1138 trace_e1000e_rx_metadata_l3_csum_validation_failed(); 1139 } else { 1140 csum_error = csum_valid ? 0 : E1000_RXDEXT_STATERR_IPE; 1141 *status_flags |= E1000_RXD_STAT_IPCS | csum_error; 1142 } 1143 } else { 1144 trace_e1000e_rx_metadata_l3_cso_disabled(); 1145 } 1146 1147 if (!e1000e_rx_l4_cso_enabled(core)) { 1148 trace_e1000e_rx_metadata_l4_cso_disabled(); 1149 return; 1150 } 1151 1152 if (!net_rx_pkt_validate_l4_csum(pkt, &csum_valid)) { 1153 trace_e1000e_rx_metadata_l4_csum_validation_failed(); 1154 return; 1155 } 1156 1157 csum_error = csum_valid ? 0 : E1000_RXDEXT_STATERR_TCPE; 1158 *status_flags |= E1000_RXD_STAT_TCPCS | csum_error; 1159 1160 if (l4hdr_proto == ETH_L4_HDR_PROTO_UDP) { 1161 *status_flags |= E1000_RXD_STAT_UDPCS; 1162 } 1163 } 1164 1165 static inline bool 1166 e1000e_is_tcp_ack(E1000ECore *core, struct NetRxPkt *rx_pkt) 1167 { 1168 if (!net_rx_pkt_is_tcp_ack(rx_pkt)) { 1169 return false; 1170 } 1171 1172 if (core->mac[RFCTL] & E1000_RFCTL_ACK_DATA_DIS) { 1173 return !net_rx_pkt_has_tcp_data(rx_pkt); 1174 } 1175 1176 return true; 1177 } 1178 1179 static void 1180 e1000e_build_rx_metadata(E1000ECore *core, 1181 struct NetRxPkt *pkt, 1182 bool is_eop, 1183 const E1000E_RSSInfo *rss_info, 1184 uint32_t *rss, uint32_t *mrq, 1185 uint32_t *status_flags, 1186 uint16_t *ip_id, 1187 uint16_t *vlan_tag) 1188 { 1189 struct virtio_net_hdr *vhdr; 1190 bool hasip4, hasip6; 1191 EthL4HdrProto l4hdr_proto; 1192 uint32_t pkt_type; 1193 1194 *status_flags = E1000_RXD_STAT_DD; 1195 1196 /* No additional metadata needed for non-EOP descriptors */ 1197 if (!is_eop) { 1198 goto func_exit; 1199 } 1200 1201 *status_flags |= E1000_RXD_STAT_EOP; 1202 1203 net_rx_pkt_get_protocols(pkt, &hasip4, &hasip6, &l4hdr_proto); 1204 trace_e1000e_rx_metadata_protocols(hasip4, hasip6, l4hdr_proto); 1205 1206 /* VLAN state */ 1207 if (net_rx_pkt_is_vlan_stripped(pkt)) { 1208 *status_flags |= E1000_RXD_STAT_VP; 1209 *vlan_tag = cpu_to_le16(net_rx_pkt_get_vlan_tag(pkt)); 1210 trace_e1000e_rx_metadata_vlan(*vlan_tag); 1211 } 1212 1213 /* Packet parsing results */ 1214 if ((core->mac[RXCSUM] & E1000_RXCSUM_PCSD) != 0) { 1215 if (rss_info->enabled) { 1216 *rss = cpu_to_le32(rss_info->hash); 1217 *mrq = cpu_to_le32(rss_info->type | (rss_info->queue << 8)); 1218 trace_e1000e_rx_metadata_rss(*rss, *mrq); 1219 } 1220 } else if (hasip4) { 1221 *status_flags |= E1000_RXD_STAT_IPIDV; 1222 *ip_id = cpu_to_le16(net_rx_pkt_get_ip_id(pkt)); 1223 trace_e1000e_rx_metadata_ip_id(*ip_id); 1224 } 1225 1226 if (l4hdr_proto == ETH_L4_HDR_PROTO_TCP && e1000e_is_tcp_ack(core, pkt)) { 1227 *status_flags |= E1000_RXD_STAT_ACK; 1228 trace_e1000e_rx_metadata_ack(); 1229 } 1230 1231 if (hasip6 && (core->mac[RFCTL] & E1000_RFCTL_IPV6_DIS)) { 1232 trace_e1000e_rx_metadata_ipv6_filtering_disabled(); 1233 pkt_type = E1000_RXD_PKT_MAC; 1234 } else if (l4hdr_proto == ETH_L4_HDR_PROTO_TCP || 1235 l4hdr_proto == ETH_L4_HDR_PROTO_UDP) { 1236 pkt_type = hasip4 ? E1000_RXD_PKT_IP4_XDP : E1000_RXD_PKT_IP6_XDP; 1237 } else if (hasip4 || hasip6) { 1238 pkt_type = hasip4 ? E1000_RXD_PKT_IP4 : E1000_RXD_PKT_IP6; 1239 } else { 1240 pkt_type = E1000_RXD_PKT_MAC; 1241 } 1242 1243 *status_flags |= E1000_RXD_PKT_TYPE(pkt_type); 1244 trace_e1000e_rx_metadata_pkt_type(pkt_type); 1245 1246 /* RX CSO information */ 1247 if (hasip6 && (core->mac[RFCTL] & E1000_RFCTL_IPV6_XSUM_DIS)) { 1248 trace_e1000e_rx_metadata_ipv6_sum_disabled(); 1249 goto func_exit; 1250 } 1251 1252 vhdr = net_rx_pkt_get_vhdr(pkt); 1253 1254 if (!(vhdr->flags & VIRTIO_NET_HDR_F_DATA_VALID) && 1255 !(vhdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM)) { 1256 trace_e1000e_rx_metadata_virthdr_no_csum_info(); 1257 e1000e_verify_csum_in_sw(core, pkt, status_flags, l4hdr_proto); 1258 goto func_exit; 1259 } 1260 1261 if (e1000e_rx_l3_cso_enabled(core)) { 1262 *status_flags |= hasip4 ? E1000_RXD_STAT_IPCS : 0; 1263 } else { 1264 trace_e1000e_rx_metadata_l3_cso_disabled(); 1265 } 1266 1267 if (e1000e_rx_l4_cso_enabled(core)) { 1268 switch (l4hdr_proto) { 1269 case ETH_L4_HDR_PROTO_TCP: 1270 *status_flags |= E1000_RXD_STAT_TCPCS; 1271 break; 1272 1273 case ETH_L4_HDR_PROTO_UDP: 1274 *status_flags |= E1000_RXD_STAT_TCPCS | E1000_RXD_STAT_UDPCS; 1275 break; 1276 1277 default: 1278 break; 1279 } 1280 } else { 1281 trace_e1000e_rx_metadata_l4_cso_disabled(); 1282 } 1283 1284 trace_e1000e_rx_metadata_status_flags(*status_flags); 1285 1286 func_exit: 1287 *status_flags = cpu_to_le32(*status_flags); 1288 } 1289 1290 static inline void 1291 e1000e_write_lgcy_rx_descr(E1000ECore *core, struct e1000_rx_desc *desc, 1292 struct NetRxPkt *pkt, 1293 const E1000E_RSSInfo *rss_info, 1294 uint16_t length) 1295 { 1296 uint32_t status_flags, rss, mrq; 1297 uint16_t ip_id; 1298 1299 assert(!rss_info->enabled); 1300 1301 desc->length = cpu_to_le16(length); 1302 desc->csum = 0; 1303 1304 e1000e_build_rx_metadata(core, pkt, pkt != NULL, 1305 rss_info, 1306 &rss, &mrq, 1307 &status_flags, &ip_id, 1308 &desc->special); 1309 desc->errors = (uint8_t) (le32_to_cpu(status_flags) >> 24); 1310 desc->status = (uint8_t) le32_to_cpu(status_flags); 1311 } 1312 1313 static inline void 1314 e1000e_write_ext_rx_descr(E1000ECore *core, union e1000_rx_desc_extended *desc, 1315 struct NetRxPkt *pkt, 1316 const E1000E_RSSInfo *rss_info, 1317 uint16_t length) 1318 { 1319 memset(&desc->wb, 0, sizeof(desc->wb)); 1320 1321 desc->wb.upper.length = cpu_to_le16(length); 1322 1323 e1000e_build_rx_metadata(core, pkt, pkt != NULL, 1324 rss_info, 1325 &desc->wb.lower.hi_dword.rss, 1326 &desc->wb.lower.mrq, 1327 &desc->wb.upper.status_error, 1328 &desc->wb.lower.hi_dword.csum_ip.ip_id, 1329 &desc->wb.upper.vlan); 1330 } 1331 1332 static inline void 1333 e1000e_write_ps_rx_descr(E1000ECore *core, 1334 union e1000_rx_desc_packet_split *desc, 1335 struct NetRxPkt *pkt, 1336 const E1000E_RSSInfo *rss_info, 1337 size_t ps_hdr_len, 1338 uint16_t(*written)[MAX_PS_BUFFERS]) 1339 { 1340 int i; 1341 1342 memset(&desc->wb, 0, sizeof(desc->wb)); 1343 1344 desc->wb.middle.length0 = cpu_to_le16((*written)[0]); 1345 1346 for (i = 0; i < PS_PAGE_BUFFERS; i++) { 1347 desc->wb.upper.length[i] = cpu_to_le16((*written)[i + 1]); 1348 } 1349 1350 e1000e_build_rx_metadata(core, pkt, pkt != NULL, 1351 rss_info, 1352 &desc->wb.lower.hi_dword.rss, 1353 &desc->wb.lower.mrq, 1354 &desc->wb.middle.status_error, 1355 &desc->wb.lower.hi_dword.csum_ip.ip_id, 1356 &desc->wb.middle.vlan); 1357 1358 desc->wb.upper.header_status = 1359 cpu_to_le16(ps_hdr_len | (ps_hdr_len ? E1000_RXDPS_HDRSTAT_HDRSP : 0)); 1360 1361 trace_e1000e_rx_desc_ps_write((*written)[0], (*written)[1], 1362 (*written)[2], (*written)[3]); 1363 } 1364 1365 static inline void 1366 e1000e_write_rx_descr(E1000ECore *core, union e1000_rx_desc_union *desc, 1367 struct NetRxPkt *pkt, const E1000E_RSSInfo *rss_info, 1368 size_t ps_hdr_len, uint16_t(*written)[MAX_PS_BUFFERS]) 1369 { 1370 if (e1000e_rx_use_legacy_descriptor(core)) { 1371 assert(ps_hdr_len == 0); 1372 e1000e_write_lgcy_rx_descr(core, &desc->legacy, pkt, rss_info, 1373 (*written)[0]); 1374 } else { 1375 if (core->mac[RCTL] & E1000_RCTL_DTYP_PS) { 1376 e1000e_write_ps_rx_descr(core, &desc->packet_split, pkt, rss_info, 1377 ps_hdr_len, written); 1378 } else { 1379 assert(ps_hdr_len == 0); 1380 e1000e_write_ext_rx_descr(core, &desc->extended, pkt, rss_info, 1381 (*written)[0]); 1382 } 1383 } 1384 } 1385 1386 static inline void 1387 e1000e_pci_dma_write_rx_desc(E1000ECore *core, dma_addr_t addr, 1388 union e1000_rx_desc_union *desc, dma_addr_t len) 1389 { 1390 PCIDevice *dev = core->owner; 1391 1392 if (e1000e_rx_use_legacy_descriptor(core)) { 1393 struct e1000_rx_desc *d = &desc->legacy; 1394 size_t offset = offsetof(struct e1000_rx_desc, status); 1395 uint8_t status = d->status; 1396 1397 d->status &= ~E1000_RXD_STAT_DD; 1398 pci_dma_write(dev, addr, desc, len); 1399 1400 if (status & E1000_RXD_STAT_DD) { 1401 d->status = status; 1402 pci_dma_write(dev, addr + offset, &status, sizeof(status)); 1403 } 1404 } else { 1405 if (core->mac[RCTL] & E1000_RCTL_DTYP_PS) { 1406 union e1000_rx_desc_packet_split *d = &desc->packet_split; 1407 size_t offset = offsetof(union e1000_rx_desc_packet_split, 1408 wb.middle.status_error); 1409 uint32_t status = d->wb.middle.status_error; 1410 1411 d->wb.middle.status_error &= ~E1000_RXD_STAT_DD; 1412 pci_dma_write(dev, addr, desc, len); 1413 1414 if (status & E1000_RXD_STAT_DD) { 1415 d->wb.middle.status_error = status; 1416 pci_dma_write(dev, addr + offset, &status, sizeof(status)); 1417 } 1418 } else { 1419 union e1000_rx_desc_extended *d = &desc->extended; 1420 size_t offset = offsetof(union e1000_rx_desc_extended, 1421 wb.upper.status_error); 1422 uint32_t status = d->wb.upper.status_error; 1423 1424 d->wb.upper.status_error &= ~E1000_RXD_STAT_DD; 1425 pci_dma_write(dev, addr, desc, len); 1426 1427 if (status & E1000_RXD_STAT_DD) { 1428 d->wb.upper.status_error = status; 1429 pci_dma_write(dev, addr + offset, &status, sizeof(status)); 1430 } 1431 } 1432 } 1433 } 1434 1435 typedef struct e1000e_ba_state_st { 1436 uint16_t written[MAX_PS_BUFFERS]; 1437 uint8_t cur_idx; 1438 } e1000e_ba_state; 1439 1440 static inline void 1441 e1000e_write_hdr_to_rx_buffers(E1000ECore *core, 1442 hwaddr ba[MAX_PS_BUFFERS], 1443 e1000e_ba_state *bastate, 1444 const char *data, 1445 dma_addr_t data_len) 1446 { 1447 assert(data_len <= core->rxbuf_sizes[0] - bastate->written[0]); 1448 1449 pci_dma_write(core->owner, ba[0] + bastate->written[0], data, data_len); 1450 bastate->written[0] += data_len; 1451 1452 bastate->cur_idx = 1; 1453 } 1454 1455 static void 1456 e1000e_write_to_rx_buffers(E1000ECore *core, 1457 hwaddr ba[MAX_PS_BUFFERS], 1458 e1000e_ba_state *bastate, 1459 const char *data, 1460 dma_addr_t data_len) 1461 { 1462 while (data_len > 0) { 1463 uint32_t cur_buf_len = core->rxbuf_sizes[bastate->cur_idx]; 1464 uint32_t cur_buf_bytes_left = cur_buf_len - 1465 bastate->written[bastate->cur_idx]; 1466 uint32_t bytes_to_write = MIN(data_len, cur_buf_bytes_left); 1467 1468 trace_e1000e_rx_desc_buff_write(bastate->cur_idx, 1469 ba[bastate->cur_idx], 1470 bastate->written[bastate->cur_idx], 1471 data, 1472 bytes_to_write); 1473 1474 pci_dma_write(core->owner, 1475 ba[bastate->cur_idx] + bastate->written[bastate->cur_idx], 1476 data, bytes_to_write); 1477 1478 bastate->written[bastate->cur_idx] += bytes_to_write; 1479 data += bytes_to_write; 1480 data_len -= bytes_to_write; 1481 1482 if (bastate->written[bastate->cur_idx] == cur_buf_len) { 1483 bastate->cur_idx++; 1484 } 1485 1486 assert(bastate->cur_idx < MAX_PS_BUFFERS); 1487 } 1488 } 1489 1490 static void 1491 e1000e_update_rx_stats(E1000ECore *core, size_t pkt_size, size_t pkt_fcs_size) 1492 { 1493 eth_pkt_types_e pkt_type = net_rx_pkt_get_packet_type(core->rx_pkt); 1494 e1000x_update_rx_total_stats(core->mac, pkt_type, pkt_size, pkt_fcs_size); 1495 } 1496 1497 static inline bool 1498 e1000e_rx_descr_threshold_hit(E1000ECore *core, const E1000E_RingInfo *rxi) 1499 { 1500 return e1000e_ring_free_descr_num(core, rxi) == 1501 e1000e_ring_len(core, rxi) >> core->rxbuf_min_shift; 1502 } 1503 1504 static bool 1505 e1000e_do_ps(E1000ECore *core, struct NetRxPkt *pkt, size_t *hdr_len) 1506 { 1507 bool hasip4, hasip6; 1508 EthL4HdrProto l4hdr_proto; 1509 bool fragment; 1510 1511 if (!e1000e_rx_use_ps_descriptor(core)) { 1512 return false; 1513 } 1514 1515 net_rx_pkt_get_protocols(pkt, &hasip4, &hasip6, &l4hdr_proto); 1516 1517 if (hasip4) { 1518 fragment = net_rx_pkt_get_ip4_info(pkt)->fragment; 1519 } else if (hasip6) { 1520 fragment = net_rx_pkt_get_ip6_info(pkt)->fragment; 1521 } else { 1522 return false; 1523 } 1524 1525 if (fragment && (core->mac[RFCTL] & E1000_RFCTL_IPFRSP_DIS)) { 1526 return false; 1527 } 1528 1529 if (l4hdr_proto == ETH_L4_HDR_PROTO_TCP || 1530 l4hdr_proto == ETH_L4_HDR_PROTO_UDP) { 1531 *hdr_len = net_rx_pkt_get_l5_hdr_offset(pkt); 1532 } else { 1533 *hdr_len = net_rx_pkt_get_l4_hdr_offset(pkt); 1534 } 1535 1536 if ((*hdr_len > core->rxbuf_sizes[0]) || 1537 (*hdr_len > net_rx_pkt_get_total_len(pkt))) { 1538 return false; 1539 } 1540 1541 return true; 1542 } 1543 1544 static void 1545 e1000e_write_packet_to_guest(E1000ECore *core, struct NetRxPkt *pkt, 1546 const E1000E_RxRing *rxr, 1547 const E1000E_RSSInfo *rss_info) 1548 { 1549 PCIDevice *d = core->owner; 1550 dma_addr_t base; 1551 union e1000_rx_desc_union desc; 1552 size_t desc_size; 1553 size_t desc_offset = 0; 1554 size_t iov_ofs = 0; 1555 1556 struct iovec *iov = net_rx_pkt_get_iovec(pkt); 1557 size_t size = net_rx_pkt_get_total_len(pkt); 1558 size_t total_size = size + e1000x_fcs_len(core->mac); 1559 const E1000E_RingInfo *rxi; 1560 size_t ps_hdr_len = 0; 1561 bool do_ps = e1000e_do_ps(core, pkt, &ps_hdr_len); 1562 bool is_first = true; 1563 1564 rxi = rxr->i; 1565 1566 do { 1567 hwaddr ba[MAX_PS_BUFFERS]; 1568 e1000e_ba_state bastate = { { 0 } }; 1569 bool is_last = false; 1570 1571 desc_size = total_size - desc_offset; 1572 1573 if (desc_size > core->rx_desc_buf_size) { 1574 desc_size = core->rx_desc_buf_size; 1575 } 1576 1577 if (e1000e_ring_empty(core, rxi)) { 1578 return; 1579 } 1580 1581 base = e1000e_ring_head_descr(core, rxi); 1582 1583 pci_dma_read(d, base, &desc, core->rx_desc_len); 1584 1585 trace_e1000e_rx_descr(rxi->idx, base, core->rx_desc_len); 1586 1587 e1000e_read_rx_descr(core, &desc, ba); 1588 1589 if (ba[0]) { 1590 if (desc_offset < size) { 1591 static const uint32_t fcs_pad; 1592 size_t iov_copy; 1593 size_t copy_size = size - desc_offset; 1594 if (copy_size > core->rx_desc_buf_size) { 1595 copy_size = core->rx_desc_buf_size; 1596 } 1597 1598 /* For PS mode copy the packet header first */ 1599 if (do_ps) { 1600 if (is_first) { 1601 size_t ps_hdr_copied = 0; 1602 do { 1603 iov_copy = MIN(ps_hdr_len - ps_hdr_copied, 1604 iov->iov_len - iov_ofs); 1605 1606 e1000e_write_hdr_to_rx_buffers(core, ba, &bastate, 1607 iov->iov_base, iov_copy); 1608 1609 copy_size -= iov_copy; 1610 ps_hdr_copied += iov_copy; 1611 1612 iov_ofs += iov_copy; 1613 if (iov_ofs == iov->iov_len) { 1614 iov++; 1615 iov_ofs = 0; 1616 } 1617 } while (ps_hdr_copied < ps_hdr_len); 1618 1619 is_first = false; 1620 } else { 1621 /* Leave buffer 0 of each descriptor except first */ 1622 /* empty as per spec 7.1.5.1 */ 1623 e1000e_write_hdr_to_rx_buffers(core, ba, &bastate, 1624 NULL, 0); 1625 } 1626 } 1627 1628 /* Copy packet payload */ 1629 while (copy_size) { 1630 iov_copy = MIN(copy_size, iov->iov_len - iov_ofs); 1631 1632 e1000e_write_to_rx_buffers(core, ba, &bastate, 1633 iov->iov_base + iov_ofs, iov_copy); 1634 1635 copy_size -= iov_copy; 1636 iov_ofs += iov_copy; 1637 if (iov_ofs == iov->iov_len) { 1638 iov++; 1639 iov_ofs = 0; 1640 } 1641 } 1642 1643 if (desc_offset + desc_size >= total_size) { 1644 /* Simulate FCS checksum presence in the last descriptor */ 1645 e1000e_write_to_rx_buffers(core, ba, &bastate, 1646 (const char *) &fcs_pad, e1000x_fcs_len(core->mac)); 1647 } 1648 } 1649 } else { /* as per intel docs; skip descriptors with null buf addr */ 1650 trace_e1000e_rx_null_descriptor(); 1651 } 1652 desc_offset += desc_size; 1653 if (desc_offset >= total_size) { 1654 is_last = true; 1655 } 1656 1657 e1000e_write_rx_descr(core, &desc, is_last ? core->rx_pkt : NULL, 1658 rss_info, do_ps ? ps_hdr_len : 0, &bastate.written); 1659 e1000e_pci_dma_write_rx_desc(core, base, &desc, core->rx_desc_len); 1660 1661 e1000e_ring_advance(core, rxi, 1662 core->rx_desc_len / E1000_MIN_RX_DESC_LEN); 1663 1664 } while (desc_offset < total_size); 1665 1666 e1000e_update_rx_stats(core, size, total_size); 1667 } 1668 1669 static inline void 1670 e1000e_rx_fix_l4_csum(E1000ECore *core, struct NetRxPkt *pkt) 1671 { 1672 struct virtio_net_hdr *vhdr = net_rx_pkt_get_vhdr(pkt); 1673 1674 if (vhdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) { 1675 net_rx_pkt_fix_l4_csum(pkt); 1676 } 1677 } 1678 1679 ssize_t 1680 e1000e_receive_iov(E1000ECore *core, const struct iovec *iov, int iovcnt) 1681 { 1682 return e1000e_receive_internal(core, iov, iovcnt, core->has_vnet); 1683 } 1684 1685 static ssize_t 1686 e1000e_receive_internal(E1000ECore *core, const struct iovec *iov, int iovcnt, 1687 bool has_vnet) 1688 { 1689 static const int maximum_ethernet_hdr_len = (ETH_HLEN + 4); 1690 1691 uint32_t n = 0; 1692 uint8_t min_buf[ETH_ZLEN]; 1693 struct iovec min_iov; 1694 uint8_t *filter_buf; 1695 size_t size, orig_size; 1696 size_t iov_ofs = 0; 1697 E1000E_RxRing rxr; 1698 E1000E_RSSInfo rss_info; 1699 size_t total_size; 1700 ssize_t retval; 1701 bool rdmts_hit; 1702 1703 trace_e1000e_rx_receive_iov(iovcnt); 1704 1705 if (!e1000x_hw_rx_enabled(core->mac)) { 1706 return -1; 1707 } 1708 1709 /* Pull virtio header in */ 1710 if (has_vnet) { 1711 net_rx_pkt_set_vhdr_iovec(core->rx_pkt, iov, iovcnt); 1712 iov_ofs = sizeof(struct virtio_net_hdr); 1713 } else { 1714 net_rx_pkt_unset_vhdr(core->rx_pkt); 1715 } 1716 1717 filter_buf = iov->iov_base + iov_ofs; 1718 orig_size = iov_size(iov, iovcnt); 1719 size = orig_size - iov_ofs; 1720 1721 /* Pad to minimum Ethernet frame length */ 1722 if (size < sizeof(min_buf)) { 1723 iov_to_buf(iov, iovcnt, iov_ofs, min_buf, size); 1724 memset(&min_buf[size], 0, sizeof(min_buf) - size); 1725 e1000x_inc_reg_if_not_full(core->mac, RUC); 1726 min_iov.iov_base = filter_buf = min_buf; 1727 min_iov.iov_len = size = sizeof(min_buf); 1728 iovcnt = 1; 1729 iov = &min_iov; 1730 iov_ofs = 0; 1731 } else if (iov->iov_len < maximum_ethernet_hdr_len) { 1732 /* This is very unlikely, but may happen. */ 1733 iov_to_buf(iov, iovcnt, iov_ofs, min_buf, maximum_ethernet_hdr_len); 1734 filter_buf = min_buf; 1735 } 1736 1737 /* Discard oversized packets if !LPE and !SBP. */ 1738 if (e1000x_is_oversized(core->mac, size)) { 1739 return orig_size; 1740 } 1741 1742 net_rx_pkt_set_packet_type(core->rx_pkt, 1743 get_eth_packet_type(PKT_GET_ETH_HDR(filter_buf))); 1744 1745 if (!e1000e_receive_filter(core, filter_buf, size)) { 1746 trace_e1000e_rx_flt_dropped(); 1747 return orig_size; 1748 } 1749 1750 net_rx_pkt_attach_iovec_ex(core->rx_pkt, iov, iovcnt, iov_ofs, 1751 e1000x_vlan_enabled(core->mac), core->mac[VET]); 1752 1753 e1000e_rss_parse_packet(core, core->rx_pkt, &rss_info); 1754 e1000e_rx_ring_init(core, &rxr, rss_info.queue); 1755 1756 total_size = net_rx_pkt_get_total_len(core->rx_pkt) + 1757 e1000x_fcs_len(core->mac); 1758 1759 if (e1000e_has_rxbufs(core, rxr.i, total_size)) { 1760 e1000e_rx_fix_l4_csum(core, core->rx_pkt); 1761 1762 e1000e_write_packet_to_guest(core, core->rx_pkt, &rxr, &rss_info); 1763 1764 retval = orig_size; 1765 1766 /* Perform small receive detection (RSRPD) */ 1767 if (total_size < core->mac[RSRPD]) { 1768 n |= E1000_ICS_SRPD; 1769 } 1770 1771 /* Perform ACK receive detection */ 1772 if (!(core->mac[RFCTL] & E1000_RFCTL_ACK_DIS) && 1773 (e1000e_is_tcp_ack(core, core->rx_pkt))) { 1774 n |= E1000_ICS_ACK; 1775 } 1776 1777 /* Check if receive descriptor minimum threshold hit */ 1778 rdmts_hit = e1000e_rx_descr_threshold_hit(core, rxr.i); 1779 n |= e1000e_rx_wb_interrupt_cause(core, rxr.i->idx, rdmts_hit); 1780 1781 trace_e1000e_rx_written_to_guest(rxr.i->idx); 1782 } else { 1783 n |= E1000_ICS_RXO; 1784 retval = 0; 1785 1786 trace_e1000e_rx_not_written_to_guest(rxr.i->idx); 1787 } 1788 1789 if (!e1000e_intrmgr_delay_rx_causes(core, &n)) { 1790 trace_e1000e_rx_interrupt_set(n); 1791 e1000e_set_interrupt_cause(core, n); 1792 } else { 1793 trace_e1000e_rx_interrupt_delayed(n); 1794 } 1795 1796 return retval; 1797 } 1798 1799 static inline bool 1800 e1000e_have_autoneg(E1000ECore *core) 1801 { 1802 return core->phy[0][MII_BMCR] & MII_BMCR_AUTOEN; 1803 } 1804 1805 static void e1000e_update_flowctl_status(E1000ECore *core) 1806 { 1807 if (e1000e_have_autoneg(core) && 1808 core->phy[0][MII_BMSR] & MII_BMSR_AN_COMP) { 1809 trace_e1000e_link_autoneg_flowctl(true); 1810 core->mac[CTRL] |= E1000_CTRL_TFCE | E1000_CTRL_RFCE; 1811 } else { 1812 trace_e1000e_link_autoneg_flowctl(false); 1813 } 1814 } 1815 1816 static inline void 1817 e1000e_link_down(E1000ECore *core) 1818 { 1819 e1000x_update_regs_on_link_down(core->mac, core->phy[0]); 1820 e1000e_update_flowctl_status(core); 1821 } 1822 1823 static inline void 1824 e1000e_set_phy_ctrl(E1000ECore *core, int index, uint16_t val) 1825 { 1826 /* bits 0-5 reserved; MII_BMCR_[ANRESTART,RESET] are self clearing */ 1827 core->phy[0][MII_BMCR] = val & ~(0x3f | 1828 MII_BMCR_RESET | 1829 MII_BMCR_ANRESTART); 1830 1831 if ((val & MII_BMCR_ANRESTART) && 1832 e1000e_have_autoneg(core)) { 1833 e1000x_restart_autoneg(core->mac, core->phy[0], core->autoneg_timer); 1834 } 1835 } 1836 1837 static void 1838 e1000e_set_phy_oem_bits(E1000ECore *core, int index, uint16_t val) 1839 { 1840 core->phy[0][PHY_OEM_BITS] = val & ~BIT(10); 1841 1842 if (val & BIT(10)) { 1843 e1000x_restart_autoneg(core->mac, core->phy[0], core->autoneg_timer); 1844 } 1845 } 1846 1847 static void 1848 e1000e_set_phy_page(E1000ECore *core, int index, uint16_t val) 1849 { 1850 core->phy[0][PHY_PAGE] = val & PHY_PAGE_RW_MASK; 1851 } 1852 1853 void 1854 e1000e_core_set_link_status(E1000ECore *core) 1855 { 1856 NetClientState *nc = qemu_get_queue(core->owner_nic); 1857 uint32_t old_status = core->mac[STATUS]; 1858 1859 trace_e1000e_link_status_changed(nc->link_down ? false : true); 1860 1861 if (nc->link_down) { 1862 e1000x_update_regs_on_link_down(core->mac, core->phy[0]); 1863 } else { 1864 if (e1000e_have_autoneg(core) && 1865 !(core->phy[0][MII_BMSR] & MII_BMSR_AN_COMP)) { 1866 e1000x_restart_autoneg(core->mac, core->phy[0], 1867 core->autoneg_timer); 1868 } else { 1869 e1000x_update_regs_on_link_up(core->mac, core->phy[0]); 1870 e1000e_start_recv(core); 1871 } 1872 } 1873 1874 if (core->mac[STATUS] != old_status) { 1875 e1000e_set_interrupt_cause(core, E1000_ICR_LSC); 1876 } 1877 } 1878 1879 static void 1880 e1000e_set_ctrl(E1000ECore *core, int index, uint32_t val) 1881 { 1882 trace_e1000e_core_ctrl_write(index, val); 1883 1884 /* RST is self clearing */ 1885 core->mac[CTRL] = val & ~E1000_CTRL_RST; 1886 core->mac[CTRL_DUP] = core->mac[CTRL]; 1887 1888 trace_e1000e_link_set_params( 1889 !!(val & E1000_CTRL_ASDE), 1890 (val & E1000_CTRL_SPD_SEL) >> E1000_CTRL_SPD_SHIFT, 1891 !!(val & E1000_CTRL_FRCSPD), 1892 !!(val & E1000_CTRL_FRCDPX), 1893 !!(val & E1000_CTRL_RFCE), 1894 !!(val & E1000_CTRL_TFCE)); 1895 1896 if (val & E1000_CTRL_RST) { 1897 trace_e1000e_core_ctrl_sw_reset(); 1898 e1000e_reset(core, true); 1899 } 1900 1901 if (val & E1000_CTRL_PHY_RST) { 1902 trace_e1000e_core_ctrl_phy_reset(); 1903 core->mac[STATUS] |= E1000_STATUS_PHYRA; 1904 } 1905 } 1906 1907 static void 1908 e1000e_set_rfctl(E1000ECore *core, int index, uint32_t val) 1909 { 1910 trace_e1000e_rx_set_rfctl(val); 1911 1912 if (!(val & E1000_RFCTL_ISCSI_DIS)) { 1913 trace_e1000e_wrn_iscsi_filtering_not_supported(); 1914 } 1915 1916 if (!(val & E1000_RFCTL_NFSW_DIS)) { 1917 trace_e1000e_wrn_nfsw_filtering_not_supported(); 1918 } 1919 1920 if (!(val & E1000_RFCTL_NFSR_DIS)) { 1921 trace_e1000e_wrn_nfsr_filtering_not_supported(); 1922 } 1923 1924 core->mac[RFCTL] = val; 1925 } 1926 1927 static void 1928 e1000e_calc_per_desc_buf_size(E1000ECore *core) 1929 { 1930 int i; 1931 core->rx_desc_buf_size = 0; 1932 1933 for (i = 0; i < ARRAY_SIZE(core->rxbuf_sizes); i++) { 1934 core->rx_desc_buf_size += core->rxbuf_sizes[i]; 1935 } 1936 } 1937 1938 static void 1939 e1000e_parse_rxbufsize(E1000ECore *core) 1940 { 1941 uint32_t rctl = core->mac[RCTL]; 1942 1943 memset(core->rxbuf_sizes, 0, sizeof(core->rxbuf_sizes)); 1944 1945 if (rctl & E1000_RCTL_DTYP_MASK) { 1946 uint32_t bsize; 1947 1948 bsize = core->mac[PSRCTL] & E1000_PSRCTL_BSIZE0_MASK; 1949 core->rxbuf_sizes[0] = (bsize >> E1000_PSRCTL_BSIZE0_SHIFT) * 128; 1950 1951 bsize = core->mac[PSRCTL] & E1000_PSRCTL_BSIZE1_MASK; 1952 core->rxbuf_sizes[1] = (bsize >> E1000_PSRCTL_BSIZE1_SHIFT) * 1024; 1953 1954 bsize = core->mac[PSRCTL] & E1000_PSRCTL_BSIZE2_MASK; 1955 core->rxbuf_sizes[2] = (bsize >> E1000_PSRCTL_BSIZE2_SHIFT) * 1024; 1956 1957 bsize = core->mac[PSRCTL] & E1000_PSRCTL_BSIZE3_MASK; 1958 core->rxbuf_sizes[3] = (bsize >> E1000_PSRCTL_BSIZE3_SHIFT) * 1024; 1959 } else if (rctl & E1000_RCTL_FLXBUF_MASK) { 1960 int flxbuf = rctl & E1000_RCTL_FLXBUF_MASK; 1961 core->rxbuf_sizes[0] = (flxbuf >> E1000_RCTL_FLXBUF_SHIFT) * 1024; 1962 } else { 1963 core->rxbuf_sizes[0] = e1000x_rxbufsize(rctl); 1964 } 1965 1966 trace_e1000e_rx_desc_buff_sizes(core->rxbuf_sizes[0], core->rxbuf_sizes[1], 1967 core->rxbuf_sizes[2], core->rxbuf_sizes[3]); 1968 1969 e1000e_calc_per_desc_buf_size(core); 1970 } 1971 1972 static void 1973 e1000e_calc_rxdesclen(E1000ECore *core) 1974 { 1975 if (e1000e_rx_use_legacy_descriptor(core)) { 1976 core->rx_desc_len = sizeof(struct e1000_rx_desc); 1977 } else { 1978 if (core->mac[RCTL] & E1000_RCTL_DTYP_PS) { 1979 core->rx_desc_len = sizeof(union e1000_rx_desc_packet_split); 1980 } else { 1981 core->rx_desc_len = sizeof(union e1000_rx_desc_extended); 1982 } 1983 } 1984 trace_e1000e_rx_desc_len(core->rx_desc_len); 1985 } 1986 1987 static void 1988 e1000e_set_rx_control(E1000ECore *core, int index, uint32_t val) 1989 { 1990 core->mac[RCTL] = val; 1991 trace_e1000e_rx_set_rctl(core->mac[RCTL]); 1992 1993 if (val & E1000_RCTL_EN) { 1994 e1000e_parse_rxbufsize(core); 1995 e1000e_calc_rxdesclen(core); 1996 core->rxbuf_min_shift = ((val / E1000_RCTL_RDMTS_QUAT) & 3) + 1 + 1997 E1000_RING_DESC_LEN_SHIFT; 1998 1999 e1000e_start_recv(core); 2000 } 2001 } 2002 2003 static 2004 void(*e1000e_phyreg_writeops[E1000E_PHY_PAGES][E1000E_PHY_PAGE_SIZE]) 2005 (E1000ECore *, int, uint16_t) = { 2006 [0] = { 2007 [MII_BMCR] = e1000e_set_phy_ctrl, 2008 [PHY_PAGE] = e1000e_set_phy_page, 2009 [PHY_OEM_BITS] = e1000e_set_phy_oem_bits 2010 } 2011 }; 2012 2013 static inline void 2014 e1000e_clear_ims_bits(E1000ECore *core, uint32_t bits) 2015 { 2016 trace_e1000e_irq_clear_ims(bits, core->mac[IMS], core->mac[IMS] & ~bits); 2017 core->mac[IMS] &= ~bits; 2018 } 2019 2020 static inline bool 2021 e1000e_postpone_interrupt(E1000IntrDelayTimer *timer) 2022 { 2023 if (timer->running) { 2024 trace_e1000e_irq_postponed_by_xitr(timer->delay_reg << 2); 2025 2026 return true; 2027 } 2028 2029 if (timer->core->mac[timer->delay_reg] != 0) { 2030 e1000e_intrmgr_rearm_timer(timer); 2031 } 2032 2033 return false; 2034 } 2035 2036 static inline bool 2037 e1000e_itr_should_postpone(E1000ECore *core) 2038 { 2039 return e1000e_postpone_interrupt(&core->itr); 2040 } 2041 2042 static inline bool 2043 e1000e_eitr_should_postpone(E1000ECore *core, int idx) 2044 { 2045 return e1000e_postpone_interrupt(&core->eitr[idx]); 2046 } 2047 2048 static void 2049 e1000e_msix_notify_one(E1000ECore *core, uint32_t cause, uint32_t int_cfg) 2050 { 2051 uint32_t effective_eiac; 2052 2053 if (E1000_IVAR_ENTRY_VALID(int_cfg)) { 2054 uint32_t vec = E1000_IVAR_ENTRY_VEC(int_cfg); 2055 if (vec < E1000E_MSIX_VEC_NUM) { 2056 if (!e1000e_eitr_should_postpone(core, vec)) { 2057 trace_e1000e_irq_msix_notify_vec(vec); 2058 msix_notify(core->owner, vec); 2059 } 2060 } else { 2061 trace_e1000e_wrn_msix_vec_wrong(cause, int_cfg); 2062 } 2063 } else { 2064 trace_e1000e_wrn_msix_invalid(cause, int_cfg); 2065 } 2066 2067 if (core->mac[CTRL_EXT] & E1000_CTRL_EXT_EIAME) { 2068 trace_e1000e_irq_iam_clear_eiame(core->mac[IAM], cause); 2069 core->mac[IAM] &= ~cause; 2070 } 2071 2072 trace_e1000e_irq_icr_clear_eiac(core->mac[ICR], core->mac[EIAC]); 2073 2074 effective_eiac = core->mac[EIAC] & cause; 2075 2076 core->mac[ICR] &= ~effective_eiac; 2077 core->msi_causes_pending &= ~effective_eiac; 2078 2079 if (!(core->mac[CTRL_EXT] & E1000_CTRL_EXT_IAME)) { 2080 core->mac[IMS] &= ~effective_eiac; 2081 } 2082 } 2083 2084 static void 2085 e1000e_msix_notify(E1000ECore *core, uint32_t causes) 2086 { 2087 if (causes & E1000_ICR_RXQ0) { 2088 e1000e_msix_notify_one(core, E1000_ICR_RXQ0, 2089 E1000_IVAR_RXQ0(core->mac[IVAR])); 2090 } 2091 2092 if (causes & E1000_ICR_RXQ1) { 2093 e1000e_msix_notify_one(core, E1000_ICR_RXQ1, 2094 E1000_IVAR_RXQ1(core->mac[IVAR])); 2095 } 2096 2097 if (causes & E1000_ICR_TXQ0) { 2098 e1000e_msix_notify_one(core, E1000_ICR_TXQ0, 2099 E1000_IVAR_TXQ0(core->mac[IVAR])); 2100 } 2101 2102 if (causes & E1000_ICR_TXQ1) { 2103 e1000e_msix_notify_one(core, E1000_ICR_TXQ1, 2104 E1000_IVAR_TXQ1(core->mac[IVAR])); 2105 } 2106 2107 if (causes & E1000_ICR_OTHER) { 2108 e1000e_msix_notify_one(core, E1000_ICR_OTHER, 2109 E1000_IVAR_OTHER(core->mac[IVAR])); 2110 } 2111 } 2112 2113 static void 2114 e1000e_msix_clear_one(E1000ECore *core, uint32_t cause, uint32_t int_cfg) 2115 { 2116 if (E1000_IVAR_ENTRY_VALID(int_cfg)) { 2117 uint32_t vec = E1000_IVAR_ENTRY_VEC(int_cfg); 2118 if (vec < E1000E_MSIX_VEC_NUM) { 2119 trace_e1000e_irq_msix_pending_clearing(cause, int_cfg, vec); 2120 msix_clr_pending(core->owner, vec); 2121 } else { 2122 trace_e1000e_wrn_msix_vec_wrong(cause, int_cfg); 2123 } 2124 } else { 2125 trace_e1000e_wrn_msix_invalid(cause, int_cfg); 2126 } 2127 } 2128 2129 static void 2130 e1000e_msix_clear(E1000ECore *core, uint32_t causes) 2131 { 2132 if (causes & E1000_ICR_RXQ0) { 2133 e1000e_msix_clear_one(core, E1000_ICR_RXQ0, 2134 E1000_IVAR_RXQ0(core->mac[IVAR])); 2135 } 2136 2137 if (causes & E1000_ICR_RXQ1) { 2138 e1000e_msix_clear_one(core, E1000_ICR_RXQ1, 2139 E1000_IVAR_RXQ1(core->mac[IVAR])); 2140 } 2141 2142 if (causes & E1000_ICR_TXQ0) { 2143 e1000e_msix_clear_one(core, E1000_ICR_TXQ0, 2144 E1000_IVAR_TXQ0(core->mac[IVAR])); 2145 } 2146 2147 if (causes & E1000_ICR_TXQ1) { 2148 e1000e_msix_clear_one(core, E1000_ICR_TXQ1, 2149 E1000_IVAR_TXQ1(core->mac[IVAR])); 2150 } 2151 2152 if (causes & E1000_ICR_OTHER) { 2153 e1000e_msix_clear_one(core, E1000_ICR_OTHER, 2154 E1000_IVAR_OTHER(core->mac[IVAR])); 2155 } 2156 } 2157 2158 static inline void 2159 e1000e_fix_icr_asserted(E1000ECore *core) 2160 { 2161 core->mac[ICR] &= ~E1000_ICR_ASSERTED; 2162 if (core->mac[ICR]) { 2163 core->mac[ICR] |= E1000_ICR_ASSERTED; 2164 } 2165 2166 trace_e1000e_irq_fix_icr_asserted(core->mac[ICR]); 2167 } 2168 2169 static void 2170 e1000e_send_msi(E1000ECore *core, bool msix) 2171 { 2172 uint32_t causes = core->mac[ICR] & core->mac[IMS] & ~E1000_ICR_ASSERTED; 2173 2174 core->msi_causes_pending &= causes; 2175 causes ^= core->msi_causes_pending; 2176 if (causes == 0) { 2177 return; 2178 } 2179 core->msi_causes_pending |= causes; 2180 2181 if (msix) { 2182 e1000e_msix_notify(core, causes); 2183 } else { 2184 if (!e1000e_itr_should_postpone(core)) { 2185 trace_e1000e_irq_msi_notify(causes); 2186 msi_notify(core->owner, 0); 2187 } 2188 } 2189 } 2190 2191 static void 2192 e1000e_update_interrupt_state(E1000ECore *core) 2193 { 2194 bool interrupts_pending; 2195 bool is_msix = msix_enabled(core->owner); 2196 2197 /* Set ICR[OTHER] for MSI-X */ 2198 if (is_msix) { 2199 if (core->mac[ICR] & E1000_ICR_OTHER_CAUSES) { 2200 core->mac[ICR] |= E1000_ICR_OTHER; 2201 trace_e1000e_irq_add_msi_other(core->mac[ICR]); 2202 } 2203 } 2204 2205 e1000e_fix_icr_asserted(core); 2206 2207 /* 2208 * Make sure ICR and ICS registers have the same value. 2209 * The spec says that the ICS register is write-only. However in practice, 2210 * on real hardware ICS is readable, and for reads it has the same value as 2211 * ICR (except that ICS does not have the clear on read behaviour of ICR). 2212 * 2213 * The VxWorks PRO/1000 driver uses this behaviour. 2214 */ 2215 core->mac[ICS] = core->mac[ICR]; 2216 2217 interrupts_pending = (core->mac[IMS] & core->mac[ICR]) ? true : false; 2218 if (!interrupts_pending) { 2219 core->msi_causes_pending = 0; 2220 } 2221 2222 trace_e1000e_irq_pending_interrupts(core->mac[ICR] & core->mac[IMS], 2223 core->mac[ICR], core->mac[IMS]); 2224 2225 if (is_msix || msi_enabled(core->owner)) { 2226 if (interrupts_pending) { 2227 e1000e_send_msi(core, is_msix); 2228 } 2229 } else { 2230 if (interrupts_pending) { 2231 if (!e1000e_itr_should_postpone(core)) { 2232 e1000e_raise_legacy_irq(core); 2233 } 2234 } else { 2235 e1000e_lower_legacy_irq(core); 2236 } 2237 } 2238 } 2239 2240 static void 2241 e1000e_set_interrupt_cause(E1000ECore *core, uint32_t val) 2242 { 2243 trace_e1000e_irq_set_cause_entry(val, core->mac[ICR]); 2244 2245 val |= e1000e_intmgr_collect_delayed_causes(core); 2246 core->mac[ICR] |= val; 2247 2248 trace_e1000e_irq_set_cause_exit(val, core->mac[ICR]); 2249 2250 e1000e_update_interrupt_state(core); 2251 } 2252 2253 static inline void 2254 e1000e_autoneg_timer(void *opaque) 2255 { 2256 E1000ECore *core = opaque; 2257 if (!qemu_get_queue(core->owner_nic)->link_down) { 2258 e1000x_update_regs_on_autoneg_done(core->mac, core->phy[0]); 2259 e1000e_start_recv(core); 2260 2261 e1000e_update_flowctl_status(core); 2262 /* signal link status change to the guest */ 2263 e1000e_set_interrupt_cause(core, E1000_ICR_LSC); 2264 } 2265 } 2266 2267 static inline uint16_t 2268 e1000e_get_reg_index_with_offset(const uint16_t *mac_reg_access, hwaddr addr) 2269 { 2270 uint16_t index = (addr & 0x1ffff) >> 2; 2271 return index + (mac_reg_access[index] & 0xfffe); 2272 } 2273 2274 static const char e1000e_phy_regcap[E1000E_PHY_PAGES][0x20] = { 2275 [0] = { 2276 [MII_BMCR] = PHY_ANYPAGE | PHY_RW, 2277 [MII_BMSR] = PHY_ANYPAGE | PHY_R, 2278 [MII_PHYID1] = PHY_ANYPAGE | PHY_R, 2279 [MII_PHYID2] = PHY_ANYPAGE | PHY_R, 2280 [MII_ANAR] = PHY_ANYPAGE | PHY_RW, 2281 [MII_ANLPAR] = PHY_ANYPAGE | PHY_R, 2282 [MII_ANER] = PHY_ANYPAGE | PHY_R, 2283 [MII_ANNP] = PHY_ANYPAGE | PHY_RW, 2284 [MII_ANLPRNP] = PHY_ANYPAGE | PHY_R, 2285 [MII_CTRL1000] = PHY_ANYPAGE | PHY_RW, 2286 [MII_STAT1000] = PHY_ANYPAGE | PHY_R, 2287 [MII_EXTSTAT] = PHY_ANYPAGE | PHY_R, 2288 [PHY_PAGE] = PHY_ANYPAGE | PHY_RW, 2289 2290 [PHY_COPPER_CTRL1] = PHY_RW, 2291 [PHY_COPPER_STAT1] = PHY_R, 2292 [PHY_COPPER_CTRL3] = PHY_RW, 2293 [PHY_RX_ERR_CNTR] = PHY_R, 2294 [PHY_OEM_BITS] = PHY_RW, 2295 [PHY_BIAS_1] = PHY_RW, 2296 [PHY_BIAS_2] = PHY_RW, 2297 [PHY_COPPER_INT_ENABLE] = PHY_RW, 2298 [PHY_COPPER_STAT2] = PHY_R, 2299 [PHY_COPPER_CTRL2] = PHY_RW 2300 }, 2301 [2] = { 2302 [PHY_MAC_CTRL1] = PHY_RW, 2303 [PHY_MAC_INT_ENABLE] = PHY_RW, 2304 [PHY_MAC_STAT] = PHY_R, 2305 [PHY_MAC_CTRL2] = PHY_RW 2306 }, 2307 [3] = { 2308 [PHY_LED_03_FUNC_CTRL1] = PHY_RW, 2309 [PHY_LED_03_POL_CTRL] = PHY_RW, 2310 [PHY_LED_TIMER_CTRL] = PHY_RW, 2311 [PHY_LED_45_CTRL] = PHY_RW 2312 }, 2313 [5] = { 2314 [PHY_1000T_SKEW] = PHY_R, 2315 [PHY_1000T_SWAP] = PHY_R 2316 }, 2317 [6] = { 2318 [PHY_CRC_COUNTERS] = PHY_R 2319 } 2320 }; 2321 2322 static bool 2323 e1000e_phy_reg_check_cap(E1000ECore *core, uint32_t addr, 2324 char cap, uint8_t *page) 2325 { 2326 *page = 2327 (e1000e_phy_regcap[0][addr] & PHY_ANYPAGE) ? 0 2328 : core->phy[0][PHY_PAGE]; 2329 2330 if (*page >= E1000E_PHY_PAGES) { 2331 return false; 2332 } 2333 2334 return e1000e_phy_regcap[*page][addr] & cap; 2335 } 2336 2337 static void 2338 e1000e_phy_reg_write(E1000ECore *core, uint8_t page, 2339 uint32_t addr, uint16_t data) 2340 { 2341 assert(page < E1000E_PHY_PAGES); 2342 assert(addr < E1000E_PHY_PAGE_SIZE); 2343 2344 if (e1000e_phyreg_writeops[page][addr]) { 2345 e1000e_phyreg_writeops[page][addr](core, addr, data); 2346 } else { 2347 core->phy[page][addr] = data; 2348 } 2349 } 2350 2351 static void 2352 e1000e_set_mdic(E1000ECore *core, int index, uint32_t val) 2353 { 2354 uint32_t data = val & E1000_MDIC_DATA_MASK; 2355 uint32_t addr = ((val & E1000_MDIC_REG_MASK) >> E1000_MDIC_REG_SHIFT); 2356 uint8_t page; 2357 2358 if ((val & E1000_MDIC_PHY_MASK) >> E1000_MDIC_PHY_SHIFT != 1) { /* phy # */ 2359 val = core->mac[MDIC] | E1000_MDIC_ERROR; 2360 } else if (val & E1000_MDIC_OP_READ) { 2361 if (!e1000e_phy_reg_check_cap(core, addr, PHY_R, &page)) { 2362 trace_e1000e_core_mdic_read_unhandled(page, addr); 2363 val |= E1000_MDIC_ERROR; 2364 } else { 2365 val = (val ^ data) | core->phy[page][addr]; 2366 trace_e1000e_core_mdic_read(page, addr, val); 2367 } 2368 } else if (val & E1000_MDIC_OP_WRITE) { 2369 if (!e1000e_phy_reg_check_cap(core, addr, PHY_W, &page)) { 2370 trace_e1000e_core_mdic_write_unhandled(page, addr); 2371 val |= E1000_MDIC_ERROR; 2372 } else { 2373 trace_e1000e_core_mdic_write(page, addr, data); 2374 e1000e_phy_reg_write(core, page, addr, data); 2375 } 2376 } 2377 core->mac[MDIC] = val | E1000_MDIC_READY; 2378 2379 if (val & E1000_MDIC_INT_EN) { 2380 e1000e_set_interrupt_cause(core, E1000_ICR_MDAC); 2381 } 2382 } 2383 2384 static void 2385 e1000e_set_rdt(E1000ECore *core, int index, uint32_t val) 2386 { 2387 core->mac[index] = val & 0xffff; 2388 trace_e1000e_rx_set_rdt(e1000e_mq_queue_idx(RDT0, index), val); 2389 e1000e_start_recv(core); 2390 } 2391 2392 static void 2393 e1000e_set_status(E1000ECore *core, int index, uint32_t val) 2394 { 2395 if ((val & E1000_STATUS_PHYRA) == 0) { 2396 core->mac[index] &= ~E1000_STATUS_PHYRA; 2397 } 2398 } 2399 2400 static void 2401 e1000e_set_ctrlext(E1000ECore *core, int index, uint32_t val) 2402 { 2403 trace_e1000e_link_set_ext_params(!!(val & E1000_CTRL_EXT_ASDCHK), 2404 !!(val & E1000_CTRL_EXT_SPD_BYPS)); 2405 2406 /* Zero self-clearing bits */ 2407 val &= ~(E1000_CTRL_EXT_ASDCHK | E1000_CTRL_EXT_EE_RST); 2408 core->mac[CTRL_EXT] = val; 2409 } 2410 2411 static void 2412 e1000e_set_pbaclr(E1000ECore *core, int index, uint32_t val) 2413 { 2414 int i; 2415 2416 core->mac[PBACLR] = val & E1000_PBACLR_VALID_MASK; 2417 2418 if (!msix_enabled(core->owner)) { 2419 return; 2420 } 2421 2422 for (i = 0; i < E1000E_MSIX_VEC_NUM; i++) { 2423 if (core->mac[PBACLR] & BIT(i)) { 2424 msix_clr_pending(core->owner, i); 2425 } 2426 } 2427 } 2428 2429 static void 2430 e1000e_set_fcrth(E1000ECore *core, int index, uint32_t val) 2431 { 2432 core->mac[FCRTH] = val & 0xFFF8; 2433 } 2434 2435 static void 2436 e1000e_set_fcrtl(E1000ECore *core, int index, uint32_t val) 2437 { 2438 core->mac[FCRTL] = val & 0x8000FFF8; 2439 } 2440 2441 #define E1000E_LOW_BITS_SET_FUNC(num) \ 2442 static void \ 2443 e1000e_set_##num##bit(E1000ECore *core, int index, uint32_t val) \ 2444 { \ 2445 core->mac[index] = val & (BIT(num) - 1); \ 2446 } 2447 2448 E1000E_LOW_BITS_SET_FUNC(4) 2449 E1000E_LOW_BITS_SET_FUNC(6) 2450 E1000E_LOW_BITS_SET_FUNC(11) 2451 E1000E_LOW_BITS_SET_FUNC(12) 2452 E1000E_LOW_BITS_SET_FUNC(13) 2453 E1000E_LOW_BITS_SET_FUNC(16) 2454 2455 static void 2456 e1000e_set_vet(E1000ECore *core, int index, uint32_t val) 2457 { 2458 core->mac[VET] = val & 0xffff; 2459 trace_e1000e_vlan_vet(core->mac[VET]); 2460 } 2461 2462 static void 2463 e1000e_set_dlen(E1000ECore *core, int index, uint32_t val) 2464 { 2465 core->mac[index] = val & E1000_XDLEN_MASK; 2466 } 2467 2468 static void 2469 e1000e_set_dbal(E1000ECore *core, int index, uint32_t val) 2470 { 2471 core->mac[index] = val & E1000_XDBAL_MASK; 2472 } 2473 2474 static void 2475 e1000e_set_tctl(E1000ECore *core, int index, uint32_t val) 2476 { 2477 E1000E_TxRing txr; 2478 core->mac[index] = val; 2479 2480 if (core->mac[TARC0] & E1000_TARC_ENABLE) { 2481 e1000e_tx_ring_init(core, &txr, 0); 2482 e1000e_start_xmit(core, &txr); 2483 } 2484 2485 if (core->mac[TARC1] & E1000_TARC_ENABLE) { 2486 e1000e_tx_ring_init(core, &txr, 1); 2487 e1000e_start_xmit(core, &txr); 2488 } 2489 } 2490 2491 static void 2492 e1000e_set_tdt(E1000ECore *core, int index, uint32_t val) 2493 { 2494 E1000E_TxRing txr; 2495 int qidx = e1000e_mq_queue_idx(TDT, index); 2496 uint32_t tarc_reg = (qidx == 0) ? TARC0 : TARC1; 2497 2498 core->mac[index] = val & 0xffff; 2499 2500 if (core->mac[tarc_reg] & E1000_TARC_ENABLE) { 2501 e1000e_tx_ring_init(core, &txr, qidx); 2502 e1000e_start_xmit(core, &txr); 2503 } 2504 } 2505 2506 static void 2507 e1000e_set_ics(E1000ECore *core, int index, uint32_t val) 2508 { 2509 trace_e1000e_irq_write_ics(val); 2510 e1000e_set_interrupt_cause(core, val); 2511 } 2512 2513 static void 2514 e1000e_set_icr(E1000ECore *core, int index, uint32_t val) 2515 { 2516 uint32_t icr = 0; 2517 if ((core->mac[ICR] & E1000_ICR_ASSERTED) && 2518 (core->mac[CTRL_EXT] & E1000_CTRL_EXT_IAME)) { 2519 trace_e1000e_irq_icr_process_iame(); 2520 e1000e_clear_ims_bits(core, core->mac[IAM]); 2521 } 2522 2523 icr = core->mac[ICR] & ~val; 2524 /* 2525 * Windows driver expects that the "receive overrun" bit and other 2526 * ones to be cleared when the "Other" bit (#24) is cleared. 2527 */ 2528 icr = (val & E1000_ICR_OTHER) ? (icr & ~E1000_ICR_OTHER_CAUSES) : icr; 2529 trace_e1000e_irq_icr_write(val, core->mac[ICR], icr); 2530 core->mac[ICR] = icr; 2531 e1000e_update_interrupt_state(core); 2532 } 2533 2534 static void 2535 e1000e_set_imc(E1000ECore *core, int index, uint32_t val) 2536 { 2537 trace_e1000e_irq_ims_clear_set_imc(val); 2538 e1000e_clear_ims_bits(core, val); 2539 e1000e_update_interrupt_state(core); 2540 } 2541 2542 static void 2543 e1000e_set_ims(E1000ECore *core, int index, uint32_t val) 2544 { 2545 static const uint32_t ims_ext_mask = 2546 E1000_IMS_RXQ0 | E1000_IMS_RXQ1 | 2547 E1000_IMS_TXQ0 | E1000_IMS_TXQ1 | 2548 E1000_IMS_OTHER; 2549 2550 static const uint32_t ims_valid_mask = 2551 E1000_IMS_TXDW | E1000_IMS_TXQE | E1000_IMS_LSC | 2552 E1000_IMS_RXDMT0 | E1000_IMS_RXO | E1000_IMS_RXT0 | 2553 E1000_IMS_MDAC | E1000_IMS_TXD_LOW | E1000_IMS_SRPD | 2554 E1000_IMS_ACK | E1000_IMS_MNG | E1000_IMS_RXQ0 | 2555 E1000_IMS_RXQ1 | E1000_IMS_TXQ0 | E1000_IMS_TXQ1 | 2556 E1000_IMS_OTHER; 2557 2558 uint32_t valid_val = val & ims_valid_mask; 2559 2560 trace_e1000e_irq_set_ims(val, core->mac[IMS], core->mac[IMS] | valid_val); 2561 core->mac[IMS] |= valid_val; 2562 2563 if ((valid_val & ims_ext_mask) && 2564 (core->mac[CTRL_EXT] & E1000_CTRL_EXT_PBA_CLR) && 2565 msix_enabled(core->owner)) { 2566 e1000e_msix_clear(core, valid_val); 2567 } 2568 2569 if ((valid_val == ims_valid_mask) && 2570 (core->mac[CTRL_EXT] & E1000_CTRL_EXT_INT_TIMERS_CLEAR_ENA)) { 2571 trace_e1000e_irq_fire_all_timers(val); 2572 e1000e_intrmgr_fire_all_timers(core); 2573 } 2574 2575 e1000e_update_interrupt_state(core); 2576 } 2577 2578 static void 2579 e1000e_set_rdtr(E1000ECore *core, int index, uint32_t val) 2580 { 2581 e1000e_set_16bit(core, index, val); 2582 2583 if ((val & E1000_RDTR_FPD) && (core->rdtr.running)) { 2584 trace_e1000e_irq_rdtr_fpd_running(); 2585 e1000e_intrmgr_fire_delayed_interrupts(core); 2586 } else { 2587 trace_e1000e_irq_rdtr_fpd_not_running(); 2588 } 2589 } 2590 2591 static void 2592 e1000e_set_tidv(E1000ECore *core, int index, uint32_t val) 2593 { 2594 e1000e_set_16bit(core, index, val); 2595 2596 if ((val & E1000_TIDV_FPD) && (core->tidv.running)) { 2597 trace_e1000e_irq_tidv_fpd_running(); 2598 e1000e_intrmgr_fire_delayed_interrupts(core); 2599 } else { 2600 trace_e1000e_irq_tidv_fpd_not_running(); 2601 } 2602 } 2603 2604 static uint32_t 2605 e1000e_mac_readreg(E1000ECore *core, int index) 2606 { 2607 return core->mac[index]; 2608 } 2609 2610 static uint32_t 2611 e1000e_mac_ics_read(E1000ECore *core, int index) 2612 { 2613 trace_e1000e_irq_read_ics(core->mac[ICS]); 2614 return core->mac[ICS]; 2615 } 2616 2617 static uint32_t 2618 e1000e_mac_ims_read(E1000ECore *core, int index) 2619 { 2620 trace_e1000e_irq_read_ims(core->mac[IMS]); 2621 return core->mac[IMS]; 2622 } 2623 2624 static uint32_t 2625 e1000e_mac_swsm_read(E1000ECore *core, int index) 2626 { 2627 uint32_t val = core->mac[SWSM]; 2628 core->mac[SWSM] = val | E1000_SWSM_SMBI; 2629 return val; 2630 } 2631 2632 static uint32_t 2633 e1000e_mac_itr_read(E1000ECore *core, int index) 2634 { 2635 return core->itr_guest_value; 2636 } 2637 2638 static uint32_t 2639 e1000e_mac_eitr_read(E1000ECore *core, int index) 2640 { 2641 return core->eitr_guest_value[index - EITR]; 2642 } 2643 2644 static uint32_t 2645 e1000e_mac_icr_read(E1000ECore *core, int index) 2646 { 2647 uint32_t ret = core->mac[ICR]; 2648 trace_e1000e_irq_icr_read_entry(ret); 2649 2650 if (core->mac[IMS] == 0) { 2651 trace_e1000e_irq_icr_clear_zero_ims(); 2652 core->mac[ICR] = 0; 2653 } 2654 2655 if (!msix_enabled(core->owner)) { 2656 trace_e1000e_irq_icr_clear_nonmsix_icr_read(); 2657 core->mac[ICR] = 0; 2658 } 2659 2660 if ((core->mac[ICR] & E1000_ICR_ASSERTED) && 2661 (core->mac[CTRL_EXT] & E1000_CTRL_EXT_IAME)) { 2662 trace_e1000e_irq_icr_clear_iame(); 2663 core->mac[ICR] = 0; 2664 trace_e1000e_irq_icr_process_iame(); 2665 e1000e_clear_ims_bits(core, core->mac[IAM]); 2666 } 2667 2668 trace_e1000e_irq_icr_read_exit(core->mac[ICR]); 2669 e1000e_update_interrupt_state(core); 2670 return ret; 2671 } 2672 2673 static uint32_t 2674 e1000e_mac_read_clr4(E1000ECore *core, int index) 2675 { 2676 uint32_t ret = core->mac[index]; 2677 2678 core->mac[index] = 0; 2679 return ret; 2680 } 2681 2682 static uint32_t 2683 e1000e_mac_read_clr8(E1000ECore *core, int index) 2684 { 2685 uint32_t ret = core->mac[index]; 2686 2687 core->mac[index] = 0; 2688 core->mac[index - 1] = 0; 2689 return ret; 2690 } 2691 2692 static uint32_t 2693 e1000e_get_ctrl(E1000ECore *core, int index) 2694 { 2695 uint32_t val = core->mac[CTRL]; 2696 2697 trace_e1000e_link_read_params( 2698 !!(val & E1000_CTRL_ASDE), 2699 (val & E1000_CTRL_SPD_SEL) >> E1000_CTRL_SPD_SHIFT, 2700 !!(val & E1000_CTRL_FRCSPD), 2701 !!(val & E1000_CTRL_FRCDPX), 2702 !!(val & E1000_CTRL_RFCE), 2703 !!(val & E1000_CTRL_TFCE)); 2704 2705 return val; 2706 } 2707 2708 static uint32_t 2709 e1000e_get_status(E1000ECore *core, int index) 2710 { 2711 uint32_t res = core->mac[STATUS]; 2712 2713 if (!(core->mac[CTRL] & E1000_CTRL_GIO_MASTER_DISABLE)) { 2714 res |= E1000_STATUS_GIO_MASTER_ENABLE; 2715 } 2716 2717 if (core->mac[CTRL] & E1000_CTRL_FRCDPX) { 2718 res |= (core->mac[CTRL] & E1000_CTRL_FD) ? E1000_STATUS_FD : 0; 2719 } else { 2720 res |= E1000_STATUS_FD; 2721 } 2722 2723 if ((core->mac[CTRL] & E1000_CTRL_FRCSPD) || 2724 (core->mac[CTRL_EXT] & E1000_CTRL_EXT_SPD_BYPS)) { 2725 switch (core->mac[CTRL] & E1000_CTRL_SPD_SEL) { 2726 case E1000_CTRL_SPD_10: 2727 res |= E1000_STATUS_SPEED_10; 2728 break; 2729 case E1000_CTRL_SPD_100: 2730 res |= E1000_STATUS_SPEED_100; 2731 break; 2732 case E1000_CTRL_SPD_1000: 2733 default: 2734 res |= E1000_STATUS_SPEED_1000; 2735 break; 2736 } 2737 } else { 2738 res |= E1000_STATUS_SPEED_1000; 2739 } 2740 2741 trace_e1000e_link_status( 2742 !!(res & E1000_STATUS_LU), 2743 !!(res & E1000_STATUS_FD), 2744 (res & E1000_STATUS_SPEED_MASK) >> E1000_STATUS_SPEED_SHIFT, 2745 (res & E1000_STATUS_ASDV) >> E1000_STATUS_ASDV_SHIFT); 2746 2747 return res; 2748 } 2749 2750 static uint32_t 2751 e1000e_get_tarc(E1000ECore *core, int index) 2752 { 2753 return core->mac[index] & ((BIT(11) - 1) | 2754 BIT(27) | 2755 BIT(28) | 2756 BIT(29) | 2757 BIT(30)); 2758 } 2759 2760 static void 2761 e1000e_mac_writereg(E1000ECore *core, int index, uint32_t val) 2762 { 2763 core->mac[index] = val; 2764 } 2765 2766 static void 2767 e1000e_mac_setmacaddr(E1000ECore *core, int index, uint32_t val) 2768 { 2769 uint32_t macaddr[2]; 2770 2771 core->mac[index] = val; 2772 2773 macaddr[0] = cpu_to_le32(core->mac[RA]); 2774 macaddr[1] = cpu_to_le32(core->mac[RA + 1]); 2775 qemu_format_nic_info_str(qemu_get_queue(core->owner_nic), 2776 (uint8_t *) macaddr); 2777 2778 trace_e1000e_mac_set_sw(MAC_ARG(macaddr)); 2779 } 2780 2781 static void 2782 e1000e_set_eecd(E1000ECore *core, int index, uint32_t val) 2783 { 2784 static const uint32_t ro_bits = E1000_EECD_PRES | 2785 E1000_EECD_AUTO_RD | 2786 E1000_EECD_SIZE_EX_MASK; 2787 2788 core->mac[EECD] = (core->mac[EECD] & ro_bits) | (val & ~ro_bits); 2789 } 2790 2791 static void 2792 e1000e_set_eerd(E1000ECore *core, int index, uint32_t val) 2793 { 2794 uint32_t addr = (val >> E1000_EERW_ADDR_SHIFT) & E1000_EERW_ADDR_MASK; 2795 uint32_t flags = 0; 2796 uint32_t data = 0; 2797 2798 if ((addr < E1000E_EEPROM_SIZE) && (val & E1000_EERW_START)) { 2799 data = core->eeprom[addr]; 2800 flags = E1000_EERW_DONE; 2801 } 2802 2803 core->mac[EERD] = flags | 2804 (addr << E1000_EERW_ADDR_SHIFT) | 2805 (data << E1000_EERW_DATA_SHIFT); 2806 } 2807 2808 static void 2809 e1000e_set_eewr(E1000ECore *core, int index, uint32_t val) 2810 { 2811 uint32_t addr = (val >> E1000_EERW_ADDR_SHIFT) & E1000_EERW_ADDR_MASK; 2812 uint32_t data = (val >> E1000_EERW_DATA_SHIFT) & E1000_EERW_DATA_MASK; 2813 uint32_t flags = 0; 2814 2815 if ((addr < E1000E_EEPROM_SIZE) && (val & E1000_EERW_START)) { 2816 core->eeprom[addr] = data; 2817 flags = E1000_EERW_DONE; 2818 } 2819 2820 core->mac[EERD] = flags | 2821 (addr << E1000_EERW_ADDR_SHIFT) | 2822 (data << E1000_EERW_DATA_SHIFT); 2823 } 2824 2825 static void 2826 e1000e_set_rxdctl(E1000ECore *core, int index, uint32_t val) 2827 { 2828 core->mac[RXDCTL] = core->mac[RXDCTL1] = val; 2829 } 2830 2831 static void 2832 e1000e_set_itr(E1000ECore *core, int index, uint32_t val) 2833 { 2834 uint32_t interval = val & 0xffff; 2835 2836 trace_e1000e_irq_itr_set(val); 2837 2838 core->itr_guest_value = interval; 2839 core->mac[index] = MAX(interval, E1000E_MIN_XITR); 2840 } 2841 2842 static void 2843 e1000e_set_eitr(E1000ECore *core, int index, uint32_t val) 2844 { 2845 uint32_t interval = val & 0xffff; 2846 uint32_t eitr_num = index - EITR; 2847 2848 trace_e1000e_irq_eitr_set(eitr_num, val); 2849 2850 core->eitr_guest_value[eitr_num] = interval; 2851 core->mac[index] = MAX(interval, E1000E_MIN_XITR); 2852 } 2853 2854 static void 2855 e1000e_set_psrctl(E1000ECore *core, int index, uint32_t val) 2856 { 2857 if (core->mac[RCTL] & E1000_RCTL_DTYP_MASK) { 2858 2859 if ((val & E1000_PSRCTL_BSIZE0_MASK) == 0) { 2860 qemu_log_mask(LOG_GUEST_ERROR, 2861 "e1000e: PSRCTL.BSIZE0 cannot be zero"); 2862 return; 2863 } 2864 2865 if ((val & E1000_PSRCTL_BSIZE1_MASK) == 0) { 2866 qemu_log_mask(LOG_GUEST_ERROR, 2867 "e1000e: PSRCTL.BSIZE1 cannot be zero"); 2868 return; 2869 } 2870 } 2871 2872 core->mac[PSRCTL] = val; 2873 } 2874 2875 static void 2876 e1000e_update_rx_offloads(E1000ECore *core) 2877 { 2878 int cso_state = e1000e_rx_l4_cso_enabled(core); 2879 2880 trace_e1000e_rx_set_cso(cso_state); 2881 2882 if (core->has_vnet) { 2883 qemu_set_offload(qemu_get_queue(core->owner_nic)->peer, 2884 cso_state, 0, 0, 0, 0); 2885 } 2886 } 2887 2888 static void 2889 e1000e_set_rxcsum(E1000ECore *core, int index, uint32_t val) 2890 { 2891 core->mac[RXCSUM] = val; 2892 e1000e_update_rx_offloads(core); 2893 } 2894 2895 static void 2896 e1000e_set_gcr(E1000ECore *core, int index, uint32_t val) 2897 { 2898 uint32_t ro_bits = core->mac[GCR] & E1000_GCR_RO_BITS; 2899 core->mac[GCR] = (val & ~E1000_GCR_RO_BITS) | ro_bits; 2900 } 2901 2902 static uint32_t e1000e_get_systiml(E1000ECore *core, int index) 2903 { 2904 e1000x_timestamp(core->mac, core->timadj, SYSTIML, SYSTIMH); 2905 return core->mac[SYSTIML]; 2906 } 2907 2908 static uint32_t e1000e_get_rxsatrh(E1000ECore *core, int index) 2909 { 2910 core->mac[TSYNCRXCTL] &= ~E1000_TSYNCRXCTL_VALID; 2911 return core->mac[RXSATRH]; 2912 } 2913 2914 static uint32_t e1000e_get_txstmph(E1000ECore *core, int index) 2915 { 2916 core->mac[TSYNCTXCTL] &= ~E1000_TSYNCTXCTL_VALID; 2917 return core->mac[TXSTMPH]; 2918 } 2919 2920 static void e1000e_set_timinca(E1000ECore *core, int index, uint32_t val) 2921 { 2922 e1000x_set_timinca(core->mac, &core->timadj, val); 2923 } 2924 2925 static void e1000e_set_timadjh(E1000ECore *core, int index, uint32_t val) 2926 { 2927 core->mac[TIMADJH] = val; 2928 core->timadj += core->mac[TIMADJL] | ((int64_t)core->mac[TIMADJH] << 32); 2929 } 2930 2931 #define e1000e_getreg(x) [x] = e1000e_mac_readreg 2932 typedef uint32_t (*readops)(E1000ECore *, int); 2933 static const readops e1000e_macreg_readops[] = { 2934 e1000e_getreg(PBA), 2935 e1000e_getreg(WUFC), 2936 e1000e_getreg(MANC), 2937 e1000e_getreg(TOTL), 2938 e1000e_getreg(RDT0), 2939 e1000e_getreg(RDBAH0), 2940 e1000e_getreg(TDBAL1), 2941 e1000e_getreg(RDLEN0), 2942 e1000e_getreg(RDH1), 2943 e1000e_getreg(LATECOL), 2944 e1000e_getreg(SEQEC), 2945 e1000e_getreg(XONTXC), 2946 e1000e_getreg(AIT), 2947 e1000e_getreg(TDFH), 2948 e1000e_getreg(TDFT), 2949 e1000e_getreg(TDFHS), 2950 e1000e_getreg(TDFTS), 2951 e1000e_getreg(TDFPC), 2952 e1000e_getreg(WUS), 2953 e1000e_getreg(PBS), 2954 e1000e_getreg(RDFH), 2955 e1000e_getreg(RDFT), 2956 e1000e_getreg(RDFHS), 2957 e1000e_getreg(RDFTS), 2958 e1000e_getreg(RDFPC), 2959 e1000e_getreg(GORCL), 2960 e1000e_getreg(MGTPRC), 2961 e1000e_getreg(EERD), 2962 e1000e_getreg(EIAC), 2963 e1000e_getreg(PSRCTL), 2964 e1000e_getreg(MANC2H), 2965 e1000e_getreg(RXCSUM), 2966 e1000e_getreg(GSCL_3), 2967 e1000e_getreg(GSCN_2), 2968 e1000e_getreg(RSRPD), 2969 e1000e_getreg(RDBAL1), 2970 e1000e_getreg(FCAH), 2971 e1000e_getreg(FCRTH), 2972 e1000e_getreg(FLOP), 2973 e1000e_getreg(FLASHT), 2974 e1000e_getreg(RXSTMPH), 2975 e1000e_getreg(TXSTMPL), 2976 e1000e_getreg(TIMADJL), 2977 e1000e_getreg(TXDCTL), 2978 e1000e_getreg(RDH0), 2979 e1000e_getreg(TDT1), 2980 e1000e_getreg(TNCRS), 2981 e1000e_getreg(RJC), 2982 e1000e_getreg(IAM), 2983 e1000e_getreg(GSCL_2), 2984 e1000e_getreg(RDBAH1), 2985 e1000e_getreg(FLSWDATA), 2986 e1000e_getreg(TIPG), 2987 e1000e_getreg(FLMNGCTL), 2988 e1000e_getreg(FLMNGCNT), 2989 e1000e_getreg(TSYNCTXCTL), 2990 e1000e_getreg(EXTCNF_SIZE), 2991 e1000e_getreg(EXTCNF_CTRL), 2992 e1000e_getreg(EEMNGDATA), 2993 e1000e_getreg(CTRL_EXT), 2994 e1000e_getreg(SYSTIMH), 2995 e1000e_getreg(EEMNGCTL), 2996 e1000e_getreg(FLMNGDATA), 2997 e1000e_getreg(TSYNCRXCTL), 2998 e1000e_getreg(TDH), 2999 e1000e_getreg(LEDCTL), 3000 e1000e_getreg(TCTL), 3001 e1000e_getreg(TDBAL), 3002 e1000e_getreg(TDLEN), 3003 e1000e_getreg(TDH1), 3004 e1000e_getreg(RADV), 3005 e1000e_getreg(ECOL), 3006 e1000e_getreg(DC), 3007 e1000e_getreg(RLEC), 3008 e1000e_getreg(XOFFTXC), 3009 e1000e_getreg(RFC), 3010 e1000e_getreg(RNBC), 3011 e1000e_getreg(MGTPTC), 3012 e1000e_getreg(TIMINCA), 3013 e1000e_getreg(RXCFGL), 3014 e1000e_getreg(MFUTP01), 3015 e1000e_getreg(FACTPS), 3016 e1000e_getreg(GSCL_1), 3017 e1000e_getreg(GSCN_0), 3018 e1000e_getreg(GCR2), 3019 e1000e_getreg(RDT1), 3020 e1000e_getreg(PBACLR), 3021 e1000e_getreg(FCTTV), 3022 e1000e_getreg(EEWR), 3023 e1000e_getreg(FLSWCTL), 3024 e1000e_getreg(RXDCTL1), 3025 e1000e_getreg(RXSATRL), 3026 e1000e_getreg(RXUDP), 3027 e1000e_getreg(TORL), 3028 e1000e_getreg(TDLEN1), 3029 e1000e_getreg(MCC), 3030 e1000e_getreg(WUC), 3031 e1000e_getreg(EECD), 3032 e1000e_getreg(MFUTP23), 3033 e1000e_getreg(RAID), 3034 e1000e_getreg(FCRTV), 3035 e1000e_getreg(TXDCTL1), 3036 e1000e_getreg(RCTL), 3037 e1000e_getreg(TDT), 3038 e1000e_getreg(MDIC), 3039 e1000e_getreg(FCRUC), 3040 e1000e_getreg(VET), 3041 e1000e_getreg(RDBAL0), 3042 e1000e_getreg(TDBAH1), 3043 e1000e_getreg(RDTR), 3044 e1000e_getreg(SCC), 3045 e1000e_getreg(COLC), 3046 e1000e_getreg(CEXTERR), 3047 e1000e_getreg(XOFFRXC), 3048 e1000e_getreg(IPAV), 3049 e1000e_getreg(GOTCL), 3050 e1000e_getreg(MGTPDC), 3051 e1000e_getreg(GCR), 3052 e1000e_getreg(IVAR), 3053 e1000e_getreg(POEMB), 3054 e1000e_getreg(MFVAL), 3055 e1000e_getreg(FUNCTAG), 3056 e1000e_getreg(GSCL_4), 3057 e1000e_getreg(GSCN_3), 3058 e1000e_getreg(MRQC), 3059 e1000e_getreg(RDLEN1), 3060 e1000e_getreg(FCT), 3061 e1000e_getreg(FLA), 3062 e1000e_getreg(FLOL), 3063 e1000e_getreg(RXDCTL), 3064 e1000e_getreg(RXSTMPL), 3065 e1000e_getreg(TIMADJH), 3066 e1000e_getreg(FCRTL), 3067 e1000e_getreg(TDBAH), 3068 e1000e_getreg(TADV), 3069 e1000e_getreg(XONRXC), 3070 e1000e_getreg(TSCTFC), 3071 e1000e_getreg(RFCTL), 3072 e1000e_getreg(GSCN_1), 3073 e1000e_getreg(FCAL), 3074 e1000e_getreg(FLSWCNT), 3075 3076 [TOTH] = e1000e_mac_read_clr8, 3077 [GOTCH] = e1000e_mac_read_clr8, 3078 [PRC64] = e1000e_mac_read_clr4, 3079 [PRC255] = e1000e_mac_read_clr4, 3080 [PRC1023] = e1000e_mac_read_clr4, 3081 [PTC64] = e1000e_mac_read_clr4, 3082 [PTC255] = e1000e_mac_read_clr4, 3083 [PTC1023] = e1000e_mac_read_clr4, 3084 [GPRC] = e1000e_mac_read_clr4, 3085 [TPT] = e1000e_mac_read_clr4, 3086 [RUC] = e1000e_mac_read_clr4, 3087 [BPRC] = e1000e_mac_read_clr4, 3088 [MPTC] = e1000e_mac_read_clr4, 3089 [IAC] = e1000e_mac_read_clr4, 3090 [ICR] = e1000e_mac_icr_read, 3091 [STATUS] = e1000e_get_status, 3092 [TARC0] = e1000e_get_tarc, 3093 [ICS] = e1000e_mac_ics_read, 3094 [TORH] = e1000e_mac_read_clr8, 3095 [GORCH] = e1000e_mac_read_clr8, 3096 [PRC127] = e1000e_mac_read_clr4, 3097 [PRC511] = e1000e_mac_read_clr4, 3098 [PRC1522] = e1000e_mac_read_clr4, 3099 [PTC127] = e1000e_mac_read_clr4, 3100 [PTC511] = e1000e_mac_read_clr4, 3101 [PTC1522] = e1000e_mac_read_clr4, 3102 [GPTC] = e1000e_mac_read_clr4, 3103 [TPR] = e1000e_mac_read_clr4, 3104 [ROC] = e1000e_mac_read_clr4, 3105 [MPRC] = e1000e_mac_read_clr4, 3106 [BPTC] = e1000e_mac_read_clr4, 3107 [TSCTC] = e1000e_mac_read_clr4, 3108 [ITR] = e1000e_mac_itr_read, 3109 [CTRL] = e1000e_get_ctrl, 3110 [TARC1] = e1000e_get_tarc, 3111 [SWSM] = e1000e_mac_swsm_read, 3112 [IMS] = e1000e_mac_ims_read, 3113 [SYSTIML] = e1000e_get_systiml, 3114 [RXSATRH] = e1000e_get_rxsatrh, 3115 [TXSTMPH] = e1000e_get_txstmph, 3116 3117 [CRCERRS ... MPC] = e1000e_mac_readreg, 3118 [IP6AT ... IP6AT + 3] = e1000e_mac_readreg, 3119 [IP4AT ... IP4AT + 6] = e1000e_mac_readreg, 3120 [RA ... RA + 31] = e1000e_mac_readreg, 3121 [WUPM ... WUPM + 31] = e1000e_mac_readreg, 3122 [MTA ... MTA + E1000_MC_TBL_SIZE - 1] = e1000e_mac_readreg, 3123 [VFTA ... VFTA + E1000_VLAN_FILTER_TBL_SIZE - 1] = e1000e_mac_readreg, 3124 [FFMT ... FFMT + 254] = e1000e_mac_readreg, 3125 [FFVT ... FFVT + 254] = e1000e_mac_readreg, 3126 [MDEF ... MDEF + 7] = e1000e_mac_readreg, 3127 [FFLT ... FFLT + 10] = e1000e_mac_readreg, 3128 [FTFT ... FTFT + 254] = e1000e_mac_readreg, 3129 [PBM ... PBM + 10239] = e1000e_mac_readreg, 3130 [RETA ... RETA + 31] = e1000e_mac_readreg, 3131 [RSSRK ... RSSRK + 31] = e1000e_mac_readreg, 3132 [MAVTV0 ... MAVTV3] = e1000e_mac_readreg, 3133 [EITR...EITR + E1000E_MSIX_VEC_NUM - 1] = e1000e_mac_eitr_read 3134 }; 3135 enum { E1000E_NREADOPS = ARRAY_SIZE(e1000e_macreg_readops) }; 3136 3137 #define e1000e_putreg(x) [x] = e1000e_mac_writereg 3138 typedef void (*writeops)(E1000ECore *, int, uint32_t); 3139 static const writeops e1000e_macreg_writeops[] = { 3140 e1000e_putreg(PBA), 3141 e1000e_putreg(SWSM), 3142 e1000e_putreg(WUFC), 3143 e1000e_putreg(RDBAH1), 3144 e1000e_putreg(TDBAH), 3145 e1000e_putreg(TXDCTL), 3146 e1000e_putreg(RDBAH0), 3147 e1000e_putreg(LEDCTL), 3148 e1000e_putreg(FCAL), 3149 e1000e_putreg(FCRUC), 3150 e1000e_putreg(WUC), 3151 e1000e_putreg(WUS), 3152 e1000e_putreg(IPAV), 3153 e1000e_putreg(TDBAH1), 3154 e1000e_putreg(IAM), 3155 e1000e_putreg(EIAC), 3156 e1000e_putreg(IVAR), 3157 e1000e_putreg(TARC0), 3158 e1000e_putreg(TARC1), 3159 e1000e_putreg(FLSWDATA), 3160 e1000e_putreg(POEMB), 3161 e1000e_putreg(MFUTP01), 3162 e1000e_putreg(MFUTP23), 3163 e1000e_putreg(MANC), 3164 e1000e_putreg(MANC2H), 3165 e1000e_putreg(MFVAL), 3166 e1000e_putreg(EXTCNF_CTRL), 3167 e1000e_putreg(FACTPS), 3168 e1000e_putreg(FUNCTAG), 3169 e1000e_putreg(GSCL_1), 3170 e1000e_putreg(GSCL_2), 3171 e1000e_putreg(GSCL_3), 3172 e1000e_putreg(GSCL_4), 3173 e1000e_putreg(GSCN_0), 3174 e1000e_putreg(GSCN_1), 3175 e1000e_putreg(GSCN_2), 3176 e1000e_putreg(GSCN_3), 3177 e1000e_putreg(GCR2), 3178 e1000e_putreg(MRQC), 3179 e1000e_putreg(FLOP), 3180 e1000e_putreg(FLOL), 3181 e1000e_putreg(FLSWCTL), 3182 e1000e_putreg(FLSWCNT), 3183 e1000e_putreg(FLA), 3184 e1000e_putreg(RXDCTL1), 3185 e1000e_putreg(TXDCTL1), 3186 e1000e_putreg(TIPG), 3187 e1000e_putreg(RXSTMPH), 3188 e1000e_putreg(RXSTMPL), 3189 e1000e_putreg(RXSATRL), 3190 e1000e_putreg(RXSATRH), 3191 e1000e_putreg(TXSTMPL), 3192 e1000e_putreg(TXSTMPH), 3193 e1000e_putreg(SYSTIML), 3194 e1000e_putreg(SYSTIMH), 3195 e1000e_putreg(TIMADJL), 3196 e1000e_putreg(RXUDP), 3197 e1000e_putreg(RXCFGL), 3198 e1000e_putreg(TSYNCRXCTL), 3199 e1000e_putreg(TSYNCTXCTL), 3200 e1000e_putreg(EXTCNF_SIZE), 3201 e1000e_putreg(EEMNGCTL), 3202 e1000e_putreg(RA), 3203 3204 [TDH1] = e1000e_set_16bit, 3205 [TDT1] = e1000e_set_tdt, 3206 [TCTL] = e1000e_set_tctl, 3207 [TDT] = e1000e_set_tdt, 3208 [MDIC] = e1000e_set_mdic, 3209 [ICS] = e1000e_set_ics, 3210 [TDH] = e1000e_set_16bit, 3211 [RDH0] = e1000e_set_16bit, 3212 [RDT0] = e1000e_set_rdt, 3213 [IMC] = e1000e_set_imc, 3214 [IMS] = e1000e_set_ims, 3215 [ICR] = e1000e_set_icr, 3216 [EECD] = e1000e_set_eecd, 3217 [RCTL] = e1000e_set_rx_control, 3218 [CTRL] = e1000e_set_ctrl, 3219 [RDTR] = e1000e_set_rdtr, 3220 [RADV] = e1000e_set_16bit, 3221 [TADV] = e1000e_set_16bit, 3222 [ITR] = e1000e_set_itr, 3223 [EERD] = e1000e_set_eerd, 3224 [AIT] = e1000e_set_16bit, 3225 [TDFH] = e1000e_set_13bit, 3226 [TDFT] = e1000e_set_13bit, 3227 [TDFHS] = e1000e_set_13bit, 3228 [TDFTS] = e1000e_set_13bit, 3229 [TDFPC] = e1000e_set_13bit, 3230 [RDFH] = e1000e_set_13bit, 3231 [RDFHS] = e1000e_set_13bit, 3232 [RDFT] = e1000e_set_13bit, 3233 [RDFTS] = e1000e_set_13bit, 3234 [RDFPC] = e1000e_set_13bit, 3235 [PBS] = e1000e_set_6bit, 3236 [GCR] = e1000e_set_gcr, 3237 [PSRCTL] = e1000e_set_psrctl, 3238 [RXCSUM] = e1000e_set_rxcsum, 3239 [RAID] = e1000e_set_16bit, 3240 [RSRPD] = e1000e_set_12bit, 3241 [TIDV] = e1000e_set_tidv, 3242 [TDLEN1] = e1000e_set_dlen, 3243 [TDLEN] = e1000e_set_dlen, 3244 [RDLEN0] = e1000e_set_dlen, 3245 [RDLEN1] = e1000e_set_dlen, 3246 [TDBAL] = e1000e_set_dbal, 3247 [TDBAL1] = e1000e_set_dbal, 3248 [RDBAL0] = e1000e_set_dbal, 3249 [RDBAL1] = e1000e_set_dbal, 3250 [RDH1] = e1000e_set_16bit, 3251 [RDT1] = e1000e_set_rdt, 3252 [STATUS] = e1000e_set_status, 3253 [PBACLR] = e1000e_set_pbaclr, 3254 [CTRL_EXT] = e1000e_set_ctrlext, 3255 [FCAH] = e1000e_set_16bit, 3256 [FCT] = e1000e_set_16bit, 3257 [FCTTV] = e1000e_set_16bit, 3258 [FCRTV] = e1000e_set_16bit, 3259 [FCRTH] = e1000e_set_fcrth, 3260 [FCRTL] = e1000e_set_fcrtl, 3261 [VET] = e1000e_set_vet, 3262 [RXDCTL] = e1000e_set_rxdctl, 3263 [FLASHT] = e1000e_set_16bit, 3264 [EEWR] = e1000e_set_eewr, 3265 [CTRL_DUP] = e1000e_set_ctrl, 3266 [RFCTL] = e1000e_set_rfctl, 3267 [RA + 1] = e1000e_mac_setmacaddr, 3268 [TIMINCA] = e1000e_set_timinca, 3269 [TIMADJH] = e1000e_set_timadjh, 3270 3271 [IP6AT ... IP6AT + 3] = e1000e_mac_writereg, 3272 [IP4AT ... IP4AT + 6] = e1000e_mac_writereg, 3273 [RA + 2 ... RA + 31] = e1000e_mac_writereg, 3274 [WUPM ... WUPM + 31] = e1000e_mac_writereg, 3275 [MTA ... MTA + E1000_MC_TBL_SIZE - 1] = e1000e_mac_writereg, 3276 [VFTA ... VFTA + E1000_VLAN_FILTER_TBL_SIZE - 1] = e1000e_mac_writereg, 3277 [FFMT ... FFMT + 254] = e1000e_set_4bit, 3278 [FFVT ... FFVT + 254] = e1000e_mac_writereg, 3279 [PBM ... PBM + 10239] = e1000e_mac_writereg, 3280 [MDEF ... MDEF + 7] = e1000e_mac_writereg, 3281 [FFLT ... FFLT + 10] = e1000e_set_11bit, 3282 [FTFT ... FTFT + 254] = e1000e_mac_writereg, 3283 [RETA ... RETA + 31] = e1000e_mac_writereg, 3284 [RSSRK ... RSSRK + 31] = e1000e_mac_writereg, 3285 [MAVTV0 ... MAVTV3] = e1000e_mac_writereg, 3286 [EITR...EITR + E1000E_MSIX_VEC_NUM - 1] = e1000e_set_eitr 3287 }; 3288 enum { E1000E_NWRITEOPS = ARRAY_SIZE(e1000e_macreg_writeops) }; 3289 3290 enum { MAC_ACCESS_PARTIAL = 1 }; 3291 3292 /* 3293 * The array below combines alias offsets of the index values for the 3294 * MAC registers that have aliases, with the indication of not fully 3295 * implemented registers (lowest bit). This combination is possible 3296 * because all of the offsets are even. 3297 */ 3298 static const uint16_t mac_reg_access[E1000E_MAC_SIZE] = { 3299 /* Alias index offsets */ 3300 [FCRTL_A] = 0x07fe, [FCRTH_A] = 0x0802, 3301 [RDH0_A] = 0x09bc, [RDT0_A] = 0x09bc, [RDTR_A] = 0x09c6, 3302 [RDFH_A] = 0xe904, [RDFT_A] = 0xe904, 3303 [TDH_A] = 0x0cf8, [TDT_A] = 0x0cf8, [TIDV_A] = 0x0cf8, 3304 [TDFH_A] = 0xed00, [TDFT_A] = 0xed00, 3305 [RA_A ... RA_A + 31] = 0x14f0, 3306 [VFTA_A ... VFTA_A + E1000_VLAN_FILTER_TBL_SIZE - 1] = 0x1400, 3307 [RDBAL0_A ... RDLEN0_A] = 0x09bc, 3308 [TDBAL_A ... TDLEN_A] = 0x0cf8, 3309 /* Access options */ 3310 [RDFH] = MAC_ACCESS_PARTIAL, [RDFT] = MAC_ACCESS_PARTIAL, 3311 [RDFHS] = MAC_ACCESS_PARTIAL, [RDFTS] = MAC_ACCESS_PARTIAL, 3312 [RDFPC] = MAC_ACCESS_PARTIAL, 3313 [TDFH] = MAC_ACCESS_PARTIAL, [TDFT] = MAC_ACCESS_PARTIAL, 3314 [TDFHS] = MAC_ACCESS_PARTIAL, [TDFTS] = MAC_ACCESS_PARTIAL, 3315 [TDFPC] = MAC_ACCESS_PARTIAL, [EECD] = MAC_ACCESS_PARTIAL, 3316 [PBM] = MAC_ACCESS_PARTIAL, [FLA] = MAC_ACCESS_PARTIAL, 3317 [FCAL] = MAC_ACCESS_PARTIAL, [FCAH] = MAC_ACCESS_PARTIAL, 3318 [FCT] = MAC_ACCESS_PARTIAL, [FCTTV] = MAC_ACCESS_PARTIAL, 3319 [FCRTV] = MAC_ACCESS_PARTIAL, [FCRTL] = MAC_ACCESS_PARTIAL, 3320 [FCRTH] = MAC_ACCESS_PARTIAL, [TXDCTL] = MAC_ACCESS_PARTIAL, 3321 [TXDCTL1] = MAC_ACCESS_PARTIAL, 3322 [MAVTV0 ... MAVTV3] = MAC_ACCESS_PARTIAL 3323 }; 3324 3325 void 3326 e1000e_core_write(E1000ECore *core, hwaddr addr, uint64_t val, unsigned size) 3327 { 3328 uint16_t index = e1000e_get_reg_index_with_offset(mac_reg_access, addr); 3329 3330 if (index < E1000E_NWRITEOPS && e1000e_macreg_writeops[index]) { 3331 if (mac_reg_access[index] & MAC_ACCESS_PARTIAL) { 3332 trace_e1000e_wrn_regs_write_trivial(index << 2); 3333 } 3334 trace_e1000e_core_write(index << 2, size, val); 3335 e1000e_macreg_writeops[index](core, index, val); 3336 } else if (index < E1000E_NREADOPS && e1000e_macreg_readops[index]) { 3337 trace_e1000e_wrn_regs_write_ro(index << 2, size, val); 3338 } else { 3339 trace_e1000e_wrn_regs_write_unknown(index << 2, size, val); 3340 } 3341 } 3342 3343 uint64_t 3344 e1000e_core_read(E1000ECore *core, hwaddr addr, unsigned size) 3345 { 3346 uint64_t val; 3347 uint16_t index = e1000e_get_reg_index_with_offset(mac_reg_access, addr); 3348 3349 if (index < E1000E_NREADOPS && e1000e_macreg_readops[index]) { 3350 if (mac_reg_access[index] & MAC_ACCESS_PARTIAL) { 3351 trace_e1000e_wrn_regs_read_trivial(index << 2); 3352 } 3353 val = e1000e_macreg_readops[index](core, index); 3354 trace_e1000e_core_read(index << 2, size, val); 3355 return val; 3356 } else { 3357 trace_e1000e_wrn_regs_read_unknown(index << 2, size); 3358 } 3359 return 0; 3360 } 3361 3362 static inline void 3363 e1000e_autoneg_pause(E1000ECore *core) 3364 { 3365 timer_del(core->autoneg_timer); 3366 } 3367 3368 static void 3369 e1000e_autoneg_resume(E1000ECore *core) 3370 { 3371 if (e1000e_have_autoneg(core) && 3372 !(core->phy[0][MII_BMSR] & MII_BMSR_AN_COMP)) { 3373 qemu_get_queue(core->owner_nic)->link_down = false; 3374 timer_mod(core->autoneg_timer, 3375 qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) + 500); 3376 } 3377 } 3378 3379 static void 3380 e1000e_vm_state_change(void *opaque, bool running, RunState state) 3381 { 3382 E1000ECore *core = opaque; 3383 3384 if (running) { 3385 trace_e1000e_vm_state_running(); 3386 e1000e_intrmgr_resume(core); 3387 e1000e_autoneg_resume(core); 3388 } else { 3389 trace_e1000e_vm_state_stopped(); 3390 e1000e_autoneg_pause(core); 3391 e1000e_intrmgr_pause(core); 3392 } 3393 } 3394 3395 void 3396 e1000e_core_pci_realize(E1000ECore *core, 3397 const uint16_t *eeprom_templ, 3398 uint32_t eeprom_size, 3399 const uint8_t *macaddr) 3400 { 3401 int i; 3402 3403 core->autoneg_timer = timer_new_ms(QEMU_CLOCK_VIRTUAL, 3404 e1000e_autoneg_timer, core); 3405 e1000e_intrmgr_pci_realize(core); 3406 3407 core->vmstate = 3408 qemu_add_vm_change_state_handler(e1000e_vm_state_change, core); 3409 3410 for (i = 0; i < E1000E_NUM_QUEUES; i++) { 3411 net_tx_pkt_init(&core->tx[i].tx_pkt, E1000E_MAX_TX_FRAGS); 3412 } 3413 3414 net_rx_pkt_init(&core->rx_pkt); 3415 3416 e1000x_core_prepare_eeprom(core->eeprom, 3417 eeprom_templ, 3418 eeprom_size, 3419 PCI_DEVICE_GET_CLASS(core->owner)->device_id, 3420 macaddr); 3421 e1000e_update_rx_offloads(core); 3422 } 3423 3424 void 3425 e1000e_core_pci_uninit(E1000ECore *core) 3426 { 3427 int i; 3428 3429 timer_free(core->autoneg_timer); 3430 3431 e1000e_intrmgr_pci_unint(core); 3432 3433 qemu_del_vm_change_state_handler(core->vmstate); 3434 3435 for (i = 0; i < E1000E_NUM_QUEUES; i++) { 3436 net_tx_pkt_reset(core->tx[i].tx_pkt, 3437 net_tx_pkt_unmap_frag_pci, core->owner); 3438 net_tx_pkt_uninit(core->tx[i].tx_pkt); 3439 } 3440 3441 net_rx_pkt_uninit(core->rx_pkt); 3442 } 3443 3444 static const uint16_t 3445 e1000e_phy_reg_init[E1000E_PHY_PAGES][E1000E_PHY_PAGE_SIZE] = { 3446 [0] = { 3447 [MII_BMCR] = MII_BMCR_SPEED1000 | 3448 MII_BMCR_FD | 3449 MII_BMCR_AUTOEN, 3450 3451 [MII_BMSR] = MII_BMSR_EXTCAP | 3452 MII_BMSR_LINK_ST | 3453 MII_BMSR_AUTONEG | 3454 MII_BMSR_MFPS | 3455 MII_BMSR_EXTSTAT | 3456 MII_BMSR_10T_HD | 3457 MII_BMSR_10T_FD | 3458 MII_BMSR_100TX_HD | 3459 MII_BMSR_100TX_FD, 3460 3461 [MII_PHYID1] = 0x141, 3462 [MII_PHYID2] = E1000_PHY_ID2_82574x, 3463 [MII_ANAR] = MII_ANAR_CSMACD | MII_ANAR_10 | 3464 MII_ANAR_10FD | MII_ANAR_TX | 3465 MII_ANAR_TXFD | MII_ANAR_PAUSE | 3466 MII_ANAR_PAUSE_ASYM, 3467 [MII_ANLPAR] = MII_ANLPAR_10 | MII_ANLPAR_10FD | 3468 MII_ANLPAR_TX | MII_ANLPAR_TXFD | 3469 MII_ANLPAR_T4 | MII_ANLPAR_PAUSE, 3470 [MII_ANER] = MII_ANER_NP | MII_ANER_NWAY, 3471 [MII_ANNP] = 1 | MII_ANNP_MP, 3472 [MII_CTRL1000] = MII_CTRL1000_HALF | MII_CTRL1000_FULL | 3473 MII_CTRL1000_PORT | MII_CTRL1000_MASTER, 3474 [MII_STAT1000] = MII_STAT1000_HALF | MII_STAT1000_FULL | 3475 MII_STAT1000_ROK | MII_STAT1000_LOK, 3476 [MII_EXTSTAT] = MII_EXTSTAT_1000T_HD | MII_EXTSTAT_1000T_FD, 3477 3478 [PHY_COPPER_CTRL1] = BIT(5) | BIT(6) | BIT(8) | BIT(9) | 3479 BIT(12) | BIT(13), 3480 [PHY_COPPER_STAT1] = BIT(3) | BIT(10) | BIT(11) | BIT(13) | BIT(15) 3481 }, 3482 [2] = { 3483 [PHY_MAC_CTRL1] = BIT(3) | BIT(7), 3484 [PHY_MAC_CTRL2] = BIT(1) | BIT(2) | BIT(6) | BIT(12) 3485 }, 3486 [3] = { 3487 [PHY_LED_TIMER_CTRL] = BIT(0) | BIT(2) | BIT(14) 3488 } 3489 }; 3490 3491 static const uint32_t e1000e_mac_reg_init[] = { 3492 [PBA] = 0x00140014, 3493 [LEDCTL] = BIT(1) | BIT(8) | BIT(9) | BIT(15) | BIT(17) | BIT(18), 3494 [EXTCNF_CTRL] = BIT(3), 3495 [EEMNGCTL] = BIT(31), 3496 [FLASHT] = 0x2, 3497 [FLSWCTL] = BIT(30) | BIT(31), 3498 [FLOL] = BIT(0), 3499 [RXDCTL] = BIT(16), 3500 [RXDCTL1] = BIT(16), 3501 [TIPG] = 0x8 | (0x8 << 10) | (0x6 << 20), 3502 [RXCFGL] = 0x88F7, 3503 [RXUDP] = 0x319, 3504 [CTRL] = E1000_CTRL_FD | E1000_CTRL_SWDPIN2 | E1000_CTRL_SWDPIN0 | 3505 E1000_CTRL_SPD_1000 | E1000_CTRL_SLU | 3506 E1000_CTRL_ADVD3WUC, 3507 [STATUS] = E1000_STATUS_ASDV_1000 | E1000_STATUS_LU, 3508 [PSRCTL] = (2 << E1000_PSRCTL_BSIZE0_SHIFT) | 3509 (4 << E1000_PSRCTL_BSIZE1_SHIFT) | 3510 (4 << E1000_PSRCTL_BSIZE2_SHIFT), 3511 [TARC0] = 0x3 | E1000_TARC_ENABLE, 3512 [TARC1] = 0x3 | E1000_TARC_ENABLE, 3513 [EECD] = E1000_EECD_AUTO_RD | E1000_EECD_PRES, 3514 [EERD] = E1000_EERW_DONE, 3515 [EEWR] = E1000_EERW_DONE, 3516 [GCR] = E1000_L0S_ADJUST | 3517 E1000_L1_ENTRY_LATENCY_MSB | 3518 E1000_L1_ENTRY_LATENCY_LSB, 3519 [TDFH] = 0x600, 3520 [TDFT] = 0x600, 3521 [TDFHS] = 0x600, 3522 [TDFTS] = 0x600, 3523 [POEMB] = 0x30D, 3524 [PBS] = 0x028, 3525 [MANC] = E1000_MANC_DIS_IP_CHK_ARP, 3526 [FACTPS] = E1000_FACTPS_LAN0_ON | 0x20000000, 3527 [SWSM] = 1, 3528 [RXCSUM] = E1000_RXCSUM_IPOFLD | E1000_RXCSUM_TUOFLD, 3529 [ITR] = E1000E_MIN_XITR, 3530 [EITR...EITR + E1000E_MSIX_VEC_NUM - 1] = E1000E_MIN_XITR, 3531 }; 3532 3533 static void e1000e_reset(E1000ECore *core, bool sw) 3534 { 3535 int i; 3536 3537 timer_del(core->autoneg_timer); 3538 3539 e1000e_intrmgr_reset(core); 3540 3541 memset(core->phy, 0, sizeof core->phy); 3542 memcpy(core->phy, e1000e_phy_reg_init, sizeof e1000e_phy_reg_init); 3543 3544 for (i = 0; i < E1000E_MAC_SIZE; i++) { 3545 if (sw && (i == PBA || i == PBS || i == FLA)) { 3546 continue; 3547 } 3548 3549 core->mac[i] = i < ARRAY_SIZE(e1000e_mac_reg_init) ? 3550 e1000e_mac_reg_init[i] : 0; 3551 } 3552 3553 core->rxbuf_min_shift = 1 + E1000_RING_DESC_LEN_SHIFT; 3554 3555 if (qemu_get_queue(core->owner_nic)->link_down) { 3556 e1000e_link_down(core); 3557 } 3558 3559 e1000x_reset_mac_addr(core->owner_nic, core->mac, core->permanent_mac); 3560 3561 for (i = 0; i < ARRAY_SIZE(core->tx); i++) { 3562 net_tx_pkt_reset(core->tx[i].tx_pkt, 3563 net_tx_pkt_unmap_frag_pci, core->owner); 3564 memset(&core->tx[i].props, 0, sizeof(core->tx[i].props)); 3565 core->tx[i].skip_cp = false; 3566 } 3567 } 3568 3569 void 3570 e1000e_core_reset(E1000ECore *core) 3571 { 3572 e1000e_reset(core, false); 3573 } 3574 3575 void e1000e_core_pre_save(E1000ECore *core) 3576 { 3577 int i; 3578 NetClientState *nc = qemu_get_queue(core->owner_nic); 3579 3580 /* 3581 * If link is down and auto-negotiation is supported and ongoing, 3582 * complete auto-negotiation immediately. This allows us to look 3583 * at MII_BMSR_AN_COMP to infer link status on load. 3584 */ 3585 if (nc->link_down && e1000e_have_autoneg(core)) { 3586 core->phy[0][MII_BMSR] |= MII_BMSR_AN_COMP; 3587 e1000e_update_flowctl_status(core); 3588 } 3589 3590 for (i = 0; i < ARRAY_SIZE(core->tx); i++) { 3591 if (net_tx_pkt_has_fragments(core->tx[i].tx_pkt)) { 3592 core->tx[i].skip_cp = true; 3593 } 3594 } 3595 } 3596 3597 int 3598 e1000e_core_post_load(E1000ECore *core) 3599 { 3600 NetClientState *nc = qemu_get_queue(core->owner_nic); 3601 3602 /* 3603 * nc.link_down can't be migrated, so infer link_down according 3604 * to link status bit in core.mac[STATUS]. 3605 */ 3606 nc->link_down = (core->mac[STATUS] & E1000_STATUS_LU) == 0; 3607 3608 return 0; 3609 } 3610