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.
32 * Device model for Intel's 8254x line of gigabit ethernet controllers.
35 #ifndef __DEV_I8254XGBE_HH__
36 #define __DEV_I8254XGBE_HH__
41 #include "base/cp_annotate.hh"
42 #include "base/inet.hh"
43 #include "debug/EthernetDesc.hh"
44 #include "debug/EthernetIntr.hh"
45 #include "dev/etherdevice.hh"
46 #include "dev/etherint.hh"
47 #include "dev/etherpkt.hh"
48 #include "dev/i8254xGBe_defs.hh"
49 #include "dev/pcidev.hh"
50 #include "dev/pktfifo.hh"
51 #include "params/IGbE.hh"
52 #include "sim/eventq.hh"
56 class IGbE : public EtherDevice
65 // eeprom data, status and control bits
66 int eeOpBits, eeAddrBits, eeDataBits;
67 uint8_t eeOpcode, eeAddr;
68 uint16_t flash[iGbReg::EEPROM_SIZE];
70 // The drain event if we have one
71 DrainManager *drainManager;
77 // Packet that we are currently putting into the txFifo
78 EthPacketPtr txPacket;
80 // Should to Rx/Tx State machine tick?
87 // Number of bytes copied from current RX packet
90 // Delays in managaging descriptors
91 Tick fetchDelay, wbDelay;
92 Tick fetchCompDelay, wbCompDelay;
93 Tick rxWriteDelay, txReadDelay;
95 // Event and function to deal with RDTR timer expiring
97 rxDescCache.writeback(0);
99 "Posting RXT interrupt because RDTR timer expired\n");
100 postInterrupt(iGbReg::IT_RXT);
103 //friend class EventWrapper<IGbE, &IGbE::rdtrProcess>;
104 EventWrapper<IGbE, &IGbE::rdtrProcess> rdtrEvent;
106 // Event and function to deal with RADV timer expiring
108 rxDescCache.writeback(0);
109 DPRINTF(EthernetIntr,
110 "Posting RXT interrupt because RADV timer expired\n");
111 postInterrupt(iGbReg::IT_RXT);
114 //friend class EventWrapper<IGbE, &IGbE::radvProcess>;
115 EventWrapper<IGbE, &IGbE::radvProcess> radvEvent;
117 // Event and function to deal with TADV timer expiring
119 txDescCache.writeback(0);
120 DPRINTF(EthernetIntr,
121 "Posting TXDW interrupt because TADV timer expired\n");
122 postInterrupt(iGbReg::IT_TXDW);
125 //friend class EventWrapper<IGbE, &IGbE::tadvProcess>;
126 EventWrapper<IGbE, &IGbE::tadvProcess> tadvEvent;
128 // Event and function to deal with TIDV timer expiring
130 txDescCache.writeback(0);
131 DPRINTF(EthernetIntr,
132 "Posting TXDW interrupt because TIDV timer expired\n");
133 postInterrupt(iGbReg::IT_TXDW);
135 //friend class EventWrapper<IGbE, &IGbE::tidvProcess>;
136 EventWrapper<IGbE, &IGbE::tidvProcess> tidvEvent;
138 // Main event to tick the device
140 //friend class EventWrapper<IGbE, &IGbE::tick>;
141 EventWrapper<IGbE, &IGbE::tick> tickEvent;
146 void rxStateMachine();
147 void txStateMachine();
150 /** Write an interrupt into the interrupt pending register and check mask
151 * and interrupt limit timer before sending interrupt to CPU
152 * @param t the type of interrupt we are posting
153 * @param now should we ignore the interrupt limiting timer
155 void postInterrupt(iGbReg::IntTypes t, bool now = false);
157 /** Check and see if changes to the mask register have caused an interrupt
158 * to need to be sent or perhaps removed an interrupt cause.
162 /** Send an interrupt to the cpu
164 void delayIntEvent();
166 // Event to moderate interrupts
167 EventWrapper<IGbE, &IGbE::delayIntEvent> interEvent;
169 /** Clear the interupt line to the cpu
173 Tick intClock() { return SimClock::Int::ns * 1024; }
175 /** This function is used to restart the clock so it can handle things like
176 * draining and resume in one place. */
179 /** Check if all the draining things that need to occur have occured and
180 * handle the drain event if so.
184 void anBegin(std::string sm, std::string st, int flags = CPA::FL_NONE) {
185 cpa->hwBegin((CPA::flags)flags, sys, macAddr, sm, st);
188 void anQ(std::string sm, std::string q) {
189 cpa->hwQ(CPA::FL_NONE, sys, macAddr, sm, q, macAddr);
192 void anDq(std::string sm, std::string q) {
193 cpa->hwDq(CPA::FL_NONE, sys, macAddr, sm, q, macAddr);
196 void anPq(std::string sm, std::string q, int num = 1) {
197 cpa->hwPq(CPA::FL_NONE, sys, macAddr, sm, q, macAddr, NULL, num);
200 void anRq(std::string sm, std::string q, int num = 1) {
201 cpa->hwRq(CPA::FL_NONE, sys, macAddr, sm, q, macAddr, NULL, num);
204 void anWe(std::string sm, std::string q) {
205 cpa->hwWe(CPA::FL_NONE, sys, macAddr, sm, q, macAddr);
208 void anWf(std::string sm, std::string q) {
209 cpa->hwWf(CPA::FL_NONE, sys, macAddr, sm, q, macAddr);
217 virtual Addr descBase() const = 0;
218 virtual long descHead() const = 0;
219 virtual long descTail() const = 0;
220 virtual long descLen() const = 0;
221 virtual void updateHead(long h) = 0;
222 virtual void enableSm() = 0;
223 virtual void actionAfterWb() {}
224 virtual void fetchAfterWb() = 0;
226 typedef std::deque<T *> CacheType;
228 CacheType unusedCache;
233 // Pointer to the device we cache for
236 // Name of this descriptor cache
239 // How far we've cached
242 // The size of the descriptor cache
245 // How many descriptors we are currently fetching
248 // How many descriptors we are currently writing back
251 // if the we wrote back to the end of the descriptor ring and are going
252 // to have to wrap and write more
255 // What the alignment is of the next descriptor writeback
258 /** The packet that is currently being dmad to memory if any */
261 /** Shortcut for DMA address translation */
262 Addr pciToDma(Addr a) { return igbe->platform->pciToDma(a); }
266 std::string annSmFetch, annSmWb, annUnusedDescQ, annUsedCacheQ,
267 annUsedDescQ, annUnusedCacheQ, annDescQ;
269 DescCache(IGbE *i, const std::string n, int s);
270 virtual ~DescCache();
272 std::string name() { return _name; }
274 /** If the address/len/head change when we've got descriptors that are
275 * dirty that is very bad. This function checks that we don't and if we
280 void writeback(Addr aMask);
282 EventWrapper<DescCache, &DescCache::writeback1> wbDelayEvent;
284 /** Fetch a chunk of descriptors into the descriptor cache.
285 * Calls fetchComplete when the memory system returns the data
287 void fetchDescriptors();
288 void fetchDescriptors1();
289 EventWrapper<DescCache, &DescCache::fetchDescriptors1> fetchDelayEvent;
291 /** Called by event when dma to read descriptors is completed
293 void fetchComplete();
294 EventWrapper<DescCache, &DescCache::fetchComplete> fetchEvent;
296 /** Called by event when dma to writeback descriptors is completed
299 EventWrapper<DescCache, &DescCache::wbComplete> wbEvent;
301 /* Return the number of descriptors left in the ring, so the device has
302 * a way to figure out if it needs to interrupt.
307 unsigned left = unusedCache.size();
308 if (cachePnt > descTail())
309 left += (descLen() - cachePnt + descTail());
311 left += (descTail() - cachePnt);
316 /* Return the number of descriptors used and not written back.
318 unsigned descUsed() const { return usedCache.size(); }
320 /* Return the number of cache unused descriptors we have. */
321 unsigned descUnused() const { return unusedCache.size(); }
323 /* Get into a state where the descriptor address/head/etc colud be
327 virtual void serialize(std::ostream &os);
328 virtual void unserialize(Checkpoint *cp, const std::string §ion);
330 virtual bool hasOutstandingEvents() {
331 return wbEvent.scheduled() || fetchEvent.scheduled();
337 class RxDescCache : public DescCache<iGbReg::RxDesc>
340 virtual Addr descBase() const { return igbe->regs.rdba(); }
341 virtual long descHead() const { return igbe->regs.rdh(); }
342 virtual long descLen() const { return igbe->regs.rdlen() >> 4; }
343 virtual long descTail() const { return igbe->regs.rdt(); }
344 virtual void updateHead(long h) { igbe->regs.rdh(h); }
345 virtual void enableSm();
346 virtual void fetchAfterWb() {
347 if (!igbe->rxTick && igbe->getDrainState() == Drainable::Running)
353 /** Variable to head with header/data completion events */
356 /** Bytes of packet that have been copied, so we know when to
358 unsigned bytesCopied;
361 RxDescCache(IGbE *i, std::string n, int s);
363 /** Write the given packet into the buffer(s) pointed to by the
364 * descriptor and update the book keeping. Should only be called when
365 * there are no dma's pending.
366 * @param packet ethernet packet to write
367 * @param pkt_offset bytes already copied from the packet to memory
368 * @return pkt_offset + number of bytes copied during this call
370 int writePacket(EthPacketPtr packet, int pkt_offset);
372 /** Called by event when dma to write packet is completed
376 /** Check if the dma on the packet has completed and RX state machine
381 EventWrapper<RxDescCache, &RxDescCache::pktComplete> pktEvent;
383 // Event to handle issuing header and data write at the same time
384 // and only callking pktComplete() when both are completed
386 EventWrapper<RxDescCache, &RxDescCache::pktSplitDone> pktHdrEvent;
387 EventWrapper<RxDescCache, &RxDescCache::pktSplitDone> pktDataEvent;
389 virtual bool hasOutstandingEvents();
391 virtual void serialize(std::ostream &os);
392 virtual void unserialize(Checkpoint *cp, const std::string §ion);
394 friend class RxDescCache;
396 RxDescCache rxDescCache;
398 class TxDescCache : public DescCache<iGbReg::TxDesc>
401 virtual Addr descBase() const { return igbe->regs.tdba(); }
402 virtual long descHead() const { return igbe->regs.tdh(); }
403 virtual long descTail() const { return igbe->regs.tdt(); }
404 virtual long descLen() const { return igbe->regs.tdlen() >> 4; }
405 virtual void updateHead(long h) { igbe->regs.tdh(h); }
406 virtual void enableSm();
407 virtual void actionAfterWb();
408 virtual void fetchAfterWb() {
409 if (!igbe->txTick && igbe->getDrainState() == Drainable::Running)
419 Addr completionAddress;
420 bool completionEnabled;
431 Addr tsoPktPayloadBytes;
432 bool tsoLoadedHeader;
433 bool tsoPktHasHeader;
434 uint8_t tsoHeader[256];
435 Addr tsoDescBytesUsed;
440 TxDescCache(IGbE *i, std::string n, int s);
442 /** Tell the cache to DMA a packet from main memory into its buffer and
443 * return the size the of the packet to reserve space in tx fifo.
444 * @return size of the packet
446 unsigned getPacketSize(EthPacketPtr p);
447 void getPacketData(EthPacketPtr p);
448 void processContextDesc();
450 /** Return the number of dsecriptors in a cache block for threshold
454 descInBlock(unsigned num_desc)
456 return num_desc / igbe->cacheBlockSize() / sizeof(iGbReg::TxDesc);
459 /** Ask if the packet has been transfered so the state machine can give
461 * @return packet available in descriptor cache
463 bool packetAvailable();
465 /** Ask if we are still waiting for the packet to be transfered.
466 * @return packet still in transit.
468 bool packetWaiting() { return pktWaiting; }
470 /** Ask if this packet is composed of multiple descriptors
471 * so even if we've got data, we need to wait for more before
472 * we can send it out.
473 * @return packet can't be sent out because it's a multi-descriptor
476 bool packetMultiDesc() { return pktMultiDesc;}
478 /** Called by event when dma to write packet is completed
481 EventWrapper<TxDescCache, &TxDescCache::pktComplete> pktEvent;
483 void headerComplete();
484 EventWrapper<TxDescCache, &TxDescCache::headerComplete> headerEvent;
487 void completionWriteback(Addr a, bool enabled) {
488 DPRINTF(EthernetDesc,
489 "Completion writeback Addr: %#x enabled: %d\n",
491 completionAddress = a;
492 completionEnabled = enabled;
495 virtual bool hasOutstandingEvents();
497 void nullCallback() {
498 DPRINTF(EthernetDesc, "Completion writeback complete\n");
500 EventWrapper<TxDescCache, &TxDescCache::nullCallback> nullEvent;
502 virtual void serialize(std::ostream &os);
503 virtual void unserialize(Checkpoint *cp, const std::string §ion);
506 friend class TxDescCache;
508 TxDescCache txDescCache;
511 typedef IGbEParams Params;
514 return dynamic_cast<const Params *>(_params);
517 IGbE(const Params *params);
521 virtual EtherInt *getEthPort(const std::string &if_name, int idx);
525 virtual Tick read(PacketPtr pkt);
526 virtual Tick write(PacketPtr pkt);
528 virtual Tick writeConfig(PacketPtr pkt);
530 bool ethRxPkt(EthPacketPtr packet);
533 virtual void serialize(std::ostream &os);
534 virtual void unserialize(Checkpoint *cp, const std::string §ion);
536 unsigned int drain(DrainManager *dm);
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_I8254XGBE_HH__