rtl8139.c 97 KB

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