ef10.c 140 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859
  1. /****************************************************************************
  2. * Driver for Solarflare network controllers and boards
  3. * Copyright 2012-2013 Solarflare Communications Inc.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License version 2 as published
  7. * by the Free Software Foundation, incorporated herein by reference.
  8. */
  9. #include "net_driver.h"
  10. #include "ef10_regs.h"
  11. #include "io.h"
  12. #include "mcdi.h"
  13. #include "mcdi_pcol.h"
  14. #include "nic.h"
  15. #include "workarounds.h"
  16. #include "selftest.h"
  17. #include "ef10_sriov.h"
  18. #include <linux/in.h>
  19. #include <linux/jhash.h>
  20. #include <linux/wait.h>
  21. #include <linux/workqueue.h>
  22. /* Hardware control for EF10 architecture including 'Huntington'. */
  23. #define EFX_EF10_DRVGEN_EV 7
  24. enum {
  25. EFX_EF10_TEST = 1,
  26. EFX_EF10_REFILL,
  27. };
  28. /* The reserved RSS context value */
  29. #define EFX_EF10_RSS_CONTEXT_INVALID 0xffffffff
  30. /* The maximum size of a shared RSS context */
  31. /* TODO: this should really be from the mcdi protocol export */
  32. #define EFX_EF10_MAX_SHARED_RSS_CONTEXT_SIZE 64UL
  33. /* The filter table(s) are managed by firmware and we have write-only
  34. * access. When removing filters we must identify them to the
  35. * firmware by a 64-bit handle, but this is too wide for Linux kernel
  36. * interfaces (32-bit for RX NFC, 16-bit for RFS). Also, we need to
  37. * be able to tell in advance whether a requested insertion will
  38. * replace an existing filter. Therefore we maintain a software hash
  39. * table, which should be at least as large as the hardware hash
  40. * table.
  41. *
  42. * Huntington has a single 8K filter table shared between all filter
  43. * types and both ports.
  44. */
  45. #define HUNT_FILTER_TBL_ROWS 8192
  46. #define EFX_EF10_FILTER_ID_INVALID 0xffff
  47. struct efx_ef10_dev_addr {
  48. u8 addr[ETH_ALEN];
  49. u16 id;
  50. };
  51. struct efx_ef10_filter_table {
  52. /* The RX match field masks supported by this fw & hw, in order of priority */
  53. enum efx_filter_match_flags rx_match_flags[
  54. MC_CMD_GET_PARSER_DISP_INFO_OUT_SUPPORTED_MATCHES_MAXNUM];
  55. unsigned int rx_match_count;
  56. struct {
  57. unsigned long spec; /* pointer to spec plus flag bits */
  58. /* BUSY flag indicates that an update is in progress. AUTO_OLD is
  59. * used to mark and sweep MAC filters for the device address lists.
  60. */
  61. #define EFX_EF10_FILTER_FLAG_BUSY 1UL
  62. #define EFX_EF10_FILTER_FLAG_AUTO_OLD 2UL
  63. #define EFX_EF10_FILTER_FLAGS 3UL
  64. u64 handle; /* firmware handle */
  65. } *entry;
  66. wait_queue_head_t waitq;
  67. /* Shadow of net_device address lists, guarded by mac_lock */
  68. #define EFX_EF10_FILTER_DEV_UC_MAX 32
  69. #define EFX_EF10_FILTER_DEV_MC_MAX 256
  70. struct efx_ef10_dev_addr dev_uc_list[EFX_EF10_FILTER_DEV_UC_MAX];
  71. struct efx_ef10_dev_addr dev_mc_list[EFX_EF10_FILTER_DEV_MC_MAX];
  72. int dev_uc_count;
  73. int dev_mc_count;
  74. /* Indices (like efx_ef10_dev_addr.id) for promisc/allmulti filters */
  75. u16 ucdef_id;
  76. u16 bcast_id;
  77. u16 mcdef_id;
  78. };
  79. /* An arbitrary search limit for the software hash table */
  80. #define EFX_EF10_FILTER_SEARCH_LIMIT 200
  81. static void efx_ef10_rx_free_indir_table(struct efx_nic *efx);
  82. static void efx_ef10_filter_table_remove(struct efx_nic *efx);
  83. static int efx_ef10_get_warm_boot_count(struct efx_nic *efx)
  84. {
  85. efx_dword_t reg;
  86. efx_readd(efx, &reg, ER_DZ_BIU_MC_SFT_STATUS);
  87. return EFX_DWORD_FIELD(reg, EFX_WORD_1) == 0xb007 ?
  88. EFX_DWORD_FIELD(reg, EFX_WORD_0) : -EIO;
  89. }
  90. static unsigned int efx_ef10_mem_map_size(struct efx_nic *efx)
  91. {
  92. int bar;
  93. bar = efx->type->mem_bar;
  94. return resource_size(&efx->pci_dev->resource[bar]);
  95. }
  96. static bool efx_ef10_is_vf(struct efx_nic *efx)
  97. {
  98. return efx->type->is_vf;
  99. }
  100. static int efx_ef10_get_pf_index(struct efx_nic *efx)
  101. {
  102. MCDI_DECLARE_BUF(outbuf, MC_CMD_GET_FUNCTION_INFO_OUT_LEN);
  103. struct efx_ef10_nic_data *nic_data = efx->nic_data;
  104. size_t outlen;
  105. int rc;
  106. rc = efx_mcdi_rpc(efx, MC_CMD_GET_FUNCTION_INFO, NULL, 0, outbuf,
  107. sizeof(outbuf), &outlen);
  108. if (rc)
  109. return rc;
  110. if (outlen < sizeof(outbuf))
  111. return -EIO;
  112. nic_data->pf_index = MCDI_DWORD(outbuf, GET_FUNCTION_INFO_OUT_PF);
  113. return 0;
  114. }
  115. #ifdef CONFIG_SFC_SRIOV
  116. static int efx_ef10_get_vf_index(struct efx_nic *efx)
  117. {
  118. MCDI_DECLARE_BUF(outbuf, MC_CMD_GET_FUNCTION_INFO_OUT_LEN);
  119. struct efx_ef10_nic_data *nic_data = efx->nic_data;
  120. size_t outlen;
  121. int rc;
  122. rc = efx_mcdi_rpc(efx, MC_CMD_GET_FUNCTION_INFO, NULL, 0, outbuf,
  123. sizeof(outbuf), &outlen);
  124. if (rc)
  125. return rc;
  126. if (outlen < sizeof(outbuf))
  127. return -EIO;
  128. nic_data->vf_index = MCDI_DWORD(outbuf, GET_FUNCTION_INFO_OUT_VF);
  129. return 0;
  130. }
  131. #endif
  132. static int efx_ef10_init_datapath_caps(struct efx_nic *efx)
  133. {
  134. MCDI_DECLARE_BUF(outbuf, MC_CMD_GET_CAPABILITIES_OUT_LEN);
  135. struct efx_ef10_nic_data *nic_data = efx->nic_data;
  136. size_t outlen;
  137. int rc;
  138. BUILD_BUG_ON(MC_CMD_GET_CAPABILITIES_IN_LEN != 0);
  139. rc = efx_mcdi_rpc(efx, MC_CMD_GET_CAPABILITIES, NULL, 0,
  140. outbuf, sizeof(outbuf), &outlen);
  141. if (rc)
  142. return rc;
  143. if (outlen < sizeof(outbuf)) {
  144. netif_err(efx, drv, efx->net_dev,
  145. "unable to read datapath firmware capabilities\n");
  146. return -EIO;
  147. }
  148. nic_data->datapath_caps =
  149. MCDI_DWORD(outbuf, GET_CAPABILITIES_OUT_FLAGS1);
  150. /* record the DPCPU firmware IDs to determine VEB vswitching support.
  151. */
  152. nic_data->rx_dpcpu_fw_id =
  153. MCDI_WORD(outbuf, GET_CAPABILITIES_OUT_RX_DPCPU_FW_ID);
  154. nic_data->tx_dpcpu_fw_id =
  155. MCDI_WORD(outbuf, GET_CAPABILITIES_OUT_TX_DPCPU_FW_ID);
  156. if (!(nic_data->datapath_caps &
  157. (1 << MC_CMD_GET_CAPABILITIES_OUT_TX_TSO_LBN))) {
  158. netif_err(efx, drv, efx->net_dev,
  159. "current firmware does not support TSO\n");
  160. return -ENODEV;
  161. }
  162. if (!(nic_data->datapath_caps &
  163. (1 << MC_CMD_GET_CAPABILITIES_OUT_RX_PREFIX_LEN_14_LBN))) {
  164. netif_err(efx, probe, efx->net_dev,
  165. "current firmware does not support an RX prefix\n");
  166. return -ENODEV;
  167. }
  168. return 0;
  169. }
  170. static int efx_ef10_get_sysclk_freq(struct efx_nic *efx)
  171. {
  172. MCDI_DECLARE_BUF(outbuf, MC_CMD_GET_CLOCK_OUT_LEN);
  173. int rc;
  174. rc = efx_mcdi_rpc(efx, MC_CMD_GET_CLOCK, NULL, 0,
  175. outbuf, sizeof(outbuf), NULL);
  176. if (rc)
  177. return rc;
  178. rc = MCDI_DWORD(outbuf, GET_CLOCK_OUT_SYS_FREQ);
  179. return rc > 0 ? rc : -ERANGE;
  180. }
  181. static int efx_ef10_get_mac_address_pf(struct efx_nic *efx, u8 *mac_address)
  182. {
  183. MCDI_DECLARE_BUF(outbuf, MC_CMD_GET_MAC_ADDRESSES_OUT_LEN);
  184. size_t outlen;
  185. int rc;
  186. BUILD_BUG_ON(MC_CMD_GET_MAC_ADDRESSES_IN_LEN != 0);
  187. rc = efx_mcdi_rpc(efx, MC_CMD_GET_MAC_ADDRESSES, NULL, 0,
  188. outbuf, sizeof(outbuf), &outlen);
  189. if (rc)
  190. return rc;
  191. if (outlen < MC_CMD_GET_MAC_ADDRESSES_OUT_LEN)
  192. return -EIO;
  193. ether_addr_copy(mac_address,
  194. MCDI_PTR(outbuf, GET_MAC_ADDRESSES_OUT_MAC_ADDR_BASE));
  195. return 0;
  196. }
  197. static int efx_ef10_get_mac_address_vf(struct efx_nic *efx, u8 *mac_address)
  198. {
  199. MCDI_DECLARE_BUF(inbuf, MC_CMD_VPORT_GET_MAC_ADDRESSES_IN_LEN);
  200. MCDI_DECLARE_BUF(outbuf, MC_CMD_VPORT_GET_MAC_ADDRESSES_OUT_LENMAX);
  201. size_t outlen;
  202. int num_addrs, rc;
  203. MCDI_SET_DWORD(inbuf, VPORT_GET_MAC_ADDRESSES_IN_VPORT_ID,
  204. EVB_PORT_ID_ASSIGNED);
  205. rc = efx_mcdi_rpc(efx, MC_CMD_VPORT_GET_MAC_ADDRESSES, inbuf,
  206. sizeof(inbuf), outbuf, sizeof(outbuf), &outlen);
  207. if (rc)
  208. return rc;
  209. if (outlen < MC_CMD_VPORT_GET_MAC_ADDRESSES_OUT_LENMIN)
  210. return -EIO;
  211. num_addrs = MCDI_DWORD(outbuf,
  212. VPORT_GET_MAC_ADDRESSES_OUT_MACADDR_COUNT);
  213. WARN_ON(num_addrs != 1);
  214. ether_addr_copy(mac_address,
  215. MCDI_PTR(outbuf, VPORT_GET_MAC_ADDRESSES_OUT_MACADDR));
  216. return 0;
  217. }
  218. static ssize_t efx_ef10_show_link_control_flag(struct device *dev,
  219. struct device_attribute *attr,
  220. char *buf)
  221. {
  222. struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
  223. return sprintf(buf, "%d\n",
  224. ((efx->mcdi->fn_flags) &
  225. (1 << MC_CMD_DRV_ATTACH_EXT_OUT_FLAG_LINKCTRL))
  226. ? 1 : 0);
  227. }
  228. static ssize_t efx_ef10_show_primary_flag(struct device *dev,
  229. struct device_attribute *attr,
  230. char *buf)
  231. {
  232. struct efx_nic *efx = pci_get_drvdata(to_pci_dev(dev));
  233. return sprintf(buf, "%d\n",
  234. ((efx->mcdi->fn_flags) &
  235. (1 << MC_CMD_DRV_ATTACH_EXT_OUT_FLAG_PRIMARY))
  236. ? 1 : 0);
  237. }
  238. static DEVICE_ATTR(link_control_flag, 0444, efx_ef10_show_link_control_flag,
  239. NULL);
  240. static DEVICE_ATTR(primary_flag, 0444, efx_ef10_show_primary_flag, NULL);
  241. static int efx_ef10_probe(struct efx_nic *efx)
  242. {
  243. struct efx_ef10_nic_data *nic_data;
  244. struct net_device *net_dev = efx->net_dev;
  245. int i, rc;
  246. /* We can have one VI for each 8K region. However, until we
  247. * use TX option descriptors we need two TX queues per channel.
  248. */
  249. efx->max_channels = min_t(unsigned int,
  250. EFX_MAX_CHANNELS,
  251. efx_ef10_mem_map_size(efx) /
  252. (EFX_VI_PAGE_SIZE * EFX_TXQ_TYPES));
  253. efx->max_tx_channels = efx->max_channels;
  254. if (WARN_ON(efx->max_channels == 0))
  255. return -EIO;
  256. nic_data = kzalloc(sizeof(*nic_data), GFP_KERNEL);
  257. if (!nic_data)
  258. return -ENOMEM;
  259. efx->nic_data = nic_data;
  260. /* we assume later that we can copy from this buffer in dwords */
  261. BUILD_BUG_ON(MCDI_CTL_SDU_LEN_MAX_V2 % 4);
  262. rc = efx_nic_alloc_buffer(efx, &nic_data->mcdi_buf,
  263. 8 + MCDI_CTL_SDU_LEN_MAX_V2, GFP_KERNEL);
  264. if (rc)
  265. goto fail1;
  266. /* Get the MC's warm boot count. In case it's rebooting right
  267. * now, be prepared to retry.
  268. */
  269. i = 0;
  270. for (;;) {
  271. rc = efx_ef10_get_warm_boot_count(efx);
  272. if (rc >= 0)
  273. break;
  274. if (++i == 5)
  275. goto fail2;
  276. ssleep(1);
  277. }
  278. nic_data->warm_boot_count = rc;
  279. nic_data->rx_rss_context = EFX_EF10_RSS_CONTEXT_INVALID;
  280. nic_data->vport_id = EVB_PORT_ID_ASSIGNED;
  281. /* In case we're recovering from a crash (kexec), we want to
  282. * cancel any outstanding request by the previous user of this
  283. * function. We send a special message using the least
  284. * significant bits of the 'high' (doorbell) register.
  285. */
  286. _efx_writed(efx, cpu_to_le32(1), ER_DZ_MC_DB_HWRD);
  287. rc = efx_mcdi_init(efx);
  288. if (rc)
  289. goto fail2;
  290. /* Reset (most) configuration for this function */
  291. rc = efx_mcdi_reset(efx, RESET_TYPE_ALL);
  292. if (rc)
  293. goto fail3;
  294. /* Enable event logging */
  295. rc = efx_mcdi_log_ctrl(efx, true, false, 0);
  296. if (rc)
  297. goto fail3;
  298. rc = device_create_file(&efx->pci_dev->dev,
  299. &dev_attr_link_control_flag);
  300. if (rc)
  301. goto fail3;
  302. rc = device_create_file(&efx->pci_dev->dev, &dev_attr_primary_flag);
  303. if (rc)
  304. goto fail4;
  305. rc = efx_ef10_get_pf_index(efx);
  306. if (rc)
  307. goto fail5;
  308. rc = efx_ef10_init_datapath_caps(efx);
  309. if (rc < 0)
  310. goto fail5;
  311. efx->rx_packet_len_offset =
  312. ES_DZ_RX_PREFIX_PKTLEN_OFST - ES_DZ_RX_PREFIX_SIZE;
  313. rc = efx_mcdi_port_get_number(efx);
  314. if (rc < 0)
  315. goto fail5;
  316. efx->port_num = rc;
  317. net_dev->dev_port = rc;
  318. rc = efx->type->get_mac_address(efx, efx->net_dev->perm_addr);
  319. if (rc)
  320. goto fail5;
  321. rc = efx_ef10_get_sysclk_freq(efx);
  322. if (rc < 0)
  323. goto fail5;
  324. efx->timer_quantum_ns = 1536000 / rc; /* 1536 cycles */
  325. /* Check whether firmware supports bug 35388 workaround.
  326. * First try to enable it, then if we get EPERM, just
  327. * ask if it's already enabled
  328. */
  329. rc = efx_mcdi_set_workaround(efx, MC_CMD_WORKAROUND_BUG35388, true, NULL);
  330. if (rc == 0) {
  331. nic_data->workaround_35388 = true;
  332. } else if (rc == -EPERM) {
  333. unsigned int enabled;
  334. rc = efx_mcdi_get_workarounds(efx, NULL, &enabled);
  335. if (rc)
  336. goto fail3;
  337. nic_data->workaround_35388 = enabled &
  338. MC_CMD_GET_WORKAROUNDS_OUT_BUG35388;
  339. } else if (rc != -ENOSYS && rc != -ENOENT) {
  340. goto fail5;
  341. }
  342. netif_dbg(efx, probe, efx->net_dev,
  343. "workaround for bug 35388 is %sabled\n",
  344. nic_data->workaround_35388 ? "en" : "dis");
  345. rc = efx_mcdi_mon_probe(efx);
  346. if (rc && rc != -EPERM)
  347. goto fail5;
  348. efx_ptp_probe(efx, NULL);
  349. #ifdef CONFIG_SFC_SRIOV
  350. if ((efx->pci_dev->physfn) && (!efx->pci_dev->is_physfn)) {
  351. struct pci_dev *pci_dev_pf = efx->pci_dev->physfn;
  352. struct efx_nic *efx_pf = pci_get_drvdata(pci_dev_pf);
  353. efx_pf->type->get_mac_address(efx_pf, nic_data->port_id);
  354. } else
  355. #endif
  356. ether_addr_copy(nic_data->port_id, efx->net_dev->perm_addr);
  357. return 0;
  358. fail5:
  359. device_remove_file(&efx->pci_dev->dev, &dev_attr_primary_flag);
  360. fail4:
  361. device_remove_file(&efx->pci_dev->dev, &dev_attr_link_control_flag);
  362. fail3:
  363. efx_mcdi_fini(efx);
  364. fail2:
  365. efx_nic_free_buffer(efx, &nic_data->mcdi_buf);
  366. fail1:
  367. kfree(nic_data);
  368. efx->nic_data = NULL;
  369. return rc;
  370. }
  371. static int efx_ef10_free_vis(struct efx_nic *efx)
  372. {
  373. MCDI_DECLARE_BUF_ERR(outbuf);
  374. size_t outlen;
  375. int rc = efx_mcdi_rpc_quiet(efx, MC_CMD_FREE_VIS, NULL, 0,
  376. outbuf, sizeof(outbuf), &outlen);
  377. /* -EALREADY means nothing to free, so ignore */
  378. if (rc == -EALREADY)
  379. rc = 0;
  380. if (rc)
  381. efx_mcdi_display_error(efx, MC_CMD_FREE_VIS, 0, outbuf, outlen,
  382. rc);
  383. return rc;
  384. }
  385. #ifdef EFX_USE_PIO
  386. static void efx_ef10_free_piobufs(struct efx_nic *efx)
  387. {
  388. struct efx_ef10_nic_data *nic_data = efx->nic_data;
  389. MCDI_DECLARE_BUF(inbuf, MC_CMD_FREE_PIOBUF_IN_LEN);
  390. unsigned int i;
  391. int rc;
  392. BUILD_BUG_ON(MC_CMD_FREE_PIOBUF_OUT_LEN != 0);
  393. for (i = 0; i < nic_data->n_piobufs; i++) {
  394. MCDI_SET_DWORD(inbuf, FREE_PIOBUF_IN_PIOBUF_HANDLE,
  395. nic_data->piobuf_handle[i]);
  396. rc = efx_mcdi_rpc(efx, MC_CMD_FREE_PIOBUF, inbuf, sizeof(inbuf),
  397. NULL, 0, NULL);
  398. WARN_ON(rc);
  399. }
  400. nic_data->n_piobufs = 0;
  401. }
  402. static int efx_ef10_alloc_piobufs(struct efx_nic *efx, unsigned int n)
  403. {
  404. struct efx_ef10_nic_data *nic_data = efx->nic_data;
  405. MCDI_DECLARE_BUF(outbuf, MC_CMD_ALLOC_PIOBUF_OUT_LEN);
  406. unsigned int i;
  407. size_t outlen;
  408. int rc = 0;
  409. BUILD_BUG_ON(MC_CMD_ALLOC_PIOBUF_IN_LEN != 0);
  410. for (i = 0; i < n; i++) {
  411. rc = efx_mcdi_rpc(efx, MC_CMD_ALLOC_PIOBUF, NULL, 0,
  412. outbuf, sizeof(outbuf), &outlen);
  413. if (rc)
  414. break;
  415. if (outlen < MC_CMD_ALLOC_PIOBUF_OUT_LEN) {
  416. rc = -EIO;
  417. break;
  418. }
  419. nic_data->piobuf_handle[i] =
  420. MCDI_DWORD(outbuf, ALLOC_PIOBUF_OUT_PIOBUF_HANDLE);
  421. netif_dbg(efx, probe, efx->net_dev,
  422. "allocated PIO buffer %u handle %x\n", i,
  423. nic_data->piobuf_handle[i]);
  424. }
  425. nic_data->n_piobufs = i;
  426. if (rc)
  427. efx_ef10_free_piobufs(efx);
  428. return rc;
  429. }
  430. static int efx_ef10_link_piobufs(struct efx_nic *efx)
  431. {
  432. struct efx_ef10_nic_data *nic_data = efx->nic_data;
  433. _MCDI_DECLARE_BUF(inbuf,
  434. max(MC_CMD_LINK_PIOBUF_IN_LEN,
  435. MC_CMD_UNLINK_PIOBUF_IN_LEN));
  436. struct efx_channel *channel;
  437. struct efx_tx_queue *tx_queue;
  438. unsigned int offset, index;
  439. int rc;
  440. BUILD_BUG_ON(MC_CMD_LINK_PIOBUF_OUT_LEN != 0);
  441. BUILD_BUG_ON(MC_CMD_UNLINK_PIOBUF_OUT_LEN != 0);
  442. memset(inbuf, 0, sizeof(inbuf));
  443. /* Link a buffer to each VI in the write-combining mapping */
  444. for (index = 0; index < nic_data->n_piobufs; ++index) {
  445. MCDI_SET_DWORD(inbuf, LINK_PIOBUF_IN_PIOBUF_HANDLE,
  446. nic_data->piobuf_handle[index]);
  447. MCDI_SET_DWORD(inbuf, LINK_PIOBUF_IN_TXQ_INSTANCE,
  448. nic_data->pio_write_vi_base + index);
  449. rc = efx_mcdi_rpc(efx, MC_CMD_LINK_PIOBUF,
  450. inbuf, MC_CMD_LINK_PIOBUF_IN_LEN,
  451. NULL, 0, NULL);
  452. if (rc) {
  453. netif_err(efx, drv, efx->net_dev,
  454. "failed to link VI %u to PIO buffer %u (%d)\n",
  455. nic_data->pio_write_vi_base + index, index,
  456. rc);
  457. goto fail;
  458. }
  459. netif_dbg(efx, probe, efx->net_dev,
  460. "linked VI %u to PIO buffer %u\n",
  461. nic_data->pio_write_vi_base + index, index);
  462. }
  463. /* Link a buffer to each TX queue */
  464. efx_for_each_channel(channel, efx) {
  465. efx_for_each_channel_tx_queue(tx_queue, channel) {
  466. /* We assign the PIO buffers to queues in
  467. * reverse order to allow for the following
  468. * special case.
  469. */
  470. offset = ((efx->tx_channel_offset + efx->n_tx_channels -
  471. tx_queue->channel->channel - 1) *
  472. efx_piobuf_size);
  473. index = offset / ER_DZ_TX_PIOBUF_SIZE;
  474. offset = offset % ER_DZ_TX_PIOBUF_SIZE;
  475. /* When the host page size is 4K, the first
  476. * host page in the WC mapping may be within
  477. * the same VI page as the last TX queue. We
  478. * can only link one buffer to each VI.
  479. */
  480. if (tx_queue->queue == nic_data->pio_write_vi_base) {
  481. BUG_ON(index != 0);
  482. rc = 0;
  483. } else {
  484. MCDI_SET_DWORD(inbuf,
  485. LINK_PIOBUF_IN_PIOBUF_HANDLE,
  486. nic_data->piobuf_handle[index]);
  487. MCDI_SET_DWORD(inbuf,
  488. LINK_PIOBUF_IN_TXQ_INSTANCE,
  489. tx_queue->queue);
  490. rc = efx_mcdi_rpc(efx, MC_CMD_LINK_PIOBUF,
  491. inbuf, MC_CMD_LINK_PIOBUF_IN_LEN,
  492. NULL, 0, NULL);
  493. }
  494. if (rc) {
  495. /* This is non-fatal; the TX path just
  496. * won't use PIO for this queue
  497. */
  498. netif_err(efx, drv, efx->net_dev,
  499. "failed to link VI %u to PIO buffer %u (%d)\n",
  500. tx_queue->queue, index, rc);
  501. tx_queue->piobuf = NULL;
  502. } else {
  503. tx_queue->piobuf =
  504. nic_data->pio_write_base +
  505. index * EFX_VI_PAGE_SIZE + offset;
  506. tx_queue->piobuf_offset = offset;
  507. netif_dbg(efx, probe, efx->net_dev,
  508. "linked VI %u to PIO buffer %u offset %x addr %p\n",
  509. tx_queue->queue, index,
  510. tx_queue->piobuf_offset,
  511. tx_queue->piobuf);
  512. }
  513. }
  514. }
  515. return 0;
  516. fail:
  517. while (index--) {
  518. MCDI_SET_DWORD(inbuf, UNLINK_PIOBUF_IN_TXQ_INSTANCE,
  519. nic_data->pio_write_vi_base + index);
  520. efx_mcdi_rpc(efx, MC_CMD_UNLINK_PIOBUF,
  521. inbuf, MC_CMD_UNLINK_PIOBUF_IN_LEN,
  522. NULL, 0, NULL);
  523. }
  524. return rc;
  525. }
  526. #else /* !EFX_USE_PIO */
  527. static int efx_ef10_alloc_piobufs(struct efx_nic *efx, unsigned int n)
  528. {
  529. return n == 0 ? 0 : -ENOBUFS;
  530. }
  531. static int efx_ef10_link_piobufs(struct efx_nic *efx)
  532. {
  533. return 0;
  534. }
  535. static void efx_ef10_free_piobufs(struct efx_nic *efx)
  536. {
  537. }
  538. #endif /* EFX_USE_PIO */
  539. static void efx_ef10_remove(struct efx_nic *efx)
  540. {
  541. struct efx_ef10_nic_data *nic_data = efx->nic_data;
  542. int rc;
  543. #ifdef CONFIG_SFC_SRIOV
  544. struct efx_ef10_nic_data *nic_data_pf;
  545. struct pci_dev *pci_dev_pf;
  546. struct efx_nic *efx_pf;
  547. struct ef10_vf *vf;
  548. if (efx->pci_dev->is_virtfn) {
  549. pci_dev_pf = efx->pci_dev->physfn;
  550. if (pci_dev_pf) {
  551. efx_pf = pci_get_drvdata(pci_dev_pf);
  552. nic_data_pf = efx_pf->nic_data;
  553. vf = nic_data_pf->vf + nic_data->vf_index;
  554. vf->efx = NULL;
  555. } else
  556. netif_info(efx, drv, efx->net_dev,
  557. "Could not get the PF id from VF\n");
  558. }
  559. #endif
  560. efx_ptp_remove(efx);
  561. efx_mcdi_mon_remove(efx);
  562. efx_ef10_rx_free_indir_table(efx);
  563. if (nic_data->wc_membase)
  564. iounmap(nic_data->wc_membase);
  565. rc = efx_ef10_free_vis(efx);
  566. WARN_ON(rc != 0);
  567. if (!nic_data->must_restore_piobufs)
  568. efx_ef10_free_piobufs(efx);
  569. device_remove_file(&efx->pci_dev->dev, &dev_attr_primary_flag);
  570. device_remove_file(&efx->pci_dev->dev, &dev_attr_link_control_flag);
  571. efx_mcdi_fini(efx);
  572. efx_nic_free_buffer(efx, &nic_data->mcdi_buf);
  573. kfree(nic_data);
  574. }
  575. static int efx_ef10_probe_pf(struct efx_nic *efx)
  576. {
  577. return efx_ef10_probe(efx);
  578. }
  579. int efx_ef10_vadaptor_alloc(struct efx_nic *efx, unsigned int port_id)
  580. {
  581. MCDI_DECLARE_BUF(inbuf, MC_CMD_VADAPTOR_ALLOC_IN_LEN);
  582. MCDI_SET_DWORD(inbuf, VADAPTOR_ALLOC_IN_UPSTREAM_PORT_ID, port_id);
  583. return efx_mcdi_rpc(efx, MC_CMD_VADAPTOR_ALLOC, inbuf, sizeof(inbuf),
  584. NULL, 0, NULL);
  585. }
  586. int efx_ef10_vadaptor_free(struct efx_nic *efx, unsigned int port_id)
  587. {
  588. MCDI_DECLARE_BUF(inbuf, MC_CMD_VADAPTOR_FREE_IN_LEN);
  589. MCDI_SET_DWORD(inbuf, VADAPTOR_FREE_IN_UPSTREAM_PORT_ID, port_id);
  590. return efx_mcdi_rpc(efx, MC_CMD_VADAPTOR_FREE, inbuf, sizeof(inbuf),
  591. NULL, 0, NULL);
  592. }
  593. int efx_ef10_vport_add_mac(struct efx_nic *efx,
  594. unsigned int port_id, u8 *mac)
  595. {
  596. MCDI_DECLARE_BUF(inbuf, MC_CMD_VPORT_ADD_MAC_ADDRESS_IN_LEN);
  597. MCDI_SET_DWORD(inbuf, VPORT_ADD_MAC_ADDRESS_IN_VPORT_ID, port_id);
  598. ether_addr_copy(MCDI_PTR(inbuf, VPORT_ADD_MAC_ADDRESS_IN_MACADDR), mac);
  599. return efx_mcdi_rpc(efx, MC_CMD_VPORT_ADD_MAC_ADDRESS, inbuf,
  600. sizeof(inbuf), NULL, 0, NULL);
  601. }
  602. int efx_ef10_vport_del_mac(struct efx_nic *efx,
  603. unsigned int port_id, u8 *mac)
  604. {
  605. MCDI_DECLARE_BUF(inbuf, MC_CMD_VPORT_DEL_MAC_ADDRESS_IN_LEN);
  606. MCDI_SET_DWORD(inbuf, VPORT_DEL_MAC_ADDRESS_IN_VPORT_ID, port_id);
  607. ether_addr_copy(MCDI_PTR(inbuf, VPORT_DEL_MAC_ADDRESS_IN_MACADDR), mac);
  608. return efx_mcdi_rpc(efx, MC_CMD_VPORT_DEL_MAC_ADDRESS, inbuf,
  609. sizeof(inbuf), NULL, 0, NULL);
  610. }
  611. #ifdef CONFIG_SFC_SRIOV
  612. static int efx_ef10_probe_vf(struct efx_nic *efx)
  613. {
  614. int rc;
  615. struct pci_dev *pci_dev_pf;
  616. /* If the parent PF has no VF data structure, it doesn't know about this
  617. * VF so fail probe. The VF needs to be re-created. This can happen
  618. * if the PF driver is unloaded while the VF is assigned to a guest.
  619. */
  620. pci_dev_pf = efx->pci_dev->physfn;
  621. if (pci_dev_pf) {
  622. struct efx_nic *efx_pf = pci_get_drvdata(pci_dev_pf);
  623. struct efx_ef10_nic_data *nic_data_pf = efx_pf->nic_data;
  624. if (!nic_data_pf->vf) {
  625. netif_info(efx, drv, efx->net_dev,
  626. "The VF cannot link to its parent PF; "
  627. "please destroy and re-create the VF\n");
  628. return -EBUSY;
  629. }
  630. }
  631. rc = efx_ef10_probe(efx);
  632. if (rc)
  633. return rc;
  634. rc = efx_ef10_get_vf_index(efx);
  635. if (rc)
  636. goto fail;
  637. if (efx->pci_dev->is_virtfn) {
  638. if (efx->pci_dev->physfn) {
  639. struct efx_nic *efx_pf =
  640. pci_get_drvdata(efx->pci_dev->physfn);
  641. struct efx_ef10_nic_data *nic_data_p = efx_pf->nic_data;
  642. struct efx_ef10_nic_data *nic_data = efx->nic_data;
  643. nic_data_p->vf[nic_data->vf_index].efx = efx;
  644. nic_data_p->vf[nic_data->vf_index].pci_dev =
  645. efx->pci_dev;
  646. } else
  647. netif_info(efx, drv, efx->net_dev,
  648. "Could not get the PF id from VF\n");
  649. }
  650. return 0;
  651. fail:
  652. efx_ef10_remove(efx);
  653. return rc;
  654. }
  655. #else
  656. static int efx_ef10_probe_vf(struct efx_nic *efx __attribute__ ((unused)))
  657. {
  658. return 0;
  659. }
  660. #endif
  661. static int efx_ef10_alloc_vis(struct efx_nic *efx,
  662. unsigned int min_vis, unsigned int max_vis)
  663. {
  664. MCDI_DECLARE_BUF(inbuf, MC_CMD_ALLOC_VIS_IN_LEN);
  665. MCDI_DECLARE_BUF(outbuf, MC_CMD_ALLOC_VIS_OUT_LEN);
  666. struct efx_ef10_nic_data *nic_data = efx->nic_data;
  667. size_t outlen;
  668. int rc;
  669. MCDI_SET_DWORD(inbuf, ALLOC_VIS_IN_MIN_VI_COUNT, min_vis);
  670. MCDI_SET_DWORD(inbuf, ALLOC_VIS_IN_MAX_VI_COUNT, max_vis);
  671. rc = efx_mcdi_rpc(efx, MC_CMD_ALLOC_VIS, inbuf, sizeof(inbuf),
  672. outbuf, sizeof(outbuf), &outlen);
  673. if (rc != 0)
  674. return rc;
  675. if (outlen < MC_CMD_ALLOC_VIS_OUT_LEN)
  676. return -EIO;
  677. netif_dbg(efx, drv, efx->net_dev, "base VI is A0x%03x\n",
  678. MCDI_DWORD(outbuf, ALLOC_VIS_OUT_VI_BASE));
  679. nic_data->vi_base = MCDI_DWORD(outbuf, ALLOC_VIS_OUT_VI_BASE);
  680. nic_data->n_allocated_vis = MCDI_DWORD(outbuf, ALLOC_VIS_OUT_VI_COUNT);
  681. return 0;
  682. }
  683. /* Note that the failure path of this function does not free
  684. * resources, as this will be done by efx_ef10_remove().
  685. */
  686. static int efx_ef10_dimension_resources(struct efx_nic *efx)
  687. {
  688. struct efx_ef10_nic_data *nic_data = efx->nic_data;
  689. unsigned int uc_mem_map_size, wc_mem_map_size;
  690. unsigned int min_vis = max(EFX_TXQ_TYPES,
  691. efx_separate_tx_channels ? 2 : 1);
  692. unsigned int channel_vis, pio_write_vi_base, max_vis;
  693. void __iomem *membase;
  694. int rc;
  695. channel_vis = max(efx->n_channels, efx->n_tx_channels * EFX_TXQ_TYPES);
  696. #ifdef EFX_USE_PIO
  697. /* Try to allocate PIO buffers if wanted and if the full
  698. * number of PIO buffers would be sufficient to allocate one
  699. * copy-buffer per TX channel. Failure is non-fatal, as there
  700. * are only a small number of PIO buffers shared between all
  701. * functions of the controller.
  702. */
  703. if (efx_piobuf_size != 0 &&
  704. ER_DZ_TX_PIOBUF_SIZE / efx_piobuf_size * EF10_TX_PIOBUF_COUNT >=
  705. efx->n_tx_channels) {
  706. unsigned int n_piobufs =
  707. DIV_ROUND_UP(efx->n_tx_channels,
  708. ER_DZ_TX_PIOBUF_SIZE / efx_piobuf_size);
  709. rc = efx_ef10_alloc_piobufs(efx, n_piobufs);
  710. if (rc)
  711. netif_err(efx, probe, efx->net_dev,
  712. "failed to allocate PIO buffers (%d)\n", rc);
  713. else
  714. netif_dbg(efx, probe, efx->net_dev,
  715. "allocated %u PIO buffers\n", n_piobufs);
  716. }
  717. #else
  718. nic_data->n_piobufs = 0;
  719. #endif
  720. /* PIO buffers should be mapped with write-combining enabled,
  721. * and we want to make single UC and WC mappings rather than
  722. * several of each (in fact that's the only option if host
  723. * page size is >4K). So we may allocate some extra VIs just
  724. * for writing PIO buffers through.
  725. *
  726. * The UC mapping contains (channel_vis - 1) complete VIs and the
  727. * first half of the next VI. Then the WC mapping begins with
  728. * the second half of this last VI.
  729. */
  730. uc_mem_map_size = PAGE_ALIGN((channel_vis - 1) * EFX_VI_PAGE_SIZE +
  731. ER_DZ_TX_PIOBUF);
  732. if (nic_data->n_piobufs) {
  733. /* pio_write_vi_base rounds down to give the number of complete
  734. * VIs inside the UC mapping.
  735. */
  736. pio_write_vi_base = uc_mem_map_size / EFX_VI_PAGE_SIZE;
  737. wc_mem_map_size = (PAGE_ALIGN((pio_write_vi_base +
  738. nic_data->n_piobufs) *
  739. EFX_VI_PAGE_SIZE) -
  740. uc_mem_map_size);
  741. max_vis = pio_write_vi_base + nic_data->n_piobufs;
  742. } else {
  743. pio_write_vi_base = 0;
  744. wc_mem_map_size = 0;
  745. max_vis = channel_vis;
  746. }
  747. /* In case the last attached driver failed to free VIs, do it now */
  748. rc = efx_ef10_free_vis(efx);
  749. if (rc != 0)
  750. return rc;
  751. rc = efx_ef10_alloc_vis(efx, min_vis, max_vis);
  752. if (rc != 0)
  753. return rc;
  754. if (nic_data->n_allocated_vis < channel_vis) {
  755. netif_info(efx, drv, efx->net_dev,
  756. "Could not allocate enough VIs to satisfy RSS"
  757. " requirements. Performance may not be optimal.\n");
  758. /* We didn't get the VIs to populate our channels.
  759. * We could keep what we got but then we'd have more
  760. * interrupts than we need.
  761. * Instead calculate new max_channels and restart
  762. */
  763. efx->max_channels = nic_data->n_allocated_vis;
  764. efx->max_tx_channels =
  765. nic_data->n_allocated_vis / EFX_TXQ_TYPES;
  766. efx_ef10_free_vis(efx);
  767. return -EAGAIN;
  768. }
  769. /* If we didn't get enough VIs to map all the PIO buffers, free the
  770. * PIO buffers
  771. */
  772. if (nic_data->n_piobufs &&
  773. nic_data->n_allocated_vis <
  774. pio_write_vi_base + nic_data->n_piobufs) {
  775. netif_dbg(efx, probe, efx->net_dev,
  776. "%u VIs are not sufficient to map %u PIO buffers\n",
  777. nic_data->n_allocated_vis, nic_data->n_piobufs);
  778. efx_ef10_free_piobufs(efx);
  779. }
  780. /* Shrink the original UC mapping of the memory BAR */
  781. membase = ioremap_nocache(efx->membase_phys, uc_mem_map_size);
  782. if (!membase) {
  783. netif_err(efx, probe, efx->net_dev,
  784. "could not shrink memory BAR to %x\n",
  785. uc_mem_map_size);
  786. return -ENOMEM;
  787. }
  788. iounmap(efx->membase);
  789. efx->membase = membase;
  790. /* Set up the WC mapping if needed */
  791. if (wc_mem_map_size) {
  792. nic_data->wc_membase = ioremap_wc(efx->membase_phys +
  793. uc_mem_map_size,
  794. wc_mem_map_size);
  795. if (!nic_data->wc_membase) {
  796. netif_err(efx, probe, efx->net_dev,
  797. "could not allocate WC mapping of size %x\n",
  798. wc_mem_map_size);
  799. return -ENOMEM;
  800. }
  801. nic_data->pio_write_vi_base = pio_write_vi_base;
  802. nic_data->pio_write_base =
  803. nic_data->wc_membase +
  804. (pio_write_vi_base * EFX_VI_PAGE_SIZE + ER_DZ_TX_PIOBUF -
  805. uc_mem_map_size);
  806. rc = efx_ef10_link_piobufs(efx);
  807. if (rc)
  808. efx_ef10_free_piobufs(efx);
  809. }
  810. netif_dbg(efx, probe, efx->net_dev,
  811. "memory BAR at %pa (virtual %p+%x UC, %p+%x WC)\n",
  812. &efx->membase_phys, efx->membase, uc_mem_map_size,
  813. nic_data->wc_membase, wc_mem_map_size);
  814. return 0;
  815. }
  816. static int efx_ef10_init_nic(struct efx_nic *efx)
  817. {
  818. struct efx_ef10_nic_data *nic_data = efx->nic_data;
  819. int rc;
  820. if (nic_data->must_check_datapath_caps) {
  821. rc = efx_ef10_init_datapath_caps(efx);
  822. if (rc)
  823. return rc;
  824. nic_data->must_check_datapath_caps = false;
  825. }
  826. if (nic_data->must_realloc_vis) {
  827. /* We cannot let the number of VIs change now */
  828. rc = efx_ef10_alloc_vis(efx, nic_data->n_allocated_vis,
  829. nic_data->n_allocated_vis);
  830. if (rc)
  831. return rc;
  832. nic_data->must_realloc_vis = false;
  833. }
  834. if (nic_data->must_restore_piobufs && nic_data->n_piobufs) {
  835. rc = efx_ef10_alloc_piobufs(efx, nic_data->n_piobufs);
  836. if (rc == 0) {
  837. rc = efx_ef10_link_piobufs(efx);
  838. if (rc)
  839. efx_ef10_free_piobufs(efx);
  840. }
  841. /* Log an error on failure, but this is non-fatal */
  842. if (rc)
  843. netif_err(efx, drv, efx->net_dev,
  844. "failed to restore PIO buffers (%d)\n", rc);
  845. nic_data->must_restore_piobufs = false;
  846. }
  847. /* don't fail init if RSS setup doesn't work */
  848. efx->type->rx_push_rss_config(efx, false, efx->rx_indir_table);
  849. return 0;
  850. }
  851. static void efx_ef10_reset_mc_allocations(struct efx_nic *efx)
  852. {
  853. struct efx_ef10_nic_data *nic_data = efx->nic_data;
  854. #ifdef CONFIG_SFC_SRIOV
  855. unsigned int i;
  856. #endif
  857. /* All our allocations have been reset */
  858. nic_data->must_realloc_vis = true;
  859. nic_data->must_restore_filters = true;
  860. nic_data->must_restore_piobufs = true;
  861. nic_data->rx_rss_context = EFX_EF10_RSS_CONTEXT_INVALID;
  862. /* Driver-created vswitches and vports must be re-created */
  863. nic_data->must_probe_vswitching = true;
  864. nic_data->vport_id = EVB_PORT_ID_ASSIGNED;
  865. #ifdef CONFIG_SFC_SRIOV
  866. if (nic_data->vf)
  867. for (i = 0; i < efx->vf_count; i++)
  868. nic_data->vf[i].vport_id = 0;
  869. #endif
  870. }
  871. static enum reset_type efx_ef10_map_reset_reason(enum reset_type reason)
  872. {
  873. if (reason == RESET_TYPE_MC_FAILURE)
  874. return RESET_TYPE_DATAPATH;
  875. return efx_mcdi_map_reset_reason(reason);
  876. }
  877. static int efx_ef10_map_reset_flags(u32 *flags)
  878. {
  879. enum {
  880. EF10_RESET_PORT = ((ETH_RESET_MAC | ETH_RESET_PHY) <<
  881. ETH_RESET_SHARED_SHIFT),
  882. EF10_RESET_MC = ((ETH_RESET_DMA | ETH_RESET_FILTER |
  883. ETH_RESET_OFFLOAD | ETH_RESET_MAC |
  884. ETH_RESET_PHY | ETH_RESET_MGMT) <<
  885. ETH_RESET_SHARED_SHIFT)
  886. };
  887. /* We assume for now that our PCI function is permitted to
  888. * reset everything.
  889. */
  890. if ((*flags & EF10_RESET_MC) == EF10_RESET_MC) {
  891. *flags &= ~EF10_RESET_MC;
  892. return RESET_TYPE_WORLD;
  893. }
  894. if ((*flags & EF10_RESET_PORT) == EF10_RESET_PORT) {
  895. *flags &= ~EF10_RESET_PORT;
  896. return RESET_TYPE_ALL;
  897. }
  898. /* no invisible reset implemented */
  899. return -EINVAL;
  900. }
  901. static int efx_ef10_reset(struct efx_nic *efx, enum reset_type reset_type)
  902. {
  903. int rc = efx_mcdi_reset(efx, reset_type);
  904. /* Unprivileged functions return -EPERM, but need to return success
  905. * here so that the datapath is brought back up.
  906. */
  907. if (reset_type == RESET_TYPE_WORLD && rc == -EPERM)
  908. rc = 0;
  909. /* If it was a port reset, trigger reallocation of MC resources.
  910. * Note that on an MC reset nothing needs to be done now because we'll
  911. * detect the MC reset later and handle it then.
  912. * For an FLR, we never get an MC reset event, but the MC has reset all
  913. * resources assigned to us, so we have to trigger reallocation now.
  914. */
  915. if ((reset_type == RESET_TYPE_ALL ||
  916. reset_type == RESET_TYPE_MCDI_TIMEOUT) && !rc)
  917. efx_ef10_reset_mc_allocations(efx);
  918. return rc;
  919. }
  920. #define EF10_DMA_STAT(ext_name, mcdi_name) \
  921. [EF10_STAT_ ## ext_name] = \
  922. { #ext_name, 64, 8 * MC_CMD_MAC_ ## mcdi_name }
  923. #define EF10_DMA_INVIS_STAT(int_name, mcdi_name) \
  924. [EF10_STAT_ ## int_name] = \
  925. { NULL, 64, 8 * MC_CMD_MAC_ ## mcdi_name }
  926. #define EF10_OTHER_STAT(ext_name) \
  927. [EF10_STAT_ ## ext_name] = { #ext_name, 0, 0 }
  928. #define GENERIC_SW_STAT(ext_name) \
  929. [GENERIC_STAT_ ## ext_name] = { #ext_name, 0, 0 }
  930. static const struct efx_hw_stat_desc efx_ef10_stat_desc[EF10_STAT_COUNT] = {
  931. EF10_DMA_STAT(port_tx_bytes, TX_BYTES),
  932. EF10_DMA_STAT(port_tx_packets, TX_PKTS),
  933. EF10_DMA_STAT(port_tx_pause, TX_PAUSE_PKTS),
  934. EF10_DMA_STAT(port_tx_control, TX_CONTROL_PKTS),
  935. EF10_DMA_STAT(port_tx_unicast, TX_UNICAST_PKTS),
  936. EF10_DMA_STAT(port_tx_multicast, TX_MULTICAST_PKTS),
  937. EF10_DMA_STAT(port_tx_broadcast, TX_BROADCAST_PKTS),
  938. EF10_DMA_STAT(port_tx_lt64, TX_LT64_PKTS),
  939. EF10_DMA_STAT(port_tx_64, TX_64_PKTS),
  940. EF10_DMA_STAT(port_tx_65_to_127, TX_65_TO_127_PKTS),
  941. EF10_DMA_STAT(port_tx_128_to_255, TX_128_TO_255_PKTS),
  942. EF10_DMA_STAT(port_tx_256_to_511, TX_256_TO_511_PKTS),
  943. EF10_DMA_STAT(port_tx_512_to_1023, TX_512_TO_1023_PKTS),
  944. EF10_DMA_STAT(port_tx_1024_to_15xx, TX_1024_TO_15XX_PKTS),
  945. EF10_DMA_STAT(port_tx_15xx_to_jumbo, TX_15XX_TO_JUMBO_PKTS),
  946. EF10_DMA_STAT(port_rx_bytes, RX_BYTES),
  947. EF10_DMA_INVIS_STAT(port_rx_bytes_minus_good_bytes, RX_BAD_BYTES),
  948. EF10_OTHER_STAT(port_rx_good_bytes),
  949. EF10_OTHER_STAT(port_rx_bad_bytes),
  950. EF10_DMA_STAT(port_rx_packets, RX_PKTS),
  951. EF10_DMA_STAT(port_rx_good, RX_GOOD_PKTS),
  952. EF10_DMA_STAT(port_rx_bad, RX_BAD_FCS_PKTS),
  953. EF10_DMA_STAT(port_rx_pause, RX_PAUSE_PKTS),
  954. EF10_DMA_STAT(port_rx_control, RX_CONTROL_PKTS),
  955. EF10_DMA_STAT(port_rx_unicast, RX_UNICAST_PKTS),
  956. EF10_DMA_STAT(port_rx_multicast, RX_MULTICAST_PKTS),
  957. EF10_DMA_STAT(port_rx_broadcast, RX_BROADCAST_PKTS),
  958. EF10_DMA_STAT(port_rx_lt64, RX_UNDERSIZE_PKTS),
  959. EF10_DMA_STAT(port_rx_64, RX_64_PKTS),
  960. EF10_DMA_STAT(port_rx_65_to_127, RX_65_TO_127_PKTS),
  961. EF10_DMA_STAT(port_rx_128_to_255, RX_128_TO_255_PKTS),
  962. EF10_DMA_STAT(port_rx_256_to_511, RX_256_TO_511_PKTS),
  963. EF10_DMA_STAT(port_rx_512_to_1023, RX_512_TO_1023_PKTS),
  964. EF10_DMA_STAT(port_rx_1024_to_15xx, RX_1024_TO_15XX_PKTS),
  965. EF10_DMA_STAT(port_rx_15xx_to_jumbo, RX_15XX_TO_JUMBO_PKTS),
  966. EF10_DMA_STAT(port_rx_gtjumbo, RX_GTJUMBO_PKTS),
  967. EF10_DMA_STAT(port_rx_bad_gtjumbo, RX_JABBER_PKTS),
  968. EF10_DMA_STAT(port_rx_overflow, RX_OVERFLOW_PKTS),
  969. EF10_DMA_STAT(port_rx_align_error, RX_ALIGN_ERROR_PKTS),
  970. EF10_DMA_STAT(port_rx_length_error, RX_LENGTH_ERROR_PKTS),
  971. EF10_DMA_STAT(port_rx_nodesc_drops, RX_NODESC_DROPS),
  972. GENERIC_SW_STAT(rx_nodesc_trunc),
  973. GENERIC_SW_STAT(rx_noskb_drops),
  974. EF10_DMA_STAT(port_rx_pm_trunc_bb_overflow, PM_TRUNC_BB_OVERFLOW),
  975. EF10_DMA_STAT(port_rx_pm_discard_bb_overflow, PM_DISCARD_BB_OVERFLOW),
  976. EF10_DMA_STAT(port_rx_pm_trunc_vfifo_full, PM_TRUNC_VFIFO_FULL),
  977. EF10_DMA_STAT(port_rx_pm_discard_vfifo_full, PM_DISCARD_VFIFO_FULL),
  978. EF10_DMA_STAT(port_rx_pm_trunc_qbb, PM_TRUNC_QBB),
  979. EF10_DMA_STAT(port_rx_pm_discard_qbb, PM_DISCARD_QBB),
  980. EF10_DMA_STAT(port_rx_pm_discard_mapping, PM_DISCARD_MAPPING),
  981. EF10_DMA_STAT(port_rx_dp_q_disabled_packets, RXDP_Q_DISABLED_PKTS),
  982. EF10_DMA_STAT(port_rx_dp_di_dropped_packets, RXDP_DI_DROPPED_PKTS),
  983. EF10_DMA_STAT(port_rx_dp_streaming_packets, RXDP_STREAMING_PKTS),
  984. EF10_DMA_STAT(port_rx_dp_hlb_fetch, RXDP_HLB_FETCH_CONDITIONS),
  985. EF10_DMA_STAT(port_rx_dp_hlb_wait, RXDP_HLB_WAIT_CONDITIONS),
  986. EF10_DMA_STAT(rx_unicast, VADAPTER_RX_UNICAST_PACKETS),
  987. EF10_DMA_STAT(rx_unicast_bytes, VADAPTER_RX_UNICAST_BYTES),
  988. EF10_DMA_STAT(rx_multicast, VADAPTER_RX_MULTICAST_PACKETS),
  989. EF10_DMA_STAT(rx_multicast_bytes, VADAPTER_RX_MULTICAST_BYTES),
  990. EF10_DMA_STAT(rx_broadcast, VADAPTER_RX_BROADCAST_PACKETS),
  991. EF10_DMA_STAT(rx_broadcast_bytes, VADAPTER_RX_BROADCAST_BYTES),
  992. EF10_DMA_STAT(rx_bad, VADAPTER_RX_BAD_PACKETS),
  993. EF10_DMA_STAT(rx_bad_bytes, VADAPTER_RX_BAD_BYTES),
  994. EF10_DMA_STAT(rx_overflow, VADAPTER_RX_OVERFLOW),
  995. EF10_DMA_STAT(tx_unicast, VADAPTER_TX_UNICAST_PACKETS),
  996. EF10_DMA_STAT(tx_unicast_bytes, VADAPTER_TX_UNICAST_BYTES),
  997. EF10_DMA_STAT(tx_multicast, VADAPTER_TX_MULTICAST_PACKETS),
  998. EF10_DMA_STAT(tx_multicast_bytes, VADAPTER_TX_MULTICAST_BYTES),
  999. EF10_DMA_STAT(tx_broadcast, VADAPTER_TX_BROADCAST_PACKETS),
  1000. EF10_DMA_STAT(tx_broadcast_bytes, VADAPTER_TX_BROADCAST_BYTES),
  1001. EF10_DMA_STAT(tx_bad, VADAPTER_TX_BAD_PACKETS),
  1002. EF10_DMA_STAT(tx_bad_bytes, VADAPTER_TX_BAD_BYTES),
  1003. EF10_DMA_STAT(tx_overflow, VADAPTER_TX_OVERFLOW),
  1004. };
  1005. #define HUNT_COMMON_STAT_MASK ((1ULL << EF10_STAT_port_tx_bytes) | \
  1006. (1ULL << EF10_STAT_port_tx_packets) | \
  1007. (1ULL << EF10_STAT_port_tx_pause) | \
  1008. (1ULL << EF10_STAT_port_tx_unicast) | \
  1009. (1ULL << EF10_STAT_port_tx_multicast) | \
  1010. (1ULL << EF10_STAT_port_tx_broadcast) | \
  1011. (1ULL << EF10_STAT_port_rx_bytes) | \
  1012. (1ULL << \
  1013. EF10_STAT_port_rx_bytes_minus_good_bytes) | \
  1014. (1ULL << EF10_STAT_port_rx_good_bytes) | \
  1015. (1ULL << EF10_STAT_port_rx_bad_bytes) | \
  1016. (1ULL << EF10_STAT_port_rx_packets) | \
  1017. (1ULL << EF10_STAT_port_rx_good) | \
  1018. (1ULL << EF10_STAT_port_rx_bad) | \
  1019. (1ULL << EF10_STAT_port_rx_pause) | \
  1020. (1ULL << EF10_STAT_port_rx_control) | \
  1021. (1ULL << EF10_STAT_port_rx_unicast) | \
  1022. (1ULL << EF10_STAT_port_rx_multicast) | \
  1023. (1ULL << EF10_STAT_port_rx_broadcast) | \
  1024. (1ULL << EF10_STAT_port_rx_lt64) | \
  1025. (1ULL << EF10_STAT_port_rx_64) | \
  1026. (1ULL << EF10_STAT_port_rx_65_to_127) | \
  1027. (1ULL << EF10_STAT_port_rx_128_to_255) | \
  1028. (1ULL << EF10_STAT_port_rx_256_to_511) | \
  1029. (1ULL << EF10_STAT_port_rx_512_to_1023) |\
  1030. (1ULL << EF10_STAT_port_rx_1024_to_15xx) |\
  1031. (1ULL << EF10_STAT_port_rx_15xx_to_jumbo) |\
  1032. (1ULL << EF10_STAT_port_rx_gtjumbo) | \
  1033. (1ULL << EF10_STAT_port_rx_bad_gtjumbo) |\
  1034. (1ULL << EF10_STAT_port_rx_overflow) | \
  1035. (1ULL << EF10_STAT_port_rx_nodesc_drops) |\
  1036. (1ULL << GENERIC_STAT_rx_nodesc_trunc) | \
  1037. (1ULL << GENERIC_STAT_rx_noskb_drops))
  1038. /* These statistics are only provided by the 10G MAC. For a 10G/40G
  1039. * switchable port we do not expose these because they might not
  1040. * include all the packets they should.
  1041. */
  1042. #define HUNT_10G_ONLY_STAT_MASK ((1ULL << EF10_STAT_port_tx_control) | \
  1043. (1ULL << EF10_STAT_port_tx_lt64) | \
  1044. (1ULL << EF10_STAT_port_tx_64) | \
  1045. (1ULL << EF10_STAT_port_tx_65_to_127) |\
  1046. (1ULL << EF10_STAT_port_tx_128_to_255) |\
  1047. (1ULL << EF10_STAT_port_tx_256_to_511) |\
  1048. (1ULL << EF10_STAT_port_tx_512_to_1023) |\
  1049. (1ULL << EF10_STAT_port_tx_1024_to_15xx) |\
  1050. (1ULL << EF10_STAT_port_tx_15xx_to_jumbo))
  1051. /* These statistics are only provided by the 40G MAC. For a 10G/40G
  1052. * switchable port we do expose these because the errors will otherwise
  1053. * be silent.
  1054. */
  1055. #define HUNT_40G_EXTRA_STAT_MASK ((1ULL << EF10_STAT_port_rx_align_error) |\
  1056. (1ULL << EF10_STAT_port_rx_length_error))
  1057. /* These statistics are only provided if the firmware supports the
  1058. * capability PM_AND_RXDP_COUNTERS.
  1059. */
  1060. #define HUNT_PM_AND_RXDP_STAT_MASK ( \
  1061. (1ULL << EF10_STAT_port_rx_pm_trunc_bb_overflow) | \
  1062. (1ULL << EF10_STAT_port_rx_pm_discard_bb_overflow) | \
  1063. (1ULL << EF10_STAT_port_rx_pm_trunc_vfifo_full) | \
  1064. (1ULL << EF10_STAT_port_rx_pm_discard_vfifo_full) | \
  1065. (1ULL << EF10_STAT_port_rx_pm_trunc_qbb) | \
  1066. (1ULL << EF10_STAT_port_rx_pm_discard_qbb) | \
  1067. (1ULL << EF10_STAT_port_rx_pm_discard_mapping) | \
  1068. (1ULL << EF10_STAT_port_rx_dp_q_disabled_packets) | \
  1069. (1ULL << EF10_STAT_port_rx_dp_di_dropped_packets) | \
  1070. (1ULL << EF10_STAT_port_rx_dp_streaming_packets) | \
  1071. (1ULL << EF10_STAT_port_rx_dp_hlb_fetch) | \
  1072. (1ULL << EF10_STAT_port_rx_dp_hlb_wait))
  1073. static u64 efx_ef10_raw_stat_mask(struct efx_nic *efx)
  1074. {
  1075. u64 raw_mask = HUNT_COMMON_STAT_MASK;
  1076. u32 port_caps = efx_mcdi_phy_get_caps(efx);
  1077. struct efx_ef10_nic_data *nic_data = efx->nic_data;
  1078. if (!(efx->mcdi->fn_flags &
  1079. 1 << MC_CMD_DRV_ATTACH_EXT_OUT_FLAG_LINKCTRL))
  1080. return 0;
  1081. if (port_caps & (1 << MC_CMD_PHY_CAP_40000FDX_LBN))
  1082. raw_mask |= HUNT_40G_EXTRA_STAT_MASK;
  1083. else
  1084. raw_mask |= HUNT_10G_ONLY_STAT_MASK;
  1085. if (nic_data->datapath_caps &
  1086. (1 << MC_CMD_GET_CAPABILITIES_OUT_PM_AND_RXDP_COUNTERS_LBN))
  1087. raw_mask |= HUNT_PM_AND_RXDP_STAT_MASK;
  1088. return raw_mask;
  1089. }
  1090. static void efx_ef10_get_stat_mask(struct efx_nic *efx, unsigned long *mask)
  1091. {
  1092. struct efx_ef10_nic_data *nic_data = efx->nic_data;
  1093. u64 raw_mask[2];
  1094. raw_mask[0] = efx_ef10_raw_stat_mask(efx);
  1095. /* Only show vadaptor stats when EVB capability is present */
  1096. if (nic_data->datapath_caps &
  1097. (1 << MC_CMD_GET_CAPABILITIES_OUT_EVB_LBN)) {
  1098. raw_mask[0] |= ~((1ULL << EF10_STAT_rx_unicast) - 1);
  1099. raw_mask[1] = (1ULL << (EF10_STAT_COUNT - 63)) - 1;
  1100. } else {
  1101. raw_mask[1] = 0;
  1102. }
  1103. #if BITS_PER_LONG == 64
  1104. mask[0] = raw_mask[0];
  1105. mask[1] = raw_mask[1];
  1106. #else
  1107. mask[0] = raw_mask[0] & 0xffffffff;
  1108. mask[1] = raw_mask[0] >> 32;
  1109. mask[2] = raw_mask[1] & 0xffffffff;
  1110. mask[3] = raw_mask[1] >> 32;
  1111. #endif
  1112. }
  1113. static size_t efx_ef10_describe_stats(struct efx_nic *efx, u8 *names)
  1114. {
  1115. DECLARE_BITMAP(mask, EF10_STAT_COUNT);
  1116. efx_ef10_get_stat_mask(efx, mask);
  1117. return efx_nic_describe_stats(efx_ef10_stat_desc, EF10_STAT_COUNT,
  1118. mask, names);
  1119. }
  1120. static size_t efx_ef10_update_stats_common(struct efx_nic *efx, u64 *full_stats,
  1121. struct rtnl_link_stats64 *core_stats)
  1122. {
  1123. DECLARE_BITMAP(mask, EF10_STAT_COUNT);
  1124. struct efx_ef10_nic_data *nic_data = efx->nic_data;
  1125. u64 *stats = nic_data->stats;
  1126. size_t stats_count = 0, index;
  1127. efx_ef10_get_stat_mask(efx, mask);
  1128. if (full_stats) {
  1129. for_each_set_bit(index, mask, EF10_STAT_COUNT) {
  1130. if (efx_ef10_stat_desc[index].name) {
  1131. *full_stats++ = stats[index];
  1132. ++stats_count;
  1133. }
  1134. }
  1135. }
  1136. if (!core_stats)
  1137. return stats_count;
  1138. if (nic_data->datapath_caps &
  1139. 1 << MC_CMD_GET_CAPABILITIES_OUT_EVB_LBN) {
  1140. /* Use vadaptor stats. */
  1141. core_stats->rx_packets = stats[EF10_STAT_rx_unicast] +
  1142. stats[EF10_STAT_rx_multicast] +
  1143. stats[EF10_STAT_rx_broadcast];
  1144. core_stats->tx_packets = stats[EF10_STAT_tx_unicast] +
  1145. stats[EF10_STAT_tx_multicast] +
  1146. stats[EF10_STAT_tx_broadcast];
  1147. core_stats->rx_bytes = stats[EF10_STAT_rx_unicast_bytes] +
  1148. stats[EF10_STAT_rx_multicast_bytes] +
  1149. stats[EF10_STAT_rx_broadcast_bytes];
  1150. core_stats->tx_bytes = stats[EF10_STAT_tx_unicast_bytes] +
  1151. stats[EF10_STAT_tx_multicast_bytes] +
  1152. stats[EF10_STAT_tx_broadcast_bytes];
  1153. core_stats->rx_dropped = stats[GENERIC_STAT_rx_nodesc_trunc] +
  1154. stats[GENERIC_STAT_rx_noskb_drops];
  1155. core_stats->multicast = stats[EF10_STAT_rx_multicast];
  1156. core_stats->rx_crc_errors = stats[EF10_STAT_rx_bad];
  1157. core_stats->rx_fifo_errors = stats[EF10_STAT_rx_overflow];
  1158. core_stats->rx_errors = core_stats->rx_crc_errors;
  1159. core_stats->tx_errors = stats[EF10_STAT_tx_bad];
  1160. } else {
  1161. /* Use port stats. */
  1162. core_stats->rx_packets = stats[EF10_STAT_port_rx_packets];
  1163. core_stats->tx_packets = stats[EF10_STAT_port_tx_packets];
  1164. core_stats->rx_bytes = stats[EF10_STAT_port_rx_bytes];
  1165. core_stats->tx_bytes = stats[EF10_STAT_port_tx_bytes];
  1166. core_stats->rx_dropped = stats[EF10_STAT_port_rx_nodesc_drops] +
  1167. stats[GENERIC_STAT_rx_nodesc_trunc] +
  1168. stats[GENERIC_STAT_rx_noskb_drops];
  1169. core_stats->multicast = stats[EF10_STAT_port_rx_multicast];
  1170. core_stats->rx_length_errors =
  1171. stats[EF10_STAT_port_rx_gtjumbo] +
  1172. stats[EF10_STAT_port_rx_length_error];
  1173. core_stats->rx_crc_errors = stats[EF10_STAT_port_rx_bad];
  1174. core_stats->rx_frame_errors =
  1175. stats[EF10_STAT_port_rx_align_error];
  1176. core_stats->rx_fifo_errors = stats[EF10_STAT_port_rx_overflow];
  1177. core_stats->rx_errors = (core_stats->rx_length_errors +
  1178. core_stats->rx_crc_errors +
  1179. core_stats->rx_frame_errors);
  1180. }
  1181. return stats_count;
  1182. }
  1183. static int efx_ef10_try_update_nic_stats_pf(struct efx_nic *efx)
  1184. {
  1185. struct efx_ef10_nic_data *nic_data = efx->nic_data;
  1186. DECLARE_BITMAP(mask, EF10_STAT_COUNT);
  1187. __le64 generation_start, generation_end;
  1188. u64 *stats = nic_data->stats;
  1189. __le64 *dma_stats;
  1190. efx_ef10_get_stat_mask(efx, mask);
  1191. dma_stats = efx->stats_buffer.addr;
  1192. nic_data = efx->nic_data;
  1193. generation_end = dma_stats[MC_CMD_MAC_GENERATION_END];
  1194. if (generation_end == EFX_MC_STATS_GENERATION_INVALID)
  1195. return 0;
  1196. rmb();
  1197. efx_nic_update_stats(efx_ef10_stat_desc, EF10_STAT_COUNT, mask,
  1198. stats, efx->stats_buffer.addr, false);
  1199. rmb();
  1200. generation_start = dma_stats[MC_CMD_MAC_GENERATION_START];
  1201. if (generation_end != generation_start)
  1202. return -EAGAIN;
  1203. /* Update derived statistics */
  1204. efx_nic_fix_nodesc_drop_stat(efx,
  1205. &stats[EF10_STAT_port_rx_nodesc_drops]);
  1206. stats[EF10_STAT_port_rx_good_bytes] =
  1207. stats[EF10_STAT_port_rx_bytes] -
  1208. stats[EF10_STAT_port_rx_bytes_minus_good_bytes];
  1209. efx_update_diff_stat(&stats[EF10_STAT_port_rx_bad_bytes],
  1210. stats[EF10_STAT_port_rx_bytes_minus_good_bytes]);
  1211. efx_update_sw_stats(efx, stats);
  1212. return 0;
  1213. }
  1214. static size_t efx_ef10_update_stats_pf(struct efx_nic *efx, u64 *full_stats,
  1215. struct rtnl_link_stats64 *core_stats)
  1216. {
  1217. int retry;
  1218. /* If we're unlucky enough to read statistics during the DMA, wait
  1219. * up to 10ms for it to finish (typically takes <500us)
  1220. */
  1221. for (retry = 0; retry < 100; ++retry) {
  1222. if (efx_ef10_try_update_nic_stats_pf(efx) == 0)
  1223. break;
  1224. udelay(100);
  1225. }
  1226. return efx_ef10_update_stats_common(efx, full_stats, core_stats);
  1227. }
  1228. static int efx_ef10_try_update_nic_stats_vf(struct efx_nic *efx)
  1229. {
  1230. MCDI_DECLARE_BUF(inbuf, MC_CMD_MAC_STATS_IN_LEN);
  1231. struct efx_ef10_nic_data *nic_data = efx->nic_data;
  1232. DECLARE_BITMAP(mask, EF10_STAT_COUNT);
  1233. __le64 generation_start, generation_end;
  1234. u64 *stats = nic_data->stats;
  1235. u32 dma_len = MC_CMD_MAC_NSTATS * sizeof(u64);
  1236. struct efx_buffer stats_buf;
  1237. __le64 *dma_stats;
  1238. int rc;
  1239. spin_unlock_bh(&efx->stats_lock);
  1240. if (in_interrupt()) {
  1241. /* If in atomic context, cannot update stats. Just update the
  1242. * software stats and return so the caller can continue.
  1243. */
  1244. spin_lock_bh(&efx->stats_lock);
  1245. efx_update_sw_stats(efx, stats);
  1246. return 0;
  1247. }
  1248. efx_ef10_get_stat_mask(efx, mask);
  1249. rc = efx_nic_alloc_buffer(efx, &stats_buf, dma_len, GFP_ATOMIC);
  1250. if (rc) {
  1251. spin_lock_bh(&efx->stats_lock);
  1252. return rc;
  1253. }
  1254. dma_stats = stats_buf.addr;
  1255. dma_stats[MC_CMD_MAC_GENERATION_END] = EFX_MC_STATS_GENERATION_INVALID;
  1256. MCDI_SET_QWORD(inbuf, MAC_STATS_IN_DMA_ADDR, stats_buf.dma_addr);
  1257. MCDI_POPULATE_DWORD_1(inbuf, MAC_STATS_IN_CMD,
  1258. MAC_STATS_IN_DMA, 1);
  1259. MCDI_SET_DWORD(inbuf, MAC_STATS_IN_DMA_LEN, dma_len);
  1260. MCDI_SET_DWORD(inbuf, MAC_STATS_IN_PORT_ID, EVB_PORT_ID_ASSIGNED);
  1261. rc = efx_mcdi_rpc_quiet(efx, MC_CMD_MAC_STATS, inbuf, sizeof(inbuf),
  1262. NULL, 0, NULL);
  1263. spin_lock_bh(&efx->stats_lock);
  1264. if (rc) {
  1265. /* Expect ENOENT if DMA queues have not been set up */
  1266. if (rc != -ENOENT || atomic_read(&efx->active_queues))
  1267. efx_mcdi_display_error(efx, MC_CMD_MAC_STATS,
  1268. sizeof(inbuf), NULL, 0, rc);
  1269. goto out;
  1270. }
  1271. generation_end = dma_stats[MC_CMD_MAC_GENERATION_END];
  1272. if (generation_end == EFX_MC_STATS_GENERATION_INVALID) {
  1273. WARN_ON_ONCE(1);
  1274. goto out;
  1275. }
  1276. rmb();
  1277. efx_nic_update_stats(efx_ef10_stat_desc, EF10_STAT_COUNT, mask,
  1278. stats, stats_buf.addr, false);
  1279. rmb();
  1280. generation_start = dma_stats[MC_CMD_MAC_GENERATION_START];
  1281. if (generation_end != generation_start) {
  1282. rc = -EAGAIN;
  1283. goto out;
  1284. }
  1285. efx_update_sw_stats(efx, stats);
  1286. out:
  1287. efx_nic_free_buffer(efx, &stats_buf);
  1288. return rc;
  1289. }
  1290. static size_t efx_ef10_update_stats_vf(struct efx_nic *efx, u64 *full_stats,
  1291. struct rtnl_link_stats64 *core_stats)
  1292. {
  1293. if (efx_ef10_try_update_nic_stats_vf(efx))
  1294. return 0;
  1295. return efx_ef10_update_stats_common(efx, full_stats, core_stats);
  1296. }
  1297. static void efx_ef10_push_irq_moderation(struct efx_channel *channel)
  1298. {
  1299. struct efx_nic *efx = channel->efx;
  1300. unsigned int mode, value;
  1301. efx_dword_t timer_cmd;
  1302. if (channel->irq_moderation) {
  1303. mode = 3;
  1304. value = channel->irq_moderation - 1;
  1305. } else {
  1306. mode = 0;
  1307. value = 0;
  1308. }
  1309. if (EFX_EF10_WORKAROUND_35388(efx)) {
  1310. EFX_POPULATE_DWORD_3(timer_cmd, ERF_DD_EVQ_IND_TIMER_FLAGS,
  1311. EFE_DD_EVQ_IND_TIMER_FLAGS,
  1312. ERF_DD_EVQ_IND_TIMER_MODE, mode,
  1313. ERF_DD_EVQ_IND_TIMER_VAL, value);
  1314. efx_writed_page(efx, &timer_cmd, ER_DD_EVQ_INDIRECT,
  1315. channel->channel);
  1316. } else {
  1317. EFX_POPULATE_DWORD_2(timer_cmd, ERF_DZ_TC_TIMER_MODE, mode,
  1318. ERF_DZ_TC_TIMER_VAL, value);
  1319. efx_writed_page(efx, &timer_cmd, ER_DZ_EVQ_TMR,
  1320. channel->channel);
  1321. }
  1322. }
  1323. static void efx_ef10_get_wol_vf(struct efx_nic *efx,
  1324. struct ethtool_wolinfo *wol) {}
  1325. static int efx_ef10_set_wol_vf(struct efx_nic *efx, u32 type)
  1326. {
  1327. return -EOPNOTSUPP;
  1328. }
  1329. static void efx_ef10_get_wol(struct efx_nic *efx, struct ethtool_wolinfo *wol)
  1330. {
  1331. wol->supported = 0;
  1332. wol->wolopts = 0;
  1333. memset(&wol->sopass, 0, sizeof(wol->sopass));
  1334. }
  1335. static int efx_ef10_set_wol(struct efx_nic *efx, u32 type)
  1336. {
  1337. if (type != 0)
  1338. return -EINVAL;
  1339. return 0;
  1340. }
  1341. static void efx_ef10_mcdi_request(struct efx_nic *efx,
  1342. const efx_dword_t *hdr, size_t hdr_len,
  1343. const efx_dword_t *sdu, size_t sdu_len)
  1344. {
  1345. struct efx_ef10_nic_data *nic_data = efx->nic_data;
  1346. u8 *pdu = nic_data->mcdi_buf.addr;
  1347. memcpy(pdu, hdr, hdr_len);
  1348. memcpy(pdu + hdr_len, sdu, sdu_len);
  1349. wmb();
  1350. /* The hardware provides 'low' and 'high' (doorbell) registers
  1351. * for passing the 64-bit address of an MCDI request to
  1352. * firmware. However the dwords are swapped by firmware. The
  1353. * least significant bits of the doorbell are then 0 for all
  1354. * MCDI requests due to alignment.
  1355. */
  1356. _efx_writed(efx, cpu_to_le32((u64)nic_data->mcdi_buf.dma_addr >> 32),
  1357. ER_DZ_MC_DB_LWRD);
  1358. _efx_writed(efx, cpu_to_le32((u32)nic_data->mcdi_buf.dma_addr),
  1359. ER_DZ_MC_DB_HWRD);
  1360. }
  1361. static bool efx_ef10_mcdi_poll_response(struct efx_nic *efx)
  1362. {
  1363. struct efx_ef10_nic_data *nic_data = efx->nic_data;
  1364. const efx_dword_t hdr = *(const efx_dword_t *)nic_data->mcdi_buf.addr;
  1365. rmb();
  1366. return EFX_DWORD_FIELD(hdr, MCDI_HEADER_RESPONSE);
  1367. }
  1368. static void
  1369. efx_ef10_mcdi_read_response(struct efx_nic *efx, efx_dword_t *outbuf,
  1370. size_t offset, size_t outlen)
  1371. {
  1372. struct efx_ef10_nic_data *nic_data = efx->nic_data;
  1373. const u8 *pdu = nic_data->mcdi_buf.addr;
  1374. memcpy(outbuf, pdu + offset, outlen);
  1375. }
  1376. static int efx_ef10_mcdi_poll_reboot(struct efx_nic *efx)
  1377. {
  1378. struct efx_ef10_nic_data *nic_data = efx->nic_data;
  1379. int rc;
  1380. rc = efx_ef10_get_warm_boot_count(efx);
  1381. if (rc < 0) {
  1382. /* The firmware is presumably in the process of
  1383. * rebooting. However, we are supposed to report each
  1384. * reboot just once, so we must only do that once we
  1385. * can read and store the updated warm boot count.
  1386. */
  1387. return 0;
  1388. }
  1389. if (rc == nic_data->warm_boot_count)
  1390. return 0;
  1391. nic_data->warm_boot_count = rc;
  1392. /* All our allocations have been reset */
  1393. efx_ef10_reset_mc_allocations(efx);
  1394. /* The datapath firmware might have been changed */
  1395. nic_data->must_check_datapath_caps = true;
  1396. /* MAC statistics have been cleared on the NIC; clear the local
  1397. * statistic that we update with efx_update_diff_stat().
  1398. */
  1399. nic_data->stats[EF10_STAT_port_rx_bad_bytes] = 0;
  1400. return -EIO;
  1401. }
  1402. /* Handle an MSI interrupt
  1403. *
  1404. * Handle an MSI hardware interrupt. This routine schedules event
  1405. * queue processing. No interrupt acknowledgement cycle is necessary.
  1406. * Also, we never need to check that the interrupt is for us, since
  1407. * MSI interrupts cannot be shared.
  1408. */
  1409. static irqreturn_t efx_ef10_msi_interrupt(int irq, void *dev_id)
  1410. {
  1411. struct efx_msi_context *context = dev_id;
  1412. struct efx_nic *efx = context->efx;
  1413. netif_vdbg(efx, intr, efx->net_dev,
  1414. "IRQ %d on CPU %d\n", irq, raw_smp_processor_id());
  1415. if (likely(ACCESS_ONCE(efx->irq_soft_enabled))) {
  1416. /* Note test interrupts */
  1417. if (context->index == efx->irq_level)
  1418. efx->last_irq_cpu = raw_smp_processor_id();
  1419. /* Schedule processing of the channel */
  1420. efx_schedule_channel_irq(efx->channel[context->index]);
  1421. }
  1422. return IRQ_HANDLED;
  1423. }
  1424. static irqreturn_t efx_ef10_legacy_interrupt(int irq, void *dev_id)
  1425. {
  1426. struct efx_nic *efx = dev_id;
  1427. bool soft_enabled = ACCESS_ONCE(efx->irq_soft_enabled);
  1428. struct efx_channel *channel;
  1429. efx_dword_t reg;
  1430. u32 queues;
  1431. /* Read the ISR which also ACKs the interrupts */
  1432. efx_readd(efx, &reg, ER_DZ_BIU_INT_ISR);
  1433. queues = EFX_DWORD_FIELD(reg, ERF_DZ_ISR_REG);
  1434. if (queues == 0)
  1435. return IRQ_NONE;
  1436. if (likely(soft_enabled)) {
  1437. /* Note test interrupts */
  1438. if (queues & (1U << efx->irq_level))
  1439. efx->last_irq_cpu = raw_smp_processor_id();
  1440. efx_for_each_channel(channel, efx) {
  1441. if (queues & 1)
  1442. efx_schedule_channel_irq(channel);
  1443. queues >>= 1;
  1444. }
  1445. }
  1446. netif_vdbg(efx, intr, efx->net_dev,
  1447. "IRQ %d on CPU %d status " EFX_DWORD_FMT "\n",
  1448. irq, raw_smp_processor_id(), EFX_DWORD_VAL(reg));
  1449. return IRQ_HANDLED;
  1450. }
  1451. static void efx_ef10_irq_test_generate(struct efx_nic *efx)
  1452. {
  1453. MCDI_DECLARE_BUF(inbuf, MC_CMD_TRIGGER_INTERRUPT_IN_LEN);
  1454. BUILD_BUG_ON(MC_CMD_TRIGGER_INTERRUPT_OUT_LEN != 0);
  1455. MCDI_SET_DWORD(inbuf, TRIGGER_INTERRUPT_IN_INTR_LEVEL, efx->irq_level);
  1456. (void) efx_mcdi_rpc(efx, MC_CMD_TRIGGER_INTERRUPT,
  1457. inbuf, sizeof(inbuf), NULL, 0, NULL);
  1458. }
  1459. static int efx_ef10_tx_probe(struct efx_tx_queue *tx_queue)
  1460. {
  1461. return efx_nic_alloc_buffer(tx_queue->efx, &tx_queue->txd.buf,
  1462. (tx_queue->ptr_mask + 1) *
  1463. sizeof(efx_qword_t),
  1464. GFP_KERNEL);
  1465. }
  1466. /* This writes to the TX_DESC_WPTR and also pushes data */
  1467. static inline void efx_ef10_push_tx_desc(struct efx_tx_queue *tx_queue,
  1468. const efx_qword_t *txd)
  1469. {
  1470. unsigned int write_ptr;
  1471. efx_oword_t reg;
  1472. write_ptr = tx_queue->write_count & tx_queue->ptr_mask;
  1473. EFX_POPULATE_OWORD_1(reg, ERF_DZ_TX_DESC_WPTR, write_ptr);
  1474. reg.qword[0] = *txd;
  1475. efx_writeo_page(tx_queue->efx, &reg,
  1476. ER_DZ_TX_DESC_UPD, tx_queue->queue);
  1477. }
  1478. static void efx_ef10_tx_init(struct efx_tx_queue *tx_queue)
  1479. {
  1480. MCDI_DECLARE_BUF(inbuf, MC_CMD_INIT_TXQ_IN_LEN(EFX_MAX_DMAQ_SIZE * 8 /
  1481. EFX_BUF_SIZE));
  1482. bool csum_offload = tx_queue->queue & EFX_TXQ_TYPE_OFFLOAD;
  1483. size_t entries = tx_queue->txd.buf.len / EFX_BUF_SIZE;
  1484. struct efx_channel *channel = tx_queue->channel;
  1485. struct efx_nic *efx = tx_queue->efx;
  1486. struct efx_ef10_nic_data *nic_data = efx->nic_data;
  1487. size_t inlen;
  1488. dma_addr_t dma_addr;
  1489. efx_qword_t *txd;
  1490. int rc;
  1491. int i;
  1492. BUILD_BUG_ON(MC_CMD_INIT_TXQ_OUT_LEN != 0);
  1493. MCDI_SET_DWORD(inbuf, INIT_TXQ_IN_SIZE, tx_queue->ptr_mask + 1);
  1494. MCDI_SET_DWORD(inbuf, INIT_TXQ_IN_TARGET_EVQ, channel->channel);
  1495. MCDI_SET_DWORD(inbuf, INIT_TXQ_IN_LABEL, tx_queue->queue);
  1496. MCDI_SET_DWORD(inbuf, INIT_TXQ_IN_INSTANCE, tx_queue->queue);
  1497. MCDI_POPULATE_DWORD_2(inbuf, INIT_TXQ_IN_FLAGS,
  1498. INIT_TXQ_IN_FLAG_IP_CSUM_DIS, !csum_offload,
  1499. INIT_TXQ_IN_FLAG_TCP_CSUM_DIS, !csum_offload);
  1500. MCDI_SET_DWORD(inbuf, INIT_TXQ_IN_OWNER_ID, 0);
  1501. MCDI_SET_DWORD(inbuf, INIT_TXQ_IN_PORT_ID, nic_data->vport_id);
  1502. dma_addr = tx_queue->txd.buf.dma_addr;
  1503. netif_dbg(efx, hw, efx->net_dev, "pushing TXQ %d. %zu entries (%llx)\n",
  1504. tx_queue->queue, entries, (u64)dma_addr);
  1505. for (i = 0; i < entries; ++i) {
  1506. MCDI_SET_ARRAY_QWORD(inbuf, INIT_TXQ_IN_DMA_ADDR, i, dma_addr);
  1507. dma_addr += EFX_BUF_SIZE;
  1508. }
  1509. inlen = MC_CMD_INIT_TXQ_IN_LEN(entries);
  1510. rc = efx_mcdi_rpc(efx, MC_CMD_INIT_TXQ, inbuf, inlen,
  1511. NULL, 0, NULL);
  1512. if (rc)
  1513. goto fail;
  1514. /* A previous user of this TX queue might have set us up the
  1515. * bomb by writing a descriptor to the TX push collector but
  1516. * not the doorbell. (Each collector belongs to a port, not a
  1517. * queue or function, so cannot easily be reset.) We must
  1518. * attempt to push a no-op descriptor in its place.
  1519. */
  1520. tx_queue->buffer[0].flags = EFX_TX_BUF_OPTION;
  1521. tx_queue->insert_count = 1;
  1522. txd = efx_tx_desc(tx_queue, 0);
  1523. EFX_POPULATE_QWORD_4(*txd,
  1524. ESF_DZ_TX_DESC_IS_OPT, true,
  1525. ESF_DZ_TX_OPTION_TYPE,
  1526. ESE_DZ_TX_OPTION_DESC_CRC_CSUM,
  1527. ESF_DZ_TX_OPTION_UDP_TCP_CSUM, csum_offload,
  1528. ESF_DZ_TX_OPTION_IP_CSUM, csum_offload);
  1529. tx_queue->write_count = 1;
  1530. wmb();
  1531. efx_ef10_push_tx_desc(tx_queue, txd);
  1532. return;
  1533. fail:
  1534. netdev_WARN(efx->net_dev, "failed to initialise TXQ %d\n",
  1535. tx_queue->queue);
  1536. }
  1537. static void efx_ef10_tx_fini(struct efx_tx_queue *tx_queue)
  1538. {
  1539. MCDI_DECLARE_BUF(inbuf, MC_CMD_FINI_TXQ_IN_LEN);
  1540. MCDI_DECLARE_BUF_ERR(outbuf);
  1541. struct efx_nic *efx = tx_queue->efx;
  1542. size_t outlen;
  1543. int rc;
  1544. MCDI_SET_DWORD(inbuf, FINI_TXQ_IN_INSTANCE,
  1545. tx_queue->queue);
  1546. rc = efx_mcdi_rpc_quiet(efx, MC_CMD_FINI_TXQ, inbuf, sizeof(inbuf),
  1547. outbuf, sizeof(outbuf), &outlen);
  1548. if (rc && rc != -EALREADY)
  1549. goto fail;
  1550. return;
  1551. fail:
  1552. efx_mcdi_display_error(efx, MC_CMD_FINI_TXQ, MC_CMD_FINI_TXQ_IN_LEN,
  1553. outbuf, outlen, rc);
  1554. }
  1555. static void efx_ef10_tx_remove(struct efx_tx_queue *tx_queue)
  1556. {
  1557. efx_nic_free_buffer(tx_queue->efx, &tx_queue->txd.buf);
  1558. }
  1559. /* This writes to the TX_DESC_WPTR; write pointer for TX descriptor ring */
  1560. static inline void efx_ef10_notify_tx_desc(struct efx_tx_queue *tx_queue)
  1561. {
  1562. unsigned int write_ptr;
  1563. efx_dword_t reg;
  1564. write_ptr = tx_queue->write_count & tx_queue->ptr_mask;
  1565. EFX_POPULATE_DWORD_1(reg, ERF_DZ_TX_DESC_WPTR_DWORD, write_ptr);
  1566. efx_writed_page(tx_queue->efx, &reg,
  1567. ER_DZ_TX_DESC_UPD_DWORD, tx_queue->queue);
  1568. }
  1569. static void efx_ef10_tx_write(struct efx_tx_queue *tx_queue)
  1570. {
  1571. unsigned int old_write_count = tx_queue->write_count;
  1572. struct efx_tx_buffer *buffer;
  1573. unsigned int write_ptr;
  1574. efx_qword_t *txd;
  1575. BUG_ON(tx_queue->write_count == tx_queue->insert_count);
  1576. do {
  1577. write_ptr = tx_queue->write_count & tx_queue->ptr_mask;
  1578. buffer = &tx_queue->buffer[write_ptr];
  1579. txd = efx_tx_desc(tx_queue, write_ptr);
  1580. ++tx_queue->write_count;
  1581. /* Create TX descriptor ring entry */
  1582. if (buffer->flags & EFX_TX_BUF_OPTION) {
  1583. *txd = buffer->option;
  1584. } else {
  1585. BUILD_BUG_ON(EFX_TX_BUF_CONT != 1);
  1586. EFX_POPULATE_QWORD_3(
  1587. *txd,
  1588. ESF_DZ_TX_KER_CONT,
  1589. buffer->flags & EFX_TX_BUF_CONT,
  1590. ESF_DZ_TX_KER_BYTE_CNT, buffer->len,
  1591. ESF_DZ_TX_KER_BUF_ADDR, buffer->dma_addr);
  1592. }
  1593. } while (tx_queue->write_count != tx_queue->insert_count);
  1594. wmb(); /* Ensure descriptors are written before they are fetched */
  1595. if (efx_nic_may_push_tx_desc(tx_queue, old_write_count)) {
  1596. txd = efx_tx_desc(tx_queue,
  1597. old_write_count & tx_queue->ptr_mask);
  1598. efx_ef10_push_tx_desc(tx_queue, txd);
  1599. ++tx_queue->pushes;
  1600. } else {
  1601. efx_ef10_notify_tx_desc(tx_queue);
  1602. }
  1603. }
  1604. static int efx_ef10_alloc_rss_context(struct efx_nic *efx, u32 *context,
  1605. bool exclusive, unsigned *context_size)
  1606. {
  1607. MCDI_DECLARE_BUF(inbuf, MC_CMD_RSS_CONTEXT_ALLOC_IN_LEN);
  1608. MCDI_DECLARE_BUF(outbuf, MC_CMD_RSS_CONTEXT_ALLOC_OUT_LEN);
  1609. struct efx_ef10_nic_data *nic_data = efx->nic_data;
  1610. size_t outlen;
  1611. int rc;
  1612. u32 alloc_type = exclusive ?
  1613. MC_CMD_RSS_CONTEXT_ALLOC_IN_TYPE_EXCLUSIVE :
  1614. MC_CMD_RSS_CONTEXT_ALLOC_IN_TYPE_SHARED;
  1615. unsigned rss_spread = exclusive ?
  1616. efx->rss_spread :
  1617. min(rounddown_pow_of_two(efx->rss_spread),
  1618. EFX_EF10_MAX_SHARED_RSS_CONTEXT_SIZE);
  1619. if (!exclusive && rss_spread == 1) {
  1620. *context = EFX_EF10_RSS_CONTEXT_INVALID;
  1621. if (context_size)
  1622. *context_size = 1;
  1623. return 0;
  1624. }
  1625. MCDI_SET_DWORD(inbuf, RSS_CONTEXT_ALLOC_IN_UPSTREAM_PORT_ID,
  1626. nic_data->vport_id);
  1627. MCDI_SET_DWORD(inbuf, RSS_CONTEXT_ALLOC_IN_TYPE, alloc_type);
  1628. MCDI_SET_DWORD(inbuf, RSS_CONTEXT_ALLOC_IN_NUM_QUEUES, rss_spread);
  1629. rc = efx_mcdi_rpc(efx, MC_CMD_RSS_CONTEXT_ALLOC, inbuf, sizeof(inbuf),
  1630. outbuf, sizeof(outbuf), &outlen);
  1631. if (rc != 0)
  1632. return rc;
  1633. if (outlen < MC_CMD_RSS_CONTEXT_ALLOC_OUT_LEN)
  1634. return -EIO;
  1635. *context = MCDI_DWORD(outbuf, RSS_CONTEXT_ALLOC_OUT_RSS_CONTEXT_ID);
  1636. if (context_size)
  1637. *context_size = rss_spread;
  1638. return 0;
  1639. }
  1640. static void efx_ef10_free_rss_context(struct efx_nic *efx, u32 context)
  1641. {
  1642. MCDI_DECLARE_BUF(inbuf, MC_CMD_RSS_CONTEXT_FREE_IN_LEN);
  1643. int rc;
  1644. MCDI_SET_DWORD(inbuf, RSS_CONTEXT_FREE_IN_RSS_CONTEXT_ID,
  1645. context);
  1646. rc = efx_mcdi_rpc(efx, MC_CMD_RSS_CONTEXT_FREE, inbuf, sizeof(inbuf),
  1647. NULL, 0, NULL);
  1648. WARN_ON(rc != 0);
  1649. }
  1650. static int efx_ef10_populate_rss_table(struct efx_nic *efx, u32 context,
  1651. const u32 *rx_indir_table)
  1652. {
  1653. MCDI_DECLARE_BUF(tablebuf, MC_CMD_RSS_CONTEXT_SET_TABLE_IN_LEN);
  1654. MCDI_DECLARE_BUF(keybuf, MC_CMD_RSS_CONTEXT_SET_KEY_IN_LEN);
  1655. int i, rc;
  1656. MCDI_SET_DWORD(tablebuf, RSS_CONTEXT_SET_TABLE_IN_RSS_CONTEXT_ID,
  1657. context);
  1658. BUILD_BUG_ON(ARRAY_SIZE(efx->rx_indir_table) !=
  1659. MC_CMD_RSS_CONTEXT_SET_TABLE_IN_INDIRECTION_TABLE_LEN);
  1660. for (i = 0; i < ARRAY_SIZE(efx->rx_indir_table); ++i)
  1661. MCDI_PTR(tablebuf,
  1662. RSS_CONTEXT_SET_TABLE_IN_INDIRECTION_TABLE)[i] =
  1663. (u8) rx_indir_table[i];
  1664. rc = efx_mcdi_rpc(efx, MC_CMD_RSS_CONTEXT_SET_TABLE, tablebuf,
  1665. sizeof(tablebuf), NULL, 0, NULL);
  1666. if (rc != 0)
  1667. return rc;
  1668. MCDI_SET_DWORD(keybuf, RSS_CONTEXT_SET_KEY_IN_RSS_CONTEXT_ID,
  1669. context);
  1670. BUILD_BUG_ON(ARRAY_SIZE(efx->rx_hash_key) !=
  1671. MC_CMD_RSS_CONTEXT_SET_KEY_IN_TOEPLITZ_KEY_LEN);
  1672. for (i = 0; i < ARRAY_SIZE(efx->rx_hash_key); ++i)
  1673. MCDI_PTR(keybuf, RSS_CONTEXT_SET_KEY_IN_TOEPLITZ_KEY)[i] =
  1674. efx->rx_hash_key[i];
  1675. return efx_mcdi_rpc(efx, MC_CMD_RSS_CONTEXT_SET_KEY, keybuf,
  1676. sizeof(keybuf), NULL, 0, NULL);
  1677. }
  1678. static void efx_ef10_rx_free_indir_table(struct efx_nic *efx)
  1679. {
  1680. struct efx_ef10_nic_data *nic_data = efx->nic_data;
  1681. if (nic_data->rx_rss_context != EFX_EF10_RSS_CONTEXT_INVALID)
  1682. efx_ef10_free_rss_context(efx, nic_data->rx_rss_context);
  1683. nic_data->rx_rss_context = EFX_EF10_RSS_CONTEXT_INVALID;
  1684. }
  1685. static int efx_ef10_rx_push_shared_rss_config(struct efx_nic *efx,
  1686. unsigned *context_size)
  1687. {
  1688. u32 new_rx_rss_context;
  1689. struct efx_ef10_nic_data *nic_data = efx->nic_data;
  1690. int rc = efx_ef10_alloc_rss_context(efx, &new_rx_rss_context,
  1691. false, context_size);
  1692. if (rc != 0)
  1693. return rc;
  1694. nic_data->rx_rss_context = new_rx_rss_context;
  1695. nic_data->rx_rss_context_exclusive = false;
  1696. efx_set_default_rx_indir_table(efx);
  1697. return 0;
  1698. }
  1699. static int efx_ef10_rx_push_exclusive_rss_config(struct efx_nic *efx,
  1700. const u32 *rx_indir_table)
  1701. {
  1702. struct efx_ef10_nic_data *nic_data = efx->nic_data;
  1703. int rc;
  1704. u32 new_rx_rss_context;
  1705. if (nic_data->rx_rss_context == EFX_EF10_RSS_CONTEXT_INVALID ||
  1706. !nic_data->rx_rss_context_exclusive) {
  1707. rc = efx_ef10_alloc_rss_context(efx, &new_rx_rss_context,
  1708. true, NULL);
  1709. if (rc == -EOPNOTSUPP)
  1710. return rc;
  1711. else if (rc != 0)
  1712. goto fail1;
  1713. } else {
  1714. new_rx_rss_context = nic_data->rx_rss_context;
  1715. }
  1716. rc = efx_ef10_populate_rss_table(efx, new_rx_rss_context,
  1717. rx_indir_table);
  1718. if (rc != 0)
  1719. goto fail2;
  1720. if (nic_data->rx_rss_context != new_rx_rss_context)
  1721. efx_ef10_rx_free_indir_table(efx);
  1722. nic_data->rx_rss_context = new_rx_rss_context;
  1723. nic_data->rx_rss_context_exclusive = true;
  1724. if (rx_indir_table != efx->rx_indir_table)
  1725. memcpy(efx->rx_indir_table, rx_indir_table,
  1726. sizeof(efx->rx_indir_table));
  1727. return 0;
  1728. fail2:
  1729. if (new_rx_rss_context != nic_data->rx_rss_context)
  1730. efx_ef10_free_rss_context(efx, new_rx_rss_context);
  1731. fail1:
  1732. netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
  1733. return rc;
  1734. }
  1735. static int efx_ef10_pf_rx_push_rss_config(struct efx_nic *efx, bool user,
  1736. const u32 *rx_indir_table)
  1737. {
  1738. int rc;
  1739. if (efx->rss_spread == 1)
  1740. return 0;
  1741. rc = efx_ef10_rx_push_exclusive_rss_config(efx, rx_indir_table);
  1742. if (rc == -ENOBUFS && !user) {
  1743. unsigned context_size;
  1744. bool mismatch = false;
  1745. size_t i;
  1746. for (i = 0; i < ARRAY_SIZE(efx->rx_indir_table) && !mismatch;
  1747. i++)
  1748. mismatch = rx_indir_table[i] !=
  1749. ethtool_rxfh_indir_default(i, efx->rss_spread);
  1750. rc = efx_ef10_rx_push_shared_rss_config(efx, &context_size);
  1751. if (rc == 0) {
  1752. if (context_size != efx->rss_spread)
  1753. netif_warn(efx, probe, efx->net_dev,
  1754. "Could not allocate an exclusive RSS"
  1755. " context; allocated a shared one of"
  1756. " different size."
  1757. " Wanted %u, got %u.\n",
  1758. efx->rss_spread, context_size);
  1759. else if (mismatch)
  1760. netif_warn(efx, probe, efx->net_dev,
  1761. "Could not allocate an exclusive RSS"
  1762. " context; allocated a shared one but"
  1763. " could not apply custom"
  1764. " indirection.\n");
  1765. else
  1766. netif_info(efx, probe, efx->net_dev,
  1767. "Could not allocate an exclusive RSS"
  1768. " context; allocated a shared one.\n");
  1769. }
  1770. }
  1771. return rc;
  1772. }
  1773. static int efx_ef10_vf_rx_push_rss_config(struct efx_nic *efx, bool user,
  1774. const u32 *rx_indir_table
  1775. __attribute__ ((unused)))
  1776. {
  1777. struct efx_ef10_nic_data *nic_data = efx->nic_data;
  1778. if (user)
  1779. return -EOPNOTSUPP;
  1780. if (nic_data->rx_rss_context != EFX_EF10_RSS_CONTEXT_INVALID)
  1781. return 0;
  1782. return efx_ef10_rx_push_shared_rss_config(efx, NULL);
  1783. }
  1784. static int efx_ef10_rx_probe(struct efx_rx_queue *rx_queue)
  1785. {
  1786. return efx_nic_alloc_buffer(rx_queue->efx, &rx_queue->rxd.buf,
  1787. (rx_queue->ptr_mask + 1) *
  1788. sizeof(efx_qword_t),
  1789. GFP_KERNEL);
  1790. }
  1791. static void efx_ef10_rx_init(struct efx_rx_queue *rx_queue)
  1792. {
  1793. MCDI_DECLARE_BUF(inbuf,
  1794. MC_CMD_INIT_RXQ_IN_LEN(EFX_MAX_DMAQ_SIZE * 8 /
  1795. EFX_BUF_SIZE));
  1796. struct efx_channel *channel = efx_rx_queue_channel(rx_queue);
  1797. size_t entries = rx_queue->rxd.buf.len / EFX_BUF_SIZE;
  1798. struct efx_nic *efx = rx_queue->efx;
  1799. struct efx_ef10_nic_data *nic_data = efx->nic_data;
  1800. size_t inlen;
  1801. dma_addr_t dma_addr;
  1802. int rc;
  1803. int i;
  1804. BUILD_BUG_ON(MC_CMD_INIT_RXQ_OUT_LEN != 0);
  1805. rx_queue->scatter_n = 0;
  1806. rx_queue->scatter_len = 0;
  1807. MCDI_SET_DWORD(inbuf, INIT_RXQ_IN_SIZE, rx_queue->ptr_mask + 1);
  1808. MCDI_SET_DWORD(inbuf, INIT_RXQ_IN_TARGET_EVQ, channel->channel);
  1809. MCDI_SET_DWORD(inbuf, INIT_RXQ_IN_LABEL, efx_rx_queue_index(rx_queue));
  1810. MCDI_SET_DWORD(inbuf, INIT_RXQ_IN_INSTANCE,
  1811. efx_rx_queue_index(rx_queue));
  1812. MCDI_POPULATE_DWORD_2(inbuf, INIT_RXQ_IN_FLAGS,
  1813. INIT_RXQ_IN_FLAG_PREFIX, 1,
  1814. INIT_RXQ_IN_FLAG_TIMESTAMP, 1);
  1815. MCDI_SET_DWORD(inbuf, INIT_RXQ_IN_OWNER_ID, 0);
  1816. MCDI_SET_DWORD(inbuf, INIT_RXQ_IN_PORT_ID, nic_data->vport_id);
  1817. dma_addr = rx_queue->rxd.buf.dma_addr;
  1818. netif_dbg(efx, hw, efx->net_dev, "pushing RXQ %d. %zu entries (%llx)\n",
  1819. efx_rx_queue_index(rx_queue), entries, (u64)dma_addr);
  1820. for (i = 0; i < entries; ++i) {
  1821. MCDI_SET_ARRAY_QWORD(inbuf, INIT_RXQ_IN_DMA_ADDR, i, dma_addr);
  1822. dma_addr += EFX_BUF_SIZE;
  1823. }
  1824. inlen = MC_CMD_INIT_RXQ_IN_LEN(entries);
  1825. rc = efx_mcdi_rpc(efx, MC_CMD_INIT_RXQ, inbuf, inlen,
  1826. NULL, 0, NULL);
  1827. if (rc)
  1828. netdev_WARN(efx->net_dev, "failed to initialise RXQ %d\n",
  1829. efx_rx_queue_index(rx_queue));
  1830. }
  1831. static void efx_ef10_rx_fini(struct efx_rx_queue *rx_queue)
  1832. {
  1833. MCDI_DECLARE_BUF(inbuf, MC_CMD_FINI_RXQ_IN_LEN);
  1834. MCDI_DECLARE_BUF_ERR(outbuf);
  1835. struct efx_nic *efx = rx_queue->efx;
  1836. size_t outlen;
  1837. int rc;
  1838. MCDI_SET_DWORD(inbuf, FINI_RXQ_IN_INSTANCE,
  1839. efx_rx_queue_index(rx_queue));
  1840. rc = efx_mcdi_rpc_quiet(efx, MC_CMD_FINI_RXQ, inbuf, sizeof(inbuf),
  1841. outbuf, sizeof(outbuf), &outlen);
  1842. if (rc && rc != -EALREADY)
  1843. goto fail;
  1844. return;
  1845. fail:
  1846. efx_mcdi_display_error(efx, MC_CMD_FINI_RXQ, MC_CMD_FINI_RXQ_IN_LEN,
  1847. outbuf, outlen, rc);
  1848. }
  1849. static void efx_ef10_rx_remove(struct efx_rx_queue *rx_queue)
  1850. {
  1851. efx_nic_free_buffer(rx_queue->efx, &rx_queue->rxd.buf);
  1852. }
  1853. /* This creates an entry in the RX descriptor queue */
  1854. static inline void
  1855. efx_ef10_build_rx_desc(struct efx_rx_queue *rx_queue, unsigned int index)
  1856. {
  1857. struct efx_rx_buffer *rx_buf;
  1858. efx_qword_t *rxd;
  1859. rxd = efx_rx_desc(rx_queue, index);
  1860. rx_buf = efx_rx_buffer(rx_queue, index);
  1861. EFX_POPULATE_QWORD_2(*rxd,
  1862. ESF_DZ_RX_KER_BYTE_CNT, rx_buf->len,
  1863. ESF_DZ_RX_KER_BUF_ADDR, rx_buf->dma_addr);
  1864. }
  1865. static void efx_ef10_rx_write(struct efx_rx_queue *rx_queue)
  1866. {
  1867. struct efx_nic *efx = rx_queue->efx;
  1868. unsigned int write_count;
  1869. efx_dword_t reg;
  1870. /* Firmware requires that RX_DESC_WPTR be a multiple of 8 */
  1871. write_count = rx_queue->added_count & ~7;
  1872. if (rx_queue->notified_count == write_count)
  1873. return;
  1874. do
  1875. efx_ef10_build_rx_desc(
  1876. rx_queue,
  1877. rx_queue->notified_count & rx_queue->ptr_mask);
  1878. while (++rx_queue->notified_count != write_count);
  1879. wmb();
  1880. EFX_POPULATE_DWORD_1(reg, ERF_DZ_RX_DESC_WPTR,
  1881. write_count & rx_queue->ptr_mask);
  1882. efx_writed_page(efx, &reg, ER_DZ_RX_DESC_UPD,
  1883. efx_rx_queue_index(rx_queue));
  1884. }
  1885. static efx_mcdi_async_completer efx_ef10_rx_defer_refill_complete;
  1886. static void efx_ef10_rx_defer_refill(struct efx_rx_queue *rx_queue)
  1887. {
  1888. struct efx_channel *channel = efx_rx_queue_channel(rx_queue);
  1889. MCDI_DECLARE_BUF(inbuf, MC_CMD_DRIVER_EVENT_IN_LEN);
  1890. efx_qword_t event;
  1891. EFX_POPULATE_QWORD_2(event,
  1892. ESF_DZ_EV_CODE, EFX_EF10_DRVGEN_EV,
  1893. ESF_DZ_EV_DATA, EFX_EF10_REFILL);
  1894. MCDI_SET_DWORD(inbuf, DRIVER_EVENT_IN_EVQ, channel->channel);
  1895. /* MCDI_SET_QWORD is not appropriate here since EFX_POPULATE_* has
  1896. * already swapped the data to little-endian order.
  1897. */
  1898. memcpy(MCDI_PTR(inbuf, DRIVER_EVENT_IN_DATA), &event.u64[0],
  1899. sizeof(efx_qword_t));
  1900. efx_mcdi_rpc_async(channel->efx, MC_CMD_DRIVER_EVENT,
  1901. inbuf, sizeof(inbuf), 0,
  1902. efx_ef10_rx_defer_refill_complete, 0);
  1903. }
  1904. static void
  1905. efx_ef10_rx_defer_refill_complete(struct efx_nic *efx, unsigned long cookie,
  1906. int rc, efx_dword_t *outbuf,
  1907. size_t outlen_actual)
  1908. {
  1909. /* nothing to do */
  1910. }
  1911. static int efx_ef10_ev_probe(struct efx_channel *channel)
  1912. {
  1913. return efx_nic_alloc_buffer(channel->efx, &channel->eventq.buf,
  1914. (channel->eventq_mask + 1) *
  1915. sizeof(efx_qword_t),
  1916. GFP_KERNEL);
  1917. }
  1918. static void efx_ef10_ev_fini(struct efx_channel *channel)
  1919. {
  1920. MCDI_DECLARE_BUF(inbuf, MC_CMD_FINI_EVQ_IN_LEN);
  1921. MCDI_DECLARE_BUF_ERR(outbuf);
  1922. struct efx_nic *efx = channel->efx;
  1923. size_t outlen;
  1924. int rc;
  1925. MCDI_SET_DWORD(inbuf, FINI_EVQ_IN_INSTANCE, channel->channel);
  1926. rc = efx_mcdi_rpc_quiet(efx, MC_CMD_FINI_EVQ, inbuf, sizeof(inbuf),
  1927. outbuf, sizeof(outbuf), &outlen);
  1928. if (rc && rc != -EALREADY)
  1929. goto fail;
  1930. return;
  1931. fail:
  1932. efx_mcdi_display_error(efx, MC_CMD_FINI_EVQ, MC_CMD_FINI_EVQ_IN_LEN,
  1933. outbuf, outlen, rc);
  1934. }
  1935. static int efx_ef10_ev_init(struct efx_channel *channel)
  1936. {
  1937. MCDI_DECLARE_BUF(inbuf,
  1938. MC_CMD_INIT_EVQ_IN_LEN(EFX_MAX_EVQ_SIZE * 8 /
  1939. EFX_BUF_SIZE));
  1940. MCDI_DECLARE_BUF(outbuf, MC_CMD_INIT_EVQ_OUT_LEN);
  1941. size_t entries = channel->eventq.buf.len / EFX_BUF_SIZE;
  1942. struct efx_nic *efx = channel->efx;
  1943. struct efx_ef10_nic_data *nic_data;
  1944. bool supports_rx_merge;
  1945. size_t inlen, outlen;
  1946. unsigned int enabled, implemented;
  1947. dma_addr_t dma_addr;
  1948. int rc;
  1949. int i;
  1950. nic_data = efx->nic_data;
  1951. supports_rx_merge =
  1952. !!(nic_data->datapath_caps &
  1953. 1 << MC_CMD_GET_CAPABILITIES_OUT_RX_BATCHING_LBN);
  1954. /* Fill event queue with all ones (i.e. empty events) */
  1955. memset(channel->eventq.buf.addr, 0xff, channel->eventq.buf.len);
  1956. MCDI_SET_DWORD(inbuf, INIT_EVQ_IN_SIZE, channel->eventq_mask + 1);
  1957. MCDI_SET_DWORD(inbuf, INIT_EVQ_IN_INSTANCE, channel->channel);
  1958. /* INIT_EVQ expects index in vector table, not absolute */
  1959. MCDI_SET_DWORD(inbuf, INIT_EVQ_IN_IRQ_NUM, channel->channel);
  1960. MCDI_POPULATE_DWORD_4(inbuf, INIT_EVQ_IN_FLAGS,
  1961. INIT_EVQ_IN_FLAG_INTERRUPTING, 1,
  1962. INIT_EVQ_IN_FLAG_RX_MERGE, 1,
  1963. INIT_EVQ_IN_FLAG_TX_MERGE, 1,
  1964. INIT_EVQ_IN_FLAG_CUT_THRU, !supports_rx_merge);
  1965. MCDI_SET_DWORD(inbuf, INIT_EVQ_IN_TMR_MODE,
  1966. MC_CMD_INIT_EVQ_IN_TMR_MODE_DIS);
  1967. MCDI_SET_DWORD(inbuf, INIT_EVQ_IN_TMR_LOAD, 0);
  1968. MCDI_SET_DWORD(inbuf, INIT_EVQ_IN_TMR_RELOAD, 0);
  1969. MCDI_SET_DWORD(inbuf, INIT_EVQ_IN_COUNT_MODE,
  1970. MC_CMD_INIT_EVQ_IN_COUNT_MODE_DIS);
  1971. MCDI_SET_DWORD(inbuf, INIT_EVQ_IN_COUNT_THRSHLD, 0);
  1972. dma_addr = channel->eventq.buf.dma_addr;
  1973. for (i = 0; i < entries; ++i) {
  1974. MCDI_SET_ARRAY_QWORD(inbuf, INIT_EVQ_IN_DMA_ADDR, i, dma_addr);
  1975. dma_addr += EFX_BUF_SIZE;
  1976. }
  1977. inlen = MC_CMD_INIT_EVQ_IN_LEN(entries);
  1978. rc = efx_mcdi_rpc(efx, MC_CMD_INIT_EVQ, inbuf, inlen,
  1979. outbuf, sizeof(outbuf), &outlen);
  1980. /* IRQ return is ignored */
  1981. if (channel->channel || rc)
  1982. return rc;
  1983. /* Successfully created event queue on channel 0 */
  1984. rc = efx_mcdi_get_workarounds(efx, &implemented, &enabled);
  1985. if (rc == -ENOSYS) {
  1986. /* GET_WORKAROUNDS was implemented before the bug26807
  1987. * workaround, thus the latter must be unavailable in this fw
  1988. */
  1989. nic_data->workaround_26807 = false;
  1990. rc = 0;
  1991. } else if (rc) {
  1992. goto fail;
  1993. } else {
  1994. nic_data->workaround_26807 =
  1995. !!(enabled & MC_CMD_GET_WORKAROUNDS_OUT_BUG26807);
  1996. if (implemented & MC_CMD_GET_WORKAROUNDS_OUT_BUG26807 &&
  1997. !nic_data->workaround_26807) {
  1998. unsigned int flags;
  1999. rc = efx_mcdi_set_workaround(efx,
  2000. MC_CMD_WORKAROUND_BUG26807,
  2001. true, &flags);
  2002. if (!rc) {
  2003. if (flags &
  2004. 1 << MC_CMD_WORKAROUND_EXT_OUT_FLR_DONE_LBN) {
  2005. netif_info(efx, drv, efx->net_dev,
  2006. "other functions on NIC have been reset\n");
  2007. /* MC's boot count has incremented */
  2008. ++nic_data->warm_boot_count;
  2009. }
  2010. nic_data->workaround_26807 = true;
  2011. } else if (rc == -EPERM) {
  2012. rc = 0;
  2013. }
  2014. }
  2015. }
  2016. if (!rc)
  2017. return 0;
  2018. fail:
  2019. efx_ef10_ev_fini(channel);
  2020. return rc;
  2021. }
  2022. static void efx_ef10_ev_remove(struct efx_channel *channel)
  2023. {
  2024. efx_nic_free_buffer(channel->efx, &channel->eventq.buf);
  2025. }
  2026. static void efx_ef10_handle_rx_wrong_queue(struct efx_rx_queue *rx_queue,
  2027. unsigned int rx_queue_label)
  2028. {
  2029. struct efx_nic *efx = rx_queue->efx;
  2030. netif_info(efx, hw, efx->net_dev,
  2031. "rx event arrived on queue %d labeled as queue %u\n",
  2032. efx_rx_queue_index(rx_queue), rx_queue_label);
  2033. efx_schedule_reset(efx, RESET_TYPE_DISABLE);
  2034. }
  2035. static void
  2036. efx_ef10_handle_rx_bad_lbits(struct efx_rx_queue *rx_queue,
  2037. unsigned int actual, unsigned int expected)
  2038. {
  2039. unsigned int dropped = (actual - expected) & rx_queue->ptr_mask;
  2040. struct efx_nic *efx = rx_queue->efx;
  2041. netif_info(efx, hw, efx->net_dev,
  2042. "dropped %d events (index=%d expected=%d)\n",
  2043. dropped, actual, expected);
  2044. efx_schedule_reset(efx, RESET_TYPE_DISABLE);
  2045. }
  2046. /* partially received RX was aborted. clean up. */
  2047. static void efx_ef10_handle_rx_abort(struct efx_rx_queue *rx_queue)
  2048. {
  2049. unsigned int rx_desc_ptr;
  2050. netif_dbg(rx_queue->efx, hw, rx_queue->efx->net_dev,
  2051. "scattered RX aborted (dropping %u buffers)\n",
  2052. rx_queue->scatter_n);
  2053. rx_desc_ptr = rx_queue->removed_count & rx_queue->ptr_mask;
  2054. efx_rx_packet(rx_queue, rx_desc_ptr, rx_queue->scatter_n,
  2055. 0, EFX_RX_PKT_DISCARD);
  2056. rx_queue->removed_count += rx_queue->scatter_n;
  2057. rx_queue->scatter_n = 0;
  2058. rx_queue->scatter_len = 0;
  2059. ++efx_rx_queue_channel(rx_queue)->n_rx_nodesc_trunc;
  2060. }
  2061. static int efx_ef10_handle_rx_event(struct efx_channel *channel,
  2062. const efx_qword_t *event)
  2063. {
  2064. unsigned int rx_bytes, next_ptr_lbits, rx_queue_label, rx_l4_class;
  2065. unsigned int n_descs, n_packets, i;
  2066. struct efx_nic *efx = channel->efx;
  2067. struct efx_rx_queue *rx_queue;
  2068. bool rx_cont;
  2069. u16 flags = 0;
  2070. if (unlikely(ACCESS_ONCE(efx->reset_pending)))
  2071. return 0;
  2072. /* Basic packet information */
  2073. rx_bytes = EFX_QWORD_FIELD(*event, ESF_DZ_RX_BYTES);
  2074. next_ptr_lbits = EFX_QWORD_FIELD(*event, ESF_DZ_RX_DSC_PTR_LBITS);
  2075. rx_queue_label = EFX_QWORD_FIELD(*event, ESF_DZ_RX_QLABEL);
  2076. rx_l4_class = EFX_QWORD_FIELD(*event, ESF_DZ_RX_L4_CLASS);
  2077. rx_cont = EFX_QWORD_FIELD(*event, ESF_DZ_RX_CONT);
  2078. if (EFX_QWORD_FIELD(*event, ESF_DZ_RX_DROP_EVENT))
  2079. netdev_WARN(efx->net_dev, "saw RX_DROP_EVENT: event="
  2080. EFX_QWORD_FMT "\n",
  2081. EFX_QWORD_VAL(*event));
  2082. rx_queue = efx_channel_get_rx_queue(channel);
  2083. if (unlikely(rx_queue_label != efx_rx_queue_index(rx_queue)))
  2084. efx_ef10_handle_rx_wrong_queue(rx_queue, rx_queue_label);
  2085. n_descs = ((next_ptr_lbits - rx_queue->removed_count) &
  2086. ((1 << ESF_DZ_RX_DSC_PTR_LBITS_WIDTH) - 1));
  2087. if (n_descs != rx_queue->scatter_n + 1) {
  2088. struct efx_ef10_nic_data *nic_data = efx->nic_data;
  2089. /* detect rx abort */
  2090. if (unlikely(n_descs == rx_queue->scatter_n)) {
  2091. if (rx_queue->scatter_n == 0 || rx_bytes != 0)
  2092. netdev_WARN(efx->net_dev,
  2093. "invalid RX abort: scatter_n=%u event="
  2094. EFX_QWORD_FMT "\n",
  2095. rx_queue->scatter_n,
  2096. EFX_QWORD_VAL(*event));
  2097. efx_ef10_handle_rx_abort(rx_queue);
  2098. return 0;
  2099. }
  2100. /* Check that RX completion merging is valid, i.e.
  2101. * the current firmware supports it and this is a
  2102. * non-scattered packet.
  2103. */
  2104. if (!(nic_data->datapath_caps &
  2105. (1 << MC_CMD_GET_CAPABILITIES_OUT_RX_BATCHING_LBN)) ||
  2106. rx_queue->scatter_n != 0 || rx_cont) {
  2107. efx_ef10_handle_rx_bad_lbits(
  2108. rx_queue, next_ptr_lbits,
  2109. (rx_queue->removed_count +
  2110. rx_queue->scatter_n + 1) &
  2111. ((1 << ESF_DZ_RX_DSC_PTR_LBITS_WIDTH) - 1));
  2112. return 0;
  2113. }
  2114. /* Merged completion for multiple non-scattered packets */
  2115. rx_queue->scatter_n = 1;
  2116. rx_queue->scatter_len = 0;
  2117. n_packets = n_descs;
  2118. ++channel->n_rx_merge_events;
  2119. channel->n_rx_merge_packets += n_packets;
  2120. flags |= EFX_RX_PKT_PREFIX_LEN;
  2121. } else {
  2122. ++rx_queue->scatter_n;
  2123. rx_queue->scatter_len += rx_bytes;
  2124. if (rx_cont)
  2125. return 0;
  2126. n_packets = 1;
  2127. }
  2128. if (unlikely(EFX_QWORD_FIELD(*event, ESF_DZ_RX_ECRC_ERR)))
  2129. flags |= EFX_RX_PKT_DISCARD;
  2130. if (unlikely(EFX_QWORD_FIELD(*event, ESF_DZ_RX_IPCKSUM_ERR))) {
  2131. channel->n_rx_ip_hdr_chksum_err += n_packets;
  2132. } else if (unlikely(EFX_QWORD_FIELD(*event,
  2133. ESF_DZ_RX_TCPUDP_CKSUM_ERR))) {
  2134. channel->n_rx_tcp_udp_chksum_err += n_packets;
  2135. } else if (rx_l4_class == ESE_DZ_L4_CLASS_TCP ||
  2136. rx_l4_class == ESE_DZ_L4_CLASS_UDP) {
  2137. flags |= EFX_RX_PKT_CSUMMED;
  2138. }
  2139. if (rx_l4_class == ESE_DZ_L4_CLASS_TCP)
  2140. flags |= EFX_RX_PKT_TCP;
  2141. channel->irq_mod_score += 2 * n_packets;
  2142. /* Handle received packet(s) */
  2143. for (i = 0; i < n_packets; i++) {
  2144. efx_rx_packet(rx_queue,
  2145. rx_queue->removed_count & rx_queue->ptr_mask,
  2146. rx_queue->scatter_n, rx_queue->scatter_len,
  2147. flags);
  2148. rx_queue->removed_count += rx_queue->scatter_n;
  2149. }
  2150. rx_queue->scatter_n = 0;
  2151. rx_queue->scatter_len = 0;
  2152. return n_packets;
  2153. }
  2154. static int
  2155. efx_ef10_handle_tx_event(struct efx_channel *channel, efx_qword_t *event)
  2156. {
  2157. struct efx_nic *efx = channel->efx;
  2158. struct efx_tx_queue *tx_queue;
  2159. unsigned int tx_ev_desc_ptr;
  2160. unsigned int tx_ev_q_label;
  2161. int tx_descs = 0;
  2162. if (unlikely(ACCESS_ONCE(efx->reset_pending)))
  2163. return 0;
  2164. if (unlikely(EFX_QWORD_FIELD(*event, ESF_DZ_TX_DROP_EVENT)))
  2165. return 0;
  2166. /* Transmit completion */
  2167. tx_ev_desc_ptr = EFX_QWORD_FIELD(*event, ESF_DZ_TX_DESCR_INDX);
  2168. tx_ev_q_label = EFX_QWORD_FIELD(*event, ESF_DZ_TX_QLABEL);
  2169. tx_queue = efx_channel_get_tx_queue(channel,
  2170. tx_ev_q_label % EFX_TXQ_TYPES);
  2171. tx_descs = ((tx_ev_desc_ptr + 1 - tx_queue->read_count) &
  2172. tx_queue->ptr_mask);
  2173. efx_xmit_done(tx_queue, tx_ev_desc_ptr & tx_queue->ptr_mask);
  2174. return tx_descs;
  2175. }
  2176. static void
  2177. efx_ef10_handle_driver_event(struct efx_channel *channel, efx_qword_t *event)
  2178. {
  2179. struct efx_nic *efx = channel->efx;
  2180. int subcode;
  2181. subcode = EFX_QWORD_FIELD(*event, ESF_DZ_DRV_SUB_CODE);
  2182. switch (subcode) {
  2183. case ESE_DZ_DRV_TIMER_EV:
  2184. case ESE_DZ_DRV_WAKE_UP_EV:
  2185. break;
  2186. case ESE_DZ_DRV_START_UP_EV:
  2187. /* event queue init complete. ok. */
  2188. break;
  2189. default:
  2190. netif_err(efx, hw, efx->net_dev,
  2191. "channel %d unknown driver event type %d"
  2192. " (data " EFX_QWORD_FMT ")\n",
  2193. channel->channel, subcode,
  2194. EFX_QWORD_VAL(*event));
  2195. }
  2196. }
  2197. static void efx_ef10_handle_driver_generated_event(struct efx_channel *channel,
  2198. efx_qword_t *event)
  2199. {
  2200. struct efx_nic *efx = channel->efx;
  2201. u32 subcode;
  2202. subcode = EFX_QWORD_FIELD(*event, EFX_DWORD_0);
  2203. switch (subcode) {
  2204. case EFX_EF10_TEST:
  2205. channel->event_test_cpu = raw_smp_processor_id();
  2206. break;
  2207. case EFX_EF10_REFILL:
  2208. /* The queue must be empty, so we won't receive any rx
  2209. * events, so efx_process_channel() won't refill the
  2210. * queue. Refill it here
  2211. */
  2212. efx_fast_push_rx_descriptors(&channel->rx_queue, true);
  2213. break;
  2214. default:
  2215. netif_err(efx, hw, efx->net_dev,
  2216. "channel %d unknown driver event type %u"
  2217. " (data " EFX_QWORD_FMT ")\n",
  2218. channel->channel, (unsigned) subcode,
  2219. EFX_QWORD_VAL(*event));
  2220. }
  2221. }
  2222. static int efx_ef10_ev_process(struct efx_channel *channel, int quota)
  2223. {
  2224. struct efx_nic *efx = channel->efx;
  2225. efx_qword_t event, *p_event;
  2226. unsigned int read_ptr;
  2227. int ev_code;
  2228. int tx_descs = 0;
  2229. int spent = 0;
  2230. if (quota <= 0)
  2231. return spent;
  2232. read_ptr = channel->eventq_read_ptr;
  2233. for (;;) {
  2234. p_event = efx_event(channel, read_ptr);
  2235. event = *p_event;
  2236. if (!efx_event_present(&event))
  2237. break;
  2238. EFX_SET_QWORD(*p_event);
  2239. ++read_ptr;
  2240. ev_code = EFX_QWORD_FIELD(event, ESF_DZ_EV_CODE);
  2241. netif_vdbg(efx, drv, efx->net_dev,
  2242. "processing event on %d " EFX_QWORD_FMT "\n",
  2243. channel->channel, EFX_QWORD_VAL(event));
  2244. switch (ev_code) {
  2245. case ESE_DZ_EV_CODE_MCDI_EV:
  2246. efx_mcdi_process_event(channel, &event);
  2247. break;
  2248. case ESE_DZ_EV_CODE_RX_EV:
  2249. spent += efx_ef10_handle_rx_event(channel, &event);
  2250. if (spent >= quota) {
  2251. /* XXX can we split a merged event to
  2252. * avoid going over-quota?
  2253. */
  2254. spent = quota;
  2255. goto out;
  2256. }
  2257. break;
  2258. case ESE_DZ_EV_CODE_TX_EV:
  2259. tx_descs += efx_ef10_handle_tx_event(channel, &event);
  2260. if (tx_descs > efx->txq_entries) {
  2261. spent = quota;
  2262. goto out;
  2263. } else if (++spent == quota) {
  2264. goto out;
  2265. }
  2266. break;
  2267. case ESE_DZ_EV_CODE_DRIVER_EV:
  2268. efx_ef10_handle_driver_event(channel, &event);
  2269. if (++spent == quota)
  2270. goto out;
  2271. break;
  2272. case EFX_EF10_DRVGEN_EV:
  2273. efx_ef10_handle_driver_generated_event(channel, &event);
  2274. break;
  2275. default:
  2276. netif_err(efx, hw, efx->net_dev,
  2277. "channel %d unknown event type %d"
  2278. " (data " EFX_QWORD_FMT ")\n",
  2279. channel->channel, ev_code,
  2280. EFX_QWORD_VAL(event));
  2281. }
  2282. }
  2283. out:
  2284. channel->eventq_read_ptr = read_ptr;
  2285. return spent;
  2286. }
  2287. static void efx_ef10_ev_read_ack(struct efx_channel *channel)
  2288. {
  2289. struct efx_nic *efx = channel->efx;
  2290. efx_dword_t rptr;
  2291. if (EFX_EF10_WORKAROUND_35388(efx)) {
  2292. BUILD_BUG_ON(EFX_MIN_EVQ_SIZE <
  2293. (1 << ERF_DD_EVQ_IND_RPTR_WIDTH));
  2294. BUILD_BUG_ON(EFX_MAX_EVQ_SIZE >
  2295. (1 << 2 * ERF_DD_EVQ_IND_RPTR_WIDTH));
  2296. EFX_POPULATE_DWORD_2(rptr, ERF_DD_EVQ_IND_RPTR_FLAGS,
  2297. EFE_DD_EVQ_IND_RPTR_FLAGS_HIGH,
  2298. ERF_DD_EVQ_IND_RPTR,
  2299. (channel->eventq_read_ptr &
  2300. channel->eventq_mask) >>
  2301. ERF_DD_EVQ_IND_RPTR_WIDTH);
  2302. efx_writed_page(efx, &rptr, ER_DD_EVQ_INDIRECT,
  2303. channel->channel);
  2304. EFX_POPULATE_DWORD_2(rptr, ERF_DD_EVQ_IND_RPTR_FLAGS,
  2305. EFE_DD_EVQ_IND_RPTR_FLAGS_LOW,
  2306. ERF_DD_EVQ_IND_RPTR,
  2307. channel->eventq_read_ptr &
  2308. ((1 << ERF_DD_EVQ_IND_RPTR_WIDTH) - 1));
  2309. efx_writed_page(efx, &rptr, ER_DD_EVQ_INDIRECT,
  2310. channel->channel);
  2311. } else {
  2312. EFX_POPULATE_DWORD_1(rptr, ERF_DZ_EVQ_RPTR,
  2313. channel->eventq_read_ptr &
  2314. channel->eventq_mask);
  2315. efx_writed_page(efx, &rptr, ER_DZ_EVQ_RPTR, channel->channel);
  2316. }
  2317. }
  2318. static void efx_ef10_ev_test_generate(struct efx_channel *channel)
  2319. {
  2320. MCDI_DECLARE_BUF(inbuf, MC_CMD_DRIVER_EVENT_IN_LEN);
  2321. struct efx_nic *efx = channel->efx;
  2322. efx_qword_t event;
  2323. int rc;
  2324. EFX_POPULATE_QWORD_2(event,
  2325. ESF_DZ_EV_CODE, EFX_EF10_DRVGEN_EV,
  2326. ESF_DZ_EV_DATA, EFX_EF10_TEST);
  2327. MCDI_SET_DWORD(inbuf, DRIVER_EVENT_IN_EVQ, channel->channel);
  2328. /* MCDI_SET_QWORD is not appropriate here since EFX_POPULATE_* has
  2329. * already swapped the data to little-endian order.
  2330. */
  2331. memcpy(MCDI_PTR(inbuf, DRIVER_EVENT_IN_DATA), &event.u64[0],
  2332. sizeof(efx_qword_t));
  2333. rc = efx_mcdi_rpc(efx, MC_CMD_DRIVER_EVENT, inbuf, sizeof(inbuf),
  2334. NULL, 0, NULL);
  2335. if (rc != 0)
  2336. goto fail;
  2337. return;
  2338. fail:
  2339. WARN_ON(true);
  2340. netif_err(efx, hw, efx->net_dev, "%s: failed rc=%d\n", __func__, rc);
  2341. }
  2342. void efx_ef10_handle_drain_event(struct efx_nic *efx)
  2343. {
  2344. if (atomic_dec_and_test(&efx->active_queues))
  2345. wake_up(&efx->flush_wq);
  2346. WARN_ON(atomic_read(&efx->active_queues) < 0);
  2347. }
  2348. static int efx_ef10_fini_dmaq(struct efx_nic *efx)
  2349. {
  2350. struct efx_ef10_nic_data *nic_data = efx->nic_data;
  2351. struct efx_channel *channel;
  2352. struct efx_tx_queue *tx_queue;
  2353. struct efx_rx_queue *rx_queue;
  2354. int pending;
  2355. /* If the MC has just rebooted, the TX/RX queues will have already been
  2356. * torn down, but efx->active_queues needs to be set to zero.
  2357. */
  2358. if (nic_data->must_realloc_vis) {
  2359. atomic_set(&efx->active_queues, 0);
  2360. return 0;
  2361. }
  2362. /* Do not attempt to write to the NIC during EEH recovery */
  2363. if (efx->state != STATE_RECOVERY) {
  2364. efx_for_each_channel(channel, efx) {
  2365. efx_for_each_channel_rx_queue(rx_queue, channel)
  2366. efx_ef10_rx_fini(rx_queue);
  2367. efx_for_each_channel_tx_queue(tx_queue, channel)
  2368. efx_ef10_tx_fini(tx_queue);
  2369. }
  2370. wait_event_timeout(efx->flush_wq,
  2371. atomic_read(&efx->active_queues) == 0,
  2372. msecs_to_jiffies(EFX_MAX_FLUSH_TIME));
  2373. pending = atomic_read(&efx->active_queues);
  2374. if (pending) {
  2375. netif_err(efx, hw, efx->net_dev, "failed to flush %d queues\n",
  2376. pending);
  2377. return -ETIMEDOUT;
  2378. }
  2379. }
  2380. return 0;
  2381. }
  2382. static void efx_ef10_prepare_flr(struct efx_nic *efx)
  2383. {
  2384. atomic_set(&efx->active_queues, 0);
  2385. }
  2386. static bool efx_ef10_filter_equal(const struct efx_filter_spec *left,
  2387. const struct efx_filter_spec *right)
  2388. {
  2389. if ((left->match_flags ^ right->match_flags) |
  2390. ((left->flags ^ right->flags) &
  2391. (EFX_FILTER_FLAG_RX | EFX_FILTER_FLAG_TX)))
  2392. return false;
  2393. return memcmp(&left->outer_vid, &right->outer_vid,
  2394. sizeof(struct efx_filter_spec) -
  2395. offsetof(struct efx_filter_spec, outer_vid)) == 0;
  2396. }
  2397. static unsigned int efx_ef10_filter_hash(const struct efx_filter_spec *spec)
  2398. {
  2399. BUILD_BUG_ON(offsetof(struct efx_filter_spec, outer_vid) & 3);
  2400. return jhash2((const u32 *)&spec->outer_vid,
  2401. (sizeof(struct efx_filter_spec) -
  2402. offsetof(struct efx_filter_spec, outer_vid)) / 4,
  2403. 0);
  2404. /* XXX should we randomise the initval? */
  2405. }
  2406. /* Decide whether a filter should be exclusive or else should allow
  2407. * delivery to additional recipients. Currently we decide that
  2408. * filters for specific local unicast MAC and IP addresses are
  2409. * exclusive.
  2410. */
  2411. static bool efx_ef10_filter_is_exclusive(const struct efx_filter_spec *spec)
  2412. {
  2413. if (spec->match_flags & EFX_FILTER_MATCH_LOC_MAC &&
  2414. !is_multicast_ether_addr(spec->loc_mac))
  2415. return true;
  2416. if ((spec->match_flags &
  2417. (EFX_FILTER_MATCH_ETHER_TYPE | EFX_FILTER_MATCH_LOC_HOST)) ==
  2418. (EFX_FILTER_MATCH_ETHER_TYPE | EFX_FILTER_MATCH_LOC_HOST)) {
  2419. if (spec->ether_type == htons(ETH_P_IP) &&
  2420. !ipv4_is_multicast(spec->loc_host[0]))
  2421. return true;
  2422. if (spec->ether_type == htons(ETH_P_IPV6) &&
  2423. ((const u8 *)spec->loc_host)[0] != 0xff)
  2424. return true;
  2425. }
  2426. return false;
  2427. }
  2428. static struct efx_filter_spec *
  2429. efx_ef10_filter_entry_spec(const struct efx_ef10_filter_table *table,
  2430. unsigned int filter_idx)
  2431. {
  2432. return (struct efx_filter_spec *)(table->entry[filter_idx].spec &
  2433. ~EFX_EF10_FILTER_FLAGS);
  2434. }
  2435. static unsigned int
  2436. efx_ef10_filter_entry_flags(const struct efx_ef10_filter_table *table,
  2437. unsigned int filter_idx)
  2438. {
  2439. return table->entry[filter_idx].spec & EFX_EF10_FILTER_FLAGS;
  2440. }
  2441. static void
  2442. efx_ef10_filter_set_entry(struct efx_ef10_filter_table *table,
  2443. unsigned int filter_idx,
  2444. const struct efx_filter_spec *spec,
  2445. unsigned int flags)
  2446. {
  2447. table->entry[filter_idx].spec = (unsigned long)spec | flags;
  2448. }
  2449. static void efx_ef10_filter_push_prep(struct efx_nic *efx,
  2450. const struct efx_filter_spec *spec,
  2451. efx_dword_t *inbuf, u64 handle,
  2452. bool replacing)
  2453. {
  2454. struct efx_ef10_nic_data *nic_data = efx->nic_data;
  2455. memset(inbuf, 0, MC_CMD_FILTER_OP_IN_LEN);
  2456. if (replacing) {
  2457. MCDI_SET_DWORD(inbuf, FILTER_OP_IN_OP,
  2458. MC_CMD_FILTER_OP_IN_OP_REPLACE);
  2459. MCDI_SET_QWORD(inbuf, FILTER_OP_IN_HANDLE, handle);
  2460. } else {
  2461. u32 match_fields = 0;
  2462. MCDI_SET_DWORD(inbuf, FILTER_OP_IN_OP,
  2463. efx_ef10_filter_is_exclusive(spec) ?
  2464. MC_CMD_FILTER_OP_IN_OP_INSERT :
  2465. MC_CMD_FILTER_OP_IN_OP_SUBSCRIBE);
  2466. /* Convert match flags and values. Unlike almost
  2467. * everything else in MCDI, these fields are in
  2468. * network byte order.
  2469. */
  2470. if (spec->match_flags & EFX_FILTER_MATCH_LOC_MAC_IG)
  2471. match_fields |=
  2472. is_multicast_ether_addr(spec->loc_mac) ?
  2473. 1 << MC_CMD_FILTER_OP_IN_MATCH_UNKNOWN_MCAST_DST_LBN :
  2474. 1 << MC_CMD_FILTER_OP_IN_MATCH_UNKNOWN_UCAST_DST_LBN;
  2475. #define COPY_FIELD(gen_flag, gen_field, mcdi_field) \
  2476. if (spec->match_flags & EFX_FILTER_MATCH_ ## gen_flag) { \
  2477. match_fields |= \
  2478. 1 << MC_CMD_FILTER_OP_IN_MATCH_ ## \
  2479. mcdi_field ## _LBN; \
  2480. BUILD_BUG_ON( \
  2481. MC_CMD_FILTER_OP_IN_ ## mcdi_field ## _LEN < \
  2482. sizeof(spec->gen_field)); \
  2483. memcpy(MCDI_PTR(inbuf, FILTER_OP_IN_ ## mcdi_field), \
  2484. &spec->gen_field, sizeof(spec->gen_field)); \
  2485. }
  2486. COPY_FIELD(REM_HOST, rem_host, SRC_IP);
  2487. COPY_FIELD(LOC_HOST, loc_host, DST_IP);
  2488. COPY_FIELD(REM_MAC, rem_mac, SRC_MAC);
  2489. COPY_FIELD(REM_PORT, rem_port, SRC_PORT);
  2490. COPY_FIELD(LOC_MAC, loc_mac, DST_MAC);
  2491. COPY_FIELD(LOC_PORT, loc_port, DST_PORT);
  2492. COPY_FIELD(ETHER_TYPE, ether_type, ETHER_TYPE);
  2493. COPY_FIELD(INNER_VID, inner_vid, INNER_VLAN);
  2494. COPY_FIELD(OUTER_VID, outer_vid, OUTER_VLAN);
  2495. COPY_FIELD(IP_PROTO, ip_proto, IP_PROTO);
  2496. #undef COPY_FIELD
  2497. MCDI_SET_DWORD(inbuf, FILTER_OP_IN_MATCH_FIELDS,
  2498. match_fields);
  2499. }
  2500. MCDI_SET_DWORD(inbuf, FILTER_OP_IN_PORT_ID, nic_data->vport_id);
  2501. MCDI_SET_DWORD(inbuf, FILTER_OP_IN_RX_DEST,
  2502. spec->dmaq_id == EFX_FILTER_RX_DMAQ_ID_DROP ?
  2503. MC_CMD_FILTER_OP_IN_RX_DEST_DROP :
  2504. MC_CMD_FILTER_OP_IN_RX_DEST_HOST);
  2505. MCDI_SET_DWORD(inbuf, FILTER_OP_IN_TX_DOMAIN, 0);
  2506. MCDI_SET_DWORD(inbuf, FILTER_OP_IN_TX_DEST,
  2507. MC_CMD_FILTER_OP_IN_TX_DEST_DEFAULT);
  2508. MCDI_SET_DWORD(inbuf, FILTER_OP_IN_RX_QUEUE,
  2509. spec->dmaq_id == EFX_FILTER_RX_DMAQ_ID_DROP ?
  2510. 0 : spec->dmaq_id);
  2511. MCDI_SET_DWORD(inbuf, FILTER_OP_IN_RX_MODE,
  2512. (spec->flags & EFX_FILTER_FLAG_RX_RSS) ?
  2513. MC_CMD_FILTER_OP_IN_RX_MODE_RSS :
  2514. MC_CMD_FILTER_OP_IN_RX_MODE_SIMPLE);
  2515. if (spec->flags & EFX_FILTER_FLAG_RX_RSS)
  2516. MCDI_SET_DWORD(inbuf, FILTER_OP_IN_RX_CONTEXT,
  2517. spec->rss_context !=
  2518. EFX_FILTER_RSS_CONTEXT_DEFAULT ?
  2519. spec->rss_context : nic_data->rx_rss_context);
  2520. }
  2521. static int efx_ef10_filter_push(struct efx_nic *efx,
  2522. const struct efx_filter_spec *spec,
  2523. u64 *handle, bool replacing)
  2524. {
  2525. MCDI_DECLARE_BUF(inbuf, MC_CMD_FILTER_OP_IN_LEN);
  2526. MCDI_DECLARE_BUF(outbuf, MC_CMD_FILTER_OP_OUT_LEN);
  2527. int rc;
  2528. efx_ef10_filter_push_prep(efx, spec, inbuf, *handle, replacing);
  2529. rc = efx_mcdi_rpc(efx, MC_CMD_FILTER_OP, inbuf, sizeof(inbuf),
  2530. outbuf, sizeof(outbuf), NULL);
  2531. if (rc == 0)
  2532. *handle = MCDI_QWORD(outbuf, FILTER_OP_OUT_HANDLE);
  2533. if (rc == -ENOSPC)
  2534. rc = -EBUSY; /* to match efx_farch_filter_insert() */
  2535. return rc;
  2536. }
  2537. static int efx_ef10_filter_rx_match_pri(struct efx_ef10_filter_table *table,
  2538. enum efx_filter_match_flags match_flags)
  2539. {
  2540. unsigned int match_pri;
  2541. for (match_pri = 0;
  2542. match_pri < table->rx_match_count;
  2543. match_pri++)
  2544. if (table->rx_match_flags[match_pri] == match_flags)
  2545. return match_pri;
  2546. return -EPROTONOSUPPORT;
  2547. }
  2548. static s32 efx_ef10_filter_insert(struct efx_nic *efx,
  2549. struct efx_filter_spec *spec,
  2550. bool replace_equal)
  2551. {
  2552. struct efx_ef10_filter_table *table = efx->filter_state;
  2553. DECLARE_BITMAP(mc_rem_map, EFX_EF10_FILTER_SEARCH_LIMIT);
  2554. struct efx_filter_spec *saved_spec;
  2555. unsigned int match_pri, hash;
  2556. unsigned int priv_flags;
  2557. bool replacing = false;
  2558. int ins_index = -1;
  2559. DEFINE_WAIT(wait);
  2560. bool is_mc_recip;
  2561. s32 rc;
  2562. /* For now, only support RX filters */
  2563. if ((spec->flags & (EFX_FILTER_FLAG_RX | EFX_FILTER_FLAG_TX)) !=
  2564. EFX_FILTER_FLAG_RX)
  2565. return -EINVAL;
  2566. rc = efx_ef10_filter_rx_match_pri(table, spec->match_flags);
  2567. if (rc < 0)
  2568. return rc;
  2569. match_pri = rc;
  2570. hash = efx_ef10_filter_hash(spec);
  2571. is_mc_recip = efx_filter_is_mc_recipient(spec);
  2572. if (is_mc_recip)
  2573. bitmap_zero(mc_rem_map, EFX_EF10_FILTER_SEARCH_LIMIT);
  2574. /* Find any existing filters with the same match tuple or
  2575. * else a free slot to insert at. If any of them are busy,
  2576. * we have to wait and retry.
  2577. */
  2578. for (;;) {
  2579. unsigned int depth = 1;
  2580. unsigned int i;
  2581. spin_lock_bh(&efx->filter_lock);
  2582. for (;;) {
  2583. i = (hash + depth) & (HUNT_FILTER_TBL_ROWS - 1);
  2584. saved_spec = efx_ef10_filter_entry_spec(table, i);
  2585. if (!saved_spec) {
  2586. if (ins_index < 0)
  2587. ins_index = i;
  2588. } else if (efx_ef10_filter_equal(spec, saved_spec)) {
  2589. if (table->entry[i].spec &
  2590. EFX_EF10_FILTER_FLAG_BUSY)
  2591. break;
  2592. if (spec->priority < saved_spec->priority &&
  2593. spec->priority != EFX_FILTER_PRI_AUTO) {
  2594. rc = -EPERM;
  2595. goto out_unlock;
  2596. }
  2597. if (!is_mc_recip) {
  2598. /* This is the only one */
  2599. if (spec->priority ==
  2600. saved_spec->priority &&
  2601. !replace_equal) {
  2602. rc = -EEXIST;
  2603. goto out_unlock;
  2604. }
  2605. ins_index = i;
  2606. goto found;
  2607. } else if (spec->priority >
  2608. saved_spec->priority ||
  2609. (spec->priority ==
  2610. saved_spec->priority &&
  2611. replace_equal)) {
  2612. if (ins_index < 0)
  2613. ins_index = i;
  2614. else
  2615. __set_bit(depth, mc_rem_map);
  2616. }
  2617. }
  2618. /* Once we reach the maximum search depth, use
  2619. * the first suitable slot or return -EBUSY if
  2620. * there was none
  2621. */
  2622. if (depth == EFX_EF10_FILTER_SEARCH_LIMIT) {
  2623. if (ins_index < 0) {
  2624. rc = -EBUSY;
  2625. goto out_unlock;
  2626. }
  2627. goto found;
  2628. }
  2629. ++depth;
  2630. }
  2631. prepare_to_wait(&table->waitq, &wait, TASK_UNINTERRUPTIBLE);
  2632. spin_unlock_bh(&efx->filter_lock);
  2633. schedule();
  2634. }
  2635. found:
  2636. /* Create a software table entry if necessary, and mark it
  2637. * busy. We might yet fail to insert, but any attempt to
  2638. * insert a conflicting filter while we're waiting for the
  2639. * firmware must find the busy entry.
  2640. */
  2641. saved_spec = efx_ef10_filter_entry_spec(table, ins_index);
  2642. if (saved_spec) {
  2643. if (spec->priority == EFX_FILTER_PRI_AUTO &&
  2644. saved_spec->priority >= EFX_FILTER_PRI_AUTO) {
  2645. /* Just make sure it won't be removed */
  2646. if (saved_spec->priority > EFX_FILTER_PRI_AUTO)
  2647. saved_spec->flags |= EFX_FILTER_FLAG_RX_OVER_AUTO;
  2648. table->entry[ins_index].spec &=
  2649. ~EFX_EF10_FILTER_FLAG_AUTO_OLD;
  2650. rc = ins_index;
  2651. goto out_unlock;
  2652. }
  2653. replacing = true;
  2654. priv_flags = efx_ef10_filter_entry_flags(table, ins_index);
  2655. } else {
  2656. saved_spec = kmalloc(sizeof(*spec), GFP_ATOMIC);
  2657. if (!saved_spec) {
  2658. rc = -ENOMEM;
  2659. goto out_unlock;
  2660. }
  2661. *saved_spec = *spec;
  2662. priv_flags = 0;
  2663. }
  2664. efx_ef10_filter_set_entry(table, ins_index, saved_spec,
  2665. priv_flags | EFX_EF10_FILTER_FLAG_BUSY);
  2666. /* Mark lower-priority multicast recipients busy prior to removal */
  2667. if (is_mc_recip) {
  2668. unsigned int depth, i;
  2669. for (depth = 0; depth < EFX_EF10_FILTER_SEARCH_LIMIT; depth++) {
  2670. i = (hash + depth) & (HUNT_FILTER_TBL_ROWS - 1);
  2671. if (test_bit(depth, mc_rem_map))
  2672. table->entry[i].spec |=
  2673. EFX_EF10_FILTER_FLAG_BUSY;
  2674. }
  2675. }
  2676. spin_unlock_bh(&efx->filter_lock);
  2677. rc = efx_ef10_filter_push(efx, spec, &table->entry[ins_index].handle,
  2678. replacing);
  2679. /* Finalise the software table entry */
  2680. spin_lock_bh(&efx->filter_lock);
  2681. if (rc == 0) {
  2682. if (replacing) {
  2683. /* Update the fields that may differ */
  2684. if (saved_spec->priority == EFX_FILTER_PRI_AUTO)
  2685. saved_spec->flags |=
  2686. EFX_FILTER_FLAG_RX_OVER_AUTO;
  2687. saved_spec->priority = spec->priority;
  2688. saved_spec->flags &= EFX_FILTER_FLAG_RX_OVER_AUTO;
  2689. saved_spec->flags |= spec->flags;
  2690. saved_spec->rss_context = spec->rss_context;
  2691. saved_spec->dmaq_id = spec->dmaq_id;
  2692. }
  2693. } else if (!replacing) {
  2694. kfree(saved_spec);
  2695. saved_spec = NULL;
  2696. }
  2697. efx_ef10_filter_set_entry(table, ins_index, saved_spec, priv_flags);
  2698. /* Remove and finalise entries for lower-priority multicast
  2699. * recipients
  2700. */
  2701. if (is_mc_recip) {
  2702. MCDI_DECLARE_BUF(inbuf, MC_CMD_FILTER_OP_IN_LEN);
  2703. unsigned int depth, i;
  2704. memset(inbuf, 0, sizeof(inbuf));
  2705. for (depth = 0; depth < EFX_EF10_FILTER_SEARCH_LIMIT; depth++) {
  2706. if (!test_bit(depth, mc_rem_map))
  2707. continue;
  2708. i = (hash + depth) & (HUNT_FILTER_TBL_ROWS - 1);
  2709. saved_spec = efx_ef10_filter_entry_spec(table, i);
  2710. priv_flags = efx_ef10_filter_entry_flags(table, i);
  2711. if (rc == 0) {
  2712. spin_unlock_bh(&efx->filter_lock);
  2713. MCDI_SET_DWORD(inbuf, FILTER_OP_IN_OP,
  2714. MC_CMD_FILTER_OP_IN_OP_UNSUBSCRIBE);
  2715. MCDI_SET_QWORD(inbuf, FILTER_OP_IN_HANDLE,
  2716. table->entry[i].handle);
  2717. rc = efx_mcdi_rpc(efx, MC_CMD_FILTER_OP,
  2718. inbuf, sizeof(inbuf),
  2719. NULL, 0, NULL);
  2720. spin_lock_bh(&efx->filter_lock);
  2721. }
  2722. if (rc == 0) {
  2723. kfree(saved_spec);
  2724. saved_spec = NULL;
  2725. priv_flags = 0;
  2726. } else {
  2727. priv_flags &= ~EFX_EF10_FILTER_FLAG_BUSY;
  2728. }
  2729. efx_ef10_filter_set_entry(table, i, saved_spec,
  2730. priv_flags);
  2731. }
  2732. }
  2733. /* If successful, return the inserted filter ID */
  2734. if (rc == 0)
  2735. rc = match_pri * HUNT_FILTER_TBL_ROWS + ins_index;
  2736. wake_up_all(&table->waitq);
  2737. out_unlock:
  2738. spin_unlock_bh(&efx->filter_lock);
  2739. finish_wait(&table->waitq, &wait);
  2740. return rc;
  2741. }
  2742. static void efx_ef10_filter_update_rx_scatter(struct efx_nic *efx)
  2743. {
  2744. /* no need to do anything here on EF10 */
  2745. }
  2746. /* Remove a filter.
  2747. * If !by_index, remove by ID
  2748. * If by_index, remove by index
  2749. * Filter ID may come from userland and must be range-checked.
  2750. */
  2751. static int efx_ef10_filter_remove_internal(struct efx_nic *efx,
  2752. unsigned int priority_mask,
  2753. u32 filter_id, bool by_index)
  2754. {
  2755. unsigned int filter_idx = filter_id % HUNT_FILTER_TBL_ROWS;
  2756. struct efx_ef10_filter_table *table = efx->filter_state;
  2757. MCDI_DECLARE_BUF(inbuf,
  2758. MC_CMD_FILTER_OP_IN_HANDLE_OFST +
  2759. MC_CMD_FILTER_OP_IN_HANDLE_LEN);
  2760. struct efx_filter_spec *spec;
  2761. DEFINE_WAIT(wait);
  2762. int rc;
  2763. /* Find the software table entry and mark it busy. Don't
  2764. * remove it yet; any attempt to update while we're waiting
  2765. * for the firmware must find the busy entry.
  2766. */
  2767. for (;;) {
  2768. spin_lock_bh(&efx->filter_lock);
  2769. if (!(table->entry[filter_idx].spec &
  2770. EFX_EF10_FILTER_FLAG_BUSY))
  2771. break;
  2772. prepare_to_wait(&table->waitq, &wait, TASK_UNINTERRUPTIBLE);
  2773. spin_unlock_bh(&efx->filter_lock);
  2774. schedule();
  2775. }
  2776. spec = efx_ef10_filter_entry_spec(table, filter_idx);
  2777. if (!spec ||
  2778. (!by_index &&
  2779. efx_ef10_filter_rx_match_pri(table, spec->match_flags) !=
  2780. filter_id / HUNT_FILTER_TBL_ROWS)) {
  2781. rc = -ENOENT;
  2782. goto out_unlock;
  2783. }
  2784. if (spec->flags & EFX_FILTER_FLAG_RX_OVER_AUTO &&
  2785. priority_mask == (1U << EFX_FILTER_PRI_AUTO)) {
  2786. /* Just remove flags */
  2787. spec->flags &= ~EFX_FILTER_FLAG_RX_OVER_AUTO;
  2788. table->entry[filter_idx].spec &= ~EFX_EF10_FILTER_FLAG_AUTO_OLD;
  2789. rc = 0;
  2790. goto out_unlock;
  2791. }
  2792. if (!(priority_mask & (1U << spec->priority))) {
  2793. rc = -ENOENT;
  2794. goto out_unlock;
  2795. }
  2796. table->entry[filter_idx].spec |= EFX_EF10_FILTER_FLAG_BUSY;
  2797. spin_unlock_bh(&efx->filter_lock);
  2798. if (spec->flags & EFX_FILTER_FLAG_RX_OVER_AUTO) {
  2799. /* Reset to an automatic filter */
  2800. struct efx_filter_spec new_spec = *spec;
  2801. new_spec.priority = EFX_FILTER_PRI_AUTO;
  2802. new_spec.flags = (EFX_FILTER_FLAG_RX |
  2803. EFX_FILTER_FLAG_RX_RSS);
  2804. new_spec.dmaq_id = 0;
  2805. new_spec.rss_context = EFX_FILTER_RSS_CONTEXT_DEFAULT;
  2806. rc = efx_ef10_filter_push(efx, &new_spec,
  2807. &table->entry[filter_idx].handle,
  2808. true);
  2809. spin_lock_bh(&efx->filter_lock);
  2810. if (rc == 0)
  2811. *spec = new_spec;
  2812. } else {
  2813. /* Really remove the filter */
  2814. MCDI_SET_DWORD(inbuf, FILTER_OP_IN_OP,
  2815. efx_ef10_filter_is_exclusive(spec) ?
  2816. MC_CMD_FILTER_OP_IN_OP_REMOVE :
  2817. MC_CMD_FILTER_OP_IN_OP_UNSUBSCRIBE);
  2818. MCDI_SET_QWORD(inbuf, FILTER_OP_IN_HANDLE,
  2819. table->entry[filter_idx].handle);
  2820. rc = efx_mcdi_rpc(efx, MC_CMD_FILTER_OP,
  2821. inbuf, sizeof(inbuf), NULL, 0, NULL);
  2822. spin_lock_bh(&efx->filter_lock);
  2823. if (rc == 0) {
  2824. kfree(spec);
  2825. efx_ef10_filter_set_entry(table, filter_idx, NULL, 0);
  2826. }
  2827. }
  2828. table->entry[filter_idx].spec &= ~EFX_EF10_FILTER_FLAG_BUSY;
  2829. wake_up_all(&table->waitq);
  2830. out_unlock:
  2831. spin_unlock_bh(&efx->filter_lock);
  2832. finish_wait(&table->waitq, &wait);
  2833. return rc;
  2834. }
  2835. static int efx_ef10_filter_remove_safe(struct efx_nic *efx,
  2836. enum efx_filter_priority priority,
  2837. u32 filter_id)
  2838. {
  2839. return efx_ef10_filter_remove_internal(efx, 1U << priority,
  2840. filter_id, false);
  2841. }
  2842. static u32 efx_ef10_filter_get_unsafe_id(struct efx_nic *efx, u32 filter_id)
  2843. {
  2844. return filter_id % HUNT_FILTER_TBL_ROWS;
  2845. }
  2846. static int efx_ef10_filter_remove_unsafe(struct efx_nic *efx,
  2847. enum efx_filter_priority priority,
  2848. u32 filter_id)
  2849. {
  2850. return efx_ef10_filter_remove_internal(efx, 1U << priority,
  2851. filter_id, true);
  2852. }
  2853. static int efx_ef10_filter_get_safe(struct efx_nic *efx,
  2854. enum efx_filter_priority priority,
  2855. u32 filter_id, struct efx_filter_spec *spec)
  2856. {
  2857. unsigned int filter_idx = filter_id % HUNT_FILTER_TBL_ROWS;
  2858. struct efx_ef10_filter_table *table = efx->filter_state;
  2859. const struct efx_filter_spec *saved_spec;
  2860. int rc;
  2861. spin_lock_bh(&efx->filter_lock);
  2862. saved_spec = efx_ef10_filter_entry_spec(table, filter_idx);
  2863. if (saved_spec && saved_spec->priority == priority &&
  2864. efx_ef10_filter_rx_match_pri(table, saved_spec->match_flags) ==
  2865. filter_id / HUNT_FILTER_TBL_ROWS) {
  2866. *spec = *saved_spec;
  2867. rc = 0;
  2868. } else {
  2869. rc = -ENOENT;
  2870. }
  2871. spin_unlock_bh(&efx->filter_lock);
  2872. return rc;
  2873. }
  2874. static int efx_ef10_filter_clear_rx(struct efx_nic *efx,
  2875. enum efx_filter_priority priority)
  2876. {
  2877. unsigned int priority_mask;
  2878. unsigned int i;
  2879. int rc;
  2880. priority_mask = (((1U << (priority + 1)) - 1) &
  2881. ~(1U << EFX_FILTER_PRI_AUTO));
  2882. for (i = 0; i < HUNT_FILTER_TBL_ROWS; i++) {
  2883. rc = efx_ef10_filter_remove_internal(efx, priority_mask,
  2884. i, true);
  2885. if (rc && rc != -ENOENT)
  2886. return rc;
  2887. }
  2888. return 0;
  2889. }
  2890. static u32 efx_ef10_filter_count_rx_used(struct efx_nic *efx,
  2891. enum efx_filter_priority priority)
  2892. {
  2893. struct efx_ef10_filter_table *table = efx->filter_state;
  2894. unsigned int filter_idx;
  2895. s32 count = 0;
  2896. spin_lock_bh(&efx->filter_lock);
  2897. for (filter_idx = 0; filter_idx < HUNT_FILTER_TBL_ROWS; filter_idx++) {
  2898. if (table->entry[filter_idx].spec &&
  2899. efx_ef10_filter_entry_spec(table, filter_idx)->priority ==
  2900. priority)
  2901. ++count;
  2902. }
  2903. spin_unlock_bh(&efx->filter_lock);
  2904. return count;
  2905. }
  2906. static u32 efx_ef10_filter_get_rx_id_limit(struct efx_nic *efx)
  2907. {
  2908. struct efx_ef10_filter_table *table = efx->filter_state;
  2909. return table->rx_match_count * HUNT_FILTER_TBL_ROWS;
  2910. }
  2911. static s32 efx_ef10_filter_get_rx_ids(struct efx_nic *efx,
  2912. enum efx_filter_priority priority,
  2913. u32 *buf, u32 size)
  2914. {
  2915. struct efx_ef10_filter_table *table = efx->filter_state;
  2916. struct efx_filter_spec *spec;
  2917. unsigned int filter_idx;
  2918. s32 count = 0;
  2919. spin_lock_bh(&efx->filter_lock);
  2920. for (filter_idx = 0; filter_idx < HUNT_FILTER_TBL_ROWS; filter_idx++) {
  2921. spec = efx_ef10_filter_entry_spec(table, filter_idx);
  2922. if (spec && spec->priority == priority) {
  2923. if (count == size) {
  2924. count = -EMSGSIZE;
  2925. break;
  2926. }
  2927. buf[count++] = (efx_ef10_filter_rx_match_pri(
  2928. table, spec->match_flags) *
  2929. HUNT_FILTER_TBL_ROWS +
  2930. filter_idx);
  2931. }
  2932. }
  2933. spin_unlock_bh(&efx->filter_lock);
  2934. return count;
  2935. }
  2936. #ifdef CONFIG_RFS_ACCEL
  2937. static efx_mcdi_async_completer efx_ef10_filter_rfs_insert_complete;
  2938. static s32 efx_ef10_filter_rfs_insert(struct efx_nic *efx,
  2939. struct efx_filter_spec *spec)
  2940. {
  2941. struct efx_ef10_filter_table *table = efx->filter_state;
  2942. MCDI_DECLARE_BUF(inbuf, MC_CMD_FILTER_OP_IN_LEN);
  2943. struct efx_filter_spec *saved_spec;
  2944. unsigned int hash, i, depth = 1;
  2945. bool replacing = false;
  2946. int ins_index = -1;
  2947. u64 cookie;
  2948. s32 rc;
  2949. /* Must be an RX filter without RSS and not for a multicast
  2950. * destination address (RFS only works for connected sockets).
  2951. * These restrictions allow us to pass only a tiny amount of
  2952. * data through to the completion function.
  2953. */
  2954. EFX_WARN_ON_PARANOID(spec->flags !=
  2955. (EFX_FILTER_FLAG_RX | EFX_FILTER_FLAG_RX_SCATTER));
  2956. EFX_WARN_ON_PARANOID(spec->priority != EFX_FILTER_PRI_HINT);
  2957. EFX_WARN_ON_PARANOID(efx_filter_is_mc_recipient(spec));
  2958. hash = efx_ef10_filter_hash(spec);
  2959. spin_lock_bh(&efx->filter_lock);
  2960. /* Find any existing filter with the same match tuple or else
  2961. * a free slot to insert at. If an existing filter is busy,
  2962. * we have to give up.
  2963. */
  2964. for (;;) {
  2965. i = (hash + depth) & (HUNT_FILTER_TBL_ROWS - 1);
  2966. saved_spec = efx_ef10_filter_entry_spec(table, i);
  2967. if (!saved_spec) {
  2968. if (ins_index < 0)
  2969. ins_index = i;
  2970. } else if (efx_ef10_filter_equal(spec, saved_spec)) {
  2971. if (table->entry[i].spec & EFX_EF10_FILTER_FLAG_BUSY) {
  2972. rc = -EBUSY;
  2973. goto fail_unlock;
  2974. }
  2975. if (spec->priority < saved_spec->priority) {
  2976. rc = -EPERM;
  2977. goto fail_unlock;
  2978. }
  2979. ins_index = i;
  2980. break;
  2981. }
  2982. /* Once we reach the maximum search depth, use the
  2983. * first suitable slot or return -EBUSY if there was
  2984. * none
  2985. */
  2986. if (depth == EFX_EF10_FILTER_SEARCH_LIMIT) {
  2987. if (ins_index < 0) {
  2988. rc = -EBUSY;
  2989. goto fail_unlock;
  2990. }
  2991. break;
  2992. }
  2993. ++depth;
  2994. }
  2995. /* Create a software table entry if necessary, and mark it
  2996. * busy. We might yet fail to insert, but any attempt to
  2997. * insert a conflicting filter while we're waiting for the
  2998. * firmware must find the busy entry.
  2999. */
  3000. saved_spec = efx_ef10_filter_entry_spec(table, ins_index);
  3001. if (saved_spec) {
  3002. replacing = true;
  3003. } else {
  3004. saved_spec = kmalloc(sizeof(*spec), GFP_ATOMIC);
  3005. if (!saved_spec) {
  3006. rc = -ENOMEM;
  3007. goto fail_unlock;
  3008. }
  3009. *saved_spec = *spec;
  3010. }
  3011. efx_ef10_filter_set_entry(table, ins_index, saved_spec,
  3012. EFX_EF10_FILTER_FLAG_BUSY);
  3013. spin_unlock_bh(&efx->filter_lock);
  3014. /* Pack up the variables needed on completion */
  3015. cookie = replacing << 31 | ins_index << 16 | spec->dmaq_id;
  3016. efx_ef10_filter_push_prep(efx, spec, inbuf,
  3017. table->entry[ins_index].handle, replacing);
  3018. efx_mcdi_rpc_async(efx, MC_CMD_FILTER_OP, inbuf, sizeof(inbuf),
  3019. MC_CMD_FILTER_OP_OUT_LEN,
  3020. efx_ef10_filter_rfs_insert_complete, cookie);
  3021. return ins_index;
  3022. fail_unlock:
  3023. spin_unlock_bh(&efx->filter_lock);
  3024. return rc;
  3025. }
  3026. static void
  3027. efx_ef10_filter_rfs_insert_complete(struct efx_nic *efx, unsigned long cookie,
  3028. int rc, efx_dword_t *outbuf,
  3029. size_t outlen_actual)
  3030. {
  3031. struct efx_ef10_filter_table *table = efx->filter_state;
  3032. unsigned int ins_index, dmaq_id;
  3033. struct efx_filter_spec *spec;
  3034. bool replacing;
  3035. /* Unpack the cookie */
  3036. replacing = cookie >> 31;
  3037. ins_index = (cookie >> 16) & (HUNT_FILTER_TBL_ROWS - 1);
  3038. dmaq_id = cookie & 0xffff;
  3039. spin_lock_bh(&efx->filter_lock);
  3040. spec = efx_ef10_filter_entry_spec(table, ins_index);
  3041. if (rc == 0) {
  3042. table->entry[ins_index].handle =
  3043. MCDI_QWORD(outbuf, FILTER_OP_OUT_HANDLE);
  3044. if (replacing)
  3045. spec->dmaq_id = dmaq_id;
  3046. } else if (!replacing) {
  3047. kfree(spec);
  3048. spec = NULL;
  3049. }
  3050. efx_ef10_filter_set_entry(table, ins_index, spec, 0);
  3051. spin_unlock_bh(&efx->filter_lock);
  3052. wake_up_all(&table->waitq);
  3053. }
  3054. static void
  3055. efx_ef10_filter_rfs_expire_complete(struct efx_nic *efx,
  3056. unsigned long filter_idx,
  3057. int rc, efx_dword_t *outbuf,
  3058. size_t outlen_actual);
  3059. static bool efx_ef10_filter_rfs_expire_one(struct efx_nic *efx, u32 flow_id,
  3060. unsigned int filter_idx)
  3061. {
  3062. struct efx_ef10_filter_table *table = efx->filter_state;
  3063. struct efx_filter_spec *spec =
  3064. efx_ef10_filter_entry_spec(table, filter_idx);
  3065. MCDI_DECLARE_BUF(inbuf,
  3066. MC_CMD_FILTER_OP_IN_HANDLE_OFST +
  3067. MC_CMD_FILTER_OP_IN_HANDLE_LEN);
  3068. if (!spec ||
  3069. (table->entry[filter_idx].spec & EFX_EF10_FILTER_FLAG_BUSY) ||
  3070. spec->priority != EFX_FILTER_PRI_HINT ||
  3071. !rps_may_expire_flow(efx->net_dev, spec->dmaq_id,
  3072. flow_id, filter_idx))
  3073. return false;
  3074. MCDI_SET_DWORD(inbuf, FILTER_OP_IN_OP,
  3075. MC_CMD_FILTER_OP_IN_OP_REMOVE);
  3076. MCDI_SET_QWORD(inbuf, FILTER_OP_IN_HANDLE,
  3077. table->entry[filter_idx].handle);
  3078. if (efx_mcdi_rpc_async(efx, MC_CMD_FILTER_OP, inbuf, sizeof(inbuf), 0,
  3079. efx_ef10_filter_rfs_expire_complete, filter_idx))
  3080. return false;
  3081. table->entry[filter_idx].spec |= EFX_EF10_FILTER_FLAG_BUSY;
  3082. return true;
  3083. }
  3084. static void
  3085. efx_ef10_filter_rfs_expire_complete(struct efx_nic *efx,
  3086. unsigned long filter_idx,
  3087. int rc, efx_dword_t *outbuf,
  3088. size_t outlen_actual)
  3089. {
  3090. struct efx_ef10_filter_table *table = efx->filter_state;
  3091. struct efx_filter_spec *spec =
  3092. efx_ef10_filter_entry_spec(table, filter_idx);
  3093. spin_lock_bh(&efx->filter_lock);
  3094. if (rc == 0) {
  3095. kfree(spec);
  3096. efx_ef10_filter_set_entry(table, filter_idx, NULL, 0);
  3097. }
  3098. table->entry[filter_idx].spec &= ~EFX_EF10_FILTER_FLAG_BUSY;
  3099. wake_up_all(&table->waitq);
  3100. spin_unlock_bh(&efx->filter_lock);
  3101. }
  3102. #endif /* CONFIG_RFS_ACCEL */
  3103. static int efx_ef10_filter_match_flags_from_mcdi(u32 mcdi_flags)
  3104. {
  3105. int match_flags = 0;
  3106. #define MAP_FLAG(gen_flag, mcdi_field) { \
  3107. u32 old_mcdi_flags = mcdi_flags; \
  3108. mcdi_flags &= ~(1 << MC_CMD_FILTER_OP_IN_MATCH_ ## \
  3109. mcdi_field ## _LBN); \
  3110. if (mcdi_flags != old_mcdi_flags) \
  3111. match_flags |= EFX_FILTER_MATCH_ ## gen_flag; \
  3112. }
  3113. MAP_FLAG(LOC_MAC_IG, UNKNOWN_UCAST_DST);
  3114. MAP_FLAG(LOC_MAC_IG, UNKNOWN_MCAST_DST);
  3115. MAP_FLAG(REM_HOST, SRC_IP);
  3116. MAP_FLAG(LOC_HOST, DST_IP);
  3117. MAP_FLAG(REM_MAC, SRC_MAC);
  3118. MAP_FLAG(REM_PORT, SRC_PORT);
  3119. MAP_FLAG(LOC_MAC, DST_MAC);
  3120. MAP_FLAG(LOC_PORT, DST_PORT);
  3121. MAP_FLAG(ETHER_TYPE, ETHER_TYPE);
  3122. MAP_FLAG(INNER_VID, INNER_VLAN);
  3123. MAP_FLAG(OUTER_VID, OUTER_VLAN);
  3124. MAP_FLAG(IP_PROTO, IP_PROTO);
  3125. #undef MAP_FLAG
  3126. /* Did we map them all? */
  3127. if (mcdi_flags)
  3128. return -EINVAL;
  3129. return match_flags;
  3130. }
  3131. static int efx_ef10_filter_table_probe(struct efx_nic *efx)
  3132. {
  3133. MCDI_DECLARE_BUF(inbuf, MC_CMD_GET_PARSER_DISP_INFO_IN_LEN);
  3134. MCDI_DECLARE_BUF(outbuf, MC_CMD_GET_PARSER_DISP_INFO_OUT_LENMAX);
  3135. unsigned int pd_match_pri, pd_match_count;
  3136. struct efx_ef10_filter_table *table;
  3137. size_t outlen;
  3138. int rc;
  3139. table = kzalloc(sizeof(*table), GFP_KERNEL);
  3140. if (!table)
  3141. return -ENOMEM;
  3142. /* Find out which RX filter types are supported, and their priorities */
  3143. MCDI_SET_DWORD(inbuf, GET_PARSER_DISP_INFO_IN_OP,
  3144. MC_CMD_GET_PARSER_DISP_INFO_IN_OP_GET_SUPPORTED_RX_MATCHES);
  3145. rc = efx_mcdi_rpc(efx, MC_CMD_GET_PARSER_DISP_INFO,
  3146. inbuf, sizeof(inbuf), outbuf, sizeof(outbuf),
  3147. &outlen);
  3148. if (rc)
  3149. goto fail;
  3150. pd_match_count = MCDI_VAR_ARRAY_LEN(
  3151. outlen, GET_PARSER_DISP_INFO_OUT_SUPPORTED_MATCHES);
  3152. table->rx_match_count = 0;
  3153. for (pd_match_pri = 0; pd_match_pri < pd_match_count; pd_match_pri++) {
  3154. u32 mcdi_flags =
  3155. MCDI_ARRAY_DWORD(
  3156. outbuf,
  3157. GET_PARSER_DISP_INFO_OUT_SUPPORTED_MATCHES,
  3158. pd_match_pri);
  3159. rc = efx_ef10_filter_match_flags_from_mcdi(mcdi_flags);
  3160. if (rc < 0) {
  3161. netif_dbg(efx, probe, efx->net_dev,
  3162. "%s: fw flags %#x pri %u not supported in driver\n",
  3163. __func__, mcdi_flags, pd_match_pri);
  3164. } else {
  3165. netif_dbg(efx, probe, efx->net_dev,
  3166. "%s: fw flags %#x pri %u supported as driver flags %#x pri %u\n",
  3167. __func__, mcdi_flags, pd_match_pri,
  3168. rc, table->rx_match_count);
  3169. table->rx_match_flags[table->rx_match_count++] = rc;
  3170. }
  3171. }
  3172. table->entry = vzalloc(HUNT_FILTER_TBL_ROWS * sizeof(*table->entry));
  3173. if (!table->entry) {
  3174. rc = -ENOMEM;
  3175. goto fail;
  3176. }
  3177. table->ucdef_id = EFX_EF10_FILTER_ID_INVALID;
  3178. table->bcast_id = EFX_EF10_FILTER_ID_INVALID;
  3179. table->mcdef_id = EFX_EF10_FILTER_ID_INVALID;
  3180. efx->filter_state = table;
  3181. init_waitqueue_head(&table->waitq);
  3182. return 0;
  3183. fail:
  3184. kfree(table);
  3185. return rc;
  3186. }
  3187. /* Caller must hold efx->filter_sem for read if race against
  3188. * efx_ef10_filter_table_remove() is possible
  3189. */
  3190. static void efx_ef10_filter_table_restore(struct efx_nic *efx)
  3191. {
  3192. struct efx_ef10_filter_table *table = efx->filter_state;
  3193. struct efx_ef10_nic_data *nic_data = efx->nic_data;
  3194. struct efx_filter_spec *spec;
  3195. unsigned int filter_idx;
  3196. bool failed = false;
  3197. int rc;
  3198. WARN_ON(!rwsem_is_locked(&efx->filter_sem));
  3199. if (!nic_data->must_restore_filters)
  3200. return;
  3201. if (!table)
  3202. return;
  3203. spin_lock_bh(&efx->filter_lock);
  3204. for (filter_idx = 0; filter_idx < HUNT_FILTER_TBL_ROWS; filter_idx++) {
  3205. spec = efx_ef10_filter_entry_spec(table, filter_idx);
  3206. if (!spec)
  3207. continue;
  3208. table->entry[filter_idx].spec |= EFX_EF10_FILTER_FLAG_BUSY;
  3209. spin_unlock_bh(&efx->filter_lock);
  3210. rc = efx_ef10_filter_push(efx, spec,
  3211. &table->entry[filter_idx].handle,
  3212. false);
  3213. if (rc)
  3214. failed = true;
  3215. spin_lock_bh(&efx->filter_lock);
  3216. if (rc) {
  3217. kfree(spec);
  3218. efx_ef10_filter_set_entry(table, filter_idx, NULL, 0);
  3219. } else {
  3220. table->entry[filter_idx].spec &=
  3221. ~EFX_EF10_FILTER_FLAG_BUSY;
  3222. }
  3223. }
  3224. spin_unlock_bh(&efx->filter_lock);
  3225. if (failed)
  3226. netif_err(efx, hw, efx->net_dev,
  3227. "unable to restore all filters\n");
  3228. else
  3229. nic_data->must_restore_filters = false;
  3230. }
  3231. /* Caller must hold efx->filter_sem for write */
  3232. static void efx_ef10_filter_table_remove(struct efx_nic *efx)
  3233. {
  3234. struct efx_ef10_filter_table *table = efx->filter_state;
  3235. MCDI_DECLARE_BUF(inbuf, MC_CMD_FILTER_OP_IN_LEN);
  3236. struct efx_filter_spec *spec;
  3237. unsigned int filter_idx;
  3238. int rc;
  3239. efx->filter_state = NULL;
  3240. if (!table)
  3241. return;
  3242. for (filter_idx = 0; filter_idx < HUNT_FILTER_TBL_ROWS; filter_idx++) {
  3243. spec = efx_ef10_filter_entry_spec(table, filter_idx);
  3244. if (!spec)
  3245. continue;
  3246. MCDI_SET_DWORD(inbuf, FILTER_OP_IN_OP,
  3247. efx_ef10_filter_is_exclusive(spec) ?
  3248. MC_CMD_FILTER_OP_IN_OP_REMOVE :
  3249. MC_CMD_FILTER_OP_IN_OP_UNSUBSCRIBE);
  3250. MCDI_SET_QWORD(inbuf, FILTER_OP_IN_HANDLE,
  3251. table->entry[filter_idx].handle);
  3252. rc = efx_mcdi_rpc(efx, MC_CMD_FILTER_OP, inbuf, sizeof(inbuf),
  3253. NULL, 0, NULL);
  3254. if (rc)
  3255. netdev_WARN(efx->net_dev,
  3256. "filter_idx=%#x handle=%#llx\n",
  3257. filter_idx,
  3258. table->entry[filter_idx].handle);
  3259. kfree(spec);
  3260. }
  3261. vfree(table->entry);
  3262. kfree(table);
  3263. }
  3264. #define EFX_EF10_FILTER_DO_MARK_OLD(id) \
  3265. if (id != EFX_EF10_FILTER_ID_INVALID) { \
  3266. filter_idx = efx_ef10_filter_get_unsafe_id(efx, id); \
  3267. WARN_ON(!table->entry[filter_idx].spec); \
  3268. table->entry[filter_idx].spec |= EFX_EF10_FILTER_FLAG_AUTO_OLD; \
  3269. }
  3270. static void efx_ef10_filter_mark_old(struct efx_nic *efx)
  3271. {
  3272. struct efx_ef10_filter_table *table = efx->filter_state;
  3273. unsigned int filter_idx, i;
  3274. if (!table)
  3275. return;
  3276. /* Mark old filters that may need to be removed */
  3277. spin_lock_bh(&efx->filter_lock);
  3278. for (i = 0; i < table->dev_uc_count; i++)
  3279. EFX_EF10_FILTER_DO_MARK_OLD(table->dev_uc_list[i].id);
  3280. for (i = 0; i < table->dev_mc_count; i++)
  3281. EFX_EF10_FILTER_DO_MARK_OLD(table->dev_mc_list[i].id);
  3282. EFX_EF10_FILTER_DO_MARK_OLD(table->ucdef_id);
  3283. EFX_EF10_FILTER_DO_MARK_OLD(table->bcast_id);
  3284. EFX_EF10_FILTER_DO_MARK_OLD(table->mcdef_id);
  3285. spin_unlock_bh(&efx->filter_lock);
  3286. }
  3287. #undef EFX_EF10_FILTER_DO_MARK_OLD
  3288. static void efx_ef10_filter_uc_addr_list(struct efx_nic *efx, bool *promisc)
  3289. {
  3290. struct efx_ef10_filter_table *table = efx->filter_state;
  3291. struct net_device *net_dev = efx->net_dev;
  3292. struct netdev_hw_addr *uc;
  3293. int addr_count;
  3294. unsigned int i;
  3295. table->ucdef_id = EFX_EF10_FILTER_ID_INVALID;
  3296. addr_count = netdev_uc_count(net_dev);
  3297. if (net_dev->flags & IFF_PROMISC)
  3298. *promisc = true;
  3299. table->dev_uc_count = 1 + addr_count;
  3300. ether_addr_copy(table->dev_uc_list[0].addr, net_dev->dev_addr);
  3301. i = 1;
  3302. netdev_for_each_uc_addr(uc, net_dev) {
  3303. if (i >= EFX_EF10_FILTER_DEV_UC_MAX) {
  3304. *promisc = true;
  3305. break;
  3306. }
  3307. ether_addr_copy(table->dev_uc_list[i].addr, uc->addr);
  3308. table->dev_uc_list[i].id = EFX_EF10_FILTER_ID_INVALID;
  3309. i++;
  3310. }
  3311. }
  3312. static void efx_ef10_filter_mc_addr_list(struct efx_nic *efx, bool *promisc)
  3313. {
  3314. struct efx_ef10_filter_table *table = efx->filter_state;
  3315. struct net_device *net_dev = efx->net_dev;
  3316. struct netdev_hw_addr *mc;
  3317. unsigned int i, addr_count;
  3318. table->mcdef_id = EFX_EF10_FILTER_ID_INVALID;
  3319. table->bcast_id = EFX_EF10_FILTER_ID_INVALID;
  3320. if (net_dev->flags & (IFF_PROMISC | IFF_ALLMULTI))
  3321. *promisc = true;
  3322. addr_count = netdev_mc_count(net_dev);
  3323. i = 0;
  3324. netdev_for_each_mc_addr(mc, net_dev) {
  3325. if (i >= EFX_EF10_FILTER_DEV_MC_MAX) {
  3326. *promisc = true;
  3327. break;
  3328. }
  3329. ether_addr_copy(table->dev_mc_list[i].addr, mc->addr);
  3330. table->dev_mc_list[i].id = EFX_EF10_FILTER_ID_INVALID;
  3331. i++;
  3332. }
  3333. table->dev_mc_count = i;
  3334. }
  3335. static int efx_ef10_filter_insert_addr_list(struct efx_nic *efx,
  3336. bool multicast, bool rollback)
  3337. {
  3338. struct efx_ef10_filter_table *table = efx->filter_state;
  3339. struct efx_ef10_dev_addr *addr_list;
  3340. struct efx_filter_spec spec;
  3341. u8 baddr[ETH_ALEN];
  3342. unsigned int i, j;
  3343. int addr_count;
  3344. int rc;
  3345. if (multicast) {
  3346. addr_list = table->dev_mc_list;
  3347. addr_count = table->dev_mc_count;
  3348. } else {
  3349. addr_list = table->dev_uc_list;
  3350. addr_count = table->dev_uc_count;
  3351. }
  3352. /* Insert/renew filters */
  3353. for (i = 0; i < addr_count; i++) {
  3354. efx_filter_init_rx(&spec, EFX_FILTER_PRI_AUTO,
  3355. EFX_FILTER_FLAG_RX_RSS,
  3356. 0);
  3357. efx_filter_set_eth_local(&spec, EFX_FILTER_VID_UNSPEC,
  3358. addr_list[i].addr);
  3359. rc = efx_ef10_filter_insert(efx, &spec, true);
  3360. if (rc < 0) {
  3361. if (rollback) {
  3362. netif_info(efx, drv, efx->net_dev,
  3363. "efx_ef10_filter_insert failed rc=%d\n",
  3364. rc);
  3365. /* Fall back to promiscuous */
  3366. for (j = 0; j < i; j++) {
  3367. if (addr_list[j].id == EFX_EF10_FILTER_ID_INVALID)
  3368. continue;
  3369. efx_ef10_filter_remove_unsafe(
  3370. efx, EFX_FILTER_PRI_AUTO,
  3371. addr_list[j].id);
  3372. addr_list[j].id = EFX_EF10_FILTER_ID_INVALID;
  3373. }
  3374. return rc;
  3375. } else {
  3376. /* mark as not inserted, and carry on */
  3377. rc = EFX_EF10_FILTER_ID_INVALID;
  3378. }
  3379. }
  3380. addr_list[i].id = efx_ef10_filter_get_unsafe_id(efx, rc);
  3381. }
  3382. if (multicast && rollback) {
  3383. /* Also need an Ethernet broadcast filter */
  3384. efx_filter_init_rx(&spec, EFX_FILTER_PRI_AUTO,
  3385. EFX_FILTER_FLAG_RX_RSS,
  3386. 0);
  3387. eth_broadcast_addr(baddr);
  3388. efx_filter_set_eth_local(&spec, EFX_FILTER_VID_UNSPEC, baddr);
  3389. rc = efx_ef10_filter_insert(efx, &spec, true);
  3390. if (rc < 0) {
  3391. netif_warn(efx, drv, efx->net_dev,
  3392. "Broadcast filter insert failed rc=%d\n", rc);
  3393. /* Fall back to promiscuous */
  3394. for (j = 0; j < i; j++) {
  3395. if (addr_list[j].id == EFX_EF10_FILTER_ID_INVALID)
  3396. continue;
  3397. efx_ef10_filter_remove_unsafe(
  3398. efx, EFX_FILTER_PRI_AUTO,
  3399. addr_list[j].id);
  3400. addr_list[j].id = EFX_EF10_FILTER_ID_INVALID;
  3401. }
  3402. return rc;
  3403. } else {
  3404. table->bcast_id = efx_ef10_filter_get_unsafe_id(efx, rc);
  3405. }
  3406. }
  3407. return 0;
  3408. }
  3409. static int efx_ef10_filter_insert_def(struct efx_nic *efx, bool multicast,
  3410. bool rollback)
  3411. {
  3412. struct efx_ef10_filter_table *table = efx->filter_state;
  3413. struct efx_ef10_nic_data *nic_data = efx->nic_data;
  3414. struct efx_filter_spec spec;
  3415. u8 baddr[ETH_ALEN];
  3416. int rc;
  3417. efx_filter_init_rx(&spec, EFX_FILTER_PRI_AUTO,
  3418. EFX_FILTER_FLAG_RX_RSS,
  3419. 0);
  3420. if (multicast)
  3421. efx_filter_set_mc_def(&spec);
  3422. else
  3423. efx_filter_set_uc_def(&spec);
  3424. rc = efx_ef10_filter_insert(efx, &spec, true);
  3425. if (rc < 0) {
  3426. netif_warn(efx, drv, efx->net_dev,
  3427. "%scast mismatch filter insert failed rc=%d\n",
  3428. multicast ? "Multi" : "Uni", rc);
  3429. } else if (multicast) {
  3430. table->mcdef_id = efx_ef10_filter_get_unsafe_id(efx, rc);
  3431. if (!nic_data->workaround_26807) {
  3432. /* Also need an Ethernet broadcast filter */
  3433. efx_filter_init_rx(&spec, EFX_FILTER_PRI_AUTO,
  3434. EFX_FILTER_FLAG_RX_RSS,
  3435. 0);
  3436. eth_broadcast_addr(baddr);
  3437. efx_filter_set_eth_local(&spec, EFX_FILTER_VID_UNSPEC,
  3438. baddr);
  3439. rc = efx_ef10_filter_insert(efx, &spec, true);
  3440. if (rc < 0) {
  3441. netif_warn(efx, drv, efx->net_dev,
  3442. "Broadcast filter insert failed rc=%d\n",
  3443. rc);
  3444. if (rollback) {
  3445. /* Roll back the mc_def filter */
  3446. efx_ef10_filter_remove_unsafe(
  3447. efx, EFX_FILTER_PRI_AUTO,
  3448. table->mcdef_id);
  3449. table->mcdef_id = EFX_EF10_FILTER_ID_INVALID;
  3450. return rc;
  3451. }
  3452. } else {
  3453. table->bcast_id = efx_ef10_filter_get_unsafe_id(efx, rc);
  3454. }
  3455. }
  3456. rc = 0;
  3457. } else {
  3458. table->ucdef_id = rc;
  3459. rc = 0;
  3460. }
  3461. return rc;
  3462. }
  3463. /* Remove filters that weren't renewed. Since nothing else changes the AUTO_OLD
  3464. * flag or removes these filters, we don't need to hold the filter_lock while
  3465. * scanning for these filters.
  3466. */
  3467. static void efx_ef10_filter_remove_old(struct efx_nic *efx)
  3468. {
  3469. struct efx_ef10_filter_table *table = efx->filter_state;
  3470. bool remove_failed = false;
  3471. int i;
  3472. for (i = 0; i < HUNT_FILTER_TBL_ROWS; i++) {
  3473. if (ACCESS_ONCE(table->entry[i].spec) &
  3474. EFX_EF10_FILTER_FLAG_AUTO_OLD) {
  3475. if (efx_ef10_filter_remove_internal(
  3476. efx, 1U << EFX_FILTER_PRI_AUTO,
  3477. i, true) < 0)
  3478. remove_failed = true;
  3479. }
  3480. }
  3481. WARN_ON(remove_failed);
  3482. }
  3483. static int efx_ef10_vport_set_mac_address(struct efx_nic *efx)
  3484. {
  3485. struct efx_ef10_nic_data *nic_data = efx->nic_data;
  3486. u8 mac_old[ETH_ALEN];
  3487. int rc, rc2;
  3488. /* Only reconfigure a PF-created vport */
  3489. if (is_zero_ether_addr(nic_data->vport_mac))
  3490. return 0;
  3491. efx_device_detach_sync(efx);
  3492. efx_net_stop(efx->net_dev);
  3493. down_write(&efx->filter_sem);
  3494. efx_ef10_filter_table_remove(efx);
  3495. up_write(&efx->filter_sem);
  3496. rc = efx_ef10_vadaptor_free(efx, nic_data->vport_id);
  3497. if (rc)
  3498. goto restore_filters;
  3499. ether_addr_copy(mac_old, nic_data->vport_mac);
  3500. rc = efx_ef10_vport_del_mac(efx, nic_data->vport_id,
  3501. nic_data->vport_mac);
  3502. if (rc)
  3503. goto restore_vadaptor;
  3504. rc = efx_ef10_vport_add_mac(efx, nic_data->vport_id,
  3505. efx->net_dev->dev_addr);
  3506. if (!rc) {
  3507. ether_addr_copy(nic_data->vport_mac, efx->net_dev->dev_addr);
  3508. } else {
  3509. rc2 = efx_ef10_vport_add_mac(efx, nic_data->vport_id, mac_old);
  3510. if (rc2) {
  3511. /* Failed to add original MAC, so clear vport_mac */
  3512. eth_zero_addr(nic_data->vport_mac);
  3513. goto reset_nic;
  3514. }
  3515. }
  3516. restore_vadaptor:
  3517. rc2 = efx_ef10_vadaptor_alloc(efx, nic_data->vport_id);
  3518. if (rc2)
  3519. goto reset_nic;
  3520. restore_filters:
  3521. down_write(&efx->filter_sem);
  3522. rc2 = efx_ef10_filter_table_probe(efx);
  3523. up_write(&efx->filter_sem);
  3524. if (rc2)
  3525. goto reset_nic;
  3526. rc2 = efx_net_open(efx->net_dev);
  3527. if (rc2)
  3528. goto reset_nic;
  3529. netif_device_attach(efx->net_dev);
  3530. return rc;
  3531. reset_nic:
  3532. netif_err(efx, drv, efx->net_dev,
  3533. "Failed to restore when changing MAC address - scheduling reset\n");
  3534. efx_schedule_reset(efx, RESET_TYPE_DATAPATH);
  3535. return rc ? rc : rc2;
  3536. }
  3537. /* Caller must hold efx->filter_sem for read if race against
  3538. * efx_ef10_filter_table_remove() is possible
  3539. */
  3540. static void efx_ef10_filter_sync_rx_mode(struct efx_nic *efx)
  3541. {
  3542. struct efx_ef10_filter_table *table = efx->filter_state;
  3543. struct efx_ef10_nic_data *nic_data = efx->nic_data;
  3544. struct net_device *net_dev = efx->net_dev;
  3545. bool uc_promisc = false, mc_promisc = false;
  3546. if (!efx_dev_registered(efx))
  3547. return;
  3548. if (!table)
  3549. return;
  3550. efx_ef10_filter_mark_old(efx);
  3551. /* Copy/convert the address lists; add the primary station
  3552. * address and broadcast address
  3553. */
  3554. netif_addr_lock_bh(net_dev);
  3555. efx_ef10_filter_uc_addr_list(efx, &uc_promisc);
  3556. efx_ef10_filter_mc_addr_list(efx, &mc_promisc);
  3557. netif_addr_unlock_bh(net_dev);
  3558. /* Insert/renew unicast filters */
  3559. if (uc_promisc) {
  3560. efx_ef10_filter_insert_def(efx, false, false);
  3561. efx_ef10_filter_insert_addr_list(efx, false, false);
  3562. } else {
  3563. /* If any of the filters failed to insert, fall back to
  3564. * promiscuous mode - add in the uc_def filter. But keep
  3565. * our individual unicast filters.
  3566. */
  3567. if (efx_ef10_filter_insert_addr_list(efx, false, false))
  3568. efx_ef10_filter_insert_def(efx, false, false);
  3569. }
  3570. /* Insert/renew multicast filters */
  3571. /* If changing promiscuous state with cascaded multicast filters, remove
  3572. * old filters first, so that packets are dropped rather than duplicated
  3573. */
  3574. if (nic_data->workaround_26807 && efx->mc_promisc != mc_promisc)
  3575. efx_ef10_filter_remove_old(efx);
  3576. if (mc_promisc) {
  3577. if (nic_data->workaround_26807) {
  3578. /* If we failed to insert promiscuous filters, rollback
  3579. * and fall back to individual multicast filters
  3580. */
  3581. if (efx_ef10_filter_insert_def(efx, true, true)) {
  3582. /* Changing promisc state, so remove old filters */
  3583. efx_ef10_filter_remove_old(efx);
  3584. efx_ef10_filter_insert_addr_list(efx, true, false);
  3585. }
  3586. } else {
  3587. /* If we failed to insert promiscuous filters, don't
  3588. * rollback. Regardless, also insert the mc_list
  3589. */
  3590. efx_ef10_filter_insert_def(efx, true, false);
  3591. efx_ef10_filter_insert_addr_list(efx, true, false);
  3592. }
  3593. } else {
  3594. /* If any filters failed to insert, rollback and fall back to
  3595. * promiscuous mode - mc_def filter and maybe broadcast. If
  3596. * that fails, roll back again and insert as many of our
  3597. * individual multicast filters as we can.
  3598. */
  3599. if (efx_ef10_filter_insert_addr_list(efx, true, true)) {
  3600. /* Changing promisc state, so remove old filters */
  3601. if (nic_data->workaround_26807)
  3602. efx_ef10_filter_remove_old(efx);
  3603. if (efx_ef10_filter_insert_def(efx, true, true))
  3604. efx_ef10_filter_insert_addr_list(efx, true, false);
  3605. }
  3606. }
  3607. efx_ef10_filter_remove_old(efx);
  3608. efx->mc_promisc = mc_promisc;
  3609. }
  3610. static int efx_ef10_set_mac_address(struct efx_nic *efx)
  3611. {
  3612. MCDI_DECLARE_BUF(inbuf, MC_CMD_VADAPTOR_SET_MAC_IN_LEN);
  3613. struct efx_ef10_nic_data *nic_data = efx->nic_data;
  3614. bool was_enabled = efx->port_enabled;
  3615. int rc;
  3616. efx_device_detach_sync(efx);
  3617. efx_net_stop(efx->net_dev);
  3618. down_write(&efx->filter_sem);
  3619. efx_ef10_filter_table_remove(efx);
  3620. ether_addr_copy(MCDI_PTR(inbuf, VADAPTOR_SET_MAC_IN_MACADDR),
  3621. efx->net_dev->dev_addr);
  3622. MCDI_SET_DWORD(inbuf, VADAPTOR_SET_MAC_IN_UPSTREAM_PORT_ID,
  3623. nic_data->vport_id);
  3624. rc = efx_mcdi_rpc_quiet(efx, MC_CMD_VADAPTOR_SET_MAC, inbuf,
  3625. sizeof(inbuf), NULL, 0, NULL);
  3626. efx_ef10_filter_table_probe(efx);
  3627. up_write(&efx->filter_sem);
  3628. if (was_enabled)
  3629. efx_net_open(efx->net_dev);
  3630. netif_device_attach(efx->net_dev);
  3631. #ifdef CONFIG_SFC_SRIOV
  3632. if (efx->pci_dev->is_virtfn && efx->pci_dev->physfn) {
  3633. struct pci_dev *pci_dev_pf = efx->pci_dev->physfn;
  3634. if (rc == -EPERM) {
  3635. struct efx_nic *efx_pf;
  3636. /* Switch to PF and change MAC address on vport */
  3637. efx_pf = pci_get_drvdata(pci_dev_pf);
  3638. rc = efx_ef10_sriov_set_vf_mac(efx_pf,
  3639. nic_data->vf_index,
  3640. efx->net_dev->dev_addr);
  3641. } else if (!rc) {
  3642. struct efx_nic *efx_pf = pci_get_drvdata(pci_dev_pf);
  3643. struct efx_ef10_nic_data *nic_data = efx_pf->nic_data;
  3644. unsigned int i;
  3645. /* MAC address successfully changed by VF (with MAC
  3646. * spoofing) so update the parent PF if possible.
  3647. */
  3648. for (i = 0; i < efx_pf->vf_count; ++i) {
  3649. struct ef10_vf *vf = nic_data->vf + i;
  3650. if (vf->efx == efx) {
  3651. ether_addr_copy(vf->mac,
  3652. efx->net_dev->dev_addr);
  3653. return 0;
  3654. }
  3655. }
  3656. }
  3657. } else
  3658. #endif
  3659. if (rc == -EPERM) {
  3660. netif_err(efx, drv, efx->net_dev,
  3661. "Cannot change MAC address; use sfboot to enable"
  3662. " mac-spoofing on this interface\n");
  3663. } else if (rc == -ENOSYS && !efx_ef10_is_vf(efx)) {
  3664. /* If the active MCFW does not support MC_CMD_VADAPTOR_SET_MAC
  3665. * fall-back to the method of changing the MAC address on the
  3666. * vport. This only applies to PFs because such versions of
  3667. * MCFW do not support VFs.
  3668. */
  3669. rc = efx_ef10_vport_set_mac_address(efx);
  3670. } else {
  3671. efx_mcdi_display_error(efx, MC_CMD_VADAPTOR_SET_MAC,
  3672. sizeof(inbuf), NULL, 0, rc);
  3673. }
  3674. return rc;
  3675. }
  3676. static int efx_ef10_mac_reconfigure(struct efx_nic *efx)
  3677. {
  3678. efx_ef10_filter_sync_rx_mode(efx);
  3679. return efx_mcdi_set_mac(efx);
  3680. }
  3681. static int efx_ef10_mac_reconfigure_vf(struct efx_nic *efx)
  3682. {
  3683. efx_ef10_filter_sync_rx_mode(efx);
  3684. return 0;
  3685. }
  3686. static int efx_ef10_start_bist(struct efx_nic *efx, u32 bist_type)
  3687. {
  3688. MCDI_DECLARE_BUF(inbuf, MC_CMD_START_BIST_IN_LEN);
  3689. MCDI_SET_DWORD(inbuf, START_BIST_IN_TYPE, bist_type);
  3690. return efx_mcdi_rpc(efx, MC_CMD_START_BIST, inbuf, sizeof(inbuf),
  3691. NULL, 0, NULL);
  3692. }
  3693. /* MC BISTs follow a different poll mechanism to phy BISTs.
  3694. * The BIST is done in the poll handler on the MC, and the MCDI command
  3695. * will block until the BIST is done.
  3696. */
  3697. static int efx_ef10_poll_bist(struct efx_nic *efx)
  3698. {
  3699. int rc;
  3700. MCDI_DECLARE_BUF(outbuf, MC_CMD_POLL_BIST_OUT_LEN);
  3701. size_t outlen;
  3702. u32 result;
  3703. rc = efx_mcdi_rpc(efx, MC_CMD_POLL_BIST, NULL, 0,
  3704. outbuf, sizeof(outbuf), &outlen);
  3705. if (rc != 0)
  3706. return rc;
  3707. if (outlen < MC_CMD_POLL_BIST_OUT_LEN)
  3708. return -EIO;
  3709. result = MCDI_DWORD(outbuf, POLL_BIST_OUT_RESULT);
  3710. switch (result) {
  3711. case MC_CMD_POLL_BIST_PASSED:
  3712. netif_dbg(efx, hw, efx->net_dev, "BIST passed.\n");
  3713. return 0;
  3714. case MC_CMD_POLL_BIST_TIMEOUT:
  3715. netif_err(efx, hw, efx->net_dev, "BIST timed out\n");
  3716. return -EIO;
  3717. case MC_CMD_POLL_BIST_FAILED:
  3718. netif_err(efx, hw, efx->net_dev, "BIST failed.\n");
  3719. return -EIO;
  3720. default:
  3721. netif_err(efx, hw, efx->net_dev,
  3722. "BIST returned unknown result %u", result);
  3723. return -EIO;
  3724. }
  3725. }
  3726. static int efx_ef10_run_bist(struct efx_nic *efx, u32 bist_type)
  3727. {
  3728. int rc;
  3729. netif_dbg(efx, drv, efx->net_dev, "starting BIST type %u\n", bist_type);
  3730. rc = efx_ef10_start_bist(efx, bist_type);
  3731. if (rc != 0)
  3732. return rc;
  3733. return efx_ef10_poll_bist(efx);
  3734. }
  3735. static int
  3736. efx_ef10_test_chip(struct efx_nic *efx, struct efx_self_tests *tests)
  3737. {
  3738. int rc, rc2;
  3739. efx_reset_down(efx, RESET_TYPE_WORLD);
  3740. rc = efx_mcdi_rpc(efx, MC_CMD_ENABLE_OFFLINE_BIST,
  3741. NULL, 0, NULL, 0, NULL);
  3742. if (rc != 0)
  3743. goto out;
  3744. tests->memory = efx_ef10_run_bist(efx, MC_CMD_MC_MEM_BIST) ? -1 : 1;
  3745. tests->registers = efx_ef10_run_bist(efx, MC_CMD_REG_BIST) ? -1 : 1;
  3746. rc = efx_mcdi_reset(efx, RESET_TYPE_WORLD);
  3747. out:
  3748. if (rc == -EPERM)
  3749. rc = 0;
  3750. rc2 = efx_reset_up(efx, RESET_TYPE_WORLD, rc == 0);
  3751. return rc ? rc : rc2;
  3752. }
  3753. #ifdef CONFIG_SFC_MTD
  3754. struct efx_ef10_nvram_type_info {
  3755. u16 type, type_mask;
  3756. u8 port;
  3757. const char *name;
  3758. };
  3759. static const struct efx_ef10_nvram_type_info efx_ef10_nvram_types[] = {
  3760. { NVRAM_PARTITION_TYPE_MC_FIRMWARE, 0, 0, "sfc_mcfw" },
  3761. { NVRAM_PARTITION_TYPE_MC_FIRMWARE_BACKUP, 0, 0, "sfc_mcfw_backup" },
  3762. { NVRAM_PARTITION_TYPE_EXPANSION_ROM, 0, 0, "sfc_exp_rom" },
  3763. { NVRAM_PARTITION_TYPE_STATIC_CONFIG, 0, 0, "sfc_static_cfg" },
  3764. { NVRAM_PARTITION_TYPE_DYNAMIC_CONFIG, 0, 0, "sfc_dynamic_cfg" },
  3765. { NVRAM_PARTITION_TYPE_EXPROM_CONFIG_PORT0, 0, 0, "sfc_exp_rom_cfg" },
  3766. { NVRAM_PARTITION_TYPE_EXPROM_CONFIG_PORT1, 0, 1, "sfc_exp_rom_cfg" },
  3767. { NVRAM_PARTITION_TYPE_EXPROM_CONFIG_PORT2, 0, 2, "sfc_exp_rom_cfg" },
  3768. { NVRAM_PARTITION_TYPE_EXPROM_CONFIG_PORT3, 0, 3, "sfc_exp_rom_cfg" },
  3769. { NVRAM_PARTITION_TYPE_LICENSE, 0, 0, "sfc_license" },
  3770. { NVRAM_PARTITION_TYPE_PHY_MIN, 0xff, 0, "sfc_phy_fw" },
  3771. };
  3772. static int efx_ef10_mtd_probe_partition(struct efx_nic *efx,
  3773. struct efx_mcdi_mtd_partition *part,
  3774. unsigned int type)
  3775. {
  3776. MCDI_DECLARE_BUF(inbuf, MC_CMD_NVRAM_METADATA_IN_LEN);
  3777. MCDI_DECLARE_BUF(outbuf, MC_CMD_NVRAM_METADATA_OUT_LENMAX);
  3778. const struct efx_ef10_nvram_type_info *info;
  3779. size_t size, erase_size, outlen;
  3780. bool protected;
  3781. int rc;
  3782. for (info = efx_ef10_nvram_types; ; info++) {
  3783. if (info ==
  3784. efx_ef10_nvram_types + ARRAY_SIZE(efx_ef10_nvram_types))
  3785. return -ENODEV;
  3786. if ((type & ~info->type_mask) == info->type)
  3787. break;
  3788. }
  3789. if (info->port != efx_port_num(efx))
  3790. return -ENODEV;
  3791. rc = efx_mcdi_nvram_info(efx, type, &size, &erase_size, &protected);
  3792. if (rc)
  3793. return rc;
  3794. if (protected)
  3795. return -ENODEV; /* hide it */
  3796. part->nvram_type = type;
  3797. MCDI_SET_DWORD(inbuf, NVRAM_METADATA_IN_TYPE, type);
  3798. rc = efx_mcdi_rpc(efx, MC_CMD_NVRAM_METADATA, inbuf, sizeof(inbuf),
  3799. outbuf, sizeof(outbuf), &outlen);
  3800. if (rc)
  3801. return rc;
  3802. if (outlen < MC_CMD_NVRAM_METADATA_OUT_LENMIN)
  3803. return -EIO;
  3804. if (MCDI_DWORD(outbuf, NVRAM_METADATA_OUT_FLAGS) &
  3805. (1 << MC_CMD_NVRAM_METADATA_OUT_SUBTYPE_VALID_LBN))
  3806. part->fw_subtype = MCDI_DWORD(outbuf,
  3807. NVRAM_METADATA_OUT_SUBTYPE);
  3808. part->common.dev_type_name = "EF10 NVRAM manager";
  3809. part->common.type_name = info->name;
  3810. part->common.mtd.type = MTD_NORFLASH;
  3811. part->common.mtd.flags = MTD_CAP_NORFLASH;
  3812. part->common.mtd.size = size;
  3813. part->common.mtd.erasesize = erase_size;
  3814. return 0;
  3815. }
  3816. static int efx_ef10_mtd_probe(struct efx_nic *efx)
  3817. {
  3818. MCDI_DECLARE_BUF(outbuf, MC_CMD_NVRAM_PARTITIONS_OUT_LENMAX);
  3819. struct efx_mcdi_mtd_partition *parts;
  3820. size_t outlen, n_parts_total, i, n_parts;
  3821. unsigned int type;
  3822. int rc;
  3823. ASSERT_RTNL();
  3824. BUILD_BUG_ON(MC_CMD_NVRAM_PARTITIONS_IN_LEN != 0);
  3825. rc = efx_mcdi_rpc(efx, MC_CMD_NVRAM_PARTITIONS, NULL, 0,
  3826. outbuf, sizeof(outbuf), &outlen);
  3827. if (rc)
  3828. return rc;
  3829. if (outlen < MC_CMD_NVRAM_PARTITIONS_OUT_LENMIN)
  3830. return -EIO;
  3831. n_parts_total = MCDI_DWORD(outbuf, NVRAM_PARTITIONS_OUT_NUM_PARTITIONS);
  3832. if (n_parts_total >
  3833. MCDI_VAR_ARRAY_LEN(outlen, NVRAM_PARTITIONS_OUT_TYPE_ID))
  3834. return -EIO;
  3835. parts = kcalloc(n_parts_total, sizeof(*parts), GFP_KERNEL);
  3836. if (!parts)
  3837. return -ENOMEM;
  3838. n_parts = 0;
  3839. for (i = 0; i < n_parts_total; i++) {
  3840. type = MCDI_ARRAY_DWORD(outbuf, NVRAM_PARTITIONS_OUT_TYPE_ID,
  3841. i);
  3842. rc = efx_ef10_mtd_probe_partition(efx, &parts[n_parts], type);
  3843. if (rc == 0)
  3844. n_parts++;
  3845. else if (rc != -ENODEV)
  3846. goto fail;
  3847. }
  3848. rc = efx_mtd_add(efx, &parts[0].common, n_parts, sizeof(*parts));
  3849. fail:
  3850. if (rc)
  3851. kfree(parts);
  3852. return rc;
  3853. }
  3854. #endif /* CONFIG_SFC_MTD */
  3855. static void efx_ef10_ptp_write_host_time(struct efx_nic *efx, u32 host_time)
  3856. {
  3857. _efx_writed(efx, cpu_to_le32(host_time), ER_DZ_MC_DB_LWRD);
  3858. }
  3859. static void efx_ef10_ptp_write_host_time_vf(struct efx_nic *efx,
  3860. u32 host_time) {}
  3861. static int efx_ef10_rx_enable_timestamping(struct efx_channel *channel,
  3862. bool temp)
  3863. {
  3864. MCDI_DECLARE_BUF(inbuf, MC_CMD_PTP_IN_TIME_EVENT_SUBSCRIBE_LEN);
  3865. int rc;
  3866. if (channel->sync_events_state == SYNC_EVENTS_REQUESTED ||
  3867. channel->sync_events_state == SYNC_EVENTS_VALID ||
  3868. (temp && channel->sync_events_state == SYNC_EVENTS_DISABLED))
  3869. return 0;
  3870. channel->sync_events_state = SYNC_EVENTS_REQUESTED;
  3871. MCDI_SET_DWORD(inbuf, PTP_IN_OP, MC_CMD_PTP_OP_TIME_EVENT_SUBSCRIBE);
  3872. MCDI_SET_DWORD(inbuf, PTP_IN_PERIPH_ID, 0);
  3873. MCDI_SET_DWORD(inbuf, PTP_IN_TIME_EVENT_SUBSCRIBE_QUEUE,
  3874. channel->channel);
  3875. rc = efx_mcdi_rpc(channel->efx, MC_CMD_PTP,
  3876. inbuf, sizeof(inbuf), NULL, 0, NULL);
  3877. if (rc != 0)
  3878. channel->sync_events_state = temp ? SYNC_EVENTS_QUIESCENT :
  3879. SYNC_EVENTS_DISABLED;
  3880. return rc;
  3881. }
  3882. static int efx_ef10_rx_disable_timestamping(struct efx_channel *channel,
  3883. bool temp)
  3884. {
  3885. MCDI_DECLARE_BUF(inbuf, MC_CMD_PTP_IN_TIME_EVENT_UNSUBSCRIBE_LEN);
  3886. int rc;
  3887. if (channel->sync_events_state == SYNC_EVENTS_DISABLED ||
  3888. (temp && channel->sync_events_state == SYNC_EVENTS_QUIESCENT))
  3889. return 0;
  3890. if (channel->sync_events_state == SYNC_EVENTS_QUIESCENT) {
  3891. channel->sync_events_state = SYNC_EVENTS_DISABLED;
  3892. return 0;
  3893. }
  3894. channel->sync_events_state = temp ? SYNC_EVENTS_QUIESCENT :
  3895. SYNC_EVENTS_DISABLED;
  3896. MCDI_SET_DWORD(inbuf, PTP_IN_OP, MC_CMD_PTP_OP_TIME_EVENT_UNSUBSCRIBE);
  3897. MCDI_SET_DWORD(inbuf, PTP_IN_PERIPH_ID, 0);
  3898. MCDI_SET_DWORD(inbuf, PTP_IN_TIME_EVENT_UNSUBSCRIBE_CONTROL,
  3899. MC_CMD_PTP_IN_TIME_EVENT_UNSUBSCRIBE_SINGLE);
  3900. MCDI_SET_DWORD(inbuf, PTP_IN_TIME_EVENT_UNSUBSCRIBE_QUEUE,
  3901. channel->channel);
  3902. rc = efx_mcdi_rpc(channel->efx, MC_CMD_PTP,
  3903. inbuf, sizeof(inbuf), NULL, 0, NULL);
  3904. return rc;
  3905. }
  3906. static int efx_ef10_ptp_set_ts_sync_events(struct efx_nic *efx, bool en,
  3907. bool temp)
  3908. {
  3909. int (*set)(struct efx_channel *channel, bool temp);
  3910. struct efx_channel *channel;
  3911. set = en ?
  3912. efx_ef10_rx_enable_timestamping :
  3913. efx_ef10_rx_disable_timestamping;
  3914. efx_for_each_channel(channel, efx) {
  3915. int rc = set(channel, temp);
  3916. if (en && rc != 0) {
  3917. efx_ef10_ptp_set_ts_sync_events(efx, false, temp);
  3918. return rc;
  3919. }
  3920. }
  3921. return 0;
  3922. }
  3923. static int efx_ef10_ptp_set_ts_config_vf(struct efx_nic *efx,
  3924. struct hwtstamp_config *init)
  3925. {
  3926. return -EOPNOTSUPP;
  3927. }
  3928. static int efx_ef10_ptp_set_ts_config(struct efx_nic *efx,
  3929. struct hwtstamp_config *init)
  3930. {
  3931. int rc;
  3932. switch (init->rx_filter) {
  3933. case HWTSTAMP_FILTER_NONE:
  3934. efx_ef10_ptp_set_ts_sync_events(efx, false, false);
  3935. /* if TX timestamping is still requested then leave PTP on */
  3936. return efx_ptp_change_mode(efx,
  3937. init->tx_type != HWTSTAMP_TX_OFF, 0);
  3938. case HWTSTAMP_FILTER_ALL:
  3939. case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
  3940. case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
  3941. case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
  3942. case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
  3943. case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
  3944. case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
  3945. case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
  3946. case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
  3947. case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
  3948. case HWTSTAMP_FILTER_PTP_V2_EVENT:
  3949. case HWTSTAMP_FILTER_PTP_V2_SYNC:
  3950. case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
  3951. init->rx_filter = HWTSTAMP_FILTER_ALL;
  3952. rc = efx_ptp_change_mode(efx, true, 0);
  3953. if (!rc)
  3954. rc = efx_ef10_ptp_set_ts_sync_events(efx, true, false);
  3955. if (rc)
  3956. efx_ptp_change_mode(efx, false, 0);
  3957. return rc;
  3958. default:
  3959. return -ERANGE;
  3960. }
  3961. }
  3962. const struct efx_nic_type efx_hunt_a0_vf_nic_type = {
  3963. .is_vf = true,
  3964. .mem_bar = EFX_MEM_VF_BAR,
  3965. .mem_map_size = efx_ef10_mem_map_size,
  3966. .probe = efx_ef10_probe_vf,
  3967. .remove = efx_ef10_remove,
  3968. .dimension_resources = efx_ef10_dimension_resources,
  3969. .init = efx_ef10_init_nic,
  3970. .fini = efx_port_dummy_op_void,
  3971. .map_reset_reason = efx_ef10_map_reset_reason,
  3972. .map_reset_flags = efx_ef10_map_reset_flags,
  3973. .reset = efx_ef10_reset,
  3974. .probe_port = efx_mcdi_port_probe,
  3975. .remove_port = efx_mcdi_port_remove,
  3976. .fini_dmaq = efx_ef10_fini_dmaq,
  3977. .prepare_flr = efx_ef10_prepare_flr,
  3978. .finish_flr = efx_port_dummy_op_void,
  3979. .describe_stats = efx_ef10_describe_stats,
  3980. .update_stats = efx_ef10_update_stats_vf,
  3981. .start_stats = efx_port_dummy_op_void,
  3982. .pull_stats = efx_port_dummy_op_void,
  3983. .stop_stats = efx_port_dummy_op_void,
  3984. .set_id_led = efx_mcdi_set_id_led,
  3985. .push_irq_moderation = efx_ef10_push_irq_moderation,
  3986. .reconfigure_mac = efx_ef10_mac_reconfigure_vf,
  3987. .check_mac_fault = efx_mcdi_mac_check_fault,
  3988. .reconfigure_port = efx_mcdi_port_reconfigure,
  3989. .get_wol = efx_ef10_get_wol_vf,
  3990. .set_wol = efx_ef10_set_wol_vf,
  3991. .resume_wol = efx_port_dummy_op_void,
  3992. .mcdi_request = efx_ef10_mcdi_request,
  3993. .mcdi_poll_response = efx_ef10_mcdi_poll_response,
  3994. .mcdi_read_response = efx_ef10_mcdi_read_response,
  3995. .mcdi_poll_reboot = efx_ef10_mcdi_poll_reboot,
  3996. .irq_enable_master = efx_port_dummy_op_void,
  3997. .irq_test_generate = efx_ef10_irq_test_generate,
  3998. .irq_disable_non_ev = efx_port_dummy_op_void,
  3999. .irq_handle_msi = efx_ef10_msi_interrupt,
  4000. .irq_handle_legacy = efx_ef10_legacy_interrupt,
  4001. .tx_probe = efx_ef10_tx_probe,
  4002. .tx_init = efx_ef10_tx_init,
  4003. .tx_remove = efx_ef10_tx_remove,
  4004. .tx_write = efx_ef10_tx_write,
  4005. .rx_push_rss_config = efx_ef10_vf_rx_push_rss_config,
  4006. .rx_probe = efx_ef10_rx_probe,
  4007. .rx_init = efx_ef10_rx_init,
  4008. .rx_remove = efx_ef10_rx_remove,
  4009. .rx_write = efx_ef10_rx_write,
  4010. .rx_defer_refill = efx_ef10_rx_defer_refill,
  4011. .ev_probe = efx_ef10_ev_probe,
  4012. .ev_init = efx_ef10_ev_init,
  4013. .ev_fini = efx_ef10_ev_fini,
  4014. .ev_remove = efx_ef10_ev_remove,
  4015. .ev_process = efx_ef10_ev_process,
  4016. .ev_read_ack = efx_ef10_ev_read_ack,
  4017. .ev_test_generate = efx_ef10_ev_test_generate,
  4018. .filter_table_probe = efx_ef10_filter_table_probe,
  4019. .filter_table_restore = efx_ef10_filter_table_restore,
  4020. .filter_table_remove = efx_ef10_filter_table_remove,
  4021. .filter_update_rx_scatter = efx_ef10_filter_update_rx_scatter,
  4022. .filter_insert = efx_ef10_filter_insert,
  4023. .filter_remove_safe = efx_ef10_filter_remove_safe,
  4024. .filter_get_safe = efx_ef10_filter_get_safe,
  4025. .filter_clear_rx = efx_ef10_filter_clear_rx,
  4026. .filter_count_rx_used = efx_ef10_filter_count_rx_used,
  4027. .filter_get_rx_id_limit = efx_ef10_filter_get_rx_id_limit,
  4028. .filter_get_rx_ids = efx_ef10_filter_get_rx_ids,
  4029. #ifdef CONFIG_RFS_ACCEL
  4030. .filter_rfs_insert = efx_ef10_filter_rfs_insert,
  4031. .filter_rfs_expire_one = efx_ef10_filter_rfs_expire_one,
  4032. #endif
  4033. #ifdef CONFIG_SFC_MTD
  4034. .mtd_probe = efx_port_dummy_op_int,
  4035. #endif
  4036. .ptp_write_host_time = efx_ef10_ptp_write_host_time_vf,
  4037. .ptp_set_ts_config = efx_ef10_ptp_set_ts_config_vf,
  4038. #ifdef CONFIG_SFC_SRIOV
  4039. .vswitching_probe = efx_ef10_vswitching_probe_vf,
  4040. .vswitching_restore = efx_ef10_vswitching_restore_vf,
  4041. .vswitching_remove = efx_ef10_vswitching_remove_vf,
  4042. .sriov_get_phys_port_id = efx_ef10_sriov_get_phys_port_id,
  4043. #endif
  4044. .get_mac_address = efx_ef10_get_mac_address_vf,
  4045. .set_mac_address = efx_ef10_set_mac_address,
  4046. .revision = EFX_REV_HUNT_A0,
  4047. .max_dma_mask = DMA_BIT_MASK(ESF_DZ_TX_KER_BUF_ADDR_WIDTH),
  4048. .rx_prefix_size = ES_DZ_RX_PREFIX_SIZE,
  4049. .rx_hash_offset = ES_DZ_RX_PREFIX_HASH_OFST,
  4050. .rx_ts_offset = ES_DZ_RX_PREFIX_TSTAMP_OFST,
  4051. .can_rx_scatter = true,
  4052. .always_rx_scatter = true,
  4053. .max_interrupt_mode = EFX_INT_MODE_MSIX,
  4054. .timer_period_max = 1 << ERF_DD_EVQ_IND_TIMER_VAL_WIDTH,
  4055. .offload_features = (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
  4056. NETIF_F_RXHASH | NETIF_F_NTUPLE),
  4057. .mcdi_max_ver = 2,
  4058. .max_rx_ip_filters = HUNT_FILTER_TBL_ROWS,
  4059. .hwtstamp_filters = 1 << HWTSTAMP_FILTER_NONE |
  4060. 1 << HWTSTAMP_FILTER_ALL,
  4061. };
  4062. const struct efx_nic_type efx_hunt_a0_nic_type = {
  4063. .is_vf = false,
  4064. .mem_bar = EFX_MEM_BAR,
  4065. .mem_map_size = efx_ef10_mem_map_size,
  4066. .probe = efx_ef10_probe_pf,
  4067. .remove = efx_ef10_remove,
  4068. .dimension_resources = efx_ef10_dimension_resources,
  4069. .init = efx_ef10_init_nic,
  4070. .fini = efx_port_dummy_op_void,
  4071. .map_reset_reason = efx_ef10_map_reset_reason,
  4072. .map_reset_flags = efx_ef10_map_reset_flags,
  4073. .reset = efx_ef10_reset,
  4074. .probe_port = efx_mcdi_port_probe,
  4075. .remove_port = efx_mcdi_port_remove,
  4076. .fini_dmaq = efx_ef10_fini_dmaq,
  4077. .prepare_flr = efx_ef10_prepare_flr,
  4078. .finish_flr = efx_port_dummy_op_void,
  4079. .describe_stats = efx_ef10_describe_stats,
  4080. .update_stats = efx_ef10_update_stats_pf,
  4081. .start_stats = efx_mcdi_mac_start_stats,
  4082. .pull_stats = efx_mcdi_mac_pull_stats,
  4083. .stop_stats = efx_mcdi_mac_stop_stats,
  4084. .set_id_led = efx_mcdi_set_id_led,
  4085. .push_irq_moderation = efx_ef10_push_irq_moderation,
  4086. .reconfigure_mac = efx_ef10_mac_reconfigure,
  4087. .check_mac_fault = efx_mcdi_mac_check_fault,
  4088. .reconfigure_port = efx_mcdi_port_reconfigure,
  4089. .get_wol = efx_ef10_get_wol,
  4090. .set_wol = efx_ef10_set_wol,
  4091. .resume_wol = efx_port_dummy_op_void,
  4092. .test_chip = efx_ef10_test_chip,
  4093. .test_nvram = efx_mcdi_nvram_test_all,
  4094. .mcdi_request = efx_ef10_mcdi_request,
  4095. .mcdi_poll_response = efx_ef10_mcdi_poll_response,
  4096. .mcdi_read_response = efx_ef10_mcdi_read_response,
  4097. .mcdi_poll_reboot = efx_ef10_mcdi_poll_reboot,
  4098. .irq_enable_master = efx_port_dummy_op_void,
  4099. .irq_test_generate = efx_ef10_irq_test_generate,
  4100. .irq_disable_non_ev = efx_port_dummy_op_void,
  4101. .irq_handle_msi = efx_ef10_msi_interrupt,
  4102. .irq_handle_legacy = efx_ef10_legacy_interrupt,
  4103. .tx_probe = efx_ef10_tx_probe,
  4104. .tx_init = efx_ef10_tx_init,
  4105. .tx_remove = efx_ef10_tx_remove,
  4106. .tx_write = efx_ef10_tx_write,
  4107. .rx_push_rss_config = efx_ef10_pf_rx_push_rss_config,
  4108. .rx_probe = efx_ef10_rx_probe,
  4109. .rx_init = efx_ef10_rx_init,
  4110. .rx_remove = efx_ef10_rx_remove,
  4111. .rx_write = efx_ef10_rx_write,
  4112. .rx_defer_refill = efx_ef10_rx_defer_refill,
  4113. .ev_probe = efx_ef10_ev_probe,
  4114. .ev_init = efx_ef10_ev_init,
  4115. .ev_fini = efx_ef10_ev_fini,
  4116. .ev_remove = efx_ef10_ev_remove,
  4117. .ev_process = efx_ef10_ev_process,
  4118. .ev_read_ack = efx_ef10_ev_read_ack,
  4119. .ev_test_generate = efx_ef10_ev_test_generate,
  4120. .filter_table_probe = efx_ef10_filter_table_probe,
  4121. .filter_table_restore = efx_ef10_filter_table_restore,
  4122. .filter_table_remove = efx_ef10_filter_table_remove,
  4123. .filter_update_rx_scatter = efx_ef10_filter_update_rx_scatter,
  4124. .filter_insert = efx_ef10_filter_insert,
  4125. .filter_remove_safe = efx_ef10_filter_remove_safe,
  4126. .filter_get_safe = efx_ef10_filter_get_safe,
  4127. .filter_clear_rx = efx_ef10_filter_clear_rx,
  4128. .filter_count_rx_used = efx_ef10_filter_count_rx_used,
  4129. .filter_get_rx_id_limit = efx_ef10_filter_get_rx_id_limit,
  4130. .filter_get_rx_ids = efx_ef10_filter_get_rx_ids,
  4131. #ifdef CONFIG_RFS_ACCEL
  4132. .filter_rfs_insert = efx_ef10_filter_rfs_insert,
  4133. .filter_rfs_expire_one = efx_ef10_filter_rfs_expire_one,
  4134. #endif
  4135. #ifdef CONFIG_SFC_MTD
  4136. .mtd_probe = efx_ef10_mtd_probe,
  4137. .mtd_rename = efx_mcdi_mtd_rename,
  4138. .mtd_read = efx_mcdi_mtd_read,
  4139. .mtd_erase = efx_mcdi_mtd_erase,
  4140. .mtd_write = efx_mcdi_mtd_write,
  4141. .mtd_sync = efx_mcdi_mtd_sync,
  4142. #endif
  4143. .ptp_write_host_time = efx_ef10_ptp_write_host_time,
  4144. .ptp_set_ts_sync_events = efx_ef10_ptp_set_ts_sync_events,
  4145. .ptp_set_ts_config = efx_ef10_ptp_set_ts_config,
  4146. #ifdef CONFIG_SFC_SRIOV
  4147. .sriov_configure = efx_ef10_sriov_configure,
  4148. .sriov_init = efx_ef10_sriov_init,
  4149. .sriov_fini = efx_ef10_sriov_fini,
  4150. .sriov_wanted = efx_ef10_sriov_wanted,
  4151. .sriov_reset = efx_ef10_sriov_reset,
  4152. .sriov_flr = efx_ef10_sriov_flr,
  4153. .sriov_set_vf_mac = efx_ef10_sriov_set_vf_mac,
  4154. .sriov_set_vf_vlan = efx_ef10_sriov_set_vf_vlan,
  4155. .sriov_set_vf_spoofchk = efx_ef10_sriov_set_vf_spoofchk,
  4156. .sriov_get_vf_config = efx_ef10_sriov_get_vf_config,
  4157. .sriov_set_vf_link_state = efx_ef10_sriov_set_vf_link_state,
  4158. .vswitching_probe = efx_ef10_vswitching_probe_pf,
  4159. .vswitching_restore = efx_ef10_vswitching_restore_pf,
  4160. .vswitching_remove = efx_ef10_vswitching_remove_pf,
  4161. #endif
  4162. .get_mac_address = efx_ef10_get_mac_address_pf,
  4163. .set_mac_address = efx_ef10_set_mac_address,
  4164. .revision = EFX_REV_HUNT_A0,
  4165. .max_dma_mask = DMA_BIT_MASK(ESF_DZ_TX_KER_BUF_ADDR_WIDTH),
  4166. .rx_prefix_size = ES_DZ_RX_PREFIX_SIZE,
  4167. .rx_hash_offset = ES_DZ_RX_PREFIX_HASH_OFST,
  4168. .rx_ts_offset = ES_DZ_RX_PREFIX_TSTAMP_OFST,
  4169. .can_rx_scatter = true,
  4170. .always_rx_scatter = true,
  4171. .max_interrupt_mode = EFX_INT_MODE_MSIX,
  4172. .timer_period_max = 1 << ERF_DD_EVQ_IND_TIMER_VAL_WIDTH,
  4173. .offload_features = (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
  4174. NETIF_F_RXHASH | NETIF_F_NTUPLE),
  4175. .mcdi_max_ver = 2,
  4176. .max_rx_ip_filters = HUNT_FILTER_TBL_ROWS,
  4177. .hwtstamp_filters = 1 << HWTSTAMP_FILTER_NONE |
  4178. 1 << HWTSTAMP_FILTER_ALL,
  4179. };