rtl8139.c 100 KB

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