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
73 // cached parameters from params struct
80 // Packet that we are currently putting into the txFifo
81 EthPacketPtr txPacket;
83 // Should to Rx/Tx State machine tick?
90 // Number of bytes copied from current RX packet
93 // Delays in managaging descriptors
94 Tick fetchDelay, wbDelay;
95 Tick fetchCompDelay, wbCompDelay;
96 Tick rxWriteDelay, txReadDelay;
98 // Event and function to deal with RDTR timer expiring
100 rxDescCache.writeback(0);
101 DPRINTF(EthernetIntr,
102 "Posting RXT interrupt because RDTR timer expired\n");
103 postInterrupt(iGbReg::IT_RXT);
106 //friend class EventWrapper<IGbE, &IGbE::rdtrProcess>;
107 EventWrapper<IGbE, &IGbE::rdtrProcess> rdtrEvent;
109 // Event and function to deal with RADV timer expiring
111 rxDescCache.writeback(0);
112 DPRINTF(EthernetIntr,
113 "Posting RXT interrupt because RADV timer expired\n");
114 postInterrupt(iGbReg::IT_RXT);
117 //friend class EventWrapper<IGbE, &IGbE::radvProcess>;
118 EventWrapper<IGbE, &IGbE::radvProcess> radvEvent;
120 // Event and function to deal with TADV timer expiring
122 txDescCache.writeback(0);
123 DPRINTF(EthernetIntr,
124 "Posting TXDW interrupt because TADV timer expired\n");
125 postInterrupt(iGbReg::IT_TXDW);
128 //friend class EventWrapper<IGbE, &IGbE::tadvProcess>;
129 EventWrapper<IGbE, &IGbE::tadvProcess> tadvEvent;
131 // Event and function to deal with TIDV timer expiring
133 txDescCache.writeback(0);
134 DPRINTF(EthernetIntr,
135 "Posting TXDW interrupt because TIDV timer expired\n");
136 postInterrupt(iGbReg::IT_TXDW);
138 //friend class EventWrapper<IGbE, &IGbE::tidvProcess>;
139 EventWrapper<IGbE, &IGbE::tidvProcess> tidvEvent;
141 // Main event to tick the device
143 //friend class EventWrapper<IGbE, &IGbE::tick>;
144 EventWrapper<IGbE, &IGbE::tick> tickEvent;
149 void rxStateMachine();
150 void txStateMachine();
153 /** Write an interrupt into the interrupt pending register and check mask
154 * and interrupt limit timer before sending interrupt to CPU
155 * @param t the type of interrupt we are posting
156 * @param now should we ignore the interrupt limiting timer
158 void postInterrupt(iGbReg::IntTypes t, bool now = false);
160 /** Check and see if changes to the mask register have caused an interrupt
161 * to need to be sent or perhaps removed an interrupt cause.
165 /** Send an interrupt to the cpu
167 void delayIntEvent();
169 // Event to moderate interrupts
170 EventWrapper<IGbE, &IGbE::delayIntEvent> interEvent;
172 /** Clear the interupt line to the cpu
176 Tick intClock() { return SimClock::Int::ns * 1024; }
178 /** This function is used to restart the clock so it can handle things like
179 * draining and resume in one place. */
182 /** Check if all the draining things that need to occur have occured and
183 * handle the drain event if so.
187 void anBegin(std::string sm, std::string st, int flags = CPA::FL_NONE) {
188 cpa->hwBegin((CPA::flags)flags, sys, macAddr, sm, st);
191 void anQ(std::string sm, std::string q) {
192 cpa->hwQ(CPA::FL_NONE, sys, macAddr, sm, q, macAddr);
195 void anDq(std::string sm, std::string q) {
196 cpa->hwDq(CPA::FL_NONE, sys, macAddr, sm, q, macAddr);
199 void anPq(std::string sm, std::string q, int num = 1) {
200 cpa->hwPq(CPA::FL_NONE, sys, macAddr, sm, q, macAddr, NULL, num);
203 void anRq(std::string sm, std::string q, int num = 1) {
204 cpa->hwRq(CPA::FL_NONE, sys, macAddr, sm, q, macAddr, NULL, num);
207 void anWe(std::string sm, std::string q) {
208 cpa->hwWe(CPA::FL_NONE, sys, macAddr, sm, q, macAddr);
211 void anWf(std::string sm, std::string q) {
212 cpa->hwWf(CPA::FL_NONE, sys, macAddr, sm, q, macAddr);
220 virtual Addr descBase() const = 0;
221 virtual long descHead() const = 0;
222 virtual long descTail() const = 0;
223 virtual long descLen() const = 0;
224 virtual void updateHead(long h) = 0;
225 virtual void enableSm() = 0;
226 virtual void actionAfterWb() {}
227 virtual void fetchAfterWb() = 0;
229 typedef std::deque<T *> CacheType;
231 CacheType unusedCache;
236 // Pointer to the device we cache for
239 // Name of this descriptor cache
242 // How far we've cached
245 // The size of the descriptor cache
248 // How many descriptors we are currently fetching
251 // How many descriptors we are currently writing back
254 // if the we wrote back to the end of the descriptor ring and are going
255 // to have to wrap and write more
258 // What the alignment is of the next descriptor writeback
261 /** The packet that is currently being dmad to memory if any */
264 /** Shortcut for DMA address translation */
265 Addr pciToDma(Addr a) { return igbe->platform->pciToDma(a); }
269 std::string annSmFetch, annSmWb, annUnusedDescQ, annUsedCacheQ,
270 annUsedDescQ, annUnusedCacheQ, annDescQ;
272 DescCache(IGbE *i, const std::string n, int s);
273 virtual ~DescCache();
275 std::string name() { return _name; }
277 /** If the address/len/head change when we've got descriptors that are
278 * dirty that is very bad. This function checks that we don't and if we
283 void writeback(Addr aMask);
285 EventWrapper<DescCache, &DescCache::writeback1> wbDelayEvent;
287 /** Fetch a chunk of descriptors into the descriptor cache.
288 * Calls fetchComplete when the memory system returns the data
290 void fetchDescriptors();
291 void fetchDescriptors1();
292 EventWrapper<DescCache, &DescCache::fetchDescriptors1> fetchDelayEvent;
294 /** Called by event when dma to read descriptors is completed
296 void fetchComplete();
297 EventWrapper<DescCache, &DescCache::fetchComplete> fetchEvent;
299 /** Called by event when dma to writeback descriptors is completed
302 EventWrapper<DescCache, &DescCache::wbComplete> wbEvent;
304 /* Return the number of descriptors left in the ring, so the device has
305 * a way to figure out if it needs to interrupt.
310 unsigned left = unusedCache.size();
311 if (cachePnt > descTail())
312 left += (descLen() - cachePnt + descTail());
314 left += (descTail() - cachePnt);
319 /* Return the number of descriptors used and not written back.
321 unsigned descUsed() const { return usedCache.size(); }
323 /* Return the number of cache unused descriptors we have. */
324 unsigned descUnused() const { return unusedCache.size(); }
326 /* Get into a state where the descriptor address/head/etc colud be
330 virtual void serialize(std::ostream &os);
331 virtual void unserialize(Checkpoint *cp, const std::string §ion);
333 virtual bool hasOutstandingEvents() {
334 return wbEvent.scheduled() || fetchEvent.scheduled();
340 class RxDescCache : public DescCache<iGbReg::RxDesc>
343 virtual Addr descBase() const { return igbe->regs.rdba(); }
344 virtual long descHead() const { return igbe->regs.rdh(); }
345 virtual long descLen() const { return igbe->regs.rdlen() >> 4; }
346 virtual long descTail() const { return igbe->regs.rdt(); }
347 virtual void updateHead(long h) { igbe->regs.rdh(h); }
348 virtual void enableSm();
349 virtual void fetchAfterWb() {
350 if (!igbe->rxTick && igbe->getState() == SimObject::Running)
356 /** Variable to head with header/data completion events */
359 /** Bytes of packet that have been copied, so we know when to
361 unsigned bytesCopied;
364 RxDescCache(IGbE *i, std::string n, int s);
366 /** Write the given packet into the buffer(s) pointed to by the
367 * descriptor and update the book keeping. Should only be called when
368 * there are no dma's pending.
369 * @param packet ethernet packet to write
370 * @param pkt_offset bytes already copied from the packet to memory
371 * @return pkt_offset + number of bytes copied during this call
373 int writePacket(EthPacketPtr packet, int pkt_offset);
375 /** Called by event when dma to write packet is completed
379 /** Check if the dma on the packet has completed and RX state machine
384 EventWrapper<RxDescCache, &RxDescCache::pktComplete> pktEvent;
386 // Event to handle issuing header and data write at the same time
387 // and only callking pktComplete() when both are completed
389 EventWrapper<RxDescCache, &RxDescCache::pktSplitDone> pktHdrEvent;
390 EventWrapper<RxDescCache, &RxDescCache::pktSplitDone> pktDataEvent;
392 virtual bool hasOutstandingEvents();
394 virtual void serialize(std::ostream &os);
395 virtual void unserialize(Checkpoint *cp, const std::string §ion);
397 friend class RxDescCache;
399 RxDescCache rxDescCache;
401 class TxDescCache : public DescCache<iGbReg::TxDesc>
404 virtual Addr descBase() const { return igbe->regs.tdba(); }
405 virtual long descHead() const { return igbe->regs.tdh(); }
406 virtual long descTail() const { return igbe->regs.tdt(); }
407 virtual long descLen() const { return igbe->regs.tdlen() >> 4; }
408 virtual void updateHead(long h) { igbe->regs.tdh(h); }
409 virtual void enableSm();
410 virtual void actionAfterWb();
411 virtual void fetchAfterWb() {
412 if (!igbe->txTick && igbe->getState() == SimObject::Running)
422 Addr completionAddress;
423 bool completionEnabled;
434 Addr tsoPktPayloadBytes;
435 bool tsoLoadedHeader;
436 bool tsoPktHasHeader;
437 uint8_t tsoHeader[256];
438 Addr tsoDescBytesUsed;
443 TxDescCache(IGbE *i, std::string n, int s);
445 /** Tell the cache to DMA a packet from main memory into its buffer and
446 * return the size the of the packet to reserve space in tx fifo.
447 * @return size of the packet
449 unsigned getPacketSize(EthPacketPtr p);
450 void getPacketData(EthPacketPtr p);
451 void processContextDesc();
453 /** Return the number of dsecriptors in a cache block for threshold
457 descInBlock(unsigned num_desc)
459 return num_desc / igbe->cacheBlockSize() / sizeof(iGbReg::TxDesc);
462 /** Ask if the packet has been transfered so the state machine can give
464 * @return packet available in descriptor cache
466 bool packetAvailable();
468 /** Ask if we are still waiting for the packet to be transfered.
469 * @return packet still in transit.
471 bool packetWaiting() { return pktWaiting; }
473 /** Ask if this packet is composed of multiple descriptors
474 * so even if we've got data, we need to wait for more before
475 * we can send it out.
476 * @return packet can't be sent out because it's a multi-descriptor
479 bool packetMultiDesc() { return pktMultiDesc;}
481 /** Called by event when dma to write packet is completed
484 EventWrapper<TxDescCache, &TxDescCache::pktComplete> pktEvent;
486 void headerComplete();
487 EventWrapper<TxDescCache, &TxDescCache::headerComplete> headerEvent;
490 void completionWriteback(Addr a, bool enabled) {
491 DPRINTF(EthernetDesc,
492 "Completion writeback Addr: %#x enabled: %d\n",
494 completionAddress = a;
495 completionEnabled = enabled;
498 virtual bool hasOutstandingEvents();
500 void nullCallback() {
501 DPRINTF(EthernetDesc, "Completion writeback complete\n");
503 EventWrapper<TxDescCache, &TxDescCache::nullCallback> nullEvent;
505 virtual void serialize(std::ostream &os);
506 virtual void unserialize(Checkpoint *cp, const std::string §ion);
509 friend class TxDescCache;
511 TxDescCache txDescCache;
514 typedef IGbEParams Params;
517 return dynamic_cast<const Params *>(_params);
520 IGbE(const Params *params);
524 virtual EtherInt *getEthPort(const std::string &if_name, int idx);
528 inline Tick ticks(int numCycles) const { return numCycles * clock; }
530 virtual Tick read(PacketPtr pkt);
531 virtual Tick write(PacketPtr pkt);
533 virtual Tick writeConfig(PacketPtr pkt);
535 bool ethRxPkt(EthPacketPtr packet);
538 virtual void serialize(std::ostream &os);
539 virtual void unserialize(Checkpoint *cp, const std::string §ion);
540 virtual unsigned int drain(Event *de);
541 virtual void resume();
545 class IGbEInt : public EtherInt
551 IGbEInt(const std::string &name, IGbE *d)
552 : EtherInt(name), dev(d)
555 virtual bool recvPacket(EthPacketPtr pkt) { return dev->ethRxPkt(pkt); }
556 virtual void sendDone() { dev->ethTxDone(); }
559 #endif //__DEV_I8254XGBE_HH__