e1000e_core.c 103 KB

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