rtl8139.c 101 KB

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