2
0

e1000e_core.c 103 KB

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