e1000e_core.c 103 KB

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