rtl8139.c 102 KB

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