ice_main.c 139 KB

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