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/inet.hh"
42 #include "dev/etherdevice.hh"
43 #include "dev/etherint.hh"
44 #include "dev/etherpkt.hh"
45 #include "dev/i8254xGBe_defs.hh"
46 #include "dev/pcidev.hh"
47 #include "dev/pktfifo.hh"
48 #include "params/IGbE.hh"
49 #include "sim/eventq.hh"
53 class IGbE : public EtherDevice
61 // eeprom data, status and control bits
62 int eeOpBits, eeAddrBits, eeDataBits;
63 uint8_t eeOpcode, eeAddr;
64 uint16_t flash[iGbReg::EEPROM_SIZE];
66 // The drain event if we have one
69 // cached parameters from params struct
76 // Packet that we are currently putting into the txFifo
77 EthPacketPtr txPacket;
79 // Should to Rx/Tx State machine tick?
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);
94 DPRINTF(EthernetIntr, "Posting RXT interrupt because RDTR timer expired\n");
95 postInterrupt(iGbReg::IT_RXT);
98 //friend class EventWrapper<IGbE, &IGbE::rdtrProcess>;
99 EventWrapper<IGbE, &IGbE::rdtrProcess> rdtrEvent;
101 // Event and function to deal with RADV timer expiring
103 rxDescCache.writeback(0);
104 DPRINTF(EthernetIntr, "Posting RXT interrupt because RADV timer expired\n");
105 postInterrupt(iGbReg::IT_RXT);
108 //friend class EventWrapper<IGbE, &IGbE::radvProcess>;
109 EventWrapper<IGbE, &IGbE::radvProcess> radvEvent;
111 // Event and function to deal with TADV timer expiring
113 txDescCache.writeback(0);
114 DPRINTF(EthernetIntr, "Posting TXDW interrupt because TADV timer expired\n");
115 postInterrupt(iGbReg::IT_TXDW);
118 //friend class EventWrapper<IGbE, &IGbE::tadvProcess>;
119 EventWrapper<IGbE, &IGbE::tadvProcess> tadvEvent;
121 // Event and function to deal with TIDV timer expiring
123 txDescCache.writeback(0);
124 DPRINTF(EthernetIntr, "Posting TXDW interrupt because TIDV timer expired\n");
125 postInterrupt(iGbReg::IT_TXDW);
127 //friend class EventWrapper<IGbE, &IGbE::tidvProcess>;
128 EventWrapper<IGbE, &IGbE::tidvProcess> tidvEvent;
130 // Main event to tick the device
132 //friend class EventWrapper<IGbE, &IGbE::tick>;
133 EventWrapper<IGbE, &IGbE::tick> tickEvent;
136 void rxStateMachine();
137 void txStateMachine();
140 /** Write an interrupt into the interrupt pending register and check mask
141 * and interrupt limit timer before sending interrupt to CPU
142 * @param t the type of interrupt we are posting
143 * @param now should we ignore the interrupt limiting timer
145 void postInterrupt(iGbReg::IntTypes t, bool now = false);
147 /** Check and see if changes to the mask register have caused an interrupt
148 * to need to be sent or perhaps removed an interrupt cause.
152 /** Send an interrupt to the cpu
154 void delayIntEvent();
156 // Event to moderate interrupts
157 EventWrapper<IGbE, &IGbE::delayIntEvent> interEvent;
159 /** Clear the interupt line to the cpu
163 Tick intClock() { return Clock::Int::ns * 1024; }
165 /** This function is used to restart the clock so it can handle things like
166 * draining and resume in one place. */
169 /** Check if all the draining things that need to occur have occured and
170 * handle the drain event if so.
178 virtual Addr descBase() const = 0;
179 virtual long descHead() const = 0;
180 virtual long descTail() const = 0;
181 virtual long descLen() const = 0;
182 virtual void updateHead(long h) = 0;
183 virtual void enableSm() = 0;
184 virtual void intAfterWb() const {}
185 virtual void fetchAfterWb() = 0;
187 std::deque<T*> usedCache;
188 std::deque<T*> unusedCache;
193 // Pointer to the device we cache for
196 // Name of this descriptor cache
199 // How far we've cached
202 // The size of the descriptor cache
205 // How many descriptors we are currently fetching
208 // How many descriptors we are currently writing back
211 // if the we wrote back to the end of the descriptor ring and are going
212 // to have to wrap and write more
215 // What the alignment is of the next descriptor writeback
218 /** The packet that is currently being dmad to memory if any
223 DescCache(IGbE *i, const std::string n, int s)
224 : igbe(i), _name(n), cachePnt(0), size(s), curFetching(0), wbOut(0),
225 pktPtr(NULL), wbDelayEvent(this), fetchDelayEvent(this),
226 fetchEvent(this), wbEvent(this)
228 fetchBuf = new T[size];
237 std::string name() { return _name; }
239 /** If the address/len/head change when we've got descriptors that are
240 * dirty that is very bad. This function checks that we don't and if we
245 if (usedCache.size() > 0 || curFetching || wbOut)
246 panic("Descriptor Address, Length or Head changed. Bad\n");
251 void writeback(Addr aMask)
254 if (aMask < wbAlignment) {
262 if (!wbDelayEvent.scheduled())
263 wbDelayEvent.schedule(igbe->wbDelay + curTick);
268 int curHead = descHead();
269 int max_to_wb = usedCache.size();
271 DPRINTF(EthernetDesc, "Writing back descriptors head: %d tail: "
272 "%d len: %d cachePnt: %d max_to_wb: %d descleft: %d\n",
273 curHead, descTail(), descLen(), cachePnt, max_to_wb,
276 if (max_to_wb + curHead >= descLen()) {
277 max_to_wb = descLen() - curHead;
279 // this is by definition aligned correctly
280 } else if (wbAlignment != 0) {
281 // align the wb point to the mask
282 max_to_wb = max_to_wb & ~wbAlignment;
285 DPRINTF(EthernetDesc, "Writing back %d descriptors\n", max_to_wb);
287 if (max_to_wb <= 0 || wbOut)
292 for (int x = 0; x < wbOut; x++) {
293 assert(usedCache.size());
294 memcpy(&wbBuf[x], usedCache[0], sizeof(T));
296 usedCache.pop_front();
301 igbe->dmaWrite(igbe->platform->pciToDma(descBase() + curHead * sizeof(T)),
302 wbOut * sizeof(T), &wbEvent, (uint8_t*)wbBuf,
305 EventWrapper<DescCache, &DescCache::writeback1> wbDelayEvent;
307 /** Fetch a chunk of descriptors into the descriptor cache.
308 * Calls fetchComplete when the memory system returns the data
311 void fetchDescriptors()
313 if (!fetchDelayEvent.scheduled())
314 fetchDelayEvent.schedule(igbe->fetchDelay + curTick);
317 void fetchDescriptors1()
324 if (descTail() >= cachePnt)
325 max_to_fetch = descTail() - cachePnt;
327 max_to_fetch = descLen() - cachePnt;
329 size_t free_cache = size - usedCache.size() - unusedCache.size();
331 max_to_fetch = std::min(max_to_fetch, free_cache);
333 DPRINTF(EthernetDesc, "Fetching descriptors head: %d tail: "
334 "%d len: %d cachePnt: %d max_to_fetch: %d descleft: %d\n",
335 descHead(), descTail(), descLen(), cachePnt,
336 max_to_fetch, descLeft());
339 if (max_to_fetch == 0)
342 // So we don't have two descriptor fetches going on at once
343 curFetching = max_to_fetch;
345 DPRINTF(EthernetDesc, "Fetching descriptors at %#x (%#x), size: %#x\n",
346 descBase() + cachePnt * sizeof(T),
347 igbe->platform->pciToDma(descBase() + cachePnt * sizeof(T)),
348 curFetching * sizeof(T));
350 igbe->dmaRead(igbe->platform->pciToDma(descBase() + cachePnt * sizeof(T)),
351 curFetching * sizeof(T), &fetchEvent, (uint8_t*)fetchBuf,
352 igbe->fetchCompDelay);
355 EventWrapper<DescCache, &DescCache::fetchDescriptors1> fetchDelayEvent;
357 /** Called by event when dma to read descriptors is completed
362 for (int x = 0; x < curFetching; x++) {
364 memcpy(newDesc, &fetchBuf[x], sizeof(T));
365 unusedCache.push_back(newDesc);
369 int oldCp = cachePnt;
372 cachePnt += curFetching;
373 assert(cachePnt <= descLen());
374 if (cachePnt == descLen())
379 DPRINTF(EthernetDesc, "Fetching complete cachePnt %d -> %d\n",
386 EventWrapper<DescCache, &DescCache::fetchComplete> fetchEvent;
388 /** Called by event when dma to writeback descriptors is completed
393 long curHead = descHead();
395 long oldHead = curHead;
401 if (curHead >= descLen())
402 curHead -= descLen();
407 DPRINTF(EthernetDesc, "Writeback complete curHead %d -> %d\n",
410 // If we still have more to wb, call wb now
414 DPRINTF(EthernetDesc, "Writeback has more todo\n");
415 writeback(wbAlignment);
425 EventWrapper<DescCache, &DescCache::wbComplete> wbEvent;
427 /* Return the number of descriptors left in the ring, so the device has
428 * a way to figure out if it needs to interrupt.
432 int left = unusedCache.size();
433 if (cachePnt - descTail() >= 0)
434 left += (cachePnt - descTail());
436 left += (descTail() - cachePnt);
441 /* Return the number of descriptors used and not written back.
443 int descUsed() const { return usedCache.size(); }
445 /* Return the number of cache unused descriptors we have. */
446 int descUnused() const {return unusedCache.size(); }
448 /* Get into a state where the descriptor address/head/etc colud be
452 DPRINTF(EthernetDesc, "Reseting descriptor cache\n");
453 for (int x = 0; x < usedCache.size(); x++)
455 for (int x = 0; x < unusedCache.size(); x++)
456 delete unusedCache[x];
465 virtual void serialize(std::ostream &os)
467 SERIALIZE_SCALAR(cachePnt);
468 SERIALIZE_SCALAR(curFetching);
469 SERIALIZE_SCALAR(wbOut);
470 SERIALIZE_SCALAR(moreToWb);
471 SERIALIZE_SCALAR(wbAlignment);
473 int usedCacheSize = usedCache.size();
474 SERIALIZE_SCALAR(usedCacheSize);
475 for(int x = 0; x < usedCacheSize; x++) {
476 arrayParamOut(os, csprintf("usedCache_%d", x),
477 (uint8_t*)usedCache[x],sizeof(T));
480 int unusedCacheSize = unusedCache.size();
481 SERIALIZE_SCALAR(unusedCacheSize);
482 for(int x = 0; x < unusedCacheSize; x++) {
483 arrayParamOut(os, csprintf("unusedCache_%d", x),
484 (uint8_t*)unusedCache[x],sizeof(T));
487 Tick fetch_delay = 0, wb_delay = 0;
488 if (fetchDelayEvent.scheduled())
489 fetch_delay = fetchDelayEvent.when();
490 SERIALIZE_SCALAR(fetch_delay);
491 if (wbDelayEvent.scheduled())
492 wb_delay = wbDelayEvent.when();
493 SERIALIZE_SCALAR(wb_delay);
498 virtual void unserialize(Checkpoint *cp, const std::string §ion)
500 UNSERIALIZE_SCALAR(cachePnt);
501 UNSERIALIZE_SCALAR(curFetching);
502 UNSERIALIZE_SCALAR(wbOut);
503 UNSERIALIZE_SCALAR(moreToWb);
504 UNSERIALIZE_SCALAR(wbAlignment);
507 UNSERIALIZE_SCALAR(usedCacheSize);
509 for(int x = 0; x < usedCacheSize; x++) {
511 arrayParamIn(cp, section, csprintf("usedCache_%d", x),
512 (uint8_t*)temp,sizeof(T));
513 usedCache.push_back(temp);
517 UNSERIALIZE_SCALAR(unusedCacheSize);
518 for(int x = 0; x < unusedCacheSize; x++) {
520 arrayParamIn(cp, section, csprintf("unusedCache_%d", x),
521 (uint8_t*)temp,sizeof(T));
522 unusedCache.push_back(temp);
524 Tick fetch_delay = 0, wb_delay = 0;
525 UNSERIALIZE_SCALAR(fetch_delay);
526 UNSERIALIZE_SCALAR(wb_delay);
528 fetchDelayEvent.schedule(fetch_delay);
530 wbDelayEvent.schedule(wb_delay);
534 virtual bool hasOutstandingEvents() {
535 return wbEvent.scheduled() || fetchEvent.scheduled();
541 class RxDescCache : public DescCache<iGbReg::RxDesc>
544 virtual Addr descBase() const { return igbe->regs.rdba(); }
545 virtual long descHead() const { return igbe->regs.rdh(); }
546 virtual long descLen() const { return igbe->regs.rdlen() >> 4; }
547 virtual long descTail() const { return igbe->regs.rdt(); }
548 virtual void updateHead(long h) { igbe->regs.rdh(h); }
549 virtual void enableSm();
550 virtual void fetchAfterWb() {
551 if (!igbe->rxTick && igbe->getState() == SimObject::Running)
558 RxDescCache(IGbE *i, std::string n, int s);
560 /** Write the given packet into the buffer(s) pointed to by the
561 * descriptor and update the book keeping. Should only be called when
562 * there are no dma's pending.
563 * @param packet ethernet packet to write
564 * @return if the packet could be written (there was a free descriptor)
566 void writePacket(EthPacketPtr packet);
567 /** Called by event when dma to write packet is completed
571 /** Check if the dma on the packet has completed.
576 EventWrapper<RxDescCache, &RxDescCache::pktComplete> pktEvent;
578 virtual bool hasOutstandingEvents();
580 virtual void serialize(std::ostream &os);
581 virtual void unserialize(Checkpoint *cp, const std::string §ion);
583 friend class RxDescCache;
585 RxDescCache rxDescCache;
587 class TxDescCache : public DescCache<iGbReg::TxDesc>
590 virtual Addr descBase() const { return igbe->regs.tdba(); }
591 virtual long descHead() const { return igbe->regs.tdh(); }
592 virtual long descTail() const { return igbe->regs.tdt(); }
593 virtual long descLen() const { return igbe->regs.tdlen() >> 4; }
594 virtual void updateHead(long h) { igbe->regs.tdh(h); }
595 virtual void enableSm();
596 virtual void intAfterWb() const { igbe->postInterrupt(iGbReg::IT_TXDW); }
597 virtual void fetchAfterWb() {
598 if (!igbe->txTick && igbe->getState() == SimObject::Running)
608 TxDescCache(IGbE *i, std::string n, int s);
610 /** Tell the cache to DMA a packet from main memory into its buffer and
611 * return the size the of the packet to reserve space in tx fifo.
612 * @return size of the packet
615 void getPacketData(EthPacketPtr p);
617 /** Ask if the packet has been transfered so the state machine can give
619 * @return packet available in descriptor cache
621 bool packetAvailable();
623 /** Ask if we are still waiting for the packet to be transfered.
624 * @return packet still in transit.
626 bool packetWaiting() { return pktWaiting; }
628 /** Ask if this packet is composed of multiple descriptors
629 * so even if we've got data, we need to wait for more before
630 * we can send it out.
631 * @return packet can't be sent out because it's a multi-descriptor
634 bool packetMultiDesc() { return pktMultiDesc;}
636 /** Called by event when dma to write packet is completed
639 EventWrapper<TxDescCache, &TxDescCache::pktComplete> pktEvent;
641 virtual bool hasOutstandingEvents();
643 virtual void serialize(std::ostream &os);
644 virtual void unserialize(Checkpoint *cp, const std::string §ion);
647 friend class TxDescCache;
649 TxDescCache txDescCache;
652 typedef IGbEParams Params;
656 return dynamic_cast<const Params *>(_params);
658 IGbE(const Params *params);
661 virtual EtherInt *getEthPort(const std::string &if_name, int idx);
665 inline Tick ticks(int numCycles) const { return numCycles * clock; }
667 virtual Tick read(PacketPtr pkt);
668 virtual Tick write(PacketPtr pkt);
670 virtual Tick writeConfig(PacketPtr pkt);
672 bool ethRxPkt(EthPacketPtr packet);
675 virtual void serialize(std::ostream &os);
676 virtual void unserialize(Checkpoint *cp, const std::string §ion);
677 virtual unsigned int drain(Event *de);
678 virtual void resume();
682 class IGbEInt : public EtherInt
688 IGbEInt(const std::string &name, IGbE *d)
689 : EtherInt(name), dev(d)
692 virtual bool recvPacket(EthPacketPtr pkt) { return dev->ethRxPkt(pkt); }
693 virtual void sendDone() { dev->ethTxDone(); }
700 #endif //__DEV_I8254XGBE_HH__