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