2
0

e1000e_core.c 100 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510
  1. /*
  2. * Core code for QEMU e1000e emulation
  3. *
  4. * Software developer's manuals:
  5. * http://www.intel.com/content/dam/doc/datasheet/82574l-gbe-controller-datasheet.pdf
  6. *
  7. * Copyright (c) 2015 Ravello Systems LTD (http://ravellosystems.com)
  8. * Developed by Daynix Computing LTD (http://www.daynix.com)
  9. *
  10. * Authors:
  11. * Dmitry Fleytman <dmitry@daynix.com>
  12. * Leonid Bloch <leonid@daynix.com>
  13. * Yan Vugenfirer <yan@daynix.com>
  14. *
  15. * Based on work done by:
  16. * Nir Peleg, Tutis Systems Ltd. for Qumranet Inc.
  17. * Copyright (c) 2008 Qumranet
  18. * Based on work done by:
  19. * Copyright (c) 2007 Dan Aloni
  20. * Copyright (c) 2004 Antony T Curtis
  21. *
  22. * This library is free software; you can redistribute it and/or
  23. * modify it under the terms of the GNU Lesser General Public
  24. * License as published by the Free Software Foundation; either
  25. * version 2 of the License, or (at your option) any later version.
  26. *
  27. * This library is distributed in the hope that it will be useful,
  28. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  29. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  30. * Lesser General Public License for more details.
  31. *
  32. * You should have received a copy of the GNU Lesser General Public
  33. * License along with this library; if not, see <http://www.gnu.org/licenses/>.
  34. */
  35. #include "qemu/osdep.h"
  36. #include "net/net.h"
  37. #include "net/tap.h"
  38. #include "hw/hw.h"
  39. #include "hw/pci/msi.h"
  40. #include "hw/pci/msix.h"
  41. #include "sysemu/runstate.h"
  42. #include "net_tx_pkt.h"
  43. #include "net_rx_pkt.h"
  44. #include "e1000x_common.h"
  45. #include "e1000e_core.h"
  46. #include "trace.h"
  47. #define E1000E_MIN_XITR (500) /* No more then 7813 interrupts per
  48. second according to spec 10.2.4.2 */
  49. #define E1000E_MAX_TX_FRAGS (64)
  50. static inline void
  51. e1000e_set_interrupt_cause(E1000ECore *core, uint32_t val);
  52. static inline void
  53. e1000e_process_ts_option(E1000ECore *core, struct e1000_tx_desc *dp)
  54. {
  55. if (le32_to_cpu(dp->upper.data) & E1000_TXD_EXTCMD_TSTAMP) {
  56. trace_e1000e_wrn_no_ts_support();
  57. }
  58. }
  59. static inline void
  60. e1000e_process_snap_option(E1000ECore *core, uint32_t cmd_and_length)
  61. {
  62. if (cmd_and_length & E1000_TXD_CMD_SNAP) {
  63. trace_e1000e_wrn_no_snap_support();
  64. }
  65. }
  66. static inline void
  67. e1000e_raise_legacy_irq(E1000ECore *core)
  68. {
  69. trace_e1000e_irq_legacy_notify(true);
  70. e1000x_inc_reg_if_not_full(core->mac, IAC);
  71. pci_set_irq(core->owner, 1);
  72. }
  73. static inline void
  74. e1000e_lower_legacy_irq(E1000ECore *core)
  75. {
  76. trace_e1000e_irq_legacy_notify(false);
  77. pci_set_irq(core->owner, 0);
  78. }
  79. static inline void
  80. e1000e_intrmgr_rearm_timer(E1000IntrDelayTimer *timer)
  81. {
  82. int64_t delay_ns = (int64_t) timer->core->mac[timer->delay_reg] *
  83. timer->delay_resolution_ns;
  84. trace_e1000e_irq_rearm_timer(timer->delay_reg << 2, delay_ns);
  85. timer_mod(timer->timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + delay_ns);
  86. timer->running = true;
  87. }
  88. static void
  89. e1000e_intmgr_timer_resume(E1000IntrDelayTimer *timer)
  90. {
  91. if (timer->running) {
  92. e1000e_intrmgr_rearm_timer(timer);
  93. }
  94. }
  95. static void
  96. e1000e_intmgr_timer_pause(E1000IntrDelayTimer *timer)
  97. {
  98. if (timer->running) {
  99. timer_del(timer->timer);
  100. }
  101. }
  102. static inline void
  103. e1000e_intrmgr_stop_timer(E1000IntrDelayTimer *timer)
  104. {
  105. if (timer->running) {
  106. timer_del(timer->timer);
  107. timer->running = false;
  108. }
  109. }
  110. static inline void
  111. e1000e_intrmgr_fire_delayed_interrupts(E1000ECore *core)
  112. {
  113. trace_e1000e_irq_fire_delayed_interrupts();
  114. e1000e_set_interrupt_cause(core, 0);
  115. }
  116. static void
  117. e1000e_intrmgr_on_timer(void *opaque)
  118. {
  119. E1000IntrDelayTimer *timer = opaque;
  120. trace_e1000e_irq_throttling_timer(timer->delay_reg << 2);
  121. timer->running = false;
  122. e1000e_intrmgr_fire_delayed_interrupts(timer->core);
  123. }
  124. static void
  125. e1000e_intrmgr_on_throttling_timer(void *opaque)
  126. {
  127. E1000IntrDelayTimer *timer = opaque;
  128. assert(!msix_enabled(timer->core->owner));
  129. timer->running = false;
  130. if (!timer->core->itr_intr_pending) {
  131. trace_e1000e_irq_throttling_no_pending_interrupts();
  132. return;
  133. }
  134. if (msi_enabled(timer->core->owner)) {
  135. trace_e1000e_irq_msi_notify_postponed();
  136. e1000e_set_interrupt_cause(timer->core, 0);
  137. } else {
  138. trace_e1000e_irq_legacy_notify_postponed();
  139. e1000e_set_interrupt_cause(timer->core, 0);
  140. }
  141. }
  142. static void
  143. e1000e_intrmgr_on_msix_throttling_timer(void *opaque)
  144. {
  145. E1000IntrDelayTimer *timer = opaque;
  146. int idx = timer - &timer->core->eitr[0];
  147. assert(msix_enabled(timer->core->owner));
  148. timer->running = false;
  149. if (!timer->core->eitr_intr_pending[idx]) {
  150. trace_e1000e_irq_throttling_no_pending_vec(idx);
  151. return;
  152. }
  153. trace_e1000e_irq_msix_notify_postponed_vec(idx);
  154. msix_notify(timer->core->owner, idx);
  155. }
  156. static void
  157. e1000e_intrmgr_initialize_all_timers(E1000ECore *core, bool create)
  158. {
  159. int i;
  160. core->radv.delay_reg = RADV;
  161. core->rdtr.delay_reg = RDTR;
  162. core->raid.delay_reg = RAID;
  163. core->tadv.delay_reg = TADV;
  164. core->tidv.delay_reg = TIDV;
  165. core->radv.delay_resolution_ns = E1000_INTR_DELAY_NS_RES;
  166. core->rdtr.delay_resolution_ns = E1000_INTR_DELAY_NS_RES;
  167. core->raid.delay_resolution_ns = E1000_INTR_DELAY_NS_RES;
  168. core->tadv.delay_resolution_ns = E1000_INTR_DELAY_NS_RES;
  169. core->tidv.delay_resolution_ns = E1000_INTR_DELAY_NS_RES;
  170. core->radv.core = core;
  171. core->rdtr.core = core;
  172. core->raid.core = core;
  173. core->tadv.core = core;
  174. core->tidv.core = core;
  175. core->itr.core = core;
  176. core->itr.delay_reg = ITR;
  177. core->itr.delay_resolution_ns = E1000_INTR_THROTTLING_NS_RES;
  178. for (i = 0; i < E1000E_MSIX_VEC_NUM; i++) {
  179. core->eitr[i].core = core;
  180. core->eitr[i].delay_reg = EITR + i;
  181. core->eitr[i].delay_resolution_ns = E1000_INTR_THROTTLING_NS_RES;
  182. }
  183. if (!create) {
  184. return;
  185. }
  186. core->radv.timer =
  187. timer_new_ns(QEMU_CLOCK_VIRTUAL, e1000e_intrmgr_on_timer, &core->radv);
  188. core->rdtr.timer =
  189. timer_new_ns(QEMU_CLOCK_VIRTUAL, e1000e_intrmgr_on_timer, &core->rdtr);
  190. core->raid.timer =
  191. timer_new_ns(QEMU_CLOCK_VIRTUAL, e1000e_intrmgr_on_timer, &core->raid);
  192. core->tadv.timer =
  193. timer_new_ns(QEMU_CLOCK_VIRTUAL, e1000e_intrmgr_on_timer, &core->tadv);
  194. core->tidv.timer =
  195. timer_new_ns(QEMU_CLOCK_VIRTUAL, e1000e_intrmgr_on_timer, &core->tidv);
  196. core->itr.timer = timer_new_ns(QEMU_CLOCK_VIRTUAL,
  197. e1000e_intrmgr_on_throttling_timer,
  198. &core->itr);
  199. for (i = 0; i < E1000E_MSIX_VEC_NUM; i++) {
  200. core->eitr[i].timer =
  201. timer_new_ns(QEMU_CLOCK_VIRTUAL,
  202. e1000e_intrmgr_on_msix_throttling_timer,
  203. &core->eitr[i]);
  204. }
  205. }
  206. static inline void
  207. e1000e_intrmgr_stop_delay_timers(E1000ECore *core)
  208. {
  209. e1000e_intrmgr_stop_timer(&core->radv);
  210. e1000e_intrmgr_stop_timer(&core->rdtr);
  211. e1000e_intrmgr_stop_timer(&core->raid);
  212. e1000e_intrmgr_stop_timer(&core->tidv);
  213. e1000e_intrmgr_stop_timer(&core->tadv);
  214. }
  215. static bool
  216. e1000e_intrmgr_delay_rx_causes(E1000ECore *core, uint32_t *causes)
  217. {
  218. uint32_t delayable_causes;
  219. uint32_t rdtr = core->mac[RDTR];
  220. uint32_t radv = core->mac[RADV];
  221. uint32_t raid = core->mac[RAID];
  222. if (msix_enabled(core->owner)) {
  223. return false;
  224. }
  225. delayable_causes = E1000_ICR_RXQ0 |
  226. E1000_ICR_RXQ1 |
  227. E1000_ICR_RXT0;
  228. if (!(core->mac[RFCTL] & E1000_RFCTL_ACK_DIS)) {
  229. delayable_causes |= E1000_ICR_ACK;
  230. }
  231. /* Clean up all causes that may be delayed */
  232. core->delayed_causes |= *causes & delayable_causes;
  233. *causes &= ~delayable_causes;
  234. /* Check if delayed RX interrupts disabled by client
  235. or if there are causes that cannot be delayed */
  236. if ((rdtr == 0) || (*causes != 0)) {
  237. return false;
  238. }
  239. /* Check if delayed RX ACK interrupts disabled by client
  240. and there is an ACK packet received */
  241. if ((raid == 0) && (core->delayed_causes & E1000_ICR_ACK)) {
  242. return false;
  243. }
  244. /* All causes delayed */
  245. e1000e_intrmgr_rearm_timer(&core->rdtr);
  246. if (!core->radv.running && (radv != 0)) {
  247. e1000e_intrmgr_rearm_timer(&core->radv);
  248. }
  249. if (!core->raid.running && (core->delayed_causes & E1000_ICR_ACK)) {
  250. e1000e_intrmgr_rearm_timer(&core->raid);
  251. }
  252. return true;
  253. }
  254. static bool
  255. e1000e_intrmgr_delay_tx_causes(E1000ECore *core, uint32_t *causes)
  256. {
  257. static const uint32_t delayable_causes = E1000_ICR_TXQ0 |
  258. E1000_ICR_TXQ1 |
  259. E1000_ICR_TXQE |
  260. E1000_ICR_TXDW;
  261. if (msix_enabled(core->owner)) {
  262. return false;
  263. }
  264. /* Clean up all causes that may be delayed */
  265. core->delayed_causes |= *causes & delayable_causes;
  266. *causes &= ~delayable_causes;
  267. /* If there are causes that cannot be delayed */
  268. if (*causes != 0) {
  269. return false;
  270. }
  271. /* All causes delayed */
  272. e1000e_intrmgr_rearm_timer(&core->tidv);
  273. if (!core->tadv.running && (core->mac[TADV] != 0)) {
  274. e1000e_intrmgr_rearm_timer(&core->tadv);
  275. }
  276. return true;
  277. }
  278. static uint32_t
  279. e1000e_intmgr_collect_delayed_causes(E1000ECore *core)
  280. {
  281. uint32_t res;
  282. if (msix_enabled(core->owner)) {
  283. assert(core->delayed_causes == 0);
  284. return 0;
  285. }
  286. res = core->delayed_causes;
  287. core->delayed_causes = 0;
  288. e1000e_intrmgr_stop_delay_timers(core);
  289. return res;
  290. }
  291. static void
  292. e1000e_intrmgr_fire_all_timers(E1000ECore *core)
  293. {
  294. int i;
  295. uint32_t val = e1000e_intmgr_collect_delayed_causes(core);
  296. trace_e1000e_irq_adding_delayed_causes(val, core->mac[ICR]);
  297. core->mac[ICR] |= val;
  298. if (core->itr.running) {
  299. timer_del(core->itr.timer);
  300. e1000e_intrmgr_on_throttling_timer(&core->itr);
  301. }
  302. for (i = 0; i < E1000E_MSIX_VEC_NUM; i++) {
  303. if (core->eitr[i].running) {
  304. timer_del(core->eitr[i].timer);
  305. e1000e_intrmgr_on_msix_throttling_timer(&core->eitr[i]);
  306. }
  307. }
  308. }
  309. static void
  310. e1000e_intrmgr_resume(E1000ECore *core)
  311. {
  312. int i;
  313. e1000e_intmgr_timer_resume(&core->radv);
  314. e1000e_intmgr_timer_resume(&core->rdtr);
  315. e1000e_intmgr_timer_resume(&core->raid);
  316. e1000e_intmgr_timer_resume(&core->tidv);
  317. e1000e_intmgr_timer_resume(&core->tadv);
  318. e1000e_intmgr_timer_resume(&core->itr);
  319. for (i = 0; i < E1000E_MSIX_VEC_NUM; i++) {
  320. e1000e_intmgr_timer_resume(&core->eitr[i]);
  321. }
  322. }
  323. static void
  324. e1000e_intrmgr_pause(E1000ECore *core)
  325. {
  326. int i;
  327. e1000e_intmgr_timer_pause(&core->radv);
  328. e1000e_intmgr_timer_pause(&core->rdtr);
  329. e1000e_intmgr_timer_pause(&core->raid);
  330. e1000e_intmgr_timer_pause(&core->tidv);
  331. e1000e_intmgr_timer_pause(&core->tadv);
  332. e1000e_intmgr_timer_pause(&core->itr);
  333. for (i = 0; i < E1000E_MSIX_VEC_NUM; i++) {
  334. e1000e_intmgr_timer_pause(&core->eitr[i]);
  335. }
  336. }
  337. static void
  338. e1000e_intrmgr_reset(E1000ECore *core)
  339. {
  340. int i;
  341. core->delayed_causes = 0;
  342. e1000e_intrmgr_stop_delay_timers(core);
  343. e1000e_intrmgr_stop_timer(&core->itr);
  344. for (i = 0; i < E1000E_MSIX_VEC_NUM; i++) {
  345. e1000e_intrmgr_stop_timer(&core->eitr[i]);
  346. }
  347. }
  348. static void
  349. e1000e_intrmgr_pci_unint(E1000ECore *core)
  350. {
  351. int i;
  352. timer_del(core->radv.timer);
  353. timer_free(core->radv.timer);
  354. timer_del(core->rdtr.timer);
  355. timer_free(core->rdtr.timer);
  356. timer_del(core->raid.timer);
  357. timer_free(core->raid.timer);
  358. timer_del(core->tadv.timer);
  359. timer_free(core->tadv.timer);
  360. timer_del(core->tidv.timer);
  361. timer_free(core->tidv.timer);
  362. timer_del(core->itr.timer);
  363. timer_free(core->itr.timer);
  364. for (i = 0; i < E1000E_MSIX_VEC_NUM; i++) {
  365. timer_del(core->eitr[i].timer);
  366. timer_free(core->eitr[i].timer);
  367. }
  368. }
  369. static void
  370. e1000e_intrmgr_pci_realize(E1000ECore *core)
  371. {
  372. e1000e_intrmgr_initialize_all_timers(core, true);
  373. }
  374. static inline bool
  375. e1000e_rx_csum_enabled(E1000ECore *core)
  376. {
  377. return (core->mac[RXCSUM] & E1000_RXCSUM_PCSD) ? false : true;
  378. }
  379. static inline bool
  380. e1000e_rx_use_legacy_descriptor(E1000ECore *core)
  381. {
  382. return (core->mac[RFCTL] & E1000_RFCTL_EXTEN) ? false : true;
  383. }
  384. static inline bool
  385. e1000e_rx_use_ps_descriptor(E1000ECore *core)
  386. {
  387. return !e1000e_rx_use_legacy_descriptor(core) &&
  388. (core->mac[RCTL] & E1000_RCTL_DTYP_PS);
  389. }
  390. static inline bool
  391. e1000e_rss_enabled(E1000ECore *core)
  392. {
  393. return E1000_MRQC_ENABLED(core->mac[MRQC]) &&
  394. !e1000e_rx_csum_enabled(core) &&
  395. !e1000e_rx_use_legacy_descriptor(core);
  396. }
  397. typedef struct E1000E_RSSInfo_st {
  398. bool enabled;
  399. uint32_t hash;
  400. uint32_t queue;
  401. uint32_t type;
  402. } E1000E_RSSInfo;
  403. static uint32_t
  404. e1000e_rss_get_hash_type(E1000ECore *core, struct NetRxPkt *pkt)
  405. {
  406. bool isip4, isip6, isudp, istcp;
  407. assert(e1000e_rss_enabled(core));
  408. net_rx_pkt_get_protocols(pkt, &isip4, &isip6, &isudp, &istcp);
  409. if (isip4) {
  410. bool fragment = net_rx_pkt_get_ip4_info(pkt)->fragment;
  411. trace_e1000e_rx_rss_ip4(fragment, istcp, core->mac[MRQC],
  412. E1000_MRQC_EN_TCPIPV4(core->mac[MRQC]),
  413. E1000_MRQC_EN_IPV4(core->mac[MRQC]));
  414. if (!fragment && istcp && E1000_MRQC_EN_TCPIPV4(core->mac[MRQC])) {
  415. return E1000_MRQ_RSS_TYPE_IPV4TCP;
  416. }
  417. if (E1000_MRQC_EN_IPV4(core->mac[MRQC])) {
  418. return E1000_MRQ_RSS_TYPE_IPV4;
  419. }
  420. } else if (isip6) {
  421. eth_ip6_hdr_info *ip6info = net_rx_pkt_get_ip6_info(pkt);
  422. bool ex_dis = core->mac[RFCTL] & E1000_RFCTL_IPV6_EX_DIS;
  423. bool new_ex_dis = core->mac[RFCTL] & E1000_RFCTL_NEW_IPV6_EXT_DIS;
  424. /*
  425. * Following two traces must not be combined because resulting
  426. * event will have 11 arguments totally and some trace backends
  427. * (at least "ust") have limitation of maximum 10 arguments per
  428. * event. Events with more arguments fail to compile for
  429. * backends like these.
  430. */
  431. trace_e1000e_rx_rss_ip6_rfctl(core->mac[RFCTL]);
  432. trace_e1000e_rx_rss_ip6(ex_dis, new_ex_dis, istcp,
  433. ip6info->has_ext_hdrs,
  434. ip6info->rss_ex_dst_valid,
  435. ip6info->rss_ex_src_valid,
  436. core->mac[MRQC],
  437. E1000_MRQC_EN_TCPIPV6(core->mac[MRQC]),
  438. E1000_MRQC_EN_IPV6EX(core->mac[MRQC]),
  439. E1000_MRQC_EN_IPV6(core->mac[MRQC]));
  440. if ((!ex_dis || !ip6info->has_ext_hdrs) &&
  441. (!new_ex_dis || !(ip6info->rss_ex_dst_valid ||
  442. ip6info->rss_ex_src_valid))) {
  443. if (istcp && !ip6info->fragment &&
  444. E1000_MRQC_EN_TCPIPV6(core->mac[MRQC])) {
  445. return E1000_MRQ_RSS_TYPE_IPV6TCP;
  446. }
  447. if (E1000_MRQC_EN_IPV6EX(core->mac[MRQC])) {
  448. return E1000_MRQ_RSS_TYPE_IPV6EX;
  449. }
  450. }
  451. if (E1000_MRQC_EN_IPV6(core->mac[MRQC])) {
  452. return E1000_MRQ_RSS_TYPE_IPV6;
  453. }
  454. }
  455. return E1000_MRQ_RSS_TYPE_NONE;
  456. }
  457. static uint32_t
  458. e1000e_rss_calc_hash(E1000ECore *core,
  459. struct NetRxPkt *pkt,
  460. E1000E_RSSInfo *info)
  461. {
  462. NetRxPktRssType type;
  463. assert(e1000e_rss_enabled(core));
  464. switch (info->type) {
  465. case E1000_MRQ_RSS_TYPE_IPV4:
  466. type = NetPktRssIpV4;
  467. break;
  468. case E1000_MRQ_RSS_TYPE_IPV4TCP:
  469. type = NetPktRssIpV4Tcp;
  470. break;
  471. case E1000_MRQ_RSS_TYPE_IPV6TCP:
  472. type = NetPktRssIpV6Tcp;
  473. break;
  474. case E1000_MRQ_RSS_TYPE_IPV6:
  475. type = NetPktRssIpV6;
  476. break;
  477. case E1000_MRQ_RSS_TYPE_IPV6EX:
  478. type = NetPktRssIpV6Ex;
  479. break;
  480. default:
  481. assert(false);
  482. return 0;
  483. }
  484. return net_rx_pkt_calc_rss_hash(pkt, type, (uint8_t *) &core->mac[RSSRK]);
  485. }
  486. static void
  487. e1000e_rss_parse_packet(E1000ECore *core,
  488. struct NetRxPkt *pkt,
  489. E1000E_RSSInfo *info)
  490. {
  491. trace_e1000e_rx_rss_started();
  492. if (!e1000e_rss_enabled(core)) {
  493. info->enabled = false;
  494. info->hash = 0;
  495. info->queue = 0;
  496. info->type = 0;
  497. trace_e1000e_rx_rss_disabled();
  498. return;
  499. }
  500. info->enabled = true;
  501. info->type = e1000e_rss_get_hash_type(core, pkt);
  502. trace_e1000e_rx_rss_type(info->type);
  503. if (info->type == E1000_MRQ_RSS_TYPE_NONE) {
  504. info->hash = 0;
  505. info->queue = 0;
  506. return;
  507. }
  508. info->hash = e1000e_rss_calc_hash(core, pkt, info);
  509. info->queue = E1000_RSS_QUEUE(&core->mac[RETA], info->hash);
  510. }
  511. static void
  512. e1000e_setup_tx_offloads(E1000ECore *core, struct e1000e_tx *tx)
  513. {
  514. if (tx->props.tse && tx->cptse) {
  515. net_tx_pkt_build_vheader(tx->tx_pkt, true, true, tx->props.mss);
  516. net_tx_pkt_update_ip_checksums(tx->tx_pkt);
  517. e1000x_inc_reg_if_not_full(core->mac, TSCTC);
  518. return;
  519. }
  520. if (tx->sum_needed & E1000_TXD_POPTS_TXSM) {
  521. net_tx_pkt_build_vheader(tx->tx_pkt, false, true, 0);
  522. }
  523. if (tx->sum_needed & E1000_TXD_POPTS_IXSM) {
  524. net_tx_pkt_update_ip_hdr_checksum(tx->tx_pkt);
  525. }
  526. }
  527. static bool
  528. e1000e_tx_pkt_send(E1000ECore *core, struct e1000e_tx *tx, int queue_index)
  529. {
  530. int target_queue = MIN(core->max_queue_num, queue_index);
  531. NetClientState *queue = qemu_get_subqueue(core->owner_nic, target_queue);
  532. e1000e_setup_tx_offloads(core, tx);
  533. net_tx_pkt_dump(tx->tx_pkt);
  534. if ((core->phy[0][PHY_CTRL] & MII_CR_LOOPBACK) ||
  535. ((core->mac[RCTL] & E1000_RCTL_LBM_MAC) == E1000_RCTL_LBM_MAC)) {
  536. return net_tx_pkt_send_loopback(tx->tx_pkt, queue);
  537. } else {
  538. return net_tx_pkt_send(tx->tx_pkt, queue);
  539. }
  540. }
  541. static void
  542. e1000e_on_tx_done_update_stats(E1000ECore *core, struct NetTxPkt *tx_pkt)
  543. {
  544. static const int PTCregs[6] = { PTC64, PTC127, PTC255, PTC511,
  545. PTC1023, PTC1522 };
  546. size_t tot_len = net_tx_pkt_get_total_len(tx_pkt);
  547. e1000x_increase_size_stats(core->mac, PTCregs, tot_len);
  548. e1000x_inc_reg_if_not_full(core->mac, TPT);
  549. e1000x_grow_8reg_if_not_full(core->mac, TOTL, tot_len);
  550. switch (net_tx_pkt_get_packet_type(tx_pkt)) {
  551. case ETH_PKT_BCAST:
  552. e1000x_inc_reg_if_not_full(core->mac, BPTC);
  553. break;
  554. case ETH_PKT_MCAST:
  555. e1000x_inc_reg_if_not_full(core->mac, MPTC);
  556. break;
  557. case ETH_PKT_UCAST:
  558. break;
  559. default:
  560. g_assert_not_reached();
  561. }
  562. core->mac[GPTC] = core->mac[TPT];
  563. core->mac[GOTCL] = core->mac[TOTL];
  564. core->mac[GOTCH] = core->mac[TOTH];
  565. }
  566. static void
  567. e1000e_process_tx_desc(E1000ECore *core,
  568. struct e1000e_tx *tx,
  569. struct e1000_tx_desc *dp,
  570. int queue_index)
  571. {
  572. uint32_t txd_lower = le32_to_cpu(dp->lower.data);
  573. uint32_t dtype = txd_lower & (E1000_TXD_CMD_DEXT | E1000_TXD_DTYP_D);
  574. unsigned int split_size = txd_lower & 0xffff;
  575. uint64_t addr;
  576. struct e1000_context_desc *xp = (struct e1000_context_desc *)dp;
  577. bool eop = txd_lower & E1000_TXD_CMD_EOP;
  578. if (dtype == E1000_TXD_CMD_DEXT) { /* context descriptor */
  579. e1000x_read_tx_ctx_descr(xp, &tx->props);
  580. e1000e_process_snap_option(core, le32_to_cpu(xp->cmd_and_length));
  581. return;
  582. } else if (dtype == (E1000_TXD_CMD_DEXT | E1000_TXD_DTYP_D)) {
  583. /* data descriptor */
  584. tx->sum_needed = le32_to_cpu(dp->upper.data) >> 8;
  585. tx->cptse = (txd_lower & E1000_TXD_CMD_TSE) ? 1 : 0;
  586. e1000e_process_ts_option(core, dp);
  587. } else {
  588. /* legacy descriptor */
  589. e1000e_process_ts_option(core, dp);
  590. tx->cptse = 0;
  591. }
  592. addr = le64_to_cpu(dp->buffer_addr);
  593. if (!tx->skip_cp) {
  594. if (!net_tx_pkt_add_raw_fragment(tx->tx_pkt, addr, split_size)) {
  595. tx->skip_cp = true;
  596. }
  597. }
  598. if (eop) {
  599. if (!tx->skip_cp && net_tx_pkt_parse(tx->tx_pkt)) {
  600. if (e1000x_vlan_enabled(core->mac) &&
  601. e1000x_is_vlan_txd(txd_lower)) {
  602. net_tx_pkt_setup_vlan_header_ex(tx->tx_pkt,
  603. le16_to_cpu(dp->upper.fields.special), core->vet);
  604. }
  605. if (e1000e_tx_pkt_send(core, tx, queue_index)) {
  606. e1000e_on_tx_done_update_stats(core, tx->tx_pkt);
  607. }
  608. }
  609. tx->skip_cp = false;
  610. net_tx_pkt_reset(tx->tx_pkt);
  611. tx->sum_needed = 0;
  612. tx->cptse = 0;
  613. }
  614. }
  615. static inline uint32_t
  616. e1000e_tx_wb_interrupt_cause(E1000ECore *core, int queue_idx)
  617. {
  618. if (!msix_enabled(core->owner)) {
  619. return E1000_ICR_TXDW;
  620. }
  621. return (queue_idx == 0) ? E1000_ICR_TXQ0 : E1000_ICR_TXQ1;
  622. }
  623. static inline uint32_t
  624. e1000e_rx_wb_interrupt_cause(E1000ECore *core, int queue_idx,
  625. bool min_threshold_hit)
  626. {
  627. if (!msix_enabled(core->owner)) {
  628. return E1000_ICS_RXT0 | (min_threshold_hit ? E1000_ICS_RXDMT0 : 0);
  629. }
  630. return (queue_idx == 0) ? E1000_ICR_RXQ0 : E1000_ICR_RXQ1;
  631. }
  632. static uint32_t
  633. e1000e_txdesc_writeback(E1000ECore *core, dma_addr_t base,
  634. struct e1000_tx_desc *dp, bool *ide, int queue_idx)
  635. {
  636. uint32_t txd_upper, txd_lower = le32_to_cpu(dp->lower.data);
  637. if (!(txd_lower & E1000_TXD_CMD_RS) &&
  638. !(core->mac[IVAR] & E1000_IVAR_TX_INT_EVERY_WB)) {
  639. return 0;
  640. }
  641. *ide = (txd_lower & E1000_TXD_CMD_IDE) ? true : false;
  642. txd_upper = le32_to_cpu(dp->upper.data) | E1000_TXD_STAT_DD;
  643. dp->upper.data = cpu_to_le32(txd_upper);
  644. pci_dma_write(core->owner, base + ((char *)&dp->upper - (char *)dp),
  645. &dp->upper, sizeof(dp->upper));
  646. return e1000e_tx_wb_interrupt_cause(core, queue_idx);
  647. }
  648. typedef struct E1000E_RingInfo_st {
  649. int dbah;
  650. int dbal;
  651. int dlen;
  652. int dh;
  653. int dt;
  654. int idx;
  655. } E1000E_RingInfo;
  656. static inline bool
  657. e1000e_ring_empty(E1000ECore *core, const E1000E_RingInfo *r)
  658. {
  659. return core->mac[r->dh] == core->mac[r->dt] ||
  660. core->mac[r->dt] >= core->mac[r->dlen] / E1000_RING_DESC_LEN;
  661. }
  662. static inline uint64_t
  663. e1000e_ring_base(E1000ECore *core, const E1000E_RingInfo *r)
  664. {
  665. uint64_t bah = core->mac[r->dbah];
  666. uint64_t bal = core->mac[r->dbal];
  667. return (bah << 32) + bal;
  668. }
  669. static inline uint64_t
  670. e1000e_ring_head_descr(E1000ECore *core, const E1000E_RingInfo *r)
  671. {
  672. return e1000e_ring_base(core, r) + E1000_RING_DESC_LEN * core->mac[r->dh];
  673. }
  674. static inline void
  675. e1000e_ring_advance(E1000ECore *core, const E1000E_RingInfo *r, uint32_t count)
  676. {
  677. core->mac[r->dh] += count;
  678. if (core->mac[r->dh] * E1000_RING_DESC_LEN >= core->mac[r->dlen]) {
  679. core->mac[r->dh] = 0;
  680. }
  681. }
  682. static inline uint32_t
  683. e1000e_ring_free_descr_num(E1000ECore *core, const E1000E_RingInfo *r)
  684. {
  685. trace_e1000e_ring_free_space(r->idx, core->mac[r->dlen],
  686. core->mac[r->dh], core->mac[r->dt]);
  687. if (core->mac[r->dh] <= core->mac[r->dt]) {
  688. return core->mac[r->dt] - core->mac[r->dh];
  689. }
  690. if (core->mac[r->dh] > core->mac[r->dt]) {
  691. return core->mac[r->dlen] / E1000_RING_DESC_LEN +
  692. core->mac[r->dt] - core->mac[r->dh];
  693. }
  694. g_assert_not_reached();
  695. return 0;
  696. }
  697. static inline bool
  698. e1000e_ring_enabled(E1000ECore *core, const E1000E_RingInfo *r)
  699. {
  700. return core->mac[r->dlen] > 0;
  701. }
  702. static inline uint32_t
  703. e1000e_ring_len(E1000ECore *core, const E1000E_RingInfo *r)
  704. {
  705. return core->mac[r->dlen];
  706. }
  707. typedef struct E1000E_TxRing_st {
  708. const E1000E_RingInfo *i;
  709. struct e1000e_tx *tx;
  710. } E1000E_TxRing;
  711. static inline int
  712. e1000e_mq_queue_idx(int base_reg_idx, int reg_idx)
  713. {
  714. return (reg_idx - base_reg_idx) / (0x100 >> 2);
  715. }
  716. static inline void
  717. e1000e_tx_ring_init(E1000ECore *core, E1000E_TxRing *txr, int idx)
  718. {
  719. static const E1000E_RingInfo i[E1000E_NUM_QUEUES] = {
  720. { TDBAH, TDBAL, TDLEN, TDH, TDT, 0 },
  721. { TDBAH1, TDBAL1, TDLEN1, TDH1, TDT1, 1 }
  722. };
  723. assert(idx < ARRAY_SIZE(i));
  724. txr->i = &i[idx];
  725. txr->tx = &core->tx[idx];
  726. }
  727. typedef struct E1000E_RxRing_st {
  728. const E1000E_RingInfo *i;
  729. } E1000E_RxRing;
  730. static inline void
  731. e1000e_rx_ring_init(E1000ECore *core, E1000E_RxRing *rxr, int idx)
  732. {
  733. static const E1000E_RingInfo i[E1000E_NUM_QUEUES] = {
  734. { RDBAH0, RDBAL0, RDLEN0, RDH0, RDT0, 0 },
  735. { RDBAH1, RDBAL1, RDLEN1, RDH1, RDT1, 1 }
  736. };
  737. assert(idx < ARRAY_SIZE(i));
  738. rxr->i = &i[idx];
  739. }
  740. static void
  741. e1000e_start_xmit(E1000ECore *core, const E1000E_TxRing *txr)
  742. {
  743. dma_addr_t base;
  744. struct e1000_tx_desc desc;
  745. bool ide = false;
  746. const E1000E_RingInfo *txi = txr->i;
  747. uint32_t cause = E1000_ICS_TXQE;
  748. if (!(core->mac[TCTL] & E1000_TCTL_EN)) {
  749. trace_e1000e_tx_disabled();
  750. return;
  751. }
  752. while (!e1000e_ring_empty(core, txi)) {
  753. base = e1000e_ring_head_descr(core, txi);
  754. pci_dma_read(core->owner, base, &desc, sizeof(desc));
  755. trace_e1000e_tx_descr((void *)(intptr_t)desc.buffer_addr,
  756. desc.lower.data, desc.upper.data);
  757. e1000e_process_tx_desc(core, txr->tx, &desc, txi->idx);
  758. cause |= e1000e_txdesc_writeback(core, base, &desc, &ide, txi->idx);
  759. e1000e_ring_advance(core, txi, 1);
  760. }
  761. if (!ide || !e1000e_intrmgr_delay_tx_causes(core, &cause)) {
  762. e1000e_set_interrupt_cause(core, cause);
  763. }
  764. }
  765. static bool
  766. e1000e_has_rxbufs(E1000ECore *core, const E1000E_RingInfo *r,
  767. size_t total_size)
  768. {
  769. uint32_t bufs = e1000e_ring_free_descr_num(core, r);
  770. trace_e1000e_rx_has_buffers(r->idx, bufs, total_size,
  771. core->rx_desc_buf_size);
  772. return total_size <= bufs / (core->rx_desc_len / E1000_MIN_RX_DESC_LEN) *
  773. core->rx_desc_buf_size;
  774. }
  775. void
  776. e1000e_start_recv(E1000ECore *core)
  777. {
  778. int i;
  779. trace_e1000e_rx_start_recv();
  780. for (i = 0; i <= core->max_queue_num; i++) {
  781. qemu_flush_queued_packets(qemu_get_subqueue(core->owner_nic, i));
  782. }
  783. }
  784. int
  785. e1000e_can_receive(E1000ECore *core)
  786. {
  787. int i;
  788. if (!e1000x_rx_ready(core->owner, core->mac)) {
  789. return false;
  790. }
  791. for (i = 0; i < E1000E_NUM_QUEUES; i++) {
  792. E1000E_RxRing rxr;
  793. e1000e_rx_ring_init(core, &rxr, i);
  794. if (e1000e_ring_enabled(core, rxr.i) &&
  795. e1000e_has_rxbufs(core, rxr.i, 1)) {
  796. trace_e1000e_rx_can_recv();
  797. return true;
  798. }
  799. }
  800. trace_e1000e_rx_can_recv_rings_full();
  801. return false;
  802. }
  803. ssize_t
  804. e1000e_receive(E1000ECore *core, const uint8_t *buf, size_t size)
  805. {
  806. const struct iovec iov = {
  807. .iov_base = (uint8_t *)buf,
  808. .iov_len = size
  809. };
  810. return e1000e_receive_iov(core, &iov, 1);
  811. }
  812. static inline bool
  813. e1000e_rx_l3_cso_enabled(E1000ECore *core)
  814. {
  815. return !!(core->mac[RXCSUM] & E1000_RXCSUM_IPOFLD);
  816. }
  817. static inline bool
  818. e1000e_rx_l4_cso_enabled(E1000ECore *core)
  819. {
  820. return !!(core->mac[RXCSUM] & E1000_RXCSUM_TUOFLD);
  821. }
  822. static bool
  823. e1000e_receive_filter(E1000ECore *core, const uint8_t *buf, int size)
  824. {
  825. uint32_t rctl = core->mac[RCTL];
  826. if (e1000x_is_vlan_packet(buf, core->vet) &&
  827. e1000x_vlan_rx_filter_enabled(core->mac)) {
  828. uint16_t vid = lduw_be_p(buf + 14);
  829. uint32_t vfta = ldl_le_p((uint32_t *)(core->mac + VFTA) +
  830. ((vid >> 5) & 0x7f));
  831. if ((vfta & (1 << (vid & 0x1f))) == 0) {
  832. trace_e1000e_rx_flt_vlan_mismatch(vid);
  833. return false;
  834. } else {
  835. trace_e1000e_rx_flt_vlan_match(vid);
  836. }
  837. }
  838. switch (net_rx_pkt_get_packet_type(core->rx_pkt)) {
  839. case ETH_PKT_UCAST:
  840. if (rctl & E1000_RCTL_UPE) {
  841. return true; /* promiscuous ucast */
  842. }
  843. break;
  844. case ETH_PKT_BCAST:
  845. if (rctl & E1000_RCTL_BAM) {
  846. return true; /* broadcast enabled */
  847. }
  848. break;
  849. case ETH_PKT_MCAST:
  850. if (rctl & E1000_RCTL_MPE) {
  851. return true; /* promiscuous mcast */
  852. }
  853. break;
  854. default:
  855. g_assert_not_reached();
  856. }
  857. return e1000x_rx_group_filter(core->mac, buf);
  858. }
  859. static inline void
  860. e1000e_read_lgcy_rx_descr(E1000ECore *core, uint8_t *desc, hwaddr *buff_addr)
  861. {
  862. struct e1000_rx_desc *d = (struct e1000_rx_desc *) desc;
  863. *buff_addr = le64_to_cpu(d->buffer_addr);
  864. }
  865. static inline void
  866. e1000e_read_ext_rx_descr(E1000ECore *core, uint8_t *desc, hwaddr *buff_addr)
  867. {
  868. union e1000_rx_desc_extended *d = (union e1000_rx_desc_extended *) desc;
  869. *buff_addr = le64_to_cpu(d->read.buffer_addr);
  870. }
  871. static inline void
  872. e1000e_read_ps_rx_descr(E1000ECore *core, uint8_t *desc,
  873. hwaddr (*buff_addr)[MAX_PS_BUFFERS])
  874. {
  875. int i;
  876. union e1000_rx_desc_packet_split *d =
  877. (union e1000_rx_desc_packet_split *) desc;
  878. for (i = 0; i < MAX_PS_BUFFERS; i++) {
  879. (*buff_addr)[i] = le64_to_cpu(d->read.buffer_addr[i]);
  880. }
  881. trace_e1000e_rx_desc_ps_read((*buff_addr)[0], (*buff_addr)[1],
  882. (*buff_addr)[2], (*buff_addr)[3]);
  883. }
  884. static inline void
  885. e1000e_read_rx_descr(E1000ECore *core, uint8_t *desc,
  886. hwaddr (*buff_addr)[MAX_PS_BUFFERS])
  887. {
  888. if (e1000e_rx_use_legacy_descriptor(core)) {
  889. e1000e_read_lgcy_rx_descr(core, desc, &(*buff_addr)[0]);
  890. (*buff_addr)[1] = (*buff_addr)[2] = (*buff_addr)[3] = 0;
  891. } else {
  892. if (core->mac[RCTL] & E1000_RCTL_DTYP_PS) {
  893. e1000e_read_ps_rx_descr(core, desc, buff_addr);
  894. } else {
  895. e1000e_read_ext_rx_descr(core, desc, &(*buff_addr)[0]);
  896. (*buff_addr)[1] = (*buff_addr)[2] = (*buff_addr)[3] = 0;
  897. }
  898. }
  899. }
  900. static void
  901. e1000e_verify_csum_in_sw(E1000ECore *core,
  902. struct NetRxPkt *pkt,
  903. uint32_t *status_flags,
  904. bool istcp, bool isudp)
  905. {
  906. bool csum_valid;
  907. uint32_t csum_error;
  908. if (e1000e_rx_l3_cso_enabled(core)) {
  909. if (!net_rx_pkt_validate_l3_csum(pkt, &csum_valid)) {
  910. trace_e1000e_rx_metadata_l3_csum_validation_failed();
  911. } else {
  912. csum_error = csum_valid ? 0 : E1000_RXDEXT_STATERR_IPE;
  913. *status_flags |= E1000_RXD_STAT_IPCS | csum_error;
  914. }
  915. } else {
  916. trace_e1000e_rx_metadata_l3_cso_disabled();
  917. }
  918. if (!e1000e_rx_l4_cso_enabled(core)) {
  919. trace_e1000e_rx_metadata_l4_cso_disabled();
  920. return;
  921. }
  922. if (!net_rx_pkt_validate_l4_csum(pkt, &csum_valid)) {
  923. trace_e1000e_rx_metadata_l4_csum_validation_failed();
  924. return;
  925. }
  926. csum_error = csum_valid ? 0 : E1000_RXDEXT_STATERR_TCPE;
  927. if (istcp) {
  928. *status_flags |= E1000_RXD_STAT_TCPCS |
  929. csum_error;
  930. } else if (isudp) {
  931. *status_flags |= E1000_RXD_STAT_TCPCS |
  932. E1000_RXD_STAT_UDPCS |
  933. csum_error;
  934. }
  935. }
  936. static inline bool
  937. e1000e_is_tcp_ack(E1000ECore *core, struct NetRxPkt *rx_pkt)
  938. {
  939. if (!net_rx_pkt_is_tcp_ack(rx_pkt)) {
  940. return false;
  941. }
  942. if (core->mac[RFCTL] & E1000_RFCTL_ACK_DATA_DIS) {
  943. return !net_rx_pkt_has_tcp_data(rx_pkt);
  944. }
  945. return true;
  946. }
  947. static void
  948. e1000e_build_rx_metadata(E1000ECore *core,
  949. struct NetRxPkt *pkt,
  950. bool is_eop,
  951. const E1000E_RSSInfo *rss_info,
  952. uint32_t *rss, uint32_t *mrq,
  953. uint32_t *status_flags,
  954. uint16_t *ip_id,
  955. uint16_t *vlan_tag)
  956. {
  957. struct virtio_net_hdr *vhdr;
  958. bool isip4, isip6, istcp, isudp;
  959. uint32_t pkt_type;
  960. *status_flags = E1000_RXD_STAT_DD;
  961. /* No additional metadata needed for non-EOP descriptors */
  962. if (!is_eop) {
  963. goto func_exit;
  964. }
  965. *status_flags |= E1000_RXD_STAT_EOP;
  966. net_rx_pkt_get_protocols(pkt, &isip4, &isip6, &isudp, &istcp);
  967. trace_e1000e_rx_metadata_protocols(isip4, isip6, isudp, istcp);
  968. /* VLAN state */
  969. if (net_rx_pkt_is_vlan_stripped(pkt)) {
  970. *status_flags |= E1000_RXD_STAT_VP;
  971. *vlan_tag = cpu_to_le16(net_rx_pkt_get_vlan_tag(pkt));
  972. trace_e1000e_rx_metadata_vlan(*vlan_tag);
  973. }
  974. /* Packet parsing results */
  975. if ((core->mac[RXCSUM] & E1000_RXCSUM_PCSD) != 0) {
  976. if (rss_info->enabled) {
  977. *rss = cpu_to_le32(rss_info->hash);
  978. *mrq = cpu_to_le32(rss_info->type | (rss_info->queue << 8));
  979. trace_e1000e_rx_metadata_rss(*rss, *mrq);
  980. }
  981. } else if (isip4) {
  982. *status_flags |= E1000_RXD_STAT_IPIDV;
  983. *ip_id = cpu_to_le16(net_rx_pkt_get_ip_id(pkt));
  984. trace_e1000e_rx_metadata_ip_id(*ip_id);
  985. }
  986. if (istcp && e1000e_is_tcp_ack(core, pkt)) {
  987. *status_flags |= E1000_RXD_STAT_ACK;
  988. trace_e1000e_rx_metadata_ack();
  989. }
  990. if (isip6 && (core->mac[RFCTL] & E1000_RFCTL_IPV6_DIS)) {
  991. trace_e1000e_rx_metadata_ipv6_filtering_disabled();
  992. pkt_type = E1000_RXD_PKT_MAC;
  993. } else if (istcp || isudp) {
  994. pkt_type = isip4 ? E1000_RXD_PKT_IP4_XDP : E1000_RXD_PKT_IP6_XDP;
  995. } else if (isip4 || isip6) {
  996. pkt_type = isip4 ? E1000_RXD_PKT_IP4 : E1000_RXD_PKT_IP6;
  997. } else {
  998. pkt_type = E1000_RXD_PKT_MAC;
  999. }
  1000. *status_flags |= E1000_RXD_PKT_TYPE(pkt_type);
  1001. trace_e1000e_rx_metadata_pkt_type(pkt_type);
  1002. /* RX CSO information */
  1003. if (isip6 && (core->mac[RFCTL] & E1000_RFCTL_IPV6_XSUM_DIS)) {
  1004. trace_e1000e_rx_metadata_ipv6_sum_disabled();
  1005. goto func_exit;
  1006. }
  1007. if (!net_rx_pkt_has_virt_hdr(pkt)) {
  1008. trace_e1000e_rx_metadata_no_virthdr();
  1009. e1000e_verify_csum_in_sw(core, pkt, status_flags, istcp, isudp);
  1010. goto func_exit;
  1011. }
  1012. vhdr = net_rx_pkt_get_vhdr(pkt);
  1013. if (!(vhdr->flags & VIRTIO_NET_HDR_F_DATA_VALID) &&
  1014. !(vhdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM)) {
  1015. trace_e1000e_rx_metadata_virthdr_no_csum_info();
  1016. e1000e_verify_csum_in_sw(core, pkt, status_flags, istcp, isudp);
  1017. goto func_exit;
  1018. }
  1019. if (e1000e_rx_l3_cso_enabled(core)) {
  1020. *status_flags |= isip4 ? E1000_RXD_STAT_IPCS : 0;
  1021. } else {
  1022. trace_e1000e_rx_metadata_l3_cso_disabled();
  1023. }
  1024. if (e1000e_rx_l4_cso_enabled(core)) {
  1025. if (istcp) {
  1026. *status_flags |= E1000_RXD_STAT_TCPCS;
  1027. } else if (isudp) {
  1028. *status_flags |= E1000_RXD_STAT_TCPCS | E1000_RXD_STAT_UDPCS;
  1029. }
  1030. } else {
  1031. trace_e1000e_rx_metadata_l4_cso_disabled();
  1032. }
  1033. trace_e1000e_rx_metadata_status_flags(*status_flags);
  1034. func_exit:
  1035. *status_flags = cpu_to_le32(*status_flags);
  1036. }
  1037. static inline void
  1038. e1000e_write_lgcy_rx_descr(E1000ECore *core, uint8_t *desc,
  1039. struct NetRxPkt *pkt,
  1040. const E1000E_RSSInfo *rss_info,
  1041. uint16_t length)
  1042. {
  1043. uint32_t status_flags, rss, mrq;
  1044. uint16_t ip_id;
  1045. struct e1000_rx_desc *d = (struct e1000_rx_desc *) desc;
  1046. assert(!rss_info->enabled);
  1047. d->length = cpu_to_le16(length);
  1048. d->csum = 0;
  1049. e1000e_build_rx_metadata(core, pkt, pkt != NULL,
  1050. rss_info,
  1051. &rss, &mrq,
  1052. &status_flags, &ip_id,
  1053. &d->special);
  1054. d->errors = (uint8_t) (le32_to_cpu(status_flags) >> 24);
  1055. d->status = (uint8_t) le32_to_cpu(status_flags);
  1056. d->special = 0;
  1057. }
  1058. static inline void
  1059. e1000e_write_ext_rx_descr(E1000ECore *core, uint8_t *desc,
  1060. struct NetRxPkt *pkt,
  1061. const E1000E_RSSInfo *rss_info,
  1062. uint16_t length)
  1063. {
  1064. union e1000_rx_desc_extended *d = (union e1000_rx_desc_extended *) desc;
  1065. memset(&d->wb, 0, sizeof(d->wb));
  1066. d->wb.upper.length = cpu_to_le16(length);
  1067. e1000e_build_rx_metadata(core, pkt, pkt != NULL,
  1068. rss_info,
  1069. &d->wb.lower.hi_dword.rss,
  1070. &d->wb.lower.mrq,
  1071. &d->wb.upper.status_error,
  1072. &d->wb.lower.hi_dword.csum_ip.ip_id,
  1073. &d->wb.upper.vlan);
  1074. }
  1075. static inline void
  1076. e1000e_write_ps_rx_descr(E1000ECore *core, uint8_t *desc,
  1077. struct NetRxPkt *pkt,
  1078. const E1000E_RSSInfo *rss_info,
  1079. size_t ps_hdr_len,
  1080. uint16_t(*written)[MAX_PS_BUFFERS])
  1081. {
  1082. int i;
  1083. union e1000_rx_desc_packet_split *d =
  1084. (union e1000_rx_desc_packet_split *) desc;
  1085. memset(&d->wb, 0, sizeof(d->wb));
  1086. d->wb.middle.length0 = cpu_to_le16((*written)[0]);
  1087. for (i = 0; i < PS_PAGE_BUFFERS; i++) {
  1088. d->wb.upper.length[i] = cpu_to_le16((*written)[i + 1]);
  1089. }
  1090. e1000e_build_rx_metadata(core, pkt, pkt != NULL,
  1091. rss_info,
  1092. &d->wb.lower.hi_dword.rss,
  1093. &d->wb.lower.mrq,
  1094. &d->wb.middle.status_error,
  1095. &d->wb.lower.hi_dword.csum_ip.ip_id,
  1096. &d->wb.middle.vlan);
  1097. d->wb.upper.header_status =
  1098. cpu_to_le16(ps_hdr_len | (ps_hdr_len ? E1000_RXDPS_HDRSTAT_HDRSP : 0));
  1099. trace_e1000e_rx_desc_ps_write((*written)[0], (*written)[1],
  1100. (*written)[2], (*written)[3]);
  1101. }
  1102. static inline void
  1103. e1000e_write_rx_descr(E1000ECore *core, uint8_t *desc,
  1104. struct NetRxPkt *pkt, const E1000E_RSSInfo *rss_info,
  1105. size_t ps_hdr_len, uint16_t(*written)[MAX_PS_BUFFERS])
  1106. {
  1107. if (e1000e_rx_use_legacy_descriptor(core)) {
  1108. assert(ps_hdr_len == 0);
  1109. e1000e_write_lgcy_rx_descr(core, desc, pkt, rss_info, (*written)[0]);
  1110. } else {
  1111. if (core->mac[RCTL] & E1000_RCTL_DTYP_PS) {
  1112. e1000e_write_ps_rx_descr(core, desc, pkt, rss_info,
  1113. ps_hdr_len, written);
  1114. } else {
  1115. assert(ps_hdr_len == 0);
  1116. e1000e_write_ext_rx_descr(core, desc, pkt, rss_info,
  1117. (*written)[0]);
  1118. }
  1119. }
  1120. }
  1121. typedef struct e1000e_ba_state_st {
  1122. uint16_t written[MAX_PS_BUFFERS];
  1123. uint8_t cur_idx;
  1124. } e1000e_ba_state;
  1125. static inline void
  1126. e1000e_write_hdr_to_rx_buffers(E1000ECore *core,
  1127. hwaddr (*ba)[MAX_PS_BUFFERS],
  1128. e1000e_ba_state *bastate,
  1129. const char *data,
  1130. dma_addr_t data_len)
  1131. {
  1132. assert(data_len <= core->rxbuf_sizes[0] - bastate->written[0]);
  1133. pci_dma_write(core->owner, (*ba)[0] + bastate->written[0], data, data_len);
  1134. bastate->written[0] += data_len;
  1135. bastate->cur_idx = 1;
  1136. }
  1137. static void
  1138. e1000e_write_to_rx_buffers(E1000ECore *core,
  1139. hwaddr (*ba)[MAX_PS_BUFFERS],
  1140. e1000e_ba_state *bastate,
  1141. const char *data,
  1142. dma_addr_t data_len)
  1143. {
  1144. while (data_len > 0) {
  1145. uint32_t cur_buf_len = core->rxbuf_sizes[bastate->cur_idx];
  1146. uint32_t cur_buf_bytes_left = cur_buf_len -
  1147. bastate->written[bastate->cur_idx];
  1148. uint32_t bytes_to_write = MIN(data_len, cur_buf_bytes_left);
  1149. trace_e1000e_rx_desc_buff_write(bastate->cur_idx,
  1150. (*ba)[bastate->cur_idx],
  1151. bastate->written[bastate->cur_idx],
  1152. data,
  1153. bytes_to_write);
  1154. pci_dma_write(core->owner,
  1155. (*ba)[bastate->cur_idx] + bastate->written[bastate->cur_idx],
  1156. data, bytes_to_write);
  1157. bastate->written[bastate->cur_idx] += bytes_to_write;
  1158. data += bytes_to_write;
  1159. data_len -= bytes_to_write;
  1160. if (bastate->written[bastate->cur_idx] == cur_buf_len) {
  1161. bastate->cur_idx++;
  1162. }
  1163. assert(bastate->cur_idx < MAX_PS_BUFFERS);
  1164. }
  1165. }
  1166. static void
  1167. e1000e_update_rx_stats(E1000ECore *core,
  1168. size_t data_size,
  1169. size_t data_fcs_size)
  1170. {
  1171. e1000x_update_rx_total_stats(core->mac, data_size, data_fcs_size);
  1172. switch (net_rx_pkt_get_packet_type(core->rx_pkt)) {
  1173. case ETH_PKT_BCAST:
  1174. e1000x_inc_reg_if_not_full(core->mac, BPRC);
  1175. break;
  1176. case ETH_PKT_MCAST:
  1177. e1000x_inc_reg_if_not_full(core->mac, MPRC);
  1178. break;
  1179. default:
  1180. break;
  1181. }
  1182. }
  1183. static inline bool
  1184. e1000e_rx_descr_threshold_hit(E1000ECore *core, const E1000E_RingInfo *rxi)
  1185. {
  1186. return e1000e_ring_free_descr_num(core, rxi) ==
  1187. e1000e_ring_len(core, rxi) >> core->rxbuf_min_shift;
  1188. }
  1189. static bool
  1190. e1000e_do_ps(E1000ECore *core, struct NetRxPkt *pkt, size_t *hdr_len)
  1191. {
  1192. bool isip4, isip6, isudp, istcp;
  1193. bool fragment;
  1194. if (!e1000e_rx_use_ps_descriptor(core)) {
  1195. return false;
  1196. }
  1197. net_rx_pkt_get_protocols(pkt, &isip4, &isip6, &isudp, &istcp);
  1198. if (isip4) {
  1199. fragment = net_rx_pkt_get_ip4_info(pkt)->fragment;
  1200. } else if (isip6) {
  1201. fragment = net_rx_pkt_get_ip6_info(pkt)->fragment;
  1202. } else {
  1203. return false;
  1204. }
  1205. if (fragment && (core->mac[RFCTL] & E1000_RFCTL_IPFRSP_DIS)) {
  1206. return false;
  1207. }
  1208. if (!fragment && (isudp || istcp)) {
  1209. *hdr_len = net_rx_pkt_get_l5_hdr_offset(pkt);
  1210. } else {
  1211. *hdr_len = net_rx_pkt_get_l4_hdr_offset(pkt);
  1212. }
  1213. if ((*hdr_len > core->rxbuf_sizes[0]) ||
  1214. (*hdr_len > net_rx_pkt_get_total_len(pkt))) {
  1215. return false;
  1216. }
  1217. return true;
  1218. }
  1219. static void
  1220. e1000e_write_packet_to_guest(E1000ECore *core, struct NetRxPkt *pkt,
  1221. const E1000E_RxRing *rxr,
  1222. const E1000E_RSSInfo *rss_info)
  1223. {
  1224. PCIDevice *d = core->owner;
  1225. dma_addr_t base;
  1226. uint8_t desc[E1000_MAX_RX_DESC_LEN];
  1227. size_t desc_size;
  1228. size_t desc_offset = 0;
  1229. size_t iov_ofs = 0;
  1230. struct iovec *iov = net_rx_pkt_get_iovec(pkt);
  1231. size_t size = net_rx_pkt_get_total_len(pkt);
  1232. size_t total_size = size + e1000x_fcs_len(core->mac);
  1233. const E1000E_RingInfo *rxi;
  1234. size_t ps_hdr_len = 0;
  1235. bool do_ps = e1000e_do_ps(core, pkt, &ps_hdr_len);
  1236. bool is_first = true;
  1237. rxi = rxr->i;
  1238. do {
  1239. hwaddr ba[MAX_PS_BUFFERS];
  1240. e1000e_ba_state bastate = { { 0 } };
  1241. bool is_last = false;
  1242. desc_size = total_size - desc_offset;
  1243. if (desc_size > core->rx_desc_buf_size) {
  1244. desc_size = core->rx_desc_buf_size;
  1245. }
  1246. if (e1000e_ring_empty(core, rxi)) {
  1247. return;
  1248. }
  1249. base = e1000e_ring_head_descr(core, rxi);
  1250. pci_dma_read(d, base, &desc, core->rx_desc_len);
  1251. trace_e1000e_rx_descr(rxi->idx, base, core->rx_desc_len);
  1252. e1000e_read_rx_descr(core, desc, &ba);
  1253. if (ba[0]) {
  1254. if (desc_offset < size) {
  1255. static const uint32_t fcs_pad;
  1256. size_t iov_copy;
  1257. size_t copy_size = size - desc_offset;
  1258. if (copy_size > core->rx_desc_buf_size) {
  1259. copy_size = core->rx_desc_buf_size;
  1260. }
  1261. /* For PS mode copy the packet header first */
  1262. if (do_ps) {
  1263. if (is_first) {
  1264. size_t ps_hdr_copied = 0;
  1265. do {
  1266. iov_copy = MIN(ps_hdr_len - ps_hdr_copied,
  1267. iov->iov_len - iov_ofs);
  1268. e1000e_write_hdr_to_rx_buffers(core, &ba, &bastate,
  1269. iov->iov_base, iov_copy);
  1270. copy_size -= iov_copy;
  1271. ps_hdr_copied += iov_copy;
  1272. iov_ofs += iov_copy;
  1273. if (iov_ofs == iov->iov_len) {
  1274. iov++;
  1275. iov_ofs = 0;
  1276. }
  1277. } while (ps_hdr_copied < ps_hdr_len);
  1278. is_first = false;
  1279. } else {
  1280. /* Leave buffer 0 of each descriptor except first */
  1281. /* empty as per spec 7.1.5.1 */
  1282. e1000e_write_hdr_to_rx_buffers(core, &ba, &bastate,
  1283. NULL, 0);
  1284. }
  1285. }
  1286. /* Copy packet payload */
  1287. while (copy_size) {
  1288. iov_copy = MIN(copy_size, iov->iov_len - iov_ofs);
  1289. e1000e_write_to_rx_buffers(core, &ba, &bastate,
  1290. iov->iov_base + iov_ofs, iov_copy);
  1291. copy_size -= iov_copy;
  1292. iov_ofs += iov_copy;
  1293. if (iov_ofs == iov->iov_len) {
  1294. iov++;
  1295. iov_ofs = 0;
  1296. }
  1297. }
  1298. if (desc_offset + desc_size >= total_size) {
  1299. /* Simulate FCS checksum presence in the last descriptor */
  1300. e1000e_write_to_rx_buffers(core, &ba, &bastate,
  1301. (const char *) &fcs_pad, e1000x_fcs_len(core->mac));
  1302. }
  1303. }
  1304. desc_offset += desc_size;
  1305. if (desc_offset >= total_size) {
  1306. is_last = true;
  1307. }
  1308. } else { /* as per intel docs; skip descriptors with null buf addr */
  1309. trace_e1000e_rx_null_descriptor();
  1310. }
  1311. e1000e_write_rx_descr(core, desc, is_last ? core->rx_pkt : NULL,
  1312. rss_info, do_ps ? ps_hdr_len : 0, &bastate.written);
  1313. pci_dma_write(d, base, &desc, core->rx_desc_len);
  1314. e1000e_ring_advance(core, rxi,
  1315. core->rx_desc_len / E1000_MIN_RX_DESC_LEN);
  1316. } while (desc_offset < total_size);
  1317. e1000e_update_rx_stats(core, size, total_size);
  1318. }
  1319. static inline void
  1320. e1000e_rx_fix_l4_csum(E1000ECore *core, struct NetRxPkt *pkt)
  1321. {
  1322. if (net_rx_pkt_has_virt_hdr(pkt)) {
  1323. struct virtio_net_hdr *vhdr = net_rx_pkt_get_vhdr(pkt);
  1324. if (vhdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) {
  1325. net_rx_pkt_fix_l4_csum(pkt);
  1326. }
  1327. }
  1328. }
  1329. ssize_t
  1330. e1000e_receive_iov(E1000ECore *core, const struct iovec *iov, int iovcnt)
  1331. {
  1332. static const int maximum_ethernet_hdr_len = (14 + 4);
  1333. /* Min. octets in an ethernet frame sans FCS */
  1334. static const int min_buf_size = 60;
  1335. uint32_t n = 0;
  1336. uint8_t min_buf[min_buf_size];
  1337. struct iovec min_iov;
  1338. uint8_t *filter_buf;
  1339. size_t size, orig_size;
  1340. size_t iov_ofs = 0;
  1341. E1000E_RxRing rxr;
  1342. E1000E_RSSInfo rss_info;
  1343. size_t total_size;
  1344. ssize_t retval;
  1345. bool rdmts_hit;
  1346. trace_e1000e_rx_receive_iov(iovcnt);
  1347. if (!e1000x_hw_rx_enabled(core->mac)) {
  1348. return -1;
  1349. }
  1350. /* Pull virtio header in */
  1351. if (core->has_vnet) {
  1352. net_rx_pkt_set_vhdr_iovec(core->rx_pkt, iov, iovcnt);
  1353. iov_ofs = sizeof(struct virtio_net_hdr);
  1354. }
  1355. filter_buf = iov->iov_base + iov_ofs;
  1356. orig_size = iov_size(iov, iovcnt);
  1357. size = orig_size - iov_ofs;
  1358. /* Pad to minimum Ethernet frame length */
  1359. if (size < sizeof(min_buf)) {
  1360. iov_to_buf(iov, iovcnt, iov_ofs, min_buf, size);
  1361. memset(&min_buf[size], 0, sizeof(min_buf) - size);
  1362. e1000x_inc_reg_if_not_full(core->mac, RUC);
  1363. min_iov.iov_base = filter_buf = min_buf;
  1364. min_iov.iov_len = size = sizeof(min_buf);
  1365. iovcnt = 1;
  1366. iov = &min_iov;
  1367. iov_ofs = 0;
  1368. } else if (iov->iov_len < maximum_ethernet_hdr_len) {
  1369. /* This is very unlikely, but may happen. */
  1370. iov_to_buf(iov, iovcnt, iov_ofs, min_buf, maximum_ethernet_hdr_len);
  1371. filter_buf = min_buf;
  1372. }
  1373. /* Discard oversized packets if !LPE and !SBP. */
  1374. if (e1000x_is_oversized(core->mac, size)) {
  1375. return orig_size;
  1376. }
  1377. net_rx_pkt_set_packet_type(core->rx_pkt,
  1378. get_eth_packet_type(PKT_GET_ETH_HDR(filter_buf)));
  1379. if (!e1000e_receive_filter(core, filter_buf, size)) {
  1380. trace_e1000e_rx_flt_dropped();
  1381. return orig_size;
  1382. }
  1383. net_rx_pkt_attach_iovec_ex(core->rx_pkt, iov, iovcnt, iov_ofs,
  1384. e1000x_vlan_enabled(core->mac), core->vet);
  1385. e1000e_rss_parse_packet(core, core->rx_pkt, &rss_info);
  1386. e1000e_rx_ring_init(core, &rxr, rss_info.queue);
  1387. trace_e1000e_rx_rss_dispatched_to_queue(rxr.i->idx);
  1388. total_size = net_rx_pkt_get_total_len(core->rx_pkt) +
  1389. e1000x_fcs_len(core->mac);
  1390. if (e1000e_has_rxbufs(core, rxr.i, total_size)) {
  1391. e1000e_rx_fix_l4_csum(core, core->rx_pkt);
  1392. e1000e_write_packet_to_guest(core, core->rx_pkt, &rxr, &rss_info);
  1393. retval = orig_size;
  1394. /* Perform small receive detection (RSRPD) */
  1395. if (total_size < core->mac[RSRPD]) {
  1396. n |= E1000_ICS_SRPD;
  1397. }
  1398. /* Perform ACK receive detection */
  1399. if (!(core->mac[RFCTL] & E1000_RFCTL_ACK_DIS) &&
  1400. (e1000e_is_tcp_ack(core, core->rx_pkt))) {
  1401. n |= E1000_ICS_ACK;
  1402. }
  1403. /* Check if receive descriptor minimum threshold hit */
  1404. rdmts_hit = e1000e_rx_descr_threshold_hit(core, rxr.i);
  1405. n |= e1000e_rx_wb_interrupt_cause(core, rxr.i->idx, rdmts_hit);
  1406. trace_e1000e_rx_written_to_guest(n);
  1407. } else {
  1408. n |= E1000_ICS_RXO;
  1409. retval = 0;
  1410. trace_e1000e_rx_not_written_to_guest(n);
  1411. }
  1412. if (!e1000e_intrmgr_delay_rx_causes(core, &n)) {
  1413. trace_e1000e_rx_interrupt_set(n);
  1414. e1000e_set_interrupt_cause(core, n);
  1415. } else {
  1416. trace_e1000e_rx_interrupt_delayed(n);
  1417. }
  1418. return retval;
  1419. }
  1420. static inline bool
  1421. e1000e_have_autoneg(E1000ECore *core)
  1422. {
  1423. return core->phy[0][PHY_CTRL] & MII_CR_AUTO_NEG_EN;
  1424. }
  1425. static void e1000e_update_flowctl_status(E1000ECore *core)
  1426. {
  1427. if (e1000e_have_autoneg(core) &&
  1428. core->phy[0][PHY_STATUS] & MII_SR_AUTONEG_COMPLETE) {
  1429. trace_e1000e_link_autoneg_flowctl(true);
  1430. core->mac[CTRL] |= E1000_CTRL_TFCE | E1000_CTRL_RFCE;
  1431. } else {
  1432. trace_e1000e_link_autoneg_flowctl(false);
  1433. }
  1434. }
  1435. static inline void
  1436. e1000e_link_down(E1000ECore *core)
  1437. {
  1438. e1000x_update_regs_on_link_down(core->mac, core->phy[0]);
  1439. e1000e_update_flowctl_status(core);
  1440. }
  1441. static inline void
  1442. e1000e_set_phy_ctrl(E1000ECore *core, int index, uint16_t val)
  1443. {
  1444. /* bits 0-5 reserved; MII_CR_[RESTART_AUTO_NEG,RESET] are self clearing */
  1445. core->phy[0][PHY_CTRL] = val & ~(0x3f |
  1446. MII_CR_RESET |
  1447. MII_CR_RESTART_AUTO_NEG);
  1448. if ((val & MII_CR_RESTART_AUTO_NEG) &&
  1449. e1000e_have_autoneg(core)) {
  1450. e1000x_restart_autoneg(core->mac, core->phy[0], core->autoneg_timer);
  1451. }
  1452. }
  1453. static void
  1454. e1000e_set_phy_oem_bits(E1000ECore *core, int index, uint16_t val)
  1455. {
  1456. core->phy[0][PHY_OEM_BITS] = val & ~BIT(10);
  1457. if (val & BIT(10)) {
  1458. e1000x_restart_autoneg(core->mac, core->phy[0], core->autoneg_timer);
  1459. }
  1460. }
  1461. static void
  1462. e1000e_set_phy_page(E1000ECore *core, int index, uint16_t val)
  1463. {
  1464. core->phy[0][PHY_PAGE] = val & PHY_PAGE_RW_MASK;
  1465. }
  1466. void
  1467. e1000e_core_set_link_status(E1000ECore *core)
  1468. {
  1469. NetClientState *nc = qemu_get_queue(core->owner_nic);
  1470. uint32_t old_status = core->mac[STATUS];
  1471. trace_e1000e_link_status_changed(nc->link_down ? false : true);
  1472. if (nc->link_down) {
  1473. e1000x_update_regs_on_link_down(core->mac, core->phy[0]);
  1474. } else {
  1475. if (e1000e_have_autoneg(core) &&
  1476. !(core->phy[0][PHY_STATUS] & MII_SR_AUTONEG_COMPLETE)) {
  1477. e1000x_restart_autoneg(core->mac, core->phy[0],
  1478. core->autoneg_timer);
  1479. } else {
  1480. e1000x_update_regs_on_link_up(core->mac, core->phy[0]);
  1481. e1000e_start_recv(core);
  1482. }
  1483. }
  1484. if (core->mac[STATUS] != old_status) {
  1485. e1000e_set_interrupt_cause(core, E1000_ICR_LSC);
  1486. }
  1487. }
  1488. static void
  1489. e1000e_set_ctrl(E1000ECore *core, int index, uint32_t val)
  1490. {
  1491. trace_e1000e_core_ctrl_write(index, val);
  1492. /* RST is self clearing */
  1493. core->mac[CTRL] = val & ~E1000_CTRL_RST;
  1494. core->mac[CTRL_DUP] = core->mac[CTRL];
  1495. trace_e1000e_link_set_params(
  1496. !!(val & E1000_CTRL_ASDE),
  1497. (val & E1000_CTRL_SPD_SEL) >> E1000_CTRL_SPD_SHIFT,
  1498. !!(val & E1000_CTRL_FRCSPD),
  1499. !!(val & E1000_CTRL_FRCDPX),
  1500. !!(val & E1000_CTRL_RFCE),
  1501. !!(val & E1000_CTRL_TFCE));
  1502. if (val & E1000_CTRL_RST) {
  1503. trace_e1000e_core_ctrl_sw_reset();
  1504. e1000x_reset_mac_addr(core->owner_nic, core->mac, core->permanent_mac);
  1505. }
  1506. if (val & E1000_CTRL_PHY_RST) {
  1507. trace_e1000e_core_ctrl_phy_reset();
  1508. core->mac[STATUS] |= E1000_STATUS_PHYRA;
  1509. }
  1510. }
  1511. static void
  1512. e1000e_set_rfctl(E1000ECore *core, int index, uint32_t val)
  1513. {
  1514. trace_e1000e_rx_set_rfctl(val);
  1515. if (!(val & E1000_RFCTL_ISCSI_DIS)) {
  1516. trace_e1000e_wrn_iscsi_filtering_not_supported();
  1517. }
  1518. if (!(val & E1000_RFCTL_NFSW_DIS)) {
  1519. trace_e1000e_wrn_nfsw_filtering_not_supported();
  1520. }
  1521. if (!(val & E1000_RFCTL_NFSR_DIS)) {
  1522. trace_e1000e_wrn_nfsr_filtering_not_supported();
  1523. }
  1524. core->mac[RFCTL] = val;
  1525. }
  1526. static void
  1527. e1000e_calc_per_desc_buf_size(E1000ECore *core)
  1528. {
  1529. int i;
  1530. core->rx_desc_buf_size = 0;
  1531. for (i = 0; i < ARRAY_SIZE(core->rxbuf_sizes); i++) {
  1532. core->rx_desc_buf_size += core->rxbuf_sizes[i];
  1533. }
  1534. }
  1535. static void
  1536. e1000e_parse_rxbufsize(E1000ECore *core)
  1537. {
  1538. uint32_t rctl = core->mac[RCTL];
  1539. memset(core->rxbuf_sizes, 0, sizeof(core->rxbuf_sizes));
  1540. if (rctl & E1000_RCTL_DTYP_MASK) {
  1541. uint32_t bsize;
  1542. bsize = core->mac[PSRCTL] & E1000_PSRCTL_BSIZE0_MASK;
  1543. core->rxbuf_sizes[0] = (bsize >> E1000_PSRCTL_BSIZE0_SHIFT) * 128;
  1544. bsize = core->mac[PSRCTL] & E1000_PSRCTL_BSIZE1_MASK;
  1545. core->rxbuf_sizes[1] = (bsize >> E1000_PSRCTL_BSIZE1_SHIFT) * 1024;
  1546. bsize = core->mac[PSRCTL] & E1000_PSRCTL_BSIZE2_MASK;
  1547. core->rxbuf_sizes[2] = (bsize >> E1000_PSRCTL_BSIZE2_SHIFT) * 1024;
  1548. bsize = core->mac[PSRCTL] & E1000_PSRCTL_BSIZE3_MASK;
  1549. core->rxbuf_sizes[3] = (bsize >> E1000_PSRCTL_BSIZE3_SHIFT) * 1024;
  1550. } else if (rctl & E1000_RCTL_FLXBUF_MASK) {
  1551. int flxbuf = rctl & E1000_RCTL_FLXBUF_MASK;
  1552. core->rxbuf_sizes[0] = (flxbuf >> E1000_RCTL_FLXBUF_SHIFT) * 1024;
  1553. } else {
  1554. core->rxbuf_sizes[0] = e1000x_rxbufsize(rctl);
  1555. }
  1556. trace_e1000e_rx_desc_buff_sizes(core->rxbuf_sizes[0], core->rxbuf_sizes[1],
  1557. core->rxbuf_sizes[2], core->rxbuf_sizes[3]);
  1558. e1000e_calc_per_desc_buf_size(core);
  1559. }
  1560. static void
  1561. e1000e_calc_rxdesclen(E1000ECore *core)
  1562. {
  1563. if (e1000e_rx_use_legacy_descriptor(core)) {
  1564. core->rx_desc_len = sizeof(struct e1000_rx_desc);
  1565. } else {
  1566. if (core->mac[RCTL] & E1000_RCTL_DTYP_PS) {
  1567. core->rx_desc_len = sizeof(union e1000_rx_desc_packet_split);
  1568. } else {
  1569. core->rx_desc_len = sizeof(union e1000_rx_desc_extended);
  1570. }
  1571. }
  1572. trace_e1000e_rx_desc_len(core->rx_desc_len);
  1573. }
  1574. static void
  1575. e1000e_set_rx_control(E1000ECore *core, int index, uint32_t val)
  1576. {
  1577. core->mac[RCTL] = val;
  1578. trace_e1000e_rx_set_rctl(core->mac[RCTL]);
  1579. if (val & E1000_RCTL_EN) {
  1580. e1000e_parse_rxbufsize(core);
  1581. e1000e_calc_rxdesclen(core);
  1582. core->rxbuf_min_shift = ((val / E1000_RCTL_RDMTS_QUAT) & 3) + 1 +
  1583. E1000_RING_DESC_LEN_SHIFT;
  1584. e1000e_start_recv(core);
  1585. }
  1586. }
  1587. static
  1588. void(*e1000e_phyreg_writeops[E1000E_PHY_PAGES][E1000E_PHY_PAGE_SIZE])
  1589. (E1000ECore *, int, uint16_t) = {
  1590. [0] = {
  1591. [PHY_CTRL] = e1000e_set_phy_ctrl,
  1592. [PHY_PAGE] = e1000e_set_phy_page,
  1593. [PHY_OEM_BITS] = e1000e_set_phy_oem_bits
  1594. }
  1595. };
  1596. static inline void
  1597. e1000e_clear_ims_bits(E1000ECore *core, uint32_t bits)
  1598. {
  1599. trace_e1000e_irq_clear_ims(bits, core->mac[IMS], core->mac[IMS] & ~bits);
  1600. core->mac[IMS] &= ~bits;
  1601. }
  1602. static inline bool
  1603. e1000e_postpone_interrupt(bool *interrupt_pending,
  1604. E1000IntrDelayTimer *timer)
  1605. {
  1606. if (timer->running) {
  1607. trace_e1000e_irq_postponed_by_xitr(timer->delay_reg << 2);
  1608. *interrupt_pending = true;
  1609. return true;
  1610. }
  1611. if (timer->core->mac[timer->delay_reg] != 0) {
  1612. e1000e_intrmgr_rearm_timer(timer);
  1613. }
  1614. return false;
  1615. }
  1616. static inline bool
  1617. e1000e_itr_should_postpone(E1000ECore *core)
  1618. {
  1619. return e1000e_postpone_interrupt(&core->itr_intr_pending, &core->itr);
  1620. }
  1621. static inline bool
  1622. e1000e_eitr_should_postpone(E1000ECore *core, int idx)
  1623. {
  1624. return e1000e_postpone_interrupt(&core->eitr_intr_pending[idx],
  1625. &core->eitr[idx]);
  1626. }
  1627. static void
  1628. e1000e_msix_notify_one(E1000ECore *core, uint32_t cause, uint32_t int_cfg)
  1629. {
  1630. uint32_t effective_eiac;
  1631. if (E1000_IVAR_ENTRY_VALID(int_cfg)) {
  1632. uint32_t vec = E1000_IVAR_ENTRY_VEC(int_cfg);
  1633. if (vec < E1000E_MSIX_VEC_NUM) {
  1634. if (!e1000e_eitr_should_postpone(core, vec)) {
  1635. trace_e1000e_irq_msix_notify_vec(vec);
  1636. msix_notify(core->owner, vec);
  1637. }
  1638. } else {
  1639. trace_e1000e_wrn_msix_vec_wrong(cause, int_cfg);
  1640. }
  1641. } else {
  1642. trace_e1000e_wrn_msix_invalid(cause, int_cfg);
  1643. }
  1644. if (core->mac[CTRL_EXT] & E1000_CTRL_EXT_EIAME) {
  1645. trace_e1000e_irq_iam_clear_eiame(core->mac[IAM], cause);
  1646. core->mac[IAM] &= ~cause;
  1647. }
  1648. trace_e1000e_irq_icr_clear_eiac(core->mac[ICR], core->mac[EIAC]);
  1649. effective_eiac = core->mac[EIAC] & cause;
  1650. core->mac[ICR] &= ~effective_eiac;
  1651. core->msi_causes_pending &= ~effective_eiac;
  1652. if (!(core->mac[CTRL_EXT] & E1000_CTRL_EXT_IAME)) {
  1653. core->mac[IMS] &= ~effective_eiac;
  1654. }
  1655. }
  1656. static void
  1657. e1000e_msix_notify(E1000ECore *core, uint32_t causes)
  1658. {
  1659. if (causes & E1000_ICR_RXQ0) {
  1660. e1000e_msix_notify_one(core, E1000_ICR_RXQ0,
  1661. E1000_IVAR_RXQ0(core->mac[IVAR]));
  1662. }
  1663. if (causes & E1000_ICR_RXQ1) {
  1664. e1000e_msix_notify_one(core, E1000_ICR_RXQ1,
  1665. E1000_IVAR_RXQ1(core->mac[IVAR]));
  1666. }
  1667. if (causes & E1000_ICR_TXQ0) {
  1668. e1000e_msix_notify_one(core, E1000_ICR_TXQ0,
  1669. E1000_IVAR_TXQ0(core->mac[IVAR]));
  1670. }
  1671. if (causes & E1000_ICR_TXQ1) {
  1672. e1000e_msix_notify_one(core, E1000_ICR_TXQ1,
  1673. E1000_IVAR_TXQ1(core->mac[IVAR]));
  1674. }
  1675. if (causes & E1000_ICR_OTHER) {
  1676. e1000e_msix_notify_one(core, E1000_ICR_OTHER,
  1677. E1000_IVAR_OTHER(core->mac[IVAR]));
  1678. }
  1679. }
  1680. static void
  1681. e1000e_msix_clear_one(E1000ECore *core, uint32_t cause, uint32_t int_cfg)
  1682. {
  1683. if (E1000_IVAR_ENTRY_VALID(int_cfg)) {
  1684. uint32_t vec = E1000_IVAR_ENTRY_VEC(int_cfg);
  1685. if (vec < E1000E_MSIX_VEC_NUM) {
  1686. trace_e1000e_irq_msix_pending_clearing(cause, int_cfg, vec);
  1687. msix_clr_pending(core->owner, vec);
  1688. } else {
  1689. trace_e1000e_wrn_msix_vec_wrong(cause, int_cfg);
  1690. }
  1691. } else {
  1692. trace_e1000e_wrn_msix_invalid(cause, int_cfg);
  1693. }
  1694. }
  1695. static void
  1696. e1000e_msix_clear(E1000ECore *core, uint32_t causes)
  1697. {
  1698. if (causes & E1000_ICR_RXQ0) {
  1699. e1000e_msix_clear_one(core, E1000_ICR_RXQ0,
  1700. E1000_IVAR_RXQ0(core->mac[IVAR]));
  1701. }
  1702. if (causes & E1000_ICR_RXQ1) {
  1703. e1000e_msix_clear_one(core, E1000_ICR_RXQ1,
  1704. E1000_IVAR_RXQ1(core->mac[IVAR]));
  1705. }
  1706. if (causes & E1000_ICR_TXQ0) {
  1707. e1000e_msix_clear_one(core, E1000_ICR_TXQ0,
  1708. E1000_IVAR_TXQ0(core->mac[IVAR]));
  1709. }
  1710. if (causes & E1000_ICR_TXQ1) {
  1711. e1000e_msix_clear_one(core, E1000_ICR_TXQ1,
  1712. E1000_IVAR_TXQ1(core->mac[IVAR]));
  1713. }
  1714. if (causes & E1000_ICR_OTHER) {
  1715. e1000e_msix_clear_one(core, E1000_ICR_OTHER,
  1716. E1000_IVAR_OTHER(core->mac[IVAR]));
  1717. }
  1718. }
  1719. static inline void
  1720. e1000e_fix_icr_asserted(E1000ECore *core)
  1721. {
  1722. core->mac[ICR] &= ~E1000_ICR_ASSERTED;
  1723. if (core->mac[ICR]) {
  1724. core->mac[ICR] |= E1000_ICR_ASSERTED;
  1725. }
  1726. trace_e1000e_irq_fix_icr_asserted(core->mac[ICR]);
  1727. }
  1728. static void
  1729. e1000e_send_msi(E1000ECore *core, bool msix)
  1730. {
  1731. uint32_t causes = core->mac[ICR] & core->mac[IMS] & ~E1000_ICR_ASSERTED;
  1732. core->msi_causes_pending &= causes;
  1733. causes ^= core->msi_causes_pending;
  1734. if (causes == 0) {
  1735. return;
  1736. }
  1737. core->msi_causes_pending |= causes;
  1738. if (msix) {
  1739. e1000e_msix_notify(core, causes);
  1740. } else {
  1741. if (!e1000e_itr_should_postpone(core)) {
  1742. trace_e1000e_irq_msi_notify(causes);
  1743. msi_notify(core->owner, 0);
  1744. }
  1745. }
  1746. }
  1747. static void
  1748. e1000e_update_interrupt_state(E1000ECore *core)
  1749. {
  1750. bool interrupts_pending;
  1751. bool is_msix = msix_enabled(core->owner);
  1752. /* Set ICR[OTHER] for MSI-X */
  1753. if (is_msix) {
  1754. if (core->mac[ICR] & E1000_ICR_OTHER_CAUSES) {
  1755. core->mac[ICR] |= E1000_ICR_OTHER;
  1756. trace_e1000e_irq_add_msi_other(core->mac[ICR]);
  1757. }
  1758. }
  1759. e1000e_fix_icr_asserted(core);
  1760. /*
  1761. * Make sure ICR and ICS registers have the same value.
  1762. * The spec says that the ICS register is write-only. However in practice,
  1763. * on real hardware ICS is readable, and for reads it has the same value as
  1764. * ICR (except that ICS does not have the clear on read behaviour of ICR).
  1765. *
  1766. * The VxWorks PRO/1000 driver uses this behaviour.
  1767. */
  1768. core->mac[ICS] = core->mac[ICR];
  1769. interrupts_pending = (core->mac[IMS] & core->mac[ICR]) ? true : false;
  1770. if (!interrupts_pending) {
  1771. core->msi_causes_pending = 0;
  1772. }
  1773. trace_e1000e_irq_pending_interrupts(core->mac[ICR] & core->mac[IMS],
  1774. core->mac[ICR], core->mac[IMS]);
  1775. if (is_msix || msi_enabled(core->owner)) {
  1776. if (interrupts_pending) {
  1777. e1000e_send_msi(core, is_msix);
  1778. }
  1779. } else {
  1780. if (interrupts_pending) {
  1781. if (!e1000e_itr_should_postpone(core)) {
  1782. e1000e_raise_legacy_irq(core);
  1783. }
  1784. } else {
  1785. e1000e_lower_legacy_irq(core);
  1786. }
  1787. }
  1788. }
  1789. static void
  1790. e1000e_set_interrupt_cause(E1000ECore *core, uint32_t val)
  1791. {
  1792. trace_e1000e_irq_set_cause_entry(val, core->mac[ICR]);
  1793. val |= e1000e_intmgr_collect_delayed_causes(core);
  1794. core->mac[ICR] |= val;
  1795. trace_e1000e_irq_set_cause_exit(val, core->mac[ICR]);
  1796. e1000e_update_interrupt_state(core);
  1797. }
  1798. static inline void
  1799. e1000e_autoneg_timer(void *opaque)
  1800. {
  1801. E1000ECore *core = opaque;
  1802. if (!qemu_get_queue(core->owner_nic)->link_down) {
  1803. e1000x_update_regs_on_autoneg_done(core->mac, core->phy[0]);
  1804. e1000e_start_recv(core);
  1805. e1000e_update_flowctl_status(core);
  1806. /* signal link status change to the guest */
  1807. e1000e_set_interrupt_cause(core, E1000_ICR_LSC);
  1808. }
  1809. }
  1810. static inline uint16_t
  1811. e1000e_get_reg_index_with_offset(const uint16_t *mac_reg_access, hwaddr addr)
  1812. {
  1813. uint16_t index = (addr & 0x1ffff) >> 2;
  1814. return index + (mac_reg_access[index] & 0xfffe);
  1815. }
  1816. static const char e1000e_phy_regcap[E1000E_PHY_PAGES][0x20] = {
  1817. [0] = {
  1818. [PHY_CTRL] = PHY_ANYPAGE | PHY_RW,
  1819. [PHY_STATUS] = PHY_ANYPAGE | PHY_R,
  1820. [PHY_ID1] = PHY_ANYPAGE | PHY_R,
  1821. [PHY_ID2] = PHY_ANYPAGE | PHY_R,
  1822. [PHY_AUTONEG_ADV] = PHY_ANYPAGE | PHY_RW,
  1823. [PHY_LP_ABILITY] = PHY_ANYPAGE | PHY_R,
  1824. [PHY_AUTONEG_EXP] = PHY_ANYPAGE | PHY_R,
  1825. [PHY_NEXT_PAGE_TX] = PHY_ANYPAGE | PHY_RW,
  1826. [PHY_LP_NEXT_PAGE] = PHY_ANYPAGE | PHY_R,
  1827. [PHY_1000T_CTRL] = PHY_ANYPAGE | PHY_RW,
  1828. [PHY_1000T_STATUS] = PHY_ANYPAGE | PHY_R,
  1829. [PHY_EXT_STATUS] = PHY_ANYPAGE | PHY_R,
  1830. [PHY_PAGE] = PHY_ANYPAGE | PHY_RW,
  1831. [PHY_COPPER_CTRL1] = PHY_RW,
  1832. [PHY_COPPER_STAT1] = PHY_R,
  1833. [PHY_COPPER_CTRL3] = PHY_RW,
  1834. [PHY_RX_ERR_CNTR] = PHY_R,
  1835. [PHY_OEM_BITS] = PHY_RW,
  1836. [PHY_BIAS_1] = PHY_RW,
  1837. [PHY_BIAS_2] = PHY_RW,
  1838. [PHY_COPPER_INT_ENABLE] = PHY_RW,
  1839. [PHY_COPPER_STAT2] = PHY_R,
  1840. [PHY_COPPER_CTRL2] = PHY_RW
  1841. },
  1842. [2] = {
  1843. [PHY_MAC_CTRL1] = PHY_RW,
  1844. [PHY_MAC_INT_ENABLE] = PHY_RW,
  1845. [PHY_MAC_STAT] = PHY_R,
  1846. [PHY_MAC_CTRL2] = PHY_RW
  1847. },
  1848. [3] = {
  1849. [PHY_LED_03_FUNC_CTRL1] = PHY_RW,
  1850. [PHY_LED_03_POL_CTRL] = PHY_RW,
  1851. [PHY_LED_TIMER_CTRL] = PHY_RW,
  1852. [PHY_LED_45_CTRL] = PHY_RW
  1853. },
  1854. [5] = {
  1855. [PHY_1000T_SKEW] = PHY_R,
  1856. [PHY_1000T_SWAP] = PHY_R
  1857. },
  1858. [6] = {
  1859. [PHY_CRC_COUNTERS] = PHY_R
  1860. }
  1861. };
  1862. static bool
  1863. e1000e_phy_reg_check_cap(E1000ECore *core, uint32_t addr,
  1864. char cap, uint8_t *page)
  1865. {
  1866. *page =
  1867. (e1000e_phy_regcap[0][addr] & PHY_ANYPAGE) ? 0
  1868. : core->phy[0][PHY_PAGE];
  1869. if (*page >= E1000E_PHY_PAGES) {
  1870. return false;
  1871. }
  1872. return e1000e_phy_regcap[*page][addr] & cap;
  1873. }
  1874. static void
  1875. e1000e_phy_reg_write(E1000ECore *core, uint8_t page,
  1876. uint32_t addr, uint16_t data)
  1877. {
  1878. assert(page < E1000E_PHY_PAGES);
  1879. assert(addr < E1000E_PHY_PAGE_SIZE);
  1880. if (e1000e_phyreg_writeops[page][addr]) {
  1881. e1000e_phyreg_writeops[page][addr](core, addr, data);
  1882. } else {
  1883. core->phy[page][addr] = data;
  1884. }
  1885. }
  1886. static void
  1887. e1000e_set_mdic(E1000ECore *core, int index, uint32_t val)
  1888. {
  1889. uint32_t data = val & E1000_MDIC_DATA_MASK;
  1890. uint32_t addr = ((val & E1000_MDIC_REG_MASK) >> E1000_MDIC_REG_SHIFT);
  1891. uint8_t page;
  1892. if ((val & E1000_MDIC_PHY_MASK) >> E1000_MDIC_PHY_SHIFT != 1) { /* phy # */
  1893. val = core->mac[MDIC] | E1000_MDIC_ERROR;
  1894. } else if (val & E1000_MDIC_OP_READ) {
  1895. if (!e1000e_phy_reg_check_cap(core, addr, PHY_R, &page)) {
  1896. trace_e1000e_core_mdic_read_unhandled(page, addr);
  1897. val |= E1000_MDIC_ERROR;
  1898. } else {
  1899. val = (val ^ data) | core->phy[page][addr];
  1900. trace_e1000e_core_mdic_read(page, addr, val);
  1901. }
  1902. } else if (val & E1000_MDIC_OP_WRITE) {
  1903. if (!e1000e_phy_reg_check_cap(core, addr, PHY_W, &page)) {
  1904. trace_e1000e_core_mdic_write_unhandled(page, addr);
  1905. val |= E1000_MDIC_ERROR;
  1906. } else {
  1907. trace_e1000e_core_mdic_write(page, addr, data);
  1908. e1000e_phy_reg_write(core, page, addr, data);
  1909. }
  1910. }
  1911. core->mac[MDIC] = val | E1000_MDIC_READY;
  1912. if (val & E1000_MDIC_INT_EN) {
  1913. e1000e_set_interrupt_cause(core, E1000_ICR_MDAC);
  1914. }
  1915. }
  1916. static void
  1917. e1000e_set_rdt(E1000ECore *core, int index, uint32_t val)
  1918. {
  1919. core->mac[index] = val & 0xffff;
  1920. trace_e1000e_rx_set_rdt(e1000e_mq_queue_idx(RDT0, index), val);
  1921. e1000e_start_recv(core);
  1922. }
  1923. static void
  1924. e1000e_set_status(E1000ECore *core, int index, uint32_t val)
  1925. {
  1926. if ((val & E1000_STATUS_PHYRA) == 0) {
  1927. core->mac[index] &= ~E1000_STATUS_PHYRA;
  1928. }
  1929. }
  1930. static void
  1931. e1000e_set_ctrlext(E1000ECore *core, int index, uint32_t val)
  1932. {
  1933. trace_e1000e_link_set_ext_params(!!(val & E1000_CTRL_EXT_ASDCHK),
  1934. !!(val & E1000_CTRL_EXT_SPD_BYPS));
  1935. /* Zero self-clearing bits */
  1936. val &= ~(E1000_CTRL_EXT_ASDCHK | E1000_CTRL_EXT_EE_RST);
  1937. core->mac[CTRL_EXT] = val;
  1938. }
  1939. static void
  1940. e1000e_set_pbaclr(E1000ECore *core, int index, uint32_t val)
  1941. {
  1942. int i;
  1943. core->mac[PBACLR] = val & E1000_PBACLR_VALID_MASK;
  1944. if (!msix_enabled(core->owner)) {
  1945. return;
  1946. }
  1947. for (i = 0; i < E1000E_MSIX_VEC_NUM; i++) {
  1948. if (core->mac[PBACLR] & BIT(i)) {
  1949. msix_clr_pending(core->owner, i);
  1950. }
  1951. }
  1952. }
  1953. static void
  1954. e1000e_set_fcrth(E1000ECore *core, int index, uint32_t val)
  1955. {
  1956. core->mac[FCRTH] = val & 0xFFF8;
  1957. }
  1958. static void
  1959. e1000e_set_fcrtl(E1000ECore *core, int index, uint32_t val)
  1960. {
  1961. core->mac[FCRTL] = val & 0x8000FFF8;
  1962. }
  1963. static inline void
  1964. e1000e_set_16bit(E1000ECore *core, int index, uint32_t val)
  1965. {
  1966. core->mac[index] = val & 0xffff;
  1967. }
  1968. static void
  1969. e1000e_set_12bit(E1000ECore *core, int index, uint32_t val)
  1970. {
  1971. core->mac[index] = val & 0xfff;
  1972. }
  1973. static void
  1974. e1000e_set_vet(E1000ECore *core, int index, uint32_t val)
  1975. {
  1976. core->mac[VET] = val & 0xffff;
  1977. core->vet = le16_to_cpu(core->mac[VET]);
  1978. trace_e1000e_vlan_vet(core->vet);
  1979. }
  1980. static void
  1981. e1000e_set_dlen(E1000ECore *core, int index, uint32_t val)
  1982. {
  1983. core->mac[index] = val & E1000_XDLEN_MASK;
  1984. }
  1985. static void
  1986. e1000e_set_dbal(E1000ECore *core, int index, uint32_t val)
  1987. {
  1988. core->mac[index] = val & E1000_XDBAL_MASK;
  1989. }
  1990. static void
  1991. e1000e_set_tctl(E1000ECore *core, int index, uint32_t val)
  1992. {
  1993. E1000E_TxRing txr;
  1994. core->mac[index] = val;
  1995. if (core->mac[TARC0] & E1000_TARC_ENABLE) {
  1996. e1000e_tx_ring_init(core, &txr, 0);
  1997. e1000e_start_xmit(core, &txr);
  1998. }
  1999. if (core->mac[TARC1] & E1000_TARC_ENABLE) {
  2000. e1000e_tx_ring_init(core, &txr, 1);
  2001. e1000e_start_xmit(core, &txr);
  2002. }
  2003. }
  2004. static void
  2005. e1000e_set_tdt(E1000ECore *core, int index, uint32_t val)
  2006. {
  2007. E1000E_TxRing txr;
  2008. int qidx = e1000e_mq_queue_idx(TDT, index);
  2009. uint32_t tarc_reg = (qidx == 0) ? TARC0 : TARC1;
  2010. core->mac[index] = val & 0xffff;
  2011. if (core->mac[tarc_reg] & E1000_TARC_ENABLE) {
  2012. e1000e_tx_ring_init(core, &txr, qidx);
  2013. e1000e_start_xmit(core, &txr);
  2014. }
  2015. }
  2016. static void
  2017. e1000e_set_ics(E1000ECore *core, int index, uint32_t val)
  2018. {
  2019. trace_e1000e_irq_write_ics(val);
  2020. e1000e_set_interrupt_cause(core, val);
  2021. }
  2022. static void
  2023. e1000e_set_icr(E1000ECore *core, int index, uint32_t val)
  2024. {
  2025. uint32_t icr = 0;
  2026. if ((core->mac[ICR] & E1000_ICR_ASSERTED) &&
  2027. (core->mac[CTRL_EXT] & E1000_CTRL_EXT_IAME)) {
  2028. trace_e1000e_irq_icr_process_iame();
  2029. e1000e_clear_ims_bits(core, core->mac[IAM]);
  2030. }
  2031. icr = core->mac[ICR] & ~val;
  2032. /* Windows driver expects that the "receive overrun" bit and other
  2033. * ones to be cleared when the "Other" bit (#24) is cleared.
  2034. */
  2035. icr = (val & E1000_ICR_OTHER) ? (icr & ~E1000_ICR_OTHER_CAUSES) : icr;
  2036. trace_e1000e_irq_icr_write(val, core->mac[ICR], icr);
  2037. core->mac[ICR] = icr;
  2038. e1000e_update_interrupt_state(core);
  2039. }
  2040. static void
  2041. e1000e_set_imc(E1000ECore *core, int index, uint32_t val)
  2042. {
  2043. trace_e1000e_irq_ims_clear_set_imc(val);
  2044. e1000e_clear_ims_bits(core, val);
  2045. e1000e_update_interrupt_state(core);
  2046. }
  2047. static void
  2048. e1000e_set_ims(E1000ECore *core, int index, uint32_t val)
  2049. {
  2050. static const uint32_t ims_ext_mask =
  2051. E1000_IMS_RXQ0 | E1000_IMS_RXQ1 |
  2052. E1000_IMS_TXQ0 | E1000_IMS_TXQ1 |
  2053. E1000_IMS_OTHER;
  2054. static const uint32_t ims_valid_mask =
  2055. E1000_IMS_TXDW | E1000_IMS_TXQE | E1000_IMS_LSC |
  2056. E1000_IMS_RXDMT0 | E1000_IMS_RXO | E1000_IMS_RXT0 |
  2057. E1000_IMS_MDAC | E1000_IMS_TXD_LOW | E1000_IMS_SRPD |
  2058. E1000_IMS_ACK | E1000_IMS_MNG | E1000_IMS_RXQ0 |
  2059. E1000_IMS_RXQ1 | E1000_IMS_TXQ0 | E1000_IMS_TXQ1 |
  2060. E1000_IMS_OTHER;
  2061. uint32_t valid_val = val & ims_valid_mask;
  2062. trace_e1000e_irq_set_ims(val, core->mac[IMS], core->mac[IMS] | valid_val);
  2063. core->mac[IMS] |= valid_val;
  2064. if ((valid_val & ims_ext_mask) &&
  2065. (core->mac[CTRL_EXT] & E1000_CTRL_EXT_PBA_CLR) &&
  2066. msix_enabled(core->owner)) {
  2067. e1000e_msix_clear(core, valid_val);
  2068. }
  2069. if ((valid_val == ims_valid_mask) &&
  2070. (core->mac[CTRL_EXT] & E1000_CTRL_EXT_INT_TIMERS_CLEAR_ENA)) {
  2071. trace_e1000e_irq_fire_all_timers(val);
  2072. e1000e_intrmgr_fire_all_timers(core);
  2073. }
  2074. e1000e_update_interrupt_state(core);
  2075. }
  2076. static void
  2077. e1000e_set_rdtr(E1000ECore *core, int index, uint32_t val)
  2078. {
  2079. e1000e_set_16bit(core, index, val);
  2080. if ((val & E1000_RDTR_FPD) && (core->rdtr.running)) {
  2081. trace_e1000e_irq_rdtr_fpd_running();
  2082. e1000e_intrmgr_fire_delayed_interrupts(core);
  2083. } else {
  2084. trace_e1000e_irq_rdtr_fpd_not_running();
  2085. }
  2086. }
  2087. static void
  2088. e1000e_set_tidv(E1000ECore *core, int index, uint32_t val)
  2089. {
  2090. e1000e_set_16bit(core, index, val);
  2091. if ((val & E1000_TIDV_FPD) && (core->tidv.running)) {
  2092. trace_e1000e_irq_tidv_fpd_running();
  2093. e1000e_intrmgr_fire_delayed_interrupts(core);
  2094. } else {
  2095. trace_e1000e_irq_tidv_fpd_not_running();
  2096. }
  2097. }
  2098. static uint32_t
  2099. e1000e_mac_readreg(E1000ECore *core, int index)
  2100. {
  2101. return core->mac[index];
  2102. }
  2103. static uint32_t
  2104. e1000e_mac_ics_read(E1000ECore *core, int index)
  2105. {
  2106. trace_e1000e_irq_read_ics(core->mac[ICS]);
  2107. return core->mac[ICS];
  2108. }
  2109. static uint32_t
  2110. e1000e_mac_ims_read(E1000ECore *core, int index)
  2111. {
  2112. trace_e1000e_irq_read_ims(core->mac[IMS]);
  2113. return core->mac[IMS];
  2114. }
  2115. #define E1000E_LOW_BITS_READ_FUNC(num) \
  2116. static uint32_t \
  2117. e1000e_mac_low##num##_read(E1000ECore *core, int index) \
  2118. { \
  2119. return core->mac[index] & (BIT(num) - 1); \
  2120. } \
  2121. #define E1000E_LOW_BITS_READ(num) \
  2122. e1000e_mac_low##num##_read
  2123. E1000E_LOW_BITS_READ_FUNC(4);
  2124. E1000E_LOW_BITS_READ_FUNC(6);
  2125. E1000E_LOW_BITS_READ_FUNC(11);
  2126. E1000E_LOW_BITS_READ_FUNC(13);
  2127. E1000E_LOW_BITS_READ_FUNC(16);
  2128. static uint32_t
  2129. e1000e_mac_swsm_read(E1000ECore *core, int index)
  2130. {
  2131. uint32_t val = core->mac[SWSM];
  2132. core->mac[SWSM] = val | 1;
  2133. return val;
  2134. }
  2135. static uint32_t
  2136. e1000e_mac_itr_read(E1000ECore *core, int index)
  2137. {
  2138. return core->itr_guest_value;
  2139. }
  2140. static uint32_t
  2141. e1000e_mac_eitr_read(E1000ECore *core, int index)
  2142. {
  2143. return core->eitr_guest_value[index - EITR];
  2144. }
  2145. static uint32_t
  2146. e1000e_mac_icr_read(E1000ECore *core, int index)
  2147. {
  2148. uint32_t ret = core->mac[ICR];
  2149. trace_e1000e_irq_icr_read_entry(ret);
  2150. if (core->mac[IMS] == 0) {
  2151. trace_e1000e_irq_icr_clear_zero_ims();
  2152. core->mac[ICR] = 0;
  2153. }
  2154. if ((core->mac[ICR] & E1000_ICR_ASSERTED) &&
  2155. (core->mac[CTRL_EXT] & E1000_CTRL_EXT_IAME)) {
  2156. trace_e1000e_irq_icr_clear_iame();
  2157. core->mac[ICR] = 0;
  2158. trace_e1000e_irq_icr_process_iame();
  2159. e1000e_clear_ims_bits(core, core->mac[IAM]);
  2160. }
  2161. trace_e1000e_irq_icr_read_exit(core->mac[ICR]);
  2162. e1000e_update_interrupt_state(core);
  2163. return ret;
  2164. }
  2165. static uint32_t
  2166. e1000e_mac_read_clr4(E1000ECore *core, int index)
  2167. {
  2168. uint32_t ret = core->mac[index];
  2169. core->mac[index] = 0;
  2170. return ret;
  2171. }
  2172. static uint32_t
  2173. e1000e_mac_read_clr8(E1000ECore *core, int index)
  2174. {
  2175. uint32_t ret = core->mac[index];
  2176. core->mac[index] = 0;
  2177. core->mac[index - 1] = 0;
  2178. return ret;
  2179. }
  2180. static uint32_t
  2181. e1000e_get_ctrl(E1000ECore *core, int index)
  2182. {
  2183. uint32_t val = core->mac[CTRL];
  2184. trace_e1000e_link_read_params(
  2185. !!(val & E1000_CTRL_ASDE),
  2186. (val & E1000_CTRL_SPD_SEL) >> E1000_CTRL_SPD_SHIFT,
  2187. !!(val & E1000_CTRL_FRCSPD),
  2188. !!(val & E1000_CTRL_FRCDPX),
  2189. !!(val & E1000_CTRL_RFCE),
  2190. !!(val & E1000_CTRL_TFCE));
  2191. return val;
  2192. }
  2193. static uint32_t
  2194. e1000e_get_status(E1000ECore *core, int index)
  2195. {
  2196. uint32_t res = core->mac[STATUS];
  2197. if (!(core->mac[CTRL] & E1000_CTRL_GIO_MASTER_DISABLE)) {
  2198. res |= E1000_STATUS_GIO_MASTER_ENABLE;
  2199. }
  2200. if (core->mac[CTRL] & E1000_CTRL_FRCDPX) {
  2201. res |= (core->mac[CTRL] & E1000_CTRL_FD) ? E1000_STATUS_FD : 0;
  2202. } else {
  2203. res |= E1000_STATUS_FD;
  2204. }
  2205. if ((core->mac[CTRL] & E1000_CTRL_FRCSPD) ||
  2206. (core->mac[CTRL_EXT] & E1000_CTRL_EXT_SPD_BYPS)) {
  2207. switch (core->mac[CTRL] & E1000_CTRL_SPD_SEL) {
  2208. case E1000_CTRL_SPD_10:
  2209. res |= E1000_STATUS_SPEED_10;
  2210. break;
  2211. case E1000_CTRL_SPD_100:
  2212. res |= E1000_STATUS_SPEED_100;
  2213. break;
  2214. case E1000_CTRL_SPD_1000:
  2215. default:
  2216. res |= E1000_STATUS_SPEED_1000;
  2217. break;
  2218. }
  2219. } else {
  2220. res |= E1000_STATUS_SPEED_1000;
  2221. }
  2222. trace_e1000e_link_status(
  2223. !!(res & E1000_STATUS_LU),
  2224. !!(res & E1000_STATUS_FD),
  2225. (res & E1000_STATUS_SPEED_MASK) >> E1000_STATUS_SPEED_SHIFT,
  2226. (res & E1000_STATUS_ASDV) >> E1000_STATUS_ASDV_SHIFT);
  2227. return res;
  2228. }
  2229. static uint32_t
  2230. e1000e_get_tarc(E1000ECore *core, int index)
  2231. {
  2232. return core->mac[index] & ((BIT(11) - 1) |
  2233. BIT(27) |
  2234. BIT(28) |
  2235. BIT(29) |
  2236. BIT(30));
  2237. }
  2238. static void
  2239. e1000e_mac_writereg(E1000ECore *core, int index, uint32_t val)
  2240. {
  2241. core->mac[index] = val;
  2242. }
  2243. static void
  2244. e1000e_mac_setmacaddr(E1000ECore *core, int index, uint32_t val)
  2245. {
  2246. uint32_t macaddr[2];
  2247. core->mac[index] = val;
  2248. macaddr[0] = cpu_to_le32(core->mac[RA]);
  2249. macaddr[1] = cpu_to_le32(core->mac[RA + 1]);
  2250. qemu_format_nic_info_str(qemu_get_queue(core->owner_nic),
  2251. (uint8_t *) macaddr);
  2252. trace_e1000e_mac_set_sw(MAC_ARG(macaddr));
  2253. }
  2254. static void
  2255. e1000e_set_eecd(E1000ECore *core, int index, uint32_t val)
  2256. {
  2257. static const uint32_t ro_bits = E1000_EECD_PRES |
  2258. E1000_EECD_AUTO_RD |
  2259. E1000_EECD_SIZE_EX_MASK;
  2260. core->mac[EECD] = (core->mac[EECD] & ro_bits) | (val & ~ro_bits);
  2261. }
  2262. static void
  2263. e1000e_set_eerd(E1000ECore *core, int index, uint32_t val)
  2264. {
  2265. uint32_t addr = (val >> E1000_EERW_ADDR_SHIFT) & E1000_EERW_ADDR_MASK;
  2266. uint32_t flags = 0;
  2267. uint32_t data = 0;
  2268. if ((addr < E1000E_EEPROM_SIZE) && (val & E1000_EERW_START)) {
  2269. data = core->eeprom[addr];
  2270. flags = E1000_EERW_DONE;
  2271. }
  2272. core->mac[EERD] = flags |
  2273. (addr << E1000_EERW_ADDR_SHIFT) |
  2274. (data << E1000_EERW_DATA_SHIFT);
  2275. }
  2276. static void
  2277. e1000e_set_eewr(E1000ECore *core, int index, uint32_t val)
  2278. {
  2279. uint32_t addr = (val >> E1000_EERW_ADDR_SHIFT) & E1000_EERW_ADDR_MASK;
  2280. uint32_t data = (val >> E1000_EERW_DATA_SHIFT) & E1000_EERW_DATA_MASK;
  2281. uint32_t flags = 0;
  2282. if ((addr < E1000E_EEPROM_SIZE) && (val & E1000_EERW_START)) {
  2283. core->eeprom[addr] = data;
  2284. flags = E1000_EERW_DONE;
  2285. }
  2286. core->mac[EERD] = flags |
  2287. (addr << E1000_EERW_ADDR_SHIFT) |
  2288. (data << E1000_EERW_DATA_SHIFT);
  2289. }
  2290. static void
  2291. e1000e_set_rxdctl(E1000ECore *core, int index, uint32_t val)
  2292. {
  2293. core->mac[RXDCTL] = core->mac[RXDCTL1] = val;
  2294. }
  2295. static void
  2296. e1000e_set_itr(E1000ECore *core, int index, uint32_t val)
  2297. {
  2298. uint32_t interval = val & 0xffff;
  2299. trace_e1000e_irq_itr_set(val);
  2300. core->itr_guest_value = interval;
  2301. core->mac[index] = MAX(interval, E1000E_MIN_XITR);
  2302. }
  2303. static void
  2304. e1000e_set_eitr(E1000ECore *core, int index, uint32_t val)
  2305. {
  2306. uint32_t interval = val & 0xffff;
  2307. uint32_t eitr_num = index - EITR;
  2308. trace_e1000e_irq_eitr_set(eitr_num, val);
  2309. core->eitr_guest_value[eitr_num] = interval;
  2310. core->mac[index] = MAX(interval, E1000E_MIN_XITR);
  2311. }
  2312. static void
  2313. e1000e_set_psrctl(E1000ECore *core, int index, uint32_t val)
  2314. {
  2315. if ((val & E1000_PSRCTL_BSIZE0_MASK) == 0) {
  2316. hw_error("e1000e: PSRCTL.BSIZE0 cannot be zero");
  2317. }
  2318. if ((val & E1000_PSRCTL_BSIZE1_MASK) == 0) {
  2319. hw_error("e1000e: PSRCTL.BSIZE1 cannot be zero");
  2320. }
  2321. core->mac[PSRCTL] = val;
  2322. }
  2323. static void
  2324. e1000e_update_rx_offloads(E1000ECore *core)
  2325. {
  2326. int cso_state = e1000e_rx_l4_cso_enabled(core);
  2327. trace_e1000e_rx_set_cso(cso_state);
  2328. if (core->has_vnet) {
  2329. qemu_set_offload(qemu_get_queue(core->owner_nic)->peer,
  2330. cso_state, 0, 0, 0, 0);
  2331. }
  2332. }
  2333. static void
  2334. e1000e_set_rxcsum(E1000ECore *core, int index, uint32_t val)
  2335. {
  2336. core->mac[RXCSUM] = val;
  2337. e1000e_update_rx_offloads(core);
  2338. }
  2339. static void
  2340. e1000e_set_gcr(E1000ECore *core, int index, uint32_t val)
  2341. {
  2342. uint32_t ro_bits = core->mac[GCR] & E1000_GCR_RO_BITS;
  2343. core->mac[GCR] = (val & ~E1000_GCR_RO_BITS) | ro_bits;
  2344. }
  2345. #define e1000e_getreg(x) [x] = e1000e_mac_readreg
  2346. static uint32_t (*e1000e_macreg_readops[])(E1000ECore *, int) = {
  2347. e1000e_getreg(PBA),
  2348. e1000e_getreg(WUFC),
  2349. e1000e_getreg(MANC),
  2350. e1000e_getreg(TOTL),
  2351. e1000e_getreg(RDT0),
  2352. e1000e_getreg(RDBAH0),
  2353. e1000e_getreg(TDBAL1),
  2354. e1000e_getreg(RDLEN0),
  2355. e1000e_getreg(RDH1),
  2356. e1000e_getreg(LATECOL),
  2357. e1000e_getreg(SEQEC),
  2358. e1000e_getreg(XONTXC),
  2359. e1000e_getreg(WUS),
  2360. e1000e_getreg(GORCL),
  2361. e1000e_getreg(MGTPRC),
  2362. e1000e_getreg(EERD),
  2363. e1000e_getreg(EIAC),
  2364. e1000e_getreg(PSRCTL),
  2365. e1000e_getreg(MANC2H),
  2366. e1000e_getreg(RXCSUM),
  2367. e1000e_getreg(GSCL_3),
  2368. e1000e_getreg(GSCN_2),
  2369. e1000e_getreg(RSRPD),
  2370. e1000e_getreg(RDBAL1),
  2371. e1000e_getreg(FCAH),
  2372. e1000e_getreg(FCRTH),
  2373. e1000e_getreg(FLOP),
  2374. e1000e_getreg(FLASHT),
  2375. e1000e_getreg(RXSTMPH),
  2376. e1000e_getreg(TXSTMPL),
  2377. e1000e_getreg(TIMADJL),
  2378. e1000e_getreg(TXDCTL),
  2379. e1000e_getreg(RDH0),
  2380. e1000e_getreg(TDT1),
  2381. e1000e_getreg(TNCRS),
  2382. e1000e_getreg(RJC),
  2383. e1000e_getreg(IAM),
  2384. e1000e_getreg(GSCL_2),
  2385. e1000e_getreg(RDBAH1),
  2386. e1000e_getreg(FLSWDATA),
  2387. e1000e_getreg(RXSATRH),
  2388. e1000e_getreg(TIPG),
  2389. e1000e_getreg(FLMNGCTL),
  2390. e1000e_getreg(FLMNGCNT),
  2391. e1000e_getreg(TSYNCTXCTL),
  2392. e1000e_getreg(EXTCNF_SIZE),
  2393. e1000e_getreg(EXTCNF_CTRL),
  2394. e1000e_getreg(EEMNGDATA),
  2395. e1000e_getreg(CTRL_EXT),
  2396. e1000e_getreg(SYSTIMH),
  2397. e1000e_getreg(EEMNGCTL),
  2398. e1000e_getreg(FLMNGDATA),
  2399. e1000e_getreg(TSYNCRXCTL),
  2400. e1000e_getreg(TDH),
  2401. e1000e_getreg(LEDCTL),
  2402. e1000e_getreg(STATUS),
  2403. e1000e_getreg(TCTL),
  2404. e1000e_getreg(TDBAL),
  2405. e1000e_getreg(TDLEN),
  2406. e1000e_getreg(TDH1),
  2407. e1000e_getreg(RADV),
  2408. e1000e_getreg(ECOL),
  2409. e1000e_getreg(DC),
  2410. e1000e_getreg(RLEC),
  2411. e1000e_getreg(XOFFTXC),
  2412. e1000e_getreg(RFC),
  2413. e1000e_getreg(RNBC),
  2414. e1000e_getreg(MGTPTC),
  2415. e1000e_getreg(TIMINCA),
  2416. e1000e_getreg(RXCFGL),
  2417. e1000e_getreg(MFUTP01),
  2418. e1000e_getreg(FACTPS),
  2419. e1000e_getreg(GSCL_1),
  2420. e1000e_getreg(GSCN_0),
  2421. e1000e_getreg(GCR2),
  2422. e1000e_getreg(RDT1),
  2423. e1000e_getreg(PBACLR),
  2424. e1000e_getreg(FCTTV),
  2425. e1000e_getreg(EEWR),
  2426. e1000e_getreg(FLSWCTL),
  2427. e1000e_getreg(RXDCTL1),
  2428. e1000e_getreg(RXSATRL),
  2429. e1000e_getreg(SYSTIML),
  2430. e1000e_getreg(RXUDP),
  2431. e1000e_getreg(TORL),
  2432. e1000e_getreg(TDLEN1),
  2433. e1000e_getreg(MCC),
  2434. e1000e_getreg(WUC),
  2435. e1000e_getreg(EECD),
  2436. e1000e_getreg(MFUTP23),
  2437. e1000e_getreg(RAID),
  2438. e1000e_getreg(FCRTV),
  2439. e1000e_getreg(TXDCTL1),
  2440. e1000e_getreg(RCTL),
  2441. e1000e_getreg(TDT),
  2442. e1000e_getreg(MDIC),
  2443. e1000e_getreg(FCRUC),
  2444. e1000e_getreg(VET),
  2445. e1000e_getreg(RDBAL0),
  2446. e1000e_getreg(TDBAH1),
  2447. e1000e_getreg(RDTR),
  2448. e1000e_getreg(SCC),
  2449. e1000e_getreg(COLC),
  2450. e1000e_getreg(CEXTERR),
  2451. e1000e_getreg(XOFFRXC),
  2452. e1000e_getreg(IPAV),
  2453. e1000e_getreg(GOTCL),
  2454. e1000e_getreg(MGTPDC),
  2455. e1000e_getreg(GCR),
  2456. e1000e_getreg(IVAR),
  2457. e1000e_getreg(POEMB),
  2458. e1000e_getreg(MFVAL),
  2459. e1000e_getreg(FUNCTAG),
  2460. e1000e_getreg(GSCL_4),
  2461. e1000e_getreg(GSCN_3),
  2462. e1000e_getreg(MRQC),
  2463. e1000e_getreg(RDLEN1),
  2464. e1000e_getreg(FCT),
  2465. e1000e_getreg(FLA),
  2466. e1000e_getreg(FLOL),
  2467. e1000e_getreg(RXDCTL),
  2468. e1000e_getreg(RXSTMPL),
  2469. e1000e_getreg(TXSTMPH),
  2470. e1000e_getreg(TIMADJH),
  2471. e1000e_getreg(FCRTL),
  2472. e1000e_getreg(TDBAH),
  2473. e1000e_getreg(TADV),
  2474. e1000e_getreg(XONRXC),
  2475. e1000e_getreg(TSCTFC),
  2476. e1000e_getreg(RFCTL),
  2477. e1000e_getreg(GSCN_1),
  2478. e1000e_getreg(FCAL),
  2479. e1000e_getreg(FLSWCNT),
  2480. [TOTH] = e1000e_mac_read_clr8,
  2481. [GOTCH] = e1000e_mac_read_clr8,
  2482. [PRC64] = e1000e_mac_read_clr4,
  2483. [PRC255] = e1000e_mac_read_clr4,
  2484. [PRC1023] = e1000e_mac_read_clr4,
  2485. [PTC64] = e1000e_mac_read_clr4,
  2486. [PTC255] = e1000e_mac_read_clr4,
  2487. [PTC1023] = e1000e_mac_read_clr4,
  2488. [GPRC] = e1000e_mac_read_clr4,
  2489. [TPT] = e1000e_mac_read_clr4,
  2490. [RUC] = e1000e_mac_read_clr4,
  2491. [BPRC] = e1000e_mac_read_clr4,
  2492. [MPTC] = e1000e_mac_read_clr4,
  2493. [IAC] = e1000e_mac_read_clr4,
  2494. [ICR] = e1000e_mac_icr_read,
  2495. [RDFH] = E1000E_LOW_BITS_READ(13),
  2496. [RDFHS] = E1000E_LOW_BITS_READ(13),
  2497. [RDFPC] = E1000E_LOW_BITS_READ(13),
  2498. [TDFH] = E1000E_LOW_BITS_READ(13),
  2499. [TDFHS] = E1000E_LOW_BITS_READ(13),
  2500. [STATUS] = e1000e_get_status,
  2501. [TARC0] = e1000e_get_tarc,
  2502. [PBS] = E1000E_LOW_BITS_READ(6),
  2503. [ICS] = e1000e_mac_ics_read,
  2504. [AIT] = E1000E_LOW_BITS_READ(16),
  2505. [TORH] = e1000e_mac_read_clr8,
  2506. [GORCH] = e1000e_mac_read_clr8,
  2507. [PRC127] = e1000e_mac_read_clr4,
  2508. [PRC511] = e1000e_mac_read_clr4,
  2509. [PRC1522] = e1000e_mac_read_clr4,
  2510. [PTC127] = e1000e_mac_read_clr4,
  2511. [PTC511] = e1000e_mac_read_clr4,
  2512. [PTC1522] = e1000e_mac_read_clr4,
  2513. [GPTC] = e1000e_mac_read_clr4,
  2514. [TPR] = e1000e_mac_read_clr4,
  2515. [ROC] = e1000e_mac_read_clr4,
  2516. [MPRC] = e1000e_mac_read_clr4,
  2517. [BPTC] = e1000e_mac_read_clr4,
  2518. [TSCTC] = e1000e_mac_read_clr4,
  2519. [ITR] = e1000e_mac_itr_read,
  2520. [RDFT] = E1000E_LOW_BITS_READ(13),
  2521. [RDFTS] = E1000E_LOW_BITS_READ(13),
  2522. [TDFPC] = E1000E_LOW_BITS_READ(13),
  2523. [TDFT] = E1000E_LOW_BITS_READ(13),
  2524. [TDFTS] = E1000E_LOW_BITS_READ(13),
  2525. [CTRL] = e1000e_get_ctrl,
  2526. [TARC1] = e1000e_get_tarc,
  2527. [SWSM] = e1000e_mac_swsm_read,
  2528. [IMS] = e1000e_mac_ims_read,
  2529. [CRCERRS ... MPC] = e1000e_mac_readreg,
  2530. [IP6AT ... IP6AT + 3] = e1000e_mac_readreg,
  2531. [IP4AT ... IP4AT + 6] = e1000e_mac_readreg,
  2532. [RA ... RA + 31] = e1000e_mac_readreg,
  2533. [WUPM ... WUPM + 31] = e1000e_mac_readreg,
  2534. [MTA ... MTA + 127] = e1000e_mac_readreg,
  2535. [VFTA ... VFTA + 127] = e1000e_mac_readreg,
  2536. [FFMT ... FFMT + 254] = E1000E_LOW_BITS_READ(4),
  2537. [FFVT ... FFVT + 254] = e1000e_mac_readreg,
  2538. [MDEF ... MDEF + 7] = e1000e_mac_readreg,
  2539. [FFLT ... FFLT + 10] = E1000E_LOW_BITS_READ(11),
  2540. [FTFT ... FTFT + 254] = e1000e_mac_readreg,
  2541. [PBM ... PBM + 10239] = e1000e_mac_readreg,
  2542. [RETA ... RETA + 31] = e1000e_mac_readreg,
  2543. [RSSRK ... RSSRK + 31] = e1000e_mac_readreg,
  2544. [MAVTV0 ... MAVTV3] = e1000e_mac_readreg,
  2545. [EITR...EITR + E1000E_MSIX_VEC_NUM - 1] = e1000e_mac_eitr_read
  2546. };
  2547. enum { E1000E_NREADOPS = ARRAY_SIZE(e1000e_macreg_readops) };
  2548. #define e1000e_putreg(x) [x] = e1000e_mac_writereg
  2549. static void (*e1000e_macreg_writeops[])(E1000ECore *, int, uint32_t) = {
  2550. e1000e_putreg(PBA),
  2551. e1000e_putreg(SWSM),
  2552. e1000e_putreg(WUFC),
  2553. e1000e_putreg(RDBAH1),
  2554. e1000e_putreg(TDBAH),
  2555. e1000e_putreg(TXDCTL),
  2556. e1000e_putreg(RDBAH0),
  2557. e1000e_putreg(LEDCTL),
  2558. e1000e_putreg(FCAL),
  2559. e1000e_putreg(FCRUC),
  2560. e1000e_putreg(AIT),
  2561. e1000e_putreg(TDFH),
  2562. e1000e_putreg(TDFT),
  2563. e1000e_putreg(TDFHS),
  2564. e1000e_putreg(TDFTS),
  2565. e1000e_putreg(TDFPC),
  2566. e1000e_putreg(WUC),
  2567. e1000e_putreg(WUS),
  2568. e1000e_putreg(RDFH),
  2569. e1000e_putreg(RDFT),
  2570. e1000e_putreg(RDFHS),
  2571. e1000e_putreg(RDFTS),
  2572. e1000e_putreg(RDFPC),
  2573. e1000e_putreg(IPAV),
  2574. e1000e_putreg(TDBAH1),
  2575. e1000e_putreg(TIMINCA),
  2576. e1000e_putreg(IAM),
  2577. e1000e_putreg(EIAC),
  2578. e1000e_putreg(IVAR),
  2579. e1000e_putreg(TARC0),
  2580. e1000e_putreg(TARC1),
  2581. e1000e_putreg(FLSWDATA),
  2582. e1000e_putreg(POEMB),
  2583. e1000e_putreg(PBS),
  2584. e1000e_putreg(MFUTP01),
  2585. e1000e_putreg(MFUTP23),
  2586. e1000e_putreg(MANC),
  2587. e1000e_putreg(MANC2H),
  2588. e1000e_putreg(MFVAL),
  2589. e1000e_putreg(EXTCNF_CTRL),
  2590. e1000e_putreg(FACTPS),
  2591. e1000e_putreg(FUNCTAG),
  2592. e1000e_putreg(GSCL_1),
  2593. e1000e_putreg(GSCL_2),
  2594. e1000e_putreg(GSCL_3),
  2595. e1000e_putreg(GSCL_4),
  2596. e1000e_putreg(GSCN_0),
  2597. e1000e_putreg(GSCN_1),
  2598. e1000e_putreg(GSCN_2),
  2599. e1000e_putreg(GSCN_3),
  2600. e1000e_putreg(GCR2),
  2601. e1000e_putreg(MRQC),
  2602. e1000e_putreg(FLOP),
  2603. e1000e_putreg(FLOL),
  2604. e1000e_putreg(FLSWCTL),
  2605. e1000e_putreg(FLSWCNT),
  2606. e1000e_putreg(FLA),
  2607. e1000e_putreg(RXDCTL1),
  2608. e1000e_putreg(TXDCTL1),
  2609. e1000e_putreg(TIPG),
  2610. e1000e_putreg(RXSTMPH),
  2611. e1000e_putreg(RXSTMPL),
  2612. e1000e_putreg(RXSATRL),
  2613. e1000e_putreg(RXSATRH),
  2614. e1000e_putreg(TXSTMPL),
  2615. e1000e_putreg(TXSTMPH),
  2616. e1000e_putreg(SYSTIML),
  2617. e1000e_putreg(SYSTIMH),
  2618. e1000e_putreg(TIMADJL),
  2619. e1000e_putreg(TIMADJH),
  2620. e1000e_putreg(RXUDP),
  2621. e1000e_putreg(RXCFGL),
  2622. e1000e_putreg(TSYNCRXCTL),
  2623. e1000e_putreg(TSYNCTXCTL),
  2624. e1000e_putreg(FLSWDATA),
  2625. e1000e_putreg(EXTCNF_SIZE),
  2626. e1000e_putreg(EEMNGCTL),
  2627. e1000e_putreg(RA),
  2628. [TDH1] = e1000e_set_16bit,
  2629. [TDT1] = e1000e_set_tdt,
  2630. [TCTL] = e1000e_set_tctl,
  2631. [TDT] = e1000e_set_tdt,
  2632. [MDIC] = e1000e_set_mdic,
  2633. [ICS] = e1000e_set_ics,
  2634. [TDH] = e1000e_set_16bit,
  2635. [RDH0] = e1000e_set_16bit,
  2636. [RDT0] = e1000e_set_rdt,
  2637. [IMC] = e1000e_set_imc,
  2638. [IMS] = e1000e_set_ims,
  2639. [ICR] = e1000e_set_icr,
  2640. [EECD] = e1000e_set_eecd,
  2641. [RCTL] = e1000e_set_rx_control,
  2642. [CTRL] = e1000e_set_ctrl,
  2643. [RDTR] = e1000e_set_rdtr,
  2644. [RADV] = e1000e_set_16bit,
  2645. [TADV] = e1000e_set_16bit,
  2646. [ITR] = e1000e_set_itr,
  2647. [EERD] = e1000e_set_eerd,
  2648. [GCR] = e1000e_set_gcr,
  2649. [PSRCTL] = e1000e_set_psrctl,
  2650. [RXCSUM] = e1000e_set_rxcsum,
  2651. [RAID] = e1000e_set_16bit,
  2652. [RSRPD] = e1000e_set_12bit,
  2653. [TIDV] = e1000e_set_tidv,
  2654. [TDLEN1] = e1000e_set_dlen,
  2655. [TDLEN] = e1000e_set_dlen,
  2656. [RDLEN0] = e1000e_set_dlen,
  2657. [RDLEN1] = e1000e_set_dlen,
  2658. [TDBAL] = e1000e_set_dbal,
  2659. [TDBAL1] = e1000e_set_dbal,
  2660. [RDBAL0] = e1000e_set_dbal,
  2661. [RDBAL1] = e1000e_set_dbal,
  2662. [RDH1] = e1000e_set_16bit,
  2663. [RDT1] = e1000e_set_rdt,
  2664. [STATUS] = e1000e_set_status,
  2665. [PBACLR] = e1000e_set_pbaclr,
  2666. [CTRL_EXT] = e1000e_set_ctrlext,
  2667. [FCAH] = e1000e_set_16bit,
  2668. [FCT] = e1000e_set_16bit,
  2669. [FCTTV] = e1000e_set_16bit,
  2670. [FCRTV] = e1000e_set_16bit,
  2671. [FCRTH] = e1000e_set_fcrth,
  2672. [FCRTL] = e1000e_set_fcrtl,
  2673. [VET] = e1000e_set_vet,
  2674. [RXDCTL] = e1000e_set_rxdctl,
  2675. [FLASHT] = e1000e_set_16bit,
  2676. [EEWR] = e1000e_set_eewr,
  2677. [CTRL_DUP] = e1000e_set_ctrl,
  2678. [RFCTL] = e1000e_set_rfctl,
  2679. [RA + 1] = e1000e_mac_setmacaddr,
  2680. [IP6AT ... IP6AT + 3] = e1000e_mac_writereg,
  2681. [IP4AT ... IP4AT + 6] = e1000e_mac_writereg,
  2682. [RA + 2 ... RA + 31] = e1000e_mac_writereg,
  2683. [WUPM ... WUPM + 31] = e1000e_mac_writereg,
  2684. [MTA ... MTA + 127] = e1000e_mac_writereg,
  2685. [VFTA ... VFTA + 127] = e1000e_mac_writereg,
  2686. [FFMT ... FFMT + 254] = e1000e_mac_writereg,
  2687. [FFVT ... FFVT + 254] = e1000e_mac_writereg,
  2688. [PBM ... PBM + 10239] = e1000e_mac_writereg,
  2689. [MDEF ... MDEF + 7] = e1000e_mac_writereg,
  2690. [FFLT ... FFLT + 10] = e1000e_mac_writereg,
  2691. [FTFT ... FTFT + 254] = e1000e_mac_writereg,
  2692. [RETA ... RETA + 31] = e1000e_mac_writereg,
  2693. [RSSRK ... RSSRK + 31] = e1000e_mac_writereg,
  2694. [MAVTV0 ... MAVTV3] = e1000e_mac_writereg,
  2695. [EITR...EITR + E1000E_MSIX_VEC_NUM - 1] = e1000e_set_eitr
  2696. };
  2697. enum { E1000E_NWRITEOPS = ARRAY_SIZE(e1000e_macreg_writeops) };
  2698. enum { MAC_ACCESS_PARTIAL = 1 };
  2699. /* The array below combines alias offsets of the index values for the
  2700. * MAC registers that have aliases, with the indication of not fully
  2701. * implemented registers (lowest bit). This combination is possible
  2702. * because all of the offsets are even. */
  2703. static const uint16_t mac_reg_access[E1000E_MAC_SIZE] = {
  2704. /* Alias index offsets */
  2705. [FCRTL_A] = 0x07fe, [FCRTH_A] = 0x0802,
  2706. [RDH0_A] = 0x09bc, [RDT0_A] = 0x09bc, [RDTR_A] = 0x09c6,
  2707. [RDFH_A] = 0xe904, [RDFT_A] = 0xe904,
  2708. [TDH_A] = 0x0cf8, [TDT_A] = 0x0cf8, [TIDV_A] = 0x0cf8,
  2709. [TDFH_A] = 0xed00, [TDFT_A] = 0xed00,
  2710. [RA_A ... RA_A + 31] = 0x14f0,
  2711. [VFTA_A ... VFTA_A + 127] = 0x1400,
  2712. [RDBAL0_A ... RDLEN0_A] = 0x09bc,
  2713. [TDBAL_A ... TDLEN_A] = 0x0cf8,
  2714. /* Access options */
  2715. [RDFH] = MAC_ACCESS_PARTIAL, [RDFT] = MAC_ACCESS_PARTIAL,
  2716. [RDFHS] = MAC_ACCESS_PARTIAL, [RDFTS] = MAC_ACCESS_PARTIAL,
  2717. [RDFPC] = MAC_ACCESS_PARTIAL,
  2718. [TDFH] = MAC_ACCESS_PARTIAL, [TDFT] = MAC_ACCESS_PARTIAL,
  2719. [TDFHS] = MAC_ACCESS_PARTIAL, [TDFTS] = MAC_ACCESS_PARTIAL,
  2720. [TDFPC] = MAC_ACCESS_PARTIAL, [EECD] = MAC_ACCESS_PARTIAL,
  2721. [PBM] = MAC_ACCESS_PARTIAL, [FLA] = MAC_ACCESS_PARTIAL,
  2722. [FCAL] = MAC_ACCESS_PARTIAL, [FCAH] = MAC_ACCESS_PARTIAL,
  2723. [FCT] = MAC_ACCESS_PARTIAL, [FCTTV] = MAC_ACCESS_PARTIAL,
  2724. [FCRTV] = MAC_ACCESS_PARTIAL, [FCRTL] = MAC_ACCESS_PARTIAL,
  2725. [FCRTH] = MAC_ACCESS_PARTIAL, [TXDCTL] = MAC_ACCESS_PARTIAL,
  2726. [TXDCTL1] = MAC_ACCESS_PARTIAL,
  2727. [MAVTV0 ... MAVTV3] = MAC_ACCESS_PARTIAL
  2728. };
  2729. void
  2730. e1000e_core_write(E1000ECore *core, hwaddr addr, uint64_t val, unsigned size)
  2731. {
  2732. uint16_t index = e1000e_get_reg_index_with_offset(mac_reg_access, addr);
  2733. if (index < E1000E_NWRITEOPS && e1000e_macreg_writeops[index]) {
  2734. if (mac_reg_access[index] & MAC_ACCESS_PARTIAL) {
  2735. trace_e1000e_wrn_regs_write_trivial(index << 2);
  2736. }
  2737. trace_e1000e_core_write(index << 2, size, val);
  2738. e1000e_macreg_writeops[index](core, index, val);
  2739. } else if (index < E1000E_NREADOPS && e1000e_macreg_readops[index]) {
  2740. trace_e1000e_wrn_regs_write_ro(index << 2, size, val);
  2741. } else {
  2742. trace_e1000e_wrn_regs_write_unknown(index << 2, size, val);
  2743. }
  2744. }
  2745. uint64_t
  2746. e1000e_core_read(E1000ECore *core, hwaddr addr, unsigned size)
  2747. {
  2748. uint64_t val;
  2749. uint16_t index = e1000e_get_reg_index_with_offset(mac_reg_access, addr);
  2750. if (index < E1000E_NREADOPS && e1000e_macreg_readops[index]) {
  2751. if (mac_reg_access[index] & MAC_ACCESS_PARTIAL) {
  2752. trace_e1000e_wrn_regs_read_trivial(index << 2);
  2753. }
  2754. val = e1000e_macreg_readops[index](core, index);
  2755. trace_e1000e_core_read(index << 2, size, val);
  2756. return val;
  2757. } else {
  2758. trace_e1000e_wrn_regs_read_unknown(index << 2, size);
  2759. }
  2760. return 0;
  2761. }
  2762. static inline void
  2763. e1000e_autoneg_pause(E1000ECore *core)
  2764. {
  2765. timer_del(core->autoneg_timer);
  2766. }
  2767. static void
  2768. e1000e_autoneg_resume(E1000ECore *core)
  2769. {
  2770. if (e1000e_have_autoneg(core) &&
  2771. !(core->phy[0][PHY_STATUS] & MII_SR_AUTONEG_COMPLETE)) {
  2772. qemu_get_queue(core->owner_nic)->link_down = false;
  2773. timer_mod(core->autoneg_timer,
  2774. qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) + 500);
  2775. }
  2776. }
  2777. static void
  2778. e1000e_vm_state_change(void *opaque, int running, RunState state)
  2779. {
  2780. E1000ECore *core = opaque;
  2781. if (running) {
  2782. trace_e1000e_vm_state_running();
  2783. e1000e_intrmgr_resume(core);
  2784. e1000e_autoneg_resume(core);
  2785. } else {
  2786. trace_e1000e_vm_state_stopped();
  2787. e1000e_autoneg_pause(core);
  2788. e1000e_intrmgr_pause(core);
  2789. }
  2790. }
  2791. void
  2792. e1000e_core_pci_realize(E1000ECore *core,
  2793. const uint16_t *eeprom_templ,
  2794. uint32_t eeprom_size,
  2795. const uint8_t *macaddr)
  2796. {
  2797. int i;
  2798. core->autoneg_timer = timer_new_ms(QEMU_CLOCK_VIRTUAL,
  2799. e1000e_autoneg_timer, core);
  2800. e1000e_intrmgr_pci_realize(core);
  2801. core->vmstate =
  2802. qemu_add_vm_change_state_handler(e1000e_vm_state_change, core);
  2803. for (i = 0; i < E1000E_NUM_QUEUES; i++) {
  2804. net_tx_pkt_init(&core->tx[i].tx_pkt, core->owner,
  2805. E1000E_MAX_TX_FRAGS, core->has_vnet);
  2806. }
  2807. net_rx_pkt_init(&core->rx_pkt, core->has_vnet);
  2808. e1000x_core_prepare_eeprom(core->eeprom,
  2809. eeprom_templ,
  2810. eeprom_size,
  2811. PCI_DEVICE_GET_CLASS(core->owner)->device_id,
  2812. macaddr);
  2813. e1000e_update_rx_offloads(core);
  2814. }
  2815. void
  2816. e1000e_core_pci_uninit(E1000ECore *core)
  2817. {
  2818. int i;
  2819. timer_del(core->autoneg_timer);
  2820. timer_free(core->autoneg_timer);
  2821. e1000e_intrmgr_pci_unint(core);
  2822. qemu_del_vm_change_state_handler(core->vmstate);
  2823. for (i = 0; i < E1000E_NUM_QUEUES; i++) {
  2824. net_tx_pkt_reset(core->tx[i].tx_pkt);
  2825. net_tx_pkt_uninit(core->tx[i].tx_pkt);
  2826. }
  2827. net_rx_pkt_uninit(core->rx_pkt);
  2828. }
  2829. static const uint16_t
  2830. e1000e_phy_reg_init[E1000E_PHY_PAGES][E1000E_PHY_PAGE_SIZE] = {
  2831. [0] = {
  2832. [PHY_CTRL] = MII_CR_SPEED_SELECT_MSB |
  2833. MII_CR_FULL_DUPLEX |
  2834. MII_CR_AUTO_NEG_EN,
  2835. [PHY_STATUS] = MII_SR_EXTENDED_CAPS |
  2836. MII_SR_LINK_STATUS |
  2837. MII_SR_AUTONEG_CAPS |
  2838. MII_SR_PREAMBLE_SUPPRESS |
  2839. MII_SR_EXTENDED_STATUS |
  2840. MII_SR_10T_HD_CAPS |
  2841. MII_SR_10T_FD_CAPS |
  2842. MII_SR_100X_HD_CAPS |
  2843. MII_SR_100X_FD_CAPS,
  2844. [PHY_ID1] = 0x141,
  2845. [PHY_ID2] = E1000_PHY_ID2_82574x,
  2846. [PHY_AUTONEG_ADV] = 0xde1,
  2847. [PHY_LP_ABILITY] = 0x7e0,
  2848. [PHY_AUTONEG_EXP] = BIT(2),
  2849. [PHY_NEXT_PAGE_TX] = BIT(0) | BIT(13),
  2850. [PHY_1000T_CTRL] = BIT(8) | BIT(9) | BIT(10) | BIT(11),
  2851. [PHY_1000T_STATUS] = 0x3c00,
  2852. [PHY_EXT_STATUS] = BIT(12) | BIT(13),
  2853. [PHY_COPPER_CTRL1] = BIT(5) | BIT(6) | BIT(8) | BIT(9) |
  2854. BIT(12) | BIT(13),
  2855. [PHY_COPPER_STAT1] = BIT(3) | BIT(10) | BIT(11) | BIT(13) | BIT(15)
  2856. },
  2857. [2] = {
  2858. [PHY_MAC_CTRL1] = BIT(3) | BIT(7),
  2859. [PHY_MAC_CTRL2] = BIT(1) | BIT(2) | BIT(6) | BIT(12)
  2860. },
  2861. [3] = {
  2862. [PHY_LED_TIMER_CTRL] = BIT(0) | BIT(2) | BIT(14)
  2863. }
  2864. };
  2865. static const uint32_t e1000e_mac_reg_init[] = {
  2866. [PBA] = 0x00140014,
  2867. [LEDCTL] = BIT(1) | BIT(8) | BIT(9) | BIT(15) | BIT(17) | BIT(18),
  2868. [EXTCNF_CTRL] = BIT(3),
  2869. [EEMNGCTL] = BIT(31),
  2870. [FLASHT] = 0x2,
  2871. [FLSWCTL] = BIT(30) | BIT(31),
  2872. [FLOL] = BIT(0),
  2873. [RXDCTL] = BIT(16),
  2874. [RXDCTL1] = BIT(16),
  2875. [TIPG] = 0x8 | (0x8 << 10) | (0x6 << 20),
  2876. [RXCFGL] = 0x88F7,
  2877. [RXUDP] = 0x319,
  2878. [CTRL] = E1000_CTRL_FD | E1000_CTRL_SWDPIN2 | E1000_CTRL_SWDPIN0 |
  2879. E1000_CTRL_SPD_1000 | E1000_CTRL_SLU |
  2880. E1000_CTRL_ADVD3WUC,
  2881. [STATUS] = E1000_STATUS_ASDV_1000 | E1000_STATUS_LU,
  2882. [PSRCTL] = (2 << E1000_PSRCTL_BSIZE0_SHIFT) |
  2883. (4 << E1000_PSRCTL_BSIZE1_SHIFT) |
  2884. (4 << E1000_PSRCTL_BSIZE2_SHIFT),
  2885. [TARC0] = 0x3 | E1000_TARC_ENABLE,
  2886. [TARC1] = 0x3 | E1000_TARC_ENABLE,
  2887. [EECD] = E1000_EECD_AUTO_RD | E1000_EECD_PRES,
  2888. [EERD] = E1000_EERW_DONE,
  2889. [EEWR] = E1000_EERW_DONE,
  2890. [GCR] = E1000_L0S_ADJUST |
  2891. E1000_L1_ENTRY_LATENCY_MSB |
  2892. E1000_L1_ENTRY_LATENCY_LSB,
  2893. [TDFH] = 0x600,
  2894. [TDFT] = 0x600,
  2895. [TDFHS] = 0x600,
  2896. [TDFTS] = 0x600,
  2897. [POEMB] = 0x30D,
  2898. [PBS] = 0x028,
  2899. [MANC] = E1000_MANC_DIS_IP_CHK_ARP,
  2900. [FACTPS] = E1000_FACTPS_LAN0_ON | 0x20000000,
  2901. [SWSM] = 1,
  2902. [RXCSUM] = E1000_RXCSUM_IPOFLD | E1000_RXCSUM_TUOFLD,
  2903. [ITR] = E1000E_MIN_XITR,
  2904. [EITR...EITR + E1000E_MSIX_VEC_NUM - 1] = E1000E_MIN_XITR,
  2905. };
  2906. void
  2907. e1000e_core_reset(E1000ECore *core)
  2908. {
  2909. int i;
  2910. timer_del(core->autoneg_timer);
  2911. e1000e_intrmgr_reset(core);
  2912. memset(core->phy, 0, sizeof core->phy);
  2913. memmove(core->phy, e1000e_phy_reg_init, sizeof e1000e_phy_reg_init);
  2914. memset(core->mac, 0, sizeof core->mac);
  2915. memmove(core->mac, e1000e_mac_reg_init, sizeof e1000e_mac_reg_init);
  2916. core->rxbuf_min_shift = 1 + E1000_RING_DESC_LEN_SHIFT;
  2917. if (qemu_get_queue(core->owner_nic)->link_down) {
  2918. e1000e_link_down(core);
  2919. }
  2920. e1000x_reset_mac_addr(core->owner_nic, core->mac, core->permanent_mac);
  2921. for (i = 0; i < ARRAY_SIZE(core->tx); i++) {
  2922. net_tx_pkt_reset(core->tx[i].tx_pkt);
  2923. memset(&core->tx[i].props, 0, sizeof(core->tx[i].props));
  2924. core->tx[i].skip_cp = false;
  2925. }
  2926. }
  2927. void e1000e_core_pre_save(E1000ECore *core)
  2928. {
  2929. int i;
  2930. NetClientState *nc = qemu_get_queue(core->owner_nic);
  2931. /*
  2932. * If link is down and auto-negotiation is supported and ongoing,
  2933. * complete auto-negotiation immediately. This allows us to look
  2934. * at MII_SR_AUTONEG_COMPLETE to infer link status on load.
  2935. */
  2936. if (nc->link_down && e1000e_have_autoneg(core)) {
  2937. core->phy[0][PHY_STATUS] |= MII_SR_AUTONEG_COMPLETE;
  2938. e1000e_update_flowctl_status(core);
  2939. }
  2940. for (i = 0; i < ARRAY_SIZE(core->tx); i++) {
  2941. if (net_tx_pkt_has_fragments(core->tx[i].tx_pkt)) {
  2942. core->tx[i].skip_cp = true;
  2943. }
  2944. }
  2945. }
  2946. int
  2947. e1000e_core_post_load(E1000ECore *core)
  2948. {
  2949. NetClientState *nc = qemu_get_queue(core->owner_nic);
  2950. /* nc.link_down can't be migrated, so infer link_down according
  2951. * to link status bit in core.mac[STATUS].
  2952. */
  2953. nc->link_down = (core->mac[STATUS] & E1000_STATUS_LU) == 0;
  2954. return 0;
  2955. }