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/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 "sim/eventq.hh"
52 class IGbE : public PciDev
60 // eeprom data, status and control bits
61 int eeOpBits, eeAddrBits, eeDataBits;
62 uint8_t eeOpcode, eeAddr;
63 uint16_t flash[iGbReg::EEPROM_SIZE];
65 // cached parameters from params struct
73 // Packet that we are currently putting into the txFifo
74 EthPacketPtr txPacket;
76 // Should to Rx/Tx State machine tick?
80 // Event and function to deal with RDTR timer expiring
81 void rdtrProcess() { rxDescCache.writeback(0); postInterrupt(iGbReg::IT_RXT, true); }
82 //friend class EventWrapper<IGbE, &IGbE::rdtrProcess>;
83 EventWrapper<IGbE, &IGbE::rdtrProcess> rdtrEvent;
85 // Event and function to deal with RADV timer expiring
86 void radvProcess() { rxDescCache.writeback(0); postInterrupt(iGbReg::IT_RXT, true); }
87 //friend class EventWrapper<IGbE, &IGbE::radvProcess>;
88 EventWrapper<IGbE, &IGbE::radvProcess> radvEvent;
90 // Event and function to deal with TADV timer expiring
91 void tadvProcess() { postInterrupt(iGbReg::IT_TXDW, true); }
92 //friend class EventWrapper<IGbE, &IGbE::tadvProcess>;
93 EventWrapper<IGbE, &IGbE::tadvProcess> tadvEvent;
95 // Event and function to deal with TIDV timer expiring
96 void tidvProcess() { postInterrupt(iGbReg::IT_TXDW, true); };
97 //friend class EventWrapper<IGbE, &IGbE::tidvProcess>;
98 EventWrapper<IGbE, &IGbE::tidvProcess> tidvEvent;
100 // Main event to tick the device
102 //friend class EventWrapper<IGbE, &IGbE::tick>;
103 EventWrapper<IGbE, &IGbE::tick> tickEvent;
106 void rxStateMachine();
107 void txStateMachine();
110 /** Write an interrupt into the interrupt pending register and check mask
111 * and interrupt limit timer before sending interrupt to CPU
112 * @param t the type of interrupt we are posting
113 * @param now should we ignore the interrupt limiting timer
115 void postInterrupt(iGbReg::IntTypes t, bool now = false);
117 /** Check and see if changes to the mask register have caused an interrupt
118 * to need to be sent or perhaps removed an interrupt cause.
122 /** Send an interrupt to the cpu
125 // Event to moderate interrupts
126 EventWrapper<IGbE, &IGbE::cpuPostInt> interEvent;
128 /** Clear the interupt line to the cpu
132 Tick intClock() { return Clock::Int::ns * 1024; }
140 virtual Addr descBase() const = 0;
141 virtual long descHead() const = 0;
142 virtual long descTail() const = 0;
143 virtual long descLen() const = 0;
144 virtual void updateHead(long h) = 0;
145 virtual void enableSm() = 0;
146 virtual void intAfterWb() const {}
148 std::deque<T*> usedCache;
149 std::deque<T*> unusedCache;
154 // Pointer to the device we cache for
157 // Name of this descriptor cache
160 // How far we've cached
163 // The size of the descriptor cache
166 // How many descriptors we are currently fetching
169 // How many descriptors we are currently writing back
172 // if the we wrote back to the end of the descriptor ring and are going
173 // to have to wrap and write more
176 // What the alignment is of the next descriptor writeback
179 /** The packet that is currently being dmad to memory if any
184 DescCache(IGbE *i, const std::string n, int s)
185 : igbe(i), _name(n), cachePnt(0), size(s), curFetching(0), wbOut(0),
186 pktPtr(NULL), fetchEvent(this), wbEvent(this)
188 fetchBuf = new T[size];
197 std::string name() { return _name; }
199 /** If the address/len/head change when we've got descriptors that are
200 * dirty that is very bad. This function checks that we don't and if we
205 if (usedCache.size() > 0 || unusedCache.size() > 0)
206 panic("Descriptor Address, Length or Head changed. Bad\n");
209 void writeback(Addr aMask)
211 int curHead = descHead();
212 int max_to_wb = usedCache.size();
214 DPRINTF(EthernetDesc, "Writing back descriptors head: %d tail: "
215 "%d len: %d cachePnt: %d max_to_wb: %d descleft: %d\n",
216 curHead, descTail(), descLen(), cachePnt, max_to_wb,
219 // Check if this writeback is less restrictive that the previous
220 // and if so setup another one immediately following it
221 if (wbOut && (aMask < wbAlignment)) {
224 DPRINTF(EthernetDesc, "Writing back already in process, returning\n");
232 if (max_to_wb + curHead > descLen()) {
233 max_to_wb = descLen() - curHead;
235 // this is by definition aligned correctly
236 } else if (aMask != 0) {
237 // align the wb point to the mask
238 max_to_wb = max_to_wb & ~aMask;
241 DPRINTF(EthernetDesc, "Writing back %d descriptors\n", max_to_wb);
243 if (max_to_wb <= 0 || wbOut)
248 for (int x = 0; x < wbOut; x++)
249 memcpy(&wbBuf[x], usedCache[x], sizeof(T));
251 for (int x = 0; x < wbOut; x++) {
252 assert(usedCache.size());
254 usedCache.pop_front();
259 igbe->dmaWrite(igbe->platform->pciToDma(descBase() + curHead * sizeof(T)),
260 wbOut * sizeof(T), &wbEvent, (uint8_t*)wbBuf);
263 /** Fetch a chunk of descriptors into the descriptor cache.
264 * Calls fetchComplete when the memory system returns the data
266 void fetchDescriptors()
268 size_t max_to_fetch = descTail() - cachePnt;
269 if (max_to_fetch < 0)
270 max_to_fetch = descLen() - cachePnt;
272 max_to_fetch = std::min(max_to_fetch, (size - usedCache.size() -
273 unusedCache.size()));
275 DPRINTF(EthernetDesc, "Fetching descriptors head: %d tail: "
276 "%d len: %d cachePnt: %d max_to_fetch: %d descleft: %d\n",
277 descHead(), descTail(), descLen(), cachePnt,
278 max_to_fetch, descLeft());
281 if (max_to_fetch == 0 || curFetching)
284 // So we don't have two descriptor fetches going on at once
285 curFetching = max_to_fetch;
287 DPRINTF(EthernetDesc, "Fetching descriptors at %#x (%#x), size: %#x\n",
288 descBase() + cachePnt * sizeof(T),
289 igbe->platform->pciToDma(descBase() + cachePnt * sizeof(T)),
290 curFetching * sizeof(T));
293 igbe->dmaRead(igbe->platform->pciToDma(descBase() + cachePnt * sizeof(T)),
294 curFetching * sizeof(T), &fetchEvent, (uint8_t*)fetchBuf);
298 /** Called by event when dma to read descriptors is completed
303 for (int x = 0; x < curFetching; x++) {
305 memcpy(newDesc, &fetchBuf[x], sizeof(T));
306 unusedCache.push_back(newDesc);
310 int oldCp = cachePnt;
313 cachePnt += curFetching;
314 if (cachePnt > descLen())
315 cachePnt -= descLen();
319 DPRINTF(EthernetDesc, "Fetching complete cachePnt %d -> %d\n",
326 EventWrapper<DescCache, &DescCache::fetchComplete> fetchEvent;
328 /** Called by event when dma to writeback descriptors is completed
332 long curHead = descHead();
334 long oldHead = curHead;
340 if (curHead > descLen())
346 DPRINTF(EthernetDesc, "Writeback complete curHead %d -> %d\n",
349 // If we still have more to wb, call wb now
351 DPRINTF(EthernetDesc, "Writeback has more todo\n");
352 writeback(wbAlignment);
358 EventWrapper<DescCache, &DescCache::wbComplete> wbEvent;
360 /* Return the number of descriptors left in the ring, so the device has
361 * a way to figure out if it needs to interrupt.
365 int left = unusedCache.size();
366 if (cachePnt - descTail() >= 0)
367 left += (cachePnt - descTail());
369 left += (descTail() - cachePnt);
374 /* Return the number of descriptors used and not written back.
376 int descUsed() const { return usedCache.size(); }
378 /* Return the number of cache unused descriptors we have. */
379 int descUnused() const {return unusedCache.size(); }
381 /* Get into a state where the descriptor address/head/etc colud be
385 DPRINTF(EthernetDesc, "Reseting descriptor cache\n");
386 for (int x = 0; x < usedCache.size(); x++)
388 for (int x = 0; x < unusedCache.size(); x++)
389 delete unusedCache[x];
398 class RxDescCache : public DescCache<iGbReg::RxDesc>
401 virtual Addr descBase() const { return igbe->regs.rdba(); }
402 virtual long descHead() const { return igbe->regs.rdh(); }
403 virtual long descLen() const { return igbe->regs.rdlen() >> 4; }
404 virtual long descTail() const { return igbe->regs.rdt(); }
405 virtual void updateHead(long h) { igbe->regs.rdh(h); }
406 virtual void enableSm();
411 RxDescCache(IGbE *i, std::string n, int s);
413 /** Write the given packet into the buffer(s) pointed to by the
414 * descriptor and update the book keeping. Should only be called when
415 * there are no dma's pending.
416 * @param packet ethernet packet to write
417 * @return if the packet could be written (there was a free descriptor)
419 bool writePacket(EthPacketPtr packet);
420 /** Called by event when dma to write packet is completed
424 /** Check if the dma on the packet has completed.
429 EventWrapper<RxDescCache, &RxDescCache::pktComplete> pktEvent;
432 friend class RxDescCache;
434 RxDescCache rxDescCache;
436 class TxDescCache : public DescCache<iGbReg::TxDesc>
439 virtual Addr descBase() const { return igbe->regs.tdba(); }
440 virtual long descHead() const { return igbe->regs.tdh(); }
441 virtual long descTail() const { return igbe->regs.tdt(); }
442 virtual long descLen() const { return igbe->regs.tdlen() >> 4; }
443 virtual void updateHead(long h) { igbe->regs.tdh(h); }
444 virtual void enableSm();
445 virtual void intAfterWb() const { igbe->postInterrupt(iGbReg::IT_TXDW);}
453 TxDescCache(IGbE *i, std::string n, int s);
455 /** Tell the cache to DMA a packet from main memory into its buffer and
456 * return the size the of the packet to reserve space in tx fifo.
457 * @return size of the packet
460 void getPacketData(EthPacketPtr p);
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 /** Called by event when dma to write packet is completed
476 EventWrapper<TxDescCache, &TxDescCache::pktComplete> pktEvent;
479 friend class TxDescCache;
481 TxDescCache txDescCache;
484 struct Params : public PciDev::Params
486 Net::EthAddr hardware_address;
487 bool use_flow_control;
490 int rx_desc_cache_size;
491 int tx_desc_cache_size;
495 IGbE(Params *params);
499 inline Tick cycles(int numCycles) const { return numCycles * clock; }
501 virtual Tick read(PacketPtr pkt);
502 virtual Tick write(PacketPtr pkt);
504 virtual Tick writeConfig(PacketPtr pkt);
506 bool ethRxPkt(EthPacketPtr packet);
509 void setEthInt(IGbEInt *i) { assert(!etherInt); etherInt = i; }
512 const Params *params() const {return (const Params *)_params; }
514 virtual void serialize(std::ostream &os);
515 virtual void unserialize(Checkpoint *cp, const std::string §ion);
520 class IGbEInt : public EtherInt
526 IGbEInt(const std::string &name, IGbE *d)
527 : EtherInt(name), dev(d)
528 { dev->setEthInt(this); }
530 virtual bool recvPacket(EthPacketPtr pkt) { return dev->ethRxPkt(pkt); }
531 virtual void sendDone() { dev->ethTxDone(); }
538 #endif //__DEV_I8254XGBE_HH__