rtl8139.c 101 KB

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