ns_gige.hh revision 1730
1/* 2 * Copyright (c) 2004 The Regents of The University of Michigan 3 * All rights reserved. 4 * 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. 15 * 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. 27 */ 28 29/** @file 30 * Device module for modelling the National Semiconductor 31 * DP83820 ethernet controller 32 */ 33 34#ifndef __DEV_NS_GIGE_HH__ 35#define __DEV_NS_GIGE_HH__ 36 37#include "base/inet.hh" 38#include "base/statistics.hh" 39#include "dev/etherint.hh" 40#include "dev/etherpkt.hh" 41#include "dev/io_device.hh" 42#include "dev/ns_gige_reg.h" 43#include "dev/pcidev.hh" 44#include "dev/pktfifo.hh" 45#include "mem/bus/bus.hh" 46#include "sim/eventq.hh" 47 48/** 49 * Ethernet device registers 50 */ 51struct dp_regs { 52 uint32_t command; 53 uint32_t config; 54 uint32_t mear; 55 uint32_t ptscr; 56 uint32_t isr; 57 uint32_t imr; 58 uint32_t ier; 59 uint32_t ihr; 60 uint32_t txdp; 61 uint32_t txdp_hi; 62 uint32_t txcfg; 63 uint32_t gpior; 64 uint32_t rxdp; 65 uint32_t rxdp_hi; 66 uint32_t rxcfg; 67 uint32_t pqcr; 68 uint32_t wcsr; 69 uint32_t pcr; 70 uint32_t rfcr; 71 uint32_t rfdr; 72 uint32_t srr; 73 uint32_t mibc; 74 uint32_t vrcr; 75 uint32_t vtcr; 76 uint32_t vdr; 77 uint32_t ccsr; 78 uint32_t tbicr; 79 uint32_t tbisr; 80 uint32_t tanar; 81 uint32_t tanlpar; 82 uint32_t taner; 83 uint32_t tesr; 84}; 85 86struct dp_rom { 87 /** 88 * for perfect match memory. 89 * the linux driver doesn't use any other ROM 90 */ 91 uint8_t perfectMatch[ETH_ADDR_LEN]; 92}; 93 94class NSGigEInt; 95class PhysicalMemory; 96class BaseInterface; 97class HierParams; 98class Bus; 99class PciConfigAll; 100 101/** 102 * NS DP82830 Ethernet device model 103 */ 104class NSGigE : public PciDev 105{ 106 public: 107 /** Transmit State Machine states */ 108 enum TxState 109 { 110 txIdle, 111 txDescRefr, 112 txDescRead, 113 txFifoBlock, 114 txFragRead, 115 txDescWrite, 116 txAdvance 117 }; 118 119 /** Receive State Machine States */ 120 enum RxState 121 { 122 rxIdle, 123 rxDescRefr, 124 rxDescRead, 125 rxFifoBlock, 126 rxFragWrite, 127 rxDescWrite, 128 rxAdvance 129 }; 130 131 enum DmaState 132 { 133 dmaIdle, 134 dmaReading, 135 dmaWriting, 136 dmaReadWaiting, 137 dmaWriteWaiting 138 }; 139 140 private: 141 Addr addr; 142 static const Addr size = sizeof(dp_regs); 143 144 protected: 145 typedef std::deque<PacketPtr> pktbuf_t; 146 typedef pktbuf_t::iterator pktiter_t; 147 148 /** device register file */ 149 dp_regs regs; 150 dp_rom rom; 151 152 /** pci settings */ 153 bool ioEnable; 154#if 0 155 bool memEnable; 156 bool bmEnable; 157#endif 158 159 /*** BASIC STRUCTURES FOR TX/RX ***/ 160 /* Data FIFOs */ 161 PacketFifo txFifo; 162 PacketFifo rxFifo; 163 164 /** various helper vars */ 165 PacketPtr txPacket; 166 PacketPtr rxPacket; 167 uint8_t *txPacketBufPtr; 168 uint8_t *rxPacketBufPtr; 169 uint32_t txXferLen; 170 uint32_t rxXferLen; 171 bool rxDmaFree; 172 bool txDmaFree; 173 174 /** DescCaches */ 175 ns_desc txDescCache; 176 ns_desc rxDescCache; 177 178 /* state machine cycle time */ 179 Tick clock; 180 inline Tick cycles(int numCycles) const { return numCycles * clock; } 181 182 /* tx State Machine */ 183 TxState txState; 184 bool txEnable; 185 186 /** Current Transmit Descriptor Done */ 187 bool CTDD; 188 /** halt the tx state machine after next packet */ 189 bool txHalt; 190 /** ptr to the next byte in the current fragment */ 191 Addr txFragPtr; 192 /** count of bytes remaining in the current descriptor */ 193 uint32_t txDescCnt; 194 DmaState txDmaState; 195 196 /** rx State Machine */ 197 RxState rxState; 198 bool rxEnable; 199 200 /** Current Receive Descriptor Done */ 201 bool CRDD; 202 /** num of bytes in the current packet being drained from rxDataFifo */ 203 uint32_t rxPktBytes; 204 /** halt the rx state machine after current packet */ 205 bool rxHalt; 206 /** ptr to the next byte in current fragment */ 207 Addr rxFragPtr; 208 /** count of bytes remaining in the current descriptor */ 209 uint32_t rxDescCnt; 210 DmaState rxDmaState; 211 212 bool extstsEnable; 213 214 protected: 215 Tick dmaReadDelay; 216 Tick dmaWriteDelay; 217 218 Tick dmaReadFactor; 219 Tick dmaWriteFactor; 220 221 void *rxDmaData; 222 Addr rxDmaAddr; 223 int rxDmaLen; 224 bool doRxDmaRead(); 225 bool doRxDmaWrite(); 226 void rxDmaReadCopy(); 227 void rxDmaWriteCopy(); 228 229 void *txDmaData; 230 Addr txDmaAddr; 231 int txDmaLen; 232 bool doTxDmaRead(); 233 bool doTxDmaWrite(); 234 void txDmaReadCopy(); 235 void txDmaWriteCopy(); 236 237 void rxDmaReadDone(); 238 friend class EventWrapper<NSGigE, &NSGigE::rxDmaReadDone>; 239 EventWrapper<NSGigE, &NSGigE::rxDmaReadDone> rxDmaReadEvent; 240 241 void rxDmaWriteDone(); 242 friend class EventWrapper<NSGigE, &NSGigE::rxDmaWriteDone>; 243 EventWrapper<NSGigE, &NSGigE::rxDmaWriteDone> rxDmaWriteEvent; 244 245 void txDmaReadDone(); 246 friend class EventWrapper<NSGigE, &NSGigE::txDmaReadDone>; 247 EventWrapper<NSGigE, &NSGigE::txDmaReadDone> txDmaReadEvent; 248 249 void txDmaWriteDone(); 250 friend class EventWrapper<NSGigE, &NSGigE::txDmaWriteDone>; 251 EventWrapper<NSGigE, &NSGigE::txDmaWriteDone> txDmaWriteEvent; 252 253 bool dmaDescFree; 254 bool dmaDataFree; 255 256 257 protected: 258 Tick txDelay; 259 Tick rxDelay; 260 261 void txReset(); 262 void rxReset(); 263 void regsReset(); 264 265 void rxKick(); 266 Tick rxKickTick; 267 typedef EventWrapper<NSGigE, &NSGigE::rxKick> RxKickEvent; 268 friend void RxKickEvent::process(); 269 270 void txKick(); 271 Tick txKickTick; 272 typedef EventWrapper<NSGigE, &NSGigE::txKick> TxKickEvent; 273 friend void TxKickEvent::process(); 274 275 /** 276 * Retransmit event 277 */ 278 void transmit(); 279 void txEventTransmit() 280 { 281 transmit(); 282 if (txState == txFifoBlock) 283 txKick(); 284 } 285 typedef EventWrapper<NSGigE, &NSGigE::txEventTransmit> TxEvent; 286 friend void TxEvent::process(); 287 TxEvent txEvent; 288 289 void txDump() const; 290 void rxDump() const; 291 292 /** 293 * receive address filter 294 */ 295 bool rxFilterEnable; 296 bool rxFilter(const PacketPtr &packet); 297 bool acceptBroadcast; 298 bool acceptMulticast; 299 bool acceptUnicast; 300 bool acceptPerfect; 301 bool acceptArp; 302 303 PhysicalMemory *physmem; 304 305 /** 306 * Interrupt management 307 */ 308 void devIntrPost(uint32_t interrupts); 309 void devIntrClear(uint32_t interrupts); 310 void devIntrChangeMask(); 311 312 Tick intrDelay; 313 Tick intrTick; 314 bool cpuPendingIntr; 315 void cpuIntrPost(Tick when); 316 void cpuInterrupt(); 317 void cpuIntrClear(); 318 319 typedef EventWrapper<NSGigE, &NSGigE::cpuInterrupt> IntrEvent; 320 friend void IntrEvent::process(); 321 IntrEvent *intrEvent; 322 NSGigEInt *interface; 323 324 public: 325 struct Params : public PciDev::Params 326 { 327 PhysicalMemory *pmem; 328 HierParams *hier; 329 Bus *header_bus; 330 Bus *payload_bus; 331 Tick clock; 332 Tick intr_delay; 333 Tick tx_delay; 334 Tick rx_delay; 335 Tick pio_latency; 336 bool dma_desc_free; 337 bool dma_data_free; 338 Tick dma_read_delay; 339 Tick dma_write_delay; 340 Tick dma_read_factor; 341 Tick dma_write_factor; 342 bool rx_filter; 343 Net::EthAddr eaddr; 344 uint32_t tx_fifo_size; 345 uint32_t rx_fifo_size; 346 uint32_t m5reg; 347 bool dma_no_allocate; 348 }; 349 350 NSGigE(Params *params); 351 ~NSGigE(); 352 const Params *params() const { return (const Params *)_params; } 353 354 virtual void WriteConfig(int offset, int size, uint32_t data); 355 virtual void ReadConfig(int offset, int size, uint8_t *data); 356 357 virtual Fault read(MemReqPtr &req, uint8_t *data); 358 virtual Fault write(MemReqPtr &req, const uint8_t *data); 359 360 bool cpuIntrPending() const; 361 void cpuIntrAck() { cpuIntrClear(); } 362 363 bool recvPacket(PacketPtr packet); 364 void transferDone(); 365 366 void setInterface(NSGigEInt *i) { assert(!interface); interface = i; } 367 368 virtual void serialize(std::ostream &os); 369 virtual void unserialize(Checkpoint *cp, const std::string §ion); 370 371 public: 372 void regStats(); 373 374 private: 375 Stats::Scalar<> txBytes; 376 Stats::Scalar<> rxBytes; 377 Stats::Scalar<> txPackets; 378 Stats::Scalar<> rxPackets; 379 Stats::Scalar<> txIpChecksums; 380 Stats::Scalar<> rxIpChecksums; 381 Stats::Scalar<> txTcpChecksums; 382 Stats::Scalar<> rxTcpChecksums; 383 Stats::Scalar<> txUdpChecksums; 384 Stats::Scalar<> rxUdpChecksums; 385 Stats::Scalar<> descDmaReads; 386 Stats::Scalar<> descDmaWrites; 387 Stats::Scalar<> descDmaRdBytes; 388 Stats::Scalar<> descDmaWrBytes; 389 Stats::Formula totBandwidth; 390 Stats::Formula totPackets; 391 Stats::Formula totBytes; 392 Stats::Formula totPacketRate; 393 Stats::Formula txBandwidth; 394 Stats::Formula rxBandwidth; 395 Stats::Formula txPacketRate; 396 Stats::Formula rxPacketRate; 397 Stats::Scalar<> postedSwi; 398 Stats::Formula coalescedSwi; 399 Stats::Scalar<> totalSwi; 400 Stats::Scalar<> postedRxIdle; 401 Stats::Formula coalescedRxIdle; 402 Stats::Scalar<> totalRxIdle; 403 Stats::Scalar<> postedRxOk; 404 Stats::Formula coalescedRxOk; 405 Stats::Scalar<> totalRxOk; 406 Stats::Scalar<> postedRxDesc; 407 Stats::Formula coalescedRxDesc; 408 Stats::Scalar<> totalRxDesc; 409 Stats::Scalar<> postedTxOk; 410 Stats::Formula coalescedTxOk; 411 Stats::Scalar<> totalTxOk; 412 Stats::Scalar<> postedTxIdle; 413 Stats::Formula coalescedTxIdle; 414 Stats::Scalar<> totalTxIdle; 415 Stats::Scalar<> postedTxDesc; 416 Stats::Formula coalescedTxDesc; 417 Stats::Scalar<> totalTxDesc; 418 Stats::Scalar<> postedRxOrn; 419 Stats::Formula coalescedRxOrn; 420 Stats::Scalar<> totalRxOrn; 421 Stats::Formula coalescedTotal; 422 Stats::Scalar<> postedInterrupts; 423 Stats::Scalar<> droppedPackets; 424 425 public: 426 Tick cacheAccess(MemReqPtr &req); 427}; 428 429/* 430 * Ethernet Interface for an Ethernet Device 431 */ 432class NSGigEInt : public EtherInt 433{ 434 private: 435 NSGigE *dev; 436 437 public: 438 NSGigEInt(const std::string &name, NSGigE *d) 439 : EtherInt(name), dev(d) { dev->setInterface(this); } 440 441 virtual bool recvPacket(PacketPtr pkt) { return dev->recvPacket(pkt); } 442 virtual void sendDone() { dev->transferDone(); } 443}; 444 445#endif // __DEV_NS_GIGE_HH__ 446