e1000e_core.c 101 KB

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