rtl8139.c 100 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528
  1. /**
  2. * QEMU RTL8139 emulation
  3. *
  4. * Copyright (c) 2006 Igor Kovalenko
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. * Modifications:
  24. * 2006-Jan-28 Mark Malakanov : TSAD and CSCR implementation (for Windows driver)
  25. *
  26. * 2006-Apr-28 Juergen Lock : EEPROM emulation changes for FreeBSD driver
  27. * HW revision ID changes for FreeBSD driver
  28. *
  29. * 2006-Jul-01 Igor Kovalenko : Implemented loopback mode for FreeBSD driver
  30. * Corrected packet transfer reassembly routine for 8139C+ mode
  31. * Rearranged debugging print statements
  32. * Implemented PCI timer interrupt (disabled by default)
  33. * Implemented Tally Counters, increased VM load/save version
  34. * Implemented IP/TCP/UDP checksum task offloading
  35. *
  36. * 2006-Jul-04 Igor Kovalenko : Implemented TCP segmentation offloading
  37. * Fixed MTU=1500 for produced ethernet frames
  38. *
  39. * 2006-Jul-09 Igor Kovalenko : Fixed TCP header length calculation while processing
  40. * segmentation offloading
  41. * Removed slirp.h dependency
  42. * Added rx/tx buffer reset when enabling rx/tx operation
  43. *
  44. * 2010-Feb-04 Frediano Ziglio: Rewrote timer support using QEMU timer only
  45. * when strictly needed (required for for
  46. * Darwin)
  47. * 2011-Mar-22 Benjamin Poirier: Implemented VLAN offloading
  48. */
  49. /* For crc32 */
  50. #include <zlib.h>
  51. #include "hw.h"
  52. #include "pci.h"
  53. #include "qemu-timer.h"
  54. #include "net.h"
  55. #include "loader.h"
  56. #include "sysemu.h"
  57. #include "iov.h"
  58. /* debug RTL8139 card */
  59. //#define DEBUG_RTL8139 1
  60. #define PCI_FREQUENCY 33000000L
  61. /* debug RTL8139 card C+ mode only */
  62. //#define DEBUG_RTL8139CP 1
  63. #define SET_MASKED(input, mask, curr) \
  64. ( ( (input) & ~(mask) ) | ( (curr) & (mask) ) )
  65. /* arg % size for size which is a power of 2 */
  66. #define MOD2(input, size) \
  67. ( ( input ) & ( size - 1 ) )
  68. #define ETHER_ADDR_LEN 6
  69. #define ETHER_TYPE_LEN 2
  70. #define ETH_HLEN (ETHER_ADDR_LEN * 2 + ETHER_TYPE_LEN)
  71. #define ETH_P_IP 0x0800 /* Internet Protocol packet */
  72. #define ETH_P_8021Q 0x8100 /* 802.1Q VLAN Extended Header */
  73. #define ETH_MTU 1500
  74. #define VLAN_TCI_LEN 2
  75. #define VLAN_HLEN (ETHER_TYPE_LEN + VLAN_TCI_LEN)
  76. #if defined (DEBUG_RTL8139)
  77. # define DPRINTF(fmt, ...) \
  78. do { fprintf(stderr, "RTL8139: " fmt, ## __VA_ARGS__); } while (0)
  79. #else
  80. static inline GCC_FMT_ATTR(1, 2) int DPRINTF(const char *fmt, ...)
  81. {
  82. return 0;
  83. }
  84. #endif
  85. /* Symbolic offsets to registers. */
  86. enum RTL8139_registers {
  87. MAC0 = 0, /* Ethernet hardware address. */
  88. MAR0 = 8, /* Multicast filter. */
  89. TxStatus0 = 0x10,/* Transmit status (Four 32bit registers). C mode only */
  90. /* Dump Tally Conter control register(64bit). C+ mode only */
  91. TxAddr0 = 0x20, /* Tx descriptors (also four 32bit). */
  92. RxBuf = 0x30,
  93. ChipCmd = 0x37,
  94. RxBufPtr = 0x38,
  95. RxBufAddr = 0x3A,
  96. IntrMask = 0x3C,
  97. IntrStatus = 0x3E,
  98. TxConfig = 0x40,
  99. RxConfig = 0x44,
  100. Timer = 0x48, /* A general-purpose counter. */
  101. RxMissed = 0x4C, /* 24 bits valid, write clears. */
  102. Cfg9346 = 0x50,
  103. Config0 = 0x51,
  104. Config1 = 0x52,
  105. FlashReg = 0x54,
  106. MediaStatus = 0x58,
  107. Config3 = 0x59,
  108. Config4 = 0x5A, /* absent on RTL-8139A */
  109. HltClk = 0x5B,
  110. MultiIntr = 0x5C,
  111. PCIRevisionID = 0x5E,
  112. TxSummary = 0x60, /* TSAD register. Transmit Status of All Descriptors*/
  113. BasicModeCtrl = 0x62,
  114. BasicModeStatus = 0x64,
  115. NWayAdvert = 0x66,
  116. NWayLPAR = 0x68,
  117. NWayExpansion = 0x6A,
  118. /* Undocumented registers, but required for proper operation. */
  119. FIFOTMS = 0x70, /* FIFO Control and test. */
  120. CSCR = 0x74, /* Chip Status and Configuration Register. */
  121. PARA78 = 0x78,
  122. PARA7c = 0x7c, /* Magic transceiver parameter register. */
  123. Config5 = 0xD8, /* absent on RTL-8139A */
  124. /* C+ mode */
  125. TxPoll = 0xD9, /* Tell chip to check Tx descriptors for work */
  126. RxMaxSize = 0xDA, /* Max size of an Rx packet (8169 only) */
  127. CpCmd = 0xE0, /* C+ Command register (C+ mode only) */
  128. IntrMitigate = 0xE2, /* rx/tx interrupt mitigation control */
  129. RxRingAddrLO = 0xE4, /* 64-bit start addr of Rx ring */
  130. RxRingAddrHI = 0xE8, /* 64-bit start addr of Rx ring */
  131. TxThresh = 0xEC, /* Early Tx threshold */
  132. };
  133. enum ClearBitMasks {
  134. MultiIntrClear = 0xF000,
  135. ChipCmdClear = 0xE2,
  136. Config1Clear = (1<<7)|(1<<6)|(1<<3)|(1<<2)|(1<<1),
  137. };
  138. enum ChipCmdBits {
  139. CmdReset = 0x10,
  140. CmdRxEnb = 0x08,
  141. CmdTxEnb = 0x04,
  142. RxBufEmpty = 0x01,
  143. };
  144. /* C+ mode */
  145. enum CplusCmdBits {
  146. CPlusRxVLAN = 0x0040, /* enable receive VLAN detagging */
  147. CPlusRxChkSum = 0x0020, /* enable receive checksum offloading */
  148. CPlusRxEnb = 0x0002,
  149. CPlusTxEnb = 0x0001,
  150. };
  151. /* Interrupt register bits, using my own meaningful names. */
  152. enum IntrStatusBits {
  153. PCIErr = 0x8000,
  154. PCSTimeout = 0x4000,
  155. RxFIFOOver = 0x40,
  156. RxUnderrun = 0x20,
  157. RxOverflow = 0x10,
  158. TxErr = 0x08,
  159. TxOK = 0x04,
  160. RxErr = 0x02,
  161. RxOK = 0x01,
  162. RxAckBits = RxFIFOOver | RxOverflow | RxOK,
  163. };
  164. enum TxStatusBits {
  165. TxHostOwns = 0x2000,
  166. TxUnderrun = 0x4000,
  167. TxStatOK = 0x8000,
  168. TxOutOfWindow = 0x20000000,
  169. TxAborted = 0x40000000,
  170. TxCarrierLost = 0x80000000,
  171. };
  172. enum RxStatusBits {
  173. RxMulticast = 0x8000,
  174. RxPhysical = 0x4000,
  175. RxBroadcast = 0x2000,
  176. RxBadSymbol = 0x0020,
  177. RxRunt = 0x0010,
  178. RxTooLong = 0x0008,
  179. RxCRCErr = 0x0004,
  180. RxBadAlign = 0x0002,
  181. RxStatusOK = 0x0001,
  182. };
  183. /* Bits in RxConfig. */
  184. enum rx_mode_bits {
  185. AcceptErr = 0x20,
  186. AcceptRunt = 0x10,
  187. AcceptBroadcast = 0x08,
  188. AcceptMulticast = 0x04,
  189. AcceptMyPhys = 0x02,
  190. AcceptAllPhys = 0x01,
  191. };
  192. /* Bits in TxConfig. */
  193. enum tx_config_bits {
  194. /* Interframe Gap Time. Only TxIFG96 doesn't violate IEEE 802.3 */
  195. TxIFGShift = 24,
  196. TxIFG84 = (0 << TxIFGShift), /* 8.4us / 840ns (10 / 100Mbps) */
  197. TxIFG88 = (1 << TxIFGShift), /* 8.8us / 880ns (10 / 100Mbps) */
  198. TxIFG92 = (2 << TxIFGShift), /* 9.2us / 920ns (10 / 100Mbps) */
  199. TxIFG96 = (3 << TxIFGShift), /* 9.6us / 960ns (10 / 100Mbps) */
  200. TxLoopBack = (1 << 18) | (1 << 17), /* enable loopback test mode */
  201. TxCRC = (1 << 16), /* DISABLE appending CRC to end of Tx packets */
  202. TxClearAbt = (1 << 0), /* Clear abort (WO) */
  203. TxDMAShift = 8, /* DMA burst value (0-7) is shifted this many bits */
  204. TxRetryShift = 4, /* TXRR value (0-15) is shifted this many bits */
  205. TxVersionMask = 0x7C800000, /* mask out version bits 30-26, 23 */
  206. };
  207. /* Transmit Status of All Descriptors (TSAD) Register */
  208. enum TSAD_bits {
  209. TSAD_TOK3 = 1<<15, // TOK bit of Descriptor 3
  210. TSAD_TOK2 = 1<<14, // TOK bit of Descriptor 2
  211. TSAD_TOK1 = 1<<13, // TOK bit of Descriptor 1
  212. TSAD_TOK0 = 1<<12, // TOK bit of Descriptor 0
  213. TSAD_TUN3 = 1<<11, // TUN bit of Descriptor 3
  214. TSAD_TUN2 = 1<<10, // TUN bit of Descriptor 2
  215. TSAD_TUN1 = 1<<9, // TUN bit of Descriptor 1
  216. TSAD_TUN0 = 1<<8, // TUN bit of Descriptor 0
  217. TSAD_TABT3 = 1<<07, // TABT bit of Descriptor 3
  218. TSAD_TABT2 = 1<<06, // TABT bit of Descriptor 2
  219. TSAD_TABT1 = 1<<05, // TABT bit of Descriptor 1
  220. TSAD_TABT0 = 1<<04, // TABT bit of Descriptor 0
  221. TSAD_OWN3 = 1<<03, // OWN bit of Descriptor 3
  222. TSAD_OWN2 = 1<<02, // OWN bit of Descriptor 2
  223. TSAD_OWN1 = 1<<01, // OWN bit of Descriptor 1
  224. TSAD_OWN0 = 1<<00, // OWN bit of Descriptor 0
  225. };
  226. /* Bits in Config1 */
  227. enum Config1Bits {
  228. Cfg1_PM_Enable = 0x01,
  229. Cfg1_VPD_Enable = 0x02,
  230. Cfg1_PIO = 0x04,
  231. Cfg1_MMIO = 0x08,
  232. LWAKE = 0x10, /* not on 8139, 8139A */
  233. Cfg1_Driver_Load = 0x20,
  234. Cfg1_LED0 = 0x40,
  235. Cfg1_LED1 = 0x80,
  236. SLEEP = (1 << 1), /* only on 8139, 8139A */
  237. PWRDN = (1 << 0), /* only on 8139, 8139A */
  238. };
  239. /* Bits in Config3 */
  240. enum Config3Bits {
  241. Cfg3_FBtBEn = (1 << 0), /* 1 = Fast Back to Back */
  242. Cfg3_FuncRegEn = (1 << 1), /* 1 = enable CardBus Function registers */
  243. Cfg3_CLKRUN_En = (1 << 2), /* 1 = enable CLKRUN */
  244. Cfg3_CardB_En = (1 << 3), /* 1 = enable CardBus registers */
  245. Cfg3_LinkUp = (1 << 4), /* 1 = wake up on link up */
  246. Cfg3_Magic = (1 << 5), /* 1 = wake up on Magic Packet (tm) */
  247. Cfg3_PARM_En = (1 << 6), /* 0 = software can set twister parameters */
  248. Cfg3_GNTSel = (1 << 7), /* 1 = delay 1 clock from PCI GNT signal */
  249. };
  250. /* Bits in Config4 */
  251. enum Config4Bits {
  252. LWPTN = (1 << 2), /* not on 8139, 8139A */
  253. };
  254. /* Bits in Config5 */
  255. enum Config5Bits {
  256. Cfg5_PME_STS = (1 << 0), /* 1 = PCI reset resets PME_Status */
  257. Cfg5_LANWake = (1 << 1), /* 1 = enable LANWake signal */
  258. Cfg5_LDPS = (1 << 2), /* 0 = save power when link is down */
  259. Cfg5_FIFOAddrPtr = (1 << 3), /* Realtek internal SRAM testing */
  260. Cfg5_UWF = (1 << 4), /* 1 = accept unicast wakeup frame */
  261. Cfg5_MWF = (1 << 5), /* 1 = accept multicast wakeup frame */
  262. Cfg5_BWF = (1 << 6), /* 1 = accept broadcast wakeup frame */
  263. };
  264. enum RxConfigBits {
  265. /* rx fifo threshold */
  266. RxCfgFIFOShift = 13,
  267. RxCfgFIFONone = (7 << RxCfgFIFOShift),
  268. /* Max DMA burst */
  269. RxCfgDMAShift = 8,
  270. RxCfgDMAUnlimited = (7 << RxCfgDMAShift),
  271. /* rx ring buffer length */
  272. RxCfgRcv8K = 0,
  273. RxCfgRcv16K = (1 << 11),
  274. RxCfgRcv32K = (1 << 12),
  275. RxCfgRcv64K = (1 << 11) | (1 << 12),
  276. /* Disable packet wrap at end of Rx buffer. (not possible with 64k) */
  277. RxNoWrap = (1 << 7),
  278. };
  279. /* Twister tuning parameters from RealTek.
  280. Completely undocumented, but required to tune bad links on some boards. */
  281. /*
  282. enum CSCRBits {
  283. CSCR_LinkOKBit = 0x0400,
  284. CSCR_LinkChangeBit = 0x0800,
  285. CSCR_LinkStatusBits = 0x0f000,
  286. CSCR_LinkDownOffCmd = 0x003c0,
  287. CSCR_LinkDownCmd = 0x0f3c0,
  288. */
  289. enum CSCRBits {
  290. CSCR_Testfun = 1<<15, /* 1 = Auto-neg speeds up internal timer, WO, def 0 */
  291. CSCR_LD = 1<<9, /* Active low TPI link disable signal. When low, TPI still transmits link pulses and TPI stays in good link state. def 1*/
  292. CSCR_HEART_BIT = 1<<8, /* 1 = HEART BEAT enable, 0 = HEART BEAT disable. HEART BEAT function is only valid in 10Mbps mode. def 1*/
  293. CSCR_JBEN = 1<<7, /* 1 = enable jabber function. 0 = disable jabber function, def 1*/
  294. CSCR_F_LINK_100 = 1<<6, /* Used to login force good link in 100Mbps for diagnostic purposes. 1 = DISABLE, 0 = ENABLE. def 1*/
  295. CSCR_F_Connect = 1<<5, /* Assertion of this bit forces the disconnect function to be bypassed. def 0*/
  296. CSCR_Con_status = 1<<3, /* This bit indicates the status of the connection. 1 = valid connected link detected; 0 = disconnected link detected. RO def 0*/
  297. CSCR_Con_status_En = 1<<2, /* Assertion of this bit configures LED1 pin to indicate connection status. def 0*/
  298. CSCR_PASS_SCR = 1<<0, /* Bypass Scramble, def 0*/
  299. };
  300. enum Cfg9346Bits {
  301. Cfg9346_Lock = 0x00,
  302. Cfg9346_Unlock = 0xC0,
  303. };
  304. typedef enum {
  305. CH_8139 = 0,
  306. CH_8139_K,
  307. CH_8139A,
  308. CH_8139A_G,
  309. CH_8139B,
  310. CH_8130,
  311. CH_8139C,
  312. CH_8100,
  313. CH_8100B_8139D,
  314. CH_8101,
  315. } chip_t;
  316. enum chip_flags {
  317. HasHltClk = (1 << 0),
  318. HasLWake = (1 << 1),
  319. };
  320. #define HW_REVID(b30, b29, b28, b27, b26, b23, b22) \
  321. (b30<<30 | b29<<29 | b28<<28 | b27<<27 | b26<<26 | b23<<23 | b22<<22)
  322. #define HW_REVID_MASK HW_REVID(1, 1, 1, 1, 1, 1, 1)
  323. #define RTL8139_PCI_REVID_8139 0x10
  324. #define RTL8139_PCI_REVID_8139CPLUS 0x20
  325. #define RTL8139_PCI_REVID RTL8139_PCI_REVID_8139CPLUS
  326. /* Size is 64 * 16bit words */
  327. #define EEPROM_9346_ADDR_BITS 6
  328. #define EEPROM_9346_SIZE (1 << EEPROM_9346_ADDR_BITS)
  329. #define EEPROM_9346_ADDR_MASK (EEPROM_9346_SIZE - 1)
  330. enum Chip9346Operation
  331. {
  332. Chip9346_op_mask = 0xc0, /* 10 zzzzzz */
  333. Chip9346_op_read = 0x80, /* 10 AAAAAA */
  334. Chip9346_op_write = 0x40, /* 01 AAAAAA D(15)..D(0) */
  335. Chip9346_op_ext_mask = 0xf0, /* 11 zzzzzz */
  336. Chip9346_op_write_enable = 0x30, /* 00 11zzzz */
  337. Chip9346_op_write_all = 0x10, /* 00 01zzzz */
  338. Chip9346_op_write_disable = 0x00, /* 00 00zzzz */
  339. };
  340. enum Chip9346Mode
  341. {
  342. Chip9346_none = 0,
  343. Chip9346_enter_command_mode,
  344. Chip9346_read_command,
  345. Chip9346_data_read, /* from output register */
  346. Chip9346_data_write, /* to input register, then to contents at specified address */
  347. Chip9346_data_write_all, /* to input register, then filling contents */
  348. };
  349. typedef struct EEprom9346
  350. {
  351. uint16_t contents[EEPROM_9346_SIZE];
  352. int mode;
  353. uint32_t tick;
  354. uint8_t address;
  355. uint16_t input;
  356. uint16_t output;
  357. uint8_t eecs;
  358. uint8_t eesk;
  359. uint8_t eedi;
  360. uint8_t eedo;
  361. } EEprom9346;
  362. typedef struct RTL8139TallyCounters
  363. {
  364. /* Tally counters */
  365. uint64_t TxOk;
  366. uint64_t RxOk;
  367. uint64_t TxERR;
  368. uint32_t RxERR;
  369. uint16_t MissPkt;
  370. uint16_t FAE;
  371. uint32_t Tx1Col;
  372. uint32_t TxMCol;
  373. uint64_t RxOkPhy;
  374. uint64_t RxOkBrd;
  375. uint32_t RxOkMul;
  376. uint16_t TxAbt;
  377. uint16_t TxUndrn;
  378. } RTL8139TallyCounters;
  379. /* Clears all tally counters */
  380. static void RTL8139TallyCounters_clear(RTL8139TallyCounters* counters);
  381. /* Writes tally counters to specified physical memory address */
  382. static void RTL8139TallyCounters_physical_memory_write(target_phys_addr_t tc_addr, RTL8139TallyCounters* counters);
  383. typedef struct RTL8139State {
  384. PCIDevice dev;
  385. uint8_t phys[8]; /* mac address */
  386. uint8_t mult[8]; /* multicast mask array */
  387. uint32_t TxStatus[4]; /* TxStatus0 in C mode*/ /* also DTCCR[0] and DTCCR[1] in C+ mode */
  388. uint32_t TxAddr[4]; /* TxAddr0 */
  389. uint32_t RxBuf; /* Receive buffer */
  390. uint32_t RxBufferSize;/* internal variable, receive ring buffer size in C mode */
  391. uint32_t RxBufPtr;
  392. uint32_t RxBufAddr;
  393. uint16_t IntrStatus;
  394. uint16_t IntrMask;
  395. uint32_t TxConfig;
  396. uint32_t RxConfig;
  397. uint32_t RxMissed;
  398. uint16_t CSCR;
  399. uint8_t Cfg9346;
  400. uint8_t Config0;
  401. uint8_t Config1;
  402. uint8_t Config3;
  403. uint8_t Config4;
  404. uint8_t Config5;
  405. uint8_t clock_enabled;
  406. uint8_t bChipCmdState;
  407. uint16_t MultiIntr;
  408. uint16_t BasicModeCtrl;
  409. uint16_t BasicModeStatus;
  410. uint16_t NWayAdvert;
  411. uint16_t NWayLPAR;
  412. uint16_t NWayExpansion;
  413. uint16_t CpCmd;
  414. uint8_t TxThresh;
  415. NICState *nic;
  416. NICConf conf;
  417. int rtl8139_mmio_io_addr;
  418. /* C ring mode */
  419. uint32_t currTxDesc;
  420. /* C+ mode */
  421. uint32_t cplus_enabled;
  422. uint32_t currCPlusRxDesc;
  423. uint32_t currCPlusTxDesc;
  424. uint32_t RxRingAddrLO;
  425. uint32_t RxRingAddrHI;
  426. EEprom9346 eeprom;
  427. uint32_t TCTR;
  428. uint32_t TimerInt;
  429. int64_t TCTR_base;
  430. /* Tally counters */
  431. RTL8139TallyCounters tally_counters;
  432. /* Non-persistent data */
  433. uint8_t *cplus_txbuffer;
  434. int cplus_txbuffer_len;
  435. int cplus_txbuffer_offset;
  436. /* PCI interrupt timer */
  437. QEMUTimer *timer;
  438. int64_t TimerExpire;
  439. /* Support migration to/from old versions */
  440. int rtl8139_mmio_io_addr_dummy;
  441. } RTL8139State;
  442. static void rtl8139_set_next_tctr_time(RTL8139State *s, int64_t current_time);
  443. static void prom9346_decode_command(EEprom9346 *eeprom, uint8_t command)
  444. {
  445. DPRINTF("eeprom command 0x%02x\n", command);
  446. switch (command & Chip9346_op_mask)
  447. {
  448. case Chip9346_op_read:
  449. {
  450. eeprom->address = command & EEPROM_9346_ADDR_MASK;
  451. eeprom->output = eeprom->contents[eeprom->address];
  452. eeprom->eedo = 0;
  453. eeprom->tick = 0;
  454. eeprom->mode = Chip9346_data_read;
  455. DPRINTF("eeprom read from address 0x%02x data=0x%04x\n",
  456. eeprom->address, eeprom->output);
  457. }
  458. break;
  459. case Chip9346_op_write:
  460. {
  461. eeprom->address = command & EEPROM_9346_ADDR_MASK;
  462. eeprom->input = 0;
  463. eeprom->tick = 0;
  464. eeprom->mode = Chip9346_none; /* Chip9346_data_write */
  465. DPRINTF("eeprom begin write to address 0x%02x\n",
  466. eeprom->address);
  467. }
  468. break;
  469. default:
  470. eeprom->mode = Chip9346_none;
  471. switch (command & Chip9346_op_ext_mask)
  472. {
  473. case Chip9346_op_write_enable:
  474. DPRINTF("eeprom write enabled\n");
  475. break;
  476. case Chip9346_op_write_all:
  477. DPRINTF("eeprom begin write all\n");
  478. break;
  479. case Chip9346_op_write_disable:
  480. DPRINTF("eeprom write disabled\n");
  481. break;
  482. }
  483. break;
  484. }
  485. }
  486. static void prom9346_shift_clock(EEprom9346 *eeprom)
  487. {
  488. int bit = eeprom->eedi?1:0;
  489. ++ eeprom->tick;
  490. DPRINTF("eeprom: tick %d eedi=%d eedo=%d\n", eeprom->tick, eeprom->eedi,
  491. eeprom->eedo);
  492. switch (eeprom->mode)
  493. {
  494. case Chip9346_enter_command_mode:
  495. if (bit)
  496. {
  497. eeprom->mode = Chip9346_read_command;
  498. eeprom->tick = 0;
  499. eeprom->input = 0;
  500. DPRINTF("eeprom: +++ synchronized, begin command read\n");
  501. }
  502. break;
  503. case Chip9346_read_command:
  504. eeprom->input = (eeprom->input << 1) | (bit & 1);
  505. if (eeprom->tick == 8)
  506. {
  507. prom9346_decode_command(eeprom, eeprom->input & 0xff);
  508. }
  509. break;
  510. case Chip9346_data_read:
  511. eeprom->eedo = (eeprom->output & 0x8000)?1:0;
  512. eeprom->output <<= 1;
  513. if (eeprom->tick == 16)
  514. {
  515. #if 1
  516. // the FreeBSD drivers (rl and re) don't explicitly toggle
  517. // CS between reads (or does setting Cfg9346 to 0 count too?),
  518. // so we need to enter wait-for-command state here
  519. eeprom->mode = Chip9346_enter_command_mode;
  520. eeprom->input = 0;
  521. eeprom->tick = 0;
  522. DPRINTF("eeprom: +++ end of read, awaiting next command\n");
  523. #else
  524. // original behaviour
  525. ++eeprom->address;
  526. eeprom->address &= EEPROM_9346_ADDR_MASK;
  527. eeprom->output = eeprom->contents[eeprom->address];
  528. eeprom->tick = 0;
  529. DPRINTF("eeprom: +++ read next address 0x%02x data=0x%04x\n",
  530. eeprom->address, eeprom->output);
  531. #endif
  532. }
  533. break;
  534. case Chip9346_data_write:
  535. eeprom->input = (eeprom->input << 1) | (bit & 1);
  536. if (eeprom->tick == 16)
  537. {
  538. DPRINTF("eeprom write to address 0x%02x data=0x%04x\n",
  539. eeprom->address, eeprom->input);
  540. eeprom->contents[eeprom->address] = eeprom->input;
  541. eeprom->mode = Chip9346_none; /* waiting for next command after CS cycle */
  542. eeprom->tick = 0;
  543. eeprom->input = 0;
  544. }
  545. break;
  546. case Chip9346_data_write_all:
  547. eeprom->input = (eeprom->input << 1) | (bit & 1);
  548. if (eeprom->tick == 16)
  549. {
  550. int i;
  551. for (i = 0; i < EEPROM_9346_SIZE; i++)
  552. {
  553. eeprom->contents[i] = eeprom->input;
  554. }
  555. DPRINTF("eeprom filled with data=0x%04x\n", eeprom->input);
  556. eeprom->mode = Chip9346_enter_command_mode;
  557. eeprom->tick = 0;
  558. eeprom->input = 0;
  559. }
  560. break;
  561. default:
  562. break;
  563. }
  564. }
  565. static int prom9346_get_wire(RTL8139State *s)
  566. {
  567. EEprom9346 *eeprom = &s->eeprom;
  568. if (!eeprom->eecs)
  569. return 0;
  570. return eeprom->eedo;
  571. }
  572. /* FIXME: This should be merged into/replaced by eeprom93xx.c. */
  573. static void prom9346_set_wire(RTL8139State *s, int eecs, int eesk, int eedi)
  574. {
  575. EEprom9346 *eeprom = &s->eeprom;
  576. uint8_t old_eecs = eeprom->eecs;
  577. uint8_t old_eesk = eeprom->eesk;
  578. eeprom->eecs = eecs;
  579. eeprom->eesk = eesk;
  580. eeprom->eedi = eedi;
  581. DPRINTF("eeprom: +++ wires CS=%d SK=%d DI=%d DO=%d\n", eeprom->eecs,
  582. eeprom->eesk, eeprom->eedi, eeprom->eedo);
  583. if (!old_eecs && eecs)
  584. {
  585. /* Synchronize start */
  586. eeprom->tick = 0;
  587. eeprom->input = 0;
  588. eeprom->output = 0;
  589. eeprom->mode = Chip9346_enter_command_mode;
  590. DPRINTF("=== eeprom: begin access, enter command mode\n");
  591. }
  592. if (!eecs)
  593. {
  594. DPRINTF("=== eeprom: end access\n");
  595. return;
  596. }
  597. if (!old_eesk && eesk)
  598. {
  599. /* SK front rules */
  600. prom9346_shift_clock(eeprom);
  601. }
  602. }
  603. static void rtl8139_update_irq(RTL8139State *s)
  604. {
  605. int isr;
  606. isr = (s->IntrStatus & s->IntrMask) & 0xffff;
  607. DPRINTF("Set IRQ to %d (%04x %04x)\n", isr ? 1 : 0, s->IntrStatus,
  608. s->IntrMask);
  609. qemu_set_irq(s->dev.irq[0], (isr != 0));
  610. }
  611. #define POLYNOMIAL 0x04c11db6
  612. /* From FreeBSD */
  613. /* XXX: optimize */
  614. static int compute_mcast_idx(const uint8_t *ep)
  615. {
  616. uint32_t crc;
  617. int carry, i, j;
  618. uint8_t b;
  619. crc = 0xffffffff;
  620. for (i = 0; i < 6; i++) {
  621. b = *ep++;
  622. for (j = 0; j < 8; j++) {
  623. carry = ((crc & 0x80000000L) ? 1 : 0) ^ (b & 0x01);
  624. crc <<= 1;
  625. b >>= 1;
  626. if (carry)
  627. crc = ((crc ^ POLYNOMIAL) | carry);
  628. }
  629. }
  630. return (crc >> 26);
  631. }
  632. static int rtl8139_RxWrap(RTL8139State *s)
  633. {
  634. /* wrapping enabled; assume 1.5k more buffer space if size < 65536 */
  635. return (s->RxConfig & (1 << 7));
  636. }
  637. static int rtl8139_receiver_enabled(RTL8139State *s)
  638. {
  639. return s->bChipCmdState & CmdRxEnb;
  640. }
  641. static int rtl8139_transmitter_enabled(RTL8139State *s)
  642. {
  643. return s->bChipCmdState & CmdTxEnb;
  644. }
  645. static int rtl8139_cp_receiver_enabled(RTL8139State *s)
  646. {
  647. return s->CpCmd & CPlusRxEnb;
  648. }
  649. static int rtl8139_cp_transmitter_enabled(RTL8139State *s)
  650. {
  651. return s->CpCmd & CPlusTxEnb;
  652. }
  653. static void rtl8139_write_buffer(RTL8139State *s, const void *buf, int size)
  654. {
  655. if (s->RxBufAddr + size > s->RxBufferSize)
  656. {
  657. int wrapped = MOD2(s->RxBufAddr + size, s->RxBufferSize);
  658. /* write packet data */
  659. if (wrapped && !(s->RxBufferSize < 65536 && rtl8139_RxWrap(s)))
  660. {
  661. DPRINTF(">>> rx packet wrapped in buffer at %d\n", size - wrapped);
  662. if (size > wrapped)
  663. {
  664. cpu_physical_memory_write( s->RxBuf + s->RxBufAddr,
  665. buf, size-wrapped );
  666. }
  667. /* reset buffer pointer */
  668. s->RxBufAddr = 0;
  669. cpu_physical_memory_write( s->RxBuf + s->RxBufAddr,
  670. buf + (size-wrapped), wrapped );
  671. s->RxBufAddr = wrapped;
  672. return;
  673. }
  674. }
  675. /* non-wrapping path or overwrapping enabled */
  676. cpu_physical_memory_write( s->RxBuf + s->RxBufAddr, buf, size );
  677. s->RxBufAddr += size;
  678. }
  679. #define MIN_BUF_SIZE 60
  680. static inline target_phys_addr_t rtl8139_addr64(uint32_t low, uint32_t high)
  681. {
  682. #if TARGET_PHYS_ADDR_BITS > 32
  683. return low | ((target_phys_addr_t)high << 32);
  684. #else
  685. return low;
  686. #endif
  687. }
  688. static int rtl8139_can_receive(VLANClientState *nc)
  689. {
  690. RTL8139State *s = DO_UPCAST(NICState, nc, nc)->opaque;
  691. int avail;
  692. /* Receive (drop) packets if card is disabled. */
  693. if (!s->clock_enabled)
  694. return 1;
  695. if (!rtl8139_receiver_enabled(s))
  696. return 1;
  697. if (rtl8139_cp_receiver_enabled(s)) {
  698. /* ??? Flow control not implemented in c+ mode.
  699. This is a hack to work around slirp deficiencies anyway. */
  700. return 1;
  701. } else {
  702. avail = MOD2(s->RxBufferSize + s->RxBufPtr - s->RxBufAddr,
  703. s->RxBufferSize);
  704. return (avail == 0 || avail >= 1514);
  705. }
  706. }
  707. static ssize_t rtl8139_do_receive(VLANClientState *nc, const uint8_t *buf, size_t size_, int do_interrupt)
  708. {
  709. RTL8139State *s = DO_UPCAST(NICState, nc, nc)->opaque;
  710. /* size is the length of the buffer passed to the driver */
  711. int size = size_;
  712. const uint8_t *dot1q_buf = NULL;
  713. uint32_t packet_header = 0;
  714. uint8_t buf1[MIN_BUF_SIZE + VLAN_HLEN];
  715. static const uint8_t broadcast_macaddr[6] =
  716. { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
  717. DPRINTF(">>> received len=%d\n", size);
  718. /* test if board clock is stopped */
  719. if (!s->clock_enabled)
  720. {
  721. DPRINTF("stopped ==========================\n");
  722. return -1;
  723. }
  724. /* first check if receiver is enabled */
  725. if (!rtl8139_receiver_enabled(s))
  726. {
  727. DPRINTF("receiver disabled ================\n");
  728. return -1;
  729. }
  730. /* XXX: check this */
  731. if (s->RxConfig & AcceptAllPhys) {
  732. /* promiscuous: receive all */
  733. DPRINTF(">>> packet received in promiscuous mode\n");
  734. } else {
  735. if (!memcmp(buf, broadcast_macaddr, 6)) {
  736. /* broadcast address */
  737. if (!(s->RxConfig & AcceptBroadcast))
  738. {
  739. DPRINTF(">>> broadcast packet rejected\n");
  740. /* update tally counter */
  741. ++s->tally_counters.RxERR;
  742. return size;
  743. }
  744. packet_header |= RxBroadcast;
  745. DPRINTF(">>> broadcast packet received\n");
  746. /* update tally counter */
  747. ++s->tally_counters.RxOkBrd;
  748. } else if (buf[0] & 0x01) {
  749. /* multicast */
  750. if (!(s->RxConfig & AcceptMulticast))
  751. {
  752. DPRINTF(">>> multicast packet rejected\n");
  753. /* update tally counter */
  754. ++s->tally_counters.RxERR;
  755. return size;
  756. }
  757. int mcast_idx = compute_mcast_idx(buf);
  758. if (!(s->mult[mcast_idx >> 3] & (1 << (mcast_idx & 7))))
  759. {
  760. DPRINTF(">>> multicast address mismatch\n");
  761. /* update tally counter */
  762. ++s->tally_counters.RxERR;
  763. return size;
  764. }
  765. packet_header |= RxMulticast;
  766. DPRINTF(">>> multicast packet received\n");
  767. /* update tally counter */
  768. ++s->tally_counters.RxOkMul;
  769. } else if (s->phys[0] == buf[0] &&
  770. s->phys[1] == buf[1] &&
  771. s->phys[2] == buf[2] &&
  772. s->phys[3] == buf[3] &&
  773. s->phys[4] == buf[4] &&
  774. s->phys[5] == buf[5]) {
  775. /* match */
  776. if (!(s->RxConfig & AcceptMyPhys))
  777. {
  778. DPRINTF(">>> rejecting physical address matching packet\n");
  779. /* update tally counter */
  780. ++s->tally_counters.RxERR;
  781. return size;
  782. }
  783. packet_header |= RxPhysical;
  784. DPRINTF(">>> physical address matching packet received\n");
  785. /* update tally counter */
  786. ++s->tally_counters.RxOkPhy;
  787. } else {
  788. DPRINTF(">>> unknown packet\n");
  789. /* update tally counter */
  790. ++s->tally_counters.RxERR;
  791. return size;
  792. }
  793. }
  794. /* if too small buffer, then expand it
  795. * Include some tailroom in case a vlan tag is later removed. */
  796. if (size < MIN_BUF_SIZE + VLAN_HLEN) {
  797. memcpy(buf1, buf, size);
  798. memset(buf1 + size, 0, MIN_BUF_SIZE + VLAN_HLEN - size);
  799. buf = buf1;
  800. if (size < MIN_BUF_SIZE) {
  801. size = MIN_BUF_SIZE;
  802. }
  803. }
  804. if (rtl8139_cp_receiver_enabled(s))
  805. {
  806. DPRINTF("in C+ Rx mode ================\n");
  807. /* begin C+ receiver mode */
  808. /* w0 ownership flag */
  809. #define CP_RX_OWN (1<<31)
  810. /* w0 end of ring flag */
  811. #define CP_RX_EOR (1<<30)
  812. /* w0 bits 0...12 : buffer size */
  813. #define CP_RX_BUFFER_SIZE_MASK ((1<<13) - 1)
  814. /* w1 tag available flag */
  815. #define CP_RX_TAVA (1<<16)
  816. /* w1 bits 0...15 : VLAN tag */
  817. #define CP_RX_VLAN_TAG_MASK ((1<<16) - 1)
  818. /* w2 low 32bit of Rx buffer ptr */
  819. /* w3 high 32bit of Rx buffer ptr */
  820. int descriptor = s->currCPlusRxDesc;
  821. target_phys_addr_t cplus_rx_ring_desc;
  822. cplus_rx_ring_desc = rtl8139_addr64(s->RxRingAddrLO, s->RxRingAddrHI);
  823. cplus_rx_ring_desc += 16 * descriptor;
  824. DPRINTF("+++ C+ mode reading RX descriptor %d from host memory at "
  825. "%08x %08x = "TARGET_FMT_plx"\n", descriptor, s->RxRingAddrHI,
  826. s->RxRingAddrLO, cplus_rx_ring_desc);
  827. uint32_t val, rxdw0,rxdw1,rxbufLO,rxbufHI;
  828. cpu_physical_memory_read(cplus_rx_ring_desc, (uint8_t *)&val, 4);
  829. rxdw0 = le32_to_cpu(val);
  830. cpu_physical_memory_read(cplus_rx_ring_desc+4, (uint8_t *)&val, 4);
  831. rxdw1 = le32_to_cpu(val);
  832. cpu_physical_memory_read(cplus_rx_ring_desc+8, (uint8_t *)&val, 4);
  833. rxbufLO = le32_to_cpu(val);
  834. cpu_physical_memory_read(cplus_rx_ring_desc+12, (uint8_t *)&val, 4);
  835. rxbufHI = le32_to_cpu(val);
  836. DPRINTF("+++ C+ mode RX descriptor %d %08x %08x %08x %08x\n",
  837. descriptor, rxdw0, rxdw1, rxbufLO, rxbufHI);
  838. if (!(rxdw0 & CP_RX_OWN))
  839. {
  840. DPRINTF("C+ Rx mode : descriptor %d is owned by host\n",
  841. descriptor);
  842. s->IntrStatus |= RxOverflow;
  843. ++s->RxMissed;
  844. /* update tally counter */
  845. ++s->tally_counters.RxERR;
  846. ++s->tally_counters.MissPkt;
  847. rtl8139_update_irq(s);
  848. return size_;
  849. }
  850. uint32_t rx_space = rxdw0 & CP_RX_BUFFER_SIZE_MASK;
  851. /* write VLAN info to descriptor variables. */
  852. if (s->CpCmd & CPlusRxVLAN && be16_to_cpup((uint16_t *)
  853. &buf[ETHER_ADDR_LEN * 2]) == ETH_P_8021Q) {
  854. dot1q_buf = &buf[ETHER_ADDR_LEN * 2];
  855. size -= VLAN_HLEN;
  856. /* if too small buffer, use the tailroom added duing expansion */
  857. if (size < MIN_BUF_SIZE) {
  858. size = MIN_BUF_SIZE;
  859. }
  860. rxdw1 &= ~CP_RX_VLAN_TAG_MASK;
  861. /* BE + ~le_to_cpu()~ + cpu_to_le() = BE */
  862. rxdw1 |= CP_RX_TAVA | le16_to_cpup((uint16_t *)
  863. &dot1q_buf[ETHER_TYPE_LEN]);
  864. DPRINTF("C+ Rx mode : extracted vlan tag with tci: ""%u\n",
  865. be16_to_cpup((uint16_t *)&dot1q_buf[ETHER_TYPE_LEN]));
  866. } else {
  867. /* reset VLAN tag flag */
  868. rxdw1 &= ~CP_RX_TAVA;
  869. }
  870. /* TODO: scatter the packet over available receive ring descriptors space */
  871. if (size+4 > rx_space)
  872. {
  873. DPRINTF("C+ Rx mode : descriptor %d size %d received %d + 4\n",
  874. descriptor, rx_space, size);
  875. s->IntrStatus |= RxOverflow;
  876. ++s->RxMissed;
  877. /* update tally counter */
  878. ++s->tally_counters.RxERR;
  879. ++s->tally_counters.MissPkt;
  880. rtl8139_update_irq(s);
  881. return size_;
  882. }
  883. target_phys_addr_t rx_addr = rtl8139_addr64(rxbufLO, rxbufHI);
  884. /* receive/copy to target memory */
  885. if (dot1q_buf) {
  886. cpu_physical_memory_write(rx_addr, buf, 2 * ETHER_ADDR_LEN);
  887. cpu_physical_memory_write(rx_addr + 2 * ETHER_ADDR_LEN,
  888. buf + 2 * ETHER_ADDR_LEN + VLAN_HLEN,
  889. size - 2 * ETHER_ADDR_LEN);
  890. } else {
  891. cpu_physical_memory_write(rx_addr, buf, size);
  892. }
  893. if (s->CpCmd & CPlusRxChkSum)
  894. {
  895. /* do some packet checksumming */
  896. }
  897. /* write checksum */
  898. val = cpu_to_le32(crc32(0, buf, size_));
  899. cpu_physical_memory_write( rx_addr+size, (uint8_t *)&val, 4);
  900. /* first segment of received packet flag */
  901. #define CP_RX_STATUS_FS (1<<29)
  902. /* last segment of received packet flag */
  903. #define CP_RX_STATUS_LS (1<<28)
  904. /* multicast packet flag */
  905. #define CP_RX_STATUS_MAR (1<<26)
  906. /* physical-matching packet flag */
  907. #define CP_RX_STATUS_PAM (1<<25)
  908. /* broadcast packet flag */
  909. #define CP_RX_STATUS_BAR (1<<24)
  910. /* runt packet flag */
  911. #define CP_RX_STATUS_RUNT (1<<19)
  912. /* crc error flag */
  913. #define CP_RX_STATUS_CRC (1<<18)
  914. /* IP checksum error flag */
  915. #define CP_RX_STATUS_IPF (1<<15)
  916. /* UDP checksum error flag */
  917. #define CP_RX_STATUS_UDPF (1<<14)
  918. /* TCP checksum error flag */
  919. #define CP_RX_STATUS_TCPF (1<<13)
  920. /* transfer ownership to target */
  921. rxdw0 &= ~CP_RX_OWN;
  922. /* set first segment bit */
  923. rxdw0 |= CP_RX_STATUS_FS;
  924. /* set last segment bit */
  925. rxdw0 |= CP_RX_STATUS_LS;
  926. /* set received packet type flags */
  927. if (packet_header & RxBroadcast)
  928. rxdw0 |= CP_RX_STATUS_BAR;
  929. if (packet_header & RxMulticast)
  930. rxdw0 |= CP_RX_STATUS_MAR;
  931. if (packet_header & RxPhysical)
  932. rxdw0 |= CP_RX_STATUS_PAM;
  933. /* set received size */
  934. rxdw0 &= ~CP_RX_BUFFER_SIZE_MASK;
  935. rxdw0 |= (size+4);
  936. /* update ring data */
  937. val = cpu_to_le32(rxdw0);
  938. cpu_physical_memory_write(cplus_rx_ring_desc, (uint8_t *)&val, 4);
  939. val = cpu_to_le32(rxdw1);
  940. cpu_physical_memory_write(cplus_rx_ring_desc+4, (uint8_t *)&val, 4);
  941. /* update tally counter */
  942. ++s->tally_counters.RxOk;
  943. /* seek to next Rx descriptor */
  944. if (rxdw0 & CP_RX_EOR)
  945. {
  946. s->currCPlusRxDesc = 0;
  947. }
  948. else
  949. {
  950. ++s->currCPlusRxDesc;
  951. }
  952. DPRINTF("done C+ Rx mode ----------------\n");
  953. }
  954. else
  955. {
  956. DPRINTF("in ring Rx mode ================\n");
  957. /* begin ring receiver mode */
  958. int avail = MOD2(s->RxBufferSize + s->RxBufPtr - s->RxBufAddr, s->RxBufferSize);
  959. /* if receiver buffer is empty then avail == 0 */
  960. if (avail != 0 && size + 8 >= avail)
  961. {
  962. DPRINTF("rx overflow: rx buffer length %d head 0x%04x "
  963. "read 0x%04x === available 0x%04x need 0x%04x\n",
  964. s->RxBufferSize, s->RxBufAddr, s->RxBufPtr, avail, size + 8);
  965. s->IntrStatus |= RxOverflow;
  966. ++s->RxMissed;
  967. rtl8139_update_irq(s);
  968. return size_;
  969. }
  970. packet_header |= RxStatusOK;
  971. packet_header |= (((size+4) << 16) & 0xffff0000);
  972. /* write header */
  973. uint32_t val = cpu_to_le32(packet_header);
  974. rtl8139_write_buffer(s, (uint8_t *)&val, 4);
  975. rtl8139_write_buffer(s, buf, size);
  976. /* write checksum */
  977. val = cpu_to_le32(crc32(0, buf, size));
  978. rtl8139_write_buffer(s, (uint8_t *)&val, 4);
  979. /* correct buffer write pointer */
  980. s->RxBufAddr = MOD2((s->RxBufAddr + 3) & ~0x3, s->RxBufferSize);
  981. /* now we can signal we have received something */
  982. DPRINTF("received: rx buffer length %d head 0x%04x read 0x%04x\n",
  983. s->RxBufferSize, s->RxBufAddr, s->RxBufPtr);
  984. }
  985. s->IntrStatus |= RxOK;
  986. if (do_interrupt)
  987. {
  988. rtl8139_update_irq(s);
  989. }
  990. return size_;
  991. }
  992. static ssize_t rtl8139_receive(VLANClientState *nc, const uint8_t *buf, size_t size)
  993. {
  994. return rtl8139_do_receive(nc, buf, size, 1);
  995. }
  996. static void rtl8139_reset_rxring(RTL8139State *s, uint32_t bufferSize)
  997. {
  998. s->RxBufferSize = bufferSize;
  999. s->RxBufPtr = 0;
  1000. s->RxBufAddr = 0;
  1001. }
  1002. static void rtl8139_reset(DeviceState *d)
  1003. {
  1004. RTL8139State *s = container_of(d, RTL8139State, dev.qdev);
  1005. int i;
  1006. /* restore MAC address */
  1007. memcpy(s->phys, s->conf.macaddr.a, 6);
  1008. /* reset interrupt mask */
  1009. s->IntrStatus = 0;
  1010. s->IntrMask = 0;
  1011. rtl8139_update_irq(s);
  1012. /* mark all status registers as owned by host */
  1013. for (i = 0; i < 4; ++i)
  1014. {
  1015. s->TxStatus[i] = TxHostOwns;
  1016. }
  1017. s->currTxDesc = 0;
  1018. s->currCPlusRxDesc = 0;
  1019. s->currCPlusTxDesc = 0;
  1020. s->RxRingAddrLO = 0;
  1021. s->RxRingAddrHI = 0;
  1022. s->RxBuf = 0;
  1023. rtl8139_reset_rxring(s, 8192);
  1024. /* ACK the reset */
  1025. s->TxConfig = 0;
  1026. #if 0
  1027. // s->TxConfig |= HW_REVID(1, 0, 0, 0, 0, 0, 0); // RTL-8139 HasHltClk
  1028. s->clock_enabled = 0;
  1029. #else
  1030. s->TxConfig |= HW_REVID(1, 1, 1, 0, 1, 1, 0); // RTL-8139C+ HasLWake
  1031. s->clock_enabled = 1;
  1032. #endif
  1033. s->bChipCmdState = CmdReset; /* RxBufEmpty bit is calculated on read from ChipCmd */;
  1034. /* set initial state data */
  1035. s->Config0 = 0x0; /* No boot ROM */
  1036. s->Config1 = 0xC; /* IO mapped and MEM mapped registers available */
  1037. s->Config3 = 0x1; /* fast back-to-back compatible */
  1038. s->Config5 = 0x0;
  1039. s->CSCR = CSCR_F_LINK_100 | CSCR_HEART_BIT | CSCR_LD;
  1040. s->CpCmd = 0x0; /* reset C+ mode */
  1041. s->cplus_enabled = 0;
  1042. // s->BasicModeCtrl = 0x3100; // 100Mbps, full duplex, autonegotiation
  1043. // s->BasicModeCtrl = 0x2100; // 100Mbps, full duplex
  1044. s->BasicModeCtrl = 0x1000; // autonegotiation
  1045. s->BasicModeStatus = 0x7809;
  1046. //s->BasicModeStatus |= 0x0040; /* UTP medium */
  1047. s->BasicModeStatus |= 0x0020; /* autonegotiation completed */
  1048. s->BasicModeStatus |= 0x0004; /* link is up */
  1049. s->NWayAdvert = 0x05e1; /* all modes, full duplex */
  1050. s->NWayLPAR = 0x05e1; /* all modes, full duplex */
  1051. s->NWayExpansion = 0x0001; /* autonegotiation supported */
  1052. /* also reset timer and disable timer interrupt */
  1053. s->TCTR = 0;
  1054. s->TimerInt = 0;
  1055. s->TCTR_base = 0;
  1056. /* reset tally counters */
  1057. RTL8139TallyCounters_clear(&s->tally_counters);
  1058. }
  1059. static void RTL8139TallyCounters_clear(RTL8139TallyCounters* counters)
  1060. {
  1061. counters->TxOk = 0;
  1062. counters->RxOk = 0;
  1063. counters->TxERR = 0;
  1064. counters->RxERR = 0;
  1065. counters->MissPkt = 0;
  1066. counters->FAE = 0;
  1067. counters->Tx1Col = 0;
  1068. counters->TxMCol = 0;
  1069. counters->RxOkPhy = 0;
  1070. counters->RxOkBrd = 0;
  1071. counters->RxOkMul = 0;
  1072. counters->TxAbt = 0;
  1073. counters->TxUndrn = 0;
  1074. }
  1075. static void RTL8139TallyCounters_physical_memory_write(target_phys_addr_t tc_addr, RTL8139TallyCounters* tally_counters)
  1076. {
  1077. uint16_t val16;
  1078. uint32_t val32;
  1079. uint64_t val64;
  1080. val64 = cpu_to_le64(tally_counters->TxOk);
  1081. cpu_physical_memory_write(tc_addr + 0, (uint8_t *)&val64, 8);
  1082. val64 = cpu_to_le64(tally_counters->RxOk);
  1083. cpu_physical_memory_write(tc_addr + 8, (uint8_t *)&val64, 8);
  1084. val64 = cpu_to_le64(tally_counters->TxERR);
  1085. cpu_physical_memory_write(tc_addr + 16, (uint8_t *)&val64, 8);
  1086. val32 = cpu_to_le32(tally_counters->RxERR);
  1087. cpu_physical_memory_write(tc_addr + 24, (uint8_t *)&val32, 4);
  1088. val16 = cpu_to_le16(tally_counters->MissPkt);
  1089. cpu_physical_memory_write(tc_addr + 28, (uint8_t *)&val16, 2);
  1090. val16 = cpu_to_le16(tally_counters->FAE);
  1091. cpu_physical_memory_write(tc_addr + 30, (uint8_t *)&val16, 2);
  1092. val32 = cpu_to_le32(tally_counters->Tx1Col);
  1093. cpu_physical_memory_write(tc_addr + 32, (uint8_t *)&val32, 4);
  1094. val32 = cpu_to_le32(tally_counters->TxMCol);
  1095. cpu_physical_memory_write(tc_addr + 36, (uint8_t *)&val32, 4);
  1096. val64 = cpu_to_le64(tally_counters->RxOkPhy);
  1097. cpu_physical_memory_write(tc_addr + 40, (uint8_t *)&val64, 8);
  1098. val64 = cpu_to_le64(tally_counters->RxOkBrd);
  1099. cpu_physical_memory_write(tc_addr + 48, (uint8_t *)&val64, 8);
  1100. val32 = cpu_to_le32(tally_counters->RxOkMul);
  1101. cpu_physical_memory_write(tc_addr + 56, (uint8_t *)&val32, 4);
  1102. val16 = cpu_to_le16(tally_counters->TxAbt);
  1103. cpu_physical_memory_write(tc_addr + 60, (uint8_t *)&val16, 2);
  1104. val16 = cpu_to_le16(tally_counters->TxUndrn);
  1105. cpu_physical_memory_write(tc_addr + 62, (uint8_t *)&val16, 2);
  1106. }
  1107. /* Loads values of tally counters from VM state file */
  1108. static const VMStateDescription vmstate_tally_counters = {
  1109. .name = "tally_counters",
  1110. .version_id = 1,
  1111. .minimum_version_id = 1,
  1112. .minimum_version_id_old = 1,
  1113. .fields = (VMStateField []) {
  1114. VMSTATE_UINT64(TxOk, RTL8139TallyCounters),
  1115. VMSTATE_UINT64(RxOk, RTL8139TallyCounters),
  1116. VMSTATE_UINT64(TxERR, RTL8139TallyCounters),
  1117. VMSTATE_UINT32(RxERR, RTL8139TallyCounters),
  1118. VMSTATE_UINT16(MissPkt, RTL8139TallyCounters),
  1119. VMSTATE_UINT16(FAE, RTL8139TallyCounters),
  1120. VMSTATE_UINT32(Tx1Col, RTL8139TallyCounters),
  1121. VMSTATE_UINT32(TxMCol, RTL8139TallyCounters),
  1122. VMSTATE_UINT64(RxOkPhy, RTL8139TallyCounters),
  1123. VMSTATE_UINT64(RxOkBrd, RTL8139TallyCounters),
  1124. VMSTATE_UINT16(TxAbt, RTL8139TallyCounters),
  1125. VMSTATE_UINT16(TxUndrn, RTL8139TallyCounters),
  1126. VMSTATE_END_OF_LIST()
  1127. }
  1128. };
  1129. static void rtl8139_ChipCmd_write(RTL8139State *s, uint32_t val)
  1130. {
  1131. val &= 0xff;
  1132. DPRINTF("ChipCmd write val=0x%08x\n", val);
  1133. if (val & CmdReset)
  1134. {
  1135. DPRINTF("ChipCmd reset\n");
  1136. rtl8139_reset(&s->dev.qdev);
  1137. }
  1138. if (val & CmdRxEnb)
  1139. {
  1140. DPRINTF("ChipCmd enable receiver\n");
  1141. s->currCPlusRxDesc = 0;
  1142. }
  1143. if (val & CmdTxEnb)
  1144. {
  1145. DPRINTF("ChipCmd enable transmitter\n");
  1146. s->currCPlusTxDesc = 0;
  1147. }
  1148. /* mask unwritable bits */
  1149. val = SET_MASKED(val, 0xe3, s->bChipCmdState);
  1150. /* Deassert reset pin before next read */
  1151. val &= ~CmdReset;
  1152. s->bChipCmdState = val;
  1153. }
  1154. static int rtl8139_RxBufferEmpty(RTL8139State *s)
  1155. {
  1156. int unread = MOD2(s->RxBufferSize + s->RxBufAddr - s->RxBufPtr, s->RxBufferSize);
  1157. if (unread != 0)
  1158. {
  1159. DPRINTF("receiver buffer data available 0x%04x\n", unread);
  1160. return 0;
  1161. }
  1162. DPRINTF("receiver buffer is empty\n");
  1163. return 1;
  1164. }
  1165. static uint32_t rtl8139_ChipCmd_read(RTL8139State *s)
  1166. {
  1167. uint32_t ret = s->bChipCmdState;
  1168. if (rtl8139_RxBufferEmpty(s))
  1169. ret |= RxBufEmpty;
  1170. DPRINTF("ChipCmd read val=0x%04x\n", ret);
  1171. return ret;
  1172. }
  1173. static void rtl8139_CpCmd_write(RTL8139State *s, uint32_t val)
  1174. {
  1175. val &= 0xffff;
  1176. DPRINTF("C+ command register write(w) val=0x%04x\n", val);
  1177. s->cplus_enabled = 1;
  1178. /* mask unwritable bits */
  1179. val = SET_MASKED(val, 0xff84, s->CpCmd);
  1180. s->CpCmd = val;
  1181. }
  1182. static uint32_t rtl8139_CpCmd_read(RTL8139State *s)
  1183. {
  1184. uint32_t ret = s->CpCmd;
  1185. DPRINTF("C+ command register read(w) val=0x%04x\n", ret);
  1186. return ret;
  1187. }
  1188. static void rtl8139_IntrMitigate_write(RTL8139State *s, uint32_t val)
  1189. {
  1190. DPRINTF("C+ IntrMitigate register write(w) val=0x%04x\n", val);
  1191. }
  1192. static uint32_t rtl8139_IntrMitigate_read(RTL8139State *s)
  1193. {
  1194. uint32_t ret = 0;
  1195. DPRINTF("C+ IntrMitigate register read(w) val=0x%04x\n", ret);
  1196. return ret;
  1197. }
  1198. static int rtl8139_config_writable(RTL8139State *s)
  1199. {
  1200. if (s->Cfg9346 & Cfg9346_Unlock)
  1201. {
  1202. return 1;
  1203. }
  1204. DPRINTF("Configuration registers are write-protected\n");
  1205. return 0;
  1206. }
  1207. static void rtl8139_BasicModeCtrl_write(RTL8139State *s, uint32_t val)
  1208. {
  1209. val &= 0xffff;
  1210. DPRINTF("BasicModeCtrl register write(w) val=0x%04x\n", val);
  1211. /* mask unwritable bits */
  1212. uint32_t mask = 0x4cff;
  1213. if (1 || !rtl8139_config_writable(s))
  1214. {
  1215. /* Speed setting and autonegotiation enable bits are read-only */
  1216. mask |= 0x3000;
  1217. /* Duplex mode setting is read-only */
  1218. mask |= 0x0100;
  1219. }
  1220. val = SET_MASKED(val, mask, s->BasicModeCtrl);
  1221. s->BasicModeCtrl = val;
  1222. }
  1223. static uint32_t rtl8139_BasicModeCtrl_read(RTL8139State *s)
  1224. {
  1225. uint32_t ret = s->BasicModeCtrl;
  1226. DPRINTF("BasicModeCtrl register read(w) val=0x%04x\n", ret);
  1227. return ret;
  1228. }
  1229. static void rtl8139_BasicModeStatus_write(RTL8139State *s, uint32_t val)
  1230. {
  1231. val &= 0xffff;
  1232. DPRINTF("BasicModeStatus register write(w) val=0x%04x\n", val);
  1233. /* mask unwritable bits */
  1234. val = SET_MASKED(val, 0xff3f, s->BasicModeStatus);
  1235. s->BasicModeStatus = val;
  1236. }
  1237. static uint32_t rtl8139_BasicModeStatus_read(RTL8139State *s)
  1238. {
  1239. uint32_t ret = s->BasicModeStatus;
  1240. DPRINTF("BasicModeStatus register read(w) val=0x%04x\n", ret);
  1241. return ret;
  1242. }
  1243. static void rtl8139_Cfg9346_write(RTL8139State *s, uint32_t val)
  1244. {
  1245. val &= 0xff;
  1246. DPRINTF("Cfg9346 write val=0x%02x\n", val);
  1247. /* mask unwritable bits */
  1248. val = SET_MASKED(val, 0x31, s->Cfg9346);
  1249. uint32_t opmode = val & 0xc0;
  1250. uint32_t eeprom_val = val & 0xf;
  1251. if (opmode == 0x80) {
  1252. /* eeprom access */
  1253. int eecs = (eeprom_val & 0x08)?1:0;
  1254. int eesk = (eeprom_val & 0x04)?1:0;
  1255. int eedi = (eeprom_val & 0x02)?1:0;
  1256. prom9346_set_wire(s, eecs, eesk, eedi);
  1257. } else if (opmode == 0x40) {
  1258. /* Reset. */
  1259. val = 0;
  1260. rtl8139_reset(&s->dev.qdev);
  1261. }
  1262. s->Cfg9346 = val;
  1263. }
  1264. static uint32_t rtl8139_Cfg9346_read(RTL8139State *s)
  1265. {
  1266. uint32_t ret = s->Cfg9346;
  1267. uint32_t opmode = ret & 0xc0;
  1268. if (opmode == 0x80)
  1269. {
  1270. /* eeprom access */
  1271. int eedo = prom9346_get_wire(s);
  1272. if (eedo)
  1273. {
  1274. ret |= 0x01;
  1275. }
  1276. else
  1277. {
  1278. ret &= ~0x01;
  1279. }
  1280. }
  1281. DPRINTF("Cfg9346 read val=0x%02x\n", ret);
  1282. return ret;
  1283. }
  1284. static void rtl8139_Config0_write(RTL8139State *s, uint32_t val)
  1285. {
  1286. val &= 0xff;
  1287. DPRINTF("Config0 write val=0x%02x\n", val);
  1288. if (!rtl8139_config_writable(s)) {
  1289. return;
  1290. }
  1291. /* mask unwritable bits */
  1292. val = SET_MASKED(val, 0xf8, s->Config0);
  1293. s->Config0 = val;
  1294. }
  1295. static uint32_t rtl8139_Config0_read(RTL8139State *s)
  1296. {
  1297. uint32_t ret = s->Config0;
  1298. DPRINTF("Config0 read val=0x%02x\n", ret);
  1299. return ret;
  1300. }
  1301. static void rtl8139_Config1_write(RTL8139State *s, uint32_t val)
  1302. {
  1303. val &= 0xff;
  1304. DPRINTF("Config1 write val=0x%02x\n", val);
  1305. if (!rtl8139_config_writable(s)) {
  1306. return;
  1307. }
  1308. /* mask unwritable bits */
  1309. val = SET_MASKED(val, 0xC, s->Config1);
  1310. s->Config1 = val;
  1311. }
  1312. static uint32_t rtl8139_Config1_read(RTL8139State *s)
  1313. {
  1314. uint32_t ret = s->Config1;
  1315. DPRINTF("Config1 read val=0x%02x\n", ret);
  1316. return ret;
  1317. }
  1318. static void rtl8139_Config3_write(RTL8139State *s, uint32_t val)
  1319. {
  1320. val &= 0xff;
  1321. DPRINTF("Config3 write val=0x%02x\n", val);
  1322. if (!rtl8139_config_writable(s)) {
  1323. return;
  1324. }
  1325. /* mask unwritable bits */
  1326. val = SET_MASKED(val, 0x8F, s->Config3);
  1327. s->Config3 = val;
  1328. }
  1329. static uint32_t rtl8139_Config3_read(RTL8139State *s)
  1330. {
  1331. uint32_t ret = s->Config3;
  1332. DPRINTF("Config3 read val=0x%02x\n", ret);
  1333. return ret;
  1334. }
  1335. static void rtl8139_Config4_write(RTL8139State *s, uint32_t val)
  1336. {
  1337. val &= 0xff;
  1338. DPRINTF("Config4 write val=0x%02x\n", val);
  1339. if (!rtl8139_config_writable(s)) {
  1340. return;
  1341. }
  1342. /* mask unwritable bits */
  1343. val = SET_MASKED(val, 0x0a, s->Config4);
  1344. s->Config4 = val;
  1345. }
  1346. static uint32_t rtl8139_Config4_read(RTL8139State *s)
  1347. {
  1348. uint32_t ret = s->Config4;
  1349. DPRINTF("Config4 read val=0x%02x\n", ret);
  1350. return ret;
  1351. }
  1352. static void rtl8139_Config5_write(RTL8139State *s, uint32_t val)
  1353. {
  1354. val &= 0xff;
  1355. DPRINTF("Config5 write val=0x%02x\n", val);
  1356. /* mask unwritable bits */
  1357. val = SET_MASKED(val, 0x80, s->Config5);
  1358. s->Config5 = val;
  1359. }
  1360. static uint32_t rtl8139_Config5_read(RTL8139State *s)
  1361. {
  1362. uint32_t ret = s->Config5;
  1363. DPRINTF("Config5 read val=0x%02x\n", ret);
  1364. return ret;
  1365. }
  1366. static void rtl8139_TxConfig_write(RTL8139State *s, uint32_t val)
  1367. {
  1368. if (!rtl8139_transmitter_enabled(s))
  1369. {
  1370. DPRINTF("transmitter disabled; no TxConfig write val=0x%08x\n", val);
  1371. return;
  1372. }
  1373. DPRINTF("TxConfig write val=0x%08x\n", val);
  1374. val = SET_MASKED(val, TxVersionMask | 0x8070f80f, s->TxConfig);
  1375. s->TxConfig = val;
  1376. }
  1377. static void rtl8139_TxConfig_writeb(RTL8139State *s, uint32_t val)
  1378. {
  1379. DPRINTF("RTL8139C TxConfig via write(b) val=0x%02x\n", val);
  1380. uint32_t tc = s->TxConfig;
  1381. tc &= 0xFFFFFF00;
  1382. tc |= (val & 0x000000FF);
  1383. rtl8139_TxConfig_write(s, tc);
  1384. }
  1385. static uint32_t rtl8139_TxConfig_read(RTL8139State *s)
  1386. {
  1387. uint32_t ret = s->TxConfig;
  1388. DPRINTF("TxConfig read val=0x%04x\n", ret);
  1389. return ret;
  1390. }
  1391. static void rtl8139_RxConfig_write(RTL8139State *s, uint32_t val)
  1392. {
  1393. DPRINTF("RxConfig write val=0x%08x\n", val);
  1394. /* mask unwritable bits */
  1395. val = SET_MASKED(val, 0xf0fc0040, s->RxConfig);
  1396. s->RxConfig = val;
  1397. /* reset buffer size and read/write pointers */
  1398. rtl8139_reset_rxring(s, 8192 << ((s->RxConfig >> 11) & 0x3));
  1399. DPRINTF("RxConfig write reset buffer size to %d\n", s->RxBufferSize);
  1400. }
  1401. static uint32_t rtl8139_RxConfig_read(RTL8139State *s)
  1402. {
  1403. uint32_t ret = s->RxConfig;
  1404. DPRINTF("RxConfig read val=0x%08x\n", ret);
  1405. return ret;
  1406. }
  1407. static void rtl8139_transfer_frame(RTL8139State *s, uint8_t *buf, int size,
  1408. int do_interrupt, const uint8_t *dot1q_buf)
  1409. {
  1410. struct iovec *iov = NULL;
  1411. if (!size)
  1412. {
  1413. DPRINTF("+++ empty ethernet frame\n");
  1414. return;
  1415. }
  1416. if (dot1q_buf && size >= ETHER_ADDR_LEN * 2) {
  1417. iov = (struct iovec[3]) {
  1418. { .iov_base = buf, .iov_len = ETHER_ADDR_LEN * 2 },
  1419. { .iov_base = (void *) dot1q_buf, .iov_len = VLAN_HLEN },
  1420. { .iov_base = buf + ETHER_ADDR_LEN * 2,
  1421. .iov_len = size - ETHER_ADDR_LEN * 2 },
  1422. };
  1423. }
  1424. if (TxLoopBack == (s->TxConfig & TxLoopBack))
  1425. {
  1426. size_t buf2_size;
  1427. uint8_t *buf2;
  1428. if (iov) {
  1429. buf2_size = iov_size(iov, 3);
  1430. buf2 = qemu_malloc(buf2_size);
  1431. iov_to_buf(iov, 3, buf2, 0, buf2_size);
  1432. buf = buf2;
  1433. }
  1434. DPRINTF("+++ transmit loopback mode\n");
  1435. rtl8139_do_receive(&s->nic->nc, buf, size, do_interrupt);
  1436. if (iov) {
  1437. qemu_free(buf2);
  1438. }
  1439. }
  1440. else
  1441. {
  1442. if (iov) {
  1443. qemu_sendv_packet(&s->nic->nc, iov, 3);
  1444. } else {
  1445. qemu_send_packet(&s->nic->nc, buf, size);
  1446. }
  1447. }
  1448. }
  1449. static int rtl8139_transmit_one(RTL8139State *s, int descriptor)
  1450. {
  1451. if (!rtl8139_transmitter_enabled(s))
  1452. {
  1453. DPRINTF("+++ cannot transmit from descriptor %d: transmitter "
  1454. "disabled\n", descriptor);
  1455. return 0;
  1456. }
  1457. if (s->TxStatus[descriptor] & TxHostOwns)
  1458. {
  1459. DPRINTF("+++ cannot transmit from descriptor %d: owned by host "
  1460. "(%08x)\n", descriptor, s->TxStatus[descriptor]);
  1461. return 0;
  1462. }
  1463. DPRINTF("+++ transmitting from descriptor %d\n", descriptor);
  1464. int txsize = s->TxStatus[descriptor] & 0x1fff;
  1465. uint8_t txbuffer[0x2000];
  1466. DPRINTF("+++ transmit reading %d bytes from host memory at 0x%08x\n",
  1467. txsize, s->TxAddr[descriptor]);
  1468. cpu_physical_memory_read(s->TxAddr[descriptor], txbuffer, txsize);
  1469. /* Mark descriptor as transferred */
  1470. s->TxStatus[descriptor] |= TxHostOwns;
  1471. s->TxStatus[descriptor] |= TxStatOK;
  1472. rtl8139_transfer_frame(s, txbuffer, txsize, 0, NULL);
  1473. DPRINTF("+++ transmitted %d bytes from descriptor %d\n", txsize,
  1474. descriptor);
  1475. /* update interrupt */
  1476. s->IntrStatus |= TxOK;
  1477. rtl8139_update_irq(s);
  1478. return 1;
  1479. }
  1480. /* structures and macros for task offloading */
  1481. typedef struct ip_header
  1482. {
  1483. uint8_t ip_ver_len; /* version and header length */
  1484. uint8_t ip_tos; /* type of service */
  1485. uint16_t ip_len; /* total length */
  1486. uint16_t ip_id; /* identification */
  1487. uint16_t ip_off; /* fragment offset field */
  1488. uint8_t ip_ttl; /* time to live */
  1489. uint8_t ip_p; /* protocol */
  1490. uint16_t ip_sum; /* checksum */
  1491. uint32_t ip_src,ip_dst; /* source and dest address */
  1492. } ip_header;
  1493. #define IP_HEADER_VERSION_4 4
  1494. #define IP_HEADER_VERSION(ip) ((ip->ip_ver_len >> 4)&0xf)
  1495. #define IP_HEADER_LENGTH(ip) (((ip->ip_ver_len)&0xf) << 2)
  1496. typedef struct tcp_header
  1497. {
  1498. uint16_t th_sport; /* source port */
  1499. uint16_t th_dport; /* destination port */
  1500. uint32_t th_seq; /* sequence number */
  1501. uint32_t th_ack; /* acknowledgement number */
  1502. uint16_t th_offset_flags; /* data offset, reserved 6 bits, TCP protocol flags */
  1503. uint16_t th_win; /* window */
  1504. uint16_t th_sum; /* checksum */
  1505. uint16_t th_urp; /* urgent pointer */
  1506. } tcp_header;
  1507. typedef struct udp_header
  1508. {
  1509. uint16_t uh_sport; /* source port */
  1510. uint16_t uh_dport; /* destination port */
  1511. uint16_t uh_ulen; /* udp length */
  1512. uint16_t uh_sum; /* udp checksum */
  1513. } udp_header;
  1514. typedef struct ip_pseudo_header
  1515. {
  1516. uint32_t ip_src;
  1517. uint32_t ip_dst;
  1518. uint8_t zeros;
  1519. uint8_t ip_proto;
  1520. uint16_t ip_payload;
  1521. } ip_pseudo_header;
  1522. #define IP_PROTO_TCP 6
  1523. #define IP_PROTO_UDP 17
  1524. #define TCP_HEADER_DATA_OFFSET(tcp) (((be16_to_cpu(tcp->th_offset_flags) >> 12)&0xf) << 2)
  1525. #define TCP_FLAGS_ONLY(flags) ((flags)&0x3f)
  1526. #define TCP_HEADER_FLAGS(tcp) TCP_FLAGS_ONLY(be16_to_cpu(tcp->th_offset_flags))
  1527. #define TCP_HEADER_CLEAR_FLAGS(tcp, off) ((tcp)->th_offset_flags &= cpu_to_be16(~TCP_FLAGS_ONLY(off)))
  1528. #define TCP_FLAG_FIN 0x01
  1529. #define TCP_FLAG_PUSH 0x08
  1530. /* produces ones' complement sum of data */
  1531. static uint16_t ones_complement_sum(uint8_t *data, size_t len)
  1532. {
  1533. uint32_t result = 0;
  1534. for (; len > 1; data+=2, len-=2)
  1535. {
  1536. result += *(uint16_t*)data;
  1537. }
  1538. /* add the remainder byte */
  1539. if (len)
  1540. {
  1541. uint8_t odd[2] = {*data, 0};
  1542. result += *(uint16_t*)odd;
  1543. }
  1544. while (result>>16)
  1545. result = (result & 0xffff) + (result >> 16);
  1546. return result;
  1547. }
  1548. static uint16_t ip_checksum(void *data, size_t len)
  1549. {
  1550. return ~ones_complement_sum((uint8_t*)data, len);
  1551. }
  1552. static int rtl8139_cplus_transmit_one(RTL8139State *s)
  1553. {
  1554. if (!rtl8139_transmitter_enabled(s))
  1555. {
  1556. DPRINTF("+++ C+ mode: transmitter disabled\n");
  1557. return 0;
  1558. }
  1559. if (!rtl8139_cp_transmitter_enabled(s))
  1560. {
  1561. DPRINTF("+++ C+ mode: C+ transmitter disabled\n");
  1562. return 0 ;
  1563. }
  1564. int descriptor = s->currCPlusTxDesc;
  1565. target_phys_addr_t cplus_tx_ring_desc =
  1566. rtl8139_addr64(s->TxAddr[0], s->TxAddr[1]);
  1567. /* Normal priority ring */
  1568. cplus_tx_ring_desc += 16 * descriptor;
  1569. DPRINTF("+++ C+ mode reading TX descriptor %d from host memory at "
  1570. "%08x0x%08x = 0x"TARGET_FMT_plx"\n", descriptor, s->TxAddr[1],
  1571. s->TxAddr[0], cplus_tx_ring_desc);
  1572. uint32_t val, txdw0,txdw1,txbufLO,txbufHI;
  1573. cpu_physical_memory_read(cplus_tx_ring_desc, (uint8_t *)&val, 4);
  1574. txdw0 = le32_to_cpu(val);
  1575. cpu_physical_memory_read(cplus_tx_ring_desc+4, (uint8_t *)&val, 4);
  1576. txdw1 = le32_to_cpu(val);
  1577. cpu_physical_memory_read(cplus_tx_ring_desc+8, (uint8_t *)&val, 4);
  1578. txbufLO = le32_to_cpu(val);
  1579. cpu_physical_memory_read(cplus_tx_ring_desc+12, (uint8_t *)&val, 4);
  1580. txbufHI = le32_to_cpu(val);
  1581. DPRINTF("+++ C+ mode TX descriptor %d %08x %08x %08x %08x\n", descriptor,
  1582. txdw0, txdw1, txbufLO, txbufHI);
  1583. /* w0 ownership flag */
  1584. #define CP_TX_OWN (1<<31)
  1585. /* w0 end of ring flag */
  1586. #define CP_TX_EOR (1<<30)
  1587. /* first segment of received packet flag */
  1588. #define CP_TX_FS (1<<29)
  1589. /* last segment of received packet flag */
  1590. #define CP_TX_LS (1<<28)
  1591. /* large send packet flag */
  1592. #define CP_TX_LGSEN (1<<27)
  1593. /* large send MSS mask, bits 16...25 */
  1594. #define CP_TC_LGSEN_MSS_MASK ((1 << 12) - 1)
  1595. /* IP checksum offload flag */
  1596. #define CP_TX_IPCS (1<<18)
  1597. /* UDP checksum offload flag */
  1598. #define CP_TX_UDPCS (1<<17)
  1599. /* TCP checksum offload flag */
  1600. #define CP_TX_TCPCS (1<<16)
  1601. /* w0 bits 0...15 : buffer size */
  1602. #define CP_TX_BUFFER_SIZE (1<<16)
  1603. #define CP_TX_BUFFER_SIZE_MASK (CP_TX_BUFFER_SIZE - 1)
  1604. /* w1 add tag flag */
  1605. #define CP_TX_TAGC (1<<17)
  1606. /* w1 bits 0...15 : VLAN tag (big endian) */
  1607. #define CP_TX_VLAN_TAG_MASK ((1<<16) - 1)
  1608. /* w2 low 32bit of Rx buffer ptr */
  1609. /* w3 high 32bit of Rx buffer ptr */
  1610. /* set after transmission */
  1611. /* FIFO underrun flag */
  1612. #define CP_TX_STATUS_UNF (1<<25)
  1613. /* transmit error summary flag, valid if set any of three below */
  1614. #define CP_TX_STATUS_TES (1<<23)
  1615. /* out-of-window collision flag */
  1616. #define CP_TX_STATUS_OWC (1<<22)
  1617. /* link failure flag */
  1618. #define CP_TX_STATUS_LNKF (1<<21)
  1619. /* excessive collisions flag */
  1620. #define CP_TX_STATUS_EXC (1<<20)
  1621. if (!(txdw0 & CP_TX_OWN))
  1622. {
  1623. DPRINTF("C+ Tx mode : descriptor %d is owned by host\n", descriptor);
  1624. return 0 ;
  1625. }
  1626. DPRINTF("+++ C+ Tx mode : transmitting from descriptor %d\n", descriptor);
  1627. if (txdw0 & CP_TX_FS)
  1628. {
  1629. DPRINTF("+++ C+ Tx mode : descriptor %d is first segment "
  1630. "descriptor\n", descriptor);
  1631. /* reset internal buffer offset */
  1632. s->cplus_txbuffer_offset = 0;
  1633. }
  1634. int txsize = txdw0 & CP_TX_BUFFER_SIZE_MASK;
  1635. target_phys_addr_t tx_addr = rtl8139_addr64(txbufLO, txbufHI);
  1636. /* make sure we have enough space to assemble the packet */
  1637. if (!s->cplus_txbuffer)
  1638. {
  1639. s->cplus_txbuffer_len = CP_TX_BUFFER_SIZE;
  1640. s->cplus_txbuffer = qemu_malloc(s->cplus_txbuffer_len);
  1641. s->cplus_txbuffer_offset = 0;
  1642. DPRINTF("+++ C+ mode transmission buffer allocated space %d\n",
  1643. s->cplus_txbuffer_len);
  1644. }
  1645. while (s->cplus_txbuffer && s->cplus_txbuffer_offset + txsize >= s->cplus_txbuffer_len)
  1646. {
  1647. s->cplus_txbuffer_len += CP_TX_BUFFER_SIZE;
  1648. s->cplus_txbuffer = qemu_realloc(s->cplus_txbuffer, s->cplus_txbuffer_len);
  1649. DPRINTF("+++ C+ mode transmission buffer space changed to %d\n",
  1650. s->cplus_txbuffer_len);
  1651. }
  1652. if (!s->cplus_txbuffer)
  1653. {
  1654. /* out of memory */
  1655. DPRINTF("+++ C+ mode transmiter failed to reallocate %d bytes\n",
  1656. s->cplus_txbuffer_len);
  1657. /* update tally counter */
  1658. ++s->tally_counters.TxERR;
  1659. ++s->tally_counters.TxAbt;
  1660. return 0;
  1661. }
  1662. /* append more data to the packet */
  1663. DPRINTF("+++ C+ mode transmit reading %d bytes from host memory at "
  1664. TARGET_FMT_plx" to offset %d\n", txsize, tx_addr,
  1665. s->cplus_txbuffer_offset);
  1666. cpu_physical_memory_read(tx_addr, s->cplus_txbuffer + s->cplus_txbuffer_offset, txsize);
  1667. s->cplus_txbuffer_offset += txsize;
  1668. /* seek to next Rx descriptor */
  1669. if (txdw0 & CP_TX_EOR)
  1670. {
  1671. s->currCPlusTxDesc = 0;
  1672. }
  1673. else
  1674. {
  1675. ++s->currCPlusTxDesc;
  1676. if (s->currCPlusTxDesc >= 64)
  1677. s->currCPlusTxDesc = 0;
  1678. }
  1679. /* transfer ownership to target */
  1680. txdw0 &= ~CP_RX_OWN;
  1681. /* reset error indicator bits */
  1682. txdw0 &= ~CP_TX_STATUS_UNF;
  1683. txdw0 &= ~CP_TX_STATUS_TES;
  1684. txdw0 &= ~CP_TX_STATUS_OWC;
  1685. txdw0 &= ~CP_TX_STATUS_LNKF;
  1686. txdw0 &= ~CP_TX_STATUS_EXC;
  1687. /* update ring data */
  1688. val = cpu_to_le32(txdw0);
  1689. cpu_physical_memory_write(cplus_tx_ring_desc, (uint8_t *)&val, 4);
  1690. /* Now decide if descriptor being processed is holding the last segment of packet */
  1691. if (txdw0 & CP_TX_LS)
  1692. {
  1693. uint8_t dot1q_buffer_space[VLAN_HLEN];
  1694. uint16_t *dot1q_buffer;
  1695. DPRINTF("+++ C+ Tx mode : descriptor %d is last segment descriptor\n",
  1696. descriptor);
  1697. /* can transfer fully assembled packet */
  1698. uint8_t *saved_buffer = s->cplus_txbuffer;
  1699. int saved_size = s->cplus_txbuffer_offset;
  1700. int saved_buffer_len = s->cplus_txbuffer_len;
  1701. /* create vlan tag */
  1702. if (txdw1 & CP_TX_TAGC) {
  1703. /* the vlan tag is in BE byte order in the descriptor
  1704. * BE + le_to_cpu() + ~swap()~ = cpu */
  1705. DPRINTF("+++ C+ Tx mode : inserting vlan tag with ""tci: %u\n",
  1706. bswap16(txdw1 & CP_TX_VLAN_TAG_MASK));
  1707. dot1q_buffer = (uint16_t *) dot1q_buffer_space;
  1708. dot1q_buffer[0] = cpu_to_be16(ETH_P_8021Q);
  1709. /* BE + le_to_cpu() + ~cpu_to_le()~ = BE */
  1710. dot1q_buffer[1] = cpu_to_le16(txdw1 & CP_TX_VLAN_TAG_MASK);
  1711. } else {
  1712. dot1q_buffer = NULL;
  1713. }
  1714. /* reset the card space to protect from recursive call */
  1715. s->cplus_txbuffer = NULL;
  1716. s->cplus_txbuffer_offset = 0;
  1717. s->cplus_txbuffer_len = 0;
  1718. if (txdw0 & (CP_TX_IPCS | CP_TX_UDPCS | CP_TX_TCPCS | CP_TX_LGSEN))
  1719. {
  1720. DPRINTF("+++ C+ mode offloaded task checksum\n");
  1721. /* ip packet header */
  1722. ip_header *ip = NULL;
  1723. int hlen = 0;
  1724. uint8_t ip_protocol = 0;
  1725. uint16_t ip_data_len = 0;
  1726. uint8_t *eth_payload_data = NULL;
  1727. size_t eth_payload_len = 0;
  1728. int proto = be16_to_cpu(*(uint16_t *)(saved_buffer + 12));
  1729. if (proto == ETH_P_IP)
  1730. {
  1731. DPRINTF("+++ C+ mode has IP packet\n");
  1732. /* not aligned */
  1733. eth_payload_data = saved_buffer + ETH_HLEN;
  1734. eth_payload_len = saved_size - ETH_HLEN;
  1735. ip = (ip_header*)eth_payload_data;
  1736. if (IP_HEADER_VERSION(ip) != IP_HEADER_VERSION_4) {
  1737. DPRINTF("+++ C+ mode packet has bad IP version %d "
  1738. "expected %d\n", IP_HEADER_VERSION(ip),
  1739. IP_HEADER_VERSION_4);
  1740. ip = NULL;
  1741. } else {
  1742. hlen = IP_HEADER_LENGTH(ip);
  1743. ip_protocol = ip->ip_p;
  1744. ip_data_len = be16_to_cpu(ip->ip_len) - hlen;
  1745. }
  1746. }
  1747. if (ip)
  1748. {
  1749. if (txdw0 & CP_TX_IPCS)
  1750. {
  1751. DPRINTF("+++ C+ mode need IP checksum\n");
  1752. if (hlen<sizeof(ip_header) || hlen>eth_payload_len) {/* min header length */
  1753. /* bad packet header len */
  1754. /* or packet too short */
  1755. }
  1756. else
  1757. {
  1758. ip->ip_sum = 0;
  1759. ip->ip_sum = ip_checksum(ip, hlen);
  1760. DPRINTF("+++ C+ mode IP header len=%d checksum=%04x\n",
  1761. hlen, ip->ip_sum);
  1762. }
  1763. }
  1764. if ((txdw0 & CP_TX_LGSEN) && ip_protocol == IP_PROTO_TCP)
  1765. {
  1766. int large_send_mss = (txdw0 >> 16) & CP_TC_LGSEN_MSS_MASK;
  1767. DPRINTF("+++ C+ mode offloaded task TSO MTU=%d IP data %d "
  1768. "frame data %d specified MSS=%d\n", ETH_MTU,
  1769. ip_data_len, saved_size - ETH_HLEN, large_send_mss);
  1770. int tcp_send_offset = 0;
  1771. int send_count = 0;
  1772. /* maximum IP header length is 60 bytes */
  1773. uint8_t saved_ip_header[60];
  1774. /* save IP header template; data area is used in tcp checksum calculation */
  1775. memcpy(saved_ip_header, eth_payload_data, hlen);
  1776. /* a placeholder for checksum calculation routine in tcp case */
  1777. uint8_t *data_to_checksum = eth_payload_data + hlen - 12;
  1778. // size_t data_to_checksum_len = eth_payload_len - hlen + 12;
  1779. /* pointer to TCP header */
  1780. tcp_header *p_tcp_hdr = (tcp_header*)(eth_payload_data + hlen);
  1781. int tcp_hlen = TCP_HEADER_DATA_OFFSET(p_tcp_hdr);
  1782. /* ETH_MTU = ip header len + tcp header len + payload */
  1783. int tcp_data_len = ip_data_len - tcp_hlen;
  1784. int tcp_chunk_size = ETH_MTU - hlen - tcp_hlen;
  1785. DPRINTF("+++ C+ mode TSO IP data len %d TCP hlen %d TCP "
  1786. "data len %d TCP chunk size %d\n", ip_data_len,
  1787. tcp_hlen, tcp_data_len, tcp_chunk_size);
  1788. /* note the cycle below overwrites IP header data,
  1789. but restores it from saved_ip_header before sending packet */
  1790. int is_last_frame = 0;
  1791. for (tcp_send_offset = 0; tcp_send_offset < tcp_data_len; tcp_send_offset += tcp_chunk_size)
  1792. {
  1793. uint16_t chunk_size = tcp_chunk_size;
  1794. /* check if this is the last frame */
  1795. if (tcp_send_offset + tcp_chunk_size >= tcp_data_len)
  1796. {
  1797. is_last_frame = 1;
  1798. chunk_size = tcp_data_len - tcp_send_offset;
  1799. }
  1800. DPRINTF("+++ C+ mode TSO TCP seqno %08x\n",
  1801. be32_to_cpu(p_tcp_hdr->th_seq));
  1802. /* add 4 TCP pseudoheader fields */
  1803. /* copy IP source and destination fields */
  1804. memcpy(data_to_checksum, saved_ip_header + 12, 8);
  1805. DPRINTF("+++ C+ mode TSO calculating TCP checksum for "
  1806. "packet with %d bytes data\n", tcp_hlen +
  1807. chunk_size);
  1808. if (tcp_send_offset)
  1809. {
  1810. memcpy((uint8_t*)p_tcp_hdr + tcp_hlen, (uint8_t*)p_tcp_hdr + tcp_hlen + tcp_send_offset, chunk_size);
  1811. }
  1812. /* keep PUSH and FIN flags only for the last frame */
  1813. if (!is_last_frame)
  1814. {
  1815. TCP_HEADER_CLEAR_FLAGS(p_tcp_hdr, TCP_FLAG_PUSH|TCP_FLAG_FIN);
  1816. }
  1817. /* recalculate TCP checksum */
  1818. ip_pseudo_header *p_tcpip_hdr = (ip_pseudo_header *)data_to_checksum;
  1819. p_tcpip_hdr->zeros = 0;
  1820. p_tcpip_hdr->ip_proto = IP_PROTO_TCP;
  1821. p_tcpip_hdr->ip_payload = cpu_to_be16(tcp_hlen + chunk_size);
  1822. p_tcp_hdr->th_sum = 0;
  1823. int tcp_checksum = ip_checksum(data_to_checksum, tcp_hlen + chunk_size + 12);
  1824. DPRINTF("+++ C+ mode TSO TCP checksum %04x\n",
  1825. tcp_checksum);
  1826. p_tcp_hdr->th_sum = tcp_checksum;
  1827. /* restore IP header */
  1828. memcpy(eth_payload_data, saved_ip_header, hlen);
  1829. /* set IP data length and recalculate IP checksum */
  1830. ip->ip_len = cpu_to_be16(hlen + tcp_hlen + chunk_size);
  1831. /* increment IP id for subsequent frames */
  1832. ip->ip_id = cpu_to_be16(tcp_send_offset/tcp_chunk_size + be16_to_cpu(ip->ip_id));
  1833. ip->ip_sum = 0;
  1834. ip->ip_sum = ip_checksum(eth_payload_data, hlen);
  1835. DPRINTF("+++ C+ mode TSO IP header len=%d "
  1836. "checksum=%04x\n", hlen, ip->ip_sum);
  1837. int tso_send_size = ETH_HLEN + hlen + tcp_hlen + chunk_size;
  1838. DPRINTF("+++ C+ mode TSO transferring packet size "
  1839. "%d\n", tso_send_size);
  1840. rtl8139_transfer_frame(s, saved_buffer, tso_send_size,
  1841. 0, (uint8_t *) dot1q_buffer);
  1842. /* add transferred count to TCP sequence number */
  1843. p_tcp_hdr->th_seq = cpu_to_be32(chunk_size + be32_to_cpu(p_tcp_hdr->th_seq));
  1844. ++send_count;
  1845. }
  1846. /* Stop sending this frame */
  1847. saved_size = 0;
  1848. }
  1849. else if (txdw0 & (CP_TX_TCPCS|CP_TX_UDPCS))
  1850. {
  1851. DPRINTF("+++ C+ mode need TCP or UDP checksum\n");
  1852. /* maximum IP header length is 60 bytes */
  1853. uint8_t saved_ip_header[60];
  1854. memcpy(saved_ip_header, eth_payload_data, hlen);
  1855. uint8_t *data_to_checksum = eth_payload_data + hlen - 12;
  1856. // size_t data_to_checksum_len = eth_payload_len - hlen + 12;
  1857. /* add 4 TCP pseudoheader fields */
  1858. /* copy IP source and destination fields */
  1859. memcpy(data_to_checksum, saved_ip_header + 12, 8);
  1860. if ((txdw0 & CP_TX_TCPCS) && ip_protocol == IP_PROTO_TCP)
  1861. {
  1862. DPRINTF("+++ C+ mode calculating TCP checksum for "
  1863. "packet with %d bytes data\n", ip_data_len);
  1864. ip_pseudo_header *p_tcpip_hdr = (ip_pseudo_header *)data_to_checksum;
  1865. p_tcpip_hdr->zeros = 0;
  1866. p_tcpip_hdr->ip_proto = IP_PROTO_TCP;
  1867. p_tcpip_hdr->ip_payload = cpu_to_be16(ip_data_len);
  1868. tcp_header* p_tcp_hdr = (tcp_header *) (data_to_checksum+12);
  1869. p_tcp_hdr->th_sum = 0;
  1870. int tcp_checksum = ip_checksum(data_to_checksum, ip_data_len + 12);
  1871. DPRINTF("+++ C+ mode TCP checksum %04x\n",
  1872. tcp_checksum);
  1873. p_tcp_hdr->th_sum = tcp_checksum;
  1874. }
  1875. else if ((txdw0 & CP_TX_UDPCS) && ip_protocol == IP_PROTO_UDP)
  1876. {
  1877. DPRINTF("+++ C+ mode calculating UDP checksum for "
  1878. "packet with %d bytes data\n", ip_data_len);
  1879. ip_pseudo_header *p_udpip_hdr = (ip_pseudo_header *)data_to_checksum;
  1880. p_udpip_hdr->zeros = 0;
  1881. p_udpip_hdr->ip_proto = IP_PROTO_UDP;
  1882. p_udpip_hdr->ip_payload = cpu_to_be16(ip_data_len);
  1883. udp_header *p_udp_hdr = (udp_header *) (data_to_checksum+12);
  1884. p_udp_hdr->uh_sum = 0;
  1885. int udp_checksum = ip_checksum(data_to_checksum, ip_data_len + 12);
  1886. DPRINTF("+++ C+ mode UDP checksum %04x\n",
  1887. udp_checksum);
  1888. p_udp_hdr->uh_sum = udp_checksum;
  1889. }
  1890. /* restore IP header */
  1891. memcpy(eth_payload_data, saved_ip_header, hlen);
  1892. }
  1893. }
  1894. }
  1895. /* update tally counter */
  1896. ++s->tally_counters.TxOk;
  1897. DPRINTF("+++ C+ mode transmitting %d bytes packet\n", saved_size);
  1898. rtl8139_transfer_frame(s, saved_buffer, saved_size, 1,
  1899. (uint8_t *) dot1q_buffer);
  1900. /* restore card space if there was no recursion and reset offset */
  1901. if (!s->cplus_txbuffer)
  1902. {
  1903. s->cplus_txbuffer = saved_buffer;
  1904. s->cplus_txbuffer_len = saved_buffer_len;
  1905. s->cplus_txbuffer_offset = 0;
  1906. }
  1907. else
  1908. {
  1909. qemu_free(saved_buffer);
  1910. }
  1911. }
  1912. else
  1913. {
  1914. DPRINTF("+++ C+ mode transmission continue to next descriptor\n");
  1915. }
  1916. return 1;
  1917. }
  1918. static void rtl8139_cplus_transmit(RTL8139State *s)
  1919. {
  1920. int txcount = 0;
  1921. while (rtl8139_cplus_transmit_one(s))
  1922. {
  1923. ++txcount;
  1924. }
  1925. /* Mark transfer completed */
  1926. if (!txcount)
  1927. {
  1928. DPRINTF("C+ mode : transmitter queue stalled, current TxDesc = %d\n",
  1929. s->currCPlusTxDesc);
  1930. }
  1931. else
  1932. {
  1933. /* update interrupt status */
  1934. s->IntrStatus |= TxOK;
  1935. rtl8139_update_irq(s);
  1936. }
  1937. }
  1938. static void rtl8139_transmit(RTL8139State *s)
  1939. {
  1940. int descriptor = s->currTxDesc, txcount = 0;
  1941. /*while*/
  1942. if (rtl8139_transmit_one(s, descriptor))
  1943. {
  1944. ++s->currTxDesc;
  1945. s->currTxDesc %= 4;
  1946. ++txcount;
  1947. }
  1948. /* Mark transfer completed */
  1949. if (!txcount)
  1950. {
  1951. DPRINTF("transmitter queue stalled, current TxDesc = %d\n",
  1952. s->currTxDesc);
  1953. }
  1954. }
  1955. static void rtl8139_TxStatus_write(RTL8139State *s, uint32_t txRegOffset, uint32_t val)
  1956. {
  1957. int descriptor = txRegOffset/4;
  1958. /* handle C+ transmit mode register configuration */
  1959. if (s->cplus_enabled)
  1960. {
  1961. DPRINTF("RTL8139C+ DTCCR write offset=0x%x val=0x%08x "
  1962. "descriptor=%d\n", txRegOffset, val, descriptor);
  1963. /* handle Dump Tally Counters command */
  1964. s->TxStatus[descriptor] = val;
  1965. if (descriptor == 0 && (val & 0x8))
  1966. {
  1967. target_phys_addr_t tc_addr = rtl8139_addr64(s->TxStatus[0] & ~0x3f, s->TxStatus[1]);
  1968. /* dump tally counters to specified memory location */
  1969. RTL8139TallyCounters_physical_memory_write( tc_addr, &s->tally_counters);
  1970. /* mark dump completed */
  1971. s->TxStatus[0] &= ~0x8;
  1972. }
  1973. return;
  1974. }
  1975. DPRINTF("TxStatus write offset=0x%x val=0x%08x descriptor=%d\n",
  1976. txRegOffset, val, descriptor);
  1977. /* mask only reserved bits */
  1978. val &= ~0xff00c000; /* these bits are reset on write */
  1979. val = SET_MASKED(val, 0x00c00000, s->TxStatus[descriptor]);
  1980. s->TxStatus[descriptor] = val;
  1981. /* attempt to start transmission */
  1982. rtl8139_transmit(s);
  1983. }
  1984. static uint32_t rtl8139_TxStatus_read(RTL8139State *s, uint32_t txRegOffset)
  1985. {
  1986. uint32_t ret = s->TxStatus[txRegOffset/4];
  1987. DPRINTF("TxStatus read offset=0x%x val=0x%08x\n", txRegOffset, ret);
  1988. return ret;
  1989. }
  1990. static uint16_t rtl8139_TSAD_read(RTL8139State *s)
  1991. {
  1992. uint16_t ret = 0;
  1993. /* Simulate TSAD, it is read only anyway */
  1994. ret = ((s->TxStatus[3] & TxStatOK )?TSAD_TOK3:0)
  1995. |((s->TxStatus[2] & TxStatOK )?TSAD_TOK2:0)
  1996. |((s->TxStatus[1] & TxStatOK )?TSAD_TOK1:0)
  1997. |((s->TxStatus[0] & TxStatOK )?TSAD_TOK0:0)
  1998. |((s->TxStatus[3] & TxUnderrun)?TSAD_TUN3:0)
  1999. |((s->TxStatus[2] & TxUnderrun)?TSAD_TUN2:0)
  2000. |((s->TxStatus[1] & TxUnderrun)?TSAD_TUN1:0)
  2001. |((s->TxStatus[0] & TxUnderrun)?TSAD_TUN0:0)
  2002. |((s->TxStatus[3] & TxAborted )?TSAD_TABT3:0)
  2003. |((s->TxStatus[2] & TxAborted )?TSAD_TABT2:0)
  2004. |((s->TxStatus[1] & TxAborted )?TSAD_TABT1:0)
  2005. |((s->TxStatus[0] & TxAborted )?TSAD_TABT0:0)
  2006. |((s->TxStatus[3] & TxHostOwns )?TSAD_OWN3:0)
  2007. |((s->TxStatus[2] & TxHostOwns )?TSAD_OWN2:0)
  2008. |((s->TxStatus[1] & TxHostOwns )?TSAD_OWN1:0)
  2009. |((s->TxStatus[0] & TxHostOwns )?TSAD_OWN0:0) ;
  2010. DPRINTF("TSAD read val=0x%04x\n", ret);
  2011. return ret;
  2012. }
  2013. static uint16_t rtl8139_CSCR_read(RTL8139State *s)
  2014. {
  2015. uint16_t ret = s->CSCR;
  2016. DPRINTF("CSCR read val=0x%04x\n", ret);
  2017. return ret;
  2018. }
  2019. static void rtl8139_TxAddr_write(RTL8139State *s, uint32_t txAddrOffset, uint32_t val)
  2020. {
  2021. DPRINTF("TxAddr write offset=0x%x val=0x%08x\n", txAddrOffset, val);
  2022. s->TxAddr[txAddrOffset/4] = val;
  2023. }
  2024. static uint32_t rtl8139_TxAddr_read(RTL8139State *s, uint32_t txAddrOffset)
  2025. {
  2026. uint32_t ret = s->TxAddr[txAddrOffset/4];
  2027. DPRINTF("TxAddr read offset=0x%x val=0x%08x\n", txAddrOffset, ret);
  2028. return ret;
  2029. }
  2030. static void rtl8139_RxBufPtr_write(RTL8139State *s, uint32_t val)
  2031. {
  2032. DPRINTF("RxBufPtr write val=0x%04x\n", val);
  2033. /* this value is off by 16 */
  2034. s->RxBufPtr = MOD2(val + 0x10, s->RxBufferSize);
  2035. DPRINTF(" CAPR write: rx buffer length %d head 0x%04x read 0x%04x\n",
  2036. s->RxBufferSize, s->RxBufAddr, s->RxBufPtr);
  2037. }
  2038. static uint32_t rtl8139_RxBufPtr_read(RTL8139State *s)
  2039. {
  2040. /* this value is off by 16 */
  2041. uint32_t ret = s->RxBufPtr - 0x10;
  2042. DPRINTF("RxBufPtr read val=0x%04x\n", ret);
  2043. return ret;
  2044. }
  2045. static uint32_t rtl8139_RxBufAddr_read(RTL8139State *s)
  2046. {
  2047. /* this value is NOT off by 16 */
  2048. uint32_t ret = s->RxBufAddr;
  2049. DPRINTF("RxBufAddr read val=0x%04x\n", ret);
  2050. return ret;
  2051. }
  2052. static void rtl8139_RxBuf_write(RTL8139State *s, uint32_t val)
  2053. {
  2054. DPRINTF("RxBuf write val=0x%08x\n", val);
  2055. s->RxBuf = val;
  2056. /* may need to reset rxring here */
  2057. }
  2058. static uint32_t rtl8139_RxBuf_read(RTL8139State *s)
  2059. {
  2060. uint32_t ret = s->RxBuf;
  2061. DPRINTF("RxBuf read val=0x%08x\n", ret);
  2062. return ret;
  2063. }
  2064. static void rtl8139_IntrMask_write(RTL8139State *s, uint32_t val)
  2065. {
  2066. DPRINTF("IntrMask write(w) val=0x%04x\n", val);
  2067. /* mask unwritable bits */
  2068. val = SET_MASKED(val, 0x1e00, s->IntrMask);
  2069. s->IntrMask = val;
  2070. rtl8139_set_next_tctr_time(s, qemu_get_clock_ns(vm_clock));
  2071. rtl8139_update_irq(s);
  2072. }
  2073. static uint32_t rtl8139_IntrMask_read(RTL8139State *s)
  2074. {
  2075. uint32_t ret = s->IntrMask;
  2076. DPRINTF("IntrMask read(w) val=0x%04x\n", ret);
  2077. return ret;
  2078. }
  2079. static void rtl8139_IntrStatus_write(RTL8139State *s, uint32_t val)
  2080. {
  2081. DPRINTF("IntrStatus write(w) val=0x%04x\n", val);
  2082. #if 0
  2083. /* writing to ISR has no effect */
  2084. return;
  2085. #else
  2086. uint16_t newStatus = s->IntrStatus & ~val;
  2087. /* mask unwritable bits */
  2088. newStatus = SET_MASKED(newStatus, 0x1e00, s->IntrStatus);
  2089. /* writing 1 to interrupt status register bit clears it */
  2090. s->IntrStatus = 0;
  2091. rtl8139_update_irq(s);
  2092. s->IntrStatus = newStatus;
  2093. /*
  2094. * Computing if we miss an interrupt here is not that correct but
  2095. * considered that we should have had already an interrupt
  2096. * and probably emulated is slower is better to assume this resetting was
  2097. * done before testing on previous rtl8139_update_irq lead to IRQ loosing
  2098. */
  2099. rtl8139_set_next_tctr_time(s, qemu_get_clock_ns(vm_clock));
  2100. rtl8139_update_irq(s);
  2101. #endif
  2102. }
  2103. static uint32_t rtl8139_IntrStatus_read(RTL8139State *s)
  2104. {
  2105. rtl8139_set_next_tctr_time(s, qemu_get_clock_ns(vm_clock));
  2106. uint32_t ret = s->IntrStatus;
  2107. DPRINTF("IntrStatus read(w) val=0x%04x\n", ret);
  2108. #if 0
  2109. /* reading ISR clears all interrupts */
  2110. s->IntrStatus = 0;
  2111. rtl8139_update_irq(s);
  2112. #endif
  2113. return ret;
  2114. }
  2115. static void rtl8139_MultiIntr_write(RTL8139State *s, uint32_t val)
  2116. {
  2117. DPRINTF("MultiIntr write(w) val=0x%04x\n", val);
  2118. /* mask unwritable bits */
  2119. val = SET_MASKED(val, 0xf000, s->MultiIntr);
  2120. s->MultiIntr = val;
  2121. }
  2122. static uint32_t rtl8139_MultiIntr_read(RTL8139State *s)
  2123. {
  2124. uint32_t ret = s->MultiIntr;
  2125. DPRINTF("MultiIntr read(w) val=0x%04x\n", ret);
  2126. return ret;
  2127. }
  2128. static void rtl8139_io_writeb(void *opaque, uint8_t addr, uint32_t val)
  2129. {
  2130. RTL8139State *s = opaque;
  2131. addr &= 0xff;
  2132. switch (addr)
  2133. {
  2134. case MAC0 ... MAC0+5:
  2135. s->phys[addr - MAC0] = val;
  2136. break;
  2137. case MAC0+6 ... MAC0+7:
  2138. /* reserved */
  2139. break;
  2140. case MAR0 ... MAR0+7:
  2141. s->mult[addr - MAR0] = val;
  2142. break;
  2143. case ChipCmd:
  2144. rtl8139_ChipCmd_write(s, val);
  2145. break;
  2146. case Cfg9346:
  2147. rtl8139_Cfg9346_write(s, val);
  2148. break;
  2149. case TxConfig: /* windows driver sometimes writes using byte-lenth call */
  2150. rtl8139_TxConfig_writeb(s, val);
  2151. break;
  2152. case Config0:
  2153. rtl8139_Config0_write(s, val);
  2154. break;
  2155. case Config1:
  2156. rtl8139_Config1_write(s, val);
  2157. break;
  2158. case Config3:
  2159. rtl8139_Config3_write(s, val);
  2160. break;
  2161. case Config4:
  2162. rtl8139_Config4_write(s, val);
  2163. break;
  2164. case Config5:
  2165. rtl8139_Config5_write(s, val);
  2166. break;
  2167. case MediaStatus:
  2168. /* ignore */
  2169. DPRINTF("not implemented write(b) to MediaStatus val=0x%02x\n",
  2170. val);
  2171. break;
  2172. case HltClk:
  2173. DPRINTF("HltClk write val=0x%08x\n", val);
  2174. if (val == 'R')
  2175. {
  2176. s->clock_enabled = 1;
  2177. }
  2178. else if (val == 'H')
  2179. {
  2180. s->clock_enabled = 0;
  2181. }
  2182. break;
  2183. case TxThresh:
  2184. DPRINTF("C+ TxThresh write(b) val=0x%02x\n", val);
  2185. s->TxThresh = val;
  2186. break;
  2187. case TxPoll:
  2188. DPRINTF("C+ TxPoll write(b) val=0x%02x\n", val);
  2189. if (val & (1 << 7))
  2190. {
  2191. DPRINTF("C+ TxPoll high priority transmission (not "
  2192. "implemented)\n");
  2193. //rtl8139_cplus_transmit(s);
  2194. }
  2195. if (val & (1 << 6))
  2196. {
  2197. DPRINTF("C+ TxPoll normal priority transmission\n");
  2198. rtl8139_cplus_transmit(s);
  2199. }
  2200. break;
  2201. default:
  2202. DPRINTF("not implemented write(b) addr=0x%x val=0x%02x\n", addr,
  2203. val);
  2204. break;
  2205. }
  2206. }
  2207. static void rtl8139_io_writew(void *opaque, uint8_t addr, uint32_t val)
  2208. {
  2209. RTL8139State *s = opaque;
  2210. addr &= 0xfe;
  2211. switch (addr)
  2212. {
  2213. case IntrMask:
  2214. rtl8139_IntrMask_write(s, val);
  2215. break;
  2216. case IntrStatus:
  2217. rtl8139_IntrStatus_write(s, val);
  2218. break;
  2219. case MultiIntr:
  2220. rtl8139_MultiIntr_write(s, val);
  2221. break;
  2222. case RxBufPtr:
  2223. rtl8139_RxBufPtr_write(s, val);
  2224. break;
  2225. case BasicModeCtrl:
  2226. rtl8139_BasicModeCtrl_write(s, val);
  2227. break;
  2228. case BasicModeStatus:
  2229. rtl8139_BasicModeStatus_write(s, val);
  2230. break;
  2231. case NWayAdvert:
  2232. DPRINTF("NWayAdvert write(w) val=0x%04x\n", val);
  2233. s->NWayAdvert = val;
  2234. break;
  2235. case NWayLPAR:
  2236. DPRINTF("forbidden NWayLPAR write(w) val=0x%04x\n", val);
  2237. break;
  2238. case NWayExpansion:
  2239. DPRINTF("NWayExpansion write(w) val=0x%04x\n", val);
  2240. s->NWayExpansion = val;
  2241. break;
  2242. case CpCmd:
  2243. rtl8139_CpCmd_write(s, val);
  2244. break;
  2245. case IntrMitigate:
  2246. rtl8139_IntrMitigate_write(s, val);
  2247. break;
  2248. default:
  2249. DPRINTF("ioport write(w) addr=0x%x val=0x%04x via write(b)\n",
  2250. addr, val);
  2251. rtl8139_io_writeb(opaque, addr, val & 0xff);
  2252. rtl8139_io_writeb(opaque, addr + 1, (val >> 8) & 0xff);
  2253. break;
  2254. }
  2255. }
  2256. static void rtl8139_set_next_tctr_time(RTL8139State *s, int64_t current_time)
  2257. {
  2258. int64_t pci_time, next_time;
  2259. uint32_t low_pci;
  2260. DPRINTF("entered rtl8139_set_next_tctr_time\n");
  2261. if (s->TimerExpire && current_time >= s->TimerExpire) {
  2262. s->IntrStatus |= PCSTimeout;
  2263. rtl8139_update_irq(s);
  2264. }
  2265. /* Set QEMU timer only if needed that is
  2266. * - TimerInt <> 0 (we have a timer)
  2267. * - mask = 1 (we want an interrupt timer)
  2268. * - irq = 0 (irq is not already active)
  2269. * If any of above change we need to compute timer again
  2270. * Also we must check if timer is passed without QEMU timer
  2271. */
  2272. s->TimerExpire = 0;
  2273. if (!s->TimerInt) {
  2274. return;
  2275. }
  2276. pci_time = muldiv64(current_time - s->TCTR_base, PCI_FREQUENCY,
  2277. get_ticks_per_sec());
  2278. low_pci = pci_time & 0xffffffff;
  2279. pci_time = pci_time - low_pci + s->TimerInt;
  2280. if (low_pci >= s->TimerInt) {
  2281. pci_time += 0x100000000LL;
  2282. }
  2283. next_time = s->TCTR_base + muldiv64(pci_time, get_ticks_per_sec(),
  2284. PCI_FREQUENCY);
  2285. s->TimerExpire = next_time;
  2286. if ((s->IntrMask & PCSTimeout) != 0 && (s->IntrStatus & PCSTimeout) == 0) {
  2287. qemu_mod_timer(s->timer, next_time);
  2288. }
  2289. }
  2290. static void rtl8139_io_writel(void *opaque, uint8_t addr, uint32_t val)
  2291. {
  2292. RTL8139State *s = opaque;
  2293. addr &= 0xfc;
  2294. switch (addr)
  2295. {
  2296. case RxMissed:
  2297. DPRINTF("RxMissed clearing on write\n");
  2298. s->RxMissed = 0;
  2299. break;
  2300. case TxConfig:
  2301. rtl8139_TxConfig_write(s, val);
  2302. break;
  2303. case RxConfig:
  2304. rtl8139_RxConfig_write(s, val);
  2305. break;
  2306. case TxStatus0 ... TxStatus0+4*4-1:
  2307. rtl8139_TxStatus_write(s, addr-TxStatus0, val);
  2308. break;
  2309. case TxAddr0 ... TxAddr0+4*4-1:
  2310. rtl8139_TxAddr_write(s, addr-TxAddr0, val);
  2311. break;
  2312. case RxBuf:
  2313. rtl8139_RxBuf_write(s, val);
  2314. break;
  2315. case RxRingAddrLO:
  2316. DPRINTF("C+ RxRing low bits write val=0x%08x\n", val);
  2317. s->RxRingAddrLO = val;
  2318. break;
  2319. case RxRingAddrHI:
  2320. DPRINTF("C+ RxRing high bits write val=0x%08x\n", val);
  2321. s->RxRingAddrHI = val;
  2322. break;
  2323. case Timer:
  2324. DPRINTF("TCTR Timer reset on write\n");
  2325. s->TCTR_base = qemu_get_clock_ns(vm_clock);
  2326. rtl8139_set_next_tctr_time(s, s->TCTR_base);
  2327. break;
  2328. case FlashReg:
  2329. DPRINTF("FlashReg TimerInt write val=0x%08x\n", val);
  2330. if (s->TimerInt != val) {
  2331. s->TimerInt = val;
  2332. rtl8139_set_next_tctr_time(s, qemu_get_clock_ns(vm_clock));
  2333. }
  2334. break;
  2335. default:
  2336. DPRINTF("ioport write(l) addr=0x%x val=0x%08x via write(b)\n",
  2337. addr, val);
  2338. rtl8139_io_writeb(opaque, addr, val & 0xff);
  2339. rtl8139_io_writeb(opaque, addr + 1, (val >> 8) & 0xff);
  2340. rtl8139_io_writeb(opaque, addr + 2, (val >> 16) & 0xff);
  2341. rtl8139_io_writeb(opaque, addr + 3, (val >> 24) & 0xff);
  2342. break;
  2343. }
  2344. }
  2345. static uint32_t rtl8139_io_readb(void *opaque, uint8_t addr)
  2346. {
  2347. RTL8139State *s = opaque;
  2348. int ret;
  2349. addr &= 0xff;
  2350. switch (addr)
  2351. {
  2352. case MAC0 ... MAC0+5:
  2353. ret = s->phys[addr - MAC0];
  2354. break;
  2355. case MAC0+6 ... MAC0+7:
  2356. ret = 0;
  2357. break;
  2358. case MAR0 ... MAR0+7:
  2359. ret = s->mult[addr - MAR0];
  2360. break;
  2361. case ChipCmd:
  2362. ret = rtl8139_ChipCmd_read(s);
  2363. break;
  2364. case Cfg9346:
  2365. ret = rtl8139_Cfg9346_read(s);
  2366. break;
  2367. case Config0:
  2368. ret = rtl8139_Config0_read(s);
  2369. break;
  2370. case Config1:
  2371. ret = rtl8139_Config1_read(s);
  2372. break;
  2373. case Config3:
  2374. ret = rtl8139_Config3_read(s);
  2375. break;
  2376. case Config4:
  2377. ret = rtl8139_Config4_read(s);
  2378. break;
  2379. case Config5:
  2380. ret = rtl8139_Config5_read(s);
  2381. break;
  2382. case MediaStatus:
  2383. ret = 0xd0;
  2384. DPRINTF("MediaStatus read 0x%x\n", ret);
  2385. break;
  2386. case HltClk:
  2387. ret = s->clock_enabled;
  2388. DPRINTF("HltClk read 0x%x\n", ret);
  2389. break;
  2390. case PCIRevisionID:
  2391. ret = RTL8139_PCI_REVID;
  2392. DPRINTF("PCI Revision ID read 0x%x\n", ret);
  2393. break;
  2394. case TxThresh:
  2395. ret = s->TxThresh;
  2396. DPRINTF("C+ TxThresh read(b) val=0x%02x\n", ret);
  2397. break;
  2398. case 0x43: /* Part of TxConfig register. Windows driver tries to read it */
  2399. ret = s->TxConfig >> 24;
  2400. DPRINTF("RTL8139C TxConfig at 0x43 read(b) val=0x%02x\n", ret);
  2401. break;
  2402. default:
  2403. DPRINTF("not implemented read(b) addr=0x%x\n", addr);
  2404. ret = 0;
  2405. break;
  2406. }
  2407. return ret;
  2408. }
  2409. static uint32_t rtl8139_io_readw(void *opaque, uint8_t addr)
  2410. {
  2411. RTL8139State *s = opaque;
  2412. uint32_t ret;
  2413. addr &= 0xfe; /* mask lower bit */
  2414. switch (addr)
  2415. {
  2416. case IntrMask:
  2417. ret = rtl8139_IntrMask_read(s);
  2418. break;
  2419. case IntrStatus:
  2420. ret = rtl8139_IntrStatus_read(s);
  2421. break;
  2422. case MultiIntr:
  2423. ret = rtl8139_MultiIntr_read(s);
  2424. break;
  2425. case RxBufPtr:
  2426. ret = rtl8139_RxBufPtr_read(s);
  2427. break;
  2428. case RxBufAddr:
  2429. ret = rtl8139_RxBufAddr_read(s);
  2430. break;
  2431. case BasicModeCtrl:
  2432. ret = rtl8139_BasicModeCtrl_read(s);
  2433. break;
  2434. case BasicModeStatus:
  2435. ret = rtl8139_BasicModeStatus_read(s);
  2436. break;
  2437. case NWayAdvert:
  2438. ret = s->NWayAdvert;
  2439. DPRINTF("NWayAdvert read(w) val=0x%04x\n", ret);
  2440. break;
  2441. case NWayLPAR:
  2442. ret = s->NWayLPAR;
  2443. DPRINTF("NWayLPAR read(w) val=0x%04x\n", ret);
  2444. break;
  2445. case NWayExpansion:
  2446. ret = s->NWayExpansion;
  2447. DPRINTF("NWayExpansion read(w) val=0x%04x\n", ret);
  2448. break;
  2449. case CpCmd:
  2450. ret = rtl8139_CpCmd_read(s);
  2451. break;
  2452. case IntrMitigate:
  2453. ret = rtl8139_IntrMitigate_read(s);
  2454. break;
  2455. case TxSummary:
  2456. ret = rtl8139_TSAD_read(s);
  2457. break;
  2458. case CSCR:
  2459. ret = rtl8139_CSCR_read(s);
  2460. break;
  2461. default:
  2462. DPRINTF("ioport read(w) addr=0x%x via read(b)\n", addr);
  2463. ret = rtl8139_io_readb(opaque, addr);
  2464. ret |= rtl8139_io_readb(opaque, addr + 1) << 8;
  2465. DPRINTF("ioport read(w) addr=0x%x val=0x%04x\n", addr, ret);
  2466. break;
  2467. }
  2468. return ret;
  2469. }
  2470. static uint32_t rtl8139_io_readl(void *opaque, uint8_t addr)
  2471. {
  2472. RTL8139State *s = opaque;
  2473. uint32_t ret;
  2474. addr &= 0xfc; /* also mask low 2 bits */
  2475. switch (addr)
  2476. {
  2477. case RxMissed:
  2478. ret = s->RxMissed;
  2479. DPRINTF("RxMissed read val=0x%08x\n", ret);
  2480. break;
  2481. case TxConfig:
  2482. ret = rtl8139_TxConfig_read(s);
  2483. break;
  2484. case RxConfig:
  2485. ret = rtl8139_RxConfig_read(s);
  2486. break;
  2487. case TxStatus0 ... TxStatus0+4*4-1:
  2488. ret = rtl8139_TxStatus_read(s, addr-TxStatus0);
  2489. break;
  2490. case TxAddr0 ... TxAddr0+4*4-1:
  2491. ret = rtl8139_TxAddr_read(s, addr-TxAddr0);
  2492. break;
  2493. case RxBuf:
  2494. ret = rtl8139_RxBuf_read(s);
  2495. break;
  2496. case RxRingAddrLO:
  2497. ret = s->RxRingAddrLO;
  2498. DPRINTF("C+ RxRing low bits read val=0x%08x\n", ret);
  2499. break;
  2500. case RxRingAddrHI:
  2501. ret = s->RxRingAddrHI;
  2502. DPRINTF("C+ RxRing high bits read val=0x%08x\n", ret);
  2503. break;
  2504. case Timer:
  2505. ret = muldiv64(qemu_get_clock_ns(vm_clock) - s->TCTR_base,
  2506. PCI_FREQUENCY, get_ticks_per_sec());
  2507. DPRINTF("TCTR Timer read val=0x%08x\n", ret);
  2508. break;
  2509. case FlashReg:
  2510. ret = s->TimerInt;
  2511. DPRINTF("FlashReg TimerInt read val=0x%08x\n", ret);
  2512. break;
  2513. default:
  2514. DPRINTF("ioport read(l) addr=0x%x via read(b)\n", addr);
  2515. ret = rtl8139_io_readb(opaque, addr);
  2516. ret |= rtl8139_io_readb(opaque, addr + 1) << 8;
  2517. ret |= rtl8139_io_readb(opaque, addr + 2) << 16;
  2518. ret |= rtl8139_io_readb(opaque, addr + 3) << 24;
  2519. DPRINTF("read(l) addr=0x%x val=%08x\n", addr, ret);
  2520. break;
  2521. }
  2522. return ret;
  2523. }
  2524. /* */
  2525. static void rtl8139_ioport_writeb(void *opaque, uint32_t addr, uint32_t val)
  2526. {
  2527. rtl8139_io_writeb(opaque, addr & 0xFF, val);
  2528. }
  2529. static void rtl8139_ioport_writew(void *opaque, uint32_t addr, uint32_t val)
  2530. {
  2531. rtl8139_io_writew(opaque, addr & 0xFF, val);
  2532. }
  2533. static void rtl8139_ioport_writel(void *opaque, uint32_t addr, uint32_t val)
  2534. {
  2535. rtl8139_io_writel(opaque, addr & 0xFF, val);
  2536. }
  2537. static uint32_t rtl8139_ioport_readb(void *opaque, uint32_t addr)
  2538. {
  2539. return rtl8139_io_readb(opaque, addr & 0xFF);
  2540. }
  2541. static uint32_t rtl8139_ioport_readw(void *opaque, uint32_t addr)
  2542. {
  2543. return rtl8139_io_readw(opaque, addr & 0xFF);
  2544. }
  2545. static uint32_t rtl8139_ioport_readl(void *opaque, uint32_t addr)
  2546. {
  2547. return rtl8139_io_readl(opaque, addr & 0xFF);
  2548. }
  2549. /* */
  2550. static void rtl8139_mmio_writeb(void *opaque, target_phys_addr_t addr, uint32_t val)
  2551. {
  2552. rtl8139_io_writeb(opaque, addr & 0xFF, val);
  2553. }
  2554. static void rtl8139_mmio_writew(void *opaque, target_phys_addr_t addr, uint32_t val)
  2555. {
  2556. rtl8139_io_writew(opaque, addr & 0xFF, val);
  2557. }
  2558. static void rtl8139_mmio_writel(void *opaque, target_phys_addr_t addr, uint32_t val)
  2559. {
  2560. rtl8139_io_writel(opaque, addr & 0xFF, val);
  2561. }
  2562. static uint32_t rtl8139_mmio_readb(void *opaque, target_phys_addr_t addr)
  2563. {
  2564. return rtl8139_io_readb(opaque, addr & 0xFF);
  2565. }
  2566. static uint32_t rtl8139_mmio_readw(void *opaque, target_phys_addr_t addr)
  2567. {
  2568. uint32_t val = rtl8139_io_readw(opaque, addr & 0xFF);
  2569. return val;
  2570. }
  2571. static uint32_t rtl8139_mmio_readl(void *opaque, target_phys_addr_t addr)
  2572. {
  2573. uint32_t val = rtl8139_io_readl(opaque, addr & 0xFF);
  2574. return val;
  2575. }
  2576. static int rtl8139_post_load(void *opaque, int version_id)
  2577. {
  2578. RTL8139State* s = opaque;
  2579. rtl8139_set_next_tctr_time(s, qemu_get_clock_ns(vm_clock));
  2580. if (version_id < 4) {
  2581. s->cplus_enabled = s->CpCmd != 0;
  2582. }
  2583. return 0;
  2584. }
  2585. static bool rtl8139_hotplug_ready_needed(void *opaque)
  2586. {
  2587. return qdev_machine_modified();
  2588. }
  2589. static const VMStateDescription vmstate_rtl8139_hotplug_ready ={
  2590. .name = "rtl8139/hotplug_ready",
  2591. .version_id = 1,
  2592. .minimum_version_id = 1,
  2593. .minimum_version_id_old = 1,
  2594. .fields = (VMStateField []) {
  2595. VMSTATE_END_OF_LIST()
  2596. }
  2597. };
  2598. static void rtl8139_pre_save(void *opaque)
  2599. {
  2600. RTL8139State* s = opaque;
  2601. int64_t current_time = qemu_get_clock_ns(vm_clock);
  2602. /* set IntrStatus correctly */
  2603. rtl8139_set_next_tctr_time(s, current_time);
  2604. s->TCTR = muldiv64(current_time - s->TCTR_base, PCI_FREQUENCY,
  2605. get_ticks_per_sec());
  2606. s->rtl8139_mmio_io_addr_dummy = s->rtl8139_mmio_io_addr;
  2607. }
  2608. static const VMStateDescription vmstate_rtl8139 = {
  2609. .name = "rtl8139",
  2610. .version_id = 4,
  2611. .minimum_version_id = 3,
  2612. .minimum_version_id_old = 3,
  2613. .post_load = rtl8139_post_load,
  2614. .pre_save = rtl8139_pre_save,
  2615. .fields = (VMStateField []) {
  2616. VMSTATE_PCI_DEVICE(dev, RTL8139State),
  2617. VMSTATE_PARTIAL_BUFFER(phys, RTL8139State, 6),
  2618. VMSTATE_BUFFER(mult, RTL8139State),
  2619. VMSTATE_UINT32_ARRAY(TxStatus, RTL8139State, 4),
  2620. VMSTATE_UINT32_ARRAY(TxAddr, RTL8139State, 4),
  2621. VMSTATE_UINT32(RxBuf, RTL8139State),
  2622. VMSTATE_UINT32(RxBufferSize, RTL8139State),
  2623. VMSTATE_UINT32(RxBufPtr, RTL8139State),
  2624. VMSTATE_UINT32(RxBufAddr, RTL8139State),
  2625. VMSTATE_UINT16(IntrStatus, RTL8139State),
  2626. VMSTATE_UINT16(IntrMask, RTL8139State),
  2627. VMSTATE_UINT32(TxConfig, RTL8139State),
  2628. VMSTATE_UINT32(RxConfig, RTL8139State),
  2629. VMSTATE_UINT32(RxMissed, RTL8139State),
  2630. VMSTATE_UINT16(CSCR, RTL8139State),
  2631. VMSTATE_UINT8(Cfg9346, RTL8139State),
  2632. VMSTATE_UINT8(Config0, RTL8139State),
  2633. VMSTATE_UINT8(Config1, RTL8139State),
  2634. VMSTATE_UINT8(Config3, RTL8139State),
  2635. VMSTATE_UINT8(Config4, RTL8139State),
  2636. VMSTATE_UINT8(Config5, RTL8139State),
  2637. VMSTATE_UINT8(clock_enabled, RTL8139State),
  2638. VMSTATE_UINT8(bChipCmdState, RTL8139State),
  2639. VMSTATE_UINT16(MultiIntr, RTL8139State),
  2640. VMSTATE_UINT16(BasicModeCtrl, RTL8139State),
  2641. VMSTATE_UINT16(BasicModeStatus, RTL8139State),
  2642. VMSTATE_UINT16(NWayAdvert, RTL8139State),
  2643. VMSTATE_UINT16(NWayLPAR, RTL8139State),
  2644. VMSTATE_UINT16(NWayExpansion, RTL8139State),
  2645. VMSTATE_UINT16(CpCmd, RTL8139State),
  2646. VMSTATE_UINT8(TxThresh, RTL8139State),
  2647. VMSTATE_UNUSED(4),
  2648. VMSTATE_MACADDR(conf.macaddr, RTL8139State),
  2649. VMSTATE_INT32(rtl8139_mmio_io_addr_dummy, RTL8139State),
  2650. VMSTATE_UINT32(currTxDesc, RTL8139State),
  2651. VMSTATE_UINT32(currCPlusRxDesc, RTL8139State),
  2652. VMSTATE_UINT32(currCPlusTxDesc, RTL8139State),
  2653. VMSTATE_UINT32(RxRingAddrLO, RTL8139State),
  2654. VMSTATE_UINT32(RxRingAddrHI, RTL8139State),
  2655. VMSTATE_UINT16_ARRAY(eeprom.contents, RTL8139State, EEPROM_9346_SIZE),
  2656. VMSTATE_INT32(eeprom.mode, RTL8139State),
  2657. VMSTATE_UINT32(eeprom.tick, RTL8139State),
  2658. VMSTATE_UINT8(eeprom.address, RTL8139State),
  2659. VMSTATE_UINT16(eeprom.input, RTL8139State),
  2660. VMSTATE_UINT16(eeprom.output, RTL8139State),
  2661. VMSTATE_UINT8(eeprom.eecs, RTL8139State),
  2662. VMSTATE_UINT8(eeprom.eesk, RTL8139State),
  2663. VMSTATE_UINT8(eeprom.eedi, RTL8139State),
  2664. VMSTATE_UINT8(eeprom.eedo, RTL8139State),
  2665. VMSTATE_UINT32(TCTR, RTL8139State),
  2666. VMSTATE_UINT32(TimerInt, RTL8139State),
  2667. VMSTATE_INT64(TCTR_base, RTL8139State),
  2668. VMSTATE_STRUCT(tally_counters, RTL8139State, 0,
  2669. vmstate_tally_counters, RTL8139TallyCounters),
  2670. VMSTATE_UINT32_V(cplus_enabled, RTL8139State, 4),
  2671. VMSTATE_END_OF_LIST()
  2672. },
  2673. .subsections = (VMStateSubsection []) {
  2674. {
  2675. .vmsd = &vmstate_rtl8139_hotplug_ready,
  2676. .needed = rtl8139_hotplug_ready_needed,
  2677. }, {
  2678. /* empty */
  2679. }
  2680. }
  2681. };
  2682. /***********************************************************/
  2683. /* PCI RTL8139 definitions */
  2684. static void rtl8139_ioport_map(PCIDevice *pci_dev, int region_num,
  2685. pcibus_t addr, pcibus_t size, int type)
  2686. {
  2687. RTL8139State *s = DO_UPCAST(RTL8139State, dev, pci_dev);
  2688. register_ioport_write(addr, 0x100, 1, rtl8139_ioport_writeb, s);
  2689. register_ioport_read( addr, 0x100, 1, rtl8139_ioport_readb, s);
  2690. register_ioport_write(addr, 0x100, 2, rtl8139_ioport_writew, s);
  2691. register_ioport_read( addr, 0x100, 2, rtl8139_ioport_readw, s);
  2692. register_ioport_write(addr, 0x100, 4, rtl8139_ioport_writel, s);
  2693. register_ioport_read( addr, 0x100, 4, rtl8139_ioport_readl, s);
  2694. }
  2695. static CPUReadMemoryFunc * const rtl8139_mmio_read[3] = {
  2696. rtl8139_mmio_readb,
  2697. rtl8139_mmio_readw,
  2698. rtl8139_mmio_readl,
  2699. };
  2700. static CPUWriteMemoryFunc * const rtl8139_mmio_write[3] = {
  2701. rtl8139_mmio_writeb,
  2702. rtl8139_mmio_writew,
  2703. rtl8139_mmio_writel,
  2704. };
  2705. static void rtl8139_timer(void *opaque)
  2706. {
  2707. RTL8139State *s = opaque;
  2708. if (!s->clock_enabled)
  2709. {
  2710. DPRINTF(">>> timer: clock is not running\n");
  2711. return;
  2712. }
  2713. s->IntrStatus |= PCSTimeout;
  2714. rtl8139_update_irq(s);
  2715. rtl8139_set_next_tctr_time(s, qemu_get_clock_ns(vm_clock));
  2716. }
  2717. static void rtl8139_cleanup(VLANClientState *nc)
  2718. {
  2719. RTL8139State *s = DO_UPCAST(NICState, nc, nc)->opaque;
  2720. s->nic = NULL;
  2721. }
  2722. static int pci_rtl8139_uninit(PCIDevice *dev)
  2723. {
  2724. RTL8139State *s = DO_UPCAST(RTL8139State, dev, dev);
  2725. cpu_unregister_io_memory(s->rtl8139_mmio_io_addr);
  2726. if (s->cplus_txbuffer) {
  2727. qemu_free(s->cplus_txbuffer);
  2728. s->cplus_txbuffer = NULL;
  2729. }
  2730. qemu_del_timer(s->timer);
  2731. qemu_free_timer(s->timer);
  2732. qemu_del_vlan_client(&s->nic->nc);
  2733. return 0;
  2734. }
  2735. static NetClientInfo net_rtl8139_info = {
  2736. .type = NET_CLIENT_TYPE_NIC,
  2737. .size = sizeof(NICState),
  2738. .can_receive = rtl8139_can_receive,
  2739. .receive = rtl8139_receive,
  2740. .cleanup = rtl8139_cleanup,
  2741. };
  2742. static int pci_rtl8139_init(PCIDevice *dev)
  2743. {
  2744. RTL8139State * s = DO_UPCAST(RTL8139State, dev, dev);
  2745. uint8_t *pci_conf;
  2746. pci_conf = s->dev.config;
  2747. pci_conf[PCI_INTERRUPT_PIN] = 1; /* interrupt pin 0 */
  2748. /* TODO: start of capability list, but no capability
  2749. * list bit in status register, and offset 0xdc seems unused. */
  2750. pci_conf[PCI_CAPABILITY_LIST] = 0xdc;
  2751. /* I/O handler for memory-mapped I/O */
  2752. s->rtl8139_mmio_io_addr =
  2753. cpu_register_io_memory(rtl8139_mmio_read, rtl8139_mmio_write, s,
  2754. DEVICE_LITTLE_ENDIAN);
  2755. pci_register_bar(&s->dev, 0, 0x100,
  2756. PCI_BASE_ADDRESS_SPACE_IO, rtl8139_ioport_map);
  2757. pci_register_bar_simple(&s->dev, 1, 0x100, 0, s->rtl8139_mmio_io_addr);
  2758. qemu_macaddr_default_if_unset(&s->conf.macaddr);
  2759. /* prepare eeprom */
  2760. s->eeprom.contents[0] = 0x8129;
  2761. #if 1
  2762. /* PCI vendor and device ID should be mirrored here */
  2763. s->eeprom.contents[1] = PCI_VENDOR_ID_REALTEK;
  2764. s->eeprom.contents[2] = PCI_DEVICE_ID_REALTEK_8139;
  2765. #endif
  2766. s->eeprom.contents[7] = s->conf.macaddr.a[0] | s->conf.macaddr.a[1] << 8;
  2767. s->eeprom.contents[8] = s->conf.macaddr.a[2] | s->conf.macaddr.a[3] << 8;
  2768. s->eeprom.contents[9] = s->conf.macaddr.a[4] | s->conf.macaddr.a[5] << 8;
  2769. s->nic = qemu_new_nic(&net_rtl8139_info, &s->conf,
  2770. dev->qdev.info->name, dev->qdev.id, s);
  2771. qemu_format_nic_info_str(&s->nic->nc, s->conf.macaddr.a);
  2772. s->cplus_txbuffer = NULL;
  2773. s->cplus_txbuffer_len = 0;
  2774. s->cplus_txbuffer_offset = 0;
  2775. s->TimerExpire = 0;
  2776. s->timer = qemu_new_timer_ns(vm_clock, rtl8139_timer, s);
  2777. rtl8139_set_next_tctr_time(s, qemu_get_clock_ns(vm_clock));
  2778. add_boot_device_path(s->conf.bootindex, &dev->qdev, "/ethernet-phy@0");
  2779. return 0;
  2780. }
  2781. static PCIDeviceInfo rtl8139_info = {
  2782. .qdev.name = "rtl8139",
  2783. .qdev.size = sizeof(RTL8139State),
  2784. .qdev.reset = rtl8139_reset,
  2785. .qdev.vmsd = &vmstate_rtl8139,
  2786. .init = pci_rtl8139_init,
  2787. .exit = pci_rtl8139_uninit,
  2788. .romfile = "pxe-rtl8139.rom",
  2789. .vendor_id = PCI_VENDOR_ID_REALTEK,
  2790. .device_id = PCI_DEVICE_ID_REALTEK_8139,
  2791. .revision = RTL8139_PCI_REVID, /* >=0x20 is for 8139C+ */
  2792. .class_id = PCI_CLASS_NETWORK_ETHERNET,
  2793. .qdev.props = (Property[]) {
  2794. DEFINE_NIC_PROPERTIES(RTL8139State, conf),
  2795. DEFINE_PROP_END_OF_LIST(),
  2796. }
  2797. };
  2798. static void rtl8139_register_devices(void)
  2799. {
  2800. pci_qdev_register(&rtl8139_info);
  2801. }
  2802. device_init(rtl8139_register_devices)