i40e_ethtool.c 151 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright(c) 2013 - 2018 Intel Corporation. */
  3. /* ethtool support for i40e */
  4. #include "i40e.h"
  5. #include "i40e_diag.h"
  6. #include "i40e_txrx_common.h"
  7. /* ethtool statistics helpers */
  8. /**
  9. * struct i40e_stats - definition for an ethtool statistic
  10. * @stat_string: statistic name to display in ethtool -S output
  11. * @sizeof_stat: the sizeof() the stat, must be no greater than sizeof(u64)
  12. * @stat_offset: offsetof() the stat from a base pointer
  13. *
  14. * This structure defines a statistic to be added to the ethtool stats buffer.
  15. * It defines a statistic as offset from a common base pointer. Stats should
  16. * be defined in constant arrays using the I40E_STAT macro, with every element
  17. * of the array using the same _type for calculating the sizeof_stat and
  18. * stat_offset.
  19. *
  20. * The @sizeof_stat is expected to be sizeof(u8), sizeof(u16), sizeof(u32) or
  21. * sizeof(u64). Other sizes are not expected and will produce a WARN_ONCE from
  22. * the i40e_add_ethtool_stat() helper function.
  23. *
  24. * The @stat_string is interpreted as a format string, allowing formatted
  25. * values to be inserted while looping over multiple structures for a given
  26. * statistics array. Thus, every statistic string in an array should have the
  27. * same type and number of format specifiers, to be formatted by variadic
  28. * arguments to the i40e_add_stat_string() helper function.
  29. **/
  30. struct i40e_stats {
  31. char stat_string[ETH_GSTRING_LEN];
  32. int sizeof_stat;
  33. int stat_offset;
  34. };
  35. /* Helper macro to define an i40e_stat structure with proper size and type.
  36. * Use this when defining constant statistics arrays. Note that @_type expects
  37. * only a type name and is used multiple times.
  38. */
  39. #define I40E_STAT(_type, _name, _stat) { \
  40. .stat_string = _name, \
  41. .sizeof_stat = FIELD_SIZEOF(_type, _stat), \
  42. .stat_offset = offsetof(_type, _stat) \
  43. }
  44. /* Helper macro for defining some statistics directly copied from the netdev
  45. * stats structure.
  46. */
  47. #define I40E_NETDEV_STAT(_net_stat) \
  48. I40E_STAT(struct rtnl_link_stats64, #_net_stat, _net_stat)
  49. /* Helper macro for defining some statistics related to queues */
  50. #define I40E_QUEUE_STAT(_name, _stat) \
  51. I40E_STAT(struct i40e_ring, _name, _stat)
  52. /* Stats associated with a Tx or Rx ring */
  53. static const struct i40e_stats i40e_gstrings_queue_stats[] = {
  54. I40E_QUEUE_STAT("%s-%u.packets", stats.packets),
  55. I40E_QUEUE_STAT("%s-%u.bytes", stats.bytes),
  56. };
  57. /**
  58. * i40e_add_one_ethtool_stat - copy the stat into the supplied buffer
  59. * @data: location to store the stat value
  60. * @pointer: basis for where to copy from
  61. * @stat: the stat definition
  62. *
  63. * Copies the stat data defined by the pointer and stat structure pair into
  64. * the memory supplied as data. Used to implement i40e_add_ethtool_stats and
  65. * i40e_add_queue_stats. If the pointer is null, data will be zero'd.
  66. */
  67. static void
  68. i40e_add_one_ethtool_stat(u64 *data, void *pointer,
  69. const struct i40e_stats *stat)
  70. {
  71. char *p;
  72. if (!pointer) {
  73. /* ensure that the ethtool data buffer is zero'd for any stats
  74. * which don't have a valid pointer.
  75. */
  76. *data = 0;
  77. return;
  78. }
  79. p = (char *)pointer + stat->stat_offset;
  80. switch (stat->sizeof_stat) {
  81. case sizeof(u64):
  82. *data = *((u64 *)p);
  83. break;
  84. case sizeof(u32):
  85. *data = *((u32 *)p);
  86. break;
  87. case sizeof(u16):
  88. *data = *((u16 *)p);
  89. break;
  90. case sizeof(u8):
  91. *data = *((u8 *)p);
  92. break;
  93. default:
  94. WARN_ONCE(1, "unexpected stat size for %s",
  95. stat->stat_string);
  96. *data = 0;
  97. }
  98. }
  99. /**
  100. * __i40e_add_ethtool_stats - copy stats into the ethtool supplied buffer
  101. * @data: ethtool stats buffer
  102. * @pointer: location to copy stats from
  103. * @stats: array of stats to copy
  104. * @size: the size of the stats definition
  105. *
  106. * Copy the stats defined by the stats array using the pointer as a base into
  107. * the data buffer supplied by ethtool. Updates the data pointer to point to
  108. * the next empty location for successive calls to __i40e_add_ethtool_stats.
  109. * If pointer is null, set the data values to zero and update the pointer to
  110. * skip these stats.
  111. **/
  112. static void
  113. __i40e_add_ethtool_stats(u64 **data, void *pointer,
  114. const struct i40e_stats stats[],
  115. const unsigned int size)
  116. {
  117. unsigned int i;
  118. for (i = 0; i < size; i++)
  119. i40e_add_one_ethtool_stat((*data)++, pointer, &stats[i]);
  120. }
  121. /**
  122. * i40e_add_ethtool_stats - copy stats into ethtool supplied buffer
  123. * @data: ethtool stats buffer
  124. * @pointer: location where stats are stored
  125. * @stats: static const array of stat definitions
  126. *
  127. * Macro to ease the use of __i40e_add_ethtool_stats by taking a static
  128. * constant stats array and passing the ARRAY_SIZE(). This avoids typos by
  129. * ensuring that we pass the size associated with the given stats array.
  130. *
  131. * The parameter @stats is evaluated twice, so parameters with side effects
  132. * should be avoided.
  133. **/
  134. #define i40e_add_ethtool_stats(data, pointer, stats) \
  135. __i40e_add_ethtool_stats(data, pointer, stats, ARRAY_SIZE(stats))
  136. /**
  137. * i40e_add_queue_stats - copy queue statistics into supplied buffer
  138. * @data: ethtool stats buffer
  139. * @ring: the ring to copy
  140. *
  141. * Queue statistics must be copied while protected by
  142. * u64_stats_fetch_begin_irq, so we can't directly use i40e_add_ethtool_stats.
  143. * Assumes that queue stats are defined in i40e_gstrings_queue_stats. If the
  144. * ring pointer is null, zero out the queue stat values and update the data
  145. * pointer. Otherwise safely copy the stats from the ring into the supplied
  146. * buffer and update the data pointer when finished.
  147. *
  148. * This function expects to be called while under rcu_read_lock().
  149. **/
  150. static void
  151. i40e_add_queue_stats(u64 **data, struct i40e_ring *ring)
  152. {
  153. const unsigned int size = ARRAY_SIZE(i40e_gstrings_queue_stats);
  154. const struct i40e_stats *stats = i40e_gstrings_queue_stats;
  155. unsigned int start;
  156. unsigned int i;
  157. /* To avoid invalid statistics values, ensure that we keep retrying
  158. * the copy until we get a consistent value according to
  159. * u64_stats_fetch_retry_irq. But first, make sure our ring is
  160. * non-null before attempting to access its syncp.
  161. */
  162. do {
  163. start = !ring ? 0 : u64_stats_fetch_begin_irq(&ring->syncp);
  164. for (i = 0; i < size; i++) {
  165. i40e_add_one_ethtool_stat(&(*data)[i], ring,
  166. &stats[i]);
  167. }
  168. } while (ring && u64_stats_fetch_retry_irq(&ring->syncp, start));
  169. /* Once we successfully copy the stats in, update the data pointer */
  170. *data += size;
  171. }
  172. /**
  173. * __i40e_add_stat_strings - copy stat strings into ethtool buffer
  174. * @p: ethtool supplied buffer
  175. * @stats: stat definitions array
  176. * @size: size of the stats array
  177. *
  178. * Format and copy the strings described by stats into the buffer pointed at
  179. * by p.
  180. **/
  181. static void __i40e_add_stat_strings(u8 **p, const struct i40e_stats stats[],
  182. const unsigned int size, ...)
  183. {
  184. unsigned int i;
  185. for (i = 0; i < size; i++) {
  186. va_list args;
  187. va_start(args, size);
  188. vsnprintf(*p, ETH_GSTRING_LEN, stats[i].stat_string, args);
  189. *p += ETH_GSTRING_LEN;
  190. va_end(args);
  191. }
  192. }
  193. /**
  194. * 40e_add_stat_strings - copy stat strings into ethtool buffer
  195. * @p: ethtool supplied buffer
  196. * @stats: stat definitions array
  197. *
  198. * Format and copy the strings described by the const static stats value into
  199. * the buffer pointed at by p.
  200. *
  201. * The parameter @stats is evaluated twice, so parameters with side effects
  202. * should be avoided. Additionally, stats must be an array such that
  203. * ARRAY_SIZE can be called on it.
  204. **/
  205. #define i40e_add_stat_strings(p, stats, ...) \
  206. __i40e_add_stat_strings(p, stats, ARRAY_SIZE(stats), ## __VA_ARGS__)
  207. #define I40E_PF_STAT(_name, _stat) \
  208. I40E_STAT(struct i40e_pf, _name, _stat)
  209. #define I40E_VSI_STAT(_name, _stat) \
  210. I40E_STAT(struct i40e_vsi, _name, _stat)
  211. #define I40E_VEB_STAT(_name, _stat) \
  212. I40E_STAT(struct i40e_veb, _name, _stat)
  213. #define I40E_PFC_STAT(_name, _stat) \
  214. I40E_STAT(struct i40e_pfc_stats, _name, _stat)
  215. #define I40E_QUEUE_STAT(_name, _stat) \
  216. I40E_STAT(struct i40e_ring, _name, _stat)
  217. static const struct i40e_stats i40e_gstrings_net_stats[] = {
  218. I40E_NETDEV_STAT(rx_packets),
  219. I40E_NETDEV_STAT(tx_packets),
  220. I40E_NETDEV_STAT(rx_bytes),
  221. I40E_NETDEV_STAT(tx_bytes),
  222. I40E_NETDEV_STAT(rx_errors),
  223. I40E_NETDEV_STAT(tx_errors),
  224. I40E_NETDEV_STAT(rx_dropped),
  225. I40E_NETDEV_STAT(tx_dropped),
  226. I40E_NETDEV_STAT(collisions),
  227. I40E_NETDEV_STAT(rx_length_errors),
  228. I40E_NETDEV_STAT(rx_crc_errors),
  229. };
  230. static const struct i40e_stats i40e_gstrings_veb_stats[] = {
  231. I40E_VEB_STAT("veb.rx_bytes", stats.rx_bytes),
  232. I40E_VEB_STAT("veb.tx_bytes", stats.tx_bytes),
  233. I40E_VEB_STAT("veb.rx_unicast", stats.rx_unicast),
  234. I40E_VEB_STAT("veb.tx_unicast", stats.tx_unicast),
  235. I40E_VEB_STAT("veb.rx_multicast", stats.rx_multicast),
  236. I40E_VEB_STAT("veb.tx_multicast", stats.tx_multicast),
  237. I40E_VEB_STAT("veb.rx_broadcast", stats.rx_broadcast),
  238. I40E_VEB_STAT("veb.tx_broadcast", stats.tx_broadcast),
  239. I40E_VEB_STAT("veb.rx_discards", stats.rx_discards),
  240. I40E_VEB_STAT("veb.tx_discards", stats.tx_discards),
  241. I40E_VEB_STAT("veb.tx_errors", stats.tx_errors),
  242. I40E_VEB_STAT("veb.rx_unknown_protocol", stats.rx_unknown_protocol),
  243. };
  244. static const struct i40e_stats i40e_gstrings_veb_tc_stats[] = {
  245. I40E_VEB_STAT("veb.tc_%u_tx_packets", tc_stats.tc_tx_packets),
  246. I40E_VEB_STAT("veb.tc_%u_tx_bytes", tc_stats.tc_tx_bytes),
  247. I40E_VEB_STAT("veb.tc_%u_rx_packets", tc_stats.tc_rx_packets),
  248. I40E_VEB_STAT("veb.tc_%u_rx_bytes", tc_stats.tc_rx_bytes),
  249. };
  250. static const struct i40e_stats i40e_gstrings_misc_stats[] = {
  251. I40E_VSI_STAT("rx_unicast", eth_stats.rx_unicast),
  252. I40E_VSI_STAT("tx_unicast", eth_stats.tx_unicast),
  253. I40E_VSI_STAT("rx_multicast", eth_stats.rx_multicast),
  254. I40E_VSI_STAT("tx_multicast", eth_stats.tx_multicast),
  255. I40E_VSI_STAT("rx_broadcast", eth_stats.rx_broadcast),
  256. I40E_VSI_STAT("tx_broadcast", eth_stats.tx_broadcast),
  257. I40E_VSI_STAT("rx_unknown_protocol", eth_stats.rx_unknown_protocol),
  258. I40E_VSI_STAT("tx_linearize", tx_linearize),
  259. I40E_VSI_STAT("tx_force_wb", tx_force_wb),
  260. I40E_VSI_STAT("tx_busy", tx_busy),
  261. I40E_VSI_STAT("rx_alloc_fail", rx_buf_failed),
  262. I40E_VSI_STAT("rx_pg_alloc_fail", rx_page_failed),
  263. };
  264. /* These PF_STATs might look like duplicates of some NETDEV_STATs,
  265. * but they are separate. This device supports Virtualization, and
  266. * as such might have several netdevs supporting VMDq and FCoE going
  267. * through a single port. The NETDEV_STATs are for individual netdevs
  268. * seen at the top of the stack, and the PF_STATs are for the physical
  269. * function at the bottom of the stack hosting those netdevs.
  270. *
  271. * The PF_STATs are appended to the netdev stats only when ethtool -S
  272. * is queried on the base PF netdev, not on the VMDq or FCoE netdev.
  273. */
  274. static const struct i40e_stats i40e_gstrings_stats[] = {
  275. I40E_PF_STAT("port.rx_bytes", stats.eth.rx_bytes),
  276. I40E_PF_STAT("port.tx_bytes", stats.eth.tx_bytes),
  277. I40E_PF_STAT("port.rx_unicast", stats.eth.rx_unicast),
  278. I40E_PF_STAT("port.tx_unicast", stats.eth.tx_unicast),
  279. I40E_PF_STAT("port.rx_multicast", stats.eth.rx_multicast),
  280. I40E_PF_STAT("port.tx_multicast", stats.eth.tx_multicast),
  281. I40E_PF_STAT("port.rx_broadcast", stats.eth.rx_broadcast),
  282. I40E_PF_STAT("port.tx_broadcast", stats.eth.tx_broadcast),
  283. I40E_PF_STAT("port.tx_errors", stats.eth.tx_errors),
  284. I40E_PF_STAT("port.rx_dropped", stats.eth.rx_discards),
  285. I40E_PF_STAT("port.tx_dropped_link_down", stats.tx_dropped_link_down),
  286. I40E_PF_STAT("port.rx_crc_errors", stats.crc_errors),
  287. I40E_PF_STAT("port.illegal_bytes", stats.illegal_bytes),
  288. I40E_PF_STAT("port.mac_local_faults", stats.mac_local_faults),
  289. I40E_PF_STAT("port.mac_remote_faults", stats.mac_remote_faults),
  290. I40E_PF_STAT("port.tx_timeout", tx_timeout_count),
  291. I40E_PF_STAT("port.rx_csum_bad", hw_csum_rx_error),
  292. I40E_PF_STAT("port.rx_length_errors", stats.rx_length_errors),
  293. I40E_PF_STAT("port.link_xon_rx", stats.link_xon_rx),
  294. I40E_PF_STAT("port.link_xoff_rx", stats.link_xoff_rx),
  295. I40E_PF_STAT("port.link_xon_tx", stats.link_xon_tx),
  296. I40E_PF_STAT("port.link_xoff_tx", stats.link_xoff_tx),
  297. I40E_PF_STAT("port.rx_size_64", stats.rx_size_64),
  298. I40E_PF_STAT("port.rx_size_127", stats.rx_size_127),
  299. I40E_PF_STAT("port.rx_size_255", stats.rx_size_255),
  300. I40E_PF_STAT("port.rx_size_511", stats.rx_size_511),
  301. I40E_PF_STAT("port.rx_size_1023", stats.rx_size_1023),
  302. I40E_PF_STAT("port.rx_size_1522", stats.rx_size_1522),
  303. I40E_PF_STAT("port.rx_size_big", stats.rx_size_big),
  304. I40E_PF_STAT("port.tx_size_64", stats.tx_size_64),
  305. I40E_PF_STAT("port.tx_size_127", stats.tx_size_127),
  306. I40E_PF_STAT("port.tx_size_255", stats.tx_size_255),
  307. I40E_PF_STAT("port.tx_size_511", stats.tx_size_511),
  308. I40E_PF_STAT("port.tx_size_1023", stats.tx_size_1023),
  309. I40E_PF_STAT("port.tx_size_1522", stats.tx_size_1522),
  310. I40E_PF_STAT("port.tx_size_big", stats.tx_size_big),
  311. I40E_PF_STAT("port.rx_undersize", stats.rx_undersize),
  312. I40E_PF_STAT("port.rx_fragments", stats.rx_fragments),
  313. I40E_PF_STAT("port.rx_oversize", stats.rx_oversize),
  314. I40E_PF_STAT("port.rx_jabber", stats.rx_jabber),
  315. I40E_PF_STAT("port.VF_admin_queue_requests", vf_aq_requests),
  316. I40E_PF_STAT("port.arq_overflows", arq_overflows),
  317. I40E_PF_STAT("port.tx_hwtstamp_timeouts", tx_hwtstamp_timeouts),
  318. I40E_PF_STAT("port.rx_hwtstamp_cleared", rx_hwtstamp_cleared),
  319. I40E_PF_STAT("port.tx_hwtstamp_skipped", tx_hwtstamp_skipped),
  320. I40E_PF_STAT("port.fdir_flush_cnt", fd_flush_cnt),
  321. I40E_PF_STAT("port.fdir_atr_match", stats.fd_atr_match),
  322. I40E_PF_STAT("port.fdir_atr_tunnel_match", stats.fd_atr_tunnel_match),
  323. I40E_PF_STAT("port.fdir_atr_status", stats.fd_atr_status),
  324. I40E_PF_STAT("port.fdir_sb_match", stats.fd_sb_match),
  325. I40E_PF_STAT("port.fdir_sb_status", stats.fd_sb_status),
  326. /* LPI stats */
  327. I40E_PF_STAT("port.tx_lpi_status", stats.tx_lpi_status),
  328. I40E_PF_STAT("port.rx_lpi_status", stats.rx_lpi_status),
  329. I40E_PF_STAT("port.tx_lpi_count", stats.tx_lpi_count),
  330. I40E_PF_STAT("port.rx_lpi_count", stats.rx_lpi_count),
  331. };
  332. struct i40e_pfc_stats {
  333. u64 priority_xon_rx;
  334. u64 priority_xoff_rx;
  335. u64 priority_xon_tx;
  336. u64 priority_xoff_tx;
  337. u64 priority_xon_2_xoff;
  338. };
  339. static const struct i40e_stats i40e_gstrings_pfc_stats[] = {
  340. I40E_PFC_STAT("port.tx_priority_%u_xon_tx", priority_xon_tx),
  341. I40E_PFC_STAT("port.tx_priority_%u_xoff_tx", priority_xoff_tx),
  342. I40E_PFC_STAT("port.rx_priority_%u_xon_rx", priority_xon_rx),
  343. I40E_PFC_STAT("port.rx_priority_%u_xoff_rx", priority_xoff_rx),
  344. I40E_PFC_STAT("port.rx_priority_%u_xon_2_xoff", priority_xon_2_xoff),
  345. };
  346. #define I40E_NETDEV_STATS_LEN ARRAY_SIZE(i40e_gstrings_net_stats)
  347. #define I40E_MISC_STATS_LEN ARRAY_SIZE(i40e_gstrings_misc_stats)
  348. #define I40E_VSI_STATS_LEN (I40E_NETDEV_STATS_LEN + I40E_MISC_STATS_LEN)
  349. #define I40E_PFC_STATS_LEN (ARRAY_SIZE(i40e_gstrings_pfc_stats) * \
  350. I40E_MAX_USER_PRIORITY)
  351. #define I40E_VEB_STATS_LEN (ARRAY_SIZE(i40e_gstrings_veb_stats) + \
  352. (ARRAY_SIZE(i40e_gstrings_veb_tc_stats) * \
  353. I40E_MAX_TRAFFIC_CLASS))
  354. #define I40E_GLOBAL_STATS_LEN ARRAY_SIZE(i40e_gstrings_stats)
  355. #define I40E_PF_STATS_LEN (I40E_GLOBAL_STATS_LEN + \
  356. I40E_PFC_STATS_LEN + \
  357. I40E_VEB_STATS_LEN + \
  358. I40E_VSI_STATS_LEN)
  359. /* Length of stats for a single queue */
  360. #define I40E_QUEUE_STATS_LEN ARRAY_SIZE(i40e_gstrings_queue_stats)
  361. enum i40e_ethtool_test_id {
  362. I40E_ETH_TEST_REG = 0,
  363. I40E_ETH_TEST_EEPROM,
  364. I40E_ETH_TEST_INTR,
  365. I40E_ETH_TEST_LINK,
  366. };
  367. static const char i40e_gstrings_test[][ETH_GSTRING_LEN] = {
  368. "Register test (offline)",
  369. "Eeprom test (offline)",
  370. "Interrupt test (offline)",
  371. "Link test (on/offline)"
  372. };
  373. #define I40E_TEST_LEN (sizeof(i40e_gstrings_test) / ETH_GSTRING_LEN)
  374. struct i40e_priv_flags {
  375. char flag_string[ETH_GSTRING_LEN];
  376. u64 flag;
  377. bool read_only;
  378. };
  379. #define I40E_PRIV_FLAG(_name, _flag, _read_only) { \
  380. .flag_string = _name, \
  381. .flag = _flag, \
  382. .read_only = _read_only, \
  383. }
  384. static const struct i40e_priv_flags i40e_gstrings_priv_flags[] = {
  385. /* NOTE: MFP setting cannot be changed */
  386. I40E_PRIV_FLAG("MFP", I40E_FLAG_MFP_ENABLED, 1),
  387. I40E_PRIV_FLAG("LinkPolling", I40E_FLAG_LINK_POLLING_ENABLED, 0),
  388. I40E_PRIV_FLAG("flow-director-atr", I40E_FLAG_FD_ATR_ENABLED, 0),
  389. I40E_PRIV_FLAG("veb-stats", I40E_FLAG_VEB_STATS_ENABLED, 0),
  390. I40E_PRIV_FLAG("hw-atr-eviction", I40E_FLAG_HW_ATR_EVICT_ENABLED, 0),
  391. I40E_PRIV_FLAG("link-down-on-close",
  392. I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED, 0),
  393. I40E_PRIV_FLAG("legacy-rx", I40E_FLAG_LEGACY_RX, 0),
  394. I40E_PRIV_FLAG("disable-source-pruning",
  395. I40E_FLAG_SOURCE_PRUNING_DISABLED, 0),
  396. I40E_PRIV_FLAG("disable-fw-lldp", I40E_FLAG_DISABLE_FW_LLDP, 0),
  397. };
  398. #define I40E_PRIV_FLAGS_STR_LEN ARRAY_SIZE(i40e_gstrings_priv_flags)
  399. /* Private flags with a global effect, restricted to PF 0 */
  400. static const struct i40e_priv_flags i40e_gl_gstrings_priv_flags[] = {
  401. I40E_PRIV_FLAG("vf-true-promisc-support",
  402. I40E_FLAG_TRUE_PROMISC_SUPPORT, 0),
  403. };
  404. #define I40E_GL_PRIV_FLAGS_STR_LEN ARRAY_SIZE(i40e_gl_gstrings_priv_flags)
  405. /**
  406. * i40e_partition_setting_complaint - generic complaint for MFP restriction
  407. * @pf: the PF struct
  408. **/
  409. static void i40e_partition_setting_complaint(struct i40e_pf *pf)
  410. {
  411. dev_info(&pf->pdev->dev,
  412. "The link settings are allowed to be changed only from the first partition of a given port. Please switch to the first partition in order to change the setting.\n");
  413. }
  414. /**
  415. * i40e_phy_type_to_ethtool - convert the phy_types to ethtool link modes
  416. * @pf: PF struct with phy_types
  417. * @ks: ethtool link ksettings struct to fill out
  418. *
  419. **/
  420. static void i40e_phy_type_to_ethtool(struct i40e_pf *pf,
  421. struct ethtool_link_ksettings *ks)
  422. {
  423. struct i40e_link_status *hw_link_info = &pf->hw.phy.link_info;
  424. u64 phy_types = pf->hw.phy.phy_types;
  425. ethtool_link_ksettings_zero_link_mode(ks, supported);
  426. ethtool_link_ksettings_zero_link_mode(ks, advertising);
  427. if (phy_types & I40E_CAP_PHY_TYPE_SGMII) {
  428. ethtool_link_ksettings_add_link_mode(ks, supported,
  429. 1000baseT_Full);
  430. if (hw_link_info->requested_speeds & I40E_LINK_SPEED_1GB)
  431. ethtool_link_ksettings_add_link_mode(ks, advertising,
  432. 1000baseT_Full);
  433. if (pf->hw_features & I40E_HW_100M_SGMII_CAPABLE) {
  434. ethtool_link_ksettings_add_link_mode(ks, supported,
  435. 100baseT_Full);
  436. ethtool_link_ksettings_add_link_mode(ks, advertising,
  437. 100baseT_Full);
  438. }
  439. }
  440. if (phy_types & I40E_CAP_PHY_TYPE_XAUI ||
  441. phy_types & I40E_CAP_PHY_TYPE_XFI ||
  442. phy_types & I40E_CAP_PHY_TYPE_SFI ||
  443. phy_types & I40E_CAP_PHY_TYPE_10GBASE_SFPP_CU ||
  444. phy_types & I40E_CAP_PHY_TYPE_10GBASE_AOC) {
  445. ethtool_link_ksettings_add_link_mode(ks, supported,
  446. 10000baseT_Full);
  447. if (hw_link_info->requested_speeds & I40E_LINK_SPEED_10GB)
  448. ethtool_link_ksettings_add_link_mode(ks, advertising,
  449. 10000baseT_Full);
  450. }
  451. if (phy_types & I40E_CAP_PHY_TYPE_10GBASE_T) {
  452. ethtool_link_ksettings_add_link_mode(ks, supported,
  453. 10000baseT_Full);
  454. if (hw_link_info->requested_speeds & I40E_LINK_SPEED_10GB)
  455. ethtool_link_ksettings_add_link_mode(ks, advertising,
  456. 10000baseT_Full);
  457. }
  458. if (phy_types & I40E_CAP_PHY_TYPE_XLAUI ||
  459. phy_types & I40E_CAP_PHY_TYPE_XLPPI ||
  460. phy_types & I40E_CAP_PHY_TYPE_40GBASE_AOC)
  461. ethtool_link_ksettings_add_link_mode(ks, supported,
  462. 40000baseCR4_Full);
  463. if (phy_types & I40E_CAP_PHY_TYPE_40GBASE_CR4_CU ||
  464. phy_types & I40E_CAP_PHY_TYPE_40GBASE_CR4) {
  465. ethtool_link_ksettings_add_link_mode(ks, supported,
  466. 40000baseCR4_Full);
  467. if (hw_link_info->requested_speeds & I40E_LINK_SPEED_40GB)
  468. ethtool_link_ksettings_add_link_mode(ks, advertising,
  469. 40000baseCR4_Full);
  470. }
  471. if (phy_types & I40E_CAP_PHY_TYPE_100BASE_TX) {
  472. ethtool_link_ksettings_add_link_mode(ks, supported,
  473. 100baseT_Full);
  474. if (hw_link_info->requested_speeds & I40E_LINK_SPEED_100MB)
  475. ethtool_link_ksettings_add_link_mode(ks, advertising,
  476. 100baseT_Full);
  477. }
  478. if (phy_types & I40E_CAP_PHY_TYPE_1000BASE_T) {
  479. ethtool_link_ksettings_add_link_mode(ks, supported,
  480. 1000baseT_Full);
  481. if (hw_link_info->requested_speeds & I40E_LINK_SPEED_1GB)
  482. ethtool_link_ksettings_add_link_mode(ks, advertising,
  483. 1000baseT_Full);
  484. }
  485. if (phy_types & I40E_CAP_PHY_TYPE_40GBASE_SR4)
  486. ethtool_link_ksettings_add_link_mode(ks, supported,
  487. 40000baseSR4_Full);
  488. if (phy_types & I40E_CAP_PHY_TYPE_40GBASE_LR4)
  489. ethtool_link_ksettings_add_link_mode(ks, supported,
  490. 40000baseLR4_Full);
  491. if (phy_types & I40E_CAP_PHY_TYPE_40GBASE_KR4) {
  492. ethtool_link_ksettings_add_link_mode(ks, supported,
  493. 40000baseLR4_Full);
  494. ethtool_link_ksettings_add_link_mode(ks, advertising,
  495. 40000baseLR4_Full);
  496. }
  497. if (phy_types & I40E_CAP_PHY_TYPE_20GBASE_KR2) {
  498. ethtool_link_ksettings_add_link_mode(ks, supported,
  499. 20000baseKR2_Full);
  500. if (hw_link_info->requested_speeds & I40E_LINK_SPEED_20GB)
  501. ethtool_link_ksettings_add_link_mode(ks, advertising,
  502. 20000baseKR2_Full);
  503. }
  504. if (phy_types & I40E_CAP_PHY_TYPE_10GBASE_KX4) {
  505. ethtool_link_ksettings_add_link_mode(ks, supported,
  506. 10000baseKX4_Full);
  507. if (hw_link_info->requested_speeds & I40E_LINK_SPEED_10GB)
  508. ethtool_link_ksettings_add_link_mode(ks, advertising,
  509. 10000baseKX4_Full);
  510. }
  511. if (phy_types & I40E_CAP_PHY_TYPE_10GBASE_KR &&
  512. !(pf->hw_features & I40E_HW_HAVE_CRT_RETIMER)) {
  513. ethtool_link_ksettings_add_link_mode(ks, supported,
  514. 10000baseKR_Full);
  515. if (hw_link_info->requested_speeds & I40E_LINK_SPEED_10GB)
  516. ethtool_link_ksettings_add_link_mode(ks, advertising,
  517. 10000baseKR_Full);
  518. }
  519. if (phy_types & I40E_CAP_PHY_TYPE_1000BASE_KX &&
  520. !(pf->hw_features & I40E_HW_HAVE_CRT_RETIMER)) {
  521. ethtool_link_ksettings_add_link_mode(ks, supported,
  522. 1000baseKX_Full);
  523. if (hw_link_info->requested_speeds & I40E_LINK_SPEED_1GB)
  524. ethtool_link_ksettings_add_link_mode(ks, advertising,
  525. 1000baseKX_Full);
  526. }
  527. /* need to add 25G PHY types */
  528. if (phy_types & I40E_CAP_PHY_TYPE_25GBASE_KR) {
  529. ethtool_link_ksettings_add_link_mode(ks, supported,
  530. 25000baseKR_Full);
  531. if (hw_link_info->requested_speeds & I40E_LINK_SPEED_25GB)
  532. ethtool_link_ksettings_add_link_mode(ks, advertising,
  533. 25000baseKR_Full);
  534. }
  535. if (phy_types & I40E_CAP_PHY_TYPE_25GBASE_CR) {
  536. ethtool_link_ksettings_add_link_mode(ks, supported,
  537. 25000baseCR_Full);
  538. if (hw_link_info->requested_speeds & I40E_LINK_SPEED_25GB)
  539. ethtool_link_ksettings_add_link_mode(ks, advertising,
  540. 25000baseCR_Full);
  541. }
  542. if (phy_types & I40E_CAP_PHY_TYPE_25GBASE_SR ||
  543. phy_types & I40E_CAP_PHY_TYPE_25GBASE_LR) {
  544. ethtool_link_ksettings_add_link_mode(ks, supported,
  545. 25000baseSR_Full);
  546. if (hw_link_info->requested_speeds & I40E_LINK_SPEED_25GB)
  547. ethtool_link_ksettings_add_link_mode(ks, advertising,
  548. 25000baseSR_Full);
  549. }
  550. if (phy_types & I40E_CAP_PHY_TYPE_25GBASE_AOC ||
  551. phy_types & I40E_CAP_PHY_TYPE_25GBASE_ACC) {
  552. ethtool_link_ksettings_add_link_mode(ks, supported,
  553. 25000baseCR_Full);
  554. if (hw_link_info->requested_speeds & I40E_LINK_SPEED_25GB)
  555. ethtool_link_ksettings_add_link_mode(ks, advertising,
  556. 25000baseCR_Full);
  557. }
  558. /* need to add new 10G PHY types */
  559. if (phy_types & I40E_CAP_PHY_TYPE_10GBASE_CR1 ||
  560. phy_types & I40E_CAP_PHY_TYPE_10GBASE_CR1_CU) {
  561. ethtool_link_ksettings_add_link_mode(ks, supported,
  562. 10000baseCR_Full);
  563. if (hw_link_info->requested_speeds & I40E_LINK_SPEED_10GB)
  564. ethtool_link_ksettings_add_link_mode(ks, advertising,
  565. 10000baseCR_Full);
  566. }
  567. if (phy_types & I40E_CAP_PHY_TYPE_10GBASE_SR) {
  568. ethtool_link_ksettings_add_link_mode(ks, supported,
  569. 10000baseSR_Full);
  570. if (hw_link_info->requested_speeds & I40E_LINK_SPEED_10GB)
  571. ethtool_link_ksettings_add_link_mode(ks, advertising,
  572. 10000baseSR_Full);
  573. }
  574. if (phy_types & I40E_CAP_PHY_TYPE_10GBASE_LR) {
  575. ethtool_link_ksettings_add_link_mode(ks, supported,
  576. 10000baseLR_Full);
  577. if (hw_link_info->requested_speeds & I40E_LINK_SPEED_10GB)
  578. ethtool_link_ksettings_add_link_mode(ks, advertising,
  579. 10000baseLR_Full);
  580. }
  581. if (phy_types & I40E_CAP_PHY_TYPE_1000BASE_SX ||
  582. phy_types & I40E_CAP_PHY_TYPE_1000BASE_LX ||
  583. phy_types & I40E_CAP_PHY_TYPE_1000BASE_T_OPTICAL) {
  584. ethtool_link_ksettings_add_link_mode(ks, supported,
  585. 1000baseX_Full);
  586. if (hw_link_info->requested_speeds & I40E_LINK_SPEED_1GB)
  587. ethtool_link_ksettings_add_link_mode(ks, advertising,
  588. 1000baseX_Full);
  589. }
  590. /* Autoneg PHY types */
  591. if (phy_types & I40E_CAP_PHY_TYPE_SGMII ||
  592. phy_types & I40E_CAP_PHY_TYPE_40GBASE_KR4 ||
  593. phy_types & I40E_CAP_PHY_TYPE_40GBASE_CR4_CU ||
  594. phy_types & I40E_CAP_PHY_TYPE_40GBASE_CR4 ||
  595. phy_types & I40E_CAP_PHY_TYPE_25GBASE_SR ||
  596. phy_types & I40E_CAP_PHY_TYPE_25GBASE_LR ||
  597. phy_types & I40E_CAP_PHY_TYPE_25GBASE_KR ||
  598. phy_types & I40E_CAP_PHY_TYPE_25GBASE_CR ||
  599. phy_types & I40E_CAP_PHY_TYPE_20GBASE_KR2 ||
  600. phy_types & I40E_CAP_PHY_TYPE_10GBASE_T ||
  601. phy_types & I40E_CAP_PHY_TYPE_10GBASE_SR ||
  602. phy_types & I40E_CAP_PHY_TYPE_10GBASE_LR ||
  603. phy_types & I40E_CAP_PHY_TYPE_10GBASE_KX4 ||
  604. phy_types & I40E_CAP_PHY_TYPE_10GBASE_KR ||
  605. phy_types & I40E_CAP_PHY_TYPE_10GBASE_CR1_CU ||
  606. phy_types & I40E_CAP_PHY_TYPE_10GBASE_CR1 ||
  607. phy_types & I40E_CAP_PHY_TYPE_1000BASE_T_OPTICAL ||
  608. phy_types & I40E_CAP_PHY_TYPE_1000BASE_T ||
  609. phy_types & I40E_CAP_PHY_TYPE_1000BASE_SX ||
  610. phy_types & I40E_CAP_PHY_TYPE_1000BASE_LX ||
  611. phy_types & I40E_CAP_PHY_TYPE_1000BASE_KX ||
  612. phy_types & I40E_CAP_PHY_TYPE_100BASE_TX) {
  613. ethtool_link_ksettings_add_link_mode(ks, supported,
  614. Autoneg);
  615. ethtool_link_ksettings_add_link_mode(ks, advertising,
  616. Autoneg);
  617. }
  618. }
  619. /**
  620. * i40e_get_settings_link_up - Get the Link settings for when link is up
  621. * @hw: hw structure
  622. * @ks: ethtool ksettings to fill in
  623. * @netdev: network interface device structure
  624. * @pf: pointer to physical function struct
  625. **/
  626. static void i40e_get_settings_link_up(struct i40e_hw *hw,
  627. struct ethtool_link_ksettings *ks,
  628. struct net_device *netdev,
  629. struct i40e_pf *pf)
  630. {
  631. struct i40e_link_status *hw_link_info = &hw->phy.link_info;
  632. struct ethtool_link_ksettings cap_ksettings;
  633. u32 link_speed = hw_link_info->link_speed;
  634. /* Initialize supported and advertised settings based on phy settings */
  635. switch (hw_link_info->phy_type) {
  636. case I40E_PHY_TYPE_40GBASE_CR4:
  637. case I40E_PHY_TYPE_40GBASE_CR4_CU:
  638. ethtool_link_ksettings_add_link_mode(ks, supported, Autoneg);
  639. ethtool_link_ksettings_add_link_mode(ks, supported,
  640. 40000baseCR4_Full);
  641. ethtool_link_ksettings_add_link_mode(ks, advertising, Autoneg);
  642. ethtool_link_ksettings_add_link_mode(ks, advertising,
  643. 40000baseCR4_Full);
  644. break;
  645. case I40E_PHY_TYPE_XLAUI:
  646. case I40E_PHY_TYPE_XLPPI:
  647. case I40E_PHY_TYPE_40GBASE_AOC:
  648. ethtool_link_ksettings_add_link_mode(ks, supported,
  649. 40000baseCR4_Full);
  650. break;
  651. case I40E_PHY_TYPE_40GBASE_SR4:
  652. ethtool_link_ksettings_add_link_mode(ks, supported,
  653. 40000baseSR4_Full);
  654. break;
  655. case I40E_PHY_TYPE_40GBASE_LR4:
  656. ethtool_link_ksettings_add_link_mode(ks, supported,
  657. 40000baseLR4_Full);
  658. break;
  659. case I40E_PHY_TYPE_25GBASE_SR:
  660. case I40E_PHY_TYPE_25GBASE_LR:
  661. case I40E_PHY_TYPE_10GBASE_SR:
  662. case I40E_PHY_TYPE_10GBASE_LR:
  663. case I40E_PHY_TYPE_1000BASE_SX:
  664. case I40E_PHY_TYPE_1000BASE_LX:
  665. ethtool_link_ksettings_add_link_mode(ks, supported, Autoneg);
  666. ethtool_link_ksettings_add_link_mode(ks, advertising, Autoneg);
  667. ethtool_link_ksettings_add_link_mode(ks, supported,
  668. 25000baseSR_Full);
  669. ethtool_link_ksettings_add_link_mode(ks, advertising,
  670. 25000baseSR_Full);
  671. ethtool_link_ksettings_add_link_mode(ks, supported,
  672. 10000baseSR_Full);
  673. ethtool_link_ksettings_add_link_mode(ks, advertising,
  674. 10000baseSR_Full);
  675. ethtool_link_ksettings_add_link_mode(ks, supported,
  676. 10000baseLR_Full);
  677. ethtool_link_ksettings_add_link_mode(ks, advertising,
  678. 10000baseLR_Full);
  679. ethtool_link_ksettings_add_link_mode(ks, supported,
  680. 1000baseX_Full);
  681. ethtool_link_ksettings_add_link_mode(ks, advertising,
  682. 1000baseX_Full);
  683. ethtool_link_ksettings_add_link_mode(ks, supported,
  684. 10000baseT_Full);
  685. if (hw_link_info->module_type[2] &
  686. I40E_MODULE_TYPE_1000BASE_SX ||
  687. hw_link_info->module_type[2] &
  688. I40E_MODULE_TYPE_1000BASE_LX) {
  689. ethtool_link_ksettings_add_link_mode(ks, supported,
  690. 1000baseT_Full);
  691. if (hw_link_info->requested_speeds &
  692. I40E_LINK_SPEED_1GB)
  693. ethtool_link_ksettings_add_link_mode(
  694. ks, advertising, 1000baseT_Full);
  695. }
  696. if (hw_link_info->requested_speeds & I40E_LINK_SPEED_10GB)
  697. ethtool_link_ksettings_add_link_mode(ks, advertising,
  698. 10000baseT_Full);
  699. break;
  700. case I40E_PHY_TYPE_10GBASE_T:
  701. case I40E_PHY_TYPE_1000BASE_T:
  702. case I40E_PHY_TYPE_100BASE_TX:
  703. ethtool_link_ksettings_add_link_mode(ks, supported, Autoneg);
  704. ethtool_link_ksettings_add_link_mode(ks, supported,
  705. 10000baseT_Full);
  706. ethtool_link_ksettings_add_link_mode(ks, supported,
  707. 1000baseT_Full);
  708. ethtool_link_ksettings_add_link_mode(ks, supported,
  709. 100baseT_Full);
  710. ethtool_link_ksettings_add_link_mode(ks, advertising, Autoneg);
  711. if (hw_link_info->requested_speeds & I40E_LINK_SPEED_10GB)
  712. ethtool_link_ksettings_add_link_mode(ks, advertising,
  713. 10000baseT_Full);
  714. if (hw_link_info->requested_speeds & I40E_LINK_SPEED_1GB)
  715. ethtool_link_ksettings_add_link_mode(ks, advertising,
  716. 1000baseT_Full);
  717. if (hw_link_info->requested_speeds & I40E_LINK_SPEED_100MB)
  718. ethtool_link_ksettings_add_link_mode(ks, advertising,
  719. 100baseT_Full);
  720. break;
  721. case I40E_PHY_TYPE_1000BASE_T_OPTICAL:
  722. ethtool_link_ksettings_add_link_mode(ks, supported, Autoneg);
  723. ethtool_link_ksettings_add_link_mode(ks, supported,
  724. 1000baseT_Full);
  725. ethtool_link_ksettings_add_link_mode(ks, advertising, Autoneg);
  726. ethtool_link_ksettings_add_link_mode(ks, advertising,
  727. 1000baseT_Full);
  728. break;
  729. case I40E_PHY_TYPE_10GBASE_CR1_CU:
  730. case I40E_PHY_TYPE_10GBASE_CR1:
  731. ethtool_link_ksettings_add_link_mode(ks, supported, Autoneg);
  732. ethtool_link_ksettings_add_link_mode(ks, supported,
  733. 10000baseT_Full);
  734. ethtool_link_ksettings_add_link_mode(ks, advertising, Autoneg);
  735. ethtool_link_ksettings_add_link_mode(ks, advertising,
  736. 10000baseT_Full);
  737. break;
  738. case I40E_PHY_TYPE_XAUI:
  739. case I40E_PHY_TYPE_XFI:
  740. case I40E_PHY_TYPE_SFI:
  741. case I40E_PHY_TYPE_10GBASE_SFPP_CU:
  742. case I40E_PHY_TYPE_10GBASE_AOC:
  743. ethtool_link_ksettings_add_link_mode(ks, supported,
  744. 10000baseT_Full);
  745. if (hw_link_info->requested_speeds & I40E_LINK_SPEED_10GB)
  746. ethtool_link_ksettings_add_link_mode(ks, advertising,
  747. 10000baseT_Full);
  748. break;
  749. case I40E_PHY_TYPE_SGMII:
  750. ethtool_link_ksettings_add_link_mode(ks, supported, Autoneg);
  751. ethtool_link_ksettings_add_link_mode(ks, supported,
  752. 1000baseT_Full);
  753. if (hw_link_info->requested_speeds & I40E_LINK_SPEED_1GB)
  754. ethtool_link_ksettings_add_link_mode(ks, advertising,
  755. 1000baseT_Full);
  756. if (pf->hw_features & I40E_HW_100M_SGMII_CAPABLE) {
  757. ethtool_link_ksettings_add_link_mode(ks, supported,
  758. 100baseT_Full);
  759. if (hw_link_info->requested_speeds &
  760. I40E_LINK_SPEED_100MB)
  761. ethtool_link_ksettings_add_link_mode(
  762. ks, advertising, 100baseT_Full);
  763. }
  764. break;
  765. case I40E_PHY_TYPE_40GBASE_KR4:
  766. case I40E_PHY_TYPE_25GBASE_KR:
  767. case I40E_PHY_TYPE_20GBASE_KR2:
  768. case I40E_PHY_TYPE_10GBASE_KR:
  769. case I40E_PHY_TYPE_10GBASE_KX4:
  770. case I40E_PHY_TYPE_1000BASE_KX:
  771. ethtool_link_ksettings_add_link_mode(ks, supported,
  772. 40000baseKR4_Full);
  773. ethtool_link_ksettings_add_link_mode(ks, supported,
  774. 25000baseKR_Full);
  775. ethtool_link_ksettings_add_link_mode(ks, supported,
  776. 20000baseKR2_Full);
  777. ethtool_link_ksettings_add_link_mode(ks, supported,
  778. 10000baseKR_Full);
  779. ethtool_link_ksettings_add_link_mode(ks, supported,
  780. 10000baseKX4_Full);
  781. ethtool_link_ksettings_add_link_mode(ks, supported,
  782. 1000baseKX_Full);
  783. ethtool_link_ksettings_add_link_mode(ks, supported, Autoneg);
  784. ethtool_link_ksettings_add_link_mode(ks, advertising,
  785. 40000baseKR4_Full);
  786. ethtool_link_ksettings_add_link_mode(ks, advertising,
  787. 25000baseKR_Full);
  788. ethtool_link_ksettings_add_link_mode(ks, advertising,
  789. 20000baseKR2_Full);
  790. ethtool_link_ksettings_add_link_mode(ks, advertising,
  791. 10000baseKR_Full);
  792. ethtool_link_ksettings_add_link_mode(ks, advertising,
  793. 10000baseKX4_Full);
  794. ethtool_link_ksettings_add_link_mode(ks, advertising,
  795. 1000baseKX_Full);
  796. ethtool_link_ksettings_add_link_mode(ks, advertising, Autoneg);
  797. break;
  798. case I40E_PHY_TYPE_25GBASE_CR:
  799. ethtool_link_ksettings_add_link_mode(ks, supported, Autoneg);
  800. ethtool_link_ksettings_add_link_mode(ks, advertising, Autoneg);
  801. ethtool_link_ksettings_add_link_mode(ks, supported,
  802. 25000baseCR_Full);
  803. ethtool_link_ksettings_add_link_mode(ks, advertising,
  804. 25000baseCR_Full);
  805. break;
  806. case I40E_PHY_TYPE_25GBASE_AOC:
  807. case I40E_PHY_TYPE_25GBASE_ACC:
  808. ethtool_link_ksettings_add_link_mode(ks, supported, Autoneg);
  809. ethtool_link_ksettings_add_link_mode(ks, advertising, Autoneg);
  810. ethtool_link_ksettings_add_link_mode(ks, supported,
  811. 25000baseCR_Full);
  812. ethtool_link_ksettings_add_link_mode(ks, advertising,
  813. 25000baseCR_Full);
  814. ethtool_link_ksettings_add_link_mode(ks, supported,
  815. 10000baseCR_Full);
  816. ethtool_link_ksettings_add_link_mode(ks, advertising,
  817. 10000baseCR_Full);
  818. break;
  819. default:
  820. /* if we got here and link is up something bad is afoot */
  821. netdev_info(netdev,
  822. "WARNING: Link is up but PHY type 0x%x is not recognized.\n",
  823. hw_link_info->phy_type);
  824. }
  825. /* Now that we've worked out everything that could be supported by the
  826. * current PHY type, get what is supported by the NVM and intersect
  827. * them to get what is truly supported
  828. */
  829. memset(&cap_ksettings, 0, sizeof(struct ethtool_link_ksettings));
  830. i40e_phy_type_to_ethtool(pf, &cap_ksettings);
  831. ethtool_intersect_link_masks(ks, &cap_ksettings);
  832. /* Set speed and duplex */
  833. switch (link_speed) {
  834. case I40E_LINK_SPEED_40GB:
  835. ks->base.speed = SPEED_40000;
  836. break;
  837. case I40E_LINK_SPEED_25GB:
  838. ks->base.speed = SPEED_25000;
  839. break;
  840. case I40E_LINK_SPEED_20GB:
  841. ks->base.speed = SPEED_20000;
  842. break;
  843. case I40E_LINK_SPEED_10GB:
  844. ks->base.speed = SPEED_10000;
  845. break;
  846. case I40E_LINK_SPEED_1GB:
  847. ks->base.speed = SPEED_1000;
  848. break;
  849. case I40E_LINK_SPEED_100MB:
  850. ks->base.speed = SPEED_100;
  851. break;
  852. default:
  853. break;
  854. }
  855. ks->base.duplex = DUPLEX_FULL;
  856. }
  857. /**
  858. * i40e_get_settings_link_down - Get the Link settings for when link is down
  859. * @hw: hw structure
  860. * @ks: ethtool ksettings to fill in
  861. * @pf: pointer to physical function struct
  862. *
  863. * Reports link settings that can be determined when link is down
  864. **/
  865. static void i40e_get_settings_link_down(struct i40e_hw *hw,
  866. struct ethtool_link_ksettings *ks,
  867. struct i40e_pf *pf)
  868. {
  869. /* link is down and the driver needs to fall back on
  870. * supported phy types to figure out what info to display
  871. */
  872. i40e_phy_type_to_ethtool(pf, ks);
  873. /* With no link speed and duplex are unknown */
  874. ks->base.speed = SPEED_UNKNOWN;
  875. ks->base.duplex = DUPLEX_UNKNOWN;
  876. }
  877. /**
  878. * i40e_get_link_ksettings - Get Link Speed and Duplex settings
  879. * @netdev: network interface device structure
  880. * @ks: ethtool ksettings
  881. *
  882. * Reports speed/duplex settings based on media_type
  883. **/
  884. static int i40e_get_link_ksettings(struct net_device *netdev,
  885. struct ethtool_link_ksettings *ks)
  886. {
  887. struct i40e_netdev_priv *np = netdev_priv(netdev);
  888. struct i40e_pf *pf = np->vsi->back;
  889. struct i40e_hw *hw = &pf->hw;
  890. struct i40e_link_status *hw_link_info = &hw->phy.link_info;
  891. bool link_up = hw_link_info->link_info & I40E_AQ_LINK_UP;
  892. ethtool_link_ksettings_zero_link_mode(ks, supported);
  893. ethtool_link_ksettings_zero_link_mode(ks, advertising);
  894. if (link_up)
  895. i40e_get_settings_link_up(hw, ks, netdev, pf);
  896. else
  897. i40e_get_settings_link_down(hw, ks, pf);
  898. /* Now set the settings that don't rely on link being up/down */
  899. /* Set autoneg settings */
  900. ks->base.autoneg = ((hw_link_info->an_info & I40E_AQ_AN_COMPLETED) ?
  901. AUTONEG_ENABLE : AUTONEG_DISABLE);
  902. /* Set media type settings */
  903. switch (hw->phy.media_type) {
  904. case I40E_MEDIA_TYPE_BACKPLANE:
  905. ethtool_link_ksettings_add_link_mode(ks, supported, Autoneg);
  906. ethtool_link_ksettings_add_link_mode(ks, supported, Backplane);
  907. ethtool_link_ksettings_add_link_mode(ks, advertising, Autoneg);
  908. ethtool_link_ksettings_add_link_mode(ks, advertising,
  909. Backplane);
  910. ks->base.port = PORT_NONE;
  911. break;
  912. case I40E_MEDIA_TYPE_BASET:
  913. ethtool_link_ksettings_add_link_mode(ks, supported, TP);
  914. ethtool_link_ksettings_add_link_mode(ks, advertising, TP);
  915. ks->base.port = PORT_TP;
  916. break;
  917. case I40E_MEDIA_TYPE_DA:
  918. case I40E_MEDIA_TYPE_CX4:
  919. ethtool_link_ksettings_add_link_mode(ks, supported, FIBRE);
  920. ethtool_link_ksettings_add_link_mode(ks, advertising, FIBRE);
  921. ks->base.port = PORT_DA;
  922. break;
  923. case I40E_MEDIA_TYPE_FIBER:
  924. ethtool_link_ksettings_add_link_mode(ks, supported, FIBRE);
  925. ks->base.port = PORT_FIBRE;
  926. break;
  927. case I40E_MEDIA_TYPE_UNKNOWN:
  928. default:
  929. ks->base.port = PORT_OTHER;
  930. break;
  931. }
  932. /* Set flow control settings */
  933. ethtool_link_ksettings_add_link_mode(ks, supported, Pause);
  934. switch (hw->fc.requested_mode) {
  935. case I40E_FC_FULL:
  936. ethtool_link_ksettings_add_link_mode(ks, advertising, Pause);
  937. break;
  938. case I40E_FC_TX_PAUSE:
  939. ethtool_link_ksettings_add_link_mode(ks, advertising,
  940. Asym_Pause);
  941. break;
  942. case I40E_FC_RX_PAUSE:
  943. ethtool_link_ksettings_add_link_mode(ks, advertising, Pause);
  944. ethtool_link_ksettings_add_link_mode(ks, advertising,
  945. Asym_Pause);
  946. break;
  947. default:
  948. ethtool_link_ksettings_del_link_mode(ks, advertising, Pause);
  949. ethtool_link_ksettings_del_link_mode(ks, advertising,
  950. Asym_Pause);
  951. break;
  952. }
  953. return 0;
  954. }
  955. /**
  956. * i40e_set_link_ksettings - Set Speed and Duplex
  957. * @netdev: network interface device structure
  958. * @ks: ethtool ksettings
  959. *
  960. * Set speed/duplex per media_types advertised/forced
  961. **/
  962. static int i40e_set_link_ksettings(struct net_device *netdev,
  963. const struct ethtool_link_ksettings *ks)
  964. {
  965. struct i40e_netdev_priv *np = netdev_priv(netdev);
  966. struct i40e_aq_get_phy_abilities_resp abilities;
  967. struct ethtool_link_ksettings safe_ks;
  968. struct ethtool_link_ksettings copy_ks;
  969. struct i40e_aq_set_phy_config config;
  970. struct i40e_pf *pf = np->vsi->back;
  971. struct i40e_vsi *vsi = np->vsi;
  972. struct i40e_hw *hw = &pf->hw;
  973. bool autoneg_changed = false;
  974. i40e_status status = 0;
  975. int timeout = 50;
  976. int err = 0;
  977. u8 autoneg;
  978. /* Changing port settings is not supported if this isn't the
  979. * port's controlling PF
  980. */
  981. if (hw->partition_id != 1) {
  982. i40e_partition_setting_complaint(pf);
  983. return -EOPNOTSUPP;
  984. }
  985. if (vsi != pf->vsi[pf->lan_vsi])
  986. return -EOPNOTSUPP;
  987. if (hw->phy.media_type != I40E_MEDIA_TYPE_BASET &&
  988. hw->phy.media_type != I40E_MEDIA_TYPE_FIBER &&
  989. hw->phy.media_type != I40E_MEDIA_TYPE_BACKPLANE &&
  990. hw->phy.media_type != I40E_MEDIA_TYPE_DA &&
  991. hw->phy.link_info.link_info & I40E_AQ_LINK_UP)
  992. return -EOPNOTSUPP;
  993. if (hw->device_id == I40E_DEV_ID_KX_B ||
  994. hw->device_id == I40E_DEV_ID_KX_C ||
  995. hw->device_id == I40E_DEV_ID_20G_KR2 ||
  996. hw->device_id == I40E_DEV_ID_20G_KR2_A ||
  997. hw->device_id == I40E_DEV_ID_25G_B ||
  998. hw->device_id == I40E_DEV_ID_KX_X722) {
  999. netdev_info(netdev, "Changing settings is not supported on backplane.\n");
  1000. return -EOPNOTSUPP;
  1001. }
  1002. /* copy the ksettings to copy_ks to avoid modifying the origin */
  1003. memcpy(&copy_ks, ks, sizeof(struct ethtool_link_ksettings));
  1004. /* save autoneg out of ksettings */
  1005. autoneg = copy_ks.base.autoneg;
  1006. /* get our own copy of the bits to check against */
  1007. memset(&safe_ks, 0, sizeof(struct ethtool_link_ksettings));
  1008. safe_ks.base.cmd = copy_ks.base.cmd;
  1009. safe_ks.base.link_mode_masks_nwords =
  1010. copy_ks.base.link_mode_masks_nwords;
  1011. i40e_get_link_ksettings(netdev, &safe_ks);
  1012. /* Get link modes supported by hardware and check against modes
  1013. * requested by the user. Return an error if unsupported mode was set.
  1014. */
  1015. if (!bitmap_subset(copy_ks.link_modes.advertising,
  1016. safe_ks.link_modes.supported,
  1017. __ETHTOOL_LINK_MODE_MASK_NBITS))
  1018. return -EINVAL;
  1019. /* set autoneg back to what it currently is */
  1020. copy_ks.base.autoneg = safe_ks.base.autoneg;
  1021. /* If copy_ks.base and safe_ks.base are not the same now, then they are
  1022. * trying to set something that we do not support.
  1023. */
  1024. if (memcmp(&copy_ks.base, &safe_ks.base,
  1025. sizeof(struct ethtool_link_settings)))
  1026. return -EOPNOTSUPP;
  1027. while (test_and_set_bit(__I40E_CONFIG_BUSY, pf->state)) {
  1028. timeout--;
  1029. if (!timeout)
  1030. return -EBUSY;
  1031. usleep_range(1000, 2000);
  1032. }
  1033. /* Get the current phy config */
  1034. status = i40e_aq_get_phy_capabilities(hw, false, false, &abilities,
  1035. NULL);
  1036. if (status) {
  1037. err = -EAGAIN;
  1038. goto done;
  1039. }
  1040. /* Copy abilities to config in case autoneg is not
  1041. * set below
  1042. */
  1043. memset(&config, 0, sizeof(struct i40e_aq_set_phy_config));
  1044. config.abilities = abilities.abilities;
  1045. /* Check autoneg */
  1046. if (autoneg == AUTONEG_ENABLE) {
  1047. /* If autoneg was not already enabled */
  1048. if (!(hw->phy.link_info.an_info & I40E_AQ_AN_COMPLETED)) {
  1049. /* If autoneg is not supported, return error */
  1050. if (!ethtool_link_ksettings_test_link_mode(&safe_ks,
  1051. supported,
  1052. Autoneg)) {
  1053. netdev_info(netdev, "Autoneg not supported on this phy\n");
  1054. err = -EINVAL;
  1055. goto done;
  1056. }
  1057. /* Autoneg is allowed to change */
  1058. config.abilities = abilities.abilities |
  1059. I40E_AQ_PHY_ENABLE_AN;
  1060. autoneg_changed = true;
  1061. }
  1062. } else {
  1063. /* If autoneg is currently enabled */
  1064. if (hw->phy.link_info.an_info & I40E_AQ_AN_COMPLETED) {
  1065. /* If autoneg is supported 10GBASE_T is the only PHY
  1066. * that can disable it, so otherwise return error
  1067. */
  1068. if (ethtool_link_ksettings_test_link_mode(&safe_ks,
  1069. supported,
  1070. Autoneg) &&
  1071. hw->phy.link_info.phy_type !=
  1072. I40E_PHY_TYPE_10GBASE_T) {
  1073. netdev_info(netdev, "Autoneg cannot be disabled on this phy\n");
  1074. err = -EINVAL;
  1075. goto done;
  1076. }
  1077. /* Autoneg is allowed to change */
  1078. config.abilities = abilities.abilities &
  1079. ~I40E_AQ_PHY_ENABLE_AN;
  1080. autoneg_changed = true;
  1081. }
  1082. }
  1083. if (ethtool_link_ksettings_test_link_mode(ks, advertising,
  1084. 100baseT_Full))
  1085. config.link_speed |= I40E_LINK_SPEED_100MB;
  1086. if (ethtool_link_ksettings_test_link_mode(ks, advertising,
  1087. 1000baseT_Full) ||
  1088. ethtool_link_ksettings_test_link_mode(ks, advertising,
  1089. 1000baseX_Full) ||
  1090. ethtool_link_ksettings_test_link_mode(ks, advertising,
  1091. 1000baseKX_Full))
  1092. config.link_speed |= I40E_LINK_SPEED_1GB;
  1093. if (ethtool_link_ksettings_test_link_mode(ks, advertising,
  1094. 10000baseT_Full) ||
  1095. ethtool_link_ksettings_test_link_mode(ks, advertising,
  1096. 10000baseKX4_Full) ||
  1097. ethtool_link_ksettings_test_link_mode(ks, advertising,
  1098. 10000baseKR_Full) ||
  1099. ethtool_link_ksettings_test_link_mode(ks, advertising,
  1100. 10000baseCR_Full) ||
  1101. ethtool_link_ksettings_test_link_mode(ks, advertising,
  1102. 10000baseSR_Full) ||
  1103. ethtool_link_ksettings_test_link_mode(ks, advertising,
  1104. 10000baseLR_Full))
  1105. config.link_speed |= I40E_LINK_SPEED_10GB;
  1106. if (ethtool_link_ksettings_test_link_mode(ks, advertising,
  1107. 20000baseKR2_Full))
  1108. config.link_speed |= I40E_LINK_SPEED_20GB;
  1109. if (ethtool_link_ksettings_test_link_mode(ks, advertising,
  1110. 25000baseCR_Full) ||
  1111. ethtool_link_ksettings_test_link_mode(ks, advertising,
  1112. 25000baseKR_Full) ||
  1113. ethtool_link_ksettings_test_link_mode(ks, advertising,
  1114. 25000baseSR_Full))
  1115. config.link_speed |= I40E_LINK_SPEED_25GB;
  1116. if (ethtool_link_ksettings_test_link_mode(ks, advertising,
  1117. 40000baseKR4_Full) ||
  1118. ethtool_link_ksettings_test_link_mode(ks, advertising,
  1119. 40000baseCR4_Full) ||
  1120. ethtool_link_ksettings_test_link_mode(ks, advertising,
  1121. 40000baseSR4_Full) ||
  1122. ethtool_link_ksettings_test_link_mode(ks, advertising,
  1123. 40000baseLR4_Full))
  1124. config.link_speed |= I40E_LINK_SPEED_40GB;
  1125. /* If speed didn't get set, set it to what it currently is.
  1126. * This is needed because if advertise is 0 (as it is when autoneg
  1127. * is disabled) then speed won't get set.
  1128. */
  1129. if (!config.link_speed)
  1130. config.link_speed = abilities.link_speed;
  1131. if (autoneg_changed || abilities.link_speed != config.link_speed) {
  1132. /* copy over the rest of the abilities */
  1133. config.phy_type = abilities.phy_type;
  1134. config.phy_type_ext = abilities.phy_type_ext;
  1135. config.eee_capability = abilities.eee_capability;
  1136. config.eeer = abilities.eeer_val;
  1137. config.low_power_ctrl = abilities.d3_lpan;
  1138. config.fec_config = abilities.fec_cfg_curr_mod_ext_info &
  1139. I40E_AQ_PHY_FEC_CONFIG_MASK;
  1140. /* save the requested speeds */
  1141. hw->phy.link_info.requested_speeds = config.link_speed;
  1142. /* set link and auto negotiation so changes take effect */
  1143. config.abilities |= I40E_AQ_PHY_ENABLE_ATOMIC_LINK;
  1144. /* If link is up put link down */
  1145. if (hw->phy.link_info.link_info & I40E_AQ_LINK_UP) {
  1146. /* Tell the OS link is going down, the link will go
  1147. * back up when fw says it is ready asynchronously
  1148. */
  1149. i40e_print_link_message(vsi, false);
  1150. netif_carrier_off(netdev);
  1151. netif_tx_stop_all_queues(netdev);
  1152. }
  1153. /* make the aq call */
  1154. status = i40e_aq_set_phy_config(hw, &config, NULL);
  1155. if (status) {
  1156. netdev_info(netdev,
  1157. "Set phy config failed, err %s aq_err %s\n",
  1158. i40e_stat_str(hw, status),
  1159. i40e_aq_str(hw, hw->aq.asq_last_status));
  1160. err = -EAGAIN;
  1161. goto done;
  1162. }
  1163. status = i40e_update_link_info(hw);
  1164. if (status)
  1165. netdev_dbg(netdev,
  1166. "Updating link info failed with err %s aq_err %s\n",
  1167. i40e_stat_str(hw, status),
  1168. i40e_aq_str(hw, hw->aq.asq_last_status));
  1169. } else {
  1170. netdev_info(netdev, "Nothing changed, exiting without setting anything.\n");
  1171. }
  1172. done:
  1173. clear_bit(__I40E_CONFIG_BUSY, pf->state);
  1174. return err;
  1175. }
  1176. static int i40e_nway_reset(struct net_device *netdev)
  1177. {
  1178. /* restart autonegotiation */
  1179. struct i40e_netdev_priv *np = netdev_priv(netdev);
  1180. struct i40e_pf *pf = np->vsi->back;
  1181. struct i40e_hw *hw = &pf->hw;
  1182. bool link_up = hw->phy.link_info.link_info & I40E_AQ_LINK_UP;
  1183. i40e_status ret = 0;
  1184. ret = i40e_aq_set_link_restart_an(hw, link_up, NULL);
  1185. if (ret) {
  1186. netdev_info(netdev, "link restart failed, err %s aq_err %s\n",
  1187. i40e_stat_str(hw, ret),
  1188. i40e_aq_str(hw, hw->aq.asq_last_status));
  1189. return -EIO;
  1190. }
  1191. return 0;
  1192. }
  1193. /**
  1194. * i40e_get_pauseparam - Get Flow Control status
  1195. * @netdev: netdevice structure
  1196. * @pause: buffer to return pause parameters
  1197. *
  1198. * Return tx/rx-pause status
  1199. **/
  1200. static void i40e_get_pauseparam(struct net_device *netdev,
  1201. struct ethtool_pauseparam *pause)
  1202. {
  1203. struct i40e_netdev_priv *np = netdev_priv(netdev);
  1204. struct i40e_pf *pf = np->vsi->back;
  1205. struct i40e_hw *hw = &pf->hw;
  1206. struct i40e_link_status *hw_link_info = &hw->phy.link_info;
  1207. struct i40e_dcbx_config *dcbx_cfg = &hw->local_dcbx_config;
  1208. pause->autoneg =
  1209. ((hw_link_info->an_info & I40E_AQ_AN_COMPLETED) ?
  1210. AUTONEG_ENABLE : AUTONEG_DISABLE);
  1211. /* PFC enabled so report LFC as off */
  1212. if (dcbx_cfg->pfc.pfcenable) {
  1213. pause->rx_pause = 0;
  1214. pause->tx_pause = 0;
  1215. return;
  1216. }
  1217. if (hw->fc.current_mode == I40E_FC_RX_PAUSE) {
  1218. pause->rx_pause = 1;
  1219. } else if (hw->fc.current_mode == I40E_FC_TX_PAUSE) {
  1220. pause->tx_pause = 1;
  1221. } else if (hw->fc.current_mode == I40E_FC_FULL) {
  1222. pause->rx_pause = 1;
  1223. pause->tx_pause = 1;
  1224. }
  1225. }
  1226. /**
  1227. * i40e_set_pauseparam - Set Flow Control parameter
  1228. * @netdev: network interface device structure
  1229. * @pause: return tx/rx flow control status
  1230. **/
  1231. static int i40e_set_pauseparam(struct net_device *netdev,
  1232. struct ethtool_pauseparam *pause)
  1233. {
  1234. struct i40e_netdev_priv *np = netdev_priv(netdev);
  1235. struct i40e_pf *pf = np->vsi->back;
  1236. struct i40e_vsi *vsi = np->vsi;
  1237. struct i40e_hw *hw = &pf->hw;
  1238. struct i40e_link_status *hw_link_info = &hw->phy.link_info;
  1239. struct i40e_dcbx_config *dcbx_cfg = &hw->local_dcbx_config;
  1240. bool link_up = hw_link_info->link_info & I40E_AQ_LINK_UP;
  1241. i40e_status status;
  1242. u8 aq_failures;
  1243. int err = 0;
  1244. /* Changing the port's flow control is not supported if this isn't the
  1245. * port's controlling PF
  1246. */
  1247. if (hw->partition_id != 1) {
  1248. i40e_partition_setting_complaint(pf);
  1249. return -EOPNOTSUPP;
  1250. }
  1251. if (vsi != pf->vsi[pf->lan_vsi])
  1252. return -EOPNOTSUPP;
  1253. if (pause->autoneg != ((hw_link_info->an_info & I40E_AQ_AN_COMPLETED) ?
  1254. AUTONEG_ENABLE : AUTONEG_DISABLE)) {
  1255. netdev_info(netdev, "To change autoneg please use: ethtool -s <dev> autoneg <on|off>\n");
  1256. return -EOPNOTSUPP;
  1257. }
  1258. /* If we have link and don't have autoneg */
  1259. if (!test_bit(__I40E_DOWN, pf->state) &&
  1260. !(hw_link_info->an_info & I40E_AQ_AN_COMPLETED)) {
  1261. /* Send message that it might not necessarily work*/
  1262. netdev_info(netdev, "Autoneg did not complete so changing settings may not result in an actual change.\n");
  1263. }
  1264. if (dcbx_cfg->pfc.pfcenable) {
  1265. netdev_info(netdev,
  1266. "Priority flow control enabled. Cannot set link flow control.\n");
  1267. return -EOPNOTSUPP;
  1268. }
  1269. if (pause->rx_pause && pause->tx_pause)
  1270. hw->fc.requested_mode = I40E_FC_FULL;
  1271. else if (pause->rx_pause && !pause->tx_pause)
  1272. hw->fc.requested_mode = I40E_FC_RX_PAUSE;
  1273. else if (!pause->rx_pause && pause->tx_pause)
  1274. hw->fc.requested_mode = I40E_FC_TX_PAUSE;
  1275. else if (!pause->rx_pause && !pause->tx_pause)
  1276. hw->fc.requested_mode = I40E_FC_NONE;
  1277. else
  1278. return -EINVAL;
  1279. /* Tell the OS link is going down, the link will go back up when fw
  1280. * says it is ready asynchronously
  1281. */
  1282. i40e_print_link_message(vsi, false);
  1283. netif_carrier_off(netdev);
  1284. netif_tx_stop_all_queues(netdev);
  1285. /* Set the fc mode and only restart an if link is up*/
  1286. status = i40e_set_fc(hw, &aq_failures, link_up);
  1287. if (aq_failures & I40E_SET_FC_AQ_FAIL_GET) {
  1288. netdev_info(netdev, "Set fc failed on the get_phy_capabilities call with err %s aq_err %s\n",
  1289. i40e_stat_str(hw, status),
  1290. i40e_aq_str(hw, hw->aq.asq_last_status));
  1291. err = -EAGAIN;
  1292. }
  1293. if (aq_failures & I40E_SET_FC_AQ_FAIL_SET) {
  1294. netdev_info(netdev, "Set fc failed on the set_phy_config call with err %s aq_err %s\n",
  1295. i40e_stat_str(hw, status),
  1296. i40e_aq_str(hw, hw->aq.asq_last_status));
  1297. err = -EAGAIN;
  1298. }
  1299. if (aq_failures & I40E_SET_FC_AQ_FAIL_UPDATE) {
  1300. netdev_info(netdev, "Set fc failed on the get_link_info call with err %s aq_err %s\n",
  1301. i40e_stat_str(hw, status),
  1302. i40e_aq_str(hw, hw->aq.asq_last_status));
  1303. err = -EAGAIN;
  1304. }
  1305. if (!test_bit(__I40E_DOWN, pf->state)) {
  1306. /* Give it a little more time to try to come back */
  1307. msleep(75);
  1308. if (!test_bit(__I40E_DOWN, pf->state))
  1309. return i40e_nway_reset(netdev);
  1310. }
  1311. return err;
  1312. }
  1313. static u32 i40e_get_msglevel(struct net_device *netdev)
  1314. {
  1315. struct i40e_netdev_priv *np = netdev_priv(netdev);
  1316. struct i40e_pf *pf = np->vsi->back;
  1317. u32 debug_mask = pf->hw.debug_mask;
  1318. if (debug_mask)
  1319. netdev_info(netdev, "i40e debug_mask: 0x%08X\n", debug_mask);
  1320. return pf->msg_enable;
  1321. }
  1322. static void i40e_set_msglevel(struct net_device *netdev, u32 data)
  1323. {
  1324. struct i40e_netdev_priv *np = netdev_priv(netdev);
  1325. struct i40e_pf *pf = np->vsi->back;
  1326. if (I40E_DEBUG_USER & data)
  1327. pf->hw.debug_mask = data;
  1328. else
  1329. pf->msg_enable = data;
  1330. }
  1331. static int i40e_get_regs_len(struct net_device *netdev)
  1332. {
  1333. int reg_count = 0;
  1334. int i;
  1335. for (i = 0; i40e_reg_list[i].offset != 0; i++)
  1336. reg_count += i40e_reg_list[i].elements;
  1337. return reg_count * sizeof(u32);
  1338. }
  1339. static void i40e_get_regs(struct net_device *netdev, struct ethtool_regs *regs,
  1340. void *p)
  1341. {
  1342. struct i40e_netdev_priv *np = netdev_priv(netdev);
  1343. struct i40e_pf *pf = np->vsi->back;
  1344. struct i40e_hw *hw = &pf->hw;
  1345. u32 *reg_buf = p;
  1346. unsigned int i, j, ri;
  1347. u32 reg;
  1348. /* Tell ethtool which driver-version-specific regs output we have.
  1349. *
  1350. * At some point, if we have ethtool doing special formatting of
  1351. * this data, it will rely on this version number to know how to
  1352. * interpret things. Hence, this needs to be updated if/when the
  1353. * diags register table is changed.
  1354. */
  1355. regs->version = 1;
  1356. /* loop through the diags reg table for what to print */
  1357. ri = 0;
  1358. for (i = 0; i40e_reg_list[i].offset != 0; i++) {
  1359. for (j = 0; j < i40e_reg_list[i].elements; j++) {
  1360. reg = i40e_reg_list[i].offset
  1361. + (j * i40e_reg_list[i].stride);
  1362. reg_buf[ri++] = rd32(hw, reg);
  1363. }
  1364. }
  1365. }
  1366. static int i40e_get_eeprom(struct net_device *netdev,
  1367. struct ethtool_eeprom *eeprom, u8 *bytes)
  1368. {
  1369. struct i40e_netdev_priv *np = netdev_priv(netdev);
  1370. struct i40e_hw *hw = &np->vsi->back->hw;
  1371. struct i40e_pf *pf = np->vsi->back;
  1372. int ret_val = 0, len, offset;
  1373. u8 *eeprom_buff;
  1374. u16 i, sectors;
  1375. bool last;
  1376. u32 magic;
  1377. #define I40E_NVM_SECTOR_SIZE 4096
  1378. if (eeprom->len == 0)
  1379. return -EINVAL;
  1380. /* check for NVMUpdate access method */
  1381. magic = hw->vendor_id | (hw->device_id << 16);
  1382. if (eeprom->magic && eeprom->magic != magic) {
  1383. struct i40e_nvm_access *cmd = (struct i40e_nvm_access *)eeprom;
  1384. int errno = 0;
  1385. /* make sure it is the right magic for NVMUpdate */
  1386. if ((eeprom->magic >> 16) != hw->device_id)
  1387. errno = -EINVAL;
  1388. else if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state) ||
  1389. test_bit(__I40E_RESET_INTR_RECEIVED, pf->state))
  1390. errno = -EBUSY;
  1391. else
  1392. ret_val = i40e_nvmupd_command(hw, cmd, bytes, &errno);
  1393. if ((errno || ret_val) && (hw->debug_mask & I40E_DEBUG_NVM))
  1394. dev_info(&pf->pdev->dev,
  1395. "NVMUpdate read failed err=%d status=0x%x errno=%d module=%d offset=0x%x size=%d\n",
  1396. ret_val, hw->aq.asq_last_status, errno,
  1397. (u8)(cmd->config & I40E_NVM_MOD_PNT_MASK),
  1398. cmd->offset, cmd->data_size);
  1399. return errno;
  1400. }
  1401. /* normal ethtool get_eeprom support */
  1402. eeprom->magic = hw->vendor_id | (hw->device_id << 16);
  1403. eeprom_buff = kzalloc(eeprom->len, GFP_KERNEL);
  1404. if (!eeprom_buff)
  1405. return -ENOMEM;
  1406. ret_val = i40e_acquire_nvm(hw, I40E_RESOURCE_READ);
  1407. if (ret_val) {
  1408. dev_info(&pf->pdev->dev,
  1409. "Failed Acquiring NVM resource for read err=%d status=0x%x\n",
  1410. ret_val, hw->aq.asq_last_status);
  1411. goto free_buff;
  1412. }
  1413. sectors = eeprom->len / I40E_NVM_SECTOR_SIZE;
  1414. sectors += (eeprom->len % I40E_NVM_SECTOR_SIZE) ? 1 : 0;
  1415. len = I40E_NVM_SECTOR_SIZE;
  1416. last = false;
  1417. for (i = 0; i < sectors; i++) {
  1418. if (i == (sectors - 1)) {
  1419. len = eeprom->len - (I40E_NVM_SECTOR_SIZE * i);
  1420. last = true;
  1421. }
  1422. offset = eeprom->offset + (I40E_NVM_SECTOR_SIZE * i),
  1423. ret_val = i40e_aq_read_nvm(hw, 0x0, offset, len,
  1424. (u8 *)eeprom_buff + (I40E_NVM_SECTOR_SIZE * i),
  1425. last, NULL);
  1426. if (ret_val && hw->aq.asq_last_status == I40E_AQ_RC_EPERM) {
  1427. dev_info(&pf->pdev->dev,
  1428. "read NVM failed, invalid offset 0x%x\n",
  1429. offset);
  1430. break;
  1431. } else if (ret_val &&
  1432. hw->aq.asq_last_status == I40E_AQ_RC_EACCES) {
  1433. dev_info(&pf->pdev->dev,
  1434. "read NVM failed, access, offset 0x%x\n",
  1435. offset);
  1436. break;
  1437. } else if (ret_val) {
  1438. dev_info(&pf->pdev->dev,
  1439. "read NVM failed offset %d err=%d status=0x%x\n",
  1440. offset, ret_val, hw->aq.asq_last_status);
  1441. break;
  1442. }
  1443. }
  1444. i40e_release_nvm(hw);
  1445. memcpy(bytes, (u8 *)eeprom_buff, eeprom->len);
  1446. free_buff:
  1447. kfree(eeprom_buff);
  1448. return ret_val;
  1449. }
  1450. static int i40e_get_eeprom_len(struct net_device *netdev)
  1451. {
  1452. struct i40e_netdev_priv *np = netdev_priv(netdev);
  1453. struct i40e_hw *hw = &np->vsi->back->hw;
  1454. u32 val;
  1455. #define X722_EEPROM_SCOPE_LIMIT 0x5B9FFF
  1456. if (hw->mac.type == I40E_MAC_X722) {
  1457. val = X722_EEPROM_SCOPE_LIMIT + 1;
  1458. return val;
  1459. }
  1460. val = (rd32(hw, I40E_GLPCI_LBARCTRL)
  1461. & I40E_GLPCI_LBARCTRL_FL_SIZE_MASK)
  1462. >> I40E_GLPCI_LBARCTRL_FL_SIZE_SHIFT;
  1463. /* register returns value in power of 2, 64Kbyte chunks. */
  1464. val = (64 * 1024) * BIT(val);
  1465. return val;
  1466. }
  1467. static int i40e_set_eeprom(struct net_device *netdev,
  1468. struct ethtool_eeprom *eeprom, u8 *bytes)
  1469. {
  1470. struct i40e_netdev_priv *np = netdev_priv(netdev);
  1471. struct i40e_hw *hw = &np->vsi->back->hw;
  1472. struct i40e_pf *pf = np->vsi->back;
  1473. struct i40e_nvm_access *cmd = (struct i40e_nvm_access *)eeprom;
  1474. int ret_val = 0;
  1475. int errno = 0;
  1476. u32 magic;
  1477. /* normal ethtool set_eeprom is not supported */
  1478. magic = hw->vendor_id | (hw->device_id << 16);
  1479. if (eeprom->magic == magic)
  1480. errno = -EOPNOTSUPP;
  1481. /* check for NVMUpdate access method */
  1482. else if (!eeprom->magic || (eeprom->magic >> 16) != hw->device_id)
  1483. errno = -EINVAL;
  1484. else if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state) ||
  1485. test_bit(__I40E_RESET_INTR_RECEIVED, pf->state))
  1486. errno = -EBUSY;
  1487. else
  1488. ret_val = i40e_nvmupd_command(hw, cmd, bytes, &errno);
  1489. if ((errno || ret_val) && (hw->debug_mask & I40E_DEBUG_NVM))
  1490. dev_info(&pf->pdev->dev,
  1491. "NVMUpdate write failed err=%d status=0x%x errno=%d module=%d offset=0x%x size=%d\n",
  1492. ret_val, hw->aq.asq_last_status, errno,
  1493. (u8)(cmd->config & I40E_NVM_MOD_PNT_MASK),
  1494. cmd->offset, cmd->data_size);
  1495. return errno;
  1496. }
  1497. static void i40e_get_drvinfo(struct net_device *netdev,
  1498. struct ethtool_drvinfo *drvinfo)
  1499. {
  1500. struct i40e_netdev_priv *np = netdev_priv(netdev);
  1501. struct i40e_vsi *vsi = np->vsi;
  1502. struct i40e_pf *pf = vsi->back;
  1503. strlcpy(drvinfo->driver, i40e_driver_name, sizeof(drvinfo->driver));
  1504. strlcpy(drvinfo->version, i40e_driver_version_str,
  1505. sizeof(drvinfo->version));
  1506. strlcpy(drvinfo->fw_version, i40e_nvm_version_str(&pf->hw),
  1507. sizeof(drvinfo->fw_version));
  1508. strlcpy(drvinfo->bus_info, pci_name(pf->pdev),
  1509. sizeof(drvinfo->bus_info));
  1510. drvinfo->n_priv_flags = I40E_PRIV_FLAGS_STR_LEN;
  1511. if (pf->hw.pf_id == 0)
  1512. drvinfo->n_priv_flags += I40E_GL_PRIV_FLAGS_STR_LEN;
  1513. }
  1514. static void i40e_get_ringparam(struct net_device *netdev,
  1515. struct ethtool_ringparam *ring)
  1516. {
  1517. struct i40e_netdev_priv *np = netdev_priv(netdev);
  1518. struct i40e_pf *pf = np->vsi->back;
  1519. struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
  1520. ring->rx_max_pending = I40E_MAX_NUM_DESCRIPTORS;
  1521. ring->tx_max_pending = I40E_MAX_NUM_DESCRIPTORS;
  1522. ring->rx_mini_max_pending = 0;
  1523. ring->rx_jumbo_max_pending = 0;
  1524. ring->rx_pending = vsi->rx_rings[0]->count;
  1525. ring->tx_pending = vsi->tx_rings[0]->count;
  1526. ring->rx_mini_pending = 0;
  1527. ring->rx_jumbo_pending = 0;
  1528. }
  1529. static bool i40e_active_tx_ring_index(struct i40e_vsi *vsi, u16 index)
  1530. {
  1531. if (i40e_enabled_xdp_vsi(vsi)) {
  1532. return index < vsi->num_queue_pairs ||
  1533. (index >= vsi->alloc_queue_pairs &&
  1534. index < vsi->alloc_queue_pairs + vsi->num_queue_pairs);
  1535. }
  1536. return index < vsi->num_queue_pairs;
  1537. }
  1538. static int i40e_set_ringparam(struct net_device *netdev,
  1539. struct ethtool_ringparam *ring)
  1540. {
  1541. struct i40e_ring *tx_rings = NULL, *rx_rings = NULL;
  1542. struct i40e_netdev_priv *np = netdev_priv(netdev);
  1543. struct i40e_hw *hw = &np->vsi->back->hw;
  1544. struct i40e_vsi *vsi = np->vsi;
  1545. struct i40e_pf *pf = vsi->back;
  1546. u32 new_rx_count, new_tx_count;
  1547. u16 tx_alloc_queue_pairs;
  1548. int timeout = 50;
  1549. int i, err = 0;
  1550. if ((ring->rx_mini_pending) || (ring->rx_jumbo_pending))
  1551. return -EINVAL;
  1552. if (ring->tx_pending > I40E_MAX_NUM_DESCRIPTORS ||
  1553. ring->tx_pending < I40E_MIN_NUM_DESCRIPTORS ||
  1554. ring->rx_pending > I40E_MAX_NUM_DESCRIPTORS ||
  1555. ring->rx_pending < I40E_MIN_NUM_DESCRIPTORS) {
  1556. netdev_info(netdev,
  1557. "Descriptors requested (Tx: %d / Rx: %d) out of range [%d-%d]\n",
  1558. ring->tx_pending, ring->rx_pending,
  1559. I40E_MIN_NUM_DESCRIPTORS, I40E_MAX_NUM_DESCRIPTORS);
  1560. return -EINVAL;
  1561. }
  1562. new_tx_count = ALIGN(ring->tx_pending, I40E_REQ_DESCRIPTOR_MULTIPLE);
  1563. new_rx_count = ALIGN(ring->rx_pending, I40E_REQ_DESCRIPTOR_MULTIPLE);
  1564. /* if nothing to do return success */
  1565. if ((new_tx_count == vsi->tx_rings[0]->count) &&
  1566. (new_rx_count == vsi->rx_rings[0]->count))
  1567. return 0;
  1568. /* If there is a AF_XDP UMEM attached to any of Rx rings,
  1569. * disallow changing the number of descriptors -- regardless
  1570. * if the netdev is running or not.
  1571. */
  1572. if (i40e_xsk_any_rx_ring_enabled(vsi))
  1573. return -EBUSY;
  1574. while (test_and_set_bit(__I40E_CONFIG_BUSY, pf->state)) {
  1575. timeout--;
  1576. if (!timeout)
  1577. return -EBUSY;
  1578. usleep_range(1000, 2000);
  1579. }
  1580. if (!netif_running(vsi->netdev)) {
  1581. /* simple case - set for the next time the netdev is started */
  1582. for (i = 0; i < vsi->num_queue_pairs; i++) {
  1583. vsi->tx_rings[i]->count = new_tx_count;
  1584. vsi->rx_rings[i]->count = new_rx_count;
  1585. if (i40e_enabled_xdp_vsi(vsi))
  1586. vsi->xdp_rings[i]->count = new_tx_count;
  1587. }
  1588. goto done;
  1589. }
  1590. /* We can't just free everything and then setup again,
  1591. * because the ISRs in MSI-X mode get passed pointers
  1592. * to the Tx and Rx ring structs.
  1593. */
  1594. /* alloc updated Tx and XDP Tx resources */
  1595. tx_alloc_queue_pairs = vsi->alloc_queue_pairs *
  1596. (i40e_enabled_xdp_vsi(vsi) ? 2 : 1);
  1597. if (new_tx_count != vsi->tx_rings[0]->count) {
  1598. netdev_info(netdev,
  1599. "Changing Tx descriptor count from %d to %d.\n",
  1600. vsi->tx_rings[0]->count, new_tx_count);
  1601. tx_rings = kcalloc(tx_alloc_queue_pairs,
  1602. sizeof(struct i40e_ring), GFP_KERNEL);
  1603. if (!tx_rings) {
  1604. err = -ENOMEM;
  1605. goto done;
  1606. }
  1607. for (i = 0; i < tx_alloc_queue_pairs; i++) {
  1608. if (!i40e_active_tx_ring_index(vsi, i))
  1609. continue;
  1610. tx_rings[i] = *vsi->tx_rings[i];
  1611. tx_rings[i].count = new_tx_count;
  1612. /* the desc and bi pointers will be reallocated in the
  1613. * setup call
  1614. */
  1615. tx_rings[i].desc = NULL;
  1616. tx_rings[i].rx_bi = NULL;
  1617. err = i40e_setup_tx_descriptors(&tx_rings[i]);
  1618. if (err) {
  1619. while (i) {
  1620. i--;
  1621. if (!i40e_active_tx_ring_index(vsi, i))
  1622. continue;
  1623. i40e_free_tx_resources(&tx_rings[i]);
  1624. }
  1625. kfree(tx_rings);
  1626. tx_rings = NULL;
  1627. goto done;
  1628. }
  1629. }
  1630. }
  1631. /* alloc updated Rx resources */
  1632. if (new_rx_count != vsi->rx_rings[0]->count) {
  1633. netdev_info(netdev,
  1634. "Changing Rx descriptor count from %d to %d\n",
  1635. vsi->rx_rings[0]->count, new_rx_count);
  1636. rx_rings = kcalloc(vsi->alloc_queue_pairs,
  1637. sizeof(struct i40e_ring), GFP_KERNEL);
  1638. if (!rx_rings) {
  1639. err = -ENOMEM;
  1640. goto free_tx;
  1641. }
  1642. for (i = 0; i < vsi->num_queue_pairs; i++) {
  1643. u16 unused;
  1644. /* clone ring and setup updated count */
  1645. rx_rings[i] = *vsi->rx_rings[i];
  1646. rx_rings[i].count = new_rx_count;
  1647. /* the desc and bi pointers will be reallocated in the
  1648. * setup call
  1649. */
  1650. rx_rings[i].desc = NULL;
  1651. rx_rings[i].rx_bi = NULL;
  1652. /* Clear cloned XDP RX-queue info before setup call */
  1653. memset(&rx_rings[i].xdp_rxq, 0, sizeof(rx_rings[i].xdp_rxq));
  1654. /* this is to allow wr32 to have something to write to
  1655. * during early allocation of Rx buffers
  1656. */
  1657. rx_rings[i].tail = hw->hw_addr + I40E_PRTGEN_STATUS;
  1658. err = i40e_setup_rx_descriptors(&rx_rings[i]);
  1659. if (err)
  1660. goto rx_unwind;
  1661. /* now allocate the Rx buffers to make sure the OS
  1662. * has enough memory, any failure here means abort
  1663. */
  1664. unused = I40E_DESC_UNUSED(&rx_rings[i]);
  1665. err = i40e_alloc_rx_buffers(&rx_rings[i], unused);
  1666. rx_unwind:
  1667. if (err) {
  1668. do {
  1669. i40e_free_rx_resources(&rx_rings[i]);
  1670. } while (i--);
  1671. kfree(rx_rings);
  1672. rx_rings = NULL;
  1673. goto free_tx;
  1674. }
  1675. }
  1676. }
  1677. /* Bring interface down, copy in the new ring info,
  1678. * then restore the interface
  1679. */
  1680. i40e_down(vsi);
  1681. if (tx_rings) {
  1682. for (i = 0; i < tx_alloc_queue_pairs; i++) {
  1683. if (i40e_active_tx_ring_index(vsi, i)) {
  1684. i40e_free_tx_resources(vsi->tx_rings[i]);
  1685. *vsi->tx_rings[i] = tx_rings[i];
  1686. }
  1687. }
  1688. kfree(tx_rings);
  1689. tx_rings = NULL;
  1690. }
  1691. if (rx_rings) {
  1692. for (i = 0; i < vsi->num_queue_pairs; i++) {
  1693. i40e_free_rx_resources(vsi->rx_rings[i]);
  1694. /* get the real tail offset */
  1695. rx_rings[i].tail = vsi->rx_rings[i]->tail;
  1696. /* this is to fake out the allocation routine
  1697. * into thinking it has to realloc everything
  1698. * but the recycling logic will let us re-use
  1699. * the buffers allocated above
  1700. */
  1701. rx_rings[i].next_to_use = 0;
  1702. rx_rings[i].next_to_clean = 0;
  1703. rx_rings[i].next_to_alloc = 0;
  1704. /* do a struct copy */
  1705. *vsi->rx_rings[i] = rx_rings[i];
  1706. }
  1707. kfree(rx_rings);
  1708. rx_rings = NULL;
  1709. }
  1710. i40e_up(vsi);
  1711. free_tx:
  1712. /* error cleanup if the Rx allocations failed after getting Tx */
  1713. if (tx_rings) {
  1714. for (i = 0; i < tx_alloc_queue_pairs; i++) {
  1715. if (i40e_active_tx_ring_index(vsi, i))
  1716. i40e_free_tx_resources(vsi->tx_rings[i]);
  1717. }
  1718. kfree(tx_rings);
  1719. tx_rings = NULL;
  1720. }
  1721. done:
  1722. clear_bit(__I40E_CONFIG_BUSY, pf->state);
  1723. return err;
  1724. }
  1725. /**
  1726. * i40e_get_stats_count - return the stats count for a device
  1727. * @netdev: the netdev to return the count for
  1728. *
  1729. * Returns the total number of statistics for this netdev. Note that even
  1730. * though this is a function, it is required that the count for a specific
  1731. * netdev must never change. Basing the count on static values such as the
  1732. * maximum number of queues or the device type is ok. However, the API for
  1733. * obtaining stats is *not* safe against changes based on non-static
  1734. * values such as the *current* number of queues, or runtime flags.
  1735. *
  1736. * If a statistic is not always enabled, return it as part of the count
  1737. * anyways, always return its string, and report its value as zero.
  1738. **/
  1739. static int i40e_get_stats_count(struct net_device *netdev)
  1740. {
  1741. struct i40e_netdev_priv *np = netdev_priv(netdev);
  1742. struct i40e_vsi *vsi = np->vsi;
  1743. struct i40e_pf *pf = vsi->back;
  1744. int stats_len;
  1745. if (vsi == pf->vsi[pf->lan_vsi] && pf->hw.partition_id == 1)
  1746. stats_len = I40E_PF_STATS_LEN;
  1747. else
  1748. stats_len = I40E_VSI_STATS_LEN;
  1749. /* The number of stats reported for a given net_device must remain
  1750. * constant throughout the life of that device.
  1751. *
  1752. * This is because the API for obtaining the size, strings, and stats
  1753. * is spread out over three separate ethtool ioctls. There is no safe
  1754. * way to lock the number of stats across these calls, so we must
  1755. * assume that they will never change.
  1756. *
  1757. * Due to this, we report the maximum number of queues, even if not
  1758. * every queue is currently configured. Since we always allocate
  1759. * queues in pairs, we'll just use netdev->num_tx_queues * 2. This
  1760. * works because the num_tx_queues is set at device creation and never
  1761. * changes.
  1762. */
  1763. stats_len += I40E_QUEUE_STATS_LEN * 2 * netdev->num_tx_queues;
  1764. return stats_len;
  1765. }
  1766. static int i40e_get_sset_count(struct net_device *netdev, int sset)
  1767. {
  1768. struct i40e_netdev_priv *np = netdev_priv(netdev);
  1769. struct i40e_vsi *vsi = np->vsi;
  1770. struct i40e_pf *pf = vsi->back;
  1771. switch (sset) {
  1772. case ETH_SS_TEST:
  1773. return I40E_TEST_LEN;
  1774. case ETH_SS_STATS:
  1775. return i40e_get_stats_count(netdev);
  1776. case ETH_SS_PRIV_FLAGS:
  1777. return I40E_PRIV_FLAGS_STR_LEN +
  1778. (pf->hw.pf_id == 0 ? I40E_GL_PRIV_FLAGS_STR_LEN : 0);
  1779. default:
  1780. return -EOPNOTSUPP;
  1781. }
  1782. }
  1783. /**
  1784. * i40e_get_pfc_stats - copy HW PFC statistics to formatted structure
  1785. * @pf: the PF device structure
  1786. * @i: the priority value to copy
  1787. *
  1788. * The PFC stats are found as arrays in pf->stats, which is not easy to pass
  1789. * into i40e_add_ethtool_stats. Produce a formatted i40e_pfc_stats structure
  1790. * of the PFC stats for the given priority.
  1791. **/
  1792. static inline struct i40e_pfc_stats
  1793. i40e_get_pfc_stats(struct i40e_pf *pf, unsigned int i)
  1794. {
  1795. #define I40E_GET_PFC_STAT(stat, priority) \
  1796. .stat = pf->stats.stat[priority]
  1797. struct i40e_pfc_stats pfc = {
  1798. I40E_GET_PFC_STAT(priority_xon_rx, i),
  1799. I40E_GET_PFC_STAT(priority_xoff_rx, i),
  1800. I40E_GET_PFC_STAT(priority_xon_tx, i),
  1801. I40E_GET_PFC_STAT(priority_xoff_tx, i),
  1802. I40E_GET_PFC_STAT(priority_xon_2_xoff, i),
  1803. };
  1804. return pfc;
  1805. }
  1806. /**
  1807. * i40e_get_ethtool_stats - copy stat values into supplied buffer
  1808. * @netdev: the netdev to collect stats for
  1809. * @stats: ethtool stats command structure
  1810. * @data: ethtool supplied buffer
  1811. *
  1812. * Copy the stats values for this netdev into the buffer. Expects data to be
  1813. * pre-allocated to the size returned by i40e_get_stats_count.. Note that all
  1814. * statistics must be copied in a static order, and the count must not change
  1815. * for a given netdev. See i40e_get_stats_count for more details.
  1816. *
  1817. * If a statistic is not currently valid (such as a disabled queue), this
  1818. * function reports its value as zero.
  1819. **/
  1820. static void i40e_get_ethtool_stats(struct net_device *netdev,
  1821. struct ethtool_stats *stats, u64 *data)
  1822. {
  1823. struct i40e_netdev_priv *np = netdev_priv(netdev);
  1824. struct i40e_vsi *vsi = np->vsi;
  1825. struct i40e_pf *pf = vsi->back;
  1826. struct i40e_veb *veb = pf->veb[pf->lan_veb];
  1827. unsigned int i;
  1828. bool veb_stats;
  1829. u64 *p = data;
  1830. i40e_update_stats(vsi);
  1831. i40e_add_ethtool_stats(&data, i40e_get_vsi_stats_struct(vsi),
  1832. i40e_gstrings_net_stats);
  1833. i40e_add_ethtool_stats(&data, vsi, i40e_gstrings_misc_stats);
  1834. rcu_read_lock();
  1835. for (i = 0; i < netdev->num_tx_queues; i++) {
  1836. i40e_add_queue_stats(&data, READ_ONCE(vsi->tx_rings[i]));
  1837. i40e_add_queue_stats(&data, READ_ONCE(vsi->rx_rings[i]));
  1838. }
  1839. rcu_read_unlock();
  1840. if (vsi != pf->vsi[pf->lan_vsi] || pf->hw.partition_id != 1)
  1841. goto check_data_pointer;
  1842. veb_stats = ((pf->lan_veb != I40E_NO_VEB) &&
  1843. (pf->flags & I40E_FLAG_VEB_STATS_ENABLED));
  1844. /* If veb stats aren't enabled, pass NULL instead of the veb so that
  1845. * we initialize stats to zero and update the data pointer
  1846. * intelligently
  1847. */
  1848. i40e_add_ethtool_stats(&data, veb_stats ? veb : NULL,
  1849. i40e_gstrings_veb_stats);
  1850. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
  1851. i40e_add_ethtool_stats(&data, veb_stats ? veb : NULL,
  1852. i40e_gstrings_veb_tc_stats);
  1853. i40e_add_ethtool_stats(&data, pf, i40e_gstrings_stats);
  1854. for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
  1855. struct i40e_pfc_stats pfc = i40e_get_pfc_stats(pf, i);
  1856. i40e_add_ethtool_stats(&data, &pfc, i40e_gstrings_pfc_stats);
  1857. }
  1858. check_data_pointer:
  1859. WARN_ONCE(data - p != i40e_get_stats_count(netdev),
  1860. "ethtool stats count mismatch!");
  1861. }
  1862. /**
  1863. * i40e_get_stat_strings - copy stat strings into supplied buffer
  1864. * @netdev: the netdev to collect strings for
  1865. * @data: supplied buffer to copy strings into
  1866. *
  1867. * Copy the strings related to stats for this netdev. Expects data to be
  1868. * pre-allocated with the size reported by i40e_get_stats_count. Note that the
  1869. * strings must be copied in a static order and the total count must not
  1870. * change for a given netdev. See i40e_get_stats_count for more details.
  1871. **/
  1872. static void i40e_get_stat_strings(struct net_device *netdev, u8 *data)
  1873. {
  1874. struct i40e_netdev_priv *np = netdev_priv(netdev);
  1875. struct i40e_vsi *vsi = np->vsi;
  1876. struct i40e_pf *pf = vsi->back;
  1877. unsigned int i;
  1878. u8 *p = data;
  1879. i40e_add_stat_strings(&data, i40e_gstrings_net_stats);
  1880. i40e_add_stat_strings(&data, i40e_gstrings_misc_stats);
  1881. for (i = 0; i < netdev->num_tx_queues; i++) {
  1882. i40e_add_stat_strings(&data, i40e_gstrings_queue_stats,
  1883. "tx", i);
  1884. i40e_add_stat_strings(&data, i40e_gstrings_queue_stats,
  1885. "rx", i);
  1886. }
  1887. if (vsi != pf->vsi[pf->lan_vsi] || pf->hw.partition_id != 1)
  1888. return;
  1889. i40e_add_stat_strings(&data, i40e_gstrings_veb_stats);
  1890. for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
  1891. i40e_add_stat_strings(&data, i40e_gstrings_veb_tc_stats, i);
  1892. i40e_add_stat_strings(&data, i40e_gstrings_stats);
  1893. for (i = 0; i < I40E_MAX_USER_PRIORITY; i++)
  1894. i40e_add_stat_strings(&data, i40e_gstrings_pfc_stats, i);
  1895. WARN_ONCE(data - p != i40e_get_stats_count(netdev) * ETH_GSTRING_LEN,
  1896. "stat strings count mismatch!");
  1897. }
  1898. static void i40e_get_priv_flag_strings(struct net_device *netdev, u8 *data)
  1899. {
  1900. struct i40e_netdev_priv *np = netdev_priv(netdev);
  1901. struct i40e_vsi *vsi = np->vsi;
  1902. struct i40e_pf *pf = vsi->back;
  1903. char *p = (char *)data;
  1904. unsigned int i;
  1905. for (i = 0; i < I40E_PRIV_FLAGS_STR_LEN; i++) {
  1906. snprintf(p, ETH_GSTRING_LEN, "%s",
  1907. i40e_gstrings_priv_flags[i].flag_string);
  1908. p += ETH_GSTRING_LEN;
  1909. }
  1910. if (pf->hw.pf_id != 0)
  1911. return;
  1912. for (i = 0; i < I40E_GL_PRIV_FLAGS_STR_LEN; i++) {
  1913. snprintf(p, ETH_GSTRING_LEN, "%s",
  1914. i40e_gl_gstrings_priv_flags[i].flag_string);
  1915. p += ETH_GSTRING_LEN;
  1916. }
  1917. }
  1918. static void i40e_get_strings(struct net_device *netdev, u32 stringset,
  1919. u8 *data)
  1920. {
  1921. switch (stringset) {
  1922. case ETH_SS_TEST:
  1923. memcpy(data, i40e_gstrings_test,
  1924. I40E_TEST_LEN * ETH_GSTRING_LEN);
  1925. break;
  1926. case ETH_SS_STATS:
  1927. i40e_get_stat_strings(netdev, data);
  1928. break;
  1929. case ETH_SS_PRIV_FLAGS:
  1930. i40e_get_priv_flag_strings(netdev, data);
  1931. break;
  1932. default:
  1933. break;
  1934. }
  1935. }
  1936. static int i40e_get_ts_info(struct net_device *dev,
  1937. struct ethtool_ts_info *info)
  1938. {
  1939. struct i40e_pf *pf = i40e_netdev_to_pf(dev);
  1940. /* only report HW timestamping if PTP is enabled */
  1941. if (!(pf->flags & I40E_FLAG_PTP))
  1942. return ethtool_op_get_ts_info(dev, info);
  1943. info->so_timestamping = SOF_TIMESTAMPING_TX_SOFTWARE |
  1944. SOF_TIMESTAMPING_RX_SOFTWARE |
  1945. SOF_TIMESTAMPING_SOFTWARE |
  1946. SOF_TIMESTAMPING_TX_HARDWARE |
  1947. SOF_TIMESTAMPING_RX_HARDWARE |
  1948. SOF_TIMESTAMPING_RAW_HARDWARE;
  1949. if (pf->ptp_clock)
  1950. info->phc_index = ptp_clock_index(pf->ptp_clock);
  1951. else
  1952. info->phc_index = -1;
  1953. info->tx_types = BIT(HWTSTAMP_TX_OFF) | BIT(HWTSTAMP_TX_ON);
  1954. info->rx_filters = BIT(HWTSTAMP_FILTER_NONE) |
  1955. BIT(HWTSTAMP_FILTER_PTP_V2_L2_EVENT) |
  1956. BIT(HWTSTAMP_FILTER_PTP_V2_L2_SYNC) |
  1957. BIT(HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ);
  1958. if (pf->hw_features & I40E_HW_PTP_L4_CAPABLE)
  1959. info->rx_filters |= BIT(HWTSTAMP_FILTER_PTP_V1_L4_SYNC) |
  1960. BIT(HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ) |
  1961. BIT(HWTSTAMP_FILTER_PTP_V2_EVENT) |
  1962. BIT(HWTSTAMP_FILTER_PTP_V2_L4_EVENT) |
  1963. BIT(HWTSTAMP_FILTER_PTP_V2_SYNC) |
  1964. BIT(HWTSTAMP_FILTER_PTP_V2_L4_SYNC) |
  1965. BIT(HWTSTAMP_FILTER_PTP_V2_DELAY_REQ) |
  1966. BIT(HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ);
  1967. return 0;
  1968. }
  1969. static int i40e_link_test(struct net_device *netdev, u64 *data)
  1970. {
  1971. struct i40e_netdev_priv *np = netdev_priv(netdev);
  1972. struct i40e_pf *pf = np->vsi->back;
  1973. i40e_status status;
  1974. bool link_up = false;
  1975. netif_info(pf, hw, netdev, "link test\n");
  1976. status = i40e_get_link_status(&pf->hw, &link_up);
  1977. if (status) {
  1978. netif_err(pf, drv, netdev, "link query timed out, please retry test\n");
  1979. *data = 1;
  1980. return *data;
  1981. }
  1982. if (link_up)
  1983. *data = 0;
  1984. else
  1985. *data = 1;
  1986. return *data;
  1987. }
  1988. static int i40e_reg_test(struct net_device *netdev, u64 *data)
  1989. {
  1990. struct i40e_netdev_priv *np = netdev_priv(netdev);
  1991. struct i40e_pf *pf = np->vsi->back;
  1992. netif_info(pf, hw, netdev, "register test\n");
  1993. *data = i40e_diag_reg_test(&pf->hw);
  1994. return *data;
  1995. }
  1996. static int i40e_eeprom_test(struct net_device *netdev, u64 *data)
  1997. {
  1998. struct i40e_netdev_priv *np = netdev_priv(netdev);
  1999. struct i40e_pf *pf = np->vsi->back;
  2000. netif_info(pf, hw, netdev, "eeprom test\n");
  2001. *data = i40e_diag_eeprom_test(&pf->hw);
  2002. /* forcebly clear the NVM Update state machine */
  2003. pf->hw.nvmupd_state = I40E_NVMUPD_STATE_INIT;
  2004. return *data;
  2005. }
  2006. static int i40e_intr_test(struct net_device *netdev, u64 *data)
  2007. {
  2008. struct i40e_netdev_priv *np = netdev_priv(netdev);
  2009. struct i40e_pf *pf = np->vsi->back;
  2010. u16 swc_old = pf->sw_int_count;
  2011. netif_info(pf, hw, netdev, "interrupt test\n");
  2012. wr32(&pf->hw, I40E_PFINT_DYN_CTL0,
  2013. (I40E_PFINT_DYN_CTL0_INTENA_MASK |
  2014. I40E_PFINT_DYN_CTL0_SWINT_TRIG_MASK |
  2015. I40E_PFINT_DYN_CTL0_ITR_INDX_MASK |
  2016. I40E_PFINT_DYN_CTL0_SW_ITR_INDX_ENA_MASK |
  2017. I40E_PFINT_DYN_CTL0_SW_ITR_INDX_MASK));
  2018. usleep_range(1000, 2000);
  2019. *data = (swc_old == pf->sw_int_count);
  2020. return *data;
  2021. }
  2022. static inline bool i40e_active_vfs(struct i40e_pf *pf)
  2023. {
  2024. struct i40e_vf *vfs = pf->vf;
  2025. int i;
  2026. for (i = 0; i < pf->num_alloc_vfs; i++)
  2027. if (test_bit(I40E_VF_STATE_ACTIVE, &vfs[i].vf_states))
  2028. return true;
  2029. return false;
  2030. }
  2031. static inline bool i40e_active_vmdqs(struct i40e_pf *pf)
  2032. {
  2033. return !!i40e_find_vsi_by_type(pf, I40E_VSI_VMDQ2);
  2034. }
  2035. static void i40e_diag_test(struct net_device *netdev,
  2036. struct ethtool_test *eth_test, u64 *data)
  2037. {
  2038. struct i40e_netdev_priv *np = netdev_priv(netdev);
  2039. bool if_running = netif_running(netdev);
  2040. struct i40e_pf *pf = np->vsi->back;
  2041. if (eth_test->flags == ETH_TEST_FL_OFFLINE) {
  2042. /* Offline tests */
  2043. netif_info(pf, drv, netdev, "offline testing starting\n");
  2044. set_bit(__I40E_TESTING, pf->state);
  2045. if (i40e_active_vfs(pf) || i40e_active_vmdqs(pf)) {
  2046. dev_warn(&pf->pdev->dev,
  2047. "Please take active VFs and Netqueues offline and restart the adapter before running NIC diagnostics\n");
  2048. data[I40E_ETH_TEST_REG] = 1;
  2049. data[I40E_ETH_TEST_EEPROM] = 1;
  2050. data[I40E_ETH_TEST_INTR] = 1;
  2051. data[I40E_ETH_TEST_LINK] = 1;
  2052. eth_test->flags |= ETH_TEST_FL_FAILED;
  2053. clear_bit(__I40E_TESTING, pf->state);
  2054. goto skip_ol_tests;
  2055. }
  2056. /* If the device is online then take it offline */
  2057. if (if_running)
  2058. /* indicate we're in test mode */
  2059. i40e_close(netdev);
  2060. else
  2061. /* This reset does not affect link - if it is
  2062. * changed to a type of reset that does affect
  2063. * link then the following link test would have
  2064. * to be moved to before the reset
  2065. */
  2066. i40e_do_reset(pf, BIT(__I40E_PF_RESET_REQUESTED), true);
  2067. if (i40e_link_test(netdev, &data[I40E_ETH_TEST_LINK]))
  2068. eth_test->flags |= ETH_TEST_FL_FAILED;
  2069. if (i40e_eeprom_test(netdev, &data[I40E_ETH_TEST_EEPROM]))
  2070. eth_test->flags |= ETH_TEST_FL_FAILED;
  2071. if (i40e_intr_test(netdev, &data[I40E_ETH_TEST_INTR]))
  2072. eth_test->flags |= ETH_TEST_FL_FAILED;
  2073. /* run reg test last, a reset is required after it */
  2074. if (i40e_reg_test(netdev, &data[I40E_ETH_TEST_REG]))
  2075. eth_test->flags |= ETH_TEST_FL_FAILED;
  2076. clear_bit(__I40E_TESTING, pf->state);
  2077. i40e_do_reset(pf, BIT(__I40E_PF_RESET_REQUESTED), true);
  2078. if (if_running)
  2079. i40e_open(netdev);
  2080. } else {
  2081. /* Online tests */
  2082. netif_info(pf, drv, netdev, "online testing starting\n");
  2083. if (i40e_link_test(netdev, &data[I40E_ETH_TEST_LINK]))
  2084. eth_test->flags |= ETH_TEST_FL_FAILED;
  2085. /* Offline only tests, not run in online; pass by default */
  2086. data[I40E_ETH_TEST_REG] = 0;
  2087. data[I40E_ETH_TEST_EEPROM] = 0;
  2088. data[I40E_ETH_TEST_INTR] = 0;
  2089. }
  2090. skip_ol_tests:
  2091. netif_info(pf, drv, netdev, "testing finished\n");
  2092. }
  2093. static void i40e_get_wol(struct net_device *netdev,
  2094. struct ethtool_wolinfo *wol)
  2095. {
  2096. struct i40e_netdev_priv *np = netdev_priv(netdev);
  2097. struct i40e_pf *pf = np->vsi->back;
  2098. struct i40e_hw *hw = &pf->hw;
  2099. u16 wol_nvm_bits;
  2100. /* NVM bit on means WoL disabled for the port */
  2101. i40e_read_nvm_word(hw, I40E_SR_NVM_WAKE_ON_LAN, &wol_nvm_bits);
  2102. if ((BIT(hw->port) & wol_nvm_bits) || (hw->partition_id != 1)) {
  2103. wol->supported = 0;
  2104. wol->wolopts = 0;
  2105. } else {
  2106. wol->supported = WAKE_MAGIC;
  2107. wol->wolopts = (pf->wol_en ? WAKE_MAGIC : 0);
  2108. }
  2109. }
  2110. /**
  2111. * i40e_set_wol - set the WakeOnLAN configuration
  2112. * @netdev: the netdev in question
  2113. * @wol: the ethtool WoL setting data
  2114. **/
  2115. static int i40e_set_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
  2116. {
  2117. struct i40e_netdev_priv *np = netdev_priv(netdev);
  2118. struct i40e_pf *pf = np->vsi->back;
  2119. struct i40e_vsi *vsi = np->vsi;
  2120. struct i40e_hw *hw = &pf->hw;
  2121. u16 wol_nvm_bits;
  2122. /* WoL not supported if this isn't the controlling PF on the port */
  2123. if (hw->partition_id != 1) {
  2124. i40e_partition_setting_complaint(pf);
  2125. return -EOPNOTSUPP;
  2126. }
  2127. if (vsi != pf->vsi[pf->lan_vsi])
  2128. return -EOPNOTSUPP;
  2129. /* NVM bit on means WoL disabled for the port */
  2130. i40e_read_nvm_word(hw, I40E_SR_NVM_WAKE_ON_LAN, &wol_nvm_bits);
  2131. if (BIT(hw->port) & wol_nvm_bits)
  2132. return -EOPNOTSUPP;
  2133. /* only magic packet is supported */
  2134. if (wol->wolopts && (wol->wolopts != WAKE_MAGIC))
  2135. return -EOPNOTSUPP;
  2136. /* is this a new value? */
  2137. if (pf->wol_en != !!wol->wolopts) {
  2138. pf->wol_en = !!wol->wolopts;
  2139. device_set_wakeup_enable(&pf->pdev->dev, pf->wol_en);
  2140. }
  2141. return 0;
  2142. }
  2143. static int i40e_set_phys_id(struct net_device *netdev,
  2144. enum ethtool_phys_id_state state)
  2145. {
  2146. struct i40e_netdev_priv *np = netdev_priv(netdev);
  2147. i40e_status ret = 0;
  2148. struct i40e_pf *pf = np->vsi->back;
  2149. struct i40e_hw *hw = &pf->hw;
  2150. int blink_freq = 2;
  2151. u16 temp_status;
  2152. switch (state) {
  2153. case ETHTOOL_ID_ACTIVE:
  2154. if (!(pf->hw_features & I40E_HW_PHY_CONTROLS_LEDS)) {
  2155. pf->led_status = i40e_led_get(hw);
  2156. } else {
  2157. if (!(hw->flags & I40E_HW_FLAG_AQ_PHY_ACCESS_CAPABLE))
  2158. i40e_aq_set_phy_debug(hw, I40E_PHY_DEBUG_ALL,
  2159. NULL);
  2160. ret = i40e_led_get_phy(hw, &temp_status,
  2161. &pf->phy_led_val);
  2162. pf->led_status = temp_status;
  2163. }
  2164. return blink_freq;
  2165. case ETHTOOL_ID_ON:
  2166. if (!(pf->hw_features & I40E_HW_PHY_CONTROLS_LEDS))
  2167. i40e_led_set(hw, 0xf, false);
  2168. else
  2169. ret = i40e_led_set_phy(hw, true, pf->led_status, 0);
  2170. break;
  2171. case ETHTOOL_ID_OFF:
  2172. if (!(pf->hw_features & I40E_HW_PHY_CONTROLS_LEDS))
  2173. i40e_led_set(hw, 0x0, false);
  2174. else
  2175. ret = i40e_led_set_phy(hw, false, pf->led_status, 0);
  2176. break;
  2177. case ETHTOOL_ID_INACTIVE:
  2178. if (!(pf->hw_features & I40E_HW_PHY_CONTROLS_LEDS)) {
  2179. i40e_led_set(hw, pf->led_status, false);
  2180. } else {
  2181. ret = i40e_led_set_phy(hw, false, pf->led_status,
  2182. (pf->phy_led_val |
  2183. I40E_PHY_LED_MODE_ORIG));
  2184. if (!(hw->flags & I40E_HW_FLAG_AQ_PHY_ACCESS_CAPABLE))
  2185. i40e_aq_set_phy_debug(hw, 0, NULL);
  2186. }
  2187. break;
  2188. default:
  2189. break;
  2190. }
  2191. if (ret)
  2192. return -ENOENT;
  2193. else
  2194. return 0;
  2195. }
  2196. /* NOTE: i40e hardware uses a conversion factor of 2 for Interrupt
  2197. * Throttle Rate (ITR) ie. ITR(1) = 2us ITR(10) = 20 us, and also
  2198. * 125us (8000 interrupts per second) == ITR(62)
  2199. */
  2200. /**
  2201. * __i40e_get_coalesce - get per-queue coalesce settings
  2202. * @netdev: the netdev to check
  2203. * @ec: ethtool coalesce data structure
  2204. * @queue: which queue to pick
  2205. *
  2206. * Gets the per-queue settings for coalescence. Specifically Rx and Tx usecs
  2207. * are per queue. If queue is <0 then we default to queue 0 as the
  2208. * representative value.
  2209. **/
  2210. static int __i40e_get_coalesce(struct net_device *netdev,
  2211. struct ethtool_coalesce *ec,
  2212. int queue)
  2213. {
  2214. struct i40e_netdev_priv *np = netdev_priv(netdev);
  2215. struct i40e_ring *rx_ring, *tx_ring;
  2216. struct i40e_vsi *vsi = np->vsi;
  2217. ec->tx_max_coalesced_frames_irq = vsi->work_limit;
  2218. ec->rx_max_coalesced_frames_irq = vsi->work_limit;
  2219. /* rx and tx usecs has per queue value. If user doesn't specify the
  2220. * queue, return queue 0's value to represent.
  2221. */
  2222. if (queue < 0)
  2223. queue = 0;
  2224. else if (queue >= vsi->num_queue_pairs)
  2225. return -EINVAL;
  2226. rx_ring = vsi->rx_rings[queue];
  2227. tx_ring = vsi->tx_rings[queue];
  2228. if (ITR_IS_DYNAMIC(rx_ring->itr_setting))
  2229. ec->use_adaptive_rx_coalesce = 1;
  2230. if (ITR_IS_DYNAMIC(tx_ring->itr_setting))
  2231. ec->use_adaptive_tx_coalesce = 1;
  2232. ec->rx_coalesce_usecs = rx_ring->itr_setting & ~I40E_ITR_DYNAMIC;
  2233. ec->tx_coalesce_usecs = tx_ring->itr_setting & ~I40E_ITR_DYNAMIC;
  2234. /* we use the _usecs_high to store/set the interrupt rate limit
  2235. * that the hardware supports, that almost but not quite
  2236. * fits the original intent of the ethtool variable,
  2237. * the rx_coalesce_usecs_high limits total interrupts
  2238. * per second from both tx/rx sources.
  2239. */
  2240. ec->rx_coalesce_usecs_high = vsi->int_rate_limit;
  2241. ec->tx_coalesce_usecs_high = vsi->int_rate_limit;
  2242. return 0;
  2243. }
  2244. /**
  2245. * i40e_get_coalesce - get a netdev's coalesce settings
  2246. * @netdev: the netdev to check
  2247. * @ec: ethtool coalesce data structure
  2248. *
  2249. * Gets the coalesce settings for a particular netdev. Note that if user has
  2250. * modified per-queue settings, this only guarantees to represent queue 0. See
  2251. * __i40e_get_coalesce for more details.
  2252. **/
  2253. static int i40e_get_coalesce(struct net_device *netdev,
  2254. struct ethtool_coalesce *ec)
  2255. {
  2256. return __i40e_get_coalesce(netdev, ec, -1);
  2257. }
  2258. /**
  2259. * i40e_get_per_queue_coalesce - gets coalesce settings for particular queue
  2260. * @netdev: netdev structure
  2261. * @ec: ethtool's coalesce settings
  2262. * @queue: the particular queue to read
  2263. *
  2264. * Will read a specific queue's coalesce settings
  2265. **/
  2266. static int i40e_get_per_queue_coalesce(struct net_device *netdev, u32 queue,
  2267. struct ethtool_coalesce *ec)
  2268. {
  2269. return __i40e_get_coalesce(netdev, ec, queue);
  2270. }
  2271. /**
  2272. * i40e_set_itr_per_queue - set ITR values for specific queue
  2273. * @vsi: the VSI to set values for
  2274. * @ec: coalesce settings from ethtool
  2275. * @queue: the queue to modify
  2276. *
  2277. * Change the ITR settings for a specific queue.
  2278. **/
  2279. static void i40e_set_itr_per_queue(struct i40e_vsi *vsi,
  2280. struct ethtool_coalesce *ec,
  2281. int queue)
  2282. {
  2283. struct i40e_ring *rx_ring = vsi->rx_rings[queue];
  2284. struct i40e_ring *tx_ring = vsi->tx_rings[queue];
  2285. struct i40e_pf *pf = vsi->back;
  2286. struct i40e_hw *hw = &pf->hw;
  2287. struct i40e_q_vector *q_vector;
  2288. u16 intrl;
  2289. intrl = i40e_intrl_usec_to_reg(vsi->int_rate_limit);
  2290. rx_ring->itr_setting = ITR_REG_ALIGN(ec->rx_coalesce_usecs);
  2291. tx_ring->itr_setting = ITR_REG_ALIGN(ec->tx_coalesce_usecs);
  2292. if (ec->use_adaptive_rx_coalesce)
  2293. rx_ring->itr_setting |= I40E_ITR_DYNAMIC;
  2294. else
  2295. rx_ring->itr_setting &= ~I40E_ITR_DYNAMIC;
  2296. if (ec->use_adaptive_tx_coalesce)
  2297. tx_ring->itr_setting |= I40E_ITR_DYNAMIC;
  2298. else
  2299. tx_ring->itr_setting &= ~I40E_ITR_DYNAMIC;
  2300. q_vector = rx_ring->q_vector;
  2301. q_vector->rx.target_itr = ITR_TO_REG(rx_ring->itr_setting);
  2302. q_vector = tx_ring->q_vector;
  2303. q_vector->tx.target_itr = ITR_TO_REG(tx_ring->itr_setting);
  2304. /* The interrupt handler itself will take care of programming
  2305. * the Tx and Rx ITR values based on the values we have entered
  2306. * into the q_vector, no need to write the values now.
  2307. */
  2308. wr32(hw, I40E_PFINT_RATEN(q_vector->reg_idx), intrl);
  2309. i40e_flush(hw);
  2310. }
  2311. /**
  2312. * __i40e_set_coalesce - set coalesce settings for particular queue
  2313. * @netdev: the netdev to change
  2314. * @ec: ethtool coalesce settings
  2315. * @queue: the queue to change
  2316. *
  2317. * Sets the coalesce settings for a particular queue.
  2318. **/
  2319. static int __i40e_set_coalesce(struct net_device *netdev,
  2320. struct ethtool_coalesce *ec,
  2321. int queue)
  2322. {
  2323. struct i40e_netdev_priv *np = netdev_priv(netdev);
  2324. u16 intrl_reg, cur_rx_itr, cur_tx_itr;
  2325. struct i40e_vsi *vsi = np->vsi;
  2326. struct i40e_pf *pf = vsi->back;
  2327. int i;
  2328. if (ec->tx_max_coalesced_frames_irq || ec->rx_max_coalesced_frames_irq)
  2329. vsi->work_limit = ec->tx_max_coalesced_frames_irq;
  2330. if (queue < 0) {
  2331. cur_rx_itr = vsi->rx_rings[0]->itr_setting;
  2332. cur_tx_itr = vsi->tx_rings[0]->itr_setting;
  2333. } else if (queue < vsi->num_queue_pairs) {
  2334. cur_rx_itr = vsi->rx_rings[queue]->itr_setting;
  2335. cur_tx_itr = vsi->tx_rings[queue]->itr_setting;
  2336. } else {
  2337. netif_info(pf, drv, netdev, "Invalid queue value, queue range is 0 - %d\n",
  2338. vsi->num_queue_pairs - 1);
  2339. return -EINVAL;
  2340. }
  2341. cur_tx_itr &= ~I40E_ITR_DYNAMIC;
  2342. cur_rx_itr &= ~I40E_ITR_DYNAMIC;
  2343. /* tx_coalesce_usecs_high is ignored, use rx-usecs-high instead */
  2344. if (ec->tx_coalesce_usecs_high != vsi->int_rate_limit) {
  2345. netif_info(pf, drv, netdev, "tx-usecs-high is not used, please program rx-usecs-high\n");
  2346. return -EINVAL;
  2347. }
  2348. if (ec->rx_coalesce_usecs_high > INTRL_REG_TO_USEC(I40E_MAX_INTRL)) {
  2349. netif_info(pf, drv, netdev, "Invalid value, rx-usecs-high range is 0-%lu\n",
  2350. INTRL_REG_TO_USEC(I40E_MAX_INTRL));
  2351. return -EINVAL;
  2352. }
  2353. if (ec->rx_coalesce_usecs != cur_rx_itr &&
  2354. ec->use_adaptive_rx_coalesce) {
  2355. netif_info(pf, drv, netdev, "RX interrupt moderation cannot be changed if adaptive-rx is enabled.\n");
  2356. return -EINVAL;
  2357. }
  2358. if (ec->rx_coalesce_usecs > I40E_MAX_ITR) {
  2359. netif_info(pf, drv, netdev, "Invalid value, rx-usecs range is 0-8160\n");
  2360. return -EINVAL;
  2361. }
  2362. if (ec->tx_coalesce_usecs != cur_tx_itr &&
  2363. ec->use_adaptive_tx_coalesce) {
  2364. netif_info(pf, drv, netdev, "TX interrupt moderation cannot be changed if adaptive-tx is enabled.\n");
  2365. return -EINVAL;
  2366. }
  2367. if (ec->tx_coalesce_usecs > I40E_MAX_ITR) {
  2368. netif_info(pf, drv, netdev, "Invalid value, tx-usecs range is 0-8160\n");
  2369. return -EINVAL;
  2370. }
  2371. if (ec->use_adaptive_rx_coalesce && !cur_rx_itr)
  2372. ec->rx_coalesce_usecs = I40E_MIN_ITR;
  2373. if (ec->use_adaptive_tx_coalesce && !cur_tx_itr)
  2374. ec->tx_coalesce_usecs = I40E_MIN_ITR;
  2375. intrl_reg = i40e_intrl_usec_to_reg(ec->rx_coalesce_usecs_high);
  2376. vsi->int_rate_limit = INTRL_REG_TO_USEC(intrl_reg);
  2377. if (vsi->int_rate_limit != ec->rx_coalesce_usecs_high) {
  2378. netif_info(pf, drv, netdev, "Interrupt rate limit rounded down to %d\n",
  2379. vsi->int_rate_limit);
  2380. }
  2381. /* rx and tx usecs has per queue value. If user doesn't specify the
  2382. * queue, apply to all queues.
  2383. */
  2384. if (queue < 0) {
  2385. for (i = 0; i < vsi->num_queue_pairs; i++)
  2386. i40e_set_itr_per_queue(vsi, ec, i);
  2387. } else {
  2388. i40e_set_itr_per_queue(vsi, ec, queue);
  2389. }
  2390. return 0;
  2391. }
  2392. /**
  2393. * i40e_set_coalesce - set coalesce settings for every queue on the netdev
  2394. * @netdev: the netdev to change
  2395. * @ec: ethtool coalesce settings
  2396. *
  2397. * This will set each queue to the same coalesce settings.
  2398. **/
  2399. static int i40e_set_coalesce(struct net_device *netdev,
  2400. struct ethtool_coalesce *ec)
  2401. {
  2402. return __i40e_set_coalesce(netdev, ec, -1);
  2403. }
  2404. /**
  2405. * i40e_set_per_queue_coalesce - set specific queue's coalesce settings
  2406. * @netdev: the netdev to change
  2407. * @ec: ethtool's coalesce settings
  2408. * @queue: the queue to change
  2409. *
  2410. * Sets the specified queue's coalesce settings.
  2411. **/
  2412. static int i40e_set_per_queue_coalesce(struct net_device *netdev, u32 queue,
  2413. struct ethtool_coalesce *ec)
  2414. {
  2415. return __i40e_set_coalesce(netdev, ec, queue);
  2416. }
  2417. /**
  2418. * i40e_get_rss_hash_opts - Get RSS hash Input Set for each flow type
  2419. * @pf: pointer to the physical function struct
  2420. * @cmd: ethtool rxnfc command
  2421. *
  2422. * Returns Success if the flow is supported, else Invalid Input.
  2423. **/
  2424. static int i40e_get_rss_hash_opts(struct i40e_pf *pf, struct ethtool_rxnfc *cmd)
  2425. {
  2426. struct i40e_hw *hw = &pf->hw;
  2427. u8 flow_pctype = 0;
  2428. u64 i_set = 0;
  2429. cmd->data = 0;
  2430. switch (cmd->flow_type) {
  2431. case TCP_V4_FLOW:
  2432. flow_pctype = I40E_FILTER_PCTYPE_NONF_IPV4_TCP;
  2433. break;
  2434. case UDP_V4_FLOW:
  2435. flow_pctype = I40E_FILTER_PCTYPE_NONF_IPV4_UDP;
  2436. break;
  2437. case TCP_V6_FLOW:
  2438. flow_pctype = I40E_FILTER_PCTYPE_NONF_IPV6_TCP;
  2439. break;
  2440. case UDP_V6_FLOW:
  2441. flow_pctype = I40E_FILTER_PCTYPE_NONF_IPV6_UDP;
  2442. break;
  2443. case SCTP_V4_FLOW:
  2444. case AH_ESP_V4_FLOW:
  2445. case AH_V4_FLOW:
  2446. case ESP_V4_FLOW:
  2447. case IPV4_FLOW:
  2448. case SCTP_V6_FLOW:
  2449. case AH_ESP_V6_FLOW:
  2450. case AH_V6_FLOW:
  2451. case ESP_V6_FLOW:
  2452. case IPV6_FLOW:
  2453. /* Default is src/dest for IP, no matter the L4 hashing */
  2454. cmd->data |= RXH_IP_SRC | RXH_IP_DST;
  2455. break;
  2456. default:
  2457. return -EINVAL;
  2458. }
  2459. /* Read flow based hash input set register */
  2460. if (flow_pctype) {
  2461. i_set = (u64)i40e_read_rx_ctl(hw, I40E_GLQF_HASH_INSET(0,
  2462. flow_pctype)) |
  2463. ((u64)i40e_read_rx_ctl(hw, I40E_GLQF_HASH_INSET(1,
  2464. flow_pctype)) << 32);
  2465. }
  2466. /* Process bits of hash input set */
  2467. if (i_set) {
  2468. if (i_set & I40E_L4_SRC_MASK)
  2469. cmd->data |= RXH_L4_B_0_1;
  2470. if (i_set & I40E_L4_DST_MASK)
  2471. cmd->data |= RXH_L4_B_2_3;
  2472. if (cmd->flow_type == TCP_V4_FLOW ||
  2473. cmd->flow_type == UDP_V4_FLOW) {
  2474. if (i_set & I40E_L3_SRC_MASK)
  2475. cmd->data |= RXH_IP_SRC;
  2476. if (i_set & I40E_L3_DST_MASK)
  2477. cmd->data |= RXH_IP_DST;
  2478. } else if (cmd->flow_type == TCP_V6_FLOW ||
  2479. cmd->flow_type == UDP_V6_FLOW) {
  2480. if (i_set & I40E_L3_V6_SRC_MASK)
  2481. cmd->data |= RXH_IP_SRC;
  2482. if (i_set & I40E_L3_V6_DST_MASK)
  2483. cmd->data |= RXH_IP_DST;
  2484. }
  2485. }
  2486. return 0;
  2487. }
  2488. /**
  2489. * i40e_check_mask - Check whether a mask field is set
  2490. * @mask: the full mask value
  2491. * @field: mask of the field to check
  2492. *
  2493. * If the given mask is fully set, return positive value. If the mask for the
  2494. * field is fully unset, return zero. Otherwise return a negative error code.
  2495. **/
  2496. static int i40e_check_mask(u64 mask, u64 field)
  2497. {
  2498. u64 value = mask & field;
  2499. if (value == field)
  2500. return 1;
  2501. else if (!value)
  2502. return 0;
  2503. else
  2504. return -1;
  2505. }
  2506. /**
  2507. * i40e_parse_rx_flow_user_data - Deconstruct user-defined data
  2508. * @fsp: pointer to rx flow specification
  2509. * @data: pointer to userdef data structure for storage
  2510. *
  2511. * Read the user-defined data and deconstruct the value into a structure. No
  2512. * other code should read the user-defined data, so as to ensure that every
  2513. * place consistently reads the value correctly.
  2514. *
  2515. * The user-defined field is a 64bit Big Endian format value, which we
  2516. * deconstruct by reading bits or bit fields from it. Single bit flags shall
  2517. * be defined starting from the highest bits, while small bit field values
  2518. * shall be defined starting from the lowest bits.
  2519. *
  2520. * Returns 0 if the data is valid, and non-zero if the userdef data is invalid
  2521. * and the filter should be rejected. The data structure will always be
  2522. * modified even if FLOW_EXT is not set.
  2523. *
  2524. **/
  2525. static int i40e_parse_rx_flow_user_data(struct ethtool_rx_flow_spec *fsp,
  2526. struct i40e_rx_flow_userdef *data)
  2527. {
  2528. u64 value, mask;
  2529. int valid;
  2530. /* Zero memory first so it's always consistent. */
  2531. memset(data, 0, sizeof(*data));
  2532. if (!(fsp->flow_type & FLOW_EXT))
  2533. return 0;
  2534. value = be64_to_cpu(*((__be64 *)fsp->h_ext.data));
  2535. mask = be64_to_cpu(*((__be64 *)fsp->m_ext.data));
  2536. #define I40E_USERDEF_FLEX_WORD GENMASK_ULL(15, 0)
  2537. #define I40E_USERDEF_FLEX_OFFSET GENMASK_ULL(31, 16)
  2538. #define I40E_USERDEF_FLEX_FILTER GENMASK_ULL(31, 0)
  2539. valid = i40e_check_mask(mask, I40E_USERDEF_FLEX_FILTER);
  2540. if (valid < 0) {
  2541. return -EINVAL;
  2542. } else if (valid) {
  2543. data->flex_word = value & I40E_USERDEF_FLEX_WORD;
  2544. data->flex_offset =
  2545. (value & I40E_USERDEF_FLEX_OFFSET) >> 16;
  2546. data->flex_filter = true;
  2547. }
  2548. return 0;
  2549. }
  2550. /**
  2551. * i40e_fill_rx_flow_user_data - Fill in user-defined data field
  2552. * @fsp: pointer to rx_flow specification
  2553. * @data: pointer to return userdef data
  2554. *
  2555. * Reads the userdef data structure and properly fills in the user defined
  2556. * fields of the rx_flow_spec.
  2557. **/
  2558. static void i40e_fill_rx_flow_user_data(struct ethtool_rx_flow_spec *fsp,
  2559. struct i40e_rx_flow_userdef *data)
  2560. {
  2561. u64 value = 0, mask = 0;
  2562. if (data->flex_filter) {
  2563. value |= data->flex_word;
  2564. value |= (u64)data->flex_offset << 16;
  2565. mask |= I40E_USERDEF_FLEX_FILTER;
  2566. }
  2567. if (value || mask)
  2568. fsp->flow_type |= FLOW_EXT;
  2569. *((__be64 *)fsp->h_ext.data) = cpu_to_be64(value);
  2570. *((__be64 *)fsp->m_ext.data) = cpu_to_be64(mask);
  2571. }
  2572. /**
  2573. * i40e_get_ethtool_fdir_all - Populates the rule count of a command
  2574. * @pf: Pointer to the physical function struct
  2575. * @cmd: The command to get or set Rx flow classification rules
  2576. * @rule_locs: Array of used rule locations
  2577. *
  2578. * This function populates both the total and actual rule count of
  2579. * the ethtool flow classification command
  2580. *
  2581. * Returns 0 on success or -EMSGSIZE if entry not found
  2582. **/
  2583. static int i40e_get_ethtool_fdir_all(struct i40e_pf *pf,
  2584. struct ethtool_rxnfc *cmd,
  2585. u32 *rule_locs)
  2586. {
  2587. struct i40e_fdir_filter *rule;
  2588. struct hlist_node *node2;
  2589. int cnt = 0;
  2590. /* report total rule count */
  2591. cmd->data = i40e_get_fd_cnt_all(pf);
  2592. hlist_for_each_entry_safe(rule, node2,
  2593. &pf->fdir_filter_list, fdir_node) {
  2594. if (cnt == cmd->rule_cnt)
  2595. return -EMSGSIZE;
  2596. rule_locs[cnt] = rule->fd_id;
  2597. cnt++;
  2598. }
  2599. cmd->rule_cnt = cnt;
  2600. return 0;
  2601. }
  2602. /**
  2603. * i40e_get_ethtool_fdir_entry - Look up a filter based on Rx flow
  2604. * @pf: Pointer to the physical function struct
  2605. * @cmd: The command to get or set Rx flow classification rules
  2606. *
  2607. * This function looks up a filter based on the Rx flow classification
  2608. * command and fills the flow spec info for it if found
  2609. *
  2610. * Returns 0 on success or -EINVAL if filter not found
  2611. **/
  2612. static int i40e_get_ethtool_fdir_entry(struct i40e_pf *pf,
  2613. struct ethtool_rxnfc *cmd)
  2614. {
  2615. struct ethtool_rx_flow_spec *fsp =
  2616. (struct ethtool_rx_flow_spec *)&cmd->fs;
  2617. struct i40e_rx_flow_userdef userdef = {0};
  2618. struct i40e_fdir_filter *rule = NULL;
  2619. struct hlist_node *node2;
  2620. u64 input_set;
  2621. u16 index;
  2622. hlist_for_each_entry_safe(rule, node2,
  2623. &pf->fdir_filter_list, fdir_node) {
  2624. if (fsp->location <= rule->fd_id)
  2625. break;
  2626. }
  2627. if (!rule || fsp->location != rule->fd_id)
  2628. return -EINVAL;
  2629. fsp->flow_type = rule->flow_type;
  2630. if (fsp->flow_type == IP_USER_FLOW) {
  2631. fsp->h_u.usr_ip4_spec.ip_ver = ETH_RX_NFC_IP4;
  2632. fsp->h_u.usr_ip4_spec.proto = 0;
  2633. fsp->m_u.usr_ip4_spec.proto = 0;
  2634. }
  2635. /* Reverse the src and dest notion, since the HW views them from
  2636. * Tx perspective where as the user expects it from Rx filter view.
  2637. */
  2638. fsp->h_u.tcp_ip4_spec.psrc = rule->dst_port;
  2639. fsp->h_u.tcp_ip4_spec.pdst = rule->src_port;
  2640. fsp->h_u.tcp_ip4_spec.ip4src = rule->dst_ip;
  2641. fsp->h_u.tcp_ip4_spec.ip4dst = rule->src_ip;
  2642. switch (rule->flow_type) {
  2643. case SCTP_V4_FLOW:
  2644. index = I40E_FILTER_PCTYPE_NONF_IPV4_SCTP;
  2645. break;
  2646. case TCP_V4_FLOW:
  2647. index = I40E_FILTER_PCTYPE_NONF_IPV4_TCP;
  2648. break;
  2649. case UDP_V4_FLOW:
  2650. index = I40E_FILTER_PCTYPE_NONF_IPV4_UDP;
  2651. break;
  2652. case IP_USER_FLOW:
  2653. index = I40E_FILTER_PCTYPE_NONF_IPV4_OTHER;
  2654. break;
  2655. default:
  2656. /* If we have stored a filter with a flow type not listed here
  2657. * it is almost certainly a driver bug. WARN(), and then
  2658. * assign the input_set as if all fields are enabled to avoid
  2659. * reading unassigned memory.
  2660. */
  2661. WARN(1, "Missing input set index for flow_type %d\n",
  2662. rule->flow_type);
  2663. input_set = 0xFFFFFFFFFFFFFFFFULL;
  2664. goto no_input_set;
  2665. }
  2666. input_set = i40e_read_fd_input_set(pf, index);
  2667. no_input_set:
  2668. if (input_set & I40E_L3_SRC_MASK)
  2669. fsp->m_u.tcp_ip4_spec.ip4src = htonl(0xFFFFFFFF);
  2670. if (input_set & I40E_L3_DST_MASK)
  2671. fsp->m_u.tcp_ip4_spec.ip4dst = htonl(0xFFFFFFFF);
  2672. if (input_set & I40E_L4_SRC_MASK)
  2673. fsp->m_u.tcp_ip4_spec.psrc = htons(0xFFFF);
  2674. if (input_set & I40E_L4_DST_MASK)
  2675. fsp->m_u.tcp_ip4_spec.pdst = htons(0xFFFF);
  2676. if (rule->dest_ctl == I40E_FILTER_PROGRAM_DESC_DEST_DROP_PACKET)
  2677. fsp->ring_cookie = RX_CLS_FLOW_DISC;
  2678. else
  2679. fsp->ring_cookie = rule->q_index;
  2680. if (rule->dest_vsi != pf->vsi[pf->lan_vsi]->id) {
  2681. struct i40e_vsi *vsi;
  2682. vsi = i40e_find_vsi_from_id(pf, rule->dest_vsi);
  2683. if (vsi && vsi->type == I40E_VSI_SRIOV) {
  2684. /* VFs are zero-indexed by the driver, but ethtool
  2685. * expects them to be one-indexed, so add one here
  2686. */
  2687. u64 ring_vf = vsi->vf_id + 1;
  2688. ring_vf <<= ETHTOOL_RX_FLOW_SPEC_RING_VF_OFF;
  2689. fsp->ring_cookie |= ring_vf;
  2690. }
  2691. }
  2692. if (rule->flex_filter) {
  2693. userdef.flex_filter = true;
  2694. userdef.flex_word = be16_to_cpu(rule->flex_word);
  2695. userdef.flex_offset = rule->flex_offset;
  2696. }
  2697. i40e_fill_rx_flow_user_data(fsp, &userdef);
  2698. return 0;
  2699. }
  2700. /**
  2701. * i40e_get_rxnfc - command to get RX flow classification rules
  2702. * @netdev: network interface device structure
  2703. * @cmd: ethtool rxnfc command
  2704. * @rule_locs: pointer to store rule data
  2705. *
  2706. * Returns Success if the command is supported.
  2707. **/
  2708. static int i40e_get_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd,
  2709. u32 *rule_locs)
  2710. {
  2711. struct i40e_netdev_priv *np = netdev_priv(netdev);
  2712. struct i40e_vsi *vsi = np->vsi;
  2713. struct i40e_pf *pf = vsi->back;
  2714. int ret = -EOPNOTSUPP;
  2715. switch (cmd->cmd) {
  2716. case ETHTOOL_GRXRINGS:
  2717. cmd->data = vsi->rss_size;
  2718. ret = 0;
  2719. break;
  2720. case ETHTOOL_GRXFH:
  2721. ret = i40e_get_rss_hash_opts(pf, cmd);
  2722. break;
  2723. case ETHTOOL_GRXCLSRLCNT:
  2724. cmd->rule_cnt = pf->fdir_pf_active_filters;
  2725. /* report total rule count */
  2726. cmd->data = i40e_get_fd_cnt_all(pf);
  2727. ret = 0;
  2728. break;
  2729. case ETHTOOL_GRXCLSRULE:
  2730. ret = i40e_get_ethtool_fdir_entry(pf, cmd);
  2731. break;
  2732. case ETHTOOL_GRXCLSRLALL:
  2733. ret = i40e_get_ethtool_fdir_all(pf, cmd, rule_locs);
  2734. break;
  2735. default:
  2736. break;
  2737. }
  2738. return ret;
  2739. }
  2740. /**
  2741. * i40e_get_rss_hash_bits - Read RSS Hash bits from register
  2742. * @nfc: pointer to user request
  2743. * @i_setc: bits currently set
  2744. *
  2745. * Returns value of bits to be set per user request
  2746. **/
  2747. static u64 i40e_get_rss_hash_bits(struct ethtool_rxnfc *nfc, u64 i_setc)
  2748. {
  2749. u64 i_set = i_setc;
  2750. u64 src_l3 = 0, dst_l3 = 0;
  2751. if (nfc->data & RXH_L4_B_0_1)
  2752. i_set |= I40E_L4_SRC_MASK;
  2753. else
  2754. i_set &= ~I40E_L4_SRC_MASK;
  2755. if (nfc->data & RXH_L4_B_2_3)
  2756. i_set |= I40E_L4_DST_MASK;
  2757. else
  2758. i_set &= ~I40E_L4_DST_MASK;
  2759. if (nfc->flow_type == TCP_V6_FLOW || nfc->flow_type == UDP_V6_FLOW) {
  2760. src_l3 = I40E_L3_V6_SRC_MASK;
  2761. dst_l3 = I40E_L3_V6_DST_MASK;
  2762. } else if (nfc->flow_type == TCP_V4_FLOW ||
  2763. nfc->flow_type == UDP_V4_FLOW) {
  2764. src_l3 = I40E_L3_SRC_MASK;
  2765. dst_l3 = I40E_L3_DST_MASK;
  2766. } else {
  2767. /* Any other flow type are not supported here */
  2768. return i_set;
  2769. }
  2770. if (nfc->data & RXH_IP_SRC)
  2771. i_set |= src_l3;
  2772. else
  2773. i_set &= ~src_l3;
  2774. if (nfc->data & RXH_IP_DST)
  2775. i_set |= dst_l3;
  2776. else
  2777. i_set &= ~dst_l3;
  2778. return i_set;
  2779. }
  2780. /**
  2781. * i40e_set_rss_hash_opt - Enable/Disable flow types for RSS hash
  2782. * @pf: pointer to the physical function struct
  2783. * @nfc: ethtool rxnfc command
  2784. *
  2785. * Returns Success if the flow input set is supported.
  2786. **/
  2787. static int i40e_set_rss_hash_opt(struct i40e_pf *pf, struct ethtool_rxnfc *nfc)
  2788. {
  2789. struct i40e_hw *hw = &pf->hw;
  2790. u64 hena = (u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(0)) |
  2791. ((u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(1)) << 32);
  2792. u8 flow_pctype = 0;
  2793. u64 i_set, i_setc;
  2794. if (pf->flags & I40E_FLAG_MFP_ENABLED) {
  2795. dev_err(&pf->pdev->dev,
  2796. "Change of RSS hash input set is not supported when MFP mode is enabled\n");
  2797. return -EOPNOTSUPP;
  2798. }
  2799. /* RSS does not support anything other than hashing
  2800. * to queues on src and dst IPs and ports
  2801. */
  2802. if (nfc->data & ~(RXH_IP_SRC | RXH_IP_DST |
  2803. RXH_L4_B_0_1 | RXH_L4_B_2_3))
  2804. return -EINVAL;
  2805. switch (nfc->flow_type) {
  2806. case TCP_V4_FLOW:
  2807. flow_pctype = I40E_FILTER_PCTYPE_NONF_IPV4_TCP;
  2808. if (pf->hw_features & I40E_HW_MULTIPLE_TCP_UDP_RSS_PCTYPE)
  2809. hena |=
  2810. BIT_ULL(I40E_FILTER_PCTYPE_NONF_IPV4_TCP_SYN_NO_ACK);
  2811. break;
  2812. case TCP_V6_FLOW:
  2813. flow_pctype = I40E_FILTER_PCTYPE_NONF_IPV6_TCP;
  2814. if (pf->hw_features & I40E_HW_MULTIPLE_TCP_UDP_RSS_PCTYPE)
  2815. hena |=
  2816. BIT_ULL(I40E_FILTER_PCTYPE_NONF_IPV4_TCP_SYN_NO_ACK);
  2817. if (pf->hw_features & I40E_HW_MULTIPLE_TCP_UDP_RSS_PCTYPE)
  2818. hena |=
  2819. BIT_ULL(I40E_FILTER_PCTYPE_NONF_IPV6_TCP_SYN_NO_ACK);
  2820. break;
  2821. case UDP_V4_FLOW:
  2822. flow_pctype = I40E_FILTER_PCTYPE_NONF_IPV4_UDP;
  2823. if (pf->hw_features & I40E_HW_MULTIPLE_TCP_UDP_RSS_PCTYPE)
  2824. hena |=
  2825. BIT_ULL(I40E_FILTER_PCTYPE_NONF_UNICAST_IPV4_UDP) |
  2826. BIT_ULL(I40E_FILTER_PCTYPE_NONF_MULTICAST_IPV4_UDP);
  2827. hena |= BIT_ULL(I40E_FILTER_PCTYPE_FRAG_IPV4);
  2828. break;
  2829. case UDP_V6_FLOW:
  2830. flow_pctype = I40E_FILTER_PCTYPE_NONF_IPV6_UDP;
  2831. if (pf->hw_features & I40E_HW_MULTIPLE_TCP_UDP_RSS_PCTYPE)
  2832. hena |=
  2833. BIT_ULL(I40E_FILTER_PCTYPE_NONF_UNICAST_IPV6_UDP) |
  2834. BIT_ULL(I40E_FILTER_PCTYPE_NONF_MULTICAST_IPV6_UDP);
  2835. hena |= BIT_ULL(I40E_FILTER_PCTYPE_FRAG_IPV6);
  2836. break;
  2837. case AH_ESP_V4_FLOW:
  2838. case AH_V4_FLOW:
  2839. case ESP_V4_FLOW:
  2840. case SCTP_V4_FLOW:
  2841. if ((nfc->data & RXH_L4_B_0_1) ||
  2842. (nfc->data & RXH_L4_B_2_3))
  2843. return -EINVAL;
  2844. hena |= BIT_ULL(I40E_FILTER_PCTYPE_NONF_IPV4_OTHER);
  2845. break;
  2846. case AH_ESP_V6_FLOW:
  2847. case AH_V6_FLOW:
  2848. case ESP_V6_FLOW:
  2849. case SCTP_V6_FLOW:
  2850. if ((nfc->data & RXH_L4_B_0_1) ||
  2851. (nfc->data & RXH_L4_B_2_3))
  2852. return -EINVAL;
  2853. hena |= BIT_ULL(I40E_FILTER_PCTYPE_NONF_IPV6_OTHER);
  2854. break;
  2855. case IPV4_FLOW:
  2856. hena |= BIT_ULL(I40E_FILTER_PCTYPE_NONF_IPV4_OTHER) |
  2857. BIT_ULL(I40E_FILTER_PCTYPE_FRAG_IPV4);
  2858. break;
  2859. case IPV6_FLOW:
  2860. hena |= BIT_ULL(I40E_FILTER_PCTYPE_NONF_IPV6_OTHER) |
  2861. BIT_ULL(I40E_FILTER_PCTYPE_FRAG_IPV6);
  2862. break;
  2863. default:
  2864. return -EINVAL;
  2865. }
  2866. if (flow_pctype) {
  2867. i_setc = (u64)i40e_read_rx_ctl(hw, I40E_GLQF_HASH_INSET(0,
  2868. flow_pctype)) |
  2869. ((u64)i40e_read_rx_ctl(hw, I40E_GLQF_HASH_INSET(1,
  2870. flow_pctype)) << 32);
  2871. i_set = i40e_get_rss_hash_bits(nfc, i_setc);
  2872. i40e_write_rx_ctl(hw, I40E_GLQF_HASH_INSET(0, flow_pctype),
  2873. (u32)i_set);
  2874. i40e_write_rx_ctl(hw, I40E_GLQF_HASH_INSET(1, flow_pctype),
  2875. (u32)(i_set >> 32));
  2876. hena |= BIT_ULL(flow_pctype);
  2877. }
  2878. i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), (u32)hena);
  2879. i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), (u32)(hena >> 32));
  2880. i40e_flush(hw);
  2881. return 0;
  2882. }
  2883. /**
  2884. * i40e_update_ethtool_fdir_entry - Updates the fdir filter entry
  2885. * @vsi: Pointer to the targeted VSI
  2886. * @input: The filter to update or NULL to indicate deletion
  2887. * @sw_idx: Software index to the filter
  2888. * @cmd: The command to get or set Rx flow classification rules
  2889. *
  2890. * This function updates (or deletes) a Flow Director entry from
  2891. * the hlist of the corresponding PF
  2892. *
  2893. * Returns 0 on success
  2894. **/
  2895. static int i40e_update_ethtool_fdir_entry(struct i40e_vsi *vsi,
  2896. struct i40e_fdir_filter *input,
  2897. u16 sw_idx,
  2898. struct ethtool_rxnfc *cmd)
  2899. {
  2900. struct i40e_fdir_filter *rule, *parent;
  2901. struct i40e_pf *pf = vsi->back;
  2902. struct hlist_node *node2;
  2903. int err = -EINVAL;
  2904. parent = NULL;
  2905. rule = NULL;
  2906. hlist_for_each_entry_safe(rule, node2,
  2907. &pf->fdir_filter_list, fdir_node) {
  2908. /* hash found, or no matching entry */
  2909. if (rule->fd_id >= sw_idx)
  2910. break;
  2911. parent = rule;
  2912. }
  2913. /* if there is an old rule occupying our place remove it */
  2914. if (rule && (rule->fd_id == sw_idx)) {
  2915. /* Remove this rule, since we're either deleting it, or
  2916. * replacing it.
  2917. */
  2918. err = i40e_add_del_fdir(vsi, rule, false);
  2919. hlist_del(&rule->fdir_node);
  2920. kfree(rule);
  2921. pf->fdir_pf_active_filters--;
  2922. }
  2923. /* If we weren't given an input, this is a delete, so just return the
  2924. * error code indicating if there was an entry at the requested slot
  2925. */
  2926. if (!input)
  2927. return err;
  2928. /* Otherwise, install the new rule as requested */
  2929. INIT_HLIST_NODE(&input->fdir_node);
  2930. /* add filter to the list */
  2931. if (parent)
  2932. hlist_add_behind(&input->fdir_node, &parent->fdir_node);
  2933. else
  2934. hlist_add_head(&input->fdir_node,
  2935. &pf->fdir_filter_list);
  2936. /* update counts */
  2937. pf->fdir_pf_active_filters++;
  2938. return 0;
  2939. }
  2940. /**
  2941. * i40e_prune_flex_pit_list - Cleanup unused entries in FLX_PIT table
  2942. * @pf: pointer to PF structure
  2943. *
  2944. * This function searches the list of filters and determines which FLX_PIT
  2945. * entries are still required. It will prune any entries which are no longer
  2946. * in use after the deletion.
  2947. **/
  2948. static void i40e_prune_flex_pit_list(struct i40e_pf *pf)
  2949. {
  2950. struct i40e_flex_pit *entry, *tmp;
  2951. struct i40e_fdir_filter *rule;
  2952. /* First, we'll check the l3 table */
  2953. list_for_each_entry_safe(entry, tmp, &pf->l3_flex_pit_list, list) {
  2954. bool found = false;
  2955. hlist_for_each_entry(rule, &pf->fdir_filter_list, fdir_node) {
  2956. if (rule->flow_type != IP_USER_FLOW)
  2957. continue;
  2958. if (rule->flex_filter &&
  2959. rule->flex_offset == entry->src_offset) {
  2960. found = true;
  2961. break;
  2962. }
  2963. }
  2964. /* If we didn't find the filter, then we can prune this entry
  2965. * from the list.
  2966. */
  2967. if (!found) {
  2968. list_del(&entry->list);
  2969. kfree(entry);
  2970. }
  2971. }
  2972. /* Followed by the L4 table */
  2973. list_for_each_entry_safe(entry, tmp, &pf->l4_flex_pit_list, list) {
  2974. bool found = false;
  2975. hlist_for_each_entry(rule, &pf->fdir_filter_list, fdir_node) {
  2976. /* Skip this filter if it's L3, since we already
  2977. * checked those in the above loop
  2978. */
  2979. if (rule->flow_type == IP_USER_FLOW)
  2980. continue;
  2981. if (rule->flex_filter &&
  2982. rule->flex_offset == entry->src_offset) {
  2983. found = true;
  2984. break;
  2985. }
  2986. }
  2987. /* If we didn't find the filter, then we can prune this entry
  2988. * from the list.
  2989. */
  2990. if (!found) {
  2991. list_del(&entry->list);
  2992. kfree(entry);
  2993. }
  2994. }
  2995. }
  2996. /**
  2997. * i40e_del_fdir_entry - Deletes a Flow Director filter entry
  2998. * @vsi: Pointer to the targeted VSI
  2999. * @cmd: The command to get or set Rx flow classification rules
  3000. *
  3001. * The function removes a Flow Director filter entry from the
  3002. * hlist of the corresponding PF
  3003. *
  3004. * Returns 0 on success
  3005. */
  3006. static int i40e_del_fdir_entry(struct i40e_vsi *vsi,
  3007. struct ethtool_rxnfc *cmd)
  3008. {
  3009. struct ethtool_rx_flow_spec *fsp =
  3010. (struct ethtool_rx_flow_spec *)&cmd->fs;
  3011. struct i40e_pf *pf = vsi->back;
  3012. int ret = 0;
  3013. if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state) ||
  3014. test_bit(__I40E_RESET_INTR_RECEIVED, pf->state))
  3015. return -EBUSY;
  3016. if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state))
  3017. return -EBUSY;
  3018. ret = i40e_update_ethtool_fdir_entry(vsi, NULL, fsp->location, cmd);
  3019. i40e_prune_flex_pit_list(pf);
  3020. i40e_fdir_check_and_reenable(pf);
  3021. return ret;
  3022. }
  3023. /**
  3024. * i40e_unused_pit_index - Find an unused PIT index for given list
  3025. * @pf: the PF data structure
  3026. *
  3027. * Find the first unused flexible PIT index entry. We search both the L3 and
  3028. * L4 flexible PIT lists so that the returned index is unique and unused by
  3029. * either currently programmed L3 or L4 filters. We use a bit field as storage
  3030. * to track which indexes are already used.
  3031. **/
  3032. static u8 i40e_unused_pit_index(struct i40e_pf *pf)
  3033. {
  3034. unsigned long available_index = 0xFF;
  3035. struct i40e_flex_pit *entry;
  3036. /* We need to make sure that the new index isn't in use by either L3
  3037. * or L4 filters so that IP_USER_FLOW filters can program both L3 and
  3038. * L4 to use the same index.
  3039. */
  3040. list_for_each_entry(entry, &pf->l4_flex_pit_list, list)
  3041. clear_bit(entry->pit_index, &available_index);
  3042. list_for_each_entry(entry, &pf->l3_flex_pit_list, list)
  3043. clear_bit(entry->pit_index, &available_index);
  3044. return find_first_bit(&available_index, 8);
  3045. }
  3046. /**
  3047. * i40e_find_flex_offset - Find an existing flex src_offset
  3048. * @flex_pit_list: L3 or L4 flex PIT list
  3049. * @src_offset: new src_offset to find
  3050. *
  3051. * Searches the flex_pit_list for an existing offset. If no offset is
  3052. * currently programmed, then this will return an ERR_PTR if there is no space
  3053. * to add a new offset, otherwise it returns NULL.
  3054. **/
  3055. static
  3056. struct i40e_flex_pit *i40e_find_flex_offset(struct list_head *flex_pit_list,
  3057. u16 src_offset)
  3058. {
  3059. struct i40e_flex_pit *entry;
  3060. int size = 0;
  3061. /* Search for the src_offset first. If we find a matching entry
  3062. * already programmed, we can simply re-use it.
  3063. */
  3064. list_for_each_entry(entry, flex_pit_list, list) {
  3065. size++;
  3066. if (entry->src_offset == src_offset)
  3067. return entry;
  3068. }
  3069. /* If we haven't found an entry yet, then the provided src offset has
  3070. * not yet been programmed. We will program the src offset later on,
  3071. * but we need to indicate whether there is enough space to do so
  3072. * here. We'll make use of ERR_PTR for this purpose.
  3073. */
  3074. if (size >= I40E_FLEX_PIT_TABLE_SIZE)
  3075. return ERR_PTR(-ENOSPC);
  3076. return NULL;
  3077. }
  3078. /**
  3079. * i40e_add_flex_offset - Add src_offset to flex PIT table list
  3080. * @flex_pit_list: L3 or L4 flex PIT list
  3081. * @src_offset: new src_offset to add
  3082. * @pit_index: the PIT index to program
  3083. *
  3084. * This function programs the new src_offset to the list. It is expected that
  3085. * i40e_find_flex_offset has already been tried and returned NULL, indicating
  3086. * that this offset is not programmed, and that the list has enough space to
  3087. * store another offset.
  3088. *
  3089. * Returns 0 on success, and negative value on error.
  3090. **/
  3091. static int i40e_add_flex_offset(struct list_head *flex_pit_list,
  3092. u16 src_offset,
  3093. u8 pit_index)
  3094. {
  3095. struct i40e_flex_pit *new_pit, *entry;
  3096. new_pit = kzalloc(sizeof(*entry), GFP_KERNEL);
  3097. if (!new_pit)
  3098. return -ENOMEM;
  3099. new_pit->src_offset = src_offset;
  3100. new_pit->pit_index = pit_index;
  3101. /* We need to insert this item such that the list is sorted by
  3102. * src_offset in ascending order.
  3103. */
  3104. list_for_each_entry(entry, flex_pit_list, list) {
  3105. if (new_pit->src_offset < entry->src_offset) {
  3106. list_add_tail(&new_pit->list, &entry->list);
  3107. return 0;
  3108. }
  3109. /* If we found an entry with our offset already programmed we
  3110. * can simply return here, after freeing the memory. However,
  3111. * if the pit_index does not match we need to report an error.
  3112. */
  3113. if (new_pit->src_offset == entry->src_offset) {
  3114. int err = 0;
  3115. /* If the PIT index is not the same we can't re-use
  3116. * the entry, so we must report an error.
  3117. */
  3118. if (new_pit->pit_index != entry->pit_index)
  3119. err = -EINVAL;
  3120. kfree(new_pit);
  3121. return err;
  3122. }
  3123. }
  3124. /* If we reached here, then we haven't yet added the item. This means
  3125. * that we should add the item at the end of the list.
  3126. */
  3127. list_add_tail(&new_pit->list, flex_pit_list);
  3128. return 0;
  3129. }
  3130. /**
  3131. * __i40e_reprogram_flex_pit - Re-program specific FLX_PIT table
  3132. * @pf: Pointer to the PF structure
  3133. * @flex_pit_list: list of flexible src offsets in use
  3134. * @flex_pit_start: index to first entry for this section of the table
  3135. *
  3136. * In order to handle flexible data, the hardware uses a table of values
  3137. * called the FLX_PIT table. This table is used to indicate which sections of
  3138. * the input correspond to what PIT index values. Unfortunately, hardware is
  3139. * very restrictive about programming this table. Entries must be ordered by
  3140. * src_offset in ascending order, without duplicates. Additionally, unused
  3141. * entries must be set to the unused index value, and must have valid size and
  3142. * length according to the src_offset ordering.
  3143. *
  3144. * This function will reprogram the FLX_PIT register from a book-keeping
  3145. * structure that we guarantee is already ordered correctly, and has no more
  3146. * than 3 entries.
  3147. *
  3148. * To make things easier, we only support flexible values of one word length,
  3149. * rather than allowing variable length flexible values.
  3150. **/
  3151. static void __i40e_reprogram_flex_pit(struct i40e_pf *pf,
  3152. struct list_head *flex_pit_list,
  3153. int flex_pit_start)
  3154. {
  3155. struct i40e_flex_pit *entry = NULL;
  3156. u16 last_offset = 0;
  3157. int i = 0, j = 0;
  3158. /* First, loop over the list of flex PIT entries, and reprogram the
  3159. * registers.
  3160. */
  3161. list_for_each_entry(entry, flex_pit_list, list) {
  3162. /* We have to be careful when programming values for the
  3163. * largest SRC_OFFSET value. It is possible that adding
  3164. * additional empty values at the end would overflow the space
  3165. * for the SRC_OFFSET in the FLX_PIT register. To avoid this,
  3166. * we check here and add the empty values prior to adding the
  3167. * largest value.
  3168. *
  3169. * To determine this, we will use a loop from i+1 to 3, which
  3170. * will determine whether the unused entries would have valid
  3171. * SRC_OFFSET. Note that there cannot be extra entries past
  3172. * this value, because the only valid values would have been
  3173. * larger than I40E_MAX_FLEX_SRC_OFFSET, and thus would not
  3174. * have been added to the list in the first place.
  3175. */
  3176. for (j = i + 1; j < 3; j++) {
  3177. u16 offset = entry->src_offset + j;
  3178. int index = flex_pit_start + i;
  3179. u32 value = I40E_FLEX_PREP_VAL(I40E_FLEX_DEST_UNUSED,
  3180. 1,
  3181. offset - 3);
  3182. if (offset > I40E_MAX_FLEX_SRC_OFFSET) {
  3183. i40e_write_rx_ctl(&pf->hw,
  3184. I40E_PRTQF_FLX_PIT(index),
  3185. value);
  3186. i++;
  3187. }
  3188. }
  3189. /* Now, we can program the actual value into the table */
  3190. i40e_write_rx_ctl(&pf->hw,
  3191. I40E_PRTQF_FLX_PIT(flex_pit_start + i),
  3192. I40E_FLEX_PREP_VAL(entry->pit_index + 50,
  3193. 1,
  3194. entry->src_offset));
  3195. i++;
  3196. }
  3197. /* In order to program the last entries in the table, we need to
  3198. * determine the valid offset. If the list is empty, we'll just start
  3199. * with 0. Otherwise, we'll start with the last item offset and add 1.
  3200. * This ensures that all entries have valid sizes. If we don't do this
  3201. * correctly, the hardware will disable flexible field parsing.
  3202. */
  3203. if (!list_empty(flex_pit_list))
  3204. last_offset = list_prev_entry(entry, list)->src_offset + 1;
  3205. for (; i < 3; i++, last_offset++) {
  3206. i40e_write_rx_ctl(&pf->hw,
  3207. I40E_PRTQF_FLX_PIT(flex_pit_start + i),
  3208. I40E_FLEX_PREP_VAL(I40E_FLEX_DEST_UNUSED,
  3209. 1,
  3210. last_offset));
  3211. }
  3212. }
  3213. /**
  3214. * i40e_reprogram_flex_pit - Reprogram all FLX_PIT tables after input set change
  3215. * @pf: pointer to the PF structure
  3216. *
  3217. * This function reprograms both the L3 and L4 FLX_PIT tables. See the
  3218. * internal helper function for implementation details.
  3219. **/
  3220. static void i40e_reprogram_flex_pit(struct i40e_pf *pf)
  3221. {
  3222. __i40e_reprogram_flex_pit(pf, &pf->l3_flex_pit_list,
  3223. I40E_FLEX_PIT_IDX_START_L3);
  3224. __i40e_reprogram_flex_pit(pf, &pf->l4_flex_pit_list,
  3225. I40E_FLEX_PIT_IDX_START_L4);
  3226. /* We also need to program the L3 and L4 GLQF ORT register */
  3227. i40e_write_rx_ctl(&pf->hw,
  3228. I40E_GLQF_ORT(I40E_L3_GLQF_ORT_IDX),
  3229. I40E_ORT_PREP_VAL(I40E_FLEX_PIT_IDX_START_L3,
  3230. 3, 1));
  3231. i40e_write_rx_ctl(&pf->hw,
  3232. I40E_GLQF_ORT(I40E_L4_GLQF_ORT_IDX),
  3233. I40E_ORT_PREP_VAL(I40E_FLEX_PIT_IDX_START_L4,
  3234. 3, 1));
  3235. }
  3236. /**
  3237. * i40e_flow_str - Converts a flow_type into a human readable string
  3238. * @fsp: the flow specification
  3239. *
  3240. * Currently only flow types we support are included here, and the string
  3241. * value attempts to match what ethtool would use to configure this flow type.
  3242. **/
  3243. static const char *i40e_flow_str(struct ethtool_rx_flow_spec *fsp)
  3244. {
  3245. switch (fsp->flow_type & ~FLOW_EXT) {
  3246. case TCP_V4_FLOW:
  3247. return "tcp4";
  3248. case UDP_V4_FLOW:
  3249. return "udp4";
  3250. case SCTP_V4_FLOW:
  3251. return "sctp4";
  3252. case IP_USER_FLOW:
  3253. return "ip4";
  3254. default:
  3255. return "unknown";
  3256. }
  3257. }
  3258. /**
  3259. * i40e_pit_index_to_mask - Return the FLEX mask for a given PIT index
  3260. * @pit_index: PIT index to convert
  3261. *
  3262. * Returns the mask for a given PIT index. Will return 0 if the pit_index is
  3263. * of range.
  3264. **/
  3265. static u64 i40e_pit_index_to_mask(int pit_index)
  3266. {
  3267. switch (pit_index) {
  3268. case 0:
  3269. return I40E_FLEX_50_MASK;
  3270. case 1:
  3271. return I40E_FLEX_51_MASK;
  3272. case 2:
  3273. return I40E_FLEX_52_MASK;
  3274. case 3:
  3275. return I40E_FLEX_53_MASK;
  3276. case 4:
  3277. return I40E_FLEX_54_MASK;
  3278. case 5:
  3279. return I40E_FLEX_55_MASK;
  3280. case 6:
  3281. return I40E_FLEX_56_MASK;
  3282. case 7:
  3283. return I40E_FLEX_57_MASK;
  3284. default:
  3285. return 0;
  3286. }
  3287. }
  3288. /**
  3289. * i40e_print_input_set - Show changes between two input sets
  3290. * @vsi: the vsi being configured
  3291. * @old: the old input set
  3292. * @new: the new input set
  3293. *
  3294. * Print the difference between old and new input sets by showing which series
  3295. * of words are toggled on or off. Only displays the bits we actually support
  3296. * changing.
  3297. **/
  3298. static void i40e_print_input_set(struct i40e_vsi *vsi, u64 old, u64 new)
  3299. {
  3300. struct i40e_pf *pf = vsi->back;
  3301. bool old_value, new_value;
  3302. int i;
  3303. old_value = !!(old & I40E_L3_SRC_MASK);
  3304. new_value = !!(new & I40E_L3_SRC_MASK);
  3305. if (old_value != new_value)
  3306. netif_info(pf, drv, vsi->netdev, "L3 source address: %s -> %s\n",
  3307. old_value ? "ON" : "OFF",
  3308. new_value ? "ON" : "OFF");
  3309. old_value = !!(old & I40E_L3_DST_MASK);
  3310. new_value = !!(new & I40E_L3_DST_MASK);
  3311. if (old_value != new_value)
  3312. netif_info(pf, drv, vsi->netdev, "L3 destination address: %s -> %s\n",
  3313. old_value ? "ON" : "OFF",
  3314. new_value ? "ON" : "OFF");
  3315. old_value = !!(old & I40E_L4_SRC_MASK);
  3316. new_value = !!(new & I40E_L4_SRC_MASK);
  3317. if (old_value != new_value)
  3318. netif_info(pf, drv, vsi->netdev, "L4 source port: %s -> %s\n",
  3319. old_value ? "ON" : "OFF",
  3320. new_value ? "ON" : "OFF");
  3321. old_value = !!(old & I40E_L4_DST_MASK);
  3322. new_value = !!(new & I40E_L4_DST_MASK);
  3323. if (old_value != new_value)
  3324. netif_info(pf, drv, vsi->netdev, "L4 destination port: %s -> %s\n",
  3325. old_value ? "ON" : "OFF",
  3326. new_value ? "ON" : "OFF");
  3327. old_value = !!(old & I40E_VERIFY_TAG_MASK);
  3328. new_value = !!(new & I40E_VERIFY_TAG_MASK);
  3329. if (old_value != new_value)
  3330. netif_info(pf, drv, vsi->netdev, "SCTP verification tag: %s -> %s\n",
  3331. old_value ? "ON" : "OFF",
  3332. new_value ? "ON" : "OFF");
  3333. /* Show change of flexible filter entries */
  3334. for (i = 0; i < I40E_FLEX_INDEX_ENTRIES; i++) {
  3335. u64 flex_mask = i40e_pit_index_to_mask(i);
  3336. old_value = !!(old & flex_mask);
  3337. new_value = !!(new & flex_mask);
  3338. if (old_value != new_value)
  3339. netif_info(pf, drv, vsi->netdev, "FLEX index %d: %s -> %s\n",
  3340. i,
  3341. old_value ? "ON" : "OFF",
  3342. new_value ? "ON" : "OFF");
  3343. }
  3344. netif_info(pf, drv, vsi->netdev, " Current input set: %0llx\n",
  3345. old);
  3346. netif_info(pf, drv, vsi->netdev, "Requested input set: %0llx\n",
  3347. new);
  3348. }
  3349. /**
  3350. * i40e_check_fdir_input_set - Check that a given rx_flow_spec mask is valid
  3351. * @vsi: pointer to the targeted VSI
  3352. * @fsp: pointer to Rx flow specification
  3353. * @userdef: userdefined data from flow specification
  3354. *
  3355. * Ensures that a given ethtool_rx_flow_spec has a valid mask. Some support
  3356. * for partial matches exists with a few limitations. First, hardware only
  3357. * supports masking by word boundary (2 bytes) and not per individual bit.
  3358. * Second, hardware is limited to using one mask for a flow type and cannot
  3359. * use a separate mask for each filter.
  3360. *
  3361. * To support these limitations, if we already have a configured filter for
  3362. * the specified type, this function enforces that new filters of the type
  3363. * match the configured input set. Otherwise, if we do not have a filter of
  3364. * the specified type, we allow the input set to be updated to match the
  3365. * desired filter.
  3366. *
  3367. * To help ensure that administrators understand why filters weren't displayed
  3368. * as supported, we print a diagnostic message displaying how the input set
  3369. * would change and warning to delete the preexisting filters if required.
  3370. *
  3371. * Returns 0 on successful input set match, and a negative return code on
  3372. * failure.
  3373. **/
  3374. static int i40e_check_fdir_input_set(struct i40e_vsi *vsi,
  3375. struct ethtool_rx_flow_spec *fsp,
  3376. struct i40e_rx_flow_userdef *userdef)
  3377. {
  3378. struct i40e_pf *pf = vsi->back;
  3379. struct ethtool_tcpip4_spec *tcp_ip4_spec;
  3380. struct ethtool_usrip4_spec *usr_ip4_spec;
  3381. u64 current_mask, new_mask;
  3382. bool new_flex_offset = false;
  3383. bool flex_l3 = false;
  3384. u16 *fdir_filter_count;
  3385. u16 index, src_offset = 0;
  3386. u8 pit_index = 0;
  3387. int err;
  3388. switch (fsp->flow_type & ~FLOW_EXT) {
  3389. case SCTP_V4_FLOW:
  3390. index = I40E_FILTER_PCTYPE_NONF_IPV4_SCTP;
  3391. fdir_filter_count = &pf->fd_sctp4_filter_cnt;
  3392. break;
  3393. case TCP_V4_FLOW:
  3394. index = I40E_FILTER_PCTYPE_NONF_IPV4_TCP;
  3395. fdir_filter_count = &pf->fd_tcp4_filter_cnt;
  3396. break;
  3397. case UDP_V4_FLOW:
  3398. index = I40E_FILTER_PCTYPE_NONF_IPV4_UDP;
  3399. fdir_filter_count = &pf->fd_udp4_filter_cnt;
  3400. break;
  3401. case IP_USER_FLOW:
  3402. index = I40E_FILTER_PCTYPE_NONF_IPV4_OTHER;
  3403. fdir_filter_count = &pf->fd_ip4_filter_cnt;
  3404. flex_l3 = true;
  3405. break;
  3406. default:
  3407. return -EOPNOTSUPP;
  3408. }
  3409. /* Read the current input set from register memory. */
  3410. current_mask = i40e_read_fd_input_set(pf, index);
  3411. new_mask = current_mask;
  3412. /* Determine, if any, the required changes to the input set in order
  3413. * to support the provided mask.
  3414. *
  3415. * Hardware only supports masking at word (2 byte) granularity and does
  3416. * not support full bitwise masking. This implementation simplifies
  3417. * even further and only supports fully enabled or fully disabled
  3418. * masks for each field, even though we could split the ip4src and
  3419. * ip4dst fields.
  3420. */
  3421. switch (fsp->flow_type & ~FLOW_EXT) {
  3422. case SCTP_V4_FLOW:
  3423. new_mask &= ~I40E_VERIFY_TAG_MASK;
  3424. /* Fall through */
  3425. case TCP_V4_FLOW:
  3426. case UDP_V4_FLOW:
  3427. tcp_ip4_spec = &fsp->m_u.tcp_ip4_spec;
  3428. /* IPv4 source address */
  3429. if (tcp_ip4_spec->ip4src == htonl(0xFFFFFFFF))
  3430. new_mask |= I40E_L3_SRC_MASK;
  3431. else if (!tcp_ip4_spec->ip4src)
  3432. new_mask &= ~I40E_L3_SRC_MASK;
  3433. else
  3434. return -EOPNOTSUPP;
  3435. /* IPv4 destination address */
  3436. if (tcp_ip4_spec->ip4dst == htonl(0xFFFFFFFF))
  3437. new_mask |= I40E_L3_DST_MASK;
  3438. else if (!tcp_ip4_spec->ip4dst)
  3439. new_mask &= ~I40E_L3_DST_MASK;
  3440. else
  3441. return -EOPNOTSUPP;
  3442. /* L4 source port */
  3443. if (tcp_ip4_spec->psrc == htons(0xFFFF))
  3444. new_mask |= I40E_L4_SRC_MASK;
  3445. else if (!tcp_ip4_spec->psrc)
  3446. new_mask &= ~I40E_L4_SRC_MASK;
  3447. else
  3448. return -EOPNOTSUPP;
  3449. /* L4 destination port */
  3450. if (tcp_ip4_spec->pdst == htons(0xFFFF))
  3451. new_mask |= I40E_L4_DST_MASK;
  3452. else if (!tcp_ip4_spec->pdst)
  3453. new_mask &= ~I40E_L4_DST_MASK;
  3454. else
  3455. return -EOPNOTSUPP;
  3456. /* Filtering on Type of Service is not supported. */
  3457. if (tcp_ip4_spec->tos)
  3458. return -EOPNOTSUPP;
  3459. break;
  3460. case IP_USER_FLOW:
  3461. usr_ip4_spec = &fsp->m_u.usr_ip4_spec;
  3462. /* IPv4 source address */
  3463. if (usr_ip4_spec->ip4src == htonl(0xFFFFFFFF))
  3464. new_mask |= I40E_L3_SRC_MASK;
  3465. else if (!usr_ip4_spec->ip4src)
  3466. new_mask &= ~I40E_L3_SRC_MASK;
  3467. else
  3468. return -EOPNOTSUPP;
  3469. /* IPv4 destination address */
  3470. if (usr_ip4_spec->ip4dst == htonl(0xFFFFFFFF))
  3471. new_mask |= I40E_L3_DST_MASK;
  3472. else if (!usr_ip4_spec->ip4dst)
  3473. new_mask &= ~I40E_L3_DST_MASK;
  3474. else
  3475. return -EOPNOTSUPP;
  3476. /* First 4 bytes of L4 header */
  3477. if (usr_ip4_spec->l4_4_bytes == htonl(0xFFFFFFFF))
  3478. new_mask |= I40E_L4_SRC_MASK | I40E_L4_DST_MASK;
  3479. else if (!usr_ip4_spec->l4_4_bytes)
  3480. new_mask &= ~(I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
  3481. else
  3482. return -EOPNOTSUPP;
  3483. /* Filtering on Type of Service is not supported. */
  3484. if (usr_ip4_spec->tos)
  3485. return -EOPNOTSUPP;
  3486. /* Filtering on IP version is not supported */
  3487. if (usr_ip4_spec->ip_ver)
  3488. return -EINVAL;
  3489. /* Filtering on L4 protocol is not supported */
  3490. if (usr_ip4_spec->proto)
  3491. return -EINVAL;
  3492. break;
  3493. default:
  3494. return -EOPNOTSUPP;
  3495. }
  3496. /* First, clear all flexible filter entries */
  3497. new_mask &= ~I40E_FLEX_INPUT_MASK;
  3498. /* If we have a flexible filter, try to add this offset to the correct
  3499. * flexible filter PIT list. Once finished, we can update the mask.
  3500. * If the src_offset changed, we will get a new mask value which will
  3501. * trigger an input set change.
  3502. */
  3503. if (userdef->flex_filter) {
  3504. struct i40e_flex_pit *l3_flex_pit = NULL, *flex_pit = NULL;
  3505. /* Flexible offset must be even, since the flexible payload
  3506. * must be aligned on 2-byte boundary.
  3507. */
  3508. if (userdef->flex_offset & 0x1) {
  3509. dev_warn(&pf->pdev->dev,
  3510. "Flexible data offset must be 2-byte aligned\n");
  3511. return -EINVAL;
  3512. }
  3513. src_offset = userdef->flex_offset >> 1;
  3514. /* FLX_PIT source offset value is only so large */
  3515. if (src_offset > I40E_MAX_FLEX_SRC_OFFSET) {
  3516. dev_warn(&pf->pdev->dev,
  3517. "Flexible data must reside within first 64 bytes of the packet payload\n");
  3518. return -EINVAL;
  3519. }
  3520. /* See if this offset has already been programmed. If we get
  3521. * an ERR_PTR, then the filter is not safe to add. Otherwise,
  3522. * if we get a NULL pointer, this means we will need to add
  3523. * the offset.
  3524. */
  3525. flex_pit = i40e_find_flex_offset(&pf->l4_flex_pit_list,
  3526. src_offset);
  3527. if (IS_ERR(flex_pit))
  3528. return PTR_ERR(flex_pit);
  3529. /* IP_USER_FLOW filters match both L4 (ICMP) and L3 (unknown)
  3530. * packet types, and thus we need to program both L3 and L4
  3531. * flexible values. These must have identical flexible index,
  3532. * as otherwise we can't correctly program the input set. So
  3533. * we'll find both an L3 and L4 index and make sure they are
  3534. * the same.
  3535. */
  3536. if (flex_l3) {
  3537. l3_flex_pit =
  3538. i40e_find_flex_offset(&pf->l3_flex_pit_list,
  3539. src_offset);
  3540. if (IS_ERR(l3_flex_pit))
  3541. return PTR_ERR(l3_flex_pit);
  3542. if (flex_pit) {
  3543. /* If we already had a matching L4 entry, we
  3544. * need to make sure that the L3 entry we
  3545. * obtained uses the same index.
  3546. */
  3547. if (l3_flex_pit) {
  3548. if (l3_flex_pit->pit_index !=
  3549. flex_pit->pit_index) {
  3550. return -EINVAL;
  3551. }
  3552. } else {
  3553. new_flex_offset = true;
  3554. }
  3555. } else {
  3556. flex_pit = l3_flex_pit;
  3557. }
  3558. }
  3559. /* If we didn't find an existing flex offset, we need to
  3560. * program a new one. However, we don't immediately program it
  3561. * here because we will wait to program until after we check
  3562. * that it is safe to change the input set.
  3563. */
  3564. if (!flex_pit) {
  3565. new_flex_offset = true;
  3566. pit_index = i40e_unused_pit_index(pf);
  3567. } else {
  3568. pit_index = flex_pit->pit_index;
  3569. }
  3570. /* Update the mask with the new offset */
  3571. new_mask |= i40e_pit_index_to_mask(pit_index);
  3572. }
  3573. /* If the mask and flexible filter offsets for this filter match the
  3574. * currently programmed values we don't need any input set change, so
  3575. * this filter is safe to install.
  3576. */
  3577. if (new_mask == current_mask && !new_flex_offset)
  3578. return 0;
  3579. netif_info(pf, drv, vsi->netdev, "Input set change requested for %s flows:\n",
  3580. i40e_flow_str(fsp));
  3581. i40e_print_input_set(vsi, current_mask, new_mask);
  3582. if (new_flex_offset) {
  3583. netif_info(pf, drv, vsi->netdev, "FLEX index %d: Offset -> %d",
  3584. pit_index, src_offset);
  3585. }
  3586. /* Hardware input sets are global across multiple ports, so even the
  3587. * main port cannot change them when in MFP mode as this would impact
  3588. * any filters on the other ports.
  3589. */
  3590. if (pf->flags & I40E_FLAG_MFP_ENABLED) {
  3591. netif_err(pf, drv, vsi->netdev, "Cannot change Flow Director input sets while MFP is enabled\n");
  3592. return -EOPNOTSUPP;
  3593. }
  3594. /* This filter requires us to update the input set. However, hardware
  3595. * only supports one input set per flow type, and does not support
  3596. * separate masks for each filter. This means that we can only support
  3597. * a single mask for all filters of a specific type.
  3598. *
  3599. * If we have preexisting filters, they obviously depend on the
  3600. * current programmed input set. Display a diagnostic message in this
  3601. * case explaining why the filter could not be accepted.
  3602. */
  3603. if (*fdir_filter_count) {
  3604. netif_err(pf, drv, vsi->netdev, "Cannot change input set for %s flows until %d preexisting filters are removed\n",
  3605. i40e_flow_str(fsp),
  3606. *fdir_filter_count);
  3607. return -EOPNOTSUPP;
  3608. }
  3609. i40e_write_fd_input_set(pf, index, new_mask);
  3610. /* IP_USER_FLOW filters match both IPv4/Other and IPv4/Fragmented
  3611. * frames. If we're programming the input set for IPv4/Other, we also
  3612. * need to program the IPv4/Fragmented input set. Since we don't have
  3613. * separate support, we'll always assume and enforce that the two flow
  3614. * types must have matching input sets.
  3615. */
  3616. if (index == I40E_FILTER_PCTYPE_NONF_IPV4_OTHER)
  3617. i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_FRAG_IPV4,
  3618. new_mask);
  3619. /* Add the new offset and update table, if necessary */
  3620. if (new_flex_offset) {
  3621. err = i40e_add_flex_offset(&pf->l4_flex_pit_list, src_offset,
  3622. pit_index);
  3623. if (err)
  3624. return err;
  3625. if (flex_l3) {
  3626. err = i40e_add_flex_offset(&pf->l3_flex_pit_list,
  3627. src_offset,
  3628. pit_index);
  3629. if (err)
  3630. return err;
  3631. }
  3632. i40e_reprogram_flex_pit(pf);
  3633. }
  3634. return 0;
  3635. }
  3636. /**
  3637. * i40e_match_fdir_filter - Return true of two filters match
  3638. * @a: pointer to filter struct
  3639. * @b: pointer to filter struct
  3640. *
  3641. * Returns true if the two filters match exactly the same criteria. I.e. they
  3642. * match the same flow type and have the same parameters. We don't need to
  3643. * check any input-set since all filters of the same flow type must use the
  3644. * same input set.
  3645. **/
  3646. static bool i40e_match_fdir_filter(struct i40e_fdir_filter *a,
  3647. struct i40e_fdir_filter *b)
  3648. {
  3649. /* The filters do not much if any of these criteria differ. */
  3650. if (a->dst_ip != b->dst_ip ||
  3651. a->src_ip != b->src_ip ||
  3652. a->dst_port != b->dst_port ||
  3653. a->src_port != b->src_port ||
  3654. a->flow_type != b->flow_type ||
  3655. a->ip4_proto != b->ip4_proto)
  3656. return false;
  3657. return true;
  3658. }
  3659. /**
  3660. * i40e_disallow_matching_filters - Check that new filters differ
  3661. * @vsi: pointer to the targeted VSI
  3662. * @input: new filter to check
  3663. *
  3664. * Due to hardware limitations, it is not possible for two filters that match
  3665. * similar criteria to be programmed at the same time. This is true for a few
  3666. * reasons:
  3667. *
  3668. * (a) all filters matching a particular flow type must use the same input
  3669. * set, that is they must match the same criteria.
  3670. * (b) different flow types will never match the same packet, as the flow type
  3671. * is decided by hardware before checking which rules apply.
  3672. * (c) hardware has no way to distinguish which order filters apply in.
  3673. *
  3674. * Due to this, we can't really support using the location data to order
  3675. * filters in the hardware parsing. It is technically possible for the user to
  3676. * request two filters matching the same criteria but which select different
  3677. * queues. In this case, rather than keep both filters in the list, we reject
  3678. * the 2nd filter when the user requests adding it.
  3679. *
  3680. * This avoids needing to track location for programming the filter to
  3681. * hardware, and ensures that we avoid some strange scenarios involving
  3682. * deleting filters which match the same criteria.
  3683. **/
  3684. static int i40e_disallow_matching_filters(struct i40e_vsi *vsi,
  3685. struct i40e_fdir_filter *input)
  3686. {
  3687. struct i40e_pf *pf = vsi->back;
  3688. struct i40e_fdir_filter *rule;
  3689. struct hlist_node *node2;
  3690. /* Loop through every filter, and check that it doesn't match */
  3691. hlist_for_each_entry_safe(rule, node2,
  3692. &pf->fdir_filter_list, fdir_node) {
  3693. /* Don't check the filters match if they share the same fd_id,
  3694. * since the new filter is actually just updating the target
  3695. * of the old filter.
  3696. */
  3697. if (rule->fd_id == input->fd_id)
  3698. continue;
  3699. /* If any filters match, then print a warning message to the
  3700. * kernel message buffer and bail out.
  3701. */
  3702. if (i40e_match_fdir_filter(rule, input)) {
  3703. dev_warn(&pf->pdev->dev,
  3704. "Existing user defined filter %d already matches this flow.\n",
  3705. rule->fd_id);
  3706. return -EINVAL;
  3707. }
  3708. }
  3709. return 0;
  3710. }
  3711. /**
  3712. * i40e_add_fdir_ethtool - Add/Remove Flow Director filters
  3713. * @vsi: pointer to the targeted VSI
  3714. * @cmd: command to get or set RX flow classification rules
  3715. *
  3716. * Add Flow Director filters for a specific flow spec based on their
  3717. * protocol. Returns 0 if the filters were successfully added.
  3718. **/
  3719. static int i40e_add_fdir_ethtool(struct i40e_vsi *vsi,
  3720. struct ethtool_rxnfc *cmd)
  3721. {
  3722. struct i40e_rx_flow_userdef userdef;
  3723. struct ethtool_rx_flow_spec *fsp;
  3724. struct i40e_fdir_filter *input;
  3725. u16 dest_vsi = 0, q_index = 0;
  3726. struct i40e_pf *pf;
  3727. int ret = -EINVAL;
  3728. u8 dest_ctl;
  3729. if (!vsi)
  3730. return -EINVAL;
  3731. pf = vsi->back;
  3732. if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
  3733. return -EOPNOTSUPP;
  3734. if (test_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state))
  3735. return -ENOSPC;
  3736. if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state) ||
  3737. test_bit(__I40E_RESET_INTR_RECEIVED, pf->state))
  3738. return -EBUSY;
  3739. if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state))
  3740. return -EBUSY;
  3741. fsp = (struct ethtool_rx_flow_spec *)&cmd->fs;
  3742. /* Parse the user-defined field */
  3743. if (i40e_parse_rx_flow_user_data(fsp, &userdef))
  3744. return -EINVAL;
  3745. /* Extended MAC field is not supported */
  3746. if (fsp->flow_type & FLOW_MAC_EXT)
  3747. return -EINVAL;
  3748. ret = i40e_check_fdir_input_set(vsi, fsp, &userdef);
  3749. if (ret)
  3750. return ret;
  3751. if (fsp->location >= (pf->hw.func_caps.fd_filters_best_effort +
  3752. pf->hw.func_caps.fd_filters_guaranteed)) {
  3753. return -EINVAL;
  3754. }
  3755. /* ring_cookie is either the drop index, or is a mask of the queue
  3756. * index and VF id we wish to target.
  3757. */
  3758. if (fsp->ring_cookie == RX_CLS_FLOW_DISC) {
  3759. dest_ctl = I40E_FILTER_PROGRAM_DESC_DEST_DROP_PACKET;
  3760. } else {
  3761. u32 ring = ethtool_get_flow_spec_ring(fsp->ring_cookie);
  3762. u8 vf = ethtool_get_flow_spec_ring_vf(fsp->ring_cookie);
  3763. if (!vf) {
  3764. if (ring >= vsi->num_queue_pairs)
  3765. return -EINVAL;
  3766. dest_vsi = vsi->id;
  3767. } else {
  3768. /* VFs are zero-indexed, so we subtract one here */
  3769. vf--;
  3770. if (vf >= pf->num_alloc_vfs)
  3771. return -EINVAL;
  3772. if (ring >= pf->vf[vf].num_queue_pairs)
  3773. return -EINVAL;
  3774. dest_vsi = pf->vf[vf].lan_vsi_id;
  3775. }
  3776. dest_ctl = I40E_FILTER_PROGRAM_DESC_DEST_DIRECT_PACKET_QINDEX;
  3777. q_index = ring;
  3778. }
  3779. input = kzalloc(sizeof(*input), GFP_KERNEL);
  3780. if (!input)
  3781. return -ENOMEM;
  3782. input->fd_id = fsp->location;
  3783. input->q_index = q_index;
  3784. input->dest_vsi = dest_vsi;
  3785. input->dest_ctl = dest_ctl;
  3786. input->fd_status = I40E_FILTER_PROGRAM_DESC_FD_STATUS_FD_ID;
  3787. input->cnt_index = I40E_FD_SB_STAT_IDX(pf->hw.pf_id);
  3788. input->dst_ip = fsp->h_u.tcp_ip4_spec.ip4src;
  3789. input->src_ip = fsp->h_u.tcp_ip4_spec.ip4dst;
  3790. input->flow_type = fsp->flow_type & ~FLOW_EXT;
  3791. input->ip4_proto = fsp->h_u.usr_ip4_spec.proto;
  3792. /* Reverse the src and dest notion, since the HW expects them to be from
  3793. * Tx perspective where as the input from user is from Rx filter view.
  3794. */
  3795. input->dst_port = fsp->h_u.tcp_ip4_spec.psrc;
  3796. input->src_port = fsp->h_u.tcp_ip4_spec.pdst;
  3797. input->dst_ip = fsp->h_u.tcp_ip4_spec.ip4src;
  3798. input->src_ip = fsp->h_u.tcp_ip4_spec.ip4dst;
  3799. if (userdef.flex_filter) {
  3800. input->flex_filter = true;
  3801. input->flex_word = cpu_to_be16(userdef.flex_word);
  3802. input->flex_offset = userdef.flex_offset;
  3803. }
  3804. /* Avoid programming two filters with identical match criteria. */
  3805. ret = i40e_disallow_matching_filters(vsi, input);
  3806. if (ret)
  3807. goto free_filter_memory;
  3808. /* Add the input filter to the fdir_input_list, possibly replacing
  3809. * a previous filter. Do not free the input structure after adding it
  3810. * to the list as this would cause a use-after-free bug.
  3811. */
  3812. i40e_update_ethtool_fdir_entry(vsi, input, fsp->location, NULL);
  3813. ret = i40e_add_del_fdir(vsi, input, true);
  3814. if (ret)
  3815. goto remove_sw_rule;
  3816. return 0;
  3817. remove_sw_rule:
  3818. hlist_del(&input->fdir_node);
  3819. pf->fdir_pf_active_filters--;
  3820. free_filter_memory:
  3821. kfree(input);
  3822. return ret;
  3823. }
  3824. /**
  3825. * i40e_set_rxnfc - command to set RX flow classification rules
  3826. * @netdev: network interface device structure
  3827. * @cmd: ethtool rxnfc command
  3828. *
  3829. * Returns Success if the command is supported.
  3830. **/
  3831. static int i40e_set_rxnfc(struct net_device *netdev, struct ethtool_rxnfc *cmd)
  3832. {
  3833. struct i40e_netdev_priv *np = netdev_priv(netdev);
  3834. struct i40e_vsi *vsi = np->vsi;
  3835. struct i40e_pf *pf = vsi->back;
  3836. int ret = -EOPNOTSUPP;
  3837. switch (cmd->cmd) {
  3838. case ETHTOOL_SRXFH:
  3839. ret = i40e_set_rss_hash_opt(pf, cmd);
  3840. break;
  3841. case ETHTOOL_SRXCLSRLINS:
  3842. ret = i40e_add_fdir_ethtool(vsi, cmd);
  3843. break;
  3844. case ETHTOOL_SRXCLSRLDEL:
  3845. ret = i40e_del_fdir_entry(vsi, cmd);
  3846. break;
  3847. default:
  3848. break;
  3849. }
  3850. return ret;
  3851. }
  3852. /**
  3853. * i40e_max_channels - get Max number of combined channels supported
  3854. * @vsi: vsi pointer
  3855. **/
  3856. static unsigned int i40e_max_channels(struct i40e_vsi *vsi)
  3857. {
  3858. /* TODO: This code assumes DCB and FD is disabled for now. */
  3859. return vsi->alloc_queue_pairs;
  3860. }
  3861. /**
  3862. * i40e_get_channels - Get the current channels enabled and max supported etc.
  3863. * @dev: network interface device structure
  3864. * @ch: ethtool channels structure
  3865. *
  3866. * We don't support separate tx and rx queues as channels. The other count
  3867. * represents how many queues are being used for control. max_combined counts
  3868. * how many queue pairs we can support. They may not be mapped 1 to 1 with
  3869. * q_vectors since we support a lot more queue pairs than q_vectors.
  3870. **/
  3871. static void i40e_get_channels(struct net_device *dev,
  3872. struct ethtool_channels *ch)
  3873. {
  3874. struct i40e_netdev_priv *np = netdev_priv(dev);
  3875. struct i40e_vsi *vsi = np->vsi;
  3876. struct i40e_pf *pf = vsi->back;
  3877. /* report maximum channels */
  3878. ch->max_combined = i40e_max_channels(vsi);
  3879. /* report info for other vector */
  3880. ch->other_count = (pf->flags & I40E_FLAG_FD_SB_ENABLED) ? 1 : 0;
  3881. ch->max_other = ch->other_count;
  3882. /* Note: This code assumes DCB is disabled for now. */
  3883. ch->combined_count = vsi->num_queue_pairs;
  3884. }
  3885. /**
  3886. * i40e_set_channels - Set the new channels count.
  3887. * @dev: network interface device structure
  3888. * @ch: ethtool channels structure
  3889. *
  3890. * The new channels count may not be the same as requested by the user
  3891. * since it gets rounded down to a power of 2 value.
  3892. **/
  3893. static int i40e_set_channels(struct net_device *dev,
  3894. struct ethtool_channels *ch)
  3895. {
  3896. const u8 drop = I40E_FILTER_PROGRAM_DESC_DEST_DROP_PACKET;
  3897. struct i40e_netdev_priv *np = netdev_priv(dev);
  3898. unsigned int count = ch->combined_count;
  3899. struct i40e_vsi *vsi = np->vsi;
  3900. struct i40e_pf *pf = vsi->back;
  3901. struct i40e_fdir_filter *rule;
  3902. struct hlist_node *node2;
  3903. int new_count;
  3904. int err = 0;
  3905. /* We do not support setting channels for any other VSI at present */
  3906. if (vsi->type != I40E_VSI_MAIN)
  3907. return -EINVAL;
  3908. /* We do not support setting channels via ethtool when TCs are
  3909. * configured through mqprio
  3910. */
  3911. if (pf->flags & I40E_FLAG_TC_MQPRIO)
  3912. return -EINVAL;
  3913. /* verify they are not requesting separate vectors */
  3914. if (!count || ch->rx_count || ch->tx_count)
  3915. return -EINVAL;
  3916. /* verify other_count has not changed */
  3917. if (ch->other_count != ((pf->flags & I40E_FLAG_FD_SB_ENABLED) ? 1 : 0))
  3918. return -EINVAL;
  3919. /* verify the number of channels does not exceed hardware limits */
  3920. if (count > i40e_max_channels(vsi))
  3921. return -EINVAL;
  3922. /* verify that the number of channels does not invalidate any current
  3923. * flow director rules
  3924. */
  3925. hlist_for_each_entry_safe(rule, node2,
  3926. &pf->fdir_filter_list, fdir_node) {
  3927. if (rule->dest_ctl != drop && count <= rule->q_index) {
  3928. dev_warn(&pf->pdev->dev,
  3929. "Existing user defined filter %d assigns flow to queue %d\n",
  3930. rule->fd_id, rule->q_index);
  3931. err = -EINVAL;
  3932. }
  3933. }
  3934. if (err) {
  3935. dev_err(&pf->pdev->dev,
  3936. "Existing filter rules must be deleted to reduce combined channel count to %d\n",
  3937. count);
  3938. return err;
  3939. }
  3940. /* update feature limits from largest to smallest supported values */
  3941. /* TODO: Flow director limit, DCB etc */
  3942. /* use rss_reconfig to rebuild with new queue count and update traffic
  3943. * class queue mapping
  3944. */
  3945. new_count = i40e_reconfig_rss_queues(pf, count);
  3946. if (new_count > 0)
  3947. return 0;
  3948. else
  3949. return -EINVAL;
  3950. }
  3951. /**
  3952. * i40e_get_rxfh_key_size - get the RSS hash key size
  3953. * @netdev: network interface device structure
  3954. *
  3955. * Returns the table size.
  3956. **/
  3957. static u32 i40e_get_rxfh_key_size(struct net_device *netdev)
  3958. {
  3959. return I40E_HKEY_ARRAY_SIZE;
  3960. }
  3961. /**
  3962. * i40e_get_rxfh_indir_size - get the rx flow hash indirection table size
  3963. * @netdev: network interface device structure
  3964. *
  3965. * Returns the table size.
  3966. **/
  3967. static u32 i40e_get_rxfh_indir_size(struct net_device *netdev)
  3968. {
  3969. return I40E_HLUT_ARRAY_SIZE;
  3970. }
  3971. /**
  3972. * i40e_get_rxfh - get the rx flow hash indirection table
  3973. * @netdev: network interface device structure
  3974. * @indir: indirection table
  3975. * @key: hash key
  3976. * @hfunc: hash function
  3977. *
  3978. * Reads the indirection table directly from the hardware. Returns 0 on
  3979. * success.
  3980. **/
  3981. static int i40e_get_rxfh(struct net_device *netdev, u32 *indir, u8 *key,
  3982. u8 *hfunc)
  3983. {
  3984. struct i40e_netdev_priv *np = netdev_priv(netdev);
  3985. struct i40e_vsi *vsi = np->vsi;
  3986. u8 *lut, *seed = NULL;
  3987. int ret;
  3988. u16 i;
  3989. if (hfunc)
  3990. *hfunc = ETH_RSS_HASH_TOP;
  3991. if (!indir)
  3992. return 0;
  3993. seed = key;
  3994. lut = kzalloc(I40E_HLUT_ARRAY_SIZE, GFP_KERNEL);
  3995. if (!lut)
  3996. return -ENOMEM;
  3997. ret = i40e_get_rss(vsi, seed, lut, I40E_HLUT_ARRAY_SIZE);
  3998. if (ret)
  3999. goto out;
  4000. for (i = 0; i < I40E_HLUT_ARRAY_SIZE; i++)
  4001. indir[i] = (u32)(lut[i]);
  4002. out:
  4003. kfree(lut);
  4004. return ret;
  4005. }
  4006. /**
  4007. * i40e_set_rxfh - set the rx flow hash indirection table
  4008. * @netdev: network interface device structure
  4009. * @indir: indirection table
  4010. * @key: hash key
  4011. * @hfunc: hash function to use
  4012. *
  4013. * Returns -EINVAL if the table specifies an invalid queue id, otherwise
  4014. * returns 0 after programming the table.
  4015. **/
  4016. static int i40e_set_rxfh(struct net_device *netdev, const u32 *indir,
  4017. const u8 *key, const u8 hfunc)
  4018. {
  4019. struct i40e_netdev_priv *np = netdev_priv(netdev);
  4020. struct i40e_vsi *vsi = np->vsi;
  4021. struct i40e_pf *pf = vsi->back;
  4022. u8 *seed = NULL;
  4023. u16 i;
  4024. if (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP)
  4025. return -EOPNOTSUPP;
  4026. if (key) {
  4027. if (!vsi->rss_hkey_user) {
  4028. vsi->rss_hkey_user = kzalloc(I40E_HKEY_ARRAY_SIZE,
  4029. GFP_KERNEL);
  4030. if (!vsi->rss_hkey_user)
  4031. return -ENOMEM;
  4032. }
  4033. memcpy(vsi->rss_hkey_user, key, I40E_HKEY_ARRAY_SIZE);
  4034. seed = vsi->rss_hkey_user;
  4035. }
  4036. if (!vsi->rss_lut_user) {
  4037. vsi->rss_lut_user = kzalloc(I40E_HLUT_ARRAY_SIZE, GFP_KERNEL);
  4038. if (!vsi->rss_lut_user)
  4039. return -ENOMEM;
  4040. }
  4041. /* Each 32 bits pointed by 'indir' is stored with a lut entry */
  4042. if (indir)
  4043. for (i = 0; i < I40E_HLUT_ARRAY_SIZE; i++)
  4044. vsi->rss_lut_user[i] = (u8)(indir[i]);
  4045. else
  4046. i40e_fill_rss_lut(pf, vsi->rss_lut_user, I40E_HLUT_ARRAY_SIZE,
  4047. vsi->rss_size);
  4048. return i40e_config_rss(vsi, seed, vsi->rss_lut_user,
  4049. I40E_HLUT_ARRAY_SIZE);
  4050. }
  4051. /**
  4052. * i40e_get_priv_flags - report device private flags
  4053. * @dev: network interface device structure
  4054. *
  4055. * The get string set count and the string set should be matched for each
  4056. * flag returned. Add new strings for each flag to the i40e_gstrings_priv_flags
  4057. * array.
  4058. *
  4059. * Returns a u32 bitmap of flags.
  4060. **/
  4061. static u32 i40e_get_priv_flags(struct net_device *dev)
  4062. {
  4063. struct i40e_netdev_priv *np = netdev_priv(dev);
  4064. struct i40e_vsi *vsi = np->vsi;
  4065. struct i40e_pf *pf = vsi->back;
  4066. u32 i, j, ret_flags = 0;
  4067. for (i = 0; i < I40E_PRIV_FLAGS_STR_LEN; i++) {
  4068. const struct i40e_priv_flags *priv_flags;
  4069. priv_flags = &i40e_gstrings_priv_flags[i];
  4070. if (priv_flags->flag & pf->flags)
  4071. ret_flags |= BIT(i);
  4072. }
  4073. if (pf->hw.pf_id != 0)
  4074. return ret_flags;
  4075. for (j = 0; j < I40E_GL_PRIV_FLAGS_STR_LEN; j++) {
  4076. const struct i40e_priv_flags *priv_flags;
  4077. priv_flags = &i40e_gl_gstrings_priv_flags[j];
  4078. if (priv_flags->flag & pf->flags)
  4079. ret_flags |= BIT(i + j);
  4080. }
  4081. return ret_flags;
  4082. }
  4083. /**
  4084. * i40e_set_priv_flags - set private flags
  4085. * @dev: network interface device structure
  4086. * @flags: bit flags to be set
  4087. **/
  4088. static int i40e_set_priv_flags(struct net_device *dev, u32 flags)
  4089. {
  4090. struct i40e_netdev_priv *np = netdev_priv(dev);
  4091. struct i40e_vsi *vsi = np->vsi;
  4092. struct i40e_pf *pf = vsi->back;
  4093. u64 orig_flags, new_flags, changed_flags;
  4094. u32 i, j;
  4095. orig_flags = READ_ONCE(pf->flags);
  4096. new_flags = orig_flags;
  4097. for (i = 0; i < I40E_PRIV_FLAGS_STR_LEN; i++) {
  4098. const struct i40e_priv_flags *priv_flags;
  4099. priv_flags = &i40e_gstrings_priv_flags[i];
  4100. if (flags & BIT(i))
  4101. new_flags |= priv_flags->flag;
  4102. else
  4103. new_flags &= ~(priv_flags->flag);
  4104. /* If this is a read-only flag, it can't be changed */
  4105. if (priv_flags->read_only &&
  4106. ((orig_flags ^ new_flags) & ~BIT(i)))
  4107. return -EOPNOTSUPP;
  4108. }
  4109. if (pf->hw.pf_id != 0)
  4110. goto flags_complete;
  4111. for (j = 0; j < I40E_GL_PRIV_FLAGS_STR_LEN; j++) {
  4112. const struct i40e_priv_flags *priv_flags;
  4113. priv_flags = &i40e_gl_gstrings_priv_flags[j];
  4114. if (flags & BIT(i + j))
  4115. new_flags |= priv_flags->flag;
  4116. else
  4117. new_flags &= ~(priv_flags->flag);
  4118. /* If this is a read-only flag, it can't be changed */
  4119. if (priv_flags->read_only &&
  4120. ((orig_flags ^ new_flags) & ~BIT(i)))
  4121. return -EOPNOTSUPP;
  4122. }
  4123. flags_complete:
  4124. changed_flags = orig_flags ^ new_flags;
  4125. /* Before we finalize any flag changes, we need to perform some
  4126. * checks to ensure that the changes are supported and safe.
  4127. */
  4128. /* ATR eviction is not supported on all devices */
  4129. if ((new_flags & I40E_FLAG_HW_ATR_EVICT_ENABLED) &&
  4130. !(pf->hw_features & I40E_HW_ATR_EVICT_CAPABLE))
  4131. return -EOPNOTSUPP;
  4132. /* If the driver detected FW LLDP was disabled on init, this flag could
  4133. * be set, however we do not support _changing_ the flag if NPAR is
  4134. * enabled or FW API version < 1.7. There are situations where older
  4135. * FW versions/NPAR enabled PFs could disable LLDP, however we _must_
  4136. * not allow the user to enable/disable LLDP with this flag on
  4137. * unsupported FW versions.
  4138. */
  4139. if (changed_flags & I40E_FLAG_DISABLE_FW_LLDP) {
  4140. if (!(pf->hw_features & I40E_HW_STOPPABLE_FW_LLDP)) {
  4141. dev_warn(&pf->pdev->dev,
  4142. "Device does not support changing FW LLDP\n");
  4143. return -EOPNOTSUPP;
  4144. }
  4145. }
  4146. /* Now that we've checked to ensure that the new flags are valid, load
  4147. * them into place. Since we only modify flags either (a) during
  4148. * initialization or (b) while holding the RTNL lock, we don't need
  4149. * anything fancy here.
  4150. */
  4151. pf->flags = new_flags;
  4152. /* Process any additional changes needed as a result of flag changes.
  4153. * The changed_flags value reflects the list of bits that were
  4154. * changed in the code above.
  4155. */
  4156. /* Flush current ATR settings if ATR was disabled */
  4157. if ((changed_flags & I40E_FLAG_FD_ATR_ENABLED) &&
  4158. !(pf->flags & I40E_FLAG_FD_ATR_ENABLED)) {
  4159. set_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state);
  4160. set_bit(__I40E_FD_FLUSH_REQUESTED, pf->state);
  4161. }
  4162. if (changed_flags & I40E_FLAG_TRUE_PROMISC_SUPPORT) {
  4163. u16 sw_flags = 0, valid_flags = 0;
  4164. int ret;
  4165. if (!(pf->flags & I40E_FLAG_TRUE_PROMISC_SUPPORT))
  4166. sw_flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
  4167. valid_flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
  4168. ret = i40e_aq_set_switch_config(&pf->hw, sw_flags, valid_flags,
  4169. 0, NULL);
  4170. if (ret && pf->hw.aq.asq_last_status != I40E_AQ_RC_ESRCH) {
  4171. dev_info(&pf->pdev->dev,
  4172. "couldn't set switch config bits, err %s aq_err %s\n",
  4173. i40e_stat_str(&pf->hw, ret),
  4174. i40e_aq_str(&pf->hw,
  4175. pf->hw.aq.asq_last_status));
  4176. /* not a fatal problem, just keep going */
  4177. }
  4178. }
  4179. if ((changed_flags & pf->flags &
  4180. I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED) &&
  4181. (pf->flags & I40E_FLAG_MFP_ENABLED))
  4182. dev_warn(&pf->pdev->dev,
  4183. "Turning on link-down-on-close flag may affect other partitions\n");
  4184. if (changed_flags & I40E_FLAG_DISABLE_FW_LLDP) {
  4185. if (pf->flags & I40E_FLAG_DISABLE_FW_LLDP) {
  4186. struct i40e_dcbx_config *dcbcfg;
  4187. i40e_aq_stop_lldp(&pf->hw, true, NULL);
  4188. i40e_aq_set_dcb_parameters(&pf->hw, true, NULL);
  4189. /* reset local_dcbx_config to default */
  4190. dcbcfg = &pf->hw.local_dcbx_config;
  4191. dcbcfg->etscfg.willing = 1;
  4192. dcbcfg->etscfg.maxtcs = 0;
  4193. dcbcfg->etscfg.tcbwtable[0] = 100;
  4194. for (i = 1; i < I40E_MAX_TRAFFIC_CLASS; i++)
  4195. dcbcfg->etscfg.tcbwtable[i] = 0;
  4196. for (i = 0; i < I40E_MAX_USER_PRIORITY; i++)
  4197. dcbcfg->etscfg.prioritytable[i] = 0;
  4198. dcbcfg->etscfg.tsatable[0] = I40E_IEEE_TSA_ETS;
  4199. dcbcfg->pfc.willing = 1;
  4200. dcbcfg->pfc.pfccap = I40E_MAX_TRAFFIC_CLASS;
  4201. } else {
  4202. i40e_aq_start_lldp(&pf->hw, NULL);
  4203. }
  4204. }
  4205. /* Issue reset to cause things to take effect, as additional bits
  4206. * are added we will need to create a mask of bits requiring reset
  4207. */
  4208. if (changed_flags & (I40E_FLAG_VEB_STATS_ENABLED |
  4209. I40E_FLAG_LEGACY_RX |
  4210. I40E_FLAG_SOURCE_PRUNING_DISABLED |
  4211. I40E_FLAG_DISABLE_FW_LLDP))
  4212. i40e_do_reset(pf, BIT(__I40E_PF_RESET_REQUESTED), true);
  4213. return 0;
  4214. }
  4215. /**
  4216. * i40e_get_module_info - get (Q)SFP+ module type info
  4217. * @netdev: network interface device structure
  4218. * @modinfo: module EEPROM size and layout information structure
  4219. **/
  4220. static int i40e_get_module_info(struct net_device *netdev,
  4221. struct ethtool_modinfo *modinfo)
  4222. {
  4223. struct i40e_netdev_priv *np = netdev_priv(netdev);
  4224. struct i40e_vsi *vsi = np->vsi;
  4225. struct i40e_pf *pf = vsi->back;
  4226. struct i40e_hw *hw = &pf->hw;
  4227. u32 sff8472_comp = 0;
  4228. u32 sff8472_swap = 0;
  4229. u32 sff8636_rev = 0;
  4230. i40e_status status;
  4231. u32 type = 0;
  4232. /* Check if firmware supports reading module EEPROM. */
  4233. if (!(hw->flags & I40E_HW_FLAG_AQ_PHY_ACCESS_CAPABLE)) {
  4234. netdev_err(vsi->netdev, "Module EEPROM memory read not supported. Please update the NVM image.\n");
  4235. return -EINVAL;
  4236. }
  4237. status = i40e_update_link_info(hw);
  4238. if (status)
  4239. return -EIO;
  4240. if (hw->phy.link_info.phy_type == I40E_PHY_TYPE_EMPTY) {
  4241. netdev_err(vsi->netdev, "Cannot read module EEPROM memory. No module connected.\n");
  4242. return -EINVAL;
  4243. }
  4244. type = hw->phy.link_info.module_type[0];
  4245. switch (type) {
  4246. case I40E_MODULE_TYPE_SFP:
  4247. status = i40e_aq_get_phy_register(hw,
  4248. I40E_AQ_PHY_REG_ACCESS_EXTERNAL_MODULE,
  4249. I40E_I2C_EEPROM_DEV_ADDR,
  4250. I40E_MODULE_SFF_8472_COMP,
  4251. &sff8472_comp, NULL);
  4252. if (status)
  4253. return -EIO;
  4254. status = i40e_aq_get_phy_register(hw,
  4255. I40E_AQ_PHY_REG_ACCESS_EXTERNAL_MODULE,
  4256. I40E_I2C_EEPROM_DEV_ADDR,
  4257. I40E_MODULE_SFF_8472_SWAP,
  4258. &sff8472_swap, NULL);
  4259. if (status)
  4260. return -EIO;
  4261. /* Check if the module requires address swap to access
  4262. * the other EEPROM memory page.
  4263. */
  4264. if (sff8472_swap & I40E_MODULE_SFF_ADDR_MODE) {
  4265. netdev_warn(vsi->netdev, "Module address swap to access page 0xA2 is not supported.\n");
  4266. modinfo->type = ETH_MODULE_SFF_8079;
  4267. modinfo->eeprom_len = ETH_MODULE_SFF_8079_LEN;
  4268. } else if (sff8472_comp == 0x00) {
  4269. /* Module is not SFF-8472 compliant */
  4270. modinfo->type = ETH_MODULE_SFF_8079;
  4271. modinfo->eeprom_len = ETH_MODULE_SFF_8079_LEN;
  4272. } else {
  4273. modinfo->type = ETH_MODULE_SFF_8472;
  4274. modinfo->eeprom_len = ETH_MODULE_SFF_8472_LEN;
  4275. }
  4276. break;
  4277. case I40E_MODULE_TYPE_QSFP_PLUS:
  4278. /* Read from memory page 0. */
  4279. status = i40e_aq_get_phy_register(hw,
  4280. I40E_AQ_PHY_REG_ACCESS_EXTERNAL_MODULE,
  4281. 0,
  4282. I40E_MODULE_REVISION_ADDR,
  4283. &sff8636_rev, NULL);
  4284. if (status)
  4285. return -EIO;
  4286. /* Determine revision compliance byte */
  4287. if (sff8636_rev > 0x02) {
  4288. /* Module is SFF-8636 compliant */
  4289. modinfo->type = ETH_MODULE_SFF_8636;
  4290. modinfo->eeprom_len = I40E_MODULE_QSFP_MAX_LEN;
  4291. } else {
  4292. modinfo->type = ETH_MODULE_SFF_8436;
  4293. modinfo->eeprom_len = I40E_MODULE_QSFP_MAX_LEN;
  4294. }
  4295. break;
  4296. case I40E_MODULE_TYPE_QSFP28:
  4297. modinfo->type = ETH_MODULE_SFF_8636;
  4298. modinfo->eeprom_len = I40E_MODULE_QSFP_MAX_LEN;
  4299. break;
  4300. default:
  4301. netdev_err(vsi->netdev, "Module type unrecognized\n");
  4302. return -EINVAL;
  4303. }
  4304. return 0;
  4305. }
  4306. /**
  4307. * i40e_get_module_eeprom - fills buffer with (Q)SFP+ module memory contents
  4308. * @netdev: network interface device structure
  4309. * @ee: EEPROM dump request structure
  4310. * @data: buffer to be filled with EEPROM contents
  4311. **/
  4312. static int i40e_get_module_eeprom(struct net_device *netdev,
  4313. struct ethtool_eeprom *ee,
  4314. u8 *data)
  4315. {
  4316. struct i40e_netdev_priv *np = netdev_priv(netdev);
  4317. struct i40e_vsi *vsi = np->vsi;
  4318. struct i40e_pf *pf = vsi->back;
  4319. struct i40e_hw *hw = &pf->hw;
  4320. bool is_sfp = false;
  4321. i40e_status status;
  4322. u32 value = 0;
  4323. int i;
  4324. if (!ee || !ee->len || !data)
  4325. return -EINVAL;
  4326. if (hw->phy.link_info.module_type[0] == I40E_MODULE_TYPE_SFP)
  4327. is_sfp = true;
  4328. for (i = 0; i < ee->len; i++) {
  4329. u32 offset = i + ee->offset;
  4330. u32 addr = is_sfp ? I40E_I2C_EEPROM_DEV_ADDR : 0;
  4331. /* Check if we need to access the other memory page */
  4332. if (is_sfp) {
  4333. if (offset >= ETH_MODULE_SFF_8079_LEN) {
  4334. offset -= ETH_MODULE_SFF_8079_LEN;
  4335. addr = I40E_I2C_EEPROM_DEV_ADDR2;
  4336. }
  4337. } else {
  4338. while (offset >= ETH_MODULE_SFF_8436_LEN) {
  4339. /* Compute memory page number and offset. */
  4340. offset -= ETH_MODULE_SFF_8436_LEN / 2;
  4341. addr++;
  4342. }
  4343. }
  4344. status = i40e_aq_get_phy_register(hw,
  4345. I40E_AQ_PHY_REG_ACCESS_EXTERNAL_MODULE,
  4346. addr, offset, &value, NULL);
  4347. if (status)
  4348. return -EIO;
  4349. data[i] = value;
  4350. }
  4351. return 0;
  4352. }
  4353. static const struct ethtool_ops i40e_ethtool_ops = {
  4354. .get_drvinfo = i40e_get_drvinfo,
  4355. .get_regs_len = i40e_get_regs_len,
  4356. .get_regs = i40e_get_regs,
  4357. .nway_reset = i40e_nway_reset,
  4358. .get_link = ethtool_op_get_link,
  4359. .get_wol = i40e_get_wol,
  4360. .set_wol = i40e_set_wol,
  4361. .set_eeprom = i40e_set_eeprom,
  4362. .get_eeprom_len = i40e_get_eeprom_len,
  4363. .get_eeprom = i40e_get_eeprom,
  4364. .get_ringparam = i40e_get_ringparam,
  4365. .set_ringparam = i40e_set_ringparam,
  4366. .get_pauseparam = i40e_get_pauseparam,
  4367. .set_pauseparam = i40e_set_pauseparam,
  4368. .get_msglevel = i40e_get_msglevel,
  4369. .set_msglevel = i40e_set_msglevel,
  4370. .get_rxnfc = i40e_get_rxnfc,
  4371. .set_rxnfc = i40e_set_rxnfc,
  4372. .self_test = i40e_diag_test,
  4373. .get_strings = i40e_get_strings,
  4374. .set_phys_id = i40e_set_phys_id,
  4375. .get_sset_count = i40e_get_sset_count,
  4376. .get_ethtool_stats = i40e_get_ethtool_stats,
  4377. .get_coalesce = i40e_get_coalesce,
  4378. .set_coalesce = i40e_set_coalesce,
  4379. .get_rxfh_key_size = i40e_get_rxfh_key_size,
  4380. .get_rxfh_indir_size = i40e_get_rxfh_indir_size,
  4381. .get_rxfh = i40e_get_rxfh,
  4382. .set_rxfh = i40e_set_rxfh,
  4383. .get_channels = i40e_get_channels,
  4384. .set_channels = i40e_set_channels,
  4385. .get_module_info = i40e_get_module_info,
  4386. .get_module_eeprom = i40e_get_module_eeprom,
  4387. .get_ts_info = i40e_get_ts_info,
  4388. .get_priv_flags = i40e_get_priv_flags,
  4389. .set_priv_flags = i40e_set_priv_flags,
  4390. .get_per_queue_coalesce = i40e_get_per_queue_coalesce,
  4391. .set_per_queue_coalesce = i40e_set_per_queue_coalesce,
  4392. .get_link_ksettings = i40e_get_link_ksettings,
  4393. .set_link_ksettings = i40e_set_link_ksettings,
  4394. };
  4395. void i40e_set_ethtool_ops(struct net_device *netdev)
  4396. {
  4397. netdev->ethtool_ops = &i40e_ethtool_ops;
  4398. }