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 "base/statistics.hh"
43 #include "dev/etherdevice.hh"
44 #include "dev/etherint.hh"
45 #include "dev/etherpkt.hh"
46 #include "dev/i8254xGBe_defs.hh"
47 #include "dev/pcidev.hh"
48 #include "dev/pktfifo.hh"
49 #include "params/IGbE.hh"
50 #include "sim/eventq.hh"
54 class IGbE : public EtherDevice
62 // eeprom data, status and control bits
63 int eeOpBits, eeAddrBits, eeDataBits;
64 uint8_t eeOpcode, eeAddr;
65 uint16_t flash[iGbReg::EEPROM_SIZE];
67 // The drain event if we have one
70 // cached parameters from params struct
77 // Packet that we are currently putting into the txFifo
78 EthPacketPtr txPacket;
80 // Should to Rx/Tx State machine tick?
87 // Event and function to deal with RDTR timer expiring
89 rxDescCache.writeback(0);
90 DPRINTF(EthernetIntr, "Posting RXT interrupt because RDTR timer expired\n");
91 postInterrupt(iGbReg::IT_RXT, true);
94 //friend class EventWrapper<IGbE, &IGbE::rdtrProcess>;
95 EventWrapper<IGbE, &IGbE::rdtrProcess> rdtrEvent;
97 // Event and function to deal with RADV timer expiring
99 rxDescCache.writeback(0);
100 DPRINTF(EthernetIntr, "Posting RXT interrupt because RADV timer expired\n");
101 postInterrupt(iGbReg::IT_RXT, true);
104 //friend class EventWrapper<IGbE, &IGbE::radvProcess>;
105 EventWrapper<IGbE, &IGbE::radvProcess> radvEvent;
107 // Event and function to deal with TADV timer expiring
109 txDescCache.writeback(0);
110 DPRINTF(EthernetIntr, "Posting TXDW interrupt because TADV timer expired\n");
111 postInterrupt(iGbReg::IT_TXDW, true);
114 //friend class EventWrapper<IGbE, &IGbE::tadvProcess>;
115 EventWrapper<IGbE, &IGbE::tadvProcess> tadvEvent;
117 // Event and function to deal with TIDV timer expiring
119 txDescCache.writeback(0);
120 DPRINTF(EthernetIntr, "Posting TXDW interrupt because TIDV timer expired\n");
121 postInterrupt(iGbReg::IT_TXDW, true);
123 //friend class EventWrapper<IGbE, &IGbE::tidvProcess>;
124 EventWrapper<IGbE, &IGbE::tidvProcess> tidvEvent;
126 // Main event to tick the device
128 //friend class EventWrapper<IGbE, &IGbE::tick>;
129 EventWrapper<IGbE, &IGbE::tick> tickEvent;
132 void rxStateMachine();
133 void txStateMachine();
136 /** Write an interrupt into the interrupt pending register and check mask
137 * and interrupt limit timer before sending interrupt to CPU
138 * @param t the type of interrupt we are posting
139 * @param now should we ignore the interrupt limiting timer
141 void postInterrupt(iGbReg::IntTypes t, bool now = false);
143 /** Check and see if changes to the mask register have caused an interrupt
144 * to need to be sent or perhaps removed an interrupt cause.
148 /** Send an interrupt to the cpu
150 void delayIntEvent();
152 // Event to moderate interrupts
153 EventWrapper<IGbE, &IGbE::delayIntEvent> interEvent;
155 /** Clear the interupt line to the cpu
159 Tick intClock() { return Clock::Int::ns * 1024; }
161 /** This function is used to restart the clock so it can handle things like
162 * draining and resume in one place. */
165 /** Check if all the draining things that need to occur have occured and
166 * handle the drain event if so.
174 virtual Addr descBase() const = 0;
175 virtual long descHead() const = 0;
176 virtual long descTail() const = 0;
177 virtual long descLen() const = 0;
178 virtual void updateHead(long h) = 0;
179 virtual void enableSm() = 0;
180 virtual void intAfterWb() const {}
181 virtual void fetchAfterWb() = 0;
183 std::deque<T*> usedCache;
184 std::deque<T*> unusedCache;
189 // Pointer to the device we cache for
192 // Name of this descriptor cache
195 // How far we've cached
198 // The size of the descriptor cache
201 // How many descriptors we are currently fetching
204 // How many descriptors we are currently writing back
207 // if the we wrote back to the end of the descriptor ring and are going
208 // to have to wrap and write more
211 // What the alignment is of the next descriptor writeback
214 /** The packet that is currently being dmad to memory if any
219 DescCache(IGbE *i, const std::string n, int s)
220 : igbe(i), _name(n), cachePnt(0), size(s), curFetching(0), wbOut(0),
221 pktPtr(NULL), fetchEvent(this), wbEvent(this)
223 fetchBuf = new T[size];
232 std::string name() { return _name; }
234 /** If the address/len/head change when we've got descriptors that are
235 * dirty that is very bad. This function checks that we don't and if we
240 if (usedCache.size() > 0 || curFetching || wbOut)
241 panic("Descriptor Address, Length or Head changed. Bad\n");
246 void writeback(Addr aMask)
248 int curHead = descHead();
249 int max_to_wb = usedCache.size();
251 DPRINTF(EthernetDesc, "Writing back descriptors head: %d tail: "
252 "%d len: %d cachePnt: %d max_to_wb: %d descleft: %d\n",
253 curHead, descTail(), descLen(), cachePnt, max_to_wb,
256 // Check if this writeback is less restrictive that the previous
257 // and if so setup another one immediately following it
258 if (wbOut && (aMask < wbAlignment)) {
261 DPRINTF(EthernetDesc, "Writing back already in process, returning\n");
269 if (max_to_wb + curHead >= descLen()) {
270 max_to_wb = descLen() - curHead;
272 // this is by definition aligned correctly
273 } else if (aMask != 0) {
274 // align the wb point to the mask
275 max_to_wb = max_to_wb & ~aMask;
278 DPRINTF(EthernetDesc, "Writing back %d descriptors\n", max_to_wb);
280 if (max_to_wb <= 0 || wbOut)
285 for (int x = 0; x < wbOut; x++) {
286 assert(usedCache.size());
287 memcpy(&wbBuf[x], usedCache[0], sizeof(T));
289 usedCache.pop_front();
294 igbe->dmaWrite(igbe->platform->pciToDma(descBase() + curHead * sizeof(T)),
295 wbOut * sizeof(T), &wbEvent, (uint8_t*)wbBuf);
298 /** Fetch a chunk of descriptors into the descriptor cache.
299 * Calls fetchComplete when the memory system returns the data
301 void fetchDescriptors()
308 if (descTail() >= cachePnt)
309 max_to_fetch = descTail() - cachePnt;
311 max_to_fetch = descLen() - cachePnt;
313 size_t free_cache = size - usedCache.size() - unusedCache.size();
315 max_to_fetch = std::min(max_to_fetch, free_cache);
317 DPRINTF(EthernetDesc, "Fetching descriptors head: %d tail: "
318 "%d len: %d cachePnt: %d max_to_fetch: %d descleft: %d\n",
319 descHead(), descTail(), descLen(), cachePnt,
320 max_to_fetch, descLeft());
323 if (max_to_fetch == 0)
326 // So we don't have two descriptor fetches going on at once
327 curFetching = max_to_fetch;
329 DPRINTF(EthernetDesc, "Fetching descriptors at %#x (%#x), size: %#x\n",
330 descBase() + cachePnt * sizeof(T),
331 igbe->platform->pciToDma(descBase() + cachePnt * sizeof(T)),
332 curFetching * sizeof(T));
334 igbe->dmaRead(igbe->platform->pciToDma(descBase() + cachePnt * sizeof(T)),
335 curFetching * sizeof(T), &fetchEvent, (uint8_t*)fetchBuf);
339 /** Called by event when dma to read descriptors is completed
344 for (int x = 0; x < curFetching; x++) {
346 memcpy(newDesc, &fetchBuf[x], sizeof(T));
347 unusedCache.push_back(newDesc);
351 int oldCp = cachePnt;
354 cachePnt += curFetching;
355 assert(cachePnt <= descLen());
356 if (cachePnt == descLen())
361 DPRINTF(EthernetDesc, "Fetching complete cachePnt %d -> %d\n",
368 EventWrapper<DescCache, &DescCache::fetchComplete> fetchEvent;
370 /** Called by event when dma to writeback descriptors is completed
375 long curHead = descHead();
377 long oldHead = curHead;
383 if (curHead >= descLen())
384 curHead -= descLen();
389 DPRINTF(EthernetDesc, "Writeback complete curHead %d -> %d\n",
392 // If we still have more to wb, call wb now
395 DPRINTF(EthernetDesc, "Writeback has more todo\n");
396 writeback(wbAlignment);
406 EventWrapper<DescCache, &DescCache::wbComplete> wbEvent;
408 /* Return the number of descriptors left in the ring, so the device has
409 * a way to figure out if it needs to interrupt.
413 int left = unusedCache.size();
414 if (cachePnt - descTail() >= 0)
415 left += (cachePnt - descTail());
417 left += (descTail() - cachePnt);
422 /* Return the number of descriptors used and not written back.
424 int descUsed() const { return usedCache.size(); }
426 /* Return the number of cache unused descriptors we have. */
427 int descUnused() const {return unusedCache.size(); }
429 /* Get into a state where the descriptor address/head/etc colud be
433 DPRINTF(EthernetDesc, "Reseting descriptor cache\n");
434 for (int x = 0; x < usedCache.size(); x++)
436 for (int x = 0; x < unusedCache.size(); x++)
437 delete unusedCache[x];
446 virtual void serialize(std::ostream &os)
448 SERIALIZE_SCALAR(cachePnt);
449 SERIALIZE_SCALAR(curFetching);
450 SERIALIZE_SCALAR(wbOut);
451 SERIALIZE_SCALAR(moreToWb);
452 SERIALIZE_SCALAR(wbAlignment);
454 int usedCacheSize = usedCache.size();
455 SERIALIZE_SCALAR(usedCacheSize);
456 for(int x = 0; x < usedCacheSize; x++) {
457 arrayParamOut(os, csprintf("usedCache_%d", x),
458 (uint8_t*)usedCache[x],sizeof(T));
461 int unusedCacheSize = unusedCache.size();
462 SERIALIZE_SCALAR(unusedCacheSize);
463 for(int x = 0; x < unusedCacheSize; x++) {
464 arrayParamOut(os, csprintf("unusedCache_%d", x),
465 (uint8_t*)unusedCache[x],sizeof(T));
469 virtual void unserialize(Checkpoint *cp, const std::string §ion)
471 UNSERIALIZE_SCALAR(cachePnt);
472 UNSERIALIZE_SCALAR(curFetching);
473 UNSERIALIZE_SCALAR(wbOut);
474 UNSERIALIZE_SCALAR(moreToWb);
475 UNSERIALIZE_SCALAR(wbAlignment);
478 UNSERIALIZE_SCALAR(usedCacheSize);
480 for(int x = 0; x < usedCacheSize; x++) {
482 arrayParamIn(cp, section, csprintf("usedCache_%d", x),
483 (uint8_t*)temp,sizeof(T));
484 usedCache.push_back(temp);
488 UNSERIALIZE_SCALAR(unusedCacheSize);
489 for(int x = 0; x < unusedCacheSize; x++) {
491 arrayParamIn(cp, section, csprintf("unusedCache_%d", x),
492 (uint8_t*)temp,sizeof(T));
493 unusedCache.push_back(temp);
496 virtual bool hasOutstandingEvents() {
497 return wbEvent.scheduled() || fetchEvent.scheduled();
503 class RxDescCache : public DescCache<iGbReg::RxDesc>
506 virtual Addr descBase() const { return igbe->regs.rdba(); }
507 virtual long descHead() const { return igbe->regs.rdh(); }
508 virtual long descLen() const { return igbe->regs.rdlen() >> 4; }
509 virtual long descTail() const { return igbe->regs.rdt(); }
510 virtual void updateHead(long h) { igbe->regs.rdh(h); }
511 virtual void enableSm();
512 virtual void fetchAfterWb() {
513 if (!igbe->rxTick && igbe->getState() == SimObject::Running)
520 RxDescCache(IGbE *i, std::string n, int s);
522 /** Write the given packet into the buffer(s) pointed to by the
523 * descriptor and update the book keeping. Should only be called when
524 * there are no dma's pending.
525 * @param packet ethernet packet to write
526 * @return if the packet could be written (there was a free descriptor)
528 void writePacket(EthPacketPtr packet);
529 /** Called by event when dma to write packet is completed
533 /** Check if the dma on the packet has completed.
538 EventWrapper<RxDescCache, &RxDescCache::pktComplete> pktEvent;
540 virtual bool hasOutstandingEvents();
542 virtual void serialize(std::ostream &os);
543 virtual void unserialize(Checkpoint *cp, const std::string §ion);
545 friend class RxDescCache;
547 RxDescCache rxDescCache;
549 class TxDescCache : public DescCache<iGbReg::TxDesc>
552 virtual Addr descBase() const { return igbe->regs.tdba(); }
553 virtual long descHead() const { return igbe->regs.tdh(); }
554 virtual long descTail() const { return igbe->regs.tdt(); }
555 virtual long descLen() const { return igbe->regs.tdlen() >> 4; }
556 virtual void updateHead(long h) { igbe->regs.tdh(h); }
557 virtual void enableSm();
558 virtual void intAfterWb() const { igbe->postInterrupt(iGbReg::IT_TXDW); }
559 virtual void fetchAfterWb() {
560 if (!igbe->txTick && igbe->getState() == SimObject::Running)
570 TxDescCache(IGbE *i, std::string n, int s);
572 /** Tell the cache to DMA a packet from main memory into its buffer and
573 * return the size the of the packet to reserve space in tx fifo.
574 * @return size of the packet
577 void getPacketData(EthPacketPtr p);
579 /** Ask if the packet has been transfered so the state machine can give
581 * @return packet available in descriptor cache
583 bool packetAvailable();
585 /** Ask if we are still waiting for the packet to be transfered.
586 * @return packet still in transit.
588 bool packetWaiting() { return pktWaiting; }
590 /** Ask if this packet is composed of multiple descriptors
591 * so even if we've got data, we need to wait for more before
592 * we can send it out.
593 * @return packet can't be sent out because it's a multi-descriptor
596 bool packetMultiDesc() { return pktMultiDesc;}
598 /** Called by event when dma to write packet is completed
601 EventWrapper<TxDescCache, &TxDescCache::pktComplete> pktEvent;
603 virtual bool hasOutstandingEvents();
605 virtual void serialize(std::ostream &os);
606 virtual void unserialize(Checkpoint *cp, const std::string §ion);
609 friend class TxDescCache;
611 TxDescCache txDescCache;
614 typedef IGbEParams Params;
618 return dynamic_cast<const Params *>(_params);
620 IGbE(const Params *params);
623 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__