2
0

rtl8139.c 97 KB

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