ice_main.c 138 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright (c) 2018, Intel Corporation. */
  3. /* Intel(R) Ethernet Connection E800 Series Linux Driver */
  4. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  5. #include "ice.h"
  6. #define DRV_VERSION "ice-0.7.0-k"
  7. #define DRV_SUMMARY "Intel(R) Ethernet Connection E800 Series Linux Driver"
  8. const char ice_drv_ver[] = DRV_VERSION;
  9. static const char ice_driver_string[] = DRV_SUMMARY;
  10. static const char ice_copyright[] = "Copyright (c) 2018, Intel Corporation.";
  11. MODULE_AUTHOR("Intel Corporation, <linux.nics@intel.com>");
  12. MODULE_DESCRIPTION(DRV_SUMMARY);
  13. MODULE_LICENSE("GPL");
  14. MODULE_VERSION(DRV_VERSION);
  15. static int debug = -1;
  16. module_param(debug, int, 0644);
  17. #ifndef CONFIG_DYNAMIC_DEBUG
  18. MODULE_PARM_DESC(debug, "netif level (0=none,...,16=all), hw debug_mask (0x8XXXXXXX)");
  19. #else
  20. MODULE_PARM_DESC(debug, "netif level (0=none,...,16=all)");
  21. #endif /* !CONFIG_DYNAMIC_DEBUG */
  22. static struct workqueue_struct *ice_wq;
  23. static const struct net_device_ops ice_netdev_ops;
  24. static void ice_pf_dis_all_vsi(struct ice_pf *pf);
  25. static void ice_rebuild(struct ice_pf *pf);
  26. static int ice_vsi_release(struct ice_vsi *vsi);
  27. static void ice_update_vsi_stats(struct ice_vsi *vsi);
  28. static void ice_update_pf_stats(struct ice_pf *pf);
  29. /**
  30. * ice_get_free_slot - get the next non-NULL location index in array
  31. * @array: array to search
  32. * @size: size of the array
  33. * @curr: last known occupied index to be used as a search hint
  34. *
  35. * void * is being used to keep the functionality generic. This lets us use this
  36. * function on any array of pointers.
  37. */
  38. static int ice_get_free_slot(void *array, int size, int curr)
  39. {
  40. int **tmp_array = (int **)array;
  41. int next;
  42. if (curr < (size - 1) && !tmp_array[curr + 1]) {
  43. next = curr + 1;
  44. } else {
  45. int i = 0;
  46. while ((i < size) && (tmp_array[i]))
  47. i++;
  48. if (i == size)
  49. next = ICE_NO_VSI;
  50. else
  51. next = i;
  52. }
  53. return next;
  54. }
  55. /**
  56. * ice_search_res - Search the tracker for a block of resources
  57. * @res: pointer to the resource
  58. * @needed: size of the block needed
  59. * @id: identifier to track owner
  60. * Returns the base item index of the block, or -ENOMEM for error
  61. */
  62. static int ice_search_res(struct ice_res_tracker *res, u16 needed, u16 id)
  63. {
  64. int start = res->search_hint;
  65. int end = start;
  66. id |= ICE_RES_VALID_BIT;
  67. do {
  68. /* skip already allocated entries */
  69. if (res->list[end++] & ICE_RES_VALID_BIT) {
  70. start = end;
  71. if ((start + needed) > res->num_entries)
  72. break;
  73. }
  74. if (end == (start + needed)) {
  75. int i = start;
  76. /* there was enough, so assign it to the requestor */
  77. while (i != end)
  78. res->list[i++] = id;
  79. if (end == res->num_entries)
  80. end = 0;
  81. res->search_hint = end;
  82. return start;
  83. }
  84. } while (1);
  85. return -ENOMEM;
  86. }
  87. /**
  88. * ice_get_res - get a block of resources
  89. * @pf: board private structure
  90. * @res: pointer to the resource
  91. * @needed: size of the block needed
  92. * @id: identifier to track owner
  93. *
  94. * Returns the base item index of the block, or -ENOMEM for error
  95. * The search_hint trick and lack of advanced fit-finding only works
  96. * because we're highly likely to have all the same sized requests.
  97. * Linear search time and any fragmentation should be minimal.
  98. */
  99. static int
  100. ice_get_res(struct ice_pf *pf, struct ice_res_tracker *res, u16 needed, u16 id)
  101. {
  102. int ret;
  103. if (!res || !pf)
  104. return -EINVAL;
  105. if (!needed || needed > res->num_entries || id >= ICE_RES_VALID_BIT) {
  106. dev_err(&pf->pdev->dev,
  107. "param err: needed=%d, num_entries = %d id=0x%04x\n",
  108. needed, res->num_entries, id);
  109. return -EINVAL;
  110. }
  111. /* search based on search_hint */
  112. ret = ice_search_res(res, needed, id);
  113. if (ret < 0) {
  114. /* previous search failed. Reset search hint and try again */
  115. res->search_hint = 0;
  116. ret = ice_search_res(res, needed, id);
  117. }
  118. return ret;
  119. }
  120. /**
  121. * ice_free_res - free a block of resources
  122. * @res: pointer to the resource
  123. * @index: starting index previously returned by ice_get_res
  124. * @id: identifier to track owner
  125. * Returns number of resources freed
  126. */
  127. static int ice_free_res(struct ice_res_tracker *res, u16 index, u16 id)
  128. {
  129. int count = 0;
  130. int i;
  131. if (!res || index >= res->num_entries)
  132. return -EINVAL;
  133. id |= ICE_RES_VALID_BIT;
  134. for (i = index; i < res->num_entries && res->list[i] == id; i++) {
  135. res->list[i] = 0;
  136. count++;
  137. }
  138. return count;
  139. }
  140. /**
  141. * ice_add_mac_to_list - Add a mac address filter entry to the list
  142. * @vsi: the VSI to be forwarded to
  143. * @add_list: pointer to the list which contains MAC filter entries
  144. * @macaddr: the MAC address to be added.
  145. *
  146. * Adds mac address filter entry to the temp list
  147. *
  148. * Returns 0 on success or ENOMEM on failure.
  149. */
  150. static int ice_add_mac_to_list(struct ice_vsi *vsi, struct list_head *add_list,
  151. const u8 *macaddr)
  152. {
  153. struct ice_fltr_list_entry *tmp;
  154. struct ice_pf *pf = vsi->back;
  155. tmp = devm_kzalloc(&pf->pdev->dev, sizeof(*tmp), GFP_ATOMIC);
  156. if (!tmp)
  157. return -ENOMEM;
  158. tmp->fltr_info.flag = ICE_FLTR_TX;
  159. tmp->fltr_info.src = vsi->vsi_num;
  160. tmp->fltr_info.lkup_type = ICE_SW_LKUP_MAC;
  161. tmp->fltr_info.fltr_act = ICE_FWD_TO_VSI;
  162. tmp->fltr_info.fwd_id.vsi_id = vsi->vsi_num;
  163. ether_addr_copy(tmp->fltr_info.l_data.mac.mac_addr, macaddr);
  164. INIT_LIST_HEAD(&tmp->list_entry);
  165. list_add(&tmp->list_entry, add_list);
  166. return 0;
  167. }
  168. /**
  169. * ice_add_mac_to_sync_list - creates list of mac addresses to be synced
  170. * @netdev: the net device on which the sync is happening
  171. * @addr: mac address to sync
  172. *
  173. * This is a callback function which is called by the in kernel device sync
  174. * functions (like __dev_uc_sync, __dev_mc_sync, etc). This function only
  175. * populates the tmp_sync_list, which is later used by ice_add_mac to add the
  176. * mac filters from the hardware.
  177. */
  178. static int ice_add_mac_to_sync_list(struct net_device *netdev, const u8 *addr)
  179. {
  180. struct ice_netdev_priv *np = netdev_priv(netdev);
  181. struct ice_vsi *vsi = np->vsi;
  182. if (ice_add_mac_to_list(vsi, &vsi->tmp_sync_list, addr))
  183. return -EINVAL;
  184. return 0;
  185. }
  186. /**
  187. * ice_add_mac_to_unsync_list - creates list of mac addresses to be unsynced
  188. * @netdev: the net device on which the unsync is happening
  189. * @addr: mac address to unsync
  190. *
  191. * This is a callback function which is called by the in kernel device unsync
  192. * functions (like __dev_uc_unsync, __dev_mc_unsync, etc). This function only
  193. * populates the tmp_unsync_list, which is later used by ice_remove_mac to
  194. * delete the mac filters from the hardware.
  195. */
  196. static int ice_add_mac_to_unsync_list(struct net_device *netdev, const u8 *addr)
  197. {
  198. struct ice_netdev_priv *np = netdev_priv(netdev);
  199. struct ice_vsi *vsi = np->vsi;
  200. if (ice_add_mac_to_list(vsi, &vsi->tmp_unsync_list, addr))
  201. return -EINVAL;
  202. return 0;
  203. }
  204. /**
  205. * ice_free_fltr_list - free filter lists helper
  206. * @dev: pointer to the device struct
  207. * @h: pointer to the list head to be freed
  208. *
  209. * Helper function to free filter lists previously created using
  210. * ice_add_mac_to_list
  211. */
  212. static void ice_free_fltr_list(struct device *dev, struct list_head *h)
  213. {
  214. struct ice_fltr_list_entry *e, *tmp;
  215. list_for_each_entry_safe(e, tmp, h, list_entry) {
  216. list_del(&e->list_entry);
  217. devm_kfree(dev, e);
  218. }
  219. }
  220. /**
  221. * ice_vsi_fltr_changed - check if filter state changed
  222. * @vsi: VSI to be checked
  223. *
  224. * returns true if filter state has changed, false otherwise.
  225. */
  226. static bool ice_vsi_fltr_changed(struct ice_vsi *vsi)
  227. {
  228. return test_bit(ICE_VSI_FLAG_UMAC_FLTR_CHANGED, vsi->flags) ||
  229. test_bit(ICE_VSI_FLAG_MMAC_FLTR_CHANGED, vsi->flags) ||
  230. test_bit(ICE_VSI_FLAG_VLAN_FLTR_CHANGED, vsi->flags);
  231. }
  232. /**
  233. * ice_vsi_sync_fltr - Update the VSI filter list to the HW
  234. * @vsi: ptr to the VSI
  235. *
  236. * Push any outstanding VSI filter changes through the AdminQ.
  237. */
  238. static int ice_vsi_sync_fltr(struct ice_vsi *vsi)
  239. {
  240. struct device *dev = &vsi->back->pdev->dev;
  241. struct net_device *netdev = vsi->netdev;
  242. bool promisc_forced_on = false;
  243. struct ice_pf *pf = vsi->back;
  244. struct ice_hw *hw = &pf->hw;
  245. enum ice_status status = 0;
  246. u32 changed_flags = 0;
  247. int err = 0;
  248. if (!vsi->netdev)
  249. return -EINVAL;
  250. while (test_and_set_bit(__ICE_CFG_BUSY, vsi->state))
  251. usleep_range(1000, 2000);
  252. changed_flags = vsi->current_netdev_flags ^ vsi->netdev->flags;
  253. vsi->current_netdev_flags = vsi->netdev->flags;
  254. INIT_LIST_HEAD(&vsi->tmp_sync_list);
  255. INIT_LIST_HEAD(&vsi->tmp_unsync_list);
  256. if (ice_vsi_fltr_changed(vsi)) {
  257. clear_bit(ICE_VSI_FLAG_UMAC_FLTR_CHANGED, vsi->flags);
  258. clear_bit(ICE_VSI_FLAG_MMAC_FLTR_CHANGED, vsi->flags);
  259. clear_bit(ICE_VSI_FLAG_VLAN_FLTR_CHANGED, vsi->flags);
  260. /* grab the netdev's addr_list_lock */
  261. netif_addr_lock_bh(netdev);
  262. __dev_uc_sync(netdev, ice_add_mac_to_sync_list,
  263. ice_add_mac_to_unsync_list);
  264. __dev_mc_sync(netdev, ice_add_mac_to_sync_list,
  265. ice_add_mac_to_unsync_list);
  266. /* our temp lists are populated. release lock */
  267. netif_addr_unlock_bh(netdev);
  268. }
  269. /* Remove mac addresses in the unsync list */
  270. status = ice_remove_mac(hw, &vsi->tmp_unsync_list);
  271. ice_free_fltr_list(dev, &vsi->tmp_unsync_list);
  272. if (status) {
  273. netdev_err(netdev, "Failed to delete MAC filters\n");
  274. /* if we failed because of alloc failures, just bail */
  275. if (status == ICE_ERR_NO_MEMORY) {
  276. err = -ENOMEM;
  277. goto out;
  278. }
  279. }
  280. /* Add mac addresses in the sync list */
  281. status = ice_add_mac(hw, &vsi->tmp_sync_list);
  282. ice_free_fltr_list(dev, &vsi->tmp_sync_list);
  283. if (status) {
  284. netdev_err(netdev, "Failed to add MAC filters\n");
  285. /* If there is no more space for new umac filters, vsi
  286. * should go into promiscuous mode. There should be some
  287. * space reserved for promiscuous filters.
  288. */
  289. if (hw->adminq.sq_last_status == ICE_AQ_RC_ENOSPC &&
  290. !test_and_set_bit(__ICE_FLTR_OVERFLOW_PROMISC,
  291. vsi->state)) {
  292. promisc_forced_on = true;
  293. netdev_warn(netdev,
  294. "Reached MAC filter limit, forcing promisc mode on VSI %d\n",
  295. vsi->vsi_num);
  296. } else {
  297. err = -EIO;
  298. goto out;
  299. }
  300. }
  301. /* check for changes in promiscuous modes */
  302. if (changed_flags & IFF_ALLMULTI)
  303. netdev_warn(netdev, "Unsupported configuration\n");
  304. if (((changed_flags & IFF_PROMISC) || promisc_forced_on) ||
  305. test_bit(ICE_VSI_FLAG_PROMISC_CHANGED, vsi->flags)) {
  306. clear_bit(ICE_VSI_FLAG_PROMISC_CHANGED, vsi->flags);
  307. if (vsi->current_netdev_flags & IFF_PROMISC) {
  308. /* Apply TX filter rule to get traffic from VMs */
  309. status = ice_cfg_dflt_vsi(hw, vsi->vsi_num, true,
  310. ICE_FLTR_TX);
  311. if (status) {
  312. netdev_err(netdev, "Error setting default VSI %i tx rule\n",
  313. vsi->vsi_num);
  314. vsi->current_netdev_flags &= ~IFF_PROMISC;
  315. err = -EIO;
  316. goto out_promisc;
  317. }
  318. /* Apply RX filter rule to get traffic from wire */
  319. status = ice_cfg_dflt_vsi(hw, vsi->vsi_num, true,
  320. ICE_FLTR_RX);
  321. if (status) {
  322. netdev_err(netdev, "Error setting default VSI %i rx rule\n",
  323. vsi->vsi_num);
  324. vsi->current_netdev_flags &= ~IFF_PROMISC;
  325. err = -EIO;
  326. goto out_promisc;
  327. }
  328. } else {
  329. /* Clear TX filter rule to stop traffic from VMs */
  330. status = ice_cfg_dflt_vsi(hw, vsi->vsi_num, false,
  331. ICE_FLTR_TX);
  332. if (status) {
  333. netdev_err(netdev, "Error clearing default VSI %i tx rule\n",
  334. vsi->vsi_num);
  335. vsi->current_netdev_flags |= IFF_PROMISC;
  336. err = -EIO;
  337. goto out_promisc;
  338. }
  339. /* Clear filter RX to remove traffic from wire */
  340. status = ice_cfg_dflt_vsi(hw, vsi->vsi_num, false,
  341. ICE_FLTR_RX);
  342. if (status) {
  343. netdev_err(netdev, "Error clearing default VSI %i rx rule\n",
  344. vsi->vsi_num);
  345. vsi->current_netdev_flags |= IFF_PROMISC;
  346. err = -EIO;
  347. goto out_promisc;
  348. }
  349. }
  350. }
  351. goto exit;
  352. out_promisc:
  353. set_bit(ICE_VSI_FLAG_PROMISC_CHANGED, vsi->flags);
  354. goto exit;
  355. out:
  356. /* if something went wrong then set the changed flag so we try again */
  357. set_bit(ICE_VSI_FLAG_UMAC_FLTR_CHANGED, vsi->flags);
  358. set_bit(ICE_VSI_FLAG_MMAC_FLTR_CHANGED, vsi->flags);
  359. exit:
  360. clear_bit(__ICE_CFG_BUSY, vsi->state);
  361. return err;
  362. }
  363. /**
  364. * ice_sync_fltr_subtask - Sync the VSI filter list with HW
  365. * @pf: board private structure
  366. */
  367. static void ice_sync_fltr_subtask(struct ice_pf *pf)
  368. {
  369. int v;
  370. if (!pf || !(test_bit(ICE_FLAG_FLTR_SYNC, pf->flags)))
  371. return;
  372. clear_bit(ICE_FLAG_FLTR_SYNC, pf->flags);
  373. for (v = 0; v < pf->num_alloc_vsi; v++)
  374. if (pf->vsi[v] && ice_vsi_fltr_changed(pf->vsi[v]) &&
  375. ice_vsi_sync_fltr(pf->vsi[v])) {
  376. /* come back and try again later */
  377. set_bit(ICE_FLAG_FLTR_SYNC, pf->flags);
  378. break;
  379. }
  380. }
  381. /**
  382. * ice_is_reset_recovery_pending - schedule a reset
  383. * @state: pf state field
  384. */
  385. static bool ice_is_reset_recovery_pending(unsigned long int *state)
  386. {
  387. return test_bit(__ICE_RESET_RECOVERY_PENDING, state);
  388. }
  389. /**
  390. * ice_prepare_for_reset - prep for the core to reset
  391. * @pf: board private structure
  392. *
  393. * Inform or close all dependent features in prep for reset.
  394. */
  395. static void
  396. ice_prepare_for_reset(struct ice_pf *pf)
  397. {
  398. struct ice_hw *hw = &pf->hw;
  399. u32 v;
  400. ice_for_each_vsi(pf, v)
  401. if (pf->vsi[v])
  402. ice_remove_vsi_fltr(hw, pf->vsi[v]->vsi_num);
  403. dev_dbg(&pf->pdev->dev, "Tearing down internal switch for reset\n");
  404. /* disable the VSIs and their queues that are not already DOWN */
  405. /* pf_dis_all_vsi modifies netdev structures -rtnl_lock needed */
  406. ice_pf_dis_all_vsi(pf);
  407. ice_for_each_vsi(pf, v)
  408. if (pf->vsi[v])
  409. pf->vsi[v]->vsi_num = 0;
  410. ice_shutdown_all_ctrlq(hw);
  411. }
  412. /**
  413. * ice_do_reset - Initiate one of many types of resets
  414. * @pf: board private structure
  415. * @reset_type: reset type requested
  416. * before this function was called.
  417. */
  418. static void ice_do_reset(struct ice_pf *pf, enum ice_reset_req reset_type)
  419. {
  420. struct device *dev = &pf->pdev->dev;
  421. struct ice_hw *hw = &pf->hw;
  422. dev_dbg(dev, "reset_type 0x%x requested\n", reset_type);
  423. WARN_ON(in_interrupt());
  424. /* PFR is a bit of a special case because it doesn't result in an OICR
  425. * interrupt. So for PFR, we prepare for reset, issue the reset and
  426. * rebuild sequentially.
  427. */
  428. if (reset_type == ICE_RESET_PFR) {
  429. set_bit(__ICE_RESET_RECOVERY_PENDING, pf->state);
  430. ice_prepare_for_reset(pf);
  431. }
  432. /* trigger the reset */
  433. if (ice_reset(hw, reset_type)) {
  434. dev_err(dev, "reset %d failed\n", reset_type);
  435. set_bit(__ICE_RESET_FAILED, pf->state);
  436. clear_bit(__ICE_RESET_RECOVERY_PENDING, pf->state);
  437. return;
  438. }
  439. if (reset_type == ICE_RESET_PFR) {
  440. pf->pfr_count++;
  441. ice_rebuild(pf);
  442. clear_bit(__ICE_RESET_RECOVERY_PENDING, pf->state);
  443. }
  444. }
  445. /**
  446. * ice_reset_subtask - Set up for resetting the device and driver
  447. * @pf: board private structure
  448. */
  449. static void ice_reset_subtask(struct ice_pf *pf)
  450. {
  451. enum ice_reset_req reset_type;
  452. rtnl_lock();
  453. /* When a CORER/GLOBR/EMPR is about to happen, the hardware triggers an
  454. * OICR interrupt. The OICR handler (ice_misc_intr) determines what
  455. * type of reset happened and sets __ICE_RESET_RECOVERY_PENDING bit in
  456. * pf->state. So if reset/recovery is pending (as indicated by this bit)
  457. * we do a rebuild and return.
  458. */
  459. if (ice_is_reset_recovery_pending(pf->state)) {
  460. clear_bit(__ICE_GLOBR_RECV, pf->state);
  461. clear_bit(__ICE_CORER_RECV, pf->state);
  462. ice_prepare_for_reset(pf);
  463. /* make sure we are ready to rebuild */
  464. if (ice_check_reset(&pf->hw))
  465. set_bit(__ICE_RESET_FAILED, pf->state);
  466. else
  467. ice_rebuild(pf);
  468. clear_bit(__ICE_RESET_RECOVERY_PENDING, pf->state);
  469. goto unlock;
  470. }
  471. /* No pending resets to finish processing. Check for new resets */
  472. if (test_and_clear_bit(__ICE_GLOBR_REQ, pf->state))
  473. reset_type = ICE_RESET_GLOBR;
  474. else if (test_and_clear_bit(__ICE_CORER_REQ, pf->state))
  475. reset_type = ICE_RESET_CORER;
  476. else if (test_and_clear_bit(__ICE_PFR_REQ, pf->state))
  477. reset_type = ICE_RESET_PFR;
  478. else
  479. goto unlock;
  480. /* reset if not already down or resetting */
  481. if (!test_bit(__ICE_DOWN, pf->state) &&
  482. !test_bit(__ICE_CFG_BUSY, pf->state)) {
  483. ice_do_reset(pf, reset_type);
  484. }
  485. unlock:
  486. rtnl_unlock();
  487. }
  488. /**
  489. * ice_watchdog_subtask - periodic tasks not using event driven scheduling
  490. * @pf: board private structure
  491. */
  492. static void ice_watchdog_subtask(struct ice_pf *pf)
  493. {
  494. int i;
  495. /* if interface is down do nothing */
  496. if (test_bit(__ICE_DOWN, pf->state) ||
  497. test_bit(__ICE_CFG_BUSY, pf->state))
  498. return;
  499. /* make sure we don't do these things too often */
  500. if (time_before(jiffies,
  501. pf->serv_tmr_prev + pf->serv_tmr_period))
  502. return;
  503. pf->serv_tmr_prev = jiffies;
  504. /* Update the stats for active netdevs so the network stack
  505. * can look at updated numbers whenever it cares to
  506. */
  507. ice_update_pf_stats(pf);
  508. for (i = 0; i < pf->num_alloc_vsi; i++)
  509. if (pf->vsi[i] && pf->vsi[i]->netdev)
  510. ice_update_vsi_stats(pf->vsi[i]);
  511. }
  512. /**
  513. * ice_print_link_msg - print link up or down message
  514. * @vsi: the VSI whose link status is being queried
  515. * @isup: boolean for if the link is now up or down
  516. */
  517. void ice_print_link_msg(struct ice_vsi *vsi, bool isup)
  518. {
  519. const char *speed;
  520. const char *fc;
  521. if (vsi->current_isup == isup)
  522. return;
  523. vsi->current_isup = isup;
  524. if (!isup) {
  525. netdev_info(vsi->netdev, "NIC Link is Down\n");
  526. return;
  527. }
  528. switch (vsi->port_info->phy.link_info.link_speed) {
  529. case ICE_AQ_LINK_SPEED_40GB:
  530. speed = "40 G";
  531. break;
  532. case ICE_AQ_LINK_SPEED_25GB:
  533. speed = "25 G";
  534. break;
  535. case ICE_AQ_LINK_SPEED_20GB:
  536. speed = "20 G";
  537. break;
  538. case ICE_AQ_LINK_SPEED_10GB:
  539. speed = "10 G";
  540. break;
  541. case ICE_AQ_LINK_SPEED_5GB:
  542. speed = "5 G";
  543. break;
  544. case ICE_AQ_LINK_SPEED_2500MB:
  545. speed = "2.5 G";
  546. break;
  547. case ICE_AQ_LINK_SPEED_1000MB:
  548. speed = "1 G";
  549. break;
  550. case ICE_AQ_LINK_SPEED_100MB:
  551. speed = "100 M";
  552. break;
  553. default:
  554. speed = "Unknown";
  555. break;
  556. }
  557. switch (vsi->port_info->fc.current_mode) {
  558. case ICE_FC_FULL:
  559. fc = "RX/TX";
  560. break;
  561. case ICE_FC_TX_PAUSE:
  562. fc = "TX";
  563. break;
  564. case ICE_FC_RX_PAUSE:
  565. fc = "RX";
  566. break;
  567. case ICE_FC_NONE:
  568. fc = "None";
  569. break;
  570. default:
  571. fc = "Unknown";
  572. break;
  573. }
  574. netdev_info(vsi->netdev, "NIC Link is up %sbps, Flow Control: %s\n",
  575. speed, fc);
  576. }
  577. /**
  578. * ice_init_link_events - enable/initialize link events
  579. * @pi: pointer to the port_info instance
  580. *
  581. * Returns -EIO on failure, 0 on success
  582. */
  583. static int ice_init_link_events(struct ice_port_info *pi)
  584. {
  585. u16 mask;
  586. mask = ~((u16)(ICE_AQ_LINK_EVENT_UPDOWN | ICE_AQ_LINK_EVENT_MEDIA_NA |
  587. ICE_AQ_LINK_EVENT_MODULE_QUAL_FAIL));
  588. if (ice_aq_set_event_mask(pi->hw, pi->lport, mask, NULL)) {
  589. dev_dbg(ice_hw_to_dev(pi->hw),
  590. "Failed to set link event mask for port %d\n",
  591. pi->lport);
  592. return -EIO;
  593. }
  594. if (ice_aq_get_link_info(pi, true, NULL, NULL)) {
  595. dev_dbg(ice_hw_to_dev(pi->hw),
  596. "Failed to enable link events for port %d\n",
  597. pi->lport);
  598. return -EIO;
  599. }
  600. return 0;
  601. }
  602. /**
  603. * ice_vsi_link_event - update the vsi's netdev
  604. * @vsi: the vsi on which the link event occurred
  605. * @link_up: whether or not the vsi needs to be set up or down
  606. */
  607. static void ice_vsi_link_event(struct ice_vsi *vsi, bool link_up)
  608. {
  609. if (!vsi || test_bit(__ICE_DOWN, vsi->state))
  610. return;
  611. if (vsi->type == ICE_VSI_PF) {
  612. if (!vsi->netdev) {
  613. dev_dbg(&vsi->back->pdev->dev,
  614. "vsi->netdev is not initialized!\n");
  615. return;
  616. }
  617. if (link_up) {
  618. netif_carrier_on(vsi->netdev);
  619. netif_tx_wake_all_queues(vsi->netdev);
  620. } else {
  621. netif_carrier_off(vsi->netdev);
  622. netif_tx_stop_all_queues(vsi->netdev);
  623. }
  624. }
  625. }
  626. /**
  627. * ice_link_event - process the link event
  628. * @pf: pf that the link event is associated with
  629. * @pi: port_info for the port that the link event is associated with
  630. *
  631. * Returns -EIO if ice_get_link_status() fails
  632. * Returns 0 on success
  633. */
  634. static int
  635. ice_link_event(struct ice_pf *pf, struct ice_port_info *pi)
  636. {
  637. u8 new_link_speed, old_link_speed;
  638. struct ice_phy_info *phy_info;
  639. bool new_link_same_as_old;
  640. bool new_link, old_link;
  641. u8 lport;
  642. u16 v;
  643. phy_info = &pi->phy;
  644. phy_info->link_info_old = phy_info->link_info;
  645. /* Force ice_get_link_status() to update link info */
  646. phy_info->get_link_info = true;
  647. old_link = (phy_info->link_info_old.link_info & ICE_AQ_LINK_UP);
  648. old_link_speed = phy_info->link_info_old.link_speed;
  649. lport = pi->lport;
  650. if (ice_get_link_status(pi, &new_link)) {
  651. dev_dbg(&pf->pdev->dev,
  652. "Could not get link status for port %d\n", lport);
  653. return -EIO;
  654. }
  655. new_link_speed = phy_info->link_info.link_speed;
  656. new_link_same_as_old = (new_link == old_link &&
  657. new_link_speed == old_link_speed);
  658. ice_for_each_vsi(pf, v) {
  659. struct ice_vsi *vsi = pf->vsi[v];
  660. if (!vsi || !vsi->port_info)
  661. continue;
  662. if (new_link_same_as_old &&
  663. (test_bit(__ICE_DOWN, vsi->state) ||
  664. new_link == netif_carrier_ok(vsi->netdev)))
  665. continue;
  666. if (vsi->port_info->lport == lport) {
  667. ice_print_link_msg(vsi, new_link);
  668. ice_vsi_link_event(vsi, new_link);
  669. }
  670. }
  671. return 0;
  672. }
  673. /**
  674. * ice_handle_link_event - handle link event via ARQ
  675. * @pf: pf that the link event is associated with
  676. *
  677. * Return -EINVAL if port_info is null
  678. * Return status on succes
  679. */
  680. static int ice_handle_link_event(struct ice_pf *pf)
  681. {
  682. struct ice_port_info *port_info;
  683. int status;
  684. port_info = pf->hw.port_info;
  685. if (!port_info)
  686. return -EINVAL;
  687. status = ice_link_event(pf, port_info);
  688. if (status)
  689. dev_dbg(&pf->pdev->dev,
  690. "Could not process link event, error %d\n", status);
  691. return status;
  692. }
  693. /**
  694. * __ice_clean_ctrlq - helper function to clean controlq rings
  695. * @pf: ptr to struct ice_pf
  696. * @q_type: specific Control queue type
  697. */
  698. static int __ice_clean_ctrlq(struct ice_pf *pf, enum ice_ctl_q q_type)
  699. {
  700. struct ice_rq_event_info event;
  701. struct ice_hw *hw = &pf->hw;
  702. struct ice_ctl_q_info *cq;
  703. u16 pending, i = 0;
  704. const char *qtype;
  705. u32 oldval, val;
  706. /* Do not clean control queue if/when PF reset fails */
  707. if (test_bit(__ICE_RESET_FAILED, pf->state))
  708. return 0;
  709. switch (q_type) {
  710. case ICE_CTL_Q_ADMIN:
  711. cq = &hw->adminq;
  712. qtype = "Admin";
  713. break;
  714. default:
  715. dev_warn(&pf->pdev->dev, "Unknown control queue type 0x%x\n",
  716. q_type);
  717. return 0;
  718. }
  719. /* check for error indications - PF_xx_AxQLEN register layout for
  720. * FW/MBX/SB are identical so just use defines for PF_FW_AxQLEN.
  721. */
  722. val = rd32(hw, cq->rq.len);
  723. if (val & (PF_FW_ARQLEN_ARQVFE_M | PF_FW_ARQLEN_ARQOVFL_M |
  724. PF_FW_ARQLEN_ARQCRIT_M)) {
  725. oldval = val;
  726. if (val & PF_FW_ARQLEN_ARQVFE_M)
  727. dev_dbg(&pf->pdev->dev,
  728. "%s Receive Queue VF Error detected\n", qtype);
  729. if (val & PF_FW_ARQLEN_ARQOVFL_M) {
  730. dev_dbg(&pf->pdev->dev,
  731. "%s Receive Queue Overflow Error detected\n",
  732. qtype);
  733. }
  734. if (val & PF_FW_ARQLEN_ARQCRIT_M)
  735. dev_dbg(&pf->pdev->dev,
  736. "%s Receive Queue Critical Error detected\n",
  737. qtype);
  738. val &= ~(PF_FW_ARQLEN_ARQVFE_M | PF_FW_ARQLEN_ARQOVFL_M |
  739. PF_FW_ARQLEN_ARQCRIT_M);
  740. if (oldval != val)
  741. wr32(hw, cq->rq.len, val);
  742. }
  743. val = rd32(hw, cq->sq.len);
  744. if (val & (PF_FW_ATQLEN_ATQVFE_M | PF_FW_ATQLEN_ATQOVFL_M |
  745. PF_FW_ATQLEN_ATQCRIT_M)) {
  746. oldval = val;
  747. if (val & PF_FW_ATQLEN_ATQVFE_M)
  748. dev_dbg(&pf->pdev->dev,
  749. "%s Send Queue VF Error detected\n", qtype);
  750. if (val & PF_FW_ATQLEN_ATQOVFL_M) {
  751. dev_dbg(&pf->pdev->dev,
  752. "%s Send Queue Overflow Error detected\n",
  753. qtype);
  754. }
  755. if (val & PF_FW_ATQLEN_ATQCRIT_M)
  756. dev_dbg(&pf->pdev->dev,
  757. "%s Send Queue Critical Error detected\n",
  758. qtype);
  759. val &= ~(PF_FW_ATQLEN_ATQVFE_M | PF_FW_ATQLEN_ATQOVFL_M |
  760. PF_FW_ATQLEN_ATQCRIT_M);
  761. if (oldval != val)
  762. wr32(hw, cq->sq.len, val);
  763. }
  764. event.buf_len = cq->rq_buf_size;
  765. event.msg_buf = devm_kzalloc(&pf->pdev->dev, event.buf_len,
  766. GFP_KERNEL);
  767. if (!event.msg_buf)
  768. return 0;
  769. do {
  770. enum ice_status ret;
  771. u16 opcode;
  772. ret = ice_clean_rq_elem(hw, cq, &event, &pending);
  773. if (ret == ICE_ERR_AQ_NO_WORK)
  774. break;
  775. if (ret) {
  776. dev_err(&pf->pdev->dev,
  777. "%s Receive Queue event error %d\n", qtype,
  778. ret);
  779. break;
  780. }
  781. opcode = le16_to_cpu(event.desc.opcode);
  782. switch (opcode) {
  783. case ice_aqc_opc_get_link_status:
  784. if (ice_handle_link_event(pf))
  785. dev_err(&pf->pdev->dev,
  786. "Could not handle link event\n");
  787. break;
  788. default:
  789. dev_dbg(&pf->pdev->dev,
  790. "%s Receive Queue unknown event 0x%04x ignored\n",
  791. qtype, opcode);
  792. break;
  793. }
  794. } while (pending && (i++ < ICE_DFLT_IRQ_WORK));
  795. devm_kfree(&pf->pdev->dev, event.msg_buf);
  796. return pending && (i == ICE_DFLT_IRQ_WORK);
  797. }
  798. /**
  799. * ice_ctrlq_pending - check if there is a difference between ntc and ntu
  800. * @hw: pointer to hardware info
  801. * @cq: control queue information
  802. *
  803. * returns true if there are pending messages in a queue, false if there aren't
  804. */
  805. static bool ice_ctrlq_pending(struct ice_hw *hw, struct ice_ctl_q_info *cq)
  806. {
  807. u16 ntu;
  808. ntu = (u16)(rd32(hw, cq->rq.head) & cq->rq.head_mask);
  809. return cq->rq.next_to_clean != ntu;
  810. }
  811. /**
  812. * ice_clean_adminq_subtask - clean the AdminQ rings
  813. * @pf: board private structure
  814. */
  815. static void ice_clean_adminq_subtask(struct ice_pf *pf)
  816. {
  817. struct ice_hw *hw = &pf->hw;
  818. if (!test_bit(__ICE_ADMINQ_EVENT_PENDING, pf->state))
  819. return;
  820. if (__ice_clean_ctrlq(pf, ICE_CTL_Q_ADMIN))
  821. return;
  822. clear_bit(__ICE_ADMINQ_EVENT_PENDING, pf->state);
  823. /* There might be a situation where new messages arrive to a control
  824. * queue between processing the last message and clearing the
  825. * EVENT_PENDING bit. So before exiting, check queue head again (using
  826. * ice_ctrlq_pending) and process new messages if any.
  827. */
  828. if (ice_ctrlq_pending(hw, &hw->adminq))
  829. __ice_clean_ctrlq(pf, ICE_CTL_Q_ADMIN);
  830. ice_flush(hw);
  831. }
  832. /**
  833. * ice_service_task_schedule - schedule the service task to wake up
  834. * @pf: board private structure
  835. *
  836. * If not already scheduled, this puts the task into the work queue.
  837. */
  838. static void ice_service_task_schedule(struct ice_pf *pf)
  839. {
  840. if (!test_bit(__ICE_DOWN, pf->state) &&
  841. !test_and_set_bit(__ICE_SERVICE_SCHED, pf->state))
  842. queue_work(ice_wq, &pf->serv_task);
  843. }
  844. /**
  845. * ice_service_task_complete - finish up the service task
  846. * @pf: board private structure
  847. */
  848. static void ice_service_task_complete(struct ice_pf *pf)
  849. {
  850. WARN_ON(!test_bit(__ICE_SERVICE_SCHED, pf->state));
  851. /* force memory (pf->state) to sync before next service task */
  852. smp_mb__before_atomic();
  853. clear_bit(__ICE_SERVICE_SCHED, pf->state);
  854. }
  855. /**
  856. * ice_service_timer - timer callback to schedule service task
  857. * @t: pointer to timer_list
  858. */
  859. static void ice_service_timer(struct timer_list *t)
  860. {
  861. struct ice_pf *pf = from_timer(pf, t, serv_tmr);
  862. mod_timer(&pf->serv_tmr, round_jiffies(pf->serv_tmr_period + jiffies));
  863. ice_service_task_schedule(pf);
  864. }
  865. /**
  866. * ice_service_task - manage and run subtasks
  867. * @work: pointer to work_struct contained by the PF struct
  868. */
  869. static void ice_service_task(struct work_struct *work)
  870. {
  871. struct ice_pf *pf = container_of(work, struct ice_pf, serv_task);
  872. unsigned long start_time = jiffies;
  873. /* subtasks */
  874. /* process reset requests first */
  875. ice_reset_subtask(pf);
  876. /* bail if a reset/recovery cycle is pending */
  877. if (ice_is_reset_recovery_pending(pf->state) ||
  878. test_bit(__ICE_SUSPENDED, pf->state)) {
  879. ice_service_task_complete(pf);
  880. return;
  881. }
  882. ice_sync_fltr_subtask(pf);
  883. ice_watchdog_subtask(pf);
  884. ice_clean_adminq_subtask(pf);
  885. /* Clear __ICE_SERVICE_SCHED flag to allow scheduling next event */
  886. ice_service_task_complete(pf);
  887. /* If the tasks have taken longer than one service timer period
  888. * or there is more work to be done, reset the service timer to
  889. * schedule the service task now.
  890. */
  891. if (time_after(jiffies, (start_time + pf->serv_tmr_period)) ||
  892. test_bit(__ICE_ADMINQ_EVENT_PENDING, pf->state))
  893. mod_timer(&pf->serv_tmr, jiffies);
  894. }
  895. /**
  896. * ice_set_ctrlq_len - helper function to set controlq length
  897. * @hw: pointer to the hw instance
  898. */
  899. static void ice_set_ctrlq_len(struct ice_hw *hw)
  900. {
  901. hw->adminq.num_rq_entries = ICE_AQ_LEN;
  902. hw->adminq.num_sq_entries = ICE_AQ_LEN;
  903. hw->adminq.rq_buf_size = ICE_AQ_MAX_BUF_LEN;
  904. hw->adminq.sq_buf_size = ICE_AQ_MAX_BUF_LEN;
  905. }
  906. /**
  907. * ice_irq_affinity_notify - Callback for affinity changes
  908. * @notify: context as to what irq was changed
  909. * @mask: the new affinity mask
  910. *
  911. * This is a callback function used by the irq_set_affinity_notifier function
  912. * so that we may register to receive changes to the irq affinity masks.
  913. */
  914. static void ice_irq_affinity_notify(struct irq_affinity_notify *notify,
  915. const cpumask_t *mask)
  916. {
  917. struct ice_q_vector *q_vector =
  918. container_of(notify, struct ice_q_vector, affinity_notify);
  919. cpumask_copy(&q_vector->affinity_mask, mask);
  920. }
  921. /**
  922. * ice_irq_affinity_release - Callback for affinity notifier release
  923. * @ref: internal core kernel usage
  924. *
  925. * This is a callback function used by the irq_set_affinity_notifier function
  926. * to inform the current notification subscriber that they will no longer
  927. * receive notifications.
  928. */
  929. static void ice_irq_affinity_release(struct kref __always_unused *ref) {}
  930. /**
  931. * ice_vsi_dis_irq - Mask off queue interrupt generation on the VSI
  932. * @vsi: the VSI being un-configured
  933. */
  934. static void ice_vsi_dis_irq(struct ice_vsi *vsi)
  935. {
  936. struct ice_pf *pf = vsi->back;
  937. struct ice_hw *hw = &pf->hw;
  938. int base = vsi->base_vector;
  939. u32 val;
  940. int i;
  941. /* disable interrupt causation from each queue */
  942. if (vsi->tx_rings) {
  943. ice_for_each_txq(vsi, i) {
  944. if (vsi->tx_rings[i]) {
  945. u16 reg;
  946. reg = vsi->tx_rings[i]->reg_idx;
  947. val = rd32(hw, QINT_TQCTL(reg));
  948. val &= ~QINT_TQCTL_CAUSE_ENA_M;
  949. wr32(hw, QINT_TQCTL(reg), val);
  950. }
  951. }
  952. }
  953. if (vsi->rx_rings) {
  954. ice_for_each_rxq(vsi, i) {
  955. if (vsi->rx_rings[i]) {
  956. u16 reg;
  957. reg = vsi->rx_rings[i]->reg_idx;
  958. val = rd32(hw, QINT_RQCTL(reg));
  959. val &= ~QINT_RQCTL_CAUSE_ENA_M;
  960. wr32(hw, QINT_RQCTL(reg), val);
  961. }
  962. }
  963. }
  964. /* disable each interrupt */
  965. if (test_bit(ICE_FLAG_MSIX_ENA, pf->flags)) {
  966. for (i = vsi->base_vector;
  967. i < (vsi->num_q_vectors + vsi->base_vector); i++)
  968. wr32(hw, GLINT_DYN_CTL(i), 0);
  969. ice_flush(hw);
  970. for (i = 0; i < vsi->num_q_vectors; i++)
  971. synchronize_irq(pf->msix_entries[i + base].vector);
  972. }
  973. }
  974. /**
  975. * ice_vsi_ena_irq - Enable IRQ for the given VSI
  976. * @vsi: the VSI being configured
  977. */
  978. static int ice_vsi_ena_irq(struct ice_vsi *vsi)
  979. {
  980. struct ice_pf *pf = vsi->back;
  981. struct ice_hw *hw = &pf->hw;
  982. if (test_bit(ICE_FLAG_MSIX_ENA, pf->flags)) {
  983. int i;
  984. for (i = 0; i < vsi->num_q_vectors; i++)
  985. ice_irq_dynamic_ena(hw, vsi, vsi->q_vectors[i]);
  986. }
  987. ice_flush(hw);
  988. return 0;
  989. }
  990. /**
  991. * ice_vsi_delete - delete a VSI from the switch
  992. * @vsi: pointer to VSI being removed
  993. */
  994. static void ice_vsi_delete(struct ice_vsi *vsi)
  995. {
  996. struct ice_pf *pf = vsi->back;
  997. struct ice_vsi_ctx ctxt;
  998. enum ice_status status;
  999. ctxt.vsi_num = vsi->vsi_num;
  1000. memcpy(&ctxt.info, &vsi->info, sizeof(struct ice_aqc_vsi_props));
  1001. status = ice_aq_free_vsi(&pf->hw, &ctxt, false, NULL);
  1002. if (status)
  1003. dev_err(&pf->pdev->dev, "Failed to delete VSI %i in FW\n",
  1004. vsi->vsi_num);
  1005. }
  1006. /**
  1007. * ice_vsi_req_irq_msix - get MSI-X vectors from the OS for the VSI
  1008. * @vsi: the VSI being configured
  1009. * @basename: name for the vector
  1010. */
  1011. static int ice_vsi_req_irq_msix(struct ice_vsi *vsi, char *basename)
  1012. {
  1013. int q_vectors = vsi->num_q_vectors;
  1014. struct ice_pf *pf = vsi->back;
  1015. int base = vsi->base_vector;
  1016. int rx_int_idx = 0;
  1017. int tx_int_idx = 0;
  1018. int vector, err;
  1019. int irq_num;
  1020. for (vector = 0; vector < q_vectors; vector++) {
  1021. struct ice_q_vector *q_vector = vsi->q_vectors[vector];
  1022. irq_num = pf->msix_entries[base + vector].vector;
  1023. if (q_vector->tx.ring && q_vector->rx.ring) {
  1024. snprintf(q_vector->name, sizeof(q_vector->name) - 1,
  1025. "%s-%s-%d", basename, "TxRx", rx_int_idx++);
  1026. tx_int_idx++;
  1027. } else if (q_vector->rx.ring) {
  1028. snprintf(q_vector->name, sizeof(q_vector->name) - 1,
  1029. "%s-%s-%d", basename, "rx", rx_int_idx++);
  1030. } else if (q_vector->tx.ring) {
  1031. snprintf(q_vector->name, sizeof(q_vector->name) - 1,
  1032. "%s-%s-%d", basename, "tx", tx_int_idx++);
  1033. } else {
  1034. /* skip this unused q_vector */
  1035. continue;
  1036. }
  1037. err = devm_request_irq(&pf->pdev->dev,
  1038. pf->msix_entries[base + vector].vector,
  1039. vsi->irq_handler, 0, q_vector->name,
  1040. q_vector);
  1041. if (err) {
  1042. netdev_err(vsi->netdev,
  1043. "MSIX request_irq failed, error: %d\n", err);
  1044. goto free_q_irqs;
  1045. }
  1046. /* register for affinity change notifications */
  1047. q_vector->affinity_notify.notify = ice_irq_affinity_notify;
  1048. q_vector->affinity_notify.release = ice_irq_affinity_release;
  1049. irq_set_affinity_notifier(irq_num, &q_vector->affinity_notify);
  1050. /* assign the mask for this irq */
  1051. irq_set_affinity_hint(irq_num, &q_vector->affinity_mask);
  1052. }
  1053. vsi->irqs_ready = true;
  1054. return 0;
  1055. free_q_irqs:
  1056. while (vector) {
  1057. vector--;
  1058. irq_num = pf->msix_entries[base + vector].vector,
  1059. irq_set_affinity_notifier(irq_num, NULL);
  1060. irq_set_affinity_hint(irq_num, NULL);
  1061. devm_free_irq(&pf->pdev->dev, irq_num, &vsi->q_vectors[vector]);
  1062. }
  1063. return err;
  1064. }
  1065. /**
  1066. * ice_vsi_set_rss_params - Setup RSS capabilities per VSI type
  1067. * @vsi: the VSI being configured
  1068. */
  1069. static void ice_vsi_set_rss_params(struct ice_vsi *vsi)
  1070. {
  1071. struct ice_hw_common_caps *cap;
  1072. struct ice_pf *pf = vsi->back;
  1073. if (!test_bit(ICE_FLAG_RSS_ENA, pf->flags)) {
  1074. vsi->rss_size = 1;
  1075. return;
  1076. }
  1077. cap = &pf->hw.func_caps.common_cap;
  1078. switch (vsi->type) {
  1079. case ICE_VSI_PF:
  1080. /* PF VSI will inherit RSS instance of PF */
  1081. vsi->rss_table_size = cap->rss_table_size;
  1082. vsi->rss_size = min_t(int, num_online_cpus(),
  1083. BIT(cap->rss_table_entry_width));
  1084. vsi->rss_lut_type = ICE_AQC_GSET_RSS_LUT_TABLE_TYPE_PF;
  1085. break;
  1086. default:
  1087. dev_warn(&pf->pdev->dev, "Unknown VSI type %d\n", vsi->type);
  1088. break;
  1089. }
  1090. }
  1091. /**
  1092. * ice_vsi_setup_q_map - Setup a VSI queue map
  1093. * @vsi: the VSI being configured
  1094. * @ctxt: VSI context structure
  1095. */
  1096. static void ice_vsi_setup_q_map(struct ice_vsi *vsi, struct ice_vsi_ctx *ctxt)
  1097. {
  1098. u16 offset = 0, qmap = 0, numq_tc;
  1099. u16 pow = 0, max_rss = 0, qcount;
  1100. u16 qcount_tx = vsi->alloc_txq;
  1101. u16 qcount_rx = vsi->alloc_rxq;
  1102. bool ena_tc0 = false;
  1103. int i;
  1104. /* at least TC0 should be enabled by default */
  1105. if (vsi->tc_cfg.numtc) {
  1106. if (!(vsi->tc_cfg.ena_tc & BIT(0)))
  1107. ena_tc0 = true;
  1108. } else {
  1109. ena_tc0 = true;
  1110. }
  1111. if (ena_tc0) {
  1112. vsi->tc_cfg.numtc++;
  1113. vsi->tc_cfg.ena_tc |= 1;
  1114. }
  1115. numq_tc = qcount_rx / vsi->tc_cfg.numtc;
  1116. /* TC mapping is a function of the number of Rx queues assigned to the
  1117. * VSI for each traffic class and the offset of these queues.
  1118. * The first 10 bits are for queue offset for TC0, next 4 bits for no:of
  1119. * queues allocated to TC0. No:of queues is a power-of-2.
  1120. *
  1121. * If TC is not enabled, the queue offset is set to 0, and allocate one
  1122. * queue, this way, traffic for the given TC will be sent to the default
  1123. * queue.
  1124. *
  1125. * Setup number and offset of Rx queues for all TCs for the VSI
  1126. */
  1127. /* qcount will change if RSS is enabled */
  1128. if (test_bit(ICE_FLAG_RSS_ENA, vsi->back->flags)) {
  1129. if (vsi->type == ICE_VSI_PF)
  1130. max_rss = ICE_MAX_LG_RSS_QS;
  1131. else
  1132. max_rss = ICE_MAX_SMALL_RSS_QS;
  1133. qcount = min_t(int, numq_tc, max_rss);
  1134. qcount = min_t(int, qcount, vsi->rss_size);
  1135. } else {
  1136. qcount = numq_tc;
  1137. }
  1138. /* find the (rounded up) power-of-2 of qcount */
  1139. pow = order_base_2(qcount);
  1140. for (i = 0; i < ICE_MAX_TRAFFIC_CLASS; i++) {
  1141. if (!(vsi->tc_cfg.ena_tc & BIT(i))) {
  1142. /* TC is not enabled */
  1143. vsi->tc_cfg.tc_info[i].qoffset = 0;
  1144. vsi->tc_cfg.tc_info[i].qcount = 1;
  1145. ctxt->info.tc_mapping[i] = 0;
  1146. continue;
  1147. }
  1148. /* TC is enabled */
  1149. vsi->tc_cfg.tc_info[i].qoffset = offset;
  1150. vsi->tc_cfg.tc_info[i].qcount = qcount;
  1151. qmap = ((offset << ICE_AQ_VSI_TC_Q_OFFSET_S) &
  1152. ICE_AQ_VSI_TC_Q_OFFSET_M) |
  1153. ((pow << ICE_AQ_VSI_TC_Q_NUM_S) &
  1154. ICE_AQ_VSI_TC_Q_NUM_M);
  1155. offset += qcount;
  1156. ctxt->info.tc_mapping[i] = cpu_to_le16(qmap);
  1157. }
  1158. vsi->num_txq = qcount_tx;
  1159. vsi->num_rxq = offset;
  1160. /* Rx queue mapping */
  1161. ctxt->info.mapping_flags |= cpu_to_le16(ICE_AQ_VSI_Q_MAP_CONTIG);
  1162. /* q_mapping buffer holds the info for the first queue allocated for
  1163. * this VSI in the PF space and also the number of queues associated
  1164. * with this VSI.
  1165. */
  1166. ctxt->info.q_mapping[0] = cpu_to_le16(vsi->rxq_map[0]);
  1167. ctxt->info.q_mapping[1] = cpu_to_le16(vsi->num_rxq);
  1168. }
  1169. /**
  1170. * ice_set_dflt_vsi_ctx - Set default VSI context before adding a VSI
  1171. * @ctxt: the VSI context being set
  1172. *
  1173. * This initializes a default VSI context for all sections except the Queues.
  1174. */
  1175. static void ice_set_dflt_vsi_ctx(struct ice_vsi_ctx *ctxt)
  1176. {
  1177. u32 table = 0;
  1178. memset(&ctxt->info, 0, sizeof(ctxt->info));
  1179. /* VSI's should be allocated from shared pool */
  1180. ctxt->alloc_from_pool = true;
  1181. /* Src pruning enabled by default */
  1182. ctxt->info.sw_flags = ICE_AQ_VSI_SW_FLAG_SRC_PRUNE;
  1183. /* Traffic from VSI can be sent to LAN */
  1184. ctxt->info.sw_flags2 = ICE_AQ_VSI_SW_FLAG_LAN_ENA;
  1185. /* By default bits 3 and 4 in vlan_flags are 0's which results in legacy
  1186. * behavior (show VLAN, DEI, and UP) in descriptor. Also, allow all
  1187. * packets untagged/tagged.
  1188. */
  1189. ctxt->info.vlan_flags = ((ICE_AQ_VSI_VLAN_MODE_ALL &
  1190. ICE_AQ_VSI_VLAN_MODE_M) >>
  1191. ICE_AQ_VSI_VLAN_MODE_S);
  1192. /* Have 1:1 UP mapping for both ingress/egress tables */
  1193. table |= ICE_UP_TABLE_TRANSLATE(0, 0);
  1194. table |= ICE_UP_TABLE_TRANSLATE(1, 1);
  1195. table |= ICE_UP_TABLE_TRANSLATE(2, 2);
  1196. table |= ICE_UP_TABLE_TRANSLATE(3, 3);
  1197. table |= ICE_UP_TABLE_TRANSLATE(4, 4);
  1198. table |= ICE_UP_TABLE_TRANSLATE(5, 5);
  1199. table |= ICE_UP_TABLE_TRANSLATE(6, 6);
  1200. table |= ICE_UP_TABLE_TRANSLATE(7, 7);
  1201. ctxt->info.ingress_table = cpu_to_le32(table);
  1202. ctxt->info.egress_table = cpu_to_le32(table);
  1203. /* Have 1:1 UP mapping for outer to inner UP table */
  1204. ctxt->info.outer_up_table = cpu_to_le32(table);
  1205. /* No Outer tag support outer_tag_flags remains to zero */
  1206. }
  1207. /**
  1208. * ice_set_rss_vsi_ctx - Set RSS VSI context before adding a VSI
  1209. * @ctxt: the VSI context being set
  1210. * @vsi: the VSI being configured
  1211. */
  1212. static void ice_set_rss_vsi_ctx(struct ice_vsi_ctx *ctxt, struct ice_vsi *vsi)
  1213. {
  1214. u8 lut_type, hash_type;
  1215. switch (vsi->type) {
  1216. case ICE_VSI_PF:
  1217. /* PF VSI will inherit RSS instance of PF */
  1218. lut_type = ICE_AQ_VSI_Q_OPT_RSS_LUT_PF;
  1219. hash_type = ICE_AQ_VSI_Q_OPT_RSS_TPLZ;
  1220. break;
  1221. default:
  1222. dev_warn(&vsi->back->pdev->dev, "Unknown VSI type %d\n",
  1223. vsi->type);
  1224. return;
  1225. }
  1226. ctxt->info.q_opt_rss = ((lut_type << ICE_AQ_VSI_Q_OPT_RSS_LUT_S) &
  1227. ICE_AQ_VSI_Q_OPT_RSS_LUT_M) |
  1228. ((hash_type << ICE_AQ_VSI_Q_OPT_RSS_HASH_S) &
  1229. ICE_AQ_VSI_Q_OPT_RSS_HASH_M);
  1230. }
  1231. /**
  1232. * ice_vsi_add - Create a new VSI or fetch preallocated VSI
  1233. * @vsi: the VSI being configured
  1234. *
  1235. * This initializes a VSI context depending on the VSI type to be added and
  1236. * passes it down to the add_vsi aq command to create a new VSI.
  1237. */
  1238. static int ice_vsi_add(struct ice_vsi *vsi)
  1239. {
  1240. struct ice_vsi_ctx ctxt = { 0 };
  1241. struct ice_pf *pf = vsi->back;
  1242. struct ice_hw *hw = &pf->hw;
  1243. int ret = 0;
  1244. switch (vsi->type) {
  1245. case ICE_VSI_PF:
  1246. ctxt.flags = ICE_AQ_VSI_TYPE_PF;
  1247. break;
  1248. default:
  1249. return -ENODEV;
  1250. }
  1251. ice_set_dflt_vsi_ctx(&ctxt);
  1252. /* if the switch is in VEB mode, allow VSI loopback */
  1253. if (vsi->vsw->bridge_mode == BRIDGE_MODE_VEB)
  1254. ctxt.info.sw_flags |= ICE_AQ_VSI_SW_FLAG_ALLOW_LB;
  1255. /* Set LUT type and HASH type if RSS is enabled */
  1256. if (test_bit(ICE_FLAG_RSS_ENA, pf->flags))
  1257. ice_set_rss_vsi_ctx(&ctxt, vsi);
  1258. ctxt.info.sw_id = vsi->port_info->sw_id;
  1259. ice_vsi_setup_q_map(vsi, &ctxt);
  1260. ret = ice_aq_add_vsi(hw, &ctxt, NULL);
  1261. if (ret) {
  1262. dev_err(&vsi->back->pdev->dev,
  1263. "Add VSI AQ call failed, err %d\n", ret);
  1264. return -EIO;
  1265. }
  1266. vsi->info = ctxt.info;
  1267. vsi->vsi_num = ctxt.vsi_num;
  1268. return ret;
  1269. }
  1270. /**
  1271. * ice_vsi_release_msix - Clear the queue to Interrupt mapping in HW
  1272. * @vsi: the VSI being cleaned up
  1273. */
  1274. static void ice_vsi_release_msix(struct ice_vsi *vsi)
  1275. {
  1276. struct ice_pf *pf = vsi->back;
  1277. u16 vector = vsi->base_vector;
  1278. struct ice_hw *hw = &pf->hw;
  1279. u32 txq = 0;
  1280. u32 rxq = 0;
  1281. int i, q;
  1282. for (i = 0; i < vsi->num_q_vectors; i++, vector++) {
  1283. struct ice_q_vector *q_vector = vsi->q_vectors[i];
  1284. wr32(hw, GLINT_ITR(ICE_RX_ITR, vector), 0);
  1285. wr32(hw, GLINT_ITR(ICE_TX_ITR, vector), 0);
  1286. for (q = 0; q < q_vector->num_ring_tx; q++) {
  1287. wr32(hw, QINT_TQCTL(vsi->txq_map[txq]), 0);
  1288. txq++;
  1289. }
  1290. for (q = 0; q < q_vector->num_ring_rx; q++) {
  1291. wr32(hw, QINT_RQCTL(vsi->rxq_map[rxq]), 0);
  1292. rxq++;
  1293. }
  1294. }
  1295. ice_flush(hw);
  1296. }
  1297. /**
  1298. * ice_vsi_clear_rings - Deallocates the Tx and Rx rings for VSI
  1299. * @vsi: the VSI having rings deallocated
  1300. */
  1301. static void ice_vsi_clear_rings(struct ice_vsi *vsi)
  1302. {
  1303. int i;
  1304. if (vsi->tx_rings) {
  1305. for (i = 0; i < vsi->alloc_txq; i++) {
  1306. if (vsi->tx_rings[i]) {
  1307. kfree_rcu(vsi->tx_rings[i], rcu);
  1308. vsi->tx_rings[i] = NULL;
  1309. }
  1310. }
  1311. }
  1312. if (vsi->rx_rings) {
  1313. for (i = 0; i < vsi->alloc_rxq; i++) {
  1314. if (vsi->rx_rings[i]) {
  1315. kfree_rcu(vsi->rx_rings[i], rcu);
  1316. vsi->rx_rings[i] = NULL;
  1317. }
  1318. }
  1319. }
  1320. }
  1321. /**
  1322. * ice_vsi_alloc_rings - Allocates Tx and Rx rings for the VSI
  1323. * @vsi: VSI which is having rings allocated
  1324. */
  1325. static int ice_vsi_alloc_rings(struct ice_vsi *vsi)
  1326. {
  1327. struct ice_pf *pf = vsi->back;
  1328. int i;
  1329. /* Allocate tx_rings */
  1330. for (i = 0; i < vsi->alloc_txq; i++) {
  1331. struct ice_ring *ring;
  1332. /* allocate with kzalloc(), free with kfree_rcu() */
  1333. ring = kzalloc(sizeof(*ring), GFP_KERNEL);
  1334. if (!ring)
  1335. goto err_out;
  1336. ring->q_index = i;
  1337. ring->reg_idx = vsi->txq_map[i];
  1338. ring->ring_active = false;
  1339. ring->vsi = vsi;
  1340. ring->netdev = vsi->netdev;
  1341. ring->dev = &pf->pdev->dev;
  1342. ring->count = vsi->num_desc;
  1343. vsi->tx_rings[i] = ring;
  1344. }
  1345. /* Allocate rx_rings */
  1346. for (i = 0; i < vsi->alloc_rxq; i++) {
  1347. struct ice_ring *ring;
  1348. /* allocate with kzalloc(), free with kfree_rcu() */
  1349. ring = kzalloc(sizeof(*ring), GFP_KERNEL);
  1350. if (!ring)
  1351. goto err_out;
  1352. ring->q_index = i;
  1353. ring->reg_idx = vsi->rxq_map[i];
  1354. ring->ring_active = false;
  1355. ring->vsi = vsi;
  1356. ring->netdev = vsi->netdev;
  1357. ring->dev = &pf->pdev->dev;
  1358. ring->count = vsi->num_desc;
  1359. vsi->rx_rings[i] = ring;
  1360. }
  1361. return 0;
  1362. err_out:
  1363. ice_vsi_clear_rings(vsi);
  1364. return -ENOMEM;
  1365. }
  1366. /**
  1367. * ice_vsi_free_irq - Free the irq association with the OS
  1368. * @vsi: the VSI being configured
  1369. */
  1370. static void ice_vsi_free_irq(struct ice_vsi *vsi)
  1371. {
  1372. struct ice_pf *pf = vsi->back;
  1373. int base = vsi->base_vector;
  1374. if (test_bit(ICE_FLAG_MSIX_ENA, pf->flags)) {
  1375. int i;
  1376. if (!vsi->q_vectors || !vsi->irqs_ready)
  1377. return;
  1378. vsi->irqs_ready = false;
  1379. for (i = 0; i < vsi->num_q_vectors; i++) {
  1380. u16 vector = i + base;
  1381. int irq_num;
  1382. irq_num = pf->msix_entries[vector].vector;
  1383. /* free only the irqs that were actually requested */
  1384. if (!vsi->q_vectors[i] ||
  1385. !(vsi->q_vectors[i]->num_ring_tx ||
  1386. vsi->q_vectors[i]->num_ring_rx))
  1387. continue;
  1388. /* clear the affinity notifier in the IRQ descriptor */
  1389. irq_set_affinity_notifier(irq_num, NULL);
  1390. /* clear the affinity_mask in the IRQ descriptor */
  1391. irq_set_affinity_hint(irq_num, NULL);
  1392. synchronize_irq(irq_num);
  1393. devm_free_irq(&pf->pdev->dev, irq_num,
  1394. vsi->q_vectors[i]);
  1395. }
  1396. ice_vsi_release_msix(vsi);
  1397. }
  1398. }
  1399. /**
  1400. * ice_vsi_cfg_msix - MSIX mode Interrupt Config in the HW
  1401. * @vsi: the VSI being configured
  1402. */
  1403. static void ice_vsi_cfg_msix(struct ice_vsi *vsi)
  1404. {
  1405. struct ice_pf *pf = vsi->back;
  1406. u16 vector = vsi->base_vector;
  1407. struct ice_hw *hw = &pf->hw;
  1408. u32 txq = 0, rxq = 0;
  1409. int i, q, itr;
  1410. u8 itr_gran;
  1411. for (i = 0; i < vsi->num_q_vectors; i++, vector++) {
  1412. struct ice_q_vector *q_vector = vsi->q_vectors[i];
  1413. itr_gran = hw->itr_gran_200;
  1414. if (q_vector->num_ring_rx) {
  1415. q_vector->rx.itr =
  1416. ITR_TO_REG(vsi->rx_rings[rxq]->rx_itr_setting,
  1417. itr_gran);
  1418. q_vector->rx.latency_range = ICE_LOW_LATENCY;
  1419. }
  1420. if (q_vector->num_ring_tx) {
  1421. q_vector->tx.itr =
  1422. ITR_TO_REG(vsi->tx_rings[txq]->tx_itr_setting,
  1423. itr_gran);
  1424. q_vector->tx.latency_range = ICE_LOW_LATENCY;
  1425. }
  1426. wr32(hw, GLINT_ITR(ICE_RX_ITR, vector), q_vector->rx.itr);
  1427. wr32(hw, GLINT_ITR(ICE_TX_ITR, vector), q_vector->tx.itr);
  1428. /* Both Transmit Queue Interrupt Cause Control register
  1429. * and Receive Queue Interrupt Cause control register
  1430. * expects MSIX_INDX field to be the vector index
  1431. * within the function space and not the absolute
  1432. * vector index across PF or across device.
  1433. * For SR-IOV VF VSIs queue vector index always starts
  1434. * with 1 since first vector index(0) is used for OICR
  1435. * in VF space. Since VMDq and other PF VSIs are withtin
  1436. * the PF function space, use the vector index thats
  1437. * tracked for this PF.
  1438. */
  1439. for (q = 0; q < q_vector->num_ring_tx; q++) {
  1440. u32 val;
  1441. itr = ICE_TX_ITR;
  1442. val = QINT_TQCTL_CAUSE_ENA_M |
  1443. (itr << QINT_TQCTL_ITR_INDX_S) |
  1444. (vector << QINT_TQCTL_MSIX_INDX_S);
  1445. wr32(hw, QINT_TQCTL(vsi->txq_map[txq]), val);
  1446. txq++;
  1447. }
  1448. for (q = 0; q < q_vector->num_ring_rx; q++) {
  1449. u32 val;
  1450. itr = ICE_RX_ITR;
  1451. val = QINT_RQCTL_CAUSE_ENA_M |
  1452. (itr << QINT_RQCTL_ITR_INDX_S) |
  1453. (vector << QINT_RQCTL_MSIX_INDX_S);
  1454. wr32(hw, QINT_RQCTL(vsi->rxq_map[rxq]), val);
  1455. rxq++;
  1456. }
  1457. }
  1458. ice_flush(hw);
  1459. }
  1460. /**
  1461. * ice_ena_misc_vector - enable the non-queue interrupts
  1462. * @pf: board private structure
  1463. */
  1464. static void ice_ena_misc_vector(struct ice_pf *pf)
  1465. {
  1466. struct ice_hw *hw = &pf->hw;
  1467. u32 val;
  1468. /* clear things first */
  1469. wr32(hw, PFINT_OICR_ENA, 0); /* disable all */
  1470. rd32(hw, PFINT_OICR); /* read to clear */
  1471. val = (PFINT_OICR_ECC_ERR_M |
  1472. PFINT_OICR_MAL_DETECT_M |
  1473. PFINT_OICR_GRST_M |
  1474. PFINT_OICR_PCI_EXCEPTION_M |
  1475. PFINT_OICR_HMC_ERR_M |
  1476. PFINT_OICR_PE_CRITERR_M);
  1477. wr32(hw, PFINT_OICR_ENA, val);
  1478. /* SW_ITR_IDX = 0, but don't change INTENA */
  1479. wr32(hw, GLINT_DYN_CTL(pf->oicr_idx),
  1480. GLINT_DYN_CTL_SW_ITR_INDX_M | GLINT_DYN_CTL_INTENA_MSK_M);
  1481. }
  1482. /**
  1483. * ice_misc_intr - misc interrupt handler
  1484. * @irq: interrupt number
  1485. * @data: pointer to a q_vector
  1486. */
  1487. static irqreturn_t ice_misc_intr(int __always_unused irq, void *data)
  1488. {
  1489. struct ice_pf *pf = (struct ice_pf *)data;
  1490. struct ice_hw *hw = &pf->hw;
  1491. irqreturn_t ret = IRQ_NONE;
  1492. u32 oicr, ena_mask;
  1493. set_bit(__ICE_ADMINQ_EVENT_PENDING, pf->state);
  1494. oicr = rd32(hw, PFINT_OICR);
  1495. ena_mask = rd32(hw, PFINT_OICR_ENA);
  1496. if (oicr & PFINT_OICR_GRST_M) {
  1497. u32 reset;
  1498. /* we have a reset warning */
  1499. ena_mask &= ~PFINT_OICR_GRST_M;
  1500. reset = (rd32(hw, GLGEN_RSTAT) & GLGEN_RSTAT_RESET_TYPE_M) >>
  1501. GLGEN_RSTAT_RESET_TYPE_S;
  1502. if (reset == ICE_RESET_CORER)
  1503. pf->corer_count++;
  1504. else if (reset == ICE_RESET_GLOBR)
  1505. pf->globr_count++;
  1506. else
  1507. pf->empr_count++;
  1508. /* If a reset cycle isn't already in progress, we set a bit in
  1509. * pf->state so that the service task can start a reset/rebuild.
  1510. * We also make note of which reset happened so that peer
  1511. * devices/drivers can be informed.
  1512. */
  1513. if (!test_bit(__ICE_RESET_RECOVERY_PENDING, pf->state)) {
  1514. if (reset == ICE_RESET_CORER)
  1515. set_bit(__ICE_CORER_RECV, pf->state);
  1516. else if (reset == ICE_RESET_GLOBR)
  1517. set_bit(__ICE_GLOBR_RECV, pf->state);
  1518. else
  1519. set_bit(__ICE_EMPR_RECV, pf->state);
  1520. set_bit(__ICE_RESET_RECOVERY_PENDING, pf->state);
  1521. }
  1522. }
  1523. if (oicr & PFINT_OICR_HMC_ERR_M) {
  1524. ena_mask &= ~PFINT_OICR_HMC_ERR_M;
  1525. dev_dbg(&pf->pdev->dev,
  1526. "HMC Error interrupt - info 0x%x, data 0x%x\n",
  1527. rd32(hw, PFHMC_ERRORINFO),
  1528. rd32(hw, PFHMC_ERRORDATA));
  1529. }
  1530. /* Report and mask off any remaining unexpected interrupts */
  1531. oicr &= ena_mask;
  1532. if (oicr) {
  1533. dev_dbg(&pf->pdev->dev, "unhandled interrupt oicr=0x%08x\n",
  1534. oicr);
  1535. /* If a critical error is pending there is no choice but to
  1536. * reset the device.
  1537. */
  1538. if (oicr & (PFINT_OICR_PE_CRITERR_M |
  1539. PFINT_OICR_PCI_EXCEPTION_M |
  1540. PFINT_OICR_ECC_ERR_M)) {
  1541. set_bit(__ICE_PFR_REQ, pf->state);
  1542. ice_service_task_schedule(pf);
  1543. }
  1544. ena_mask &= ~oicr;
  1545. }
  1546. ret = IRQ_HANDLED;
  1547. /* re-enable interrupt causes that are not handled during this pass */
  1548. wr32(hw, PFINT_OICR_ENA, ena_mask);
  1549. if (!test_bit(__ICE_DOWN, pf->state)) {
  1550. ice_service_task_schedule(pf);
  1551. ice_irq_dynamic_ena(hw, NULL, NULL);
  1552. }
  1553. return ret;
  1554. }
  1555. /**
  1556. * ice_vsi_map_rings_to_vectors - Map VSI rings to interrupt vectors
  1557. * @vsi: the VSI being configured
  1558. *
  1559. * This function maps descriptor rings to the queue-specific vectors allotted
  1560. * through the MSI-X enabling code. On a constrained vector budget, we map Tx
  1561. * and Rx rings to the vector as "efficiently" as possible.
  1562. */
  1563. static void ice_vsi_map_rings_to_vectors(struct ice_vsi *vsi)
  1564. {
  1565. int q_vectors = vsi->num_q_vectors;
  1566. int tx_rings_rem, rx_rings_rem;
  1567. int v_id;
  1568. /* initially assigning remaining rings count to VSIs num queue value */
  1569. tx_rings_rem = vsi->num_txq;
  1570. rx_rings_rem = vsi->num_rxq;
  1571. for (v_id = 0; v_id < q_vectors; v_id++) {
  1572. struct ice_q_vector *q_vector = vsi->q_vectors[v_id];
  1573. int tx_rings_per_v, rx_rings_per_v, q_id, q_base;
  1574. /* Tx rings mapping to vector */
  1575. tx_rings_per_v = DIV_ROUND_UP(tx_rings_rem, q_vectors - v_id);
  1576. q_vector->num_ring_tx = tx_rings_per_v;
  1577. q_vector->tx.ring = NULL;
  1578. q_base = vsi->num_txq - tx_rings_rem;
  1579. for (q_id = q_base; q_id < (q_base + tx_rings_per_v); q_id++) {
  1580. struct ice_ring *tx_ring = vsi->tx_rings[q_id];
  1581. tx_ring->q_vector = q_vector;
  1582. tx_ring->next = q_vector->tx.ring;
  1583. q_vector->tx.ring = tx_ring;
  1584. }
  1585. tx_rings_rem -= tx_rings_per_v;
  1586. /* Rx rings mapping to vector */
  1587. rx_rings_per_v = DIV_ROUND_UP(rx_rings_rem, q_vectors - v_id);
  1588. q_vector->num_ring_rx = rx_rings_per_v;
  1589. q_vector->rx.ring = NULL;
  1590. q_base = vsi->num_rxq - rx_rings_rem;
  1591. for (q_id = q_base; q_id < (q_base + rx_rings_per_v); q_id++) {
  1592. struct ice_ring *rx_ring = vsi->rx_rings[q_id];
  1593. rx_ring->q_vector = q_vector;
  1594. rx_ring->next = q_vector->rx.ring;
  1595. q_vector->rx.ring = rx_ring;
  1596. }
  1597. rx_rings_rem -= rx_rings_per_v;
  1598. }
  1599. }
  1600. /**
  1601. * ice_vsi_set_num_qs - Set num queues, descriptors and vectors for a VSI
  1602. * @vsi: the VSI being configured
  1603. *
  1604. * Return 0 on success and a negative value on error
  1605. */
  1606. static void ice_vsi_set_num_qs(struct ice_vsi *vsi)
  1607. {
  1608. struct ice_pf *pf = vsi->back;
  1609. switch (vsi->type) {
  1610. case ICE_VSI_PF:
  1611. vsi->alloc_txq = pf->num_lan_tx;
  1612. vsi->alloc_rxq = pf->num_lan_rx;
  1613. vsi->num_desc = ALIGN(ICE_DFLT_NUM_DESC, ICE_REQ_DESC_MULTIPLE);
  1614. vsi->num_q_vectors = max_t(int, pf->num_lan_rx, pf->num_lan_tx);
  1615. break;
  1616. default:
  1617. dev_warn(&vsi->back->pdev->dev, "Unknown VSI type %d\n",
  1618. vsi->type);
  1619. break;
  1620. }
  1621. }
  1622. /**
  1623. * ice_vsi_alloc_arrays - Allocate queue and vector pointer arrays for the vsi
  1624. * @vsi: VSI pointer
  1625. * @alloc_qvectors: a bool to specify if q_vectors need to be allocated.
  1626. *
  1627. * On error: returns error code (negative)
  1628. * On success: returns 0
  1629. */
  1630. static int ice_vsi_alloc_arrays(struct ice_vsi *vsi, bool alloc_qvectors)
  1631. {
  1632. struct ice_pf *pf = vsi->back;
  1633. /* allocate memory for both Tx and Rx ring pointers */
  1634. vsi->tx_rings = devm_kcalloc(&pf->pdev->dev, vsi->alloc_txq,
  1635. sizeof(struct ice_ring *), GFP_KERNEL);
  1636. if (!vsi->tx_rings)
  1637. goto err_txrings;
  1638. vsi->rx_rings = devm_kcalloc(&pf->pdev->dev, vsi->alloc_rxq,
  1639. sizeof(struct ice_ring *), GFP_KERNEL);
  1640. if (!vsi->rx_rings)
  1641. goto err_rxrings;
  1642. if (alloc_qvectors) {
  1643. /* allocate memory for q_vector pointers */
  1644. vsi->q_vectors = devm_kcalloc(&pf->pdev->dev,
  1645. vsi->num_q_vectors,
  1646. sizeof(struct ice_q_vector *),
  1647. GFP_KERNEL);
  1648. if (!vsi->q_vectors)
  1649. goto err_vectors;
  1650. }
  1651. return 0;
  1652. err_vectors:
  1653. devm_kfree(&pf->pdev->dev, vsi->rx_rings);
  1654. err_rxrings:
  1655. devm_kfree(&pf->pdev->dev, vsi->tx_rings);
  1656. err_txrings:
  1657. return -ENOMEM;
  1658. }
  1659. /**
  1660. * ice_msix_clean_rings - MSIX mode Interrupt Handler
  1661. * @irq: interrupt number
  1662. * @data: pointer to a q_vector
  1663. */
  1664. static irqreturn_t ice_msix_clean_rings(int __always_unused irq, void *data)
  1665. {
  1666. struct ice_q_vector *q_vector = (struct ice_q_vector *)data;
  1667. if (!q_vector->tx.ring && !q_vector->rx.ring)
  1668. return IRQ_HANDLED;
  1669. napi_schedule(&q_vector->napi);
  1670. return IRQ_HANDLED;
  1671. }
  1672. /**
  1673. * ice_vsi_alloc - Allocates the next available struct vsi in the PF
  1674. * @pf: board private structure
  1675. * @type: type of VSI
  1676. *
  1677. * returns a pointer to a VSI on success, NULL on failure.
  1678. */
  1679. static struct ice_vsi *ice_vsi_alloc(struct ice_pf *pf, enum ice_vsi_type type)
  1680. {
  1681. struct ice_vsi *vsi = NULL;
  1682. /* Need to protect the allocation of the VSIs at the PF level */
  1683. mutex_lock(&pf->sw_mutex);
  1684. /* If we have already allocated our maximum number of VSIs,
  1685. * pf->next_vsi will be ICE_NO_VSI. If not, pf->next_vsi index
  1686. * is available to be populated
  1687. */
  1688. if (pf->next_vsi == ICE_NO_VSI) {
  1689. dev_dbg(&pf->pdev->dev, "out of VSI slots!\n");
  1690. goto unlock_pf;
  1691. }
  1692. vsi = devm_kzalloc(&pf->pdev->dev, sizeof(*vsi), GFP_KERNEL);
  1693. if (!vsi)
  1694. goto unlock_pf;
  1695. vsi->type = type;
  1696. vsi->back = pf;
  1697. set_bit(__ICE_DOWN, vsi->state);
  1698. vsi->idx = pf->next_vsi;
  1699. vsi->work_lmt = ICE_DFLT_IRQ_WORK;
  1700. ice_vsi_set_num_qs(vsi);
  1701. switch (vsi->type) {
  1702. case ICE_VSI_PF:
  1703. if (ice_vsi_alloc_arrays(vsi, true))
  1704. goto err_rings;
  1705. /* Setup default MSIX irq handler for VSI */
  1706. vsi->irq_handler = ice_msix_clean_rings;
  1707. break;
  1708. default:
  1709. dev_warn(&pf->pdev->dev, "Unknown VSI type %d\n", vsi->type);
  1710. goto unlock_pf;
  1711. }
  1712. /* fill VSI slot in the PF struct */
  1713. pf->vsi[pf->next_vsi] = vsi;
  1714. /* prepare pf->next_vsi for next use */
  1715. pf->next_vsi = ice_get_free_slot(pf->vsi, pf->num_alloc_vsi,
  1716. pf->next_vsi);
  1717. goto unlock_pf;
  1718. err_rings:
  1719. devm_kfree(&pf->pdev->dev, vsi);
  1720. vsi = NULL;
  1721. unlock_pf:
  1722. mutex_unlock(&pf->sw_mutex);
  1723. return vsi;
  1724. }
  1725. /**
  1726. * ice_free_irq_msix_misc - Unroll misc vector setup
  1727. * @pf: board private structure
  1728. */
  1729. static void ice_free_irq_msix_misc(struct ice_pf *pf)
  1730. {
  1731. /* disable OICR interrupt */
  1732. wr32(&pf->hw, PFINT_OICR_ENA, 0);
  1733. ice_flush(&pf->hw);
  1734. if (test_bit(ICE_FLAG_MSIX_ENA, pf->flags) && pf->msix_entries) {
  1735. synchronize_irq(pf->msix_entries[pf->oicr_idx].vector);
  1736. devm_free_irq(&pf->pdev->dev,
  1737. pf->msix_entries[pf->oicr_idx].vector, pf);
  1738. }
  1739. ice_free_res(pf->irq_tracker, pf->oicr_idx, ICE_RES_MISC_VEC_ID);
  1740. }
  1741. /**
  1742. * ice_req_irq_msix_misc - Setup the misc vector to handle non queue events
  1743. * @pf: board private structure
  1744. *
  1745. * This sets up the handler for MSIX 0, which is used to manage the
  1746. * non-queue interrupts, e.g. AdminQ and errors. This is not used
  1747. * when in MSI or Legacy interrupt mode.
  1748. */
  1749. static int ice_req_irq_msix_misc(struct ice_pf *pf)
  1750. {
  1751. struct ice_hw *hw = &pf->hw;
  1752. int oicr_idx, err = 0;
  1753. u8 itr_gran;
  1754. u32 val;
  1755. if (!pf->int_name[0])
  1756. snprintf(pf->int_name, sizeof(pf->int_name) - 1, "%s-%s:misc",
  1757. dev_driver_string(&pf->pdev->dev),
  1758. dev_name(&pf->pdev->dev));
  1759. /* Do not request IRQ but do enable OICR interrupt since settings are
  1760. * lost during reset. Note that this function is called only during
  1761. * rebuild path and not while reset is in progress.
  1762. */
  1763. if (ice_is_reset_recovery_pending(pf->state))
  1764. goto skip_req_irq;
  1765. /* reserve one vector in irq_tracker for misc interrupts */
  1766. oicr_idx = ice_get_res(pf, pf->irq_tracker, 1, ICE_RES_MISC_VEC_ID);
  1767. if (oicr_idx < 0)
  1768. return oicr_idx;
  1769. pf->oicr_idx = oicr_idx;
  1770. err = devm_request_irq(&pf->pdev->dev,
  1771. pf->msix_entries[pf->oicr_idx].vector,
  1772. ice_misc_intr, 0, pf->int_name, pf);
  1773. if (err) {
  1774. dev_err(&pf->pdev->dev,
  1775. "devm_request_irq for %s failed: %d\n",
  1776. pf->int_name, err);
  1777. ice_free_res(pf->irq_tracker, 1, ICE_RES_MISC_VEC_ID);
  1778. return err;
  1779. }
  1780. skip_req_irq:
  1781. ice_ena_misc_vector(pf);
  1782. val = ((pf->oicr_idx & PFINT_OICR_CTL_MSIX_INDX_M) |
  1783. PFINT_OICR_CTL_CAUSE_ENA_M);
  1784. wr32(hw, PFINT_OICR_CTL, val);
  1785. /* This enables Admin queue Interrupt causes */
  1786. val = ((pf->oicr_idx & PFINT_FW_CTL_MSIX_INDX_M) |
  1787. PFINT_FW_CTL_CAUSE_ENA_M);
  1788. wr32(hw, PFINT_FW_CTL, val);
  1789. itr_gran = hw->itr_gran_200;
  1790. wr32(hw, GLINT_ITR(ICE_RX_ITR, pf->oicr_idx),
  1791. ITR_TO_REG(ICE_ITR_8K, itr_gran));
  1792. ice_flush(hw);
  1793. ice_irq_dynamic_ena(hw, NULL, NULL);
  1794. return 0;
  1795. }
  1796. /**
  1797. * ice_vsi_get_qs_contig - Assign a contiguous chunk of queues to VSI
  1798. * @vsi: the VSI getting queues
  1799. *
  1800. * Return 0 on success and a negative value on error
  1801. */
  1802. static int ice_vsi_get_qs_contig(struct ice_vsi *vsi)
  1803. {
  1804. struct ice_pf *pf = vsi->back;
  1805. int offset, ret = 0;
  1806. mutex_lock(&pf->avail_q_mutex);
  1807. /* look for contiguous block of queues for tx */
  1808. offset = bitmap_find_next_zero_area(pf->avail_txqs, ICE_MAX_TXQS,
  1809. 0, vsi->alloc_txq, 0);
  1810. if (offset < ICE_MAX_TXQS) {
  1811. int i;
  1812. bitmap_set(pf->avail_txqs, offset, vsi->alloc_txq);
  1813. for (i = 0; i < vsi->alloc_txq; i++)
  1814. vsi->txq_map[i] = i + offset;
  1815. } else {
  1816. ret = -ENOMEM;
  1817. vsi->tx_mapping_mode = ICE_VSI_MAP_SCATTER;
  1818. }
  1819. /* look for contiguous block of queues for rx */
  1820. offset = bitmap_find_next_zero_area(pf->avail_rxqs, ICE_MAX_RXQS,
  1821. 0, vsi->alloc_rxq, 0);
  1822. if (offset < ICE_MAX_RXQS) {
  1823. int i;
  1824. bitmap_set(pf->avail_rxqs, offset, vsi->alloc_rxq);
  1825. for (i = 0; i < vsi->alloc_rxq; i++)
  1826. vsi->rxq_map[i] = i + offset;
  1827. } else {
  1828. ret = -ENOMEM;
  1829. vsi->rx_mapping_mode = ICE_VSI_MAP_SCATTER;
  1830. }
  1831. mutex_unlock(&pf->avail_q_mutex);
  1832. return ret;
  1833. }
  1834. /**
  1835. * ice_vsi_get_qs_scatter - Assign a scattered queues to VSI
  1836. * @vsi: the VSI getting queues
  1837. *
  1838. * Return 0 on success and a negative value on error
  1839. */
  1840. static int ice_vsi_get_qs_scatter(struct ice_vsi *vsi)
  1841. {
  1842. struct ice_pf *pf = vsi->back;
  1843. int i, index = 0;
  1844. mutex_lock(&pf->avail_q_mutex);
  1845. if (vsi->tx_mapping_mode == ICE_VSI_MAP_SCATTER) {
  1846. for (i = 0; i < vsi->alloc_txq; i++) {
  1847. index = find_next_zero_bit(pf->avail_txqs,
  1848. ICE_MAX_TXQS, index);
  1849. if (index < ICE_MAX_TXQS) {
  1850. set_bit(index, pf->avail_txqs);
  1851. vsi->txq_map[i] = index;
  1852. } else {
  1853. goto err_scatter_tx;
  1854. }
  1855. }
  1856. }
  1857. if (vsi->rx_mapping_mode == ICE_VSI_MAP_SCATTER) {
  1858. for (i = 0; i < vsi->alloc_rxq; i++) {
  1859. index = find_next_zero_bit(pf->avail_rxqs,
  1860. ICE_MAX_RXQS, index);
  1861. if (index < ICE_MAX_RXQS) {
  1862. set_bit(index, pf->avail_rxqs);
  1863. vsi->rxq_map[i] = index;
  1864. } else {
  1865. goto err_scatter_rx;
  1866. }
  1867. }
  1868. }
  1869. mutex_unlock(&pf->avail_q_mutex);
  1870. return 0;
  1871. err_scatter_rx:
  1872. /* unflag any queues we have grabbed (i is failed position) */
  1873. for (index = 0; index < i; index++) {
  1874. clear_bit(vsi->rxq_map[index], pf->avail_rxqs);
  1875. vsi->rxq_map[index] = 0;
  1876. }
  1877. i = vsi->alloc_txq;
  1878. err_scatter_tx:
  1879. /* i is either position of failed attempt or vsi->alloc_txq */
  1880. for (index = 0; index < i; index++) {
  1881. clear_bit(vsi->txq_map[index], pf->avail_txqs);
  1882. vsi->txq_map[index] = 0;
  1883. }
  1884. mutex_unlock(&pf->avail_q_mutex);
  1885. return -ENOMEM;
  1886. }
  1887. /**
  1888. * ice_vsi_get_qs - Assign queues from PF to VSI
  1889. * @vsi: the VSI to assign queues to
  1890. *
  1891. * Returns 0 on success and a negative value on error
  1892. */
  1893. static int ice_vsi_get_qs(struct ice_vsi *vsi)
  1894. {
  1895. int ret = 0;
  1896. vsi->tx_mapping_mode = ICE_VSI_MAP_CONTIG;
  1897. vsi->rx_mapping_mode = ICE_VSI_MAP_CONTIG;
  1898. /* NOTE: ice_vsi_get_qs_contig() will set the rx/tx mapping
  1899. * modes individually to scatter if assigning contiguous queues
  1900. * to rx or tx fails
  1901. */
  1902. ret = ice_vsi_get_qs_contig(vsi);
  1903. if (ret < 0) {
  1904. if (vsi->tx_mapping_mode == ICE_VSI_MAP_SCATTER)
  1905. vsi->alloc_txq = max_t(u16, vsi->alloc_txq,
  1906. ICE_MAX_SCATTER_TXQS);
  1907. if (vsi->rx_mapping_mode == ICE_VSI_MAP_SCATTER)
  1908. vsi->alloc_rxq = max_t(u16, vsi->alloc_rxq,
  1909. ICE_MAX_SCATTER_RXQS);
  1910. ret = ice_vsi_get_qs_scatter(vsi);
  1911. }
  1912. return ret;
  1913. }
  1914. /**
  1915. * ice_vsi_put_qs - Release queues from VSI to PF
  1916. * @vsi: the VSI thats going to release queues
  1917. */
  1918. static void ice_vsi_put_qs(struct ice_vsi *vsi)
  1919. {
  1920. struct ice_pf *pf = vsi->back;
  1921. int i;
  1922. mutex_lock(&pf->avail_q_mutex);
  1923. for (i = 0; i < vsi->alloc_txq; i++) {
  1924. clear_bit(vsi->txq_map[i], pf->avail_txqs);
  1925. vsi->txq_map[i] = ICE_INVAL_Q_INDEX;
  1926. }
  1927. for (i = 0; i < vsi->alloc_rxq; i++) {
  1928. clear_bit(vsi->rxq_map[i], pf->avail_rxqs);
  1929. vsi->rxq_map[i] = ICE_INVAL_Q_INDEX;
  1930. }
  1931. mutex_unlock(&pf->avail_q_mutex);
  1932. }
  1933. /**
  1934. * ice_free_q_vector - Free memory allocated for a specific interrupt vector
  1935. * @vsi: VSI having the memory freed
  1936. * @v_idx: index of the vector to be freed
  1937. */
  1938. static void ice_free_q_vector(struct ice_vsi *vsi, int v_idx)
  1939. {
  1940. struct ice_q_vector *q_vector;
  1941. struct ice_ring *ring;
  1942. if (!vsi->q_vectors[v_idx]) {
  1943. dev_dbg(&vsi->back->pdev->dev, "Queue vector at index %d not found\n",
  1944. v_idx);
  1945. return;
  1946. }
  1947. q_vector = vsi->q_vectors[v_idx];
  1948. ice_for_each_ring(ring, q_vector->tx)
  1949. ring->q_vector = NULL;
  1950. ice_for_each_ring(ring, q_vector->rx)
  1951. ring->q_vector = NULL;
  1952. /* only VSI with an associated netdev is set up with NAPI */
  1953. if (vsi->netdev)
  1954. netif_napi_del(&q_vector->napi);
  1955. devm_kfree(&vsi->back->pdev->dev, q_vector);
  1956. vsi->q_vectors[v_idx] = NULL;
  1957. }
  1958. /**
  1959. * ice_vsi_free_q_vectors - Free memory allocated for interrupt vectors
  1960. * @vsi: the VSI having memory freed
  1961. */
  1962. static void ice_vsi_free_q_vectors(struct ice_vsi *vsi)
  1963. {
  1964. int v_idx;
  1965. for (v_idx = 0; v_idx < vsi->num_q_vectors; v_idx++)
  1966. ice_free_q_vector(vsi, v_idx);
  1967. }
  1968. /**
  1969. * ice_cfg_netdev - Setup the netdev flags
  1970. * @vsi: the VSI being configured
  1971. *
  1972. * Returns 0 on success, negative value on failure
  1973. */
  1974. static int ice_cfg_netdev(struct ice_vsi *vsi)
  1975. {
  1976. netdev_features_t csumo_features;
  1977. netdev_features_t vlano_features;
  1978. netdev_features_t dflt_features;
  1979. netdev_features_t tso_features;
  1980. struct ice_netdev_priv *np;
  1981. struct net_device *netdev;
  1982. u8 mac_addr[ETH_ALEN];
  1983. netdev = alloc_etherdev_mqs(sizeof(struct ice_netdev_priv),
  1984. vsi->alloc_txq, vsi->alloc_rxq);
  1985. if (!netdev)
  1986. return -ENOMEM;
  1987. vsi->netdev = netdev;
  1988. np = netdev_priv(netdev);
  1989. np->vsi = vsi;
  1990. dflt_features = NETIF_F_SG |
  1991. NETIF_F_HIGHDMA |
  1992. NETIF_F_RXHASH;
  1993. csumo_features = NETIF_F_RXCSUM |
  1994. NETIF_F_IP_CSUM |
  1995. NETIF_F_IPV6_CSUM;
  1996. vlano_features = NETIF_F_HW_VLAN_CTAG_FILTER |
  1997. NETIF_F_HW_VLAN_CTAG_TX |
  1998. NETIF_F_HW_VLAN_CTAG_RX;
  1999. tso_features = NETIF_F_TSO;
  2000. /* set features that user can change */
  2001. netdev->hw_features = dflt_features | csumo_features |
  2002. vlano_features | tso_features;
  2003. /* enable features */
  2004. netdev->features |= netdev->hw_features;
  2005. /* encap and VLAN devices inherit default, csumo and tso features */
  2006. netdev->hw_enc_features |= dflt_features | csumo_features |
  2007. tso_features;
  2008. netdev->vlan_features |= dflt_features | csumo_features |
  2009. tso_features;
  2010. if (vsi->type == ICE_VSI_PF) {
  2011. SET_NETDEV_DEV(netdev, &vsi->back->pdev->dev);
  2012. ether_addr_copy(mac_addr, vsi->port_info->mac.perm_addr);
  2013. ether_addr_copy(netdev->dev_addr, mac_addr);
  2014. ether_addr_copy(netdev->perm_addr, mac_addr);
  2015. }
  2016. netdev->priv_flags |= IFF_UNICAST_FLT;
  2017. /* assign netdev_ops */
  2018. netdev->netdev_ops = &ice_netdev_ops;
  2019. /* setup watchdog timeout value to be 5 second */
  2020. netdev->watchdog_timeo = 5 * HZ;
  2021. ice_set_ethtool_ops(netdev);
  2022. netdev->min_mtu = ETH_MIN_MTU;
  2023. netdev->max_mtu = ICE_MAX_MTU;
  2024. return 0;
  2025. }
  2026. /**
  2027. * ice_vsi_free_arrays - clean up vsi resources
  2028. * @vsi: pointer to VSI being cleared
  2029. * @free_qvectors: bool to specify if q_vectors should be deallocated
  2030. */
  2031. static void ice_vsi_free_arrays(struct ice_vsi *vsi, bool free_qvectors)
  2032. {
  2033. struct ice_pf *pf = vsi->back;
  2034. /* free the ring and vector containers */
  2035. if (free_qvectors && vsi->q_vectors) {
  2036. devm_kfree(&pf->pdev->dev, vsi->q_vectors);
  2037. vsi->q_vectors = NULL;
  2038. }
  2039. if (vsi->tx_rings) {
  2040. devm_kfree(&pf->pdev->dev, vsi->tx_rings);
  2041. vsi->tx_rings = NULL;
  2042. }
  2043. if (vsi->rx_rings) {
  2044. devm_kfree(&pf->pdev->dev, vsi->rx_rings);
  2045. vsi->rx_rings = NULL;
  2046. }
  2047. }
  2048. /**
  2049. * ice_vsi_clear - clean up and deallocate the provided vsi
  2050. * @vsi: pointer to VSI being cleared
  2051. *
  2052. * This deallocates the vsi's queue resources, removes it from the PF's
  2053. * VSI array if necessary, and deallocates the VSI
  2054. *
  2055. * Returns 0 on success, negative on failure
  2056. */
  2057. static int ice_vsi_clear(struct ice_vsi *vsi)
  2058. {
  2059. struct ice_pf *pf = NULL;
  2060. if (!vsi)
  2061. return 0;
  2062. if (!vsi->back)
  2063. return -EINVAL;
  2064. pf = vsi->back;
  2065. if (!pf->vsi[vsi->idx] || pf->vsi[vsi->idx] != vsi) {
  2066. dev_dbg(&pf->pdev->dev, "vsi does not exist at pf->vsi[%d]\n",
  2067. vsi->idx);
  2068. return -EINVAL;
  2069. }
  2070. mutex_lock(&pf->sw_mutex);
  2071. /* updates the PF for this cleared vsi */
  2072. pf->vsi[vsi->idx] = NULL;
  2073. if (vsi->idx < pf->next_vsi)
  2074. pf->next_vsi = vsi->idx;
  2075. ice_vsi_free_arrays(vsi, true);
  2076. mutex_unlock(&pf->sw_mutex);
  2077. devm_kfree(&pf->pdev->dev, vsi);
  2078. return 0;
  2079. }
  2080. /**
  2081. * ice_vsi_alloc_q_vector - Allocate memory for a single interrupt vector
  2082. * @vsi: the VSI being configured
  2083. * @v_idx: index of the vector in the vsi struct
  2084. *
  2085. * We allocate one q_vector. If allocation fails we return -ENOMEM.
  2086. */
  2087. static int ice_vsi_alloc_q_vector(struct ice_vsi *vsi, int v_idx)
  2088. {
  2089. struct ice_pf *pf = vsi->back;
  2090. struct ice_q_vector *q_vector;
  2091. /* allocate q_vector */
  2092. q_vector = devm_kzalloc(&pf->pdev->dev, sizeof(*q_vector), GFP_KERNEL);
  2093. if (!q_vector)
  2094. return -ENOMEM;
  2095. q_vector->vsi = vsi;
  2096. q_vector->v_idx = v_idx;
  2097. /* only set affinity_mask if the CPU is online */
  2098. if (cpu_online(v_idx))
  2099. cpumask_set_cpu(v_idx, &q_vector->affinity_mask);
  2100. if (vsi->netdev)
  2101. netif_napi_add(vsi->netdev, &q_vector->napi, ice_napi_poll,
  2102. NAPI_POLL_WEIGHT);
  2103. /* tie q_vector and vsi together */
  2104. vsi->q_vectors[v_idx] = q_vector;
  2105. return 0;
  2106. }
  2107. /**
  2108. * ice_vsi_alloc_q_vectors - Allocate memory for interrupt vectors
  2109. * @vsi: the VSI being configured
  2110. *
  2111. * We allocate one q_vector per queue interrupt. If allocation fails we
  2112. * return -ENOMEM.
  2113. */
  2114. static int ice_vsi_alloc_q_vectors(struct ice_vsi *vsi)
  2115. {
  2116. struct ice_pf *pf = vsi->back;
  2117. int v_idx = 0, num_q_vectors;
  2118. int err;
  2119. if (vsi->q_vectors[0]) {
  2120. dev_dbg(&pf->pdev->dev, "VSI %d has existing q_vectors\n",
  2121. vsi->vsi_num);
  2122. return -EEXIST;
  2123. }
  2124. if (test_bit(ICE_FLAG_MSIX_ENA, pf->flags)) {
  2125. num_q_vectors = vsi->num_q_vectors;
  2126. } else {
  2127. err = -EINVAL;
  2128. goto err_out;
  2129. }
  2130. for (v_idx = 0; v_idx < num_q_vectors; v_idx++) {
  2131. err = ice_vsi_alloc_q_vector(vsi, v_idx);
  2132. if (err)
  2133. goto err_out;
  2134. }
  2135. return 0;
  2136. err_out:
  2137. while (v_idx--)
  2138. ice_free_q_vector(vsi, v_idx);
  2139. dev_err(&pf->pdev->dev,
  2140. "Failed to allocate %d q_vector for VSI %d, ret=%d\n",
  2141. vsi->num_q_vectors, vsi->vsi_num, err);
  2142. vsi->num_q_vectors = 0;
  2143. return err;
  2144. }
  2145. /**
  2146. * ice_vsi_setup_vector_base - Set up the base vector for the given VSI
  2147. * @vsi: ptr to the VSI
  2148. *
  2149. * This should only be called after ice_vsi_alloc() which allocates the
  2150. * corresponding SW VSI structure and initializes num_queue_pairs for the
  2151. * newly allocated VSI.
  2152. *
  2153. * Returns 0 on success or negative on failure
  2154. */
  2155. static int ice_vsi_setup_vector_base(struct ice_vsi *vsi)
  2156. {
  2157. struct ice_pf *pf = vsi->back;
  2158. int num_q_vectors = 0;
  2159. if (vsi->base_vector) {
  2160. dev_dbg(&pf->pdev->dev, "VSI %d has non-zero base vector %d\n",
  2161. vsi->vsi_num, vsi->base_vector);
  2162. return -EEXIST;
  2163. }
  2164. if (!test_bit(ICE_FLAG_MSIX_ENA, pf->flags))
  2165. return -ENOENT;
  2166. switch (vsi->type) {
  2167. case ICE_VSI_PF:
  2168. num_q_vectors = vsi->num_q_vectors;
  2169. break;
  2170. default:
  2171. dev_warn(&vsi->back->pdev->dev, "Unknown VSI type %d\n",
  2172. vsi->type);
  2173. break;
  2174. }
  2175. if (num_q_vectors)
  2176. vsi->base_vector = ice_get_res(pf, pf->irq_tracker,
  2177. num_q_vectors, vsi->idx);
  2178. if (vsi->base_vector < 0) {
  2179. dev_err(&pf->pdev->dev,
  2180. "Failed to get tracking for %d vectors for VSI %d, err=%d\n",
  2181. num_q_vectors, vsi->vsi_num, vsi->base_vector);
  2182. return -ENOENT;
  2183. }
  2184. return 0;
  2185. }
  2186. /**
  2187. * ice_fill_rss_lut - Fill the RSS lookup table with default values
  2188. * @lut: Lookup table
  2189. * @rss_table_size: Lookup table size
  2190. * @rss_size: Range of queue number for hashing
  2191. */
  2192. void ice_fill_rss_lut(u8 *lut, u16 rss_table_size, u16 rss_size)
  2193. {
  2194. u16 i;
  2195. for (i = 0; i < rss_table_size; i++)
  2196. lut[i] = i % rss_size;
  2197. }
  2198. /**
  2199. * ice_vsi_cfg_rss - Configure RSS params for a VSI
  2200. * @vsi: VSI to be configured
  2201. */
  2202. static int ice_vsi_cfg_rss(struct ice_vsi *vsi)
  2203. {
  2204. u8 seed[ICE_AQC_GET_SET_RSS_KEY_DATA_RSS_KEY_SIZE];
  2205. struct ice_aqc_get_set_rss_keys *key;
  2206. struct ice_pf *pf = vsi->back;
  2207. enum ice_status status;
  2208. int err = 0;
  2209. u8 *lut;
  2210. vsi->rss_size = min_t(int, vsi->rss_size, vsi->num_rxq);
  2211. lut = devm_kzalloc(&pf->pdev->dev, vsi->rss_table_size, GFP_KERNEL);
  2212. if (!lut)
  2213. return -ENOMEM;
  2214. if (vsi->rss_lut_user)
  2215. memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
  2216. else
  2217. ice_fill_rss_lut(lut, vsi->rss_table_size, vsi->rss_size);
  2218. status = ice_aq_set_rss_lut(&pf->hw, vsi->vsi_num, vsi->rss_lut_type,
  2219. lut, vsi->rss_table_size);
  2220. if (status) {
  2221. dev_err(&vsi->back->pdev->dev,
  2222. "set_rss_lut failed, error %d\n", status);
  2223. err = -EIO;
  2224. goto ice_vsi_cfg_rss_exit;
  2225. }
  2226. key = devm_kzalloc(&vsi->back->pdev->dev, sizeof(*key), GFP_KERNEL);
  2227. if (!key) {
  2228. err = -ENOMEM;
  2229. goto ice_vsi_cfg_rss_exit;
  2230. }
  2231. if (vsi->rss_hkey_user)
  2232. memcpy(seed, vsi->rss_hkey_user,
  2233. ICE_AQC_GET_SET_RSS_KEY_DATA_RSS_KEY_SIZE);
  2234. else
  2235. netdev_rss_key_fill((void *)seed,
  2236. ICE_AQC_GET_SET_RSS_KEY_DATA_RSS_KEY_SIZE);
  2237. memcpy(&key->standard_rss_key, seed,
  2238. ICE_AQC_GET_SET_RSS_KEY_DATA_RSS_KEY_SIZE);
  2239. status = ice_aq_set_rss_key(&pf->hw, vsi->vsi_num, key);
  2240. if (status) {
  2241. dev_err(&vsi->back->pdev->dev, "set_rss_key failed, error %d\n",
  2242. status);
  2243. err = -EIO;
  2244. }
  2245. devm_kfree(&pf->pdev->dev, key);
  2246. ice_vsi_cfg_rss_exit:
  2247. devm_kfree(&pf->pdev->dev, lut);
  2248. return err;
  2249. }
  2250. /**
  2251. * ice_vsi_reinit_setup - return resource and reallocate resource for a VSI
  2252. * @vsi: pointer to the ice_vsi
  2253. *
  2254. * This reallocates the VSIs queue resources
  2255. *
  2256. * Returns 0 on success and negative value on failure
  2257. */
  2258. static int ice_vsi_reinit_setup(struct ice_vsi *vsi)
  2259. {
  2260. u16 max_txqs[ICE_MAX_TRAFFIC_CLASS] = { 0 };
  2261. int ret, i;
  2262. if (!vsi)
  2263. return -EINVAL;
  2264. ice_vsi_free_q_vectors(vsi);
  2265. ice_free_res(vsi->back->irq_tracker, vsi->base_vector, vsi->idx);
  2266. vsi->base_vector = 0;
  2267. ice_vsi_clear_rings(vsi);
  2268. ice_vsi_free_arrays(vsi, false);
  2269. ice_vsi_set_num_qs(vsi);
  2270. /* Initialize VSI struct elements and create VSI in FW */
  2271. ret = ice_vsi_add(vsi);
  2272. if (ret < 0)
  2273. goto err_vsi;
  2274. ret = ice_vsi_alloc_arrays(vsi, false);
  2275. if (ret < 0)
  2276. goto err_vsi;
  2277. switch (vsi->type) {
  2278. case ICE_VSI_PF:
  2279. if (!vsi->netdev) {
  2280. ret = ice_cfg_netdev(vsi);
  2281. if (ret)
  2282. goto err_rings;
  2283. ret = register_netdev(vsi->netdev);
  2284. if (ret)
  2285. goto err_rings;
  2286. netif_carrier_off(vsi->netdev);
  2287. netif_tx_stop_all_queues(vsi->netdev);
  2288. }
  2289. ret = ice_vsi_alloc_q_vectors(vsi);
  2290. if (ret)
  2291. goto err_rings;
  2292. ret = ice_vsi_setup_vector_base(vsi);
  2293. if (ret)
  2294. goto err_vectors;
  2295. ret = ice_vsi_alloc_rings(vsi);
  2296. if (ret)
  2297. goto err_vectors;
  2298. ice_vsi_map_rings_to_vectors(vsi);
  2299. break;
  2300. default:
  2301. break;
  2302. }
  2303. ice_vsi_set_tc_cfg(vsi);
  2304. /* configure VSI nodes based on number of queues and TC's */
  2305. for (i = 0; i < vsi->tc_cfg.numtc; i++)
  2306. max_txqs[i] = vsi->num_txq;
  2307. ret = ice_cfg_vsi_lan(vsi->port_info, vsi->vsi_num,
  2308. vsi->tc_cfg.ena_tc, max_txqs);
  2309. if (ret) {
  2310. dev_info(&vsi->back->pdev->dev,
  2311. "Failed VSI lan queue config\n");
  2312. goto err_vectors;
  2313. }
  2314. return 0;
  2315. err_vectors:
  2316. ice_vsi_free_q_vectors(vsi);
  2317. err_rings:
  2318. if (vsi->netdev) {
  2319. vsi->current_netdev_flags = 0;
  2320. unregister_netdev(vsi->netdev);
  2321. free_netdev(vsi->netdev);
  2322. vsi->netdev = NULL;
  2323. }
  2324. err_vsi:
  2325. ice_vsi_clear(vsi);
  2326. set_bit(__ICE_RESET_FAILED, vsi->back->state);
  2327. return ret;
  2328. }
  2329. /**
  2330. * ice_vsi_setup - Set up a VSI by a given type
  2331. * @pf: board private structure
  2332. * @type: VSI type
  2333. * @pi: pointer to the port_info instance
  2334. *
  2335. * This allocates the sw VSI structure and its queue resources.
  2336. *
  2337. * Returns pointer to the successfully allocated and configure VSI sw struct on
  2338. * success, otherwise returns NULL on failure.
  2339. */
  2340. static struct ice_vsi *
  2341. ice_vsi_setup(struct ice_pf *pf, enum ice_vsi_type type,
  2342. struct ice_port_info *pi)
  2343. {
  2344. u16 max_txqs[ICE_MAX_TRAFFIC_CLASS] = { 0 };
  2345. struct device *dev = &pf->pdev->dev;
  2346. struct ice_vsi_ctx ctxt = { 0 };
  2347. struct ice_vsi *vsi;
  2348. int ret, i;
  2349. vsi = ice_vsi_alloc(pf, type);
  2350. if (!vsi) {
  2351. dev_err(dev, "could not allocate VSI\n");
  2352. return NULL;
  2353. }
  2354. vsi->port_info = pi;
  2355. vsi->vsw = pf->first_sw;
  2356. if (ice_vsi_get_qs(vsi)) {
  2357. dev_err(dev, "Failed to allocate queues. vsi->idx = %d\n",
  2358. vsi->idx);
  2359. goto err_get_qs;
  2360. }
  2361. /* set RSS capabilities */
  2362. ice_vsi_set_rss_params(vsi);
  2363. /* create the VSI */
  2364. ret = ice_vsi_add(vsi);
  2365. if (ret)
  2366. goto err_vsi;
  2367. ctxt.vsi_num = vsi->vsi_num;
  2368. switch (vsi->type) {
  2369. case ICE_VSI_PF:
  2370. ret = ice_cfg_netdev(vsi);
  2371. if (ret)
  2372. goto err_cfg_netdev;
  2373. ret = register_netdev(vsi->netdev);
  2374. if (ret)
  2375. goto err_register_netdev;
  2376. netif_carrier_off(vsi->netdev);
  2377. /* make sure transmit queues start off as stopped */
  2378. netif_tx_stop_all_queues(vsi->netdev);
  2379. ret = ice_vsi_alloc_q_vectors(vsi);
  2380. if (ret)
  2381. goto err_msix;
  2382. ret = ice_vsi_setup_vector_base(vsi);
  2383. if (ret)
  2384. goto err_rings;
  2385. ret = ice_vsi_alloc_rings(vsi);
  2386. if (ret)
  2387. goto err_rings;
  2388. ice_vsi_map_rings_to_vectors(vsi);
  2389. /* Do not exit if configuring RSS had an issue, at least
  2390. * receive traffic on first queue. Hence no need to capture
  2391. * return value
  2392. */
  2393. if (test_bit(ICE_FLAG_RSS_ENA, pf->flags))
  2394. ice_vsi_cfg_rss(vsi);
  2395. break;
  2396. default:
  2397. /* if vsi type is not recognized, clean up the resources and
  2398. * exit
  2399. */
  2400. goto err_rings;
  2401. }
  2402. ice_vsi_set_tc_cfg(vsi);
  2403. /* configure VSI nodes based on number of queues and TC's */
  2404. for (i = 0; i < vsi->tc_cfg.numtc; i++)
  2405. max_txqs[i] = vsi->num_txq;
  2406. ret = ice_cfg_vsi_lan(vsi->port_info, vsi->vsi_num,
  2407. vsi->tc_cfg.ena_tc, max_txqs);
  2408. if (ret) {
  2409. dev_info(&pf->pdev->dev, "Failed VSI lan queue config\n");
  2410. goto err_rings;
  2411. }
  2412. return vsi;
  2413. err_rings:
  2414. ice_vsi_free_q_vectors(vsi);
  2415. err_msix:
  2416. if (vsi->netdev && vsi->netdev->reg_state == NETREG_REGISTERED)
  2417. unregister_netdev(vsi->netdev);
  2418. err_register_netdev:
  2419. if (vsi->netdev) {
  2420. free_netdev(vsi->netdev);
  2421. vsi->netdev = NULL;
  2422. }
  2423. err_cfg_netdev:
  2424. ret = ice_aq_free_vsi(&pf->hw, &ctxt, false, NULL);
  2425. if (ret)
  2426. dev_err(&vsi->back->pdev->dev,
  2427. "Free VSI AQ call failed, err %d\n", ret);
  2428. err_vsi:
  2429. ice_vsi_put_qs(vsi);
  2430. err_get_qs:
  2431. pf->q_left_tx += vsi->alloc_txq;
  2432. pf->q_left_rx += vsi->alloc_rxq;
  2433. ice_vsi_clear(vsi);
  2434. return NULL;
  2435. }
  2436. /**
  2437. * ice_vsi_add_vlan - Add vsi membership for given vlan
  2438. * @vsi: the vsi being configured
  2439. * @vid: vlan id to be added
  2440. */
  2441. static int ice_vsi_add_vlan(struct ice_vsi *vsi, u16 vid)
  2442. {
  2443. struct ice_fltr_list_entry *tmp;
  2444. struct ice_pf *pf = vsi->back;
  2445. LIST_HEAD(tmp_add_list);
  2446. enum ice_status status;
  2447. int err = 0;
  2448. tmp = devm_kzalloc(&pf->pdev->dev, sizeof(*tmp), GFP_KERNEL);
  2449. if (!tmp)
  2450. return -ENOMEM;
  2451. tmp->fltr_info.lkup_type = ICE_SW_LKUP_VLAN;
  2452. tmp->fltr_info.fltr_act = ICE_FWD_TO_VSI;
  2453. tmp->fltr_info.flag = ICE_FLTR_TX;
  2454. tmp->fltr_info.src = vsi->vsi_num;
  2455. tmp->fltr_info.fwd_id.vsi_id = vsi->vsi_num;
  2456. tmp->fltr_info.l_data.vlan.vlan_id = vid;
  2457. INIT_LIST_HEAD(&tmp->list_entry);
  2458. list_add(&tmp->list_entry, &tmp_add_list);
  2459. status = ice_add_vlan(&pf->hw, &tmp_add_list);
  2460. if (status) {
  2461. err = -ENODEV;
  2462. dev_err(&pf->pdev->dev, "Failure Adding VLAN %d on VSI %i\n",
  2463. vid, vsi->vsi_num);
  2464. }
  2465. ice_free_fltr_list(&pf->pdev->dev, &tmp_add_list);
  2466. return err;
  2467. }
  2468. /**
  2469. * ice_vlan_rx_add_vid - Add a vlan id filter to HW offload
  2470. * @netdev: network interface to be adjusted
  2471. * @proto: unused protocol
  2472. * @vid: vlan id to be added
  2473. *
  2474. * net_device_ops implementation for adding vlan ids
  2475. */
  2476. static int ice_vlan_rx_add_vid(struct net_device *netdev,
  2477. __always_unused __be16 proto, u16 vid)
  2478. {
  2479. struct ice_netdev_priv *np = netdev_priv(netdev);
  2480. struct ice_vsi *vsi = np->vsi;
  2481. int ret = 0;
  2482. if (vid >= VLAN_N_VID) {
  2483. netdev_err(netdev, "VLAN id requested %d is out of range %d\n",
  2484. vid, VLAN_N_VID);
  2485. return -EINVAL;
  2486. }
  2487. if (vsi->info.pvid)
  2488. return -EINVAL;
  2489. /* Add all VLAN ids including 0 to the switch filter. VLAN id 0 is
  2490. * needed to continue allowing all untagged packets since VLAN prune
  2491. * list is applied to all packets by the switch
  2492. */
  2493. ret = ice_vsi_add_vlan(vsi, vid);
  2494. if (!ret)
  2495. set_bit(vid, vsi->active_vlans);
  2496. return ret;
  2497. }
  2498. /**
  2499. * ice_vsi_kill_vlan - Remove VSI membership for a given VLAN
  2500. * @vsi: the VSI being configured
  2501. * @vid: VLAN id to be removed
  2502. */
  2503. static void ice_vsi_kill_vlan(struct ice_vsi *vsi, u16 vid)
  2504. {
  2505. struct ice_fltr_list_entry *list;
  2506. struct ice_pf *pf = vsi->back;
  2507. LIST_HEAD(tmp_add_list);
  2508. list = devm_kzalloc(&pf->pdev->dev, sizeof(*list), GFP_KERNEL);
  2509. if (!list)
  2510. return;
  2511. list->fltr_info.lkup_type = ICE_SW_LKUP_VLAN;
  2512. list->fltr_info.fwd_id.vsi_id = vsi->vsi_num;
  2513. list->fltr_info.fltr_act = ICE_FWD_TO_VSI;
  2514. list->fltr_info.l_data.vlan.vlan_id = vid;
  2515. list->fltr_info.flag = ICE_FLTR_TX;
  2516. list->fltr_info.src = vsi->vsi_num;
  2517. INIT_LIST_HEAD(&list->list_entry);
  2518. list_add(&list->list_entry, &tmp_add_list);
  2519. if (ice_remove_vlan(&pf->hw, &tmp_add_list))
  2520. dev_err(&pf->pdev->dev, "Error removing VLAN %d on vsi %i\n",
  2521. vid, vsi->vsi_num);
  2522. ice_free_fltr_list(&pf->pdev->dev, &tmp_add_list);
  2523. }
  2524. /**
  2525. * ice_vlan_rx_kill_vid - Remove a vlan id filter from HW offload
  2526. * @netdev: network interface to be adjusted
  2527. * @proto: unused protocol
  2528. * @vid: vlan id to be removed
  2529. *
  2530. * net_device_ops implementation for removing vlan ids
  2531. */
  2532. static int ice_vlan_rx_kill_vid(struct net_device *netdev,
  2533. __always_unused __be16 proto, u16 vid)
  2534. {
  2535. struct ice_netdev_priv *np = netdev_priv(netdev);
  2536. struct ice_vsi *vsi = np->vsi;
  2537. if (vsi->info.pvid)
  2538. return -EINVAL;
  2539. /* return code is ignored as there is nothing a user
  2540. * can do about failure to remove and a log message was
  2541. * already printed from the other function
  2542. */
  2543. ice_vsi_kill_vlan(vsi, vid);
  2544. clear_bit(vid, vsi->active_vlans);
  2545. return 0;
  2546. }
  2547. /**
  2548. * ice_setup_pf_sw - Setup the HW switch on startup or after reset
  2549. * @pf: board private structure
  2550. *
  2551. * Returns 0 on success, negative value on failure
  2552. */
  2553. static int ice_setup_pf_sw(struct ice_pf *pf)
  2554. {
  2555. LIST_HEAD(tmp_add_list);
  2556. u8 broadcast[ETH_ALEN];
  2557. struct ice_vsi *vsi;
  2558. int status = 0;
  2559. if (!ice_is_reset_recovery_pending(pf->state)) {
  2560. vsi = ice_vsi_setup(pf, ICE_VSI_PF, pf->hw.port_info);
  2561. if (!vsi) {
  2562. status = -ENOMEM;
  2563. goto error_exit;
  2564. }
  2565. } else {
  2566. vsi = pf->vsi[0];
  2567. status = ice_vsi_reinit_setup(vsi);
  2568. if (status < 0)
  2569. return -EIO;
  2570. }
  2571. /* tmp_add_list contains a list of MAC addresses for which MAC
  2572. * filters need to be programmed. Add the VSI's unicast MAC to
  2573. * this list
  2574. */
  2575. status = ice_add_mac_to_list(vsi, &tmp_add_list,
  2576. vsi->port_info->mac.perm_addr);
  2577. if (status)
  2578. goto error_exit;
  2579. /* VSI needs to receive broadcast traffic, so add the broadcast
  2580. * MAC address to the list.
  2581. */
  2582. eth_broadcast_addr(broadcast);
  2583. status = ice_add_mac_to_list(vsi, &tmp_add_list, broadcast);
  2584. if (status)
  2585. goto error_exit;
  2586. /* program MAC filters for entries in tmp_add_list */
  2587. status = ice_add_mac(&pf->hw, &tmp_add_list);
  2588. if (status) {
  2589. dev_err(&pf->pdev->dev, "Could not add MAC filters\n");
  2590. status = -ENOMEM;
  2591. goto error_exit;
  2592. }
  2593. ice_free_fltr_list(&pf->pdev->dev, &tmp_add_list);
  2594. return status;
  2595. error_exit:
  2596. ice_free_fltr_list(&pf->pdev->dev, &tmp_add_list);
  2597. if (vsi) {
  2598. ice_vsi_free_q_vectors(vsi);
  2599. if (vsi->netdev && vsi->netdev->reg_state == NETREG_REGISTERED)
  2600. unregister_netdev(vsi->netdev);
  2601. if (vsi->netdev) {
  2602. free_netdev(vsi->netdev);
  2603. vsi->netdev = NULL;
  2604. }
  2605. ice_vsi_delete(vsi);
  2606. ice_vsi_put_qs(vsi);
  2607. pf->q_left_tx += vsi->alloc_txq;
  2608. pf->q_left_rx += vsi->alloc_rxq;
  2609. ice_vsi_clear(vsi);
  2610. }
  2611. return status;
  2612. }
  2613. /**
  2614. * ice_determine_q_usage - Calculate queue distribution
  2615. * @pf: board private structure
  2616. *
  2617. * Return -ENOMEM if we don't get enough queues for all ports
  2618. */
  2619. static void ice_determine_q_usage(struct ice_pf *pf)
  2620. {
  2621. u16 q_left_tx, q_left_rx;
  2622. q_left_tx = pf->hw.func_caps.common_cap.num_txq;
  2623. q_left_rx = pf->hw.func_caps.common_cap.num_rxq;
  2624. pf->num_lan_tx = min_t(int, q_left_tx, num_online_cpus());
  2625. /* only 1 rx queue unless RSS is enabled */
  2626. if (!test_bit(ICE_FLAG_RSS_ENA, pf->flags))
  2627. pf->num_lan_rx = 1;
  2628. else
  2629. pf->num_lan_rx = min_t(int, q_left_rx, num_online_cpus());
  2630. pf->q_left_tx = q_left_tx - pf->num_lan_tx;
  2631. pf->q_left_rx = q_left_rx - pf->num_lan_rx;
  2632. }
  2633. /**
  2634. * ice_deinit_pf - Unrolls initialziations done by ice_init_pf
  2635. * @pf: board private structure to initialize
  2636. */
  2637. static void ice_deinit_pf(struct ice_pf *pf)
  2638. {
  2639. if (pf->serv_tmr.function)
  2640. del_timer_sync(&pf->serv_tmr);
  2641. if (pf->serv_task.func)
  2642. cancel_work_sync(&pf->serv_task);
  2643. mutex_destroy(&pf->sw_mutex);
  2644. mutex_destroy(&pf->avail_q_mutex);
  2645. }
  2646. /**
  2647. * ice_init_pf - Initialize general software structures (struct ice_pf)
  2648. * @pf: board private structure to initialize
  2649. */
  2650. static void ice_init_pf(struct ice_pf *pf)
  2651. {
  2652. bitmap_zero(pf->flags, ICE_PF_FLAGS_NBITS);
  2653. set_bit(ICE_FLAG_MSIX_ENA, pf->flags);
  2654. mutex_init(&pf->sw_mutex);
  2655. mutex_init(&pf->avail_q_mutex);
  2656. /* Clear avail_[t|r]x_qs bitmaps (set all to avail) */
  2657. mutex_lock(&pf->avail_q_mutex);
  2658. bitmap_zero(pf->avail_txqs, ICE_MAX_TXQS);
  2659. bitmap_zero(pf->avail_rxqs, ICE_MAX_RXQS);
  2660. mutex_unlock(&pf->avail_q_mutex);
  2661. if (pf->hw.func_caps.common_cap.rss_table_size)
  2662. set_bit(ICE_FLAG_RSS_ENA, pf->flags);
  2663. /* setup service timer and periodic service task */
  2664. timer_setup(&pf->serv_tmr, ice_service_timer, 0);
  2665. pf->serv_tmr_period = HZ;
  2666. INIT_WORK(&pf->serv_task, ice_service_task);
  2667. clear_bit(__ICE_SERVICE_SCHED, pf->state);
  2668. }
  2669. /**
  2670. * ice_ena_msix_range - Request a range of MSIX vectors from the OS
  2671. * @pf: board private structure
  2672. *
  2673. * compute the number of MSIX vectors required (v_budget) and request from
  2674. * the OS. Return the number of vectors reserved or negative on failure
  2675. */
  2676. static int ice_ena_msix_range(struct ice_pf *pf)
  2677. {
  2678. int v_left, v_actual, v_budget = 0;
  2679. int needed, err, i;
  2680. v_left = pf->hw.func_caps.common_cap.num_msix_vectors;
  2681. /* reserve one vector for miscellaneous handler */
  2682. needed = 1;
  2683. v_budget += needed;
  2684. v_left -= needed;
  2685. /* reserve vectors for LAN traffic */
  2686. pf->num_lan_msix = min_t(int, num_online_cpus(), v_left);
  2687. v_budget += pf->num_lan_msix;
  2688. pf->msix_entries = devm_kcalloc(&pf->pdev->dev, v_budget,
  2689. sizeof(struct msix_entry), GFP_KERNEL);
  2690. if (!pf->msix_entries) {
  2691. err = -ENOMEM;
  2692. goto exit_err;
  2693. }
  2694. for (i = 0; i < v_budget; i++)
  2695. pf->msix_entries[i].entry = i;
  2696. /* actually reserve the vectors */
  2697. v_actual = pci_enable_msix_range(pf->pdev, pf->msix_entries,
  2698. ICE_MIN_MSIX, v_budget);
  2699. if (v_actual < 0) {
  2700. dev_err(&pf->pdev->dev, "unable to reserve MSI-X vectors\n");
  2701. err = v_actual;
  2702. goto msix_err;
  2703. }
  2704. if (v_actual < v_budget) {
  2705. dev_warn(&pf->pdev->dev,
  2706. "not enough vectors. requested = %d, obtained = %d\n",
  2707. v_budget, v_actual);
  2708. if (v_actual >= (pf->num_lan_msix + 1)) {
  2709. pf->num_avail_msix = v_actual - (pf->num_lan_msix + 1);
  2710. } else if (v_actual >= 2) {
  2711. pf->num_lan_msix = 1;
  2712. pf->num_avail_msix = v_actual - 2;
  2713. } else {
  2714. pci_disable_msix(pf->pdev);
  2715. err = -ERANGE;
  2716. goto msix_err;
  2717. }
  2718. }
  2719. return v_actual;
  2720. msix_err:
  2721. devm_kfree(&pf->pdev->dev, pf->msix_entries);
  2722. goto exit_err;
  2723. exit_err:
  2724. pf->num_lan_msix = 0;
  2725. clear_bit(ICE_FLAG_MSIX_ENA, pf->flags);
  2726. return err;
  2727. }
  2728. /**
  2729. * ice_dis_msix - Disable MSI-X interrupt setup in OS
  2730. * @pf: board private structure
  2731. */
  2732. static void ice_dis_msix(struct ice_pf *pf)
  2733. {
  2734. pci_disable_msix(pf->pdev);
  2735. devm_kfree(&pf->pdev->dev, pf->msix_entries);
  2736. pf->msix_entries = NULL;
  2737. clear_bit(ICE_FLAG_MSIX_ENA, pf->flags);
  2738. }
  2739. /**
  2740. * ice_init_interrupt_scheme - Determine proper interrupt scheme
  2741. * @pf: board private structure to initialize
  2742. */
  2743. static int ice_init_interrupt_scheme(struct ice_pf *pf)
  2744. {
  2745. int vectors = 0;
  2746. ssize_t size;
  2747. if (test_bit(ICE_FLAG_MSIX_ENA, pf->flags))
  2748. vectors = ice_ena_msix_range(pf);
  2749. else
  2750. return -ENODEV;
  2751. if (vectors < 0)
  2752. return vectors;
  2753. /* set up vector assignment tracking */
  2754. size = sizeof(struct ice_res_tracker) + (sizeof(u16) * vectors);
  2755. pf->irq_tracker = devm_kzalloc(&pf->pdev->dev, size, GFP_KERNEL);
  2756. if (!pf->irq_tracker) {
  2757. ice_dis_msix(pf);
  2758. return -ENOMEM;
  2759. }
  2760. pf->irq_tracker->num_entries = vectors;
  2761. return 0;
  2762. }
  2763. /**
  2764. * ice_clear_interrupt_scheme - Undo things done by ice_init_interrupt_scheme
  2765. * @pf: board private structure
  2766. */
  2767. static void ice_clear_interrupt_scheme(struct ice_pf *pf)
  2768. {
  2769. if (test_bit(ICE_FLAG_MSIX_ENA, pf->flags))
  2770. ice_dis_msix(pf);
  2771. if (pf->irq_tracker) {
  2772. devm_kfree(&pf->pdev->dev, pf->irq_tracker);
  2773. pf->irq_tracker = NULL;
  2774. }
  2775. }
  2776. /**
  2777. * ice_probe - Device initialization routine
  2778. * @pdev: PCI device information struct
  2779. * @ent: entry in ice_pci_tbl
  2780. *
  2781. * Returns 0 on success, negative on failure
  2782. */
  2783. static int ice_probe(struct pci_dev *pdev,
  2784. const struct pci_device_id __always_unused *ent)
  2785. {
  2786. struct ice_pf *pf;
  2787. struct ice_hw *hw;
  2788. int err;
  2789. /* this driver uses devres, see Documentation/driver-model/devres.txt */
  2790. err = pcim_enable_device(pdev);
  2791. if (err)
  2792. return err;
  2793. err = pcim_iomap_regions(pdev, BIT(ICE_BAR0), pci_name(pdev));
  2794. if (err) {
  2795. dev_err(&pdev->dev, "BAR0 I/O map error %d\n", err);
  2796. return err;
  2797. }
  2798. pf = devm_kzalloc(&pdev->dev, sizeof(*pf), GFP_KERNEL);
  2799. if (!pf)
  2800. return -ENOMEM;
  2801. /* set up for high or low dma */
  2802. err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
  2803. if (err)
  2804. err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
  2805. if (err) {
  2806. dev_err(&pdev->dev, "DMA configuration failed: 0x%x\n", err);
  2807. return err;
  2808. }
  2809. pci_enable_pcie_error_reporting(pdev);
  2810. pci_set_master(pdev);
  2811. pf->pdev = pdev;
  2812. pci_set_drvdata(pdev, pf);
  2813. set_bit(__ICE_DOWN, pf->state);
  2814. hw = &pf->hw;
  2815. hw->hw_addr = pcim_iomap_table(pdev)[ICE_BAR0];
  2816. hw->back = pf;
  2817. hw->vendor_id = pdev->vendor;
  2818. hw->device_id = pdev->device;
  2819. pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
  2820. hw->subsystem_vendor_id = pdev->subsystem_vendor;
  2821. hw->subsystem_device_id = pdev->subsystem_device;
  2822. hw->bus.device = PCI_SLOT(pdev->devfn);
  2823. hw->bus.func = PCI_FUNC(pdev->devfn);
  2824. ice_set_ctrlq_len(hw);
  2825. pf->msg_enable = netif_msg_init(debug, ICE_DFLT_NETIF_M);
  2826. #ifndef CONFIG_DYNAMIC_DEBUG
  2827. if (debug < -1)
  2828. hw->debug_mask = debug;
  2829. #endif
  2830. err = ice_init_hw(hw);
  2831. if (err) {
  2832. dev_err(&pdev->dev, "ice_init_hw failed: %d\n", err);
  2833. err = -EIO;
  2834. goto err_exit_unroll;
  2835. }
  2836. dev_info(&pdev->dev, "firmware %d.%d.%05d api %d.%d\n",
  2837. hw->fw_maj_ver, hw->fw_min_ver, hw->fw_build,
  2838. hw->api_maj_ver, hw->api_min_ver);
  2839. ice_init_pf(pf);
  2840. ice_determine_q_usage(pf);
  2841. pf->num_alloc_vsi = min_t(u16, ICE_MAX_VSI_ALLOC,
  2842. hw->func_caps.guaranteed_num_vsi);
  2843. if (!pf->num_alloc_vsi) {
  2844. err = -EIO;
  2845. goto err_init_pf_unroll;
  2846. }
  2847. pf->vsi = devm_kcalloc(&pdev->dev, pf->num_alloc_vsi,
  2848. sizeof(struct ice_vsi *), GFP_KERNEL);
  2849. if (!pf->vsi) {
  2850. err = -ENOMEM;
  2851. goto err_init_pf_unroll;
  2852. }
  2853. err = ice_init_interrupt_scheme(pf);
  2854. if (err) {
  2855. dev_err(&pdev->dev,
  2856. "ice_init_interrupt_scheme failed: %d\n", err);
  2857. err = -EIO;
  2858. goto err_init_interrupt_unroll;
  2859. }
  2860. /* In case of MSIX we are going to setup the misc vector right here
  2861. * to handle admin queue events etc. In case of legacy and MSI
  2862. * the misc functionality and queue processing is combined in
  2863. * the same vector and that gets setup at open.
  2864. */
  2865. if (test_bit(ICE_FLAG_MSIX_ENA, pf->flags)) {
  2866. err = ice_req_irq_msix_misc(pf);
  2867. if (err) {
  2868. dev_err(&pdev->dev,
  2869. "setup of misc vector failed: %d\n", err);
  2870. goto err_init_interrupt_unroll;
  2871. }
  2872. }
  2873. /* create switch struct for the switch element created by FW on boot */
  2874. pf->first_sw = devm_kzalloc(&pdev->dev, sizeof(struct ice_sw),
  2875. GFP_KERNEL);
  2876. if (!pf->first_sw) {
  2877. err = -ENOMEM;
  2878. goto err_msix_misc_unroll;
  2879. }
  2880. pf->first_sw->bridge_mode = BRIDGE_MODE_VEB;
  2881. pf->first_sw->pf = pf;
  2882. /* record the sw_id available for later use */
  2883. pf->first_sw->sw_id = hw->port_info->sw_id;
  2884. err = ice_setup_pf_sw(pf);
  2885. if (err) {
  2886. dev_err(&pdev->dev,
  2887. "probe failed due to setup pf switch:%d\n", err);
  2888. goto err_alloc_sw_unroll;
  2889. }
  2890. /* Driver is mostly up */
  2891. clear_bit(__ICE_DOWN, pf->state);
  2892. /* since everything is good, start the service timer */
  2893. mod_timer(&pf->serv_tmr, round_jiffies(jiffies + pf->serv_tmr_period));
  2894. err = ice_init_link_events(pf->hw.port_info);
  2895. if (err) {
  2896. dev_err(&pdev->dev, "ice_init_link_events failed: %d\n", err);
  2897. goto err_alloc_sw_unroll;
  2898. }
  2899. return 0;
  2900. err_alloc_sw_unroll:
  2901. set_bit(__ICE_DOWN, pf->state);
  2902. devm_kfree(&pf->pdev->dev, pf->first_sw);
  2903. err_msix_misc_unroll:
  2904. ice_free_irq_msix_misc(pf);
  2905. err_init_interrupt_unroll:
  2906. ice_clear_interrupt_scheme(pf);
  2907. devm_kfree(&pdev->dev, pf->vsi);
  2908. err_init_pf_unroll:
  2909. ice_deinit_pf(pf);
  2910. ice_deinit_hw(hw);
  2911. err_exit_unroll:
  2912. pci_disable_pcie_error_reporting(pdev);
  2913. return err;
  2914. }
  2915. /**
  2916. * ice_remove - Device removal routine
  2917. * @pdev: PCI device information struct
  2918. */
  2919. static void ice_remove(struct pci_dev *pdev)
  2920. {
  2921. struct ice_pf *pf = pci_get_drvdata(pdev);
  2922. int i = 0;
  2923. int err;
  2924. if (!pf)
  2925. return;
  2926. set_bit(__ICE_DOWN, pf->state);
  2927. for (i = 0; i < pf->num_alloc_vsi; i++) {
  2928. if (!pf->vsi[i])
  2929. continue;
  2930. err = ice_vsi_release(pf->vsi[i]);
  2931. if (err)
  2932. dev_dbg(&pf->pdev->dev, "Failed to release VSI index %d (err %d)\n",
  2933. i, err);
  2934. }
  2935. ice_free_irq_msix_misc(pf);
  2936. ice_clear_interrupt_scheme(pf);
  2937. ice_deinit_pf(pf);
  2938. ice_deinit_hw(&pf->hw);
  2939. pci_disable_pcie_error_reporting(pdev);
  2940. }
  2941. /* ice_pci_tbl - PCI Device ID Table
  2942. *
  2943. * Wildcard entries (PCI_ANY_ID) should come last
  2944. * Last entry must be all 0s
  2945. *
  2946. * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
  2947. * Class, Class Mask, private data (not used) }
  2948. */
  2949. static const struct pci_device_id ice_pci_tbl[] = {
  2950. { PCI_VDEVICE(INTEL, ICE_DEV_ID_C810_BACKPLANE), 0 },
  2951. { PCI_VDEVICE(INTEL, ICE_DEV_ID_C810_QSFP), 0 },
  2952. { PCI_VDEVICE(INTEL, ICE_DEV_ID_C810_SFP), 0 },
  2953. { PCI_VDEVICE(INTEL, ICE_DEV_ID_C810_10G_BASE_T), 0 },
  2954. { PCI_VDEVICE(INTEL, ICE_DEV_ID_C810_SGMII), 0 },
  2955. /* required last entry */
  2956. { 0, }
  2957. };
  2958. MODULE_DEVICE_TABLE(pci, ice_pci_tbl);
  2959. static struct pci_driver ice_driver = {
  2960. .name = KBUILD_MODNAME,
  2961. .id_table = ice_pci_tbl,
  2962. .probe = ice_probe,
  2963. .remove = ice_remove,
  2964. };
  2965. /**
  2966. * ice_module_init - Driver registration routine
  2967. *
  2968. * ice_module_init is the first routine called when the driver is
  2969. * loaded. All it does is register with the PCI subsystem.
  2970. */
  2971. static int __init ice_module_init(void)
  2972. {
  2973. int status;
  2974. pr_info("%s - version %s\n", ice_driver_string, ice_drv_ver);
  2975. pr_info("%s\n", ice_copyright);
  2976. ice_wq = alloc_ordered_workqueue("%s", WQ_MEM_RECLAIM, KBUILD_MODNAME);
  2977. if (!ice_wq) {
  2978. pr_err("Failed to create workqueue\n");
  2979. return -ENOMEM;
  2980. }
  2981. status = pci_register_driver(&ice_driver);
  2982. if (status) {
  2983. pr_err("failed to register pci driver, err %d\n", status);
  2984. destroy_workqueue(ice_wq);
  2985. }
  2986. return status;
  2987. }
  2988. module_init(ice_module_init);
  2989. /**
  2990. * ice_module_exit - Driver exit cleanup routine
  2991. *
  2992. * ice_module_exit is called just before the driver is removed
  2993. * from memory.
  2994. */
  2995. static void __exit ice_module_exit(void)
  2996. {
  2997. pci_unregister_driver(&ice_driver);
  2998. destroy_workqueue(ice_wq);
  2999. pr_info("module unloaded\n");
  3000. }
  3001. module_exit(ice_module_exit);
  3002. /**
  3003. * ice_set_mac_address - NDO callback to set mac address
  3004. * @netdev: network interface device structure
  3005. * @pi: pointer to an address structure
  3006. *
  3007. * Returns 0 on success, negative on failure
  3008. */
  3009. static int ice_set_mac_address(struct net_device *netdev, void *pi)
  3010. {
  3011. struct ice_netdev_priv *np = netdev_priv(netdev);
  3012. struct ice_vsi *vsi = np->vsi;
  3013. struct ice_pf *pf = vsi->back;
  3014. struct ice_hw *hw = &pf->hw;
  3015. struct sockaddr *addr = pi;
  3016. enum ice_status status;
  3017. LIST_HEAD(a_mac_list);
  3018. LIST_HEAD(r_mac_list);
  3019. u8 flags = 0;
  3020. int err;
  3021. u8 *mac;
  3022. mac = (u8 *)addr->sa_data;
  3023. if (!is_valid_ether_addr(mac))
  3024. return -EADDRNOTAVAIL;
  3025. if (ether_addr_equal(netdev->dev_addr, mac)) {
  3026. netdev_warn(netdev, "already using mac %pM\n", mac);
  3027. return 0;
  3028. }
  3029. if (test_bit(__ICE_DOWN, pf->state) ||
  3030. ice_is_reset_recovery_pending(pf->state)) {
  3031. netdev_err(netdev, "can't set mac %pM. device not ready\n",
  3032. mac);
  3033. return -EBUSY;
  3034. }
  3035. /* When we change the mac address we also have to change the mac address
  3036. * based filter rules that were created previously for the old mac
  3037. * address. So first, we remove the old filter rule using ice_remove_mac
  3038. * and then create a new filter rule using ice_add_mac. Note that for
  3039. * both these operations, we first need to form a "list" of mac
  3040. * addresses (even though in this case, we have only 1 mac address to be
  3041. * added/removed) and this done using ice_add_mac_to_list. Depending on
  3042. * the ensuing operation this "list" of mac addresses is either to be
  3043. * added or removed from the filter.
  3044. */
  3045. err = ice_add_mac_to_list(vsi, &r_mac_list, netdev->dev_addr);
  3046. if (err) {
  3047. err = -EADDRNOTAVAIL;
  3048. goto free_lists;
  3049. }
  3050. status = ice_remove_mac(hw, &r_mac_list);
  3051. if (status) {
  3052. err = -EADDRNOTAVAIL;
  3053. goto free_lists;
  3054. }
  3055. err = ice_add_mac_to_list(vsi, &a_mac_list, mac);
  3056. if (err) {
  3057. err = -EADDRNOTAVAIL;
  3058. goto free_lists;
  3059. }
  3060. status = ice_add_mac(hw, &a_mac_list);
  3061. if (status) {
  3062. err = -EADDRNOTAVAIL;
  3063. goto free_lists;
  3064. }
  3065. free_lists:
  3066. /* free list entries */
  3067. ice_free_fltr_list(&pf->pdev->dev, &r_mac_list);
  3068. ice_free_fltr_list(&pf->pdev->dev, &a_mac_list);
  3069. if (err) {
  3070. netdev_err(netdev, "can't set mac %pM. filter update failed\n",
  3071. mac);
  3072. return err;
  3073. }
  3074. /* change the netdev's mac address */
  3075. memcpy(netdev->dev_addr, mac, netdev->addr_len);
  3076. netdev_dbg(vsi->netdev, "updated mac address to %pM\n",
  3077. netdev->dev_addr);
  3078. /* write new mac address to the firmware */
  3079. flags = ICE_AQC_MAN_MAC_UPDATE_LAA_WOL;
  3080. status = ice_aq_manage_mac_write(hw, mac, flags, NULL);
  3081. if (status) {
  3082. netdev_err(netdev, "can't set mac %pM. write to firmware failed.\n",
  3083. mac);
  3084. }
  3085. return 0;
  3086. }
  3087. /**
  3088. * ice_set_rx_mode - NDO callback to set the netdev filters
  3089. * @netdev: network interface device structure
  3090. */
  3091. static void ice_set_rx_mode(struct net_device *netdev)
  3092. {
  3093. struct ice_netdev_priv *np = netdev_priv(netdev);
  3094. struct ice_vsi *vsi = np->vsi;
  3095. if (!vsi)
  3096. return;
  3097. /* Set the flags to synchronize filters
  3098. * ndo_set_rx_mode may be triggered even without a change in netdev
  3099. * flags
  3100. */
  3101. set_bit(ICE_VSI_FLAG_UMAC_FLTR_CHANGED, vsi->flags);
  3102. set_bit(ICE_VSI_FLAG_MMAC_FLTR_CHANGED, vsi->flags);
  3103. set_bit(ICE_FLAG_FLTR_SYNC, vsi->back->flags);
  3104. /* schedule our worker thread which will take care of
  3105. * applying the new filter changes
  3106. */
  3107. ice_service_task_schedule(vsi->back);
  3108. }
  3109. /**
  3110. * ice_fdb_add - add an entry to the hardware database
  3111. * @ndm: the input from the stack
  3112. * @tb: pointer to array of nladdr (unused)
  3113. * @dev: the net device pointer
  3114. * @addr: the MAC address entry being added
  3115. * @vid: VLAN id
  3116. * @flags: instructions from stack about fdb operation
  3117. */
  3118. static int ice_fdb_add(struct ndmsg *ndm, struct nlattr __always_unused *tb[],
  3119. struct net_device *dev, const unsigned char *addr,
  3120. u16 vid, u16 flags)
  3121. {
  3122. int err;
  3123. if (vid) {
  3124. netdev_err(dev, "VLANs aren't supported yet for dev_uc|mc_add()\n");
  3125. return -EINVAL;
  3126. }
  3127. if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
  3128. netdev_err(dev, "FDB only supports static addresses\n");
  3129. return -EINVAL;
  3130. }
  3131. if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
  3132. err = dev_uc_add_excl(dev, addr);
  3133. else if (is_multicast_ether_addr(addr))
  3134. err = dev_mc_add_excl(dev, addr);
  3135. else
  3136. err = -EINVAL;
  3137. /* Only return duplicate errors if NLM_F_EXCL is set */
  3138. if (err == -EEXIST && !(flags & NLM_F_EXCL))
  3139. err = 0;
  3140. return err;
  3141. }
  3142. /**
  3143. * ice_fdb_del - delete an entry from the hardware database
  3144. * @ndm: the input from the stack
  3145. * @tb: pointer to array of nladdr (unused)
  3146. * @dev: the net device pointer
  3147. * @addr: the MAC address entry being added
  3148. * @vid: VLAN id
  3149. */
  3150. static int ice_fdb_del(struct ndmsg *ndm, __always_unused struct nlattr *tb[],
  3151. struct net_device *dev, const unsigned char *addr,
  3152. __always_unused u16 vid)
  3153. {
  3154. int err;
  3155. if (ndm->ndm_state & NUD_PERMANENT) {
  3156. netdev_err(dev, "FDB only supports static addresses\n");
  3157. return -EINVAL;
  3158. }
  3159. if (is_unicast_ether_addr(addr))
  3160. err = dev_uc_del(dev, addr);
  3161. else if (is_multicast_ether_addr(addr))
  3162. err = dev_mc_del(dev, addr);
  3163. else
  3164. err = -EINVAL;
  3165. return err;
  3166. }
  3167. /**
  3168. * ice_vsi_manage_vlan_insertion - Manage VLAN insertion for the VSI for Tx
  3169. * @vsi: the vsi being changed
  3170. */
  3171. static int ice_vsi_manage_vlan_insertion(struct ice_vsi *vsi)
  3172. {
  3173. struct device *dev = &vsi->back->pdev->dev;
  3174. struct ice_hw *hw = &vsi->back->hw;
  3175. struct ice_vsi_ctx ctxt = { 0 };
  3176. enum ice_status status;
  3177. /* Here we are configuring the VSI to let the driver add VLAN tags by
  3178. * setting vlan_flags to ICE_AQ_VSI_VLAN_MODE_ALL. The actual VLAN tag
  3179. * insertion happens in the Tx hot path, in ice_tx_map.
  3180. */
  3181. ctxt.info.vlan_flags = ICE_AQ_VSI_VLAN_MODE_ALL;
  3182. ctxt.info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_VLAN_VALID);
  3183. ctxt.vsi_num = vsi->vsi_num;
  3184. status = ice_aq_update_vsi(hw, &ctxt, NULL);
  3185. if (status) {
  3186. dev_err(dev, "update VSI for VLAN insert failed, err %d aq_err %d\n",
  3187. status, hw->adminq.sq_last_status);
  3188. return -EIO;
  3189. }
  3190. vsi->info.vlan_flags = ctxt.info.vlan_flags;
  3191. return 0;
  3192. }
  3193. /**
  3194. * ice_vsi_manage_vlan_stripping - Manage VLAN stripping for the VSI for Rx
  3195. * @vsi: the vsi being changed
  3196. * @ena: boolean value indicating if this is a enable or disable request
  3197. */
  3198. static int ice_vsi_manage_vlan_stripping(struct ice_vsi *vsi, bool ena)
  3199. {
  3200. struct device *dev = &vsi->back->pdev->dev;
  3201. struct ice_hw *hw = &vsi->back->hw;
  3202. struct ice_vsi_ctx ctxt = { 0 };
  3203. enum ice_status status;
  3204. /* Here we are configuring what the VSI should do with the VLAN tag in
  3205. * the Rx packet. We can either leave the tag in the packet or put it in
  3206. * the Rx descriptor.
  3207. */
  3208. if (ena) {
  3209. /* Strip VLAN tag from Rx packet and put it in the desc */
  3210. ctxt.info.vlan_flags = ICE_AQ_VSI_VLAN_EMOD_STR_BOTH;
  3211. } else {
  3212. /* Disable stripping. Leave tag in packet */
  3213. ctxt.info.vlan_flags = ICE_AQ_VSI_VLAN_EMOD_NOTHING;
  3214. }
  3215. /* Allow all packets untagged/tagged */
  3216. ctxt.info.vlan_flags |= ICE_AQ_VSI_VLAN_MODE_ALL;
  3217. ctxt.info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_VLAN_VALID);
  3218. ctxt.vsi_num = vsi->vsi_num;
  3219. status = ice_aq_update_vsi(hw, &ctxt, NULL);
  3220. if (status) {
  3221. dev_err(dev, "update VSI for VALN strip failed, ena = %d err %d aq_err %d\n",
  3222. ena, status, hw->adminq.sq_last_status);
  3223. return -EIO;
  3224. }
  3225. vsi->info.vlan_flags = ctxt.info.vlan_flags;
  3226. return 0;
  3227. }
  3228. /**
  3229. * ice_set_features - set the netdev feature flags
  3230. * @netdev: ptr to the netdev being adjusted
  3231. * @features: the feature set that the stack is suggesting
  3232. */
  3233. static int ice_set_features(struct net_device *netdev,
  3234. netdev_features_t features)
  3235. {
  3236. struct ice_netdev_priv *np = netdev_priv(netdev);
  3237. struct ice_vsi *vsi = np->vsi;
  3238. int ret = 0;
  3239. if ((features & NETIF_F_HW_VLAN_CTAG_RX) &&
  3240. !(netdev->features & NETIF_F_HW_VLAN_CTAG_RX))
  3241. ret = ice_vsi_manage_vlan_stripping(vsi, true);
  3242. else if (!(features & NETIF_F_HW_VLAN_CTAG_RX) &&
  3243. (netdev->features & NETIF_F_HW_VLAN_CTAG_RX))
  3244. ret = ice_vsi_manage_vlan_stripping(vsi, false);
  3245. else if ((features & NETIF_F_HW_VLAN_CTAG_TX) &&
  3246. !(netdev->features & NETIF_F_HW_VLAN_CTAG_TX))
  3247. ret = ice_vsi_manage_vlan_insertion(vsi);
  3248. else if (!(features & NETIF_F_HW_VLAN_CTAG_TX) &&
  3249. (netdev->features & NETIF_F_HW_VLAN_CTAG_TX))
  3250. ret = ice_vsi_manage_vlan_insertion(vsi);
  3251. return ret;
  3252. }
  3253. /**
  3254. * ice_vsi_vlan_setup - Setup vlan offload properties on a VSI
  3255. * @vsi: VSI to setup vlan properties for
  3256. */
  3257. static int ice_vsi_vlan_setup(struct ice_vsi *vsi)
  3258. {
  3259. int ret = 0;
  3260. if (vsi->netdev->features & NETIF_F_HW_VLAN_CTAG_RX)
  3261. ret = ice_vsi_manage_vlan_stripping(vsi, true);
  3262. if (vsi->netdev->features & NETIF_F_HW_VLAN_CTAG_TX)
  3263. ret = ice_vsi_manage_vlan_insertion(vsi);
  3264. return ret;
  3265. }
  3266. /**
  3267. * ice_restore_vlan - Reinstate VLANs when vsi/netdev comes back up
  3268. * @vsi: the VSI being brought back up
  3269. */
  3270. static int ice_restore_vlan(struct ice_vsi *vsi)
  3271. {
  3272. int err;
  3273. u16 vid;
  3274. if (!vsi->netdev)
  3275. return -EINVAL;
  3276. err = ice_vsi_vlan_setup(vsi);
  3277. if (err)
  3278. return err;
  3279. for_each_set_bit(vid, vsi->active_vlans, VLAN_N_VID) {
  3280. err = ice_vlan_rx_add_vid(vsi->netdev, htons(ETH_P_8021Q), vid);
  3281. if (err)
  3282. break;
  3283. }
  3284. return err;
  3285. }
  3286. /**
  3287. * ice_setup_tx_ctx - setup a struct ice_tlan_ctx instance
  3288. * @ring: The Tx ring to configure
  3289. * @tlan_ctx: Pointer to the Tx LAN queue context structure to be initialized
  3290. * @pf_q: queue index in the PF space
  3291. *
  3292. * Configure the Tx descriptor ring in TLAN context.
  3293. */
  3294. static void
  3295. ice_setup_tx_ctx(struct ice_ring *ring, struct ice_tlan_ctx *tlan_ctx, u16 pf_q)
  3296. {
  3297. struct ice_vsi *vsi = ring->vsi;
  3298. struct ice_hw *hw = &vsi->back->hw;
  3299. tlan_ctx->base = ring->dma >> ICE_TLAN_CTX_BASE_S;
  3300. tlan_ctx->port_num = vsi->port_info->lport;
  3301. /* Transmit Queue Length */
  3302. tlan_ctx->qlen = ring->count;
  3303. /* PF number */
  3304. tlan_ctx->pf_num = hw->pf_id;
  3305. /* queue belongs to a specific VSI type
  3306. * VF / VM index should be programmed per vmvf_type setting:
  3307. * for vmvf_type = VF, it is VF number between 0-256
  3308. * for vmvf_type = VM, it is VM number between 0-767
  3309. * for PF or EMP this field should be set to zero
  3310. */
  3311. switch (vsi->type) {
  3312. case ICE_VSI_PF:
  3313. tlan_ctx->vmvf_type = ICE_TLAN_CTX_VMVF_TYPE_PF;
  3314. break;
  3315. default:
  3316. return;
  3317. }
  3318. /* make sure the context is associated with the right VSI */
  3319. tlan_ctx->src_vsi = vsi->vsi_num;
  3320. tlan_ctx->tso_ena = ICE_TX_LEGACY;
  3321. tlan_ctx->tso_qnum = pf_q;
  3322. /* Legacy or Advanced Host Interface:
  3323. * 0: Advanced Host Interface
  3324. * 1: Legacy Host Interface
  3325. */
  3326. tlan_ctx->legacy_int = ICE_TX_LEGACY;
  3327. }
  3328. /**
  3329. * ice_vsi_cfg_txqs - Configure the VSI for Tx
  3330. * @vsi: the VSI being configured
  3331. *
  3332. * Return 0 on success and a negative value on error
  3333. * Configure the Tx VSI for operation.
  3334. */
  3335. static int ice_vsi_cfg_txqs(struct ice_vsi *vsi)
  3336. {
  3337. struct ice_aqc_add_tx_qgrp *qg_buf;
  3338. struct ice_aqc_add_txqs_perq *txq;
  3339. struct ice_pf *pf = vsi->back;
  3340. enum ice_status status;
  3341. u16 buf_len, i, pf_q;
  3342. int err = 0, tc = 0;
  3343. u8 num_q_grps;
  3344. buf_len = sizeof(struct ice_aqc_add_tx_qgrp);
  3345. qg_buf = devm_kzalloc(&pf->pdev->dev, buf_len, GFP_KERNEL);
  3346. if (!qg_buf)
  3347. return -ENOMEM;
  3348. if (vsi->num_txq > ICE_MAX_TXQ_PER_TXQG) {
  3349. err = -EINVAL;
  3350. goto err_cfg_txqs;
  3351. }
  3352. qg_buf->num_txqs = 1;
  3353. num_q_grps = 1;
  3354. /* set up and configure the tx queues */
  3355. ice_for_each_txq(vsi, i) {
  3356. struct ice_tlan_ctx tlan_ctx = { 0 };
  3357. pf_q = vsi->txq_map[i];
  3358. ice_setup_tx_ctx(vsi->tx_rings[i], &tlan_ctx, pf_q);
  3359. /* copy context contents into the qg_buf */
  3360. qg_buf->txqs[0].txq_id = cpu_to_le16(pf_q);
  3361. ice_set_ctx((u8 *)&tlan_ctx, qg_buf->txqs[0].txq_ctx,
  3362. ice_tlan_ctx_info);
  3363. /* init queue specific tail reg. It is referred as transmit
  3364. * comm scheduler queue doorbell.
  3365. */
  3366. vsi->tx_rings[i]->tail = pf->hw.hw_addr + QTX_COMM_DBELL(pf_q);
  3367. status = ice_ena_vsi_txq(vsi->port_info, vsi->vsi_num, tc,
  3368. num_q_grps, qg_buf, buf_len, NULL);
  3369. if (status) {
  3370. dev_err(&vsi->back->pdev->dev,
  3371. "Failed to set LAN Tx queue context, error: %d\n",
  3372. status);
  3373. err = -ENODEV;
  3374. goto err_cfg_txqs;
  3375. }
  3376. /* Add Tx Queue TEID into the VSI tx ring from the response
  3377. * This will complete configuring and enabling the queue.
  3378. */
  3379. txq = &qg_buf->txqs[0];
  3380. if (pf_q == le16_to_cpu(txq->txq_id))
  3381. vsi->tx_rings[i]->txq_teid =
  3382. le32_to_cpu(txq->q_teid);
  3383. }
  3384. err_cfg_txqs:
  3385. devm_kfree(&pf->pdev->dev, qg_buf);
  3386. return err;
  3387. }
  3388. /**
  3389. * ice_setup_rx_ctx - Configure a receive ring context
  3390. * @ring: The Rx ring to configure
  3391. *
  3392. * Configure the Rx descriptor ring in RLAN context.
  3393. */
  3394. static int ice_setup_rx_ctx(struct ice_ring *ring)
  3395. {
  3396. struct ice_vsi *vsi = ring->vsi;
  3397. struct ice_hw *hw = &vsi->back->hw;
  3398. u32 rxdid = ICE_RXDID_FLEX_NIC;
  3399. struct ice_rlan_ctx rlan_ctx;
  3400. u32 regval;
  3401. u16 pf_q;
  3402. int err;
  3403. /* what is RX queue number in global space of 2K rx queues */
  3404. pf_q = vsi->rxq_map[ring->q_index];
  3405. /* clear the context structure first */
  3406. memset(&rlan_ctx, 0, sizeof(rlan_ctx));
  3407. rlan_ctx.base = ring->dma >> ICE_RLAN_BASE_S;
  3408. rlan_ctx.qlen = ring->count;
  3409. /* Receive Packet Data Buffer Size.
  3410. * The Packet Data Buffer Size is defined in 128 byte units.
  3411. */
  3412. rlan_ctx.dbuf = vsi->rx_buf_len >> ICE_RLAN_CTX_DBUF_S;
  3413. /* use 32 byte descriptors */
  3414. rlan_ctx.dsize = 1;
  3415. /* Strip the Ethernet CRC bytes before the packet is posted to host
  3416. * memory.
  3417. */
  3418. rlan_ctx.crcstrip = 1;
  3419. /* L2TSEL flag defines the reported L2 Tags in the receive descriptor */
  3420. rlan_ctx.l2tsel = 1;
  3421. rlan_ctx.dtype = ICE_RX_DTYPE_NO_SPLIT;
  3422. rlan_ctx.hsplit_0 = ICE_RLAN_RX_HSPLIT_0_NO_SPLIT;
  3423. rlan_ctx.hsplit_1 = ICE_RLAN_RX_HSPLIT_1_NO_SPLIT;
  3424. /* This controls whether VLAN is stripped from inner headers
  3425. * The VLAN in the inner L2 header is stripped to the receive
  3426. * descriptor if enabled by this flag.
  3427. */
  3428. rlan_ctx.showiv = 0;
  3429. /* Max packet size for this queue - must not be set to a larger value
  3430. * than 5 x DBUF
  3431. */
  3432. rlan_ctx.rxmax = min_t(u16, vsi->max_frame,
  3433. ICE_MAX_CHAINED_RX_BUFS * vsi->rx_buf_len);
  3434. /* Rx queue threshold in units of 64 */
  3435. rlan_ctx.lrxqthresh = 1;
  3436. /* Enable Flexible Descriptors in the queue context which
  3437. * allows this driver to select a specific receive descriptor format
  3438. */
  3439. regval = rd32(hw, QRXFLXP_CNTXT(pf_q));
  3440. regval |= (rxdid << QRXFLXP_CNTXT_RXDID_IDX_S) &
  3441. QRXFLXP_CNTXT_RXDID_IDX_M;
  3442. /* increasing context priority to pick up profile id;
  3443. * default is 0x01; setting to 0x03 to ensure profile
  3444. * is programming if prev context is of same priority
  3445. */
  3446. regval |= (0x03 << QRXFLXP_CNTXT_RXDID_PRIO_S) &
  3447. QRXFLXP_CNTXT_RXDID_PRIO_M;
  3448. wr32(hw, QRXFLXP_CNTXT(pf_q), regval);
  3449. /* Absolute queue number out of 2K needs to be passed */
  3450. err = ice_write_rxq_ctx(hw, &rlan_ctx, pf_q);
  3451. if (err) {
  3452. dev_err(&vsi->back->pdev->dev,
  3453. "Failed to set LAN Rx queue context for absolute Rx queue %d error: %d\n",
  3454. pf_q, err);
  3455. return -EIO;
  3456. }
  3457. /* init queue specific tail register */
  3458. ring->tail = hw->hw_addr + QRX_TAIL(pf_q);
  3459. writel(0, ring->tail);
  3460. ice_alloc_rx_bufs(ring, ICE_DESC_UNUSED(ring));
  3461. return 0;
  3462. }
  3463. /**
  3464. * ice_vsi_cfg_rxqs - Configure the VSI for Rx
  3465. * @vsi: the VSI being configured
  3466. *
  3467. * Return 0 on success and a negative value on error
  3468. * Configure the Rx VSI for operation.
  3469. */
  3470. static int ice_vsi_cfg_rxqs(struct ice_vsi *vsi)
  3471. {
  3472. int err = 0;
  3473. u16 i;
  3474. if (vsi->netdev && vsi->netdev->mtu > ETH_DATA_LEN)
  3475. vsi->max_frame = vsi->netdev->mtu +
  3476. ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
  3477. else
  3478. vsi->max_frame = ICE_RXBUF_2048;
  3479. vsi->rx_buf_len = ICE_RXBUF_2048;
  3480. /* set up individual rings */
  3481. for (i = 0; i < vsi->num_rxq && !err; i++)
  3482. err = ice_setup_rx_ctx(vsi->rx_rings[i]);
  3483. if (err) {
  3484. dev_err(&vsi->back->pdev->dev, "ice_setup_rx_ctx failed\n");
  3485. return -EIO;
  3486. }
  3487. return err;
  3488. }
  3489. /**
  3490. * ice_vsi_cfg - Setup the VSI
  3491. * @vsi: the VSI being configured
  3492. *
  3493. * Return 0 on success and negative value on error
  3494. */
  3495. static int ice_vsi_cfg(struct ice_vsi *vsi)
  3496. {
  3497. int err;
  3498. if (vsi->netdev) {
  3499. ice_set_rx_mode(vsi->netdev);
  3500. err = ice_restore_vlan(vsi);
  3501. if (err)
  3502. return err;
  3503. }
  3504. err = ice_vsi_cfg_txqs(vsi);
  3505. if (!err)
  3506. err = ice_vsi_cfg_rxqs(vsi);
  3507. return err;
  3508. }
  3509. /**
  3510. * ice_vsi_stop_tx_rings - Disable Tx rings
  3511. * @vsi: the VSI being configured
  3512. */
  3513. static int ice_vsi_stop_tx_rings(struct ice_vsi *vsi)
  3514. {
  3515. struct ice_pf *pf = vsi->back;
  3516. struct ice_hw *hw = &pf->hw;
  3517. enum ice_status status;
  3518. u32 *q_teids, val;
  3519. u16 *q_ids, i;
  3520. int err = 0;
  3521. if (vsi->num_txq > ICE_LAN_TXQ_MAX_QDIS)
  3522. return -EINVAL;
  3523. q_teids = devm_kcalloc(&pf->pdev->dev, vsi->num_txq, sizeof(*q_teids),
  3524. GFP_KERNEL);
  3525. if (!q_teids)
  3526. return -ENOMEM;
  3527. q_ids = devm_kcalloc(&pf->pdev->dev, vsi->num_txq, sizeof(*q_ids),
  3528. GFP_KERNEL);
  3529. if (!q_ids) {
  3530. err = -ENOMEM;
  3531. goto err_alloc_q_ids;
  3532. }
  3533. /* set up the tx queue list to be disabled */
  3534. ice_for_each_txq(vsi, i) {
  3535. u16 v_idx;
  3536. if (!vsi->tx_rings || !vsi->tx_rings[i]) {
  3537. err = -EINVAL;
  3538. goto err_out;
  3539. }
  3540. q_ids[i] = vsi->txq_map[i];
  3541. q_teids[i] = vsi->tx_rings[i]->txq_teid;
  3542. /* clear cause_ena bit for disabled queues */
  3543. val = rd32(hw, QINT_TQCTL(vsi->tx_rings[i]->reg_idx));
  3544. val &= ~QINT_TQCTL_CAUSE_ENA_M;
  3545. wr32(hw, QINT_TQCTL(vsi->tx_rings[i]->reg_idx), val);
  3546. /* software is expected to wait for 100 ns */
  3547. ndelay(100);
  3548. /* trigger a software interrupt for the vector associated to
  3549. * the queue to schedule napi handler
  3550. */
  3551. v_idx = vsi->tx_rings[i]->q_vector->v_idx;
  3552. wr32(hw, GLINT_DYN_CTL(vsi->base_vector + v_idx),
  3553. GLINT_DYN_CTL_SWINT_TRIG_M | GLINT_DYN_CTL_INTENA_MSK_M);
  3554. }
  3555. status = ice_dis_vsi_txq(vsi->port_info, vsi->num_txq, q_ids, q_teids,
  3556. NULL);
  3557. if (status) {
  3558. dev_err(&pf->pdev->dev,
  3559. "Failed to disable LAN Tx queues, error: %d\n",
  3560. status);
  3561. err = -ENODEV;
  3562. }
  3563. err_out:
  3564. devm_kfree(&pf->pdev->dev, q_ids);
  3565. err_alloc_q_ids:
  3566. devm_kfree(&pf->pdev->dev, q_teids);
  3567. return err;
  3568. }
  3569. /**
  3570. * ice_pf_rxq_wait - Wait for a PF's Rx queue to be enabled or disabled
  3571. * @pf: the PF being configured
  3572. * @pf_q: the PF queue
  3573. * @ena: enable or disable state of the queue
  3574. *
  3575. * This routine will wait for the given Rx queue of the PF to reach the
  3576. * enabled or disabled state.
  3577. * Returns -ETIMEDOUT in case of failing to reach the requested state after
  3578. * multiple retries; else will return 0 in case of success.
  3579. */
  3580. static int ice_pf_rxq_wait(struct ice_pf *pf, int pf_q, bool ena)
  3581. {
  3582. int i;
  3583. for (i = 0; i < ICE_Q_WAIT_RETRY_LIMIT; i++) {
  3584. u32 rx_reg = rd32(&pf->hw, QRX_CTRL(pf_q));
  3585. if (ena == !!(rx_reg & QRX_CTRL_QENA_STAT_M))
  3586. break;
  3587. usleep_range(10, 20);
  3588. }
  3589. if (i >= ICE_Q_WAIT_RETRY_LIMIT)
  3590. return -ETIMEDOUT;
  3591. return 0;
  3592. }
  3593. /**
  3594. * ice_vsi_ctrl_rx_rings - Start or stop a VSI's rx rings
  3595. * @vsi: the VSI being configured
  3596. * @ena: start or stop the rx rings
  3597. */
  3598. static int ice_vsi_ctrl_rx_rings(struct ice_vsi *vsi, bool ena)
  3599. {
  3600. struct ice_pf *pf = vsi->back;
  3601. struct ice_hw *hw = &pf->hw;
  3602. int i, j, ret = 0;
  3603. for (i = 0; i < vsi->num_rxq; i++) {
  3604. int pf_q = vsi->rxq_map[i];
  3605. u32 rx_reg;
  3606. for (j = 0; j < ICE_Q_WAIT_MAX_RETRY; j++) {
  3607. rx_reg = rd32(hw, QRX_CTRL(pf_q));
  3608. if (((rx_reg >> QRX_CTRL_QENA_REQ_S) & 1) ==
  3609. ((rx_reg >> QRX_CTRL_QENA_STAT_S) & 1))
  3610. break;
  3611. usleep_range(1000, 2000);
  3612. }
  3613. /* Skip if the queue is already in the requested state */
  3614. if (ena == !!(rx_reg & QRX_CTRL_QENA_STAT_M))
  3615. continue;
  3616. /* turn on/off the queue */
  3617. if (ena)
  3618. rx_reg |= QRX_CTRL_QENA_REQ_M;
  3619. else
  3620. rx_reg &= ~QRX_CTRL_QENA_REQ_M;
  3621. wr32(hw, QRX_CTRL(pf_q), rx_reg);
  3622. /* wait for the change to finish */
  3623. ret = ice_pf_rxq_wait(pf, pf_q, ena);
  3624. if (ret) {
  3625. dev_err(&pf->pdev->dev,
  3626. "VSI idx %d Rx ring %d %sable timeout\n",
  3627. vsi->idx, pf_q, (ena ? "en" : "dis"));
  3628. break;
  3629. }
  3630. }
  3631. return ret;
  3632. }
  3633. /**
  3634. * ice_vsi_start_rx_rings - start VSI's rx rings
  3635. * @vsi: the VSI whose rings are to be started
  3636. *
  3637. * Returns 0 on success and a negative value on error
  3638. */
  3639. static int ice_vsi_start_rx_rings(struct ice_vsi *vsi)
  3640. {
  3641. return ice_vsi_ctrl_rx_rings(vsi, true);
  3642. }
  3643. /**
  3644. * ice_vsi_stop_rx_rings - stop VSI's rx rings
  3645. * @vsi: the VSI
  3646. *
  3647. * Returns 0 on success and a negative value on error
  3648. */
  3649. static int ice_vsi_stop_rx_rings(struct ice_vsi *vsi)
  3650. {
  3651. return ice_vsi_ctrl_rx_rings(vsi, false);
  3652. }
  3653. /**
  3654. * ice_vsi_stop_tx_rx_rings - stop VSI's tx and rx rings
  3655. * @vsi: the VSI
  3656. * Returns 0 on success and a negative value on error
  3657. */
  3658. static int ice_vsi_stop_tx_rx_rings(struct ice_vsi *vsi)
  3659. {
  3660. int err_tx, err_rx;
  3661. err_tx = ice_vsi_stop_tx_rings(vsi);
  3662. if (err_tx)
  3663. dev_dbg(&vsi->back->pdev->dev, "Failed to disable Tx rings\n");
  3664. err_rx = ice_vsi_stop_rx_rings(vsi);
  3665. if (err_rx)
  3666. dev_dbg(&vsi->back->pdev->dev, "Failed to disable Rx rings\n");
  3667. if (err_tx || err_rx)
  3668. return -EIO;
  3669. return 0;
  3670. }
  3671. /**
  3672. * ice_napi_enable_all - Enable NAPI for all q_vectors in the VSI
  3673. * @vsi: the VSI being configured
  3674. */
  3675. static void ice_napi_enable_all(struct ice_vsi *vsi)
  3676. {
  3677. int q_idx;
  3678. if (!vsi->netdev)
  3679. return;
  3680. for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++) {
  3681. struct ice_q_vector *q_vector = vsi->q_vectors[q_idx];
  3682. if (q_vector->rx.ring || q_vector->tx.ring)
  3683. napi_enable(&q_vector->napi);
  3684. }
  3685. }
  3686. /**
  3687. * ice_up_complete - Finish the last steps of bringing up a connection
  3688. * @vsi: The VSI being configured
  3689. *
  3690. * Return 0 on success and negative value on error
  3691. */
  3692. static int ice_up_complete(struct ice_vsi *vsi)
  3693. {
  3694. struct ice_pf *pf = vsi->back;
  3695. int err;
  3696. if (test_bit(ICE_FLAG_MSIX_ENA, pf->flags))
  3697. ice_vsi_cfg_msix(vsi);
  3698. else
  3699. return -ENOTSUPP;
  3700. /* Enable only Rx rings, Tx rings were enabled by the FW when the
  3701. * Tx queue group list was configured and the context bits were
  3702. * programmed using ice_vsi_cfg_txqs
  3703. */
  3704. err = ice_vsi_start_rx_rings(vsi);
  3705. if (err)
  3706. return err;
  3707. clear_bit(__ICE_DOWN, vsi->state);
  3708. ice_napi_enable_all(vsi);
  3709. ice_vsi_ena_irq(vsi);
  3710. if (vsi->port_info &&
  3711. (vsi->port_info->phy.link_info.link_info & ICE_AQ_LINK_UP) &&
  3712. vsi->netdev) {
  3713. ice_print_link_msg(vsi, true);
  3714. netif_tx_start_all_queues(vsi->netdev);
  3715. netif_carrier_on(vsi->netdev);
  3716. }
  3717. ice_service_task_schedule(pf);
  3718. return err;
  3719. }
  3720. /**
  3721. * ice_up - Bring the connection back up after being down
  3722. * @vsi: VSI being configured
  3723. */
  3724. int ice_up(struct ice_vsi *vsi)
  3725. {
  3726. int err;
  3727. err = ice_vsi_cfg(vsi);
  3728. if (!err)
  3729. err = ice_up_complete(vsi);
  3730. return err;
  3731. }
  3732. /**
  3733. * ice_fetch_u64_stats_per_ring - get packets and bytes stats per ring
  3734. * @ring: Tx or Rx ring to read stats from
  3735. * @pkts: packets stats counter
  3736. * @bytes: bytes stats counter
  3737. *
  3738. * This function fetches stats from the ring considering the atomic operations
  3739. * that needs to be performed to read u64 values in 32 bit machine.
  3740. */
  3741. static void ice_fetch_u64_stats_per_ring(struct ice_ring *ring, u64 *pkts,
  3742. u64 *bytes)
  3743. {
  3744. unsigned int start;
  3745. *pkts = 0;
  3746. *bytes = 0;
  3747. if (!ring)
  3748. return;
  3749. do {
  3750. start = u64_stats_fetch_begin_irq(&ring->syncp);
  3751. *pkts = ring->stats.pkts;
  3752. *bytes = ring->stats.bytes;
  3753. } while (u64_stats_fetch_retry_irq(&ring->syncp, start));
  3754. }
  3755. /**
  3756. * ice_stat_update40 - read 40 bit stat from the chip and update stat values
  3757. * @hw: ptr to the hardware info
  3758. * @hireg: high 32 bit HW register to read from
  3759. * @loreg: low 32 bit HW register to read from
  3760. * @prev_stat_loaded: bool to specify if previous stats are loaded
  3761. * @prev_stat: ptr to previous loaded stat value
  3762. * @cur_stat: ptr to current stat value
  3763. */
  3764. static void ice_stat_update40(struct ice_hw *hw, u32 hireg, u32 loreg,
  3765. bool prev_stat_loaded, u64 *prev_stat,
  3766. u64 *cur_stat)
  3767. {
  3768. u64 new_data;
  3769. new_data = rd32(hw, loreg);
  3770. new_data |= ((u64)(rd32(hw, hireg) & 0xFFFF)) << 32;
  3771. /* device stats are not reset at PFR, they likely will not be zeroed
  3772. * when the driver starts. So save the first values read and use them as
  3773. * offsets to be subtracted from the raw values in order to report stats
  3774. * that count from zero.
  3775. */
  3776. if (!prev_stat_loaded)
  3777. *prev_stat = new_data;
  3778. if (likely(new_data >= *prev_stat))
  3779. *cur_stat = new_data - *prev_stat;
  3780. else
  3781. /* to manage the potential roll-over */
  3782. *cur_stat = (new_data + BIT_ULL(40)) - *prev_stat;
  3783. *cur_stat &= 0xFFFFFFFFFFULL;
  3784. }
  3785. /**
  3786. * ice_stat_update32 - read 32 bit stat from the chip and update stat values
  3787. * @hw: ptr to the hardware info
  3788. * @reg: HW register to read from
  3789. * @prev_stat_loaded: bool to specify if previous stats are loaded
  3790. * @prev_stat: ptr to previous loaded stat value
  3791. * @cur_stat: ptr to current stat value
  3792. */
  3793. static void ice_stat_update32(struct ice_hw *hw, u32 reg, bool prev_stat_loaded,
  3794. u64 *prev_stat, u64 *cur_stat)
  3795. {
  3796. u32 new_data;
  3797. new_data = rd32(hw, reg);
  3798. /* device stats are not reset at PFR, they likely will not be zeroed
  3799. * when the driver starts. So save the first values read and use them as
  3800. * offsets to be subtracted from the raw values in order to report stats
  3801. * that count from zero.
  3802. */
  3803. if (!prev_stat_loaded)
  3804. *prev_stat = new_data;
  3805. if (likely(new_data >= *prev_stat))
  3806. *cur_stat = new_data - *prev_stat;
  3807. else
  3808. /* to manage the potential roll-over */
  3809. *cur_stat = (new_data + BIT_ULL(32)) - *prev_stat;
  3810. }
  3811. /**
  3812. * ice_update_eth_stats - Update VSI-specific ethernet statistics counters
  3813. * @vsi: the VSI to be updated
  3814. */
  3815. static void ice_update_eth_stats(struct ice_vsi *vsi)
  3816. {
  3817. struct ice_eth_stats *prev_es, *cur_es;
  3818. struct ice_hw *hw = &vsi->back->hw;
  3819. u16 vsi_num = vsi->vsi_num; /* HW absolute index of a VSI */
  3820. prev_es = &vsi->eth_stats_prev;
  3821. cur_es = &vsi->eth_stats;
  3822. ice_stat_update40(hw, GLV_GORCH(vsi_num), GLV_GORCL(vsi_num),
  3823. vsi->stat_offsets_loaded, &prev_es->rx_bytes,
  3824. &cur_es->rx_bytes);
  3825. ice_stat_update40(hw, GLV_UPRCH(vsi_num), GLV_UPRCL(vsi_num),
  3826. vsi->stat_offsets_loaded, &prev_es->rx_unicast,
  3827. &cur_es->rx_unicast);
  3828. ice_stat_update40(hw, GLV_MPRCH(vsi_num), GLV_MPRCL(vsi_num),
  3829. vsi->stat_offsets_loaded, &prev_es->rx_multicast,
  3830. &cur_es->rx_multicast);
  3831. ice_stat_update40(hw, GLV_BPRCH(vsi_num), GLV_BPRCL(vsi_num),
  3832. vsi->stat_offsets_loaded, &prev_es->rx_broadcast,
  3833. &cur_es->rx_broadcast);
  3834. ice_stat_update32(hw, GLV_RDPC(vsi_num), vsi->stat_offsets_loaded,
  3835. &prev_es->rx_discards, &cur_es->rx_discards);
  3836. ice_stat_update40(hw, GLV_GOTCH(vsi_num), GLV_GOTCL(vsi_num),
  3837. vsi->stat_offsets_loaded, &prev_es->tx_bytes,
  3838. &cur_es->tx_bytes);
  3839. ice_stat_update40(hw, GLV_UPTCH(vsi_num), GLV_UPTCL(vsi_num),
  3840. vsi->stat_offsets_loaded, &prev_es->tx_unicast,
  3841. &cur_es->tx_unicast);
  3842. ice_stat_update40(hw, GLV_MPTCH(vsi_num), GLV_MPTCL(vsi_num),
  3843. vsi->stat_offsets_loaded, &prev_es->tx_multicast,
  3844. &cur_es->tx_multicast);
  3845. ice_stat_update40(hw, GLV_BPTCH(vsi_num), GLV_BPTCL(vsi_num),
  3846. vsi->stat_offsets_loaded, &prev_es->tx_broadcast,
  3847. &cur_es->tx_broadcast);
  3848. ice_stat_update32(hw, GLV_TEPC(vsi_num), vsi->stat_offsets_loaded,
  3849. &prev_es->tx_errors, &cur_es->tx_errors);
  3850. vsi->stat_offsets_loaded = true;
  3851. }
  3852. /**
  3853. * ice_update_vsi_ring_stats - Update VSI stats counters
  3854. * @vsi: the VSI to be updated
  3855. */
  3856. static void ice_update_vsi_ring_stats(struct ice_vsi *vsi)
  3857. {
  3858. struct rtnl_link_stats64 *vsi_stats = &vsi->net_stats;
  3859. struct ice_ring *ring;
  3860. u64 pkts, bytes;
  3861. int i;
  3862. /* reset netdev stats */
  3863. vsi_stats->tx_packets = 0;
  3864. vsi_stats->tx_bytes = 0;
  3865. vsi_stats->rx_packets = 0;
  3866. vsi_stats->rx_bytes = 0;
  3867. /* reset non-netdev (extended) stats */
  3868. vsi->tx_restart = 0;
  3869. vsi->tx_busy = 0;
  3870. vsi->tx_linearize = 0;
  3871. vsi->rx_buf_failed = 0;
  3872. vsi->rx_page_failed = 0;
  3873. rcu_read_lock();
  3874. /* update Tx rings counters */
  3875. ice_for_each_txq(vsi, i) {
  3876. ring = READ_ONCE(vsi->tx_rings[i]);
  3877. ice_fetch_u64_stats_per_ring(ring, &pkts, &bytes);
  3878. vsi_stats->tx_packets += pkts;
  3879. vsi_stats->tx_bytes += bytes;
  3880. vsi->tx_restart += ring->tx_stats.restart_q;
  3881. vsi->tx_busy += ring->tx_stats.tx_busy;
  3882. vsi->tx_linearize += ring->tx_stats.tx_linearize;
  3883. }
  3884. /* update Rx rings counters */
  3885. ice_for_each_rxq(vsi, i) {
  3886. ring = READ_ONCE(vsi->rx_rings[i]);
  3887. ice_fetch_u64_stats_per_ring(ring, &pkts, &bytes);
  3888. vsi_stats->rx_packets += pkts;
  3889. vsi_stats->rx_bytes += bytes;
  3890. vsi->rx_buf_failed += ring->rx_stats.alloc_buf_failed;
  3891. vsi->rx_page_failed += ring->rx_stats.alloc_page_failed;
  3892. }
  3893. rcu_read_unlock();
  3894. }
  3895. /**
  3896. * ice_update_vsi_stats - Update VSI stats counters
  3897. * @vsi: the VSI to be updated
  3898. */
  3899. static void ice_update_vsi_stats(struct ice_vsi *vsi)
  3900. {
  3901. struct rtnl_link_stats64 *cur_ns = &vsi->net_stats;
  3902. struct ice_eth_stats *cur_es = &vsi->eth_stats;
  3903. struct ice_pf *pf = vsi->back;
  3904. if (test_bit(__ICE_DOWN, vsi->state) ||
  3905. test_bit(__ICE_CFG_BUSY, pf->state))
  3906. return;
  3907. /* get stats as recorded by Tx/Rx rings */
  3908. ice_update_vsi_ring_stats(vsi);
  3909. /* get VSI stats as recorded by the hardware */
  3910. ice_update_eth_stats(vsi);
  3911. cur_ns->tx_errors = cur_es->tx_errors;
  3912. cur_ns->rx_dropped = cur_es->rx_discards;
  3913. cur_ns->tx_dropped = cur_es->tx_discards;
  3914. cur_ns->multicast = cur_es->rx_multicast;
  3915. /* update some more netdev stats if this is main VSI */
  3916. if (vsi->type == ICE_VSI_PF) {
  3917. cur_ns->rx_crc_errors = pf->stats.crc_errors;
  3918. cur_ns->rx_errors = pf->stats.crc_errors +
  3919. pf->stats.illegal_bytes;
  3920. cur_ns->rx_length_errors = pf->stats.rx_len_errors;
  3921. }
  3922. }
  3923. /**
  3924. * ice_update_pf_stats - Update PF port stats counters
  3925. * @pf: PF whose stats needs to be updated
  3926. */
  3927. static void ice_update_pf_stats(struct ice_pf *pf)
  3928. {
  3929. struct ice_hw_port_stats *prev_ps, *cur_ps;
  3930. struct ice_hw *hw = &pf->hw;
  3931. u8 pf_id;
  3932. prev_ps = &pf->stats_prev;
  3933. cur_ps = &pf->stats;
  3934. pf_id = hw->pf_id;
  3935. ice_stat_update40(hw, GLPRT_GORCH(pf_id), GLPRT_GORCL(pf_id),
  3936. pf->stat_prev_loaded, &prev_ps->eth.rx_bytes,
  3937. &cur_ps->eth.rx_bytes);
  3938. ice_stat_update40(hw, GLPRT_UPRCH(pf_id), GLPRT_UPRCL(pf_id),
  3939. pf->stat_prev_loaded, &prev_ps->eth.rx_unicast,
  3940. &cur_ps->eth.rx_unicast);
  3941. ice_stat_update40(hw, GLPRT_MPRCH(pf_id), GLPRT_MPRCL(pf_id),
  3942. pf->stat_prev_loaded, &prev_ps->eth.rx_multicast,
  3943. &cur_ps->eth.rx_multicast);
  3944. ice_stat_update40(hw, GLPRT_BPRCH(pf_id), GLPRT_BPRCL(pf_id),
  3945. pf->stat_prev_loaded, &prev_ps->eth.rx_broadcast,
  3946. &cur_ps->eth.rx_broadcast);
  3947. ice_stat_update40(hw, GLPRT_GOTCH(pf_id), GLPRT_GOTCL(pf_id),
  3948. pf->stat_prev_loaded, &prev_ps->eth.tx_bytes,
  3949. &cur_ps->eth.tx_bytes);
  3950. ice_stat_update40(hw, GLPRT_UPTCH(pf_id), GLPRT_UPTCL(pf_id),
  3951. pf->stat_prev_loaded, &prev_ps->eth.tx_unicast,
  3952. &cur_ps->eth.tx_unicast);
  3953. ice_stat_update40(hw, GLPRT_MPTCH(pf_id), GLPRT_MPTCL(pf_id),
  3954. pf->stat_prev_loaded, &prev_ps->eth.tx_multicast,
  3955. &cur_ps->eth.tx_multicast);
  3956. ice_stat_update40(hw, GLPRT_BPTCH(pf_id), GLPRT_BPTCL(pf_id),
  3957. pf->stat_prev_loaded, &prev_ps->eth.tx_broadcast,
  3958. &cur_ps->eth.tx_broadcast);
  3959. ice_stat_update32(hw, GLPRT_TDOLD(pf_id), pf->stat_prev_loaded,
  3960. &prev_ps->tx_dropped_link_down,
  3961. &cur_ps->tx_dropped_link_down);
  3962. ice_stat_update40(hw, GLPRT_PRC64H(pf_id), GLPRT_PRC64L(pf_id),
  3963. pf->stat_prev_loaded, &prev_ps->rx_size_64,
  3964. &cur_ps->rx_size_64);
  3965. ice_stat_update40(hw, GLPRT_PRC127H(pf_id), GLPRT_PRC127L(pf_id),
  3966. pf->stat_prev_loaded, &prev_ps->rx_size_127,
  3967. &cur_ps->rx_size_127);
  3968. ice_stat_update40(hw, GLPRT_PRC255H(pf_id), GLPRT_PRC255L(pf_id),
  3969. pf->stat_prev_loaded, &prev_ps->rx_size_255,
  3970. &cur_ps->rx_size_255);
  3971. ice_stat_update40(hw, GLPRT_PRC511H(pf_id), GLPRT_PRC511L(pf_id),
  3972. pf->stat_prev_loaded, &prev_ps->rx_size_511,
  3973. &cur_ps->rx_size_511);
  3974. ice_stat_update40(hw, GLPRT_PRC1023H(pf_id),
  3975. GLPRT_PRC1023L(pf_id), pf->stat_prev_loaded,
  3976. &prev_ps->rx_size_1023, &cur_ps->rx_size_1023);
  3977. ice_stat_update40(hw, GLPRT_PRC1522H(pf_id),
  3978. GLPRT_PRC1522L(pf_id), pf->stat_prev_loaded,
  3979. &prev_ps->rx_size_1522, &cur_ps->rx_size_1522);
  3980. ice_stat_update40(hw, GLPRT_PRC9522H(pf_id),
  3981. GLPRT_PRC9522L(pf_id), pf->stat_prev_loaded,
  3982. &prev_ps->rx_size_big, &cur_ps->rx_size_big);
  3983. ice_stat_update40(hw, GLPRT_PTC64H(pf_id), GLPRT_PTC64L(pf_id),
  3984. pf->stat_prev_loaded, &prev_ps->tx_size_64,
  3985. &cur_ps->tx_size_64);
  3986. ice_stat_update40(hw, GLPRT_PTC127H(pf_id), GLPRT_PTC127L(pf_id),
  3987. pf->stat_prev_loaded, &prev_ps->tx_size_127,
  3988. &cur_ps->tx_size_127);
  3989. ice_stat_update40(hw, GLPRT_PTC255H(pf_id), GLPRT_PTC255L(pf_id),
  3990. pf->stat_prev_loaded, &prev_ps->tx_size_255,
  3991. &cur_ps->tx_size_255);
  3992. ice_stat_update40(hw, GLPRT_PTC511H(pf_id), GLPRT_PTC511L(pf_id),
  3993. pf->stat_prev_loaded, &prev_ps->tx_size_511,
  3994. &cur_ps->tx_size_511);
  3995. ice_stat_update40(hw, GLPRT_PTC1023H(pf_id),
  3996. GLPRT_PTC1023L(pf_id), pf->stat_prev_loaded,
  3997. &prev_ps->tx_size_1023, &cur_ps->tx_size_1023);
  3998. ice_stat_update40(hw, GLPRT_PTC1522H(pf_id),
  3999. GLPRT_PTC1522L(pf_id), pf->stat_prev_loaded,
  4000. &prev_ps->tx_size_1522, &cur_ps->tx_size_1522);
  4001. ice_stat_update40(hw, GLPRT_PTC9522H(pf_id),
  4002. GLPRT_PTC9522L(pf_id), pf->stat_prev_loaded,
  4003. &prev_ps->tx_size_big, &cur_ps->tx_size_big);
  4004. ice_stat_update32(hw, GLPRT_LXONRXC(pf_id), pf->stat_prev_loaded,
  4005. &prev_ps->link_xon_rx, &cur_ps->link_xon_rx);
  4006. ice_stat_update32(hw, GLPRT_LXOFFRXC(pf_id), pf->stat_prev_loaded,
  4007. &prev_ps->link_xoff_rx, &cur_ps->link_xoff_rx);
  4008. ice_stat_update32(hw, GLPRT_LXONTXC(pf_id), pf->stat_prev_loaded,
  4009. &prev_ps->link_xon_tx, &cur_ps->link_xon_tx);
  4010. ice_stat_update32(hw, GLPRT_LXOFFTXC(pf_id), pf->stat_prev_loaded,
  4011. &prev_ps->link_xoff_tx, &cur_ps->link_xoff_tx);
  4012. ice_stat_update32(hw, GLPRT_CRCERRS(pf_id), pf->stat_prev_loaded,
  4013. &prev_ps->crc_errors, &cur_ps->crc_errors);
  4014. ice_stat_update32(hw, GLPRT_ILLERRC(pf_id), pf->stat_prev_loaded,
  4015. &prev_ps->illegal_bytes, &cur_ps->illegal_bytes);
  4016. ice_stat_update32(hw, GLPRT_MLFC(pf_id), pf->stat_prev_loaded,
  4017. &prev_ps->mac_local_faults,
  4018. &cur_ps->mac_local_faults);
  4019. ice_stat_update32(hw, GLPRT_MRFC(pf_id), pf->stat_prev_loaded,
  4020. &prev_ps->mac_remote_faults,
  4021. &cur_ps->mac_remote_faults);
  4022. ice_stat_update32(hw, GLPRT_RLEC(pf_id), pf->stat_prev_loaded,
  4023. &prev_ps->rx_len_errors, &cur_ps->rx_len_errors);
  4024. ice_stat_update32(hw, GLPRT_RUC(pf_id), pf->stat_prev_loaded,
  4025. &prev_ps->rx_undersize, &cur_ps->rx_undersize);
  4026. ice_stat_update32(hw, GLPRT_RFC(pf_id), pf->stat_prev_loaded,
  4027. &prev_ps->rx_fragments, &cur_ps->rx_fragments);
  4028. ice_stat_update32(hw, GLPRT_ROC(pf_id), pf->stat_prev_loaded,
  4029. &prev_ps->rx_oversize, &cur_ps->rx_oversize);
  4030. ice_stat_update32(hw, GLPRT_RJC(pf_id), pf->stat_prev_loaded,
  4031. &prev_ps->rx_jabber, &cur_ps->rx_jabber);
  4032. pf->stat_prev_loaded = true;
  4033. }
  4034. /**
  4035. * ice_get_stats64 - get statistics for network device structure
  4036. * @netdev: network interface device structure
  4037. * @stats: main device statistics structure
  4038. */
  4039. static
  4040. void ice_get_stats64(struct net_device *netdev, struct rtnl_link_stats64 *stats)
  4041. {
  4042. struct ice_netdev_priv *np = netdev_priv(netdev);
  4043. struct rtnl_link_stats64 *vsi_stats;
  4044. struct ice_vsi *vsi = np->vsi;
  4045. vsi_stats = &vsi->net_stats;
  4046. if (test_bit(__ICE_DOWN, vsi->state) || !vsi->num_txq || !vsi->num_rxq)
  4047. return;
  4048. /* netdev packet/byte stats come from ring counter. These are obtained
  4049. * by summing up ring counters (done by ice_update_vsi_ring_stats).
  4050. */
  4051. ice_update_vsi_ring_stats(vsi);
  4052. stats->tx_packets = vsi_stats->tx_packets;
  4053. stats->tx_bytes = vsi_stats->tx_bytes;
  4054. stats->rx_packets = vsi_stats->rx_packets;
  4055. stats->rx_bytes = vsi_stats->rx_bytes;
  4056. /* The rest of the stats can be read from the hardware but instead we
  4057. * just return values that the watchdog task has already obtained from
  4058. * the hardware.
  4059. */
  4060. stats->multicast = vsi_stats->multicast;
  4061. stats->tx_errors = vsi_stats->tx_errors;
  4062. stats->tx_dropped = vsi_stats->tx_dropped;
  4063. stats->rx_errors = vsi_stats->rx_errors;
  4064. stats->rx_dropped = vsi_stats->rx_dropped;
  4065. stats->rx_crc_errors = vsi_stats->rx_crc_errors;
  4066. stats->rx_length_errors = vsi_stats->rx_length_errors;
  4067. }
  4068. /**
  4069. * ice_napi_disable_all - Disable NAPI for all q_vectors in the VSI
  4070. * @vsi: VSI having NAPI disabled
  4071. */
  4072. static void ice_napi_disable_all(struct ice_vsi *vsi)
  4073. {
  4074. int q_idx;
  4075. if (!vsi->netdev)
  4076. return;
  4077. for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++) {
  4078. struct ice_q_vector *q_vector = vsi->q_vectors[q_idx];
  4079. if (q_vector->rx.ring || q_vector->tx.ring)
  4080. napi_disable(&q_vector->napi);
  4081. }
  4082. }
  4083. /**
  4084. * ice_down - Shutdown the connection
  4085. * @vsi: The VSI being stopped
  4086. */
  4087. int ice_down(struct ice_vsi *vsi)
  4088. {
  4089. int i, err;
  4090. /* Caller of this function is expected to set the
  4091. * vsi->state __ICE_DOWN bit
  4092. */
  4093. if (vsi->netdev) {
  4094. netif_carrier_off(vsi->netdev);
  4095. netif_tx_disable(vsi->netdev);
  4096. }
  4097. ice_vsi_dis_irq(vsi);
  4098. err = ice_vsi_stop_tx_rx_rings(vsi);
  4099. ice_napi_disable_all(vsi);
  4100. ice_for_each_txq(vsi, i)
  4101. ice_clean_tx_ring(vsi->tx_rings[i]);
  4102. ice_for_each_rxq(vsi, i)
  4103. ice_clean_rx_ring(vsi->rx_rings[i]);
  4104. if (err)
  4105. netdev_err(vsi->netdev, "Failed to close VSI 0x%04X on switch 0x%04X\n",
  4106. vsi->vsi_num, vsi->vsw->sw_id);
  4107. return err;
  4108. }
  4109. /**
  4110. * ice_vsi_setup_tx_rings - Allocate VSI Tx queue resources
  4111. * @vsi: VSI having resources allocated
  4112. *
  4113. * Return 0 on success, negative on failure
  4114. */
  4115. static int ice_vsi_setup_tx_rings(struct ice_vsi *vsi)
  4116. {
  4117. int i, err = 0;
  4118. if (!vsi->num_txq) {
  4119. dev_err(&vsi->back->pdev->dev, "VSI %d has 0 Tx queues\n",
  4120. vsi->vsi_num);
  4121. return -EINVAL;
  4122. }
  4123. ice_for_each_txq(vsi, i) {
  4124. err = ice_setup_tx_ring(vsi->tx_rings[i]);
  4125. if (err)
  4126. break;
  4127. }
  4128. return err;
  4129. }
  4130. /**
  4131. * ice_vsi_setup_rx_rings - Allocate VSI Rx queue resources
  4132. * @vsi: VSI having resources allocated
  4133. *
  4134. * Return 0 on success, negative on failure
  4135. */
  4136. static int ice_vsi_setup_rx_rings(struct ice_vsi *vsi)
  4137. {
  4138. int i, err = 0;
  4139. if (!vsi->num_rxq) {
  4140. dev_err(&vsi->back->pdev->dev, "VSI %d has 0 Rx queues\n",
  4141. vsi->vsi_num);
  4142. return -EINVAL;
  4143. }
  4144. ice_for_each_rxq(vsi, i) {
  4145. err = ice_setup_rx_ring(vsi->rx_rings[i]);
  4146. if (err)
  4147. break;
  4148. }
  4149. return err;
  4150. }
  4151. /**
  4152. * ice_vsi_req_irq - Request IRQ from the OS
  4153. * @vsi: The VSI IRQ is being requested for
  4154. * @basename: name for the vector
  4155. *
  4156. * Return 0 on success and a negative value on error
  4157. */
  4158. static int ice_vsi_req_irq(struct ice_vsi *vsi, char *basename)
  4159. {
  4160. struct ice_pf *pf = vsi->back;
  4161. int err = -EINVAL;
  4162. if (test_bit(ICE_FLAG_MSIX_ENA, pf->flags))
  4163. err = ice_vsi_req_irq_msix(vsi, basename);
  4164. return err;
  4165. }
  4166. /**
  4167. * ice_vsi_free_tx_rings - Free Tx resources for VSI queues
  4168. * @vsi: the VSI having resources freed
  4169. */
  4170. static void ice_vsi_free_tx_rings(struct ice_vsi *vsi)
  4171. {
  4172. int i;
  4173. if (!vsi->tx_rings)
  4174. return;
  4175. ice_for_each_txq(vsi, i)
  4176. if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
  4177. ice_free_tx_ring(vsi->tx_rings[i]);
  4178. }
  4179. /**
  4180. * ice_vsi_free_rx_rings - Free Rx resources for VSI queues
  4181. * @vsi: the VSI having resources freed
  4182. */
  4183. static void ice_vsi_free_rx_rings(struct ice_vsi *vsi)
  4184. {
  4185. int i;
  4186. if (!vsi->rx_rings)
  4187. return;
  4188. ice_for_each_rxq(vsi, i)
  4189. if (vsi->rx_rings[i] && vsi->rx_rings[i]->desc)
  4190. ice_free_rx_ring(vsi->rx_rings[i]);
  4191. }
  4192. /**
  4193. * ice_vsi_open - Called when a network interface is made active
  4194. * @vsi: the VSI to open
  4195. *
  4196. * Initialization of the VSI
  4197. *
  4198. * Returns 0 on success, negative value on error
  4199. */
  4200. static int ice_vsi_open(struct ice_vsi *vsi)
  4201. {
  4202. char int_name[ICE_INT_NAME_STR_LEN];
  4203. struct ice_pf *pf = vsi->back;
  4204. int err;
  4205. /* allocate descriptors */
  4206. err = ice_vsi_setup_tx_rings(vsi);
  4207. if (err)
  4208. goto err_setup_tx;
  4209. err = ice_vsi_setup_rx_rings(vsi);
  4210. if (err)
  4211. goto err_setup_rx;
  4212. err = ice_vsi_cfg(vsi);
  4213. if (err)
  4214. goto err_setup_rx;
  4215. snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
  4216. dev_driver_string(&pf->pdev->dev), vsi->netdev->name);
  4217. err = ice_vsi_req_irq(vsi, int_name);
  4218. if (err)
  4219. goto err_setup_rx;
  4220. /* Notify the stack of the actual queue counts. */
  4221. err = netif_set_real_num_tx_queues(vsi->netdev, vsi->num_txq);
  4222. if (err)
  4223. goto err_set_qs;
  4224. err = netif_set_real_num_rx_queues(vsi->netdev, vsi->num_rxq);
  4225. if (err)
  4226. goto err_set_qs;
  4227. err = ice_up_complete(vsi);
  4228. if (err)
  4229. goto err_up_complete;
  4230. return 0;
  4231. err_up_complete:
  4232. ice_down(vsi);
  4233. err_set_qs:
  4234. ice_vsi_free_irq(vsi);
  4235. err_setup_rx:
  4236. ice_vsi_free_rx_rings(vsi);
  4237. err_setup_tx:
  4238. ice_vsi_free_tx_rings(vsi);
  4239. return err;
  4240. }
  4241. /**
  4242. * ice_vsi_close - Shut down a VSI
  4243. * @vsi: the VSI being shut down
  4244. */
  4245. static void ice_vsi_close(struct ice_vsi *vsi)
  4246. {
  4247. if (!test_and_set_bit(__ICE_DOWN, vsi->state))
  4248. ice_down(vsi);
  4249. ice_vsi_free_irq(vsi);
  4250. ice_vsi_free_tx_rings(vsi);
  4251. ice_vsi_free_rx_rings(vsi);
  4252. }
  4253. /**
  4254. * ice_rss_clean - Delete RSS related VSI structures that hold user inputs
  4255. * @vsi: the VSI being removed
  4256. */
  4257. static void ice_rss_clean(struct ice_vsi *vsi)
  4258. {
  4259. struct ice_pf *pf;
  4260. pf = vsi->back;
  4261. if (vsi->rss_hkey_user)
  4262. devm_kfree(&pf->pdev->dev, vsi->rss_hkey_user);
  4263. if (vsi->rss_lut_user)
  4264. devm_kfree(&pf->pdev->dev, vsi->rss_lut_user);
  4265. }
  4266. /**
  4267. * ice_vsi_release - Delete a VSI and free its resources
  4268. * @vsi: the VSI being removed
  4269. *
  4270. * Returns 0 on success or < 0 on error
  4271. */
  4272. static int ice_vsi_release(struct ice_vsi *vsi)
  4273. {
  4274. struct ice_pf *pf;
  4275. if (!vsi->back)
  4276. return -ENODEV;
  4277. pf = vsi->back;
  4278. if (vsi->netdev) {
  4279. unregister_netdev(vsi->netdev);
  4280. free_netdev(vsi->netdev);
  4281. vsi->netdev = NULL;
  4282. }
  4283. if (test_bit(ICE_FLAG_RSS_ENA, pf->flags))
  4284. ice_rss_clean(vsi);
  4285. /* Disable VSI and free resources */
  4286. ice_vsi_dis_irq(vsi);
  4287. ice_vsi_close(vsi);
  4288. /* reclaim interrupt vectors back to PF */
  4289. ice_free_res(vsi->back->irq_tracker, vsi->base_vector, vsi->idx);
  4290. pf->num_avail_msix += vsi->num_q_vectors;
  4291. ice_remove_vsi_fltr(&pf->hw, vsi->vsi_num);
  4292. ice_vsi_delete(vsi);
  4293. ice_vsi_free_q_vectors(vsi);
  4294. ice_vsi_clear_rings(vsi);
  4295. ice_vsi_put_qs(vsi);
  4296. pf->q_left_tx += vsi->alloc_txq;
  4297. pf->q_left_rx += vsi->alloc_rxq;
  4298. ice_vsi_clear(vsi);
  4299. return 0;
  4300. }
  4301. /**
  4302. * ice_dis_vsi - pause a VSI
  4303. * @vsi: the VSI being paused
  4304. */
  4305. static void ice_dis_vsi(struct ice_vsi *vsi)
  4306. {
  4307. if (test_bit(__ICE_DOWN, vsi->state))
  4308. return;
  4309. set_bit(__ICE_NEEDS_RESTART, vsi->state);
  4310. if (vsi->netdev && netif_running(vsi->netdev) &&
  4311. vsi->type == ICE_VSI_PF)
  4312. vsi->netdev->netdev_ops->ndo_stop(vsi->netdev);
  4313. ice_vsi_close(vsi);
  4314. }
  4315. /**
  4316. * ice_ena_vsi - resume a VSI
  4317. * @vsi: the VSI being resume
  4318. */
  4319. static void ice_ena_vsi(struct ice_vsi *vsi)
  4320. {
  4321. if (!test_and_clear_bit(__ICE_NEEDS_RESTART, vsi->state))
  4322. return;
  4323. if (vsi->netdev && netif_running(vsi->netdev))
  4324. vsi->netdev->netdev_ops->ndo_open(vsi->netdev);
  4325. else if (ice_vsi_open(vsi))
  4326. /* this clears the DOWN bit */
  4327. dev_dbg(&vsi->back->pdev->dev, "Failed open VSI 0x%04X on switch 0x%04X\n",
  4328. vsi->vsi_num, vsi->vsw->sw_id);
  4329. }
  4330. /**
  4331. * ice_pf_dis_all_vsi - Pause all VSIs on a PF
  4332. * @pf: the PF
  4333. */
  4334. static void ice_pf_dis_all_vsi(struct ice_pf *pf)
  4335. {
  4336. int v;
  4337. ice_for_each_vsi(pf, v)
  4338. if (pf->vsi[v])
  4339. ice_dis_vsi(pf->vsi[v]);
  4340. }
  4341. /**
  4342. * ice_pf_ena_all_vsi - Resume all VSIs on a PF
  4343. * @pf: the PF
  4344. */
  4345. static void ice_pf_ena_all_vsi(struct ice_pf *pf)
  4346. {
  4347. int v;
  4348. ice_for_each_vsi(pf, v)
  4349. if (pf->vsi[v])
  4350. ice_ena_vsi(pf->vsi[v]);
  4351. }
  4352. /**
  4353. * ice_rebuild - rebuild after reset
  4354. * @pf: pf to rebuild
  4355. */
  4356. static void ice_rebuild(struct ice_pf *pf)
  4357. {
  4358. struct device *dev = &pf->pdev->dev;
  4359. struct ice_hw *hw = &pf->hw;
  4360. enum ice_status ret;
  4361. int err;
  4362. if (test_bit(__ICE_DOWN, pf->state))
  4363. goto clear_recovery;
  4364. dev_dbg(dev, "rebuilding pf\n");
  4365. ret = ice_init_all_ctrlq(hw);
  4366. if (ret) {
  4367. dev_err(dev, "control queues init failed %d\n", ret);
  4368. goto fail_reset;
  4369. }
  4370. ret = ice_clear_pf_cfg(hw);
  4371. if (ret) {
  4372. dev_err(dev, "clear PF configuration failed %d\n", ret);
  4373. goto fail_reset;
  4374. }
  4375. ice_clear_pxe_mode(hw);
  4376. ret = ice_get_caps(hw);
  4377. if (ret) {
  4378. dev_err(dev, "ice_get_caps failed %d\n", ret);
  4379. goto fail_reset;
  4380. }
  4381. /* basic nic switch setup */
  4382. err = ice_setup_pf_sw(pf);
  4383. if (err) {
  4384. dev_err(dev, "ice_setup_pf_sw failed\n");
  4385. goto fail_reset;
  4386. }
  4387. /* start misc vector */
  4388. if (test_bit(ICE_FLAG_MSIX_ENA, pf->flags)) {
  4389. err = ice_req_irq_msix_misc(pf);
  4390. if (err) {
  4391. dev_err(dev, "misc vector setup failed: %d\n", err);
  4392. goto fail_reset;
  4393. }
  4394. }
  4395. /* restart the VSIs that were rebuilt and running before the reset */
  4396. ice_pf_ena_all_vsi(pf);
  4397. return;
  4398. fail_reset:
  4399. ice_shutdown_all_ctrlq(hw);
  4400. set_bit(__ICE_RESET_FAILED, pf->state);
  4401. clear_recovery:
  4402. set_bit(__ICE_RESET_RECOVERY_PENDING, pf->state);
  4403. }
  4404. /**
  4405. * ice_change_mtu - NDO callback to change the MTU
  4406. * @netdev: network interface device structure
  4407. * @new_mtu: new value for maximum frame size
  4408. *
  4409. * Returns 0 on success, negative on failure
  4410. */
  4411. static int ice_change_mtu(struct net_device *netdev, int new_mtu)
  4412. {
  4413. struct ice_netdev_priv *np = netdev_priv(netdev);
  4414. struct ice_vsi *vsi = np->vsi;
  4415. struct ice_pf *pf = vsi->back;
  4416. u8 count = 0;
  4417. if (new_mtu == netdev->mtu) {
  4418. netdev_warn(netdev, "mtu is already %u\n", netdev->mtu);
  4419. return 0;
  4420. }
  4421. if (new_mtu < netdev->min_mtu) {
  4422. netdev_err(netdev, "new mtu invalid. min_mtu is %d\n",
  4423. netdev->min_mtu);
  4424. return -EINVAL;
  4425. } else if (new_mtu > netdev->max_mtu) {
  4426. netdev_err(netdev, "new mtu invalid. max_mtu is %d\n",
  4427. netdev->min_mtu);
  4428. return -EINVAL;
  4429. }
  4430. /* if a reset is in progress, wait for some time for it to complete */
  4431. do {
  4432. if (ice_is_reset_recovery_pending(pf->state)) {
  4433. count++;
  4434. usleep_range(1000, 2000);
  4435. } else {
  4436. break;
  4437. }
  4438. } while (count < 100);
  4439. if (count == 100) {
  4440. netdev_err(netdev, "can't change mtu. Device is busy\n");
  4441. return -EBUSY;
  4442. }
  4443. netdev->mtu = new_mtu;
  4444. /* if VSI is up, bring it down and then back up */
  4445. if (!test_and_set_bit(__ICE_DOWN, vsi->state)) {
  4446. int err;
  4447. err = ice_down(vsi);
  4448. if (err) {
  4449. netdev_err(netdev, "change mtu if_up err %d\n", err);
  4450. return err;
  4451. }
  4452. err = ice_up(vsi);
  4453. if (err) {
  4454. netdev_err(netdev, "change mtu if_up err %d\n", err);
  4455. return err;
  4456. }
  4457. }
  4458. netdev_dbg(netdev, "changed mtu to %d\n", new_mtu);
  4459. return 0;
  4460. }
  4461. /**
  4462. * ice_set_rss - Set RSS keys and lut
  4463. * @vsi: Pointer to VSI structure
  4464. * @seed: RSS hash seed
  4465. * @lut: Lookup table
  4466. * @lut_size: Lookup table size
  4467. *
  4468. * Returns 0 on success, negative on failure
  4469. */
  4470. int ice_set_rss(struct ice_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
  4471. {
  4472. struct ice_pf *pf = vsi->back;
  4473. struct ice_hw *hw = &pf->hw;
  4474. enum ice_status status;
  4475. if (seed) {
  4476. struct ice_aqc_get_set_rss_keys *buf =
  4477. (struct ice_aqc_get_set_rss_keys *)seed;
  4478. status = ice_aq_set_rss_key(hw, vsi->vsi_num, buf);
  4479. if (status) {
  4480. dev_err(&pf->pdev->dev,
  4481. "Cannot set RSS key, err %d aq_err %d\n",
  4482. status, hw->adminq.rq_last_status);
  4483. return -EIO;
  4484. }
  4485. }
  4486. if (lut) {
  4487. status = ice_aq_set_rss_lut(hw, vsi->vsi_num,
  4488. vsi->rss_lut_type, lut, lut_size);
  4489. if (status) {
  4490. dev_err(&pf->pdev->dev,
  4491. "Cannot set RSS lut, err %d aq_err %d\n",
  4492. status, hw->adminq.rq_last_status);
  4493. return -EIO;
  4494. }
  4495. }
  4496. return 0;
  4497. }
  4498. /**
  4499. * ice_get_rss - Get RSS keys and lut
  4500. * @vsi: Pointer to VSI structure
  4501. * @seed: Buffer to store the keys
  4502. * @lut: Buffer to store the lookup table entries
  4503. * @lut_size: Size of buffer to store the lookup table entries
  4504. *
  4505. * Returns 0 on success, negative on failure
  4506. */
  4507. int ice_get_rss(struct ice_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
  4508. {
  4509. struct ice_pf *pf = vsi->back;
  4510. struct ice_hw *hw = &pf->hw;
  4511. enum ice_status status;
  4512. if (seed) {
  4513. struct ice_aqc_get_set_rss_keys *buf =
  4514. (struct ice_aqc_get_set_rss_keys *)seed;
  4515. status = ice_aq_get_rss_key(hw, vsi->vsi_num, buf);
  4516. if (status) {
  4517. dev_err(&pf->pdev->dev,
  4518. "Cannot get RSS key, err %d aq_err %d\n",
  4519. status, hw->adminq.rq_last_status);
  4520. return -EIO;
  4521. }
  4522. }
  4523. if (lut) {
  4524. status = ice_aq_get_rss_lut(hw, vsi->vsi_num,
  4525. vsi->rss_lut_type, lut, lut_size);
  4526. if (status) {
  4527. dev_err(&pf->pdev->dev,
  4528. "Cannot get RSS lut, err %d aq_err %d\n",
  4529. status, hw->adminq.rq_last_status);
  4530. return -EIO;
  4531. }
  4532. }
  4533. return 0;
  4534. }
  4535. /**
  4536. * ice_open - Called when a network interface becomes active
  4537. * @netdev: network interface device structure
  4538. *
  4539. * The open entry point is called when a network interface is made
  4540. * active by the system (IFF_UP). At this point all resources needed
  4541. * for transmit and receive operations are allocated, the interrupt
  4542. * handler is registered with the OS, the netdev watchdog is enabled,
  4543. * and the stack is notified that the interface is ready.
  4544. *
  4545. * Returns 0 on success, negative value on failure
  4546. */
  4547. static int ice_open(struct net_device *netdev)
  4548. {
  4549. struct ice_netdev_priv *np = netdev_priv(netdev);
  4550. struct ice_vsi *vsi = np->vsi;
  4551. int err;
  4552. netif_carrier_off(netdev);
  4553. err = ice_vsi_open(vsi);
  4554. if (err)
  4555. netdev_err(netdev, "Failed to open VSI 0x%04X on switch 0x%04X\n",
  4556. vsi->vsi_num, vsi->vsw->sw_id);
  4557. return err;
  4558. }
  4559. /**
  4560. * ice_stop - Disables a network interface
  4561. * @netdev: network interface device structure
  4562. *
  4563. * The stop entry point is called when an interface is de-activated by the OS,
  4564. * and the netdevice enters the DOWN state. The hardware is still under the
  4565. * driver's control, but the netdev interface is disabled.
  4566. *
  4567. * Returns success only - not allowed to fail
  4568. */
  4569. static int ice_stop(struct net_device *netdev)
  4570. {
  4571. struct ice_netdev_priv *np = netdev_priv(netdev);
  4572. struct ice_vsi *vsi = np->vsi;
  4573. ice_vsi_close(vsi);
  4574. return 0;
  4575. }
  4576. /**
  4577. * ice_features_check - Validate encapsulated packet conforms to limits
  4578. * @skb: skb buffer
  4579. * @netdev: This port's netdev
  4580. * @features: Offload features that the stack believes apply
  4581. */
  4582. static netdev_features_t
  4583. ice_features_check(struct sk_buff *skb,
  4584. struct net_device __always_unused *netdev,
  4585. netdev_features_t features)
  4586. {
  4587. size_t len;
  4588. /* No point in doing any of this if neither checksum nor GSO are
  4589. * being requested for this frame. We can rule out both by just
  4590. * checking for CHECKSUM_PARTIAL
  4591. */
  4592. if (skb->ip_summed != CHECKSUM_PARTIAL)
  4593. return features;
  4594. /* We cannot support GSO if the MSS is going to be less than
  4595. * 64 bytes. If it is then we need to drop support for GSO.
  4596. */
  4597. if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_size < 64))
  4598. features &= ~NETIF_F_GSO_MASK;
  4599. len = skb_network_header(skb) - skb->data;
  4600. if (len & ~(ICE_TXD_MACLEN_MAX))
  4601. goto out_rm_features;
  4602. len = skb_transport_header(skb) - skb_network_header(skb);
  4603. if (len & ~(ICE_TXD_IPLEN_MAX))
  4604. goto out_rm_features;
  4605. if (skb->encapsulation) {
  4606. len = skb_inner_network_header(skb) - skb_transport_header(skb);
  4607. if (len & ~(ICE_TXD_L4LEN_MAX))
  4608. goto out_rm_features;
  4609. len = skb_inner_transport_header(skb) -
  4610. skb_inner_network_header(skb);
  4611. if (len & ~(ICE_TXD_IPLEN_MAX))
  4612. goto out_rm_features;
  4613. }
  4614. return features;
  4615. out_rm_features:
  4616. return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
  4617. }
  4618. static const struct net_device_ops ice_netdev_ops = {
  4619. .ndo_open = ice_open,
  4620. .ndo_stop = ice_stop,
  4621. .ndo_start_xmit = ice_start_xmit,
  4622. .ndo_features_check = ice_features_check,
  4623. .ndo_set_rx_mode = ice_set_rx_mode,
  4624. .ndo_set_mac_address = ice_set_mac_address,
  4625. .ndo_validate_addr = eth_validate_addr,
  4626. .ndo_change_mtu = ice_change_mtu,
  4627. .ndo_get_stats64 = ice_get_stats64,
  4628. .ndo_vlan_rx_add_vid = ice_vlan_rx_add_vid,
  4629. .ndo_vlan_rx_kill_vid = ice_vlan_rx_kill_vid,
  4630. .ndo_set_features = ice_set_features,
  4631. .ndo_fdb_add = ice_fdb_add,
  4632. .ndo_fdb_del = ice_fdb_del,
  4633. };