2 * Copyright (c) 2004-2005 The Regents of The University of Michigan
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 * Authors: Nathan Binkert
35 #include "arch/vtophys.hh"
36 #include "base/debug.hh"
37 #include "base/inet.hh"
38 #include "base/types.hh"
39 #include "config/the_isa.hh"
40 #include "cpu/intr_control.hh"
41 #include "cpu/thread_context.hh"
42 #include "debug/EthernetAll.hh"
43 #include "dev/etherlink.hh"
44 #include "dev/sinic.hh"
45 #include "mem/packet.hh"
46 #include "mem/packet_access.hh"
47 #include "sim/eventq.hh"
48 #include "sim/stats.hh"
52 using namespace TheISA
;
56 const char *RxStateStrings
[] =
65 const char *TxStateStrings
[] =
75 ///////////////////////////////////////////////////////////////////////
79 Base::Base(const Params
*p
)
80 : PciDev(p
), rxEnable(false), txEnable(false), clock(p
->clock
),
81 intrDelay(p
->intr_delay
), intrTick(0), cpuIntrEnable(false),
82 cpuPendingIntr(false), intrEvent(0), interface(NULL
)
86 Device::Device(const Params
*p
)
87 : Base(p
), rxUnique(0), txUnique(0),
88 virtualRegs(p
->virtual_count
< 1 ? 1 : p
->virtual_count
),
89 rxFifo(p
->rx_fifo_size
), txFifo(p
->tx_fifo_size
),
90 rxKickTick(0), txKickTick(0),
91 txEvent(this), rxDmaEvent(this), txDmaEvent(this),
92 dmaReadDelay(p
->dma_read_delay
), dmaReadFactor(p
->dma_read_factor
),
93 dmaWriteDelay(p
->dma_write_delay
), dmaWriteFactor(p
->dma_write_factor
)
95 interface
= new Interface(name() + ".int0", this);
107 .name(name() + ".rxBytes")
108 .desc("Bytes Received")
113 .name(name() + ".rxBandwidth")
114 .desc("Receive Bandwidth (bits/s)")
120 .name(name() + ".rxPackets")
121 .desc("Number of Packets Received")
126 .name(name() + ".rxPPS")
127 .desc("Packet Reception Rate (packets/s)")
133 .name(name() + ".rxIpPackets")
134 .desc("Number of IP Packets Received")
139 .name(name() + ".rxTcpPackets")
140 .desc("Number of Packets Received")
145 .name(name() + ".rxUdpPackets")
146 .desc("Number of UDP Packets Received")
151 .name(name() + ".rxIpChecksums")
152 .desc("Number of rx IP Checksums done by device")
158 .name(name() + ".rxTcpChecksums")
159 .desc("Number of rx TCP Checksums done by device")
165 .name(name() + ".rxUdpChecksums")
166 .desc("Number of rx UDP Checksums done by device")
172 .name(name() + ".totBandwidth")
173 .desc("Total Bandwidth (bits/s)")
179 .name(name() + ".totPackets")
180 .desc("Total Packets")
186 .name(name() + ".totBytes")
193 .name(name() + ".totPPS")
194 .desc("Total Tranmission Rate (packets/s)")
200 .name(name() + ".txBytes")
201 .desc("Bytes Transmitted")
206 .name(name() + ".txBandwidth")
207 .desc("Transmit Bandwidth (bits/s)")
213 .name(name() + ".txPackets")
214 .desc("Number of Packets Transmitted")
219 .name(name() + ".txPPS")
220 .desc("Packet Tranmission Rate (packets/s)")
226 .name(name() + ".txIpPackets")
227 .desc("Number of IP Packets Transmitted")
232 .name(name() + ".txTcpPackets")
233 .desc("Number of TCP Packets Transmitted")
238 .name(name() + ".txUdpPackets")
239 .desc("Number of Packets Transmitted")
244 .name(name() + ".txIpChecksums")
245 .desc("Number of tx IP Checksums done by device")
251 .name(name() + ".txTcpChecksums")
252 .desc("Number of tx TCP Checksums done by device")
258 .name(name() + ".txUdpChecksums")
259 .desc("Number of tx UDP Checksums done by device")
264 txBandwidth
= txBytes
* Stats::constant(8) / simSeconds
;
265 rxBandwidth
= rxBytes
* Stats::constant(8) / simSeconds
;
266 totBandwidth
= txBandwidth
+ rxBandwidth
;
267 totBytes
= txBytes
+ rxBytes
;
268 totPackets
= txPackets
+ rxPackets
;
269 txPacketRate
= txPackets
/ simSeconds
;
270 rxPacketRate
= rxPackets
/ simSeconds
;
272 _maxVnicDistance
= 0;
275 .name(name() + ".maxVnicDistance")
276 .desc("maximum vnic distance")
280 .name(name() + ".totalVnicDistance")
281 .desc("total vnic distance")
284 .name(name() + ".numVnicDistance")
285 .desc("number of vnic distance measurements")
289 .name(name() + ".avgVnicDistance")
290 .desc("average vnic distance")
293 avgVnicDistance
= totalVnicDistance
/ numVnicDistance
;
299 _maxVnicDistance
= 0;
303 Device::getEthPort(const std::string
&if_name
, int idx
)
305 if (if_name
== "interface") {
306 if (interface
->getPeer())
307 panic("interface already connected to\n");
316 Device::prepareIO(int cpu
, int index
)
318 int size
= virtualRegs
.size();
320 panic("Trying to access a vnic that doesn't exist %d > %d\n",
324 //add stats for head of line blocking
325 //add stats for average fifo length
326 //add stats for average number of vnics busy
329 Device::prepareRead(int cpu
, int index
)
331 using namespace Regs
;
332 prepareIO(cpu
, index
);
334 VirtualReg
&vnic
= virtualRegs
[index
];
336 // update rx registers
337 uint64_t rxdone
= vnic
.RxDone
;
338 rxdone
= set_RxDone_Packets(rxdone
, rxFifo
.countPacketsAfter(rxFifoPtr
));
339 rxdone
= set_RxDone_Empty(rxdone
, rxFifo
.empty());
340 rxdone
= set_RxDone_High(rxdone
, rxFifo
.size() > regs
.RxFifoHigh
);
341 rxdone
= set_RxDone_NotHigh(rxdone
, rxLow
);
342 regs
.RxData
= vnic
.RxData
;
343 regs
.RxDone
= rxdone
;
344 regs
.RxWait
= rxdone
;
346 // update tx regsiters
347 uint64_t txdone
= vnic
.TxDone
;
348 txdone
= set_TxDone_Packets(txdone
, txFifo
.packets());
349 txdone
= set_TxDone_Full(txdone
, txFifo
.avail() < regs
.TxMaxCopy
);
350 txdone
= set_TxDone_Low(txdone
, txFifo
.size() < regs
.TxFifoLow
);
351 regs
.TxData
= vnic
.TxData
;
352 regs
.TxDone
= txdone
;
353 regs
.TxWait
= txdone
;
357 if (!rxFifo
.empty()) {
358 int vnic
= rxFifo
.begin()->priv
;
359 if (vnic
!= -1 && virtualRegs
[vnic
].rxPacketOffset
> 0)
363 regs
.RxStatus
= set_RxStatus_Head(regs
.RxStatus
, head
);
364 regs
.RxStatus
= set_RxStatus_Busy(regs
.RxStatus
, rxBusyCount
);
365 regs
.RxStatus
= set_RxStatus_Mapped(regs
.RxStatus
, rxMappedCount
);
366 regs
.RxStatus
= set_RxStatus_Dirty(regs
.RxStatus
, rxDirtyCount
);
370 Device::prepareWrite(int cpu
, int index
)
372 prepareIO(cpu
, index
);
376 * I/O read of device register
379 Device::read(PacketPtr pkt
)
381 assert(config
.command
& PCI_CMD_MSE
);
382 assert(pkt
->getAddr() >= BARAddrs
[0] && pkt
->getSize() < BARSize
[0]);
384 int cpu
= pkt
->req
->contextId();
385 Addr daddr
= pkt
->getAddr() - BARAddrs
[0];
386 Addr index
= daddr
>> Regs::VirtualShift
;
387 Addr raddr
= daddr
& Regs::VirtualMask
;
391 if (!regValid(raddr
))
392 panic("invalid register: cpu=%d vnic=%d da=%#x pa=%#x size=%d",
393 cpu
, index
, daddr
, pkt
->getAddr(), pkt
->getSize());
395 const Regs::Info
&info
= regInfo(raddr
);
397 panic("read %s (write only): "
398 "cpu=%d vnic=%d da=%#x pa=%#x size=%d",
399 info
.name
, cpu
, index
, daddr
, pkt
->getAddr(), pkt
->getSize());
401 panic("read %s (invalid size): "
402 "cpu=%d vnic=%d da=%#x pa=%#x size=%d",
403 info
.name
, cpu
, index
, daddr
, pkt
->getAddr(), pkt
->getSize());
405 prepareRead(cpu
, index
);
408 if (pkt
->getSize() == 4) {
409 uint32_t reg
= regData32(raddr
);
414 if (pkt
->getSize() == 8) {
415 uint64_t reg
= regData64(raddr
);
421 "read %s: cpu=%d vnic=%d da=%#x pa=%#x size=%d val=%#x\n",
422 info
.name
, cpu
, index
, daddr
, pkt
->getAddr(), pkt
->getSize(), value
);
424 // reading the interrupt status register has the side effect of
426 if (raddr
== Regs::IntrStatus
)
433 * IPR read of device register
436 Device::iprRead(Addr daddr, int cpu, uint64_t &result)
438 if (!regValid(daddr))
439 panic("invalid address: da=%#x", daddr);
441 const Regs::Info &info = regInfo(daddr);
443 panic("reading %s (write only): cpu=%d da=%#x", info.name, cpu, daddr);
445 DPRINTF(EthernetPIO, "IPR read %s: cpu=%d da=%#x\n",
446 info.name, cpu, daddr);
451 result = regData32(daddr);
454 result = regData64(daddr);
456 DPRINTF(EthernetPIO, "IPR read %s: cpu=%s da=%#x val=%#x\n",
457 info.name, cpu, result);
463 * I/O write of device register
466 Device::write(PacketPtr pkt
)
468 assert(config
.command
& PCI_CMD_MSE
);
469 assert(pkt
->getAddr() >= BARAddrs
[0] && pkt
->getSize() < BARSize
[0]);
471 int cpu
= pkt
->req
->contextId();
472 Addr daddr
= pkt
->getAddr() - BARAddrs
[0];
473 Addr index
= daddr
>> Regs::VirtualShift
;
474 Addr raddr
= daddr
& Regs::VirtualMask
;
476 if (!regValid(raddr
))
477 panic("invalid register: cpu=%d, da=%#x pa=%#x size=%d",
478 cpu
, daddr
, pkt
->getAddr(), pkt
->getSize());
480 const Regs::Info
&info
= regInfo(raddr
);
482 panic("write %s (read only): "
483 "cpu=%d vnic=%d da=%#x pa=%#x size=%d",
484 info
.name
, cpu
, index
, daddr
, pkt
->getAddr(), pkt
->getSize());
486 if (pkt
->getSize() != info
.size
)
487 panic("write %s (invalid size): "
488 "cpu=%d vnic=%d da=%#x pa=%#x size=%d",
489 info
.name
, cpu
, index
, daddr
, pkt
->getAddr(), pkt
->getSize());
491 VirtualReg
&vnic
= virtualRegs
[index
];
494 "write %s vnic %d: cpu=%d val=%#x da=%#x pa=%#x size=%d\n",
495 info
.name
, index
, cpu
, info
.size
== 4 ? pkt
->get
<uint32_t>() :
496 pkt
->get
<uint64_t>(), daddr
, pkt
->getAddr(), pkt
->getSize());
498 prepareWrite(cpu
, index
);
502 changeConfig(pkt
->get
<uint32_t>());
506 command(pkt
->get
<uint32_t>());
509 case Regs::IntrStatus
:
510 devIntrClear(regs
.IntrStatus
& pkt
->get
<uint32_t>());
514 devIntrChangeMask(pkt
->get
<uint32_t>());
518 if (Regs::get_RxDone_Busy(vnic
.RxDone
))
519 panic("receive machine busy with another request! rxState=%s",
520 RxStateStrings
[rxState
]);
522 vnic
.rxUnique
= rxUnique
++;
523 vnic
.RxDone
= Regs::RxDone_Busy
;
524 vnic
.RxData
= pkt
->get
<uint64_t>();
527 if (Regs::get_RxData_Vaddr(pkt
->get
<uint64_t>())) {
528 panic("vtophys not implemented in newmem");
530 Addr vaddr
= Regs::get_RxData_Addr(reg64
);
531 Addr paddr
= vtophys(req
->xc
, vaddr
);
532 DPRINTF(EthernetPIO
, "write RxData vnic %d (rxunique %d): "
533 "vaddr=%#x, paddr=%#x\n",
534 index
, vnic
.rxUnique
, vaddr
, paddr
);
536 vnic
.RxData
= Regs::set_RxData_Addr(vnic
.RxData
, paddr
);
539 DPRINTF(EthernetPIO
, "write RxData vnic %d (rxunique %d)\n",
540 index
, vnic
.rxUnique
);
543 if (vnic
.rxIndex
== rxFifo
.end()) {
544 DPRINTF(EthernetPIO
, "request new packet...appending to rxList\n");
545 rxList
.push_back(index
);
547 DPRINTF(EthernetPIO
, "packet exists...appending to rxBusy\n");
548 rxBusy
.push_back(index
);
551 if (rxEnable
&& (rxState
== rxIdle
|| rxState
== rxFifoBlock
)) {
552 rxState
= rxFifoBlock
;
558 if (Regs::get_TxDone_Busy(vnic
.TxDone
))
559 panic("transmit machine busy with another request! txState=%s",
560 TxStateStrings
[txState
]);
562 vnic
.txUnique
= txUnique
++;
563 vnic
.TxDone
= Regs::TxDone_Busy
;
565 if (Regs::get_TxData_Vaddr(pkt
->get
<uint64_t>())) {
566 panic("vtophys won't work here in newmem.\n");
568 Addr vaddr
= Regs::get_TxData_Addr(reg64
);
569 Addr paddr
= vtophys(req
->xc
, vaddr
);
570 DPRINTF(EthernetPIO
, "write TxData vnic %d (txunique %d): "
571 "vaddr=%#x, paddr=%#x\n",
572 index
, vnic
.txUnique
, vaddr
, paddr
);
574 vnic
.TxData
= Regs::set_TxData_Addr(vnic
.TxData
, paddr
);
577 DPRINTF(EthernetPIO
, "write TxData vnic %d (txunique %d)\n",
578 index
, vnic
.txUnique
);
581 if (txList
.empty() || txList
.front() != index
)
582 txList
.push_back(index
);
583 if (txEnable
&& txState
== txIdle
&& txList
.front() == index
) {
584 txState
= txFifoBlock
;
594 Device::devIntrPost(uint32_t interrupts
)
596 if ((interrupts
& Regs::Intr_Res
))
597 panic("Cannot set a reserved interrupt");
599 regs
.IntrStatus
|= interrupts
;
601 DPRINTF(EthernetIntr
,
602 "interrupt written to intStatus: intr=%#x status=%#x mask=%#x\n",
603 interrupts
, regs
.IntrStatus
, regs
.IntrMask
);
605 interrupts
= regs
.IntrStatus
& regs
.IntrMask
;
607 // Intr_RxHigh is special, we only signal it if we've emptied the fifo
608 // and then filled it above the high watermark
612 interrupts
&= ~Regs::Intr_RxHigh
;
614 // Intr_TxLow is special, we only signal it if we've filled up the fifo
615 // and then dropped below the low watermark
619 interrupts
&= ~Regs::Intr_TxLow
;
622 Tick when
= curTick();
623 if ((interrupts
& Regs::Intr_NoDelay
) == 0)
630 Device::devIntrClear(uint32_t interrupts
)
632 if ((interrupts
& Regs::Intr_Res
))
633 panic("Cannot clear a reserved interrupt");
635 regs
.IntrStatus
&= ~interrupts
;
637 DPRINTF(EthernetIntr
,
638 "interrupt cleared from intStatus: intr=%x status=%x mask=%x\n",
639 interrupts
, regs
.IntrStatus
, regs
.IntrMask
);
641 if (!(regs
.IntrStatus
& regs
.IntrMask
))
646 Device::devIntrChangeMask(uint32_t newmask
)
648 if (regs
.IntrMask
== newmask
)
651 regs
.IntrMask
= newmask
;
653 DPRINTF(EthernetIntr
,
654 "interrupt mask changed: intStatus=%x intMask=%x masked=%x\n",
655 regs
.IntrStatus
, regs
.IntrMask
, regs
.IntrStatus
& regs
.IntrMask
);
657 if (regs
.IntrStatus
& regs
.IntrMask
)
658 cpuIntrPost(curTick());
664 Base::cpuIntrPost(Tick when
)
666 // If the interrupt you want to post is later than an interrupt
667 // already scheduled, just let it post in the coming one and don't
669 // HOWEVER, must be sure that the scheduled intrTick is in the
670 // future (this was formerly the source of a bug)
672 * @todo this warning should be removed and the intrTick code should
675 assert(when
>= curTick());
676 assert(intrTick
>= curTick() || intrTick
== 0);
677 if (!cpuIntrEnable
) {
678 DPRINTF(EthernetIntr
, "interrupts not enabled.\n",
683 if (when
> intrTick
&& intrTick
!= 0) {
684 DPRINTF(EthernetIntr
, "don't need to schedule event...intrTick=%d\n",
690 if (intrTick
< curTick()) {
692 intrTick
= curTick();
695 DPRINTF(EthernetIntr
, "going to schedule an interrupt for intrTick=%d\n",
700 intrEvent
= new IntrEvent(this, true);
701 schedule(intrEvent
, intrTick
);
707 assert(intrTick
== curTick());
709 // Whether or not there's a pending interrupt, we don't care about
714 // Don't send an interrupt if there's already one
715 if (cpuPendingIntr
) {
716 DPRINTF(EthernetIntr
,
717 "would send an interrupt now, but there's already pending\n");
720 cpuPendingIntr
= true;
722 DPRINTF(EthernetIntr
, "posting interrupt\n");
740 cpuPendingIntr
= false;
742 DPRINTF(EthernetIntr
, "clearing cchip interrupt\n");
747 Base::cpuIntrPending() const
748 { return cpuPendingIntr
; }
751 Device::changeConfig(uint32_t newconf
)
753 uint32_t changed
= regs
.Config
^ newconf
;
757 regs
.Config
= newconf
;
759 if ((changed
& Regs::Config_IntEn
)) {
760 cpuIntrEnable
= regs
.Config
& Regs::Config_IntEn
;
762 if (regs
.IntrStatus
& regs
.IntrMask
)
763 cpuIntrPost(curTick());
769 if ((changed
& Regs::Config_TxEn
)) {
770 txEnable
= regs
.Config
& Regs::Config_TxEn
;
775 if ((changed
& Regs::Config_RxEn
)) {
776 rxEnable
= regs
.Config
& Regs::Config_RxEn
;
783 Device::command(uint32_t command
)
785 if (command
& Regs::Command_Intr
)
786 devIntrPost(Regs::Intr_Soft
);
788 if (command
& Regs::Command_Reset
)
795 using namespace Regs
;
797 memset(®s
, 0, sizeof(regs
));
800 if (params()->rx_thread
)
801 regs
.Config
|= Config_RxThread
;
802 if (params()->tx_thread
)
803 regs
.Config
|= Config_TxThread
;
805 regs
.Config
|= Config_RSS
;
806 if (params()->zero_copy
)
807 regs
.Config
|= Config_ZeroCopy
;
808 if (params()->delay_copy
)
809 regs
.Config
|= Config_DelayCopy
;
810 if (params()->virtual_addr
)
811 regs
.Config
|= Config_Vaddr
;
813 if (params()->delay_copy
&& params()->zero_copy
)
814 panic("Can't delay copy and zero copy");
816 regs
.IntrMask
= Intr_Soft
| Intr_RxHigh
| Intr_RxPacket
| Intr_TxLow
;
817 regs
.RxMaxCopy
= params()->rx_max_copy
;
818 regs
.TxMaxCopy
= params()->tx_max_copy
;
819 regs
.ZeroCopySize
= params()->zero_copy_size
;
820 regs
.ZeroCopyMark
= params()->zero_copy_threshold
;
821 regs
.VirtualCount
= params()->virtual_count
;
822 regs
.RxMaxIntr
= params()->rx_max_intr
;
823 regs
.RxFifoSize
= params()->rx_fifo_size
;
824 regs
.TxFifoSize
= params()->tx_fifo_size
;
825 regs
.RxFifoLow
= params()->rx_fifo_low_mark
;
826 regs
.TxFifoLow
= params()->tx_fifo_threshold
;
827 regs
.RxFifoHigh
= params()->rx_fifo_threshold
;
828 regs
.TxFifoHigh
= params()->tx_fifo_high_mark
;
829 regs
.HwAddr
= params()->hardware_address
;
831 if (regs
.RxMaxCopy
< regs
.ZeroCopyMark
)
832 panic("Must be able to copy at least as many bytes as the threshold");
834 if (regs
.ZeroCopySize
>= regs
.ZeroCopyMark
)
835 panic("The number of bytes to copy must be less than the threshold");
849 rxFifoPtr
= rxFifo
.end();
855 int size
= virtualRegs
.size();
857 virtualRegs
.resize(size
);
858 for (int i
= 0; i
< size
; ++i
)
859 virtualRegs
[i
].rxIndex
= rxFifo
.end();
865 assert(rxState
== rxCopy
);
866 rxState
= rxCopyDone
;
867 DPRINTF(EthernetDMA
, "end rx dma write paddr=%#x len=%d\n",
868 rxDmaAddr
, rxDmaLen
);
869 DDUMP(EthernetData
, rxDmaData
, rxDmaLen
);
871 // If the transmit state machine has a pending DMA, let it go first
872 if (txState
== txBeginCopy
)
881 VirtualReg
*vnic
= NULL
;
883 DPRINTF(EthernetSM
, "rxKick: rxState=%s (rxFifo.size=%d)\n",
884 RxStateStrings
[rxState
], rxFifo
.size());
886 if (rxKickTick
> curTick()) {
887 DPRINTF(EthernetSM
, "rxKick: exiting, can't run till %d\n",
894 if (rxState
== rxIdle
)
897 if (rxActive
== -1) {
898 if (rxState
!= rxFifoBlock
)
899 panic("no active vnic while in state %s", RxStateStrings
[rxState
]);
901 DPRINTF(EthernetSM
, "processing rxState=%s\n",
902 RxStateStrings
[rxState
]);
904 vnic
= &virtualRegs
[rxActive
];
906 "processing rxState=%s for vnic %d (rxunique %d)\n",
907 RxStateStrings
[rxState
], rxActive
, vnic
->rxUnique
);
912 if (DTRACE(EthernetSM
)) {
913 PacketFifo::iterator end
= rxFifo
.end();
914 int size
= virtualRegs
.size();
915 for (int i
= 0; i
< size
; ++i
) {
916 VirtualReg
*vn
= &virtualRegs
[i
];
917 bool busy
= Regs::get_RxDone_Busy(vn
->RxDone
);
918 if (vn
->rxIndex
!= end
) {
919 bool dirty
= vn
->rxPacketOffset
> 0;
923 status
= "busy,dirty";
932 "vnic %d %s (rxunique %d), packet %d, slack %d\n",
933 i
, status
, vn
->rxUnique
,
934 rxFifo
.countPacketsBefore(vn
->rxIndex
),
937 DPRINTF(EthernetSM
, "vnic %d unmapped (rxunique %d)\n",
943 if (!rxBusy
.empty()) {
944 rxActive
= rxBusy
.front();
946 vnic
= &virtualRegs
[rxActive
];
948 if (vnic
->rxIndex
== rxFifo
.end())
949 panic("continuing vnic without packet\n");
952 "continue processing for vnic %d (rxunique %d)\n",
953 rxActive
, vnic
->rxUnique
);
955 rxState
= rxBeginCopy
;
957 int vnic_distance
= rxFifo
.countPacketsBefore(vnic
->rxIndex
);
958 totalVnicDistance
+= vnic_distance
;
959 numVnicDistance
+= 1;
960 if (vnic_distance
> _maxVnicDistance
) {
961 maxVnicDistance
= vnic_distance
;
962 _maxVnicDistance
= vnic_distance
;
968 if (rxFifoPtr
== rxFifo
.end()) {
969 DPRINTF(EthernetSM
, "receive waiting for data. Nothing to do.\n");
974 panic("Not idle, but nothing to do!");
976 assert(!rxFifo
.empty());
978 rxActive
= rxList
.front();
980 vnic
= &virtualRegs
[rxActive
];
983 "processing new packet for vnic %d (rxunique %d)\n",
984 rxActive
, vnic
->rxUnique
);
986 // Grab a new packet from the fifo.
987 vnic
->rxIndex
= rxFifoPtr
++;
988 vnic
->rxIndex
->priv
= rxActive
;
989 vnic
->rxPacketOffset
= 0;
990 vnic
->rxPacketBytes
= vnic
->rxIndex
->packet
->length
;
991 assert(vnic
->rxPacketBytes
);
994 vnic
->rxDoneData
= 0;
995 /* scope for variables */ {
996 IpPtr
ip(vnic
->rxIndex
->packet
);
998 DPRINTF(Ethernet
, "ID is %d\n", ip
->id());
999 vnic
->rxDoneData
|= Regs::RxDone_IpPacket
;
1001 if (cksum(ip
) != 0) {
1002 DPRINTF(EthernetCksum
, "Rx IP Checksum Error\n");
1003 vnic
->rxDoneData
|= Regs::RxDone_IpError
;
1009 "Src Port=%d, Dest Port=%d, Seq=%d, Ack=%d\n",
1010 tcp
->sport(), tcp
->dport(), tcp
->seq(),
1012 vnic
->rxDoneData
|= Regs::RxDone_TcpPacket
;
1014 if (cksum(tcp
) != 0) {
1015 DPRINTF(EthernetCksum
, "Rx TCP Checksum Error\n");
1016 vnic
->rxDoneData
|= Regs::RxDone_TcpError
;
1019 vnic
->rxDoneData
|= Regs::RxDone_UdpPacket
;
1021 if (cksum(udp
) != 0) {
1022 DPRINTF(EthernetCksum
, "Rx UDP Checksum Error\n");
1023 vnic
->rxDoneData
|= Regs::RxDone_UdpError
;
1028 rxState
= rxBeginCopy
;
1032 if (dmaPending() || getState() != Running
)
1035 rxDmaAddr
= params()->platform
->pciToDma(
1036 Regs::get_RxData_Addr(vnic
->RxData
));
1037 rxDmaLen
= min
<unsigned>(Regs::get_RxData_Len(vnic
->RxData
),
1038 vnic
->rxPacketBytes
);
1041 * if we're doing zero/delay copy and we're below the fifo
1042 * threshold, see if we should try to do the zero/defer copy
1044 if ((Regs::get_Config_ZeroCopy(regs
.Config
) ||
1045 Regs::get_Config_DelayCopy(regs
.Config
)) &&
1046 !Regs::get_RxData_NoDelay(vnic
->RxData
) && rxLow
) {
1047 if (rxDmaLen
> regs
.ZeroCopyMark
)
1048 rxDmaLen
= regs
.ZeroCopySize
;
1050 rxDmaData
= vnic
->rxIndex
->packet
->data
+ vnic
->rxPacketOffset
;
1052 if (rxDmaAddr
== 1LL) {
1053 rxState
= rxCopyDone
;
1057 dmaWrite(rxDmaAddr
, rxDmaLen
, &rxDmaEvent
, rxDmaData
);
1061 DPRINTF(EthernetSM
, "receive machine still copying\n");
1065 vnic
->RxDone
= vnic
->rxDoneData
;
1066 vnic
->RxDone
|= Regs::RxDone_Complete
;
1069 if (vnic
->rxPacketBytes
== rxDmaLen
) {
1070 if (vnic
->rxPacketOffset
)
1073 // Packet is complete. Indicate how many bytes were copied
1074 vnic
->RxDone
= Regs::set_RxDone_CopyLen(vnic
->RxDone
, rxDmaLen
);
1077 "rxKick: packet complete on vnic %d (rxunique %d)\n",
1078 rxActive
, vnic
->rxUnique
);
1079 rxFifo
.remove(vnic
->rxIndex
);
1080 vnic
->rxIndex
= rxFifo
.end();
1083 if (!vnic
->rxPacketOffset
)
1086 vnic
->rxPacketBytes
-= rxDmaLen
;
1087 vnic
->rxPacketOffset
+= rxDmaLen
;
1088 vnic
->RxDone
|= Regs::RxDone_More
;
1089 vnic
->RxDone
= Regs::set_RxDone_CopyLen(vnic
->RxDone
,
1090 vnic
->rxPacketBytes
);
1092 "rxKick: packet not complete on vnic %d (rxunique %d): "
1094 rxActive
, vnic
->rxUnique
, vnic
->rxPacketBytes
);
1098 rxState
= rxBusy
.empty() && rxList
.empty() ? rxIdle
: rxFifoBlock
;
1100 if (rxFifo
.empty()) {
1101 devIntrPost(Regs::Intr_RxEmpty
);
1105 if (rxFifo
.size() < regs
.RxFifoLow
)
1108 if (rxFifo
.size() > regs
.RxFifoHigh
)
1111 devIntrPost(Regs::Intr_RxDMA
);
1115 panic("Invalid rxState!");
1118 DPRINTF(EthernetSM
, "entering next rxState=%s\n",
1119 RxStateStrings
[rxState
]);
1125 * @todo do we want to schedule a future kick?
1127 DPRINTF(EthernetSM
, "rx state machine exited rxState=%s\n",
1128 RxStateStrings
[rxState
]);
1134 assert(txState
== txCopy
);
1135 txState
= txCopyDone
;
1136 DPRINTF(EthernetDMA
, "tx dma read paddr=%#x len=%d\n",
1137 txDmaAddr
, txDmaLen
);
1138 DDUMP(EthernetData
, txDmaData
, txDmaLen
);
1140 // If the receive state machine has a pending DMA, let it go first
1141 if (rxState
== rxBeginCopy
)
1150 if (txFifo
.empty()) {
1151 DPRINTF(Ethernet
, "nothing to transmit\n");
1155 uint32_t interrupts
;
1156 EthPacketPtr packet
= txFifo
.front();
1157 if (!interface
->sendPacket(packet
)) {
1158 DPRINTF(Ethernet
, "Packet Transmit: failed txFifo available %d\n",
1165 if (DTRACE(Ethernet
)) {
1168 DPRINTF(Ethernet
, "ID is %d\n", ip
->id());
1172 "Src Port=%d, Dest Port=%d, Seq=%d, Ack=%d\n",
1173 tcp
->sport(), tcp
->dport(), tcp
->seq(),
1180 DDUMP(EthernetData
, packet
->data
, packet
->length
);
1181 txBytes
+= packet
->length
;
1184 DPRINTF(Ethernet
, "Packet Transmit: successful txFifo Available %d\n",
1187 interrupts
= Regs::Intr_TxPacket
;
1188 if (txFifo
.size() < regs
.TxFifoLow
)
1189 interrupts
|= Regs::Intr_TxLow
;
1190 devIntrPost(interrupts
);
1197 DPRINTF(EthernetSM
, "txKick: txState=%s (txFifo.size=%d)\n",
1198 TxStateStrings
[txState
], txFifo
.size());
1200 if (txKickTick
> curTick()) {
1201 DPRINTF(EthernetSM
, "txKick: exiting, can't run till %d\n",
1207 if (txState
== txIdle
)
1210 assert(!txList
.empty());
1211 vnic
= &virtualRegs
[txList
.front()];
1215 assert(Regs::get_TxDone_Busy(vnic
->TxDone
));
1217 // Grab a new packet from the fifo.
1218 txPacket
= new EthPacketData(16384);
1222 if (txFifo
.avail() - txPacket
->length
<
1223 Regs::get_TxData_Len(vnic
->TxData
)) {
1224 DPRINTF(EthernetSM
, "transmit fifo full. Nothing to do.\n");
1228 txState
= txBeginCopy
;
1232 if (dmaPending() || getState() != Running
)
1235 txDmaAddr
= params()->platform
->pciToDma(
1236 Regs::get_TxData_Addr(vnic
->TxData
));
1237 txDmaLen
= Regs::get_TxData_Len(vnic
->TxData
);
1238 txDmaData
= txPacket
->data
+ txPacketOffset
;
1241 dmaRead(txDmaAddr
, txDmaLen
, &txDmaEvent
, txDmaData
);
1245 DPRINTF(EthernetSM
, "transmit machine still copying\n");
1249 vnic
->TxDone
= txDmaLen
| Regs::TxDone_Complete
;
1250 txPacket
->length
+= txDmaLen
;
1251 if ((vnic
->TxData
& Regs::TxData_More
)) {
1252 txPacketOffset
+= txDmaLen
;
1254 devIntrPost(Regs::Intr_TxDMA
);
1258 assert(txPacket
->length
<= txFifo
.avail());
1259 if ((vnic
->TxData
& Regs::TxData_Checksum
)) {
1265 tcp
->sum(cksum(tcp
));
1272 udp
->sum(cksum(udp
));
1282 txFifo
.push(txPacket
);
1283 if (txFifo
.avail() < regs
.TxMaxCopy
) {
1284 devIntrPost(Regs::Intr_TxFull
);
1290 txState
= txList
.empty() ? txIdle
: txFifoBlock
;
1291 devIntrPost(Regs::Intr_TxDMA
);
1295 panic("Invalid txState!");
1298 DPRINTF(EthernetSM
, "entering next txState=%s\n",
1299 TxStateStrings
[txState
]);
1305 * @todo do we want to schedule a future kick?
1307 DPRINTF(EthernetSM
, "tx state machine exited txState=%s\n",
1308 TxStateStrings
[txState
]);
1312 Device::transferDone()
1314 if (txFifo
.empty()) {
1315 DPRINTF(Ethernet
, "transfer complete: txFifo empty...nothing to do\n");
1319 DPRINTF(Ethernet
, "transfer complete: data in txFifo...schedule xmit\n");
1321 reschedule(txEvent
, curTick() + ticks(1), true);
1325 Device::rxFilter(const EthPacketPtr
&packet
)
1327 if (!Regs::get_Config_Filter(regs
.Config
))
1330 panic("receive filter not implemented\n");
1336 EthHdr
*eth
= packet
->eth();
1337 if (eth
->unicast()) {
1338 // If we're accepting all unicast addresses
1342 // If we make a perfect match
1343 if (acceptPerfect
&& params
->eaddr
== eth
.dst())
1346 if (acceptArp
&& eth
->type() == ETH_TYPE_ARP
)
1349 } else if (eth
->broadcast()) {
1350 // if we're accepting broadcasts
1351 if (acceptBroadcast
)
1354 } else if (eth
->multicast()) {
1355 // if we're accepting all multicasts
1356 if (acceptMulticast
)
1362 DPRINTF(Ethernet
, "rxFilter drop\n");
1363 DDUMP(EthernetData
, packet
->data
, packet
->length
);
1370 Device::recvPacket(EthPacketPtr packet
)
1372 rxBytes
+= packet
->length
;
1375 DPRINTF(Ethernet
, "Receiving packet from wire, rxFifo Available is %d\n",
1379 DPRINTF(Ethernet
, "receive disabled...packet dropped\n");
1383 if (rxFilter(packet
)) {
1384 DPRINTF(Ethernet
, "packet filtered...dropped\n");
1388 if (rxFifo
.size() >= regs
.RxFifoHigh
)
1389 devIntrPost(Regs::Intr_RxHigh
);
1391 if (!rxFifo
.push(packet
)) {
1393 "packet will not fit in receive buffer...packet dropped\n");
1397 // If we were at the last element, back up one ot go to the new
1398 // last element of the list.
1399 if (rxFifoPtr
== rxFifo
.end())
1402 devIntrPost(Regs::Intr_RxPacket
);
1410 SimObject::resume();
1412 // During drain we could have left the state machines in a waiting state and
1413 // they wouldn't get out until some other event occured to kick them.
1414 // This way they'll get out immediately
1419 //=====================================================================
1423 Base::serialize(std::ostream
&os
)
1425 // Serialize the PciDev base class
1426 PciDev::serialize(os
);
1428 SERIALIZE_SCALAR(rxEnable
);
1429 SERIALIZE_SCALAR(txEnable
);
1430 SERIALIZE_SCALAR(cpuIntrEnable
);
1433 * Keep track of pending interrupt status.
1435 SERIALIZE_SCALAR(intrTick
);
1436 SERIALIZE_SCALAR(cpuPendingIntr
);
1437 Tick intrEventTick
= 0;
1439 intrEventTick
= intrEvent
->when();
1440 SERIALIZE_SCALAR(intrEventTick
);
1444 Base::unserialize(Checkpoint
*cp
, const std::string
§ion
)
1446 // Unserialize the PciDev base class
1447 PciDev::unserialize(cp
, section
);
1449 UNSERIALIZE_SCALAR(rxEnable
);
1450 UNSERIALIZE_SCALAR(txEnable
);
1451 UNSERIALIZE_SCALAR(cpuIntrEnable
);
1454 * Keep track of pending interrupt status.
1456 UNSERIALIZE_SCALAR(intrTick
);
1457 UNSERIALIZE_SCALAR(cpuPendingIntr
);
1459 UNSERIALIZE_SCALAR(intrEventTick
);
1460 if (intrEventTick
) {
1461 intrEvent
= new IntrEvent(this, true);
1462 schedule(intrEvent
, intrEventTick
);
1467 Device::serialize(std::ostream
&os
)
1471 // Serialize the PciDev base class
1472 Base::serialize(os
);
1474 if (rxState
== rxCopy
)
1475 panic("can't serialize with an in flight dma request rxState=%s",
1476 RxStateStrings
[rxState
]);
1478 if (txState
== txCopy
)
1479 panic("can't serialize with an in flight dma request txState=%s",
1480 TxStateStrings
[txState
]);
1483 * Serialize the device registers that could be modified by the OS.
1485 SERIALIZE_SCALAR(regs
.Config
);
1486 SERIALIZE_SCALAR(regs
.IntrStatus
);
1487 SERIALIZE_SCALAR(regs
.IntrMask
);
1488 SERIALIZE_SCALAR(regs
.RxData
);
1489 SERIALIZE_SCALAR(regs
.TxData
);
1492 * Serialize the virtual nic state
1494 int virtualRegsSize
= virtualRegs
.size();
1495 SERIALIZE_SCALAR(virtualRegsSize
);
1496 for (int i
= 0; i
< virtualRegsSize
; ++i
) {
1497 VirtualReg
*vnic
= &virtualRegs
[i
];
1499 std::string reg
= csprintf("vnic%d", i
);
1500 paramOut(os
, reg
+ ".RxData", vnic
->RxData
);
1501 paramOut(os
, reg
+ ".RxDone", vnic
->RxDone
);
1502 paramOut(os
, reg
+ ".TxData", vnic
->TxData
);
1503 paramOut(os
, reg
+ ".TxDone", vnic
->TxDone
);
1505 bool rxPacketExists
= vnic
->rxIndex
!= rxFifo
.end();
1506 paramOut(os
, reg
+ ".rxPacketExists", rxPacketExists
);
1507 if (rxPacketExists
) {
1509 PacketFifo::iterator i
= rxFifo
.begin();
1510 while (i
!= vnic
->rxIndex
) {
1511 assert(i
!= rxFifo
.end());
1516 paramOut(os
, reg
+ ".rxPacket", rxPacket
);
1517 paramOut(os
, reg
+ ".rxPacketOffset", vnic
->rxPacketOffset
);
1518 paramOut(os
, reg
+ ".rxPacketBytes", vnic
->rxPacketBytes
);
1520 paramOut(os
, reg
+ ".rxDoneData", vnic
->rxDoneData
);
1524 if (this->rxFifoPtr
!= rxFifo
.end())
1525 rxFifoPtr
= rxFifo
.countPacketsBefore(this->rxFifoPtr
);
1526 SERIALIZE_SCALAR(rxFifoPtr
);
1528 SERIALIZE_SCALAR(rxActive
);
1529 SERIALIZE_SCALAR(rxBusyCount
);
1530 SERIALIZE_SCALAR(rxDirtyCount
);
1531 SERIALIZE_SCALAR(rxMappedCount
);
1533 VirtualList::iterator i
, end
;
1534 for (count
= 0, i
= rxList
.begin(), end
= rxList
.end(); i
!= end
; ++i
)
1535 paramOut(os
, csprintf("rxList%d", count
++), *i
);
1536 int rxListSize
= count
;
1537 SERIALIZE_SCALAR(rxListSize
);
1539 for (count
= 0, i
= rxBusy
.begin(), end
= rxBusy
.end(); i
!= end
; ++i
)
1540 paramOut(os
, csprintf("rxBusy%d", count
++), *i
);
1541 int rxBusySize
= count
;
1542 SERIALIZE_SCALAR(rxBusySize
);
1544 for (count
= 0, i
= txList
.begin(), end
= txList
.end(); i
!= end
; ++i
)
1545 paramOut(os
, csprintf("txList%d", count
++), *i
);
1546 int txListSize
= count
;
1547 SERIALIZE_SCALAR(txListSize
);
1550 * Serialize rx state machine
1552 int rxState
= this->rxState
;
1553 SERIALIZE_SCALAR(rxState
);
1554 SERIALIZE_SCALAR(rxEmpty
);
1555 SERIALIZE_SCALAR(rxLow
);
1556 rxFifo
.serialize("rxFifo", os
);
1559 * Serialize tx state machine
1561 int txState
= this->txState
;
1562 SERIALIZE_SCALAR(txState
);
1563 SERIALIZE_SCALAR(txFull
);
1564 txFifo
.serialize("txFifo", os
);
1565 bool txPacketExists
= txPacket
;
1566 SERIALIZE_SCALAR(txPacketExists
);
1567 if (txPacketExists
) {
1568 txPacket
->serialize("txPacket", os
);
1569 SERIALIZE_SCALAR(txPacketOffset
);
1570 SERIALIZE_SCALAR(txPacketBytes
);
1574 * If there's a pending transmit, store the time so we can
1575 * reschedule it later
1577 Tick transmitTick
= txEvent
.scheduled() ? txEvent
.when() - curTick() : 0;
1578 SERIALIZE_SCALAR(transmitTick
);
1582 Device::unserialize(Checkpoint
*cp
, const std::string
§ion
)
1584 // Unserialize the PciDev base class
1585 Base::unserialize(cp
, section
);
1588 * Unserialize the device registers that may have been written by the OS.
1590 UNSERIALIZE_SCALAR(regs
.Config
);
1591 UNSERIALIZE_SCALAR(regs
.IntrStatus
);
1592 UNSERIALIZE_SCALAR(regs
.IntrMask
);
1593 UNSERIALIZE_SCALAR(regs
.RxData
);
1594 UNSERIALIZE_SCALAR(regs
.TxData
);
1596 UNSERIALIZE_SCALAR(rxActive
);
1597 UNSERIALIZE_SCALAR(rxBusyCount
);
1598 UNSERIALIZE_SCALAR(rxDirtyCount
);
1599 UNSERIALIZE_SCALAR(rxMappedCount
);
1602 UNSERIALIZE_SCALAR(rxListSize
);
1604 for (int i
= 0; i
< rxListSize
; ++i
) {
1606 paramIn(cp
, section
, csprintf("rxList%d", i
), value
);
1607 rxList
.push_back(value
);
1611 UNSERIALIZE_SCALAR(rxBusySize
);
1613 for (int i
= 0; i
< rxBusySize
; ++i
) {
1615 paramIn(cp
, section
, csprintf("rxBusy%d", i
), value
);
1616 rxBusy
.push_back(value
);
1620 UNSERIALIZE_SCALAR(txListSize
);
1622 for (int i
= 0; i
< txListSize
; ++i
) {
1624 paramIn(cp
, section
, csprintf("txList%d", i
), value
);
1625 txList
.push_back(value
);
1629 * Unserialize rx state machine
1632 UNSERIALIZE_SCALAR(rxState
);
1633 UNSERIALIZE_SCALAR(rxEmpty
);
1634 UNSERIALIZE_SCALAR(rxLow
);
1635 this->rxState
= (RxState
) rxState
;
1636 rxFifo
.unserialize("rxFifo", cp
, section
);
1639 UNSERIALIZE_SCALAR(rxFifoPtr
);
1640 if (rxFifoPtr
>= 0) {
1641 this->rxFifoPtr
= rxFifo
.begin();
1642 for (int i
= 0; i
< rxFifoPtr
; ++i
)
1645 this->rxFifoPtr
= rxFifo
.end();
1649 * Unserialize tx state machine
1652 UNSERIALIZE_SCALAR(txState
);
1653 UNSERIALIZE_SCALAR(txFull
);
1654 this->txState
= (TxState
) txState
;
1655 txFifo
.unserialize("txFifo", cp
, section
);
1656 bool txPacketExists
;
1657 UNSERIALIZE_SCALAR(txPacketExists
);
1659 if (txPacketExists
) {
1660 txPacket
= new EthPacketData(16384);
1661 txPacket
->unserialize("txPacket", cp
, section
);
1662 UNSERIALIZE_SCALAR(txPacketOffset
);
1663 UNSERIALIZE_SCALAR(txPacketBytes
);
1667 * unserialize the virtual nic registers/state
1669 * this must be done after the unserialization of the rxFifo
1670 * because the packet iterators depend on the fifo being populated
1672 int virtualRegsSize
;
1673 UNSERIALIZE_SCALAR(virtualRegsSize
);
1674 virtualRegs
.clear();
1675 virtualRegs
.resize(virtualRegsSize
);
1676 for (int i
= 0; i
< virtualRegsSize
; ++i
) {
1677 VirtualReg
*vnic
= &virtualRegs
[i
];
1678 std::string reg
= csprintf("vnic%d", i
);
1680 paramIn(cp
, section
, reg
+ ".RxData", vnic
->RxData
);
1681 paramIn(cp
, section
, reg
+ ".RxDone", vnic
->RxDone
);
1682 paramIn(cp
, section
, reg
+ ".TxData", vnic
->TxData
);
1683 paramIn(cp
, section
, reg
+ ".TxDone", vnic
->TxDone
);
1685 vnic
->rxUnique
= rxUnique
++;
1686 vnic
->txUnique
= txUnique
++;
1688 bool rxPacketExists
;
1689 paramIn(cp
, section
, reg
+ ".rxPacketExists", rxPacketExists
);
1690 if (rxPacketExists
) {
1692 paramIn(cp
, section
, reg
+ ".rxPacket", rxPacket
);
1693 vnic
->rxIndex
= rxFifo
.begin();
1697 paramIn(cp
, section
, reg
+ ".rxPacketOffset",
1698 vnic
->rxPacketOffset
);
1699 paramIn(cp
, section
, reg
+ ".rxPacketBytes", vnic
->rxPacketBytes
);
1701 vnic
->rxIndex
= rxFifo
.end();
1703 paramIn(cp
, section
, reg
+ ".rxDoneData", vnic
->rxDoneData
);
1707 * If there's a pending transmit, reschedule it now
1710 UNSERIALIZE_SCALAR(transmitTick
);
1712 schedule(txEvent
, curTick() + transmitTick
);
1714 pioPort
->sendStatusChange(Port::RangeChange
);
1718 } // namespace Sinic
1721 SinicParams::create()
1723 return new Sinic::Device(this);