2
0

rtl8139.c 97 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462
  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
  46. * Darwin)
  47. * 2011-Mar-22 Benjamin Poirier: Implemented VLAN offloading
  48. */
  49. /* For crc32 */
  50. #include "qemu/osdep.h"
  51. #include <zlib.h>
  52. #include "hw/pci/pci_device.h"
  53. #include "hw/qdev-properties.h"
  54. #include "migration/vmstate.h"
  55. #include "sysemu/dma.h"
  56. #include "qemu/module.h"
  57. #include "qemu/timer.h"
  58. #include "net/net.h"
  59. #include "net/eth.h"
  60. #include "sysemu/sysemu.h"
  61. #include "qom/object.h"
  62. /* debug RTL8139 card */
  63. //#define DEBUG_RTL8139 1
  64. #define PCI_PERIOD 30 /* 30 ns period = 33.333333 Mhz frequency */
  65. #define SET_MASKED(input, mask, curr) \
  66. ( ( (input) & ~(mask) ) | ( (curr) & (mask) ) )
  67. /* arg % size for size which is a power of 2 */
  68. #define MOD2(input, size) \
  69. ( ( input ) & ( size - 1 ) )
  70. #define ETHER_TYPE_LEN 2
  71. #define VLAN_TCI_LEN 2
  72. #define VLAN_HLEN (ETHER_TYPE_LEN + VLAN_TCI_LEN)
  73. #if defined (DEBUG_RTL8139)
  74. # define DPRINTF(fmt, ...) \
  75. do { fprintf(stderr, "RTL8139: " fmt, ## __VA_ARGS__); } while (0)
  76. #else
  77. static inline G_GNUC_PRINTF(1, 2) int DPRINTF(const char *fmt, ...)
  78. {
  79. return 0;
  80. }
  81. #endif
  82. #define TYPE_RTL8139 "rtl8139"
  83. OBJECT_DECLARE_SIMPLE_TYPE(RTL8139State, RTL8139)
  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. struct RTL8139State {
  383. /*< private >*/
  384. PCIDevice parent_obj;
  385. /*< public >*/
  386. uint8_t phys[8]; /* mac address */
  387. uint8_t mult[8]; /* multicast mask array */
  388. uint32_t TxStatus[4]; /* TxStatus0 in C mode*/ /* also DTCCR[0] and DTCCR[1] in C+ mode */
  389. uint32_t TxAddr[4]; /* TxAddr0 */
  390. uint32_t RxBuf; /* Receive buffer */
  391. uint32_t RxBufferSize;/* internal variable, receive ring buffer size in C mode */
  392. uint32_t RxBufPtr;
  393. uint32_t RxBufAddr;
  394. uint16_t IntrStatus;
  395. uint16_t IntrMask;
  396. uint32_t TxConfig;
  397. uint32_t RxConfig;
  398. uint32_t RxMissed;
  399. uint16_t CSCR;
  400. uint8_t Cfg9346;
  401. uint8_t Config0;
  402. uint8_t Config1;
  403. uint8_t Config3;
  404. uint8_t Config4;
  405. uint8_t Config5;
  406. uint8_t clock_enabled;
  407. uint8_t bChipCmdState;
  408. uint16_t MultiIntr;
  409. uint16_t BasicModeCtrl;
  410. uint16_t BasicModeStatus;
  411. uint16_t NWayAdvert;
  412. uint16_t NWayLPAR;
  413. uint16_t NWayExpansion;
  414. uint16_t CpCmd;
  415. uint8_t TxThresh;
  416. NICState *nic;
  417. NICConf conf;
  418. /* C ring mode */
  419. uint32_t currTxDesc;
  420. /* C+ mode */
  421. uint32_t cplus_enabled;
  422. uint32_t currCPlusRxDesc;
  423. uint32_t currCPlusTxDesc;
  424. uint32_t RxRingAddrLO;
  425. uint32_t RxRingAddrHI;
  426. EEprom9346 eeprom;
  427. uint32_t TCTR;
  428. uint32_t TimerInt;
  429. int64_t TCTR_base;
  430. /* Tally counters */
  431. RTL8139TallyCounters tally_counters;
  432. /* Non-persistent data */
  433. uint8_t *cplus_txbuffer;
  434. int cplus_txbuffer_len;
  435. int cplus_txbuffer_offset;
  436. /* PCI interrupt timer */
  437. QEMUTimer *timer;
  438. MemoryRegion bar_io;
  439. MemoryRegion bar_mem;
  440. /* Support migration to/from old versions */
  441. int rtl8139_mmio_io_addr_dummy;
  442. };
  443. /* Writes tally counters to memory via DMA */
  444. static void RTL8139TallyCounters_dma_write(RTL8139State *s, dma_addr_t tc_addr);
  445. static void rtl8139_set_next_tctr_time(RTL8139State *s);
  446. static void prom9346_decode_command(EEprom9346 *eeprom, uint8_t command)
  447. {
  448. DPRINTF("eeprom command 0x%02x\n", command);
  449. switch (command & Chip9346_op_mask)
  450. {
  451. case Chip9346_op_read:
  452. {
  453. eeprom->address = command & EEPROM_9346_ADDR_MASK;
  454. eeprom->output = eeprom->contents[eeprom->address];
  455. eeprom->eedo = 0;
  456. eeprom->tick = 0;
  457. eeprom->mode = Chip9346_data_read;
  458. DPRINTF("eeprom read from address 0x%02x data=0x%04x\n",
  459. eeprom->address, eeprom->output);
  460. }
  461. break;
  462. case Chip9346_op_write:
  463. {
  464. eeprom->address = command & EEPROM_9346_ADDR_MASK;
  465. eeprom->input = 0;
  466. eeprom->tick = 0;
  467. eeprom->mode = Chip9346_none; /* Chip9346_data_write */
  468. DPRINTF("eeprom begin write to address 0x%02x\n",
  469. eeprom->address);
  470. }
  471. break;
  472. default:
  473. eeprom->mode = Chip9346_none;
  474. switch (command & Chip9346_op_ext_mask)
  475. {
  476. case Chip9346_op_write_enable:
  477. DPRINTF("eeprom write enabled\n");
  478. break;
  479. case Chip9346_op_write_all:
  480. DPRINTF("eeprom begin write all\n");
  481. break;
  482. case Chip9346_op_write_disable:
  483. DPRINTF("eeprom write disabled\n");
  484. break;
  485. }
  486. break;
  487. }
  488. }
  489. static void prom9346_shift_clock(EEprom9346 *eeprom)
  490. {
  491. int bit = eeprom->eedi?1:0;
  492. ++ eeprom->tick;
  493. DPRINTF("eeprom: tick %d eedi=%d eedo=%d\n", eeprom->tick, eeprom->eedi,
  494. eeprom->eedo);
  495. switch (eeprom->mode)
  496. {
  497. case Chip9346_enter_command_mode:
  498. if (bit)
  499. {
  500. eeprom->mode = Chip9346_read_command;
  501. eeprom->tick = 0;
  502. eeprom->input = 0;
  503. DPRINTF("eeprom: +++ synchronized, begin command read\n");
  504. }
  505. break;
  506. case Chip9346_read_command:
  507. eeprom->input = (eeprom->input << 1) | (bit & 1);
  508. if (eeprom->tick == 8)
  509. {
  510. prom9346_decode_command(eeprom, eeprom->input & 0xff);
  511. }
  512. break;
  513. case Chip9346_data_read:
  514. eeprom->eedo = (eeprom->output & 0x8000)?1:0;
  515. eeprom->output <<= 1;
  516. if (eeprom->tick == 16)
  517. {
  518. #if 1
  519. // the FreeBSD drivers (rl and re) don't explicitly toggle
  520. // CS between reads (or does setting Cfg9346 to 0 count too?),
  521. // so we need to enter wait-for-command state here
  522. eeprom->mode = Chip9346_enter_command_mode;
  523. eeprom->input = 0;
  524. eeprom->tick = 0;
  525. DPRINTF("eeprom: +++ end of read, awaiting next command\n");
  526. #else
  527. // original behaviour
  528. ++eeprom->address;
  529. eeprom->address &= EEPROM_9346_ADDR_MASK;
  530. eeprom->output = eeprom->contents[eeprom->address];
  531. eeprom->tick = 0;
  532. DPRINTF("eeprom: +++ read next address 0x%02x data=0x%04x\n",
  533. eeprom->address, eeprom->output);
  534. #endif
  535. }
  536. break;
  537. case Chip9346_data_write:
  538. eeprom->input = (eeprom->input << 1) | (bit & 1);
  539. if (eeprom->tick == 16)
  540. {
  541. DPRINTF("eeprom write to address 0x%02x data=0x%04x\n",
  542. eeprom->address, eeprom->input);
  543. eeprom->contents[eeprom->address] = eeprom->input;
  544. eeprom->mode = Chip9346_none; /* waiting for next command after CS cycle */
  545. eeprom->tick = 0;
  546. eeprom->input = 0;
  547. }
  548. break;
  549. case Chip9346_data_write_all:
  550. eeprom->input = (eeprom->input << 1) | (bit & 1);
  551. if (eeprom->tick == 16)
  552. {
  553. int i;
  554. for (i = 0; i < EEPROM_9346_SIZE; i++)
  555. {
  556. eeprom->contents[i] = eeprom->input;
  557. }
  558. DPRINTF("eeprom filled with data=0x%04x\n", eeprom->input);
  559. eeprom->mode = Chip9346_enter_command_mode;
  560. eeprom->tick = 0;
  561. eeprom->input = 0;
  562. }
  563. break;
  564. default:
  565. break;
  566. }
  567. }
  568. static int prom9346_get_wire(RTL8139State *s)
  569. {
  570. EEprom9346 *eeprom = &s->eeprom;
  571. if (!eeprom->eecs)
  572. return 0;
  573. return eeprom->eedo;
  574. }
  575. /* FIXME: This should be merged into/replaced by eeprom93xx.c. */
  576. static void prom9346_set_wire(RTL8139State *s, int eecs, int eesk, int eedi)
  577. {
  578. EEprom9346 *eeprom = &s->eeprom;
  579. uint8_t old_eecs = eeprom->eecs;
  580. uint8_t old_eesk = eeprom->eesk;
  581. eeprom->eecs = eecs;
  582. eeprom->eesk = eesk;
  583. eeprom->eedi = eedi;
  584. DPRINTF("eeprom: +++ wires CS=%d SK=%d DI=%d DO=%d\n", eeprom->eecs,
  585. eeprom->eesk, eeprom->eedi, eeprom->eedo);
  586. if (!old_eecs && eecs)
  587. {
  588. /* Synchronize start */
  589. eeprom->tick = 0;
  590. eeprom->input = 0;
  591. eeprom->output = 0;
  592. eeprom->mode = Chip9346_enter_command_mode;
  593. DPRINTF("=== eeprom: begin access, enter command mode\n");
  594. }
  595. if (!eecs)
  596. {
  597. DPRINTF("=== eeprom: end access\n");
  598. return;
  599. }
  600. if (!old_eesk && eesk)
  601. {
  602. /* SK front rules */
  603. prom9346_shift_clock(eeprom);
  604. }
  605. }
  606. static void rtl8139_update_irq(RTL8139State *s)
  607. {
  608. PCIDevice *d = PCI_DEVICE(s);
  609. int isr;
  610. isr = (s->IntrStatus & s->IntrMask) & 0xffff;
  611. DPRINTF("Set IRQ to %d (%04x %04x)\n", isr ? 1 : 0, s->IntrStatus,
  612. s->IntrMask);
  613. pci_set_irq(d, (isr != 0));
  614. }
  615. static int rtl8139_RxWrap(RTL8139State *s)
  616. {
  617. /* wrapping enabled; assume 1.5k more buffer space if size < 65536 */
  618. return (s->RxConfig & (1 << 7));
  619. }
  620. static int rtl8139_receiver_enabled(RTL8139State *s)
  621. {
  622. return s->bChipCmdState & CmdRxEnb;
  623. }
  624. static int rtl8139_transmitter_enabled(RTL8139State *s)
  625. {
  626. return s->bChipCmdState & CmdTxEnb;
  627. }
  628. static int rtl8139_cp_receiver_enabled(RTL8139State *s)
  629. {
  630. return s->CpCmd & CPlusRxEnb;
  631. }
  632. static int rtl8139_cp_transmitter_enabled(RTL8139State *s)
  633. {
  634. return s->CpCmd & CPlusTxEnb;
  635. }
  636. static void rtl8139_write_buffer(RTL8139State *s, const void *buf, int size)
  637. {
  638. PCIDevice *d = PCI_DEVICE(s);
  639. if (s->RxBufAddr + size > s->RxBufferSize)
  640. {
  641. int wrapped = MOD2(s->RxBufAddr + size, s->RxBufferSize);
  642. /* write packet data */
  643. if (wrapped && !(s->RxBufferSize < 65536 && rtl8139_RxWrap(s)))
  644. {
  645. DPRINTF(">>> rx packet wrapped in buffer at %d\n", size - wrapped);
  646. if (size > wrapped)
  647. {
  648. pci_dma_write(d, s->RxBuf + s->RxBufAddr,
  649. buf, size-wrapped);
  650. }
  651. /* reset buffer pointer */
  652. s->RxBufAddr = 0;
  653. pci_dma_write(d, s->RxBuf + s->RxBufAddr,
  654. buf + (size-wrapped), wrapped);
  655. s->RxBufAddr = wrapped;
  656. return;
  657. }
  658. }
  659. /* non-wrapping path or overwrapping enabled */
  660. pci_dma_write(d, s->RxBuf + s->RxBufAddr, buf, size);
  661. s->RxBufAddr += size;
  662. }
  663. #define MIN_BUF_SIZE 60
  664. static inline dma_addr_t rtl8139_addr64(uint32_t low, uint32_t high)
  665. {
  666. return low | ((uint64_t)high << 32);
  667. }
  668. /* Workaround for buggy guest driver such as linux who allocates rx
  669. * rings after the receiver were enabled. */
  670. static bool rtl8139_cp_rx_valid(RTL8139State *s)
  671. {
  672. return !(s->RxRingAddrLO == 0 && s->RxRingAddrHI == 0);
  673. }
  674. static bool rtl8139_can_receive(NetClientState *nc)
  675. {
  676. RTL8139State *s = qemu_get_nic_opaque(nc);
  677. int avail;
  678. /* Receive (drop) packets if card is disabled. */
  679. if (!s->clock_enabled) {
  680. return true;
  681. }
  682. if (!rtl8139_receiver_enabled(s)) {
  683. return true;
  684. }
  685. if (rtl8139_cp_receiver_enabled(s) && rtl8139_cp_rx_valid(s)) {
  686. /* ??? Flow control not implemented in c+ mode.
  687. This is a hack to work around slirp deficiencies anyway. */
  688. return true;
  689. }
  690. avail = MOD2(s->RxBufferSize + s->RxBufPtr - s->RxBufAddr,
  691. s->RxBufferSize);
  692. return avail == 0 || avail >= 1514 || (s->IntrMask & RxOverflow);
  693. }
  694. static ssize_t rtl8139_do_receive(NetClientState *nc, const uint8_t *buf, size_t size_, int do_interrupt)
  695. {
  696. RTL8139State *s = qemu_get_nic_opaque(nc);
  697. PCIDevice *d = PCI_DEVICE(s);
  698. /* size is the length of the buffer passed to the driver */
  699. size_t size = size_;
  700. const uint8_t *dot1q_buf = NULL;
  701. uint32_t packet_header = 0;
  702. uint8_t buf1[MIN_BUF_SIZE + VLAN_HLEN];
  703. static const uint8_t broadcast_macaddr[6] =
  704. { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
  705. DPRINTF(">>> received len=%zu\n", size);
  706. /* test if board clock is stopped */
  707. if (!s->clock_enabled)
  708. {
  709. DPRINTF("stopped ==========================\n");
  710. return -1;
  711. }
  712. /* first check if receiver is enabled */
  713. if (!rtl8139_receiver_enabled(s))
  714. {
  715. DPRINTF("receiver disabled ================\n");
  716. return -1;
  717. }
  718. /* XXX: check this */
  719. if (s->RxConfig & AcceptAllPhys) {
  720. /* promiscuous: receive all */
  721. DPRINTF(">>> packet received in promiscuous mode\n");
  722. } else {
  723. if (!memcmp(buf, broadcast_macaddr, 6)) {
  724. /* broadcast address */
  725. if (!(s->RxConfig & AcceptBroadcast))
  726. {
  727. DPRINTF(">>> broadcast packet rejected\n");
  728. /* update tally counter */
  729. ++s->tally_counters.RxERR;
  730. return size;
  731. }
  732. packet_header |= RxBroadcast;
  733. DPRINTF(">>> broadcast packet received\n");
  734. /* update tally counter */
  735. ++s->tally_counters.RxOkBrd;
  736. } else if (buf[0] & 0x01) {
  737. /* multicast */
  738. if (!(s->RxConfig & AcceptMulticast))
  739. {
  740. DPRINTF(">>> multicast packet rejected\n");
  741. /* update tally counter */
  742. ++s->tally_counters.RxERR;
  743. return size;
  744. }
  745. int mcast_idx = net_crc32(buf, ETH_ALEN) >> 26;
  746. if (!(s->mult[mcast_idx >> 3] & (1 << (mcast_idx & 7))))
  747. {
  748. DPRINTF(">>> multicast address mismatch\n");
  749. /* update tally counter */
  750. ++s->tally_counters.RxERR;
  751. return size;
  752. }
  753. packet_header |= RxMulticast;
  754. DPRINTF(">>> multicast packet received\n");
  755. /* update tally counter */
  756. ++s->tally_counters.RxOkMul;
  757. } else if (s->phys[0] == buf[0] &&
  758. s->phys[1] == buf[1] &&
  759. s->phys[2] == buf[2] &&
  760. s->phys[3] == buf[3] &&
  761. s->phys[4] == buf[4] &&
  762. s->phys[5] == buf[5]) {
  763. /* match */
  764. if (!(s->RxConfig & AcceptMyPhys))
  765. {
  766. DPRINTF(">>> rejecting physical address matching packet\n");
  767. /* update tally counter */
  768. ++s->tally_counters.RxERR;
  769. return size;
  770. }
  771. packet_header |= RxPhysical;
  772. DPRINTF(">>> physical address matching packet received\n");
  773. /* update tally counter */
  774. ++s->tally_counters.RxOkPhy;
  775. } else {
  776. DPRINTF(">>> unknown packet\n");
  777. /* update tally counter */
  778. ++s->tally_counters.RxERR;
  779. return size;
  780. }
  781. }
  782. /* if too small buffer, then expand it
  783. * Include some tailroom in case a vlan tag is later removed. */
  784. if (size < MIN_BUF_SIZE + VLAN_HLEN) {
  785. memcpy(buf1, buf, size);
  786. memset(buf1 + size, 0, MIN_BUF_SIZE + VLAN_HLEN - size);
  787. buf = buf1;
  788. if (size < MIN_BUF_SIZE) {
  789. size = MIN_BUF_SIZE;
  790. }
  791. }
  792. if (rtl8139_cp_receiver_enabled(s))
  793. {
  794. if (!rtl8139_cp_rx_valid(s)) {
  795. return size;
  796. }
  797. DPRINTF("in C+ Rx mode ================\n");
  798. /* begin C+ receiver mode */
  799. /* w0 ownership flag */
  800. #define CP_RX_OWN (1<<31)
  801. /* w0 end of ring flag */
  802. #define CP_RX_EOR (1<<30)
  803. /* w0 bits 0...12 : buffer size */
  804. #define CP_RX_BUFFER_SIZE_MASK ((1<<13) - 1)
  805. /* w1 tag available flag */
  806. #define CP_RX_TAVA (1<<16)
  807. /* w1 bits 0...15 : VLAN tag */
  808. #define CP_RX_VLAN_TAG_MASK ((1<<16) - 1)
  809. /* w2 low 32bit of Rx buffer ptr */
  810. /* w3 high 32bit of Rx buffer ptr */
  811. int descriptor = s->currCPlusRxDesc;
  812. dma_addr_t cplus_rx_ring_desc;
  813. cplus_rx_ring_desc = rtl8139_addr64(s->RxRingAddrLO, s->RxRingAddrHI);
  814. cplus_rx_ring_desc += 16 * descriptor;
  815. DPRINTF("+++ C+ mode reading RX descriptor %d from host memory at "
  816. "%08x %08x = "DMA_ADDR_FMT"\n", descriptor, s->RxRingAddrHI,
  817. s->RxRingAddrLO, cplus_rx_ring_desc);
  818. uint32_t val, rxdw0,rxdw1,rxbufLO,rxbufHI;
  819. pci_dma_read(d, cplus_rx_ring_desc, &val, 4);
  820. rxdw0 = le32_to_cpu(val);
  821. pci_dma_read(d, cplus_rx_ring_desc+4, &val, 4);
  822. rxdw1 = le32_to_cpu(val);
  823. pci_dma_read(d, cplus_rx_ring_desc+8, &val, 4);
  824. rxbufLO = le32_to_cpu(val);
  825. pci_dma_read(d, cplus_rx_ring_desc+12, &val, 4);
  826. rxbufHI = le32_to_cpu(val);
  827. DPRINTF("+++ C+ mode RX descriptor %d %08x %08x %08x %08x\n",
  828. descriptor, rxdw0, rxdw1, rxbufLO, rxbufHI);
  829. if (!(rxdw0 & CP_RX_OWN))
  830. {
  831. DPRINTF("C+ Rx mode : descriptor %d is owned by host\n",
  832. descriptor);
  833. s->IntrStatus |= RxOverflow;
  834. ++s->RxMissed;
  835. /* update tally counter */
  836. ++s->tally_counters.RxERR;
  837. ++s->tally_counters.MissPkt;
  838. rtl8139_update_irq(s);
  839. return size_;
  840. }
  841. uint32_t rx_space = rxdw0 & CP_RX_BUFFER_SIZE_MASK;
  842. /* write VLAN info to descriptor variables. */
  843. if (s->CpCmd & CPlusRxVLAN &&
  844. lduw_be_p(&buf[ETH_ALEN * 2]) == ETH_P_VLAN) {
  845. dot1q_buf = &buf[ETH_ALEN * 2];
  846. size -= VLAN_HLEN;
  847. /* if too small buffer, use the tailroom added duing expansion */
  848. if (size < MIN_BUF_SIZE) {
  849. size = MIN_BUF_SIZE;
  850. }
  851. rxdw1 &= ~CP_RX_VLAN_TAG_MASK;
  852. /* BE + ~le_to_cpu()~ + cpu_to_le() = BE */
  853. rxdw1 |= CP_RX_TAVA | lduw_le_p(&dot1q_buf[ETHER_TYPE_LEN]);
  854. DPRINTF("C+ Rx mode : extracted vlan tag with tci: ""%u\n",
  855. lduw_be_p(&dot1q_buf[ETHER_TYPE_LEN]));
  856. } else {
  857. /* reset VLAN tag flag */
  858. rxdw1 &= ~CP_RX_TAVA;
  859. }
  860. /* TODO: scatter the packet over available receive ring descriptors space */
  861. if (size+4 > rx_space)
  862. {
  863. DPRINTF("C+ Rx mode : descriptor %d size %d received %zu + 4\n",
  864. descriptor, rx_space, size);
  865. s->IntrStatus |= RxOverflow;
  866. ++s->RxMissed;
  867. /* update tally counter */
  868. ++s->tally_counters.RxERR;
  869. ++s->tally_counters.MissPkt;
  870. rtl8139_update_irq(s);
  871. return size_;
  872. }
  873. dma_addr_t rx_addr = rtl8139_addr64(rxbufLO, rxbufHI);
  874. /* receive/copy to target memory */
  875. if (dot1q_buf) {
  876. pci_dma_write(d, rx_addr, buf, 2 * ETH_ALEN);
  877. pci_dma_write(d, rx_addr + 2 * ETH_ALEN,
  878. buf + 2 * ETH_ALEN + VLAN_HLEN,
  879. size - 2 * ETH_ALEN);
  880. } else {
  881. pci_dma_write(d, rx_addr, buf, size);
  882. }
  883. if (s->CpCmd & CPlusRxChkSum)
  884. {
  885. /* do some packet checksumming */
  886. }
  887. /* write checksum */
  888. val = cpu_to_le32(crc32(0, buf, size_));
  889. pci_dma_write(d, rx_addr+size, (uint8_t *)&val, 4);
  890. /* first segment of received packet flag */
  891. #define CP_RX_STATUS_FS (1<<29)
  892. /* last segment of received packet flag */
  893. #define CP_RX_STATUS_LS (1<<28)
  894. /* multicast packet flag */
  895. #define CP_RX_STATUS_MAR (1<<26)
  896. /* physical-matching packet flag */
  897. #define CP_RX_STATUS_PAM (1<<25)
  898. /* broadcast packet flag */
  899. #define CP_RX_STATUS_BAR (1<<24)
  900. /* runt packet flag */
  901. #define CP_RX_STATUS_RUNT (1<<19)
  902. /* crc error flag */
  903. #define CP_RX_STATUS_CRC (1<<18)
  904. /* IP checksum error flag */
  905. #define CP_RX_STATUS_IPF (1<<15)
  906. /* UDP checksum error flag */
  907. #define CP_RX_STATUS_UDPF (1<<14)
  908. /* TCP checksum error flag */
  909. #define CP_RX_STATUS_TCPF (1<<13)
  910. /* transfer ownership to target */
  911. rxdw0 &= ~CP_RX_OWN;
  912. /* set first segment bit */
  913. rxdw0 |= CP_RX_STATUS_FS;
  914. /* set last segment bit */
  915. rxdw0 |= CP_RX_STATUS_LS;
  916. /* set received packet type flags */
  917. if (packet_header & RxBroadcast)
  918. rxdw0 |= CP_RX_STATUS_BAR;
  919. if (packet_header & RxMulticast)
  920. rxdw0 |= CP_RX_STATUS_MAR;
  921. if (packet_header & RxPhysical)
  922. rxdw0 |= CP_RX_STATUS_PAM;
  923. /* set received size */
  924. rxdw0 &= ~CP_RX_BUFFER_SIZE_MASK;
  925. rxdw0 |= (size+4);
  926. /* update ring data */
  927. val = cpu_to_le32(rxdw0);
  928. pci_dma_write(d, cplus_rx_ring_desc, (uint8_t *)&val, 4);
  929. val = cpu_to_le32(rxdw1);
  930. pci_dma_write(d, cplus_rx_ring_desc+4, (uint8_t *)&val, 4);
  931. /* update tally counter */
  932. ++s->tally_counters.RxOk;
  933. /* seek to next Rx descriptor */
  934. if (rxdw0 & CP_RX_EOR)
  935. {
  936. s->currCPlusRxDesc = 0;
  937. }
  938. else
  939. {
  940. ++s->currCPlusRxDesc;
  941. }
  942. DPRINTF("done C+ Rx mode ----------------\n");
  943. }
  944. else
  945. {
  946. DPRINTF("in ring Rx mode ================\n");
  947. /* begin ring receiver mode */
  948. int avail = MOD2(s->RxBufferSize + s->RxBufPtr - s->RxBufAddr, s->RxBufferSize);
  949. /* if receiver buffer is empty then avail == 0 */
  950. #define RX_ALIGN(x) (((x) + 3) & ~0x3)
  951. if (avail != 0 && RX_ALIGN(size + 8) >= avail)
  952. {
  953. DPRINTF("rx overflow: rx buffer length %d head 0x%04x "
  954. "read 0x%04x === available 0x%04x need 0x%04zx\n",
  955. s->RxBufferSize, s->RxBufAddr, s->RxBufPtr, avail, size + 8);
  956. s->IntrStatus |= RxOverflow;
  957. ++s->RxMissed;
  958. rtl8139_update_irq(s);
  959. return 0;
  960. }
  961. packet_header |= RxStatusOK;
  962. packet_header |= (((size+4) << 16) & 0xffff0000);
  963. /* write header */
  964. uint32_t val = cpu_to_le32(packet_header);
  965. rtl8139_write_buffer(s, (uint8_t *)&val, 4);
  966. rtl8139_write_buffer(s, buf, size);
  967. /* write checksum */
  968. val = cpu_to_le32(crc32(0, buf, size));
  969. rtl8139_write_buffer(s, (uint8_t *)&val, 4);
  970. /* correct buffer write pointer */
  971. s->RxBufAddr = MOD2(RX_ALIGN(s->RxBufAddr), s->RxBufferSize);
  972. /* now we can signal we have received something */
  973. DPRINTF("received: rx buffer length %d head 0x%04x read 0x%04x\n",
  974. s->RxBufferSize, s->RxBufAddr, s->RxBufPtr);
  975. }
  976. s->IntrStatus |= RxOK;
  977. if (do_interrupt)
  978. {
  979. rtl8139_update_irq(s);
  980. }
  981. return size_;
  982. }
  983. static ssize_t rtl8139_receive(NetClientState *nc, const uint8_t *buf, size_t size)
  984. {
  985. return rtl8139_do_receive(nc, buf, size, 1);
  986. }
  987. static void rtl8139_reset_rxring(RTL8139State *s, uint32_t bufferSize)
  988. {
  989. s->RxBufferSize = bufferSize;
  990. s->RxBufPtr = 0;
  991. s->RxBufAddr = 0;
  992. }
  993. static void rtl8139_reset_phy(RTL8139State *s)
  994. {
  995. s->BasicModeStatus = 0x7809;
  996. s->BasicModeStatus |= 0x0020; /* autonegotiation completed */
  997. /* preserve link state */
  998. s->BasicModeStatus |= qemu_get_queue(s->nic)->link_down ? 0 : 0x04;
  999. s->NWayAdvert = 0x05e1; /* all modes, full duplex */
  1000. s->NWayLPAR = 0x05e1; /* all modes, full duplex */
  1001. s->NWayExpansion = 0x0001; /* autonegotiation supported */
  1002. s->CSCR = CSCR_F_LINK_100 | CSCR_HEART_BIT | CSCR_LD;
  1003. }
  1004. static void rtl8139_reset(DeviceState *d)
  1005. {
  1006. RTL8139State *s = RTL8139(d);
  1007. int i;
  1008. /* restore MAC address */
  1009. memcpy(s->phys, s->conf.macaddr.a, 6);
  1010. qemu_format_nic_info_str(qemu_get_queue(s->nic), s->phys);
  1011. /* reset interrupt mask */
  1012. s->IntrStatus = 0;
  1013. s->IntrMask = 0;
  1014. rtl8139_update_irq(s);
  1015. /* mark all status registers as owned by host */
  1016. for (i = 0; i < 4; ++i)
  1017. {
  1018. s->TxStatus[i] = TxHostOwns;
  1019. }
  1020. s->currTxDesc = 0;
  1021. s->currCPlusRxDesc = 0;
  1022. s->currCPlusTxDesc = 0;
  1023. s->RxRingAddrLO = 0;
  1024. s->RxRingAddrHI = 0;
  1025. s->RxBuf = 0;
  1026. rtl8139_reset_rxring(s, 8192);
  1027. /* ACK the reset */
  1028. s->TxConfig = 0;
  1029. #if 0
  1030. // s->TxConfig |= HW_REVID(1, 0, 0, 0, 0, 0, 0); // RTL-8139 HasHltClk
  1031. s->clock_enabled = 0;
  1032. #else
  1033. s->TxConfig |= HW_REVID(1, 1, 1, 0, 1, 1, 0); // RTL-8139C+ HasLWake
  1034. s->clock_enabled = 1;
  1035. #endif
  1036. s->bChipCmdState = CmdReset; /* RxBufEmpty bit is calculated on read from ChipCmd */;
  1037. /* set initial state data */
  1038. s->Config0 = 0x0; /* No boot ROM */
  1039. s->Config1 = 0xC; /* IO mapped and MEM mapped registers available */
  1040. s->Config3 = 0x1; /* fast back-to-back compatible */
  1041. s->Config5 = 0x0;
  1042. s->CpCmd = 0x0; /* reset C+ mode */
  1043. s->cplus_enabled = 0;
  1044. // s->BasicModeCtrl = 0x3100; // 100Mbps, full duplex, autonegotiation
  1045. // s->BasicModeCtrl = 0x2100; // 100Mbps, full duplex
  1046. s->BasicModeCtrl = 0x1000; // autonegotiation
  1047. rtl8139_reset_phy(s);
  1048. /* also reset timer and disable timer interrupt */
  1049. s->TCTR = 0;
  1050. s->TimerInt = 0;
  1051. s->TCTR_base = 0;
  1052. rtl8139_set_next_tctr_time(s);
  1053. /* reset tally counters */
  1054. RTL8139TallyCounters_clear(&s->tally_counters);
  1055. }
  1056. static void RTL8139TallyCounters_clear(RTL8139TallyCounters* counters)
  1057. {
  1058. counters->TxOk = 0;
  1059. counters->RxOk = 0;
  1060. counters->TxERR = 0;
  1061. counters->RxERR = 0;
  1062. counters->MissPkt = 0;
  1063. counters->FAE = 0;
  1064. counters->Tx1Col = 0;
  1065. counters->TxMCol = 0;
  1066. counters->RxOkPhy = 0;
  1067. counters->RxOkBrd = 0;
  1068. counters->RxOkMul = 0;
  1069. counters->TxAbt = 0;
  1070. counters->TxUndrn = 0;
  1071. }
  1072. static void RTL8139TallyCounters_dma_write(RTL8139State *s, dma_addr_t tc_addr)
  1073. {
  1074. PCIDevice *d = PCI_DEVICE(s);
  1075. RTL8139TallyCounters *tally_counters = &s->tally_counters;
  1076. uint16_t val16;
  1077. uint32_t val32;
  1078. uint64_t val64;
  1079. val64 = cpu_to_le64(tally_counters->TxOk);
  1080. pci_dma_write(d, tc_addr + 0, (uint8_t *)&val64, 8);
  1081. val64 = cpu_to_le64(tally_counters->RxOk);
  1082. pci_dma_write(d, tc_addr + 8, (uint8_t *)&val64, 8);
  1083. val64 = cpu_to_le64(tally_counters->TxERR);
  1084. pci_dma_write(d, tc_addr + 16, (uint8_t *)&val64, 8);
  1085. val32 = cpu_to_le32(tally_counters->RxERR);
  1086. pci_dma_write(d, tc_addr + 24, (uint8_t *)&val32, 4);
  1087. val16 = cpu_to_le16(tally_counters->MissPkt);
  1088. pci_dma_write(d, tc_addr + 28, (uint8_t *)&val16, 2);
  1089. val16 = cpu_to_le16(tally_counters->FAE);
  1090. pci_dma_write(d, tc_addr + 30, (uint8_t *)&val16, 2);
  1091. val32 = cpu_to_le32(tally_counters->Tx1Col);
  1092. pci_dma_write(d, tc_addr + 32, (uint8_t *)&val32, 4);
  1093. val32 = cpu_to_le32(tally_counters->TxMCol);
  1094. pci_dma_write(d, tc_addr + 36, (uint8_t *)&val32, 4);
  1095. val64 = cpu_to_le64(tally_counters->RxOkPhy);
  1096. pci_dma_write(d, tc_addr + 40, (uint8_t *)&val64, 8);
  1097. val64 = cpu_to_le64(tally_counters->RxOkBrd);
  1098. pci_dma_write(d, tc_addr + 48, (uint8_t *)&val64, 8);
  1099. val32 = cpu_to_le32(tally_counters->RxOkMul);
  1100. pci_dma_write(d, tc_addr + 56, (uint8_t *)&val32, 4);
  1101. val16 = cpu_to_le16(tally_counters->TxAbt);
  1102. pci_dma_write(d, tc_addr + 60, (uint8_t *)&val16, 2);
  1103. val16 = cpu_to_le16(tally_counters->TxUndrn);
  1104. pci_dma_write(d, tc_addr + 62, (uint8_t *)&val16, 2);
  1105. }
  1106. static void rtl8139_ChipCmd_write(RTL8139State *s, uint32_t val)
  1107. {
  1108. DeviceState *d = DEVICE(s);
  1109. val &= 0xff;
  1110. DPRINTF("ChipCmd write val=0x%08x\n", val);
  1111. if (val & CmdReset)
  1112. {
  1113. DPRINTF("ChipCmd reset\n");
  1114. rtl8139_reset(d);
  1115. }
  1116. if (val & CmdRxEnb)
  1117. {
  1118. DPRINTF("ChipCmd enable receiver\n");
  1119. s->currCPlusRxDesc = 0;
  1120. }
  1121. if (val & CmdTxEnb)
  1122. {
  1123. DPRINTF("ChipCmd enable transmitter\n");
  1124. s->currCPlusTxDesc = 0;
  1125. }
  1126. /* mask unwritable bits */
  1127. val = SET_MASKED(val, 0xe3, s->bChipCmdState);
  1128. /* Deassert reset pin before next read */
  1129. val &= ~CmdReset;
  1130. s->bChipCmdState = val;
  1131. }
  1132. static int rtl8139_RxBufferEmpty(RTL8139State *s)
  1133. {
  1134. int unread = MOD2(s->RxBufferSize + s->RxBufAddr - s->RxBufPtr, s->RxBufferSize);
  1135. if (unread != 0)
  1136. {
  1137. DPRINTF("receiver buffer data available 0x%04x\n", unread);
  1138. return 0;
  1139. }
  1140. DPRINTF("receiver buffer is empty\n");
  1141. return 1;
  1142. }
  1143. static uint32_t rtl8139_ChipCmd_read(RTL8139State *s)
  1144. {
  1145. uint32_t ret = s->bChipCmdState;
  1146. if (rtl8139_RxBufferEmpty(s))
  1147. ret |= RxBufEmpty;
  1148. DPRINTF("ChipCmd read val=0x%04x\n", ret);
  1149. return ret;
  1150. }
  1151. static void rtl8139_CpCmd_write(RTL8139State *s, uint32_t val)
  1152. {
  1153. val &= 0xffff;
  1154. DPRINTF("C+ command register write(w) val=0x%04x\n", val);
  1155. s->cplus_enabled = 1;
  1156. /* mask unwritable bits */
  1157. val = SET_MASKED(val, 0xff84, s->CpCmd);
  1158. s->CpCmd = val;
  1159. }
  1160. static uint32_t rtl8139_CpCmd_read(RTL8139State *s)
  1161. {
  1162. uint32_t ret = s->CpCmd;
  1163. DPRINTF("C+ command register read(w) val=0x%04x\n", ret);
  1164. return ret;
  1165. }
  1166. static void rtl8139_IntrMitigate_write(RTL8139State *s, uint32_t val)
  1167. {
  1168. DPRINTF("C+ IntrMitigate register write(w) val=0x%04x\n", val);
  1169. }
  1170. static uint32_t rtl8139_IntrMitigate_read(RTL8139State *s)
  1171. {
  1172. uint32_t ret = 0;
  1173. DPRINTF("C+ IntrMitigate register read(w) val=0x%04x\n", ret);
  1174. return ret;
  1175. }
  1176. static int rtl8139_config_writable(RTL8139State *s)
  1177. {
  1178. if ((s->Cfg9346 & Chip9346_op_mask) == Cfg9346_ConfigWrite)
  1179. {
  1180. return 1;
  1181. }
  1182. DPRINTF("Configuration registers are write-protected\n");
  1183. return 0;
  1184. }
  1185. static void rtl8139_BasicModeCtrl_write(RTL8139State *s, uint32_t val)
  1186. {
  1187. val &= 0xffff;
  1188. DPRINTF("BasicModeCtrl register write(w) val=0x%04x\n", val);
  1189. /* mask unwritable bits */
  1190. uint32_t mask = 0xccff;
  1191. if (1 || !rtl8139_config_writable(s))
  1192. {
  1193. /* Speed setting and autonegotiation enable bits are read-only */
  1194. mask |= 0x3000;
  1195. /* Duplex mode setting is read-only */
  1196. mask |= 0x0100;
  1197. }
  1198. if (val & 0x8000) {
  1199. /* Reset PHY */
  1200. rtl8139_reset_phy(s);
  1201. }
  1202. val = SET_MASKED(val, mask, s->BasicModeCtrl);
  1203. s->BasicModeCtrl = val;
  1204. }
  1205. static uint32_t rtl8139_BasicModeCtrl_read(RTL8139State *s)
  1206. {
  1207. uint32_t ret = s->BasicModeCtrl;
  1208. DPRINTF("BasicModeCtrl register read(w) val=0x%04x\n", ret);
  1209. return ret;
  1210. }
  1211. static void rtl8139_BasicModeStatus_write(RTL8139State *s, uint32_t val)
  1212. {
  1213. val &= 0xffff;
  1214. DPRINTF("BasicModeStatus register write(w) val=0x%04x\n", val);
  1215. /* mask unwritable bits */
  1216. val = SET_MASKED(val, 0xff3f, s->BasicModeStatus);
  1217. s->BasicModeStatus = val;
  1218. }
  1219. static uint32_t rtl8139_BasicModeStatus_read(RTL8139State *s)
  1220. {
  1221. uint32_t ret = s->BasicModeStatus;
  1222. DPRINTF("BasicModeStatus register read(w) val=0x%04x\n", ret);
  1223. return ret;
  1224. }
  1225. static void rtl8139_Cfg9346_write(RTL8139State *s, uint32_t val)
  1226. {
  1227. DeviceState *d = DEVICE(s);
  1228. val &= 0xff;
  1229. DPRINTF("Cfg9346 write val=0x%02x\n", val);
  1230. /* mask unwritable bits */
  1231. val = SET_MASKED(val, 0x31, s->Cfg9346);
  1232. uint32_t opmode = val & 0xc0;
  1233. uint32_t eeprom_val = val & 0xf;
  1234. if (opmode == 0x80) {
  1235. /* eeprom access */
  1236. int eecs = (eeprom_val & 0x08)?1:0;
  1237. int eesk = (eeprom_val & 0x04)?1:0;
  1238. int eedi = (eeprom_val & 0x02)?1:0;
  1239. prom9346_set_wire(s, eecs, eesk, eedi);
  1240. } else if (opmode == 0x40) {
  1241. /* Reset. */
  1242. val = 0;
  1243. rtl8139_reset(d);
  1244. }
  1245. s->Cfg9346 = val;
  1246. }
  1247. static uint32_t rtl8139_Cfg9346_read(RTL8139State *s)
  1248. {
  1249. uint32_t ret = s->Cfg9346;
  1250. uint32_t opmode = ret & 0xc0;
  1251. if (opmode == 0x80)
  1252. {
  1253. /* eeprom access */
  1254. int eedo = prom9346_get_wire(s);
  1255. if (eedo)
  1256. {
  1257. ret |= 0x01;
  1258. }
  1259. else
  1260. {
  1261. ret &= ~0x01;
  1262. }
  1263. }
  1264. DPRINTF("Cfg9346 read val=0x%02x\n", ret);
  1265. return ret;
  1266. }
  1267. static void rtl8139_Config0_write(RTL8139State *s, uint32_t val)
  1268. {
  1269. val &= 0xff;
  1270. DPRINTF("Config0 write val=0x%02x\n", val);
  1271. if (!rtl8139_config_writable(s)) {
  1272. return;
  1273. }
  1274. /* mask unwritable bits */
  1275. val = SET_MASKED(val, 0xf8, s->Config0);
  1276. s->Config0 = val;
  1277. }
  1278. static uint32_t rtl8139_Config0_read(RTL8139State *s)
  1279. {
  1280. uint32_t ret = s->Config0;
  1281. DPRINTF("Config0 read val=0x%02x\n", ret);
  1282. return ret;
  1283. }
  1284. static void rtl8139_Config1_write(RTL8139State *s, uint32_t val)
  1285. {
  1286. val &= 0xff;
  1287. DPRINTF("Config1 write val=0x%02x\n", val);
  1288. if (!rtl8139_config_writable(s)) {
  1289. return;
  1290. }
  1291. /* mask unwritable bits */
  1292. val = SET_MASKED(val, 0xC, s->Config1);
  1293. s->Config1 = val;
  1294. }
  1295. static uint32_t rtl8139_Config1_read(RTL8139State *s)
  1296. {
  1297. uint32_t ret = s->Config1;
  1298. DPRINTF("Config1 read val=0x%02x\n", ret);
  1299. return ret;
  1300. }
  1301. static void rtl8139_Config3_write(RTL8139State *s, uint32_t val)
  1302. {
  1303. val &= 0xff;
  1304. DPRINTF("Config3 write val=0x%02x\n", val);
  1305. if (!rtl8139_config_writable(s)) {
  1306. return;
  1307. }
  1308. /* mask unwritable bits */
  1309. val = SET_MASKED(val, 0x8F, s->Config3);
  1310. s->Config3 = val;
  1311. }
  1312. static uint32_t rtl8139_Config3_read(RTL8139State *s)
  1313. {
  1314. uint32_t ret = s->Config3;
  1315. DPRINTF("Config3 read val=0x%02x\n", ret);
  1316. return ret;
  1317. }
  1318. static void rtl8139_Config4_write(RTL8139State *s, uint32_t val)
  1319. {
  1320. val &= 0xff;
  1321. DPRINTF("Config4 write val=0x%02x\n", val);
  1322. if (!rtl8139_config_writable(s)) {
  1323. return;
  1324. }
  1325. /* mask unwritable bits */
  1326. val = SET_MASKED(val, 0x0a, s->Config4);
  1327. s->Config4 = val;
  1328. }
  1329. static uint32_t rtl8139_Config4_read(RTL8139State *s)
  1330. {
  1331. uint32_t ret = s->Config4;
  1332. DPRINTF("Config4 read val=0x%02x\n", ret);
  1333. return ret;
  1334. }
  1335. static void rtl8139_Config5_write(RTL8139State *s, uint32_t val)
  1336. {
  1337. val &= 0xff;
  1338. DPRINTF("Config5 write val=0x%02x\n", val);
  1339. /* mask unwritable bits */
  1340. val = SET_MASKED(val, 0x80, s->Config5);
  1341. s->Config5 = val;
  1342. }
  1343. static uint32_t rtl8139_Config5_read(RTL8139State *s)
  1344. {
  1345. uint32_t ret = s->Config5;
  1346. DPRINTF("Config5 read val=0x%02x\n", ret);
  1347. return ret;
  1348. }
  1349. static void rtl8139_TxConfig_write(RTL8139State *s, uint32_t val)
  1350. {
  1351. if (!rtl8139_transmitter_enabled(s))
  1352. {
  1353. DPRINTF("transmitter disabled; no TxConfig write val=0x%08x\n", val);
  1354. return;
  1355. }
  1356. DPRINTF("TxConfig write val=0x%08x\n", val);
  1357. val = SET_MASKED(val, TxVersionMask | 0x8070f80f, s->TxConfig);
  1358. s->TxConfig = val;
  1359. }
  1360. static void rtl8139_TxConfig_writeb(RTL8139State *s, uint32_t val)
  1361. {
  1362. DPRINTF("RTL8139C TxConfig via write(b) val=0x%02x\n", val);
  1363. uint32_t tc = s->TxConfig;
  1364. tc &= 0xFFFFFF00;
  1365. tc |= (val & 0x000000FF);
  1366. rtl8139_TxConfig_write(s, tc);
  1367. }
  1368. static uint32_t rtl8139_TxConfig_read(RTL8139State *s)
  1369. {
  1370. uint32_t ret = s->TxConfig;
  1371. DPRINTF("TxConfig read val=0x%04x\n", ret);
  1372. return ret;
  1373. }
  1374. static void rtl8139_RxConfig_write(RTL8139State *s, uint32_t val)
  1375. {
  1376. DPRINTF("RxConfig write val=0x%08x\n", val);
  1377. /* mask unwritable bits */
  1378. val = SET_MASKED(val, 0xf0fc0040, s->RxConfig);
  1379. s->RxConfig = val;
  1380. /* reset buffer size and read/write pointers */
  1381. rtl8139_reset_rxring(s, 8192 << ((s->RxConfig >> 11) & 0x3));
  1382. DPRINTF("RxConfig write reset buffer size to %d\n", s->RxBufferSize);
  1383. }
  1384. static uint32_t rtl8139_RxConfig_read(RTL8139State *s)
  1385. {
  1386. uint32_t ret = s->RxConfig;
  1387. DPRINTF("RxConfig read val=0x%08x\n", ret);
  1388. return ret;
  1389. }
  1390. static void rtl8139_transfer_frame(RTL8139State *s, uint8_t *buf, int size,
  1391. int do_interrupt, const uint8_t *dot1q_buf)
  1392. {
  1393. struct iovec *iov = NULL;
  1394. struct iovec vlan_iov[3];
  1395. if (!size)
  1396. {
  1397. DPRINTF("+++ empty ethernet frame\n");
  1398. return;
  1399. }
  1400. if (dot1q_buf && size >= ETH_ALEN * 2) {
  1401. iov = (struct iovec[3]) {
  1402. { .iov_base = buf, .iov_len = ETH_ALEN * 2 },
  1403. { .iov_base = (void *) dot1q_buf, .iov_len = VLAN_HLEN },
  1404. { .iov_base = buf + ETH_ALEN * 2,
  1405. .iov_len = size - ETH_ALEN * 2 },
  1406. };
  1407. memcpy(vlan_iov, iov, sizeof(vlan_iov));
  1408. iov = vlan_iov;
  1409. }
  1410. if (TxLoopBack == (s->TxConfig & TxLoopBack))
  1411. {
  1412. size_t buf2_size;
  1413. uint8_t *buf2;
  1414. if (iov) {
  1415. buf2_size = iov_size(iov, 3);
  1416. buf2 = g_malloc(buf2_size);
  1417. iov_to_buf(iov, 3, 0, buf2, buf2_size);
  1418. buf = buf2;
  1419. }
  1420. DPRINTF("+++ transmit loopback mode\n");
  1421. qemu_receive_packet(qemu_get_queue(s->nic), buf, size);
  1422. if (iov) {
  1423. g_free(buf2);
  1424. }
  1425. }
  1426. else
  1427. {
  1428. if (iov) {
  1429. qemu_sendv_packet(qemu_get_queue(s->nic), iov, 3);
  1430. } else {
  1431. qemu_send_packet(qemu_get_queue(s->nic), buf, size);
  1432. }
  1433. }
  1434. }
  1435. static int rtl8139_transmit_one(RTL8139State *s, int descriptor)
  1436. {
  1437. if (!rtl8139_transmitter_enabled(s))
  1438. {
  1439. DPRINTF("+++ cannot transmit from descriptor %d: transmitter "
  1440. "disabled\n", descriptor);
  1441. return 0;
  1442. }
  1443. if (s->TxStatus[descriptor] & TxHostOwns)
  1444. {
  1445. DPRINTF("+++ cannot transmit from descriptor %d: owned by host "
  1446. "(%08x)\n", descriptor, s->TxStatus[descriptor]);
  1447. return 0;
  1448. }
  1449. DPRINTF("+++ transmitting from descriptor %d\n", descriptor);
  1450. PCIDevice *d = PCI_DEVICE(s);
  1451. int txsize = s->TxStatus[descriptor] & 0x1fff;
  1452. uint8_t txbuffer[0x2000];
  1453. DPRINTF("+++ transmit reading %d bytes from host memory at 0x%08x\n",
  1454. txsize, s->TxAddr[descriptor]);
  1455. pci_dma_read(d, s->TxAddr[descriptor], txbuffer, txsize);
  1456. /* Mark descriptor as transferred */
  1457. s->TxStatus[descriptor] |= TxHostOwns;
  1458. s->TxStatus[descriptor] |= TxStatOK;
  1459. rtl8139_transfer_frame(s, txbuffer, txsize, 0, NULL);
  1460. DPRINTF("+++ transmitted %d bytes from descriptor %d\n", txsize,
  1461. descriptor);
  1462. /* update interrupt */
  1463. s->IntrStatus |= TxOK;
  1464. rtl8139_update_irq(s);
  1465. return 1;
  1466. }
  1467. #define TCP_HEADER_CLEAR_FLAGS(tcp, off) ((tcp)->th_offset_flags &= cpu_to_be16(~TCP_FLAGS_ONLY(off)))
  1468. /* produces ones' complement sum of data */
  1469. static uint16_t ones_complement_sum(uint8_t *data, size_t len)
  1470. {
  1471. uint32_t result = 0;
  1472. for (; len > 1; data+=2, len-=2)
  1473. {
  1474. result += *(uint16_t*)data;
  1475. }
  1476. /* add the remainder byte */
  1477. if (len)
  1478. {
  1479. uint8_t odd[2] = {*data, 0};
  1480. result += *(uint16_t*)odd;
  1481. }
  1482. while (result>>16)
  1483. result = (result & 0xffff) + (result >> 16);
  1484. return result;
  1485. }
  1486. static uint16_t ip_checksum(void *data, size_t len)
  1487. {
  1488. return ~ones_complement_sum((uint8_t*)data, len);
  1489. }
  1490. static int rtl8139_cplus_transmit_one(RTL8139State *s)
  1491. {
  1492. if (!rtl8139_transmitter_enabled(s))
  1493. {
  1494. DPRINTF("+++ C+ mode: transmitter disabled\n");
  1495. return 0;
  1496. }
  1497. if (!rtl8139_cp_transmitter_enabled(s))
  1498. {
  1499. DPRINTF("+++ C+ mode: C+ transmitter disabled\n");
  1500. return 0 ;
  1501. }
  1502. PCIDevice *d = PCI_DEVICE(s);
  1503. int descriptor = s->currCPlusTxDesc;
  1504. dma_addr_t cplus_tx_ring_desc = rtl8139_addr64(s->TxAddr[0], s->TxAddr[1]);
  1505. /* Normal priority ring */
  1506. cplus_tx_ring_desc += 16 * descriptor;
  1507. DPRINTF("+++ C+ mode reading TX descriptor %d from host memory at "
  1508. "%08x %08x = 0x"DMA_ADDR_FMT"\n", descriptor, s->TxAddr[1],
  1509. s->TxAddr[0], cplus_tx_ring_desc);
  1510. uint32_t val, txdw0,txdw1,txbufLO,txbufHI;
  1511. pci_dma_read(d, cplus_tx_ring_desc, (uint8_t *)&val, 4);
  1512. txdw0 = le32_to_cpu(val);
  1513. pci_dma_read(d, cplus_tx_ring_desc+4, (uint8_t *)&val, 4);
  1514. txdw1 = le32_to_cpu(val);
  1515. pci_dma_read(d, cplus_tx_ring_desc+8, (uint8_t *)&val, 4);
  1516. txbufLO = le32_to_cpu(val);
  1517. pci_dma_read(d, cplus_tx_ring_desc+12, (uint8_t *)&val, 4);
  1518. txbufHI = le32_to_cpu(val);
  1519. DPRINTF("+++ C+ mode TX descriptor %d %08x %08x %08x %08x\n", descriptor,
  1520. txdw0, txdw1, txbufLO, txbufHI);
  1521. /* w0 ownership flag */
  1522. #define CP_TX_OWN (1<<31)
  1523. /* w0 end of ring flag */
  1524. #define CP_TX_EOR (1<<30)
  1525. /* first segment of received packet flag */
  1526. #define CP_TX_FS (1<<29)
  1527. /* last segment of received packet flag */
  1528. #define CP_TX_LS (1<<28)
  1529. /* large send packet flag */
  1530. #define CP_TX_LGSEN (1<<27)
  1531. /* large send MSS mask, bits 16...26 */
  1532. #define CP_TC_LGSEN_MSS_SHIFT 16
  1533. #define CP_TC_LGSEN_MSS_MASK ((1 << 11) - 1)
  1534. /* IP checksum offload flag */
  1535. #define CP_TX_IPCS (1<<18)
  1536. /* UDP checksum offload flag */
  1537. #define CP_TX_UDPCS (1<<17)
  1538. /* TCP checksum offload flag */
  1539. #define CP_TX_TCPCS (1<<16)
  1540. /* w0 bits 0...15 : buffer size */
  1541. #define CP_TX_BUFFER_SIZE (1<<16)
  1542. #define CP_TX_BUFFER_SIZE_MASK (CP_TX_BUFFER_SIZE - 1)
  1543. /* w1 add tag flag */
  1544. #define CP_TX_TAGC (1<<17)
  1545. /* w1 bits 0...15 : VLAN tag (big endian) */
  1546. #define CP_TX_VLAN_TAG_MASK ((1<<16) - 1)
  1547. /* w2 low 32bit of Rx buffer ptr */
  1548. /* w3 high 32bit of Rx buffer ptr */
  1549. /* set after transmission */
  1550. /* FIFO underrun flag */
  1551. #define CP_TX_STATUS_UNF (1<<25)
  1552. /* transmit error summary flag, valid if set any of three below */
  1553. #define CP_TX_STATUS_TES (1<<23)
  1554. /* out-of-window collision flag */
  1555. #define CP_TX_STATUS_OWC (1<<22)
  1556. /* link failure flag */
  1557. #define CP_TX_STATUS_LNKF (1<<21)
  1558. /* excessive collisions flag */
  1559. #define CP_TX_STATUS_EXC (1<<20)
  1560. if (!(txdw0 & CP_TX_OWN))
  1561. {
  1562. DPRINTF("C+ Tx mode : descriptor %d is owned by host\n", descriptor);
  1563. return 0 ;
  1564. }
  1565. DPRINTF("+++ C+ Tx mode : transmitting from descriptor %d\n", descriptor);
  1566. if (txdw0 & CP_TX_FS)
  1567. {
  1568. DPRINTF("+++ C+ Tx mode : descriptor %d is first segment "
  1569. "descriptor\n", descriptor);
  1570. /* reset internal buffer offset */
  1571. s->cplus_txbuffer_offset = 0;
  1572. }
  1573. int txsize = txdw0 & CP_TX_BUFFER_SIZE_MASK;
  1574. dma_addr_t tx_addr = rtl8139_addr64(txbufLO, txbufHI);
  1575. /* make sure we have enough space to assemble the packet */
  1576. if (!s->cplus_txbuffer)
  1577. {
  1578. s->cplus_txbuffer_len = CP_TX_BUFFER_SIZE;
  1579. s->cplus_txbuffer = g_malloc(s->cplus_txbuffer_len);
  1580. s->cplus_txbuffer_offset = 0;
  1581. DPRINTF("+++ C+ mode transmission buffer allocated space %d\n",
  1582. s->cplus_txbuffer_len);
  1583. }
  1584. if (s->cplus_txbuffer_offset + txsize >= s->cplus_txbuffer_len)
  1585. {
  1586. /* The spec didn't tell the maximum size, stick to CP_TX_BUFFER_SIZE */
  1587. txsize = s->cplus_txbuffer_len - s->cplus_txbuffer_offset;
  1588. DPRINTF("+++ C+ mode transmission buffer overrun, truncated descriptor"
  1589. "length to %d\n", txsize);
  1590. }
  1591. /* append more data to the packet */
  1592. DPRINTF("+++ C+ mode transmit reading %d bytes from host memory at "
  1593. DMA_ADDR_FMT" to offset %d\n", txsize, tx_addr,
  1594. s->cplus_txbuffer_offset);
  1595. pci_dma_read(d, tx_addr,
  1596. s->cplus_txbuffer + s->cplus_txbuffer_offset, txsize);
  1597. s->cplus_txbuffer_offset += txsize;
  1598. /* seek to next Rx descriptor */
  1599. if (txdw0 & CP_TX_EOR)
  1600. {
  1601. s->currCPlusTxDesc = 0;
  1602. }
  1603. else
  1604. {
  1605. ++s->currCPlusTxDesc;
  1606. if (s->currCPlusTxDesc >= 64)
  1607. s->currCPlusTxDesc = 0;
  1608. }
  1609. /* Build the Tx Status Descriptor */
  1610. uint32_t tx_status = txdw0;
  1611. /* transfer ownership to target */
  1612. tx_status &= ~CP_TX_OWN;
  1613. /* reset error indicator bits */
  1614. tx_status &= ~CP_TX_STATUS_UNF;
  1615. tx_status &= ~CP_TX_STATUS_TES;
  1616. tx_status &= ~CP_TX_STATUS_OWC;
  1617. tx_status &= ~CP_TX_STATUS_LNKF;
  1618. tx_status &= ~CP_TX_STATUS_EXC;
  1619. /* update ring data */
  1620. val = cpu_to_le32(tx_status);
  1621. pci_dma_write(d, cplus_tx_ring_desc, (uint8_t *)&val, 4);
  1622. /* Now decide if descriptor being processed is holding the last segment of packet */
  1623. if (txdw0 & CP_TX_LS)
  1624. {
  1625. uint8_t dot1q_buffer_space[VLAN_HLEN];
  1626. uint16_t *dot1q_buffer;
  1627. DPRINTF("+++ C+ Tx mode : descriptor %d is last segment descriptor\n",
  1628. descriptor);
  1629. /* can transfer fully assembled packet */
  1630. uint8_t *saved_buffer = s->cplus_txbuffer;
  1631. int saved_size = s->cplus_txbuffer_offset;
  1632. int saved_buffer_len = s->cplus_txbuffer_len;
  1633. /* create vlan tag */
  1634. if (txdw1 & CP_TX_TAGC) {
  1635. /* the vlan tag is in BE byte order in the descriptor
  1636. * BE + le_to_cpu() + ~swap()~ = cpu */
  1637. DPRINTF("+++ C+ Tx mode : inserting vlan tag with ""tci: %u\n",
  1638. bswap16(txdw1 & CP_TX_VLAN_TAG_MASK));
  1639. dot1q_buffer = (uint16_t *) dot1q_buffer_space;
  1640. dot1q_buffer[0] = cpu_to_be16(ETH_P_VLAN);
  1641. /* BE + le_to_cpu() + ~cpu_to_le()~ = BE */
  1642. dot1q_buffer[1] = cpu_to_le16(txdw1 & CP_TX_VLAN_TAG_MASK);
  1643. } else {
  1644. dot1q_buffer = NULL;
  1645. }
  1646. /* reset the card space to protect from recursive call */
  1647. s->cplus_txbuffer = NULL;
  1648. s->cplus_txbuffer_offset = 0;
  1649. s->cplus_txbuffer_len = 0;
  1650. if (txdw0 & (CP_TX_IPCS | CP_TX_UDPCS | CP_TX_TCPCS | CP_TX_LGSEN))
  1651. {
  1652. DPRINTF("+++ C+ mode offloaded task checksum\n");
  1653. /* Large enough for Ethernet and IP headers? */
  1654. if (saved_size < ETH_HLEN + sizeof(struct ip_header)) {
  1655. goto skip_offload;
  1656. }
  1657. /* ip packet header */
  1658. struct ip_header *ip = NULL;
  1659. int hlen = 0;
  1660. uint8_t ip_protocol = 0;
  1661. uint16_t ip_data_len = 0;
  1662. uint8_t *eth_payload_data = NULL;
  1663. size_t eth_payload_len = 0;
  1664. int proto = be16_to_cpu(*(uint16_t *)(saved_buffer + 12));
  1665. if (proto != ETH_P_IP)
  1666. {
  1667. goto skip_offload;
  1668. }
  1669. DPRINTF("+++ C+ mode has IP packet\n");
  1670. /* Note on memory alignment: eth_payload_data is 16-bit aligned
  1671. * since saved_buffer is allocated with g_malloc() and ETH_HLEN is
  1672. * even. 32-bit accesses must use ldl/stl wrappers to avoid
  1673. * unaligned accesses.
  1674. */
  1675. eth_payload_data = saved_buffer + ETH_HLEN;
  1676. eth_payload_len = saved_size - ETH_HLEN;
  1677. ip = (struct ip_header*)eth_payload_data;
  1678. if (IP_HEADER_VERSION(ip) != IP_HEADER_VERSION_4) {
  1679. DPRINTF("+++ C+ mode packet has bad IP version %d "
  1680. "expected %d\n", IP_HEADER_VERSION(ip),
  1681. IP_HEADER_VERSION_4);
  1682. goto skip_offload;
  1683. }
  1684. hlen = IP_HDR_GET_LEN(ip);
  1685. if (hlen < sizeof(struct ip_header) || hlen > eth_payload_len) {
  1686. goto skip_offload;
  1687. }
  1688. ip_protocol = ip->ip_p;
  1689. ip_data_len = be16_to_cpu(ip->ip_len);
  1690. if (ip_data_len < hlen || ip_data_len > eth_payload_len) {
  1691. goto skip_offload;
  1692. }
  1693. ip_data_len -= hlen;
  1694. if (!(txdw0 & CP_TX_LGSEN) && (txdw0 & CP_TX_IPCS))
  1695. {
  1696. DPRINTF("+++ C+ mode need IP checksum\n");
  1697. ip->ip_sum = 0;
  1698. ip->ip_sum = ip_checksum(ip, hlen);
  1699. DPRINTF("+++ C+ mode IP header len=%d checksum=%04x\n",
  1700. hlen, ip->ip_sum);
  1701. }
  1702. if ((txdw0 & CP_TX_LGSEN) && ip_protocol == IP_PROTO_TCP)
  1703. {
  1704. /* Large enough for the TCP header? */
  1705. if (ip_data_len < sizeof(tcp_header)) {
  1706. goto skip_offload;
  1707. }
  1708. int large_send_mss = (txdw0 >> CP_TC_LGSEN_MSS_SHIFT) &
  1709. CP_TC_LGSEN_MSS_MASK;
  1710. if (large_send_mss == 0) {
  1711. goto skip_offload;
  1712. }
  1713. DPRINTF("+++ C+ mode offloaded task TSO IP data %d "
  1714. "frame data %d specified MSS=%d\n",
  1715. ip_data_len, saved_size - ETH_HLEN, large_send_mss);
  1716. int tcp_send_offset = 0;
  1717. /* maximum IP header length is 60 bytes */
  1718. uint8_t saved_ip_header[60];
  1719. /* save IP header template; data area is used in tcp checksum calculation */
  1720. memcpy(saved_ip_header, eth_payload_data, hlen);
  1721. /* a placeholder for checksum calculation routine in tcp case */
  1722. uint8_t *data_to_checksum = eth_payload_data + hlen - 12;
  1723. // size_t data_to_checksum_len = eth_payload_len - hlen + 12;
  1724. /* pointer to TCP header */
  1725. tcp_header *p_tcp_hdr = (tcp_header*)(eth_payload_data + hlen);
  1726. int tcp_hlen = TCP_HEADER_DATA_OFFSET(p_tcp_hdr);
  1727. /* Invalid TCP data offset? */
  1728. if (tcp_hlen < sizeof(tcp_header) || tcp_hlen > ip_data_len) {
  1729. goto skip_offload;
  1730. }
  1731. int tcp_data_len = ip_data_len - tcp_hlen;
  1732. DPRINTF("+++ C+ mode TSO IP data len %d TCP hlen %d TCP "
  1733. "data len %d\n", ip_data_len, tcp_hlen, tcp_data_len);
  1734. /* note the cycle below overwrites IP header data,
  1735. but restores it from saved_ip_header before sending packet */
  1736. int is_last_frame = 0;
  1737. for (tcp_send_offset = 0; tcp_send_offset < tcp_data_len; tcp_send_offset += large_send_mss)
  1738. {
  1739. uint16_t chunk_size = large_send_mss;
  1740. /* check if this is the last frame */
  1741. if (tcp_send_offset + large_send_mss >= tcp_data_len)
  1742. {
  1743. is_last_frame = 1;
  1744. chunk_size = tcp_data_len - tcp_send_offset;
  1745. }
  1746. DPRINTF("+++ C+ mode TSO TCP seqno %08x\n",
  1747. ldl_be_p(&p_tcp_hdr->th_seq));
  1748. /* add 4 TCP pseudoheader fields */
  1749. /* copy IP source and destination fields */
  1750. memcpy(data_to_checksum, saved_ip_header + 12, 8);
  1751. DPRINTF("+++ C+ mode TSO calculating TCP checksum for "
  1752. "packet with %d bytes data\n", tcp_hlen +
  1753. chunk_size);
  1754. if (tcp_send_offset)
  1755. {
  1756. memcpy((uint8_t*)p_tcp_hdr + tcp_hlen, (uint8_t*)p_tcp_hdr + tcp_hlen + tcp_send_offset, chunk_size);
  1757. }
  1758. /* keep PUSH and FIN flags only for the last frame */
  1759. if (!is_last_frame)
  1760. {
  1761. TCP_HEADER_CLEAR_FLAGS(p_tcp_hdr, TH_PUSH | TH_FIN);
  1762. }
  1763. /* recalculate TCP checksum */
  1764. ip_pseudo_header *p_tcpip_hdr = (ip_pseudo_header *)data_to_checksum;
  1765. p_tcpip_hdr->zeros = 0;
  1766. p_tcpip_hdr->ip_proto = IP_PROTO_TCP;
  1767. p_tcpip_hdr->ip_payload = cpu_to_be16(tcp_hlen + chunk_size);
  1768. p_tcp_hdr->th_sum = 0;
  1769. int tcp_checksum = ip_checksum(data_to_checksum, tcp_hlen + chunk_size + 12);
  1770. DPRINTF("+++ C+ mode TSO TCP checksum %04x\n",
  1771. tcp_checksum);
  1772. p_tcp_hdr->th_sum = tcp_checksum;
  1773. /* restore IP header */
  1774. memcpy(eth_payload_data, saved_ip_header, hlen);
  1775. /* set IP data length and recalculate IP checksum */
  1776. ip->ip_len = cpu_to_be16(hlen + tcp_hlen + chunk_size);
  1777. /* increment IP id for subsequent frames */
  1778. ip->ip_id = cpu_to_be16(tcp_send_offset/large_send_mss + be16_to_cpu(ip->ip_id));
  1779. ip->ip_sum = 0;
  1780. ip->ip_sum = ip_checksum(eth_payload_data, hlen);
  1781. DPRINTF("+++ C+ mode TSO IP header len=%d "
  1782. "checksum=%04x\n", hlen, ip->ip_sum);
  1783. int tso_send_size = ETH_HLEN + hlen + tcp_hlen + chunk_size;
  1784. DPRINTF("+++ C+ mode TSO transferring packet size "
  1785. "%d\n", tso_send_size);
  1786. rtl8139_transfer_frame(s, saved_buffer, tso_send_size,
  1787. 0, (uint8_t *) dot1q_buffer);
  1788. /* add transferred count to TCP sequence number */
  1789. stl_be_p(&p_tcp_hdr->th_seq,
  1790. chunk_size + ldl_be_p(&p_tcp_hdr->th_seq));
  1791. }
  1792. /* Stop sending this frame */
  1793. saved_size = 0;
  1794. }
  1795. else if (!(txdw0 & CP_TX_LGSEN) && (txdw0 & (CP_TX_TCPCS|CP_TX_UDPCS)))
  1796. {
  1797. DPRINTF("+++ C+ mode need TCP or UDP checksum\n");
  1798. /* maximum IP header length is 60 bytes */
  1799. uint8_t saved_ip_header[60];
  1800. memcpy(saved_ip_header, eth_payload_data, hlen);
  1801. uint8_t *data_to_checksum = eth_payload_data + hlen - 12;
  1802. // size_t data_to_checksum_len = eth_payload_len - hlen + 12;
  1803. /* add 4 TCP pseudoheader fields */
  1804. /* copy IP source and destination fields */
  1805. memcpy(data_to_checksum, saved_ip_header + 12, 8);
  1806. if ((txdw0 & CP_TX_TCPCS) && ip_protocol == IP_PROTO_TCP)
  1807. {
  1808. DPRINTF("+++ C+ mode calculating TCP checksum for "
  1809. "packet with %d bytes data\n", ip_data_len);
  1810. ip_pseudo_header *p_tcpip_hdr = (ip_pseudo_header *)data_to_checksum;
  1811. p_tcpip_hdr->zeros = 0;
  1812. p_tcpip_hdr->ip_proto = IP_PROTO_TCP;
  1813. p_tcpip_hdr->ip_payload = cpu_to_be16(ip_data_len);
  1814. tcp_header* p_tcp_hdr = (tcp_header *) (data_to_checksum+12);
  1815. p_tcp_hdr->th_sum = 0;
  1816. int tcp_checksum = ip_checksum(data_to_checksum, ip_data_len + 12);
  1817. DPRINTF("+++ C+ mode TCP checksum %04x\n",
  1818. tcp_checksum);
  1819. p_tcp_hdr->th_sum = tcp_checksum;
  1820. }
  1821. else if ((txdw0 & CP_TX_UDPCS) && ip_protocol == IP_PROTO_UDP)
  1822. {
  1823. DPRINTF("+++ C+ mode calculating UDP checksum for "
  1824. "packet with %d bytes data\n", ip_data_len);
  1825. ip_pseudo_header *p_udpip_hdr = (ip_pseudo_header *)data_to_checksum;
  1826. p_udpip_hdr->zeros = 0;
  1827. p_udpip_hdr->ip_proto = IP_PROTO_UDP;
  1828. p_udpip_hdr->ip_payload = cpu_to_be16(ip_data_len);
  1829. udp_header *p_udp_hdr = (udp_header *) (data_to_checksum+12);
  1830. p_udp_hdr->uh_sum = 0;
  1831. int udp_checksum = ip_checksum(data_to_checksum, ip_data_len + 12);
  1832. DPRINTF("+++ C+ mode UDP checksum %04x\n",
  1833. udp_checksum);
  1834. p_udp_hdr->uh_sum = udp_checksum;
  1835. }
  1836. /* restore IP header */
  1837. memcpy(eth_payload_data, saved_ip_header, hlen);
  1838. }
  1839. }
  1840. skip_offload:
  1841. /* update tally counter */
  1842. ++s->tally_counters.TxOk;
  1843. DPRINTF("+++ C+ mode transmitting %d bytes packet\n", saved_size);
  1844. rtl8139_transfer_frame(s, saved_buffer, saved_size, 1,
  1845. (uint8_t *) dot1q_buffer);
  1846. /* restore card space if there was no recursion and reset offset */
  1847. if (!s->cplus_txbuffer)
  1848. {
  1849. s->cplus_txbuffer = saved_buffer;
  1850. s->cplus_txbuffer_len = saved_buffer_len;
  1851. s->cplus_txbuffer_offset = 0;
  1852. }
  1853. else
  1854. {
  1855. g_free(saved_buffer);
  1856. }
  1857. }
  1858. else
  1859. {
  1860. DPRINTF("+++ C+ mode transmission continue to next descriptor\n");
  1861. }
  1862. return 1;
  1863. }
  1864. static void rtl8139_cplus_transmit(RTL8139State *s)
  1865. {
  1866. int txcount = 0;
  1867. while (txcount < 64 && rtl8139_cplus_transmit_one(s))
  1868. {
  1869. ++txcount;
  1870. }
  1871. /* Mark transfer completed */
  1872. if (!txcount)
  1873. {
  1874. DPRINTF("C+ mode : transmitter queue stalled, current TxDesc = %d\n",
  1875. s->currCPlusTxDesc);
  1876. }
  1877. else
  1878. {
  1879. /* update interrupt status */
  1880. s->IntrStatus |= TxOK;
  1881. rtl8139_update_irq(s);
  1882. }
  1883. }
  1884. static void rtl8139_transmit(RTL8139State *s)
  1885. {
  1886. int descriptor = s->currTxDesc, txcount = 0;
  1887. /*while*/
  1888. if (rtl8139_transmit_one(s, descriptor))
  1889. {
  1890. ++s->currTxDesc;
  1891. s->currTxDesc %= 4;
  1892. ++txcount;
  1893. }
  1894. /* Mark transfer completed */
  1895. if (!txcount)
  1896. {
  1897. DPRINTF("transmitter queue stalled, current TxDesc = %d\n",
  1898. s->currTxDesc);
  1899. }
  1900. }
  1901. static void rtl8139_TxStatus_write(RTL8139State *s, uint32_t txRegOffset, uint32_t val)
  1902. {
  1903. int descriptor = txRegOffset/4;
  1904. /* handle C+ transmit mode register configuration */
  1905. if (s->cplus_enabled)
  1906. {
  1907. DPRINTF("RTL8139C+ DTCCR write offset=0x%x val=0x%08x "
  1908. "descriptor=%d\n", txRegOffset, val, descriptor);
  1909. /* handle Dump Tally Counters command */
  1910. s->TxStatus[descriptor] = val;
  1911. if (descriptor == 0 && (val & 0x8))
  1912. {
  1913. hwaddr tc_addr = rtl8139_addr64(s->TxStatus[0] & ~0x3f, s->TxStatus[1]);
  1914. /* dump tally counters to specified memory location */
  1915. RTL8139TallyCounters_dma_write(s, tc_addr);
  1916. /* mark dump completed */
  1917. s->TxStatus[0] &= ~0x8;
  1918. }
  1919. return;
  1920. }
  1921. DPRINTF("TxStatus write offset=0x%x val=0x%08x descriptor=%d\n",
  1922. txRegOffset, val, descriptor);
  1923. /* mask only reserved bits */
  1924. val &= ~0xff00c000; /* these bits are reset on write */
  1925. val = SET_MASKED(val, 0x00c00000, s->TxStatus[descriptor]);
  1926. s->TxStatus[descriptor] = val;
  1927. /* attempt to start transmission */
  1928. rtl8139_transmit(s);
  1929. }
  1930. static uint32_t rtl8139_TxStatus_TxAddr_read(RTL8139State *s, uint32_t regs[],
  1931. uint32_t base, uint8_t addr,
  1932. int size)
  1933. {
  1934. uint32_t reg = (addr - base) / 4;
  1935. uint32_t offset = addr & 0x3;
  1936. uint32_t ret = 0;
  1937. if (addr & (size - 1)) {
  1938. DPRINTF("not implemented read for TxStatus/TxAddr "
  1939. "addr=0x%x size=0x%x\n", addr, size);
  1940. return ret;
  1941. }
  1942. switch (size) {
  1943. case 1: /* fall through */
  1944. case 2: /* fall through */
  1945. case 4:
  1946. ret = (regs[reg] >> offset * 8) & (((uint64_t)1 << (size * 8)) - 1);
  1947. DPRINTF("TxStatus/TxAddr[%d] read addr=0x%x size=0x%x val=0x%08x\n",
  1948. reg, addr, size, ret);
  1949. break;
  1950. default:
  1951. DPRINTF("unsupported size 0x%x of TxStatus/TxAddr reading\n", size);
  1952. break;
  1953. }
  1954. return ret;
  1955. }
  1956. static uint16_t rtl8139_TSAD_read(RTL8139State *s)
  1957. {
  1958. uint16_t ret = 0;
  1959. /* Simulate TSAD, it is read only anyway */
  1960. ret = ((s->TxStatus[3] & TxStatOK )?TSAD_TOK3:0)
  1961. |((s->TxStatus[2] & TxStatOK )?TSAD_TOK2:0)
  1962. |((s->TxStatus[1] & TxStatOK )?TSAD_TOK1:0)
  1963. |((s->TxStatus[0] & TxStatOK )?TSAD_TOK0:0)
  1964. |((s->TxStatus[3] & TxUnderrun)?TSAD_TUN3:0)
  1965. |((s->TxStatus[2] & TxUnderrun)?TSAD_TUN2:0)
  1966. |((s->TxStatus[1] & TxUnderrun)?TSAD_TUN1:0)
  1967. |((s->TxStatus[0] & TxUnderrun)?TSAD_TUN0:0)
  1968. |((s->TxStatus[3] & TxAborted )?TSAD_TABT3:0)
  1969. |((s->TxStatus[2] & TxAborted )?TSAD_TABT2:0)
  1970. |((s->TxStatus[1] & TxAborted )?TSAD_TABT1:0)
  1971. |((s->TxStatus[0] & TxAborted )?TSAD_TABT0:0)
  1972. |((s->TxStatus[3] & TxHostOwns )?TSAD_OWN3:0)
  1973. |((s->TxStatus[2] & TxHostOwns )?TSAD_OWN2:0)
  1974. |((s->TxStatus[1] & TxHostOwns )?TSAD_OWN1:0)
  1975. |((s->TxStatus[0] & TxHostOwns )?TSAD_OWN0:0) ;
  1976. DPRINTF("TSAD read val=0x%04x\n", ret);
  1977. return ret;
  1978. }
  1979. static uint16_t rtl8139_CSCR_read(RTL8139State *s)
  1980. {
  1981. uint16_t ret = s->CSCR;
  1982. DPRINTF("CSCR read val=0x%04x\n", ret);
  1983. return ret;
  1984. }
  1985. static void rtl8139_TxAddr_write(RTL8139State *s, uint32_t txAddrOffset, uint32_t val)
  1986. {
  1987. DPRINTF("TxAddr write offset=0x%x val=0x%08x\n", txAddrOffset, val);
  1988. s->TxAddr[txAddrOffset/4] = val;
  1989. }
  1990. static uint32_t rtl8139_TxAddr_read(RTL8139State *s, uint32_t txAddrOffset)
  1991. {
  1992. uint32_t ret = s->TxAddr[txAddrOffset/4];
  1993. DPRINTF("TxAddr read offset=0x%x val=0x%08x\n", txAddrOffset, ret);
  1994. return ret;
  1995. }
  1996. static void rtl8139_RxBufPtr_write(RTL8139State *s, uint32_t val)
  1997. {
  1998. DPRINTF("RxBufPtr write val=0x%04x\n", val);
  1999. /* this value is off by 16 */
  2000. s->RxBufPtr = MOD2(val + 0x10, s->RxBufferSize);
  2001. /* more buffer space may be available so try to receive */
  2002. qemu_flush_queued_packets(qemu_get_queue(s->nic));
  2003. DPRINTF(" CAPR write: rx buffer length %d head 0x%04x read 0x%04x\n",
  2004. s->RxBufferSize, s->RxBufAddr, s->RxBufPtr);
  2005. }
  2006. static uint32_t rtl8139_RxBufPtr_read(RTL8139State *s)
  2007. {
  2008. /* this value is off by 16 */
  2009. uint32_t ret = s->RxBufPtr - 0x10;
  2010. DPRINTF("RxBufPtr read val=0x%04x\n", ret);
  2011. return ret;
  2012. }
  2013. static uint32_t rtl8139_RxBufAddr_read(RTL8139State *s)
  2014. {
  2015. /* this value is NOT off by 16 */
  2016. uint32_t ret = s->RxBufAddr;
  2017. DPRINTF("RxBufAddr read val=0x%04x\n", ret);
  2018. return ret;
  2019. }
  2020. static void rtl8139_RxBuf_write(RTL8139State *s, uint32_t val)
  2021. {
  2022. DPRINTF("RxBuf write val=0x%08x\n", val);
  2023. s->RxBuf = val;
  2024. /* may need to reset rxring here */
  2025. }
  2026. static uint32_t rtl8139_RxBuf_read(RTL8139State *s)
  2027. {
  2028. uint32_t ret = s->RxBuf;
  2029. DPRINTF("RxBuf read val=0x%08x\n", ret);
  2030. return ret;
  2031. }
  2032. static void rtl8139_IntrMask_write(RTL8139State *s, uint32_t val)
  2033. {
  2034. DPRINTF("IntrMask write(w) val=0x%04x\n", val);
  2035. /* mask unwritable bits */
  2036. val = SET_MASKED(val, 0x1e00, s->IntrMask);
  2037. s->IntrMask = val;
  2038. rtl8139_update_irq(s);
  2039. }
  2040. static uint32_t rtl8139_IntrMask_read(RTL8139State *s)
  2041. {
  2042. uint32_t ret = s->IntrMask;
  2043. DPRINTF("IntrMask read(w) val=0x%04x\n", ret);
  2044. return ret;
  2045. }
  2046. static void rtl8139_IntrStatus_write(RTL8139State *s, uint32_t val)
  2047. {
  2048. DPRINTF("IntrStatus write(w) val=0x%04x\n", val);
  2049. #if 0
  2050. /* writing to ISR has no effect */
  2051. return;
  2052. #else
  2053. uint16_t newStatus = s->IntrStatus & ~val;
  2054. /* mask unwritable bits */
  2055. newStatus = SET_MASKED(newStatus, 0x1e00, s->IntrStatus);
  2056. /* writing 1 to interrupt status register bit clears it */
  2057. s->IntrStatus = 0;
  2058. rtl8139_update_irq(s);
  2059. s->IntrStatus = newStatus;
  2060. rtl8139_set_next_tctr_time(s);
  2061. rtl8139_update_irq(s);
  2062. #endif
  2063. }
  2064. static uint32_t rtl8139_IntrStatus_read(RTL8139State *s)
  2065. {
  2066. uint32_t ret = s->IntrStatus;
  2067. DPRINTF("IntrStatus read(w) val=0x%04x\n", ret);
  2068. #if 0
  2069. /* reading ISR clears all interrupts */
  2070. s->IntrStatus = 0;
  2071. rtl8139_update_irq(s);
  2072. #endif
  2073. return ret;
  2074. }
  2075. static void rtl8139_MultiIntr_write(RTL8139State *s, uint32_t val)
  2076. {
  2077. DPRINTF("MultiIntr write(w) val=0x%04x\n", val);
  2078. /* mask unwritable bits */
  2079. val = SET_MASKED(val, 0xf000, s->MultiIntr);
  2080. s->MultiIntr = val;
  2081. }
  2082. static uint32_t rtl8139_MultiIntr_read(RTL8139State *s)
  2083. {
  2084. uint32_t ret = s->MultiIntr;
  2085. DPRINTF("MultiIntr read(w) val=0x%04x\n", ret);
  2086. return ret;
  2087. }
  2088. static void rtl8139_io_writeb(void *opaque, uint8_t addr, uint32_t val)
  2089. {
  2090. RTL8139State *s = opaque;
  2091. switch (addr)
  2092. {
  2093. case MAC0 ... MAC0+4:
  2094. s->phys[addr - MAC0] = val;
  2095. break;
  2096. case MAC0+5:
  2097. s->phys[addr - MAC0] = val;
  2098. qemu_format_nic_info_str(qemu_get_queue(s->nic), s->phys);
  2099. break;
  2100. case MAC0+6 ... MAC0+7:
  2101. /* reserved */
  2102. break;
  2103. case MAR0 ... MAR0+7:
  2104. s->mult[addr - MAR0] = val;
  2105. break;
  2106. case ChipCmd:
  2107. rtl8139_ChipCmd_write(s, val);
  2108. break;
  2109. case Cfg9346:
  2110. rtl8139_Cfg9346_write(s, val);
  2111. break;
  2112. case TxConfig: /* windows driver sometimes writes using byte-lenth call */
  2113. rtl8139_TxConfig_writeb(s, val);
  2114. break;
  2115. case Config0:
  2116. rtl8139_Config0_write(s, val);
  2117. break;
  2118. case Config1:
  2119. rtl8139_Config1_write(s, val);
  2120. break;
  2121. case Config3:
  2122. rtl8139_Config3_write(s, val);
  2123. break;
  2124. case Config4:
  2125. rtl8139_Config4_write(s, val);
  2126. break;
  2127. case Config5:
  2128. rtl8139_Config5_write(s, val);
  2129. break;
  2130. case MediaStatus:
  2131. /* ignore */
  2132. DPRINTF("not implemented write(b) to MediaStatus val=0x%02x\n",
  2133. val);
  2134. break;
  2135. case HltClk:
  2136. DPRINTF("HltClk write val=0x%08x\n", val);
  2137. if (val == 'R')
  2138. {
  2139. s->clock_enabled = 1;
  2140. }
  2141. else if (val == 'H')
  2142. {
  2143. s->clock_enabled = 0;
  2144. }
  2145. break;
  2146. case TxThresh:
  2147. DPRINTF("C+ TxThresh write(b) val=0x%02x\n", val);
  2148. s->TxThresh = val;
  2149. break;
  2150. case TxPoll:
  2151. DPRINTF("C+ TxPoll write(b) val=0x%02x\n", val);
  2152. if (val & (1 << 7))
  2153. {
  2154. DPRINTF("C+ TxPoll high priority transmission (not "
  2155. "implemented)\n");
  2156. //rtl8139_cplus_transmit(s);
  2157. }
  2158. if (val & (1 << 6))
  2159. {
  2160. DPRINTF("C+ TxPoll normal priority transmission\n");
  2161. rtl8139_cplus_transmit(s);
  2162. }
  2163. break;
  2164. default:
  2165. DPRINTF("not implemented write(b) addr=0x%x val=0x%02x\n", addr,
  2166. val);
  2167. break;
  2168. }
  2169. }
  2170. static void rtl8139_io_writew(void *opaque, uint8_t addr, uint32_t val)
  2171. {
  2172. RTL8139State *s = opaque;
  2173. switch (addr)
  2174. {
  2175. case IntrMask:
  2176. rtl8139_IntrMask_write(s, val);
  2177. break;
  2178. case IntrStatus:
  2179. rtl8139_IntrStatus_write(s, val);
  2180. break;
  2181. case MultiIntr:
  2182. rtl8139_MultiIntr_write(s, val);
  2183. break;
  2184. case RxBufPtr:
  2185. rtl8139_RxBufPtr_write(s, val);
  2186. break;
  2187. case BasicModeCtrl:
  2188. rtl8139_BasicModeCtrl_write(s, val);
  2189. break;
  2190. case BasicModeStatus:
  2191. rtl8139_BasicModeStatus_write(s, val);
  2192. break;
  2193. case NWayAdvert:
  2194. DPRINTF("NWayAdvert write(w) val=0x%04x\n", val);
  2195. s->NWayAdvert = val;
  2196. break;
  2197. case NWayLPAR:
  2198. DPRINTF("forbidden NWayLPAR write(w) val=0x%04x\n", val);
  2199. break;
  2200. case NWayExpansion:
  2201. DPRINTF("NWayExpansion write(w) val=0x%04x\n", val);
  2202. s->NWayExpansion = val;
  2203. break;
  2204. case CpCmd:
  2205. rtl8139_CpCmd_write(s, val);
  2206. break;
  2207. case IntrMitigate:
  2208. rtl8139_IntrMitigate_write(s, val);
  2209. break;
  2210. default:
  2211. DPRINTF("ioport write(w) addr=0x%x val=0x%04x via write(b)\n",
  2212. addr, val);
  2213. rtl8139_io_writeb(opaque, addr, val & 0xff);
  2214. rtl8139_io_writeb(opaque, addr + 1, (val >> 8) & 0xff);
  2215. break;
  2216. }
  2217. }
  2218. static void rtl8139_set_next_tctr_time(RTL8139State *s)
  2219. {
  2220. const uint64_t ns_per_period = (uint64_t)PCI_PERIOD << 32;
  2221. DPRINTF("entered rtl8139_set_next_tctr_time\n");
  2222. /* This function is called at least once per period, so it is a good
  2223. * place to update the timer base.
  2224. *
  2225. * After one iteration of this loop the value in the Timer register does
  2226. * not change, but the device model is counting up by 2^32 ticks (approx.
  2227. * 130 seconds).
  2228. */
  2229. while (s->TCTR_base + ns_per_period <= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL)) {
  2230. s->TCTR_base += ns_per_period;
  2231. }
  2232. if (!s->TimerInt) {
  2233. timer_del(s->timer);
  2234. } else {
  2235. uint64_t delta = (uint64_t)s->TimerInt * PCI_PERIOD;
  2236. if (s->TCTR_base + delta <= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL)) {
  2237. delta += ns_per_period;
  2238. }
  2239. timer_mod(s->timer, s->TCTR_base + delta);
  2240. }
  2241. }
  2242. static void rtl8139_io_writel(void *opaque, uint8_t addr, uint32_t val)
  2243. {
  2244. RTL8139State *s = opaque;
  2245. switch (addr)
  2246. {
  2247. case RxMissed:
  2248. DPRINTF("RxMissed clearing on write\n");
  2249. s->RxMissed = 0;
  2250. break;
  2251. case TxConfig:
  2252. rtl8139_TxConfig_write(s, val);
  2253. break;
  2254. case RxConfig:
  2255. rtl8139_RxConfig_write(s, val);
  2256. break;
  2257. case TxStatus0 ... TxStatus0+4*4-1:
  2258. rtl8139_TxStatus_write(s, addr-TxStatus0, val);
  2259. break;
  2260. case TxAddr0 ... TxAddr0+4*4-1:
  2261. rtl8139_TxAddr_write(s, addr-TxAddr0, val);
  2262. break;
  2263. case RxBuf:
  2264. rtl8139_RxBuf_write(s, val);
  2265. break;
  2266. case RxRingAddrLO:
  2267. DPRINTF("C+ RxRing low bits write val=0x%08x\n", val);
  2268. s->RxRingAddrLO = val;
  2269. break;
  2270. case RxRingAddrHI:
  2271. DPRINTF("C+ RxRing high bits write val=0x%08x\n", val);
  2272. s->RxRingAddrHI = val;
  2273. break;
  2274. case Timer:
  2275. DPRINTF("TCTR Timer reset on write\n");
  2276. s->TCTR_base = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
  2277. rtl8139_set_next_tctr_time(s);
  2278. break;
  2279. case FlashReg:
  2280. DPRINTF("FlashReg TimerInt write val=0x%08x\n", val);
  2281. if (s->TimerInt != val) {
  2282. s->TimerInt = val;
  2283. rtl8139_set_next_tctr_time(s);
  2284. }
  2285. break;
  2286. default:
  2287. DPRINTF("ioport write(l) addr=0x%x val=0x%08x via write(b)\n",
  2288. addr, val);
  2289. rtl8139_io_writeb(opaque, addr, val & 0xff);
  2290. rtl8139_io_writeb(opaque, addr + 1, (val >> 8) & 0xff);
  2291. rtl8139_io_writeb(opaque, addr + 2, (val >> 16) & 0xff);
  2292. rtl8139_io_writeb(opaque, addr + 3, (val >> 24) & 0xff);
  2293. break;
  2294. }
  2295. }
  2296. static uint32_t rtl8139_io_readb(void *opaque, uint8_t addr)
  2297. {
  2298. RTL8139State *s = opaque;
  2299. int ret;
  2300. switch (addr)
  2301. {
  2302. case MAC0 ... MAC0+5:
  2303. ret = s->phys[addr - MAC0];
  2304. break;
  2305. case MAC0+6 ... MAC0+7:
  2306. ret = 0;
  2307. break;
  2308. case MAR0 ... MAR0+7:
  2309. ret = s->mult[addr - MAR0];
  2310. break;
  2311. case TxStatus0 ... TxStatus0+4*4-1:
  2312. ret = rtl8139_TxStatus_TxAddr_read(s, s->TxStatus, TxStatus0,
  2313. addr, 1);
  2314. break;
  2315. case ChipCmd:
  2316. ret = rtl8139_ChipCmd_read(s);
  2317. break;
  2318. case Cfg9346:
  2319. ret = rtl8139_Cfg9346_read(s);
  2320. break;
  2321. case Config0:
  2322. ret = rtl8139_Config0_read(s);
  2323. break;
  2324. case Config1:
  2325. ret = rtl8139_Config1_read(s);
  2326. break;
  2327. case Config3:
  2328. ret = rtl8139_Config3_read(s);
  2329. break;
  2330. case Config4:
  2331. ret = rtl8139_Config4_read(s);
  2332. break;
  2333. case Config5:
  2334. ret = rtl8139_Config5_read(s);
  2335. break;
  2336. case MediaStatus:
  2337. /* The LinkDown bit of MediaStatus is inverse with link status */
  2338. ret = 0xd0 | (~s->BasicModeStatus & 0x04);
  2339. DPRINTF("MediaStatus read 0x%x\n", ret);
  2340. break;
  2341. case HltClk:
  2342. ret = s->clock_enabled;
  2343. DPRINTF("HltClk read 0x%x\n", ret);
  2344. break;
  2345. case PCIRevisionID:
  2346. ret = RTL8139_PCI_REVID;
  2347. DPRINTF("PCI Revision ID read 0x%x\n", ret);
  2348. break;
  2349. case TxThresh:
  2350. ret = s->TxThresh;
  2351. DPRINTF("C+ TxThresh read(b) val=0x%02x\n", ret);
  2352. break;
  2353. case 0x43: /* Part of TxConfig register. Windows driver tries to read it */
  2354. ret = s->TxConfig >> 24;
  2355. DPRINTF("RTL8139C TxConfig at 0x43 read(b) val=0x%02x\n", ret);
  2356. break;
  2357. default:
  2358. DPRINTF("not implemented read(b) addr=0x%x\n", addr);
  2359. ret = 0;
  2360. break;
  2361. }
  2362. return ret;
  2363. }
  2364. static uint32_t rtl8139_io_readw(void *opaque, uint8_t addr)
  2365. {
  2366. RTL8139State *s = opaque;
  2367. uint32_t ret;
  2368. switch (addr)
  2369. {
  2370. case TxAddr0 ... TxAddr0+4*4-1:
  2371. ret = rtl8139_TxStatus_TxAddr_read(s, s->TxAddr, TxAddr0, addr, 2);
  2372. break;
  2373. case IntrMask:
  2374. ret = rtl8139_IntrMask_read(s);
  2375. break;
  2376. case IntrStatus:
  2377. ret = rtl8139_IntrStatus_read(s);
  2378. break;
  2379. case MultiIntr:
  2380. ret = rtl8139_MultiIntr_read(s);
  2381. break;
  2382. case RxBufPtr:
  2383. ret = rtl8139_RxBufPtr_read(s);
  2384. break;
  2385. case RxBufAddr:
  2386. ret = rtl8139_RxBufAddr_read(s);
  2387. break;
  2388. case BasicModeCtrl:
  2389. ret = rtl8139_BasicModeCtrl_read(s);
  2390. break;
  2391. case BasicModeStatus:
  2392. ret = rtl8139_BasicModeStatus_read(s);
  2393. break;
  2394. case NWayAdvert:
  2395. ret = s->NWayAdvert;
  2396. DPRINTF("NWayAdvert read(w) val=0x%04x\n", ret);
  2397. break;
  2398. case NWayLPAR:
  2399. ret = s->NWayLPAR;
  2400. DPRINTF("NWayLPAR read(w) val=0x%04x\n", ret);
  2401. break;
  2402. case NWayExpansion:
  2403. ret = s->NWayExpansion;
  2404. DPRINTF("NWayExpansion read(w) val=0x%04x\n", ret);
  2405. break;
  2406. case CpCmd:
  2407. ret = rtl8139_CpCmd_read(s);
  2408. break;
  2409. case IntrMitigate:
  2410. ret = rtl8139_IntrMitigate_read(s);
  2411. break;
  2412. case TxSummary:
  2413. ret = rtl8139_TSAD_read(s);
  2414. break;
  2415. case CSCR:
  2416. ret = rtl8139_CSCR_read(s);
  2417. break;
  2418. default:
  2419. DPRINTF("ioport read(w) addr=0x%x via read(b)\n", addr);
  2420. ret = rtl8139_io_readb(opaque, addr);
  2421. ret |= rtl8139_io_readb(opaque, addr + 1) << 8;
  2422. DPRINTF("ioport read(w) addr=0x%x val=0x%04x\n", addr, ret);
  2423. break;
  2424. }
  2425. return ret;
  2426. }
  2427. static uint32_t rtl8139_io_readl(void *opaque, uint8_t addr)
  2428. {
  2429. RTL8139State *s = opaque;
  2430. uint32_t ret;
  2431. switch (addr)
  2432. {
  2433. case RxMissed:
  2434. ret = s->RxMissed;
  2435. DPRINTF("RxMissed read val=0x%08x\n", ret);
  2436. break;
  2437. case TxConfig:
  2438. ret = rtl8139_TxConfig_read(s);
  2439. break;
  2440. case RxConfig:
  2441. ret = rtl8139_RxConfig_read(s);
  2442. break;
  2443. case TxStatus0 ... TxStatus0+4*4-1:
  2444. ret = rtl8139_TxStatus_TxAddr_read(s, s->TxStatus, TxStatus0,
  2445. addr, 4);
  2446. break;
  2447. case TxAddr0 ... TxAddr0+4*4-1:
  2448. ret = rtl8139_TxAddr_read(s, addr-TxAddr0);
  2449. break;
  2450. case RxBuf:
  2451. ret = rtl8139_RxBuf_read(s);
  2452. break;
  2453. case RxRingAddrLO:
  2454. ret = s->RxRingAddrLO;
  2455. DPRINTF("C+ RxRing low bits read val=0x%08x\n", ret);
  2456. break;
  2457. case RxRingAddrHI:
  2458. ret = s->RxRingAddrHI;
  2459. DPRINTF("C+ RxRing high bits read val=0x%08x\n", ret);
  2460. break;
  2461. case Timer:
  2462. ret = (qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) - s->TCTR_base) /
  2463. PCI_PERIOD;
  2464. DPRINTF("TCTR Timer read val=0x%08x\n", ret);
  2465. break;
  2466. case FlashReg:
  2467. ret = s->TimerInt;
  2468. DPRINTF("FlashReg TimerInt read val=0x%08x\n", ret);
  2469. break;
  2470. default:
  2471. DPRINTF("ioport read(l) addr=0x%x via read(b)\n", addr);
  2472. ret = rtl8139_io_readb(opaque, addr);
  2473. ret |= rtl8139_io_readb(opaque, addr + 1) << 8;
  2474. ret |= rtl8139_io_readb(opaque, addr + 2) << 16;
  2475. ret |= rtl8139_io_readb(opaque, addr + 3) << 24;
  2476. DPRINTF("read(l) addr=0x%x val=%08x\n", addr, ret);
  2477. break;
  2478. }
  2479. return ret;
  2480. }
  2481. /* */
  2482. static int rtl8139_post_load(void *opaque, int version_id)
  2483. {
  2484. RTL8139State* s = opaque;
  2485. rtl8139_set_next_tctr_time(s);
  2486. if (version_id < 4) {
  2487. s->cplus_enabled = s->CpCmd != 0;
  2488. }
  2489. /* nc.link_down can't be migrated, so infer link_down according
  2490. * to link status bit in BasicModeStatus */
  2491. qemu_get_queue(s->nic)->link_down = (s->BasicModeStatus & 0x04) == 0;
  2492. return 0;
  2493. }
  2494. static bool rtl8139_hotplug_ready_needed(void *opaque)
  2495. {
  2496. return qdev_machine_modified();
  2497. }
  2498. static const VMStateDescription vmstate_rtl8139_hotplug_ready ={
  2499. .name = "rtl8139/hotplug_ready",
  2500. .version_id = 1,
  2501. .minimum_version_id = 1,
  2502. .needed = rtl8139_hotplug_ready_needed,
  2503. .fields = (VMStateField[]) {
  2504. VMSTATE_END_OF_LIST()
  2505. }
  2506. };
  2507. static int rtl8139_pre_save(void *opaque)
  2508. {
  2509. RTL8139State* s = opaque;
  2510. int64_t current_time = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
  2511. /* for migration to older versions */
  2512. s->TCTR = (current_time - s->TCTR_base) / PCI_PERIOD;
  2513. s->rtl8139_mmio_io_addr_dummy = 0;
  2514. return 0;
  2515. }
  2516. static const VMStateDescription vmstate_rtl8139 = {
  2517. .name = "rtl8139",
  2518. .version_id = 5,
  2519. .minimum_version_id = 3,
  2520. .post_load = rtl8139_post_load,
  2521. .pre_save = rtl8139_pre_save,
  2522. .fields = (VMStateField[]) {
  2523. VMSTATE_PCI_DEVICE(parent_obj, RTL8139State),
  2524. VMSTATE_PARTIAL_BUFFER(phys, RTL8139State, 6),
  2525. VMSTATE_BUFFER(mult, RTL8139State),
  2526. VMSTATE_UINT32_ARRAY(TxStatus, RTL8139State, 4),
  2527. VMSTATE_UINT32_ARRAY(TxAddr, RTL8139State, 4),
  2528. VMSTATE_UINT32(RxBuf, RTL8139State),
  2529. VMSTATE_UINT32(RxBufferSize, RTL8139State),
  2530. VMSTATE_UINT32(RxBufPtr, RTL8139State),
  2531. VMSTATE_UINT32(RxBufAddr, RTL8139State),
  2532. VMSTATE_UINT16(IntrStatus, RTL8139State),
  2533. VMSTATE_UINT16(IntrMask, RTL8139State),
  2534. VMSTATE_UINT32(TxConfig, RTL8139State),
  2535. VMSTATE_UINT32(RxConfig, RTL8139State),
  2536. VMSTATE_UINT32(RxMissed, RTL8139State),
  2537. VMSTATE_UINT16(CSCR, RTL8139State),
  2538. VMSTATE_UINT8(Cfg9346, RTL8139State),
  2539. VMSTATE_UINT8(Config0, RTL8139State),
  2540. VMSTATE_UINT8(Config1, RTL8139State),
  2541. VMSTATE_UINT8(Config3, RTL8139State),
  2542. VMSTATE_UINT8(Config4, RTL8139State),
  2543. VMSTATE_UINT8(Config5, RTL8139State),
  2544. VMSTATE_UINT8(clock_enabled, RTL8139State),
  2545. VMSTATE_UINT8(bChipCmdState, RTL8139State),
  2546. VMSTATE_UINT16(MultiIntr, RTL8139State),
  2547. VMSTATE_UINT16(BasicModeCtrl, RTL8139State),
  2548. VMSTATE_UINT16(BasicModeStatus, RTL8139State),
  2549. VMSTATE_UINT16(NWayAdvert, RTL8139State),
  2550. VMSTATE_UINT16(NWayLPAR, RTL8139State),
  2551. VMSTATE_UINT16(NWayExpansion, RTL8139State),
  2552. VMSTATE_UINT16(CpCmd, RTL8139State),
  2553. VMSTATE_UINT8(TxThresh, RTL8139State),
  2554. VMSTATE_UNUSED(4),
  2555. VMSTATE_MACADDR(conf.macaddr, RTL8139State),
  2556. VMSTATE_INT32(rtl8139_mmio_io_addr_dummy, RTL8139State),
  2557. VMSTATE_UINT32(currTxDesc, RTL8139State),
  2558. VMSTATE_UINT32(currCPlusRxDesc, RTL8139State),
  2559. VMSTATE_UINT32(currCPlusTxDesc, RTL8139State),
  2560. VMSTATE_UINT32(RxRingAddrLO, RTL8139State),
  2561. VMSTATE_UINT32(RxRingAddrHI, RTL8139State),
  2562. VMSTATE_UINT16_ARRAY(eeprom.contents, RTL8139State, EEPROM_9346_SIZE),
  2563. VMSTATE_INT32(eeprom.mode, RTL8139State),
  2564. VMSTATE_UINT32(eeprom.tick, RTL8139State),
  2565. VMSTATE_UINT8(eeprom.address, RTL8139State),
  2566. VMSTATE_UINT16(eeprom.input, RTL8139State),
  2567. VMSTATE_UINT16(eeprom.output, RTL8139State),
  2568. VMSTATE_UINT8(eeprom.eecs, RTL8139State),
  2569. VMSTATE_UINT8(eeprom.eesk, RTL8139State),
  2570. VMSTATE_UINT8(eeprom.eedi, RTL8139State),
  2571. VMSTATE_UINT8(eeprom.eedo, RTL8139State),
  2572. VMSTATE_UINT32(TCTR, RTL8139State),
  2573. VMSTATE_UINT32(TimerInt, RTL8139State),
  2574. VMSTATE_INT64(TCTR_base, RTL8139State),
  2575. VMSTATE_UINT64(tally_counters.TxOk, RTL8139State),
  2576. VMSTATE_UINT64(tally_counters.RxOk, RTL8139State),
  2577. VMSTATE_UINT64(tally_counters.TxERR, RTL8139State),
  2578. VMSTATE_UINT32(tally_counters.RxERR, RTL8139State),
  2579. VMSTATE_UINT16(tally_counters.MissPkt, RTL8139State),
  2580. VMSTATE_UINT16(tally_counters.FAE, RTL8139State),
  2581. VMSTATE_UINT32(tally_counters.Tx1Col, RTL8139State),
  2582. VMSTATE_UINT32(tally_counters.TxMCol, RTL8139State),
  2583. VMSTATE_UINT64(tally_counters.RxOkPhy, RTL8139State),
  2584. VMSTATE_UINT64(tally_counters.RxOkBrd, RTL8139State),
  2585. VMSTATE_UINT32_V(tally_counters.RxOkMul, RTL8139State, 5),
  2586. VMSTATE_UINT16(tally_counters.TxAbt, RTL8139State),
  2587. VMSTATE_UINT16(tally_counters.TxUndrn, RTL8139State),
  2588. VMSTATE_UINT32_V(cplus_enabled, RTL8139State, 4),
  2589. VMSTATE_END_OF_LIST()
  2590. },
  2591. .subsections = (const VMStateDescription*[]) {
  2592. &vmstate_rtl8139_hotplug_ready,
  2593. NULL
  2594. }
  2595. };
  2596. /***********************************************************/
  2597. /* PCI RTL8139 definitions */
  2598. static void rtl8139_ioport_write(void *opaque, hwaddr addr,
  2599. uint64_t val, unsigned size)
  2600. {
  2601. switch (size) {
  2602. case 1:
  2603. rtl8139_io_writeb(opaque, addr, val);
  2604. break;
  2605. case 2:
  2606. rtl8139_io_writew(opaque, addr, val);
  2607. break;
  2608. case 4:
  2609. rtl8139_io_writel(opaque, addr, val);
  2610. break;
  2611. }
  2612. }
  2613. static uint64_t rtl8139_ioport_read(void *opaque, hwaddr addr,
  2614. unsigned size)
  2615. {
  2616. switch (size) {
  2617. case 1:
  2618. return rtl8139_io_readb(opaque, addr);
  2619. case 2:
  2620. return rtl8139_io_readw(opaque, addr);
  2621. case 4:
  2622. return rtl8139_io_readl(opaque, addr);
  2623. }
  2624. return -1;
  2625. }
  2626. static const MemoryRegionOps rtl8139_io_ops = {
  2627. .read = rtl8139_ioport_read,
  2628. .write = rtl8139_ioport_write,
  2629. .impl = {
  2630. .min_access_size = 1,
  2631. .max_access_size = 4,
  2632. },
  2633. .endianness = DEVICE_LITTLE_ENDIAN,
  2634. };
  2635. static void rtl8139_timer(void *opaque)
  2636. {
  2637. RTL8139State *s = opaque;
  2638. if (!s->clock_enabled)
  2639. {
  2640. DPRINTF(">>> timer: clock is not running\n");
  2641. return;
  2642. }
  2643. s->IntrStatus |= PCSTimeout;
  2644. rtl8139_update_irq(s);
  2645. rtl8139_set_next_tctr_time(s);
  2646. }
  2647. static void pci_rtl8139_uninit(PCIDevice *dev)
  2648. {
  2649. RTL8139State *s = RTL8139(dev);
  2650. g_free(s->cplus_txbuffer);
  2651. s->cplus_txbuffer = NULL;
  2652. timer_free(s->timer);
  2653. qemu_del_nic(s->nic);
  2654. }
  2655. static void rtl8139_set_link_status(NetClientState *nc)
  2656. {
  2657. RTL8139State *s = qemu_get_nic_opaque(nc);
  2658. if (nc->link_down) {
  2659. s->BasicModeStatus &= ~0x04;
  2660. } else {
  2661. s->BasicModeStatus |= 0x04;
  2662. }
  2663. s->IntrStatus |= RxUnderrun;
  2664. rtl8139_update_irq(s);
  2665. }
  2666. static NetClientInfo net_rtl8139_info = {
  2667. .type = NET_CLIENT_DRIVER_NIC,
  2668. .size = sizeof(NICState),
  2669. .can_receive = rtl8139_can_receive,
  2670. .receive = rtl8139_receive,
  2671. .link_status_changed = rtl8139_set_link_status,
  2672. };
  2673. static void pci_rtl8139_realize(PCIDevice *dev, Error **errp)
  2674. {
  2675. RTL8139State *s = RTL8139(dev);
  2676. DeviceState *d = DEVICE(dev);
  2677. uint8_t *pci_conf;
  2678. pci_conf = dev->config;
  2679. pci_conf[PCI_INTERRUPT_PIN] = 1; /* interrupt pin A */
  2680. /* TODO: start of capability list, but no capability
  2681. * list bit in status register, and offset 0xdc seems unused. */
  2682. pci_conf[PCI_CAPABILITY_LIST] = 0xdc;
  2683. memory_region_init_io(&s->bar_io, OBJECT(s), &rtl8139_io_ops, s,
  2684. "rtl8139", 0x100);
  2685. memory_region_init_alias(&s->bar_mem, OBJECT(s), "rtl8139-mem", &s->bar_io,
  2686. 0, 0x100);
  2687. pci_register_bar(dev, 0, PCI_BASE_ADDRESS_SPACE_IO, &s->bar_io);
  2688. pci_register_bar(dev, 1, PCI_BASE_ADDRESS_SPACE_MEMORY, &s->bar_mem);
  2689. qemu_macaddr_default_if_unset(&s->conf.macaddr);
  2690. /* prepare eeprom */
  2691. s->eeprom.contents[0] = 0x8129;
  2692. #if 1
  2693. /* PCI vendor and device ID should be mirrored here */
  2694. s->eeprom.contents[1] = PCI_VENDOR_ID_REALTEK;
  2695. s->eeprom.contents[2] = PCI_DEVICE_ID_REALTEK_8139;
  2696. #endif
  2697. s->eeprom.contents[7] = s->conf.macaddr.a[0] | s->conf.macaddr.a[1] << 8;
  2698. s->eeprom.contents[8] = s->conf.macaddr.a[2] | s->conf.macaddr.a[3] << 8;
  2699. s->eeprom.contents[9] = s->conf.macaddr.a[4] | s->conf.macaddr.a[5] << 8;
  2700. s->nic = qemu_new_nic(&net_rtl8139_info, &s->conf,
  2701. object_get_typename(OBJECT(dev)), d->id, s);
  2702. qemu_format_nic_info_str(qemu_get_queue(s->nic), s->conf.macaddr.a);
  2703. s->cplus_txbuffer = NULL;
  2704. s->cplus_txbuffer_len = 0;
  2705. s->cplus_txbuffer_offset = 0;
  2706. s->timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, rtl8139_timer, s);
  2707. }
  2708. static void rtl8139_instance_init(Object *obj)
  2709. {
  2710. RTL8139State *s = RTL8139(obj);
  2711. device_add_bootindex_property(obj, &s->conf.bootindex,
  2712. "bootindex", "/ethernet-phy@0",
  2713. DEVICE(obj));
  2714. }
  2715. static Property rtl8139_properties[] = {
  2716. DEFINE_NIC_PROPERTIES(RTL8139State, conf),
  2717. DEFINE_PROP_END_OF_LIST(),
  2718. };
  2719. static void rtl8139_class_init(ObjectClass *klass, void *data)
  2720. {
  2721. DeviceClass *dc = DEVICE_CLASS(klass);
  2722. PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
  2723. k->realize = pci_rtl8139_realize;
  2724. k->exit = pci_rtl8139_uninit;
  2725. k->romfile = "efi-rtl8139.rom";
  2726. k->vendor_id = PCI_VENDOR_ID_REALTEK;
  2727. k->device_id = PCI_DEVICE_ID_REALTEK_8139;
  2728. k->revision = RTL8139_PCI_REVID; /* >=0x20 is for 8139C+ */
  2729. k->class_id = PCI_CLASS_NETWORK_ETHERNET;
  2730. dc->reset = rtl8139_reset;
  2731. dc->vmsd = &vmstate_rtl8139;
  2732. device_class_set_props(dc, rtl8139_properties);
  2733. set_bit(DEVICE_CATEGORY_NETWORK, dc->categories);
  2734. }
  2735. static const TypeInfo rtl8139_info = {
  2736. .name = TYPE_RTL8139,
  2737. .parent = TYPE_PCI_DEVICE,
  2738. .instance_size = sizeof(RTL8139State),
  2739. .class_init = rtl8139_class_init,
  2740. .instance_init = rtl8139_instance_init,
  2741. .interfaces = (InterfaceInfo[]) {
  2742. { INTERFACE_CONVENTIONAL_PCI_DEVICE },
  2743. { },
  2744. },
  2745. };
  2746. static void rtl8139_register_types(void)
  2747. {
  2748. type_register_static(&rtl8139_info);
  2749. }
  2750. type_init(rtl8139_register_types)