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 // Event and function to deal with RDTR timer expiring
88 rxDescCache.writeback(0);
89 DPRINTF(EthernetIntr, "Posting RXT interrupt because RDTR timer expired\n");
90 postInterrupt(iGbReg::IT_RXT, true);
93 //friend class EventWrapper<IGbE, &IGbE::rdtrProcess>;
94 EventWrapper<IGbE, &IGbE::rdtrProcess> rdtrEvent;
96 // Event and function to deal with RADV timer expiring
98 rxDescCache.writeback(0);
99 DPRINTF(EthernetIntr, "Posting RXT interrupt because RADV timer expired\n");
100 postInterrupt(iGbReg::IT_RXT, true);
103 //friend class EventWrapper<IGbE, &IGbE::radvProcess>;
104 EventWrapper<IGbE, &IGbE::radvProcess> radvEvent;
106 // Event and function to deal with TADV timer expiring
108 txDescCache.writeback(0);
109 DPRINTF(EthernetIntr, "Posting TXDW interrupt because TADV timer expired\n");
110 postInterrupt(iGbReg::IT_TXDW, true);
113 //friend class EventWrapper<IGbE, &IGbE::tadvProcess>;
114 EventWrapper<IGbE, &IGbE::tadvProcess> tadvEvent;
116 // Event and function to deal with TIDV timer expiring
118 txDescCache.writeback(0);
119 DPRINTF(EthernetIntr, "Posting TXDW interrupt because TIDV timer expired\n");
120 postInterrupt(iGbReg::IT_TXDW, true);
122 //friend class EventWrapper<IGbE, &IGbE::tidvProcess>;
123 EventWrapper<IGbE, &IGbE::tidvProcess> tidvEvent;
125 // Main event to tick the device
127 //friend class EventWrapper<IGbE, &IGbE::tick>;
128 EventWrapper<IGbE, &IGbE::tick> tickEvent;
131 void rxStateMachine();
132 void txStateMachine();
135 /** Write an interrupt into the interrupt pending register and check mask
136 * and interrupt limit timer before sending interrupt to CPU
137 * @param t the type of interrupt we are posting
138 * @param now should we ignore the interrupt limiting timer
140 void postInterrupt(iGbReg::IntTypes t, bool now = false);
142 /** Check and see if changes to the mask register have caused an interrupt
143 * to need to be sent or perhaps removed an interrupt cause.
147 /** Send an interrupt to the cpu
149 void delayIntEvent();
151 // Event to moderate interrupts
152 EventWrapper<IGbE, &IGbE::delayIntEvent> interEvent;
154 /** Clear the interupt line to the cpu
158 Tick intClock() { return Clock::Int::ns * 1024; }
160 /** This function is used to restart the clock so it can handle things like
161 * draining and resume in one place. */
164 /** Check if all the draining things that need to occur have occured and
165 * handle the drain event if so.
173 virtual Addr descBase() const = 0;
174 virtual long descHead() const = 0;
175 virtual long descTail() const = 0;
176 virtual long descLen() const = 0;
177 virtual void updateHead(long h) = 0;
178 virtual void enableSm() = 0;
179 virtual void intAfterWb() const {}
180 virtual void fetchAfterWb() = 0;
182 std::deque<T*> usedCache;
183 std::deque<T*> unusedCache;
188 // Pointer to the device we cache for
191 // Name of this descriptor cache
194 // How far we've cached
197 // The size of the descriptor cache
200 // How many descriptors we are currently fetching
203 // How many descriptors we are currently writing back
206 // if the we wrote back to the end of the descriptor ring and are going
207 // to have to wrap and write more
210 // What the alignment is of the next descriptor writeback
213 /** The packet that is currently being dmad to memory if any
218 DescCache(IGbE *i, const std::string n, int s)
219 : igbe(i), _name(n), cachePnt(0), size(s), curFetching(0), wbOut(0),
220 pktPtr(NULL), fetchEvent(this), wbEvent(this)
222 fetchBuf = new T[size];
231 std::string name() { return _name; }
233 /** If the address/len/head change when we've got descriptors that are
234 * dirty that is very bad. This function checks that we don't and if we
239 if (usedCache.size() > 0 || curFetching || wbOut)
240 panic("Descriptor Address, Length or Head changed. Bad\n");
245 void writeback(Addr aMask)
247 int curHead = descHead();
248 int max_to_wb = usedCache.size();
250 DPRINTF(EthernetDesc, "Writing back descriptors head: %d tail: "
251 "%d len: %d cachePnt: %d max_to_wb: %d descleft: %d\n",
252 curHead, descTail(), descLen(), cachePnt, max_to_wb,
255 // Check if this writeback is less restrictive that the previous
256 // and if so setup another one immediately following it
257 if (wbOut && (aMask < wbAlignment)) {
260 DPRINTF(EthernetDesc, "Writing back already in process, returning\n");
268 if (max_to_wb + curHead >= descLen()) {
269 max_to_wb = descLen() - curHead;
271 // this is by definition aligned correctly
272 } else if (aMask != 0) {
273 // align the wb point to the mask
274 max_to_wb = max_to_wb & ~aMask;
277 DPRINTF(EthernetDesc, "Writing back %d descriptors\n", max_to_wb);
279 if (max_to_wb <= 0 || wbOut)
284 for (int x = 0; x < wbOut; x++) {
285 assert(usedCache.size());
286 memcpy(&wbBuf[x], usedCache[0], sizeof(T));
288 usedCache.pop_front();
293 igbe->dmaWrite(igbe->platform->pciToDma(descBase() + curHead * sizeof(T)),
294 wbOut * sizeof(T), &wbEvent, (uint8_t*)wbBuf);
297 /** Fetch a chunk of descriptors into the descriptor cache.
298 * Calls fetchComplete when the memory system returns the data
300 void fetchDescriptors()
307 if (descTail() >= cachePnt)
308 max_to_fetch = descTail() - cachePnt;
310 max_to_fetch = descLen() - cachePnt;
312 size_t free_cache = size - usedCache.size() - unusedCache.size();
314 max_to_fetch = std::min(max_to_fetch, free_cache);
316 DPRINTF(EthernetDesc, "Fetching descriptors head: %d tail: "
317 "%d len: %d cachePnt: %d max_to_fetch: %d descleft: %d\n",
318 descHead(), descTail(), descLen(), cachePnt,
319 max_to_fetch, descLeft());
322 if (max_to_fetch == 0)
325 // So we don't have two descriptor fetches going on at once
326 curFetching = max_to_fetch;
328 DPRINTF(EthernetDesc, "Fetching descriptors at %#x (%#x), size: %#x\n",
329 descBase() + cachePnt * sizeof(T),
330 igbe->platform->pciToDma(descBase() + cachePnt * sizeof(T)),
331 curFetching * sizeof(T));
333 igbe->dmaRead(igbe->platform->pciToDma(descBase() + cachePnt * sizeof(T)),
334 curFetching * sizeof(T), &fetchEvent, (uint8_t*)fetchBuf);
338 /** Called by event when dma to read descriptors is completed
343 for (int x = 0; x < curFetching; x++) {
345 memcpy(newDesc, &fetchBuf[x], sizeof(T));
346 unusedCache.push_back(newDesc);
350 int oldCp = cachePnt;
353 cachePnt += curFetching;
354 assert(cachePnt <= descLen());
355 if (cachePnt == descLen())
360 DPRINTF(EthernetDesc, "Fetching complete cachePnt %d -> %d\n",
367 EventWrapper<DescCache, &DescCache::fetchComplete> fetchEvent;
369 /** Called by event when dma to writeback descriptors is completed
374 long curHead = descHead();
376 long oldHead = curHead;
382 if (curHead >= descLen())
383 curHead -= descLen();
388 DPRINTF(EthernetDesc, "Writeback complete curHead %d -> %d\n",
391 // If we still have more to wb, call wb now
394 DPRINTF(EthernetDesc, "Writeback has more todo\n");
395 writeback(wbAlignment);
405 EventWrapper<DescCache, &DescCache::wbComplete> wbEvent;
407 /* Return the number of descriptors left in the ring, so the device has
408 * a way to figure out if it needs to interrupt.
412 int left = unusedCache.size();
413 if (cachePnt - descTail() >= 0)
414 left += (cachePnt - descTail());
416 left += (descTail() - cachePnt);
421 /* Return the number of descriptors used and not written back.
423 int descUsed() const { return usedCache.size(); }
425 /* Return the number of cache unused descriptors we have. */
426 int descUnused() const {return unusedCache.size(); }
428 /* Get into a state where the descriptor address/head/etc colud be
432 DPRINTF(EthernetDesc, "Reseting descriptor cache\n");
433 for (int x = 0; x < usedCache.size(); x++)
435 for (int x = 0; x < unusedCache.size(); x++)
436 delete unusedCache[x];
445 virtual void serialize(std::ostream &os)
447 SERIALIZE_SCALAR(cachePnt);
448 SERIALIZE_SCALAR(curFetching);
449 SERIALIZE_SCALAR(wbOut);
450 SERIALIZE_SCALAR(moreToWb);
451 SERIALIZE_SCALAR(wbAlignment);
453 int usedCacheSize = usedCache.size();
454 SERIALIZE_SCALAR(usedCacheSize);
455 for(int x = 0; x < usedCacheSize; x++) {
456 arrayParamOut(os, csprintf("usedCache_%d", x),
457 (uint8_t*)usedCache[x],sizeof(T));
460 int unusedCacheSize = unusedCache.size();
461 SERIALIZE_SCALAR(unusedCacheSize);
462 for(int x = 0; x < unusedCacheSize; x++) {
463 arrayParamOut(os, csprintf("unusedCache_%d", x),
464 (uint8_t*)unusedCache[x],sizeof(T));
468 virtual void unserialize(Checkpoint *cp, const std::string §ion)
470 UNSERIALIZE_SCALAR(cachePnt);
471 UNSERIALIZE_SCALAR(curFetching);
472 UNSERIALIZE_SCALAR(wbOut);
473 UNSERIALIZE_SCALAR(moreToWb);
474 UNSERIALIZE_SCALAR(wbAlignment);
477 UNSERIALIZE_SCALAR(usedCacheSize);
479 for(int x = 0; x < usedCacheSize; x++) {
481 arrayParamIn(cp, section, csprintf("usedCache_%d", x),
482 (uint8_t*)temp,sizeof(T));
483 usedCache.push_back(temp);
487 UNSERIALIZE_SCALAR(unusedCacheSize);
488 for(int x = 0; x < unusedCacheSize; x++) {
490 arrayParamIn(cp, section, csprintf("unusedCache_%d", x),
491 (uint8_t*)temp,sizeof(T));
492 unusedCache.push_back(temp);
495 virtual bool hasOutstandingEvents() {
496 return wbEvent.scheduled() || fetchEvent.scheduled();
502 class RxDescCache : public DescCache<iGbReg::RxDesc>
505 virtual Addr descBase() const { return igbe->regs.rdba(); }
506 virtual long descHead() const { return igbe->regs.rdh(); }
507 virtual long descLen() const { return igbe->regs.rdlen() >> 4; }
508 virtual long descTail() const { return igbe->regs.rdt(); }
509 virtual void updateHead(long h) { igbe->regs.rdh(h); }
510 virtual void enableSm();
511 virtual void fetchAfterWb() {
512 if (!igbe->rxTick && igbe->getState() == SimObject::Running)
519 RxDescCache(IGbE *i, std::string n, int s);
521 /** Write the given packet into the buffer(s) pointed to by the
522 * descriptor and update the book keeping. Should only be called when
523 * there are no dma's pending.
524 * @param packet ethernet packet to write
525 * @return if the packet could be written (there was a free descriptor)
527 void writePacket(EthPacketPtr packet);
528 /** Called by event when dma to write packet is completed
532 /** Check if the dma on the packet has completed.
537 EventWrapper<RxDescCache, &RxDescCache::pktComplete> pktEvent;
539 virtual bool hasOutstandingEvents();
541 virtual void serialize(std::ostream &os);
542 virtual void unserialize(Checkpoint *cp, const std::string §ion);
544 friend class RxDescCache;
546 RxDescCache rxDescCache;
548 class TxDescCache : public DescCache<iGbReg::TxDesc>
551 virtual Addr descBase() const { return igbe->regs.tdba(); }
552 virtual long descHead() const { return igbe->regs.tdh(); }
553 virtual long descTail() const { return igbe->regs.tdt(); }
554 virtual long descLen() const { return igbe->regs.tdlen() >> 4; }
555 virtual void updateHead(long h) { igbe->regs.tdh(h); }
556 virtual void enableSm();
557 virtual void intAfterWb() const { igbe->postInterrupt(iGbReg::IT_TXDW); }
558 virtual void fetchAfterWb() {
559 if (!igbe->txTick && igbe->getState() == SimObject::Running)
569 TxDescCache(IGbE *i, std::string n, int s);
571 /** Tell the cache to DMA a packet from main memory into its buffer and
572 * return the size the of the packet to reserve space in tx fifo.
573 * @return size of the packet
576 void getPacketData(EthPacketPtr p);
578 /** Ask if the packet has been transfered so the state machine can give
580 * @return packet available in descriptor cache
582 bool packetAvailable();
584 /** Ask if we are still waiting for the packet to be transfered.
585 * @return packet still in transit.
587 bool packetWaiting() { return pktWaiting; }
589 /** Ask if this packet is composed of multiple descriptors
590 * so even if we've got data, we need to wait for more before
591 * we can send it out.
592 * @return packet can't be sent out because it's a multi-descriptor
595 bool packetMultiDesc() { return pktMultiDesc;}
597 /** Called by event when dma to write packet is completed
600 EventWrapper<TxDescCache, &TxDescCache::pktComplete> pktEvent;
602 virtual bool hasOutstandingEvents();
604 virtual void serialize(std::ostream &os);
605 virtual void unserialize(Checkpoint *cp, const std::string §ion);
608 friend class TxDescCache;
610 TxDescCache txDescCache;
613 typedef IGbEParams Params;
617 return dynamic_cast<const Params *>(_params);
619 IGbE(const Params *params);
622 virtual EtherInt *getEthPort(const std::string &if_name, int idx);
626 inline Tick ticks(int numCycles) const { return numCycles * clock; }
628 virtual Tick read(PacketPtr pkt);
629 virtual Tick write(PacketPtr pkt);
631 virtual Tick writeConfig(PacketPtr pkt);
633 bool ethRxPkt(EthPacketPtr packet);
636 virtual void serialize(std::ostream &os);
637 virtual void unserialize(Checkpoint *cp, const std::string §ion);
638 virtual unsigned int drain(Event *de);
639 virtual void resume();
643 class IGbEInt : public EtherInt
649 IGbEInt(const std::string &name, IGbE *d)
650 : EtherInt(name), dev(d)
653 virtual bool recvPacket(EthPacketPtr pkt) { return dev->ethRxPkt(pkt); }
654 virtual void sendDone() { dev->ethTxDone(); }
661 #endif //__DEV_I8254XGBE_HH__