2 * Copyright (c) 2006 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.
30 * Device model for Intel's 8254x line of gigabit ethernet controllers.
33 #ifndef __DEV_NET_I8254XGBE_HH__
34 #define __DEV_NET_I8254XGBE_HH__
39 #include "base/cp_annotate.hh"
40 #include "base/inet.hh"
41 #include "debug/EthernetDesc.hh"
42 #include "debug/EthernetIntr.hh"
43 #include "dev/net/etherdevice.hh"
44 #include "dev/net/etherint.hh"
45 #include "dev/net/etherpkt.hh"
46 #include "dev/net/i8254xGBe_defs.hh"
47 #include "dev/net/pktfifo.hh"
48 #include "dev/pci/device.hh"
49 #include "params/IGbE.hh"
50 #include "sim/eventq.hh"
54 class IGbE : public EtherDevice
63 // eeprom data, status and control bits
64 int eeOpBits, eeAddrBits, eeDataBits;
65 uint8_t eeOpcode, eeAddr;
66 uint16_t flash[iGbReg::EEPROM_SIZE];
72 // Packet that we are currently putting into the txFifo
73 EthPacketPtr txPacket;
75 // Should to Rx/Tx State machine tick?
83 // Number of bytes copied from current RX packet
86 // Delays in managaging descriptors
87 Tick fetchDelay, wbDelay;
88 Tick fetchCompDelay, wbCompDelay;
89 Tick rxWriteDelay, txReadDelay;
91 // Event and function to deal with RDTR timer expiring
93 rxDescCache.writeback(0);
95 "Posting RXT interrupt because RDTR timer expired\n");
96 postInterrupt(iGbReg::IT_RXT);
99 EventFunctionWrapper rdtrEvent;
101 // Event and function to deal with RADV timer expiring
103 rxDescCache.writeback(0);
104 DPRINTF(EthernetIntr,
105 "Posting RXT interrupt because RADV timer expired\n");
106 postInterrupt(iGbReg::IT_RXT);
109 EventFunctionWrapper radvEvent;
111 // Event and function to deal with TADV timer expiring
113 txDescCache.writeback(0);
114 DPRINTF(EthernetIntr,
115 "Posting TXDW interrupt because TADV timer expired\n");
116 postInterrupt(iGbReg::IT_TXDW);
119 EventFunctionWrapper tadvEvent;
121 // Event and function to deal with TIDV timer expiring
123 txDescCache.writeback(0);
124 DPRINTF(EthernetIntr,
125 "Posting TXDW interrupt because TIDV timer expired\n");
126 postInterrupt(iGbReg::IT_TXDW);
128 EventFunctionWrapper tidvEvent;
130 // Main event to tick the device
132 EventFunctionWrapper tickEvent;
137 void rxStateMachine();
138 void txStateMachine();
141 /** Write an interrupt into the interrupt pending register and check mask
142 * and interrupt limit timer before sending interrupt to CPU
143 * @param t the type of interrupt we are posting
144 * @param now should we ignore the interrupt limiting timer
146 void postInterrupt(iGbReg::IntTypes t, bool now = false);
148 /** Check and see if changes to the mask register have caused an interrupt
149 * to need to be sent or perhaps removed an interrupt cause.
153 /** Send an interrupt to the cpu
155 void delayIntEvent();
157 // Event to moderate interrupts
158 EventFunctionWrapper interEvent;
160 /** Clear the interupt line to the cpu
164 Tick intClock() { return SimClock::Int::ns * 1024; }
166 /** This function is used to restart the clock so it can handle things like
167 * draining and resume in one place. */
170 /** Check if all the draining things that need to occur have occured and
171 * handle the drain event if so.
175 void anBegin(std::string sm, std::string st, int flags = CPA::FL_NONE) {
177 cpa->hwBegin((CPA::flags)flags, sys, macAddr, sm, st);
180 void anQ(std::string sm, std::string q) {
182 cpa->hwQ(CPA::FL_NONE, sys, macAddr, sm, q, macAddr);
185 void anDq(std::string sm, std::string q) {
187 cpa->hwDq(CPA::FL_NONE, sys, macAddr, sm, q, macAddr);
190 void anPq(std::string sm, std::string q, int num = 1) {
192 cpa->hwPq(CPA::FL_NONE, sys, macAddr, sm, q, macAddr, NULL, num);
195 void anRq(std::string sm, std::string q, int num = 1) {
197 cpa->hwRq(CPA::FL_NONE, sys, macAddr, sm, q, macAddr, NULL, num);
200 void anWe(std::string sm, std::string q) {
202 cpa->hwWe(CPA::FL_NONE, sys, macAddr, sm, q, macAddr);
205 void anWf(std::string sm, std::string q) {
207 cpa->hwWf(CPA::FL_NONE, sys, macAddr, sm, q, macAddr);
212 class DescCache : public Serializable
215 virtual Addr descBase() const = 0;
216 virtual long descHead() const = 0;
217 virtual long descTail() const = 0;
218 virtual long descLen() const = 0;
219 virtual void updateHead(long h) = 0;
220 virtual void enableSm() = 0;
221 virtual void actionAfterWb() {}
222 virtual void fetchAfterWb() = 0;
224 typedef std::deque<T *> CacheType;
226 CacheType unusedCache;
231 // Pointer to the device we cache for
234 // Name of this descriptor cache
237 // How far we've cached
240 // The size of the descriptor cache
243 // How many descriptors we are currently fetching
246 // How many descriptors we are currently writing back
249 // if the we wrote back to the end of the descriptor ring and are going
250 // to have to wrap and write more
253 // What the alignment is of the next descriptor writeback
256 /** The packet that is currently being dmad to memory if any */
259 /** Shortcut for DMA address translation */
260 Addr pciToDma(Addr a) { return igbe->pciToDma(a); }
264 std::string annSmFetch, annSmWb, annUnusedDescQ, annUsedCacheQ,
265 annUsedDescQ, annUnusedCacheQ, annDescQ;
267 DescCache(IGbE *i, const std::string n, int s);
268 virtual ~DescCache();
270 std::string name() { return _name; }
272 /** If the address/len/head change when we've got descriptors that are
273 * dirty that is very bad. This function checks that we don't and if we
278 void writeback(Addr aMask);
280 EventFunctionWrapper wbDelayEvent;
282 /** Fetch a chunk of descriptors into the descriptor cache.
283 * Calls fetchComplete when the memory system returns the data
285 void fetchDescriptors();
286 void fetchDescriptors1();
287 EventFunctionWrapper fetchDelayEvent;
289 /** Called by event when dma to read descriptors is completed
291 void fetchComplete();
292 EventFunctionWrapper fetchEvent;
294 /** Called by event when dma to writeback descriptors is completed
297 EventFunctionWrapper wbEvent;
299 /* Return the number of descriptors left in the ring, so the device has
300 * a way to figure out if it needs to interrupt.
305 unsigned left = unusedCache.size();
306 if (cachePnt > descTail())
307 left += (descLen() - cachePnt + descTail());
309 left += (descTail() - cachePnt);
314 /* Return the number of descriptors used and not written back.
316 unsigned descUsed() const { return usedCache.size(); }
318 /* Return the number of cache unused descriptors we have. */
319 unsigned descUnused() const { return unusedCache.size(); }
321 /* Get into a state where the descriptor address/head/etc colud be
326 void serialize(CheckpointOut &cp) const override;
327 void unserialize(CheckpointIn &cp) override;
329 virtual bool hasOutstandingEvents() {
330 return wbEvent.scheduled() || fetchEvent.scheduled();
336 class RxDescCache : public DescCache<iGbReg::RxDesc>
339 Addr descBase() const override { return igbe->regs.rdba(); }
340 long descHead() const override { return igbe->regs.rdh(); }
341 long descLen() const override { return igbe->regs.rdlen() >> 4; }
342 long descTail() const override { return igbe->regs.rdt(); }
343 void updateHead(long h) override { igbe->regs.rdh(h); }
344 void enableSm() override;
345 void fetchAfterWb() override {
346 if (!igbe->rxTick && igbe->drainState() == DrainState::Running)
352 /** Variable to head with header/data completion events */
355 /** Bytes of packet that have been copied, so we know when to
357 unsigned bytesCopied;
360 RxDescCache(IGbE *i, std::string n, int s);
362 /** Write the given packet into the buffer(s) pointed to by the
363 * descriptor and update the book keeping. Should only be called when
364 * there are no dma's pending.
365 * @param packet ethernet packet to write
366 * @param pkt_offset bytes already copied from the packet to memory
367 * @return pkt_offset + number of bytes copied during this call
369 int writePacket(EthPacketPtr packet, int pkt_offset);
371 /** Called by event when dma to write packet is completed
375 /** Check if the dma on the packet has completed and RX state machine
380 EventFunctionWrapper pktEvent;
382 // Event to handle issuing header and data write at the same time
383 // and only callking pktComplete() when both are completed
385 EventFunctionWrapper pktHdrEvent;
386 EventFunctionWrapper pktDataEvent;
388 bool hasOutstandingEvents() override;
390 void serialize(CheckpointOut &cp) const override;
391 void unserialize(CheckpointIn &cp) override;
393 friend class RxDescCache;
395 RxDescCache rxDescCache;
397 class TxDescCache : public DescCache<iGbReg::TxDesc>
400 Addr descBase() const override { return igbe->regs.tdba(); }
401 long descHead() const override { return igbe->regs.tdh(); }
402 long descTail() const override { return igbe->regs.tdt(); }
403 long descLen() const override { return igbe->regs.tdlen() >> 4; }
404 void updateHead(long h) override { igbe->regs.tdh(h); }
405 void enableSm() override;
406 void actionAfterWb() override;
407 void fetchAfterWb() override {
408 if (!igbe->txTick && igbe->drainState() == DrainState::Running)
418 Addr completionAddress;
419 bool completionEnabled;
430 Addr tsoPktPayloadBytes;
431 bool tsoLoadedHeader;
432 bool tsoPktHasHeader;
433 uint8_t tsoHeader[256];
434 Addr tsoDescBytesUsed;
439 TxDescCache(IGbE *i, std::string n, int s);
441 /** Tell the cache to DMA a packet from main memory into its buffer and
442 * return the size the of the packet to reserve space in tx fifo.
443 * @return size of the packet
445 unsigned getPacketSize(EthPacketPtr p);
446 void getPacketData(EthPacketPtr p);
447 void processContextDesc();
449 /** Return the number of dsecriptors in a cache block for threshold
453 descInBlock(unsigned num_desc)
455 return num_desc / igbe->cacheBlockSize() / sizeof(iGbReg::TxDesc);
458 /** Ask if the packet has been transfered so the state machine can give
460 * @return packet available in descriptor cache
462 bool packetAvailable();
464 /** Ask if we are still waiting for the packet to be transfered.
465 * @return packet still in transit.
467 bool packetWaiting() { return pktWaiting; }
469 /** Ask if this packet is composed of multiple descriptors
470 * so even if we've got data, we need to wait for more before
471 * we can send it out.
472 * @return packet can't be sent out because it's a multi-descriptor
475 bool packetMultiDesc() { return pktMultiDesc;}
477 /** Called by event when dma to write packet is completed
480 EventFunctionWrapper pktEvent;
482 void headerComplete();
483 EventFunctionWrapper headerEvent;
486 void completionWriteback(Addr a, bool enabled) {
487 DPRINTF(EthernetDesc,
488 "Completion writeback Addr: %#x enabled: %d\n",
490 completionAddress = a;
491 completionEnabled = enabled;
494 bool hasOutstandingEvents() override;
496 void nullCallback() {
497 DPRINTF(EthernetDesc, "Completion writeback complete\n");
499 EventFunctionWrapper nullEvent;
501 void serialize(CheckpointOut &cp) const override;
502 void unserialize(CheckpointIn &cp) override;
505 friend class TxDescCache;
507 TxDescCache txDescCache;
510 typedef IGbEParams Params;
513 return dynamic_cast<const Params *>(_params);
516 IGbE(const Params *params);
518 void init() override;
520 Port &getPort(const std::string &if_name,
521 PortID idx=InvalidPortID) override;
525 Tick read(PacketPtr pkt) override;
526 Tick write(PacketPtr pkt) override;
528 Tick writeConfig(PacketPtr pkt) override;
530 bool ethRxPkt(EthPacketPtr packet);
533 void serialize(CheckpointOut &cp) const override;
534 void unserialize(CheckpointIn &cp) override;
536 DrainState drain() override;
537 void drainResume() override;
541 class IGbEInt : public EtherInt
547 IGbEInt(const std::string &name, IGbE *d)
548 : EtherInt(name), dev(d)
551 virtual bool recvPacket(EthPacketPtr pkt) { return dev->ethRxPkt(pkt); }
552 virtual void sendDone() { dev->ethTxDone(); }
555 #endif //__DEV_NET_I8254XGBE_HH__