bnxt.c 240 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391
  1. /* Broadcom NetXtreme-C/E network driver.
  2. *
  3. * Copyright (c) 2014-2016 Broadcom Corporation
  4. * Copyright (c) 2016-2018 Broadcom Limited
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation.
  9. */
  10. #include <linux/module.h>
  11. #include <linux/stringify.h>
  12. #include <linux/kernel.h>
  13. #include <linux/timer.h>
  14. #include <linux/errno.h>
  15. #include <linux/ioport.h>
  16. #include <linux/slab.h>
  17. #include <linux/vmalloc.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/pci.h>
  20. #include <linux/netdevice.h>
  21. #include <linux/etherdevice.h>
  22. #include <linux/skbuff.h>
  23. #include <linux/dma-mapping.h>
  24. #include <linux/bitops.h>
  25. #include <linux/io.h>
  26. #include <linux/irq.h>
  27. #include <linux/delay.h>
  28. #include <asm/byteorder.h>
  29. #include <asm/page.h>
  30. #include <linux/time.h>
  31. #include <linux/mii.h>
  32. #include <linux/if.h>
  33. #include <linux/if_vlan.h>
  34. #include <linux/if_bridge.h>
  35. #include <linux/rtc.h>
  36. #include <linux/bpf.h>
  37. #include <net/ip.h>
  38. #include <net/tcp.h>
  39. #include <net/udp.h>
  40. #include <net/checksum.h>
  41. #include <net/ip6_checksum.h>
  42. #include <net/udp_tunnel.h>
  43. #include <linux/workqueue.h>
  44. #include <linux/prefetch.h>
  45. #include <linux/cache.h>
  46. #include <linux/log2.h>
  47. #include <linux/aer.h>
  48. #include <linux/bitmap.h>
  49. #include <linux/cpu_rmap.h>
  50. #include <linux/cpumask.h>
  51. #include <net/pkt_cls.h>
  52. #include <linux/hwmon.h>
  53. #include <linux/hwmon-sysfs.h>
  54. #include "bnxt_hsi.h"
  55. #include "bnxt.h"
  56. #include "bnxt_ulp.h"
  57. #include "bnxt_sriov.h"
  58. #include "bnxt_ethtool.h"
  59. #include "bnxt_dcb.h"
  60. #include "bnxt_xdp.h"
  61. #include "bnxt_vfr.h"
  62. #include "bnxt_tc.h"
  63. #include "bnxt_devlink.h"
  64. #include "bnxt_debugfs.h"
  65. #define BNXT_TX_TIMEOUT (5 * HZ)
  66. static const char version[] =
  67. "Broadcom NetXtreme-C/E driver " DRV_MODULE_NAME " v" DRV_MODULE_VERSION "\n";
  68. MODULE_LICENSE("GPL");
  69. MODULE_DESCRIPTION("Broadcom BCM573xx network driver");
  70. MODULE_VERSION(DRV_MODULE_VERSION);
  71. #define BNXT_RX_OFFSET (NET_SKB_PAD + NET_IP_ALIGN)
  72. #define BNXT_RX_DMA_OFFSET NET_SKB_PAD
  73. #define BNXT_RX_COPY_THRESH 256
  74. #define BNXT_TX_PUSH_THRESH 164
  75. enum board_idx {
  76. BCM57301,
  77. BCM57302,
  78. BCM57304,
  79. BCM57417_NPAR,
  80. BCM58700,
  81. BCM57311,
  82. BCM57312,
  83. BCM57402,
  84. BCM57404,
  85. BCM57406,
  86. BCM57402_NPAR,
  87. BCM57407,
  88. BCM57412,
  89. BCM57414,
  90. BCM57416,
  91. BCM57417,
  92. BCM57412_NPAR,
  93. BCM57314,
  94. BCM57417_SFP,
  95. BCM57416_SFP,
  96. BCM57404_NPAR,
  97. BCM57406_NPAR,
  98. BCM57407_SFP,
  99. BCM57407_NPAR,
  100. BCM57414_NPAR,
  101. BCM57416_NPAR,
  102. BCM57452,
  103. BCM57454,
  104. BCM5745x_NPAR,
  105. BCM58802,
  106. BCM58804,
  107. BCM58808,
  108. NETXTREME_E_VF,
  109. NETXTREME_C_VF,
  110. NETXTREME_S_VF,
  111. };
  112. /* indexed by enum above */
  113. static const struct {
  114. char *name;
  115. } board_info[] = {
  116. [BCM57301] = { "Broadcom BCM57301 NetXtreme-C 10Gb Ethernet" },
  117. [BCM57302] = { "Broadcom BCM57302 NetXtreme-C 10Gb/25Gb Ethernet" },
  118. [BCM57304] = { "Broadcom BCM57304 NetXtreme-C 10Gb/25Gb/40Gb/50Gb Ethernet" },
  119. [BCM57417_NPAR] = { "Broadcom BCM57417 NetXtreme-E Ethernet Partition" },
  120. [BCM58700] = { "Broadcom BCM58700 Nitro 1Gb/2.5Gb/10Gb Ethernet" },
  121. [BCM57311] = { "Broadcom BCM57311 NetXtreme-C 10Gb Ethernet" },
  122. [BCM57312] = { "Broadcom BCM57312 NetXtreme-C 10Gb/25Gb Ethernet" },
  123. [BCM57402] = { "Broadcom BCM57402 NetXtreme-E 10Gb Ethernet" },
  124. [BCM57404] = { "Broadcom BCM57404 NetXtreme-E 10Gb/25Gb Ethernet" },
  125. [BCM57406] = { "Broadcom BCM57406 NetXtreme-E 10GBase-T Ethernet" },
  126. [BCM57402_NPAR] = { "Broadcom BCM57402 NetXtreme-E Ethernet Partition" },
  127. [BCM57407] = { "Broadcom BCM57407 NetXtreme-E 10GBase-T Ethernet" },
  128. [BCM57412] = { "Broadcom BCM57412 NetXtreme-E 10Gb Ethernet" },
  129. [BCM57414] = { "Broadcom BCM57414 NetXtreme-E 10Gb/25Gb Ethernet" },
  130. [BCM57416] = { "Broadcom BCM57416 NetXtreme-E 10GBase-T Ethernet" },
  131. [BCM57417] = { "Broadcom BCM57417 NetXtreme-E 10GBase-T Ethernet" },
  132. [BCM57412_NPAR] = { "Broadcom BCM57412 NetXtreme-E Ethernet Partition" },
  133. [BCM57314] = { "Broadcom BCM57314 NetXtreme-C 10Gb/25Gb/40Gb/50Gb Ethernet" },
  134. [BCM57417_SFP] = { "Broadcom BCM57417 NetXtreme-E 10Gb/25Gb Ethernet" },
  135. [BCM57416_SFP] = { "Broadcom BCM57416 NetXtreme-E 10Gb Ethernet" },
  136. [BCM57404_NPAR] = { "Broadcom BCM57404 NetXtreme-E Ethernet Partition" },
  137. [BCM57406_NPAR] = { "Broadcom BCM57406 NetXtreme-E Ethernet Partition" },
  138. [BCM57407_SFP] = { "Broadcom BCM57407 NetXtreme-E 25Gb Ethernet" },
  139. [BCM57407_NPAR] = { "Broadcom BCM57407 NetXtreme-E Ethernet Partition" },
  140. [BCM57414_NPAR] = { "Broadcom BCM57414 NetXtreme-E Ethernet Partition" },
  141. [BCM57416_NPAR] = { "Broadcom BCM57416 NetXtreme-E Ethernet Partition" },
  142. [BCM57452] = { "Broadcom BCM57452 NetXtreme-E 10Gb/25Gb/40Gb/50Gb Ethernet" },
  143. [BCM57454] = { "Broadcom BCM57454 NetXtreme-E 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" },
  144. [BCM5745x_NPAR] = { "Broadcom BCM5745x NetXtreme-E Ethernet Partition" },
  145. [BCM58802] = { "Broadcom BCM58802 NetXtreme-S 10Gb/25Gb/40Gb/50Gb Ethernet" },
  146. [BCM58804] = { "Broadcom BCM58804 NetXtreme-S 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" },
  147. [BCM58808] = { "Broadcom BCM58808 NetXtreme-S 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet" },
  148. [NETXTREME_E_VF] = { "Broadcom NetXtreme-E Ethernet Virtual Function" },
  149. [NETXTREME_C_VF] = { "Broadcom NetXtreme-C Ethernet Virtual Function" },
  150. [NETXTREME_S_VF] = { "Broadcom NetXtreme-S Ethernet Virtual Function" },
  151. };
  152. static const struct pci_device_id bnxt_pci_tbl[] = {
  153. { PCI_VDEVICE(BROADCOM, 0x1604), .driver_data = BCM5745x_NPAR },
  154. { PCI_VDEVICE(BROADCOM, 0x1605), .driver_data = BCM5745x_NPAR },
  155. { PCI_VDEVICE(BROADCOM, 0x1614), .driver_data = BCM57454 },
  156. { PCI_VDEVICE(BROADCOM, 0x16c0), .driver_data = BCM57417_NPAR },
  157. { PCI_VDEVICE(BROADCOM, 0x16c8), .driver_data = BCM57301 },
  158. { PCI_VDEVICE(BROADCOM, 0x16c9), .driver_data = BCM57302 },
  159. { PCI_VDEVICE(BROADCOM, 0x16ca), .driver_data = BCM57304 },
  160. { PCI_VDEVICE(BROADCOM, 0x16cc), .driver_data = BCM57417_NPAR },
  161. { PCI_VDEVICE(BROADCOM, 0x16cd), .driver_data = BCM58700 },
  162. { PCI_VDEVICE(BROADCOM, 0x16ce), .driver_data = BCM57311 },
  163. { PCI_VDEVICE(BROADCOM, 0x16cf), .driver_data = BCM57312 },
  164. { PCI_VDEVICE(BROADCOM, 0x16d0), .driver_data = BCM57402 },
  165. { PCI_VDEVICE(BROADCOM, 0x16d1), .driver_data = BCM57404 },
  166. { PCI_VDEVICE(BROADCOM, 0x16d2), .driver_data = BCM57406 },
  167. { PCI_VDEVICE(BROADCOM, 0x16d4), .driver_data = BCM57402_NPAR },
  168. { PCI_VDEVICE(BROADCOM, 0x16d5), .driver_data = BCM57407 },
  169. { PCI_VDEVICE(BROADCOM, 0x16d6), .driver_data = BCM57412 },
  170. { PCI_VDEVICE(BROADCOM, 0x16d7), .driver_data = BCM57414 },
  171. { PCI_VDEVICE(BROADCOM, 0x16d8), .driver_data = BCM57416 },
  172. { PCI_VDEVICE(BROADCOM, 0x16d9), .driver_data = BCM57417 },
  173. { PCI_VDEVICE(BROADCOM, 0x16de), .driver_data = BCM57412_NPAR },
  174. { PCI_VDEVICE(BROADCOM, 0x16df), .driver_data = BCM57314 },
  175. { PCI_VDEVICE(BROADCOM, 0x16e2), .driver_data = BCM57417_SFP },
  176. { PCI_VDEVICE(BROADCOM, 0x16e3), .driver_data = BCM57416_SFP },
  177. { PCI_VDEVICE(BROADCOM, 0x16e7), .driver_data = BCM57404_NPAR },
  178. { PCI_VDEVICE(BROADCOM, 0x16e8), .driver_data = BCM57406_NPAR },
  179. { PCI_VDEVICE(BROADCOM, 0x16e9), .driver_data = BCM57407_SFP },
  180. { PCI_VDEVICE(BROADCOM, 0x16ea), .driver_data = BCM57407_NPAR },
  181. { PCI_VDEVICE(BROADCOM, 0x16eb), .driver_data = BCM57412_NPAR },
  182. { PCI_VDEVICE(BROADCOM, 0x16ec), .driver_data = BCM57414_NPAR },
  183. { PCI_VDEVICE(BROADCOM, 0x16ed), .driver_data = BCM57414_NPAR },
  184. { PCI_VDEVICE(BROADCOM, 0x16ee), .driver_data = BCM57416_NPAR },
  185. { PCI_VDEVICE(BROADCOM, 0x16ef), .driver_data = BCM57416_NPAR },
  186. { PCI_VDEVICE(BROADCOM, 0x16f0), .driver_data = BCM58808 },
  187. { PCI_VDEVICE(BROADCOM, 0x16f1), .driver_data = BCM57452 },
  188. { PCI_VDEVICE(BROADCOM, 0xd802), .driver_data = BCM58802 },
  189. { PCI_VDEVICE(BROADCOM, 0xd804), .driver_data = BCM58804 },
  190. #ifdef CONFIG_BNXT_SRIOV
  191. { PCI_VDEVICE(BROADCOM, 0x1606), .driver_data = NETXTREME_E_VF },
  192. { PCI_VDEVICE(BROADCOM, 0x1609), .driver_data = NETXTREME_E_VF },
  193. { PCI_VDEVICE(BROADCOM, 0x16c1), .driver_data = NETXTREME_E_VF },
  194. { PCI_VDEVICE(BROADCOM, 0x16cb), .driver_data = NETXTREME_C_VF },
  195. { PCI_VDEVICE(BROADCOM, 0x16d3), .driver_data = NETXTREME_E_VF },
  196. { PCI_VDEVICE(BROADCOM, 0x16dc), .driver_data = NETXTREME_E_VF },
  197. { PCI_VDEVICE(BROADCOM, 0x16e1), .driver_data = NETXTREME_C_VF },
  198. { PCI_VDEVICE(BROADCOM, 0x16e5), .driver_data = NETXTREME_C_VF },
  199. { PCI_VDEVICE(BROADCOM, 0xd800), .driver_data = NETXTREME_S_VF },
  200. #endif
  201. { 0 }
  202. };
  203. MODULE_DEVICE_TABLE(pci, bnxt_pci_tbl);
  204. static const u16 bnxt_vf_req_snif[] = {
  205. HWRM_FUNC_CFG,
  206. HWRM_FUNC_VF_CFG,
  207. HWRM_PORT_PHY_QCFG,
  208. HWRM_CFA_L2_FILTER_ALLOC,
  209. };
  210. static const u16 bnxt_async_events_arr[] = {
  211. ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE,
  212. ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD,
  213. ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED,
  214. ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE,
  215. ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE,
  216. };
  217. static struct workqueue_struct *bnxt_pf_wq;
  218. static bool bnxt_vf_pciid(enum board_idx idx)
  219. {
  220. return (idx == NETXTREME_C_VF || idx == NETXTREME_E_VF ||
  221. idx == NETXTREME_S_VF);
  222. }
  223. #define DB_CP_REARM_FLAGS (DB_KEY_CP | DB_IDX_VALID)
  224. #define DB_CP_FLAGS (DB_KEY_CP | DB_IDX_VALID | DB_IRQ_DIS)
  225. #define DB_CP_IRQ_DIS_FLAGS (DB_KEY_CP | DB_IRQ_DIS)
  226. #define BNXT_CP_DB_REARM(db, raw_cons) \
  227. writel(DB_CP_REARM_FLAGS | RING_CMP(raw_cons), db)
  228. #define BNXT_CP_DB(db, raw_cons) \
  229. writel(DB_CP_FLAGS | RING_CMP(raw_cons), db)
  230. #define BNXT_CP_DB_IRQ_DIS(db) \
  231. writel(DB_CP_IRQ_DIS_FLAGS, db)
  232. const u16 bnxt_lhint_arr[] = {
  233. TX_BD_FLAGS_LHINT_512_AND_SMALLER,
  234. TX_BD_FLAGS_LHINT_512_TO_1023,
  235. TX_BD_FLAGS_LHINT_1024_TO_2047,
  236. TX_BD_FLAGS_LHINT_1024_TO_2047,
  237. TX_BD_FLAGS_LHINT_2048_AND_LARGER,
  238. TX_BD_FLAGS_LHINT_2048_AND_LARGER,
  239. TX_BD_FLAGS_LHINT_2048_AND_LARGER,
  240. TX_BD_FLAGS_LHINT_2048_AND_LARGER,
  241. TX_BD_FLAGS_LHINT_2048_AND_LARGER,
  242. TX_BD_FLAGS_LHINT_2048_AND_LARGER,
  243. TX_BD_FLAGS_LHINT_2048_AND_LARGER,
  244. TX_BD_FLAGS_LHINT_2048_AND_LARGER,
  245. TX_BD_FLAGS_LHINT_2048_AND_LARGER,
  246. TX_BD_FLAGS_LHINT_2048_AND_LARGER,
  247. TX_BD_FLAGS_LHINT_2048_AND_LARGER,
  248. TX_BD_FLAGS_LHINT_2048_AND_LARGER,
  249. TX_BD_FLAGS_LHINT_2048_AND_LARGER,
  250. TX_BD_FLAGS_LHINT_2048_AND_LARGER,
  251. TX_BD_FLAGS_LHINT_2048_AND_LARGER,
  252. };
  253. static u16 bnxt_xmit_get_cfa_action(struct sk_buff *skb)
  254. {
  255. struct metadata_dst *md_dst = skb_metadata_dst(skb);
  256. if (!md_dst || md_dst->type != METADATA_HW_PORT_MUX)
  257. return 0;
  258. return md_dst->u.port_info.port_id;
  259. }
  260. static netdev_tx_t bnxt_start_xmit(struct sk_buff *skb, struct net_device *dev)
  261. {
  262. struct bnxt *bp = netdev_priv(dev);
  263. struct tx_bd *txbd;
  264. struct tx_bd_ext *txbd1;
  265. struct netdev_queue *txq;
  266. int i;
  267. dma_addr_t mapping;
  268. unsigned int length, pad = 0;
  269. u32 len, free_size, vlan_tag_flags, cfa_action, flags;
  270. u16 prod, last_frag;
  271. struct pci_dev *pdev = bp->pdev;
  272. struct bnxt_tx_ring_info *txr;
  273. struct bnxt_sw_tx_bd *tx_buf;
  274. i = skb_get_queue_mapping(skb);
  275. if (unlikely(i >= bp->tx_nr_rings)) {
  276. dev_kfree_skb_any(skb);
  277. return NETDEV_TX_OK;
  278. }
  279. txq = netdev_get_tx_queue(dev, i);
  280. txr = &bp->tx_ring[bp->tx_ring_map[i]];
  281. prod = txr->tx_prod;
  282. free_size = bnxt_tx_avail(bp, txr);
  283. if (unlikely(free_size < skb_shinfo(skb)->nr_frags + 2)) {
  284. netif_tx_stop_queue(txq);
  285. return NETDEV_TX_BUSY;
  286. }
  287. length = skb->len;
  288. len = skb_headlen(skb);
  289. last_frag = skb_shinfo(skb)->nr_frags;
  290. txbd = &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
  291. txbd->tx_bd_opaque = prod;
  292. tx_buf = &txr->tx_buf_ring[prod];
  293. tx_buf->skb = skb;
  294. tx_buf->nr_frags = last_frag;
  295. vlan_tag_flags = 0;
  296. cfa_action = bnxt_xmit_get_cfa_action(skb);
  297. if (skb_vlan_tag_present(skb)) {
  298. vlan_tag_flags = TX_BD_CFA_META_KEY_VLAN |
  299. skb_vlan_tag_get(skb);
  300. /* Currently supports 8021Q, 8021AD vlan offloads
  301. * QINQ1, QINQ2, QINQ3 vlan headers are deprecated
  302. */
  303. if (skb->vlan_proto == htons(ETH_P_8021Q))
  304. vlan_tag_flags |= 1 << TX_BD_CFA_META_TPID_SHIFT;
  305. }
  306. if (free_size == bp->tx_ring_size && length <= bp->tx_push_thresh) {
  307. struct tx_push_buffer *tx_push_buf = txr->tx_push;
  308. struct tx_push_bd *tx_push = &tx_push_buf->push_bd;
  309. struct tx_bd_ext *tx_push1 = &tx_push->txbd2;
  310. void *pdata = tx_push_buf->data;
  311. u64 *end;
  312. int j, push_len;
  313. /* Set COAL_NOW to be ready quickly for the next push */
  314. tx_push->tx_bd_len_flags_type =
  315. cpu_to_le32((length << TX_BD_LEN_SHIFT) |
  316. TX_BD_TYPE_LONG_TX_BD |
  317. TX_BD_FLAGS_LHINT_512_AND_SMALLER |
  318. TX_BD_FLAGS_COAL_NOW |
  319. TX_BD_FLAGS_PACKET_END |
  320. (2 << TX_BD_FLAGS_BD_CNT_SHIFT));
  321. if (skb->ip_summed == CHECKSUM_PARTIAL)
  322. tx_push1->tx_bd_hsize_lflags =
  323. cpu_to_le32(TX_BD_FLAGS_TCP_UDP_CHKSUM);
  324. else
  325. tx_push1->tx_bd_hsize_lflags = 0;
  326. tx_push1->tx_bd_cfa_meta = cpu_to_le32(vlan_tag_flags);
  327. tx_push1->tx_bd_cfa_action =
  328. cpu_to_le32(cfa_action << TX_BD_CFA_ACTION_SHIFT);
  329. end = pdata + length;
  330. end = PTR_ALIGN(end, 8) - 1;
  331. *end = 0;
  332. skb_copy_from_linear_data(skb, pdata, len);
  333. pdata += len;
  334. for (j = 0; j < last_frag; j++) {
  335. skb_frag_t *frag = &skb_shinfo(skb)->frags[j];
  336. void *fptr;
  337. fptr = skb_frag_address_safe(frag);
  338. if (!fptr)
  339. goto normal_tx;
  340. memcpy(pdata, fptr, skb_frag_size(frag));
  341. pdata += skb_frag_size(frag);
  342. }
  343. txbd->tx_bd_len_flags_type = tx_push->tx_bd_len_flags_type;
  344. txbd->tx_bd_haddr = txr->data_mapping;
  345. prod = NEXT_TX(prod);
  346. txbd = &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
  347. memcpy(txbd, tx_push1, sizeof(*txbd));
  348. prod = NEXT_TX(prod);
  349. tx_push->doorbell =
  350. cpu_to_le32(DB_KEY_TX_PUSH | DB_LONG_TX_PUSH | prod);
  351. txr->tx_prod = prod;
  352. tx_buf->is_push = 1;
  353. netdev_tx_sent_queue(txq, skb->len);
  354. wmb(); /* Sync is_push and byte queue before pushing data */
  355. push_len = (length + sizeof(*tx_push) + 7) / 8;
  356. if (push_len > 16) {
  357. __iowrite64_copy(txr->tx_doorbell, tx_push_buf, 16);
  358. __iowrite32_copy(txr->tx_doorbell + 4, tx_push_buf + 1,
  359. (push_len - 16) << 1);
  360. } else {
  361. __iowrite64_copy(txr->tx_doorbell, tx_push_buf,
  362. push_len);
  363. }
  364. goto tx_done;
  365. }
  366. normal_tx:
  367. if (length < BNXT_MIN_PKT_SIZE) {
  368. pad = BNXT_MIN_PKT_SIZE - length;
  369. if (skb_pad(skb, pad)) {
  370. /* SKB already freed. */
  371. tx_buf->skb = NULL;
  372. return NETDEV_TX_OK;
  373. }
  374. length = BNXT_MIN_PKT_SIZE;
  375. }
  376. mapping = dma_map_single(&pdev->dev, skb->data, len, DMA_TO_DEVICE);
  377. if (unlikely(dma_mapping_error(&pdev->dev, mapping))) {
  378. dev_kfree_skb_any(skb);
  379. tx_buf->skb = NULL;
  380. return NETDEV_TX_OK;
  381. }
  382. dma_unmap_addr_set(tx_buf, mapping, mapping);
  383. flags = (len << TX_BD_LEN_SHIFT) | TX_BD_TYPE_LONG_TX_BD |
  384. ((last_frag + 2) << TX_BD_FLAGS_BD_CNT_SHIFT);
  385. txbd->tx_bd_haddr = cpu_to_le64(mapping);
  386. prod = NEXT_TX(prod);
  387. txbd1 = (struct tx_bd_ext *)
  388. &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
  389. txbd1->tx_bd_hsize_lflags = 0;
  390. if (skb_is_gso(skb)) {
  391. u32 hdr_len;
  392. if (skb->encapsulation)
  393. hdr_len = skb_inner_network_offset(skb) +
  394. skb_inner_network_header_len(skb) +
  395. inner_tcp_hdrlen(skb);
  396. else
  397. hdr_len = skb_transport_offset(skb) +
  398. tcp_hdrlen(skb);
  399. txbd1->tx_bd_hsize_lflags = cpu_to_le32(TX_BD_FLAGS_LSO |
  400. TX_BD_FLAGS_T_IPID |
  401. (hdr_len << (TX_BD_HSIZE_SHIFT - 1)));
  402. length = skb_shinfo(skb)->gso_size;
  403. txbd1->tx_bd_mss = cpu_to_le32(length);
  404. length += hdr_len;
  405. } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
  406. txbd1->tx_bd_hsize_lflags =
  407. cpu_to_le32(TX_BD_FLAGS_TCP_UDP_CHKSUM);
  408. txbd1->tx_bd_mss = 0;
  409. }
  410. length >>= 9;
  411. if (unlikely(length >= ARRAY_SIZE(bnxt_lhint_arr))) {
  412. dev_warn_ratelimited(&pdev->dev, "Dropped oversize %d bytes TX packet.\n",
  413. skb->len);
  414. i = 0;
  415. goto tx_dma_error;
  416. }
  417. flags |= bnxt_lhint_arr[length];
  418. txbd->tx_bd_len_flags_type = cpu_to_le32(flags);
  419. txbd1->tx_bd_cfa_meta = cpu_to_le32(vlan_tag_flags);
  420. txbd1->tx_bd_cfa_action =
  421. cpu_to_le32(cfa_action << TX_BD_CFA_ACTION_SHIFT);
  422. for (i = 0; i < last_frag; i++) {
  423. skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  424. prod = NEXT_TX(prod);
  425. txbd = &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
  426. len = skb_frag_size(frag);
  427. mapping = skb_frag_dma_map(&pdev->dev, frag, 0, len,
  428. DMA_TO_DEVICE);
  429. if (unlikely(dma_mapping_error(&pdev->dev, mapping)))
  430. goto tx_dma_error;
  431. tx_buf = &txr->tx_buf_ring[prod];
  432. dma_unmap_addr_set(tx_buf, mapping, mapping);
  433. txbd->tx_bd_haddr = cpu_to_le64(mapping);
  434. flags = len << TX_BD_LEN_SHIFT;
  435. txbd->tx_bd_len_flags_type = cpu_to_le32(flags);
  436. }
  437. flags &= ~TX_BD_LEN;
  438. txbd->tx_bd_len_flags_type =
  439. cpu_to_le32(((len + pad) << TX_BD_LEN_SHIFT) | flags |
  440. TX_BD_FLAGS_PACKET_END);
  441. netdev_tx_sent_queue(txq, skb->len);
  442. /* Sync BD data before updating doorbell */
  443. wmb();
  444. prod = NEXT_TX(prod);
  445. txr->tx_prod = prod;
  446. if (!skb->xmit_more || netif_xmit_stopped(txq))
  447. bnxt_db_write(bp, txr->tx_doorbell, DB_KEY_TX | prod);
  448. tx_done:
  449. mmiowb();
  450. if (unlikely(bnxt_tx_avail(bp, txr) <= MAX_SKB_FRAGS + 1)) {
  451. if (skb->xmit_more && !tx_buf->is_push)
  452. bnxt_db_write(bp, txr->tx_doorbell, DB_KEY_TX | prod);
  453. netif_tx_stop_queue(txq);
  454. /* netif_tx_stop_queue() must be done before checking
  455. * tx index in bnxt_tx_avail() below, because in
  456. * bnxt_tx_int(), we update tx index before checking for
  457. * netif_tx_queue_stopped().
  458. */
  459. smp_mb();
  460. if (bnxt_tx_avail(bp, txr) > bp->tx_wake_thresh)
  461. netif_tx_wake_queue(txq);
  462. }
  463. return NETDEV_TX_OK;
  464. tx_dma_error:
  465. last_frag = i;
  466. /* start back at beginning and unmap skb */
  467. prod = txr->tx_prod;
  468. tx_buf = &txr->tx_buf_ring[prod];
  469. tx_buf->skb = NULL;
  470. dma_unmap_single(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
  471. skb_headlen(skb), PCI_DMA_TODEVICE);
  472. prod = NEXT_TX(prod);
  473. /* unmap remaining mapped pages */
  474. for (i = 0; i < last_frag; i++) {
  475. prod = NEXT_TX(prod);
  476. tx_buf = &txr->tx_buf_ring[prod];
  477. dma_unmap_page(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
  478. skb_frag_size(&skb_shinfo(skb)->frags[i]),
  479. PCI_DMA_TODEVICE);
  480. }
  481. dev_kfree_skb_any(skb);
  482. return NETDEV_TX_OK;
  483. }
  484. static void bnxt_tx_int(struct bnxt *bp, struct bnxt_napi *bnapi, int nr_pkts)
  485. {
  486. struct bnxt_tx_ring_info *txr = bnapi->tx_ring;
  487. struct netdev_queue *txq = netdev_get_tx_queue(bp->dev, txr->txq_index);
  488. u16 cons = txr->tx_cons;
  489. struct pci_dev *pdev = bp->pdev;
  490. int i;
  491. unsigned int tx_bytes = 0;
  492. for (i = 0; i < nr_pkts; i++) {
  493. struct bnxt_sw_tx_bd *tx_buf;
  494. struct sk_buff *skb;
  495. int j, last;
  496. tx_buf = &txr->tx_buf_ring[cons];
  497. cons = NEXT_TX(cons);
  498. skb = tx_buf->skb;
  499. tx_buf->skb = NULL;
  500. if (tx_buf->is_push) {
  501. tx_buf->is_push = 0;
  502. goto next_tx_int;
  503. }
  504. dma_unmap_single(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
  505. skb_headlen(skb), PCI_DMA_TODEVICE);
  506. last = tx_buf->nr_frags;
  507. for (j = 0; j < last; j++) {
  508. cons = NEXT_TX(cons);
  509. tx_buf = &txr->tx_buf_ring[cons];
  510. dma_unmap_page(
  511. &pdev->dev,
  512. dma_unmap_addr(tx_buf, mapping),
  513. skb_frag_size(&skb_shinfo(skb)->frags[j]),
  514. PCI_DMA_TODEVICE);
  515. }
  516. next_tx_int:
  517. cons = NEXT_TX(cons);
  518. tx_bytes += skb->len;
  519. dev_kfree_skb_any(skb);
  520. }
  521. netdev_tx_completed_queue(txq, nr_pkts, tx_bytes);
  522. txr->tx_cons = cons;
  523. /* Need to make the tx_cons update visible to bnxt_start_xmit()
  524. * before checking for netif_tx_queue_stopped(). Without the
  525. * memory barrier, there is a small possibility that bnxt_start_xmit()
  526. * will miss it and cause the queue to be stopped forever.
  527. */
  528. smp_mb();
  529. if (unlikely(netif_tx_queue_stopped(txq)) &&
  530. (bnxt_tx_avail(bp, txr) > bp->tx_wake_thresh)) {
  531. __netif_tx_lock(txq, smp_processor_id());
  532. if (netif_tx_queue_stopped(txq) &&
  533. bnxt_tx_avail(bp, txr) > bp->tx_wake_thresh &&
  534. txr->dev_state != BNXT_DEV_STATE_CLOSING)
  535. netif_tx_wake_queue(txq);
  536. __netif_tx_unlock(txq);
  537. }
  538. }
  539. static struct page *__bnxt_alloc_rx_page(struct bnxt *bp, dma_addr_t *mapping,
  540. gfp_t gfp)
  541. {
  542. struct device *dev = &bp->pdev->dev;
  543. struct page *page;
  544. page = alloc_page(gfp);
  545. if (!page)
  546. return NULL;
  547. *mapping = dma_map_page_attrs(dev, page, 0, PAGE_SIZE, bp->rx_dir,
  548. DMA_ATTR_WEAK_ORDERING);
  549. if (dma_mapping_error(dev, *mapping)) {
  550. __free_page(page);
  551. return NULL;
  552. }
  553. *mapping += bp->rx_dma_offset;
  554. return page;
  555. }
  556. static inline u8 *__bnxt_alloc_rx_data(struct bnxt *bp, dma_addr_t *mapping,
  557. gfp_t gfp)
  558. {
  559. u8 *data;
  560. struct pci_dev *pdev = bp->pdev;
  561. data = kmalloc(bp->rx_buf_size, gfp);
  562. if (!data)
  563. return NULL;
  564. *mapping = dma_map_single_attrs(&pdev->dev, data + bp->rx_dma_offset,
  565. bp->rx_buf_use_size, bp->rx_dir,
  566. DMA_ATTR_WEAK_ORDERING);
  567. if (dma_mapping_error(&pdev->dev, *mapping)) {
  568. kfree(data);
  569. data = NULL;
  570. }
  571. return data;
  572. }
  573. int bnxt_alloc_rx_data(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
  574. u16 prod, gfp_t gfp)
  575. {
  576. struct rx_bd *rxbd = &rxr->rx_desc_ring[RX_RING(prod)][RX_IDX(prod)];
  577. struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[prod];
  578. dma_addr_t mapping;
  579. if (BNXT_RX_PAGE_MODE(bp)) {
  580. struct page *page = __bnxt_alloc_rx_page(bp, &mapping, gfp);
  581. if (!page)
  582. return -ENOMEM;
  583. rx_buf->data = page;
  584. rx_buf->data_ptr = page_address(page) + bp->rx_offset;
  585. } else {
  586. u8 *data = __bnxt_alloc_rx_data(bp, &mapping, gfp);
  587. if (!data)
  588. return -ENOMEM;
  589. rx_buf->data = data;
  590. rx_buf->data_ptr = data + bp->rx_offset;
  591. }
  592. rx_buf->mapping = mapping;
  593. rxbd->rx_bd_haddr = cpu_to_le64(mapping);
  594. return 0;
  595. }
  596. void bnxt_reuse_rx_data(struct bnxt_rx_ring_info *rxr, u16 cons, void *data)
  597. {
  598. u16 prod = rxr->rx_prod;
  599. struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf;
  600. struct rx_bd *cons_bd, *prod_bd;
  601. prod_rx_buf = &rxr->rx_buf_ring[prod];
  602. cons_rx_buf = &rxr->rx_buf_ring[cons];
  603. prod_rx_buf->data = data;
  604. prod_rx_buf->data_ptr = cons_rx_buf->data_ptr;
  605. prod_rx_buf->mapping = cons_rx_buf->mapping;
  606. prod_bd = &rxr->rx_desc_ring[RX_RING(prod)][RX_IDX(prod)];
  607. cons_bd = &rxr->rx_desc_ring[RX_RING(cons)][RX_IDX(cons)];
  608. prod_bd->rx_bd_haddr = cons_bd->rx_bd_haddr;
  609. }
  610. static inline u16 bnxt_find_next_agg_idx(struct bnxt_rx_ring_info *rxr, u16 idx)
  611. {
  612. u16 next, max = rxr->rx_agg_bmap_size;
  613. next = find_next_zero_bit(rxr->rx_agg_bmap, max, idx);
  614. if (next >= max)
  615. next = find_first_zero_bit(rxr->rx_agg_bmap, max);
  616. return next;
  617. }
  618. static inline int bnxt_alloc_rx_page(struct bnxt *bp,
  619. struct bnxt_rx_ring_info *rxr,
  620. u16 prod, gfp_t gfp)
  621. {
  622. struct rx_bd *rxbd =
  623. &rxr->rx_agg_desc_ring[RX_RING(prod)][RX_IDX(prod)];
  624. struct bnxt_sw_rx_agg_bd *rx_agg_buf;
  625. struct pci_dev *pdev = bp->pdev;
  626. struct page *page;
  627. dma_addr_t mapping;
  628. u16 sw_prod = rxr->rx_sw_agg_prod;
  629. unsigned int offset = 0;
  630. if (PAGE_SIZE > BNXT_RX_PAGE_SIZE) {
  631. page = rxr->rx_page;
  632. if (!page) {
  633. page = alloc_page(gfp);
  634. if (!page)
  635. return -ENOMEM;
  636. rxr->rx_page = page;
  637. rxr->rx_page_offset = 0;
  638. }
  639. offset = rxr->rx_page_offset;
  640. rxr->rx_page_offset += BNXT_RX_PAGE_SIZE;
  641. if (rxr->rx_page_offset == PAGE_SIZE)
  642. rxr->rx_page = NULL;
  643. else
  644. get_page(page);
  645. } else {
  646. page = alloc_page(gfp);
  647. if (!page)
  648. return -ENOMEM;
  649. }
  650. mapping = dma_map_page_attrs(&pdev->dev, page, offset,
  651. BNXT_RX_PAGE_SIZE, PCI_DMA_FROMDEVICE,
  652. DMA_ATTR_WEAK_ORDERING);
  653. if (dma_mapping_error(&pdev->dev, mapping)) {
  654. __free_page(page);
  655. return -EIO;
  656. }
  657. if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap)))
  658. sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod);
  659. __set_bit(sw_prod, rxr->rx_agg_bmap);
  660. rx_agg_buf = &rxr->rx_agg_ring[sw_prod];
  661. rxr->rx_sw_agg_prod = NEXT_RX_AGG(sw_prod);
  662. rx_agg_buf->page = page;
  663. rx_agg_buf->offset = offset;
  664. rx_agg_buf->mapping = mapping;
  665. rxbd->rx_bd_haddr = cpu_to_le64(mapping);
  666. rxbd->rx_bd_opaque = sw_prod;
  667. return 0;
  668. }
  669. static void bnxt_reuse_rx_agg_bufs(struct bnxt_napi *bnapi, u16 cp_cons,
  670. u32 agg_bufs)
  671. {
  672. struct bnxt *bp = bnapi->bp;
  673. struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
  674. struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
  675. u16 prod = rxr->rx_agg_prod;
  676. u16 sw_prod = rxr->rx_sw_agg_prod;
  677. u32 i;
  678. for (i = 0; i < agg_bufs; i++) {
  679. u16 cons;
  680. struct rx_agg_cmp *agg;
  681. struct bnxt_sw_rx_agg_bd *cons_rx_buf, *prod_rx_buf;
  682. struct rx_bd *prod_bd;
  683. struct page *page;
  684. agg = (struct rx_agg_cmp *)
  685. &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
  686. cons = agg->rx_agg_cmp_opaque;
  687. __clear_bit(cons, rxr->rx_agg_bmap);
  688. if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap)))
  689. sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod);
  690. __set_bit(sw_prod, rxr->rx_agg_bmap);
  691. prod_rx_buf = &rxr->rx_agg_ring[sw_prod];
  692. cons_rx_buf = &rxr->rx_agg_ring[cons];
  693. /* It is possible for sw_prod to be equal to cons, so
  694. * set cons_rx_buf->page to NULL first.
  695. */
  696. page = cons_rx_buf->page;
  697. cons_rx_buf->page = NULL;
  698. prod_rx_buf->page = page;
  699. prod_rx_buf->offset = cons_rx_buf->offset;
  700. prod_rx_buf->mapping = cons_rx_buf->mapping;
  701. prod_bd = &rxr->rx_agg_desc_ring[RX_RING(prod)][RX_IDX(prod)];
  702. prod_bd->rx_bd_haddr = cpu_to_le64(cons_rx_buf->mapping);
  703. prod_bd->rx_bd_opaque = sw_prod;
  704. prod = NEXT_RX_AGG(prod);
  705. sw_prod = NEXT_RX_AGG(sw_prod);
  706. cp_cons = NEXT_CMP(cp_cons);
  707. }
  708. rxr->rx_agg_prod = prod;
  709. rxr->rx_sw_agg_prod = sw_prod;
  710. }
  711. static struct sk_buff *bnxt_rx_page_skb(struct bnxt *bp,
  712. struct bnxt_rx_ring_info *rxr,
  713. u16 cons, void *data, u8 *data_ptr,
  714. dma_addr_t dma_addr,
  715. unsigned int offset_and_len)
  716. {
  717. unsigned int payload = offset_and_len >> 16;
  718. unsigned int len = offset_and_len & 0xffff;
  719. struct skb_frag_struct *frag;
  720. struct page *page = data;
  721. u16 prod = rxr->rx_prod;
  722. struct sk_buff *skb;
  723. int off, err;
  724. err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
  725. if (unlikely(err)) {
  726. bnxt_reuse_rx_data(rxr, cons, data);
  727. return NULL;
  728. }
  729. dma_addr -= bp->rx_dma_offset;
  730. dma_unmap_page_attrs(&bp->pdev->dev, dma_addr, PAGE_SIZE, bp->rx_dir,
  731. DMA_ATTR_WEAK_ORDERING);
  732. if (unlikely(!payload))
  733. payload = eth_get_headlen(data_ptr, len);
  734. skb = napi_alloc_skb(&rxr->bnapi->napi, payload);
  735. if (!skb) {
  736. __free_page(page);
  737. return NULL;
  738. }
  739. off = (void *)data_ptr - page_address(page);
  740. skb_add_rx_frag(skb, 0, page, off, len, PAGE_SIZE);
  741. memcpy(skb->data - NET_IP_ALIGN, data_ptr - NET_IP_ALIGN,
  742. payload + NET_IP_ALIGN);
  743. frag = &skb_shinfo(skb)->frags[0];
  744. skb_frag_size_sub(frag, payload);
  745. frag->page_offset += payload;
  746. skb->data_len -= payload;
  747. skb->tail += payload;
  748. return skb;
  749. }
  750. static struct sk_buff *bnxt_rx_skb(struct bnxt *bp,
  751. struct bnxt_rx_ring_info *rxr, u16 cons,
  752. void *data, u8 *data_ptr,
  753. dma_addr_t dma_addr,
  754. unsigned int offset_and_len)
  755. {
  756. u16 prod = rxr->rx_prod;
  757. struct sk_buff *skb;
  758. int err;
  759. err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
  760. if (unlikely(err)) {
  761. bnxt_reuse_rx_data(rxr, cons, data);
  762. return NULL;
  763. }
  764. skb = build_skb(data, 0);
  765. dma_unmap_single_attrs(&bp->pdev->dev, dma_addr, bp->rx_buf_use_size,
  766. bp->rx_dir, DMA_ATTR_WEAK_ORDERING);
  767. if (!skb) {
  768. kfree(data);
  769. return NULL;
  770. }
  771. skb_reserve(skb, bp->rx_offset);
  772. skb_put(skb, offset_and_len & 0xffff);
  773. return skb;
  774. }
  775. static struct sk_buff *bnxt_rx_pages(struct bnxt *bp, struct bnxt_napi *bnapi,
  776. struct sk_buff *skb, u16 cp_cons,
  777. u32 agg_bufs)
  778. {
  779. struct pci_dev *pdev = bp->pdev;
  780. struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
  781. struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
  782. u16 prod = rxr->rx_agg_prod;
  783. u32 i;
  784. for (i = 0; i < agg_bufs; i++) {
  785. u16 cons, frag_len;
  786. struct rx_agg_cmp *agg;
  787. struct bnxt_sw_rx_agg_bd *cons_rx_buf;
  788. struct page *page;
  789. dma_addr_t mapping;
  790. agg = (struct rx_agg_cmp *)
  791. &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
  792. cons = agg->rx_agg_cmp_opaque;
  793. frag_len = (le32_to_cpu(agg->rx_agg_cmp_len_flags_type) &
  794. RX_AGG_CMP_LEN) >> RX_AGG_CMP_LEN_SHIFT;
  795. cons_rx_buf = &rxr->rx_agg_ring[cons];
  796. skb_fill_page_desc(skb, i, cons_rx_buf->page,
  797. cons_rx_buf->offset, frag_len);
  798. __clear_bit(cons, rxr->rx_agg_bmap);
  799. /* It is possible for bnxt_alloc_rx_page() to allocate
  800. * a sw_prod index that equals the cons index, so we
  801. * need to clear the cons entry now.
  802. */
  803. mapping = cons_rx_buf->mapping;
  804. page = cons_rx_buf->page;
  805. cons_rx_buf->page = NULL;
  806. if (bnxt_alloc_rx_page(bp, rxr, prod, GFP_ATOMIC) != 0) {
  807. struct skb_shared_info *shinfo;
  808. unsigned int nr_frags;
  809. shinfo = skb_shinfo(skb);
  810. nr_frags = --shinfo->nr_frags;
  811. __skb_frag_set_page(&shinfo->frags[nr_frags], NULL);
  812. dev_kfree_skb(skb);
  813. cons_rx_buf->page = page;
  814. /* Update prod since possibly some pages have been
  815. * allocated already.
  816. */
  817. rxr->rx_agg_prod = prod;
  818. bnxt_reuse_rx_agg_bufs(bnapi, cp_cons, agg_bufs - i);
  819. return NULL;
  820. }
  821. dma_unmap_page_attrs(&pdev->dev, mapping, BNXT_RX_PAGE_SIZE,
  822. PCI_DMA_FROMDEVICE,
  823. DMA_ATTR_WEAK_ORDERING);
  824. skb->data_len += frag_len;
  825. skb->len += frag_len;
  826. skb->truesize += PAGE_SIZE;
  827. prod = NEXT_RX_AGG(prod);
  828. cp_cons = NEXT_CMP(cp_cons);
  829. }
  830. rxr->rx_agg_prod = prod;
  831. return skb;
  832. }
  833. static int bnxt_agg_bufs_valid(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
  834. u8 agg_bufs, u32 *raw_cons)
  835. {
  836. u16 last;
  837. struct rx_agg_cmp *agg;
  838. *raw_cons = ADV_RAW_CMP(*raw_cons, agg_bufs);
  839. last = RING_CMP(*raw_cons);
  840. agg = (struct rx_agg_cmp *)
  841. &cpr->cp_desc_ring[CP_RING(last)][CP_IDX(last)];
  842. return RX_AGG_CMP_VALID(agg, *raw_cons);
  843. }
  844. static inline struct sk_buff *bnxt_copy_skb(struct bnxt_napi *bnapi, u8 *data,
  845. unsigned int len,
  846. dma_addr_t mapping)
  847. {
  848. struct bnxt *bp = bnapi->bp;
  849. struct pci_dev *pdev = bp->pdev;
  850. struct sk_buff *skb;
  851. skb = napi_alloc_skb(&bnapi->napi, len);
  852. if (!skb)
  853. return NULL;
  854. dma_sync_single_for_cpu(&pdev->dev, mapping, bp->rx_copy_thresh,
  855. bp->rx_dir);
  856. memcpy(skb->data - NET_IP_ALIGN, data - NET_IP_ALIGN,
  857. len + NET_IP_ALIGN);
  858. dma_sync_single_for_device(&pdev->dev, mapping, bp->rx_copy_thresh,
  859. bp->rx_dir);
  860. skb_put(skb, len);
  861. return skb;
  862. }
  863. static int bnxt_discard_rx(struct bnxt *bp, struct bnxt_napi *bnapi,
  864. u32 *raw_cons, void *cmp)
  865. {
  866. struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
  867. struct rx_cmp *rxcmp = cmp;
  868. u32 tmp_raw_cons = *raw_cons;
  869. u8 cmp_type, agg_bufs = 0;
  870. cmp_type = RX_CMP_TYPE(rxcmp);
  871. if (cmp_type == CMP_TYPE_RX_L2_CMP) {
  872. agg_bufs = (le32_to_cpu(rxcmp->rx_cmp_misc_v1) &
  873. RX_CMP_AGG_BUFS) >>
  874. RX_CMP_AGG_BUFS_SHIFT;
  875. } else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
  876. struct rx_tpa_end_cmp *tpa_end = cmp;
  877. agg_bufs = (le32_to_cpu(tpa_end->rx_tpa_end_cmp_misc_v1) &
  878. RX_TPA_END_CMP_AGG_BUFS) >>
  879. RX_TPA_END_CMP_AGG_BUFS_SHIFT;
  880. }
  881. if (agg_bufs) {
  882. if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons))
  883. return -EBUSY;
  884. }
  885. *raw_cons = tmp_raw_cons;
  886. return 0;
  887. }
  888. static void bnxt_queue_sp_work(struct bnxt *bp)
  889. {
  890. if (BNXT_PF(bp))
  891. queue_work(bnxt_pf_wq, &bp->sp_task);
  892. else
  893. schedule_work(&bp->sp_task);
  894. }
  895. static void bnxt_cancel_sp_work(struct bnxt *bp)
  896. {
  897. if (BNXT_PF(bp))
  898. flush_workqueue(bnxt_pf_wq);
  899. else
  900. cancel_work_sync(&bp->sp_task);
  901. }
  902. static void bnxt_sched_reset(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
  903. {
  904. if (!rxr->bnapi->in_reset) {
  905. rxr->bnapi->in_reset = true;
  906. set_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event);
  907. bnxt_queue_sp_work(bp);
  908. }
  909. rxr->rx_next_cons = 0xffff;
  910. }
  911. static void bnxt_tpa_start(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
  912. struct rx_tpa_start_cmp *tpa_start,
  913. struct rx_tpa_start_cmp_ext *tpa_start1)
  914. {
  915. u8 agg_id = TPA_START_AGG_ID(tpa_start);
  916. u16 cons, prod;
  917. struct bnxt_tpa_info *tpa_info;
  918. struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf;
  919. struct rx_bd *prod_bd;
  920. dma_addr_t mapping;
  921. cons = tpa_start->rx_tpa_start_cmp_opaque;
  922. prod = rxr->rx_prod;
  923. cons_rx_buf = &rxr->rx_buf_ring[cons];
  924. prod_rx_buf = &rxr->rx_buf_ring[prod];
  925. tpa_info = &rxr->rx_tpa[agg_id];
  926. if (unlikely(cons != rxr->rx_next_cons)) {
  927. netdev_warn(bp->dev, "TPA cons %x != expected cons %x\n",
  928. cons, rxr->rx_next_cons);
  929. bnxt_sched_reset(bp, rxr);
  930. return;
  931. }
  932. /* Store cfa_code in tpa_info to use in tpa_end
  933. * completion processing.
  934. */
  935. tpa_info->cfa_code = TPA_START_CFA_CODE(tpa_start1);
  936. prod_rx_buf->data = tpa_info->data;
  937. prod_rx_buf->data_ptr = tpa_info->data_ptr;
  938. mapping = tpa_info->mapping;
  939. prod_rx_buf->mapping = mapping;
  940. prod_bd = &rxr->rx_desc_ring[RX_RING(prod)][RX_IDX(prod)];
  941. prod_bd->rx_bd_haddr = cpu_to_le64(mapping);
  942. tpa_info->data = cons_rx_buf->data;
  943. tpa_info->data_ptr = cons_rx_buf->data_ptr;
  944. cons_rx_buf->data = NULL;
  945. tpa_info->mapping = cons_rx_buf->mapping;
  946. tpa_info->len =
  947. le32_to_cpu(tpa_start->rx_tpa_start_cmp_len_flags_type) >>
  948. RX_TPA_START_CMP_LEN_SHIFT;
  949. if (likely(TPA_START_HASH_VALID(tpa_start))) {
  950. u32 hash_type = TPA_START_HASH_TYPE(tpa_start);
  951. tpa_info->hash_type = PKT_HASH_TYPE_L4;
  952. tpa_info->gso_type = SKB_GSO_TCPV4;
  953. /* RSS profiles 1 and 3 with extract code 0 for inner 4-tuple */
  954. if (hash_type == 3 || TPA_START_IS_IPV6(tpa_start1))
  955. tpa_info->gso_type = SKB_GSO_TCPV6;
  956. tpa_info->rss_hash =
  957. le32_to_cpu(tpa_start->rx_tpa_start_cmp_rss_hash);
  958. } else {
  959. tpa_info->hash_type = PKT_HASH_TYPE_NONE;
  960. tpa_info->gso_type = 0;
  961. if (netif_msg_rx_err(bp))
  962. netdev_warn(bp->dev, "TPA packet without valid hash\n");
  963. }
  964. tpa_info->flags2 = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_flags2);
  965. tpa_info->metadata = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_metadata);
  966. tpa_info->hdr_info = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_hdr_info);
  967. rxr->rx_prod = NEXT_RX(prod);
  968. cons = NEXT_RX(cons);
  969. rxr->rx_next_cons = NEXT_RX(cons);
  970. cons_rx_buf = &rxr->rx_buf_ring[cons];
  971. bnxt_reuse_rx_data(rxr, cons, cons_rx_buf->data);
  972. rxr->rx_prod = NEXT_RX(rxr->rx_prod);
  973. cons_rx_buf->data = NULL;
  974. }
  975. static void bnxt_abort_tpa(struct bnxt *bp, struct bnxt_napi *bnapi,
  976. u16 cp_cons, u32 agg_bufs)
  977. {
  978. if (agg_bufs)
  979. bnxt_reuse_rx_agg_bufs(bnapi, cp_cons, agg_bufs);
  980. }
  981. static struct sk_buff *bnxt_gro_func_5731x(struct bnxt_tpa_info *tpa_info,
  982. int payload_off, int tcp_ts,
  983. struct sk_buff *skb)
  984. {
  985. #ifdef CONFIG_INET
  986. struct tcphdr *th;
  987. int len, nw_off;
  988. u16 outer_ip_off, inner_ip_off, inner_mac_off;
  989. u32 hdr_info = tpa_info->hdr_info;
  990. bool loopback = false;
  991. inner_ip_off = BNXT_TPA_INNER_L3_OFF(hdr_info);
  992. inner_mac_off = BNXT_TPA_INNER_L2_OFF(hdr_info);
  993. outer_ip_off = BNXT_TPA_OUTER_L3_OFF(hdr_info);
  994. /* If the packet is an internal loopback packet, the offsets will
  995. * have an extra 4 bytes.
  996. */
  997. if (inner_mac_off == 4) {
  998. loopback = true;
  999. } else if (inner_mac_off > 4) {
  1000. __be16 proto = *((__be16 *)(skb->data + inner_ip_off -
  1001. ETH_HLEN - 2));
  1002. /* We only support inner iPv4/ipv6. If we don't see the
  1003. * correct protocol ID, it must be a loopback packet where
  1004. * the offsets are off by 4.
  1005. */
  1006. if (proto != htons(ETH_P_IP) && proto != htons(ETH_P_IPV6))
  1007. loopback = true;
  1008. }
  1009. if (loopback) {
  1010. /* internal loopback packet, subtract all offsets by 4 */
  1011. inner_ip_off -= 4;
  1012. inner_mac_off -= 4;
  1013. outer_ip_off -= 4;
  1014. }
  1015. nw_off = inner_ip_off - ETH_HLEN;
  1016. skb_set_network_header(skb, nw_off);
  1017. if (tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_IP_TYPE) {
  1018. struct ipv6hdr *iph = ipv6_hdr(skb);
  1019. skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr));
  1020. len = skb->len - skb_transport_offset(skb);
  1021. th = tcp_hdr(skb);
  1022. th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0);
  1023. } else {
  1024. struct iphdr *iph = ip_hdr(skb);
  1025. skb_set_transport_header(skb, nw_off + sizeof(struct iphdr));
  1026. len = skb->len - skb_transport_offset(skb);
  1027. th = tcp_hdr(skb);
  1028. th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0);
  1029. }
  1030. if (inner_mac_off) { /* tunnel */
  1031. struct udphdr *uh = NULL;
  1032. __be16 proto = *((__be16 *)(skb->data + outer_ip_off -
  1033. ETH_HLEN - 2));
  1034. if (proto == htons(ETH_P_IP)) {
  1035. struct iphdr *iph = (struct iphdr *)skb->data;
  1036. if (iph->protocol == IPPROTO_UDP)
  1037. uh = (struct udphdr *)(iph + 1);
  1038. } else {
  1039. struct ipv6hdr *iph = (struct ipv6hdr *)skb->data;
  1040. if (iph->nexthdr == IPPROTO_UDP)
  1041. uh = (struct udphdr *)(iph + 1);
  1042. }
  1043. if (uh) {
  1044. if (uh->check)
  1045. skb_shinfo(skb)->gso_type |=
  1046. SKB_GSO_UDP_TUNNEL_CSUM;
  1047. else
  1048. skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL;
  1049. }
  1050. }
  1051. #endif
  1052. return skb;
  1053. }
  1054. #define BNXT_IPV4_HDR_SIZE (sizeof(struct iphdr) + sizeof(struct tcphdr))
  1055. #define BNXT_IPV6_HDR_SIZE (sizeof(struct ipv6hdr) + sizeof(struct tcphdr))
  1056. static struct sk_buff *bnxt_gro_func_5730x(struct bnxt_tpa_info *tpa_info,
  1057. int payload_off, int tcp_ts,
  1058. struct sk_buff *skb)
  1059. {
  1060. #ifdef CONFIG_INET
  1061. struct tcphdr *th;
  1062. int len, nw_off, tcp_opt_len = 0;
  1063. if (tcp_ts)
  1064. tcp_opt_len = 12;
  1065. if (tpa_info->gso_type == SKB_GSO_TCPV4) {
  1066. struct iphdr *iph;
  1067. nw_off = payload_off - BNXT_IPV4_HDR_SIZE - tcp_opt_len -
  1068. ETH_HLEN;
  1069. skb_set_network_header(skb, nw_off);
  1070. iph = ip_hdr(skb);
  1071. skb_set_transport_header(skb, nw_off + sizeof(struct iphdr));
  1072. len = skb->len - skb_transport_offset(skb);
  1073. th = tcp_hdr(skb);
  1074. th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0);
  1075. } else if (tpa_info->gso_type == SKB_GSO_TCPV6) {
  1076. struct ipv6hdr *iph;
  1077. nw_off = payload_off - BNXT_IPV6_HDR_SIZE - tcp_opt_len -
  1078. ETH_HLEN;
  1079. skb_set_network_header(skb, nw_off);
  1080. iph = ipv6_hdr(skb);
  1081. skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr));
  1082. len = skb->len - skb_transport_offset(skb);
  1083. th = tcp_hdr(skb);
  1084. th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0);
  1085. } else {
  1086. dev_kfree_skb_any(skb);
  1087. return NULL;
  1088. }
  1089. if (nw_off) { /* tunnel */
  1090. struct udphdr *uh = NULL;
  1091. if (skb->protocol == htons(ETH_P_IP)) {
  1092. struct iphdr *iph = (struct iphdr *)skb->data;
  1093. if (iph->protocol == IPPROTO_UDP)
  1094. uh = (struct udphdr *)(iph + 1);
  1095. } else {
  1096. struct ipv6hdr *iph = (struct ipv6hdr *)skb->data;
  1097. if (iph->nexthdr == IPPROTO_UDP)
  1098. uh = (struct udphdr *)(iph + 1);
  1099. }
  1100. if (uh) {
  1101. if (uh->check)
  1102. skb_shinfo(skb)->gso_type |=
  1103. SKB_GSO_UDP_TUNNEL_CSUM;
  1104. else
  1105. skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL;
  1106. }
  1107. }
  1108. #endif
  1109. return skb;
  1110. }
  1111. static inline struct sk_buff *bnxt_gro_skb(struct bnxt *bp,
  1112. struct bnxt_tpa_info *tpa_info,
  1113. struct rx_tpa_end_cmp *tpa_end,
  1114. struct rx_tpa_end_cmp_ext *tpa_end1,
  1115. struct sk_buff *skb)
  1116. {
  1117. #ifdef CONFIG_INET
  1118. int payload_off;
  1119. u16 segs;
  1120. segs = TPA_END_TPA_SEGS(tpa_end);
  1121. if (segs == 1)
  1122. return skb;
  1123. NAPI_GRO_CB(skb)->count = segs;
  1124. skb_shinfo(skb)->gso_size =
  1125. le32_to_cpu(tpa_end1->rx_tpa_end_cmp_seg_len);
  1126. skb_shinfo(skb)->gso_type = tpa_info->gso_type;
  1127. payload_off = (le32_to_cpu(tpa_end->rx_tpa_end_cmp_misc_v1) &
  1128. RX_TPA_END_CMP_PAYLOAD_OFFSET) >>
  1129. RX_TPA_END_CMP_PAYLOAD_OFFSET_SHIFT;
  1130. skb = bp->gro_func(tpa_info, payload_off, TPA_END_GRO_TS(tpa_end), skb);
  1131. if (likely(skb))
  1132. tcp_gro_complete(skb);
  1133. #endif
  1134. return skb;
  1135. }
  1136. /* Given the cfa_code of a received packet determine which
  1137. * netdev (vf-rep or PF) the packet is destined to.
  1138. */
  1139. static struct net_device *bnxt_get_pkt_dev(struct bnxt *bp, u16 cfa_code)
  1140. {
  1141. struct net_device *dev = bnxt_get_vf_rep(bp, cfa_code);
  1142. /* if vf-rep dev is NULL, the must belongs to the PF */
  1143. return dev ? dev : bp->dev;
  1144. }
  1145. static inline struct sk_buff *bnxt_tpa_end(struct bnxt *bp,
  1146. struct bnxt_napi *bnapi,
  1147. u32 *raw_cons,
  1148. struct rx_tpa_end_cmp *tpa_end,
  1149. struct rx_tpa_end_cmp_ext *tpa_end1,
  1150. u8 *event)
  1151. {
  1152. struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
  1153. struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
  1154. u8 agg_id = TPA_END_AGG_ID(tpa_end);
  1155. u8 *data_ptr, agg_bufs;
  1156. u16 cp_cons = RING_CMP(*raw_cons);
  1157. unsigned int len;
  1158. struct bnxt_tpa_info *tpa_info;
  1159. dma_addr_t mapping;
  1160. struct sk_buff *skb;
  1161. void *data;
  1162. if (unlikely(bnapi->in_reset)) {
  1163. int rc = bnxt_discard_rx(bp, bnapi, raw_cons, tpa_end);
  1164. if (rc < 0)
  1165. return ERR_PTR(-EBUSY);
  1166. return NULL;
  1167. }
  1168. tpa_info = &rxr->rx_tpa[agg_id];
  1169. data = tpa_info->data;
  1170. data_ptr = tpa_info->data_ptr;
  1171. prefetch(data_ptr);
  1172. len = tpa_info->len;
  1173. mapping = tpa_info->mapping;
  1174. agg_bufs = (le32_to_cpu(tpa_end->rx_tpa_end_cmp_misc_v1) &
  1175. RX_TPA_END_CMP_AGG_BUFS) >> RX_TPA_END_CMP_AGG_BUFS_SHIFT;
  1176. if (agg_bufs) {
  1177. if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, raw_cons))
  1178. return ERR_PTR(-EBUSY);
  1179. *event |= BNXT_AGG_EVENT;
  1180. cp_cons = NEXT_CMP(cp_cons);
  1181. }
  1182. if (unlikely(agg_bufs > MAX_SKB_FRAGS || TPA_END_ERRORS(tpa_end1))) {
  1183. bnxt_abort_tpa(bp, bnapi, cp_cons, agg_bufs);
  1184. if (agg_bufs > MAX_SKB_FRAGS)
  1185. netdev_warn(bp->dev, "TPA frags %d exceeded MAX_SKB_FRAGS %d\n",
  1186. agg_bufs, (int)MAX_SKB_FRAGS);
  1187. return NULL;
  1188. }
  1189. if (len <= bp->rx_copy_thresh) {
  1190. skb = bnxt_copy_skb(bnapi, data_ptr, len, mapping);
  1191. if (!skb) {
  1192. bnxt_abort_tpa(bp, bnapi, cp_cons, agg_bufs);
  1193. return NULL;
  1194. }
  1195. } else {
  1196. u8 *new_data;
  1197. dma_addr_t new_mapping;
  1198. new_data = __bnxt_alloc_rx_data(bp, &new_mapping, GFP_ATOMIC);
  1199. if (!new_data) {
  1200. bnxt_abort_tpa(bp, bnapi, cp_cons, agg_bufs);
  1201. return NULL;
  1202. }
  1203. tpa_info->data = new_data;
  1204. tpa_info->data_ptr = new_data + bp->rx_offset;
  1205. tpa_info->mapping = new_mapping;
  1206. skb = build_skb(data, 0);
  1207. dma_unmap_single_attrs(&bp->pdev->dev, mapping,
  1208. bp->rx_buf_use_size, bp->rx_dir,
  1209. DMA_ATTR_WEAK_ORDERING);
  1210. if (!skb) {
  1211. kfree(data);
  1212. bnxt_abort_tpa(bp, bnapi, cp_cons, agg_bufs);
  1213. return NULL;
  1214. }
  1215. skb_reserve(skb, bp->rx_offset);
  1216. skb_put(skb, len);
  1217. }
  1218. if (agg_bufs) {
  1219. skb = bnxt_rx_pages(bp, bnapi, skb, cp_cons, agg_bufs);
  1220. if (!skb) {
  1221. /* Page reuse already handled by bnxt_rx_pages(). */
  1222. return NULL;
  1223. }
  1224. }
  1225. skb->protocol =
  1226. eth_type_trans(skb, bnxt_get_pkt_dev(bp, tpa_info->cfa_code));
  1227. if (tpa_info->hash_type != PKT_HASH_TYPE_NONE)
  1228. skb_set_hash(skb, tpa_info->rss_hash, tpa_info->hash_type);
  1229. if ((tpa_info->flags2 & RX_CMP_FLAGS2_META_FORMAT_VLAN) &&
  1230. (skb->dev->features & NETIF_F_HW_VLAN_CTAG_RX)) {
  1231. u16 vlan_proto = tpa_info->metadata >>
  1232. RX_CMP_FLAGS2_METADATA_TPID_SFT;
  1233. u16 vtag = tpa_info->metadata & RX_CMP_FLAGS2_METADATA_TCI_MASK;
  1234. __vlan_hwaccel_put_tag(skb, htons(vlan_proto), vtag);
  1235. }
  1236. skb_checksum_none_assert(skb);
  1237. if (likely(tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_L4_CS_CALC)) {
  1238. skb->ip_summed = CHECKSUM_UNNECESSARY;
  1239. skb->csum_level =
  1240. (tpa_info->flags2 & RX_CMP_FLAGS2_T_L4_CS_CALC) >> 3;
  1241. }
  1242. if (TPA_END_GRO(tpa_end))
  1243. skb = bnxt_gro_skb(bp, tpa_info, tpa_end, tpa_end1, skb);
  1244. return skb;
  1245. }
  1246. static void bnxt_deliver_skb(struct bnxt *bp, struct bnxt_napi *bnapi,
  1247. struct sk_buff *skb)
  1248. {
  1249. if (skb->dev != bp->dev) {
  1250. /* this packet belongs to a vf-rep */
  1251. bnxt_vf_rep_rx(bp, skb);
  1252. return;
  1253. }
  1254. skb_record_rx_queue(skb, bnapi->index);
  1255. napi_gro_receive(&bnapi->napi, skb);
  1256. }
  1257. /* returns the following:
  1258. * 1 - 1 packet successfully received
  1259. * 0 - successful TPA_START, packet not completed yet
  1260. * -EBUSY - completion ring does not have all the agg buffers yet
  1261. * -ENOMEM - packet aborted due to out of memory
  1262. * -EIO - packet aborted due to hw error indicated in BD
  1263. */
  1264. static int bnxt_rx_pkt(struct bnxt *bp, struct bnxt_napi *bnapi, u32 *raw_cons,
  1265. u8 *event)
  1266. {
  1267. struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
  1268. struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
  1269. struct net_device *dev = bp->dev;
  1270. struct rx_cmp *rxcmp;
  1271. struct rx_cmp_ext *rxcmp1;
  1272. u32 tmp_raw_cons = *raw_cons;
  1273. u16 cfa_code, cons, prod, cp_cons = RING_CMP(tmp_raw_cons);
  1274. struct bnxt_sw_rx_bd *rx_buf;
  1275. unsigned int len;
  1276. u8 *data_ptr, agg_bufs, cmp_type;
  1277. dma_addr_t dma_addr;
  1278. struct sk_buff *skb;
  1279. void *data;
  1280. int rc = 0;
  1281. u32 misc;
  1282. rxcmp = (struct rx_cmp *)
  1283. &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
  1284. tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons);
  1285. cp_cons = RING_CMP(tmp_raw_cons);
  1286. rxcmp1 = (struct rx_cmp_ext *)
  1287. &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
  1288. if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
  1289. return -EBUSY;
  1290. cmp_type = RX_CMP_TYPE(rxcmp);
  1291. prod = rxr->rx_prod;
  1292. if (cmp_type == CMP_TYPE_RX_L2_TPA_START_CMP) {
  1293. bnxt_tpa_start(bp, rxr, (struct rx_tpa_start_cmp *)rxcmp,
  1294. (struct rx_tpa_start_cmp_ext *)rxcmp1);
  1295. *event |= BNXT_RX_EVENT;
  1296. goto next_rx_no_prod_no_len;
  1297. } else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
  1298. skb = bnxt_tpa_end(bp, bnapi, &tmp_raw_cons,
  1299. (struct rx_tpa_end_cmp *)rxcmp,
  1300. (struct rx_tpa_end_cmp_ext *)rxcmp1, event);
  1301. if (IS_ERR(skb))
  1302. return -EBUSY;
  1303. rc = -ENOMEM;
  1304. if (likely(skb)) {
  1305. bnxt_deliver_skb(bp, bnapi, skb);
  1306. rc = 1;
  1307. }
  1308. *event |= BNXT_RX_EVENT;
  1309. goto next_rx_no_prod_no_len;
  1310. }
  1311. cons = rxcmp->rx_cmp_opaque;
  1312. if (unlikely(cons != rxr->rx_next_cons)) {
  1313. int rc1 = bnxt_discard_rx(bp, bnapi, raw_cons, rxcmp);
  1314. netdev_warn(bp->dev, "RX cons %x != expected cons %x\n",
  1315. cons, rxr->rx_next_cons);
  1316. bnxt_sched_reset(bp, rxr);
  1317. return rc1;
  1318. }
  1319. rx_buf = &rxr->rx_buf_ring[cons];
  1320. data = rx_buf->data;
  1321. data_ptr = rx_buf->data_ptr;
  1322. prefetch(data_ptr);
  1323. misc = le32_to_cpu(rxcmp->rx_cmp_misc_v1);
  1324. agg_bufs = (misc & RX_CMP_AGG_BUFS) >> RX_CMP_AGG_BUFS_SHIFT;
  1325. if (agg_bufs) {
  1326. if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons))
  1327. return -EBUSY;
  1328. cp_cons = NEXT_CMP(cp_cons);
  1329. *event |= BNXT_AGG_EVENT;
  1330. }
  1331. *event |= BNXT_RX_EVENT;
  1332. rx_buf->data = NULL;
  1333. if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L2_ERRORS) {
  1334. u32 rx_err = le32_to_cpu(rxcmp1->rx_cmp_cfa_code_errors_v2);
  1335. bnxt_reuse_rx_data(rxr, cons, data);
  1336. if (agg_bufs)
  1337. bnxt_reuse_rx_agg_bufs(bnapi, cp_cons, agg_bufs);
  1338. rc = -EIO;
  1339. if (rx_err & RX_CMPL_ERRORS_BUFFER_ERROR_MASK) {
  1340. netdev_warn(bp->dev, "RX buffer error %x\n", rx_err);
  1341. bnxt_sched_reset(bp, rxr);
  1342. }
  1343. goto next_rx_no_len;
  1344. }
  1345. len = le32_to_cpu(rxcmp->rx_cmp_len_flags_type) >> RX_CMP_LEN_SHIFT;
  1346. dma_addr = rx_buf->mapping;
  1347. if (bnxt_rx_xdp(bp, rxr, cons, data, &data_ptr, &len, event)) {
  1348. rc = 1;
  1349. goto next_rx;
  1350. }
  1351. if (len <= bp->rx_copy_thresh) {
  1352. skb = bnxt_copy_skb(bnapi, data_ptr, len, dma_addr);
  1353. bnxt_reuse_rx_data(rxr, cons, data);
  1354. if (!skb) {
  1355. if (agg_bufs)
  1356. bnxt_reuse_rx_agg_bufs(bnapi, cp_cons, agg_bufs);
  1357. rc = -ENOMEM;
  1358. goto next_rx;
  1359. }
  1360. } else {
  1361. u32 payload;
  1362. if (rx_buf->data_ptr == data_ptr)
  1363. payload = misc & RX_CMP_PAYLOAD_OFFSET;
  1364. else
  1365. payload = 0;
  1366. skb = bp->rx_skb_func(bp, rxr, cons, data, data_ptr, dma_addr,
  1367. payload | len);
  1368. if (!skb) {
  1369. rc = -ENOMEM;
  1370. goto next_rx;
  1371. }
  1372. }
  1373. if (agg_bufs) {
  1374. skb = bnxt_rx_pages(bp, bnapi, skb, cp_cons, agg_bufs);
  1375. if (!skb) {
  1376. rc = -ENOMEM;
  1377. goto next_rx;
  1378. }
  1379. }
  1380. if (RX_CMP_HASH_VALID(rxcmp)) {
  1381. u32 hash_type = RX_CMP_HASH_TYPE(rxcmp);
  1382. enum pkt_hash_types type = PKT_HASH_TYPE_L4;
  1383. /* RSS profiles 1 and 3 with extract code 0 for inner 4-tuple */
  1384. if (hash_type != 1 && hash_type != 3)
  1385. type = PKT_HASH_TYPE_L3;
  1386. skb_set_hash(skb, le32_to_cpu(rxcmp->rx_cmp_rss_hash), type);
  1387. }
  1388. cfa_code = RX_CMP_CFA_CODE(rxcmp1);
  1389. skb->protocol = eth_type_trans(skb, bnxt_get_pkt_dev(bp, cfa_code));
  1390. if ((rxcmp1->rx_cmp_flags2 &
  1391. cpu_to_le32(RX_CMP_FLAGS2_META_FORMAT_VLAN)) &&
  1392. (skb->dev->features & NETIF_F_HW_VLAN_CTAG_RX)) {
  1393. u32 meta_data = le32_to_cpu(rxcmp1->rx_cmp_meta_data);
  1394. u16 vtag = meta_data & RX_CMP_FLAGS2_METADATA_TCI_MASK;
  1395. u16 vlan_proto = meta_data >> RX_CMP_FLAGS2_METADATA_TPID_SFT;
  1396. __vlan_hwaccel_put_tag(skb, htons(vlan_proto), vtag);
  1397. }
  1398. skb_checksum_none_assert(skb);
  1399. if (RX_CMP_L4_CS_OK(rxcmp1)) {
  1400. if (dev->features & NETIF_F_RXCSUM) {
  1401. skb->ip_summed = CHECKSUM_UNNECESSARY;
  1402. skb->csum_level = RX_CMP_ENCAP(rxcmp1);
  1403. }
  1404. } else {
  1405. if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L4_CS_ERR_BITS) {
  1406. if (dev->features & NETIF_F_RXCSUM)
  1407. cpr->rx_l4_csum_errors++;
  1408. }
  1409. }
  1410. bnxt_deliver_skb(bp, bnapi, skb);
  1411. rc = 1;
  1412. next_rx:
  1413. cpr->rx_packets += 1;
  1414. cpr->rx_bytes += len;
  1415. next_rx_no_len:
  1416. rxr->rx_prod = NEXT_RX(prod);
  1417. rxr->rx_next_cons = NEXT_RX(cons);
  1418. next_rx_no_prod_no_len:
  1419. *raw_cons = tmp_raw_cons;
  1420. return rc;
  1421. }
  1422. /* In netpoll mode, if we are using a combined completion ring, we need to
  1423. * discard the rx packets and recycle the buffers.
  1424. */
  1425. static int bnxt_force_rx_discard(struct bnxt *bp, struct bnxt_napi *bnapi,
  1426. u32 *raw_cons, u8 *event)
  1427. {
  1428. struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
  1429. u32 tmp_raw_cons = *raw_cons;
  1430. struct rx_cmp_ext *rxcmp1;
  1431. struct rx_cmp *rxcmp;
  1432. u16 cp_cons;
  1433. u8 cmp_type;
  1434. cp_cons = RING_CMP(tmp_raw_cons);
  1435. rxcmp = (struct rx_cmp *)
  1436. &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
  1437. tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons);
  1438. cp_cons = RING_CMP(tmp_raw_cons);
  1439. rxcmp1 = (struct rx_cmp_ext *)
  1440. &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
  1441. if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
  1442. return -EBUSY;
  1443. cmp_type = RX_CMP_TYPE(rxcmp);
  1444. if (cmp_type == CMP_TYPE_RX_L2_CMP) {
  1445. rxcmp1->rx_cmp_cfa_code_errors_v2 |=
  1446. cpu_to_le32(RX_CMPL_ERRORS_CRC_ERROR);
  1447. } else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
  1448. struct rx_tpa_end_cmp_ext *tpa_end1;
  1449. tpa_end1 = (struct rx_tpa_end_cmp_ext *)rxcmp1;
  1450. tpa_end1->rx_tpa_end_cmp_errors_v2 |=
  1451. cpu_to_le32(RX_TPA_END_CMP_ERRORS);
  1452. }
  1453. return bnxt_rx_pkt(bp, bnapi, raw_cons, event);
  1454. }
  1455. #define BNXT_GET_EVENT_PORT(data) \
  1456. ((data) & \
  1457. ASYNC_EVENT_CMPL_PORT_CONN_NOT_ALLOWED_EVENT_DATA1_PORT_ID_MASK)
  1458. static int bnxt_async_event_process(struct bnxt *bp,
  1459. struct hwrm_async_event_cmpl *cmpl)
  1460. {
  1461. u16 event_id = le16_to_cpu(cmpl->event_id);
  1462. /* TODO CHIMP_FW: Define event id's for link change, error etc */
  1463. switch (event_id) {
  1464. case ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE: {
  1465. u32 data1 = le32_to_cpu(cmpl->event_data1);
  1466. struct bnxt_link_info *link_info = &bp->link_info;
  1467. if (BNXT_VF(bp))
  1468. goto async_event_process_exit;
  1469. /* print unsupported speed warning in forced speed mode only */
  1470. if (!(link_info->autoneg & BNXT_AUTONEG_SPEED) &&
  1471. (data1 & 0x20000)) {
  1472. u16 fw_speed = link_info->force_link_speed;
  1473. u32 speed = bnxt_fw_to_ethtool_speed(fw_speed);
  1474. if (speed != SPEED_UNKNOWN)
  1475. netdev_warn(bp->dev, "Link speed %d no longer supported\n",
  1476. speed);
  1477. }
  1478. set_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT, &bp->sp_event);
  1479. }
  1480. /* fall through */
  1481. case ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE:
  1482. set_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event);
  1483. break;
  1484. case ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD:
  1485. set_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event);
  1486. break;
  1487. case ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED: {
  1488. u32 data1 = le32_to_cpu(cmpl->event_data1);
  1489. u16 port_id = BNXT_GET_EVENT_PORT(data1);
  1490. if (BNXT_VF(bp))
  1491. break;
  1492. if (bp->pf.port_id != port_id)
  1493. break;
  1494. set_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event);
  1495. break;
  1496. }
  1497. case ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE:
  1498. if (BNXT_PF(bp))
  1499. goto async_event_process_exit;
  1500. set_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event);
  1501. break;
  1502. default:
  1503. goto async_event_process_exit;
  1504. }
  1505. bnxt_queue_sp_work(bp);
  1506. async_event_process_exit:
  1507. bnxt_ulp_async_events(bp, cmpl);
  1508. return 0;
  1509. }
  1510. static int bnxt_hwrm_handler(struct bnxt *bp, struct tx_cmp *txcmp)
  1511. {
  1512. u16 cmpl_type = TX_CMP_TYPE(txcmp), vf_id, seq_id;
  1513. struct hwrm_cmpl *h_cmpl = (struct hwrm_cmpl *)txcmp;
  1514. struct hwrm_fwd_req_cmpl *fwd_req_cmpl =
  1515. (struct hwrm_fwd_req_cmpl *)txcmp;
  1516. switch (cmpl_type) {
  1517. case CMPL_BASE_TYPE_HWRM_DONE:
  1518. seq_id = le16_to_cpu(h_cmpl->sequence_id);
  1519. if (seq_id == bp->hwrm_intr_seq_id)
  1520. bp->hwrm_intr_seq_id = HWRM_SEQ_ID_INVALID;
  1521. else
  1522. netdev_err(bp->dev, "Invalid hwrm seq id %d\n", seq_id);
  1523. break;
  1524. case CMPL_BASE_TYPE_HWRM_FWD_REQ:
  1525. vf_id = le16_to_cpu(fwd_req_cmpl->source_id);
  1526. if ((vf_id < bp->pf.first_vf_id) ||
  1527. (vf_id >= bp->pf.first_vf_id + bp->pf.active_vfs)) {
  1528. netdev_err(bp->dev, "Msg contains invalid VF id %x\n",
  1529. vf_id);
  1530. return -EINVAL;
  1531. }
  1532. set_bit(vf_id - bp->pf.first_vf_id, bp->pf.vf_event_bmap);
  1533. set_bit(BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT, &bp->sp_event);
  1534. bnxt_queue_sp_work(bp);
  1535. break;
  1536. case CMPL_BASE_TYPE_HWRM_ASYNC_EVENT:
  1537. bnxt_async_event_process(bp,
  1538. (struct hwrm_async_event_cmpl *)txcmp);
  1539. default:
  1540. break;
  1541. }
  1542. return 0;
  1543. }
  1544. static irqreturn_t bnxt_msix(int irq, void *dev_instance)
  1545. {
  1546. struct bnxt_napi *bnapi = dev_instance;
  1547. struct bnxt *bp = bnapi->bp;
  1548. struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
  1549. u32 cons = RING_CMP(cpr->cp_raw_cons);
  1550. cpr->event_ctr++;
  1551. prefetch(&cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)]);
  1552. napi_schedule(&bnapi->napi);
  1553. return IRQ_HANDLED;
  1554. }
  1555. static inline int bnxt_has_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr)
  1556. {
  1557. u32 raw_cons = cpr->cp_raw_cons;
  1558. u16 cons = RING_CMP(raw_cons);
  1559. struct tx_cmp *txcmp;
  1560. txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
  1561. return TX_CMP_VALID(txcmp, raw_cons);
  1562. }
  1563. static irqreturn_t bnxt_inta(int irq, void *dev_instance)
  1564. {
  1565. struct bnxt_napi *bnapi = dev_instance;
  1566. struct bnxt *bp = bnapi->bp;
  1567. struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
  1568. u32 cons = RING_CMP(cpr->cp_raw_cons);
  1569. u32 int_status;
  1570. prefetch(&cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)]);
  1571. if (!bnxt_has_work(bp, cpr)) {
  1572. int_status = readl(bp->bar0 + BNXT_CAG_REG_LEGACY_INT_STATUS);
  1573. /* return if erroneous interrupt */
  1574. if (!(int_status & (0x10000 << cpr->cp_ring_struct.fw_ring_id)))
  1575. return IRQ_NONE;
  1576. }
  1577. /* disable ring IRQ */
  1578. BNXT_CP_DB_IRQ_DIS(cpr->cp_doorbell);
  1579. /* Return here if interrupt is shared and is disabled. */
  1580. if (unlikely(atomic_read(&bp->intr_sem) != 0))
  1581. return IRQ_HANDLED;
  1582. napi_schedule(&bnapi->napi);
  1583. return IRQ_HANDLED;
  1584. }
  1585. static int bnxt_poll_work(struct bnxt *bp, struct bnxt_napi *bnapi, int budget)
  1586. {
  1587. struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
  1588. u32 raw_cons = cpr->cp_raw_cons;
  1589. u32 cons;
  1590. int tx_pkts = 0;
  1591. int rx_pkts = 0;
  1592. u8 event = 0;
  1593. struct tx_cmp *txcmp;
  1594. while (1) {
  1595. int rc;
  1596. cons = RING_CMP(raw_cons);
  1597. txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
  1598. if (!TX_CMP_VALID(txcmp, raw_cons))
  1599. break;
  1600. /* The valid test of the entry must be done first before
  1601. * reading any further.
  1602. */
  1603. dma_rmb();
  1604. if (TX_CMP_TYPE(txcmp) == CMP_TYPE_TX_L2_CMP) {
  1605. tx_pkts++;
  1606. /* return full budget so NAPI will complete. */
  1607. if (unlikely(tx_pkts > bp->tx_wake_thresh)) {
  1608. rx_pkts = budget;
  1609. raw_cons = NEXT_RAW_CMP(raw_cons);
  1610. break;
  1611. }
  1612. } else if ((TX_CMP_TYPE(txcmp) & 0x30) == 0x10) {
  1613. if (likely(budget))
  1614. rc = bnxt_rx_pkt(bp, bnapi, &raw_cons, &event);
  1615. else
  1616. rc = bnxt_force_rx_discard(bp, bnapi, &raw_cons,
  1617. &event);
  1618. if (likely(rc >= 0))
  1619. rx_pkts += rc;
  1620. /* Increment rx_pkts when rc is -ENOMEM to count towards
  1621. * the NAPI budget. Otherwise, we may potentially loop
  1622. * here forever if we consistently cannot allocate
  1623. * buffers.
  1624. */
  1625. else if (rc == -ENOMEM && budget)
  1626. rx_pkts++;
  1627. else if (rc == -EBUSY) /* partial completion */
  1628. break;
  1629. } else if (unlikely((TX_CMP_TYPE(txcmp) ==
  1630. CMPL_BASE_TYPE_HWRM_DONE) ||
  1631. (TX_CMP_TYPE(txcmp) ==
  1632. CMPL_BASE_TYPE_HWRM_FWD_REQ) ||
  1633. (TX_CMP_TYPE(txcmp) ==
  1634. CMPL_BASE_TYPE_HWRM_ASYNC_EVENT))) {
  1635. bnxt_hwrm_handler(bp, txcmp);
  1636. }
  1637. raw_cons = NEXT_RAW_CMP(raw_cons);
  1638. if (rx_pkts && rx_pkts == budget)
  1639. break;
  1640. }
  1641. if (event & BNXT_TX_EVENT) {
  1642. struct bnxt_tx_ring_info *txr = bnapi->tx_ring;
  1643. void __iomem *db = txr->tx_doorbell;
  1644. u16 prod = txr->tx_prod;
  1645. /* Sync BD data before updating doorbell */
  1646. wmb();
  1647. bnxt_db_write_relaxed(bp, db, DB_KEY_TX | prod);
  1648. }
  1649. cpr->cp_raw_cons = raw_cons;
  1650. /* ACK completion ring before freeing tx ring and producing new
  1651. * buffers in rx/agg rings to prevent overflowing the completion
  1652. * ring.
  1653. */
  1654. BNXT_CP_DB(cpr->cp_doorbell, cpr->cp_raw_cons);
  1655. if (tx_pkts)
  1656. bnapi->tx_int(bp, bnapi, tx_pkts);
  1657. if (event & BNXT_RX_EVENT) {
  1658. struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
  1659. bnxt_db_write(bp, rxr->rx_doorbell, DB_KEY_RX | rxr->rx_prod);
  1660. if (event & BNXT_AGG_EVENT)
  1661. bnxt_db_write(bp, rxr->rx_agg_doorbell,
  1662. DB_KEY_RX | rxr->rx_agg_prod);
  1663. }
  1664. return rx_pkts;
  1665. }
  1666. static int bnxt_poll_nitroa0(struct napi_struct *napi, int budget)
  1667. {
  1668. struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
  1669. struct bnxt *bp = bnapi->bp;
  1670. struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
  1671. struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
  1672. struct tx_cmp *txcmp;
  1673. struct rx_cmp_ext *rxcmp1;
  1674. u32 cp_cons, tmp_raw_cons;
  1675. u32 raw_cons = cpr->cp_raw_cons;
  1676. u32 rx_pkts = 0;
  1677. u8 event = 0;
  1678. while (1) {
  1679. int rc;
  1680. cp_cons = RING_CMP(raw_cons);
  1681. txcmp = &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
  1682. if (!TX_CMP_VALID(txcmp, raw_cons))
  1683. break;
  1684. if ((TX_CMP_TYPE(txcmp) & 0x30) == 0x10) {
  1685. tmp_raw_cons = NEXT_RAW_CMP(raw_cons);
  1686. cp_cons = RING_CMP(tmp_raw_cons);
  1687. rxcmp1 = (struct rx_cmp_ext *)
  1688. &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
  1689. if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
  1690. break;
  1691. /* force an error to recycle the buffer */
  1692. rxcmp1->rx_cmp_cfa_code_errors_v2 |=
  1693. cpu_to_le32(RX_CMPL_ERRORS_CRC_ERROR);
  1694. rc = bnxt_rx_pkt(bp, bnapi, &raw_cons, &event);
  1695. if (likely(rc == -EIO) && budget)
  1696. rx_pkts++;
  1697. else if (rc == -EBUSY) /* partial completion */
  1698. break;
  1699. } else if (unlikely(TX_CMP_TYPE(txcmp) ==
  1700. CMPL_BASE_TYPE_HWRM_DONE)) {
  1701. bnxt_hwrm_handler(bp, txcmp);
  1702. } else {
  1703. netdev_err(bp->dev,
  1704. "Invalid completion received on special ring\n");
  1705. }
  1706. raw_cons = NEXT_RAW_CMP(raw_cons);
  1707. if (rx_pkts == budget)
  1708. break;
  1709. }
  1710. cpr->cp_raw_cons = raw_cons;
  1711. BNXT_CP_DB(cpr->cp_doorbell, cpr->cp_raw_cons);
  1712. bnxt_db_write(bp, rxr->rx_doorbell, DB_KEY_RX | rxr->rx_prod);
  1713. if (event & BNXT_AGG_EVENT)
  1714. bnxt_db_write(bp, rxr->rx_agg_doorbell,
  1715. DB_KEY_RX | rxr->rx_agg_prod);
  1716. if (!bnxt_has_work(bp, cpr) && rx_pkts < budget) {
  1717. napi_complete_done(napi, rx_pkts);
  1718. BNXT_CP_DB_REARM(cpr->cp_doorbell, cpr->cp_raw_cons);
  1719. }
  1720. return rx_pkts;
  1721. }
  1722. static int bnxt_poll(struct napi_struct *napi, int budget)
  1723. {
  1724. struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
  1725. struct bnxt *bp = bnapi->bp;
  1726. struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
  1727. int work_done = 0;
  1728. while (1) {
  1729. work_done += bnxt_poll_work(bp, bnapi, budget - work_done);
  1730. if (work_done >= budget) {
  1731. if (!budget)
  1732. BNXT_CP_DB_REARM(cpr->cp_doorbell,
  1733. cpr->cp_raw_cons);
  1734. break;
  1735. }
  1736. if (!bnxt_has_work(bp, cpr)) {
  1737. if (napi_complete_done(napi, work_done))
  1738. BNXT_CP_DB_REARM(cpr->cp_doorbell,
  1739. cpr->cp_raw_cons);
  1740. break;
  1741. }
  1742. }
  1743. if (bp->flags & BNXT_FLAG_DIM) {
  1744. struct net_dim_sample dim_sample;
  1745. net_dim_sample(cpr->event_ctr,
  1746. cpr->rx_packets,
  1747. cpr->rx_bytes,
  1748. &dim_sample);
  1749. net_dim(&cpr->dim, dim_sample);
  1750. }
  1751. mmiowb();
  1752. return work_done;
  1753. }
  1754. static void bnxt_free_tx_skbs(struct bnxt *bp)
  1755. {
  1756. int i, max_idx;
  1757. struct pci_dev *pdev = bp->pdev;
  1758. if (!bp->tx_ring)
  1759. return;
  1760. max_idx = bp->tx_nr_pages * TX_DESC_CNT;
  1761. for (i = 0; i < bp->tx_nr_rings; i++) {
  1762. struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
  1763. int j;
  1764. for (j = 0; j < max_idx;) {
  1765. struct bnxt_sw_tx_bd *tx_buf = &txr->tx_buf_ring[j];
  1766. struct sk_buff *skb = tx_buf->skb;
  1767. int k, last;
  1768. if (!skb) {
  1769. j++;
  1770. continue;
  1771. }
  1772. tx_buf->skb = NULL;
  1773. if (tx_buf->is_push) {
  1774. dev_kfree_skb(skb);
  1775. j += 2;
  1776. continue;
  1777. }
  1778. dma_unmap_single(&pdev->dev,
  1779. dma_unmap_addr(tx_buf, mapping),
  1780. skb_headlen(skb),
  1781. PCI_DMA_TODEVICE);
  1782. last = tx_buf->nr_frags;
  1783. j += 2;
  1784. for (k = 0; k < last; k++, j++) {
  1785. int ring_idx = j & bp->tx_ring_mask;
  1786. skb_frag_t *frag = &skb_shinfo(skb)->frags[k];
  1787. tx_buf = &txr->tx_buf_ring[ring_idx];
  1788. dma_unmap_page(
  1789. &pdev->dev,
  1790. dma_unmap_addr(tx_buf, mapping),
  1791. skb_frag_size(frag), PCI_DMA_TODEVICE);
  1792. }
  1793. dev_kfree_skb(skb);
  1794. }
  1795. netdev_tx_reset_queue(netdev_get_tx_queue(bp->dev, i));
  1796. }
  1797. }
  1798. static void bnxt_free_rx_skbs(struct bnxt *bp)
  1799. {
  1800. int i, max_idx, max_agg_idx;
  1801. struct pci_dev *pdev = bp->pdev;
  1802. if (!bp->rx_ring)
  1803. return;
  1804. max_idx = bp->rx_nr_pages * RX_DESC_CNT;
  1805. max_agg_idx = bp->rx_agg_nr_pages * RX_DESC_CNT;
  1806. for (i = 0; i < bp->rx_nr_rings; i++) {
  1807. struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
  1808. int j;
  1809. if (rxr->rx_tpa) {
  1810. for (j = 0; j < MAX_TPA; j++) {
  1811. struct bnxt_tpa_info *tpa_info =
  1812. &rxr->rx_tpa[j];
  1813. u8 *data = tpa_info->data;
  1814. if (!data)
  1815. continue;
  1816. dma_unmap_single_attrs(&pdev->dev,
  1817. tpa_info->mapping,
  1818. bp->rx_buf_use_size,
  1819. bp->rx_dir,
  1820. DMA_ATTR_WEAK_ORDERING);
  1821. tpa_info->data = NULL;
  1822. kfree(data);
  1823. }
  1824. }
  1825. for (j = 0; j < max_idx; j++) {
  1826. struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[j];
  1827. dma_addr_t mapping = rx_buf->mapping;
  1828. void *data = rx_buf->data;
  1829. if (!data)
  1830. continue;
  1831. rx_buf->data = NULL;
  1832. if (BNXT_RX_PAGE_MODE(bp)) {
  1833. mapping -= bp->rx_dma_offset;
  1834. dma_unmap_page_attrs(&pdev->dev, mapping,
  1835. PAGE_SIZE, bp->rx_dir,
  1836. DMA_ATTR_WEAK_ORDERING);
  1837. __free_page(data);
  1838. } else {
  1839. dma_unmap_single_attrs(&pdev->dev, mapping,
  1840. bp->rx_buf_use_size,
  1841. bp->rx_dir,
  1842. DMA_ATTR_WEAK_ORDERING);
  1843. kfree(data);
  1844. }
  1845. }
  1846. for (j = 0; j < max_agg_idx; j++) {
  1847. struct bnxt_sw_rx_agg_bd *rx_agg_buf =
  1848. &rxr->rx_agg_ring[j];
  1849. struct page *page = rx_agg_buf->page;
  1850. if (!page)
  1851. continue;
  1852. dma_unmap_page_attrs(&pdev->dev, rx_agg_buf->mapping,
  1853. BNXT_RX_PAGE_SIZE,
  1854. PCI_DMA_FROMDEVICE,
  1855. DMA_ATTR_WEAK_ORDERING);
  1856. rx_agg_buf->page = NULL;
  1857. __clear_bit(j, rxr->rx_agg_bmap);
  1858. __free_page(page);
  1859. }
  1860. if (rxr->rx_page) {
  1861. __free_page(rxr->rx_page);
  1862. rxr->rx_page = NULL;
  1863. }
  1864. }
  1865. }
  1866. static void bnxt_free_skbs(struct bnxt *bp)
  1867. {
  1868. bnxt_free_tx_skbs(bp);
  1869. bnxt_free_rx_skbs(bp);
  1870. }
  1871. static void bnxt_free_ring(struct bnxt *bp, struct bnxt_ring_struct *ring)
  1872. {
  1873. struct pci_dev *pdev = bp->pdev;
  1874. int i;
  1875. for (i = 0; i < ring->nr_pages; i++) {
  1876. if (!ring->pg_arr[i])
  1877. continue;
  1878. dma_free_coherent(&pdev->dev, ring->page_size,
  1879. ring->pg_arr[i], ring->dma_arr[i]);
  1880. ring->pg_arr[i] = NULL;
  1881. }
  1882. if (ring->pg_tbl) {
  1883. dma_free_coherent(&pdev->dev, ring->nr_pages * 8,
  1884. ring->pg_tbl, ring->pg_tbl_map);
  1885. ring->pg_tbl = NULL;
  1886. }
  1887. if (ring->vmem_size && *ring->vmem) {
  1888. vfree(*ring->vmem);
  1889. *ring->vmem = NULL;
  1890. }
  1891. }
  1892. static int bnxt_alloc_ring(struct bnxt *bp, struct bnxt_ring_struct *ring)
  1893. {
  1894. int i;
  1895. struct pci_dev *pdev = bp->pdev;
  1896. if (ring->nr_pages > 1) {
  1897. ring->pg_tbl = dma_alloc_coherent(&pdev->dev,
  1898. ring->nr_pages * 8,
  1899. &ring->pg_tbl_map,
  1900. GFP_KERNEL);
  1901. if (!ring->pg_tbl)
  1902. return -ENOMEM;
  1903. }
  1904. for (i = 0; i < ring->nr_pages; i++) {
  1905. ring->pg_arr[i] = dma_alloc_coherent(&pdev->dev,
  1906. ring->page_size,
  1907. &ring->dma_arr[i],
  1908. GFP_KERNEL);
  1909. if (!ring->pg_arr[i])
  1910. return -ENOMEM;
  1911. if (ring->nr_pages > 1)
  1912. ring->pg_tbl[i] = cpu_to_le64(ring->dma_arr[i]);
  1913. }
  1914. if (ring->vmem_size) {
  1915. *ring->vmem = vzalloc(ring->vmem_size);
  1916. if (!(*ring->vmem))
  1917. return -ENOMEM;
  1918. }
  1919. return 0;
  1920. }
  1921. static void bnxt_free_rx_rings(struct bnxt *bp)
  1922. {
  1923. int i;
  1924. if (!bp->rx_ring)
  1925. return;
  1926. for (i = 0; i < bp->rx_nr_rings; i++) {
  1927. struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
  1928. struct bnxt_ring_struct *ring;
  1929. if (rxr->xdp_prog)
  1930. bpf_prog_put(rxr->xdp_prog);
  1931. if (xdp_rxq_info_is_reg(&rxr->xdp_rxq))
  1932. xdp_rxq_info_unreg(&rxr->xdp_rxq);
  1933. kfree(rxr->rx_tpa);
  1934. rxr->rx_tpa = NULL;
  1935. kfree(rxr->rx_agg_bmap);
  1936. rxr->rx_agg_bmap = NULL;
  1937. ring = &rxr->rx_ring_struct;
  1938. bnxt_free_ring(bp, ring);
  1939. ring = &rxr->rx_agg_ring_struct;
  1940. bnxt_free_ring(bp, ring);
  1941. }
  1942. }
  1943. static int bnxt_alloc_rx_rings(struct bnxt *bp)
  1944. {
  1945. int i, rc, agg_rings = 0, tpa_rings = 0;
  1946. if (!bp->rx_ring)
  1947. return -ENOMEM;
  1948. if (bp->flags & BNXT_FLAG_AGG_RINGS)
  1949. agg_rings = 1;
  1950. if (bp->flags & BNXT_FLAG_TPA)
  1951. tpa_rings = 1;
  1952. for (i = 0; i < bp->rx_nr_rings; i++) {
  1953. struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
  1954. struct bnxt_ring_struct *ring;
  1955. ring = &rxr->rx_ring_struct;
  1956. rc = xdp_rxq_info_reg(&rxr->xdp_rxq, bp->dev, i);
  1957. if (rc < 0)
  1958. return rc;
  1959. rc = bnxt_alloc_ring(bp, ring);
  1960. if (rc)
  1961. return rc;
  1962. if (agg_rings) {
  1963. u16 mem_size;
  1964. ring = &rxr->rx_agg_ring_struct;
  1965. rc = bnxt_alloc_ring(bp, ring);
  1966. if (rc)
  1967. return rc;
  1968. ring->grp_idx = i;
  1969. rxr->rx_agg_bmap_size = bp->rx_agg_ring_mask + 1;
  1970. mem_size = rxr->rx_agg_bmap_size / 8;
  1971. rxr->rx_agg_bmap = kzalloc(mem_size, GFP_KERNEL);
  1972. if (!rxr->rx_agg_bmap)
  1973. return -ENOMEM;
  1974. if (tpa_rings) {
  1975. rxr->rx_tpa = kcalloc(MAX_TPA,
  1976. sizeof(struct bnxt_tpa_info),
  1977. GFP_KERNEL);
  1978. if (!rxr->rx_tpa)
  1979. return -ENOMEM;
  1980. }
  1981. }
  1982. }
  1983. return 0;
  1984. }
  1985. static void bnxt_free_tx_rings(struct bnxt *bp)
  1986. {
  1987. int i;
  1988. struct pci_dev *pdev = bp->pdev;
  1989. if (!bp->tx_ring)
  1990. return;
  1991. for (i = 0; i < bp->tx_nr_rings; i++) {
  1992. struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
  1993. struct bnxt_ring_struct *ring;
  1994. if (txr->tx_push) {
  1995. dma_free_coherent(&pdev->dev, bp->tx_push_size,
  1996. txr->tx_push, txr->tx_push_mapping);
  1997. txr->tx_push = NULL;
  1998. }
  1999. ring = &txr->tx_ring_struct;
  2000. bnxt_free_ring(bp, ring);
  2001. }
  2002. }
  2003. static int bnxt_alloc_tx_rings(struct bnxt *bp)
  2004. {
  2005. int i, j, rc;
  2006. struct pci_dev *pdev = bp->pdev;
  2007. bp->tx_push_size = 0;
  2008. if (bp->tx_push_thresh) {
  2009. int push_size;
  2010. push_size = L1_CACHE_ALIGN(sizeof(struct tx_push_bd) +
  2011. bp->tx_push_thresh);
  2012. if (push_size > 256) {
  2013. push_size = 0;
  2014. bp->tx_push_thresh = 0;
  2015. }
  2016. bp->tx_push_size = push_size;
  2017. }
  2018. for (i = 0, j = 0; i < bp->tx_nr_rings; i++) {
  2019. struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
  2020. struct bnxt_ring_struct *ring;
  2021. u8 qidx;
  2022. ring = &txr->tx_ring_struct;
  2023. rc = bnxt_alloc_ring(bp, ring);
  2024. if (rc)
  2025. return rc;
  2026. ring->grp_idx = txr->bnapi->index;
  2027. if (bp->tx_push_size) {
  2028. dma_addr_t mapping;
  2029. /* One pre-allocated DMA buffer to backup
  2030. * TX push operation
  2031. */
  2032. txr->tx_push = dma_alloc_coherent(&pdev->dev,
  2033. bp->tx_push_size,
  2034. &txr->tx_push_mapping,
  2035. GFP_KERNEL);
  2036. if (!txr->tx_push)
  2037. return -ENOMEM;
  2038. mapping = txr->tx_push_mapping +
  2039. sizeof(struct tx_push_bd);
  2040. txr->data_mapping = cpu_to_le64(mapping);
  2041. memset(txr->tx_push, 0, sizeof(struct tx_push_bd));
  2042. }
  2043. qidx = bp->tc_to_qidx[j];
  2044. ring->queue_id = bp->q_info[qidx].queue_id;
  2045. if (i < bp->tx_nr_rings_xdp)
  2046. continue;
  2047. if (i % bp->tx_nr_rings_per_tc == (bp->tx_nr_rings_per_tc - 1))
  2048. j++;
  2049. }
  2050. return 0;
  2051. }
  2052. static void bnxt_free_cp_rings(struct bnxt *bp)
  2053. {
  2054. int i;
  2055. if (!bp->bnapi)
  2056. return;
  2057. for (i = 0; i < bp->cp_nr_rings; i++) {
  2058. struct bnxt_napi *bnapi = bp->bnapi[i];
  2059. struct bnxt_cp_ring_info *cpr;
  2060. struct bnxt_ring_struct *ring;
  2061. if (!bnapi)
  2062. continue;
  2063. cpr = &bnapi->cp_ring;
  2064. ring = &cpr->cp_ring_struct;
  2065. bnxt_free_ring(bp, ring);
  2066. }
  2067. }
  2068. static int bnxt_alloc_cp_rings(struct bnxt *bp)
  2069. {
  2070. int i, rc, ulp_base_vec, ulp_msix;
  2071. ulp_msix = bnxt_get_ulp_msix_num(bp);
  2072. ulp_base_vec = bnxt_get_ulp_msix_base(bp);
  2073. for (i = 0; i < bp->cp_nr_rings; i++) {
  2074. struct bnxt_napi *bnapi = bp->bnapi[i];
  2075. struct bnxt_cp_ring_info *cpr;
  2076. struct bnxt_ring_struct *ring;
  2077. if (!bnapi)
  2078. continue;
  2079. cpr = &bnapi->cp_ring;
  2080. ring = &cpr->cp_ring_struct;
  2081. rc = bnxt_alloc_ring(bp, ring);
  2082. if (rc)
  2083. return rc;
  2084. if (ulp_msix && i >= ulp_base_vec)
  2085. ring->map_idx = i + ulp_msix;
  2086. else
  2087. ring->map_idx = i;
  2088. }
  2089. return 0;
  2090. }
  2091. static void bnxt_init_ring_struct(struct bnxt *bp)
  2092. {
  2093. int i;
  2094. for (i = 0; i < bp->cp_nr_rings; i++) {
  2095. struct bnxt_napi *bnapi = bp->bnapi[i];
  2096. struct bnxt_cp_ring_info *cpr;
  2097. struct bnxt_rx_ring_info *rxr;
  2098. struct bnxt_tx_ring_info *txr;
  2099. struct bnxt_ring_struct *ring;
  2100. if (!bnapi)
  2101. continue;
  2102. cpr = &bnapi->cp_ring;
  2103. ring = &cpr->cp_ring_struct;
  2104. ring->nr_pages = bp->cp_nr_pages;
  2105. ring->page_size = HW_CMPD_RING_SIZE;
  2106. ring->pg_arr = (void **)cpr->cp_desc_ring;
  2107. ring->dma_arr = cpr->cp_desc_mapping;
  2108. ring->vmem_size = 0;
  2109. rxr = bnapi->rx_ring;
  2110. if (!rxr)
  2111. goto skip_rx;
  2112. ring = &rxr->rx_ring_struct;
  2113. ring->nr_pages = bp->rx_nr_pages;
  2114. ring->page_size = HW_RXBD_RING_SIZE;
  2115. ring->pg_arr = (void **)rxr->rx_desc_ring;
  2116. ring->dma_arr = rxr->rx_desc_mapping;
  2117. ring->vmem_size = SW_RXBD_RING_SIZE * bp->rx_nr_pages;
  2118. ring->vmem = (void **)&rxr->rx_buf_ring;
  2119. ring = &rxr->rx_agg_ring_struct;
  2120. ring->nr_pages = bp->rx_agg_nr_pages;
  2121. ring->page_size = HW_RXBD_RING_SIZE;
  2122. ring->pg_arr = (void **)rxr->rx_agg_desc_ring;
  2123. ring->dma_arr = rxr->rx_agg_desc_mapping;
  2124. ring->vmem_size = SW_RXBD_AGG_RING_SIZE * bp->rx_agg_nr_pages;
  2125. ring->vmem = (void **)&rxr->rx_agg_ring;
  2126. skip_rx:
  2127. txr = bnapi->tx_ring;
  2128. if (!txr)
  2129. continue;
  2130. ring = &txr->tx_ring_struct;
  2131. ring->nr_pages = bp->tx_nr_pages;
  2132. ring->page_size = HW_RXBD_RING_SIZE;
  2133. ring->pg_arr = (void **)txr->tx_desc_ring;
  2134. ring->dma_arr = txr->tx_desc_mapping;
  2135. ring->vmem_size = SW_TXBD_RING_SIZE * bp->tx_nr_pages;
  2136. ring->vmem = (void **)&txr->tx_buf_ring;
  2137. }
  2138. }
  2139. static void bnxt_init_rxbd_pages(struct bnxt_ring_struct *ring, u32 type)
  2140. {
  2141. int i;
  2142. u32 prod;
  2143. struct rx_bd **rx_buf_ring;
  2144. rx_buf_ring = (struct rx_bd **)ring->pg_arr;
  2145. for (i = 0, prod = 0; i < ring->nr_pages; i++) {
  2146. int j;
  2147. struct rx_bd *rxbd;
  2148. rxbd = rx_buf_ring[i];
  2149. if (!rxbd)
  2150. continue;
  2151. for (j = 0; j < RX_DESC_CNT; j++, rxbd++, prod++) {
  2152. rxbd->rx_bd_len_flags_type = cpu_to_le32(type);
  2153. rxbd->rx_bd_opaque = prod;
  2154. }
  2155. }
  2156. }
  2157. static int bnxt_init_one_rx_ring(struct bnxt *bp, int ring_nr)
  2158. {
  2159. struct net_device *dev = bp->dev;
  2160. struct bnxt_rx_ring_info *rxr;
  2161. struct bnxt_ring_struct *ring;
  2162. u32 prod, type;
  2163. int i;
  2164. type = (bp->rx_buf_use_size << RX_BD_LEN_SHIFT) |
  2165. RX_BD_TYPE_RX_PACKET_BD | RX_BD_FLAGS_EOP;
  2166. if (NET_IP_ALIGN == 2)
  2167. type |= RX_BD_FLAGS_SOP;
  2168. rxr = &bp->rx_ring[ring_nr];
  2169. ring = &rxr->rx_ring_struct;
  2170. bnxt_init_rxbd_pages(ring, type);
  2171. if (BNXT_RX_PAGE_MODE(bp) && bp->xdp_prog) {
  2172. rxr->xdp_prog = bpf_prog_add(bp->xdp_prog, 1);
  2173. if (IS_ERR(rxr->xdp_prog)) {
  2174. int rc = PTR_ERR(rxr->xdp_prog);
  2175. rxr->xdp_prog = NULL;
  2176. return rc;
  2177. }
  2178. }
  2179. prod = rxr->rx_prod;
  2180. for (i = 0; i < bp->rx_ring_size; i++) {
  2181. if (bnxt_alloc_rx_data(bp, rxr, prod, GFP_KERNEL) != 0) {
  2182. netdev_warn(dev, "init'ed rx ring %d with %d/%d skbs only\n",
  2183. ring_nr, i, bp->rx_ring_size);
  2184. break;
  2185. }
  2186. prod = NEXT_RX(prod);
  2187. }
  2188. rxr->rx_prod = prod;
  2189. ring->fw_ring_id = INVALID_HW_RING_ID;
  2190. ring = &rxr->rx_agg_ring_struct;
  2191. ring->fw_ring_id = INVALID_HW_RING_ID;
  2192. if (!(bp->flags & BNXT_FLAG_AGG_RINGS))
  2193. return 0;
  2194. type = ((u32)BNXT_RX_PAGE_SIZE << RX_BD_LEN_SHIFT) |
  2195. RX_BD_TYPE_RX_AGG_BD | RX_BD_FLAGS_SOP;
  2196. bnxt_init_rxbd_pages(ring, type);
  2197. prod = rxr->rx_agg_prod;
  2198. for (i = 0; i < bp->rx_agg_ring_size; i++) {
  2199. if (bnxt_alloc_rx_page(bp, rxr, prod, GFP_KERNEL) != 0) {
  2200. netdev_warn(dev, "init'ed rx ring %d with %d/%d pages only\n",
  2201. ring_nr, i, bp->rx_ring_size);
  2202. break;
  2203. }
  2204. prod = NEXT_RX_AGG(prod);
  2205. }
  2206. rxr->rx_agg_prod = prod;
  2207. if (bp->flags & BNXT_FLAG_TPA) {
  2208. if (rxr->rx_tpa) {
  2209. u8 *data;
  2210. dma_addr_t mapping;
  2211. for (i = 0; i < MAX_TPA; i++) {
  2212. data = __bnxt_alloc_rx_data(bp, &mapping,
  2213. GFP_KERNEL);
  2214. if (!data)
  2215. return -ENOMEM;
  2216. rxr->rx_tpa[i].data = data;
  2217. rxr->rx_tpa[i].data_ptr = data + bp->rx_offset;
  2218. rxr->rx_tpa[i].mapping = mapping;
  2219. }
  2220. } else {
  2221. netdev_err(bp->dev, "No resource allocated for LRO/GRO\n");
  2222. return -ENOMEM;
  2223. }
  2224. }
  2225. return 0;
  2226. }
  2227. static void bnxt_init_cp_rings(struct bnxt *bp)
  2228. {
  2229. int i;
  2230. for (i = 0; i < bp->cp_nr_rings; i++) {
  2231. struct bnxt_cp_ring_info *cpr = &bp->bnapi[i]->cp_ring;
  2232. struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
  2233. ring->fw_ring_id = INVALID_HW_RING_ID;
  2234. cpr->rx_ring_coal.coal_ticks = bp->rx_coal.coal_ticks;
  2235. cpr->rx_ring_coal.coal_bufs = bp->rx_coal.coal_bufs;
  2236. }
  2237. }
  2238. static int bnxt_init_rx_rings(struct bnxt *bp)
  2239. {
  2240. int i, rc = 0;
  2241. if (BNXT_RX_PAGE_MODE(bp)) {
  2242. bp->rx_offset = NET_IP_ALIGN + XDP_PACKET_HEADROOM;
  2243. bp->rx_dma_offset = XDP_PACKET_HEADROOM;
  2244. } else {
  2245. bp->rx_offset = BNXT_RX_OFFSET;
  2246. bp->rx_dma_offset = BNXT_RX_DMA_OFFSET;
  2247. }
  2248. for (i = 0; i < bp->rx_nr_rings; i++) {
  2249. rc = bnxt_init_one_rx_ring(bp, i);
  2250. if (rc)
  2251. break;
  2252. }
  2253. return rc;
  2254. }
  2255. static int bnxt_init_tx_rings(struct bnxt *bp)
  2256. {
  2257. u16 i;
  2258. bp->tx_wake_thresh = max_t(int, bp->tx_ring_size / 2,
  2259. MAX_SKB_FRAGS + 1);
  2260. for (i = 0; i < bp->tx_nr_rings; i++) {
  2261. struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
  2262. struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
  2263. ring->fw_ring_id = INVALID_HW_RING_ID;
  2264. }
  2265. return 0;
  2266. }
  2267. static void bnxt_free_ring_grps(struct bnxt *bp)
  2268. {
  2269. kfree(bp->grp_info);
  2270. bp->grp_info = NULL;
  2271. }
  2272. static int bnxt_init_ring_grps(struct bnxt *bp, bool irq_re_init)
  2273. {
  2274. int i;
  2275. if (irq_re_init) {
  2276. bp->grp_info = kcalloc(bp->cp_nr_rings,
  2277. sizeof(struct bnxt_ring_grp_info),
  2278. GFP_KERNEL);
  2279. if (!bp->grp_info)
  2280. return -ENOMEM;
  2281. }
  2282. for (i = 0; i < bp->cp_nr_rings; i++) {
  2283. if (irq_re_init)
  2284. bp->grp_info[i].fw_stats_ctx = INVALID_HW_RING_ID;
  2285. bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
  2286. bp->grp_info[i].rx_fw_ring_id = INVALID_HW_RING_ID;
  2287. bp->grp_info[i].agg_fw_ring_id = INVALID_HW_RING_ID;
  2288. bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
  2289. }
  2290. return 0;
  2291. }
  2292. static void bnxt_free_vnics(struct bnxt *bp)
  2293. {
  2294. kfree(bp->vnic_info);
  2295. bp->vnic_info = NULL;
  2296. bp->nr_vnics = 0;
  2297. }
  2298. static int bnxt_alloc_vnics(struct bnxt *bp)
  2299. {
  2300. int num_vnics = 1;
  2301. #ifdef CONFIG_RFS_ACCEL
  2302. if (bp->flags & BNXT_FLAG_RFS)
  2303. num_vnics += bp->rx_nr_rings;
  2304. #endif
  2305. if (BNXT_CHIP_TYPE_NITRO_A0(bp))
  2306. num_vnics++;
  2307. bp->vnic_info = kcalloc(num_vnics, sizeof(struct bnxt_vnic_info),
  2308. GFP_KERNEL);
  2309. if (!bp->vnic_info)
  2310. return -ENOMEM;
  2311. bp->nr_vnics = num_vnics;
  2312. return 0;
  2313. }
  2314. static void bnxt_init_vnics(struct bnxt *bp)
  2315. {
  2316. int i;
  2317. for (i = 0; i < bp->nr_vnics; i++) {
  2318. struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
  2319. vnic->fw_vnic_id = INVALID_HW_RING_ID;
  2320. vnic->fw_rss_cos_lb_ctx[0] = INVALID_HW_RING_ID;
  2321. vnic->fw_rss_cos_lb_ctx[1] = INVALID_HW_RING_ID;
  2322. vnic->fw_l2_ctx_id = INVALID_HW_RING_ID;
  2323. if (bp->vnic_info[i].rss_hash_key) {
  2324. if (i == 0)
  2325. prandom_bytes(vnic->rss_hash_key,
  2326. HW_HASH_KEY_SIZE);
  2327. else
  2328. memcpy(vnic->rss_hash_key,
  2329. bp->vnic_info[0].rss_hash_key,
  2330. HW_HASH_KEY_SIZE);
  2331. }
  2332. }
  2333. }
  2334. static int bnxt_calc_nr_ring_pages(u32 ring_size, int desc_per_pg)
  2335. {
  2336. int pages;
  2337. pages = ring_size / desc_per_pg;
  2338. if (!pages)
  2339. return 1;
  2340. pages++;
  2341. while (pages & (pages - 1))
  2342. pages++;
  2343. return pages;
  2344. }
  2345. void bnxt_set_tpa_flags(struct bnxt *bp)
  2346. {
  2347. bp->flags &= ~BNXT_FLAG_TPA;
  2348. if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
  2349. return;
  2350. if (bp->dev->features & NETIF_F_LRO)
  2351. bp->flags |= BNXT_FLAG_LRO;
  2352. else if (bp->dev->features & NETIF_F_GRO_HW)
  2353. bp->flags |= BNXT_FLAG_GRO;
  2354. }
  2355. /* bp->rx_ring_size, bp->tx_ring_size, dev->mtu, BNXT_FLAG_{G|L}RO flags must
  2356. * be set on entry.
  2357. */
  2358. void bnxt_set_ring_params(struct bnxt *bp)
  2359. {
  2360. u32 ring_size, rx_size, rx_space;
  2361. u32 agg_factor = 0, agg_ring_size = 0;
  2362. /* 8 for CRC and VLAN */
  2363. rx_size = SKB_DATA_ALIGN(bp->dev->mtu + ETH_HLEN + NET_IP_ALIGN + 8);
  2364. rx_space = rx_size + NET_SKB_PAD +
  2365. SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
  2366. bp->rx_copy_thresh = BNXT_RX_COPY_THRESH;
  2367. ring_size = bp->rx_ring_size;
  2368. bp->rx_agg_ring_size = 0;
  2369. bp->rx_agg_nr_pages = 0;
  2370. if (bp->flags & BNXT_FLAG_TPA)
  2371. agg_factor = min_t(u32, 4, 65536 / BNXT_RX_PAGE_SIZE);
  2372. bp->flags &= ~BNXT_FLAG_JUMBO;
  2373. if (rx_space > PAGE_SIZE && !(bp->flags & BNXT_FLAG_NO_AGG_RINGS)) {
  2374. u32 jumbo_factor;
  2375. bp->flags |= BNXT_FLAG_JUMBO;
  2376. jumbo_factor = PAGE_ALIGN(bp->dev->mtu - 40) >> PAGE_SHIFT;
  2377. if (jumbo_factor > agg_factor)
  2378. agg_factor = jumbo_factor;
  2379. }
  2380. agg_ring_size = ring_size * agg_factor;
  2381. if (agg_ring_size) {
  2382. bp->rx_agg_nr_pages = bnxt_calc_nr_ring_pages(agg_ring_size,
  2383. RX_DESC_CNT);
  2384. if (bp->rx_agg_nr_pages > MAX_RX_AGG_PAGES) {
  2385. u32 tmp = agg_ring_size;
  2386. bp->rx_agg_nr_pages = MAX_RX_AGG_PAGES;
  2387. agg_ring_size = MAX_RX_AGG_PAGES * RX_DESC_CNT - 1;
  2388. netdev_warn(bp->dev, "rx agg ring size %d reduced to %d.\n",
  2389. tmp, agg_ring_size);
  2390. }
  2391. bp->rx_agg_ring_size = agg_ring_size;
  2392. bp->rx_agg_ring_mask = (bp->rx_agg_nr_pages * RX_DESC_CNT) - 1;
  2393. rx_size = SKB_DATA_ALIGN(BNXT_RX_COPY_THRESH + NET_IP_ALIGN);
  2394. rx_space = rx_size + NET_SKB_PAD +
  2395. SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
  2396. }
  2397. bp->rx_buf_use_size = rx_size;
  2398. bp->rx_buf_size = rx_space;
  2399. bp->rx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, RX_DESC_CNT);
  2400. bp->rx_ring_mask = (bp->rx_nr_pages * RX_DESC_CNT) - 1;
  2401. ring_size = bp->tx_ring_size;
  2402. bp->tx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, TX_DESC_CNT);
  2403. bp->tx_ring_mask = (bp->tx_nr_pages * TX_DESC_CNT) - 1;
  2404. ring_size = bp->rx_ring_size * (2 + agg_factor) + bp->tx_ring_size;
  2405. bp->cp_ring_size = ring_size;
  2406. bp->cp_nr_pages = bnxt_calc_nr_ring_pages(ring_size, CP_DESC_CNT);
  2407. if (bp->cp_nr_pages > MAX_CP_PAGES) {
  2408. bp->cp_nr_pages = MAX_CP_PAGES;
  2409. bp->cp_ring_size = MAX_CP_PAGES * CP_DESC_CNT - 1;
  2410. netdev_warn(bp->dev, "completion ring size %d reduced to %d.\n",
  2411. ring_size, bp->cp_ring_size);
  2412. }
  2413. bp->cp_bit = bp->cp_nr_pages * CP_DESC_CNT;
  2414. bp->cp_ring_mask = bp->cp_bit - 1;
  2415. }
  2416. /* Changing allocation mode of RX rings.
  2417. * TODO: Update when extending xdp_rxq_info to support allocation modes.
  2418. */
  2419. int bnxt_set_rx_skb_mode(struct bnxt *bp, bool page_mode)
  2420. {
  2421. if (page_mode) {
  2422. if (bp->dev->mtu > BNXT_MAX_PAGE_MODE_MTU)
  2423. return -EOPNOTSUPP;
  2424. bp->dev->max_mtu =
  2425. min_t(u16, bp->max_mtu, BNXT_MAX_PAGE_MODE_MTU);
  2426. bp->flags &= ~BNXT_FLAG_AGG_RINGS;
  2427. bp->flags |= BNXT_FLAG_NO_AGG_RINGS | BNXT_FLAG_RX_PAGE_MODE;
  2428. bp->rx_dir = DMA_BIDIRECTIONAL;
  2429. bp->rx_skb_func = bnxt_rx_page_skb;
  2430. /* Disable LRO or GRO_HW */
  2431. netdev_update_features(bp->dev);
  2432. } else {
  2433. bp->dev->max_mtu = bp->max_mtu;
  2434. bp->flags &= ~BNXT_FLAG_RX_PAGE_MODE;
  2435. bp->rx_dir = DMA_FROM_DEVICE;
  2436. bp->rx_skb_func = bnxt_rx_skb;
  2437. }
  2438. return 0;
  2439. }
  2440. static void bnxt_free_vnic_attributes(struct bnxt *bp)
  2441. {
  2442. int i;
  2443. struct bnxt_vnic_info *vnic;
  2444. struct pci_dev *pdev = bp->pdev;
  2445. if (!bp->vnic_info)
  2446. return;
  2447. for (i = 0; i < bp->nr_vnics; i++) {
  2448. vnic = &bp->vnic_info[i];
  2449. kfree(vnic->fw_grp_ids);
  2450. vnic->fw_grp_ids = NULL;
  2451. kfree(vnic->uc_list);
  2452. vnic->uc_list = NULL;
  2453. if (vnic->mc_list) {
  2454. dma_free_coherent(&pdev->dev, vnic->mc_list_size,
  2455. vnic->mc_list, vnic->mc_list_mapping);
  2456. vnic->mc_list = NULL;
  2457. }
  2458. if (vnic->rss_table) {
  2459. dma_free_coherent(&pdev->dev, PAGE_SIZE,
  2460. vnic->rss_table,
  2461. vnic->rss_table_dma_addr);
  2462. vnic->rss_table = NULL;
  2463. }
  2464. vnic->rss_hash_key = NULL;
  2465. vnic->flags = 0;
  2466. }
  2467. }
  2468. static int bnxt_alloc_vnic_attributes(struct bnxt *bp)
  2469. {
  2470. int i, rc = 0, size;
  2471. struct bnxt_vnic_info *vnic;
  2472. struct pci_dev *pdev = bp->pdev;
  2473. int max_rings;
  2474. for (i = 0; i < bp->nr_vnics; i++) {
  2475. vnic = &bp->vnic_info[i];
  2476. if (vnic->flags & BNXT_VNIC_UCAST_FLAG) {
  2477. int mem_size = (BNXT_MAX_UC_ADDRS - 1) * ETH_ALEN;
  2478. if (mem_size > 0) {
  2479. vnic->uc_list = kmalloc(mem_size, GFP_KERNEL);
  2480. if (!vnic->uc_list) {
  2481. rc = -ENOMEM;
  2482. goto out;
  2483. }
  2484. }
  2485. }
  2486. if (vnic->flags & BNXT_VNIC_MCAST_FLAG) {
  2487. vnic->mc_list_size = BNXT_MAX_MC_ADDRS * ETH_ALEN;
  2488. vnic->mc_list =
  2489. dma_alloc_coherent(&pdev->dev,
  2490. vnic->mc_list_size,
  2491. &vnic->mc_list_mapping,
  2492. GFP_KERNEL);
  2493. if (!vnic->mc_list) {
  2494. rc = -ENOMEM;
  2495. goto out;
  2496. }
  2497. }
  2498. if (vnic->flags & BNXT_VNIC_RSS_FLAG)
  2499. max_rings = bp->rx_nr_rings;
  2500. else
  2501. max_rings = 1;
  2502. vnic->fw_grp_ids = kcalloc(max_rings, sizeof(u16), GFP_KERNEL);
  2503. if (!vnic->fw_grp_ids) {
  2504. rc = -ENOMEM;
  2505. goto out;
  2506. }
  2507. if ((bp->flags & BNXT_FLAG_NEW_RSS_CAP) &&
  2508. !(vnic->flags & BNXT_VNIC_RSS_FLAG))
  2509. continue;
  2510. /* Allocate rss table and hash key */
  2511. vnic->rss_table = dma_alloc_coherent(&pdev->dev, PAGE_SIZE,
  2512. &vnic->rss_table_dma_addr,
  2513. GFP_KERNEL);
  2514. if (!vnic->rss_table) {
  2515. rc = -ENOMEM;
  2516. goto out;
  2517. }
  2518. size = L1_CACHE_ALIGN(HW_HASH_INDEX_SIZE * sizeof(u16));
  2519. vnic->rss_hash_key = ((void *)vnic->rss_table) + size;
  2520. vnic->rss_hash_key_dma_addr = vnic->rss_table_dma_addr + size;
  2521. }
  2522. return 0;
  2523. out:
  2524. return rc;
  2525. }
  2526. static void bnxt_free_hwrm_resources(struct bnxt *bp)
  2527. {
  2528. struct pci_dev *pdev = bp->pdev;
  2529. if (bp->hwrm_cmd_resp_addr) {
  2530. dma_free_coherent(&pdev->dev, PAGE_SIZE, bp->hwrm_cmd_resp_addr,
  2531. bp->hwrm_cmd_resp_dma_addr);
  2532. bp->hwrm_cmd_resp_addr = NULL;
  2533. }
  2534. }
  2535. static int bnxt_alloc_hwrm_resources(struct bnxt *bp)
  2536. {
  2537. struct pci_dev *pdev = bp->pdev;
  2538. bp->hwrm_cmd_resp_addr = dma_alloc_coherent(&pdev->dev, PAGE_SIZE,
  2539. &bp->hwrm_cmd_resp_dma_addr,
  2540. GFP_KERNEL);
  2541. if (!bp->hwrm_cmd_resp_addr)
  2542. return -ENOMEM;
  2543. return 0;
  2544. }
  2545. static void bnxt_free_hwrm_short_cmd_req(struct bnxt *bp)
  2546. {
  2547. if (bp->hwrm_short_cmd_req_addr) {
  2548. struct pci_dev *pdev = bp->pdev;
  2549. dma_free_coherent(&pdev->dev, BNXT_HWRM_MAX_REQ_LEN,
  2550. bp->hwrm_short_cmd_req_addr,
  2551. bp->hwrm_short_cmd_req_dma_addr);
  2552. bp->hwrm_short_cmd_req_addr = NULL;
  2553. }
  2554. }
  2555. static int bnxt_alloc_hwrm_short_cmd_req(struct bnxt *bp)
  2556. {
  2557. struct pci_dev *pdev = bp->pdev;
  2558. bp->hwrm_short_cmd_req_addr =
  2559. dma_alloc_coherent(&pdev->dev, BNXT_HWRM_MAX_REQ_LEN,
  2560. &bp->hwrm_short_cmd_req_dma_addr,
  2561. GFP_KERNEL);
  2562. if (!bp->hwrm_short_cmd_req_addr)
  2563. return -ENOMEM;
  2564. return 0;
  2565. }
  2566. static void bnxt_free_stats(struct bnxt *bp)
  2567. {
  2568. u32 size, i;
  2569. struct pci_dev *pdev = bp->pdev;
  2570. bp->flags &= ~BNXT_FLAG_PORT_STATS;
  2571. bp->flags &= ~BNXT_FLAG_PORT_STATS_EXT;
  2572. if (bp->hw_rx_port_stats) {
  2573. dma_free_coherent(&pdev->dev, bp->hw_port_stats_size,
  2574. bp->hw_rx_port_stats,
  2575. bp->hw_rx_port_stats_map);
  2576. bp->hw_rx_port_stats = NULL;
  2577. }
  2578. if (bp->hw_rx_port_stats_ext) {
  2579. dma_free_coherent(&pdev->dev, sizeof(struct rx_port_stats_ext),
  2580. bp->hw_rx_port_stats_ext,
  2581. bp->hw_rx_port_stats_ext_map);
  2582. bp->hw_rx_port_stats_ext = NULL;
  2583. }
  2584. if (!bp->bnapi)
  2585. return;
  2586. size = sizeof(struct ctx_hw_stats);
  2587. for (i = 0; i < bp->cp_nr_rings; i++) {
  2588. struct bnxt_napi *bnapi = bp->bnapi[i];
  2589. struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
  2590. if (cpr->hw_stats) {
  2591. dma_free_coherent(&pdev->dev, size, cpr->hw_stats,
  2592. cpr->hw_stats_map);
  2593. cpr->hw_stats = NULL;
  2594. }
  2595. }
  2596. }
  2597. static int bnxt_alloc_stats(struct bnxt *bp)
  2598. {
  2599. u32 size, i;
  2600. struct pci_dev *pdev = bp->pdev;
  2601. size = sizeof(struct ctx_hw_stats);
  2602. for (i = 0; i < bp->cp_nr_rings; i++) {
  2603. struct bnxt_napi *bnapi = bp->bnapi[i];
  2604. struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
  2605. cpr->hw_stats = dma_alloc_coherent(&pdev->dev, size,
  2606. &cpr->hw_stats_map,
  2607. GFP_KERNEL);
  2608. if (!cpr->hw_stats)
  2609. return -ENOMEM;
  2610. cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
  2611. }
  2612. if (BNXT_PF(bp) && bp->chip_num != CHIP_NUM_58700) {
  2613. bp->hw_port_stats_size = sizeof(struct rx_port_stats) +
  2614. sizeof(struct tx_port_stats) + 1024;
  2615. bp->hw_rx_port_stats =
  2616. dma_alloc_coherent(&pdev->dev, bp->hw_port_stats_size,
  2617. &bp->hw_rx_port_stats_map,
  2618. GFP_KERNEL);
  2619. if (!bp->hw_rx_port_stats)
  2620. return -ENOMEM;
  2621. bp->hw_tx_port_stats = (void *)(bp->hw_rx_port_stats + 1) +
  2622. 512;
  2623. bp->hw_tx_port_stats_map = bp->hw_rx_port_stats_map +
  2624. sizeof(struct rx_port_stats) + 512;
  2625. bp->flags |= BNXT_FLAG_PORT_STATS;
  2626. /* Display extended statistics only if FW supports it */
  2627. if (bp->hwrm_spec_code < 0x10804 ||
  2628. bp->hwrm_spec_code == 0x10900)
  2629. return 0;
  2630. bp->hw_rx_port_stats_ext =
  2631. dma_zalloc_coherent(&pdev->dev,
  2632. sizeof(struct rx_port_stats_ext),
  2633. &bp->hw_rx_port_stats_ext_map,
  2634. GFP_KERNEL);
  2635. if (!bp->hw_rx_port_stats_ext)
  2636. return 0;
  2637. bp->flags |= BNXT_FLAG_PORT_STATS_EXT;
  2638. }
  2639. return 0;
  2640. }
  2641. static void bnxt_clear_ring_indices(struct bnxt *bp)
  2642. {
  2643. int i;
  2644. if (!bp->bnapi)
  2645. return;
  2646. for (i = 0; i < bp->cp_nr_rings; i++) {
  2647. struct bnxt_napi *bnapi = bp->bnapi[i];
  2648. struct bnxt_cp_ring_info *cpr;
  2649. struct bnxt_rx_ring_info *rxr;
  2650. struct bnxt_tx_ring_info *txr;
  2651. if (!bnapi)
  2652. continue;
  2653. cpr = &bnapi->cp_ring;
  2654. cpr->cp_raw_cons = 0;
  2655. txr = bnapi->tx_ring;
  2656. if (txr) {
  2657. txr->tx_prod = 0;
  2658. txr->tx_cons = 0;
  2659. }
  2660. rxr = bnapi->rx_ring;
  2661. if (rxr) {
  2662. rxr->rx_prod = 0;
  2663. rxr->rx_agg_prod = 0;
  2664. rxr->rx_sw_agg_prod = 0;
  2665. rxr->rx_next_cons = 0;
  2666. }
  2667. }
  2668. }
  2669. static void bnxt_free_ntp_fltrs(struct bnxt *bp, bool irq_reinit)
  2670. {
  2671. #ifdef CONFIG_RFS_ACCEL
  2672. int i;
  2673. /* Under rtnl_lock and all our NAPIs have been disabled. It's
  2674. * safe to delete the hash table.
  2675. */
  2676. for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
  2677. struct hlist_head *head;
  2678. struct hlist_node *tmp;
  2679. struct bnxt_ntuple_filter *fltr;
  2680. head = &bp->ntp_fltr_hash_tbl[i];
  2681. hlist_for_each_entry_safe(fltr, tmp, head, hash) {
  2682. hlist_del(&fltr->hash);
  2683. kfree(fltr);
  2684. }
  2685. }
  2686. if (irq_reinit) {
  2687. kfree(bp->ntp_fltr_bmap);
  2688. bp->ntp_fltr_bmap = NULL;
  2689. }
  2690. bp->ntp_fltr_count = 0;
  2691. #endif
  2692. }
  2693. static int bnxt_alloc_ntp_fltrs(struct bnxt *bp)
  2694. {
  2695. #ifdef CONFIG_RFS_ACCEL
  2696. int i, rc = 0;
  2697. if (!(bp->flags & BNXT_FLAG_RFS))
  2698. return 0;
  2699. for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++)
  2700. INIT_HLIST_HEAD(&bp->ntp_fltr_hash_tbl[i]);
  2701. bp->ntp_fltr_count = 0;
  2702. bp->ntp_fltr_bmap = kcalloc(BITS_TO_LONGS(BNXT_NTP_FLTR_MAX_FLTR),
  2703. sizeof(long),
  2704. GFP_KERNEL);
  2705. if (!bp->ntp_fltr_bmap)
  2706. rc = -ENOMEM;
  2707. return rc;
  2708. #else
  2709. return 0;
  2710. #endif
  2711. }
  2712. static void bnxt_free_mem(struct bnxt *bp, bool irq_re_init)
  2713. {
  2714. bnxt_free_vnic_attributes(bp);
  2715. bnxt_free_tx_rings(bp);
  2716. bnxt_free_rx_rings(bp);
  2717. bnxt_free_cp_rings(bp);
  2718. bnxt_free_ntp_fltrs(bp, irq_re_init);
  2719. if (irq_re_init) {
  2720. bnxt_free_stats(bp);
  2721. bnxt_free_ring_grps(bp);
  2722. bnxt_free_vnics(bp);
  2723. kfree(bp->tx_ring_map);
  2724. bp->tx_ring_map = NULL;
  2725. kfree(bp->tx_ring);
  2726. bp->tx_ring = NULL;
  2727. kfree(bp->rx_ring);
  2728. bp->rx_ring = NULL;
  2729. kfree(bp->bnapi);
  2730. bp->bnapi = NULL;
  2731. } else {
  2732. bnxt_clear_ring_indices(bp);
  2733. }
  2734. }
  2735. static int bnxt_alloc_mem(struct bnxt *bp, bool irq_re_init)
  2736. {
  2737. int i, j, rc, size, arr_size;
  2738. void *bnapi;
  2739. if (irq_re_init) {
  2740. /* Allocate bnapi mem pointer array and mem block for
  2741. * all queues
  2742. */
  2743. arr_size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi *) *
  2744. bp->cp_nr_rings);
  2745. size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi));
  2746. bnapi = kzalloc(arr_size + size * bp->cp_nr_rings, GFP_KERNEL);
  2747. if (!bnapi)
  2748. return -ENOMEM;
  2749. bp->bnapi = bnapi;
  2750. bnapi += arr_size;
  2751. for (i = 0; i < bp->cp_nr_rings; i++, bnapi += size) {
  2752. bp->bnapi[i] = bnapi;
  2753. bp->bnapi[i]->index = i;
  2754. bp->bnapi[i]->bp = bp;
  2755. }
  2756. bp->rx_ring = kcalloc(bp->rx_nr_rings,
  2757. sizeof(struct bnxt_rx_ring_info),
  2758. GFP_KERNEL);
  2759. if (!bp->rx_ring)
  2760. return -ENOMEM;
  2761. for (i = 0; i < bp->rx_nr_rings; i++) {
  2762. bp->rx_ring[i].bnapi = bp->bnapi[i];
  2763. bp->bnapi[i]->rx_ring = &bp->rx_ring[i];
  2764. }
  2765. bp->tx_ring = kcalloc(bp->tx_nr_rings,
  2766. sizeof(struct bnxt_tx_ring_info),
  2767. GFP_KERNEL);
  2768. if (!bp->tx_ring)
  2769. return -ENOMEM;
  2770. bp->tx_ring_map = kcalloc(bp->tx_nr_rings, sizeof(u16),
  2771. GFP_KERNEL);
  2772. if (!bp->tx_ring_map)
  2773. return -ENOMEM;
  2774. if (bp->flags & BNXT_FLAG_SHARED_RINGS)
  2775. j = 0;
  2776. else
  2777. j = bp->rx_nr_rings;
  2778. for (i = 0; i < bp->tx_nr_rings; i++, j++) {
  2779. bp->tx_ring[i].bnapi = bp->bnapi[j];
  2780. bp->bnapi[j]->tx_ring = &bp->tx_ring[i];
  2781. bp->tx_ring_map[i] = bp->tx_nr_rings_xdp + i;
  2782. if (i >= bp->tx_nr_rings_xdp) {
  2783. bp->tx_ring[i].txq_index = i -
  2784. bp->tx_nr_rings_xdp;
  2785. bp->bnapi[j]->tx_int = bnxt_tx_int;
  2786. } else {
  2787. bp->bnapi[j]->flags |= BNXT_NAPI_FLAG_XDP;
  2788. bp->bnapi[j]->tx_int = bnxt_tx_int_xdp;
  2789. }
  2790. }
  2791. rc = bnxt_alloc_stats(bp);
  2792. if (rc)
  2793. goto alloc_mem_err;
  2794. rc = bnxt_alloc_ntp_fltrs(bp);
  2795. if (rc)
  2796. goto alloc_mem_err;
  2797. rc = bnxt_alloc_vnics(bp);
  2798. if (rc)
  2799. goto alloc_mem_err;
  2800. }
  2801. bnxt_init_ring_struct(bp);
  2802. rc = bnxt_alloc_rx_rings(bp);
  2803. if (rc)
  2804. goto alloc_mem_err;
  2805. rc = bnxt_alloc_tx_rings(bp);
  2806. if (rc)
  2807. goto alloc_mem_err;
  2808. rc = bnxt_alloc_cp_rings(bp);
  2809. if (rc)
  2810. goto alloc_mem_err;
  2811. bp->vnic_info[0].flags |= BNXT_VNIC_RSS_FLAG | BNXT_VNIC_MCAST_FLAG |
  2812. BNXT_VNIC_UCAST_FLAG;
  2813. rc = bnxt_alloc_vnic_attributes(bp);
  2814. if (rc)
  2815. goto alloc_mem_err;
  2816. return 0;
  2817. alloc_mem_err:
  2818. bnxt_free_mem(bp, true);
  2819. return rc;
  2820. }
  2821. static void bnxt_disable_int(struct bnxt *bp)
  2822. {
  2823. int i;
  2824. if (!bp->bnapi)
  2825. return;
  2826. for (i = 0; i < bp->cp_nr_rings; i++) {
  2827. struct bnxt_napi *bnapi = bp->bnapi[i];
  2828. struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
  2829. struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
  2830. if (ring->fw_ring_id != INVALID_HW_RING_ID)
  2831. BNXT_CP_DB(cpr->cp_doorbell, cpr->cp_raw_cons);
  2832. }
  2833. }
  2834. static int bnxt_cp_num_to_irq_num(struct bnxt *bp, int n)
  2835. {
  2836. struct bnxt_napi *bnapi = bp->bnapi[n];
  2837. struct bnxt_cp_ring_info *cpr;
  2838. cpr = &bnapi->cp_ring;
  2839. return cpr->cp_ring_struct.map_idx;
  2840. }
  2841. static void bnxt_disable_int_sync(struct bnxt *bp)
  2842. {
  2843. int i;
  2844. atomic_inc(&bp->intr_sem);
  2845. bnxt_disable_int(bp);
  2846. for (i = 0; i < bp->cp_nr_rings; i++) {
  2847. int map_idx = bnxt_cp_num_to_irq_num(bp, i);
  2848. synchronize_irq(bp->irq_tbl[map_idx].vector);
  2849. }
  2850. }
  2851. static void bnxt_enable_int(struct bnxt *bp)
  2852. {
  2853. int i;
  2854. atomic_set(&bp->intr_sem, 0);
  2855. for (i = 0; i < bp->cp_nr_rings; i++) {
  2856. struct bnxt_napi *bnapi = bp->bnapi[i];
  2857. struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
  2858. BNXT_CP_DB_REARM(cpr->cp_doorbell, cpr->cp_raw_cons);
  2859. }
  2860. }
  2861. void bnxt_hwrm_cmd_hdr_init(struct bnxt *bp, void *request, u16 req_type,
  2862. u16 cmpl_ring, u16 target_id)
  2863. {
  2864. struct input *req = request;
  2865. req->req_type = cpu_to_le16(req_type);
  2866. req->cmpl_ring = cpu_to_le16(cmpl_ring);
  2867. req->target_id = cpu_to_le16(target_id);
  2868. req->resp_addr = cpu_to_le64(bp->hwrm_cmd_resp_dma_addr);
  2869. }
  2870. static int bnxt_hwrm_do_send_msg(struct bnxt *bp, void *msg, u32 msg_len,
  2871. int timeout, bool silent)
  2872. {
  2873. int i, intr_process, rc, tmo_count;
  2874. struct input *req = msg;
  2875. u32 *data = msg;
  2876. __le32 *resp_len;
  2877. u8 *valid;
  2878. u16 cp_ring_id, len = 0;
  2879. struct hwrm_err_output *resp = bp->hwrm_cmd_resp_addr;
  2880. u16 max_req_len = BNXT_HWRM_MAX_REQ_LEN;
  2881. struct hwrm_short_input short_input = {0};
  2882. req->seq_id = cpu_to_le16(bp->hwrm_cmd_seq++);
  2883. memset(resp, 0, PAGE_SIZE);
  2884. cp_ring_id = le16_to_cpu(req->cmpl_ring);
  2885. intr_process = (cp_ring_id == INVALID_HW_RING_ID) ? 0 : 1;
  2886. if (bp->fw_cap & BNXT_FW_CAP_SHORT_CMD) {
  2887. void *short_cmd_req = bp->hwrm_short_cmd_req_addr;
  2888. memcpy(short_cmd_req, req, msg_len);
  2889. memset(short_cmd_req + msg_len, 0, BNXT_HWRM_MAX_REQ_LEN -
  2890. msg_len);
  2891. short_input.req_type = req->req_type;
  2892. short_input.signature =
  2893. cpu_to_le16(SHORT_REQ_SIGNATURE_SHORT_CMD);
  2894. short_input.size = cpu_to_le16(msg_len);
  2895. short_input.req_addr =
  2896. cpu_to_le64(bp->hwrm_short_cmd_req_dma_addr);
  2897. data = (u32 *)&short_input;
  2898. msg_len = sizeof(short_input);
  2899. /* Sync memory write before updating doorbell */
  2900. wmb();
  2901. max_req_len = BNXT_HWRM_SHORT_REQ_LEN;
  2902. }
  2903. /* Write request msg to hwrm channel */
  2904. __iowrite32_copy(bp->bar0, data, msg_len / 4);
  2905. for (i = msg_len; i < max_req_len; i += 4)
  2906. writel(0, bp->bar0 + i);
  2907. /* currently supports only one outstanding message */
  2908. if (intr_process)
  2909. bp->hwrm_intr_seq_id = le16_to_cpu(req->seq_id);
  2910. /* Ring channel doorbell */
  2911. writel(1, bp->bar0 + 0x100);
  2912. if (!timeout)
  2913. timeout = DFLT_HWRM_CMD_TIMEOUT;
  2914. /* convert timeout to usec */
  2915. timeout *= 1000;
  2916. i = 0;
  2917. /* Short timeout for the first few iterations:
  2918. * number of loops = number of loops for short timeout +
  2919. * number of loops for standard timeout.
  2920. */
  2921. tmo_count = HWRM_SHORT_TIMEOUT_COUNTER;
  2922. timeout = timeout - HWRM_SHORT_MIN_TIMEOUT * HWRM_SHORT_TIMEOUT_COUNTER;
  2923. tmo_count += DIV_ROUND_UP(timeout, HWRM_MIN_TIMEOUT);
  2924. resp_len = bp->hwrm_cmd_resp_addr + HWRM_RESP_LEN_OFFSET;
  2925. if (intr_process) {
  2926. /* Wait until hwrm response cmpl interrupt is processed */
  2927. while (bp->hwrm_intr_seq_id != HWRM_SEQ_ID_INVALID &&
  2928. i++ < tmo_count) {
  2929. /* on first few passes, just barely sleep */
  2930. if (i < HWRM_SHORT_TIMEOUT_COUNTER)
  2931. usleep_range(HWRM_SHORT_MIN_TIMEOUT,
  2932. HWRM_SHORT_MAX_TIMEOUT);
  2933. else
  2934. usleep_range(HWRM_MIN_TIMEOUT,
  2935. HWRM_MAX_TIMEOUT);
  2936. }
  2937. if (bp->hwrm_intr_seq_id != HWRM_SEQ_ID_INVALID) {
  2938. netdev_err(bp->dev, "Resp cmpl intr err msg: 0x%x\n",
  2939. le16_to_cpu(req->req_type));
  2940. return -1;
  2941. }
  2942. len = (le32_to_cpu(*resp_len) & HWRM_RESP_LEN_MASK) >>
  2943. HWRM_RESP_LEN_SFT;
  2944. valid = bp->hwrm_cmd_resp_addr + len - 1;
  2945. } else {
  2946. int j;
  2947. /* Check if response len is updated */
  2948. for (i = 0; i < tmo_count; i++) {
  2949. len = (le32_to_cpu(*resp_len) & HWRM_RESP_LEN_MASK) >>
  2950. HWRM_RESP_LEN_SFT;
  2951. if (len)
  2952. break;
  2953. /* on first few passes, just barely sleep */
  2954. if (i < HWRM_SHORT_TIMEOUT_COUNTER)
  2955. usleep_range(HWRM_SHORT_MIN_TIMEOUT,
  2956. HWRM_SHORT_MAX_TIMEOUT);
  2957. else
  2958. usleep_range(HWRM_MIN_TIMEOUT,
  2959. HWRM_MAX_TIMEOUT);
  2960. }
  2961. if (i >= tmo_count) {
  2962. netdev_err(bp->dev, "Error (timeout: %d) msg {0x%x 0x%x} len:%d\n",
  2963. HWRM_TOTAL_TIMEOUT(i),
  2964. le16_to_cpu(req->req_type),
  2965. le16_to_cpu(req->seq_id), len);
  2966. return -1;
  2967. }
  2968. /* Last byte of resp contains valid bit */
  2969. valid = bp->hwrm_cmd_resp_addr + len - 1;
  2970. for (j = 0; j < HWRM_VALID_BIT_DELAY_USEC; j++) {
  2971. /* make sure we read from updated DMA memory */
  2972. dma_rmb();
  2973. if (*valid)
  2974. break;
  2975. usleep_range(1, 5);
  2976. }
  2977. if (j >= HWRM_VALID_BIT_DELAY_USEC) {
  2978. netdev_err(bp->dev, "Error (timeout: %d) msg {0x%x 0x%x} len:%d v:%d\n",
  2979. HWRM_TOTAL_TIMEOUT(i),
  2980. le16_to_cpu(req->req_type),
  2981. le16_to_cpu(req->seq_id), len, *valid);
  2982. return -1;
  2983. }
  2984. }
  2985. /* Zero valid bit for compatibility. Valid bit in an older spec
  2986. * may become a new field in a newer spec. We must make sure that
  2987. * a new field not implemented by old spec will read zero.
  2988. */
  2989. *valid = 0;
  2990. rc = le16_to_cpu(resp->error_code);
  2991. if (rc && !silent)
  2992. netdev_err(bp->dev, "hwrm req_type 0x%x seq id 0x%x error 0x%x\n",
  2993. le16_to_cpu(resp->req_type),
  2994. le16_to_cpu(resp->seq_id), rc);
  2995. return rc;
  2996. }
  2997. int _hwrm_send_message(struct bnxt *bp, void *msg, u32 msg_len, int timeout)
  2998. {
  2999. return bnxt_hwrm_do_send_msg(bp, msg, msg_len, timeout, false);
  3000. }
  3001. int _hwrm_send_message_silent(struct bnxt *bp, void *msg, u32 msg_len,
  3002. int timeout)
  3003. {
  3004. return bnxt_hwrm_do_send_msg(bp, msg, msg_len, timeout, true);
  3005. }
  3006. int hwrm_send_message(struct bnxt *bp, void *msg, u32 msg_len, int timeout)
  3007. {
  3008. int rc;
  3009. mutex_lock(&bp->hwrm_cmd_lock);
  3010. rc = _hwrm_send_message(bp, msg, msg_len, timeout);
  3011. mutex_unlock(&bp->hwrm_cmd_lock);
  3012. return rc;
  3013. }
  3014. int hwrm_send_message_silent(struct bnxt *bp, void *msg, u32 msg_len,
  3015. int timeout)
  3016. {
  3017. int rc;
  3018. mutex_lock(&bp->hwrm_cmd_lock);
  3019. rc = bnxt_hwrm_do_send_msg(bp, msg, msg_len, timeout, true);
  3020. mutex_unlock(&bp->hwrm_cmd_lock);
  3021. return rc;
  3022. }
  3023. int bnxt_hwrm_func_rgtr_async_events(struct bnxt *bp, unsigned long *bmap,
  3024. int bmap_size)
  3025. {
  3026. struct hwrm_func_drv_rgtr_input req = {0};
  3027. DECLARE_BITMAP(async_events_bmap, 256);
  3028. u32 *events = (u32 *)async_events_bmap;
  3029. int i;
  3030. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_DRV_RGTR, -1, -1);
  3031. req.enables =
  3032. cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_ASYNC_EVENT_FWD);
  3033. memset(async_events_bmap, 0, sizeof(async_events_bmap));
  3034. for (i = 0; i < ARRAY_SIZE(bnxt_async_events_arr); i++)
  3035. __set_bit(bnxt_async_events_arr[i], async_events_bmap);
  3036. if (bmap && bmap_size) {
  3037. for (i = 0; i < bmap_size; i++) {
  3038. if (test_bit(i, bmap))
  3039. __set_bit(i, async_events_bmap);
  3040. }
  3041. }
  3042. for (i = 0; i < 8; i++)
  3043. req.async_event_fwd[i] |= cpu_to_le32(events[i]);
  3044. return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  3045. }
  3046. static int bnxt_hwrm_func_drv_rgtr(struct bnxt *bp)
  3047. {
  3048. struct hwrm_func_drv_rgtr_output *resp = bp->hwrm_cmd_resp_addr;
  3049. struct hwrm_func_drv_rgtr_input req = {0};
  3050. int rc;
  3051. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_DRV_RGTR, -1, -1);
  3052. req.enables =
  3053. cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_OS_TYPE |
  3054. FUNC_DRV_RGTR_REQ_ENABLES_VER);
  3055. req.os_type = cpu_to_le16(FUNC_DRV_RGTR_REQ_OS_TYPE_LINUX);
  3056. req.flags = cpu_to_le32(FUNC_DRV_RGTR_REQ_FLAGS_16BIT_VER_MODE);
  3057. req.ver_maj_8b = DRV_VER_MAJ;
  3058. req.ver_min_8b = DRV_VER_MIN;
  3059. req.ver_upd_8b = DRV_VER_UPD;
  3060. req.ver_maj = cpu_to_le16(DRV_VER_MAJ);
  3061. req.ver_min = cpu_to_le16(DRV_VER_MIN);
  3062. req.ver_upd = cpu_to_le16(DRV_VER_UPD);
  3063. if (BNXT_PF(bp)) {
  3064. u32 data[8];
  3065. int i;
  3066. memset(data, 0, sizeof(data));
  3067. for (i = 0; i < ARRAY_SIZE(bnxt_vf_req_snif); i++) {
  3068. u16 cmd = bnxt_vf_req_snif[i];
  3069. unsigned int bit, idx;
  3070. idx = cmd / 32;
  3071. bit = cmd % 32;
  3072. data[idx] |= 1 << bit;
  3073. }
  3074. for (i = 0; i < 8; i++)
  3075. req.vf_req_fwd[i] = cpu_to_le32(data[i]);
  3076. req.enables |=
  3077. cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_VF_REQ_FWD);
  3078. }
  3079. mutex_lock(&bp->hwrm_cmd_lock);
  3080. rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  3081. if (rc)
  3082. rc = -EIO;
  3083. else if (resp->flags &
  3084. cpu_to_le32(FUNC_DRV_RGTR_RESP_FLAGS_IF_CHANGE_SUPPORTED))
  3085. bp->fw_cap |= BNXT_FW_CAP_IF_CHANGE;
  3086. mutex_unlock(&bp->hwrm_cmd_lock);
  3087. return rc;
  3088. }
  3089. static int bnxt_hwrm_func_drv_unrgtr(struct bnxt *bp)
  3090. {
  3091. struct hwrm_func_drv_unrgtr_input req = {0};
  3092. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_DRV_UNRGTR, -1, -1);
  3093. return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  3094. }
  3095. static int bnxt_hwrm_tunnel_dst_port_free(struct bnxt *bp, u8 tunnel_type)
  3096. {
  3097. u32 rc = 0;
  3098. struct hwrm_tunnel_dst_port_free_input req = {0};
  3099. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_TUNNEL_DST_PORT_FREE, -1, -1);
  3100. req.tunnel_type = tunnel_type;
  3101. switch (tunnel_type) {
  3102. case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN:
  3103. req.tunnel_dst_port_id = bp->vxlan_fw_dst_port_id;
  3104. break;
  3105. case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE:
  3106. req.tunnel_dst_port_id = bp->nge_fw_dst_port_id;
  3107. break;
  3108. default:
  3109. break;
  3110. }
  3111. rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  3112. if (rc)
  3113. netdev_err(bp->dev, "hwrm_tunnel_dst_port_free failed. rc:%d\n",
  3114. rc);
  3115. return rc;
  3116. }
  3117. static int bnxt_hwrm_tunnel_dst_port_alloc(struct bnxt *bp, __be16 port,
  3118. u8 tunnel_type)
  3119. {
  3120. u32 rc = 0;
  3121. struct hwrm_tunnel_dst_port_alloc_input req = {0};
  3122. struct hwrm_tunnel_dst_port_alloc_output *resp = bp->hwrm_cmd_resp_addr;
  3123. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_TUNNEL_DST_PORT_ALLOC, -1, -1);
  3124. req.tunnel_type = tunnel_type;
  3125. req.tunnel_dst_port_val = port;
  3126. mutex_lock(&bp->hwrm_cmd_lock);
  3127. rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  3128. if (rc) {
  3129. netdev_err(bp->dev, "hwrm_tunnel_dst_port_alloc failed. rc:%d\n",
  3130. rc);
  3131. goto err_out;
  3132. }
  3133. switch (tunnel_type) {
  3134. case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN:
  3135. bp->vxlan_fw_dst_port_id = resp->tunnel_dst_port_id;
  3136. break;
  3137. case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_GENEVE:
  3138. bp->nge_fw_dst_port_id = resp->tunnel_dst_port_id;
  3139. break;
  3140. default:
  3141. break;
  3142. }
  3143. err_out:
  3144. mutex_unlock(&bp->hwrm_cmd_lock);
  3145. return rc;
  3146. }
  3147. static int bnxt_hwrm_cfa_l2_set_rx_mask(struct bnxt *bp, u16 vnic_id)
  3148. {
  3149. struct hwrm_cfa_l2_set_rx_mask_input req = {0};
  3150. struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
  3151. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_L2_SET_RX_MASK, -1, -1);
  3152. req.vnic_id = cpu_to_le32(vnic->fw_vnic_id);
  3153. req.num_mc_entries = cpu_to_le32(vnic->mc_list_count);
  3154. req.mc_tbl_addr = cpu_to_le64(vnic->mc_list_mapping);
  3155. req.mask = cpu_to_le32(vnic->rx_mask);
  3156. return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  3157. }
  3158. #ifdef CONFIG_RFS_ACCEL
  3159. static int bnxt_hwrm_cfa_ntuple_filter_free(struct bnxt *bp,
  3160. struct bnxt_ntuple_filter *fltr)
  3161. {
  3162. struct hwrm_cfa_ntuple_filter_free_input req = {0};
  3163. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_NTUPLE_FILTER_FREE, -1, -1);
  3164. req.ntuple_filter_id = fltr->filter_id;
  3165. return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  3166. }
  3167. #define BNXT_NTP_FLTR_FLAGS \
  3168. (CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_L2_FILTER_ID | \
  3169. CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_ETHERTYPE | \
  3170. CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_MACADDR | \
  3171. CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IPADDR_TYPE | \
  3172. CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR | \
  3173. CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR_MASK | \
  3174. CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR | \
  3175. CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR_MASK | \
  3176. CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IP_PROTOCOL | \
  3177. CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT | \
  3178. CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT_MASK | \
  3179. CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT | \
  3180. CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT_MASK | \
  3181. CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_ID)
  3182. #define BNXT_NTP_TUNNEL_FLTR_FLAG \
  3183. CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_TUNNEL_TYPE
  3184. static int bnxt_hwrm_cfa_ntuple_filter_alloc(struct bnxt *bp,
  3185. struct bnxt_ntuple_filter *fltr)
  3186. {
  3187. int rc = 0;
  3188. struct hwrm_cfa_ntuple_filter_alloc_input req = {0};
  3189. struct hwrm_cfa_ntuple_filter_alloc_output *resp =
  3190. bp->hwrm_cmd_resp_addr;
  3191. struct flow_keys *keys = &fltr->fkeys;
  3192. struct bnxt_vnic_info *vnic = &bp->vnic_info[fltr->rxq + 1];
  3193. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_NTUPLE_FILTER_ALLOC, -1, -1);
  3194. req.l2_filter_id = bp->vnic_info[0].fw_l2_filter_id[fltr->l2_fltr_idx];
  3195. req.enables = cpu_to_le32(BNXT_NTP_FLTR_FLAGS);
  3196. req.ethertype = htons(ETH_P_IP);
  3197. memcpy(req.src_macaddr, fltr->src_mac_addr, ETH_ALEN);
  3198. req.ip_addr_type = CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV4;
  3199. req.ip_protocol = keys->basic.ip_proto;
  3200. if (keys->basic.n_proto == htons(ETH_P_IPV6)) {
  3201. int i;
  3202. req.ethertype = htons(ETH_P_IPV6);
  3203. req.ip_addr_type =
  3204. CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV6;
  3205. *(struct in6_addr *)&req.src_ipaddr[0] =
  3206. keys->addrs.v6addrs.src;
  3207. *(struct in6_addr *)&req.dst_ipaddr[0] =
  3208. keys->addrs.v6addrs.dst;
  3209. for (i = 0; i < 4; i++) {
  3210. req.src_ipaddr_mask[i] = cpu_to_be32(0xffffffff);
  3211. req.dst_ipaddr_mask[i] = cpu_to_be32(0xffffffff);
  3212. }
  3213. } else {
  3214. req.src_ipaddr[0] = keys->addrs.v4addrs.src;
  3215. req.src_ipaddr_mask[0] = cpu_to_be32(0xffffffff);
  3216. req.dst_ipaddr[0] = keys->addrs.v4addrs.dst;
  3217. req.dst_ipaddr_mask[0] = cpu_to_be32(0xffffffff);
  3218. }
  3219. if (keys->control.flags & FLOW_DIS_ENCAPSULATION) {
  3220. req.enables |= cpu_to_le32(BNXT_NTP_TUNNEL_FLTR_FLAG);
  3221. req.tunnel_type =
  3222. CFA_NTUPLE_FILTER_ALLOC_REQ_TUNNEL_TYPE_ANYTUNNEL;
  3223. }
  3224. req.src_port = keys->ports.src;
  3225. req.src_port_mask = cpu_to_be16(0xffff);
  3226. req.dst_port = keys->ports.dst;
  3227. req.dst_port_mask = cpu_to_be16(0xffff);
  3228. req.dst_id = cpu_to_le16(vnic->fw_vnic_id);
  3229. mutex_lock(&bp->hwrm_cmd_lock);
  3230. rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  3231. if (!rc)
  3232. fltr->filter_id = resp->ntuple_filter_id;
  3233. mutex_unlock(&bp->hwrm_cmd_lock);
  3234. return rc;
  3235. }
  3236. #endif
  3237. static int bnxt_hwrm_set_vnic_filter(struct bnxt *bp, u16 vnic_id, u16 idx,
  3238. u8 *mac_addr)
  3239. {
  3240. u32 rc = 0;
  3241. struct hwrm_cfa_l2_filter_alloc_input req = {0};
  3242. struct hwrm_cfa_l2_filter_alloc_output *resp = bp->hwrm_cmd_resp_addr;
  3243. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_L2_FILTER_ALLOC, -1, -1);
  3244. req.flags = cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_PATH_RX);
  3245. if (!BNXT_CHIP_TYPE_NITRO_A0(bp))
  3246. req.flags |=
  3247. cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_OUTERMOST);
  3248. req.dst_id = cpu_to_le16(bp->vnic_info[vnic_id].fw_vnic_id);
  3249. req.enables =
  3250. cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR |
  3251. CFA_L2_FILTER_ALLOC_REQ_ENABLES_DST_ID |
  3252. CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR_MASK);
  3253. memcpy(req.l2_addr, mac_addr, ETH_ALEN);
  3254. req.l2_addr_mask[0] = 0xff;
  3255. req.l2_addr_mask[1] = 0xff;
  3256. req.l2_addr_mask[2] = 0xff;
  3257. req.l2_addr_mask[3] = 0xff;
  3258. req.l2_addr_mask[4] = 0xff;
  3259. req.l2_addr_mask[5] = 0xff;
  3260. mutex_lock(&bp->hwrm_cmd_lock);
  3261. rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  3262. if (!rc)
  3263. bp->vnic_info[vnic_id].fw_l2_filter_id[idx] =
  3264. resp->l2_filter_id;
  3265. mutex_unlock(&bp->hwrm_cmd_lock);
  3266. return rc;
  3267. }
  3268. static int bnxt_hwrm_clear_vnic_filter(struct bnxt *bp)
  3269. {
  3270. u16 i, j, num_of_vnics = 1; /* only vnic 0 supported */
  3271. int rc = 0;
  3272. /* Any associated ntuple filters will also be cleared by firmware. */
  3273. mutex_lock(&bp->hwrm_cmd_lock);
  3274. for (i = 0; i < num_of_vnics; i++) {
  3275. struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
  3276. for (j = 0; j < vnic->uc_filter_count; j++) {
  3277. struct hwrm_cfa_l2_filter_free_input req = {0};
  3278. bnxt_hwrm_cmd_hdr_init(bp, &req,
  3279. HWRM_CFA_L2_FILTER_FREE, -1, -1);
  3280. req.l2_filter_id = vnic->fw_l2_filter_id[j];
  3281. rc = _hwrm_send_message(bp, &req, sizeof(req),
  3282. HWRM_CMD_TIMEOUT);
  3283. }
  3284. vnic->uc_filter_count = 0;
  3285. }
  3286. mutex_unlock(&bp->hwrm_cmd_lock);
  3287. return rc;
  3288. }
  3289. static int bnxt_hwrm_vnic_set_tpa(struct bnxt *bp, u16 vnic_id, u32 tpa_flags)
  3290. {
  3291. struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
  3292. struct hwrm_vnic_tpa_cfg_input req = {0};
  3293. if (vnic->fw_vnic_id == INVALID_HW_RING_ID)
  3294. return 0;
  3295. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_TPA_CFG, -1, -1);
  3296. if (tpa_flags) {
  3297. u16 mss = bp->dev->mtu - 40;
  3298. u32 nsegs, n, segs = 0, flags;
  3299. flags = VNIC_TPA_CFG_REQ_FLAGS_TPA |
  3300. VNIC_TPA_CFG_REQ_FLAGS_ENCAP_TPA |
  3301. VNIC_TPA_CFG_REQ_FLAGS_RSC_WND_UPDATE |
  3302. VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_ECN |
  3303. VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_SAME_GRE_SEQ;
  3304. if (tpa_flags & BNXT_FLAG_GRO)
  3305. flags |= VNIC_TPA_CFG_REQ_FLAGS_GRO;
  3306. req.flags = cpu_to_le32(flags);
  3307. req.enables =
  3308. cpu_to_le32(VNIC_TPA_CFG_REQ_ENABLES_MAX_AGG_SEGS |
  3309. VNIC_TPA_CFG_REQ_ENABLES_MAX_AGGS |
  3310. VNIC_TPA_CFG_REQ_ENABLES_MIN_AGG_LEN);
  3311. /* Number of segs are log2 units, and first packet is not
  3312. * included as part of this units.
  3313. */
  3314. if (mss <= BNXT_RX_PAGE_SIZE) {
  3315. n = BNXT_RX_PAGE_SIZE / mss;
  3316. nsegs = (MAX_SKB_FRAGS - 1) * n;
  3317. } else {
  3318. n = mss / BNXT_RX_PAGE_SIZE;
  3319. if (mss & (BNXT_RX_PAGE_SIZE - 1))
  3320. n++;
  3321. nsegs = (MAX_SKB_FRAGS - n) / n;
  3322. }
  3323. segs = ilog2(nsegs);
  3324. req.max_agg_segs = cpu_to_le16(segs);
  3325. req.max_aggs = cpu_to_le16(VNIC_TPA_CFG_REQ_MAX_AGGS_MAX);
  3326. req.min_agg_len = cpu_to_le32(512);
  3327. }
  3328. req.vnic_id = cpu_to_le16(vnic->fw_vnic_id);
  3329. return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  3330. }
  3331. static int bnxt_hwrm_vnic_set_rss(struct bnxt *bp, u16 vnic_id, bool set_rss)
  3332. {
  3333. u32 i, j, max_rings;
  3334. struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
  3335. struct hwrm_vnic_rss_cfg_input req = {0};
  3336. if (vnic->fw_rss_cos_lb_ctx[0] == INVALID_HW_RING_ID)
  3337. return 0;
  3338. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_RSS_CFG, -1, -1);
  3339. if (set_rss) {
  3340. req.hash_type = cpu_to_le32(bp->rss_hash_cfg);
  3341. req.hash_mode_flags = VNIC_RSS_CFG_REQ_HASH_MODE_FLAGS_DEFAULT;
  3342. if (vnic->flags & BNXT_VNIC_RSS_FLAG) {
  3343. if (BNXT_CHIP_TYPE_NITRO_A0(bp))
  3344. max_rings = bp->rx_nr_rings - 1;
  3345. else
  3346. max_rings = bp->rx_nr_rings;
  3347. } else {
  3348. max_rings = 1;
  3349. }
  3350. /* Fill the RSS indirection table with ring group ids */
  3351. for (i = 0, j = 0; i < HW_HASH_INDEX_SIZE; i++, j++) {
  3352. if (j == max_rings)
  3353. j = 0;
  3354. vnic->rss_table[i] = cpu_to_le16(vnic->fw_grp_ids[j]);
  3355. }
  3356. req.ring_grp_tbl_addr = cpu_to_le64(vnic->rss_table_dma_addr);
  3357. req.hash_key_tbl_addr =
  3358. cpu_to_le64(vnic->rss_hash_key_dma_addr);
  3359. }
  3360. req.rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
  3361. return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  3362. }
  3363. static int bnxt_hwrm_vnic_set_hds(struct bnxt *bp, u16 vnic_id)
  3364. {
  3365. struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
  3366. struct hwrm_vnic_plcmodes_cfg_input req = {0};
  3367. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_PLCMODES_CFG, -1, -1);
  3368. req.flags = cpu_to_le32(VNIC_PLCMODES_CFG_REQ_FLAGS_JUMBO_PLACEMENT |
  3369. VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV4 |
  3370. VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV6);
  3371. req.enables =
  3372. cpu_to_le32(VNIC_PLCMODES_CFG_REQ_ENABLES_JUMBO_THRESH_VALID |
  3373. VNIC_PLCMODES_CFG_REQ_ENABLES_HDS_THRESHOLD_VALID);
  3374. /* thresholds not implemented in firmware yet */
  3375. req.jumbo_thresh = cpu_to_le16(bp->rx_copy_thresh);
  3376. req.hds_threshold = cpu_to_le16(bp->rx_copy_thresh);
  3377. req.vnic_id = cpu_to_le32(vnic->fw_vnic_id);
  3378. return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  3379. }
  3380. static void bnxt_hwrm_vnic_ctx_free_one(struct bnxt *bp, u16 vnic_id,
  3381. u16 ctx_idx)
  3382. {
  3383. struct hwrm_vnic_rss_cos_lb_ctx_free_input req = {0};
  3384. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_RSS_COS_LB_CTX_FREE, -1, -1);
  3385. req.rss_cos_lb_ctx_id =
  3386. cpu_to_le16(bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[ctx_idx]);
  3387. hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  3388. bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[ctx_idx] = INVALID_HW_RING_ID;
  3389. }
  3390. static void bnxt_hwrm_vnic_ctx_free(struct bnxt *bp)
  3391. {
  3392. int i, j;
  3393. for (i = 0; i < bp->nr_vnics; i++) {
  3394. struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
  3395. for (j = 0; j < BNXT_MAX_CTX_PER_VNIC; j++) {
  3396. if (vnic->fw_rss_cos_lb_ctx[j] != INVALID_HW_RING_ID)
  3397. bnxt_hwrm_vnic_ctx_free_one(bp, i, j);
  3398. }
  3399. }
  3400. bp->rsscos_nr_ctxs = 0;
  3401. }
  3402. static int bnxt_hwrm_vnic_ctx_alloc(struct bnxt *bp, u16 vnic_id, u16 ctx_idx)
  3403. {
  3404. int rc;
  3405. struct hwrm_vnic_rss_cos_lb_ctx_alloc_input req = {0};
  3406. struct hwrm_vnic_rss_cos_lb_ctx_alloc_output *resp =
  3407. bp->hwrm_cmd_resp_addr;
  3408. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_RSS_COS_LB_CTX_ALLOC, -1,
  3409. -1);
  3410. mutex_lock(&bp->hwrm_cmd_lock);
  3411. rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  3412. if (!rc)
  3413. bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[ctx_idx] =
  3414. le16_to_cpu(resp->rss_cos_lb_ctx_id);
  3415. mutex_unlock(&bp->hwrm_cmd_lock);
  3416. return rc;
  3417. }
  3418. static u32 bnxt_get_roce_vnic_mode(struct bnxt *bp)
  3419. {
  3420. if (bp->flags & BNXT_FLAG_ROCE_MIRROR_CAP)
  3421. return VNIC_CFG_REQ_FLAGS_ROCE_MIRRORING_CAPABLE_VNIC_MODE;
  3422. return VNIC_CFG_REQ_FLAGS_ROCE_DUAL_VNIC_MODE;
  3423. }
  3424. int bnxt_hwrm_vnic_cfg(struct bnxt *bp, u16 vnic_id)
  3425. {
  3426. unsigned int ring = 0, grp_idx;
  3427. struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
  3428. struct hwrm_vnic_cfg_input req = {0};
  3429. u16 def_vlan = 0;
  3430. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_CFG, -1, -1);
  3431. req.enables = cpu_to_le32(VNIC_CFG_REQ_ENABLES_DFLT_RING_GRP);
  3432. /* Only RSS support for now TBD: COS & LB */
  3433. if (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID) {
  3434. req.rss_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
  3435. req.enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE |
  3436. VNIC_CFG_REQ_ENABLES_MRU);
  3437. } else if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG) {
  3438. req.rss_rule =
  3439. cpu_to_le16(bp->vnic_info[0].fw_rss_cos_lb_ctx[0]);
  3440. req.enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE |
  3441. VNIC_CFG_REQ_ENABLES_MRU);
  3442. req.flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_RSS_DFLT_CR_MODE);
  3443. } else {
  3444. req.rss_rule = cpu_to_le16(0xffff);
  3445. }
  3446. if (BNXT_CHIP_TYPE_NITRO_A0(bp) &&
  3447. (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID)) {
  3448. req.cos_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[1]);
  3449. req.enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_COS_RULE);
  3450. } else {
  3451. req.cos_rule = cpu_to_le16(0xffff);
  3452. }
  3453. if (vnic->flags & BNXT_VNIC_RSS_FLAG)
  3454. ring = 0;
  3455. else if (vnic->flags & BNXT_VNIC_RFS_FLAG)
  3456. ring = vnic_id - 1;
  3457. else if ((vnic_id == 1) && BNXT_CHIP_TYPE_NITRO_A0(bp))
  3458. ring = bp->rx_nr_rings - 1;
  3459. grp_idx = bp->rx_ring[ring].bnapi->index;
  3460. req.vnic_id = cpu_to_le16(vnic->fw_vnic_id);
  3461. req.dflt_ring_grp = cpu_to_le16(bp->grp_info[grp_idx].fw_grp_id);
  3462. req.lb_rule = cpu_to_le16(0xffff);
  3463. req.mru = cpu_to_le16(bp->dev->mtu + ETH_HLEN + ETH_FCS_LEN +
  3464. VLAN_HLEN);
  3465. #ifdef CONFIG_BNXT_SRIOV
  3466. if (BNXT_VF(bp))
  3467. def_vlan = bp->vf.vlan;
  3468. #endif
  3469. if ((bp->flags & BNXT_FLAG_STRIP_VLAN) || def_vlan)
  3470. req.flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_VLAN_STRIP_MODE);
  3471. if (!vnic_id && bnxt_ulp_registered(bp->edev, BNXT_ROCE_ULP))
  3472. req.flags |= cpu_to_le32(bnxt_get_roce_vnic_mode(bp));
  3473. return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  3474. }
  3475. static int bnxt_hwrm_vnic_free_one(struct bnxt *bp, u16 vnic_id)
  3476. {
  3477. u32 rc = 0;
  3478. if (bp->vnic_info[vnic_id].fw_vnic_id != INVALID_HW_RING_ID) {
  3479. struct hwrm_vnic_free_input req = {0};
  3480. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_FREE, -1, -1);
  3481. req.vnic_id =
  3482. cpu_to_le32(bp->vnic_info[vnic_id].fw_vnic_id);
  3483. rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  3484. if (rc)
  3485. return rc;
  3486. bp->vnic_info[vnic_id].fw_vnic_id = INVALID_HW_RING_ID;
  3487. }
  3488. return rc;
  3489. }
  3490. static void bnxt_hwrm_vnic_free(struct bnxt *bp)
  3491. {
  3492. u16 i;
  3493. for (i = 0; i < bp->nr_vnics; i++)
  3494. bnxt_hwrm_vnic_free_one(bp, i);
  3495. }
  3496. static int bnxt_hwrm_vnic_alloc(struct bnxt *bp, u16 vnic_id,
  3497. unsigned int start_rx_ring_idx,
  3498. unsigned int nr_rings)
  3499. {
  3500. int rc = 0;
  3501. unsigned int i, j, grp_idx, end_idx = start_rx_ring_idx + nr_rings;
  3502. struct hwrm_vnic_alloc_input req = {0};
  3503. struct hwrm_vnic_alloc_output *resp = bp->hwrm_cmd_resp_addr;
  3504. /* map ring groups to this vnic */
  3505. for (i = start_rx_ring_idx, j = 0; i < end_idx; i++, j++) {
  3506. grp_idx = bp->rx_ring[i].bnapi->index;
  3507. if (bp->grp_info[grp_idx].fw_grp_id == INVALID_HW_RING_ID) {
  3508. netdev_err(bp->dev, "Not enough ring groups avail:%x req:%x\n",
  3509. j, nr_rings);
  3510. break;
  3511. }
  3512. bp->vnic_info[vnic_id].fw_grp_ids[j] =
  3513. bp->grp_info[grp_idx].fw_grp_id;
  3514. }
  3515. bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[0] = INVALID_HW_RING_ID;
  3516. bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[1] = INVALID_HW_RING_ID;
  3517. if (vnic_id == 0)
  3518. req.flags = cpu_to_le32(VNIC_ALLOC_REQ_FLAGS_DEFAULT);
  3519. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_ALLOC, -1, -1);
  3520. mutex_lock(&bp->hwrm_cmd_lock);
  3521. rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  3522. if (!rc)
  3523. bp->vnic_info[vnic_id].fw_vnic_id = le32_to_cpu(resp->vnic_id);
  3524. mutex_unlock(&bp->hwrm_cmd_lock);
  3525. return rc;
  3526. }
  3527. static int bnxt_hwrm_vnic_qcaps(struct bnxt *bp)
  3528. {
  3529. struct hwrm_vnic_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
  3530. struct hwrm_vnic_qcaps_input req = {0};
  3531. int rc;
  3532. if (bp->hwrm_spec_code < 0x10600)
  3533. return 0;
  3534. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_QCAPS, -1, -1);
  3535. mutex_lock(&bp->hwrm_cmd_lock);
  3536. rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  3537. if (!rc) {
  3538. u32 flags = le32_to_cpu(resp->flags);
  3539. if (flags & VNIC_QCAPS_RESP_FLAGS_RSS_DFLT_CR_CAP)
  3540. bp->flags |= BNXT_FLAG_NEW_RSS_CAP;
  3541. if (flags &
  3542. VNIC_QCAPS_RESP_FLAGS_ROCE_MIRRORING_CAPABLE_VNIC_CAP)
  3543. bp->flags |= BNXT_FLAG_ROCE_MIRROR_CAP;
  3544. }
  3545. mutex_unlock(&bp->hwrm_cmd_lock);
  3546. return rc;
  3547. }
  3548. static int bnxt_hwrm_ring_grp_alloc(struct bnxt *bp)
  3549. {
  3550. u16 i;
  3551. u32 rc = 0;
  3552. mutex_lock(&bp->hwrm_cmd_lock);
  3553. for (i = 0; i < bp->rx_nr_rings; i++) {
  3554. struct hwrm_ring_grp_alloc_input req = {0};
  3555. struct hwrm_ring_grp_alloc_output *resp =
  3556. bp->hwrm_cmd_resp_addr;
  3557. unsigned int grp_idx = bp->rx_ring[i].bnapi->index;
  3558. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_GRP_ALLOC, -1, -1);
  3559. req.cr = cpu_to_le16(bp->grp_info[grp_idx].cp_fw_ring_id);
  3560. req.rr = cpu_to_le16(bp->grp_info[grp_idx].rx_fw_ring_id);
  3561. req.ar = cpu_to_le16(bp->grp_info[grp_idx].agg_fw_ring_id);
  3562. req.sc = cpu_to_le16(bp->grp_info[grp_idx].fw_stats_ctx);
  3563. rc = _hwrm_send_message(bp, &req, sizeof(req),
  3564. HWRM_CMD_TIMEOUT);
  3565. if (rc)
  3566. break;
  3567. bp->grp_info[grp_idx].fw_grp_id =
  3568. le32_to_cpu(resp->ring_group_id);
  3569. }
  3570. mutex_unlock(&bp->hwrm_cmd_lock);
  3571. return rc;
  3572. }
  3573. static int bnxt_hwrm_ring_grp_free(struct bnxt *bp)
  3574. {
  3575. u16 i;
  3576. u32 rc = 0;
  3577. struct hwrm_ring_grp_free_input req = {0};
  3578. if (!bp->grp_info)
  3579. return 0;
  3580. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_GRP_FREE, -1, -1);
  3581. mutex_lock(&bp->hwrm_cmd_lock);
  3582. for (i = 0; i < bp->cp_nr_rings; i++) {
  3583. if (bp->grp_info[i].fw_grp_id == INVALID_HW_RING_ID)
  3584. continue;
  3585. req.ring_group_id =
  3586. cpu_to_le32(bp->grp_info[i].fw_grp_id);
  3587. rc = _hwrm_send_message(bp, &req, sizeof(req),
  3588. HWRM_CMD_TIMEOUT);
  3589. if (rc)
  3590. break;
  3591. bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
  3592. }
  3593. mutex_unlock(&bp->hwrm_cmd_lock);
  3594. return rc;
  3595. }
  3596. static int hwrm_ring_alloc_send_msg(struct bnxt *bp,
  3597. struct bnxt_ring_struct *ring,
  3598. u32 ring_type, u32 map_index)
  3599. {
  3600. int rc = 0, err = 0;
  3601. struct hwrm_ring_alloc_input req = {0};
  3602. struct hwrm_ring_alloc_output *resp = bp->hwrm_cmd_resp_addr;
  3603. struct bnxt_ring_grp_info *grp_info;
  3604. u16 ring_id;
  3605. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_ALLOC, -1, -1);
  3606. req.enables = 0;
  3607. if (ring->nr_pages > 1) {
  3608. req.page_tbl_addr = cpu_to_le64(ring->pg_tbl_map);
  3609. /* Page size is in log2 units */
  3610. req.page_size = BNXT_PAGE_SHIFT;
  3611. req.page_tbl_depth = 1;
  3612. } else {
  3613. req.page_tbl_addr = cpu_to_le64(ring->dma_arr[0]);
  3614. }
  3615. req.fbo = 0;
  3616. /* Association of ring index with doorbell index and MSIX number */
  3617. req.logical_id = cpu_to_le16(map_index);
  3618. switch (ring_type) {
  3619. case HWRM_RING_ALLOC_TX:
  3620. req.ring_type = RING_ALLOC_REQ_RING_TYPE_TX;
  3621. /* Association of transmit ring with completion ring */
  3622. grp_info = &bp->grp_info[ring->grp_idx];
  3623. req.cmpl_ring_id = cpu_to_le16(grp_info->cp_fw_ring_id);
  3624. req.length = cpu_to_le32(bp->tx_ring_mask + 1);
  3625. req.stat_ctx_id = cpu_to_le32(grp_info->fw_stats_ctx);
  3626. req.queue_id = cpu_to_le16(ring->queue_id);
  3627. break;
  3628. case HWRM_RING_ALLOC_RX:
  3629. req.ring_type = RING_ALLOC_REQ_RING_TYPE_RX;
  3630. req.length = cpu_to_le32(bp->rx_ring_mask + 1);
  3631. break;
  3632. case HWRM_RING_ALLOC_AGG:
  3633. req.ring_type = RING_ALLOC_REQ_RING_TYPE_RX;
  3634. req.length = cpu_to_le32(bp->rx_agg_ring_mask + 1);
  3635. break;
  3636. case HWRM_RING_ALLOC_CMPL:
  3637. req.ring_type = RING_ALLOC_REQ_RING_TYPE_L2_CMPL;
  3638. req.length = cpu_to_le32(bp->cp_ring_mask + 1);
  3639. if (bp->flags & BNXT_FLAG_USING_MSIX)
  3640. req.int_mode = RING_ALLOC_REQ_INT_MODE_MSIX;
  3641. break;
  3642. default:
  3643. netdev_err(bp->dev, "hwrm alloc invalid ring type %d\n",
  3644. ring_type);
  3645. return -1;
  3646. }
  3647. mutex_lock(&bp->hwrm_cmd_lock);
  3648. rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  3649. err = le16_to_cpu(resp->error_code);
  3650. ring_id = le16_to_cpu(resp->ring_id);
  3651. mutex_unlock(&bp->hwrm_cmd_lock);
  3652. if (rc || err) {
  3653. netdev_err(bp->dev, "hwrm_ring_alloc type %d failed. rc:%x err:%x\n",
  3654. ring_type, rc, err);
  3655. return -EIO;
  3656. }
  3657. ring->fw_ring_id = ring_id;
  3658. return rc;
  3659. }
  3660. static int bnxt_hwrm_set_async_event_cr(struct bnxt *bp, int idx)
  3661. {
  3662. int rc;
  3663. if (BNXT_PF(bp)) {
  3664. struct hwrm_func_cfg_input req = {0};
  3665. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_CFG, -1, -1);
  3666. req.fid = cpu_to_le16(0xffff);
  3667. req.enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_ASYNC_EVENT_CR);
  3668. req.async_event_cr = cpu_to_le16(idx);
  3669. rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  3670. } else {
  3671. struct hwrm_func_vf_cfg_input req = {0};
  3672. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_VF_CFG, -1, -1);
  3673. req.enables =
  3674. cpu_to_le32(FUNC_VF_CFG_REQ_ENABLES_ASYNC_EVENT_CR);
  3675. req.async_event_cr = cpu_to_le16(idx);
  3676. rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  3677. }
  3678. return rc;
  3679. }
  3680. static int bnxt_hwrm_ring_alloc(struct bnxt *bp)
  3681. {
  3682. int i, rc = 0;
  3683. for (i = 0; i < bp->cp_nr_rings; i++) {
  3684. struct bnxt_napi *bnapi = bp->bnapi[i];
  3685. struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
  3686. struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
  3687. u32 map_idx = ring->map_idx;
  3688. cpr->cp_doorbell = bp->bar1 + map_idx * 0x80;
  3689. rc = hwrm_ring_alloc_send_msg(bp, ring, HWRM_RING_ALLOC_CMPL,
  3690. map_idx);
  3691. if (rc)
  3692. goto err_out;
  3693. BNXT_CP_DB(cpr->cp_doorbell, cpr->cp_raw_cons);
  3694. bp->grp_info[i].cp_fw_ring_id = ring->fw_ring_id;
  3695. if (!i) {
  3696. rc = bnxt_hwrm_set_async_event_cr(bp, ring->fw_ring_id);
  3697. if (rc)
  3698. netdev_warn(bp->dev, "Failed to set async event completion ring.\n");
  3699. }
  3700. }
  3701. for (i = 0; i < bp->tx_nr_rings; i++) {
  3702. struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
  3703. struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
  3704. u32 map_idx = i;
  3705. rc = hwrm_ring_alloc_send_msg(bp, ring, HWRM_RING_ALLOC_TX,
  3706. map_idx);
  3707. if (rc)
  3708. goto err_out;
  3709. txr->tx_doorbell = bp->bar1 + map_idx * 0x80;
  3710. }
  3711. for (i = 0; i < bp->rx_nr_rings; i++) {
  3712. struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
  3713. struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
  3714. u32 map_idx = rxr->bnapi->index;
  3715. rc = hwrm_ring_alloc_send_msg(bp, ring, HWRM_RING_ALLOC_RX,
  3716. map_idx);
  3717. if (rc)
  3718. goto err_out;
  3719. rxr->rx_doorbell = bp->bar1 + map_idx * 0x80;
  3720. writel(DB_KEY_RX | rxr->rx_prod, rxr->rx_doorbell);
  3721. bp->grp_info[map_idx].rx_fw_ring_id = ring->fw_ring_id;
  3722. }
  3723. if (bp->flags & BNXT_FLAG_AGG_RINGS) {
  3724. for (i = 0; i < bp->rx_nr_rings; i++) {
  3725. struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
  3726. struct bnxt_ring_struct *ring =
  3727. &rxr->rx_agg_ring_struct;
  3728. u32 grp_idx = ring->grp_idx;
  3729. u32 map_idx = grp_idx + bp->rx_nr_rings;
  3730. rc = hwrm_ring_alloc_send_msg(bp, ring,
  3731. HWRM_RING_ALLOC_AGG,
  3732. map_idx);
  3733. if (rc)
  3734. goto err_out;
  3735. rxr->rx_agg_doorbell = bp->bar1 + map_idx * 0x80;
  3736. writel(DB_KEY_RX | rxr->rx_agg_prod,
  3737. rxr->rx_agg_doorbell);
  3738. bp->grp_info[grp_idx].agg_fw_ring_id = ring->fw_ring_id;
  3739. }
  3740. }
  3741. err_out:
  3742. return rc;
  3743. }
  3744. static int hwrm_ring_free_send_msg(struct bnxt *bp,
  3745. struct bnxt_ring_struct *ring,
  3746. u32 ring_type, int cmpl_ring_id)
  3747. {
  3748. int rc;
  3749. struct hwrm_ring_free_input req = {0};
  3750. struct hwrm_ring_free_output *resp = bp->hwrm_cmd_resp_addr;
  3751. u16 error_code;
  3752. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_FREE, cmpl_ring_id, -1);
  3753. req.ring_type = ring_type;
  3754. req.ring_id = cpu_to_le16(ring->fw_ring_id);
  3755. mutex_lock(&bp->hwrm_cmd_lock);
  3756. rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  3757. error_code = le16_to_cpu(resp->error_code);
  3758. mutex_unlock(&bp->hwrm_cmd_lock);
  3759. if (rc || error_code) {
  3760. netdev_err(bp->dev, "hwrm_ring_free type %d failed. rc:%x err:%x\n",
  3761. ring_type, rc, error_code);
  3762. return -EIO;
  3763. }
  3764. return 0;
  3765. }
  3766. static void bnxt_hwrm_ring_free(struct bnxt *bp, bool close_path)
  3767. {
  3768. int i;
  3769. if (!bp->bnapi)
  3770. return;
  3771. for (i = 0; i < bp->tx_nr_rings; i++) {
  3772. struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
  3773. struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
  3774. u32 grp_idx = txr->bnapi->index;
  3775. u32 cmpl_ring_id = bp->grp_info[grp_idx].cp_fw_ring_id;
  3776. if (ring->fw_ring_id != INVALID_HW_RING_ID) {
  3777. hwrm_ring_free_send_msg(bp, ring,
  3778. RING_FREE_REQ_RING_TYPE_TX,
  3779. close_path ? cmpl_ring_id :
  3780. INVALID_HW_RING_ID);
  3781. ring->fw_ring_id = INVALID_HW_RING_ID;
  3782. }
  3783. }
  3784. for (i = 0; i < bp->rx_nr_rings; i++) {
  3785. struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
  3786. struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
  3787. u32 grp_idx = rxr->bnapi->index;
  3788. u32 cmpl_ring_id = bp->grp_info[grp_idx].cp_fw_ring_id;
  3789. if (ring->fw_ring_id != INVALID_HW_RING_ID) {
  3790. hwrm_ring_free_send_msg(bp, ring,
  3791. RING_FREE_REQ_RING_TYPE_RX,
  3792. close_path ? cmpl_ring_id :
  3793. INVALID_HW_RING_ID);
  3794. ring->fw_ring_id = INVALID_HW_RING_ID;
  3795. bp->grp_info[grp_idx].rx_fw_ring_id =
  3796. INVALID_HW_RING_ID;
  3797. }
  3798. }
  3799. for (i = 0; i < bp->rx_nr_rings; i++) {
  3800. struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
  3801. struct bnxt_ring_struct *ring = &rxr->rx_agg_ring_struct;
  3802. u32 grp_idx = rxr->bnapi->index;
  3803. u32 cmpl_ring_id = bp->grp_info[grp_idx].cp_fw_ring_id;
  3804. if (ring->fw_ring_id != INVALID_HW_RING_ID) {
  3805. hwrm_ring_free_send_msg(bp, ring,
  3806. RING_FREE_REQ_RING_TYPE_RX,
  3807. close_path ? cmpl_ring_id :
  3808. INVALID_HW_RING_ID);
  3809. ring->fw_ring_id = INVALID_HW_RING_ID;
  3810. bp->grp_info[grp_idx].agg_fw_ring_id =
  3811. INVALID_HW_RING_ID;
  3812. }
  3813. }
  3814. /* The completion rings are about to be freed. After that the
  3815. * IRQ doorbell will not work anymore. So we need to disable
  3816. * IRQ here.
  3817. */
  3818. bnxt_disable_int_sync(bp);
  3819. for (i = 0; i < bp->cp_nr_rings; i++) {
  3820. struct bnxt_napi *bnapi = bp->bnapi[i];
  3821. struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
  3822. struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
  3823. if (ring->fw_ring_id != INVALID_HW_RING_ID) {
  3824. hwrm_ring_free_send_msg(bp, ring,
  3825. RING_FREE_REQ_RING_TYPE_L2_CMPL,
  3826. INVALID_HW_RING_ID);
  3827. ring->fw_ring_id = INVALID_HW_RING_ID;
  3828. bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
  3829. }
  3830. }
  3831. }
  3832. static int bnxt_hwrm_get_rings(struct bnxt *bp)
  3833. {
  3834. struct hwrm_func_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
  3835. struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
  3836. struct hwrm_func_qcfg_input req = {0};
  3837. int rc;
  3838. if (bp->hwrm_spec_code < 0x10601)
  3839. return 0;
  3840. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_QCFG, -1, -1);
  3841. req.fid = cpu_to_le16(0xffff);
  3842. mutex_lock(&bp->hwrm_cmd_lock);
  3843. rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  3844. if (rc) {
  3845. mutex_unlock(&bp->hwrm_cmd_lock);
  3846. return -EIO;
  3847. }
  3848. hw_resc->resv_tx_rings = le16_to_cpu(resp->alloc_tx_rings);
  3849. if (BNXT_NEW_RM(bp)) {
  3850. u16 cp, stats;
  3851. hw_resc->resv_rx_rings = le16_to_cpu(resp->alloc_rx_rings);
  3852. hw_resc->resv_hw_ring_grps =
  3853. le32_to_cpu(resp->alloc_hw_ring_grps);
  3854. hw_resc->resv_vnics = le16_to_cpu(resp->alloc_vnics);
  3855. cp = le16_to_cpu(resp->alloc_cmpl_rings);
  3856. stats = le16_to_cpu(resp->alloc_stat_ctx);
  3857. cp = min_t(u16, cp, stats);
  3858. hw_resc->resv_cp_rings = cp;
  3859. }
  3860. mutex_unlock(&bp->hwrm_cmd_lock);
  3861. return 0;
  3862. }
  3863. /* Caller must hold bp->hwrm_cmd_lock */
  3864. int __bnxt_hwrm_get_tx_rings(struct bnxt *bp, u16 fid, int *tx_rings)
  3865. {
  3866. struct hwrm_func_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
  3867. struct hwrm_func_qcfg_input req = {0};
  3868. int rc;
  3869. if (bp->hwrm_spec_code < 0x10601)
  3870. return 0;
  3871. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_QCFG, -1, -1);
  3872. req.fid = cpu_to_le16(fid);
  3873. rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  3874. if (!rc)
  3875. *tx_rings = le16_to_cpu(resp->alloc_tx_rings);
  3876. return rc;
  3877. }
  3878. static void
  3879. __bnxt_hwrm_reserve_pf_rings(struct bnxt *bp, struct hwrm_func_cfg_input *req,
  3880. int tx_rings, int rx_rings, int ring_grps,
  3881. int cp_rings, int vnics)
  3882. {
  3883. u32 enables = 0;
  3884. bnxt_hwrm_cmd_hdr_init(bp, req, HWRM_FUNC_CFG, -1, -1);
  3885. req->fid = cpu_to_le16(0xffff);
  3886. enables |= tx_rings ? FUNC_CFG_REQ_ENABLES_NUM_TX_RINGS : 0;
  3887. req->num_tx_rings = cpu_to_le16(tx_rings);
  3888. if (BNXT_NEW_RM(bp)) {
  3889. enables |= rx_rings ? FUNC_CFG_REQ_ENABLES_NUM_RX_RINGS : 0;
  3890. enables |= cp_rings ? FUNC_CFG_REQ_ENABLES_NUM_CMPL_RINGS |
  3891. FUNC_CFG_REQ_ENABLES_NUM_STAT_CTXS : 0;
  3892. enables |= ring_grps ?
  3893. FUNC_CFG_REQ_ENABLES_NUM_HW_RING_GRPS : 0;
  3894. enables |= vnics ? FUNC_CFG_REQ_ENABLES_NUM_VNICS : 0;
  3895. req->num_rx_rings = cpu_to_le16(rx_rings);
  3896. req->num_hw_ring_grps = cpu_to_le16(ring_grps);
  3897. req->num_cmpl_rings = cpu_to_le16(cp_rings);
  3898. req->num_stat_ctxs = req->num_cmpl_rings;
  3899. req->num_vnics = cpu_to_le16(vnics);
  3900. }
  3901. req->enables = cpu_to_le32(enables);
  3902. }
  3903. static void
  3904. __bnxt_hwrm_reserve_vf_rings(struct bnxt *bp,
  3905. struct hwrm_func_vf_cfg_input *req, int tx_rings,
  3906. int rx_rings, int ring_grps, int cp_rings,
  3907. int vnics)
  3908. {
  3909. u32 enables = 0;
  3910. bnxt_hwrm_cmd_hdr_init(bp, req, HWRM_FUNC_VF_CFG, -1, -1);
  3911. enables |= tx_rings ? FUNC_VF_CFG_REQ_ENABLES_NUM_TX_RINGS : 0;
  3912. enables |= rx_rings ? FUNC_VF_CFG_REQ_ENABLES_NUM_RX_RINGS : 0;
  3913. enables |= cp_rings ? FUNC_VF_CFG_REQ_ENABLES_NUM_CMPL_RINGS |
  3914. FUNC_VF_CFG_REQ_ENABLES_NUM_STAT_CTXS : 0;
  3915. enables |= ring_grps ? FUNC_VF_CFG_REQ_ENABLES_NUM_HW_RING_GRPS : 0;
  3916. enables |= vnics ? FUNC_VF_CFG_REQ_ENABLES_NUM_VNICS : 0;
  3917. req->num_tx_rings = cpu_to_le16(tx_rings);
  3918. req->num_rx_rings = cpu_to_le16(rx_rings);
  3919. req->num_hw_ring_grps = cpu_to_le16(ring_grps);
  3920. req->num_cmpl_rings = cpu_to_le16(cp_rings);
  3921. req->num_stat_ctxs = req->num_cmpl_rings;
  3922. req->num_vnics = cpu_to_le16(vnics);
  3923. req->enables = cpu_to_le32(enables);
  3924. }
  3925. static int
  3926. bnxt_hwrm_reserve_pf_rings(struct bnxt *bp, int tx_rings, int rx_rings,
  3927. int ring_grps, int cp_rings, int vnics)
  3928. {
  3929. struct hwrm_func_cfg_input req = {0};
  3930. int rc;
  3931. __bnxt_hwrm_reserve_pf_rings(bp, &req, tx_rings, rx_rings, ring_grps,
  3932. cp_rings, vnics);
  3933. if (!req.enables)
  3934. return 0;
  3935. rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  3936. if (rc)
  3937. return -ENOMEM;
  3938. if (bp->hwrm_spec_code < 0x10601)
  3939. bp->hw_resc.resv_tx_rings = tx_rings;
  3940. rc = bnxt_hwrm_get_rings(bp);
  3941. return rc;
  3942. }
  3943. static int
  3944. bnxt_hwrm_reserve_vf_rings(struct bnxt *bp, int tx_rings, int rx_rings,
  3945. int ring_grps, int cp_rings, int vnics)
  3946. {
  3947. struct hwrm_func_vf_cfg_input req = {0};
  3948. int rc;
  3949. if (!BNXT_NEW_RM(bp)) {
  3950. bp->hw_resc.resv_tx_rings = tx_rings;
  3951. return 0;
  3952. }
  3953. __bnxt_hwrm_reserve_vf_rings(bp, &req, tx_rings, rx_rings, ring_grps,
  3954. cp_rings, vnics);
  3955. req.enables |= cpu_to_le32(FUNC_VF_CFG_REQ_ENABLES_NUM_RSSCOS_CTXS |
  3956. FUNC_VF_CFG_REQ_ENABLES_NUM_L2_CTXS);
  3957. req.num_rsscos_ctxs = cpu_to_le16(BNXT_VF_MAX_RSS_CTX);
  3958. req.num_l2_ctxs = cpu_to_le16(BNXT_VF_MAX_L2_CTX);
  3959. rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  3960. if (rc)
  3961. return -ENOMEM;
  3962. rc = bnxt_hwrm_get_rings(bp);
  3963. return rc;
  3964. }
  3965. static int bnxt_hwrm_reserve_rings(struct bnxt *bp, int tx, int rx, int grp,
  3966. int cp, int vnic)
  3967. {
  3968. if (BNXT_PF(bp))
  3969. return bnxt_hwrm_reserve_pf_rings(bp, tx, rx, grp, cp, vnic);
  3970. else
  3971. return bnxt_hwrm_reserve_vf_rings(bp, tx, rx, grp, cp, vnic);
  3972. }
  3973. static int bnxt_cp_rings_in_use(struct bnxt *bp)
  3974. {
  3975. int cp = bp->cp_nr_rings;
  3976. int ulp_msix, ulp_base;
  3977. ulp_msix = bnxt_get_ulp_msix_num(bp);
  3978. if (ulp_msix) {
  3979. ulp_base = bnxt_get_ulp_msix_base(bp);
  3980. cp += ulp_msix;
  3981. if ((ulp_base + ulp_msix) > cp)
  3982. cp = ulp_base + ulp_msix;
  3983. }
  3984. return cp;
  3985. }
  3986. static bool bnxt_need_reserve_rings(struct bnxt *bp)
  3987. {
  3988. struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
  3989. int cp = bnxt_cp_rings_in_use(bp);
  3990. int rx = bp->rx_nr_rings;
  3991. int vnic = 1, grp = rx;
  3992. if (bp->hwrm_spec_code < 0x10601)
  3993. return false;
  3994. if (hw_resc->resv_tx_rings != bp->tx_nr_rings)
  3995. return true;
  3996. if (bp->flags & BNXT_FLAG_RFS)
  3997. vnic = rx + 1;
  3998. if (bp->flags & BNXT_FLAG_AGG_RINGS)
  3999. rx <<= 1;
  4000. if (BNXT_NEW_RM(bp) &&
  4001. (hw_resc->resv_rx_rings != rx || hw_resc->resv_cp_rings != cp ||
  4002. hw_resc->resv_hw_ring_grps != grp || hw_resc->resv_vnics != vnic))
  4003. return true;
  4004. return false;
  4005. }
  4006. static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
  4007. bool shared);
  4008. static int __bnxt_reserve_rings(struct bnxt *bp)
  4009. {
  4010. struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
  4011. int cp = bnxt_cp_rings_in_use(bp);
  4012. int tx = bp->tx_nr_rings;
  4013. int rx = bp->rx_nr_rings;
  4014. int grp, rx_rings, rc;
  4015. bool sh = false;
  4016. int vnic = 1;
  4017. if (!bnxt_need_reserve_rings(bp))
  4018. return 0;
  4019. if (bp->flags & BNXT_FLAG_SHARED_RINGS)
  4020. sh = true;
  4021. if (bp->flags & BNXT_FLAG_RFS)
  4022. vnic = rx + 1;
  4023. if (bp->flags & BNXT_FLAG_AGG_RINGS)
  4024. rx <<= 1;
  4025. grp = bp->rx_nr_rings;
  4026. rc = bnxt_hwrm_reserve_rings(bp, tx, rx, grp, cp, vnic);
  4027. if (rc)
  4028. return rc;
  4029. tx = hw_resc->resv_tx_rings;
  4030. if (BNXT_NEW_RM(bp)) {
  4031. rx = hw_resc->resv_rx_rings;
  4032. cp = hw_resc->resv_cp_rings;
  4033. grp = hw_resc->resv_hw_ring_grps;
  4034. vnic = hw_resc->resv_vnics;
  4035. }
  4036. rx_rings = rx;
  4037. if (bp->flags & BNXT_FLAG_AGG_RINGS) {
  4038. if (rx >= 2) {
  4039. rx_rings = rx >> 1;
  4040. } else {
  4041. if (netif_running(bp->dev))
  4042. return -ENOMEM;
  4043. bp->flags &= ~BNXT_FLAG_AGG_RINGS;
  4044. bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
  4045. bp->dev->hw_features &= ~NETIF_F_LRO;
  4046. bp->dev->features &= ~NETIF_F_LRO;
  4047. bnxt_set_ring_params(bp);
  4048. }
  4049. }
  4050. rx_rings = min_t(int, rx_rings, grp);
  4051. rc = bnxt_trim_rings(bp, &rx_rings, &tx, cp, sh);
  4052. if (bp->flags & BNXT_FLAG_AGG_RINGS)
  4053. rx = rx_rings << 1;
  4054. cp = sh ? max_t(int, tx, rx_rings) : tx + rx_rings;
  4055. bp->tx_nr_rings = tx;
  4056. bp->rx_nr_rings = rx_rings;
  4057. bp->cp_nr_rings = cp;
  4058. if (!tx || !rx || !cp || !grp || !vnic)
  4059. return -ENOMEM;
  4060. return rc;
  4061. }
  4062. static int bnxt_hwrm_check_vf_rings(struct bnxt *bp, int tx_rings, int rx_rings,
  4063. int ring_grps, int cp_rings, int vnics)
  4064. {
  4065. struct hwrm_func_vf_cfg_input req = {0};
  4066. u32 flags;
  4067. int rc;
  4068. if (!BNXT_NEW_RM(bp))
  4069. return 0;
  4070. __bnxt_hwrm_reserve_vf_rings(bp, &req, tx_rings, rx_rings, ring_grps,
  4071. cp_rings, vnics);
  4072. flags = FUNC_VF_CFG_REQ_FLAGS_TX_ASSETS_TEST |
  4073. FUNC_VF_CFG_REQ_FLAGS_RX_ASSETS_TEST |
  4074. FUNC_VF_CFG_REQ_FLAGS_CMPL_ASSETS_TEST |
  4075. FUNC_VF_CFG_REQ_FLAGS_RING_GRP_ASSETS_TEST |
  4076. FUNC_VF_CFG_REQ_FLAGS_STAT_CTX_ASSETS_TEST |
  4077. FUNC_VF_CFG_REQ_FLAGS_VNIC_ASSETS_TEST;
  4078. req.flags = cpu_to_le32(flags);
  4079. rc = hwrm_send_message_silent(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  4080. if (rc)
  4081. return -ENOMEM;
  4082. return 0;
  4083. }
  4084. static int bnxt_hwrm_check_pf_rings(struct bnxt *bp, int tx_rings, int rx_rings,
  4085. int ring_grps, int cp_rings, int vnics)
  4086. {
  4087. struct hwrm_func_cfg_input req = {0};
  4088. u32 flags;
  4089. int rc;
  4090. __bnxt_hwrm_reserve_pf_rings(bp, &req, tx_rings, rx_rings, ring_grps,
  4091. cp_rings, vnics);
  4092. flags = FUNC_CFG_REQ_FLAGS_TX_ASSETS_TEST;
  4093. if (BNXT_NEW_RM(bp))
  4094. flags |= FUNC_CFG_REQ_FLAGS_RX_ASSETS_TEST |
  4095. FUNC_CFG_REQ_FLAGS_CMPL_ASSETS_TEST |
  4096. FUNC_CFG_REQ_FLAGS_RING_GRP_ASSETS_TEST |
  4097. FUNC_CFG_REQ_FLAGS_STAT_CTX_ASSETS_TEST |
  4098. FUNC_CFG_REQ_FLAGS_VNIC_ASSETS_TEST;
  4099. req.flags = cpu_to_le32(flags);
  4100. rc = hwrm_send_message_silent(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  4101. if (rc)
  4102. return -ENOMEM;
  4103. return 0;
  4104. }
  4105. static int bnxt_hwrm_check_rings(struct bnxt *bp, int tx_rings, int rx_rings,
  4106. int ring_grps, int cp_rings, int vnics)
  4107. {
  4108. if (bp->hwrm_spec_code < 0x10801)
  4109. return 0;
  4110. if (BNXT_PF(bp))
  4111. return bnxt_hwrm_check_pf_rings(bp, tx_rings, rx_rings,
  4112. ring_grps, cp_rings, vnics);
  4113. return bnxt_hwrm_check_vf_rings(bp, tx_rings, rx_rings, ring_grps,
  4114. cp_rings, vnics);
  4115. }
  4116. static void bnxt_hwrm_set_coal_params(struct bnxt_coal *hw_coal,
  4117. struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
  4118. {
  4119. u16 val, tmr, max, flags;
  4120. max = hw_coal->bufs_per_record * 128;
  4121. if (hw_coal->budget)
  4122. max = hw_coal->bufs_per_record * hw_coal->budget;
  4123. val = clamp_t(u16, hw_coal->coal_bufs, 1, max);
  4124. req->num_cmpl_aggr_int = cpu_to_le16(val);
  4125. /* This is a 6-bit value and must not be 0, or we'll get non stop IRQ */
  4126. val = min_t(u16, val, 63);
  4127. req->num_cmpl_dma_aggr = cpu_to_le16(val);
  4128. /* This is a 6-bit value and must not be 0, or we'll get non stop IRQ */
  4129. val = clamp_t(u16, hw_coal->coal_bufs_irq, 1, 63);
  4130. req->num_cmpl_dma_aggr_during_int = cpu_to_le16(val);
  4131. tmr = BNXT_USEC_TO_COAL_TIMER(hw_coal->coal_ticks);
  4132. tmr = max_t(u16, tmr, 1);
  4133. req->int_lat_tmr_max = cpu_to_le16(tmr);
  4134. /* min timer set to 1/2 of interrupt timer */
  4135. val = tmr / 2;
  4136. req->int_lat_tmr_min = cpu_to_le16(val);
  4137. /* buf timer set to 1/4 of interrupt timer */
  4138. val = max_t(u16, tmr / 4, 1);
  4139. req->cmpl_aggr_dma_tmr = cpu_to_le16(val);
  4140. tmr = BNXT_USEC_TO_COAL_TIMER(hw_coal->coal_ticks_irq);
  4141. tmr = max_t(u16, tmr, 1);
  4142. req->cmpl_aggr_dma_tmr_during_int = cpu_to_le16(tmr);
  4143. flags = RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_TIMER_RESET;
  4144. if (hw_coal->idle_thresh && hw_coal->coal_ticks < hw_coal->idle_thresh)
  4145. flags |= RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_RING_IDLE;
  4146. req->flags = cpu_to_le16(flags);
  4147. }
  4148. int bnxt_hwrm_set_ring_coal(struct bnxt *bp, struct bnxt_napi *bnapi)
  4149. {
  4150. struct hwrm_ring_cmpl_ring_cfg_aggint_params_input req_rx = {0};
  4151. struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
  4152. struct bnxt_coal coal;
  4153. unsigned int grp_idx;
  4154. /* Tick values in micro seconds.
  4155. * 1 coal_buf x bufs_per_record = 1 completion record.
  4156. */
  4157. memcpy(&coal, &bp->rx_coal, sizeof(struct bnxt_coal));
  4158. coal.coal_ticks = cpr->rx_ring_coal.coal_ticks;
  4159. coal.coal_bufs = cpr->rx_ring_coal.coal_bufs;
  4160. if (!bnapi->rx_ring)
  4161. return -ENODEV;
  4162. bnxt_hwrm_cmd_hdr_init(bp, &req_rx,
  4163. HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS, -1, -1);
  4164. bnxt_hwrm_set_coal_params(&coal, &req_rx);
  4165. grp_idx = bnapi->index;
  4166. req_rx.ring_id = cpu_to_le16(bp->grp_info[grp_idx].cp_fw_ring_id);
  4167. return hwrm_send_message(bp, &req_rx, sizeof(req_rx),
  4168. HWRM_CMD_TIMEOUT);
  4169. }
  4170. int bnxt_hwrm_set_coal(struct bnxt *bp)
  4171. {
  4172. int i, rc = 0;
  4173. struct hwrm_ring_cmpl_ring_cfg_aggint_params_input req_rx = {0},
  4174. req_tx = {0}, *req;
  4175. bnxt_hwrm_cmd_hdr_init(bp, &req_rx,
  4176. HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS, -1, -1);
  4177. bnxt_hwrm_cmd_hdr_init(bp, &req_tx,
  4178. HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS, -1, -1);
  4179. bnxt_hwrm_set_coal_params(&bp->rx_coal, &req_rx);
  4180. bnxt_hwrm_set_coal_params(&bp->tx_coal, &req_tx);
  4181. mutex_lock(&bp->hwrm_cmd_lock);
  4182. for (i = 0; i < bp->cp_nr_rings; i++) {
  4183. struct bnxt_napi *bnapi = bp->bnapi[i];
  4184. req = &req_rx;
  4185. if (!bnapi->rx_ring)
  4186. req = &req_tx;
  4187. req->ring_id = cpu_to_le16(bp->grp_info[i].cp_fw_ring_id);
  4188. rc = _hwrm_send_message(bp, req, sizeof(*req),
  4189. HWRM_CMD_TIMEOUT);
  4190. if (rc)
  4191. break;
  4192. }
  4193. mutex_unlock(&bp->hwrm_cmd_lock);
  4194. return rc;
  4195. }
  4196. static int bnxt_hwrm_stat_ctx_free(struct bnxt *bp)
  4197. {
  4198. int rc = 0, i;
  4199. struct hwrm_stat_ctx_free_input req = {0};
  4200. if (!bp->bnapi)
  4201. return 0;
  4202. if (BNXT_CHIP_TYPE_NITRO_A0(bp))
  4203. return 0;
  4204. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_STAT_CTX_FREE, -1, -1);
  4205. mutex_lock(&bp->hwrm_cmd_lock);
  4206. for (i = 0; i < bp->cp_nr_rings; i++) {
  4207. struct bnxt_napi *bnapi = bp->bnapi[i];
  4208. struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
  4209. if (cpr->hw_stats_ctx_id != INVALID_STATS_CTX_ID) {
  4210. req.stat_ctx_id = cpu_to_le32(cpr->hw_stats_ctx_id);
  4211. rc = _hwrm_send_message(bp, &req, sizeof(req),
  4212. HWRM_CMD_TIMEOUT);
  4213. if (rc)
  4214. break;
  4215. cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
  4216. }
  4217. }
  4218. mutex_unlock(&bp->hwrm_cmd_lock);
  4219. return rc;
  4220. }
  4221. static int bnxt_hwrm_stat_ctx_alloc(struct bnxt *bp)
  4222. {
  4223. int rc = 0, i;
  4224. struct hwrm_stat_ctx_alloc_input req = {0};
  4225. struct hwrm_stat_ctx_alloc_output *resp = bp->hwrm_cmd_resp_addr;
  4226. if (BNXT_CHIP_TYPE_NITRO_A0(bp))
  4227. return 0;
  4228. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_STAT_CTX_ALLOC, -1, -1);
  4229. req.update_period_ms = cpu_to_le32(bp->stats_coal_ticks / 1000);
  4230. mutex_lock(&bp->hwrm_cmd_lock);
  4231. for (i = 0; i < bp->cp_nr_rings; i++) {
  4232. struct bnxt_napi *bnapi = bp->bnapi[i];
  4233. struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
  4234. req.stats_dma_addr = cpu_to_le64(cpr->hw_stats_map);
  4235. rc = _hwrm_send_message(bp, &req, sizeof(req),
  4236. HWRM_CMD_TIMEOUT);
  4237. if (rc)
  4238. break;
  4239. cpr->hw_stats_ctx_id = le32_to_cpu(resp->stat_ctx_id);
  4240. bp->grp_info[i].fw_stats_ctx = cpr->hw_stats_ctx_id;
  4241. }
  4242. mutex_unlock(&bp->hwrm_cmd_lock);
  4243. return rc;
  4244. }
  4245. static int bnxt_hwrm_func_qcfg(struct bnxt *bp)
  4246. {
  4247. struct hwrm_func_qcfg_input req = {0};
  4248. struct hwrm_func_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
  4249. u16 flags;
  4250. int rc;
  4251. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_QCFG, -1, -1);
  4252. req.fid = cpu_to_le16(0xffff);
  4253. mutex_lock(&bp->hwrm_cmd_lock);
  4254. rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  4255. if (rc)
  4256. goto func_qcfg_exit;
  4257. #ifdef CONFIG_BNXT_SRIOV
  4258. if (BNXT_VF(bp)) {
  4259. struct bnxt_vf_info *vf = &bp->vf;
  4260. vf->vlan = le16_to_cpu(resp->vlan) & VLAN_VID_MASK;
  4261. }
  4262. #endif
  4263. flags = le16_to_cpu(resp->flags);
  4264. if (flags & (FUNC_QCFG_RESP_FLAGS_FW_DCBX_AGENT_ENABLED |
  4265. FUNC_QCFG_RESP_FLAGS_FW_LLDP_AGENT_ENABLED)) {
  4266. bp->fw_cap |= BNXT_FW_CAP_LLDP_AGENT;
  4267. if (flags & FUNC_QCFG_RESP_FLAGS_FW_DCBX_AGENT_ENABLED)
  4268. bp->fw_cap |= BNXT_FW_CAP_DCBX_AGENT;
  4269. }
  4270. if (BNXT_PF(bp) && (flags & FUNC_QCFG_RESP_FLAGS_MULTI_HOST))
  4271. bp->flags |= BNXT_FLAG_MULTI_HOST;
  4272. switch (resp->port_partition_type) {
  4273. case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_0:
  4274. case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_5:
  4275. case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR2_0:
  4276. bp->port_partition_type = resp->port_partition_type;
  4277. break;
  4278. }
  4279. if (bp->hwrm_spec_code < 0x10707 ||
  4280. resp->evb_mode == FUNC_QCFG_RESP_EVB_MODE_VEB)
  4281. bp->br_mode = BRIDGE_MODE_VEB;
  4282. else if (resp->evb_mode == FUNC_QCFG_RESP_EVB_MODE_VEPA)
  4283. bp->br_mode = BRIDGE_MODE_VEPA;
  4284. else
  4285. bp->br_mode = BRIDGE_MODE_UNDEF;
  4286. bp->max_mtu = le16_to_cpu(resp->max_mtu_configured);
  4287. if (!bp->max_mtu)
  4288. bp->max_mtu = BNXT_MAX_MTU;
  4289. func_qcfg_exit:
  4290. mutex_unlock(&bp->hwrm_cmd_lock);
  4291. return rc;
  4292. }
  4293. int bnxt_hwrm_func_resc_qcaps(struct bnxt *bp, bool all)
  4294. {
  4295. struct hwrm_func_resource_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
  4296. struct hwrm_func_resource_qcaps_input req = {0};
  4297. struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
  4298. int rc;
  4299. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_RESOURCE_QCAPS, -1, -1);
  4300. req.fid = cpu_to_le16(0xffff);
  4301. mutex_lock(&bp->hwrm_cmd_lock);
  4302. rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  4303. if (rc) {
  4304. rc = -EIO;
  4305. goto hwrm_func_resc_qcaps_exit;
  4306. }
  4307. hw_resc->max_tx_sch_inputs = le16_to_cpu(resp->max_tx_scheduler_inputs);
  4308. if (!all)
  4309. goto hwrm_func_resc_qcaps_exit;
  4310. hw_resc->min_rsscos_ctxs = le16_to_cpu(resp->min_rsscos_ctx);
  4311. hw_resc->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
  4312. hw_resc->min_cp_rings = le16_to_cpu(resp->min_cmpl_rings);
  4313. hw_resc->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
  4314. hw_resc->min_tx_rings = le16_to_cpu(resp->min_tx_rings);
  4315. hw_resc->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
  4316. hw_resc->min_rx_rings = le16_to_cpu(resp->min_rx_rings);
  4317. hw_resc->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
  4318. hw_resc->min_hw_ring_grps = le16_to_cpu(resp->min_hw_ring_grps);
  4319. hw_resc->max_hw_ring_grps = le16_to_cpu(resp->max_hw_ring_grps);
  4320. hw_resc->min_l2_ctxs = le16_to_cpu(resp->min_l2_ctxs);
  4321. hw_resc->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
  4322. hw_resc->min_vnics = le16_to_cpu(resp->min_vnics);
  4323. hw_resc->max_vnics = le16_to_cpu(resp->max_vnics);
  4324. hw_resc->min_stat_ctxs = le16_to_cpu(resp->min_stat_ctx);
  4325. hw_resc->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
  4326. if (BNXT_PF(bp)) {
  4327. struct bnxt_pf_info *pf = &bp->pf;
  4328. pf->vf_resv_strategy =
  4329. le16_to_cpu(resp->vf_reservation_strategy);
  4330. if (pf->vf_resv_strategy > BNXT_VF_RESV_STRATEGY_MINIMAL_STATIC)
  4331. pf->vf_resv_strategy = BNXT_VF_RESV_STRATEGY_MAXIMAL;
  4332. }
  4333. hwrm_func_resc_qcaps_exit:
  4334. mutex_unlock(&bp->hwrm_cmd_lock);
  4335. return rc;
  4336. }
  4337. static int __bnxt_hwrm_func_qcaps(struct bnxt *bp)
  4338. {
  4339. int rc = 0;
  4340. struct hwrm_func_qcaps_input req = {0};
  4341. struct hwrm_func_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
  4342. struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
  4343. u32 flags;
  4344. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_QCAPS, -1, -1);
  4345. req.fid = cpu_to_le16(0xffff);
  4346. mutex_lock(&bp->hwrm_cmd_lock);
  4347. rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  4348. if (rc)
  4349. goto hwrm_func_qcaps_exit;
  4350. flags = le32_to_cpu(resp->flags);
  4351. if (flags & FUNC_QCAPS_RESP_FLAGS_ROCE_V1_SUPPORTED)
  4352. bp->flags |= BNXT_FLAG_ROCEV1_CAP;
  4353. if (flags & FUNC_QCAPS_RESP_FLAGS_ROCE_V2_SUPPORTED)
  4354. bp->flags |= BNXT_FLAG_ROCEV2_CAP;
  4355. bp->tx_push_thresh = 0;
  4356. if (flags & FUNC_QCAPS_RESP_FLAGS_PUSH_MODE_SUPPORTED)
  4357. bp->tx_push_thresh = BNXT_TX_PUSH_THRESH;
  4358. hw_resc->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
  4359. hw_resc->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
  4360. hw_resc->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
  4361. hw_resc->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
  4362. hw_resc->max_hw_ring_grps = le32_to_cpu(resp->max_hw_ring_grps);
  4363. if (!hw_resc->max_hw_ring_grps)
  4364. hw_resc->max_hw_ring_grps = hw_resc->max_tx_rings;
  4365. hw_resc->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
  4366. hw_resc->max_vnics = le16_to_cpu(resp->max_vnics);
  4367. hw_resc->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
  4368. if (BNXT_PF(bp)) {
  4369. struct bnxt_pf_info *pf = &bp->pf;
  4370. pf->fw_fid = le16_to_cpu(resp->fid);
  4371. pf->port_id = le16_to_cpu(resp->port_id);
  4372. bp->dev->dev_port = pf->port_id;
  4373. memcpy(pf->mac_addr, resp->mac_address, ETH_ALEN);
  4374. pf->first_vf_id = le16_to_cpu(resp->first_vf_id);
  4375. pf->max_vfs = le16_to_cpu(resp->max_vfs);
  4376. pf->max_encap_records = le32_to_cpu(resp->max_encap_records);
  4377. pf->max_decap_records = le32_to_cpu(resp->max_decap_records);
  4378. pf->max_tx_em_flows = le32_to_cpu(resp->max_tx_em_flows);
  4379. pf->max_tx_wm_flows = le32_to_cpu(resp->max_tx_wm_flows);
  4380. pf->max_rx_em_flows = le32_to_cpu(resp->max_rx_em_flows);
  4381. pf->max_rx_wm_flows = le32_to_cpu(resp->max_rx_wm_flows);
  4382. if (flags & FUNC_QCAPS_RESP_FLAGS_WOL_MAGICPKT_SUPPORTED)
  4383. bp->flags |= BNXT_FLAG_WOL_CAP;
  4384. } else {
  4385. #ifdef CONFIG_BNXT_SRIOV
  4386. struct bnxt_vf_info *vf = &bp->vf;
  4387. vf->fw_fid = le16_to_cpu(resp->fid);
  4388. memcpy(vf->mac_addr, resp->mac_address, ETH_ALEN);
  4389. #endif
  4390. }
  4391. hwrm_func_qcaps_exit:
  4392. mutex_unlock(&bp->hwrm_cmd_lock);
  4393. return rc;
  4394. }
  4395. static int bnxt_hwrm_func_qcaps(struct bnxt *bp)
  4396. {
  4397. int rc;
  4398. rc = __bnxt_hwrm_func_qcaps(bp);
  4399. if (rc)
  4400. return rc;
  4401. if (bp->hwrm_spec_code >= 0x10803) {
  4402. rc = bnxt_hwrm_func_resc_qcaps(bp, true);
  4403. if (!rc)
  4404. bp->fw_cap |= BNXT_FW_CAP_NEW_RM;
  4405. }
  4406. return 0;
  4407. }
  4408. static int bnxt_hwrm_func_reset(struct bnxt *bp)
  4409. {
  4410. struct hwrm_func_reset_input req = {0};
  4411. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_RESET, -1, -1);
  4412. req.enables = 0;
  4413. return hwrm_send_message(bp, &req, sizeof(req), HWRM_RESET_TIMEOUT);
  4414. }
  4415. static int bnxt_hwrm_queue_qportcfg(struct bnxt *bp)
  4416. {
  4417. int rc = 0;
  4418. struct hwrm_queue_qportcfg_input req = {0};
  4419. struct hwrm_queue_qportcfg_output *resp = bp->hwrm_cmd_resp_addr;
  4420. u8 i, j, *qptr;
  4421. bool no_rdma;
  4422. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_QUEUE_QPORTCFG, -1, -1);
  4423. mutex_lock(&bp->hwrm_cmd_lock);
  4424. rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  4425. if (rc)
  4426. goto qportcfg_exit;
  4427. if (!resp->max_configurable_queues) {
  4428. rc = -EINVAL;
  4429. goto qportcfg_exit;
  4430. }
  4431. bp->max_tc = resp->max_configurable_queues;
  4432. bp->max_lltc = resp->max_configurable_lossless_queues;
  4433. if (bp->max_tc > BNXT_MAX_QUEUE)
  4434. bp->max_tc = BNXT_MAX_QUEUE;
  4435. no_rdma = !(bp->flags & BNXT_FLAG_ROCE_CAP);
  4436. qptr = &resp->queue_id0;
  4437. for (i = 0, j = 0; i < bp->max_tc; i++) {
  4438. bp->q_info[j].queue_id = *qptr++;
  4439. bp->q_info[j].queue_profile = *qptr++;
  4440. bp->tc_to_qidx[j] = j;
  4441. if (!BNXT_CNPQ(bp->q_info[j].queue_profile) ||
  4442. (no_rdma && BNXT_PF(bp)))
  4443. j++;
  4444. }
  4445. bp->max_tc = max_t(u8, j, 1);
  4446. if (resp->queue_cfg_info & QUEUE_QPORTCFG_RESP_QUEUE_CFG_INFO_ASYM_CFG)
  4447. bp->max_tc = 1;
  4448. if (bp->max_lltc > bp->max_tc)
  4449. bp->max_lltc = bp->max_tc;
  4450. qportcfg_exit:
  4451. mutex_unlock(&bp->hwrm_cmd_lock);
  4452. return rc;
  4453. }
  4454. static int bnxt_hwrm_ver_get(struct bnxt *bp)
  4455. {
  4456. int rc;
  4457. struct hwrm_ver_get_input req = {0};
  4458. struct hwrm_ver_get_output *resp = bp->hwrm_cmd_resp_addr;
  4459. u32 dev_caps_cfg;
  4460. bp->hwrm_max_req_len = HWRM_MAX_REQ_LEN;
  4461. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VER_GET, -1, -1);
  4462. req.hwrm_intf_maj = HWRM_VERSION_MAJOR;
  4463. req.hwrm_intf_min = HWRM_VERSION_MINOR;
  4464. req.hwrm_intf_upd = HWRM_VERSION_UPDATE;
  4465. mutex_lock(&bp->hwrm_cmd_lock);
  4466. rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  4467. if (rc)
  4468. goto hwrm_ver_get_exit;
  4469. memcpy(&bp->ver_resp, resp, sizeof(struct hwrm_ver_get_output));
  4470. bp->hwrm_spec_code = resp->hwrm_intf_maj_8b << 16 |
  4471. resp->hwrm_intf_min_8b << 8 |
  4472. resp->hwrm_intf_upd_8b;
  4473. if (resp->hwrm_intf_maj_8b < 1) {
  4474. netdev_warn(bp->dev, "HWRM interface %d.%d.%d is older than 1.0.0.\n",
  4475. resp->hwrm_intf_maj_8b, resp->hwrm_intf_min_8b,
  4476. resp->hwrm_intf_upd_8b);
  4477. netdev_warn(bp->dev, "Please update firmware with HWRM interface 1.0.0 or newer.\n");
  4478. }
  4479. snprintf(bp->fw_ver_str, BC_HWRM_STR_LEN, "%d.%d.%d.%d",
  4480. resp->hwrm_fw_maj_8b, resp->hwrm_fw_min_8b,
  4481. resp->hwrm_fw_bld_8b, resp->hwrm_fw_rsvd_8b);
  4482. bp->hwrm_cmd_timeout = le16_to_cpu(resp->def_req_timeout);
  4483. if (!bp->hwrm_cmd_timeout)
  4484. bp->hwrm_cmd_timeout = DFLT_HWRM_CMD_TIMEOUT;
  4485. if (resp->hwrm_intf_maj_8b >= 1)
  4486. bp->hwrm_max_req_len = le16_to_cpu(resp->max_req_win_len);
  4487. bp->chip_num = le16_to_cpu(resp->chip_num);
  4488. if (bp->chip_num == CHIP_NUM_58700 && !resp->chip_rev &&
  4489. !resp->chip_metal)
  4490. bp->flags |= BNXT_FLAG_CHIP_NITRO_A0;
  4491. dev_caps_cfg = le32_to_cpu(resp->dev_caps_cfg);
  4492. if ((dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_SHORT_CMD_SUPPORTED) &&
  4493. (dev_caps_cfg & VER_GET_RESP_DEV_CAPS_CFG_SHORT_CMD_REQUIRED))
  4494. bp->fw_cap |= BNXT_FW_CAP_SHORT_CMD;
  4495. hwrm_ver_get_exit:
  4496. mutex_unlock(&bp->hwrm_cmd_lock);
  4497. return rc;
  4498. }
  4499. int bnxt_hwrm_fw_set_time(struct bnxt *bp)
  4500. {
  4501. struct hwrm_fw_set_time_input req = {0};
  4502. struct tm tm;
  4503. time64_t now = ktime_get_real_seconds();
  4504. if ((BNXT_VF(bp) && bp->hwrm_spec_code < 0x10901) ||
  4505. bp->hwrm_spec_code < 0x10400)
  4506. return -EOPNOTSUPP;
  4507. time64_to_tm(now, 0, &tm);
  4508. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FW_SET_TIME, -1, -1);
  4509. req.year = cpu_to_le16(1900 + tm.tm_year);
  4510. req.month = 1 + tm.tm_mon;
  4511. req.day = tm.tm_mday;
  4512. req.hour = tm.tm_hour;
  4513. req.minute = tm.tm_min;
  4514. req.second = tm.tm_sec;
  4515. return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  4516. }
  4517. static int bnxt_hwrm_port_qstats(struct bnxt *bp)
  4518. {
  4519. int rc;
  4520. struct bnxt_pf_info *pf = &bp->pf;
  4521. struct hwrm_port_qstats_input req = {0};
  4522. if (!(bp->flags & BNXT_FLAG_PORT_STATS))
  4523. return 0;
  4524. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_QSTATS, -1, -1);
  4525. req.port_id = cpu_to_le16(pf->port_id);
  4526. req.tx_stat_host_addr = cpu_to_le64(bp->hw_tx_port_stats_map);
  4527. req.rx_stat_host_addr = cpu_to_le64(bp->hw_rx_port_stats_map);
  4528. rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  4529. return rc;
  4530. }
  4531. static int bnxt_hwrm_port_qstats_ext(struct bnxt *bp)
  4532. {
  4533. struct hwrm_port_qstats_ext_input req = {0};
  4534. struct bnxt_pf_info *pf = &bp->pf;
  4535. if (!(bp->flags & BNXT_FLAG_PORT_STATS_EXT))
  4536. return 0;
  4537. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_QSTATS_EXT, -1, -1);
  4538. req.port_id = cpu_to_le16(pf->port_id);
  4539. req.rx_stat_size = cpu_to_le16(sizeof(struct rx_port_stats_ext));
  4540. req.rx_stat_host_addr = cpu_to_le64(bp->hw_rx_port_stats_ext_map);
  4541. return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  4542. }
  4543. static void bnxt_hwrm_free_tunnel_ports(struct bnxt *bp)
  4544. {
  4545. if (bp->vxlan_port_cnt) {
  4546. bnxt_hwrm_tunnel_dst_port_free(
  4547. bp, TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN);
  4548. }
  4549. bp->vxlan_port_cnt = 0;
  4550. if (bp->nge_port_cnt) {
  4551. bnxt_hwrm_tunnel_dst_port_free(
  4552. bp, TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE);
  4553. }
  4554. bp->nge_port_cnt = 0;
  4555. }
  4556. static int bnxt_set_tpa(struct bnxt *bp, bool set_tpa)
  4557. {
  4558. int rc, i;
  4559. u32 tpa_flags = 0;
  4560. if (set_tpa)
  4561. tpa_flags = bp->flags & BNXT_FLAG_TPA;
  4562. for (i = 0; i < bp->nr_vnics; i++) {
  4563. rc = bnxt_hwrm_vnic_set_tpa(bp, i, tpa_flags);
  4564. if (rc) {
  4565. netdev_err(bp->dev, "hwrm vnic set tpa failure rc for vnic %d: %x\n",
  4566. i, rc);
  4567. return rc;
  4568. }
  4569. }
  4570. return 0;
  4571. }
  4572. static void bnxt_hwrm_clear_vnic_rss(struct bnxt *bp)
  4573. {
  4574. int i;
  4575. for (i = 0; i < bp->nr_vnics; i++)
  4576. bnxt_hwrm_vnic_set_rss(bp, i, false);
  4577. }
  4578. static void bnxt_hwrm_resource_free(struct bnxt *bp, bool close_path,
  4579. bool irq_re_init)
  4580. {
  4581. if (bp->vnic_info) {
  4582. bnxt_hwrm_clear_vnic_filter(bp);
  4583. /* clear all RSS setting before free vnic ctx */
  4584. bnxt_hwrm_clear_vnic_rss(bp);
  4585. bnxt_hwrm_vnic_ctx_free(bp);
  4586. /* before free the vnic, undo the vnic tpa settings */
  4587. if (bp->flags & BNXT_FLAG_TPA)
  4588. bnxt_set_tpa(bp, false);
  4589. bnxt_hwrm_vnic_free(bp);
  4590. }
  4591. bnxt_hwrm_ring_free(bp, close_path);
  4592. bnxt_hwrm_ring_grp_free(bp);
  4593. if (irq_re_init) {
  4594. bnxt_hwrm_stat_ctx_free(bp);
  4595. bnxt_hwrm_free_tunnel_ports(bp);
  4596. }
  4597. }
  4598. static int bnxt_hwrm_set_br_mode(struct bnxt *bp, u16 br_mode)
  4599. {
  4600. struct hwrm_func_cfg_input req = {0};
  4601. int rc;
  4602. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_CFG, -1, -1);
  4603. req.fid = cpu_to_le16(0xffff);
  4604. req.enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_EVB_MODE);
  4605. if (br_mode == BRIDGE_MODE_VEB)
  4606. req.evb_mode = FUNC_CFG_REQ_EVB_MODE_VEB;
  4607. else if (br_mode == BRIDGE_MODE_VEPA)
  4608. req.evb_mode = FUNC_CFG_REQ_EVB_MODE_VEPA;
  4609. else
  4610. return -EINVAL;
  4611. rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  4612. if (rc)
  4613. rc = -EIO;
  4614. return rc;
  4615. }
  4616. static int bnxt_hwrm_set_cache_line_size(struct bnxt *bp, int size)
  4617. {
  4618. struct hwrm_func_cfg_input req = {0};
  4619. int rc;
  4620. if (BNXT_VF(bp) || bp->hwrm_spec_code < 0x10803)
  4621. return 0;
  4622. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_CFG, -1, -1);
  4623. req.fid = cpu_to_le16(0xffff);
  4624. req.enables = cpu_to_le32(FUNC_CFG_REQ_ENABLES_CACHE_LINESIZE);
  4625. req.options = FUNC_CFG_REQ_OPTIONS_CACHE_LINESIZE_SIZE_64;
  4626. if (size == 128)
  4627. req.options = FUNC_CFG_REQ_OPTIONS_CACHE_LINESIZE_SIZE_128;
  4628. rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  4629. if (rc)
  4630. rc = -EIO;
  4631. return rc;
  4632. }
  4633. static int bnxt_setup_vnic(struct bnxt *bp, u16 vnic_id)
  4634. {
  4635. struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
  4636. int rc;
  4637. if (vnic->flags & BNXT_VNIC_RFS_NEW_RSS_FLAG)
  4638. goto skip_rss_ctx;
  4639. /* allocate context for vnic */
  4640. rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic_id, 0);
  4641. if (rc) {
  4642. netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
  4643. vnic_id, rc);
  4644. goto vnic_setup_err;
  4645. }
  4646. bp->rsscos_nr_ctxs++;
  4647. if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
  4648. rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic_id, 1);
  4649. if (rc) {
  4650. netdev_err(bp->dev, "hwrm vnic %d cos ctx alloc failure rc: %x\n",
  4651. vnic_id, rc);
  4652. goto vnic_setup_err;
  4653. }
  4654. bp->rsscos_nr_ctxs++;
  4655. }
  4656. skip_rss_ctx:
  4657. /* configure default vnic, ring grp */
  4658. rc = bnxt_hwrm_vnic_cfg(bp, vnic_id);
  4659. if (rc) {
  4660. netdev_err(bp->dev, "hwrm vnic %d cfg failure rc: %x\n",
  4661. vnic_id, rc);
  4662. goto vnic_setup_err;
  4663. }
  4664. /* Enable RSS hashing on vnic */
  4665. rc = bnxt_hwrm_vnic_set_rss(bp, vnic_id, true);
  4666. if (rc) {
  4667. netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %x\n",
  4668. vnic_id, rc);
  4669. goto vnic_setup_err;
  4670. }
  4671. if (bp->flags & BNXT_FLAG_AGG_RINGS) {
  4672. rc = bnxt_hwrm_vnic_set_hds(bp, vnic_id);
  4673. if (rc) {
  4674. netdev_err(bp->dev, "hwrm vnic %d set hds failure rc: %x\n",
  4675. vnic_id, rc);
  4676. }
  4677. }
  4678. vnic_setup_err:
  4679. return rc;
  4680. }
  4681. static int bnxt_alloc_rfs_vnics(struct bnxt *bp)
  4682. {
  4683. #ifdef CONFIG_RFS_ACCEL
  4684. int i, rc = 0;
  4685. for (i = 0; i < bp->rx_nr_rings; i++) {
  4686. struct bnxt_vnic_info *vnic;
  4687. u16 vnic_id = i + 1;
  4688. u16 ring_id = i;
  4689. if (vnic_id >= bp->nr_vnics)
  4690. break;
  4691. vnic = &bp->vnic_info[vnic_id];
  4692. vnic->flags |= BNXT_VNIC_RFS_FLAG;
  4693. if (bp->flags & BNXT_FLAG_NEW_RSS_CAP)
  4694. vnic->flags |= BNXT_VNIC_RFS_NEW_RSS_FLAG;
  4695. rc = bnxt_hwrm_vnic_alloc(bp, vnic_id, ring_id, 1);
  4696. if (rc) {
  4697. netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
  4698. vnic_id, rc);
  4699. break;
  4700. }
  4701. rc = bnxt_setup_vnic(bp, vnic_id);
  4702. if (rc)
  4703. break;
  4704. }
  4705. return rc;
  4706. #else
  4707. return 0;
  4708. #endif
  4709. }
  4710. /* Allow PF and VF with default VLAN to be in promiscuous mode */
  4711. static bool bnxt_promisc_ok(struct bnxt *bp)
  4712. {
  4713. #ifdef CONFIG_BNXT_SRIOV
  4714. if (BNXT_VF(bp) && !bp->vf.vlan)
  4715. return false;
  4716. #endif
  4717. return true;
  4718. }
  4719. static int bnxt_setup_nitroa0_vnic(struct bnxt *bp)
  4720. {
  4721. unsigned int rc = 0;
  4722. rc = bnxt_hwrm_vnic_alloc(bp, 1, bp->rx_nr_rings - 1, 1);
  4723. if (rc) {
  4724. netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
  4725. rc);
  4726. return rc;
  4727. }
  4728. rc = bnxt_hwrm_vnic_cfg(bp, 1);
  4729. if (rc) {
  4730. netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
  4731. rc);
  4732. return rc;
  4733. }
  4734. return rc;
  4735. }
  4736. static int bnxt_cfg_rx_mode(struct bnxt *);
  4737. static bool bnxt_mc_list_updated(struct bnxt *, u32 *);
  4738. static int bnxt_init_chip(struct bnxt *bp, bool irq_re_init)
  4739. {
  4740. struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
  4741. int rc = 0;
  4742. unsigned int rx_nr_rings = bp->rx_nr_rings;
  4743. if (irq_re_init) {
  4744. rc = bnxt_hwrm_stat_ctx_alloc(bp);
  4745. if (rc) {
  4746. netdev_err(bp->dev, "hwrm stat ctx alloc failure rc: %x\n",
  4747. rc);
  4748. goto err_out;
  4749. }
  4750. }
  4751. rc = bnxt_hwrm_ring_alloc(bp);
  4752. if (rc) {
  4753. netdev_err(bp->dev, "hwrm ring alloc failure rc: %x\n", rc);
  4754. goto err_out;
  4755. }
  4756. rc = bnxt_hwrm_ring_grp_alloc(bp);
  4757. if (rc) {
  4758. netdev_err(bp->dev, "hwrm_ring_grp alloc failure: %x\n", rc);
  4759. goto err_out;
  4760. }
  4761. if (BNXT_CHIP_TYPE_NITRO_A0(bp))
  4762. rx_nr_rings--;
  4763. /* default vnic 0 */
  4764. rc = bnxt_hwrm_vnic_alloc(bp, 0, 0, rx_nr_rings);
  4765. if (rc) {
  4766. netdev_err(bp->dev, "hwrm vnic alloc failure rc: %x\n", rc);
  4767. goto err_out;
  4768. }
  4769. rc = bnxt_setup_vnic(bp, 0);
  4770. if (rc)
  4771. goto err_out;
  4772. if (bp->flags & BNXT_FLAG_RFS) {
  4773. rc = bnxt_alloc_rfs_vnics(bp);
  4774. if (rc)
  4775. goto err_out;
  4776. }
  4777. if (bp->flags & BNXT_FLAG_TPA) {
  4778. rc = bnxt_set_tpa(bp, true);
  4779. if (rc)
  4780. goto err_out;
  4781. }
  4782. if (BNXT_VF(bp))
  4783. bnxt_update_vf_mac(bp);
  4784. /* Filter for default vnic 0 */
  4785. rc = bnxt_hwrm_set_vnic_filter(bp, 0, 0, bp->dev->dev_addr);
  4786. if (rc) {
  4787. netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n", rc);
  4788. goto err_out;
  4789. }
  4790. vnic->uc_filter_count = 1;
  4791. vnic->rx_mask = 0;
  4792. if (bp->dev->flags & IFF_BROADCAST)
  4793. vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_BCAST;
  4794. if ((bp->dev->flags & IFF_PROMISC) && bnxt_promisc_ok(bp))
  4795. vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
  4796. if (bp->dev->flags & IFF_ALLMULTI) {
  4797. vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
  4798. vnic->mc_list_count = 0;
  4799. } else {
  4800. u32 mask = 0;
  4801. bnxt_mc_list_updated(bp, &mask);
  4802. vnic->rx_mask |= mask;
  4803. }
  4804. rc = bnxt_cfg_rx_mode(bp);
  4805. if (rc)
  4806. goto err_out;
  4807. rc = bnxt_hwrm_set_coal(bp);
  4808. if (rc)
  4809. netdev_warn(bp->dev, "HWRM set coalescing failure rc: %x\n",
  4810. rc);
  4811. if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
  4812. rc = bnxt_setup_nitroa0_vnic(bp);
  4813. if (rc)
  4814. netdev_err(bp->dev, "Special vnic setup failure for NS2 A0 rc: %x\n",
  4815. rc);
  4816. }
  4817. if (BNXT_VF(bp)) {
  4818. bnxt_hwrm_func_qcfg(bp);
  4819. netdev_update_features(bp->dev);
  4820. }
  4821. return 0;
  4822. err_out:
  4823. bnxt_hwrm_resource_free(bp, 0, true);
  4824. return rc;
  4825. }
  4826. static int bnxt_shutdown_nic(struct bnxt *bp, bool irq_re_init)
  4827. {
  4828. bnxt_hwrm_resource_free(bp, 1, irq_re_init);
  4829. return 0;
  4830. }
  4831. static int bnxt_init_nic(struct bnxt *bp, bool irq_re_init)
  4832. {
  4833. bnxt_init_cp_rings(bp);
  4834. bnxt_init_rx_rings(bp);
  4835. bnxt_init_tx_rings(bp);
  4836. bnxt_init_ring_grps(bp, irq_re_init);
  4837. bnxt_init_vnics(bp);
  4838. return bnxt_init_chip(bp, irq_re_init);
  4839. }
  4840. static int bnxt_set_real_num_queues(struct bnxt *bp)
  4841. {
  4842. int rc;
  4843. struct net_device *dev = bp->dev;
  4844. rc = netif_set_real_num_tx_queues(dev, bp->tx_nr_rings -
  4845. bp->tx_nr_rings_xdp);
  4846. if (rc)
  4847. return rc;
  4848. rc = netif_set_real_num_rx_queues(dev, bp->rx_nr_rings);
  4849. if (rc)
  4850. return rc;
  4851. #ifdef CONFIG_RFS_ACCEL
  4852. if (bp->flags & BNXT_FLAG_RFS)
  4853. dev->rx_cpu_rmap = alloc_irq_cpu_rmap(bp->rx_nr_rings);
  4854. #endif
  4855. return rc;
  4856. }
  4857. static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
  4858. bool shared)
  4859. {
  4860. int _rx = *rx, _tx = *tx;
  4861. if (shared) {
  4862. *rx = min_t(int, _rx, max);
  4863. *tx = min_t(int, _tx, max);
  4864. } else {
  4865. if (max < 2)
  4866. return -ENOMEM;
  4867. while (_rx + _tx > max) {
  4868. if (_rx > _tx && _rx > 1)
  4869. _rx--;
  4870. else if (_tx > 1)
  4871. _tx--;
  4872. }
  4873. *rx = _rx;
  4874. *tx = _tx;
  4875. }
  4876. return 0;
  4877. }
  4878. static void bnxt_setup_msix(struct bnxt *bp)
  4879. {
  4880. const int len = sizeof(bp->irq_tbl[0].name);
  4881. struct net_device *dev = bp->dev;
  4882. int tcs, i;
  4883. tcs = netdev_get_num_tc(dev);
  4884. if (tcs) {
  4885. int i, off, count;
  4886. for (i = 0; i < tcs; i++) {
  4887. count = bp->tx_nr_rings_per_tc;
  4888. off = i * count;
  4889. netdev_set_tc_queue(dev, i, count, off);
  4890. }
  4891. }
  4892. for (i = 0; i < bp->cp_nr_rings; i++) {
  4893. int map_idx = bnxt_cp_num_to_irq_num(bp, i);
  4894. char *attr;
  4895. if (bp->flags & BNXT_FLAG_SHARED_RINGS)
  4896. attr = "TxRx";
  4897. else if (i < bp->rx_nr_rings)
  4898. attr = "rx";
  4899. else
  4900. attr = "tx";
  4901. snprintf(bp->irq_tbl[map_idx].name, len, "%s-%s-%d", dev->name,
  4902. attr, i);
  4903. bp->irq_tbl[map_idx].handler = bnxt_msix;
  4904. }
  4905. }
  4906. static void bnxt_setup_inta(struct bnxt *bp)
  4907. {
  4908. const int len = sizeof(bp->irq_tbl[0].name);
  4909. if (netdev_get_num_tc(bp->dev))
  4910. netdev_reset_tc(bp->dev);
  4911. snprintf(bp->irq_tbl[0].name, len, "%s-%s-%d", bp->dev->name, "TxRx",
  4912. 0);
  4913. bp->irq_tbl[0].handler = bnxt_inta;
  4914. }
  4915. static int bnxt_setup_int_mode(struct bnxt *bp)
  4916. {
  4917. int rc;
  4918. if (bp->flags & BNXT_FLAG_USING_MSIX)
  4919. bnxt_setup_msix(bp);
  4920. else
  4921. bnxt_setup_inta(bp);
  4922. rc = bnxt_set_real_num_queues(bp);
  4923. return rc;
  4924. }
  4925. #ifdef CONFIG_RFS_ACCEL
  4926. static unsigned int bnxt_get_max_func_rss_ctxs(struct bnxt *bp)
  4927. {
  4928. return bp->hw_resc.max_rsscos_ctxs;
  4929. }
  4930. static unsigned int bnxt_get_max_func_vnics(struct bnxt *bp)
  4931. {
  4932. return bp->hw_resc.max_vnics;
  4933. }
  4934. #endif
  4935. unsigned int bnxt_get_max_func_stat_ctxs(struct bnxt *bp)
  4936. {
  4937. return bp->hw_resc.max_stat_ctxs;
  4938. }
  4939. void bnxt_set_max_func_stat_ctxs(struct bnxt *bp, unsigned int max)
  4940. {
  4941. bp->hw_resc.max_stat_ctxs = max;
  4942. }
  4943. unsigned int bnxt_get_max_func_cp_rings(struct bnxt *bp)
  4944. {
  4945. return bp->hw_resc.max_cp_rings;
  4946. }
  4947. unsigned int bnxt_get_max_func_cp_rings_for_en(struct bnxt *bp)
  4948. {
  4949. return bp->hw_resc.max_cp_rings - bnxt_get_ulp_msix_num(bp);
  4950. }
  4951. static unsigned int bnxt_get_max_func_irqs(struct bnxt *bp)
  4952. {
  4953. struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
  4954. return min_t(unsigned int, hw_resc->max_irqs, hw_resc->max_cp_rings);
  4955. }
  4956. static void bnxt_set_max_func_irqs(struct bnxt *bp, unsigned int max_irqs)
  4957. {
  4958. bp->hw_resc.max_irqs = max_irqs;
  4959. }
  4960. int bnxt_get_avail_msix(struct bnxt *bp, int num)
  4961. {
  4962. int max_cp = bnxt_get_max_func_cp_rings(bp);
  4963. int max_irq = bnxt_get_max_func_irqs(bp);
  4964. int total_req = bp->cp_nr_rings + num;
  4965. int max_idx, avail_msix;
  4966. max_idx = min_t(int, bp->total_irqs, max_cp);
  4967. avail_msix = max_idx - bp->cp_nr_rings;
  4968. if (!BNXT_NEW_RM(bp) || avail_msix >= num)
  4969. return avail_msix;
  4970. if (max_irq < total_req) {
  4971. num = max_irq - bp->cp_nr_rings;
  4972. if (num <= 0)
  4973. return 0;
  4974. }
  4975. return num;
  4976. }
  4977. static int bnxt_get_num_msix(struct bnxt *bp)
  4978. {
  4979. if (!BNXT_NEW_RM(bp))
  4980. return bnxt_get_max_func_irqs(bp);
  4981. return bnxt_cp_rings_in_use(bp);
  4982. }
  4983. static int bnxt_init_msix(struct bnxt *bp)
  4984. {
  4985. int i, total_vecs, max, rc = 0, min = 1, ulp_msix;
  4986. struct msix_entry *msix_ent;
  4987. total_vecs = bnxt_get_num_msix(bp);
  4988. max = bnxt_get_max_func_irqs(bp);
  4989. if (total_vecs > max)
  4990. total_vecs = max;
  4991. if (!total_vecs)
  4992. return 0;
  4993. msix_ent = kcalloc(total_vecs, sizeof(struct msix_entry), GFP_KERNEL);
  4994. if (!msix_ent)
  4995. return -ENOMEM;
  4996. for (i = 0; i < total_vecs; i++) {
  4997. msix_ent[i].entry = i;
  4998. msix_ent[i].vector = 0;
  4999. }
  5000. if (!(bp->flags & BNXT_FLAG_SHARED_RINGS))
  5001. min = 2;
  5002. total_vecs = pci_enable_msix_range(bp->pdev, msix_ent, min, total_vecs);
  5003. ulp_msix = bnxt_get_ulp_msix_num(bp);
  5004. if (total_vecs < 0 || total_vecs < ulp_msix) {
  5005. rc = -ENODEV;
  5006. goto msix_setup_exit;
  5007. }
  5008. bp->irq_tbl = kcalloc(total_vecs, sizeof(struct bnxt_irq), GFP_KERNEL);
  5009. if (bp->irq_tbl) {
  5010. for (i = 0; i < total_vecs; i++)
  5011. bp->irq_tbl[i].vector = msix_ent[i].vector;
  5012. bp->total_irqs = total_vecs;
  5013. /* Trim rings based upon num of vectors allocated */
  5014. rc = bnxt_trim_rings(bp, &bp->rx_nr_rings, &bp->tx_nr_rings,
  5015. total_vecs - ulp_msix, min == 1);
  5016. if (rc)
  5017. goto msix_setup_exit;
  5018. bp->cp_nr_rings = (min == 1) ?
  5019. max_t(int, bp->tx_nr_rings, bp->rx_nr_rings) :
  5020. bp->tx_nr_rings + bp->rx_nr_rings;
  5021. } else {
  5022. rc = -ENOMEM;
  5023. goto msix_setup_exit;
  5024. }
  5025. bp->flags |= BNXT_FLAG_USING_MSIX;
  5026. kfree(msix_ent);
  5027. return 0;
  5028. msix_setup_exit:
  5029. netdev_err(bp->dev, "bnxt_init_msix err: %x\n", rc);
  5030. kfree(bp->irq_tbl);
  5031. bp->irq_tbl = NULL;
  5032. pci_disable_msix(bp->pdev);
  5033. kfree(msix_ent);
  5034. return rc;
  5035. }
  5036. static int bnxt_init_inta(struct bnxt *bp)
  5037. {
  5038. bp->irq_tbl = kcalloc(1, sizeof(struct bnxt_irq), GFP_KERNEL);
  5039. if (!bp->irq_tbl)
  5040. return -ENOMEM;
  5041. bp->total_irqs = 1;
  5042. bp->rx_nr_rings = 1;
  5043. bp->tx_nr_rings = 1;
  5044. bp->cp_nr_rings = 1;
  5045. bp->flags |= BNXT_FLAG_SHARED_RINGS;
  5046. bp->irq_tbl[0].vector = bp->pdev->irq;
  5047. return 0;
  5048. }
  5049. static int bnxt_init_int_mode(struct bnxt *bp)
  5050. {
  5051. int rc = 0;
  5052. if (bp->flags & BNXT_FLAG_MSIX_CAP)
  5053. rc = bnxt_init_msix(bp);
  5054. if (!(bp->flags & BNXT_FLAG_USING_MSIX) && BNXT_PF(bp)) {
  5055. /* fallback to INTA */
  5056. rc = bnxt_init_inta(bp);
  5057. }
  5058. return rc;
  5059. }
  5060. static void bnxt_clear_int_mode(struct bnxt *bp)
  5061. {
  5062. if (bp->flags & BNXT_FLAG_USING_MSIX)
  5063. pci_disable_msix(bp->pdev);
  5064. kfree(bp->irq_tbl);
  5065. bp->irq_tbl = NULL;
  5066. bp->flags &= ~BNXT_FLAG_USING_MSIX;
  5067. }
  5068. int bnxt_reserve_rings(struct bnxt *bp)
  5069. {
  5070. int tcs = netdev_get_num_tc(bp->dev);
  5071. bool reinit_irq = false;
  5072. int rc;
  5073. if (!bnxt_need_reserve_rings(bp))
  5074. return 0;
  5075. if (BNXT_NEW_RM(bp) && (bnxt_get_num_msix(bp) != bp->total_irqs)) {
  5076. bnxt_ulp_irq_stop(bp);
  5077. bnxt_clear_int_mode(bp);
  5078. reinit_irq = true;
  5079. }
  5080. rc = __bnxt_reserve_rings(bp);
  5081. if (reinit_irq) {
  5082. if (!rc)
  5083. rc = bnxt_init_int_mode(bp);
  5084. bnxt_ulp_irq_restart(bp, rc);
  5085. }
  5086. if (rc) {
  5087. netdev_err(bp->dev, "ring reservation/IRQ init failure rc: %d\n", rc);
  5088. return rc;
  5089. }
  5090. if (tcs && (bp->tx_nr_rings_per_tc * tcs != bp->tx_nr_rings)) {
  5091. netdev_err(bp->dev, "tx ring reservation failure\n");
  5092. netdev_reset_tc(bp->dev);
  5093. bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
  5094. return -ENOMEM;
  5095. }
  5096. bp->num_stat_ctxs = bp->cp_nr_rings;
  5097. return 0;
  5098. }
  5099. static void bnxt_free_irq(struct bnxt *bp)
  5100. {
  5101. struct bnxt_irq *irq;
  5102. int i;
  5103. #ifdef CONFIG_RFS_ACCEL
  5104. free_irq_cpu_rmap(bp->dev->rx_cpu_rmap);
  5105. bp->dev->rx_cpu_rmap = NULL;
  5106. #endif
  5107. if (!bp->irq_tbl || !bp->bnapi)
  5108. return;
  5109. for (i = 0; i < bp->cp_nr_rings; i++) {
  5110. int map_idx = bnxt_cp_num_to_irq_num(bp, i);
  5111. irq = &bp->irq_tbl[map_idx];
  5112. if (irq->requested) {
  5113. if (irq->have_cpumask) {
  5114. irq_set_affinity_hint(irq->vector, NULL);
  5115. free_cpumask_var(irq->cpu_mask);
  5116. irq->have_cpumask = 0;
  5117. }
  5118. free_irq(irq->vector, bp->bnapi[i]);
  5119. }
  5120. irq->requested = 0;
  5121. }
  5122. }
  5123. static int bnxt_request_irq(struct bnxt *bp)
  5124. {
  5125. int i, j, rc = 0;
  5126. unsigned long flags = 0;
  5127. #ifdef CONFIG_RFS_ACCEL
  5128. struct cpu_rmap *rmap;
  5129. #endif
  5130. rc = bnxt_setup_int_mode(bp);
  5131. if (rc) {
  5132. netdev_err(bp->dev, "bnxt_setup_int_mode err: %x\n",
  5133. rc);
  5134. return rc;
  5135. }
  5136. #ifdef CONFIG_RFS_ACCEL
  5137. rmap = bp->dev->rx_cpu_rmap;
  5138. #endif
  5139. if (!(bp->flags & BNXT_FLAG_USING_MSIX))
  5140. flags = IRQF_SHARED;
  5141. for (i = 0, j = 0; i < bp->cp_nr_rings; i++) {
  5142. int map_idx = bnxt_cp_num_to_irq_num(bp, i);
  5143. struct bnxt_irq *irq = &bp->irq_tbl[map_idx];
  5144. #ifdef CONFIG_RFS_ACCEL
  5145. if (rmap && bp->bnapi[i]->rx_ring) {
  5146. rc = irq_cpu_rmap_add(rmap, irq->vector);
  5147. if (rc)
  5148. netdev_warn(bp->dev, "failed adding irq rmap for ring %d\n",
  5149. j);
  5150. j++;
  5151. }
  5152. #endif
  5153. rc = request_irq(irq->vector, irq->handler, flags, irq->name,
  5154. bp->bnapi[i]);
  5155. if (rc)
  5156. break;
  5157. irq->requested = 1;
  5158. if (zalloc_cpumask_var(&irq->cpu_mask, GFP_KERNEL)) {
  5159. int numa_node = dev_to_node(&bp->pdev->dev);
  5160. irq->have_cpumask = 1;
  5161. cpumask_set_cpu(cpumask_local_spread(i, numa_node),
  5162. irq->cpu_mask);
  5163. rc = irq_set_affinity_hint(irq->vector, irq->cpu_mask);
  5164. if (rc) {
  5165. netdev_warn(bp->dev,
  5166. "Set affinity failed, IRQ = %d\n",
  5167. irq->vector);
  5168. break;
  5169. }
  5170. }
  5171. }
  5172. return rc;
  5173. }
  5174. static void bnxt_del_napi(struct bnxt *bp)
  5175. {
  5176. int i;
  5177. if (!bp->bnapi)
  5178. return;
  5179. for (i = 0; i < bp->cp_nr_rings; i++) {
  5180. struct bnxt_napi *bnapi = bp->bnapi[i];
  5181. napi_hash_del(&bnapi->napi);
  5182. netif_napi_del(&bnapi->napi);
  5183. }
  5184. /* We called napi_hash_del() before netif_napi_del(), we need
  5185. * to respect an RCU grace period before freeing napi structures.
  5186. */
  5187. synchronize_net();
  5188. }
  5189. static void bnxt_init_napi(struct bnxt *bp)
  5190. {
  5191. int i;
  5192. unsigned int cp_nr_rings = bp->cp_nr_rings;
  5193. struct bnxt_napi *bnapi;
  5194. if (bp->flags & BNXT_FLAG_USING_MSIX) {
  5195. if (BNXT_CHIP_TYPE_NITRO_A0(bp))
  5196. cp_nr_rings--;
  5197. for (i = 0; i < cp_nr_rings; i++) {
  5198. bnapi = bp->bnapi[i];
  5199. netif_napi_add(bp->dev, &bnapi->napi,
  5200. bnxt_poll, 64);
  5201. }
  5202. if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
  5203. bnapi = bp->bnapi[cp_nr_rings];
  5204. netif_napi_add(bp->dev, &bnapi->napi,
  5205. bnxt_poll_nitroa0, 64);
  5206. }
  5207. } else {
  5208. bnapi = bp->bnapi[0];
  5209. netif_napi_add(bp->dev, &bnapi->napi, bnxt_poll, 64);
  5210. }
  5211. }
  5212. static void bnxt_disable_napi(struct bnxt *bp)
  5213. {
  5214. int i;
  5215. if (!bp->bnapi)
  5216. return;
  5217. for (i = 0; i < bp->cp_nr_rings; i++) {
  5218. struct bnxt_cp_ring_info *cpr = &bp->bnapi[i]->cp_ring;
  5219. if (bp->bnapi[i]->rx_ring)
  5220. cancel_work_sync(&cpr->dim.work);
  5221. napi_disable(&bp->bnapi[i]->napi);
  5222. }
  5223. }
  5224. static void bnxt_enable_napi(struct bnxt *bp)
  5225. {
  5226. int i;
  5227. for (i = 0; i < bp->cp_nr_rings; i++) {
  5228. struct bnxt_cp_ring_info *cpr = &bp->bnapi[i]->cp_ring;
  5229. bp->bnapi[i]->in_reset = false;
  5230. if (bp->bnapi[i]->rx_ring) {
  5231. INIT_WORK(&cpr->dim.work, bnxt_dim_work);
  5232. cpr->dim.mode = NET_DIM_CQ_PERIOD_MODE_START_FROM_EQE;
  5233. }
  5234. napi_enable(&bp->bnapi[i]->napi);
  5235. }
  5236. }
  5237. void bnxt_tx_disable(struct bnxt *bp)
  5238. {
  5239. int i;
  5240. struct bnxt_tx_ring_info *txr;
  5241. if (bp->tx_ring) {
  5242. for (i = 0; i < bp->tx_nr_rings; i++) {
  5243. txr = &bp->tx_ring[i];
  5244. txr->dev_state = BNXT_DEV_STATE_CLOSING;
  5245. }
  5246. }
  5247. /* Drop carrier first to prevent TX timeout */
  5248. netif_carrier_off(bp->dev);
  5249. /* Stop all TX queues */
  5250. netif_tx_disable(bp->dev);
  5251. }
  5252. void bnxt_tx_enable(struct bnxt *bp)
  5253. {
  5254. int i;
  5255. struct bnxt_tx_ring_info *txr;
  5256. for (i = 0; i < bp->tx_nr_rings; i++) {
  5257. txr = &bp->tx_ring[i];
  5258. txr->dev_state = 0;
  5259. }
  5260. netif_tx_wake_all_queues(bp->dev);
  5261. if (bp->link_info.link_up)
  5262. netif_carrier_on(bp->dev);
  5263. }
  5264. static void bnxt_report_link(struct bnxt *bp)
  5265. {
  5266. if (bp->link_info.link_up) {
  5267. const char *duplex;
  5268. const char *flow_ctrl;
  5269. u32 speed;
  5270. u16 fec;
  5271. netif_carrier_on(bp->dev);
  5272. speed = bnxt_fw_to_ethtool_speed(bp->link_info.link_speed);
  5273. if (speed == SPEED_UNKNOWN) {
  5274. netdev_info(bp->dev, "NIC Link is Up, speed unknown\n");
  5275. return;
  5276. }
  5277. if (bp->link_info.duplex == BNXT_LINK_DUPLEX_FULL)
  5278. duplex = "full";
  5279. else
  5280. duplex = "half";
  5281. if (bp->link_info.pause == BNXT_LINK_PAUSE_BOTH)
  5282. flow_ctrl = "ON - receive & transmit";
  5283. else if (bp->link_info.pause == BNXT_LINK_PAUSE_TX)
  5284. flow_ctrl = "ON - transmit";
  5285. else if (bp->link_info.pause == BNXT_LINK_PAUSE_RX)
  5286. flow_ctrl = "ON - receive";
  5287. else
  5288. flow_ctrl = "none";
  5289. netdev_info(bp->dev, "NIC Link is Up, %u Mbps %s duplex, Flow control: %s\n",
  5290. speed, duplex, flow_ctrl);
  5291. if (bp->flags & BNXT_FLAG_EEE_CAP)
  5292. netdev_info(bp->dev, "EEE is %s\n",
  5293. bp->eee.eee_active ? "active" :
  5294. "not active");
  5295. fec = bp->link_info.fec_cfg;
  5296. if (!(fec & PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED))
  5297. netdev_info(bp->dev, "FEC autoneg %s encodings: %s\n",
  5298. (fec & BNXT_FEC_AUTONEG) ? "on" : "off",
  5299. (fec & BNXT_FEC_ENC_BASE_R) ? "BaseR" :
  5300. (fec & BNXT_FEC_ENC_RS) ? "RS" : "None");
  5301. } else {
  5302. netif_carrier_off(bp->dev);
  5303. netdev_err(bp->dev, "NIC Link is Down\n");
  5304. }
  5305. }
  5306. static int bnxt_hwrm_phy_qcaps(struct bnxt *bp)
  5307. {
  5308. int rc = 0;
  5309. struct hwrm_port_phy_qcaps_input req = {0};
  5310. struct hwrm_port_phy_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
  5311. struct bnxt_link_info *link_info = &bp->link_info;
  5312. if (bp->hwrm_spec_code < 0x10201)
  5313. return 0;
  5314. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_QCAPS, -1, -1);
  5315. mutex_lock(&bp->hwrm_cmd_lock);
  5316. rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  5317. if (rc)
  5318. goto hwrm_phy_qcaps_exit;
  5319. if (resp->flags & PORT_PHY_QCAPS_RESP_FLAGS_EEE_SUPPORTED) {
  5320. struct ethtool_eee *eee = &bp->eee;
  5321. u16 fw_speeds = le16_to_cpu(resp->supported_speeds_eee_mode);
  5322. bp->flags |= BNXT_FLAG_EEE_CAP;
  5323. eee->supported = _bnxt_fw_to_ethtool_adv_spds(fw_speeds, 0);
  5324. bp->lpi_tmr_lo = le32_to_cpu(resp->tx_lpi_timer_low) &
  5325. PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_LOW_MASK;
  5326. bp->lpi_tmr_hi = le32_to_cpu(resp->valid_tx_lpi_timer_high) &
  5327. PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_HIGH_MASK;
  5328. }
  5329. if (resp->flags & PORT_PHY_QCAPS_RESP_FLAGS_EXTERNAL_LPBK_SUPPORTED) {
  5330. if (bp->test_info)
  5331. bp->test_info->flags |= BNXT_TEST_FL_EXT_LPBK;
  5332. }
  5333. if (resp->supported_speeds_auto_mode)
  5334. link_info->support_auto_speeds =
  5335. le16_to_cpu(resp->supported_speeds_auto_mode);
  5336. bp->port_count = resp->port_cnt;
  5337. hwrm_phy_qcaps_exit:
  5338. mutex_unlock(&bp->hwrm_cmd_lock);
  5339. return rc;
  5340. }
  5341. static int bnxt_update_link(struct bnxt *bp, bool chng_link_state)
  5342. {
  5343. int rc = 0;
  5344. struct bnxt_link_info *link_info = &bp->link_info;
  5345. struct hwrm_port_phy_qcfg_input req = {0};
  5346. struct hwrm_port_phy_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
  5347. u8 link_up = link_info->link_up;
  5348. u16 diff;
  5349. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_QCFG, -1, -1);
  5350. mutex_lock(&bp->hwrm_cmd_lock);
  5351. rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  5352. if (rc) {
  5353. mutex_unlock(&bp->hwrm_cmd_lock);
  5354. return rc;
  5355. }
  5356. memcpy(&link_info->phy_qcfg_resp, resp, sizeof(*resp));
  5357. link_info->phy_link_status = resp->link;
  5358. link_info->duplex = resp->duplex_cfg;
  5359. if (bp->hwrm_spec_code >= 0x10800)
  5360. link_info->duplex = resp->duplex_state;
  5361. link_info->pause = resp->pause;
  5362. link_info->auto_mode = resp->auto_mode;
  5363. link_info->auto_pause_setting = resp->auto_pause;
  5364. link_info->lp_pause = resp->link_partner_adv_pause;
  5365. link_info->force_pause_setting = resp->force_pause;
  5366. link_info->duplex_setting = resp->duplex_cfg;
  5367. if (link_info->phy_link_status == BNXT_LINK_LINK)
  5368. link_info->link_speed = le16_to_cpu(resp->link_speed);
  5369. else
  5370. link_info->link_speed = 0;
  5371. link_info->force_link_speed = le16_to_cpu(resp->force_link_speed);
  5372. link_info->support_speeds = le16_to_cpu(resp->support_speeds);
  5373. link_info->auto_link_speeds = le16_to_cpu(resp->auto_link_speed_mask);
  5374. link_info->lp_auto_link_speeds =
  5375. le16_to_cpu(resp->link_partner_adv_speeds);
  5376. link_info->preemphasis = le32_to_cpu(resp->preemphasis);
  5377. link_info->phy_ver[0] = resp->phy_maj;
  5378. link_info->phy_ver[1] = resp->phy_min;
  5379. link_info->phy_ver[2] = resp->phy_bld;
  5380. link_info->media_type = resp->media_type;
  5381. link_info->phy_type = resp->phy_type;
  5382. link_info->transceiver = resp->xcvr_pkg_type;
  5383. link_info->phy_addr = resp->eee_config_phy_addr &
  5384. PORT_PHY_QCFG_RESP_PHY_ADDR_MASK;
  5385. link_info->module_status = resp->module_status;
  5386. if (bp->flags & BNXT_FLAG_EEE_CAP) {
  5387. struct ethtool_eee *eee = &bp->eee;
  5388. u16 fw_speeds;
  5389. eee->eee_active = 0;
  5390. if (resp->eee_config_phy_addr &
  5391. PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ACTIVE) {
  5392. eee->eee_active = 1;
  5393. fw_speeds = le16_to_cpu(
  5394. resp->link_partner_adv_eee_link_speed_mask);
  5395. eee->lp_advertised =
  5396. _bnxt_fw_to_ethtool_adv_spds(fw_speeds, 0);
  5397. }
  5398. /* Pull initial EEE config */
  5399. if (!chng_link_state) {
  5400. if (resp->eee_config_phy_addr &
  5401. PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ENABLED)
  5402. eee->eee_enabled = 1;
  5403. fw_speeds = le16_to_cpu(resp->adv_eee_link_speed_mask);
  5404. eee->advertised =
  5405. _bnxt_fw_to_ethtool_adv_spds(fw_speeds, 0);
  5406. if (resp->eee_config_phy_addr &
  5407. PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_TX_LPI) {
  5408. __le32 tmr;
  5409. eee->tx_lpi_enabled = 1;
  5410. tmr = resp->xcvr_identifier_type_tx_lpi_timer;
  5411. eee->tx_lpi_timer = le32_to_cpu(tmr) &
  5412. PORT_PHY_QCFG_RESP_TX_LPI_TIMER_MASK;
  5413. }
  5414. }
  5415. }
  5416. link_info->fec_cfg = PORT_PHY_QCFG_RESP_FEC_CFG_FEC_NONE_SUPPORTED;
  5417. if (bp->hwrm_spec_code >= 0x10504)
  5418. link_info->fec_cfg = le16_to_cpu(resp->fec_cfg);
  5419. /* TODO: need to add more logic to report VF link */
  5420. if (chng_link_state) {
  5421. if (link_info->phy_link_status == BNXT_LINK_LINK)
  5422. link_info->link_up = 1;
  5423. else
  5424. link_info->link_up = 0;
  5425. if (link_up != link_info->link_up)
  5426. bnxt_report_link(bp);
  5427. } else {
  5428. /* alwasy link down if not require to update link state */
  5429. link_info->link_up = 0;
  5430. }
  5431. mutex_unlock(&bp->hwrm_cmd_lock);
  5432. if (!BNXT_SINGLE_PF(bp))
  5433. return 0;
  5434. diff = link_info->support_auto_speeds ^ link_info->advertising;
  5435. if ((link_info->support_auto_speeds | diff) !=
  5436. link_info->support_auto_speeds) {
  5437. /* An advertised speed is no longer supported, so we need to
  5438. * update the advertisement settings. Caller holds RTNL
  5439. * so we can modify link settings.
  5440. */
  5441. link_info->advertising = link_info->support_auto_speeds;
  5442. if (link_info->autoneg & BNXT_AUTONEG_SPEED)
  5443. bnxt_hwrm_set_link_setting(bp, true, false);
  5444. }
  5445. return 0;
  5446. }
  5447. static void bnxt_get_port_module_status(struct bnxt *bp)
  5448. {
  5449. struct bnxt_link_info *link_info = &bp->link_info;
  5450. struct hwrm_port_phy_qcfg_output *resp = &link_info->phy_qcfg_resp;
  5451. u8 module_status;
  5452. if (bnxt_update_link(bp, true))
  5453. return;
  5454. module_status = link_info->module_status;
  5455. switch (module_status) {
  5456. case PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX:
  5457. case PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN:
  5458. case PORT_PHY_QCFG_RESP_MODULE_STATUS_WARNINGMSG:
  5459. netdev_warn(bp->dev, "Unqualified SFP+ module detected on port %d\n",
  5460. bp->pf.port_id);
  5461. if (bp->hwrm_spec_code >= 0x10201) {
  5462. netdev_warn(bp->dev, "Module part number %s\n",
  5463. resp->phy_vendor_partnumber);
  5464. }
  5465. if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX)
  5466. netdev_warn(bp->dev, "TX is disabled\n");
  5467. if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN)
  5468. netdev_warn(bp->dev, "SFP+ module is shutdown\n");
  5469. }
  5470. }
  5471. static void
  5472. bnxt_hwrm_set_pause_common(struct bnxt *bp, struct hwrm_port_phy_cfg_input *req)
  5473. {
  5474. if (bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) {
  5475. if (bp->hwrm_spec_code >= 0x10201)
  5476. req->auto_pause =
  5477. PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE;
  5478. if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
  5479. req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_RX;
  5480. if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
  5481. req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_TX;
  5482. req->enables |=
  5483. cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
  5484. } else {
  5485. if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
  5486. req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_RX;
  5487. if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
  5488. req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_TX;
  5489. req->enables |=
  5490. cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_PAUSE);
  5491. if (bp->hwrm_spec_code >= 0x10201) {
  5492. req->auto_pause = req->force_pause;
  5493. req->enables |= cpu_to_le32(
  5494. PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
  5495. }
  5496. }
  5497. }
  5498. static void bnxt_hwrm_set_link_common(struct bnxt *bp,
  5499. struct hwrm_port_phy_cfg_input *req)
  5500. {
  5501. u8 autoneg = bp->link_info.autoneg;
  5502. u16 fw_link_speed = bp->link_info.req_link_speed;
  5503. u16 advertising = bp->link_info.advertising;
  5504. if (autoneg & BNXT_AUTONEG_SPEED) {
  5505. req->auto_mode |=
  5506. PORT_PHY_CFG_REQ_AUTO_MODE_SPEED_MASK;
  5507. req->enables |= cpu_to_le32(
  5508. PORT_PHY_CFG_REQ_ENABLES_AUTO_LINK_SPEED_MASK);
  5509. req->auto_link_speed_mask = cpu_to_le16(advertising);
  5510. req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_MODE);
  5511. req->flags |=
  5512. cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESTART_AUTONEG);
  5513. } else {
  5514. req->force_link_speed = cpu_to_le16(fw_link_speed);
  5515. req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE);
  5516. }
  5517. /* tell chimp that the setting takes effect immediately */
  5518. req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESET_PHY);
  5519. }
  5520. int bnxt_hwrm_set_pause(struct bnxt *bp)
  5521. {
  5522. struct hwrm_port_phy_cfg_input req = {0};
  5523. int rc;
  5524. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_CFG, -1, -1);
  5525. bnxt_hwrm_set_pause_common(bp, &req);
  5526. if ((bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) ||
  5527. bp->link_info.force_link_chng)
  5528. bnxt_hwrm_set_link_common(bp, &req);
  5529. mutex_lock(&bp->hwrm_cmd_lock);
  5530. rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  5531. if (!rc && !(bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL)) {
  5532. /* since changing of pause setting doesn't trigger any link
  5533. * change event, the driver needs to update the current pause
  5534. * result upon successfully return of the phy_cfg command
  5535. */
  5536. bp->link_info.pause =
  5537. bp->link_info.force_pause_setting = bp->link_info.req_flow_ctrl;
  5538. bp->link_info.auto_pause_setting = 0;
  5539. if (!bp->link_info.force_link_chng)
  5540. bnxt_report_link(bp);
  5541. }
  5542. bp->link_info.force_link_chng = false;
  5543. mutex_unlock(&bp->hwrm_cmd_lock);
  5544. return rc;
  5545. }
  5546. static void bnxt_hwrm_set_eee(struct bnxt *bp,
  5547. struct hwrm_port_phy_cfg_input *req)
  5548. {
  5549. struct ethtool_eee *eee = &bp->eee;
  5550. if (eee->eee_enabled) {
  5551. u16 eee_speeds;
  5552. u32 flags = PORT_PHY_CFG_REQ_FLAGS_EEE_ENABLE;
  5553. if (eee->tx_lpi_enabled)
  5554. flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_ENABLE;
  5555. else
  5556. flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_DISABLE;
  5557. req->flags |= cpu_to_le32(flags);
  5558. eee_speeds = bnxt_get_fw_auto_link_speeds(eee->advertised);
  5559. req->eee_link_speed_mask = cpu_to_le16(eee_speeds);
  5560. req->tx_lpi_timer = cpu_to_le32(eee->tx_lpi_timer);
  5561. } else {
  5562. req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_EEE_DISABLE);
  5563. }
  5564. }
  5565. int bnxt_hwrm_set_link_setting(struct bnxt *bp, bool set_pause, bool set_eee)
  5566. {
  5567. struct hwrm_port_phy_cfg_input req = {0};
  5568. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_CFG, -1, -1);
  5569. if (set_pause)
  5570. bnxt_hwrm_set_pause_common(bp, &req);
  5571. bnxt_hwrm_set_link_common(bp, &req);
  5572. if (set_eee)
  5573. bnxt_hwrm_set_eee(bp, &req);
  5574. return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  5575. }
  5576. static int bnxt_hwrm_shutdown_link(struct bnxt *bp)
  5577. {
  5578. struct hwrm_port_phy_cfg_input req = {0};
  5579. if (!BNXT_SINGLE_PF(bp))
  5580. return 0;
  5581. if (pci_num_vf(bp->pdev))
  5582. return 0;
  5583. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_CFG, -1, -1);
  5584. req.flags = cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE_LINK_DWN);
  5585. return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  5586. }
  5587. static int bnxt_hwrm_if_change(struct bnxt *bp, bool up)
  5588. {
  5589. struct hwrm_func_drv_if_change_output *resp = bp->hwrm_cmd_resp_addr;
  5590. struct hwrm_func_drv_if_change_input req = {0};
  5591. bool resc_reinit = false;
  5592. int rc;
  5593. if (!(bp->fw_cap & BNXT_FW_CAP_IF_CHANGE))
  5594. return 0;
  5595. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_DRV_IF_CHANGE, -1, -1);
  5596. if (up)
  5597. req.flags = cpu_to_le32(FUNC_DRV_IF_CHANGE_REQ_FLAGS_UP);
  5598. mutex_lock(&bp->hwrm_cmd_lock);
  5599. rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  5600. if (!rc && (resp->flags &
  5601. cpu_to_le32(FUNC_DRV_IF_CHANGE_RESP_FLAGS_RESC_CHANGE)))
  5602. resc_reinit = true;
  5603. mutex_unlock(&bp->hwrm_cmd_lock);
  5604. if (up && resc_reinit && BNXT_NEW_RM(bp)) {
  5605. struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
  5606. rc = bnxt_hwrm_func_resc_qcaps(bp, true);
  5607. hw_resc->resv_cp_rings = 0;
  5608. hw_resc->resv_tx_rings = 0;
  5609. hw_resc->resv_rx_rings = 0;
  5610. hw_resc->resv_hw_ring_grps = 0;
  5611. hw_resc->resv_vnics = 0;
  5612. bp->tx_nr_rings = 0;
  5613. bp->rx_nr_rings = 0;
  5614. }
  5615. return rc;
  5616. }
  5617. static int bnxt_hwrm_port_led_qcaps(struct bnxt *bp)
  5618. {
  5619. struct hwrm_port_led_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
  5620. struct hwrm_port_led_qcaps_input req = {0};
  5621. struct bnxt_pf_info *pf = &bp->pf;
  5622. int rc;
  5623. if (BNXT_VF(bp) || bp->hwrm_spec_code < 0x10601)
  5624. return 0;
  5625. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_LED_QCAPS, -1, -1);
  5626. req.port_id = cpu_to_le16(pf->port_id);
  5627. mutex_lock(&bp->hwrm_cmd_lock);
  5628. rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  5629. if (rc) {
  5630. mutex_unlock(&bp->hwrm_cmd_lock);
  5631. return rc;
  5632. }
  5633. if (resp->num_leds > 0 && resp->num_leds < BNXT_MAX_LED) {
  5634. int i;
  5635. bp->num_leds = resp->num_leds;
  5636. memcpy(bp->leds, &resp->led0_id, sizeof(bp->leds[0]) *
  5637. bp->num_leds);
  5638. for (i = 0; i < bp->num_leds; i++) {
  5639. struct bnxt_led_info *led = &bp->leds[i];
  5640. __le16 caps = led->led_state_caps;
  5641. if (!led->led_group_id ||
  5642. !BNXT_LED_ALT_BLINK_CAP(caps)) {
  5643. bp->num_leds = 0;
  5644. break;
  5645. }
  5646. }
  5647. }
  5648. mutex_unlock(&bp->hwrm_cmd_lock);
  5649. return 0;
  5650. }
  5651. int bnxt_hwrm_alloc_wol_fltr(struct bnxt *bp)
  5652. {
  5653. struct hwrm_wol_filter_alloc_input req = {0};
  5654. struct hwrm_wol_filter_alloc_output *resp = bp->hwrm_cmd_resp_addr;
  5655. int rc;
  5656. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_WOL_FILTER_ALLOC, -1, -1);
  5657. req.port_id = cpu_to_le16(bp->pf.port_id);
  5658. req.wol_type = WOL_FILTER_ALLOC_REQ_WOL_TYPE_MAGICPKT;
  5659. req.enables = cpu_to_le32(WOL_FILTER_ALLOC_REQ_ENABLES_MAC_ADDRESS);
  5660. memcpy(req.mac_address, bp->dev->dev_addr, ETH_ALEN);
  5661. mutex_lock(&bp->hwrm_cmd_lock);
  5662. rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  5663. if (!rc)
  5664. bp->wol_filter_id = resp->wol_filter_id;
  5665. mutex_unlock(&bp->hwrm_cmd_lock);
  5666. return rc;
  5667. }
  5668. int bnxt_hwrm_free_wol_fltr(struct bnxt *bp)
  5669. {
  5670. struct hwrm_wol_filter_free_input req = {0};
  5671. int rc;
  5672. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_WOL_FILTER_FREE, -1, -1);
  5673. req.port_id = cpu_to_le16(bp->pf.port_id);
  5674. req.enables = cpu_to_le32(WOL_FILTER_FREE_REQ_ENABLES_WOL_FILTER_ID);
  5675. req.wol_filter_id = bp->wol_filter_id;
  5676. rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  5677. return rc;
  5678. }
  5679. static u16 bnxt_hwrm_get_wol_fltrs(struct bnxt *bp, u16 handle)
  5680. {
  5681. struct hwrm_wol_filter_qcfg_input req = {0};
  5682. struct hwrm_wol_filter_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
  5683. u16 next_handle = 0;
  5684. int rc;
  5685. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_WOL_FILTER_QCFG, -1, -1);
  5686. req.port_id = cpu_to_le16(bp->pf.port_id);
  5687. req.handle = cpu_to_le16(handle);
  5688. mutex_lock(&bp->hwrm_cmd_lock);
  5689. rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  5690. if (!rc) {
  5691. next_handle = le16_to_cpu(resp->next_handle);
  5692. if (next_handle != 0) {
  5693. if (resp->wol_type ==
  5694. WOL_FILTER_ALLOC_REQ_WOL_TYPE_MAGICPKT) {
  5695. bp->wol = 1;
  5696. bp->wol_filter_id = resp->wol_filter_id;
  5697. }
  5698. }
  5699. }
  5700. mutex_unlock(&bp->hwrm_cmd_lock);
  5701. return next_handle;
  5702. }
  5703. static void bnxt_get_wol_settings(struct bnxt *bp)
  5704. {
  5705. u16 handle = 0;
  5706. if (!BNXT_PF(bp) || !(bp->flags & BNXT_FLAG_WOL_CAP))
  5707. return;
  5708. do {
  5709. handle = bnxt_hwrm_get_wol_fltrs(bp, handle);
  5710. } while (handle && handle != 0xffff);
  5711. }
  5712. #ifdef CONFIG_BNXT_HWMON
  5713. static ssize_t bnxt_show_temp(struct device *dev,
  5714. struct device_attribute *devattr, char *buf)
  5715. {
  5716. struct hwrm_temp_monitor_query_input req = {0};
  5717. struct hwrm_temp_monitor_query_output *resp;
  5718. struct bnxt *bp = dev_get_drvdata(dev);
  5719. u32 len = 0;
  5720. int rc;
  5721. resp = bp->hwrm_cmd_resp_addr;
  5722. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_TEMP_MONITOR_QUERY, -1, -1);
  5723. mutex_lock(&bp->hwrm_cmd_lock);
  5724. rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  5725. if (!rc)
  5726. len = sprintf(buf, "%u\n", resp->temp * 1000); /* display millidegree */
  5727. mutex_unlock(&bp->hwrm_cmd_lock);
  5728. if (rc)
  5729. return rc;
  5730. return len;
  5731. }
  5732. static SENSOR_DEVICE_ATTR(temp1_input, 0444, bnxt_show_temp, NULL, 0);
  5733. static struct attribute *bnxt_attrs[] = {
  5734. &sensor_dev_attr_temp1_input.dev_attr.attr,
  5735. NULL
  5736. };
  5737. ATTRIBUTE_GROUPS(bnxt);
  5738. static void bnxt_hwmon_close(struct bnxt *bp)
  5739. {
  5740. if (bp->hwmon_dev) {
  5741. hwmon_device_unregister(bp->hwmon_dev);
  5742. bp->hwmon_dev = NULL;
  5743. }
  5744. }
  5745. static void bnxt_hwmon_open(struct bnxt *bp)
  5746. {
  5747. struct hwrm_temp_monitor_query_input req = {0};
  5748. struct pci_dev *pdev = bp->pdev;
  5749. int rc;
  5750. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_TEMP_MONITOR_QUERY, -1, -1);
  5751. rc = hwrm_send_message_silent(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  5752. if (rc == -EACCES || rc == -EOPNOTSUPP) {
  5753. bnxt_hwmon_close(bp);
  5754. return;
  5755. }
  5756. bp->hwmon_dev = hwmon_device_register_with_groups(&pdev->dev,
  5757. DRV_MODULE_NAME, bp,
  5758. bnxt_groups);
  5759. if (IS_ERR(bp->hwmon_dev)) {
  5760. bp->hwmon_dev = NULL;
  5761. dev_warn(&pdev->dev, "Cannot register hwmon device\n");
  5762. }
  5763. }
  5764. #else
  5765. static void bnxt_hwmon_close(struct bnxt *bp)
  5766. {
  5767. }
  5768. static void bnxt_hwmon_open(struct bnxt *bp)
  5769. {
  5770. }
  5771. #endif
  5772. static bool bnxt_eee_config_ok(struct bnxt *bp)
  5773. {
  5774. struct ethtool_eee *eee = &bp->eee;
  5775. struct bnxt_link_info *link_info = &bp->link_info;
  5776. if (!(bp->flags & BNXT_FLAG_EEE_CAP))
  5777. return true;
  5778. if (eee->eee_enabled) {
  5779. u32 advertising =
  5780. _bnxt_fw_to_ethtool_adv_spds(link_info->advertising, 0);
  5781. if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
  5782. eee->eee_enabled = 0;
  5783. return false;
  5784. }
  5785. if (eee->advertised & ~advertising) {
  5786. eee->advertised = advertising & eee->supported;
  5787. return false;
  5788. }
  5789. }
  5790. return true;
  5791. }
  5792. static int bnxt_update_phy_setting(struct bnxt *bp)
  5793. {
  5794. int rc;
  5795. bool update_link = false;
  5796. bool update_pause = false;
  5797. bool update_eee = false;
  5798. struct bnxt_link_info *link_info = &bp->link_info;
  5799. rc = bnxt_update_link(bp, true);
  5800. if (rc) {
  5801. netdev_err(bp->dev, "failed to update link (rc: %x)\n",
  5802. rc);
  5803. return rc;
  5804. }
  5805. if (!BNXT_SINGLE_PF(bp))
  5806. return 0;
  5807. if ((link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
  5808. (link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH) !=
  5809. link_info->req_flow_ctrl)
  5810. update_pause = true;
  5811. if (!(link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
  5812. link_info->force_pause_setting != link_info->req_flow_ctrl)
  5813. update_pause = true;
  5814. if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
  5815. if (BNXT_AUTO_MODE(link_info->auto_mode))
  5816. update_link = true;
  5817. if (link_info->req_link_speed != link_info->force_link_speed)
  5818. update_link = true;
  5819. if (link_info->req_duplex != link_info->duplex_setting)
  5820. update_link = true;
  5821. } else {
  5822. if (link_info->auto_mode == BNXT_LINK_AUTO_NONE)
  5823. update_link = true;
  5824. if (link_info->advertising != link_info->auto_link_speeds)
  5825. update_link = true;
  5826. }
  5827. /* The last close may have shutdown the link, so need to call
  5828. * PHY_CFG to bring it back up.
  5829. */
  5830. if (!netif_carrier_ok(bp->dev))
  5831. update_link = true;
  5832. if (!bnxt_eee_config_ok(bp))
  5833. update_eee = true;
  5834. if (update_link)
  5835. rc = bnxt_hwrm_set_link_setting(bp, update_pause, update_eee);
  5836. else if (update_pause)
  5837. rc = bnxt_hwrm_set_pause(bp);
  5838. if (rc) {
  5839. netdev_err(bp->dev, "failed to update phy setting (rc: %x)\n",
  5840. rc);
  5841. return rc;
  5842. }
  5843. return rc;
  5844. }
  5845. /* Common routine to pre-map certain register block to different GRC window.
  5846. * A PF has 16 4K windows and a VF has 4 4K windows. However, only 15 windows
  5847. * in PF and 3 windows in VF that can be customized to map in different
  5848. * register blocks.
  5849. */
  5850. static void bnxt_preset_reg_win(struct bnxt *bp)
  5851. {
  5852. if (BNXT_PF(bp)) {
  5853. /* CAG registers map to GRC window #4 */
  5854. writel(BNXT_CAG_REG_BASE,
  5855. bp->bar0 + BNXT_GRCPF_REG_WINDOW_BASE_OUT + 12);
  5856. }
  5857. }
  5858. static int bnxt_init_dflt_ring_mode(struct bnxt *bp);
  5859. static int __bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
  5860. {
  5861. int rc = 0;
  5862. bnxt_preset_reg_win(bp);
  5863. netif_carrier_off(bp->dev);
  5864. if (irq_re_init) {
  5865. /* Reserve rings now if none were reserved at driver probe. */
  5866. rc = bnxt_init_dflt_ring_mode(bp);
  5867. if (rc) {
  5868. netdev_err(bp->dev, "Failed to reserve default rings at open\n");
  5869. return rc;
  5870. }
  5871. rc = bnxt_reserve_rings(bp);
  5872. if (rc)
  5873. return rc;
  5874. }
  5875. if ((bp->flags & BNXT_FLAG_RFS) &&
  5876. !(bp->flags & BNXT_FLAG_USING_MSIX)) {
  5877. /* disable RFS if falling back to INTA */
  5878. bp->dev->hw_features &= ~NETIF_F_NTUPLE;
  5879. bp->flags &= ~BNXT_FLAG_RFS;
  5880. }
  5881. rc = bnxt_alloc_mem(bp, irq_re_init);
  5882. if (rc) {
  5883. netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc);
  5884. goto open_err_free_mem;
  5885. }
  5886. if (irq_re_init) {
  5887. bnxt_init_napi(bp);
  5888. rc = bnxt_request_irq(bp);
  5889. if (rc) {
  5890. netdev_err(bp->dev, "bnxt_request_irq err: %x\n", rc);
  5891. goto open_err_irq;
  5892. }
  5893. }
  5894. rc = bnxt_init_nic(bp, irq_re_init);
  5895. if (rc) {
  5896. netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc);
  5897. goto open_err_irq;
  5898. }
  5899. bnxt_enable_napi(bp);
  5900. bnxt_debug_dev_init(bp);
  5901. if (link_re_init) {
  5902. mutex_lock(&bp->link_lock);
  5903. rc = bnxt_update_phy_setting(bp);
  5904. mutex_unlock(&bp->link_lock);
  5905. if (rc) {
  5906. netdev_warn(bp->dev, "failed to update phy settings\n");
  5907. if (BNXT_SINGLE_PF(bp)) {
  5908. bp->link_info.phy_retry = true;
  5909. bp->link_info.phy_retry_expires =
  5910. jiffies + 5 * HZ;
  5911. }
  5912. }
  5913. }
  5914. if (irq_re_init)
  5915. udp_tunnel_get_rx_info(bp->dev);
  5916. set_bit(BNXT_STATE_OPEN, &bp->state);
  5917. bnxt_enable_int(bp);
  5918. /* Enable TX queues */
  5919. bnxt_tx_enable(bp);
  5920. mod_timer(&bp->timer, jiffies + bp->current_interval);
  5921. /* Poll link status and check for SFP+ module status */
  5922. bnxt_get_port_module_status(bp);
  5923. /* VF-reps may need to be re-opened after the PF is re-opened */
  5924. if (BNXT_PF(bp))
  5925. bnxt_vf_reps_open(bp);
  5926. return 0;
  5927. open_err_irq:
  5928. bnxt_del_napi(bp);
  5929. open_err_free_mem:
  5930. bnxt_free_skbs(bp);
  5931. bnxt_free_irq(bp);
  5932. bnxt_free_mem(bp, true);
  5933. return rc;
  5934. }
  5935. /* rtnl_lock held */
  5936. int bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
  5937. {
  5938. int rc = 0;
  5939. rc = __bnxt_open_nic(bp, irq_re_init, link_re_init);
  5940. if (rc) {
  5941. netdev_err(bp->dev, "nic open fail (rc: %x)\n", rc);
  5942. dev_close(bp->dev);
  5943. }
  5944. return rc;
  5945. }
  5946. /* rtnl_lock held, open the NIC half way by allocating all resources, but
  5947. * NAPI, IRQ, and TX are not enabled. This is mainly used for offline
  5948. * self tests.
  5949. */
  5950. int bnxt_half_open_nic(struct bnxt *bp)
  5951. {
  5952. int rc = 0;
  5953. rc = bnxt_alloc_mem(bp, false);
  5954. if (rc) {
  5955. netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc);
  5956. goto half_open_err;
  5957. }
  5958. rc = bnxt_init_nic(bp, false);
  5959. if (rc) {
  5960. netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc);
  5961. goto half_open_err;
  5962. }
  5963. return 0;
  5964. half_open_err:
  5965. bnxt_free_skbs(bp);
  5966. bnxt_free_mem(bp, false);
  5967. dev_close(bp->dev);
  5968. return rc;
  5969. }
  5970. /* rtnl_lock held, this call can only be made after a previous successful
  5971. * call to bnxt_half_open_nic().
  5972. */
  5973. void bnxt_half_close_nic(struct bnxt *bp)
  5974. {
  5975. bnxt_hwrm_resource_free(bp, false, false);
  5976. bnxt_free_skbs(bp);
  5977. bnxt_free_mem(bp, false);
  5978. }
  5979. static int bnxt_open(struct net_device *dev)
  5980. {
  5981. struct bnxt *bp = netdev_priv(dev);
  5982. int rc;
  5983. bnxt_hwrm_if_change(bp, true);
  5984. rc = __bnxt_open_nic(bp, true, true);
  5985. if (rc)
  5986. bnxt_hwrm_if_change(bp, false);
  5987. bnxt_hwmon_open(bp);
  5988. return rc;
  5989. }
  5990. static bool bnxt_drv_busy(struct bnxt *bp)
  5991. {
  5992. return (test_bit(BNXT_STATE_IN_SP_TASK, &bp->state) ||
  5993. test_bit(BNXT_STATE_READ_STATS, &bp->state));
  5994. }
  5995. static void bnxt_get_ring_stats(struct bnxt *bp,
  5996. struct rtnl_link_stats64 *stats);
  5997. static void __bnxt_close_nic(struct bnxt *bp, bool irq_re_init,
  5998. bool link_re_init)
  5999. {
  6000. /* Close the VF-reps before closing PF */
  6001. if (BNXT_PF(bp))
  6002. bnxt_vf_reps_close(bp);
  6003. /* Change device state to avoid TX queue wake up's */
  6004. bnxt_tx_disable(bp);
  6005. clear_bit(BNXT_STATE_OPEN, &bp->state);
  6006. smp_mb__after_atomic();
  6007. while (bnxt_drv_busy(bp))
  6008. msleep(20);
  6009. /* Flush rings and and disable interrupts */
  6010. bnxt_shutdown_nic(bp, irq_re_init);
  6011. /* TODO CHIMP_FW: Link/PHY related cleanup if (link_re_init) */
  6012. bnxt_debug_dev_exit(bp);
  6013. bnxt_disable_napi(bp);
  6014. del_timer_sync(&bp->timer);
  6015. bnxt_free_skbs(bp);
  6016. /* Save ring stats before shutdown */
  6017. if (bp->bnapi && irq_re_init)
  6018. bnxt_get_ring_stats(bp, &bp->net_stats_prev);
  6019. if (irq_re_init) {
  6020. bnxt_free_irq(bp);
  6021. bnxt_del_napi(bp);
  6022. }
  6023. bnxt_free_mem(bp, irq_re_init);
  6024. }
  6025. int bnxt_close_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
  6026. {
  6027. int rc = 0;
  6028. #ifdef CONFIG_BNXT_SRIOV
  6029. if (bp->sriov_cfg) {
  6030. rc = wait_event_interruptible_timeout(bp->sriov_cfg_wait,
  6031. !bp->sriov_cfg,
  6032. BNXT_SRIOV_CFG_WAIT_TMO);
  6033. if (rc)
  6034. netdev_warn(bp->dev, "timeout waiting for SRIOV config operation to complete!\n");
  6035. }
  6036. #endif
  6037. __bnxt_close_nic(bp, irq_re_init, link_re_init);
  6038. return rc;
  6039. }
  6040. static int bnxt_close(struct net_device *dev)
  6041. {
  6042. struct bnxt *bp = netdev_priv(dev);
  6043. bnxt_hwmon_close(bp);
  6044. bnxt_close_nic(bp, true, true);
  6045. bnxt_hwrm_shutdown_link(bp);
  6046. bnxt_hwrm_if_change(bp, false);
  6047. return 0;
  6048. }
  6049. /* rtnl_lock held */
  6050. static int bnxt_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  6051. {
  6052. switch (cmd) {
  6053. case SIOCGMIIPHY:
  6054. /* fallthru */
  6055. case SIOCGMIIREG: {
  6056. if (!netif_running(dev))
  6057. return -EAGAIN;
  6058. return 0;
  6059. }
  6060. case SIOCSMIIREG:
  6061. if (!netif_running(dev))
  6062. return -EAGAIN;
  6063. return 0;
  6064. default:
  6065. /* do nothing */
  6066. break;
  6067. }
  6068. return -EOPNOTSUPP;
  6069. }
  6070. static void bnxt_get_ring_stats(struct bnxt *bp,
  6071. struct rtnl_link_stats64 *stats)
  6072. {
  6073. int i;
  6074. for (i = 0; i < bp->cp_nr_rings; i++) {
  6075. struct bnxt_napi *bnapi = bp->bnapi[i];
  6076. struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
  6077. struct ctx_hw_stats *hw_stats = cpr->hw_stats;
  6078. stats->rx_packets += le64_to_cpu(hw_stats->rx_ucast_pkts);
  6079. stats->rx_packets += le64_to_cpu(hw_stats->rx_mcast_pkts);
  6080. stats->rx_packets += le64_to_cpu(hw_stats->rx_bcast_pkts);
  6081. stats->tx_packets += le64_to_cpu(hw_stats->tx_ucast_pkts);
  6082. stats->tx_packets += le64_to_cpu(hw_stats->tx_mcast_pkts);
  6083. stats->tx_packets += le64_to_cpu(hw_stats->tx_bcast_pkts);
  6084. stats->rx_bytes += le64_to_cpu(hw_stats->rx_ucast_bytes);
  6085. stats->rx_bytes += le64_to_cpu(hw_stats->rx_mcast_bytes);
  6086. stats->rx_bytes += le64_to_cpu(hw_stats->rx_bcast_bytes);
  6087. stats->tx_bytes += le64_to_cpu(hw_stats->tx_ucast_bytes);
  6088. stats->tx_bytes += le64_to_cpu(hw_stats->tx_mcast_bytes);
  6089. stats->tx_bytes += le64_to_cpu(hw_stats->tx_bcast_bytes);
  6090. stats->rx_missed_errors +=
  6091. le64_to_cpu(hw_stats->rx_discard_pkts);
  6092. stats->multicast += le64_to_cpu(hw_stats->rx_mcast_pkts);
  6093. stats->tx_dropped += le64_to_cpu(hw_stats->tx_drop_pkts);
  6094. }
  6095. }
  6096. static void bnxt_add_prev_stats(struct bnxt *bp,
  6097. struct rtnl_link_stats64 *stats)
  6098. {
  6099. struct rtnl_link_stats64 *prev_stats = &bp->net_stats_prev;
  6100. stats->rx_packets += prev_stats->rx_packets;
  6101. stats->tx_packets += prev_stats->tx_packets;
  6102. stats->rx_bytes += prev_stats->rx_bytes;
  6103. stats->tx_bytes += prev_stats->tx_bytes;
  6104. stats->rx_missed_errors += prev_stats->rx_missed_errors;
  6105. stats->multicast += prev_stats->multicast;
  6106. stats->tx_dropped += prev_stats->tx_dropped;
  6107. }
  6108. static void
  6109. bnxt_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
  6110. {
  6111. struct bnxt *bp = netdev_priv(dev);
  6112. set_bit(BNXT_STATE_READ_STATS, &bp->state);
  6113. /* Make sure bnxt_close_nic() sees that we are reading stats before
  6114. * we check the BNXT_STATE_OPEN flag.
  6115. */
  6116. smp_mb__after_atomic();
  6117. if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
  6118. clear_bit(BNXT_STATE_READ_STATS, &bp->state);
  6119. *stats = bp->net_stats_prev;
  6120. return;
  6121. }
  6122. bnxt_get_ring_stats(bp, stats);
  6123. bnxt_add_prev_stats(bp, stats);
  6124. if (bp->flags & BNXT_FLAG_PORT_STATS) {
  6125. struct rx_port_stats *rx = bp->hw_rx_port_stats;
  6126. struct tx_port_stats *tx = bp->hw_tx_port_stats;
  6127. stats->rx_crc_errors = le64_to_cpu(rx->rx_fcs_err_frames);
  6128. stats->rx_frame_errors = le64_to_cpu(rx->rx_align_err_frames);
  6129. stats->rx_length_errors = le64_to_cpu(rx->rx_undrsz_frames) +
  6130. le64_to_cpu(rx->rx_ovrsz_frames) +
  6131. le64_to_cpu(rx->rx_runt_frames);
  6132. stats->rx_errors = le64_to_cpu(rx->rx_false_carrier_frames) +
  6133. le64_to_cpu(rx->rx_jbr_frames);
  6134. stats->collisions = le64_to_cpu(tx->tx_total_collisions);
  6135. stats->tx_fifo_errors = le64_to_cpu(tx->tx_fifo_underruns);
  6136. stats->tx_errors = le64_to_cpu(tx->tx_err);
  6137. }
  6138. clear_bit(BNXT_STATE_READ_STATS, &bp->state);
  6139. }
  6140. static bool bnxt_mc_list_updated(struct bnxt *bp, u32 *rx_mask)
  6141. {
  6142. struct net_device *dev = bp->dev;
  6143. struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
  6144. struct netdev_hw_addr *ha;
  6145. u8 *haddr;
  6146. int mc_count = 0;
  6147. bool update = false;
  6148. int off = 0;
  6149. netdev_for_each_mc_addr(ha, dev) {
  6150. if (mc_count >= BNXT_MAX_MC_ADDRS) {
  6151. *rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
  6152. vnic->mc_list_count = 0;
  6153. return false;
  6154. }
  6155. haddr = ha->addr;
  6156. if (!ether_addr_equal(haddr, vnic->mc_list + off)) {
  6157. memcpy(vnic->mc_list + off, haddr, ETH_ALEN);
  6158. update = true;
  6159. }
  6160. off += ETH_ALEN;
  6161. mc_count++;
  6162. }
  6163. if (mc_count)
  6164. *rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_MCAST;
  6165. if (mc_count != vnic->mc_list_count) {
  6166. vnic->mc_list_count = mc_count;
  6167. update = true;
  6168. }
  6169. return update;
  6170. }
  6171. static bool bnxt_uc_list_updated(struct bnxt *bp)
  6172. {
  6173. struct net_device *dev = bp->dev;
  6174. struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
  6175. struct netdev_hw_addr *ha;
  6176. int off = 0;
  6177. if (netdev_uc_count(dev) != (vnic->uc_filter_count - 1))
  6178. return true;
  6179. netdev_for_each_uc_addr(ha, dev) {
  6180. if (!ether_addr_equal(ha->addr, vnic->uc_list + off))
  6181. return true;
  6182. off += ETH_ALEN;
  6183. }
  6184. return false;
  6185. }
  6186. static void bnxt_set_rx_mode(struct net_device *dev)
  6187. {
  6188. struct bnxt *bp = netdev_priv(dev);
  6189. struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
  6190. u32 mask = vnic->rx_mask;
  6191. bool mc_update = false;
  6192. bool uc_update;
  6193. if (!netif_running(dev))
  6194. return;
  6195. mask &= ~(CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS |
  6196. CFA_L2_SET_RX_MASK_REQ_MASK_MCAST |
  6197. CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST |
  6198. CFA_L2_SET_RX_MASK_REQ_MASK_BCAST);
  6199. if ((dev->flags & IFF_PROMISC) && bnxt_promisc_ok(bp))
  6200. mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
  6201. uc_update = bnxt_uc_list_updated(bp);
  6202. if (dev->flags & IFF_BROADCAST)
  6203. mask |= CFA_L2_SET_RX_MASK_REQ_MASK_BCAST;
  6204. if (dev->flags & IFF_ALLMULTI) {
  6205. mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
  6206. vnic->mc_list_count = 0;
  6207. } else {
  6208. mc_update = bnxt_mc_list_updated(bp, &mask);
  6209. }
  6210. if (mask != vnic->rx_mask || uc_update || mc_update) {
  6211. vnic->rx_mask = mask;
  6212. set_bit(BNXT_RX_MASK_SP_EVENT, &bp->sp_event);
  6213. bnxt_queue_sp_work(bp);
  6214. }
  6215. }
  6216. static int bnxt_cfg_rx_mode(struct bnxt *bp)
  6217. {
  6218. struct net_device *dev = bp->dev;
  6219. struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
  6220. struct netdev_hw_addr *ha;
  6221. int i, off = 0, rc;
  6222. bool uc_update;
  6223. netif_addr_lock_bh(dev);
  6224. uc_update = bnxt_uc_list_updated(bp);
  6225. netif_addr_unlock_bh(dev);
  6226. if (!uc_update)
  6227. goto skip_uc;
  6228. mutex_lock(&bp->hwrm_cmd_lock);
  6229. for (i = 1; i < vnic->uc_filter_count; i++) {
  6230. struct hwrm_cfa_l2_filter_free_input req = {0};
  6231. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_L2_FILTER_FREE, -1,
  6232. -1);
  6233. req.l2_filter_id = vnic->fw_l2_filter_id[i];
  6234. rc = _hwrm_send_message(bp, &req, sizeof(req),
  6235. HWRM_CMD_TIMEOUT);
  6236. }
  6237. mutex_unlock(&bp->hwrm_cmd_lock);
  6238. vnic->uc_filter_count = 1;
  6239. netif_addr_lock_bh(dev);
  6240. if (netdev_uc_count(dev) > (BNXT_MAX_UC_ADDRS - 1)) {
  6241. vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
  6242. } else {
  6243. netdev_for_each_uc_addr(ha, dev) {
  6244. memcpy(vnic->uc_list + off, ha->addr, ETH_ALEN);
  6245. off += ETH_ALEN;
  6246. vnic->uc_filter_count++;
  6247. }
  6248. }
  6249. netif_addr_unlock_bh(dev);
  6250. for (i = 1, off = 0; i < vnic->uc_filter_count; i++, off += ETH_ALEN) {
  6251. rc = bnxt_hwrm_set_vnic_filter(bp, 0, i, vnic->uc_list + off);
  6252. if (rc) {
  6253. netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n",
  6254. rc);
  6255. vnic->uc_filter_count = i;
  6256. return rc;
  6257. }
  6258. }
  6259. skip_uc:
  6260. rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, 0);
  6261. if (rc && vnic->mc_list_count) {
  6262. netdev_info(bp->dev, "Failed setting MC filters rc: %d, turning on ALL_MCAST mode\n",
  6263. rc);
  6264. vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
  6265. vnic->mc_list_count = 0;
  6266. rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, 0);
  6267. }
  6268. if (rc)
  6269. netdev_err(bp->dev, "HWRM cfa l2 rx mask failure rc: %d\n",
  6270. rc);
  6271. return rc;
  6272. }
  6273. static bool bnxt_can_reserve_rings(struct bnxt *bp)
  6274. {
  6275. #ifdef CONFIG_BNXT_SRIOV
  6276. if (BNXT_NEW_RM(bp) && BNXT_VF(bp)) {
  6277. struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
  6278. /* No minimum rings were provisioned by the PF. Don't
  6279. * reserve rings by default when device is down.
  6280. */
  6281. if (hw_resc->min_tx_rings || hw_resc->resv_tx_rings)
  6282. return true;
  6283. if (!netif_running(bp->dev))
  6284. return false;
  6285. }
  6286. #endif
  6287. return true;
  6288. }
  6289. /* If the chip and firmware supports RFS */
  6290. static bool bnxt_rfs_supported(struct bnxt *bp)
  6291. {
  6292. if (BNXT_PF(bp) && !BNXT_CHIP_TYPE_NITRO_A0(bp))
  6293. return true;
  6294. if (bp->flags & BNXT_FLAG_NEW_RSS_CAP)
  6295. return true;
  6296. return false;
  6297. }
  6298. /* If runtime conditions support RFS */
  6299. static bool bnxt_rfs_capable(struct bnxt *bp)
  6300. {
  6301. #ifdef CONFIG_RFS_ACCEL
  6302. int vnics, max_vnics, max_rss_ctxs;
  6303. if (!(bp->flags & BNXT_FLAG_MSIX_CAP) || !bnxt_can_reserve_rings(bp))
  6304. return false;
  6305. vnics = 1 + bp->rx_nr_rings;
  6306. max_vnics = bnxt_get_max_func_vnics(bp);
  6307. max_rss_ctxs = bnxt_get_max_func_rss_ctxs(bp);
  6308. /* RSS contexts not a limiting factor */
  6309. if (bp->flags & BNXT_FLAG_NEW_RSS_CAP)
  6310. max_rss_ctxs = max_vnics;
  6311. if (vnics > max_vnics || vnics > max_rss_ctxs) {
  6312. if (bp->rx_nr_rings > 1)
  6313. netdev_warn(bp->dev,
  6314. "Not enough resources to support NTUPLE filters, enough resources for up to %d rx rings\n",
  6315. min(max_rss_ctxs - 1, max_vnics - 1));
  6316. return false;
  6317. }
  6318. if (!BNXT_NEW_RM(bp))
  6319. return true;
  6320. if (vnics == bp->hw_resc.resv_vnics)
  6321. return true;
  6322. bnxt_hwrm_reserve_rings(bp, 0, 0, 0, 0, vnics);
  6323. if (vnics <= bp->hw_resc.resv_vnics)
  6324. return true;
  6325. netdev_warn(bp->dev, "Unable to reserve resources to support NTUPLE filters.\n");
  6326. bnxt_hwrm_reserve_rings(bp, 0, 0, 0, 0, 1);
  6327. return false;
  6328. #else
  6329. return false;
  6330. #endif
  6331. }
  6332. static netdev_features_t bnxt_fix_features(struct net_device *dev,
  6333. netdev_features_t features)
  6334. {
  6335. struct bnxt *bp = netdev_priv(dev);
  6336. netdev_features_t vlan_features;
  6337. if ((features & NETIF_F_NTUPLE) && !bnxt_rfs_capable(bp))
  6338. features &= ~NETIF_F_NTUPLE;
  6339. if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
  6340. features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
  6341. if (!(features & NETIF_F_GRO))
  6342. features &= ~NETIF_F_GRO_HW;
  6343. if (features & NETIF_F_GRO_HW)
  6344. features &= ~NETIF_F_LRO;
  6345. /* Both CTAG and STAG VLAN accelaration on the RX side have to be
  6346. * turned on or off together.
  6347. */
  6348. vlan_features = features & (NETIF_F_HW_VLAN_CTAG_RX |
  6349. NETIF_F_HW_VLAN_STAG_RX);
  6350. if (vlan_features != (NETIF_F_HW_VLAN_CTAG_RX |
  6351. NETIF_F_HW_VLAN_STAG_RX)) {
  6352. if (dev->features & NETIF_F_HW_VLAN_CTAG_RX)
  6353. features &= ~(NETIF_F_HW_VLAN_CTAG_RX |
  6354. NETIF_F_HW_VLAN_STAG_RX);
  6355. else if (vlan_features)
  6356. features |= NETIF_F_HW_VLAN_CTAG_RX |
  6357. NETIF_F_HW_VLAN_STAG_RX;
  6358. }
  6359. #ifdef CONFIG_BNXT_SRIOV
  6360. if (BNXT_VF(bp)) {
  6361. if (bp->vf.vlan) {
  6362. features &= ~(NETIF_F_HW_VLAN_CTAG_RX |
  6363. NETIF_F_HW_VLAN_STAG_RX);
  6364. }
  6365. }
  6366. #endif
  6367. return features;
  6368. }
  6369. static int bnxt_set_features(struct net_device *dev, netdev_features_t features)
  6370. {
  6371. struct bnxt *bp = netdev_priv(dev);
  6372. u32 flags = bp->flags;
  6373. u32 changes;
  6374. int rc = 0;
  6375. bool re_init = false;
  6376. bool update_tpa = false;
  6377. flags &= ~BNXT_FLAG_ALL_CONFIG_FEATS;
  6378. if (features & NETIF_F_GRO_HW)
  6379. flags |= BNXT_FLAG_GRO;
  6380. else if (features & NETIF_F_LRO)
  6381. flags |= BNXT_FLAG_LRO;
  6382. if (bp->flags & BNXT_FLAG_NO_AGG_RINGS)
  6383. flags &= ~BNXT_FLAG_TPA;
  6384. if (features & NETIF_F_HW_VLAN_CTAG_RX)
  6385. flags |= BNXT_FLAG_STRIP_VLAN;
  6386. if (features & NETIF_F_NTUPLE)
  6387. flags |= BNXT_FLAG_RFS;
  6388. changes = flags ^ bp->flags;
  6389. if (changes & BNXT_FLAG_TPA) {
  6390. update_tpa = true;
  6391. if ((bp->flags & BNXT_FLAG_TPA) == 0 ||
  6392. (flags & BNXT_FLAG_TPA) == 0)
  6393. re_init = true;
  6394. }
  6395. if (changes & ~BNXT_FLAG_TPA)
  6396. re_init = true;
  6397. if (flags != bp->flags) {
  6398. u32 old_flags = bp->flags;
  6399. bp->flags = flags;
  6400. if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
  6401. if (update_tpa)
  6402. bnxt_set_ring_params(bp);
  6403. return rc;
  6404. }
  6405. if (re_init) {
  6406. bnxt_close_nic(bp, false, false);
  6407. if (update_tpa)
  6408. bnxt_set_ring_params(bp);
  6409. return bnxt_open_nic(bp, false, false);
  6410. }
  6411. if (update_tpa) {
  6412. rc = bnxt_set_tpa(bp,
  6413. (flags & BNXT_FLAG_TPA) ?
  6414. true : false);
  6415. if (rc)
  6416. bp->flags = old_flags;
  6417. }
  6418. }
  6419. return rc;
  6420. }
  6421. static void bnxt_dump_tx_sw_state(struct bnxt_napi *bnapi)
  6422. {
  6423. struct bnxt_tx_ring_info *txr = bnapi->tx_ring;
  6424. int i = bnapi->index;
  6425. if (!txr)
  6426. return;
  6427. netdev_info(bnapi->bp->dev, "[%d]: tx{fw_ring: %d prod: %x cons: %x}\n",
  6428. i, txr->tx_ring_struct.fw_ring_id, txr->tx_prod,
  6429. txr->tx_cons);
  6430. }
  6431. static void bnxt_dump_rx_sw_state(struct bnxt_napi *bnapi)
  6432. {
  6433. struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
  6434. int i = bnapi->index;
  6435. if (!rxr)
  6436. return;
  6437. netdev_info(bnapi->bp->dev, "[%d]: rx{fw_ring: %d prod: %x} rx_agg{fw_ring: %d agg_prod: %x sw_agg_prod: %x}\n",
  6438. i, rxr->rx_ring_struct.fw_ring_id, rxr->rx_prod,
  6439. rxr->rx_agg_ring_struct.fw_ring_id, rxr->rx_agg_prod,
  6440. rxr->rx_sw_agg_prod);
  6441. }
  6442. static void bnxt_dump_cp_sw_state(struct bnxt_napi *bnapi)
  6443. {
  6444. struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
  6445. int i = bnapi->index;
  6446. netdev_info(bnapi->bp->dev, "[%d]: cp{fw_ring: %d raw_cons: %x}\n",
  6447. i, cpr->cp_ring_struct.fw_ring_id, cpr->cp_raw_cons);
  6448. }
  6449. static void bnxt_dbg_dump_states(struct bnxt *bp)
  6450. {
  6451. int i;
  6452. struct bnxt_napi *bnapi;
  6453. for (i = 0; i < bp->cp_nr_rings; i++) {
  6454. bnapi = bp->bnapi[i];
  6455. if (netif_msg_drv(bp)) {
  6456. bnxt_dump_tx_sw_state(bnapi);
  6457. bnxt_dump_rx_sw_state(bnapi);
  6458. bnxt_dump_cp_sw_state(bnapi);
  6459. }
  6460. }
  6461. }
  6462. static void bnxt_reset_task(struct bnxt *bp, bool silent)
  6463. {
  6464. if (!silent)
  6465. bnxt_dbg_dump_states(bp);
  6466. if (netif_running(bp->dev)) {
  6467. int rc;
  6468. if (!silent)
  6469. bnxt_ulp_stop(bp);
  6470. bnxt_close_nic(bp, false, false);
  6471. rc = bnxt_open_nic(bp, false, false);
  6472. if (!silent && !rc)
  6473. bnxt_ulp_start(bp);
  6474. }
  6475. }
  6476. static void bnxt_tx_timeout(struct net_device *dev)
  6477. {
  6478. struct bnxt *bp = netdev_priv(dev);
  6479. netdev_err(bp->dev, "TX timeout detected, starting reset task!\n");
  6480. set_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event);
  6481. bnxt_queue_sp_work(bp);
  6482. }
  6483. static void bnxt_timer(struct timer_list *t)
  6484. {
  6485. struct bnxt *bp = from_timer(bp, t, timer);
  6486. struct net_device *dev = bp->dev;
  6487. if (!netif_running(dev))
  6488. return;
  6489. if (atomic_read(&bp->intr_sem) != 0)
  6490. goto bnxt_restart_timer;
  6491. if (bp->link_info.link_up && (bp->flags & BNXT_FLAG_PORT_STATS) &&
  6492. bp->stats_coal_ticks) {
  6493. set_bit(BNXT_PERIODIC_STATS_SP_EVENT, &bp->sp_event);
  6494. bnxt_queue_sp_work(bp);
  6495. }
  6496. if (bnxt_tc_flower_enabled(bp)) {
  6497. set_bit(BNXT_FLOW_STATS_SP_EVENT, &bp->sp_event);
  6498. bnxt_queue_sp_work(bp);
  6499. }
  6500. if (bp->link_info.phy_retry) {
  6501. if (time_after(jiffies, bp->link_info.phy_retry_expires)) {
  6502. bp->link_info.phy_retry = 0;
  6503. netdev_warn(bp->dev, "failed to update phy settings after maximum retries.\n");
  6504. } else {
  6505. set_bit(BNXT_UPDATE_PHY_SP_EVENT, &bp->sp_event);
  6506. bnxt_queue_sp_work(bp);
  6507. }
  6508. }
  6509. bnxt_restart_timer:
  6510. mod_timer(&bp->timer, jiffies + bp->current_interval);
  6511. }
  6512. static void bnxt_rtnl_lock_sp(struct bnxt *bp)
  6513. {
  6514. /* We are called from bnxt_sp_task which has BNXT_STATE_IN_SP_TASK
  6515. * set. If the device is being closed, bnxt_close() may be holding
  6516. * rtnl() and waiting for BNXT_STATE_IN_SP_TASK to clear. So we
  6517. * must clear BNXT_STATE_IN_SP_TASK before holding rtnl().
  6518. */
  6519. clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
  6520. rtnl_lock();
  6521. }
  6522. static void bnxt_rtnl_unlock_sp(struct bnxt *bp)
  6523. {
  6524. set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
  6525. rtnl_unlock();
  6526. }
  6527. /* Only called from bnxt_sp_task() */
  6528. static void bnxt_reset(struct bnxt *bp, bool silent)
  6529. {
  6530. bnxt_rtnl_lock_sp(bp);
  6531. if (test_bit(BNXT_STATE_OPEN, &bp->state))
  6532. bnxt_reset_task(bp, silent);
  6533. bnxt_rtnl_unlock_sp(bp);
  6534. }
  6535. static void bnxt_cfg_ntp_filters(struct bnxt *);
  6536. static void bnxt_sp_task(struct work_struct *work)
  6537. {
  6538. struct bnxt *bp = container_of(work, struct bnxt, sp_task);
  6539. set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
  6540. smp_mb__after_atomic();
  6541. if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
  6542. clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
  6543. return;
  6544. }
  6545. if (test_and_clear_bit(BNXT_RX_MASK_SP_EVENT, &bp->sp_event))
  6546. bnxt_cfg_rx_mode(bp);
  6547. if (test_and_clear_bit(BNXT_RX_NTP_FLTR_SP_EVENT, &bp->sp_event))
  6548. bnxt_cfg_ntp_filters(bp);
  6549. if (test_and_clear_bit(BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT, &bp->sp_event))
  6550. bnxt_hwrm_exec_fwd_req(bp);
  6551. if (test_and_clear_bit(BNXT_VXLAN_ADD_PORT_SP_EVENT, &bp->sp_event)) {
  6552. bnxt_hwrm_tunnel_dst_port_alloc(
  6553. bp, bp->vxlan_port,
  6554. TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN);
  6555. }
  6556. if (test_and_clear_bit(BNXT_VXLAN_DEL_PORT_SP_EVENT, &bp->sp_event)) {
  6557. bnxt_hwrm_tunnel_dst_port_free(
  6558. bp, TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN);
  6559. }
  6560. if (test_and_clear_bit(BNXT_GENEVE_ADD_PORT_SP_EVENT, &bp->sp_event)) {
  6561. bnxt_hwrm_tunnel_dst_port_alloc(
  6562. bp, bp->nge_port,
  6563. TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE);
  6564. }
  6565. if (test_and_clear_bit(BNXT_GENEVE_DEL_PORT_SP_EVENT, &bp->sp_event)) {
  6566. bnxt_hwrm_tunnel_dst_port_free(
  6567. bp, TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE);
  6568. }
  6569. if (test_and_clear_bit(BNXT_PERIODIC_STATS_SP_EVENT, &bp->sp_event)) {
  6570. bnxt_hwrm_port_qstats(bp);
  6571. bnxt_hwrm_port_qstats_ext(bp);
  6572. }
  6573. if (test_and_clear_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event)) {
  6574. int rc;
  6575. mutex_lock(&bp->link_lock);
  6576. if (test_and_clear_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT,
  6577. &bp->sp_event))
  6578. bnxt_hwrm_phy_qcaps(bp);
  6579. rc = bnxt_update_link(bp, true);
  6580. mutex_unlock(&bp->link_lock);
  6581. if (rc)
  6582. netdev_err(bp->dev, "SP task can't update link (rc: %x)\n",
  6583. rc);
  6584. }
  6585. if (test_and_clear_bit(BNXT_UPDATE_PHY_SP_EVENT, &bp->sp_event)) {
  6586. int rc;
  6587. mutex_lock(&bp->link_lock);
  6588. rc = bnxt_update_phy_setting(bp);
  6589. mutex_unlock(&bp->link_lock);
  6590. if (rc) {
  6591. netdev_warn(bp->dev, "update phy settings retry failed\n");
  6592. } else {
  6593. bp->link_info.phy_retry = false;
  6594. netdev_info(bp->dev, "update phy settings retry succeeded\n");
  6595. }
  6596. }
  6597. if (test_and_clear_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event)) {
  6598. mutex_lock(&bp->link_lock);
  6599. bnxt_get_port_module_status(bp);
  6600. mutex_unlock(&bp->link_lock);
  6601. }
  6602. if (test_and_clear_bit(BNXT_FLOW_STATS_SP_EVENT, &bp->sp_event))
  6603. bnxt_tc_flow_stats_work(bp);
  6604. /* These functions below will clear BNXT_STATE_IN_SP_TASK. They
  6605. * must be the last functions to be called before exiting.
  6606. */
  6607. if (test_and_clear_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event))
  6608. bnxt_reset(bp, false);
  6609. if (test_and_clear_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event))
  6610. bnxt_reset(bp, true);
  6611. smp_mb__before_atomic();
  6612. clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
  6613. }
  6614. /* Under rtnl_lock */
  6615. int bnxt_check_rings(struct bnxt *bp, int tx, int rx, bool sh, int tcs,
  6616. int tx_xdp)
  6617. {
  6618. int max_rx, max_tx, tx_sets = 1;
  6619. int tx_rings_needed;
  6620. int rx_rings = rx;
  6621. int cp, vnics, rc;
  6622. if (tcs)
  6623. tx_sets = tcs;
  6624. rc = bnxt_get_max_rings(bp, &max_rx, &max_tx, sh);
  6625. if (rc)
  6626. return rc;
  6627. if (max_rx < rx)
  6628. return -ENOMEM;
  6629. tx_rings_needed = tx * tx_sets + tx_xdp;
  6630. if (max_tx < tx_rings_needed)
  6631. return -ENOMEM;
  6632. vnics = 1;
  6633. if (bp->flags & BNXT_FLAG_RFS)
  6634. vnics += rx_rings;
  6635. if (bp->flags & BNXT_FLAG_AGG_RINGS)
  6636. rx_rings <<= 1;
  6637. cp = sh ? max_t(int, tx_rings_needed, rx) : tx_rings_needed + rx;
  6638. if (BNXT_NEW_RM(bp))
  6639. cp += bnxt_get_ulp_msix_num(bp);
  6640. return bnxt_hwrm_check_rings(bp, tx_rings_needed, rx_rings, rx, cp,
  6641. vnics);
  6642. }
  6643. static void bnxt_unmap_bars(struct bnxt *bp, struct pci_dev *pdev)
  6644. {
  6645. if (bp->bar2) {
  6646. pci_iounmap(pdev, bp->bar2);
  6647. bp->bar2 = NULL;
  6648. }
  6649. if (bp->bar1) {
  6650. pci_iounmap(pdev, bp->bar1);
  6651. bp->bar1 = NULL;
  6652. }
  6653. if (bp->bar0) {
  6654. pci_iounmap(pdev, bp->bar0);
  6655. bp->bar0 = NULL;
  6656. }
  6657. }
  6658. static void bnxt_cleanup_pci(struct bnxt *bp)
  6659. {
  6660. bnxt_unmap_bars(bp, bp->pdev);
  6661. pci_release_regions(bp->pdev);
  6662. pci_disable_device(bp->pdev);
  6663. }
  6664. static void bnxt_init_dflt_coal(struct bnxt *bp)
  6665. {
  6666. struct bnxt_coal *coal;
  6667. /* Tick values in micro seconds.
  6668. * 1 coal_buf x bufs_per_record = 1 completion record.
  6669. */
  6670. coal = &bp->rx_coal;
  6671. coal->coal_ticks = 14;
  6672. coal->coal_bufs = 30;
  6673. coal->coal_ticks_irq = 1;
  6674. coal->coal_bufs_irq = 2;
  6675. coal->idle_thresh = 50;
  6676. coal->bufs_per_record = 2;
  6677. coal->budget = 64; /* NAPI budget */
  6678. coal = &bp->tx_coal;
  6679. coal->coal_ticks = 28;
  6680. coal->coal_bufs = 30;
  6681. coal->coal_ticks_irq = 2;
  6682. coal->coal_bufs_irq = 2;
  6683. coal->bufs_per_record = 1;
  6684. bp->stats_coal_ticks = BNXT_DEF_STATS_COAL_TICKS;
  6685. }
  6686. static int bnxt_init_board(struct pci_dev *pdev, struct net_device *dev)
  6687. {
  6688. int rc;
  6689. struct bnxt *bp = netdev_priv(dev);
  6690. SET_NETDEV_DEV(dev, &pdev->dev);
  6691. /* enable device (incl. PCI PM wakeup), and bus-mastering */
  6692. rc = pci_enable_device(pdev);
  6693. if (rc) {
  6694. dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
  6695. goto init_err;
  6696. }
  6697. if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
  6698. dev_err(&pdev->dev,
  6699. "Cannot find PCI device base address, aborting\n");
  6700. rc = -ENODEV;
  6701. goto init_err_disable;
  6702. }
  6703. rc = pci_request_regions(pdev, DRV_MODULE_NAME);
  6704. if (rc) {
  6705. dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
  6706. goto init_err_disable;
  6707. }
  6708. if (dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)) != 0 &&
  6709. dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)) != 0) {
  6710. dev_err(&pdev->dev, "System does not support DMA, aborting\n");
  6711. rc = -EIO;
  6712. goto init_err_release;
  6713. }
  6714. pci_set_master(pdev);
  6715. bp->dev = dev;
  6716. bp->pdev = pdev;
  6717. bp->bar0 = pci_ioremap_bar(pdev, 0);
  6718. if (!bp->bar0) {
  6719. dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
  6720. rc = -ENOMEM;
  6721. goto init_err_release;
  6722. }
  6723. bp->bar1 = pci_ioremap_bar(pdev, 2);
  6724. if (!bp->bar1) {
  6725. dev_err(&pdev->dev, "Cannot map doorbell registers, aborting\n");
  6726. rc = -ENOMEM;
  6727. goto init_err_release;
  6728. }
  6729. bp->bar2 = pci_ioremap_bar(pdev, 4);
  6730. if (!bp->bar2) {
  6731. dev_err(&pdev->dev, "Cannot map bar4 registers, aborting\n");
  6732. rc = -ENOMEM;
  6733. goto init_err_release;
  6734. }
  6735. pci_enable_pcie_error_reporting(pdev);
  6736. INIT_WORK(&bp->sp_task, bnxt_sp_task);
  6737. spin_lock_init(&bp->ntp_fltr_lock);
  6738. bp->rx_ring_size = BNXT_DEFAULT_RX_RING_SIZE;
  6739. bp->tx_ring_size = BNXT_DEFAULT_TX_RING_SIZE;
  6740. bnxt_init_dflt_coal(bp);
  6741. timer_setup(&bp->timer, bnxt_timer, 0);
  6742. bp->current_interval = BNXT_TIMER_INTERVAL;
  6743. clear_bit(BNXT_STATE_OPEN, &bp->state);
  6744. return 0;
  6745. init_err_release:
  6746. bnxt_unmap_bars(bp, pdev);
  6747. pci_release_regions(pdev);
  6748. init_err_disable:
  6749. pci_disable_device(pdev);
  6750. init_err:
  6751. return rc;
  6752. }
  6753. /* rtnl_lock held */
  6754. static int bnxt_change_mac_addr(struct net_device *dev, void *p)
  6755. {
  6756. struct sockaddr *addr = p;
  6757. struct bnxt *bp = netdev_priv(dev);
  6758. int rc = 0;
  6759. if (!is_valid_ether_addr(addr->sa_data))
  6760. return -EADDRNOTAVAIL;
  6761. if (ether_addr_equal(addr->sa_data, dev->dev_addr))
  6762. return 0;
  6763. rc = bnxt_approve_mac(bp, addr->sa_data, true);
  6764. if (rc)
  6765. return rc;
  6766. memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
  6767. if (netif_running(dev)) {
  6768. bnxt_close_nic(bp, false, false);
  6769. rc = bnxt_open_nic(bp, false, false);
  6770. }
  6771. return rc;
  6772. }
  6773. /* rtnl_lock held */
  6774. static int bnxt_change_mtu(struct net_device *dev, int new_mtu)
  6775. {
  6776. struct bnxt *bp = netdev_priv(dev);
  6777. if (netif_running(dev))
  6778. bnxt_close_nic(bp, true, false);
  6779. dev->mtu = new_mtu;
  6780. bnxt_set_ring_params(bp);
  6781. if (netif_running(dev))
  6782. return bnxt_open_nic(bp, true, false);
  6783. return 0;
  6784. }
  6785. int bnxt_setup_mq_tc(struct net_device *dev, u8 tc)
  6786. {
  6787. struct bnxt *bp = netdev_priv(dev);
  6788. bool sh = false;
  6789. int rc;
  6790. if (tc > bp->max_tc) {
  6791. netdev_err(dev, "Too many traffic classes requested: %d. Max supported is %d.\n",
  6792. tc, bp->max_tc);
  6793. return -EINVAL;
  6794. }
  6795. if (netdev_get_num_tc(dev) == tc)
  6796. return 0;
  6797. if (bp->flags & BNXT_FLAG_SHARED_RINGS)
  6798. sh = true;
  6799. rc = bnxt_check_rings(bp, bp->tx_nr_rings_per_tc, bp->rx_nr_rings,
  6800. sh, tc, bp->tx_nr_rings_xdp);
  6801. if (rc)
  6802. return rc;
  6803. /* Needs to close the device and do hw resource re-allocations */
  6804. if (netif_running(bp->dev))
  6805. bnxt_close_nic(bp, true, false);
  6806. if (tc) {
  6807. bp->tx_nr_rings = bp->tx_nr_rings_per_tc * tc;
  6808. netdev_set_num_tc(dev, tc);
  6809. } else {
  6810. bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
  6811. netdev_reset_tc(dev);
  6812. }
  6813. bp->tx_nr_rings += bp->tx_nr_rings_xdp;
  6814. bp->cp_nr_rings = sh ? max_t(int, bp->tx_nr_rings, bp->rx_nr_rings) :
  6815. bp->tx_nr_rings + bp->rx_nr_rings;
  6816. bp->num_stat_ctxs = bp->cp_nr_rings;
  6817. if (netif_running(bp->dev))
  6818. return bnxt_open_nic(bp, true, false);
  6819. return 0;
  6820. }
  6821. static int bnxt_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
  6822. void *cb_priv)
  6823. {
  6824. struct bnxt *bp = cb_priv;
  6825. if (!bnxt_tc_flower_enabled(bp) ||
  6826. !tc_cls_can_offload_and_chain0(bp->dev, type_data))
  6827. return -EOPNOTSUPP;
  6828. switch (type) {
  6829. case TC_SETUP_CLSFLOWER:
  6830. return bnxt_tc_setup_flower(bp, bp->pf.fw_fid, type_data);
  6831. default:
  6832. return -EOPNOTSUPP;
  6833. }
  6834. }
  6835. static int bnxt_setup_tc_block(struct net_device *dev,
  6836. struct tc_block_offload *f)
  6837. {
  6838. struct bnxt *bp = netdev_priv(dev);
  6839. if (f->binder_type != TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
  6840. return -EOPNOTSUPP;
  6841. switch (f->command) {
  6842. case TC_BLOCK_BIND:
  6843. return tcf_block_cb_register(f->block, bnxt_setup_tc_block_cb,
  6844. bp, bp, f->extack);
  6845. case TC_BLOCK_UNBIND:
  6846. tcf_block_cb_unregister(f->block, bnxt_setup_tc_block_cb, bp);
  6847. return 0;
  6848. default:
  6849. return -EOPNOTSUPP;
  6850. }
  6851. }
  6852. static int bnxt_setup_tc(struct net_device *dev, enum tc_setup_type type,
  6853. void *type_data)
  6854. {
  6855. switch (type) {
  6856. case TC_SETUP_BLOCK:
  6857. return bnxt_setup_tc_block(dev, type_data);
  6858. case TC_SETUP_QDISC_MQPRIO: {
  6859. struct tc_mqprio_qopt *mqprio = type_data;
  6860. mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS;
  6861. return bnxt_setup_mq_tc(dev, mqprio->num_tc);
  6862. }
  6863. default:
  6864. return -EOPNOTSUPP;
  6865. }
  6866. }
  6867. #ifdef CONFIG_RFS_ACCEL
  6868. static bool bnxt_fltr_match(struct bnxt_ntuple_filter *f1,
  6869. struct bnxt_ntuple_filter *f2)
  6870. {
  6871. struct flow_keys *keys1 = &f1->fkeys;
  6872. struct flow_keys *keys2 = &f2->fkeys;
  6873. if (keys1->basic.n_proto != keys2->basic.n_proto ||
  6874. keys1->basic.ip_proto != keys2->basic.ip_proto)
  6875. return false;
  6876. if (keys1->basic.n_proto == htons(ETH_P_IP)) {
  6877. if (keys1->addrs.v4addrs.src != keys2->addrs.v4addrs.src ||
  6878. keys1->addrs.v4addrs.dst != keys2->addrs.v4addrs.dst)
  6879. return false;
  6880. } else {
  6881. if (memcmp(&keys1->addrs.v6addrs.src, &keys2->addrs.v6addrs.src,
  6882. sizeof(keys1->addrs.v6addrs.src)) ||
  6883. memcmp(&keys1->addrs.v6addrs.dst, &keys2->addrs.v6addrs.dst,
  6884. sizeof(keys1->addrs.v6addrs.dst)))
  6885. return false;
  6886. }
  6887. if (keys1->ports.ports == keys2->ports.ports &&
  6888. keys1->control.flags == keys2->control.flags &&
  6889. ether_addr_equal(f1->src_mac_addr, f2->src_mac_addr) &&
  6890. ether_addr_equal(f1->dst_mac_addr, f2->dst_mac_addr))
  6891. return true;
  6892. return false;
  6893. }
  6894. static int bnxt_rx_flow_steer(struct net_device *dev, const struct sk_buff *skb,
  6895. u16 rxq_index, u32 flow_id)
  6896. {
  6897. struct bnxt *bp = netdev_priv(dev);
  6898. struct bnxt_ntuple_filter *fltr, *new_fltr;
  6899. struct flow_keys *fkeys;
  6900. struct ethhdr *eth = (struct ethhdr *)skb_mac_header(skb);
  6901. int rc = 0, idx, bit_id, l2_idx = 0;
  6902. struct hlist_head *head;
  6903. if (!ether_addr_equal(dev->dev_addr, eth->h_dest)) {
  6904. struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
  6905. int off = 0, j;
  6906. netif_addr_lock_bh(dev);
  6907. for (j = 0; j < vnic->uc_filter_count; j++, off += ETH_ALEN) {
  6908. if (ether_addr_equal(eth->h_dest,
  6909. vnic->uc_list + off)) {
  6910. l2_idx = j + 1;
  6911. break;
  6912. }
  6913. }
  6914. netif_addr_unlock_bh(dev);
  6915. if (!l2_idx)
  6916. return -EINVAL;
  6917. }
  6918. new_fltr = kzalloc(sizeof(*new_fltr), GFP_ATOMIC);
  6919. if (!new_fltr)
  6920. return -ENOMEM;
  6921. fkeys = &new_fltr->fkeys;
  6922. if (!skb_flow_dissect_flow_keys(skb, fkeys, 0)) {
  6923. rc = -EPROTONOSUPPORT;
  6924. goto err_free;
  6925. }
  6926. if ((fkeys->basic.n_proto != htons(ETH_P_IP) &&
  6927. fkeys->basic.n_proto != htons(ETH_P_IPV6)) ||
  6928. ((fkeys->basic.ip_proto != IPPROTO_TCP) &&
  6929. (fkeys->basic.ip_proto != IPPROTO_UDP))) {
  6930. rc = -EPROTONOSUPPORT;
  6931. goto err_free;
  6932. }
  6933. if (fkeys->basic.n_proto == htons(ETH_P_IPV6) &&
  6934. bp->hwrm_spec_code < 0x10601) {
  6935. rc = -EPROTONOSUPPORT;
  6936. goto err_free;
  6937. }
  6938. if ((fkeys->control.flags & FLOW_DIS_ENCAPSULATION) &&
  6939. bp->hwrm_spec_code < 0x10601) {
  6940. rc = -EPROTONOSUPPORT;
  6941. goto err_free;
  6942. }
  6943. memcpy(new_fltr->dst_mac_addr, eth->h_dest, ETH_ALEN);
  6944. memcpy(new_fltr->src_mac_addr, eth->h_source, ETH_ALEN);
  6945. idx = skb_get_hash_raw(skb) & BNXT_NTP_FLTR_HASH_MASK;
  6946. head = &bp->ntp_fltr_hash_tbl[idx];
  6947. rcu_read_lock();
  6948. hlist_for_each_entry_rcu(fltr, head, hash) {
  6949. if (bnxt_fltr_match(fltr, new_fltr)) {
  6950. rcu_read_unlock();
  6951. rc = 0;
  6952. goto err_free;
  6953. }
  6954. }
  6955. rcu_read_unlock();
  6956. spin_lock_bh(&bp->ntp_fltr_lock);
  6957. bit_id = bitmap_find_free_region(bp->ntp_fltr_bmap,
  6958. BNXT_NTP_FLTR_MAX_FLTR, 0);
  6959. if (bit_id < 0) {
  6960. spin_unlock_bh(&bp->ntp_fltr_lock);
  6961. rc = -ENOMEM;
  6962. goto err_free;
  6963. }
  6964. new_fltr->sw_id = (u16)bit_id;
  6965. new_fltr->flow_id = flow_id;
  6966. new_fltr->l2_fltr_idx = l2_idx;
  6967. new_fltr->rxq = rxq_index;
  6968. hlist_add_head_rcu(&new_fltr->hash, head);
  6969. bp->ntp_fltr_count++;
  6970. spin_unlock_bh(&bp->ntp_fltr_lock);
  6971. set_bit(BNXT_RX_NTP_FLTR_SP_EVENT, &bp->sp_event);
  6972. bnxt_queue_sp_work(bp);
  6973. return new_fltr->sw_id;
  6974. err_free:
  6975. kfree(new_fltr);
  6976. return rc;
  6977. }
  6978. static void bnxt_cfg_ntp_filters(struct bnxt *bp)
  6979. {
  6980. int i;
  6981. for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
  6982. struct hlist_head *head;
  6983. struct hlist_node *tmp;
  6984. struct bnxt_ntuple_filter *fltr;
  6985. int rc;
  6986. head = &bp->ntp_fltr_hash_tbl[i];
  6987. hlist_for_each_entry_safe(fltr, tmp, head, hash) {
  6988. bool del = false;
  6989. if (test_bit(BNXT_FLTR_VALID, &fltr->state)) {
  6990. if (rps_may_expire_flow(bp->dev, fltr->rxq,
  6991. fltr->flow_id,
  6992. fltr->sw_id)) {
  6993. bnxt_hwrm_cfa_ntuple_filter_free(bp,
  6994. fltr);
  6995. del = true;
  6996. }
  6997. } else {
  6998. rc = bnxt_hwrm_cfa_ntuple_filter_alloc(bp,
  6999. fltr);
  7000. if (rc)
  7001. del = true;
  7002. else
  7003. set_bit(BNXT_FLTR_VALID, &fltr->state);
  7004. }
  7005. if (del) {
  7006. spin_lock_bh(&bp->ntp_fltr_lock);
  7007. hlist_del_rcu(&fltr->hash);
  7008. bp->ntp_fltr_count--;
  7009. spin_unlock_bh(&bp->ntp_fltr_lock);
  7010. synchronize_rcu();
  7011. clear_bit(fltr->sw_id, bp->ntp_fltr_bmap);
  7012. kfree(fltr);
  7013. }
  7014. }
  7015. }
  7016. if (test_and_clear_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event))
  7017. netdev_info(bp->dev, "Receive PF driver unload event!");
  7018. }
  7019. #else
  7020. static void bnxt_cfg_ntp_filters(struct bnxt *bp)
  7021. {
  7022. }
  7023. #endif /* CONFIG_RFS_ACCEL */
  7024. static void bnxt_udp_tunnel_add(struct net_device *dev,
  7025. struct udp_tunnel_info *ti)
  7026. {
  7027. struct bnxt *bp = netdev_priv(dev);
  7028. if (ti->sa_family != AF_INET6 && ti->sa_family != AF_INET)
  7029. return;
  7030. if (!netif_running(dev))
  7031. return;
  7032. switch (ti->type) {
  7033. case UDP_TUNNEL_TYPE_VXLAN:
  7034. if (bp->vxlan_port_cnt && bp->vxlan_port != ti->port)
  7035. return;
  7036. bp->vxlan_port_cnt++;
  7037. if (bp->vxlan_port_cnt == 1) {
  7038. bp->vxlan_port = ti->port;
  7039. set_bit(BNXT_VXLAN_ADD_PORT_SP_EVENT, &bp->sp_event);
  7040. bnxt_queue_sp_work(bp);
  7041. }
  7042. break;
  7043. case UDP_TUNNEL_TYPE_GENEVE:
  7044. if (bp->nge_port_cnt && bp->nge_port != ti->port)
  7045. return;
  7046. bp->nge_port_cnt++;
  7047. if (bp->nge_port_cnt == 1) {
  7048. bp->nge_port = ti->port;
  7049. set_bit(BNXT_GENEVE_ADD_PORT_SP_EVENT, &bp->sp_event);
  7050. }
  7051. break;
  7052. default:
  7053. return;
  7054. }
  7055. bnxt_queue_sp_work(bp);
  7056. }
  7057. static void bnxt_udp_tunnel_del(struct net_device *dev,
  7058. struct udp_tunnel_info *ti)
  7059. {
  7060. struct bnxt *bp = netdev_priv(dev);
  7061. if (ti->sa_family != AF_INET6 && ti->sa_family != AF_INET)
  7062. return;
  7063. if (!netif_running(dev))
  7064. return;
  7065. switch (ti->type) {
  7066. case UDP_TUNNEL_TYPE_VXLAN:
  7067. if (!bp->vxlan_port_cnt || bp->vxlan_port != ti->port)
  7068. return;
  7069. bp->vxlan_port_cnt--;
  7070. if (bp->vxlan_port_cnt != 0)
  7071. return;
  7072. set_bit(BNXT_VXLAN_DEL_PORT_SP_EVENT, &bp->sp_event);
  7073. break;
  7074. case UDP_TUNNEL_TYPE_GENEVE:
  7075. if (!bp->nge_port_cnt || bp->nge_port != ti->port)
  7076. return;
  7077. bp->nge_port_cnt--;
  7078. if (bp->nge_port_cnt != 0)
  7079. return;
  7080. set_bit(BNXT_GENEVE_DEL_PORT_SP_EVENT, &bp->sp_event);
  7081. break;
  7082. default:
  7083. return;
  7084. }
  7085. bnxt_queue_sp_work(bp);
  7086. }
  7087. static int bnxt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
  7088. struct net_device *dev, u32 filter_mask,
  7089. int nlflags)
  7090. {
  7091. struct bnxt *bp = netdev_priv(dev);
  7092. return ndo_dflt_bridge_getlink(skb, pid, seq, dev, bp->br_mode, 0, 0,
  7093. nlflags, filter_mask, NULL);
  7094. }
  7095. static int bnxt_bridge_setlink(struct net_device *dev, struct nlmsghdr *nlh,
  7096. u16 flags)
  7097. {
  7098. struct bnxt *bp = netdev_priv(dev);
  7099. struct nlattr *attr, *br_spec;
  7100. int rem, rc = 0;
  7101. if (bp->hwrm_spec_code < 0x10708 || !BNXT_SINGLE_PF(bp))
  7102. return -EOPNOTSUPP;
  7103. br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
  7104. if (!br_spec)
  7105. return -EINVAL;
  7106. nla_for_each_nested(attr, br_spec, rem) {
  7107. u16 mode;
  7108. if (nla_type(attr) != IFLA_BRIDGE_MODE)
  7109. continue;
  7110. if (nla_len(attr) < sizeof(mode))
  7111. return -EINVAL;
  7112. mode = nla_get_u16(attr);
  7113. if (mode == bp->br_mode)
  7114. break;
  7115. rc = bnxt_hwrm_set_br_mode(bp, mode);
  7116. if (!rc)
  7117. bp->br_mode = mode;
  7118. break;
  7119. }
  7120. return rc;
  7121. }
  7122. static int bnxt_get_phys_port_name(struct net_device *dev, char *buf,
  7123. size_t len)
  7124. {
  7125. struct bnxt *bp = netdev_priv(dev);
  7126. int rc;
  7127. /* The PF and it's VF-reps only support the switchdev framework */
  7128. if (!BNXT_PF(bp))
  7129. return -EOPNOTSUPP;
  7130. rc = snprintf(buf, len, "p%d", bp->pf.port_id);
  7131. if (rc >= len)
  7132. return -EOPNOTSUPP;
  7133. return 0;
  7134. }
  7135. int bnxt_port_attr_get(struct bnxt *bp, struct switchdev_attr *attr)
  7136. {
  7137. if (bp->eswitch_mode != DEVLINK_ESWITCH_MODE_SWITCHDEV)
  7138. return -EOPNOTSUPP;
  7139. /* The PF and it's VF-reps only support the switchdev framework */
  7140. if (!BNXT_PF(bp))
  7141. return -EOPNOTSUPP;
  7142. switch (attr->id) {
  7143. case SWITCHDEV_ATTR_ID_PORT_PARENT_ID:
  7144. attr->u.ppid.id_len = sizeof(bp->switch_id);
  7145. memcpy(attr->u.ppid.id, bp->switch_id, attr->u.ppid.id_len);
  7146. break;
  7147. default:
  7148. return -EOPNOTSUPP;
  7149. }
  7150. return 0;
  7151. }
  7152. static int bnxt_swdev_port_attr_get(struct net_device *dev,
  7153. struct switchdev_attr *attr)
  7154. {
  7155. return bnxt_port_attr_get(netdev_priv(dev), attr);
  7156. }
  7157. static const struct switchdev_ops bnxt_switchdev_ops = {
  7158. .switchdev_port_attr_get = bnxt_swdev_port_attr_get
  7159. };
  7160. static const struct net_device_ops bnxt_netdev_ops = {
  7161. .ndo_open = bnxt_open,
  7162. .ndo_start_xmit = bnxt_start_xmit,
  7163. .ndo_stop = bnxt_close,
  7164. .ndo_get_stats64 = bnxt_get_stats64,
  7165. .ndo_set_rx_mode = bnxt_set_rx_mode,
  7166. .ndo_do_ioctl = bnxt_ioctl,
  7167. .ndo_validate_addr = eth_validate_addr,
  7168. .ndo_set_mac_address = bnxt_change_mac_addr,
  7169. .ndo_change_mtu = bnxt_change_mtu,
  7170. .ndo_fix_features = bnxt_fix_features,
  7171. .ndo_set_features = bnxt_set_features,
  7172. .ndo_tx_timeout = bnxt_tx_timeout,
  7173. #ifdef CONFIG_BNXT_SRIOV
  7174. .ndo_get_vf_config = bnxt_get_vf_config,
  7175. .ndo_set_vf_mac = bnxt_set_vf_mac,
  7176. .ndo_set_vf_vlan = bnxt_set_vf_vlan,
  7177. .ndo_set_vf_rate = bnxt_set_vf_bw,
  7178. .ndo_set_vf_link_state = bnxt_set_vf_link_state,
  7179. .ndo_set_vf_spoofchk = bnxt_set_vf_spoofchk,
  7180. .ndo_set_vf_trust = bnxt_set_vf_trust,
  7181. #endif
  7182. .ndo_setup_tc = bnxt_setup_tc,
  7183. #ifdef CONFIG_RFS_ACCEL
  7184. .ndo_rx_flow_steer = bnxt_rx_flow_steer,
  7185. #endif
  7186. .ndo_udp_tunnel_add = bnxt_udp_tunnel_add,
  7187. .ndo_udp_tunnel_del = bnxt_udp_tunnel_del,
  7188. .ndo_bpf = bnxt_xdp,
  7189. .ndo_bridge_getlink = bnxt_bridge_getlink,
  7190. .ndo_bridge_setlink = bnxt_bridge_setlink,
  7191. .ndo_get_phys_port_name = bnxt_get_phys_port_name
  7192. };
  7193. static void bnxt_remove_one(struct pci_dev *pdev)
  7194. {
  7195. struct net_device *dev = pci_get_drvdata(pdev);
  7196. struct bnxt *bp = netdev_priv(dev);
  7197. if (BNXT_PF(bp)) {
  7198. bnxt_sriov_disable(bp);
  7199. bnxt_dl_unregister(bp);
  7200. }
  7201. pci_disable_pcie_error_reporting(pdev);
  7202. unregister_netdev(dev);
  7203. bnxt_shutdown_tc(bp);
  7204. bnxt_cancel_sp_work(bp);
  7205. bp->sp_event = 0;
  7206. bnxt_clear_int_mode(bp);
  7207. bnxt_hwrm_func_drv_unrgtr(bp);
  7208. bnxt_free_hwrm_resources(bp);
  7209. bnxt_free_hwrm_short_cmd_req(bp);
  7210. bnxt_ethtool_free(bp);
  7211. bnxt_dcb_free(bp);
  7212. kfree(bp->edev);
  7213. bp->edev = NULL;
  7214. bnxt_cleanup_pci(bp);
  7215. free_netdev(dev);
  7216. }
  7217. static int bnxt_probe_phy(struct bnxt *bp)
  7218. {
  7219. int rc = 0;
  7220. struct bnxt_link_info *link_info = &bp->link_info;
  7221. rc = bnxt_hwrm_phy_qcaps(bp);
  7222. if (rc) {
  7223. netdev_err(bp->dev, "Probe phy can't get phy capabilities (rc: %x)\n",
  7224. rc);
  7225. return rc;
  7226. }
  7227. mutex_init(&bp->link_lock);
  7228. rc = bnxt_update_link(bp, false);
  7229. if (rc) {
  7230. netdev_err(bp->dev, "Probe phy can't update link (rc: %x)\n",
  7231. rc);
  7232. return rc;
  7233. }
  7234. /* Older firmware does not have supported_auto_speeds, so assume
  7235. * that all supported speeds can be autonegotiated.
  7236. */
  7237. if (link_info->auto_link_speeds && !link_info->support_auto_speeds)
  7238. link_info->support_auto_speeds = link_info->support_speeds;
  7239. /*initialize the ethool setting copy with NVM settings */
  7240. if (BNXT_AUTO_MODE(link_info->auto_mode)) {
  7241. link_info->autoneg = BNXT_AUTONEG_SPEED;
  7242. if (bp->hwrm_spec_code >= 0x10201) {
  7243. if (link_info->auto_pause_setting &
  7244. PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE)
  7245. link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
  7246. } else {
  7247. link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
  7248. }
  7249. link_info->advertising = link_info->auto_link_speeds;
  7250. } else {
  7251. link_info->req_link_speed = link_info->force_link_speed;
  7252. link_info->req_duplex = link_info->duplex_setting;
  7253. }
  7254. if (link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL)
  7255. link_info->req_flow_ctrl =
  7256. link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH;
  7257. else
  7258. link_info->req_flow_ctrl = link_info->force_pause_setting;
  7259. return rc;
  7260. }
  7261. static int bnxt_get_max_irq(struct pci_dev *pdev)
  7262. {
  7263. u16 ctrl;
  7264. if (!pdev->msix_cap)
  7265. return 1;
  7266. pci_read_config_word(pdev, pdev->msix_cap + PCI_MSIX_FLAGS, &ctrl);
  7267. return (ctrl & PCI_MSIX_FLAGS_QSIZE) + 1;
  7268. }
  7269. static void _bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx,
  7270. int *max_cp)
  7271. {
  7272. struct bnxt_hw_resc *hw_resc = &bp->hw_resc;
  7273. int max_ring_grps = 0;
  7274. *max_tx = hw_resc->max_tx_rings;
  7275. *max_rx = hw_resc->max_rx_rings;
  7276. *max_cp = min_t(int, bnxt_get_max_func_cp_rings_for_en(bp),
  7277. hw_resc->max_irqs - bnxt_get_ulp_msix_num(bp));
  7278. *max_cp = min_t(int, *max_cp, hw_resc->max_stat_ctxs);
  7279. max_ring_grps = hw_resc->max_hw_ring_grps;
  7280. if (BNXT_CHIP_TYPE_NITRO_A0(bp) && BNXT_PF(bp)) {
  7281. *max_cp -= 1;
  7282. *max_rx -= 2;
  7283. }
  7284. if (bp->flags & BNXT_FLAG_AGG_RINGS)
  7285. *max_rx >>= 1;
  7286. *max_rx = min_t(int, *max_rx, max_ring_grps);
  7287. }
  7288. int bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx, bool shared)
  7289. {
  7290. int rx, tx, cp;
  7291. _bnxt_get_max_rings(bp, &rx, &tx, &cp);
  7292. *max_rx = rx;
  7293. *max_tx = tx;
  7294. if (!rx || !tx || !cp)
  7295. return -ENOMEM;
  7296. return bnxt_trim_rings(bp, max_rx, max_tx, cp, shared);
  7297. }
  7298. static int bnxt_get_dflt_rings(struct bnxt *bp, int *max_rx, int *max_tx,
  7299. bool shared)
  7300. {
  7301. int rc;
  7302. rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared);
  7303. if (rc && (bp->flags & BNXT_FLAG_AGG_RINGS)) {
  7304. /* Not enough rings, try disabling agg rings. */
  7305. bp->flags &= ~BNXT_FLAG_AGG_RINGS;
  7306. rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared);
  7307. if (rc) {
  7308. /* set BNXT_FLAG_AGG_RINGS back for consistency */
  7309. bp->flags |= BNXT_FLAG_AGG_RINGS;
  7310. return rc;
  7311. }
  7312. bp->flags |= BNXT_FLAG_NO_AGG_RINGS;
  7313. bp->dev->hw_features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
  7314. bp->dev->features &= ~(NETIF_F_LRO | NETIF_F_GRO_HW);
  7315. bnxt_set_ring_params(bp);
  7316. }
  7317. if (bp->flags & BNXT_FLAG_ROCE_CAP) {
  7318. int max_cp, max_stat, max_irq;
  7319. /* Reserve minimum resources for RoCE */
  7320. max_cp = bnxt_get_max_func_cp_rings(bp);
  7321. max_stat = bnxt_get_max_func_stat_ctxs(bp);
  7322. max_irq = bnxt_get_max_func_irqs(bp);
  7323. if (max_cp <= BNXT_MIN_ROCE_CP_RINGS ||
  7324. max_irq <= BNXT_MIN_ROCE_CP_RINGS ||
  7325. max_stat <= BNXT_MIN_ROCE_STAT_CTXS)
  7326. return 0;
  7327. max_cp -= BNXT_MIN_ROCE_CP_RINGS;
  7328. max_irq -= BNXT_MIN_ROCE_CP_RINGS;
  7329. max_stat -= BNXT_MIN_ROCE_STAT_CTXS;
  7330. max_cp = min_t(int, max_cp, max_irq);
  7331. max_cp = min_t(int, max_cp, max_stat);
  7332. rc = bnxt_trim_rings(bp, max_rx, max_tx, max_cp, shared);
  7333. if (rc)
  7334. rc = 0;
  7335. }
  7336. return rc;
  7337. }
  7338. /* In initial default shared ring setting, each shared ring must have a
  7339. * RX/TX ring pair.
  7340. */
  7341. static void bnxt_trim_dflt_sh_rings(struct bnxt *bp)
  7342. {
  7343. bp->cp_nr_rings = min_t(int, bp->tx_nr_rings_per_tc, bp->rx_nr_rings);
  7344. bp->rx_nr_rings = bp->cp_nr_rings;
  7345. bp->tx_nr_rings_per_tc = bp->cp_nr_rings;
  7346. bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
  7347. }
  7348. static int bnxt_set_dflt_rings(struct bnxt *bp, bool sh)
  7349. {
  7350. int dflt_rings, max_rx_rings, max_tx_rings, rc;
  7351. if (!bnxt_can_reserve_rings(bp))
  7352. return 0;
  7353. if (sh)
  7354. bp->flags |= BNXT_FLAG_SHARED_RINGS;
  7355. dflt_rings = netif_get_num_default_rss_queues();
  7356. /* Reduce default rings on multi-port cards so that total default
  7357. * rings do not exceed CPU count.
  7358. */
  7359. if (bp->port_count > 1) {
  7360. int max_rings =
  7361. max_t(int, num_online_cpus() / bp->port_count, 1);
  7362. dflt_rings = min_t(int, dflt_rings, max_rings);
  7363. }
  7364. rc = bnxt_get_dflt_rings(bp, &max_rx_rings, &max_tx_rings, sh);
  7365. if (rc)
  7366. return rc;
  7367. bp->rx_nr_rings = min_t(int, dflt_rings, max_rx_rings);
  7368. bp->tx_nr_rings_per_tc = min_t(int, dflt_rings, max_tx_rings);
  7369. if (sh)
  7370. bnxt_trim_dflt_sh_rings(bp);
  7371. else
  7372. bp->cp_nr_rings = bp->tx_nr_rings_per_tc + bp->rx_nr_rings;
  7373. bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
  7374. rc = __bnxt_reserve_rings(bp);
  7375. if (rc)
  7376. netdev_warn(bp->dev, "Unable to reserve tx rings\n");
  7377. bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
  7378. if (sh)
  7379. bnxt_trim_dflt_sh_rings(bp);
  7380. /* Rings may have been trimmed, re-reserve the trimmed rings. */
  7381. if (bnxt_need_reserve_rings(bp)) {
  7382. rc = __bnxt_reserve_rings(bp);
  7383. if (rc)
  7384. netdev_warn(bp->dev, "2nd rings reservation failed.\n");
  7385. bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
  7386. }
  7387. bp->num_stat_ctxs = bp->cp_nr_rings;
  7388. if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
  7389. bp->rx_nr_rings++;
  7390. bp->cp_nr_rings++;
  7391. }
  7392. if (rc) {
  7393. bp->tx_nr_rings = 0;
  7394. bp->rx_nr_rings = 0;
  7395. }
  7396. return rc;
  7397. }
  7398. static int bnxt_init_dflt_ring_mode(struct bnxt *bp)
  7399. {
  7400. int rc;
  7401. if (bp->tx_nr_rings)
  7402. return 0;
  7403. bnxt_ulp_irq_stop(bp);
  7404. bnxt_clear_int_mode(bp);
  7405. rc = bnxt_set_dflt_rings(bp, true);
  7406. if (rc) {
  7407. netdev_err(bp->dev, "Not enough rings available.\n");
  7408. goto init_dflt_ring_err;
  7409. }
  7410. rc = bnxt_init_int_mode(bp);
  7411. if (rc)
  7412. goto init_dflt_ring_err;
  7413. bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
  7414. if (bnxt_rfs_supported(bp) && bnxt_rfs_capable(bp)) {
  7415. bp->flags |= BNXT_FLAG_RFS;
  7416. bp->dev->features |= NETIF_F_NTUPLE;
  7417. }
  7418. init_dflt_ring_err:
  7419. bnxt_ulp_irq_restart(bp, rc);
  7420. return rc;
  7421. }
  7422. int bnxt_restore_pf_fw_resources(struct bnxt *bp)
  7423. {
  7424. int rc;
  7425. ASSERT_RTNL();
  7426. bnxt_hwrm_func_qcaps(bp);
  7427. if (netif_running(bp->dev))
  7428. __bnxt_close_nic(bp, true, false);
  7429. bnxt_ulp_irq_stop(bp);
  7430. bnxt_clear_int_mode(bp);
  7431. rc = bnxt_init_int_mode(bp);
  7432. bnxt_ulp_irq_restart(bp, rc);
  7433. if (netif_running(bp->dev)) {
  7434. if (rc)
  7435. dev_close(bp->dev);
  7436. else
  7437. rc = bnxt_open_nic(bp, true, false);
  7438. }
  7439. return rc;
  7440. }
  7441. static int bnxt_init_mac_addr(struct bnxt *bp)
  7442. {
  7443. int rc = 0;
  7444. if (BNXT_PF(bp)) {
  7445. memcpy(bp->dev->dev_addr, bp->pf.mac_addr, ETH_ALEN);
  7446. } else {
  7447. #ifdef CONFIG_BNXT_SRIOV
  7448. struct bnxt_vf_info *vf = &bp->vf;
  7449. bool strict_approval = true;
  7450. if (is_valid_ether_addr(vf->mac_addr)) {
  7451. /* overwrite netdev dev_addr with admin VF MAC */
  7452. memcpy(bp->dev->dev_addr, vf->mac_addr, ETH_ALEN);
  7453. /* Older PF driver or firmware may not approve this
  7454. * correctly.
  7455. */
  7456. strict_approval = false;
  7457. } else {
  7458. eth_hw_addr_random(bp->dev);
  7459. }
  7460. rc = bnxt_approve_mac(bp, bp->dev->dev_addr, strict_approval);
  7461. #endif
  7462. }
  7463. return rc;
  7464. }
  7465. static int bnxt_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
  7466. {
  7467. static int version_printed;
  7468. struct net_device *dev;
  7469. struct bnxt *bp;
  7470. int rc, max_irqs;
  7471. if (pci_is_bridge(pdev))
  7472. return -ENODEV;
  7473. if (version_printed++ == 0)
  7474. pr_info("%s", version);
  7475. max_irqs = bnxt_get_max_irq(pdev);
  7476. dev = alloc_etherdev_mq(sizeof(*bp), max_irqs);
  7477. if (!dev)
  7478. return -ENOMEM;
  7479. bp = netdev_priv(dev);
  7480. if (bnxt_vf_pciid(ent->driver_data))
  7481. bp->flags |= BNXT_FLAG_VF;
  7482. if (pdev->msix_cap)
  7483. bp->flags |= BNXT_FLAG_MSIX_CAP;
  7484. rc = bnxt_init_board(pdev, dev);
  7485. if (rc < 0)
  7486. goto init_err_free;
  7487. dev->netdev_ops = &bnxt_netdev_ops;
  7488. dev->watchdog_timeo = BNXT_TX_TIMEOUT;
  7489. dev->ethtool_ops = &bnxt_ethtool_ops;
  7490. SWITCHDEV_SET_OPS(dev, &bnxt_switchdev_ops);
  7491. pci_set_drvdata(pdev, dev);
  7492. rc = bnxt_alloc_hwrm_resources(bp);
  7493. if (rc)
  7494. goto init_err_pci_clean;
  7495. mutex_init(&bp->hwrm_cmd_lock);
  7496. rc = bnxt_hwrm_ver_get(bp);
  7497. if (rc)
  7498. goto init_err_pci_clean;
  7499. if (bp->fw_cap & BNXT_FW_CAP_SHORT_CMD) {
  7500. rc = bnxt_alloc_hwrm_short_cmd_req(bp);
  7501. if (rc)
  7502. goto init_err_pci_clean;
  7503. }
  7504. rc = bnxt_hwrm_func_reset(bp);
  7505. if (rc)
  7506. goto init_err_pci_clean;
  7507. bnxt_hwrm_fw_set_time(bp);
  7508. dev->hw_features = NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
  7509. NETIF_F_TSO | NETIF_F_TSO6 |
  7510. NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
  7511. NETIF_F_GSO_IPXIP4 |
  7512. NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
  7513. NETIF_F_GSO_PARTIAL | NETIF_F_RXHASH |
  7514. NETIF_F_RXCSUM | NETIF_F_GRO;
  7515. if (!BNXT_CHIP_TYPE_NITRO_A0(bp))
  7516. dev->hw_features |= NETIF_F_LRO;
  7517. dev->hw_enc_features =
  7518. NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
  7519. NETIF_F_TSO | NETIF_F_TSO6 |
  7520. NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
  7521. NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
  7522. NETIF_F_GSO_IPXIP4 | NETIF_F_GSO_PARTIAL;
  7523. dev->gso_partial_features = NETIF_F_GSO_UDP_TUNNEL_CSUM |
  7524. NETIF_F_GSO_GRE_CSUM;
  7525. dev->vlan_features = dev->hw_features | NETIF_F_HIGHDMA;
  7526. dev->hw_features |= NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_CTAG_TX |
  7527. NETIF_F_HW_VLAN_STAG_RX | NETIF_F_HW_VLAN_STAG_TX;
  7528. if (!BNXT_CHIP_TYPE_NITRO_A0(bp))
  7529. dev->hw_features |= NETIF_F_GRO_HW;
  7530. dev->features |= dev->hw_features | NETIF_F_HIGHDMA;
  7531. if (dev->features & NETIF_F_GRO_HW)
  7532. dev->features &= ~NETIF_F_LRO;
  7533. dev->priv_flags |= IFF_UNICAST_FLT;
  7534. #ifdef CONFIG_BNXT_SRIOV
  7535. init_waitqueue_head(&bp->sriov_cfg_wait);
  7536. mutex_init(&bp->sriov_lock);
  7537. #endif
  7538. bp->gro_func = bnxt_gro_func_5730x;
  7539. if (BNXT_CHIP_P4_PLUS(bp))
  7540. bp->gro_func = bnxt_gro_func_5731x;
  7541. else
  7542. bp->flags |= BNXT_FLAG_DOUBLE_DB;
  7543. rc = bnxt_hwrm_func_drv_rgtr(bp);
  7544. if (rc)
  7545. goto init_err_pci_clean;
  7546. rc = bnxt_hwrm_func_rgtr_async_events(bp, NULL, 0);
  7547. if (rc)
  7548. goto init_err_pci_clean;
  7549. bp->ulp_probe = bnxt_ulp_probe;
  7550. /* Get the MAX capabilities for this function */
  7551. rc = bnxt_hwrm_func_qcaps(bp);
  7552. if (rc) {
  7553. netdev_err(bp->dev, "hwrm query capability failure rc: %x\n",
  7554. rc);
  7555. rc = -1;
  7556. goto init_err_pci_clean;
  7557. }
  7558. rc = bnxt_init_mac_addr(bp);
  7559. if (rc) {
  7560. dev_err(&pdev->dev, "Unable to initialize mac address.\n");
  7561. rc = -EADDRNOTAVAIL;
  7562. goto init_err_pci_clean;
  7563. }
  7564. rc = bnxt_hwrm_queue_qportcfg(bp);
  7565. if (rc) {
  7566. netdev_err(bp->dev, "hwrm query qportcfg failure rc: %x\n",
  7567. rc);
  7568. rc = -1;
  7569. goto init_err_pci_clean;
  7570. }
  7571. bnxt_hwrm_func_qcfg(bp);
  7572. bnxt_hwrm_port_led_qcaps(bp);
  7573. bnxt_ethtool_init(bp);
  7574. bnxt_dcb_init(bp);
  7575. /* MTU range: 60 - FW defined max */
  7576. dev->min_mtu = ETH_ZLEN;
  7577. dev->max_mtu = bp->max_mtu;
  7578. rc = bnxt_probe_phy(bp);
  7579. if (rc)
  7580. goto init_err_pci_clean;
  7581. bnxt_set_rx_skb_mode(bp, false);
  7582. bnxt_set_tpa_flags(bp);
  7583. bnxt_set_ring_params(bp);
  7584. bnxt_set_max_func_irqs(bp, max_irqs);
  7585. rc = bnxt_set_dflt_rings(bp, true);
  7586. if (rc) {
  7587. netdev_err(bp->dev, "Not enough rings available.\n");
  7588. rc = -ENOMEM;
  7589. goto init_err_pci_clean;
  7590. }
  7591. /* Default RSS hash cfg. */
  7592. bp->rss_hash_cfg = VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4 |
  7593. VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4 |
  7594. VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6 |
  7595. VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6;
  7596. if (BNXT_CHIP_P4_PLUS(bp) && bp->hwrm_spec_code >= 0x10501) {
  7597. bp->flags |= BNXT_FLAG_UDP_RSS_CAP;
  7598. bp->rss_hash_cfg |= VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4 |
  7599. VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6;
  7600. }
  7601. bnxt_hwrm_vnic_qcaps(bp);
  7602. if (bnxt_rfs_supported(bp)) {
  7603. dev->hw_features |= NETIF_F_NTUPLE;
  7604. if (bnxt_rfs_capable(bp)) {
  7605. bp->flags |= BNXT_FLAG_RFS;
  7606. dev->features |= NETIF_F_NTUPLE;
  7607. }
  7608. }
  7609. if (dev->hw_features & NETIF_F_HW_VLAN_CTAG_RX)
  7610. bp->flags |= BNXT_FLAG_STRIP_VLAN;
  7611. rc = bnxt_init_int_mode(bp);
  7612. if (rc)
  7613. goto init_err_pci_clean;
  7614. /* No TC has been set yet and rings may have been trimmed due to
  7615. * limited MSIX, so we re-initialize the TX rings per TC.
  7616. */
  7617. bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
  7618. bnxt_get_wol_settings(bp);
  7619. if (bp->flags & BNXT_FLAG_WOL_CAP)
  7620. device_set_wakeup_enable(&pdev->dev, bp->wol);
  7621. else
  7622. device_set_wakeup_capable(&pdev->dev, false);
  7623. bnxt_hwrm_set_cache_line_size(bp, cache_line_size());
  7624. if (BNXT_PF(bp)) {
  7625. if (!bnxt_pf_wq) {
  7626. bnxt_pf_wq =
  7627. create_singlethread_workqueue("bnxt_pf_wq");
  7628. if (!bnxt_pf_wq) {
  7629. dev_err(&pdev->dev, "Unable to create workqueue.\n");
  7630. rc = -ENOMEM;
  7631. goto init_err_pci_clean;
  7632. }
  7633. }
  7634. bnxt_init_tc(bp);
  7635. }
  7636. rc = register_netdev(dev);
  7637. if (rc)
  7638. goto init_err_cleanup_tc;
  7639. if (BNXT_PF(bp))
  7640. bnxt_dl_register(bp);
  7641. netdev_info(dev, "%s found at mem %lx, node addr %pM\n",
  7642. board_info[ent->driver_data].name,
  7643. (long)pci_resource_start(pdev, 0), dev->dev_addr);
  7644. pcie_print_link_status(pdev);
  7645. pci_save_state(pdev);
  7646. return 0;
  7647. init_err_cleanup_tc:
  7648. bnxt_shutdown_tc(bp);
  7649. bnxt_clear_int_mode(bp);
  7650. init_err_pci_clean:
  7651. bnxt_free_hwrm_short_cmd_req(bp);
  7652. bnxt_free_hwrm_resources(bp);
  7653. bnxt_cleanup_pci(bp);
  7654. init_err_free:
  7655. free_netdev(dev);
  7656. return rc;
  7657. }
  7658. static void bnxt_shutdown(struct pci_dev *pdev)
  7659. {
  7660. struct net_device *dev = pci_get_drvdata(pdev);
  7661. struct bnxt *bp;
  7662. if (!dev)
  7663. return;
  7664. rtnl_lock();
  7665. bp = netdev_priv(dev);
  7666. if (!bp)
  7667. goto shutdown_exit;
  7668. if (netif_running(dev))
  7669. dev_close(dev);
  7670. bnxt_ulp_shutdown(bp);
  7671. if (system_state == SYSTEM_POWER_OFF) {
  7672. bnxt_clear_int_mode(bp);
  7673. pci_wake_from_d3(pdev, bp->wol);
  7674. pci_set_power_state(pdev, PCI_D3hot);
  7675. }
  7676. shutdown_exit:
  7677. rtnl_unlock();
  7678. }
  7679. #ifdef CONFIG_PM_SLEEP
  7680. static int bnxt_suspend(struct device *device)
  7681. {
  7682. struct pci_dev *pdev = to_pci_dev(device);
  7683. struct net_device *dev = pci_get_drvdata(pdev);
  7684. struct bnxt *bp = netdev_priv(dev);
  7685. int rc = 0;
  7686. rtnl_lock();
  7687. if (netif_running(dev)) {
  7688. netif_device_detach(dev);
  7689. rc = bnxt_close(dev);
  7690. }
  7691. bnxt_hwrm_func_drv_unrgtr(bp);
  7692. rtnl_unlock();
  7693. return rc;
  7694. }
  7695. static int bnxt_resume(struct device *device)
  7696. {
  7697. struct pci_dev *pdev = to_pci_dev(device);
  7698. struct net_device *dev = pci_get_drvdata(pdev);
  7699. struct bnxt *bp = netdev_priv(dev);
  7700. int rc = 0;
  7701. rtnl_lock();
  7702. if (bnxt_hwrm_ver_get(bp) || bnxt_hwrm_func_drv_rgtr(bp)) {
  7703. rc = -ENODEV;
  7704. goto resume_exit;
  7705. }
  7706. rc = bnxt_hwrm_func_reset(bp);
  7707. if (rc) {
  7708. rc = -EBUSY;
  7709. goto resume_exit;
  7710. }
  7711. bnxt_get_wol_settings(bp);
  7712. if (netif_running(dev)) {
  7713. rc = bnxt_open(dev);
  7714. if (!rc)
  7715. netif_device_attach(dev);
  7716. }
  7717. resume_exit:
  7718. rtnl_unlock();
  7719. return rc;
  7720. }
  7721. static SIMPLE_DEV_PM_OPS(bnxt_pm_ops, bnxt_suspend, bnxt_resume);
  7722. #define BNXT_PM_OPS (&bnxt_pm_ops)
  7723. #else
  7724. #define BNXT_PM_OPS NULL
  7725. #endif /* CONFIG_PM_SLEEP */
  7726. /**
  7727. * bnxt_io_error_detected - called when PCI error is detected
  7728. * @pdev: Pointer to PCI device
  7729. * @state: The current pci connection state
  7730. *
  7731. * This function is called after a PCI bus error affecting
  7732. * this device has been detected.
  7733. */
  7734. static pci_ers_result_t bnxt_io_error_detected(struct pci_dev *pdev,
  7735. pci_channel_state_t state)
  7736. {
  7737. struct net_device *netdev = pci_get_drvdata(pdev);
  7738. struct bnxt *bp = netdev_priv(netdev);
  7739. netdev_info(netdev, "PCI I/O error detected\n");
  7740. rtnl_lock();
  7741. netif_device_detach(netdev);
  7742. bnxt_ulp_stop(bp);
  7743. if (state == pci_channel_io_perm_failure) {
  7744. rtnl_unlock();
  7745. return PCI_ERS_RESULT_DISCONNECT;
  7746. }
  7747. if (netif_running(netdev))
  7748. bnxt_close(netdev);
  7749. pci_disable_device(pdev);
  7750. rtnl_unlock();
  7751. /* Request a slot slot reset. */
  7752. return PCI_ERS_RESULT_NEED_RESET;
  7753. }
  7754. /**
  7755. * bnxt_io_slot_reset - called after the pci bus has been reset.
  7756. * @pdev: Pointer to PCI device
  7757. *
  7758. * Restart the card from scratch, as if from a cold-boot.
  7759. * At this point, the card has exprienced a hard reset,
  7760. * followed by fixups by BIOS, and has its config space
  7761. * set up identically to what it was at cold boot.
  7762. */
  7763. static pci_ers_result_t bnxt_io_slot_reset(struct pci_dev *pdev)
  7764. {
  7765. struct net_device *netdev = pci_get_drvdata(pdev);
  7766. struct bnxt *bp = netdev_priv(netdev);
  7767. int err = 0;
  7768. pci_ers_result_t result = PCI_ERS_RESULT_DISCONNECT;
  7769. netdev_info(bp->dev, "PCI Slot Reset\n");
  7770. rtnl_lock();
  7771. if (pci_enable_device(pdev)) {
  7772. dev_err(&pdev->dev,
  7773. "Cannot re-enable PCI device after reset.\n");
  7774. } else {
  7775. pci_set_master(pdev);
  7776. pci_restore_state(pdev);
  7777. pci_save_state(pdev);
  7778. err = bnxt_hwrm_func_reset(bp);
  7779. if (!err && netif_running(netdev))
  7780. err = bnxt_open(netdev);
  7781. if (!err) {
  7782. result = PCI_ERS_RESULT_RECOVERED;
  7783. bnxt_ulp_start(bp);
  7784. }
  7785. }
  7786. if (result != PCI_ERS_RESULT_RECOVERED) {
  7787. if (netif_running(netdev))
  7788. dev_close(netdev);
  7789. pci_disable_device(pdev);
  7790. }
  7791. rtnl_unlock();
  7792. err = pci_cleanup_aer_uncorrect_error_status(pdev);
  7793. if (err) {
  7794. dev_err(&pdev->dev,
  7795. "pci_cleanup_aer_uncorrect_error_status failed 0x%0x\n",
  7796. err); /* non-fatal, continue */
  7797. }
  7798. return result;
  7799. }
  7800. /**
  7801. * bnxt_io_resume - called when traffic can start flowing again.
  7802. * @pdev: Pointer to PCI device
  7803. *
  7804. * This callback is called when the error recovery driver tells
  7805. * us that its OK to resume normal operation.
  7806. */
  7807. static void bnxt_io_resume(struct pci_dev *pdev)
  7808. {
  7809. struct net_device *netdev = pci_get_drvdata(pdev);
  7810. rtnl_lock();
  7811. netif_device_attach(netdev);
  7812. rtnl_unlock();
  7813. }
  7814. static const struct pci_error_handlers bnxt_err_handler = {
  7815. .error_detected = bnxt_io_error_detected,
  7816. .slot_reset = bnxt_io_slot_reset,
  7817. .resume = bnxt_io_resume
  7818. };
  7819. static struct pci_driver bnxt_pci_driver = {
  7820. .name = DRV_MODULE_NAME,
  7821. .id_table = bnxt_pci_tbl,
  7822. .probe = bnxt_init_one,
  7823. .remove = bnxt_remove_one,
  7824. .shutdown = bnxt_shutdown,
  7825. .driver.pm = BNXT_PM_OPS,
  7826. .err_handler = &bnxt_err_handler,
  7827. #if defined(CONFIG_BNXT_SRIOV)
  7828. .sriov_configure = bnxt_sriov_configure,
  7829. #endif
  7830. };
  7831. static int __init bnxt_init(void)
  7832. {
  7833. bnxt_debug_init();
  7834. return pci_register_driver(&bnxt_pci_driver);
  7835. }
  7836. static void __exit bnxt_exit(void)
  7837. {
  7838. pci_unregister_driver(&bnxt_pci_driver);
  7839. if (bnxt_pf_wq)
  7840. destroy_workqueue(bnxt_pf_wq);
  7841. bnxt_debug_exit();
  7842. }
  7843. module_init(bnxt_init);
  7844. module_exit(bnxt_exit);