bnxt.c 184 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285
  1. /* Broadcom NetXtreme-C/E network driver.
  2. *
  3. * Copyright (c) 2014-2016 Broadcom Corporation
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation.
  8. */
  9. #include <linux/module.h>
  10. #include <linux/stringify.h>
  11. #include <linux/kernel.h>
  12. #include <linux/timer.h>
  13. #include <linux/errno.h>
  14. #include <linux/ioport.h>
  15. #include <linux/slab.h>
  16. #include <linux/vmalloc.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/pci.h>
  19. #include <linux/netdevice.h>
  20. #include <linux/etherdevice.h>
  21. #include <linux/skbuff.h>
  22. #include <linux/dma-mapping.h>
  23. #include <linux/bitops.h>
  24. #include <linux/io.h>
  25. #include <linux/irq.h>
  26. #include <linux/delay.h>
  27. #include <asm/byteorder.h>
  28. #include <asm/page.h>
  29. #include <linux/time.h>
  30. #include <linux/mii.h>
  31. #include <linux/if.h>
  32. #include <linux/if_vlan.h>
  33. #include <linux/rtc.h>
  34. #include <net/ip.h>
  35. #include <net/tcp.h>
  36. #include <net/udp.h>
  37. #include <net/checksum.h>
  38. #include <net/ip6_checksum.h>
  39. #include <net/udp_tunnel.h>
  40. #ifdef CONFIG_NET_RX_BUSY_POLL
  41. #include <net/busy_poll.h>
  42. #endif
  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 "bnxt_hsi.h"
  51. #include "bnxt.h"
  52. #include "bnxt_ulp.h"
  53. #include "bnxt_sriov.h"
  54. #include "bnxt_ethtool.h"
  55. #include "bnxt_dcb.h"
  56. #define BNXT_TX_TIMEOUT (5 * HZ)
  57. static const char version[] =
  58. "Broadcom NetXtreme-C/E driver " DRV_MODULE_NAME " v" DRV_MODULE_VERSION "\n";
  59. MODULE_LICENSE("GPL");
  60. MODULE_DESCRIPTION("Broadcom BCM573xx network driver");
  61. MODULE_VERSION(DRV_MODULE_VERSION);
  62. #define BNXT_RX_OFFSET (NET_SKB_PAD + NET_IP_ALIGN)
  63. #define BNXT_RX_DMA_OFFSET NET_SKB_PAD
  64. #define BNXT_RX_COPY_THRESH 256
  65. #define BNXT_TX_PUSH_THRESH 164
  66. enum board_idx {
  67. BCM57301,
  68. BCM57302,
  69. BCM57304,
  70. BCM57417_NPAR,
  71. BCM58700,
  72. BCM57311,
  73. BCM57312,
  74. BCM57402,
  75. BCM57404,
  76. BCM57406,
  77. BCM57402_NPAR,
  78. BCM57407,
  79. BCM57412,
  80. BCM57414,
  81. BCM57416,
  82. BCM57417,
  83. BCM57412_NPAR,
  84. BCM57314,
  85. BCM57417_SFP,
  86. BCM57416_SFP,
  87. BCM57404_NPAR,
  88. BCM57406_NPAR,
  89. BCM57407_SFP,
  90. BCM57407_NPAR,
  91. BCM57414_NPAR,
  92. BCM57416_NPAR,
  93. NETXTREME_E_VF,
  94. NETXTREME_C_VF,
  95. };
  96. /* indexed by enum above */
  97. static const struct {
  98. char *name;
  99. } board_info[] = {
  100. { "Broadcom BCM57301 NetXtreme-C 10Gb Ethernet" },
  101. { "Broadcom BCM57302 NetXtreme-C 10Gb/25Gb Ethernet" },
  102. { "Broadcom BCM57304 NetXtreme-C 10Gb/25Gb/40Gb/50Gb Ethernet" },
  103. { "Broadcom BCM57417 NetXtreme-E Ethernet Partition" },
  104. { "Broadcom BCM58700 Nitro 1Gb/2.5Gb/10Gb Ethernet" },
  105. { "Broadcom BCM57311 NetXtreme-C 10Gb Ethernet" },
  106. { "Broadcom BCM57312 NetXtreme-C 10Gb/25Gb Ethernet" },
  107. { "Broadcom BCM57402 NetXtreme-E 10Gb Ethernet" },
  108. { "Broadcom BCM57404 NetXtreme-E 10Gb/25Gb Ethernet" },
  109. { "Broadcom BCM57406 NetXtreme-E 10GBase-T Ethernet" },
  110. { "Broadcom BCM57402 NetXtreme-E Ethernet Partition" },
  111. { "Broadcom BCM57407 NetXtreme-E 10GBase-T Ethernet" },
  112. { "Broadcom BCM57412 NetXtreme-E 10Gb Ethernet" },
  113. { "Broadcom BCM57414 NetXtreme-E 10Gb/25Gb Ethernet" },
  114. { "Broadcom BCM57416 NetXtreme-E 10GBase-T Ethernet" },
  115. { "Broadcom BCM57417 NetXtreme-E 10GBase-T Ethernet" },
  116. { "Broadcom BCM57412 NetXtreme-E Ethernet Partition" },
  117. { "Broadcom BCM57314 NetXtreme-C 10Gb/25Gb/40Gb/50Gb Ethernet" },
  118. { "Broadcom BCM57417 NetXtreme-E 10Gb/25Gb Ethernet" },
  119. { "Broadcom BCM57416 NetXtreme-E 10Gb Ethernet" },
  120. { "Broadcom BCM57404 NetXtreme-E Ethernet Partition" },
  121. { "Broadcom BCM57406 NetXtreme-E Ethernet Partition" },
  122. { "Broadcom BCM57407 NetXtreme-E 25Gb Ethernet" },
  123. { "Broadcom BCM57407 NetXtreme-E Ethernet Partition" },
  124. { "Broadcom BCM57414 NetXtreme-E Ethernet Partition" },
  125. { "Broadcom BCM57416 NetXtreme-E Ethernet Partition" },
  126. { "Broadcom NetXtreme-E Ethernet Virtual Function" },
  127. { "Broadcom NetXtreme-C Ethernet Virtual Function" },
  128. };
  129. static const struct pci_device_id bnxt_pci_tbl[] = {
  130. { PCI_VDEVICE(BROADCOM, 0x16c0), .driver_data = BCM57417_NPAR },
  131. { PCI_VDEVICE(BROADCOM, 0x16c8), .driver_data = BCM57301 },
  132. { PCI_VDEVICE(BROADCOM, 0x16c9), .driver_data = BCM57302 },
  133. { PCI_VDEVICE(BROADCOM, 0x16ca), .driver_data = BCM57304 },
  134. { PCI_VDEVICE(BROADCOM, 0x16cc), .driver_data = BCM57417_NPAR },
  135. { PCI_VDEVICE(BROADCOM, 0x16cd), .driver_data = BCM58700 },
  136. { PCI_VDEVICE(BROADCOM, 0x16ce), .driver_data = BCM57311 },
  137. { PCI_VDEVICE(BROADCOM, 0x16cf), .driver_data = BCM57312 },
  138. { PCI_VDEVICE(BROADCOM, 0x16d0), .driver_data = BCM57402 },
  139. { PCI_VDEVICE(BROADCOM, 0x16d1), .driver_data = BCM57404 },
  140. { PCI_VDEVICE(BROADCOM, 0x16d2), .driver_data = BCM57406 },
  141. { PCI_VDEVICE(BROADCOM, 0x16d4), .driver_data = BCM57402_NPAR },
  142. { PCI_VDEVICE(BROADCOM, 0x16d5), .driver_data = BCM57407 },
  143. { PCI_VDEVICE(BROADCOM, 0x16d6), .driver_data = BCM57412 },
  144. { PCI_VDEVICE(BROADCOM, 0x16d7), .driver_data = BCM57414 },
  145. { PCI_VDEVICE(BROADCOM, 0x16d8), .driver_data = BCM57416 },
  146. { PCI_VDEVICE(BROADCOM, 0x16d9), .driver_data = BCM57417 },
  147. { PCI_VDEVICE(BROADCOM, 0x16de), .driver_data = BCM57412_NPAR },
  148. { PCI_VDEVICE(BROADCOM, 0x16df), .driver_data = BCM57314 },
  149. { PCI_VDEVICE(BROADCOM, 0x16e2), .driver_data = BCM57417_SFP },
  150. { PCI_VDEVICE(BROADCOM, 0x16e3), .driver_data = BCM57416_SFP },
  151. { PCI_VDEVICE(BROADCOM, 0x16e7), .driver_data = BCM57404_NPAR },
  152. { PCI_VDEVICE(BROADCOM, 0x16e8), .driver_data = BCM57406_NPAR },
  153. { PCI_VDEVICE(BROADCOM, 0x16e9), .driver_data = BCM57407_SFP },
  154. { PCI_VDEVICE(BROADCOM, 0x16ea), .driver_data = BCM57407_NPAR },
  155. { PCI_VDEVICE(BROADCOM, 0x16eb), .driver_data = BCM57412_NPAR },
  156. { PCI_VDEVICE(BROADCOM, 0x16ec), .driver_data = BCM57414_NPAR },
  157. { PCI_VDEVICE(BROADCOM, 0x16ed), .driver_data = BCM57414_NPAR },
  158. { PCI_VDEVICE(BROADCOM, 0x16ee), .driver_data = BCM57416_NPAR },
  159. { PCI_VDEVICE(BROADCOM, 0x16ef), .driver_data = BCM57416_NPAR },
  160. #ifdef CONFIG_BNXT_SRIOV
  161. { PCI_VDEVICE(BROADCOM, 0x16c1), .driver_data = NETXTREME_E_VF },
  162. { PCI_VDEVICE(BROADCOM, 0x16cb), .driver_data = NETXTREME_C_VF },
  163. { PCI_VDEVICE(BROADCOM, 0x16d3), .driver_data = NETXTREME_E_VF },
  164. { PCI_VDEVICE(BROADCOM, 0x16dc), .driver_data = NETXTREME_E_VF },
  165. { PCI_VDEVICE(BROADCOM, 0x16e1), .driver_data = NETXTREME_C_VF },
  166. { PCI_VDEVICE(BROADCOM, 0x16e5), .driver_data = NETXTREME_C_VF },
  167. #endif
  168. { 0 }
  169. };
  170. MODULE_DEVICE_TABLE(pci, bnxt_pci_tbl);
  171. static const u16 bnxt_vf_req_snif[] = {
  172. HWRM_FUNC_CFG,
  173. HWRM_PORT_PHY_QCFG,
  174. HWRM_CFA_L2_FILTER_ALLOC,
  175. };
  176. static const u16 bnxt_async_events_arr[] = {
  177. ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE,
  178. ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD,
  179. ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED,
  180. ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE,
  181. ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE,
  182. };
  183. static bool bnxt_vf_pciid(enum board_idx idx)
  184. {
  185. return (idx == NETXTREME_C_VF || idx == NETXTREME_E_VF);
  186. }
  187. #define DB_CP_REARM_FLAGS (DB_KEY_CP | DB_IDX_VALID)
  188. #define DB_CP_FLAGS (DB_KEY_CP | DB_IDX_VALID | DB_IRQ_DIS)
  189. #define DB_CP_IRQ_DIS_FLAGS (DB_KEY_CP | DB_IRQ_DIS)
  190. #define BNXT_CP_DB_REARM(db, raw_cons) \
  191. writel(DB_CP_REARM_FLAGS | RING_CMP(raw_cons), db)
  192. #define BNXT_CP_DB(db, raw_cons) \
  193. writel(DB_CP_FLAGS | RING_CMP(raw_cons), db)
  194. #define BNXT_CP_DB_IRQ_DIS(db) \
  195. writel(DB_CP_IRQ_DIS_FLAGS, db)
  196. static inline u32 bnxt_tx_avail(struct bnxt *bp, struct bnxt_tx_ring_info *txr)
  197. {
  198. /* Tell compiler to fetch tx indices from memory. */
  199. barrier();
  200. return bp->tx_ring_size -
  201. ((txr->tx_prod - txr->tx_cons) & bp->tx_ring_mask);
  202. }
  203. static const u16 bnxt_lhint_arr[] = {
  204. TX_BD_FLAGS_LHINT_512_AND_SMALLER,
  205. TX_BD_FLAGS_LHINT_512_TO_1023,
  206. TX_BD_FLAGS_LHINT_1024_TO_2047,
  207. TX_BD_FLAGS_LHINT_1024_TO_2047,
  208. TX_BD_FLAGS_LHINT_2048_AND_LARGER,
  209. TX_BD_FLAGS_LHINT_2048_AND_LARGER,
  210. TX_BD_FLAGS_LHINT_2048_AND_LARGER,
  211. TX_BD_FLAGS_LHINT_2048_AND_LARGER,
  212. TX_BD_FLAGS_LHINT_2048_AND_LARGER,
  213. TX_BD_FLAGS_LHINT_2048_AND_LARGER,
  214. TX_BD_FLAGS_LHINT_2048_AND_LARGER,
  215. TX_BD_FLAGS_LHINT_2048_AND_LARGER,
  216. TX_BD_FLAGS_LHINT_2048_AND_LARGER,
  217. TX_BD_FLAGS_LHINT_2048_AND_LARGER,
  218. TX_BD_FLAGS_LHINT_2048_AND_LARGER,
  219. TX_BD_FLAGS_LHINT_2048_AND_LARGER,
  220. TX_BD_FLAGS_LHINT_2048_AND_LARGER,
  221. TX_BD_FLAGS_LHINT_2048_AND_LARGER,
  222. TX_BD_FLAGS_LHINT_2048_AND_LARGER,
  223. };
  224. static netdev_tx_t bnxt_start_xmit(struct sk_buff *skb, struct net_device *dev)
  225. {
  226. struct bnxt *bp = netdev_priv(dev);
  227. struct tx_bd *txbd;
  228. struct tx_bd_ext *txbd1;
  229. struct netdev_queue *txq;
  230. int i;
  231. dma_addr_t mapping;
  232. unsigned int length, pad = 0;
  233. u32 len, free_size, vlan_tag_flags, cfa_action, flags;
  234. u16 prod, last_frag;
  235. struct pci_dev *pdev = bp->pdev;
  236. struct bnxt_tx_ring_info *txr;
  237. struct bnxt_sw_tx_bd *tx_buf;
  238. i = skb_get_queue_mapping(skb);
  239. if (unlikely(i >= bp->tx_nr_rings)) {
  240. dev_kfree_skb_any(skb);
  241. return NETDEV_TX_OK;
  242. }
  243. txr = &bp->tx_ring[i];
  244. txq = netdev_get_tx_queue(dev, i);
  245. prod = txr->tx_prod;
  246. free_size = bnxt_tx_avail(bp, txr);
  247. if (unlikely(free_size < skb_shinfo(skb)->nr_frags + 2)) {
  248. netif_tx_stop_queue(txq);
  249. return NETDEV_TX_BUSY;
  250. }
  251. length = skb->len;
  252. len = skb_headlen(skb);
  253. last_frag = skb_shinfo(skb)->nr_frags;
  254. txbd = &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
  255. txbd->tx_bd_opaque = prod;
  256. tx_buf = &txr->tx_buf_ring[prod];
  257. tx_buf->skb = skb;
  258. tx_buf->nr_frags = last_frag;
  259. vlan_tag_flags = 0;
  260. cfa_action = 0;
  261. if (skb_vlan_tag_present(skb)) {
  262. vlan_tag_flags = TX_BD_CFA_META_KEY_VLAN |
  263. skb_vlan_tag_get(skb);
  264. /* Currently supports 8021Q, 8021AD vlan offloads
  265. * QINQ1, QINQ2, QINQ3 vlan headers are deprecated
  266. */
  267. if (skb->vlan_proto == htons(ETH_P_8021Q))
  268. vlan_tag_flags |= 1 << TX_BD_CFA_META_TPID_SHIFT;
  269. }
  270. if (free_size == bp->tx_ring_size && length <= bp->tx_push_thresh) {
  271. struct tx_push_buffer *tx_push_buf = txr->tx_push;
  272. struct tx_push_bd *tx_push = &tx_push_buf->push_bd;
  273. struct tx_bd_ext *tx_push1 = &tx_push->txbd2;
  274. void *pdata = tx_push_buf->data;
  275. u64 *end;
  276. int j, push_len;
  277. /* Set COAL_NOW to be ready quickly for the next push */
  278. tx_push->tx_bd_len_flags_type =
  279. cpu_to_le32((length << TX_BD_LEN_SHIFT) |
  280. TX_BD_TYPE_LONG_TX_BD |
  281. TX_BD_FLAGS_LHINT_512_AND_SMALLER |
  282. TX_BD_FLAGS_COAL_NOW |
  283. TX_BD_FLAGS_PACKET_END |
  284. (2 << TX_BD_FLAGS_BD_CNT_SHIFT));
  285. if (skb->ip_summed == CHECKSUM_PARTIAL)
  286. tx_push1->tx_bd_hsize_lflags =
  287. cpu_to_le32(TX_BD_FLAGS_TCP_UDP_CHKSUM);
  288. else
  289. tx_push1->tx_bd_hsize_lflags = 0;
  290. tx_push1->tx_bd_cfa_meta = cpu_to_le32(vlan_tag_flags);
  291. tx_push1->tx_bd_cfa_action = cpu_to_le32(cfa_action);
  292. end = pdata + length;
  293. end = PTR_ALIGN(end, 8) - 1;
  294. *end = 0;
  295. skb_copy_from_linear_data(skb, pdata, len);
  296. pdata += len;
  297. for (j = 0; j < last_frag; j++) {
  298. skb_frag_t *frag = &skb_shinfo(skb)->frags[j];
  299. void *fptr;
  300. fptr = skb_frag_address_safe(frag);
  301. if (!fptr)
  302. goto normal_tx;
  303. memcpy(pdata, fptr, skb_frag_size(frag));
  304. pdata += skb_frag_size(frag);
  305. }
  306. txbd->tx_bd_len_flags_type = tx_push->tx_bd_len_flags_type;
  307. txbd->tx_bd_haddr = txr->data_mapping;
  308. prod = NEXT_TX(prod);
  309. txbd = &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
  310. memcpy(txbd, tx_push1, sizeof(*txbd));
  311. prod = NEXT_TX(prod);
  312. tx_push->doorbell =
  313. cpu_to_le32(DB_KEY_TX_PUSH | DB_LONG_TX_PUSH | prod);
  314. txr->tx_prod = prod;
  315. tx_buf->is_push = 1;
  316. netdev_tx_sent_queue(txq, skb->len);
  317. wmb(); /* Sync is_push and byte queue before pushing data */
  318. push_len = (length + sizeof(*tx_push) + 7) / 8;
  319. if (push_len > 16) {
  320. __iowrite64_copy(txr->tx_doorbell, tx_push_buf, 16);
  321. __iowrite32_copy(txr->tx_doorbell + 4, tx_push_buf + 1,
  322. (push_len - 16) << 1);
  323. } else {
  324. __iowrite64_copy(txr->tx_doorbell, tx_push_buf,
  325. push_len);
  326. }
  327. goto tx_done;
  328. }
  329. normal_tx:
  330. if (length < BNXT_MIN_PKT_SIZE) {
  331. pad = BNXT_MIN_PKT_SIZE - length;
  332. if (skb_pad(skb, pad)) {
  333. /* SKB already freed. */
  334. tx_buf->skb = NULL;
  335. return NETDEV_TX_OK;
  336. }
  337. length = BNXT_MIN_PKT_SIZE;
  338. }
  339. mapping = dma_map_single(&pdev->dev, skb->data, len, DMA_TO_DEVICE);
  340. if (unlikely(dma_mapping_error(&pdev->dev, mapping))) {
  341. dev_kfree_skb_any(skb);
  342. tx_buf->skb = NULL;
  343. return NETDEV_TX_OK;
  344. }
  345. dma_unmap_addr_set(tx_buf, mapping, mapping);
  346. flags = (len << TX_BD_LEN_SHIFT) | TX_BD_TYPE_LONG_TX_BD |
  347. ((last_frag + 2) << TX_BD_FLAGS_BD_CNT_SHIFT);
  348. txbd->tx_bd_haddr = cpu_to_le64(mapping);
  349. prod = NEXT_TX(prod);
  350. txbd1 = (struct tx_bd_ext *)
  351. &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
  352. txbd1->tx_bd_hsize_lflags = 0;
  353. if (skb_is_gso(skb)) {
  354. u32 hdr_len;
  355. if (skb->encapsulation)
  356. hdr_len = skb_inner_network_offset(skb) +
  357. skb_inner_network_header_len(skb) +
  358. inner_tcp_hdrlen(skb);
  359. else
  360. hdr_len = skb_transport_offset(skb) +
  361. tcp_hdrlen(skb);
  362. txbd1->tx_bd_hsize_lflags = cpu_to_le32(TX_BD_FLAGS_LSO |
  363. TX_BD_FLAGS_T_IPID |
  364. (hdr_len << (TX_BD_HSIZE_SHIFT - 1)));
  365. length = skb_shinfo(skb)->gso_size;
  366. txbd1->tx_bd_mss = cpu_to_le32(length);
  367. length += hdr_len;
  368. } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
  369. txbd1->tx_bd_hsize_lflags =
  370. cpu_to_le32(TX_BD_FLAGS_TCP_UDP_CHKSUM);
  371. txbd1->tx_bd_mss = 0;
  372. }
  373. length >>= 9;
  374. flags |= bnxt_lhint_arr[length];
  375. txbd->tx_bd_len_flags_type = cpu_to_le32(flags);
  376. txbd1->tx_bd_cfa_meta = cpu_to_le32(vlan_tag_flags);
  377. txbd1->tx_bd_cfa_action = cpu_to_le32(cfa_action);
  378. for (i = 0; i < last_frag; i++) {
  379. skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  380. prod = NEXT_TX(prod);
  381. txbd = &txr->tx_desc_ring[TX_RING(prod)][TX_IDX(prod)];
  382. len = skb_frag_size(frag);
  383. mapping = skb_frag_dma_map(&pdev->dev, frag, 0, len,
  384. DMA_TO_DEVICE);
  385. if (unlikely(dma_mapping_error(&pdev->dev, mapping)))
  386. goto tx_dma_error;
  387. tx_buf = &txr->tx_buf_ring[prod];
  388. dma_unmap_addr_set(tx_buf, mapping, mapping);
  389. txbd->tx_bd_haddr = cpu_to_le64(mapping);
  390. flags = len << TX_BD_LEN_SHIFT;
  391. txbd->tx_bd_len_flags_type = cpu_to_le32(flags);
  392. }
  393. flags &= ~TX_BD_LEN;
  394. txbd->tx_bd_len_flags_type =
  395. cpu_to_le32(((len + pad) << TX_BD_LEN_SHIFT) | flags |
  396. TX_BD_FLAGS_PACKET_END);
  397. netdev_tx_sent_queue(txq, skb->len);
  398. /* Sync BD data before updating doorbell */
  399. wmb();
  400. prod = NEXT_TX(prod);
  401. txr->tx_prod = prod;
  402. writel(DB_KEY_TX | prod, txr->tx_doorbell);
  403. writel(DB_KEY_TX | prod, txr->tx_doorbell);
  404. tx_done:
  405. mmiowb();
  406. if (unlikely(bnxt_tx_avail(bp, txr) <= MAX_SKB_FRAGS + 1)) {
  407. netif_tx_stop_queue(txq);
  408. /* netif_tx_stop_queue() must be done before checking
  409. * tx index in bnxt_tx_avail() below, because in
  410. * bnxt_tx_int(), we update tx index before checking for
  411. * netif_tx_queue_stopped().
  412. */
  413. smp_mb();
  414. if (bnxt_tx_avail(bp, txr) > bp->tx_wake_thresh)
  415. netif_tx_wake_queue(txq);
  416. }
  417. return NETDEV_TX_OK;
  418. tx_dma_error:
  419. last_frag = i;
  420. /* start back at beginning and unmap skb */
  421. prod = txr->tx_prod;
  422. tx_buf = &txr->tx_buf_ring[prod];
  423. tx_buf->skb = NULL;
  424. dma_unmap_single(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
  425. skb_headlen(skb), PCI_DMA_TODEVICE);
  426. prod = NEXT_TX(prod);
  427. /* unmap remaining mapped pages */
  428. for (i = 0; i < last_frag; i++) {
  429. prod = NEXT_TX(prod);
  430. tx_buf = &txr->tx_buf_ring[prod];
  431. dma_unmap_page(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
  432. skb_frag_size(&skb_shinfo(skb)->frags[i]),
  433. PCI_DMA_TODEVICE);
  434. }
  435. dev_kfree_skb_any(skb);
  436. return NETDEV_TX_OK;
  437. }
  438. static void bnxt_tx_int(struct bnxt *bp, struct bnxt_napi *bnapi, int nr_pkts)
  439. {
  440. struct bnxt_tx_ring_info *txr = bnapi->tx_ring;
  441. int index = txr - &bp->tx_ring[0];
  442. struct netdev_queue *txq = netdev_get_tx_queue(bp->dev, index);
  443. u16 cons = txr->tx_cons;
  444. struct pci_dev *pdev = bp->pdev;
  445. int i;
  446. unsigned int tx_bytes = 0;
  447. for (i = 0; i < nr_pkts; i++) {
  448. struct bnxt_sw_tx_bd *tx_buf;
  449. struct sk_buff *skb;
  450. int j, last;
  451. tx_buf = &txr->tx_buf_ring[cons];
  452. cons = NEXT_TX(cons);
  453. skb = tx_buf->skb;
  454. tx_buf->skb = NULL;
  455. if (tx_buf->is_push) {
  456. tx_buf->is_push = 0;
  457. goto next_tx_int;
  458. }
  459. dma_unmap_single(&pdev->dev, dma_unmap_addr(tx_buf, mapping),
  460. skb_headlen(skb), PCI_DMA_TODEVICE);
  461. last = tx_buf->nr_frags;
  462. for (j = 0; j < last; j++) {
  463. cons = NEXT_TX(cons);
  464. tx_buf = &txr->tx_buf_ring[cons];
  465. dma_unmap_page(
  466. &pdev->dev,
  467. dma_unmap_addr(tx_buf, mapping),
  468. skb_frag_size(&skb_shinfo(skb)->frags[j]),
  469. PCI_DMA_TODEVICE);
  470. }
  471. next_tx_int:
  472. cons = NEXT_TX(cons);
  473. tx_bytes += skb->len;
  474. dev_kfree_skb_any(skb);
  475. }
  476. netdev_tx_completed_queue(txq, nr_pkts, tx_bytes);
  477. txr->tx_cons = cons;
  478. /* Need to make the tx_cons update visible to bnxt_start_xmit()
  479. * before checking for netif_tx_queue_stopped(). Without the
  480. * memory barrier, there is a small possibility that bnxt_start_xmit()
  481. * will miss it and cause the queue to be stopped forever.
  482. */
  483. smp_mb();
  484. if (unlikely(netif_tx_queue_stopped(txq)) &&
  485. (bnxt_tx_avail(bp, txr) > bp->tx_wake_thresh)) {
  486. __netif_tx_lock(txq, smp_processor_id());
  487. if (netif_tx_queue_stopped(txq) &&
  488. bnxt_tx_avail(bp, txr) > bp->tx_wake_thresh &&
  489. txr->dev_state != BNXT_DEV_STATE_CLOSING)
  490. netif_tx_wake_queue(txq);
  491. __netif_tx_unlock(txq);
  492. }
  493. }
  494. static inline u8 *__bnxt_alloc_rx_data(struct bnxt *bp, dma_addr_t *mapping,
  495. gfp_t gfp)
  496. {
  497. u8 *data;
  498. struct pci_dev *pdev = bp->pdev;
  499. data = kmalloc(bp->rx_buf_size, gfp);
  500. if (!data)
  501. return NULL;
  502. *mapping = dma_map_single(&pdev->dev, data + BNXT_RX_DMA_OFFSET,
  503. bp->rx_buf_use_size, PCI_DMA_FROMDEVICE);
  504. if (dma_mapping_error(&pdev->dev, *mapping)) {
  505. kfree(data);
  506. data = NULL;
  507. }
  508. return data;
  509. }
  510. static inline int bnxt_alloc_rx_data(struct bnxt *bp,
  511. struct bnxt_rx_ring_info *rxr,
  512. u16 prod, gfp_t gfp)
  513. {
  514. struct rx_bd *rxbd = &rxr->rx_desc_ring[RX_RING(prod)][RX_IDX(prod)];
  515. struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[prod];
  516. u8 *data;
  517. dma_addr_t mapping;
  518. data = __bnxt_alloc_rx_data(bp, &mapping, gfp);
  519. if (!data)
  520. return -ENOMEM;
  521. rx_buf->data = data;
  522. dma_unmap_addr_set(rx_buf, mapping, mapping);
  523. rxbd->rx_bd_haddr = cpu_to_le64(mapping);
  524. return 0;
  525. }
  526. static void bnxt_reuse_rx_data(struct bnxt_rx_ring_info *rxr, u16 cons,
  527. u8 *data)
  528. {
  529. u16 prod = rxr->rx_prod;
  530. struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf;
  531. struct rx_bd *cons_bd, *prod_bd;
  532. prod_rx_buf = &rxr->rx_buf_ring[prod];
  533. cons_rx_buf = &rxr->rx_buf_ring[cons];
  534. prod_rx_buf->data = data;
  535. dma_unmap_addr_set(prod_rx_buf, mapping,
  536. dma_unmap_addr(cons_rx_buf, mapping));
  537. prod_bd = &rxr->rx_desc_ring[RX_RING(prod)][RX_IDX(prod)];
  538. cons_bd = &rxr->rx_desc_ring[RX_RING(cons)][RX_IDX(cons)];
  539. prod_bd->rx_bd_haddr = cons_bd->rx_bd_haddr;
  540. }
  541. static inline u16 bnxt_find_next_agg_idx(struct bnxt_rx_ring_info *rxr, u16 idx)
  542. {
  543. u16 next, max = rxr->rx_agg_bmap_size;
  544. next = find_next_zero_bit(rxr->rx_agg_bmap, max, idx);
  545. if (next >= max)
  546. next = find_first_zero_bit(rxr->rx_agg_bmap, max);
  547. return next;
  548. }
  549. static inline int bnxt_alloc_rx_page(struct bnxt *bp,
  550. struct bnxt_rx_ring_info *rxr,
  551. u16 prod, gfp_t gfp)
  552. {
  553. struct rx_bd *rxbd =
  554. &rxr->rx_agg_desc_ring[RX_RING(prod)][RX_IDX(prod)];
  555. struct bnxt_sw_rx_agg_bd *rx_agg_buf;
  556. struct pci_dev *pdev = bp->pdev;
  557. struct page *page;
  558. dma_addr_t mapping;
  559. u16 sw_prod = rxr->rx_sw_agg_prod;
  560. unsigned int offset = 0;
  561. if (PAGE_SIZE > BNXT_RX_PAGE_SIZE) {
  562. page = rxr->rx_page;
  563. if (!page) {
  564. page = alloc_page(gfp);
  565. if (!page)
  566. return -ENOMEM;
  567. rxr->rx_page = page;
  568. rxr->rx_page_offset = 0;
  569. }
  570. offset = rxr->rx_page_offset;
  571. rxr->rx_page_offset += BNXT_RX_PAGE_SIZE;
  572. if (rxr->rx_page_offset == PAGE_SIZE)
  573. rxr->rx_page = NULL;
  574. else
  575. get_page(page);
  576. } else {
  577. page = alloc_page(gfp);
  578. if (!page)
  579. return -ENOMEM;
  580. }
  581. mapping = dma_map_page(&pdev->dev, page, offset, BNXT_RX_PAGE_SIZE,
  582. PCI_DMA_FROMDEVICE);
  583. if (dma_mapping_error(&pdev->dev, mapping)) {
  584. __free_page(page);
  585. return -EIO;
  586. }
  587. if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap)))
  588. sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod);
  589. __set_bit(sw_prod, rxr->rx_agg_bmap);
  590. rx_agg_buf = &rxr->rx_agg_ring[sw_prod];
  591. rxr->rx_sw_agg_prod = NEXT_RX_AGG(sw_prod);
  592. rx_agg_buf->page = page;
  593. rx_agg_buf->offset = offset;
  594. rx_agg_buf->mapping = mapping;
  595. rxbd->rx_bd_haddr = cpu_to_le64(mapping);
  596. rxbd->rx_bd_opaque = sw_prod;
  597. return 0;
  598. }
  599. static void bnxt_reuse_rx_agg_bufs(struct bnxt_napi *bnapi, u16 cp_cons,
  600. u32 agg_bufs)
  601. {
  602. struct bnxt *bp = bnapi->bp;
  603. struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
  604. struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
  605. u16 prod = rxr->rx_agg_prod;
  606. u16 sw_prod = rxr->rx_sw_agg_prod;
  607. u32 i;
  608. for (i = 0; i < agg_bufs; i++) {
  609. u16 cons;
  610. struct rx_agg_cmp *agg;
  611. struct bnxt_sw_rx_agg_bd *cons_rx_buf, *prod_rx_buf;
  612. struct rx_bd *prod_bd;
  613. struct page *page;
  614. agg = (struct rx_agg_cmp *)
  615. &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
  616. cons = agg->rx_agg_cmp_opaque;
  617. __clear_bit(cons, rxr->rx_agg_bmap);
  618. if (unlikely(test_bit(sw_prod, rxr->rx_agg_bmap)))
  619. sw_prod = bnxt_find_next_agg_idx(rxr, sw_prod);
  620. __set_bit(sw_prod, rxr->rx_agg_bmap);
  621. prod_rx_buf = &rxr->rx_agg_ring[sw_prod];
  622. cons_rx_buf = &rxr->rx_agg_ring[cons];
  623. /* It is possible for sw_prod to be equal to cons, so
  624. * set cons_rx_buf->page to NULL first.
  625. */
  626. page = cons_rx_buf->page;
  627. cons_rx_buf->page = NULL;
  628. prod_rx_buf->page = page;
  629. prod_rx_buf->offset = cons_rx_buf->offset;
  630. prod_rx_buf->mapping = cons_rx_buf->mapping;
  631. prod_bd = &rxr->rx_agg_desc_ring[RX_RING(prod)][RX_IDX(prod)];
  632. prod_bd->rx_bd_haddr = cpu_to_le64(cons_rx_buf->mapping);
  633. prod_bd->rx_bd_opaque = sw_prod;
  634. prod = NEXT_RX_AGG(prod);
  635. sw_prod = NEXT_RX_AGG(sw_prod);
  636. cp_cons = NEXT_CMP(cp_cons);
  637. }
  638. rxr->rx_agg_prod = prod;
  639. rxr->rx_sw_agg_prod = sw_prod;
  640. }
  641. static struct sk_buff *bnxt_rx_skb(struct bnxt *bp,
  642. struct bnxt_rx_ring_info *rxr, u16 cons,
  643. u16 prod, u8 *data, dma_addr_t dma_addr,
  644. unsigned int len)
  645. {
  646. int err;
  647. struct sk_buff *skb;
  648. err = bnxt_alloc_rx_data(bp, rxr, prod, GFP_ATOMIC);
  649. if (unlikely(err)) {
  650. bnxt_reuse_rx_data(rxr, cons, data);
  651. return NULL;
  652. }
  653. skb = build_skb(data, 0);
  654. dma_unmap_single(&bp->pdev->dev, dma_addr, bp->rx_buf_use_size,
  655. PCI_DMA_FROMDEVICE);
  656. if (!skb) {
  657. kfree(data);
  658. return NULL;
  659. }
  660. skb_reserve(skb, BNXT_RX_OFFSET);
  661. skb_put(skb, len);
  662. return skb;
  663. }
  664. static struct sk_buff *bnxt_rx_pages(struct bnxt *bp, struct bnxt_napi *bnapi,
  665. struct sk_buff *skb, u16 cp_cons,
  666. u32 agg_bufs)
  667. {
  668. struct pci_dev *pdev = bp->pdev;
  669. struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
  670. struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
  671. u16 prod = rxr->rx_agg_prod;
  672. u32 i;
  673. for (i = 0; i < agg_bufs; i++) {
  674. u16 cons, frag_len;
  675. struct rx_agg_cmp *agg;
  676. struct bnxt_sw_rx_agg_bd *cons_rx_buf;
  677. struct page *page;
  678. dma_addr_t mapping;
  679. agg = (struct rx_agg_cmp *)
  680. &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
  681. cons = agg->rx_agg_cmp_opaque;
  682. frag_len = (le32_to_cpu(agg->rx_agg_cmp_len_flags_type) &
  683. RX_AGG_CMP_LEN) >> RX_AGG_CMP_LEN_SHIFT;
  684. cons_rx_buf = &rxr->rx_agg_ring[cons];
  685. skb_fill_page_desc(skb, i, cons_rx_buf->page,
  686. cons_rx_buf->offset, frag_len);
  687. __clear_bit(cons, rxr->rx_agg_bmap);
  688. /* It is possible for bnxt_alloc_rx_page() to allocate
  689. * a sw_prod index that equals the cons index, so we
  690. * need to clear the cons entry now.
  691. */
  692. mapping = dma_unmap_addr(cons_rx_buf, mapping);
  693. page = cons_rx_buf->page;
  694. cons_rx_buf->page = NULL;
  695. if (bnxt_alloc_rx_page(bp, rxr, prod, GFP_ATOMIC) != 0) {
  696. struct skb_shared_info *shinfo;
  697. unsigned int nr_frags;
  698. shinfo = skb_shinfo(skb);
  699. nr_frags = --shinfo->nr_frags;
  700. __skb_frag_set_page(&shinfo->frags[nr_frags], NULL);
  701. dev_kfree_skb(skb);
  702. cons_rx_buf->page = page;
  703. /* Update prod since possibly some pages have been
  704. * allocated already.
  705. */
  706. rxr->rx_agg_prod = prod;
  707. bnxt_reuse_rx_agg_bufs(bnapi, cp_cons, agg_bufs - i);
  708. return NULL;
  709. }
  710. dma_unmap_page(&pdev->dev, mapping, BNXT_RX_PAGE_SIZE,
  711. PCI_DMA_FROMDEVICE);
  712. skb->data_len += frag_len;
  713. skb->len += frag_len;
  714. skb->truesize += PAGE_SIZE;
  715. prod = NEXT_RX_AGG(prod);
  716. cp_cons = NEXT_CMP(cp_cons);
  717. }
  718. rxr->rx_agg_prod = prod;
  719. return skb;
  720. }
  721. static int bnxt_agg_bufs_valid(struct bnxt *bp, struct bnxt_cp_ring_info *cpr,
  722. u8 agg_bufs, u32 *raw_cons)
  723. {
  724. u16 last;
  725. struct rx_agg_cmp *agg;
  726. *raw_cons = ADV_RAW_CMP(*raw_cons, agg_bufs);
  727. last = RING_CMP(*raw_cons);
  728. agg = (struct rx_agg_cmp *)
  729. &cpr->cp_desc_ring[CP_RING(last)][CP_IDX(last)];
  730. return RX_AGG_CMP_VALID(agg, *raw_cons);
  731. }
  732. static inline struct sk_buff *bnxt_copy_skb(struct bnxt_napi *bnapi, u8 *data,
  733. unsigned int len,
  734. dma_addr_t mapping)
  735. {
  736. struct bnxt *bp = bnapi->bp;
  737. struct pci_dev *pdev = bp->pdev;
  738. struct sk_buff *skb;
  739. skb = napi_alloc_skb(&bnapi->napi, len);
  740. if (!skb)
  741. return NULL;
  742. dma_sync_single_for_cpu(&pdev->dev, mapping,
  743. bp->rx_copy_thresh, PCI_DMA_FROMDEVICE);
  744. memcpy(skb->data - BNXT_RX_OFFSET, data, len + BNXT_RX_OFFSET);
  745. dma_sync_single_for_device(&pdev->dev, mapping,
  746. bp->rx_copy_thresh,
  747. PCI_DMA_FROMDEVICE);
  748. skb_put(skb, len);
  749. return skb;
  750. }
  751. static int bnxt_discard_rx(struct bnxt *bp, struct bnxt_napi *bnapi,
  752. u32 *raw_cons, void *cmp)
  753. {
  754. struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
  755. struct rx_cmp *rxcmp = cmp;
  756. u32 tmp_raw_cons = *raw_cons;
  757. u8 cmp_type, agg_bufs = 0;
  758. cmp_type = RX_CMP_TYPE(rxcmp);
  759. if (cmp_type == CMP_TYPE_RX_L2_CMP) {
  760. agg_bufs = (le32_to_cpu(rxcmp->rx_cmp_misc_v1) &
  761. RX_CMP_AGG_BUFS) >>
  762. RX_CMP_AGG_BUFS_SHIFT;
  763. } else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
  764. struct rx_tpa_end_cmp *tpa_end = cmp;
  765. agg_bufs = (le32_to_cpu(tpa_end->rx_tpa_end_cmp_misc_v1) &
  766. RX_TPA_END_CMP_AGG_BUFS) >>
  767. RX_TPA_END_CMP_AGG_BUFS_SHIFT;
  768. }
  769. if (agg_bufs) {
  770. if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons))
  771. return -EBUSY;
  772. }
  773. *raw_cons = tmp_raw_cons;
  774. return 0;
  775. }
  776. static void bnxt_sched_reset(struct bnxt *bp, struct bnxt_rx_ring_info *rxr)
  777. {
  778. if (!rxr->bnapi->in_reset) {
  779. rxr->bnapi->in_reset = true;
  780. set_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event);
  781. schedule_work(&bp->sp_task);
  782. }
  783. rxr->rx_next_cons = 0xffff;
  784. }
  785. static void bnxt_tpa_start(struct bnxt *bp, struct bnxt_rx_ring_info *rxr,
  786. struct rx_tpa_start_cmp *tpa_start,
  787. struct rx_tpa_start_cmp_ext *tpa_start1)
  788. {
  789. u8 agg_id = TPA_START_AGG_ID(tpa_start);
  790. u16 cons, prod;
  791. struct bnxt_tpa_info *tpa_info;
  792. struct bnxt_sw_rx_bd *cons_rx_buf, *prod_rx_buf;
  793. struct rx_bd *prod_bd;
  794. dma_addr_t mapping;
  795. cons = tpa_start->rx_tpa_start_cmp_opaque;
  796. prod = rxr->rx_prod;
  797. cons_rx_buf = &rxr->rx_buf_ring[cons];
  798. prod_rx_buf = &rxr->rx_buf_ring[prod];
  799. tpa_info = &rxr->rx_tpa[agg_id];
  800. if (unlikely(cons != rxr->rx_next_cons)) {
  801. bnxt_sched_reset(bp, rxr);
  802. return;
  803. }
  804. prod_rx_buf->data = tpa_info->data;
  805. mapping = tpa_info->mapping;
  806. dma_unmap_addr_set(prod_rx_buf, mapping, mapping);
  807. prod_bd = &rxr->rx_desc_ring[RX_RING(prod)][RX_IDX(prod)];
  808. prod_bd->rx_bd_haddr = cpu_to_le64(mapping);
  809. tpa_info->data = cons_rx_buf->data;
  810. cons_rx_buf->data = NULL;
  811. tpa_info->mapping = dma_unmap_addr(cons_rx_buf, mapping);
  812. tpa_info->len =
  813. le32_to_cpu(tpa_start->rx_tpa_start_cmp_len_flags_type) >>
  814. RX_TPA_START_CMP_LEN_SHIFT;
  815. if (likely(TPA_START_HASH_VALID(tpa_start))) {
  816. u32 hash_type = TPA_START_HASH_TYPE(tpa_start);
  817. tpa_info->hash_type = PKT_HASH_TYPE_L4;
  818. tpa_info->gso_type = SKB_GSO_TCPV4;
  819. /* RSS profiles 1 and 3 with extract code 0 for inner 4-tuple */
  820. if (hash_type == 3)
  821. tpa_info->gso_type = SKB_GSO_TCPV6;
  822. tpa_info->rss_hash =
  823. le32_to_cpu(tpa_start->rx_tpa_start_cmp_rss_hash);
  824. } else {
  825. tpa_info->hash_type = PKT_HASH_TYPE_NONE;
  826. tpa_info->gso_type = 0;
  827. if (netif_msg_rx_err(bp))
  828. netdev_warn(bp->dev, "TPA packet without valid hash\n");
  829. }
  830. tpa_info->flags2 = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_flags2);
  831. tpa_info->metadata = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_metadata);
  832. tpa_info->hdr_info = le32_to_cpu(tpa_start1->rx_tpa_start_cmp_hdr_info);
  833. rxr->rx_prod = NEXT_RX(prod);
  834. cons = NEXT_RX(cons);
  835. rxr->rx_next_cons = NEXT_RX(cons);
  836. cons_rx_buf = &rxr->rx_buf_ring[cons];
  837. bnxt_reuse_rx_data(rxr, cons, cons_rx_buf->data);
  838. rxr->rx_prod = NEXT_RX(rxr->rx_prod);
  839. cons_rx_buf->data = NULL;
  840. }
  841. static void bnxt_abort_tpa(struct bnxt *bp, struct bnxt_napi *bnapi,
  842. u16 cp_cons, u32 agg_bufs)
  843. {
  844. if (agg_bufs)
  845. bnxt_reuse_rx_agg_bufs(bnapi, cp_cons, agg_bufs);
  846. }
  847. static struct sk_buff *bnxt_gro_func_5731x(struct bnxt_tpa_info *tpa_info,
  848. int payload_off, int tcp_ts,
  849. struct sk_buff *skb)
  850. {
  851. #ifdef CONFIG_INET
  852. struct tcphdr *th;
  853. int len, nw_off;
  854. u16 outer_ip_off, inner_ip_off, inner_mac_off;
  855. u32 hdr_info = tpa_info->hdr_info;
  856. bool loopback = false;
  857. inner_ip_off = BNXT_TPA_INNER_L3_OFF(hdr_info);
  858. inner_mac_off = BNXT_TPA_INNER_L2_OFF(hdr_info);
  859. outer_ip_off = BNXT_TPA_OUTER_L3_OFF(hdr_info);
  860. /* If the packet is an internal loopback packet, the offsets will
  861. * have an extra 4 bytes.
  862. */
  863. if (inner_mac_off == 4) {
  864. loopback = true;
  865. } else if (inner_mac_off > 4) {
  866. __be16 proto = *((__be16 *)(skb->data + inner_ip_off -
  867. ETH_HLEN - 2));
  868. /* We only support inner iPv4/ipv6. If we don't see the
  869. * correct protocol ID, it must be a loopback packet where
  870. * the offsets are off by 4.
  871. */
  872. if (proto != htons(ETH_P_IP) && proto != htons(ETH_P_IPV6))
  873. loopback = true;
  874. }
  875. if (loopback) {
  876. /* internal loopback packet, subtract all offsets by 4 */
  877. inner_ip_off -= 4;
  878. inner_mac_off -= 4;
  879. outer_ip_off -= 4;
  880. }
  881. nw_off = inner_ip_off - ETH_HLEN;
  882. skb_set_network_header(skb, nw_off);
  883. if (tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_IP_TYPE) {
  884. struct ipv6hdr *iph = ipv6_hdr(skb);
  885. skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr));
  886. len = skb->len - skb_transport_offset(skb);
  887. th = tcp_hdr(skb);
  888. th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0);
  889. } else {
  890. struct iphdr *iph = ip_hdr(skb);
  891. skb_set_transport_header(skb, nw_off + sizeof(struct iphdr));
  892. len = skb->len - skb_transport_offset(skb);
  893. th = tcp_hdr(skb);
  894. th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0);
  895. }
  896. if (inner_mac_off) { /* tunnel */
  897. struct udphdr *uh = NULL;
  898. __be16 proto = *((__be16 *)(skb->data + outer_ip_off -
  899. ETH_HLEN - 2));
  900. if (proto == htons(ETH_P_IP)) {
  901. struct iphdr *iph = (struct iphdr *)skb->data;
  902. if (iph->protocol == IPPROTO_UDP)
  903. uh = (struct udphdr *)(iph + 1);
  904. } else {
  905. struct ipv6hdr *iph = (struct ipv6hdr *)skb->data;
  906. if (iph->nexthdr == IPPROTO_UDP)
  907. uh = (struct udphdr *)(iph + 1);
  908. }
  909. if (uh) {
  910. if (uh->check)
  911. skb_shinfo(skb)->gso_type |=
  912. SKB_GSO_UDP_TUNNEL_CSUM;
  913. else
  914. skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL;
  915. }
  916. }
  917. #endif
  918. return skb;
  919. }
  920. #define BNXT_IPV4_HDR_SIZE (sizeof(struct iphdr) + sizeof(struct tcphdr))
  921. #define BNXT_IPV6_HDR_SIZE (sizeof(struct ipv6hdr) + sizeof(struct tcphdr))
  922. static struct sk_buff *bnxt_gro_func_5730x(struct bnxt_tpa_info *tpa_info,
  923. int payload_off, int tcp_ts,
  924. struct sk_buff *skb)
  925. {
  926. #ifdef CONFIG_INET
  927. struct tcphdr *th;
  928. int len, nw_off, tcp_opt_len;
  929. if (tcp_ts)
  930. tcp_opt_len = 12;
  931. if (tpa_info->gso_type == SKB_GSO_TCPV4) {
  932. struct iphdr *iph;
  933. nw_off = payload_off - BNXT_IPV4_HDR_SIZE - tcp_opt_len -
  934. ETH_HLEN;
  935. skb_set_network_header(skb, nw_off);
  936. iph = ip_hdr(skb);
  937. skb_set_transport_header(skb, nw_off + sizeof(struct iphdr));
  938. len = skb->len - skb_transport_offset(skb);
  939. th = tcp_hdr(skb);
  940. th->check = ~tcp_v4_check(len, iph->saddr, iph->daddr, 0);
  941. } else if (tpa_info->gso_type == SKB_GSO_TCPV6) {
  942. struct ipv6hdr *iph;
  943. nw_off = payload_off - BNXT_IPV6_HDR_SIZE - tcp_opt_len -
  944. ETH_HLEN;
  945. skb_set_network_header(skb, nw_off);
  946. iph = ipv6_hdr(skb);
  947. skb_set_transport_header(skb, nw_off + sizeof(struct ipv6hdr));
  948. len = skb->len - skb_transport_offset(skb);
  949. th = tcp_hdr(skb);
  950. th->check = ~tcp_v6_check(len, &iph->saddr, &iph->daddr, 0);
  951. } else {
  952. dev_kfree_skb_any(skb);
  953. return NULL;
  954. }
  955. tcp_gro_complete(skb);
  956. if (nw_off) { /* tunnel */
  957. struct udphdr *uh = NULL;
  958. if (skb->protocol == htons(ETH_P_IP)) {
  959. struct iphdr *iph = (struct iphdr *)skb->data;
  960. if (iph->protocol == IPPROTO_UDP)
  961. uh = (struct udphdr *)(iph + 1);
  962. } else {
  963. struct ipv6hdr *iph = (struct ipv6hdr *)skb->data;
  964. if (iph->nexthdr == IPPROTO_UDP)
  965. uh = (struct udphdr *)(iph + 1);
  966. }
  967. if (uh) {
  968. if (uh->check)
  969. skb_shinfo(skb)->gso_type |=
  970. SKB_GSO_UDP_TUNNEL_CSUM;
  971. else
  972. skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL;
  973. }
  974. }
  975. #endif
  976. return skb;
  977. }
  978. static inline struct sk_buff *bnxt_gro_skb(struct bnxt *bp,
  979. struct bnxt_tpa_info *tpa_info,
  980. struct rx_tpa_end_cmp *tpa_end,
  981. struct rx_tpa_end_cmp_ext *tpa_end1,
  982. struct sk_buff *skb)
  983. {
  984. #ifdef CONFIG_INET
  985. int payload_off;
  986. u16 segs;
  987. segs = TPA_END_TPA_SEGS(tpa_end);
  988. if (segs == 1)
  989. return skb;
  990. NAPI_GRO_CB(skb)->count = segs;
  991. skb_shinfo(skb)->gso_size =
  992. le32_to_cpu(tpa_end1->rx_tpa_end_cmp_seg_len);
  993. skb_shinfo(skb)->gso_type = tpa_info->gso_type;
  994. payload_off = (le32_to_cpu(tpa_end->rx_tpa_end_cmp_misc_v1) &
  995. RX_TPA_END_CMP_PAYLOAD_OFFSET) >>
  996. RX_TPA_END_CMP_PAYLOAD_OFFSET_SHIFT;
  997. skb = bp->gro_func(tpa_info, payload_off, TPA_END_GRO_TS(tpa_end), skb);
  998. #endif
  999. return skb;
  1000. }
  1001. static inline struct sk_buff *bnxt_tpa_end(struct bnxt *bp,
  1002. struct bnxt_napi *bnapi,
  1003. u32 *raw_cons,
  1004. struct rx_tpa_end_cmp *tpa_end,
  1005. struct rx_tpa_end_cmp_ext *tpa_end1,
  1006. bool *agg_event)
  1007. {
  1008. struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
  1009. struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
  1010. u8 agg_id = TPA_END_AGG_ID(tpa_end);
  1011. u8 *data, agg_bufs;
  1012. u16 cp_cons = RING_CMP(*raw_cons);
  1013. unsigned int len;
  1014. struct bnxt_tpa_info *tpa_info;
  1015. dma_addr_t mapping;
  1016. struct sk_buff *skb;
  1017. if (unlikely(bnapi->in_reset)) {
  1018. int rc = bnxt_discard_rx(bp, bnapi, raw_cons, tpa_end);
  1019. if (rc < 0)
  1020. return ERR_PTR(-EBUSY);
  1021. return NULL;
  1022. }
  1023. tpa_info = &rxr->rx_tpa[agg_id];
  1024. data = tpa_info->data;
  1025. prefetch(data);
  1026. len = tpa_info->len;
  1027. mapping = tpa_info->mapping;
  1028. agg_bufs = (le32_to_cpu(tpa_end->rx_tpa_end_cmp_misc_v1) &
  1029. RX_TPA_END_CMP_AGG_BUFS) >> RX_TPA_END_CMP_AGG_BUFS_SHIFT;
  1030. if (agg_bufs) {
  1031. if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, raw_cons))
  1032. return ERR_PTR(-EBUSY);
  1033. *agg_event = true;
  1034. cp_cons = NEXT_CMP(cp_cons);
  1035. }
  1036. if (unlikely(agg_bufs > MAX_SKB_FRAGS)) {
  1037. bnxt_abort_tpa(bp, bnapi, cp_cons, agg_bufs);
  1038. netdev_warn(bp->dev, "TPA frags %d exceeded MAX_SKB_FRAGS %d\n",
  1039. agg_bufs, (int)MAX_SKB_FRAGS);
  1040. return NULL;
  1041. }
  1042. if (len <= bp->rx_copy_thresh) {
  1043. skb = bnxt_copy_skb(bnapi, data, len, mapping);
  1044. if (!skb) {
  1045. bnxt_abort_tpa(bp, bnapi, cp_cons, agg_bufs);
  1046. return NULL;
  1047. }
  1048. } else {
  1049. u8 *new_data;
  1050. dma_addr_t new_mapping;
  1051. new_data = __bnxt_alloc_rx_data(bp, &new_mapping, GFP_ATOMIC);
  1052. if (!new_data) {
  1053. bnxt_abort_tpa(bp, bnapi, cp_cons, agg_bufs);
  1054. return NULL;
  1055. }
  1056. tpa_info->data = new_data;
  1057. tpa_info->mapping = new_mapping;
  1058. skb = build_skb(data, 0);
  1059. dma_unmap_single(&bp->pdev->dev, mapping, bp->rx_buf_use_size,
  1060. PCI_DMA_FROMDEVICE);
  1061. if (!skb) {
  1062. kfree(data);
  1063. bnxt_abort_tpa(bp, bnapi, cp_cons, agg_bufs);
  1064. return NULL;
  1065. }
  1066. skb_reserve(skb, BNXT_RX_OFFSET);
  1067. skb_put(skb, len);
  1068. }
  1069. if (agg_bufs) {
  1070. skb = bnxt_rx_pages(bp, bnapi, skb, cp_cons, agg_bufs);
  1071. if (!skb) {
  1072. /* Page reuse already handled by bnxt_rx_pages(). */
  1073. return NULL;
  1074. }
  1075. }
  1076. skb->protocol = eth_type_trans(skb, bp->dev);
  1077. if (tpa_info->hash_type != PKT_HASH_TYPE_NONE)
  1078. skb_set_hash(skb, tpa_info->rss_hash, tpa_info->hash_type);
  1079. if ((tpa_info->flags2 & RX_CMP_FLAGS2_META_FORMAT_VLAN) &&
  1080. (skb->dev->features & NETIF_F_HW_VLAN_CTAG_RX)) {
  1081. u16 vlan_proto = tpa_info->metadata >>
  1082. RX_CMP_FLAGS2_METADATA_TPID_SFT;
  1083. u16 vtag = tpa_info->metadata & RX_CMP_FLAGS2_METADATA_VID_MASK;
  1084. __vlan_hwaccel_put_tag(skb, htons(vlan_proto), vtag);
  1085. }
  1086. skb_checksum_none_assert(skb);
  1087. if (likely(tpa_info->flags2 & RX_TPA_START_CMP_FLAGS2_L4_CS_CALC)) {
  1088. skb->ip_summed = CHECKSUM_UNNECESSARY;
  1089. skb->csum_level =
  1090. (tpa_info->flags2 & RX_CMP_FLAGS2_T_L4_CS_CALC) >> 3;
  1091. }
  1092. if (TPA_END_GRO(tpa_end))
  1093. skb = bnxt_gro_skb(bp, tpa_info, tpa_end, tpa_end1, skb);
  1094. return skb;
  1095. }
  1096. /* returns the following:
  1097. * 1 - 1 packet successfully received
  1098. * 0 - successful TPA_START, packet not completed yet
  1099. * -EBUSY - completion ring does not have all the agg buffers yet
  1100. * -ENOMEM - packet aborted due to out of memory
  1101. * -EIO - packet aborted due to hw error indicated in BD
  1102. */
  1103. static int bnxt_rx_pkt(struct bnxt *bp, struct bnxt_napi *bnapi, u32 *raw_cons,
  1104. bool *agg_event)
  1105. {
  1106. struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
  1107. struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
  1108. struct net_device *dev = bp->dev;
  1109. struct rx_cmp *rxcmp;
  1110. struct rx_cmp_ext *rxcmp1;
  1111. u32 tmp_raw_cons = *raw_cons;
  1112. u16 cons, prod, cp_cons = RING_CMP(tmp_raw_cons);
  1113. struct bnxt_sw_rx_bd *rx_buf;
  1114. unsigned int len;
  1115. u8 *data, agg_bufs, cmp_type;
  1116. dma_addr_t dma_addr;
  1117. struct sk_buff *skb;
  1118. int rc = 0;
  1119. rxcmp = (struct rx_cmp *)
  1120. &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
  1121. tmp_raw_cons = NEXT_RAW_CMP(tmp_raw_cons);
  1122. cp_cons = RING_CMP(tmp_raw_cons);
  1123. rxcmp1 = (struct rx_cmp_ext *)
  1124. &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
  1125. if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
  1126. return -EBUSY;
  1127. cmp_type = RX_CMP_TYPE(rxcmp);
  1128. prod = rxr->rx_prod;
  1129. if (cmp_type == CMP_TYPE_RX_L2_TPA_START_CMP) {
  1130. bnxt_tpa_start(bp, rxr, (struct rx_tpa_start_cmp *)rxcmp,
  1131. (struct rx_tpa_start_cmp_ext *)rxcmp1);
  1132. goto next_rx_no_prod;
  1133. } else if (cmp_type == CMP_TYPE_RX_L2_TPA_END_CMP) {
  1134. skb = bnxt_tpa_end(bp, bnapi, &tmp_raw_cons,
  1135. (struct rx_tpa_end_cmp *)rxcmp,
  1136. (struct rx_tpa_end_cmp_ext *)rxcmp1,
  1137. agg_event);
  1138. if (unlikely(IS_ERR(skb)))
  1139. return -EBUSY;
  1140. rc = -ENOMEM;
  1141. if (likely(skb)) {
  1142. skb_record_rx_queue(skb, bnapi->index);
  1143. skb_mark_napi_id(skb, &bnapi->napi);
  1144. if (bnxt_busy_polling(bnapi))
  1145. netif_receive_skb(skb);
  1146. else
  1147. napi_gro_receive(&bnapi->napi, skb);
  1148. rc = 1;
  1149. }
  1150. goto next_rx_no_prod;
  1151. }
  1152. cons = rxcmp->rx_cmp_opaque;
  1153. rx_buf = &rxr->rx_buf_ring[cons];
  1154. data = rx_buf->data;
  1155. if (unlikely(cons != rxr->rx_next_cons)) {
  1156. int rc1 = bnxt_discard_rx(bp, bnapi, raw_cons, rxcmp);
  1157. bnxt_sched_reset(bp, rxr);
  1158. return rc1;
  1159. }
  1160. prefetch(data);
  1161. agg_bufs = (le32_to_cpu(rxcmp->rx_cmp_misc_v1) & RX_CMP_AGG_BUFS) >>
  1162. RX_CMP_AGG_BUFS_SHIFT;
  1163. if (agg_bufs) {
  1164. if (!bnxt_agg_bufs_valid(bp, cpr, agg_bufs, &tmp_raw_cons))
  1165. return -EBUSY;
  1166. cp_cons = NEXT_CMP(cp_cons);
  1167. *agg_event = true;
  1168. }
  1169. rx_buf->data = NULL;
  1170. if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L2_ERRORS) {
  1171. bnxt_reuse_rx_data(rxr, cons, data);
  1172. if (agg_bufs)
  1173. bnxt_reuse_rx_agg_bufs(bnapi, cp_cons, agg_bufs);
  1174. rc = -EIO;
  1175. goto next_rx;
  1176. }
  1177. len = le32_to_cpu(rxcmp->rx_cmp_len_flags_type) >> RX_CMP_LEN_SHIFT;
  1178. dma_addr = dma_unmap_addr(rx_buf, mapping);
  1179. if (len <= bp->rx_copy_thresh) {
  1180. skb = bnxt_copy_skb(bnapi, data, len, dma_addr);
  1181. bnxt_reuse_rx_data(rxr, cons, data);
  1182. if (!skb) {
  1183. rc = -ENOMEM;
  1184. goto next_rx;
  1185. }
  1186. } else {
  1187. skb = bnxt_rx_skb(bp, rxr, cons, prod, data, dma_addr, len);
  1188. if (!skb) {
  1189. rc = -ENOMEM;
  1190. goto next_rx;
  1191. }
  1192. }
  1193. if (agg_bufs) {
  1194. skb = bnxt_rx_pages(bp, bnapi, skb, cp_cons, agg_bufs);
  1195. if (!skb) {
  1196. rc = -ENOMEM;
  1197. goto next_rx;
  1198. }
  1199. }
  1200. if (RX_CMP_HASH_VALID(rxcmp)) {
  1201. u32 hash_type = RX_CMP_HASH_TYPE(rxcmp);
  1202. enum pkt_hash_types type = PKT_HASH_TYPE_L4;
  1203. /* RSS profiles 1 and 3 with extract code 0 for inner 4-tuple */
  1204. if (hash_type != 1 && hash_type != 3)
  1205. type = PKT_HASH_TYPE_L3;
  1206. skb_set_hash(skb, le32_to_cpu(rxcmp->rx_cmp_rss_hash), type);
  1207. }
  1208. skb->protocol = eth_type_trans(skb, dev);
  1209. if ((rxcmp1->rx_cmp_flags2 &
  1210. cpu_to_le32(RX_CMP_FLAGS2_META_FORMAT_VLAN)) &&
  1211. (skb->dev->features & NETIF_F_HW_VLAN_CTAG_RX)) {
  1212. u32 meta_data = le32_to_cpu(rxcmp1->rx_cmp_meta_data);
  1213. u16 vtag = meta_data & RX_CMP_FLAGS2_METADATA_VID_MASK;
  1214. u16 vlan_proto = meta_data >> RX_CMP_FLAGS2_METADATA_TPID_SFT;
  1215. __vlan_hwaccel_put_tag(skb, htons(vlan_proto), vtag);
  1216. }
  1217. skb_checksum_none_assert(skb);
  1218. if (RX_CMP_L4_CS_OK(rxcmp1)) {
  1219. if (dev->features & NETIF_F_RXCSUM) {
  1220. skb->ip_summed = CHECKSUM_UNNECESSARY;
  1221. skb->csum_level = RX_CMP_ENCAP(rxcmp1);
  1222. }
  1223. } else {
  1224. if (rxcmp1->rx_cmp_cfa_code_errors_v2 & RX_CMP_L4_CS_ERR_BITS) {
  1225. if (dev->features & NETIF_F_RXCSUM)
  1226. cpr->rx_l4_csum_errors++;
  1227. }
  1228. }
  1229. skb_record_rx_queue(skb, bnapi->index);
  1230. skb_mark_napi_id(skb, &bnapi->napi);
  1231. if (bnxt_busy_polling(bnapi))
  1232. netif_receive_skb(skb);
  1233. else
  1234. napi_gro_receive(&bnapi->napi, skb);
  1235. rc = 1;
  1236. next_rx:
  1237. rxr->rx_prod = NEXT_RX(prod);
  1238. rxr->rx_next_cons = NEXT_RX(cons);
  1239. next_rx_no_prod:
  1240. *raw_cons = tmp_raw_cons;
  1241. return rc;
  1242. }
  1243. #define BNXT_GET_EVENT_PORT(data) \
  1244. ((data) & \
  1245. ASYNC_EVENT_CMPL_PORT_CONN_NOT_ALLOWED_EVENT_DATA1_PORT_ID_MASK)
  1246. static int bnxt_async_event_process(struct bnxt *bp,
  1247. struct hwrm_async_event_cmpl *cmpl)
  1248. {
  1249. u16 event_id = le16_to_cpu(cmpl->event_id);
  1250. /* TODO CHIMP_FW: Define event id's for link change, error etc */
  1251. switch (event_id) {
  1252. case ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE: {
  1253. u32 data1 = le32_to_cpu(cmpl->event_data1);
  1254. struct bnxt_link_info *link_info = &bp->link_info;
  1255. if (BNXT_VF(bp))
  1256. goto async_event_process_exit;
  1257. if (data1 & 0x20000) {
  1258. u16 fw_speed = link_info->force_link_speed;
  1259. u32 speed = bnxt_fw_to_ethtool_speed(fw_speed);
  1260. netdev_warn(bp->dev, "Link speed %d no longer supported\n",
  1261. speed);
  1262. }
  1263. set_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT, &bp->sp_event);
  1264. /* fall thru */
  1265. }
  1266. case ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE:
  1267. set_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event);
  1268. break;
  1269. case ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD:
  1270. set_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event);
  1271. break;
  1272. case ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED: {
  1273. u32 data1 = le32_to_cpu(cmpl->event_data1);
  1274. u16 port_id = BNXT_GET_EVENT_PORT(data1);
  1275. if (BNXT_VF(bp))
  1276. break;
  1277. if (bp->pf.port_id != port_id)
  1278. break;
  1279. set_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event);
  1280. break;
  1281. }
  1282. case ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE:
  1283. if (BNXT_PF(bp))
  1284. goto async_event_process_exit;
  1285. set_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event);
  1286. break;
  1287. default:
  1288. goto async_event_process_exit;
  1289. }
  1290. schedule_work(&bp->sp_task);
  1291. async_event_process_exit:
  1292. bnxt_ulp_async_events(bp, cmpl);
  1293. return 0;
  1294. }
  1295. static int bnxt_hwrm_handler(struct bnxt *bp, struct tx_cmp *txcmp)
  1296. {
  1297. u16 cmpl_type = TX_CMP_TYPE(txcmp), vf_id, seq_id;
  1298. struct hwrm_cmpl *h_cmpl = (struct hwrm_cmpl *)txcmp;
  1299. struct hwrm_fwd_req_cmpl *fwd_req_cmpl =
  1300. (struct hwrm_fwd_req_cmpl *)txcmp;
  1301. switch (cmpl_type) {
  1302. case CMPL_BASE_TYPE_HWRM_DONE:
  1303. seq_id = le16_to_cpu(h_cmpl->sequence_id);
  1304. if (seq_id == bp->hwrm_intr_seq_id)
  1305. bp->hwrm_intr_seq_id = HWRM_SEQ_ID_INVALID;
  1306. else
  1307. netdev_err(bp->dev, "Invalid hwrm seq id %d\n", seq_id);
  1308. break;
  1309. case CMPL_BASE_TYPE_HWRM_FWD_REQ:
  1310. vf_id = le16_to_cpu(fwd_req_cmpl->source_id);
  1311. if ((vf_id < bp->pf.first_vf_id) ||
  1312. (vf_id >= bp->pf.first_vf_id + bp->pf.active_vfs)) {
  1313. netdev_err(bp->dev, "Msg contains invalid VF id %x\n",
  1314. vf_id);
  1315. return -EINVAL;
  1316. }
  1317. set_bit(vf_id - bp->pf.first_vf_id, bp->pf.vf_event_bmap);
  1318. set_bit(BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT, &bp->sp_event);
  1319. schedule_work(&bp->sp_task);
  1320. break;
  1321. case CMPL_BASE_TYPE_HWRM_ASYNC_EVENT:
  1322. bnxt_async_event_process(bp,
  1323. (struct hwrm_async_event_cmpl *)txcmp);
  1324. default:
  1325. break;
  1326. }
  1327. return 0;
  1328. }
  1329. static irqreturn_t bnxt_msix(int irq, void *dev_instance)
  1330. {
  1331. struct bnxt_napi *bnapi = dev_instance;
  1332. struct bnxt *bp = bnapi->bp;
  1333. struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
  1334. u32 cons = RING_CMP(cpr->cp_raw_cons);
  1335. prefetch(&cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)]);
  1336. napi_schedule(&bnapi->napi);
  1337. return IRQ_HANDLED;
  1338. }
  1339. static inline int bnxt_has_work(struct bnxt *bp, struct bnxt_cp_ring_info *cpr)
  1340. {
  1341. u32 raw_cons = cpr->cp_raw_cons;
  1342. u16 cons = RING_CMP(raw_cons);
  1343. struct tx_cmp *txcmp;
  1344. txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
  1345. return TX_CMP_VALID(txcmp, raw_cons);
  1346. }
  1347. static irqreturn_t bnxt_inta(int irq, void *dev_instance)
  1348. {
  1349. struct bnxt_napi *bnapi = dev_instance;
  1350. struct bnxt *bp = bnapi->bp;
  1351. struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
  1352. u32 cons = RING_CMP(cpr->cp_raw_cons);
  1353. u32 int_status;
  1354. prefetch(&cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)]);
  1355. if (!bnxt_has_work(bp, cpr)) {
  1356. int_status = readl(bp->bar0 + BNXT_CAG_REG_LEGACY_INT_STATUS);
  1357. /* return if erroneous interrupt */
  1358. if (!(int_status & (0x10000 << cpr->cp_ring_struct.fw_ring_id)))
  1359. return IRQ_NONE;
  1360. }
  1361. /* disable ring IRQ */
  1362. BNXT_CP_DB_IRQ_DIS(cpr->cp_doorbell);
  1363. /* Return here if interrupt is shared and is disabled. */
  1364. if (unlikely(atomic_read(&bp->intr_sem) != 0))
  1365. return IRQ_HANDLED;
  1366. napi_schedule(&bnapi->napi);
  1367. return IRQ_HANDLED;
  1368. }
  1369. static int bnxt_poll_work(struct bnxt *bp, struct bnxt_napi *bnapi, int budget)
  1370. {
  1371. struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
  1372. u32 raw_cons = cpr->cp_raw_cons;
  1373. u32 cons;
  1374. int tx_pkts = 0;
  1375. int rx_pkts = 0;
  1376. bool rx_event = false;
  1377. bool agg_event = false;
  1378. struct tx_cmp *txcmp;
  1379. while (1) {
  1380. int rc;
  1381. cons = RING_CMP(raw_cons);
  1382. txcmp = &cpr->cp_desc_ring[CP_RING(cons)][CP_IDX(cons)];
  1383. if (!TX_CMP_VALID(txcmp, raw_cons))
  1384. break;
  1385. /* The valid test of the entry must be done first before
  1386. * reading any further.
  1387. */
  1388. dma_rmb();
  1389. if (TX_CMP_TYPE(txcmp) == CMP_TYPE_TX_L2_CMP) {
  1390. tx_pkts++;
  1391. /* return full budget so NAPI will complete. */
  1392. if (unlikely(tx_pkts > bp->tx_wake_thresh))
  1393. rx_pkts = budget;
  1394. } else if ((TX_CMP_TYPE(txcmp) & 0x30) == 0x10) {
  1395. rc = bnxt_rx_pkt(bp, bnapi, &raw_cons, &agg_event);
  1396. if (likely(rc >= 0))
  1397. rx_pkts += rc;
  1398. else if (rc == -EBUSY) /* partial completion */
  1399. break;
  1400. rx_event = true;
  1401. } else if (unlikely((TX_CMP_TYPE(txcmp) ==
  1402. CMPL_BASE_TYPE_HWRM_DONE) ||
  1403. (TX_CMP_TYPE(txcmp) ==
  1404. CMPL_BASE_TYPE_HWRM_FWD_REQ) ||
  1405. (TX_CMP_TYPE(txcmp) ==
  1406. CMPL_BASE_TYPE_HWRM_ASYNC_EVENT))) {
  1407. bnxt_hwrm_handler(bp, txcmp);
  1408. }
  1409. raw_cons = NEXT_RAW_CMP(raw_cons);
  1410. if (rx_pkts == budget)
  1411. break;
  1412. }
  1413. cpr->cp_raw_cons = raw_cons;
  1414. /* ACK completion ring before freeing tx ring and producing new
  1415. * buffers in rx/agg rings to prevent overflowing the completion
  1416. * ring.
  1417. */
  1418. BNXT_CP_DB(cpr->cp_doorbell, cpr->cp_raw_cons);
  1419. if (tx_pkts)
  1420. bnxt_tx_int(bp, bnapi, tx_pkts);
  1421. if (rx_event) {
  1422. struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
  1423. writel(DB_KEY_RX | rxr->rx_prod, rxr->rx_doorbell);
  1424. writel(DB_KEY_RX | rxr->rx_prod, rxr->rx_doorbell);
  1425. if (agg_event) {
  1426. writel(DB_KEY_RX | rxr->rx_agg_prod,
  1427. rxr->rx_agg_doorbell);
  1428. writel(DB_KEY_RX | rxr->rx_agg_prod,
  1429. rxr->rx_agg_doorbell);
  1430. }
  1431. }
  1432. return rx_pkts;
  1433. }
  1434. static int bnxt_poll_nitroa0(struct napi_struct *napi, int budget)
  1435. {
  1436. struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
  1437. struct bnxt *bp = bnapi->bp;
  1438. struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
  1439. struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
  1440. struct tx_cmp *txcmp;
  1441. struct rx_cmp_ext *rxcmp1;
  1442. u32 cp_cons, tmp_raw_cons;
  1443. u32 raw_cons = cpr->cp_raw_cons;
  1444. u32 rx_pkts = 0;
  1445. bool agg_event = false;
  1446. while (1) {
  1447. int rc;
  1448. cp_cons = RING_CMP(raw_cons);
  1449. txcmp = &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
  1450. if (!TX_CMP_VALID(txcmp, raw_cons))
  1451. break;
  1452. if ((TX_CMP_TYPE(txcmp) & 0x30) == 0x10) {
  1453. tmp_raw_cons = NEXT_RAW_CMP(raw_cons);
  1454. cp_cons = RING_CMP(tmp_raw_cons);
  1455. rxcmp1 = (struct rx_cmp_ext *)
  1456. &cpr->cp_desc_ring[CP_RING(cp_cons)][CP_IDX(cp_cons)];
  1457. if (!RX_CMP_VALID(rxcmp1, tmp_raw_cons))
  1458. break;
  1459. /* force an error to recycle the buffer */
  1460. rxcmp1->rx_cmp_cfa_code_errors_v2 |=
  1461. cpu_to_le32(RX_CMPL_ERRORS_CRC_ERROR);
  1462. rc = bnxt_rx_pkt(bp, bnapi, &raw_cons, &agg_event);
  1463. if (likely(rc == -EIO))
  1464. rx_pkts++;
  1465. else if (rc == -EBUSY) /* partial completion */
  1466. break;
  1467. } else if (unlikely(TX_CMP_TYPE(txcmp) ==
  1468. CMPL_BASE_TYPE_HWRM_DONE)) {
  1469. bnxt_hwrm_handler(bp, txcmp);
  1470. } else {
  1471. netdev_err(bp->dev,
  1472. "Invalid completion received on special ring\n");
  1473. }
  1474. raw_cons = NEXT_RAW_CMP(raw_cons);
  1475. if (rx_pkts == budget)
  1476. break;
  1477. }
  1478. cpr->cp_raw_cons = raw_cons;
  1479. BNXT_CP_DB(cpr->cp_doorbell, cpr->cp_raw_cons);
  1480. writel(DB_KEY_RX | rxr->rx_prod, rxr->rx_doorbell);
  1481. writel(DB_KEY_RX | rxr->rx_prod, rxr->rx_doorbell);
  1482. if (agg_event) {
  1483. writel(DB_KEY_RX | rxr->rx_agg_prod, rxr->rx_agg_doorbell);
  1484. writel(DB_KEY_RX | rxr->rx_agg_prod, rxr->rx_agg_doorbell);
  1485. }
  1486. if (!bnxt_has_work(bp, cpr) && rx_pkts < budget) {
  1487. napi_complete(napi);
  1488. BNXT_CP_DB_REARM(cpr->cp_doorbell, cpr->cp_raw_cons);
  1489. }
  1490. return rx_pkts;
  1491. }
  1492. static int bnxt_poll(struct napi_struct *napi, int budget)
  1493. {
  1494. struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
  1495. struct bnxt *bp = bnapi->bp;
  1496. struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
  1497. int work_done = 0;
  1498. if (!bnxt_lock_napi(bnapi))
  1499. return budget;
  1500. while (1) {
  1501. work_done += bnxt_poll_work(bp, bnapi, budget - work_done);
  1502. if (work_done >= budget)
  1503. break;
  1504. if (!bnxt_has_work(bp, cpr)) {
  1505. napi_complete(napi);
  1506. BNXT_CP_DB_REARM(cpr->cp_doorbell, cpr->cp_raw_cons);
  1507. break;
  1508. }
  1509. }
  1510. mmiowb();
  1511. bnxt_unlock_napi(bnapi);
  1512. return work_done;
  1513. }
  1514. #ifdef CONFIG_NET_RX_BUSY_POLL
  1515. static int bnxt_busy_poll(struct napi_struct *napi)
  1516. {
  1517. struct bnxt_napi *bnapi = container_of(napi, struct bnxt_napi, napi);
  1518. struct bnxt *bp = bnapi->bp;
  1519. struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
  1520. int rx_work, budget = 4;
  1521. if (atomic_read(&bp->intr_sem) != 0)
  1522. return LL_FLUSH_FAILED;
  1523. if (!bp->link_info.link_up)
  1524. return LL_FLUSH_FAILED;
  1525. if (!bnxt_lock_poll(bnapi))
  1526. return LL_FLUSH_BUSY;
  1527. rx_work = bnxt_poll_work(bp, bnapi, budget);
  1528. BNXT_CP_DB_REARM(cpr->cp_doorbell, cpr->cp_raw_cons);
  1529. bnxt_unlock_poll(bnapi);
  1530. return rx_work;
  1531. }
  1532. #endif
  1533. static void bnxt_free_tx_skbs(struct bnxt *bp)
  1534. {
  1535. int i, max_idx;
  1536. struct pci_dev *pdev = bp->pdev;
  1537. if (!bp->tx_ring)
  1538. return;
  1539. max_idx = bp->tx_nr_pages * TX_DESC_CNT;
  1540. for (i = 0; i < bp->tx_nr_rings; i++) {
  1541. struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
  1542. int j;
  1543. for (j = 0; j < max_idx;) {
  1544. struct bnxt_sw_tx_bd *tx_buf = &txr->tx_buf_ring[j];
  1545. struct sk_buff *skb = tx_buf->skb;
  1546. int k, last;
  1547. if (!skb) {
  1548. j++;
  1549. continue;
  1550. }
  1551. tx_buf->skb = NULL;
  1552. if (tx_buf->is_push) {
  1553. dev_kfree_skb(skb);
  1554. j += 2;
  1555. continue;
  1556. }
  1557. dma_unmap_single(&pdev->dev,
  1558. dma_unmap_addr(tx_buf, mapping),
  1559. skb_headlen(skb),
  1560. PCI_DMA_TODEVICE);
  1561. last = tx_buf->nr_frags;
  1562. j += 2;
  1563. for (k = 0; k < last; k++, j++) {
  1564. int ring_idx = j & bp->tx_ring_mask;
  1565. skb_frag_t *frag = &skb_shinfo(skb)->frags[k];
  1566. tx_buf = &txr->tx_buf_ring[ring_idx];
  1567. dma_unmap_page(
  1568. &pdev->dev,
  1569. dma_unmap_addr(tx_buf, mapping),
  1570. skb_frag_size(frag), PCI_DMA_TODEVICE);
  1571. }
  1572. dev_kfree_skb(skb);
  1573. }
  1574. netdev_tx_reset_queue(netdev_get_tx_queue(bp->dev, i));
  1575. }
  1576. }
  1577. static void bnxt_free_rx_skbs(struct bnxt *bp)
  1578. {
  1579. int i, max_idx, max_agg_idx;
  1580. struct pci_dev *pdev = bp->pdev;
  1581. if (!bp->rx_ring)
  1582. return;
  1583. max_idx = bp->rx_nr_pages * RX_DESC_CNT;
  1584. max_agg_idx = bp->rx_agg_nr_pages * RX_DESC_CNT;
  1585. for (i = 0; i < bp->rx_nr_rings; i++) {
  1586. struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
  1587. int j;
  1588. if (rxr->rx_tpa) {
  1589. for (j = 0; j < MAX_TPA; j++) {
  1590. struct bnxt_tpa_info *tpa_info =
  1591. &rxr->rx_tpa[j];
  1592. u8 *data = tpa_info->data;
  1593. if (!data)
  1594. continue;
  1595. dma_unmap_single(
  1596. &pdev->dev,
  1597. dma_unmap_addr(tpa_info, mapping),
  1598. bp->rx_buf_use_size,
  1599. PCI_DMA_FROMDEVICE);
  1600. tpa_info->data = NULL;
  1601. kfree(data);
  1602. }
  1603. }
  1604. for (j = 0; j < max_idx; j++) {
  1605. struct bnxt_sw_rx_bd *rx_buf = &rxr->rx_buf_ring[j];
  1606. u8 *data = rx_buf->data;
  1607. if (!data)
  1608. continue;
  1609. dma_unmap_single(&pdev->dev,
  1610. dma_unmap_addr(rx_buf, mapping),
  1611. bp->rx_buf_use_size,
  1612. PCI_DMA_FROMDEVICE);
  1613. rx_buf->data = NULL;
  1614. kfree(data);
  1615. }
  1616. for (j = 0; j < max_agg_idx; j++) {
  1617. struct bnxt_sw_rx_agg_bd *rx_agg_buf =
  1618. &rxr->rx_agg_ring[j];
  1619. struct page *page = rx_agg_buf->page;
  1620. if (!page)
  1621. continue;
  1622. dma_unmap_page(&pdev->dev,
  1623. dma_unmap_addr(rx_agg_buf, mapping),
  1624. BNXT_RX_PAGE_SIZE, PCI_DMA_FROMDEVICE);
  1625. rx_agg_buf->page = NULL;
  1626. __clear_bit(j, rxr->rx_agg_bmap);
  1627. __free_page(page);
  1628. }
  1629. if (rxr->rx_page) {
  1630. __free_page(rxr->rx_page);
  1631. rxr->rx_page = NULL;
  1632. }
  1633. }
  1634. }
  1635. static void bnxt_free_skbs(struct bnxt *bp)
  1636. {
  1637. bnxt_free_tx_skbs(bp);
  1638. bnxt_free_rx_skbs(bp);
  1639. }
  1640. static void bnxt_free_ring(struct bnxt *bp, struct bnxt_ring_struct *ring)
  1641. {
  1642. struct pci_dev *pdev = bp->pdev;
  1643. int i;
  1644. for (i = 0; i < ring->nr_pages; i++) {
  1645. if (!ring->pg_arr[i])
  1646. continue;
  1647. dma_free_coherent(&pdev->dev, ring->page_size,
  1648. ring->pg_arr[i], ring->dma_arr[i]);
  1649. ring->pg_arr[i] = NULL;
  1650. }
  1651. if (ring->pg_tbl) {
  1652. dma_free_coherent(&pdev->dev, ring->nr_pages * 8,
  1653. ring->pg_tbl, ring->pg_tbl_map);
  1654. ring->pg_tbl = NULL;
  1655. }
  1656. if (ring->vmem_size && *ring->vmem) {
  1657. vfree(*ring->vmem);
  1658. *ring->vmem = NULL;
  1659. }
  1660. }
  1661. static int bnxt_alloc_ring(struct bnxt *bp, struct bnxt_ring_struct *ring)
  1662. {
  1663. int i;
  1664. struct pci_dev *pdev = bp->pdev;
  1665. if (ring->nr_pages > 1) {
  1666. ring->pg_tbl = dma_alloc_coherent(&pdev->dev,
  1667. ring->nr_pages * 8,
  1668. &ring->pg_tbl_map,
  1669. GFP_KERNEL);
  1670. if (!ring->pg_tbl)
  1671. return -ENOMEM;
  1672. }
  1673. for (i = 0; i < ring->nr_pages; i++) {
  1674. ring->pg_arr[i] = dma_alloc_coherent(&pdev->dev,
  1675. ring->page_size,
  1676. &ring->dma_arr[i],
  1677. GFP_KERNEL);
  1678. if (!ring->pg_arr[i])
  1679. return -ENOMEM;
  1680. if (ring->nr_pages > 1)
  1681. ring->pg_tbl[i] = cpu_to_le64(ring->dma_arr[i]);
  1682. }
  1683. if (ring->vmem_size) {
  1684. *ring->vmem = vzalloc(ring->vmem_size);
  1685. if (!(*ring->vmem))
  1686. return -ENOMEM;
  1687. }
  1688. return 0;
  1689. }
  1690. static void bnxt_free_rx_rings(struct bnxt *bp)
  1691. {
  1692. int i;
  1693. if (!bp->rx_ring)
  1694. return;
  1695. for (i = 0; i < bp->rx_nr_rings; i++) {
  1696. struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
  1697. struct bnxt_ring_struct *ring;
  1698. kfree(rxr->rx_tpa);
  1699. rxr->rx_tpa = NULL;
  1700. kfree(rxr->rx_agg_bmap);
  1701. rxr->rx_agg_bmap = NULL;
  1702. ring = &rxr->rx_ring_struct;
  1703. bnxt_free_ring(bp, ring);
  1704. ring = &rxr->rx_agg_ring_struct;
  1705. bnxt_free_ring(bp, ring);
  1706. }
  1707. }
  1708. static int bnxt_alloc_rx_rings(struct bnxt *bp)
  1709. {
  1710. int i, rc, agg_rings = 0, tpa_rings = 0;
  1711. if (!bp->rx_ring)
  1712. return -ENOMEM;
  1713. if (bp->flags & BNXT_FLAG_AGG_RINGS)
  1714. agg_rings = 1;
  1715. if (bp->flags & BNXT_FLAG_TPA)
  1716. tpa_rings = 1;
  1717. for (i = 0; i < bp->rx_nr_rings; i++) {
  1718. struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
  1719. struct bnxt_ring_struct *ring;
  1720. ring = &rxr->rx_ring_struct;
  1721. rc = bnxt_alloc_ring(bp, ring);
  1722. if (rc)
  1723. return rc;
  1724. if (agg_rings) {
  1725. u16 mem_size;
  1726. ring = &rxr->rx_agg_ring_struct;
  1727. rc = bnxt_alloc_ring(bp, ring);
  1728. if (rc)
  1729. return rc;
  1730. rxr->rx_agg_bmap_size = bp->rx_agg_ring_mask + 1;
  1731. mem_size = rxr->rx_agg_bmap_size / 8;
  1732. rxr->rx_agg_bmap = kzalloc(mem_size, GFP_KERNEL);
  1733. if (!rxr->rx_agg_bmap)
  1734. return -ENOMEM;
  1735. if (tpa_rings) {
  1736. rxr->rx_tpa = kcalloc(MAX_TPA,
  1737. sizeof(struct bnxt_tpa_info),
  1738. GFP_KERNEL);
  1739. if (!rxr->rx_tpa)
  1740. return -ENOMEM;
  1741. }
  1742. }
  1743. }
  1744. return 0;
  1745. }
  1746. static void bnxt_free_tx_rings(struct bnxt *bp)
  1747. {
  1748. int i;
  1749. struct pci_dev *pdev = bp->pdev;
  1750. if (!bp->tx_ring)
  1751. return;
  1752. for (i = 0; i < bp->tx_nr_rings; i++) {
  1753. struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
  1754. struct bnxt_ring_struct *ring;
  1755. if (txr->tx_push) {
  1756. dma_free_coherent(&pdev->dev, bp->tx_push_size,
  1757. txr->tx_push, txr->tx_push_mapping);
  1758. txr->tx_push = NULL;
  1759. }
  1760. ring = &txr->tx_ring_struct;
  1761. bnxt_free_ring(bp, ring);
  1762. }
  1763. }
  1764. static int bnxt_alloc_tx_rings(struct bnxt *bp)
  1765. {
  1766. int i, j, rc;
  1767. struct pci_dev *pdev = bp->pdev;
  1768. bp->tx_push_size = 0;
  1769. if (bp->tx_push_thresh) {
  1770. int push_size;
  1771. push_size = L1_CACHE_ALIGN(sizeof(struct tx_push_bd) +
  1772. bp->tx_push_thresh);
  1773. if (push_size > 256) {
  1774. push_size = 0;
  1775. bp->tx_push_thresh = 0;
  1776. }
  1777. bp->tx_push_size = push_size;
  1778. }
  1779. for (i = 0, j = 0; i < bp->tx_nr_rings; i++) {
  1780. struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
  1781. struct bnxt_ring_struct *ring;
  1782. ring = &txr->tx_ring_struct;
  1783. rc = bnxt_alloc_ring(bp, ring);
  1784. if (rc)
  1785. return rc;
  1786. if (bp->tx_push_size) {
  1787. dma_addr_t mapping;
  1788. /* One pre-allocated DMA buffer to backup
  1789. * TX push operation
  1790. */
  1791. txr->tx_push = dma_alloc_coherent(&pdev->dev,
  1792. bp->tx_push_size,
  1793. &txr->tx_push_mapping,
  1794. GFP_KERNEL);
  1795. if (!txr->tx_push)
  1796. return -ENOMEM;
  1797. mapping = txr->tx_push_mapping +
  1798. sizeof(struct tx_push_bd);
  1799. txr->data_mapping = cpu_to_le64(mapping);
  1800. memset(txr->tx_push, 0, sizeof(struct tx_push_bd));
  1801. }
  1802. ring->queue_id = bp->q_info[j].queue_id;
  1803. if (i % bp->tx_nr_rings_per_tc == (bp->tx_nr_rings_per_tc - 1))
  1804. j++;
  1805. }
  1806. return 0;
  1807. }
  1808. static void bnxt_free_cp_rings(struct bnxt *bp)
  1809. {
  1810. int i;
  1811. if (!bp->bnapi)
  1812. return;
  1813. for (i = 0; i < bp->cp_nr_rings; i++) {
  1814. struct bnxt_napi *bnapi = bp->bnapi[i];
  1815. struct bnxt_cp_ring_info *cpr;
  1816. struct bnxt_ring_struct *ring;
  1817. if (!bnapi)
  1818. continue;
  1819. cpr = &bnapi->cp_ring;
  1820. ring = &cpr->cp_ring_struct;
  1821. bnxt_free_ring(bp, ring);
  1822. }
  1823. }
  1824. static int bnxt_alloc_cp_rings(struct bnxt *bp)
  1825. {
  1826. int i, rc;
  1827. for (i = 0; i < bp->cp_nr_rings; i++) {
  1828. struct bnxt_napi *bnapi = bp->bnapi[i];
  1829. struct bnxt_cp_ring_info *cpr;
  1830. struct bnxt_ring_struct *ring;
  1831. if (!bnapi)
  1832. continue;
  1833. cpr = &bnapi->cp_ring;
  1834. ring = &cpr->cp_ring_struct;
  1835. rc = bnxt_alloc_ring(bp, ring);
  1836. if (rc)
  1837. return rc;
  1838. }
  1839. return 0;
  1840. }
  1841. static void bnxt_init_ring_struct(struct bnxt *bp)
  1842. {
  1843. int i;
  1844. for (i = 0; i < bp->cp_nr_rings; i++) {
  1845. struct bnxt_napi *bnapi = bp->bnapi[i];
  1846. struct bnxt_cp_ring_info *cpr;
  1847. struct bnxt_rx_ring_info *rxr;
  1848. struct bnxt_tx_ring_info *txr;
  1849. struct bnxt_ring_struct *ring;
  1850. if (!bnapi)
  1851. continue;
  1852. cpr = &bnapi->cp_ring;
  1853. ring = &cpr->cp_ring_struct;
  1854. ring->nr_pages = bp->cp_nr_pages;
  1855. ring->page_size = HW_CMPD_RING_SIZE;
  1856. ring->pg_arr = (void **)cpr->cp_desc_ring;
  1857. ring->dma_arr = cpr->cp_desc_mapping;
  1858. ring->vmem_size = 0;
  1859. rxr = bnapi->rx_ring;
  1860. if (!rxr)
  1861. goto skip_rx;
  1862. ring = &rxr->rx_ring_struct;
  1863. ring->nr_pages = bp->rx_nr_pages;
  1864. ring->page_size = HW_RXBD_RING_SIZE;
  1865. ring->pg_arr = (void **)rxr->rx_desc_ring;
  1866. ring->dma_arr = rxr->rx_desc_mapping;
  1867. ring->vmem_size = SW_RXBD_RING_SIZE * bp->rx_nr_pages;
  1868. ring->vmem = (void **)&rxr->rx_buf_ring;
  1869. ring = &rxr->rx_agg_ring_struct;
  1870. ring->nr_pages = bp->rx_agg_nr_pages;
  1871. ring->page_size = HW_RXBD_RING_SIZE;
  1872. ring->pg_arr = (void **)rxr->rx_agg_desc_ring;
  1873. ring->dma_arr = rxr->rx_agg_desc_mapping;
  1874. ring->vmem_size = SW_RXBD_AGG_RING_SIZE * bp->rx_agg_nr_pages;
  1875. ring->vmem = (void **)&rxr->rx_agg_ring;
  1876. skip_rx:
  1877. txr = bnapi->tx_ring;
  1878. if (!txr)
  1879. continue;
  1880. ring = &txr->tx_ring_struct;
  1881. ring->nr_pages = bp->tx_nr_pages;
  1882. ring->page_size = HW_RXBD_RING_SIZE;
  1883. ring->pg_arr = (void **)txr->tx_desc_ring;
  1884. ring->dma_arr = txr->tx_desc_mapping;
  1885. ring->vmem_size = SW_TXBD_RING_SIZE * bp->tx_nr_pages;
  1886. ring->vmem = (void **)&txr->tx_buf_ring;
  1887. }
  1888. }
  1889. static void bnxt_init_rxbd_pages(struct bnxt_ring_struct *ring, u32 type)
  1890. {
  1891. int i;
  1892. u32 prod;
  1893. struct rx_bd **rx_buf_ring;
  1894. rx_buf_ring = (struct rx_bd **)ring->pg_arr;
  1895. for (i = 0, prod = 0; i < ring->nr_pages; i++) {
  1896. int j;
  1897. struct rx_bd *rxbd;
  1898. rxbd = rx_buf_ring[i];
  1899. if (!rxbd)
  1900. continue;
  1901. for (j = 0; j < RX_DESC_CNT; j++, rxbd++, prod++) {
  1902. rxbd->rx_bd_len_flags_type = cpu_to_le32(type);
  1903. rxbd->rx_bd_opaque = prod;
  1904. }
  1905. }
  1906. }
  1907. static int bnxt_init_one_rx_ring(struct bnxt *bp, int ring_nr)
  1908. {
  1909. struct net_device *dev = bp->dev;
  1910. struct bnxt_rx_ring_info *rxr;
  1911. struct bnxt_ring_struct *ring;
  1912. u32 prod, type;
  1913. int i;
  1914. type = (bp->rx_buf_use_size << RX_BD_LEN_SHIFT) |
  1915. RX_BD_TYPE_RX_PACKET_BD | RX_BD_FLAGS_EOP;
  1916. if (NET_IP_ALIGN == 2)
  1917. type |= RX_BD_FLAGS_SOP;
  1918. rxr = &bp->rx_ring[ring_nr];
  1919. ring = &rxr->rx_ring_struct;
  1920. bnxt_init_rxbd_pages(ring, type);
  1921. prod = rxr->rx_prod;
  1922. for (i = 0; i < bp->rx_ring_size; i++) {
  1923. if (bnxt_alloc_rx_data(bp, rxr, prod, GFP_KERNEL) != 0) {
  1924. netdev_warn(dev, "init'ed rx ring %d with %d/%d skbs only\n",
  1925. ring_nr, i, bp->rx_ring_size);
  1926. break;
  1927. }
  1928. prod = NEXT_RX(prod);
  1929. }
  1930. rxr->rx_prod = prod;
  1931. ring->fw_ring_id = INVALID_HW_RING_ID;
  1932. ring = &rxr->rx_agg_ring_struct;
  1933. ring->fw_ring_id = INVALID_HW_RING_ID;
  1934. if (!(bp->flags & BNXT_FLAG_AGG_RINGS))
  1935. return 0;
  1936. type = ((u32)BNXT_RX_PAGE_SIZE << RX_BD_LEN_SHIFT) |
  1937. RX_BD_TYPE_RX_AGG_BD | RX_BD_FLAGS_SOP;
  1938. bnxt_init_rxbd_pages(ring, type);
  1939. prod = rxr->rx_agg_prod;
  1940. for (i = 0; i < bp->rx_agg_ring_size; i++) {
  1941. if (bnxt_alloc_rx_page(bp, rxr, prod, GFP_KERNEL) != 0) {
  1942. netdev_warn(dev, "init'ed rx ring %d with %d/%d pages only\n",
  1943. ring_nr, i, bp->rx_ring_size);
  1944. break;
  1945. }
  1946. prod = NEXT_RX_AGG(prod);
  1947. }
  1948. rxr->rx_agg_prod = prod;
  1949. if (bp->flags & BNXT_FLAG_TPA) {
  1950. if (rxr->rx_tpa) {
  1951. u8 *data;
  1952. dma_addr_t mapping;
  1953. for (i = 0; i < MAX_TPA; i++) {
  1954. data = __bnxt_alloc_rx_data(bp, &mapping,
  1955. GFP_KERNEL);
  1956. if (!data)
  1957. return -ENOMEM;
  1958. rxr->rx_tpa[i].data = data;
  1959. rxr->rx_tpa[i].mapping = mapping;
  1960. }
  1961. } else {
  1962. netdev_err(bp->dev, "No resource allocated for LRO/GRO\n");
  1963. return -ENOMEM;
  1964. }
  1965. }
  1966. return 0;
  1967. }
  1968. static int bnxt_init_rx_rings(struct bnxt *bp)
  1969. {
  1970. int i, rc = 0;
  1971. for (i = 0; i < bp->rx_nr_rings; i++) {
  1972. rc = bnxt_init_one_rx_ring(bp, i);
  1973. if (rc)
  1974. break;
  1975. }
  1976. return rc;
  1977. }
  1978. static int bnxt_init_tx_rings(struct bnxt *bp)
  1979. {
  1980. u16 i;
  1981. bp->tx_wake_thresh = max_t(int, bp->tx_ring_size / 2,
  1982. MAX_SKB_FRAGS + 1);
  1983. for (i = 0; i < bp->tx_nr_rings; i++) {
  1984. struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
  1985. struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
  1986. ring->fw_ring_id = INVALID_HW_RING_ID;
  1987. }
  1988. return 0;
  1989. }
  1990. static void bnxt_free_ring_grps(struct bnxt *bp)
  1991. {
  1992. kfree(bp->grp_info);
  1993. bp->grp_info = NULL;
  1994. }
  1995. static int bnxt_init_ring_grps(struct bnxt *bp, bool irq_re_init)
  1996. {
  1997. int i;
  1998. if (irq_re_init) {
  1999. bp->grp_info = kcalloc(bp->cp_nr_rings,
  2000. sizeof(struct bnxt_ring_grp_info),
  2001. GFP_KERNEL);
  2002. if (!bp->grp_info)
  2003. return -ENOMEM;
  2004. }
  2005. for (i = 0; i < bp->cp_nr_rings; i++) {
  2006. if (irq_re_init)
  2007. bp->grp_info[i].fw_stats_ctx = INVALID_HW_RING_ID;
  2008. bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
  2009. bp->grp_info[i].rx_fw_ring_id = INVALID_HW_RING_ID;
  2010. bp->grp_info[i].agg_fw_ring_id = INVALID_HW_RING_ID;
  2011. bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
  2012. }
  2013. return 0;
  2014. }
  2015. static void bnxt_free_vnics(struct bnxt *bp)
  2016. {
  2017. kfree(bp->vnic_info);
  2018. bp->vnic_info = NULL;
  2019. bp->nr_vnics = 0;
  2020. }
  2021. static int bnxt_alloc_vnics(struct bnxt *bp)
  2022. {
  2023. int num_vnics = 1;
  2024. #ifdef CONFIG_RFS_ACCEL
  2025. if (bp->flags & BNXT_FLAG_RFS)
  2026. num_vnics += bp->rx_nr_rings;
  2027. #endif
  2028. if (BNXT_CHIP_TYPE_NITRO_A0(bp))
  2029. num_vnics++;
  2030. bp->vnic_info = kcalloc(num_vnics, sizeof(struct bnxt_vnic_info),
  2031. GFP_KERNEL);
  2032. if (!bp->vnic_info)
  2033. return -ENOMEM;
  2034. bp->nr_vnics = num_vnics;
  2035. return 0;
  2036. }
  2037. static void bnxt_init_vnics(struct bnxt *bp)
  2038. {
  2039. int i;
  2040. for (i = 0; i < bp->nr_vnics; i++) {
  2041. struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
  2042. vnic->fw_vnic_id = INVALID_HW_RING_ID;
  2043. vnic->fw_rss_cos_lb_ctx[0] = INVALID_HW_RING_ID;
  2044. vnic->fw_rss_cos_lb_ctx[1] = INVALID_HW_RING_ID;
  2045. vnic->fw_l2_ctx_id = INVALID_HW_RING_ID;
  2046. if (bp->vnic_info[i].rss_hash_key) {
  2047. if (i == 0)
  2048. prandom_bytes(vnic->rss_hash_key,
  2049. HW_HASH_KEY_SIZE);
  2050. else
  2051. memcpy(vnic->rss_hash_key,
  2052. bp->vnic_info[0].rss_hash_key,
  2053. HW_HASH_KEY_SIZE);
  2054. }
  2055. }
  2056. }
  2057. static int bnxt_calc_nr_ring_pages(u32 ring_size, int desc_per_pg)
  2058. {
  2059. int pages;
  2060. pages = ring_size / desc_per_pg;
  2061. if (!pages)
  2062. return 1;
  2063. pages++;
  2064. while (pages & (pages - 1))
  2065. pages++;
  2066. return pages;
  2067. }
  2068. static void bnxt_set_tpa_flags(struct bnxt *bp)
  2069. {
  2070. bp->flags &= ~BNXT_FLAG_TPA;
  2071. if (bp->dev->features & NETIF_F_LRO)
  2072. bp->flags |= BNXT_FLAG_LRO;
  2073. if (bp->dev->features & NETIF_F_GRO)
  2074. bp->flags |= BNXT_FLAG_GRO;
  2075. }
  2076. /* bp->rx_ring_size, bp->tx_ring_size, dev->mtu, BNXT_FLAG_{G|L}RO flags must
  2077. * be set on entry.
  2078. */
  2079. void bnxt_set_ring_params(struct bnxt *bp)
  2080. {
  2081. u32 ring_size, rx_size, rx_space;
  2082. u32 agg_factor = 0, agg_ring_size = 0;
  2083. /* 8 for CRC and VLAN */
  2084. rx_size = SKB_DATA_ALIGN(bp->dev->mtu + ETH_HLEN + NET_IP_ALIGN + 8);
  2085. rx_space = rx_size + NET_SKB_PAD +
  2086. SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
  2087. bp->rx_copy_thresh = BNXT_RX_COPY_THRESH;
  2088. ring_size = bp->rx_ring_size;
  2089. bp->rx_agg_ring_size = 0;
  2090. bp->rx_agg_nr_pages = 0;
  2091. if (bp->flags & BNXT_FLAG_TPA)
  2092. agg_factor = min_t(u32, 4, 65536 / BNXT_RX_PAGE_SIZE);
  2093. bp->flags &= ~BNXT_FLAG_JUMBO;
  2094. if (rx_space > PAGE_SIZE) {
  2095. u32 jumbo_factor;
  2096. bp->flags |= BNXT_FLAG_JUMBO;
  2097. jumbo_factor = PAGE_ALIGN(bp->dev->mtu - 40) >> PAGE_SHIFT;
  2098. if (jumbo_factor > agg_factor)
  2099. agg_factor = jumbo_factor;
  2100. }
  2101. agg_ring_size = ring_size * agg_factor;
  2102. if (agg_ring_size) {
  2103. bp->rx_agg_nr_pages = bnxt_calc_nr_ring_pages(agg_ring_size,
  2104. RX_DESC_CNT);
  2105. if (bp->rx_agg_nr_pages > MAX_RX_AGG_PAGES) {
  2106. u32 tmp = agg_ring_size;
  2107. bp->rx_agg_nr_pages = MAX_RX_AGG_PAGES;
  2108. agg_ring_size = MAX_RX_AGG_PAGES * RX_DESC_CNT - 1;
  2109. netdev_warn(bp->dev, "rx agg ring size %d reduced to %d.\n",
  2110. tmp, agg_ring_size);
  2111. }
  2112. bp->rx_agg_ring_size = agg_ring_size;
  2113. bp->rx_agg_ring_mask = (bp->rx_agg_nr_pages * RX_DESC_CNT) - 1;
  2114. rx_size = SKB_DATA_ALIGN(BNXT_RX_COPY_THRESH + NET_IP_ALIGN);
  2115. rx_space = rx_size + NET_SKB_PAD +
  2116. SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
  2117. }
  2118. bp->rx_buf_use_size = rx_size;
  2119. bp->rx_buf_size = rx_space;
  2120. bp->rx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, RX_DESC_CNT);
  2121. bp->rx_ring_mask = (bp->rx_nr_pages * RX_DESC_CNT) - 1;
  2122. ring_size = bp->tx_ring_size;
  2123. bp->tx_nr_pages = bnxt_calc_nr_ring_pages(ring_size, TX_DESC_CNT);
  2124. bp->tx_ring_mask = (bp->tx_nr_pages * TX_DESC_CNT) - 1;
  2125. ring_size = bp->rx_ring_size * (2 + agg_factor) + bp->tx_ring_size;
  2126. bp->cp_ring_size = ring_size;
  2127. bp->cp_nr_pages = bnxt_calc_nr_ring_pages(ring_size, CP_DESC_CNT);
  2128. if (bp->cp_nr_pages > MAX_CP_PAGES) {
  2129. bp->cp_nr_pages = MAX_CP_PAGES;
  2130. bp->cp_ring_size = MAX_CP_PAGES * CP_DESC_CNT - 1;
  2131. netdev_warn(bp->dev, "completion ring size %d reduced to %d.\n",
  2132. ring_size, bp->cp_ring_size);
  2133. }
  2134. bp->cp_bit = bp->cp_nr_pages * CP_DESC_CNT;
  2135. bp->cp_ring_mask = bp->cp_bit - 1;
  2136. }
  2137. static void bnxt_free_vnic_attributes(struct bnxt *bp)
  2138. {
  2139. int i;
  2140. struct bnxt_vnic_info *vnic;
  2141. struct pci_dev *pdev = bp->pdev;
  2142. if (!bp->vnic_info)
  2143. return;
  2144. for (i = 0; i < bp->nr_vnics; i++) {
  2145. vnic = &bp->vnic_info[i];
  2146. kfree(vnic->fw_grp_ids);
  2147. vnic->fw_grp_ids = NULL;
  2148. kfree(vnic->uc_list);
  2149. vnic->uc_list = NULL;
  2150. if (vnic->mc_list) {
  2151. dma_free_coherent(&pdev->dev, vnic->mc_list_size,
  2152. vnic->mc_list, vnic->mc_list_mapping);
  2153. vnic->mc_list = NULL;
  2154. }
  2155. if (vnic->rss_table) {
  2156. dma_free_coherent(&pdev->dev, PAGE_SIZE,
  2157. vnic->rss_table,
  2158. vnic->rss_table_dma_addr);
  2159. vnic->rss_table = NULL;
  2160. }
  2161. vnic->rss_hash_key = NULL;
  2162. vnic->flags = 0;
  2163. }
  2164. }
  2165. static int bnxt_alloc_vnic_attributes(struct bnxt *bp)
  2166. {
  2167. int i, rc = 0, size;
  2168. struct bnxt_vnic_info *vnic;
  2169. struct pci_dev *pdev = bp->pdev;
  2170. int max_rings;
  2171. for (i = 0; i < bp->nr_vnics; i++) {
  2172. vnic = &bp->vnic_info[i];
  2173. if (vnic->flags & BNXT_VNIC_UCAST_FLAG) {
  2174. int mem_size = (BNXT_MAX_UC_ADDRS - 1) * ETH_ALEN;
  2175. if (mem_size > 0) {
  2176. vnic->uc_list = kmalloc(mem_size, GFP_KERNEL);
  2177. if (!vnic->uc_list) {
  2178. rc = -ENOMEM;
  2179. goto out;
  2180. }
  2181. }
  2182. }
  2183. if (vnic->flags & BNXT_VNIC_MCAST_FLAG) {
  2184. vnic->mc_list_size = BNXT_MAX_MC_ADDRS * ETH_ALEN;
  2185. vnic->mc_list =
  2186. dma_alloc_coherent(&pdev->dev,
  2187. vnic->mc_list_size,
  2188. &vnic->mc_list_mapping,
  2189. GFP_KERNEL);
  2190. if (!vnic->mc_list) {
  2191. rc = -ENOMEM;
  2192. goto out;
  2193. }
  2194. }
  2195. if (vnic->flags & BNXT_VNIC_RSS_FLAG)
  2196. max_rings = bp->rx_nr_rings;
  2197. else
  2198. max_rings = 1;
  2199. vnic->fw_grp_ids = kcalloc(max_rings, sizeof(u16), GFP_KERNEL);
  2200. if (!vnic->fw_grp_ids) {
  2201. rc = -ENOMEM;
  2202. goto out;
  2203. }
  2204. /* Allocate rss table and hash key */
  2205. vnic->rss_table = dma_alloc_coherent(&pdev->dev, PAGE_SIZE,
  2206. &vnic->rss_table_dma_addr,
  2207. GFP_KERNEL);
  2208. if (!vnic->rss_table) {
  2209. rc = -ENOMEM;
  2210. goto out;
  2211. }
  2212. size = L1_CACHE_ALIGN(HW_HASH_INDEX_SIZE * sizeof(u16));
  2213. vnic->rss_hash_key = ((void *)vnic->rss_table) + size;
  2214. vnic->rss_hash_key_dma_addr = vnic->rss_table_dma_addr + size;
  2215. }
  2216. return 0;
  2217. out:
  2218. return rc;
  2219. }
  2220. static void bnxt_free_hwrm_resources(struct bnxt *bp)
  2221. {
  2222. struct pci_dev *pdev = bp->pdev;
  2223. dma_free_coherent(&pdev->dev, PAGE_SIZE, bp->hwrm_cmd_resp_addr,
  2224. bp->hwrm_cmd_resp_dma_addr);
  2225. bp->hwrm_cmd_resp_addr = NULL;
  2226. if (bp->hwrm_dbg_resp_addr) {
  2227. dma_free_coherent(&pdev->dev, HWRM_DBG_REG_BUF_SIZE,
  2228. bp->hwrm_dbg_resp_addr,
  2229. bp->hwrm_dbg_resp_dma_addr);
  2230. bp->hwrm_dbg_resp_addr = NULL;
  2231. }
  2232. }
  2233. static int bnxt_alloc_hwrm_resources(struct bnxt *bp)
  2234. {
  2235. struct pci_dev *pdev = bp->pdev;
  2236. bp->hwrm_cmd_resp_addr = dma_alloc_coherent(&pdev->dev, PAGE_SIZE,
  2237. &bp->hwrm_cmd_resp_dma_addr,
  2238. GFP_KERNEL);
  2239. if (!bp->hwrm_cmd_resp_addr)
  2240. return -ENOMEM;
  2241. bp->hwrm_dbg_resp_addr = dma_alloc_coherent(&pdev->dev,
  2242. HWRM_DBG_REG_BUF_SIZE,
  2243. &bp->hwrm_dbg_resp_dma_addr,
  2244. GFP_KERNEL);
  2245. if (!bp->hwrm_dbg_resp_addr)
  2246. netdev_warn(bp->dev, "fail to alloc debug register dma mem\n");
  2247. return 0;
  2248. }
  2249. static void bnxt_free_stats(struct bnxt *bp)
  2250. {
  2251. u32 size, i;
  2252. struct pci_dev *pdev = bp->pdev;
  2253. if (bp->hw_rx_port_stats) {
  2254. dma_free_coherent(&pdev->dev, bp->hw_port_stats_size,
  2255. bp->hw_rx_port_stats,
  2256. bp->hw_rx_port_stats_map);
  2257. bp->hw_rx_port_stats = NULL;
  2258. bp->flags &= ~BNXT_FLAG_PORT_STATS;
  2259. }
  2260. if (!bp->bnapi)
  2261. return;
  2262. size = sizeof(struct ctx_hw_stats);
  2263. for (i = 0; i < bp->cp_nr_rings; i++) {
  2264. struct bnxt_napi *bnapi = bp->bnapi[i];
  2265. struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
  2266. if (cpr->hw_stats) {
  2267. dma_free_coherent(&pdev->dev, size, cpr->hw_stats,
  2268. cpr->hw_stats_map);
  2269. cpr->hw_stats = NULL;
  2270. }
  2271. }
  2272. }
  2273. static int bnxt_alloc_stats(struct bnxt *bp)
  2274. {
  2275. u32 size, i;
  2276. struct pci_dev *pdev = bp->pdev;
  2277. size = sizeof(struct ctx_hw_stats);
  2278. for (i = 0; i < bp->cp_nr_rings; i++) {
  2279. struct bnxt_napi *bnapi = bp->bnapi[i];
  2280. struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
  2281. cpr->hw_stats = dma_alloc_coherent(&pdev->dev, size,
  2282. &cpr->hw_stats_map,
  2283. GFP_KERNEL);
  2284. if (!cpr->hw_stats)
  2285. return -ENOMEM;
  2286. cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
  2287. }
  2288. if (BNXT_PF(bp) && bp->chip_num != CHIP_NUM_58700) {
  2289. bp->hw_port_stats_size = sizeof(struct rx_port_stats) +
  2290. sizeof(struct tx_port_stats) + 1024;
  2291. bp->hw_rx_port_stats =
  2292. dma_alloc_coherent(&pdev->dev, bp->hw_port_stats_size,
  2293. &bp->hw_rx_port_stats_map,
  2294. GFP_KERNEL);
  2295. if (!bp->hw_rx_port_stats)
  2296. return -ENOMEM;
  2297. bp->hw_tx_port_stats = (void *)(bp->hw_rx_port_stats + 1) +
  2298. 512;
  2299. bp->hw_tx_port_stats_map = bp->hw_rx_port_stats_map +
  2300. sizeof(struct rx_port_stats) + 512;
  2301. bp->flags |= BNXT_FLAG_PORT_STATS;
  2302. }
  2303. return 0;
  2304. }
  2305. static void bnxt_clear_ring_indices(struct bnxt *bp)
  2306. {
  2307. int i;
  2308. if (!bp->bnapi)
  2309. return;
  2310. for (i = 0; i < bp->cp_nr_rings; i++) {
  2311. struct bnxt_napi *bnapi = bp->bnapi[i];
  2312. struct bnxt_cp_ring_info *cpr;
  2313. struct bnxt_rx_ring_info *rxr;
  2314. struct bnxt_tx_ring_info *txr;
  2315. if (!bnapi)
  2316. continue;
  2317. cpr = &bnapi->cp_ring;
  2318. cpr->cp_raw_cons = 0;
  2319. txr = bnapi->tx_ring;
  2320. if (txr) {
  2321. txr->tx_prod = 0;
  2322. txr->tx_cons = 0;
  2323. }
  2324. rxr = bnapi->rx_ring;
  2325. if (rxr) {
  2326. rxr->rx_prod = 0;
  2327. rxr->rx_agg_prod = 0;
  2328. rxr->rx_sw_agg_prod = 0;
  2329. rxr->rx_next_cons = 0;
  2330. }
  2331. }
  2332. }
  2333. static void bnxt_free_ntp_fltrs(struct bnxt *bp, bool irq_reinit)
  2334. {
  2335. #ifdef CONFIG_RFS_ACCEL
  2336. int i;
  2337. /* Under rtnl_lock and all our NAPIs have been disabled. It's
  2338. * safe to delete the hash table.
  2339. */
  2340. for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
  2341. struct hlist_head *head;
  2342. struct hlist_node *tmp;
  2343. struct bnxt_ntuple_filter *fltr;
  2344. head = &bp->ntp_fltr_hash_tbl[i];
  2345. hlist_for_each_entry_safe(fltr, tmp, head, hash) {
  2346. hlist_del(&fltr->hash);
  2347. kfree(fltr);
  2348. }
  2349. }
  2350. if (irq_reinit) {
  2351. kfree(bp->ntp_fltr_bmap);
  2352. bp->ntp_fltr_bmap = NULL;
  2353. }
  2354. bp->ntp_fltr_count = 0;
  2355. #endif
  2356. }
  2357. static int bnxt_alloc_ntp_fltrs(struct bnxt *bp)
  2358. {
  2359. #ifdef CONFIG_RFS_ACCEL
  2360. int i, rc = 0;
  2361. if (!(bp->flags & BNXT_FLAG_RFS))
  2362. return 0;
  2363. for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++)
  2364. INIT_HLIST_HEAD(&bp->ntp_fltr_hash_tbl[i]);
  2365. bp->ntp_fltr_count = 0;
  2366. bp->ntp_fltr_bmap = kzalloc(BITS_TO_LONGS(BNXT_NTP_FLTR_MAX_FLTR),
  2367. GFP_KERNEL);
  2368. if (!bp->ntp_fltr_bmap)
  2369. rc = -ENOMEM;
  2370. return rc;
  2371. #else
  2372. return 0;
  2373. #endif
  2374. }
  2375. static void bnxt_free_mem(struct bnxt *bp, bool irq_re_init)
  2376. {
  2377. bnxt_free_vnic_attributes(bp);
  2378. bnxt_free_tx_rings(bp);
  2379. bnxt_free_rx_rings(bp);
  2380. bnxt_free_cp_rings(bp);
  2381. bnxt_free_ntp_fltrs(bp, irq_re_init);
  2382. if (irq_re_init) {
  2383. bnxt_free_stats(bp);
  2384. bnxt_free_ring_grps(bp);
  2385. bnxt_free_vnics(bp);
  2386. kfree(bp->tx_ring);
  2387. bp->tx_ring = NULL;
  2388. kfree(bp->rx_ring);
  2389. bp->rx_ring = NULL;
  2390. kfree(bp->bnapi);
  2391. bp->bnapi = NULL;
  2392. } else {
  2393. bnxt_clear_ring_indices(bp);
  2394. }
  2395. }
  2396. static int bnxt_alloc_mem(struct bnxt *bp, bool irq_re_init)
  2397. {
  2398. int i, j, rc, size, arr_size;
  2399. void *bnapi;
  2400. if (irq_re_init) {
  2401. /* Allocate bnapi mem pointer array and mem block for
  2402. * all queues
  2403. */
  2404. arr_size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi *) *
  2405. bp->cp_nr_rings);
  2406. size = L1_CACHE_ALIGN(sizeof(struct bnxt_napi));
  2407. bnapi = kzalloc(arr_size + size * bp->cp_nr_rings, GFP_KERNEL);
  2408. if (!bnapi)
  2409. return -ENOMEM;
  2410. bp->bnapi = bnapi;
  2411. bnapi += arr_size;
  2412. for (i = 0; i < bp->cp_nr_rings; i++, bnapi += size) {
  2413. bp->bnapi[i] = bnapi;
  2414. bp->bnapi[i]->index = i;
  2415. bp->bnapi[i]->bp = bp;
  2416. }
  2417. bp->rx_ring = kcalloc(bp->rx_nr_rings,
  2418. sizeof(struct bnxt_rx_ring_info),
  2419. GFP_KERNEL);
  2420. if (!bp->rx_ring)
  2421. return -ENOMEM;
  2422. for (i = 0; i < bp->rx_nr_rings; i++) {
  2423. bp->rx_ring[i].bnapi = bp->bnapi[i];
  2424. bp->bnapi[i]->rx_ring = &bp->rx_ring[i];
  2425. }
  2426. bp->tx_ring = kcalloc(bp->tx_nr_rings,
  2427. sizeof(struct bnxt_tx_ring_info),
  2428. GFP_KERNEL);
  2429. if (!bp->tx_ring)
  2430. return -ENOMEM;
  2431. if (bp->flags & BNXT_FLAG_SHARED_RINGS)
  2432. j = 0;
  2433. else
  2434. j = bp->rx_nr_rings;
  2435. for (i = 0; i < bp->tx_nr_rings; i++, j++) {
  2436. bp->tx_ring[i].bnapi = bp->bnapi[j];
  2437. bp->bnapi[j]->tx_ring = &bp->tx_ring[i];
  2438. }
  2439. rc = bnxt_alloc_stats(bp);
  2440. if (rc)
  2441. goto alloc_mem_err;
  2442. rc = bnxt_alloc_ntp_fltrs(bp);
  2443. if (rc)
  2444. goto alloc_mem_err;
  2445. rc = bnxt_alloc_vnics(bp);
  2446. if (rc)
  2447. goto alloc_mem_err;
  2448. }
  2449. bnxt_init_ring_struct(bp);
  2450. rc = bnxt_alloc_rx_rings(bp);
  2451. if (rc)
  2452. goto alloc_mem_err;
  2453. rc = bnxt_alloc_tx_rings(bp);
  2454. if (rc)
  2455. goto alloc_mem_err;
  2456. rc = bnxt_alloc_cp_rings(bp);
  2457. if (rc)
  2458. goto alloc_mem_err;
  2459. bp->vnic_info[0].flags |= BNXT_VNIC_RSS_FLAG | BNXT_VNIC_MCAST_FLAG |
  2460. BNXT_VNIC_UCAST_FLAG;
  2461. rc = bnxt_alloc_vnic_attributes(bp);
  2462. if (rc)
  2463. goto alloc_mem_err;
  2464. return 0;
  2465. alloc_mem_err:
  2466. bnxt_free_mem(bp, true);
  2467. return rc;
  2468. }
  2469. void bnxt_hwrm_cmd_hdr_init(struct bnxt *bp, void *request, u16 req_type,
  2470. u16 cmpl_ring, u16 target_id)
  2471. {
  2472. struct input *req = request;
  2473. req->req_type = cpu_to_le16(req_type);
  2474. req->cmpl_ring = cpu_to_le16(cmpl_ring);
  2475. req->target_id = cpu_to_le16(target_id);
  2476. req->resp_addr = cpu_to_le64(bp->hwrm_cmd_resp_dma_addr);
  2477. }
  2478. static int bnxt_hwrm_do_send_msg(struct bnxt *bp, void *msg, u32 msg_len,
  2479. int timeout, bool silent)
  2480. {
  2481. int i, intr_process, rc, tmo_count;
  2482. struct input *req = msg;
  2483. u32 *data = msg;
  2484. __le32 *resp_len, *valid;
  2485. u16 cp_ring_id, len = 0;
  2486. struct hwrm_err_output *resp = bp->hwrm_cmd_resp_addr;
  2487. req->seq_id = cpu_to_le16(bp->hwrm_cmd_seq++);
  2488. memset(resp, 0, PAGE_SIZE);
  2489. cp_ring_id = le16_to_cpu(req->cmpl_ring);
  2490. intr_process = (cp_ring_id == INVALID_HW_RING_ID) ? 0 : 1;
  2491. /* Write request msg to hwrm channel */
  2492. __iowrite32_copy(bp->bar0, data, msg_len / 4);
  2493. for (i = msg_len; i < BNXT_HWRM_MAX_REQ_LEN; i += 4)
  2494. writel(0, bp->bar0 + i);
  2495. /* currently supports only one outstanding message */
  2496. if (intr_process)
  2497. bp->hwrm_intr_seq_id = le16_to_cpu(req->seq_id);
  2498. /* Ring channel doorbell */
  2499. writel(1, bp->bar0 + 0x100);
  2500. if (!timeout)
  2501. timeout = DFLT_HWRM_CMD_TIMEOUT;
  2502. i = 0;
  2503. tmo_count = timeout * 40;
  2504. if (intr_process) {
  2505. /* Wait until hwrm response cmpl interrupt is processed */
  2506. while (bp->hwrm_intr_seq_id != HWRM_SEQ_ID_INVALID &&
  2507. i++ < tmo_count) {
  2508. usleep_range(25, 40);
  2509. }
  2510. if (bp->hwrm_intr_seq_id != HWRM_SEQ_ID_INVALID) {
  2511. netdev_err(bp->dev, "Resp cmpl intr err msg: 0x%x\n",
  2512. le16_to_cpu(req->req_type));
  2513. return -1;
  2514. }
  2515. } else {
  2516. /* Check if response len is updated */
  2517. resp_len = bp->hwrm_cmd_resp_addr + HWRM_RESP_LEN_OFFSET;
  2518. for (i = 0; i < tmo_count; i++) {
  2519. len = (le32_to_cpu(*resp_len) & HWRM_RESP_LEN_MASK) >>
  2520. HWRM_RESP_LEN_SFT;
  2521. if (len)
  2522. break;
  2523. usleep_range(25, 40);
  2524. }
  2525. if (i >= tmo_count) {
  2526. netdev_err(bp->dev, "Error (timeout: %d) msg {0x%x 0x%x} len:%d\n",
  2527. timeout, le16_to_cpu(req->req_type),
  2528. le16_to_cpu(req->seq_id), len);
  2529. return -1;
  2530. }
  2531. /* Last word of resp contains valid bit */
  2532. valid = bp->hwrm_cmd_resp_addr + len - 4;
  2533. for (i = 0; i < 5; i++) {
  2534. if (le32_to_cpu(*valid) & HWRM_RESP_VALID_MASK)
  2535. break;
  2536. udelay(1);
  2537. }
  2538. if (i >= 5) {
  2539. netdev_err(bp->dev, "Error (timeout: %d) msg {0x%x 0x%x} len:%d v:%d\n",
  2540. timeout, le16_to_cpu(req->req_type),
  2541. le16_to_cpu(req->seq_id), len, *valid);
  2542. return -1;
  2543. }
  2544. }
  2545. rc = le16_to_cpu(resp->error_code);
  2546. if (rc && !silent)
  2547. netdev_err(bp->dev, "hwrm req_type 0x%x seq id 0x%x error 0x%x\n",
  2548. le16_to_cpu(resp->req_type),
  2549. le16_to_cpu(resp->seq_id), rc);
  2550. return rc;
  2551. }
  2552. int _hwrm_send_message(struct bnxt *bp, void *msg, u32 msg_len, int timeout)
  2553. {
  2554. return bnxt_hwrm_do_send_msg(bp, msg, msg_len, timeout, false);
  2555. }
  2556. int hwrm_send_message(struct bnxt *bp, void *msg, u32 msg_len, int timeout)
  2557. {
  2558. int rc;
  2559. mutex_lock(&bp->hwrm_cmd_lock);
  2560. rc = _hwrm_send_message(bp, msg, msg_len, timeout);
  2561. mutex_unlock(&bp->hwrm_cmd_lock);
  2562. return rc;
  2563. }
  2564. int hwrm_send_message_silent(struct bnxt *bp, void *msg, u32 msg_len,
  2565. int timeout)
  2566. {
  2567. int rc;
  2568. mutex_lock(&bp->hwrm_cmd_lock);
  2569. rc = bnxt_hwrm_do_send_msg(bp, msg, msg_len, timeout, true);
  2570. mutex_unlock(&bp->hwrm_cmd_lock);
  2571. return rc;
  2572. }
  2573. int bnxt_hwrm_func_rgtr_async_events(struct bnxt *bp, unsigned long *bmap,
  2574. int bmap_size)
  2575. {
  2576. struct hwrm_func_drv_rgtr_input req = {0};
  2577. DECLARE_BITMAP(async_events_bmap, 256);
  2578. u32 *events = (u32 *)async_events_bmap;
  2579. int i;
  2580. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_DRV_RGTR, -1, -1);
  2581. req.enables =
  2582. cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_ASYNC_EVENT_FWD);
  2583. memset(async_events_bmap, 0, sizeof(async_events_bmap));
  2584. for (i = 0; i < ARRAY_SIZE(bnxt_async_events_arr); i++)
  2585. __set_bit(bnxt_async_events_arr[i], async_events_bmap);
  2586. if (bmap && bmap_size) {
  2587. for (i = 0; i < bmap_size; i++) {
  2588. if (test_bit(i, bmap))
  2589. __set_bit(i, async_events_bmap);
  2590. }
  2591. }
  2592. for (i = 0; i < 8; i++)
  2593. req.async_event_fwd[i] |= cpu_to_le32(events[i]);
  2594. return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  2595. }
  2596. static int bnxt_hwrm_func_drv_rgtr(struct bnxt *bp)
  2597. {
  2598. struct hwrm_func_drv_rgtr_input req = {0};
  2599. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_DRV_RGTR, -1, -1);
  2600. req.enables =
  2601. cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_OS_TYPE |
  2602. FUNC_DRV_RGTR_REQ_ENABLES_VER);
  2603. req.os_type = cpu_to_le16(FUNC_DRV_RGTR_REQ_OS_TYPE_LINUX);
  2604. req.ver_maj = DRV_VER_MAJ;
  2605. req.ver_min = DRV_VER_MIN;
  2606. req.ver_upd = DRV_VER_UPD;
  2607. if (BNXT_PF(bp)) {
  2608. DECLARE_BITMAP(vf_req_snif_bmap, 256);
  2609. u32 *data = (u32 *)vf_req_snif_bmap;
  2610. int i;
  2611. memset(vf_req_snif_bmap, 0, sizeof(vf_req_snif_bmap));
  2612. for (i = 0; i < ARRAY_SIZE(bnxt_vf_req_snif); i++)
  2613. __set_bit(bnxt_vf_req_snif[i], vf_req_snif_bmap);
  2614. for (i = 0; i < 8; i++)
  2615. req.vf_req_fwd[i] = cpu_to_le32(data[i]);
  2616. req.enables |=
  2617. cpu_to_le32(FUNC_DRV_RGTR_REQ_ENABLES_VF_REQ_FWD);
  2618. }
  2619. return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  2620. }
  2621. static int bnxt_hwrm_func_drv_unrgtr(struct bnxt *bp)
  2622. {
  2623. struct hwrm_func_drv_unrgtr_input req = {0};
  2624. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_DRV_UNRGTR, -1, -1);
  2625. return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  2626. }
  2627. static int bnxt_hwrm_tunnel_dst_port_free(struct bnxt *bp, u8 tunnel_type)
  2628. {
  2629. u32 rc = 0;
  2630. struct hwrm_tunnel_dst_port_free_input req = {0};
  2631. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_TUNNEL_DST_PORT_FREE, -1, -1);
  2632. req.tunnel_type = tunnel_type;
  2633. switch (tunnel_type) {
  2634. case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN:
  2635. req.tunnel_dst_port_id = bp->vxlan_fw_dst_port_id;
  2636. break;
  2637. case TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE:
  2638. req.tunnel_dst_port_id = bp->nge_fw_dst_port_id;
  2639. break;
  2640. default:
  2641. break;
  2642. }
  2643. rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  2644. if (rc)
  2645. netdev_err(bp->dev, "hwrm_tunnel_dst_port_free failed. rc:%d\n",
  2646. rc);
  2647. return rc;
  2648. }
  2649. static int bnxt_hwrm_tunnel_dst_port_alloc(struct bnxt *bp, __be16 port,
  2650. u8 tunnel_type)
  2651. {
  2652. u32 rc = 0;
  2653. struct hwrm_tunnel_dst_port_alloc_input req = {0};
  2654. struct hwrm_tunnel_dst_port_alloc_output *resp = bp->hwrm_cmd_resp_addr;
  2655. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_TUNNEL_DST_PORT_ALLOC, -1, -1);
  2656. req.tunnel_type = tunnel_type;
  2657. req.tunnel_dst_port_val = port;
  2658. mutex_lock(&bp->hwrm_cmd_lock);
  2659. rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  2660. if (rc) {
  2661. netdev_err(bp->dev, "hwrm_tunnel_dst_port_alloc failed. rc:%d\n",
  2662. rc);
  2663. goto err_out;
  2664. }
  2665. switch (tunnel_type) {
  2666. case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_VXLAN:
  2667. bp->vxlan_fw_dst_port_id = resp->tunnel_dst_port_id;
  2668. break;
  2669. case TUNNEL_DST_PORT_ALLOC_REQ_TUNNEL_TYPE_GENEVE:
  2670. bp->nge_fw_dst_port_id = resp->tunnel_dst_port_id;
  2671. break;
  2672. default:
  2673. break;
  2674. }
  2675. err_out:
  2676. mutex_unlock(&bp->hwrm_cmd_lock);
  2677. return rc;
  2678. }
  2679. static int bnxt_hwrm_cfa_l2_set_rx_mask(struct bnxt *bp, u16 vnic_id)
  2680. {
  2681. struct hwrm_cfa_l2_set_rx_mask_input req = {0};
  2682. struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
  2683. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_L2_SET_RX_MASK, -1, -1);
  2684. req.vnic_id = cpu_to_le32(vnic->fw_vnic_id);
  2685. req.num_mc_entries = cpu_to_le32(vnic->mc_list_count);
  2686. req.mc_tbl_addr = cpu_to_le64(vnic->mc_list_mapping);
  2687. req.mask = cpu_to_le32(vnic->rx_mask);
  2688. return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  2689. }
  2690. #ifdef CONFIG_RFS_ACCEL
  2691. static int bnxt_hwrm_cfa_ntuple_filter_free(struct bnxt *bp,
  2692. struct bnxt_ntuple_filter *fltr)
  2693. {
  2694. struct hwrm_cfa_ntuple_filter_free_input req = {0};
  2695. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_NTUPLE_FILTER_FREE, -1, -1);
  2696. req.ntuple_filter_id = fltr->filter_id;
  2697. return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  2698. }
  2699. #define BNXT_NTP_FLTR_FLAGS \
  2700. (CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_L2_FILTER_ID | \
  2701. CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_ETHERTYPE | \
  2702. CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_MACADDR | \
  2703. CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IPADDR_TYPE | \
  2704. CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR | \
  2705. CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_IPADDR_MASK | \
  2706. CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR | \
  2707. CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_IPADDR_MASK | \
  2708. CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_IP_PROTOCOL | \
  2709. CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT | \
  2710. CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_SRC_PORT_MASK | \
  2711. CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT | \
  2712. CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_PORT_MASK | \
  2713. CFA_NTUPLE_FILTER_ALLOC_REQ_ENABLES_DST_ID)
  2714. static int bnxt_hwrm_cfa_ntuple_filter_alloc(struct bnxt *bp,
  2715. struct bnxt_ntuple_filter *fltr)
  2716. {
  2717. int rc = 0;
  2718. struct hwrm_cfa_ntuple_filter_alloc_input req = {0};
  2719. struct hwrm_cfa_ntuple_filter_alloc_output *resp =
  2720. bp->hwrm_cmd_resp_addr;
  2721. struct flow_keys *keys = &fltr->fkeys;
  2722. struct bnxt_vnic_info *vnic = &bp->vnic_info[fltr->rxq + 1];
  2723. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_NTUPLE_FILTER_ALLOC, -1, -1);
  2724. req.l2_filter_id = bp->vnic_info[0].fw_l2_filter_id[fltr->l2_fltr_idx];
  2725. req.enables = cpu_to_le32(BNXT_NTP_FLTR_FLAGS);
  2726. req.ethertype = htons(ETH_P_IP);
  2727. memcpy(req.src_macaddr, fltr->src_mac_addr, ETH_ALEN);
  2728. req.ip_addr_type = CFA_NTUPLE_FILTER_ALLOC_REQ_IP_ADDR_TYPE_IPV4;
  2729. req.ip_protocol = keys->basic.ip_proto;
  2730. req.src_ipaddr[0] = keys->addrs.v4addrs.src;
  2731. req.src_ipaddr_mask[0] = cpu_to_be32(0xffffffff);
  2732. req.dst_ipaddr[0] = keys->addrs.v4addrs.dst;
  2733. req.dst_ipaddr_mask[0] = cpu_to_be32(0xffffffff);
  2734. req.src_port = keys->ports.src;
  2735. req.src_port_mask = cpu_to_be16(0xffff);
  2736. req.dst_port = keys->ports.dst;
  2737. req.dst_port_mask = cpu_to_be16(0xffff);
  2738. req.dst_id = cpu_to_le16(vnic->fw_vnic_id);
  2739. mutex_lock(&bp->hwrm_cmd_lock);
  2740. rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  2741. if (!rc)
  2742. fltr->filter_id = resp->ntuple_filter_id;
  2743. mutex_unlock(&bp->hwrm_cmd_lock);
  2744. return rc;
  2745. }
  2746. #endif
  2747. static int bnxt_hwrm_set_vnic_filter(struct bnxt *bp, u16 vnic_id, u16 idx,
  2748. u8 *mac_addr)
  2749. {
  2750. u32 rc = 0;
  2751. struct hwrm_cfa_l2_filter_alloc_input req = {0};
  2752. struct hwrm_cfa_l2_filter_alloc_output *resp = bp->hwrm_cmd_resp_addr;
  2753. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_L2_FILTER_ALLOC, -1, -1);
  2754. req.flags = cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_PATH_RX);
  2755. if (!BNXT_CHIP_TYPE_NITRO_A0(bp))
  2756. req.flags |=
  2757. cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_FLAGS_OUTERMOST);
  2758. req.dst_id = cpu_to_le16(bp->vnic_info[vnic_id].fw_vnic_id);
  2759. req.enables =
  2760. cpu_to_le32(CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR |
  2761. CFA_L2_FILTER_ALLOC_REQ_ENABLES_DST_ID |
  2762. CFA_L2_FILTER_ALLOC_REQ_ENABLES_L2_ADDR_MASK);
  2763. memcpy(req.l2_addr, mac_addr, ETH_ALEN);
  2764. req.l2_addr_mask[0] = 0xff;
  2765. req.l2_addr_mask[1] = 0xff;
  2766. req.l2_addr_mask[2] = 0xff;
  2767. req.l2_addr_mask[3] = 0xff;
  2768. req.l2_addr_mask[4] = 0xff;
  2769. req.l2_addr_mask[5] = 0xff;
  2770. mutex_lock(&bp->hwrm_cmd_lock);
  2771. rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  2772. if (!rc)
  2773. bp->vnic_info[vnic_id].fw_l2_filter_id[idx] =
  2774. resp->l2_filter_id;
  2775. mutex_unlock(&bp->hwrm_cmd_lock);
  2776. return rc;
  2777. }
  2778. static int bnxt_hwrm_clear_vnic_filter(struct bnxt *bp)
  2779. {
  2780. u16 i, j, num_of_vnics = 1; /* only vnic 0 supported */
  2781. int rc = 0;
  2782. /* Any associated ntuple filters will also be cleared by firmware. */
  2783. mutex_lock(&bp->hwrm_cmd_lock);
  2784. for (i = 0; i < num_of_vnics; i++) {
  2785. struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
  2786. for (j = 0; j < vnic->uc_filter_count; j++) {
  2787. struct hwrm_cfa_l2_filter_free_input req = {0};
  2788. bnxt_hwrm_cmd_hdr_init(bp, &req,
  2789. HWRM_CFA_L2_FILTER_FREE, -1, -1);
  2790. req.l2_filter_id = vnic->fw_l2_filter_id[j];
  2791. rc = _hwrm_send_message(bp, &req, sizeof(req),
  2792. HWRM_CMD_TIMEOUT);
  2793. }
  2794. vnic->uc_filter_count = 0;
  2795. }
  2796. mutex_unlock(&bp->hwrm_cmd_lock);
  2797. return rc;
  2798. }
  2799. static int bnxt_hwrm_vnic_set_tpa(struct bnxt *bp, u16 vnic_id, u32 tpa_flags)
  2800. {
  2801. struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
  2802. struct hwrm_vnic_tpa_cfg_input req = {0};
  2803. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_TPA_CFG, -1, -1);
  2804. if (tpa_flags) {
  2805. u16 mss = bp->dev->mtu - 40;
  2806. u32 nsegs, n, segs = 0, flags;
  2807. flags = VNIC_TPA_CFG_REQ_FLAGS_TPA |
  2808. VNIC_TPA_CFG_REQ_FLAGS_ENCAP_TPA |
  2809. VNIC_TPA_CFG_REQ_FLAGS_RSC_WND_UPDATE |
  2810. VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_ECN |
  2811. VNIC_TPA_CFG_REQ_FLAGS_AGG_WITH_SAME_GRE_SEQ;
  2812. if (tpa_flags & BNXT_FLAG_GRO)
  2813. flags |= VNIC_TPA_CFG_REQ_FLAGS_GRO;
  2814. req.flags = cpu_to_le32(flags);
  2815. req.enables =
  2816. cpu_to_le32(VNIC_TPA_CFG_REQ_ENABLES_MAX_AGG_SEGS |
  2817. VNIC_TPA_CFG_REQ_ENABLES_MAX_AGGS |
  2818. VNIC_TPA_CFG_REQ_ENABLES_MIN_AGG_LEN);
  2819. /* Number of segs are log2 units, and first packet is not
  2820. * included as part of this units.
  2821. */
  2822. if (mss <= BNXT_RX_PAGE_SIZE) {
  2823. n = BNXT_RX_PAGE_SIZE / mss;
  2824. nsegs = (MAX_SKB_FRAGS - 1) * n;
  2825. } else {
  2826. n = mss / BNXT_RX_PAGE_SIZE;
  2827. if (mss & (BNXT_RX_PAGE_SIZE - 1))
  2828. n++;
  2829. nsegs = (MAX_SKB_FRAGS - n) / n;
  2830. }
  2831. segs = ilog2(nsegs);
  2832. req.max_agg_segs = cpu_to_le16(segs);
  2833. req.max_aggs = cpu_to_le16(VNIC_TPA_CFG_REQ_MAX_AGGS_MAX);
  2834. req.min_agg_len = cpu_to_le32(512);
  2835. }
  2836. req.vnic_id = cpu_to_le16(vnic->fw_vnic_id);
  2837. return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  2838. }
  2839. static int bnxt_hwrm_vnic_set_rss(struct bnxt *bp, u16 vnic_id, bool set_rss)
  2840. {
  2841. u32 i, j, max_rings;
  2842. struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
  2843. struct hwrm_vnic_rss_cfg_input req = {0};
  2844. if (vnic->fw_rss_cos_lb_ctx[0] == INVALID_HW_RING_ID)
  2845. return 0;
  2846. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_RSS_CFG, -1, -1);
  2847. if (set_rss) {
  2848. req.hash_type = cpu_to_le32(bp->rss_hash_cfg);
  2849. if (vnic->flags & BNXT_VNIC_RSS_FLAG) {
  2850. if (BNXT_CHIP_TYPE_NITRO_A0(bp))
  2851. max_rings = bp->rx_nr_rings - 1;
  2852. else
  2853. max_rings = bp->rx_nr_rings;
  2854. } else {
  2855. max_rings = 1;
  2856. }
  2857. /* Fill the RSS indirection table with ring group ids */
  2858. for (i = 0, j = 0; i < HW_HASH_INDEX_SIZE; i++, j++) {
  2859. if (j == max_rings)
  2860. j = 0;
  2861. vnic->rss_table[i] = cpu_to_le16(vnic->fw_grp_ids[j]);
  2862. }
  2863. req.ring_grp_tbl_addr = cpu_to_le64(vnic->rss_table_dma_addr);
  2864. req.hash_key_tbl_addr =
  2865. cpu_to_le64(vnic->rss_hash_key_dma_addr);
  2866. }
  2867. req.rss_ctx_idx = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
  2868. return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  2869. }
  2870. static int bnxt_hwrm_vnic_set_hds(struct bnxt *bp, u16 vnic_id)
  2871. {
  2872. struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
  2873. struct hwrm_vnic_plcmodes_cfg_input req = {0};
  2874. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_PLCMODES_CFG, -1, -1);
  2875. req.flags = cpu_to_le32(VNIC_PLCMODES_CFG_REQ_FLAGS_JUMBO_PLACEMENT |
  2876. VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV4 |
  2877. VNIC_PLCMODES_CFG_REQ_FLAGS_HDS_IPV6);
  2878. req.enables =
  2879. cpu_to_le32(VNIC_PLCMODES_CFG_REQ_ENABLES_JUMBO_THRESH_VALID |
  2880. VNIC_PLCMODES_CFG_REQ_ENABLES_HDS_THRESHOLD_VALID);
  2881. /* thresholds not implemented in firmware yet */
  2882. req.jumbo_thresh = cpu_to_le16(bp->rx_copy_thresh);
  2883. req.hds_threshold = cpu_to_le16(bp->rx_copy_thresh);
  2884. req.vnic_id = cpu_to_le32(vnic->fw_vnic_id);
  2885. return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  2886. }
  2887. static void bnxt_hwrm_vnic_ctx_free_one(struct bnxt *bp, u16 vnic_id,
  2888. u16 ctx_idx)
  2889. {
  2890. struct hwrm_vnic_rss_cos_lb_ctx_free_input req = {0};
  2891. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_RSS_COS_LB_CTX_FREE, -1, -1);
  2892. req.rss_cos_lb_ctx_id =
  2893. cpu_to_le16(bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[ctx_idx]);
  2894. hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  2895. bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[ctx_idx] = INVALID_HW_RING_ID;
  2896. }
  2897. static void bnxt_hwrm_vnic_ctx_free(struct bnxt *bp)
  2898. {
  2899. int i, j;
  2900. for (i = 0; i < bp->nr_vnics; i++) {
  2901. struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
  2902. for (j = 0; j < BNXT_MAX_CTX_PER_VNIC; j++) {
  2903. if (vnic->fw_rss_cos_lb_ctx[j] != INVALID_HW_RING_ID)
  2904. bnxt_hwrm_vnic_ctx_free_one(bp, i, j);
  2905. }
  2906. }
  2907. bp->rsscos_nr_ctxs = 0;
  2908. }
  2909. static int bnxt_hwrm_vnic_ctx_alloc(struct bnxt *bp, u16 vnic_id, u16 ctx_idx)
  2910. {
  2911. int rc;
  2912. struct hwrm_vnic_rss_cos_lb_ctx_alloc_input req = {0};
  2913. struct hwrm_vnic_rss_cos_lb_ctx_alloc_output *resp =
  2914. bp->hwrm_cmd_resp_addr;
  2915. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_RSS_COS_LB_CTX_ALLOC, -1,
  2916. -1);
  2917. mutex_lock(&bp->hwrm_cmd_lock);
  2918. rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  2919. if (!rc)
  2920. bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[ctx_idx] =
  2921. le16_to_cpu(resp->rss_cos_lb_ctx_id);
  2922. mutex_unlock(&bp->hwrm_cmd_lock);
  2923. return rc;
  2924. }
  2925. int bnxt_hwrm_vnic_cfg(struct bnxt *bp, u16 vnic_id)
  2926. {
  2927. unsigned int ring = 0, grp_idx;
  2928. struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
  2929. struct hwrm_vnic_cfg_input req = {0};
  2930. u16 def_vlan = 0;
  2931. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_CFG, -1, -1);
  2932. req.enables = cpu_to_le32(VNIC_CFG_REQ_ENABLES_DFLT_RING_GRP);
  2933. /* Only RSS support for now TBD: COS & LB */
  2934. if (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID) {
  2935. req.rss_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[0]);
  2936. req.enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_RSS_RULE |
  2937. VNIC_CFG_REQ_ENABLES_MRU);
  2938. } else {
  2939. req.rss_rule = cpu_to_le16(0xffff);
  2940. }
  2941. if (BNXT_CHIP_TYPE_NITRO_A0(bp) &&
  2942. (vnic->fw_rss_cos_lb_ctx[0] != INVALID_HW_RING_ID)) {
  2943. req.cos_rule = cpu_to_le16(vnic->fw_rss_cos_lb_ctx[1]);
  2944. req.enables |= cpu_to_le32(VNIC_CFG_REQ_ENABLES_COS_RULE);
  2945. } else {
  2946. req.cos_rule = cpu_to_le16(0xffff);
  2947. }
  2948. if (vnic->flags & BNXT_VNIC_RSS_FLAG)
  2949. ring = 0;
  2950. else if (vnic->flags & BNXT_VNIC_RFS_FLAG)
  2951. ring = vnic_id - 1;
  2952. else if ((vnic_id == 1) && BNXT_CHIP_TYPE_NITRO_A0(bp))
  2953. ring = bp->rx_nr_rings - 1;
  2954. grp_idx = bp->rx_ring[ring].bnapi->index;
  2955. req.vnic_id = cpu_to_le16(vnic->fw_vnic_id);
  2956. req.dflt_ring_grp = cpu_to_le16(bp->grp_info[grp_idx].fw_grp_id);
  2957. req.lb_rule = cpu_to_le16(0xffff);
  2958. req.mru = cpu_to_le16(bp->dev->mtu + ETH_HLEN + ETH_FCS_LEN +
  2959. VLAN_HLEN);
  2960. #ifdef CONFIG_BNXT_SRIOV
  2961. if (BNXT_VF(bp))
  2962. def_vlan = bp->vf.vlan;
  2963. #endif
  2964. if ((bp->flags & BNXT_FLAG_STRIP_VLAN) || def_vlan)
  2965. req.flags |= cpu_to_le32(VNIC_CFG_REQ_FLAGS_VLAN_STRIP_MODE);
  2966. if (!vnic_id && bnxt_ulp_registered(bp->edev, BNXT_ROCE_ULP))
  2967. req.flags |=
  2968. cpu_to_le32(VNIC_CFG_REQ_FLAGS_ROCE_DUAL_VNIC_MODE);
  2969. return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  2970. }
  2971. static int bnxt_hwrm_vnic_free_one(struct bnxt *bp, u16 vnic_id)
  2972. {
  2973. u32 rc = 0;
  2974. if (bp->vnic_info[vnic_id].fw_vnic_id != INVALID_HW_RING_ID) {
  2975. struct hwrm_vnic_free_input req = {0};
  2976. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_FREE, -1, -1);
  2977. req.vnic_id =
  2978. cpu_to_le32(bp->vnic_info[vnic_id].fw_vnic_id);
  2979. rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  2980. if (rc)
  2981. return rc;
  2982. bp->vnic_info[vnic_id].fw_vnic_id = INVALID_HW_RING_ID;
  2983. }
  2984. return rc;
  2985. }
  2986. static void bnxt_hwrm_vnic_free(struct bnxt *bp)
  2987. {
  2988. u16 i;
  2989. for (i = 0; i < bp->nr_vnics; i++)
  2990. bnxt_hwrm_vnic_free_one(bp, i);
  2991. }
  2992. static int bnxt_hwrm_vnic_alloc(struct bnxt *bp, u16 vnic_id,
  2993. unsigned int start_rx_ring_idx,
  2994. unsigned int nr_rings)
  2995. {
  2996. int rc = 0;
  2997. unsigned int i, j, grp_idx, end_idx = start_rx_ring_idx + nr_rings;
  2998. struct hwrm_vnic_alloc_input req = {0};
  2999. struct hwrm_vnic_alloc_output *resp = bp->hwrm_cmd_resp_addr;
  3000. /* map ring groups to this vnic */
  3001. for (i = start_rx_ring_idx, j = 0; i < end_idx; i++, j++) {
  3002. grp_idx = bp->rx_ring[i].bnapi->index;
  3003. if (bp->grp_info[grp_idx].fw_grp_id == INVALID_HW_RING_ID) {
  3004. netdev_err(bp->dev, "Not enough ring groups avail:%x req:%x\n",
  3005. j, nr_rings);
  3006. break;
  3007. }
  3008. bp->vnic_info[vnic_id].fw_grp_ids[j] =
  3009. bp->grp_info[grp_idx].fw_grp_id;
  3010. }
  3011. bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[0] = INVALID_HW_RING_ID;
  3012. bp->vnic_info[vnic_id].fw_rss_cos_lb_ctx[1] = INVALID_HW_RING_ID;
  3013. if (vnic_id == 0)
  3014. req.flags = cpu_to_le32(VNIC_ALLOC_REQ_FLAGS_DEFAULT);
  3015. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VNIC_ALLOC, -1, -1);
  3016. mutex_lock(&bp->hwrm_cmd_lock);
  3017. rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  3018. if (!rc)
  3019. bp->vnic_info[vnic_id].fw_vnic_id = le32_to_cpu(resp->vnic_id);
  3020. mutex_unlock(&bp->hwrm_cmd_lock);
  3021. return rc;
  3022. }
  3023. static int bnxt_hwrm_ring_grp_alloc(struct bnxt *bp)
  3024. {
  3025. u16 i;
  3026. u32 rc = 0;
  3027. mutex_lock(&bp->hwrm_cmd_lock);
  3028. for (i = 0; i < bp->rx_nr_rings; i++) {
  3029. struct hwrm_ring_grp_alloc_input req = {0};
  3030. struct hwrm_ring_grp_alloc_output *resp =
  3031. bp->hwrm_cmd_resp_addr;
  3032. unsigned int grp_idx = bp->rx_ring[i].bnapi->index;
  3033. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_GRP_ALLOC, -1, -1);
  3034. req.cr = cpu_to_le16(bp->grp_info[grp_idx].cp_fw_ring_id);
  3035. req.rr = cpu_to_le16(bp->grp_info[grp_idx].rx_fw_ring_id);
  3036. req.ar = cpu_to_le16(bp->grp_info[grp_idx].agg_fw_ring_id);
  3037. req.sc = cpu_to_le16(bp->grp_info[grp_idx].fw_stats_ctx);
  3038. rc = _hwrm_send_message(bp, &req, sizeof(req),
  3039. HWRM_CMD_TIMEOUT);
  3040. if (rc)
  3041. break;
  3042. bp->grp_info[grp_idx].fw_grp_id =
  3043. le32_to_cpu(resp->ring_group_id);
  3044. }
  3045. mutex_unlock(&bp->hwrm_cmd_lock);
  3046. return rc;
  3047. }
  3048. static int bnxt_hwrm_ring_grp_free(struct bnxt *bp)
  3049. {
  3050. u16 i;
  3051. u32 rc = 0;
  3052. struct hwrm_ring_grp_free_input req = {0};
  3053. if (!bp->grp_info)
  3054. return 0;
  3055. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_GRP_FREE, -1, -1);
  3056. mutex_lock(&bp->hwrm_cmd_lock);
  3057. for (i = 0; i < bp->cp_nr_rings; i++) {
  3058. if (bp->grp_info[i].fw_grp_id == INVALID_HW_RING_ID)
  3059. continue;
  3060. req.ring_group_id =
  3061. cpu_to_le32(bp->grp_info[i].fw_grp_id);
  3062. rc = _hwrm_send_message(bp, &req, sizeof(req),
  3063. HWRM_CMD_TIMEOUT);
  3064. if (rc)
  3065. break;
  3066. bp->grp_info[i].fw_grp_id = INVALID_HW_RING_ID;
  3067. }
  3068. mutex_unlock(&bp->hwrm_cmd_lock);
  3069. return rc;
  3070. }
  3071. static int hwrm_ring_alloc_send_msg(struct bnxt *bp,
  3072. struct bnxt_ring_struct *ring,
  3073. u32 ring_type, u32 map_index,
  3074. u32 stats_ctx_id)
  3075. {
  3076. int rc = 0, err = 0;
  3077. struct hwrm_ring_alloc_input req = {0};
  3078. struct hwrm_ring_alloc_output *resp = bp->hwrm_cmd_resp_addr;
  3079. u16 ring_id;
  3080. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_ALLOC, -1, -1);
  3081. req.enables = 0;
  3082. if (ring->nr_pages > 1) {
  3083. req.page_tbl_addr = cpu_to_le64(ring->pg_tbl_map);
  3084. /* Page size is in log2 units */
  3085. req.page_size = BNXT_PAGE_SHIFT;
  3086. req.page_tbl_depth = 1;
  3087. } else {
  3088. req.page_tbl_addr = cpu_to_le64(ring->dma_arr[0]);
  3089. }
  3090. req.fbo = 0;
  3091. /* Association of ring index with doorbell index and MSIX number */
  3092. req.logical_id = cpu_to_le16(map_index);
  3093. switch (ring_type) {
  3094. case HWRM_RING_ALLOC_TX:
  3095. req.ring_type = RING_ALLOC_REQ_RING_TYPE_TX;
  3096. /* Association of transmit ring with completion ring */
  3097. req.cmpl_ring_id =
  3098. cpu_to_le16(bp->grp_info[map_index].cp_fw_ring_id);
  3099. req.length = cpu_to_le32(bp->tx_ring_mask + 1);
  3100. req.stat_ctx_id = cpu_to_le32(stats_ctx_id);
  3101. req.queue_id = cpu_to_le16(ring->queue_id);
  3102. break;
  3103. case HWRM_RING_ALLOC_RX:
  3104. req.ring_type = RING_ALLOC_REQ_RING_TYPE_RX;
  3105. req.length = cpu_to_le32(bp->rx_ring_mask + 1);
  3106. break;
  3107. case HWRM_RING_ALLOC_AGG:
  3108. req.ring_type = RING_ALLOC_REQ_RING_TYPE_RX;
  3109. req.length = cpu_to_le32(bp->rx_agg_ring_mask + 1);
  3110. break;
  3111. case HWRM_RING_ALLOC_CMPL:
  3112. req.ring_type = RING_ALLOC_REQ_RING_TYPE_CMPL;
  3113. req.length = cpu_to_le32(bp->cp_ring_mask + 1);
  3114. if (bp->flags & BNXT_FLAG_USING_MSIX)
  3115. req.int_mode = RING_ALLOC_REQ_INT_MODE_MSIX;
  3116. break;
  3117. default:
  3118. netdev_err(bp->dev, "hwrm alloc invalid ring type %d\n",
  3119. ring_type);
  3120. return -1;
  3121. }
  3122. mutex_lock(&bp->hwrm_cmd_lock);
  3123. rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  3124. err = le16_to_cpu(resp->error_code);
  3125. ring_id = le16_to_cpu(resp->ring_id);
  3126. mutex_unlock(&bp->hwrm_cmd_lock);
  3127. if (rc || err) {
  3128. switch (ring_type) {
  3129. case RING_FREE_REQ_RING_TYPE_CMPL:
  3130. netdev_err(bp->dev, "hwrm_ring_alloc cp failed. rc:%x err:%x\n",
  3131. rc, err);
  3132. return -1;
  3133. case RING_FREE_REQ_RING_TYPE_RX:
  3134. netdev_err(bp->dev, "hwrm_ring_alloc rx failed. rc:%x err:%x\n",
  3135. rc, err);
  3136. return -1;
  3137. case RING_FREE_REQ_RING_TYPE_TX:
  3138. netdev_err(bp->dev, "hwrm_ring_alloc tx failed. rc:%x err:%x\n",
  3139. rc, err);
  3140. return -1;
  3141. default:
  3142. netdev_err(bp->dev, "Invalid ring\n");
  3143. return -1;
  3144. }
  3145. }
  3146. ring->fw_ring_id = ring_id;
  3147. return rc;
  3148. }
  3149. static int bnxt_hwrm_ring_alloc(struct bnxt *bp)
  3150. {
  3151. int i, rc = 0;
  3152. for (i = 0; i < bp->cp_nr_rings; i++) {
  3153. struct bnxt_napi *bnapi = bp->bnapi[i];
  3154. struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
  3155. struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
  3156. cpr->cp_doorbell = bp->bar1 + i * 0x80;
  3157. rc = hwrm_ring_alloc_send_msg(bp, ring, HWRM_RING_ALLOC_CMPL, i,
  3158. INVALID_STATS_CTX_ID);
  3159. if (rc)
  3160. goto err_out;
  3161. BNXT_CP_DB(cpr->cp_doorbell, cpr->cp_raw_cons);
  3162. bp->grp_info[i].cp_fw_ring_id = ring->fw_ring_id;
  3163. }
  3164. for (i = 0; i < bp->tx_nr_rings; i++) {
  3165. struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
  3166. struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
  3167. u32 map_idx = txr->bnapi->index;
  3168. u16 fw_stats_ctx = bp->grp_info[map_idx].fw_stats_ctx;
  3169. rc = hwrm_ring_alloc_send_msg(bp, ring, HWRM_RING_ALLOC_TX,
  3170. map_idx, fw_stats_ctx);
  3171. if (rc)
  3172. goto err_out;
  3173. txr->tx_doorbell = bp->bar1 + map_idx * 0x80;
  3174. }
  3175. for (i = 0; i < bp->rx_nr_rings; i++) {
  3176. struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
  3177. struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
  3178. u32 map_idx = rxr->bnapi->index;
  3179. rc = hwrm_ring_alloc_send_msg(bp, ring, HWRM_RING_ALLOC_RX,
  3180. map_idx, INVALID_STATS_CTX_ID);
  3181. if (rc)
  3182. goto err_out;
  3183. rxr->rx_doorbell = bp->bar1 + map_idx * 0x80;
  3184. writel(DB_KEY_RX | rxr->rx_prod, rxr->rx_doorbell);
  3185. bp->grp_info[map_idx].rx_fw_ring_id = ring->fw_ring_id;
  3186. }
  3187. if (bp->flags & BNXT_FLAG_AGG_RINGS) {
  3188. for (i = 0; i < bp->rx_nr_rings; i++) {
  3189. struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
  3190. struct bnxt_ring_struct *ring =
  3191. &rxr->rx_agg_ring_struct;
  3192. u32 grp_idx = rxr->bnapi->index;
  3193. u32 map_idx = grp_idx + bp->rx_nr_rings;
  3194. rc = hwrm_ring_alloc_send_msg(bp, ring,
  3195. HWRM_RING_ALLOC_AGG,
  3196. map_idx,
  3197. INVALID_STATS_CTX_ID);
  3198. if (rc)
  3199. goto err_out;
  3200. rxr->rx_agg_doorbell = bp->bar1 + map_idx * 0x80;
  3201. writel(DB_KEY_RX | rxr->rx_agg_prod,
  3202. rxr->rx_agg_doorbell);
  3203. bp->grp_info[grp_idx].agg_fw_ring_id = ring->fw_ring_id;
  3204. }
  3205. }
  3206. err_out:
  3207. return rc;
  3208. }
  3209. static int hwrm_ring_free_send_msg(struct bnxt *bp,
  3210. struct bnxt_ring_struct *ring,
  3211. u32 ring_type, int cmpl_ring_id)
  3212. {
  3213. int rc;
  3214. struct hwrm_ring_free_input req = {0};
  3215. struct hwrm_ring_free_output *resp = bp->hwrm_cmd_resp_addr;
  3216. u16 error_code;
  3217. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_RING_FREE, cmpl_ring_id, -1);
  3218. req.ring_type = ring_type;
  3219. req.ring_id = cpu_to_le16(ring->fw_ring_id);
  3220. mutex_lock(&bp->hwrm_cmd_lock);
  3221. rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  3222. error_code = le16_to_cpu(resp->error_code);
  3223. mutex_unlock(&bp->hwrm_cmd_lock);
  3224. if (rc || error_code) {
  3225. switch (ring_type) {
  3226. case RING_FREE_REQ_RING_TYPE_CMPL:
  3227. netdev_err(bp->dev, "hwrm_ring_free cp failed. rc:%d\n",
  3228. rc);
  3229. return rc;
  3230. case RING_FREE_REQ_RING_TYPE_RX:
  3231. netdev_err(bp->dev, "hwrm_ring_free rx failed. rc:%d\n",
  3232. rc);
  3233. return rc;
  3234. case RING_FREE_REQ_RING_TYPE_TX:
  3235. netdev_err(bp->dev, "hwrm_ring_free tx failed. rc:%d\n",
  3236. rc);
  3237. return rc;
  3238. default:
  3239. netdev_err(bp->dev, "Invalid ring\n");
  3240. return -1;
  3241. }
  3242. }
  3243. return 0;
  3244. }
  3245. static void bnxt_hwrm_ring_free(struct bnxt *bp, bool close_path)
  3246. {
  3247. int i;
  3248. if (!bp->bnapi)
  3249. return;
  3250. for (i = 0; i < bp->tx_nr_rings; i++) {
  3251. struct bnxt_tx_ring_info *txr = &bp->tx_ring[i];
  3252. struct bnxt_ring_struct *ring = &txr->tx_ring_struct;
  3253. u32 grp_idx = txr->bnapi->index;
  3254. u32 cmpl_ring_id = bp->grp_info[grp_idx].cp_fw_ring_id;
  3255. if (ring->fw_ring_id != INVALID_HW_RING_ID) {
  3256. hwrm_ring_free_send_msg(bp, ring,
  3257. RING_FREE_REQ_RING_TYPE_TX,
  3258. close_path ? cmpl_ring_id :
  3259. INVALID_HW_RING_ID);
  3260. ring->fw_ring_id = INVALID_HW_RING_ID;
  3261. }
  3262. }
  3263. for (i = 0; i < bp->rx_nr_rings; i++) {
  3264. struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
  3265. struct bnxt_ring_struct *ring = &rxr->rx_ring_struct;
  3266. u32 grp_idx = rxr->bnapi->index;
  3267. u32 cmpl_ring_id = bp->grp_info[grp_idx].cp_fw_ring_id;
  3268. if (ring->fw_ring_id != INVALID_HW_RING_ID) {
  3269. hwrm_ring_free_send_msg(bp, ring,
  3270. RING_FREE_REQ_RING_TYPE_RX,
  3271. close_path ? cmpl_ring_id :
  3272. INVALID_HW_RING_ID);
  3273. ring->fw_ring_id = INVALID_HW_RING_ID;
  3274. bp->grp_info[grp_idx].rx_fw_ring_id =
  3275. INVALID_HW_RING_ID;
  3276. }
  3277. }
  3278. for (i = 0; i < bp->rx_nr_rings; i++) {
  3279. struct bnxt_rx_ring_info *rxr = &bp->rx_ring[i];
  3280. struct bnxt_ring_struct *ring = &rxr->rx_agg_ring_struct;
  3281. u32 grp_idx = rxr->bnapi->index;
  3282. u32 cmpl_ring_id = bp->grp_info[grp_idx].cp_fw_ring_id;
  3283. if (ring->fw_ring_id != INVALID_HW_RING_ID) {
  3284. hwrm_ring_free_send_msg(bp, ring,
  3285. RING_FREE_REQ_RING_TYPE_RX,
  3286. close_path ? cmpl_ring_id :
  3287. INVALID_HW_RING_ID);
  3288. ring->fw_ring_id = INVALID_HW_RING_ID;
  3289. bp->grp_info[grp_idx].agg_fw_ring_id =
  3290. INVALID_HW_RING_ID;
  3291. }
  3292. }
  3293. for (i = 0; i < bp->cp_nr_rings; i++) {
  3294. struct bnxt_napi *bnapi = bp->bnapi[i];
  3295. struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
  3296. struct bnxt_ring_struct *ring = &cpr->cp_ring_struct;
  3297. if (ring->fw_ring_id != INVALID_HW_RING_ID) {
  3298. hwrm_ring_free_send_msg(bp, ring,
  3299. RING_FREE_REQ_RING_TYPE_CMPL,
  3300. INVALID_HW_RING_ID);
  3301. ring->fw_ring_id = INVALID_HW_RING_ID;
  3302. bp->grp_info[i].cp_fw_ring_id = INVALID_HW_RING_ID;
  3303. }
  3304. }
  3305. }
  3306. static void bnxt_hwrm_set_coal_params(struct bnxt *bp, u32 max_bufs,
  3307. u32 buf_tmrs, u16 flags,
  3308. struct hwrm_ring_cmpl_ring_cfg_aggint_params_input *req)
  3309. {
  3310. req->flags = cpu_to_le16(flags);
  3311. req->num_cmpl_dma_aggr = cpu_to_le16((u16)max_bufs);
  3312. req->num_cmpl_dma_aggr_during_int = cpu_to_le16(max_bufs >> 16);
  3313. req->cmpl_aggr_dma_tmr = cpu_to_le16((u16)buf_tmrs);
  3314. req->cmpl_aggr_dma_tmr_during_int = cpu_to_le16(buf_tmrs >> 16);
  3315. /* Minimum time between 2 interrupts set to buf_tmr x 2 */
  3316. req->int_lat_tmr_min = cpu_to_le16((u16)buf_tmrs * 2);
  3317. req->int_lat_tmr_max = cpu_to_le16((u16)buf_tmrs * 4);
  3318. req->num_cmpl_aggr_int = cpu_to_le16((u16)max_bufs * 4);
  3319. }
  3320. int bnxt_hwrm_set_coal(struct bnxt *bp)
  3321. {
  3322. int i, rc = 0;
  3323. struct hwrm_ring_cmpl_ring_cfg_aggint_params_input req_rx = {0},
  3324. req_tx = {0}, *req;
  3325. u16 max_buf, max_buf_irq;
  3326. u16 buf_tmr, buf_tmr_irq;
  3327. u32 flags;
  3328. bnxt_hwrm_cmd_hdr_init(bp, &req_rx,
  3329. HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS, -1, -1);
  3330. bnxt_hwrm_cmd_hdr_init(bp, &req_tx,
  3331. HWRM_RING_CMPL_RING_CFG_AGGINT_PARAMS, -1, -1);
  3332. /* Each rx completion (2 records) should be DMAed immediately.
  3333. * DMA 1/4 of the completion buffers at a time.
  3334. */
  3335. max_buf = min_t(u16, bp->rx_coal_bufs / 4, 2);
  3336. /* max_buf must not be zero */
  3337. max_buf = clamp_t(u16, max_buf, 1, 63);
  3338. max_buf_irq = clamp_t(u16, bp->rx_coal_bufs_irq, 1, 63);
  3339. buf_tmr = BNXT_USEC_TO_COAL_TIMER(bp->rx_coal_ticks);
  3340. /* buf timer set to 1/4 of interrupt timer */
  3341. buf_tmr = max_t(u16, buf_tmr / 4, 1);
  3342. buf_tmr_irq = BNXT_USEC_TO_COAL_TIMER(bp->rx_coal_ticks_irq);
  3343. buf_tmr_irq = max_t(u16, buf_tmr_irq, 1);
  3344. flags = RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_TIMER_RESET;
  3345. /* RING_IDLE generates more IRQs for lower latency. Enable it only
  3346. * if coal_ticks is less than 25 us.
  3347. */
  3348. if (bp->rx_coal_ticks < 25)
  3349. flags |= RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_RING_IDLE;
  3350. bnxt_hwrm_set_coal_params(bp, max_buf_irq << 16 | max_buf,
  3351. buf_tmr_irq << 16 | buf_tmr, flags, &req_rx);
  3352. /* max_buf must not be zero */
  3353. max_buf = clamp_t(u16, bp->tx_coal_bufs, 1, 63);
  3354. max_buf_irq = clamp_t(u16, bp->tx_coal_bufs_irq, 1, 63);
  3355. buf_tmr = BNXT_USEC_TO_COAL_TIMER(bp->tx_coal_ticks);
  3356. /* buf timer set to 1/4 of interrupt timer */
  3357. buf_tmr = max_t(u16, buf_tmr / 4, 1);
  3358. buf_tmr_irq = BNXT_USEC_TO_COAL_TIMER(bp->tx_coal_ticks_irq);
  3359. buf_tmr_irq = max_t(u16, buf_tmr_irq, 1);
  3360. flags = RING_CMPL_RING_CFG_AGGINT_PARAMS_REQ_FLAGS_TIMER_RESET;
  3361. bnxt_hwrm_set_coal_params(bp, max_buf_irq << 16 | max_buf,
  3362. buf_tmr_irq << 16 | buf_tmr, flags, &req_tx);
  3363. mutex_lock(&bp->hwrm_cmd_lock);
  3364. for (i = 0; i < bp->cp_nr_rings; i++) {
  3365. struct bnxt_napi *bnapi = bp->bnapi[i];
  3366. req = &req_rx;
  3367. if (!bnapi->rx_ring)
  3368. req = &req_tx;
  3369. req->ring_id = cpu_to_le16(bp->grp_info[i].cp_fw_ring_id);
  3370. rc = _hwrm_send_message(bp, req, sizeof(*req),
  3371. HWRM_CMD_TIMEOUT);
  3372. if (rc)
  3373. break;
  3374. }
  3375. mutex_unlock(&bp->hwrm_cmd_lock);
  3376. return rc;
  3377. }
  3378. static int bnxt_hwrm_stat_ctx_free(struct bnxt *bp)
  3379. {
  3380. int rc = 0, i;
  3381. struct hwrm_stat_ctx_free_input req = {0};
  3382. if (!bp->bnapi)
  3383. return 0;
  3384. if (BNXT_CHIP_TYPE_NITRO_A0(bp))
  3385. return 0;
  3386. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_STAT_CTX_FREE, -1, -1);
  3387. mutex_lock(&bp->hwrm_cmd_lock);
  3388. for (i = 0; i < bp->cp_nr_rings; i++) {
  3389. struct bnxt_napi *bnapi = bp->bnapi[i];
  3390. struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
  3391. if (cpr->hw_stats_ctx_id != INVALID_STATS_CTX_ID) {
  3392. req.stat_ctx_id = cpu_to_le32(cpr->hw_stats_ctx_id);
  3393. rc = _hwrm_send_message(bp, &req, sizeof(req),
  3394. HWRM_CMD_TIMEOUT);
  3395. if (rc)
  3396. break;
  3397. cpr->hw_stats_ctx_id = INVALID_STATS_CTX_ID;
  3398. }
  3399. }
  3400. mutex_unlock(&bp->hwrm_cmd_lock);
  3401. return rc;
  3402. }
  3403. static int bnxt_hwrm_stat_ctx_alloc(struct bnxt *bp)
  3404. {
  3405. int rc = 0, i;
  3406. struct hwrm_stat_ctx_alloc_input req = {0};
  3407. struct hwrm_stat_ctx_alloc_output *resp = bp->hwrm_cmd_resp_addr;
  3408. if (BNXT_CHIP_TYPE_NITRO_A0(bp))
  3409. return 0;
  3410. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_STAT_CTX_ALLOC, -1, -1);
  3411. req.update_period_ms = cpu_to_le32(bp->stats_coal_ticks / 1000);
  3412. mutex_lock(&bp->hwrm_cmd_lock);
  3413. for (i = 0; i < bp->cp_nr_rings; i++) {
  3414. struct bnxt_napi *bnapi = bp->bnapi[i];
  3415. struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
  3416. req.stats_dma_addr = cpu_to_le64(cpr->hw_stats_map);
  3417. rc = _hwrm_send_message(bp, &req, sizeof(req),
  3418. HWRM_CMD_TIMEOUT);
  3419. if (rc)
  3420. break;
  3421. cpr->hw_stats_ctx_id = le32_to_cpu(resp->stat_ctx_id);
  3422. bp->grp_info[i].fw_stats_ctx = cpr->hw_stats_ctx_id;
  3423. }
  3424. mutex_unlock(&bp->hwrm_cmd_lock);
  3425. return rc;
  3426. }
  3427. static int bnxt_hwrm_func_qcfg(struct bnxt *bp)
  3428. {
  3429. struct hwrm_func_qcfg_input req = {0};
  3430. struct hwrm_func_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
  3431. int rc;
  3432. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_QCFG, -1, -1);
  3433. req.fid = cpu_to_le16(0xffff);
  3434. mutex_lock(&bp->hwrm_cmd_lock);
  3435. rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  3436. if (rc)
  3437. goto func_qcfg_exit;
  3438. #ifdef CONFIG_BNXT_SRIOV
  3439. if (BNXT_VF(bp)) {
  3440. struct bnxt_vf_info *vf = &bp->vf;
  3441. vf->vlan = le16_to_cpu(resp->vlan) & VLAN_VID_MASK;
  3442. }
  3443. #endif
  3444. switch (resp->port_partition_type) {
  3445. case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_0:
  3446. case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR1_5:
  3447. case FUNC_QCFG_RESP_PORT_PARTITION_TYPE_NPAR2_0:
  3448. bp->port_partition_type = resp->port_partition_type;
  3449. break;
  3450. }
  3451. func_qcfg_exit:
  3452. mutex_unlock(&bp->hwrm_cmd_lock);
  3453. return rc;
  3454. }
  3455. static int bnxt_hwrm_func_qcaps(struct bnxt *bp)
  3456. {
  3457. int rc = 0;
  3458. struct hwrm_func_qcaps_input req = {0};
  3459. struct hwrm_func_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
  3460. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_QCAPS, -1, -1);
  3461. req.fid = cpu_to_le16(0xffff);
  3462. mutex_lock(&bp->hwrm_cmd_lock);
  3463. rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  3464. if (rc)
  3465. goto hwrm_func_qcaps_exit;
  3466. if (resp->flags & cpu_to_le32(FUNC_QCAPS_RESP_FLAGS_ROCE_V1_SUPPORTED))
  3467. bp->flags |= BNXT_FLAG_ROCEV1_CAP;
  3468. if (resp->flags & cpu_to_le32(FUNC_QCAPS_RESP_FLAGS_ROCE_V2_SUPPORTED))
  3469. bp->flags |= BNXT_FLAG_ROCEV2_CAP;
  3470. bp->tx_push_thresh = 0;
  3471. if (resp->flags &
  3472. cpu_to_le32(FUNC_QCAPS_RESP_FLAGS_PUSH_MODE_SUPPORTED))
  3473. bp->tx_push_thresh = BNXT_TX_PUSH_THRESH;
  3474. if (BNXT_PF(bp)) {
  3475. struct bnxt_pf_info *pf = &bp->pf;
  3476. pf->fw_fid = le16_to_cpu(resp->fid);
  3477. pf->port_id = le16_to_cpu(resp->port_id);
  3478. bp->dev->dev_port = pf->port_id;
  3479. memcpy(pf->mac_addr, resp->mac_address, ETH_ALEN);
  3480. memcpy(bp->dev->dev_addr, pf->mac_addr, ETH_ALEN);
  3481. pf->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
  3482. pf->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
  3483. pf->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
  3484. pf->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
  3485. pf->max_hw_ring_grps = le32_to_cpu(resp->max_hw_ring_grps);
  3486. if (!pf->max_hw_ring_grps)
  3487. pf->max_hw_ring_grps = pf->max_tx_rings;
  3488. pf->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
  3489. pf->max_vnics = le16_to_cpu(resp->max_vnics);
  3490. pf->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
  3491. pf->first_vf_id = le16_to_cpu(resp->first_vf_id);
  3492. pf->max_vfs = le16_to_cpu(resp->max_vfs);
  3493. pf->max_encap_records = le32_to_cpu(resp->max_encap_records);
  3494. pf->max_decap_records = le32_to_cpu(resp->max_decap_records);
  3495. pf->max_tx_em_flows = le32_to_cpu(resp->max_tx_em_flows);
  3496. pf->max_tx_wm_flows = le32_to_cpu(resp->max_tx_wm_flows);
  3497. pf->max_rx_em_flows = le32_to_cpu(resp->max_rx_em_flows);
  3498. pf->max_rx_wm_flows = le32_to_cpu(resp->max_rx_wm_flows);
  3499. } else {
  3500. #ifdef CONFIG_BNXT_SRIOV
  3501. struct bnxt_vf_info *vf = &bp->vf;
  3502. vf->fw_fid = le16_to_cpu(resp->fid);
  3503. vf->max_rsscos_ctxs = le16_to_cpu(resp->max_rsscos_ctx);
  3504. vf->max_cp_rings = le16_to_cpu(resp->max_cmpl_rings);
  3505. vf->max_tx_rings = le16_to_cpu(resp->max_tx_rings);
  3506. vf->max_rx_rings = le16_to_cpu(resp->max_rx_rings);
  3507. vf->max_hw_ring_grps = le32_to_cpu(resp->max_hw_ring_grps);
  3508. if (!vf->max_hw_ring_grps)
  3509. vf->max_hw_ring_grps = vf->max_tx_rings;
  3510. vf->max_l2_ctxs = le16_to_cpu(resp->max_l2_ctxs);
  3511. vf->max_vnics = le16_to_cpu(resp->max_vnics);
  3512. vf->max_stat_ctxs = le16_to_cpu(resp->max_stat_ctx);
  3513. memcpy(vf->mac_addr, resp->mac_address, ETH_ALEN);
  3514. mutex_unlock(&bp->hwrm_cmd_lock);
  3515. if (is_valid_ether_addr(vf->mac_addr)) {
  3516. /* overwrite netdev dev_adr with admin VF MAC */
  3517. memcpy(bp->dev->dev_addr, vf->mac_addr, ETH_ALEN);
  3518. } else {
  3519. random_ether_addr(bp->dev->dev_addr);
  3520. rc = bnxt_approve_mac(bp, bp->dev->dev_addr);
  3521. }
  3522. return rc;
  3523. #endif
  3524. }
  3525. hwrm_func_qcaps_exit:
  3526. mutex_unlock(&bp->hwrm_cmd_lock);
  3527. return rc;
  3528. }
  3529. static int bnxt_hwrm_func_reset(struct bnxt *bp)
  3530. {
  3531. struct hwrm_func_reset_input req = {0};
  3532. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FUNC_RESET, -1, -1);
  3533. req.enables = 0;
  3534. return hwrm_send_message(bp, &req, sizeof(req), HWRM_RESET_TIMEOUT);
  3535. }
  3536. static int bnxt_hwrm_queue_qportcfg(struct bnxt *bp)
  3537. {
  3538. int rc = 0;
  3539. struct hwrm_queue_qportcfg_input req = {0};
  3540. struct hwrm_queue_qportcfg_output *resp = bp->hwrm_cmd_resp_addr;
  3541. u8 i, *qptr;
  3542. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_QUEUE_QPORTCFG, -1, -1);
  3543. mutex_lock(&bp->hwrm_cmd_lock);
  3544. rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  3545. if (rc)
  3546. goto qportcfg_exit;
  3547. if (!resp->max_configurable_queues) {
  3548. rc = -EINVAL;
  3549. goto qportcfg_exit;
  3550. }
  3551. bp->max_tc = resp->max_configurable_queues;
  3552. bp->max_lltc = resp->max_configurable_lossless_queues;
  3553. if (bp->max_tc > BNXT_MAX_QUEUE)
  3554. bp->max_tc = BNXT_MAX_QUEUE;
  3555. if (resp->queue_cfg_info & QUEUE_QPORTCFG_RESP_QUEUE_CFG_INFO_ASYM_CFG)
  3556. bp->max_tc = 1;
  3557. if (bp->max_lltc > bp->max_tc)
  3558. bp->max_lltc = bp->max_tc;
  3559. qptr = &resp->queue_id0;
  3560. for (i = 0; i < bp->max_tc; i++) {
  3561. bp->q_info[i].queue_id = *qptr++;
  3562. bp->q_info[i].queue_profile = *qptr++;
  3563. }
  3564. qportcfg_exit:
  3565. mutex_unlock(&bp->hwrm_cmd_lock);
  3566. return rc;
  3567. }
  3568. static int bnxt_hwrm_ver_get(struct bnxt *bp)
  3569. {
  3570. int rc;
  3571. struct hwrm_ver_get_input req = {0};
  3572. struct hwrm_ver_get_output *resp = bp->hwrm_cmd_resp_addr;
  3573. bp->hwrm_max_req_len = HWRM_MAX_REQ_LEN;
  3574. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_VER_GET, -1, -1);
  3575. req.hwrm_intf_maj = HWRM_VERSION_MAJOR;
  3576. req.hwrm_intf_min = HWRM_VERSION_MINOR;
  3577. req.hwrm_intf_upd = HWRM_VERSION_UPDATE;
  3578. mutex_lock(&bp->hwrm_cmd_lock);
  3579. rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  3580. if (rc)
  3581. goto hwrm_ver_get_exit;
  3582. memcpy(&bp->ver_resp, resp, sizeof(struct hwrm_ver_get_output));
  3583. bp->hwrm_spec_code = resp->hwrm_intf_maj << 16 |
  3584. resp->hwrm_intf_min << 8 | resp->hwrm_intf_upd;
  3585. if (resp->hwrm_intf_maj < 1) {
  3586. netdev_warn(bp->dev, "HWRM interface %d.%d.%d is older than 1.0.0.\n",
  3587. resp->hwrm_intf_maj, resp->hwrm_intf_min,
  3588. resp->hwrm_intf_upd);
  3589. netdev_warn(bp->dev, "Please update firmware with HWRM interface 1.0.0 or newer.\n");
  3590. }
  3591. snprintf(bp->fw_ver_str, BC_HWRM_STR_LEN, "%d.%d.%d/%d.%d.%d",
  3592. resp->hwrm_fw_maj, resp->hwrm_fw_min, resp->hwrm_fw_bld,
  3593. resp->hwrm_intf_maj, resp->hwrm_intf_min, resp->hwrm_intf_upd);
  3594. bp->hwrm_cmd_timeout = le16_to_cpu(resp->def_req_timeout);
  3595. if (!bp->hwrm_cmd_timeout)
  3596. bp->hwrm_cmd_timeout = DFLT_HWRM_CMD_TIMEOUT;
  3597. if (resp->hwrm_intf_maj >= 1)
  3598. bp->hwrm_max_req_len = le16_to_cpu(resp->max_req_win_len);
  3599. bp->chip_num = le16_to_cpu(resp->chip_num);
  3600. if (bp->chip_num == CHIP_NUM_58700 && !resp->chip_rev &&
  3601. !resp->chip_metal)
  3602. bp->flags |= BNXT_FLAG_CHIP_NITRO_A0;
  3603. hwrm_ver_get_exit:
  3604. mutex_unlock(&bp->hwrm_cmd_lock);
  3605. return rc;
  3606. }
  3607. int bnxt_hwrm_fw_set_time(struct bnxt *bp)
  3608. {
  3609. #if IS_ENABLED(CONFIG_RTC_LIB)
  3610. struct hwrm_fw_set_time_input req = {0};
  3611. struct rtc_time tm;
  3612. struct timeval tv;
  3613. if (bp->hwrm_spec_code < 0x10400)
  3614. return -EOPNOTSUPP;
  3615. do_gettimeofday(&tv);
  3616. rtc_time_to_tm(tv.tv_sec, &tm);
  3617. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_FW_SET_TIME, -1, -1);
  3618. req.year = cpu_to_le16(1900 + tm.tm_year);
  3619. req.month = 1 + tm.tm_mon;
  3620. req.day = tm.tm_mday;
  3621. req.hour = tm.tm_hour;
  3622. req.minute = tm.tm_min;
  3623. req.second = tm.tm_sec;
  3624. return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  3625. #else
  3626. return -EOPNOTSUPP;
  3627. #endif
  3628. }
  3629. static int bnxt_hwrm_port_qstats(struct bnxt *bp)
  3630. {
  3631. int rc;
  3632. struct bnxt_pf_info *pf = &bp->pf;
  3633. struct hwrm_port_qstats_input req = {0};
  3634. if (!(bp->flags & BNXT_FLAG_PORT_STATS))
  3635. return 0;
  3636. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_QSTATS, -1, -1);
  3637. req.port_id = cpu_to_le16(pf->port_id);
  3638. req.tx_stat_host_addr = cpu_to_le64(bp->hw_tx_port_stats_map);
  3639. req.rx_stat_host_addr = cpu_to_le64(bp->hw_rx_port_stats_map);
  3640. rc = hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  3641. return rc;
  3642. }
  3643. static void bnxt_hwrm_free_tunnel_ports(struct bnxt *bp)
  3644. {
  3645. if (bp->vxlan_port_cnt) {
  3646. bnxt_hwrm_tunnel_dst_port_free(
  3647. bp, TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN);
  3648. }
  3649. bp->vxlan_port_cnt = 0;
  3650. if (bp->nge_port_cnt) {
  3651. bnxt_hwrm_tunnel_dst_port_free(
  3652. bp, TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE);
  3653. }
  3654. bp->nge_port_cnt = 0;
  3655. }
  3656. static int bnxt_set_tpa(struct bnxt *bp, bool set_tpa)
  3657. {
  3658. int rc, i;
  3659. u32 tpa_flags = 0;
  3660. if (set_tpa)
  3661. tpa_flags = bp->flags & BNXT_FLAG_TPA;
  3662. for (i = 0; i < bp->nr_vnics; i++) {
  3663. rc = bnxt_hwrm_vnic_set_tpa(bp, i, tpa_flags);
  3664. if (rc) {
  3665. netdev_err(bp->dev, "hwrm vnic set tpa failure rc for vnic %d: %x\n",
  3666. rc, i);
  3667. return rc;
  3668. }
  3669. }
  3670. return 0;
  3671. }
  3672. static void bnxt_hwrm_clear_vnic_rss(struct bnxt *bp)
  3673. {
  3674. int i;
  3675. for (i = 0; i < bp->nr_vnics; i++)
  3676. bnxt_hwrm_vnic_set_rss(bp, i, false);
  3677. }
  3678. static void bnxt_hwrm_resource_free(struct bnxt *bp, bool close_path,
  3679. bool irq_re_init)
  3680. {
  3681. if (bp->vnic_info) {
  3682. bnxt_hwrm_clear_vnic_filter(bp);
  3683. /* clear all RSS setting before free vnic ctx */
  3684. bnxt_hwrm_clear_vnic_rss(bp);
  3685. bnxt_hwrm_vnic_ctx_free(bp);
  3686. /* before free the vnic, undo the vnic tpa settings */
  3687. if (bp->flags & BNXT_FLAG_TPA)
  3688. bnxt_set_tpa(bp, false);
  3689. bnxt_hwrm_vnic_free(bp);
  3690. }
  3691. bnxt_hwrm_ring_free(bp, close_path);
  3692. bnxt_hwrm_ring_grp_free(bp);
  3693. if (irq_re_init) {
  3694. bnxt_hwrm_stat_ctx_free(bp);
  3695. bnxt_hwrm_free_tunnel_ports(bp);
  3696. }
  3697. }
  3698. static int bnxt_setup_vnic(struct bnxt *bp, u16 vnic_id)
  3699. {
  3700. int rc;
  3701. /* allocate context for vnic */
  3702. rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic_id, 0);
  3703. if (rc) {
  3704. netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
  3705. vnic_id, rc);
  3706. goto vnic_setup_err;
  3707. }
  3708. bp->rsscos_nr_ctxs++;
  3709. if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
  3710. rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic_id, 1);
  3711. if (rc) {
  3712. netdev_err(bp->dev, "hwrm vnic %d cos ctx alloc failure rc: %x\n",
  3713. vnic_id, rc);
  3714. goto vnic_setup_err;
  3715. }
  3716. bp->rsscos_nr_ctxs++;
  3717. }
  3718. /* configure default vnic, ring grp */
  3719. rc = bnxt_hwrm_vnic_cfg(bp, vnic_id);
  3720. if (rc) {
  3721. netdev_err(bp->dev, "hwrm vnic %d cfg failure rc: %x\n",
  3722. vnic_id, rc);
  3723. goto vnic_setup_err;
  3724. }
  3725. /* Enable RSS hashing on vnic */
  3726. rc = bnxt_hwrm_vnic_set_rss(bp, vnic_id, true);
  3727. if (rc) {
  3728. netdev_err(bp->dev, "hwrm vnic %d set rss failure rc: %x\n",
  3729. vnic_id, rc);
  3730. goto vnic_setup_err;
  3731. }
  3732. if (bp->flags & BNXT_FLAG_AGG_RINGS) {
  3733. rc = bnxt_hwrm_vnic_set_hds(bp, vnic_id);
  3734. if (rc) {
  3735. netdev_err(bp->dev, "hwrm vnic %d set hds failure rc: %x\n",
  3736. vnic_id, rc);
  3737. }
  3738. }
  3739. vnic_setup_err:
  3740. return rc;
  3741. }
  3742. static int bnxt_alloc_rfs_vnics(struct bnxt *bp)
  3743. {
  3744. #ifdef CONFIG_RFS_ACCEL
  3745. int i, rc = 0;
  3746. for (i = 0; i < bp->rx_nr_rings; i++) {
  3747. u16 vnic_id = i + 1;
  3748. u16 ring_id = i;
  3749. if (vnic_id >= bp->nr_vnics)
  3750. break;
  3751. bp->vnic_info[vnic_id].flags |= BNXT_VNIC_RFS_FLAG;
  3752. rc = bnxt_hwrm_vnic_alloc(bp, vnic_id, ring_id, 1);
  3753. if (rc) {
  3754. netdev_err(bp->dev, "hwrm vnic %d alloc failure rc: %x\n",
  3755. vnic_id, rc);
  3756. break;
  3757. }
  3758. rc = bnxt_setup_vnic(bp, vnic_id);
  3759. if (rc)
  3760. break;
  3761. }
  3762. return rc;
  3763. #else
  3764. return 0;
  3765. #endif
  3766. }
  3767. /* Allow PF and VF with default VLAN to be in promiscuous mode */
  3768. static bool bnxt_promisc_ok(struct bnxt *bp)
  3769. {
  3770. #ifdef CONFIG_BNXT_SRIOV
  3771. if (BNXT_VF(bp) && !bp->vf.vlan)
  3772. return false;
  3773. #endif
  3774. return true;
  3775. }
  3776. static int bnxt_setup_nitroa0_vnic(struct bnxt *bp)
  3777. {
  3778. unsigned int rc = 0;
  3779. rc = bnxt_hwrm_vnic_alloc(bp, 1, bp->rx_nr_rings - 1, 1);
  3780. if (rc) {
  3781. netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
  3782. rc);
  3783. return rc;
  3784. }
  3785. rc = bnxt_hwrm_vnic_cfg(bp, 1);
  3786. if (rc) {
  3787. netdev_err(bp->dev, "Cannot allocate special vnic for NS2 A0: %x\n",
  3788. rc);
  3789. return rc;
  3790. }
  3791. return rc;
  3792. }
  3793. static int bnxt_cfg_rx_mode(struct bnxt *);
  3794. static bool bnxt_mc_list_updated(struct bnxt *, u32 *);
  3795. static int bnxt_init_chip(struct bnxt *bp, bool irq_re_init)
  3796. {
  3797. struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
  3798. int rc = 0;
  3799. unsigned int rx_nr_rings = bp->rx_nr_rings;
  3800. if (irq_re_init) {
  3801. rc = bnxt_hwrm_stat_ctx_alloc(bp);
  3802. if (rc) {
  3803. netdev_err(bp->dev, "hwrm stat ctx alloc failure rc: %x\n",
  3804. rc);
  3805. goto err_out;
  3806. }
  3807. }
  3808. rc = bnxt_hwrm_ring_alloc(bp);
  3809. if (rc) {
  3810. netdev_err(bp->dev, "hwrm ring alloc failure rc: %x\n", rc);
  3811. goto err_out;
  3812. }
  3813. rc = bnxt_hwrm_ring_grp_alloc(bp);
  3814. if (rc) {
  3815. netdev_err(bp->dev, "hwrm_ring_grp alloc failure: %x\n", rc);
  3816. goto err_out;
  3817. }
  3818. if (BNXT_CHIP_TYPE_NITRO_A0(bp))
  3819. rx_nr_rings--;
  3820. /* default vnic 0 */
  3821. rc = bnxt_hwrm_vnic_alloc(bp, 0, 0, rx_nr_rings);
  3822. if (rc) {
  3823. netdev_err(bp->dev, "hwrm vnic alloc failure rc: %x\n", rc);
  3824. goto err_out;
  3825. }
  3826. rc = bnxt_setup_vnic(bp, 0);
  3827. if (rc)
  3828. goto err_out;
  3829. if (bp->flags & BNXT_FLAG_RFS) {
  3830. rc = bnxt_alloc_rfs_vnics(bp);
  3831. if (rc)
  3832. goto err_out;
  3833. }
  3834. if (bp->flags & BNXT_FLAG_TPA) {
  3835. rc = bnxt_set_tpa(bp, true);
  3836. if (rc)
  3837. goto err_out;
  3838. }
  3839. if (BNXT_VF(bp))
  3840. bnxt_update_vf_mac(bp);
  3841. /* Filter for default vnic 0 */
  3842. rc = bnxt_hwrm_set_vnic_filter(bp, 0, 0, bp->dev->dev_addr);
  3843. if (rc) {
  3844. netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n", rc);
  3845. goto err_out;
  3846. }
  3847. vnic->uc_filter_count = 1;
  3848. vnic->rx_mask = CFA_L2_SET_RX_MASK_REQ_MASK_BCAST;
  3849. if ((bp->dev->flags & IFF_PROMISC) && bnxt_promisc_ok(bp))
  3850. vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
  3851. if (bp->dev->flags & IFF_ALLMULTI) {
  3852. vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
  3853. vnic->mc_list_count = 0;
  3854. } else {
  3855. u32 mask = 0;
  3856. bnxt_mc_list_updated(bp, &mask);
  3857. vnic->rx_mask |= mask;
  3858. }
  3859. rc = bnxt_cfg_rx_mode(bp);
  3860. if (rc)
  3861. goto err_out;
  3862. rc = bnxt_hwrm_set_coal(bp);
  3863. if (rc)
  3864. netdev_warn(bp->dev, "HWRM set coalescing failure rc: %x\n",
  3865. rc);
  3866. if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
  3867. rc = bnxt_setup_nitroa0_vnic(bp);
  3868. if (rc)
  3869. netdev_err(bp->dev, "Special vnic setup failure for NS2 A0 rc: %x\n",
  3870. rc);
  3871. }
  3872. if (BNXT_VF(bp)) {
  3873. bnxt_hwrm_func_qcfg(bp);
  3874. netdev_update_features(bp->dev);
  3875. }
  3876. return 0;
  3877. err_out:
  3878. bnxt_hwrm_resource_free(bp, 0, true);
  3879. return rc;
  3880. }
  3881. static int bnxt_shutdown_nic(struct bnxt *bp, bool irq_re_init)
  3882. {
  3883. bnxt_hwrm_resource_free(bp, 1, irq_re_init);
  3884. return 0;
  3885. }
  3886. static int bnxt_init_nic(struct bnxt *bp, bool irq_re_init)
  3887. {
  3888. bnxt_init_rx_rings(bp);
  3889. bnxt_init_tx_rings(bp);
  3890. bnxt_init_ring_grps(bp, irq_re_init);
  3891. bnxt_init_vnics(bp);
  3892. return bnxt_init_chip(bp, irq_re_init);
  3893. }
  3894. static void bnxt_disable_int(struct bnxt *bp)
  3895. {
  3896. int i;
  3897. if (!bp->bnapi)
  3898. return;
  3899. for (i = 0; i < bp->cp_nr_rings; i++) {
  3900. struct bnxt_napi *bnapi = bp->bnapi[i];
  3901. struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
  3902. BNXT_CP_DB(cpr->cp_doorbell, cpr->cp_raw_cons);
  3903. }
  3904. }
  3905. static void bnxt_enable_int(struct bnxt *bp)
  3906. {
  3907. int i;
  3908. atomic_set(&bp->intr_sem, 0);
  3909. for (i = 0; i < bp->cp_nr_rings; i++) {
  3910. struct bnxt_napi *bnapi = bp->bnapi[i];
  3911. struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
  3912. BNXT_CP_DB_REARM(cpr->cp_doorbell, cpr->cp_raw_cons);
  3913. }
  3914. }
  3915. static int bnxt_set_real_num_queues(struct bnxt *bp)
  3916. {
  3917. int rc;
  3918. struct net_device *dev = bp->dev;
  3919. rc = netif_set_real_num_tx_queues(dev, bp->tx_nr_rings);
  3920. if (rc)
  3921. return rc;
  3922. rc = netif_set_real_num_rx_queues(dev, bp->rx_nr_rings);
  3923. if (rc)
  3924. return rc;
  3925. #ifdef CONFIG_RFS_ACCEL
  3926. if (bp->flags & BNXT_FLAG_RFS)
  3927. dev->rx_cpu_rmap = alloc_irq_cpu_rmap(bp->rx_nr_rings);
  3928. #endif
  3929. return rc;
  3930. }
  3931. static int bnxt_trim_rings(struct bnxt *bp, int *rx, int *tx, int max,
  3932. bool shared)
  3933. {
  3934. int _rx = *rx, _tx = *tx;
  3935. if (shared) {
  3936. *rx = min_t(int, _rx, max);
  3937. *tx = min_t(int, _tx, max);
  3938. } else {
  3939. if (max < 2)
  3940. return -ENOMEM;
  3941. while (_rx + _tx > max) {
  3942. if (_rx > _tx && _rx > 1)
  3943. _rx--;
  3944. else if (_tx > 1)
  3945. _tx--;
  3946. }
  3947. *rx = _rx;
  3948. *tx = _tx;
  3949. }
  3950. return 0;
  3951. }
  3952. static void bnxt_setup_msix(struct bnxt *bp)
  3953. {
  3954. const int len = sizeof(bp->irq_tbl[0].name);
  3955. struct net_device *dev = bp->dev;
  3956. int tcs, i;
  3957. tcs = netdev_get_num_tc(dev);
  3958. if (tcs > 1) {
  3959. bp->tx_nr_rings_per_tc = bp->tx_nr_rings / tcs;
  3960. if (bp->tx_nr_rings_per_tc == 0) {
  3961. netdev_reset_tc(dev);
  3962. bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
  3963. } else {
  3964. int i, off, count;
  3965. bp->tx_nr_rings = bp->tx_nr_rings_per_tc * tcs;
  3966. for (i = 0; i < tcs; i++) {
  3967. count = bp->tx_nr_rings_per_tc;
  3968. off = i * count;
  3969. netdev_set_tc_queue(dev, i, count, off);
  3970. }
  3971. }
  3972. }
  3973. for (i = 0; i < bp->cp_nr_rings; i++) {
  3974. char *attr;
  3975. if (bp->flags & BNXT_FLAG_SHARED_RINGS)
  3976. attr = "TxRx";
  3977. else if (i < bp->rx_nr_rings)
  3978. attr = "rx";
  3979. else
  3980. attr = "tx";
  3981. snprintf(bp->irq_tbl[i].name, len, "%s-%s-%d", dev->name, attr,
  3982. i);
  3983. bp->irq_tbl[i].handler = bnxt_msix;
  3984. }
  3985. }
  3986. static void bnxt_setup_inta(struct bnxt *bp)
  3987. {
  3988. const int len = sizeof(bp->irq_tbl[0].name);
  3989. if (netdev_get_num_tc(bp->dev))
  3990. netdev_reset_tc(bp->dev);
  3991. snprintf(bp->irq_tbl[0].name, len, "%s-%s-%d", bp->dev->name, "TxRx",
  3992. 0);
  3993. bp->irq_tbl[0].handler = bnxt_inta;
  3994. }
  3995. static int bnxt_setup_int_mode(struct bnxt *bp)
  3996. {
  3997. int rc;
  3998. if (bp->flags & BNXT_FLAG_USING_MSIX)
  3999. bnxt_setup_msix(bp);
  4000. else
  4001. bnxt_setup_inta(bp);
  4002. rc = bnxt_set_real_num_queues(bp);
  4003. return rc;
  4004. }
  4005. unsigned int bnxt_get_max_func_stat_ctxs(struct bnxt *bp)
  4006. {
  4007. #if defined(CONFIG_BNXT_SRIOV)
  4008. if (BNXT_VF(bp))
  4009. return bp->vf.max_stat_ctxs;
  4010. #endif
  4011. return bp->pf.max_stat_ctxs;
  4012. }
  4013. void bnxt_set_max_func_stat_ctxs(struct bnxt *bp, unsigned int max)
  4014. {
  4015. #if defined(CONFIG_BNXT_SRIOV)
  4016. if (BNXT_VF(bp))
  4017. bp->vf.max_stat_ctxs = max;
  4018. else
  4019. #endif
  4020. bp->pf.max_stat_ctxs = max;
  4021. }
  4022. unsigned int bnxt_get_max_func_cp_rings(struct bnxt *bp)
  4023. {
  4024. #if defined(CONFIG_BNXT_SRIOV)
  4025. if (BNXT_VF(bp))
  4026. return bp->vf.max_cp_rings;
  4027. #endif
  4028. return bp->pf.max_cp_rings;
  4029. }
  4030. void bnxt_set_max_func_cp_rings(struct bnxt *bp, unsigned int max)
  4031. {
  4032. #if defined(CONFIG_BNXT_SRIOV)
  4033. if (BNXT_VF(bp))
  4034. bp->vf.max_cp_rings = max;
  4035. else
  4036. #endif
  4037. bp->pf.max_cp_rings = max;
  4038. }
  4039. static unsigned int bnxt_get_max_func_irqs(struct bnxt *bp)
  4040. {
  4041. #if defined(CONFIG_BNXT_SRIOV)
  4042. if (BNXT_VF(bp))
  4043. return bp->vf.max_irqs;
  4044. #endif
  4045. return bp->pf.max_irqs;
  4046. }
  4047. void bnxt_set_max_func_irqs(struct bnxt *bp, unsigned int max_irqs)
  4048. {
  4049. #if defined(CONFIG_BNXT_SRIOV)
  4050. if (BNXT_VF(bp))
  4051. bp->vf.max_irqs = max_irqs;
  4052. else
  4053. #endif
  4054. bp->pf.max_irqs = max_irqs;
  4055. }
  4056. static int bnxt_init_msix(struct bnxt *bp)
  4057. {
  4058. int i, total_vecs, rc = 0, min = 1;
  4059. struct msix_entry *msix_ent;
  4060. total_vecs = bnxt_get_max_func_irqs(bp);
  4061. msix_ent = kcalloc(total_vecs, sizeof(struct msix_entry), GFP_KERNEL);
  4062. if (!msix_ent)
  4063. return -ENOMEM;
  4064. for (i = 0; i < total_vecs; i++) {
  4065. msix_ent[i].entry = i;
  4066. msix_ent[i].vector = 0;
  4067. }
  4068. if (!(bp->flags & BNXT_FLAG_SHARED_RINGS))
  4069. min = 2;
  4070. total_vecs = pci_enable_msix_range(bp->pdev, msix_ent, min, total_vecs);
  4071. if (total_vecs < 0) {
  4072. rc = -ENODEV;
  4073. goto msix_setup_exit;
  4074. }
  4075. bp->irq_tbl = kcalloc(total_vecs, sizeof(struct bnxt_irq), GFP_KERNEL);
  4076. if (bp->irq_tbl) {
  4077. for (i = 0; i < total_vecs; i++)
  4078. bp->irq_tbl[i].vector = msix_ent[i].vector;
  4079. bp->total_irqs = total_vecs;
  4080. /* Trim rings based upon num of vectors allocated */
  4081. rc = bnxt_trim_rings(bp, &bp->rx_nr_rings, &bp->tx_nr_rings,
  4082. total_vecs, min == 1);
  4083. if (rc)
  4084. goto msix_setup_exit;
  4085. bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
  4086. bp->cp_nr_rings = (min == 1) ?
  4087. max_t(int, bp->tx_nr_rings, bp->rx_nr_rings) :
  4088. bp->tx_nr_rings + bp->rx_nr_rings;
  4089. } else {
  4090. rc = -ENOMEM;
  4091. goto msix_setup_exit;
  4092. }
  4093. bp->flags |= BNXT_FLAG_USING_MSIX;
  4094. kfree(msix_ent);
  4095. return 0;
  4096. msix_setup_exit:
  4097. netdev_err(bp->dev, "bnxt_init_msix err: %x\n", rc);
  4098. kfree(bp->irq_tbl);
  4099. bp->irq_tbl = NULL;
  4100. pci_disable_msix(bp->pdev);
  4101. kfree(msix_ent);
  4102. return rc;
  4103. }
  4104. static int bnxt_init_inta(struct bnxt *bp)
  4105. {
  4106. bp->irq_tbl = kcalloc(1, sizeof(struct bnxt_irq), GFP_KERNEL);
  4107. if (!bp->irq_tbl)
  4108. return -ENOMEM;
  4109. bp->total_irqs = 1;
  4110. bp->rx_nr_rings = 1;
  4111. bp->tx_nr_rings = 1;
  4112. bp->cp_nr_rings = 1;
  4113. bp->tx_nr_rings_per_tc = bp->tx_nr_rings;
  4114. bp->flags |= BNXT_FLAG_SHARED_RINGS;
  4115. bp->irq_tbl[0].vector = bp->pdev->irq;
  4116. return 0;
  4117. }
  4118. static int bnxt_init_int_mode(struct bnxt *bp)
  4119. {
  4120. int rc = 0;
  4121. if (bp->flags & BNXT_FLAG_MSIX_CAP)
  4122. rc = bnxt_init_msix(bp);
  4123. if (!(bp->flags & BNXT_FLAG_USING_MSIX) && BNXT_PF(bp)) {
  4124. /* fallback to INTA */
  4125. rc = bnxt_init_inta(bp);
  4126. }
  4127. return rc;
  4128. }
  4129. static void bnxt_clear_int_mode(struct bnxt *bp)
  4130. {
  4131. if (bp->flags & BNXT_FLAG_USING_MSIX)
  4132. pci_disable_msix(bp->pdev);
  4133. kfree(bp->irq_tbl);
  4134. bp->irq_tbl = NULL;
  4135. bp->flags &= ~BNXT_FLAG_USING_MSIX;
  4136. }
  4137. static void bnxt_free_irq(struct bnxt *bp)
  4138. {
  4139. struct bnxt_irq *irq;
  4140. int i;
  4141. #ifdef CONFIG_RFS_ACCEL
  4142. free_irq_cpu_rmap(bp->dev->rx_cpu_rmap);
  4143. bp->dev->rx_cpu_rmap = NULL;
  4144. #endif
  4145. if (!bp->irq_tbl)
  4146. return;
  4147. for (i = 0; i < bp->cp_nr_rings; i++) {
  4148. irq = &bp->irq_tbl[i];
  4149. if (irq->requested)
  4150. free_irq(irq->vector, bp->bnapi[i]);
  4151. irq->requested = 0;
  4152. }
  4153. }
  4154. static int bnxt_request_irq(struct bnxt *bp)
  4155. {
  4156. int i, j, rc = 0;
  4157. unsigned long flags = 0;
  4158. #ifdef CONFIG_RFS_ACCEL
  4159. struct cpu_rmap *rmap = bp->dev->rx_cpu_rmap;
  4160. #endif
  4161. if (!(bp->flags & BNXT_FLAG_USING_MSIX))
  4162. flags = IRQF_SHARED;
  4163. for (i = 0, j = 0; i < bp->cp_nr_rings; i++) {
  4164. struct bnxt_irq *irq = &bp->irq_tbl[i];
  4165. #ifdef CONFIG_RFS_ACCEL
  4166. if (rmap && bp->bnapi[i]->rx_ring) {
  4167. rc = irq_cpu_rmap_add(rmap, irq->vector);
  4168. if (rc)
  4169. netdev_warn(bp->dev, "failed adding irq rmap for ring %d\n",
  4170. j);
  4171. j++;
  4172. }
  4173. #endif
  4174. rc = request_irq(irq->vector, irq->handler, flags, irq->name,
  4175. bp->bnapi[i]);
  4176. if (rc)
  4177. break;
  4178. irq->requested = 1;
  4179. }
  4180. return rc;
  4181. }
  4182. static void bnxt_del_napi(struct bnxt *bp)
  4183. {
  4184. int i;
  4185. if (!bp->bnapi)
  4186. return;
  4187. for (i = 0; i < bp->cp_nr_rings; i++) {
  4188. struct bnxt_napi *bnapi = bp->bnapi[i];
  4189. napi_hash_del(&bnapi->napi);
  4190. netif_napi_del(&bnapi->napi);
  4191. }
  4192. /* We called napi_hash_del() before netif_napi_del(), we need
  4193. * to respect an RCU grace period before freeing napi structures.
  4194. */
  4195. synchronize_net();
  4196. }
  4197. static void bnxt_init_napi(struct bnxt *bp)
  4198. {
  4199. int i;
  4200. unsigned int cp_nr_rings = bp->cp_nr_rings;
  4201. struct bnxt_napi *bnapi;
  4202. if (bp->flags & BNXT_FLAG_USING_MSIX) {
  4203. if (BNXT_CHIP_TYPE_NITRO_A0(bp))
  4204. cp_nr_rings--;
  4205. for (i = 0; i < cp_nr_rings; i++) {
  4206. bnapi = bp->bnapi[i];
  4207. netif_napi_add(bp->dev, &bnapi->napi,
  4208. bnxt_poll, 64);
  4209. }
  4210. if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
  4211. bnapi = bp->bnapi[cp_nr_rings];
  4212. netif_napi_add(bp->dev, &bnapi->napi,
  4213. bnxt_poll_nitroa0, 64);
  4214. }
  4215. } else {
  4216. bnapi = bp->bnapi[0];
  4217. netif_napi_add(bp->dev, &bnapi->napi, bnxt_poll, 64);
  4218. }
  4219. }
  4220. static void bnxt_disable_napi(struct bnxt *bp)
  4221. {
  4222. int i;
  4223. if (!bp->bnapi)
  4224. return;
  4225. for (i = 0; i < bp->cp_nr_rings; i++) {
  4226. napi_disable(&bp->bnapi[i]->napi);
  4227. bnxt_disable_poll(bp->bnapi[i]);
  4228. }
  4229. }
  4230. static void bnxt_enable_napi(struct bnxt *bp)
  4231. {
  4232. int i;
  4233. for (i = 0; i < bp->cp_nr_rings; i++) {
  4234. bp->bnapi[i]->in_reset = false;
  4235. bnxt_enable_poll(bp->bnapi[i]);
  4236. napi_enable(&bp->bnapi[i]->napi);
  4237. }
  4238. }
  4239. void bnxt_tx_disable(struct bnxt *bp)
  4240. {
  4241. int i;
  4242. struct bnxt_tx_ring_info *txr;
  4243. struct netdev_queue *txq;
  4244. if (bp->tx_ring) {
  4245. for (i = 0; i < bp->tx_nr_rings; i++) {
  4246. txr = &bp->tx_ring[i];
  4247. txq = netdev_get_tx_queue(bp->dev, i);
  4248. txr->dev_state = BNXT_DEV_STATE_CLOSING;
  4249. }
  4250. }
  4251. /* Stop all TX queues */
  4252. netif_tx_disable(bp->dev);
  4253. netif_carrier_off(bp->dev);
  4254. }
  4255. void bnxt_tx_enable(struct bnxt *bp)
  4256. {
  4257. int i;
  4258. struct bnxt_tx_ring_info *txr;
  4259. struct netdev_queue *txq;
  4260. for (i = 0; i < bp->tx_nr_rings; i++) {
  4261. txr = &bp->tx_ring[i];
  4262. txq = netdev_get_tx_queue(bp->dev, i);
  4263. txr->dev_state = 0;
  4264. }
  4265. netif_tx_wake_all_queues(bp->dev);
  4266. if (bp->link_info.link_up)
  4267. netif_carrier_on(bp->dev);
  4268. }
  4269. static void bnxt_report_link(struct bnxt *bp)
  4270. {
  4271. if (bp->link_info.link_up) {
  4272. const char *duplex;
  4273. const char *flow_ctrl;
  4274. u16 speed;
  4275. netif_carrier_on(bp->dev);
  4276. if (bp->link_info.duplex == BNXT_LINK_DUPLEX_FULL)
  4277. duplex = "full";
  4278. else
  4279. duplex = "half";
  4280. if (bp->link_info.pause == BNXT_LINK_PAUSE_BOTH)
  4281. flow_ctrl = "ON - receive & transmit";
  4282. else if (bp->link_info.pause == BNXT_LINK_PAUSE_TX)
  4283. flow_ctrl = "ON - transmit";
  4284. else if (bp->link_info.pause == BNXT_LINK_PAUSE_RX)
  4285. flow_ctrl = "ON - receive";
  4286. else
  4287. flow_ctrl = "none";
  4288. speed = bnxt_fw_to_ethtool_speed(bp->link_info.link_speed);
  4289. netdev_info(bp->dev, "NIC Link is Up, %d Mbps %s duplex, Flow control: %s\n",
  4290. speed, duplex, flow_ctrl);
  4291. if (bp->flags & BNXT_FLAG_EEE_CAP)
  4292. netdev_info(bp->dev, "EEE is %s\n",
  4293. bp->eee.eee_active ? "active" :
  4294. "not active");
  4295. } else {
  4296. netif_carrier_off(bp->dev);
  4297. netdev_err(bp->dev, "NIC Link is Down\n");
  4298. }
  4299. }
  4300. static int bnxt_hwrm_phy_qcaps(struct bnxt *bp)
  4301. {
  4302. int rc = 0;
  4303. struct hwrm_port_phy_qcaps_input req = {0};
  4304. struct hwrm_port_phy_qcaps_output *resp = bp->hwrm_cmd_resp_addr;
  4305. struct bnxt_link_info *link_info = &bp->link_info;
  4306. if (bp->hwrm_spec_code < 0x10201)
  4307. return 0;
  4308. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_QCAPS, -1, -1);
  4309. mutex_lock(&bp->hwrm_cmd_lock);
  4310. rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  4311. if (rc)
  4312. goto hwrm_phy_qcaps_exit;
  4313. if (resp->eee_supported & PORT_PHY_QCAPS_RESP_EEE_SUPPORTED) {
  4314. struct ethtool_eee *eee = &bp->eee;
  4315. u16 fw_speeds = le16_to_cpu(resp->supported_speeds_eee_mode);
  4316. bp->flags |= BNXT_FLAG_EEE_CAP;
  4317. eee->supported = _bnxt_fw_to_ethtool_adv_spds(fw_speeds, 0);
  4318. bp->lpi_tmr_lo = le32_to_cpu(resp->tx_lpi_timer_low) &
  4319. PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_LOW_MASK;
  4320. bp->lpi_tmr_hi = le32_to_cpu(resp->valid_tx_lpi_timer_high) &
  4321. PORT_PHY_QCAPS_RESP_TX_LPI_TIMER_HIGH_MASK;
  4322. }
  4323. link_info->support_auto_speeds =
  4324. le16_to_cpu(resp->supported_speeds_auto_mode);
  4325. hwrm_phy_qcaps_exit:
  4326. mutex_unlock(&bp->hwrm_cmd_lock);
  4327. return rc;
  4328. }
  4329. static int bnxt_update_link(struct bnxt *bp, bool chng_link_state)
  4330. {
  4331. int rc = 0;
  4332. struct bnxt_link_info *link_info = &bp->link_info;
  4333. struct hwrm_port_phy_qcfg_input req = {0};
  4334. struct hwrm_port_phy_qcfg_output *resp = bp->hwrm_cmd_resp_addr;
  4335. u8 link_up = link_info->link_up;
  4336. u16 diff;
  4337. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_QCFG, -1, -1);
  4338. mutex_lock(&bp->hwrm_cmd_lock);
  4339. rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  4340. if (rc) {
  4341. mutex_unlock(&bp->hwrm_cmd_lock);
  4342. return rc;
  4343. }
  4344. memcpy(&link_info->phy_qcfg_resp, resp, sizeof(*resp));
  4345. link_info->phy_link_status = resp->link;
  4346. link_info->duplex = resp->duplex;
  4347. link_info->pause = resp->pause;
  4348. link_info->auto_mode = resp->auto_mode;
  4349. link_info->auto_pause_setting = resp->auto_pause;
  4350. link_info->lp_pause = resp->link_partner_adv_pause;
  4351. link_info->force_pause_setting = resp->force_pause;
  4352. link_info->duplex_setting = resp->duplex;
  4353. if (link_info->phy_link_status == BNXT_LINK_LINK)
  4354. link_info->link_speed = le16_to_cpu(resp->link_speed);
  4355. else
  4356. link_info->link_speed = 0;
  4357. link_info->force_link_speed = le16_to_cpu(resp->force_link_speed);
  4358. link_info->support_speeds = le16_to_cpu(resp->support_speeds);
  4359. link_info->auto_link_speeds = le16_to_cpu(resp->auto_link_speed_mask);
  4360. link_info->lp_auto_link_speeds =
  4361. le16_to_cpu(resp->link_partner_adv_speeds);
  4362. link_info->preemphasis = le32_to_cpu(resp->preemphasis);
  4363. link_info->phy_ver[0] = resp->phy_maj;
  4364. link_info->phy_ver[1] = resp->phy_min;
  4365. link_info->phy_ver[2] = resp->phy_bld;
  4366. link_info->media_type = resp->media_type;
  4367. link_info->phy_type = resp->phy_type;
  4368. link_info->transceiver = resp->xcvr_pkg_type;
  4369. link_info->phy_addr = resp->eee_config_phy_addr &
  4370. PORT_PHY_QCFG_RESP_PHY_ADDR_MASK;
  4371. link_info->module_status = resp->module_status;
  4372. if (bp->flags & BNXT_FLAG_EEE_CAP) {
  4373. struct ethtool_eee *eee = &bp->eee;
  4374. u16 fw_speeds;
  4375. eee->eee_active = 0;
  4376. if (resp->eee_config_phy_addr &
  4377. PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ACTIVE) {
  4378. eee->eee_active = 1;
  4379. fw_speeds = le16_to_cpu(
  4380. resp->link_partner_adv_eee_link_speed_mask);
  4381. eee->lp_advertised =
  4382. _bnxt_fw_to_ethtool_adv_spds(fw_speeds, 0);
  4383. }
  4384. /* Pull initial EEE config */
  4385. if (!chng_link_state) {
  4386. if (resp->eee_config_phy_addr &
  4387. PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_ENABLED)
  4388. eee->eee_enabled = 1;
  4389. fw_speeds = le16_to_cpu(resp->adv_eee_link_speed_mask);
  4390. eee->advertised =
  4391. _bnxt_fw_to_ethtool_adv_spds(fw_speeds, 0);
  4392. if (resp->eee_config_phy_addr &
  4393. PORT_PHY_QCFG_RESP_EEE_CONFIG_EEE_TX_LPI) {
  4394. __le32 tmr;
  4395. eee->tx_lpi_enabled = 1;
  4396. tmr = resp->xcvr_identifier_type_tx_lpi_timer;
  4397. eee->tx_lpi_timer = le32_to_cpu(tmr) &
  4398. PORT_PHY_QCFG_RESP_TX_LPI_TIMER_MASK;
  4399. }
  4400. }
  4401. }
  4402. /* TODO: need to add more logic to report VF link */
  4403. if (chng_link_state) {
  4404. if (link_info->phy_link_status == BNXT_LINK_LINK)
  4405. link_info->link_up = 1;
  4406. else
  4407. link_info->link_up = 0;
  4408. if (link_up != link_info->link_up)
  4409. bnxt_report_link(bp);
  4410. } else {
  4411. /* alwasy link down if not require to update link state */
  4412. link_info->link_up = 0;
  4413. }
  4414. mutex_unlock(&bp->hwrm_cmd_lock);
  4415. diff = link_info->support_auto_speeds ^ link_info->advertising;
  4416. if ((link_info->support_auto_speeds | diff) !=
  4417. link_info->support_auto_speeds) {
  4418. /* An advertised speed is no longer supported, so we need to
  4419. * update the advertisement settings. See bnxt_reset() for
  4420. * comments about the rtnl_lock() sequence below.
  4421. */
  4422. clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
  4423. rtnl_lock();
  4424. link_info->advertising = link_info->support_auto_speeds;
  4425. if (test_bit(BNXT_STATE_OPEN, &bp->state) &&
  4426. (link_info->autoneg & BNXT_AUTONEG_SPEED))
  4427. bnxt_hwrm_set_link_setting(bp, true, false);
  4428. set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
  4429. rtnl_unlock();
  4430. }
  4431. return 0;
  4432. }
  4433. static void bnxt_get_port_module_status(struct bnxt *bp)
  4434. {
  4435. struct bnxt_link_info *link_info = &bp->link_info;
  4436. struct hwrm_port_phy_qcfg_output *resp = &link_info->phy_qcfg_resp;
  4437. u8 module_status;
  4438. if (bnxt_update_link(bp, true))
  4439. return;
  4440. module_status = link_info->module_status;
  4441. switch (module_status) {
  4442. case PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX:
  4443. case PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN:
  4444. case PORT_PHY_QCFG_RESP_MODULE_STATUS_WARNINGMSG:
  4445. netdev_warn(bp->dev, "Unqualified SFP+ module detected on port %d\n",
  4446. bp->pf.port_id);
  4447. if (bp->hwrm_spec_code >= 0x10201) {
  4448. netdev_warn(bp->dev, "Module part number %s\n",
  4449. resp->phy_vendor_partnumber);
  4450. }
  4451. if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_DISABLETX)
  4452. netdev_warn(bp->dev, "TX is disabled\n");
  4453. if (module_status == PORT_PHY_QCFG_RESP_MODULE_STATUS_PWRDOWN)
  4454. netdev_warn(bp->dev, "SFP+ module is shutdown\n");
  4455. }
  4456. }
  4457. static void
  4458. bnxt_hwrm_set_pause_common(struct bnxt *bp, struct hwrm_port_phy_cfg_input *req)
  4459. {
  4460. if (bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) {
  4461. if (bp->hwrm_spec_code >= 0x10201)
  4462. req->auto_pause =
  4463. PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE;
  4464. if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
  4465. req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_RX;
  4466. if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
  4467. req->auto_pause |= PORT_PHY_CFG_REQ_AUTO_PAUSE_TX;
  4468. req->enables |=
  4469. cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
  4470. } else {
  4471. if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_RX)
  4472. req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_RX;
  4473. if (bp->link_info.req_flow_ctrl & BNXT_LINK_PAUSE_TX)
  4474. req->force_pause |= PORT_PHY_CFG_REQ_FORCE_PAUSE_TX;
  4475. req->enables |=
  4476. cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_FORCE_PAUSE);
  4477. if (bp->hwrm_spec_code >= 0x10201) {
  4478. req->auto_pause = req->force_pause;
  4479. req->enables |= cpu_to_le32(
  4480. PORT_PHY_CFG_REQ_ENABLES_AUTO_PAUSE);
  4481. }
  4482. }
  4483. }
  4484. static void bnxt_hwrm_set_link_common(struct bnxt *bp,
  4485. struct hwrm_port_phy_cfg_input *req)
  4486. {
  4487. u8 autoneg = bp->link_info.autoneg;
  4488. u16 fw_link_speed = bp->link_info.req_link_speed;
  4489. u32 advertising = bp->link_info.advertising;
  4490. if (autoneg & BNXT_AUTONEG_SPEED) {
  4491. req->auto_mode |=
  4492. PORT_PHY_CFG_REQ_AUTO_MODE_SPEED_MASK;
  4493. req->enables |= cpu_to_le32(
  4494. PORT_PHY_CFG_REQ_ENABLES_AUTO_LINK_SPEED_MASK);
  4495. req->auto_link_speed_mask = cpu_to_le16(advertising);
  4496. req->enables |= cpu_to_le32(PORT_PHY_CFG_REQ_ENABLES_AUTO_MODE);
  4497. req->flags |=
  4498. cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESTART_AUTONEG);
  4499. } else {
  4500. req->force_link_speed = cpu_to_le16(fw_link_speed);
  4501. req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE);
  4502. }
  4503. /* tell chimp that the setting takes effect immediately */
  4504. req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_RESET_PHY);
  4505. }
  4506. int bnxt_hwrm_set_pause(struct bnxt *bp)
  4507. {
  4508. struct hwrm_port_phy_cfg_input req = {0};
  4509. int rc;
  4510. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_CFG, -1, -1);
  4511. bnxt_hwrm_set_pause_common(bp, &req);
  4512. if ((bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL) ||
  4513. bp->link_info.force_link_chng)
  4514. bnxt_hwrm_set_link_common(bp, &req);
  4515. mutex_lock(&bp->hwrm_cmd_lock);
  4516. rc = _hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  4517. if (!rc && !(bp->link_info.autoneg & BNXT_AUTONEG_FLOW_CTRL)) {
  4518. /* since changing of pause setting doesn't trigger any link
  4519. * change event, the driver needs to update the current pause
  4520. * result upon successfully return of the phy_cfg command
  4521. */
  4522. bp->link_info.pause =
  4523. bp->link_info.force_pause_setting = bp->link_info.req_flow_ctrl;
  4524. bp->link_info.auto_pause_setting = 0;
  4525. if (!bp->link_info.force_link_chng)
  4526. bnxt_report_link(bp);
  4527. }
  4528. bp->link_info.force_link_chng = false;
  4529. mutex_unlock(&bp->hwrm_cmd_lock);
  4530. return rc;
  4531. }
  4532. static void bnxt_hwrm_set_eee(struct bnxt *bp,
  4533. struct hwrm_port_phy_cfg_input *req)
  4534. {
  4535. struct ethtool_eee *eee = &bp->eee;
  4536. if (eee->eee_enabled) {
  4537. u16 eee_speeds;
  4538. u32 flags = PORT_PHY_CFG_REQ_FLAGS_EEE_ENABLE;
  4539. if (eee->tx_lpi_enabled)
  4540. flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_ENABLE;
  4541. else
  4542. flags |= PORT_PHY_CFG_REQ_FLAGS_EEE_TX_LPI_DISABLE;
  4543. req->flags |= cpu_to_le32(flags);
  4544. eee_speeds = bnxt_get_fw_auto_link_speeds(eee->advertised);
  4545. req->eee_link_speed_mask = cpu_to_le16(eee_speeds);
  4546. req->tx_lpi_timer = cpu_to_le32(eee->tx_lpi_timer);
  4547. } else {
  4548. req->flags |= cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_EEE_DISABLE);
  4549. }
  4550. }
  4551. int bnxt_hwrm_set_link_setting(struct bnxt *bp, bool set_pause, bool set_eee)
  4552. {
  4553. struct hwrm_port_phy_cfg_input req = {0};
  4554. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_CFG, -1, -1);
  4555. if (set_pause)
  4556. bnxt_hwrm_set_pause_common(bp, &req);
  4557. bnxt_hwrm_set_link_common(bp, &req);
  4558. if (set_eee)
  4559. bnxt_hwrm_set_eee(bp, &req);
  4560. return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  4561. }
  4562. static int bnxt_hwrm_shutdown_link(struct bnxt *bp)
  4563. {
  4564. struct hwrm_port_phy_cfg_input req = {0};
  4565. if (!BNXT_SINGLE_PF(bp))
  4566. return 0;
  4567. if (pci_num_vf(bp->pdev))
  4568. return 0;
  4569. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_PORT_PHY_CFG, -1, -1);
  4570. req.flags = cpu_to_le32(PORT_PHY_CFG_REQ_FLAGS_FORCE_LINK_DWN);
  4571. return hwrm_send_message(bp, &req, sizeof(req), HWRM_CMD_TIMEOUT);
  4572. }
  4573. static bool bnxt_eee_config_ok(struct bnxt *bp)
  4574. {
  4575. struct ethtool_eee *eee = &bp->eee;
  4576. struct bnxt_link_info *link_info = &bp->link_info;
  4577. if (!(bp->flags & BNXT_FLAG_EEE_CAP))
  4578. return true;
  4579. if (eee->eee_enabled) {
  4580. u32 advertising =
  4581. _bnxt_fw_to_ethtool_adv_spds(link_info->advertising, 0);
  4582. if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
  4583. eee->eee_enabled = 0;
  4584. return false;
  4585. }
  4586. if (eee->advertised & ~advertising) {
  4587. eee->advertised = advertising & eee->supported;
  4588. return false;
  4589. }
  4590. }
  4591. return true;
  4592. }
  4593. static int bnxt_update_phy_setting(struct bnxt *bp)
  4594. {
  4595. int rc;
  4596. bool update_link = false;
  4597. bool update_pause = false;
  4598. bool update_eee = false;
  4599. struct bnxt_link_info *link_info = &bp->link_info;
  4600. rc = bnxt_update_link(bp, true);
  4601. if (rc) {
  4602. netdev_err(bp->dev, "failed to update link (rc: %x)\n",
  4603. rc);
  4604. return rc;
  4605. }
  4606. if ((link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
  4607. (link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH) !=
  4608. link_info->req_flow_ctrl)
  4609. update_pause = true;
  4610. if (!(link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL) &&
  4611. link_info->force_pause_setting != link_info->req_flow_ctrl)
  4612. update_pause = true;
  4613. if (!(link_info->autoneg & BNXT_AUTONEG_SPEED)) {
  4614. if (BNXT_AUTO_MODE(link_info->auto_mode))
  4615. update_link = true;
  4616. if (link_info->req_link_speed != link_info->force_link_speed)
  4617. update_link = true;
  4618. if (link_info->req_duplex != link_info->duplex_setting)
  4619. update_link = true;
  4620. } else {
  4621. if (link_info->auto_mode == BNXT_LINK_AUTO_NONE)
  4622. update_link = true;
  4623. if (link_info->advertising != link_info->auto_link_speeds)
  4624. update_link = true;
  4625. }
  4626. /* The last close may have shutdown the link, so need to call
  4627. * PHY_CFG to bring it back up.
  4628. */
  4629. if (!netif_carrier_ok(bp->dev))
  4630. update_link = true;
  4631. if (!bnxt_eee_config_ok(bp))
  4632. update_eee = true;
  4633. if (update_link)
  4634. rc = bnxt_hwrm_set_link_setting(bp, update_pause, update_eee);
  4635. else if (update_pause)
  4636. rc = bnxt_hwrm_set_pause(bp);
  4637. if (rc) {
  4638. netdev_err(bp->dev, "failed to update phy setting (rc: %x)\n",
  4639. rc);
  4640. return rc;
  4641. }
  4642. return rc;
  4643. }
  4644. /* Common routine to pre-map certain register block to different GRC window.
  4645. * A PF has 16 4K windows and a VF has 4 4K windows. However, only 15 windows
  4646. * in PF and 3 windows in VF that can be customized to map in different
  4647. * register blocks.
  4648. */
  4649. static void bnxt_preset_reg_win(struct bnxt *bp)
  4650. {
  4651. if (BNXT_PF(bp)) {
  4652. /* CAG registers map to GRC window #4 */
  4653. writel(BNXT_CAG_REG_BASE,
  4654. bp->bar0 + BNXT_GRCPF_REG_WINDOW_BASE_OUT + 12);
  4655. }
  4656. }
  4657. static int __bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
  4658. {
  4659. int rc = 0;
  4660. bnxt_preset_reg_win(bp);
  4661. netif_carrier_off(bp->dev);
  4662. if (irq_re_init) {
  4663. rc = bnxt_setup_int_mode(bp);
  4664. if (rc) {
  4665. netdev_err(bp->dev, "bnxt_setup_int_mode err: %x\n",
  4666. rc);
  4667. return rc;
  4668. }
  4669. }
  4670. if ((bp->flags & BNXT_FLAG_RFS) &&
  4671. !(bp->flags & BNXT_FLAG_USING_MSIX)) {
  4672. /* disable RFS if falling back to INTA */
  4673. bp->dev->hw_features &= ~NETIF_F_NTUPLE;
  4674. bp->flags &= ~BNXT_FLAG_RFS;
  4675. }
  4676. rc = bnxt_alloc_mem(bp, irq_re_init);
  4677. if (rc) {
  4678. netdev_err(bp->dev, "bnxt_alloc_mem err: %x\n", rc);
  4679. goto open_err_free_mem;
  4680. }
  4681. if (irq_re_init) {
  4682. bnxt_init_napi(bp);
  4683. rc = bnxt_request_irq(bp);
  4684. if (rc) {
  4685. netdev_err(bp->dev, "bnxt_request_irq err: %x\n", rc);
  4686. goto open_err;
  4687. }
  4688. }
  4689. bnxt_enable_napi(bp);
  4690. rc = bnxt_init_nic(bp, irq_re_init);
  4691. if (rc) {
  4692. netdev_err(bp->dev, "bnxt_init_nic err: %x\n", rc);
  4693. goto open_err;
  4694. }
  4695. if (link_re_init) {
  4696. rc = bnxt_update_phy_setting(bp);
  4697. if (rc)
  4698. netdev_warn(bp->dev, "failed to update phy settings\n");
  4699. }
  4700. if (irq_re_init)
  4701. udp_tunnel_get_rx_info(bp->dev);
  4702. set_bit(BNXT_STATE_OPEN, &bp->state);
  4703. bnxt_enable_int(bp);
  4704. /* Enable TX queues */
  4705. bnxt_tx_enable(bp);
  4706. mod_timer(&bp->timer, jiffies + bp->current_interval);
  4707. /* Poll link status and check for SFP+ module status */
  4708. bnxt_get_port_module_status(bp);
  4709. return 0;
  4710. open_err:
  4711. bnxt_disable_napi(bp);
  4712. bnxt_del_napi(bp);
  4713. open_err_free_mem:
  4714. bnxt_free_skbs(bp);
  4715. bnxt_free_irq(bp);
  4716. bnxt_free_mem(bp, true);
  4717. return rc;
  4718. }
  4719. /* rtnl_lock held */
  4720. int bnxt_open_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
  4721. {
  4722. int rc = 0;
  4723. rc = __bnxt_open_nic(bp, irq_re_init, link_re_init);
  4724. if (rc) {
  4725. netdev_err(bp->dev, "nic open fail (rc: %x)\n", rc);
  4726. dev_close(bp->dev);
  4727. }
  4728. return rc;
  4729. }
  4730. static int bnxt_open(struct net_device *dev)
  4731. {
  4732. struct bnxt *bp = netdev_priv(dev);
  4733. return __bnxt_open_nic(bp, true, true);
  4734. }
  4735. static void bnxt_disable_int_sync(struct bnxt *bp)
  4736. {
  4737. int i;
  4738. atomic_inc(&bp->intr_sem);
  4739. if (!netif_running(bp->dev))
  4740. return;
  4741. bnxt_disable_int(bp);
  4742. for (i = 0; i < bp->cp_nr_rings; i++)
  4743. synchronize_irq(bp->irq_tbl[i].vector);
  4744. }
  4745. int bnxt_close_nic(struct bnxt *bp, bool irq_re_init, bool link_re_init)
  4746. {
  4747. int rc = 0;
  4748. #ifdef CONFIG_BNXT_SRIOV
  4749. if (bp->sriov_cfg) {
  4750. rc = wait_event_interruptible_timeout(bp->sriov_cfg_wait,
  4751. !bp->sriov_cfg,
  4752. BNXT_SRIOV_CFG_WAIT_TMO);
  4753. if (rc)
  4754. netdev_warn(bp->dev, "timeout waiting for SRIOV config operation to complete!\n");
  4755. }
  4756. #endif
  4757. /* Change device state to avoid TX queue wake up's */
  4758. bnxt_tx_disable(bp);
  4759. clear_bit(BNXT_STATE_OPEN, &bp->state);
  4760. smp_mb__after_atomic();
  4761. while (test_bit(BNXT_STATE_IN_SP_TASK, &bp->state))
  4762. msleep(20);
  4763. /* Flush rings before disabling interrupts */
  4764. bnxt_shutdown_nic(bp, irq_re_init);
  4765. /* TODO CHIMP_FW: Link/PHY related cleanup if (link_re_init) */
  4766. bnxt_disable_napi(bp);
  4767. bnxt_disable_int_sync(bp);
  4768. del_timer_sync(&bp->timer);
  4769. bnxt_free_skbs(bp);
  4770. if (irq_re_init) {
  4771. bnxt_free_irq(bp);
  4772. bnxt_del_napi(bp);
  4773. }
  4774. bnxt_free_mem(bp, irq_re_init);
  4775. return rc;
  4776. }
  4777. static int bnxt_close(struct net_device *dev)
  4778. {
  4779. struct bnxt *bp = netdev_priv(dev);
  4780. bnxt_close_nic(bp, true, true);
  4781. bnxt_hwrm_shutdown_link(bp);
  4782. return 0;
  4783. }
  4784. /* rtnl_lock held */
  4785. static int bnxt_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  4786. {
  4787. switch (cmd) {
  4788. case SIOCGMIIPHY:
  4789. /* fallthru */
  4790. case SIOCGMIIREG: {
  4791. if (!netif_running(dev))
  4792. return -EAGAIN;
  4793. return 0;
  4794. }
  4795. case SIOCSMIIREG:
  4796. if (!netif_running(dev))
  4797. return -EAGAIN;
  4798. return 0;
  4799. default:
  4800. /* do nothing */
  4801. break;
  4802. }
  4803. return -EOPNOTSUPP;
  4804. }
  4805. static struct rtnl_link_stats64 *
  4806. bnxt_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
  4807. {
  4808. u32 i;
  4809. struct bnxt *bp = netdev_priv(dev);
  4810. memset(stats, 0, sizeof(struct rtnl_link_stats64));
  4811. if (!bp->bnapi)
  4812. return stats;
  4813. /* TODO check if we need to synchronize with bnxt_close path */
  4814. for (i = 0; i < bp->cp_nr_rings; i++) {
  4815. struct bnxt_napi *bnapi = bp->bnapi[i];
  4816. struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
  4817. struct ctx_hw_stats *hw_stats = cpr->hw_stats;
  4818. stats->rx_packets += le64_to_cpu(hw_stats->rx_ucast_pkts);
  4819. stats->rx_packets += le64_to_cpu(hw_stats->rx_mcast_pkts);
  4820. stats->rx_packets += le64_to_cpu(hw_stats->rx_bcast_pkts);
  4821. stats->tx_packets += le64_to_cpu(hw_stats->tx_ucast_pkts);
  4822. stats->tx_packets += le64_to_cpu(hw_stats->tx_mcast_pkts);
  4823. stats->tx_packets += le64_to_cpu(hw_stats->tx_bcast_pkts);
  4824. stats->rx_bytes += le64_to_cpu(hw_stats->rx_ucast_bytes);
  4825. stats->rx_bytes += le64_to_cpu(hw_stats->rx_mcast_bytes);
  4826. stats->rx_bytes += le64_to_cpu(hw_stats->rx_bcast_bytes);
  4827. stats->tx_bytes += le64_to_cpu(hw_stats->tx_ucast_bytes);
  4828. stats->tx_bytes += le64_to_cpu(hw_stats->tx_mcast_bytes);
  4829. stats->tx_bytes += le64_to_cpu(hw_stats->tx_bcast_bytes);
  4830. stats->rx_missed_errors +=
  4831. le64_to_cpu(hw_stats->rx_discard_pkts);
  4832. stats->multicast += le64_to_cpu(hw_stats->rx_mcast_pkts);
  4833. stats->tx_dropped += le64_to_cpu(hw_stats->tx_drop_pkts);
  4834. }
  4835. if (bp->flags & BNXT_FLAG_PORT_STATS) {
  4836. struct rx_port_stats *rx = bp->hw_rx_port_stats;
  4837. struct tx_port_stats *tx = bp->hw_tx_port_stats;
  4838. stats->rx_crc_errors = le64_to_cpu(rx->rx_fcs_err_frames);
  4839. stats->rx_frame_errors = le64_to_cpu(rx->rx_align_err_frames);
  4840. stats->rx_length_errors = le64_to_cpu(rx->rx_undrsz_frames) +
  4841. le64_to_cpu(rx->rx_ovrsz_frames) +
  4842. le64_to_cpu(rx->rx_runt_frames);
  4843. stats->rx_errors = le64_to_cpu(rx->rx_false_carrier_frames) +
  4844. le64_to_cpu(rx->rx_jbr_frames);
  4845. stats->collisions = le64_to_cpu(tx->tx_total_collisions);
  4846. stats->tx_fifo_errors = le64_to_cpu(tx->tx_fifo_underruns);
  4847. stats->tx_errors = le64_to_cpu(tx->tx_err);
  4848. }
  4849. return stats;
  4850. }
  4851. static bool bnxt_mc_list_updated(struct bnxt *bp, u32 *rx_mask)
  4852. {
  4853. struct net_device *dev = bp->dev;
  4854. struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
  4855. struct netdev_hw_addr *ha;
  4856. u8 *haddr;
  4857. int mc_count = 0;
  4858. bool update = false;
  4859. int off = 0;
  4860. netdev_for_each_mc_addr(ha, dev) {
  4861. if (mc_count >= BNXT_MAX_MC_ADDRS) {
  4862. *rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
  4863. vnic->mc_list_count = 0;
  4864. return false;
  4865. }
  4866. haddr = ha->addr;
  4867. if (!ether_addr_equal(haddr, vnic->mc_list + off)) {
  4868. memcpy(vnic->mc_list + off, haddr, ETH_ALEN);
  4869. update = true;
  4870. }
  4871. off += ETH_ALEN;
  4872. mc_count++;
  4873. }
  4874. if (mc_count)
  4875. *rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_MCAST;
  4876. if (mc_count != vnic->mc_list_count) {
  4877. vnic->mc_list_count = mc_count;
  4878. update = true;
  4879. }
  4880. return update;
  4881. }
  4882. static bool bnxt_uc_list_updated(struct bnxt *bp)
  4883. {
  4884. struct net_device *dev = bp->dev;
  4885. struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
  4886. struct netdev_hw_addr *ha;
  4887. int off = 0;
  4888. if (netdev_uc_count(dev) != (vnic->uc_filter_count - 1))
  4889. return true;
  4890. netdev_for_each_uc_addr(ha, dev) {
  4891. if (!ether_addr_equal(ha->addr, vnic->uc_list + off))
  4892. return true;
  4893. off += ETH_ALEN;
  4894. }
  4895. return false;
  4896. }
  4897. static void bnxt_set_rx_mode(struct net_device *dev)
  4898. {
  4899. struct bnxt *bp = netdev_priv(dev);
  4900. struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
  4901. u32 mask = vnic->rx_mask;
  4902. bool mc_update = false;
  4903. bool uc_update;
  4904. if (!netif_running(dev))
  4905. return;
  4906. mask &= ~(CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS |
  4907. CFA_L2_SET_RX_MASK_REQ_MASK_MCAST |
  4908. CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST);
  4909. if ((dev->flags & IFF_PROMISC) && bnxt_promisc_ok(bp))
  4910. mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
  4911. uc_update = bnxt_uc_list_updated(bp);
  4912. if (dev->flags & IFF_ALLMULTI) {
  4913. mask |= CFA_L2_SET_RX_MASK_REQ_MASK_ALL_MCAST;
  4914. vnic->mc_list_count = 0;
  4915. } else {
  4916. mc_update = bnxt_mc_list_updated(bp, &mask);
  4917. }
  4918. if (mask != vnic->rx_mask || uc_update || mc_update) {
  4919. vnic->rx_mask = mask;
  4920. set_bit(BNXT_RX_MASK_SP_EVENT, &bp->sp_event);
  4921. schedule_work(&bp->sp_task);
  4922. }
  4923. }
  4924. static int bnxt_cfg_rx_mode(struct bnxt *bp)
  4925. {
  4926. struct net_device *dev = bp->dev;
  4927. struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
  4928. struct netdev_hw_addr *ha;
  4929. int i, off = 0, rc;
  4930. bool uc_update;
  4931. netif_addr_lock_bh(dev);
  4932. uc_update = bnxt_uc_list_updated(bp);
  4933. netif_addr_unlock_bh(dev);
  4934. if (!uc_update)
  4935. goto skip_uc;
  4936. mutex_lock(&bp->hwrm_cmd_lock);
  4937. for (i = 1; i < vnic->uc_filter_count; i++) {
  4938. struct hwrm_cfa_l2_filter_free_input req = {0};
  4939. bnxt_hwrm_cmd_hdr_init(bp, &req, HWRM_CFA_L2_FILTER_FREE, -1,
  4940. -1);
  4941. req.l2_filter_id = vnic->fw_l2_filter_id[i];
  4942. rc = _hwrm_send_message(bp, &req, sizeof(req),
  4943. HWRM_CMD_TIMEOUT);
  4944. }
  4945. mutex_unlock(&bp->hwrm_cmd_lock);
  4946. vnic->uc_filter_count = 1;
  4947. netif_addr_lock_bh(dev);
  4948. if (netdev_uc_count(dev) > (BNXT_MAX_UC_ADDRS - 1)) {
  4949. vnic->rx_mask |= CFA_L2_SET_RX_MASK_REQ_MASK_PROMISCUOUS;
  4950. } else {
  4951. netdev_for_each_uc_addr(ha, dev) {
  4952. memcpy(vnic->uc_list + off, ha->addr, ETH_ALEN);
  4953. off += ETH_ALEN;
  4954. vnic->uc_filter_count++;
  4955. }
  4956. }
  4957. netif_addr_unlock_bh(dev);
  4958. for (i = 1, off = 0; i < vnic->uc_filter_count; i++, off += ETH_ALEN) {
  4959. rc = bnxt_hwrm_set_vnic_filter(bp, 0, i, vnic->uc_list + off);
  4960. if (rc) {
  4961. netdev_err(bp->dev, "HWRM vnic filter failure rc: %x\n",
  4962. rc);
  4963. vnic->uc_filter_count = i;
  4964. return rc;
  4965. }
  4966. }
  4967. skip_uc:
  4968. rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, 0);
  4969. if (rc)
  4970. netdev_err(bp->dev, "HWRM cfa l2 rx mask failure rc: %x\n",
  4971. rc);
  4972. return rc;
  4973. }
  4974. static bool bnxt_rfs_capable(struct bnxt *bp)
  4975. {
  4976. #ifdef CONFIG_RFS_ACCEL
  4977. struct bnxt_pf_info *pf = &bp->pf;
  4978. int vnics;
  4979. if (BNXT_VF(bp) || !(bp->flags & BNXT_FLAG_MSIX_CAP))
  4980. return false;
  4981. vnics = 1 + bp->rx_nr_rings;
  4982. if (vnics > pf->max_rsscos_ctxs || vnics > pf->max_vnics) {
  4983. netdev_warn(bp->dev,
  4984. "Not enough resources to support NTUPLE filters, enough resources for up to %d rx rings\n",
  4985. min(pf->max_rsscos_ctxs - 1, pf->max_vnics - 1));
  4986. return false;
  4987. }
  4988. return true;
  4989. #else
  4990. return false;
  4991. #endif
  4992. }
  4993. static netdev_features_t bnxt_fix_features(struct net_device *dev,
  4994. netdev_features_t features)
  4995. {
  4996. struct bnxt *bp = netdev_priv(dev);
  4997. if ((features & NETIF_F_NTUPLE) && !bnxt_rfs_capable(bp))
  4998. features &= ~NETIF_F_NTUPLE;
  4999. /* Both CTAG and STAG VLAN accelaration on the RX side have to be
  5000. * turned on or off together.
  5001. */
  5002. if ((features & (NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_STAG_RX)) !=
  5003. (NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_STAG_RX)) {
  5004. if (dev->features & NETIF_F_HW_VLAN_CTAG_RX)
  5005. features &= ~(NETIF_F_HW_VLAN_CTAG_RX |
  5006. NETIF_F_HW_VLAN_STAG_RX);
  5007. else
  5008. features |= NETIF_F_HW_VLAN_CTAG_RX |
  5009. NETIF_F_HW_VLAN_STAG_RX;
  5010. }
  5011. #ifdef CONFIG_BNXT_SRIOV
  5012. if (BNXT_VF(bp)) {
  5013. if (bp->vf.vlan) {
  5014. features &= ~(NETIF_F_HW_VLAN_CTAG_RX |
  5015. NETIF_F_HW_VLAN_STAG_RX);
  5016. }
  5017. }
  5018. #endif
  5019. return features;
  5020. }
  5021. static int bnxt_set_features(struct net_device *dev, netdev_features_t features)
  5022. {
  5023. struct bnxt *bp = netdev_priv(dev);
  5024. u32 flags = bp->flags;
  5025. u32 changes;
  5026. int rc = 0;
  5027. bool re_init = false;
  5028. bool update_tpa = false;
  5029. flags &= ~BNXT_FLAG_ALL_CONFIG_FEATS;
  5030. if ((features & NETIF_F_GRO) && !BNXT_CHIP_TYPE_NITRO_A0(bp))
  5031. flags |= BNXT_FLAG_GRO;
  5032. if (features & NETIF_F_LRO)
  5033. flags |= BNXT_FLAG_LRO;
  5034. if (features & NETIF_F_HW_VLAN_CTAG_RX)
  5035. flags |= BNXT_FLAG_STRIP_VLAN;
  5036. if (features & NETIF_F_NTUPLE)
  5037. flags |= BNXT_FLAG_RFS;
  5038. changes = flags ^ bp->flags;
  5039. if (changes & BNXT_FLAG_TPA) {
  5040. update_tpa = true;
  5041. if ((bp->flags & BNXT_FLAG_TPA) == 0 ||
  5042. (flags & BNXT_FLAG_TPA) == 0)
  5043. re_init = true;
  5044. }
  5045. if (changes & ~BNXT_FLAG_TPA)
  5046. re_init = true;
  5047. if (flags != bp->flags) {
  5048. u32 old_flags = bp->flags;
  5049. bp->flags = flags;
  5050. if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
  5051. if (update_tpa)
  5052. bnxt_set_ring_params(bp);
  5053. return rc;
  5054. }
  5055. if (re_init) {
  5056. bnxt_close_nic(bp, false, false);
  5057. if (update_tpa)
  5058. bnxt_set_ring_params(bp);
  5059. return bnxt_open_nic(bp, false, false);
  5060. }
  5061. if (update_tpa) {
  5062. rc = bnxt_set_tpa(bp,
  5063. (flags & BNXT_FLAG_TPA) ?
  5064. true : false);
  5065. if (rc)
  5066. bp->flags = old_flags;
  5067. }
  5068. }
  5069. return rc;
  5070. }
  5071. static void bnxt_dump_tx_sw_state(struct bnxt_napi *bnapi)
  5072. {
  5073. struct bnxt_tx_ring_info *txr = bnapi->tx_ring;
  5074. int i = bnapi->index;
  5075. if (!txr)
  5076. return;
  5077. netdev_info(bnapi->bp->dev, "[%d]: tx{fw_ring: %d prod: %x cons: %x}\n",
  5078. i, txr->tx_ring_struct.fw_ring_id, txr->tx_prod,
  5079. txr->tx_cons);
  5080. }
  5081. static void bnxt_dump_rx_sw_state(struct bnxt_napi *bnapi)
  5082. {
  5083. struct bnxt_rx_ring_info *rxr = bnapi->rx_ring;
  5084. int i = bnapi->index;
  5085. if (!rxr)
  5086. return;
  5087. 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",
  5088. i, rxr->rx_ring_struct.fw_ring_id, rxr->rx_prod,
  5089. rxr->rx_agg_ring_struct.fw_ring_id, rxr->rx_agg_prod,
  5090. rxr->rx_sw_agg_prod);
  5091. }
  5092. static void bnxt_dump_cp_sw_state(struct bnxt_napi *bnapi)
  5093. {
  5094. struct bnxt_cp_ring_info *cpr = &bnapi->cp_ring;
  5095. int i = bnapi->index;
  5096. netdev_info(bnapi->bp->dev, "[%d]: cp{fw_ring: %d raw_cons: %x}\n",
  5097. i, cpr->cp_ring_struct.fw_ring_id, cpr->cp_raw_cons);
  5098. }
  5099. static void bnxt_dbg_dump_states(struct bnxt *bp)
  5100. {
  5101. int i;
  5102. struct bnxt_napi *bnapi;
  5103. for (i = 0; i < bp->cp_nr_rings; i++) {
  5104. bnapi = bp->bnapi[i];
  5105. if (netif_msg_drv(bp)) {
  5106. bnxt_dump_tx_sw_state(bnapi);
  5107. bnxt_dump_rx_sw_state(bnapi);
  5108. bnxt_dump_cp_sw_state(bnapi);
  5109. }
  5110. }
  5111. }
  5112. static void bnxt_reset_task(struct bnxt *bp, bool silent)
  5113. {
  5114. if (!silent)
  5115. bnxt_dbg_dump_states(bp);
  5116. if (netif_running(bp->dev)) {
  5117. bnxt_close_nic(bp, false, false);
  5118. bnxt_open_nic(bp, false, false);
  5119. }
  5120. }
  5121. static void bnxt_tx_timeout(struct net_device *dev)
  5122. {
  5123. struct bnxt *bp = netdev_priv(dev);
  5124. netdev_err(bp->dev, "TX timeout detected, starting reset task!\n");
  5125. set_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event);
  5126. schedule_work(&bp->sp_task);
  5127. }
  5128. #ifdef CONFIG_NET_POLL_CONTROLLER
  5129. static void bnxt_poll_controller(struct net_device *dev)
  5130. {
  5131. struct bnxt *bp = netdev_priv(dev);
  5132. int i;
  5133. for (i = 0; i < bp->cp_nr_rings; i++) {
  5134. struct bnxt_irq *irq = &bp->irq_tbl[i];
  5135. disable_irq(irq->vector);
  5136. irq->handler(irq->vector, bp->bnapi[i]);
  5137. enable_irq(irq->vector);
  5138. }
  5139. }
  5140. #endif
  5141. static void bnxt_timer(unsigned long data)
  5142. {
  5143. struct bnxt *bp = (struct bnxt *)data;
  5144. struct net_device *dev = bp->dev;
  5145. if (!netif_running(dev))
  5146. return;
  5147. if (atomic_read(&bp->intr_sem) != 0)
  5148. goto bnxt_restart_timer;
  5149. if (bp->link_info.link_up && (bp->flags & BNXT_FLAG_PORT_STATS)) {
  5150. set_bit(BNXT_PERIODIC_STATS_SP_EVENT, &bp->sp_event);
  5151. schedule_work(&bp->sp_task);
  5152. }
  5153. bnxt_restart_timer:
  5154. mod_timer(&bp->timer, jiffies + bp->current_interval);
  5155. }
  5156. /* Only called from bnxt_sp_task() */
  5157. static void bnxt_reset(struct bnxt *bp, bool silent)
  5158. {
  5159. /* bnxt_reset_task() calls bnxt_close_nic() which waits
  5160. * for BNXT_STATE_IN_SP_TASK to clear.
  5161. * If there is a parallel dev_close(), bnxt_close() may be holding
  5162. * rtnl() and waiting for BNXT_STATE_IN_SP_TASK to clear. So we
  5163. * must clear BNXT_STATE_IN_SP_TASK before holding rtnl().
  5164. */
  5165. clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
  5166. rtnl_lock();
  5167. if (test_bit(BNXT_STATE_OPEN, &bp->state))
  5168. bnxt_reset_task(bp, silent);
  5169. set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
  5170. rtnl_unlock();
  5171. }
  5172. static void bnxt_cfg_ntp_filters(struct bnxt *);
  5173. static void bnxt_sp_task(struct work_struct *work)
  5174. {
  5175. struct bnxt *bp = container_of(work, struct bnxt, sp_task);
  5176. int rc;
  5177. set_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
  5178. smp_mb__after_atomic();
  5179. if (!test_bit(BNXT_STATE_OPEN, &bp->state)) {
  5180. clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
  5181. return;
  5182. }
  5183. if (test_and_clear_bit(BNXT_RX_MASK_SP_EVENT, &bp->sp_event))
  5184. bnxt_cfg_rx_mode(bp);
  5185. if (test_and_clear_bit(BNXT_RX_NTP_FLTR_SP_EVENT, &bp->sp_event))
  5186. bnxt_cfg_ntp_filters(bp);
  5187. if (test_and_clear_bit(BNXT_LINK_CHNG_SP_EVENT, &bp->sp_event)) {
  5188. if (test_and_clear_bit(BNXT_LINK_SPEED_CHNG_SP_EVENT,
  5189. &bp->sp_event))
  5190. bnxt_hwrm_phy_qcaps(bp);
  5191. rc = bnxt_update_link(bp, true);
  5192. if (rc)
  5193. netdev_err(bp->dev, "SP task can't update link (rc: %x)\n",
  5194. rc);
  5195. }
  5196. if (test_and_clear_bit(BNXT_HWRM_EXEC_FWD_REQ_SP_EVENT, &bp->sp_event))
  5197. bnxt_hwrm_exec_fwd_req(bp);
  5198. if (test_and_clear_bit(BNXT_VXLAN_ADD_PORT_SP_EVENT, &bp->sp_event)) {
  5199. bnxt_hwrm_tunnel_dst_port_alloc(
  5200. bp, bp->vxlan_port,
  5201. TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN);
  5202. }
  5203. if (test_and_clear_bit(BNXT_VXLAN_DEL_PORT_SP_EVENT, &bp->sp_event)) {
  5204. bnxt_hwrm_tunnel_dst_port_free(
  5205. bp, TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_VXLAN);
  5206. }
  5207. if (test_and_clear_bit(BNXT_GENEVE_ADD_PORT_SP_EVENT, &bp->sp_event)) {
  5208. bnxt_hwrm_tunnel_dst_port_alloc(
  5209. bp, bp->nge_port,
  5210. TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE);
  5211. }
  5212. if (test_and_clear_bit(BNXT_GENEVE_DEL_PORT_SP_EVENT, &bp->sp_event)) {
  5213. bnxt_hwrm_tunnel_dst_port_free(
  5214. bp, TUNNEL_DST_PORT_FREE_REQ_TUNNEL_TYPE_GENEVE);
  5215. }
  5216. if (test_and_clear_bit(BNXT_RESET_TASK_SP_EVENT, &bp->sp_event))
  5217. bnxt_reset(bp, false);
  5218. if (test_and_clear_bit(BNXT_RESET_TASK_SILENT_SP_EVENT, &bp->sp_event))
  5219. bnxt_reset(bp, true);
  5220. if (test_and_clear_bit(BNXT_HWRM_PORT_MODULE_SP_EVENT, &bp->sp_event))
  5221. bnxt_get_port_module_status(bp);
  5222. if (test_and_clear_bit(BNXT_PERIODIC_STATS_SP_EVENT, &bp->sp_event))
  5223. bnxt_hwrm_port_qstats(bp);
  5224. smp_mb__before_atomic();
  5225. clear_bit(BNXT_STATE_IN_SP_TASK, &bp->state);
  5226. }
  5227. static int bnxt_init_board(struct pci_dev *pdev, struct net_device *dev)
  5228. {
  5229. int rc;
  5230. struct bnxt *bp = netdev_priv(dev);
  5231. SET_NETDEV_DEV(dev, &pdev->dev);
  5232. /* enable device (incl. PCI PM wakeup), and bus-mastering */
  5233. rc = pci_enable_device(pdev);
  5234. if (rc) {
  5235. dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
  5236. goto init_err;
  5237. }
  5238. if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
  5239. dev_err(&pdev->dev,
  5240. "Cannot find PCI device base address, aborting\n");
  5241. rc = -ENODEV;
  5242. goto init_err_disable;
  5243. }
  5244. rc = pci_request_regions(pdev, DRV_MODULE_NAME);
  5245. if (rc) {
  5246. dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
  5247. goto init_err_disable;
  5248. }
  5249. if (dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)) != 0 &&
  5250. dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)) != 0) {
  5251. dev_err(&pdev->dev, "System does not support DMA, aborting\n");
  5252. goto init_err_disable;
  5253. }
  5254. pci_set_master(pdev);
  5255. bp->dev = dev;
  5256. bp->pdev = pdev;
  5257. bp->bar0 = pci_ioremap_bar(pdev, 0);
  5258. if (!bp->bar0) {
  5259. dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
  5260. rc = -ENOMEM;
  5261. goto init_err_release;
  5262. }
  5263. bp->bar1 = pci_ioremap_bar(pdev, 2);
  5264. if (!bp->bar1) {
  5265. dev_err(&pdev->dev, "Cannot map doorbell registers, aborting\n");
  5266. rc = -ENOMEM;
  5267. goto init_err_release;
  5268. }
  5269. bp->bar2 = pci_ioremap_bar(pdev, 4);
  5270. if (!bp->bar2) {
  5271. dev_err(&pdev->dev, "Cannot map bar4 registers, aborting\n");
  5272. rc = -ENOMEM;
  5273. goto init_err_release;
  5274. }
  5275. pci_enable_pcie_error_reporting(pdev);
  5276. INIT_WORK(&bp->sp_task, bnxt_sp_task);
  5277. spin_lock_init(&bp->ntp_fltr_lock);
  5278. bp->rx_ring_size = BNXT_DEFAULT_RX_RING_SIZE;
  5279. bp->tx_ring_size = BNXT_DEFAULT_TX_RING_SIZE;
  5280. /* tick values in micro seconds */
  5281. bp->rx_coal_ticks = 12;
  5282. bp->rx_coal_bufs = 30;
  5283. bp->rx_coal_ticks_irq = 1;
  5284. bp->rx_coal_bufs_irq = 2;
  5285. bp->tx_coal_ticks = 25;
  5286. bp->tx_coal_bufs = 30;
  5287. bp->tx_coal_ticks_irq = 2;
  5288. bp->tx_coal_bufs_irq = 2;
  5289. bp->stats_coal_ticks = BNXT_DEF_STATS_COAL_TICKS;
  5290. init_timer(&bp->timer);
  5291. bp->timer.data = (unsigned long)bp;
  5292. bp->timer.function = bnxt_timer;
  5293. bp->current_interval = BNXT_TIMER_INTERVAL;
  5294. clear_bit(BNXT_STATE_OPEN, &bp->state);
  5295. return 0;
  5296. init_err_release:
  5297. if (bp->bar2) {
  5298. pci_iounmap(pdev, bp->bar2);
  5299. bp->bar2 = NULL;
  5300. }
  5301. if (bp->bar1) {
  5302. pci_iounmap(pdev, bp->bar1);
  5303. bp->bar1 = NULL;
  5304. }
  5305. if (bp->bar0) {
  5306. pci_iounmap(pdev, bp->bar0);
  5307. bp->bar0 = NULL;
  5308. }
  5309. pci_release_regions(pdev);
  5310. init_err_disable:
  5311. pci_disable_device(pdev);
  5312. init_err:
  5313. return rc;
  5314. }
  5315. /* rtnl_lock held */
  5316. static int bnxt_change_mac_addr(struct net_device *dev, void *p)
  5317. {
  5318. struct sockaddr *addr = p;
  5319. struct bnxt *bp = netdev_priv(dev);
  5320. int rc = 0;
  5321. if (!is_valid_ether_addr(addr->sa_data))
  5322. return -EADDRNOTAVAIL;
  5323. rc = bnxt_approve_mac(bp, addr->sa_data);
  5324. if (rc)
  5325. return rc;
  5326. if (ether_addr_equal(addr->sa_data, dev->dev_addr))
  5327. return 0;
  5328. memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
  5329. if (netif_running(dev)) {
  5330. bnxt_close_nic(bp, false, false);
  5331. rc = bnxt_open_nic(bp, false, false);
  5332. }
  5333. return rc;
  5334. }
  5335. /* rtnl_lock held */
  5336. static int bnxt_change_mtu(struct net_device *dev, int new_mtu)
  5337. {
  5338. struct bnxt *bp = netdev_priv(dev);
  5339. if (netif_running(dev))
  5340. bnxt_close_nic(bp, false, false);
  5341. dev->mtu = new_mtu;
  5342. bnxt_set_ring_params(bp);
  5343. if (netif_running(dev))
  5344. return bnxt_open_nic(bp, false, false);
  5345. return 0;
  5346. }
  5347. int bnxt_setup_mq_tc(struct net_device *dev, u8 tc)
  5348. {
  5349. struct bnxt *bp = netdev_priv(dev);
  5350. bool sh = false;
  5351. if (tc > bp->max_tc) {
  5352. netdev_err(dev, "too many traffic classes requested: %d Max supported is %d\n",
  5353. tc, bp->max_tc);
  5354. return -EINVAL;
  5355. }
  5356. if (netdev_get_num_tc(dev) == tc)
  5357. return 0;
  5358. if (bp->flags & BNXT_FLAG_SHARED_RINGS)
  5359. sh = true;
  5360. if (tc) {
  5361. int max_rx_rings, max_tx_rings, rc;
  5362. rc = bnxt_get_max_rings(bp, &max_rx_rings, &max_tx_rings, sh);
  5363. if (rc || bp->tx_nr_rings_per_tc * tc > max_tx_rings)
  5364. return -ENOMEM;
  5365. }
  5366. /* Needs to close the device and do hw resource re-allocations */
  5367. if (netif_running(bp->dev))
  5368. bnxt_close_nic(bp, true, false);
  5369. if (tc) {
  5370. bp->tx_nr_rings = bp->tx_nr_rings_per_tc * tc;
  5371. netdev_set_num_tc(dev, tc);
  5372. } else {
  5373. bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
  5374. netdev_reset_tc(dev);
  5375. }
  5376. bp->cp_nr_rings = sh ? max_t(int, bp->tx_nr_rings, bp->rx_nr_rings) :
  5377. bp->tx_nr_rings + bp->rx_nr_rings;
  5378. bp->num_stat_ctxs = bp->cp_nr_rings;
  5379. if (netif_running(bp->dev))
  5380. return bnxt_open_nic(bp, true, false);
  5381. return 0;
  5382. }
  5383. static int bnxt_setup_tc(struct net_device *dev, u32 handle, __be16 proto,
  5384. struct tc_to_netdev *ntc)
  5385. {
  5386. if (ntc->type != TC_SETUP_MQPRIO)
  5387. return -EINVAL;
  5388. return bnxt_setup_mq_tc(dev, ntc->tc);
  5389. }
  5390. #ifdef CONFIG_RFS_ACCEL
  5391. static bool bnxt_fltr_match(struct bnxt_ntuple_filter *f1,
  5392. struct bnxt_ntuple_filter *f2)
  5393. {
  5394. struct flow_keys *keys1 = &f1->fkeys;
  5395. struct flow_keys *keys2 = &f2->fkeys;
  5396. if (keys1->addrs.v4addrs.src == keys2->addrs.v4addrs.src &&
  5397. keys1->addrs.v4addrs.dst == keys2->addrs.v4addrs.dst &&
  5398. keys1->ports.ports == keys2->ports.ports &&
  5399. keys1->basic.ip_proto == keys2->basic.ip_proto &&
  5400. keys1->basic.n_proto == keys2->basic.n_proto &&
  5401. ether_addr_equal(f1->src_mac_addr, f2->src_mac_addr) &&
  5402. ether_addr_equal(f1->dst_mac_addr, f2->dst_mac_addr))
  5403. return true;
  5404. return false;
  5405. }
  5406. static int bnxt_rx_flow_steer(struct net_device *dev, const struct sk_buff *skb,
  5407. u16 rxq_index, u32 flow_id)
  5408. {
  5409. struct bnxt *bp = netdev_priv(dev);
  5410. struct bnxt_ntuple_filter *fltr, *new_fltr;
  5411. struct flow_keys *fkeys;
  5412. struct ethhdr *eth = (struct ethhdr *)skb_mac_header(skb);
  5413. int rc = 0, idx, bit_id, l2_idx = 0;
  5414. struct hlist_head *head;
  5415. if (skb->encapsulation)
  5416. return -EPROTONOSUPPORT;
  5417. if (!ether_addr_equal(dev->dev_addr, eth->h_dest)) {
  5418. struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
  5419. int off = 0, j;
  5420. netif_addr_lock_bh(dev);
  5421. for (j = 0; j < vnic->uc_filter_count; j++, off += ETH_ALEN) {
  5422. if (ether_addr_equal(eth->h_dest,
  5423. vnic->uc_list + off)) {
  5424. l2_idx = j + 1;
  5425. break;
  5426. }
  5427. }
  5428. netif_addr_unlock_bh(dev);
  5429. if (!l2_idx)
  5430. return -EINVAL;
  5431. }
  5432. new_fltr = kzalloc(sizeof(*new_fltr), GFP_ATOMIC);
  5433. if (!new_fltr)
  5434. return -ENOMEM;
  5435. fkeys = &new_fltr->fkeys;
  5436. if (!skb_flow_dissect_flow_keys(skb, fkeys, 0)) {
  5437. rc = -EPROTONOSUPPORT;
  5438. goto err_free;
  5439. }
  5440. if ((fkeys->basic.n_proto != htons(ETH_P_IP)) ||
  5441. ((fkeys->basic.ip_proto != IPPROTO_TCP) &&
  5442. (fkeys->basic.ip_proto != IPPROTO_UDP))) {
  5443. rc = -EPROTONOSUPPORT;
  5444. goto err_free;
  5445. }
  5446. memcpy(new_fltr->dst_mac_addr, eth->h_dest, ETH_ALEN);
  5447. memcpy(new_fltr->src_mac_addr, eth->h_source, ETH_ALEN);
  5448. idx = skb_get_hash_raw(skb) & BNXT_NTP_FLTR_HASH_MASK;
  5449. head = &bp->ntp_fltr_hash_tbl[idx];
  5450. rcu_read_lock();
  5451. hlist_for_each_entry_rcu(fltr, head, hash) {
  5452. if (bnxt_fltr_match(fltr, new_fltr)) {
  5453. rcu_read_unlock();
  5454. rc = 0;
  5455. goto err_free;
  5456. }
  5457. }
  5458. rcu_read_unlock();
  5459. spin_lock_bh(&bp->ntp_fltr_lock);
  5460. bit_id = bitmap_find_free_region(bp->ntp_fltr_bmap,
  5461. BNXT_NTP_FLTR_MAX_FLTR, 0);
  5462. if (bit_id < 0) {
  5463. spin_unlock_bh(&bp->ntp_fltr_lock);
  5464. rc = -ENOMEM;
  5465. goto err_free;
  5466. }
  5467. new_fltr->sw_id = (u16)bit_id;
  5468. new_fltr->flow_id = flow_id;
  5469. new_fltr->l2_fltr_idx = l2_idx;
  5470. new_fltr->rxq = rxq_index;
  5471. hlist_add_head_rcu(&new_fltr->hash, head);
  5472. bp->ntp_fltr_count++;
  5473. spin_unlock_bh(&bp->ntp_fltr_lock);
  5474. set_bit(BNXT_RX_NTP_FLTR_SP_EVENT, &bp->sp_event);
  5475. schedule_work(&bp->sp_task);
  5476. return new_fltr->sw_id;
  5477. err_free:
  5478. kfree(new_fltr);
  5479. return rc;
  5480. }
  5481. static void bnxt_cfg_ntp_filters(struct bnxt *bp)
  5482. {
  5483. int i;
  5484. for (i = 0; i < BNXT_NTP_FLTR_HASH_SIZE; i++) {
  5485. struct hlist_head *head;
  5486. struct hlist_node *tmp;
  5487. struct bnxt_ntuple_filter *fltr;
  5488. int rc;
  5489. head = &bp->ntp_fltr_hash_tbl[i];
  5490. hlist_for_each_entry_safe(fltr, tmp, head, hash) {
  5491. bool del = false;
  5492. if (test_bit(BNXT_FLTR_VALID, &fltr->state)) {
  5493. if (rps_may_expire_flow(bp->dev, fltr->rxq,
  5494. fltr->flow_id,
  5495. fltr->sw_id)) {
  5496. bnxt_hwrm_cfa_ntuple_filter_free(bp,
  5497. fltr);
  5498. del = true;
  5499. }
  5500. } else {
  5501. rc = bnxt_hwrm_cfa_ntuple_filter_alloc(bp,
  5502. fltr);
  5503. if (rc)
  5504. del = true;
  5505. else
  5506. set_bit(BNXT_FLTR_VALID, &fltr->state);
  5507. }
  5508. if (del) {
  5509. spin_lock_bh(&bp->ntp_fltr_lock);
  5510. hlist_del_rcu(&fltr->hash);
  5511. bp->ntp_fltr_count--;
  5512. spin_unlock_bh(&bp->ntp_fltr_lock);
  5513. synchronize_rcu();
  5514. clear_bit(fltr->sw_id, bp->ntp_fltr_bmap);
  5515. kfree(fltr);
  5516. }
  5517. }
  5518. }
  5519. if (test_and_clear_bit(BNXT_HWRM_PF_UNLOAD_SP_EVENT, &bp->sp_event))
  5520. netdev_info(bp->dev, "Receive PF driver unload event!");
  5521. }
  5522. #else
  5523. static void bnxt_cfg_ntp_filters(struct bnxt *bp)
  5524. {
  5525. }
  5526. #endif /* CONFIG_RFS_ACCEL */
  5527. static void bnxt_udp_tunnel_add(struct net_device *dev,
  5528. struct udp_tunnel_info *ti)
  5529. {
  5530. struct bnxt *bp = netdev_priv(dev);
  5531. if (ti->sa_family != AF_INET6 && ti->sa_family != AF_INET)
  5532. return;
  5533. if (!netif_running(dev))
  5534. return;
  5535. switch (ti->type) {
  5536. case UDP_TUNNEL_TYPE_VXLAN:
  5537. if (bp->vxlan_port_cnt && bp->vxlan_port != ti->port)
  5538. return;
  5539. bp->vxlan_port_cnt++;
  5540. if (bp->vxlan_port_cnt == 1) {
  5541. bp->vxlan_port = ti->port;
  5542. set_bit(BNXT_VXLAN_ADD_PORT_SP_EVENT, &bp->sp_event);
  5543. schedule_work(&bp->sp_task);
  5544. }
  5545. break;
  5546. case UDP_TUNNEL_TYPE_GENEVE:
  5547. if (bp->nge_port_cnt && bp->nge_port != ti->port)
  5548. return;
  5549. bp->nge_port_cnt++;
  5550. if (bp->nge_port_cnt == 1) {
  5551. bp->nge_port = ti->port;
  5552. set_bit(BNXT_GENEVE_ADD_PORT_SP_EVENT, &bp->sp_event);
  5553. }
  5554. break;
  5555. default:
  5556. return;
  5557. }
  5558. schedule_work(&bp->sp_task);
  5559. }
  5560. static void bnxt_udp_tunnel_del(struct net_device *dev,
  5561. struct udp_tunnel_info *ti)
  5562. {
  5563. struct bnxt *bp = netdev_priv(dev);
  5564. if (ti->sa_family != AF_INET6 && ti->sa_family != AF_INET)
  5565. return;
  5566. if (!netif_running(dev))
  5567. return;
  5568. switch (ti->type) {
  5569. case UDP_TUNNEL_TYPE_VXLAN:
  5570. if (!bp->vxlan_port_cnt || bp->vxlan_port != ti->port)
  5571. return;
  5572. bp->vxlan_port_cnt--;
  5573. if (bp->vxlan_port_cnt != 0)
  5574. return;
  5575. set_bit(BNXT_VXLAN_DEL_PORT_SP_EVENT, &bp->sp_event);
  5576. break;
  5577. case UDP_TUNNEL_TYPE_GENEVE:
  5578. if (!bp->nge_port_cnt || bp->nge_port != ti->port)
  5579. return;
  5580. bp->nge_port_cnt--;
  5581. if (bp->nge_port_cnt != 0)
  5582. return;
  5583. set_bit(BNXT_GENEVE_DEL_PORT_SP_EVENT, &bp->sp_event);
  5584. break;
  5585. default:
  5586. return;
  5587. }
  5588. schedule_work(&bp->sp_task);
  5589. }
  5590. static const struct net_device_ops bnxt_netdev_ops = {
  5591. .ndo_open = bnxt_open,
  5592. .ndo_start_xmit = bnxt_start_xmit,
  5593. .ndo_stop = bnxt_close,
  5594. .ndo_get_stats64 = bnxt_get_stats64,
  5595. .ndo_set_rx_mode = bnxt_set_rx_mode,
  5596. .ndo_do_ioctl = bnxt_ioctl,
  5597. .ndo_validate_addr = eth_validate_addr,
  5598. .ndo_set_mac_address = bnxt_change_mac_addr,
  5599. .ndo_change_mtu = bnxt_change_mtu,
  5600. .ndo_fix_features = bnxt_fix_features,
  5601. .ndo_set_features = bnxt_set_features,
  5602. .ndo_tx_timeout = bnxt_tx_timeout,
  5603. #ifdef CONFIG_BNXT_SRIOV
  5604. .ndo_get_vf_config = bnxt_get_vf_config,
  5605. .ndo_set_vf_mac = bnxt_set_vf_mac,
  5606. .ndo_set_vf_vlan = bnxt_set_vf_vlan,
  5607. .ndo_set_vf_rate = bnxt_set_vf_bw,
  5608. .ndo_set_vf_link_state = bnxt_set_vf_link_state,
  5609. .ndo_set_vf_spoofchk = bnxt_set_vf_spoofchk,
  5610. #endif
  5611. #ifdef CONFIG_NET_POLL_CONTROLLER
  5612. .ndo_poll_controller = bnxt_poll_controller,
  5613. #endif
  5614. .ndo_setup_tc = bnxt_setup_tc,
  5615. #ifdef CONFIG_RFS_ACCEL
  5616. .ndo_rx_flow_steer = bnxt_rx_flow_steer,
  5617. #endif
  5618. .ndo_udp_tunnel_add = bnxt_udp_tunnel_add,
  5619. .ndo_udp_tunnel_del = bnxt_udp_tunnel_del,
  5620. #ifdef CONFIG_NET_RX_BUSY_POLL
  5621. .ndo_busy_poll = bnxt_busy_poll,
  5622. #endif
  5623. };
  5624. static void bnxt_remove_one(struct pci_dev *pdev)
  5625. {
  5626. struct net_device *dev = pci_get_drvdata(pdev);
  5627. struct bnxt *bp = netdev_priv(dev);
  5628. if (BNXT_PF(bp))
  5629. bnxt_sriov_disable(bp);
  5630. pci_disable_pcie_error_reporting(pdev);
  5631. unregister_netdev(dev);
  5632. cancel_work_sync(&bp->sp_task);
  5633. bp->sp_event = 0;
  5634. bnxt_clear_int_mode(bp);
  5635. bnxt_hwrm_func_drv_unrgtr(bp);
  5636. bnxt_free_hwrm_resources(bp);
  5637. bnxt_dcb_free(bp);
  5638. pci_iounmap(pdev, bp->bar2);
  5639. pci_iounmap(pdev, bp->bar1);
  5640. pci_iounmap(pdev, bp->bar0);
  5641. kfree(bp->edev);
  5642. bp->edev = NULL;
  5643. free_netdev(dev);
  5644. pci_release_regions(pdev);
  5645. pci_disable_device(pdev);
  5646. }
  5647. static int bnxt_probe_phy(struct bnxt *bp)
  5648. {
  5649. int rc = 0;
  5650. struct bnxt_link_info *link_info = &bp->link_info;
  5651. rc = bnxt_hwrm_phy_qcaps(bp);
  5652. if (rc) {
  5653. netdev_err(bp->dev, "Probe phy can't get phy capabilities (rc: %x)\n",
  5654. rc);
  5655. return rc;
  5656. }
  5657. rc = bnxt_update_link(bp, false);
  5658. if (rc) {
  5659. netdev_err(bp->dev, "Probe phy can't update link (rc: %x)\n",
  5660. rc);
  5661. return rc;
  5662. }
  5663. /* Older firmware does not have supported_auto_speeds, so assume
  5664. * that all supported speeds can be autonegotiated.
  5665. */
  5666. if (link_info->auto_link_speeds && !link_info->support_auto_speeds)
  5667. link_info->support_auto_speeds = link_info->support_speeds;
  5668. /*initialize the ethool setting copy with NVM settings */
  5669. if (BNXT_AUTO_MODE(link_info->auto_mode)) {
  5670. link_info->autoneg = BNXT_AUTONEG_SPEED;
  5671. if (bp->hwrm_spec_code >= 0x10201) {
  5672. if (link_info->auto_pause_setting &
  5673. PORT_PHY_CFG_REQ_AUTO_PAUSE_AUTONEG_PAUSE)
  5674. link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
  5675. } else {
  5676. link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
  5677. }
  5678. link_info->advertising = link_info->auto_link_speeds;
  5679. } else {
  5680. link_info->req_link_speed = link_info->force_link_speed;
  5681. link_info->req_duplex = link_info->duplex_setting;
  5682. }
  5683. if (link_info->autoneg & BNXT_AUTONEG_FLOW_CTRL)
  5684. link_info->req_flow_ctrl =
  5685. link_info->auto_pause_setting & BNXT_LINK_PAUSE_BOTH;
  5686. else
  5687. link_info->req_flow_ctrl = link_info->force_pause_setting;
  5688. return rc;
  5689. }
  5690. static int bnxt_get_max_irq(struct pci_dev *pdev)
  5691. {
  5692. u16 ctrl;
  5693. if (!pdev->msix_cap)
  5694. return 1;
  5695. pci_read_config_word(pdev, pdev->msix_cap + PCI_MSIX_FLAGS, &ctrl);
  5696. return (ctrl & PCI_MSIX_FLAGS_QSIZE) + 1;
  5697. }
  5698. static void _bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx,
  5699. int *max_cp)
  5700. {
  5701. int max_ring_grps = 0;
  5702. #ifdef CONFIG_BNXT_SRIOV
  5703. if (!BNXT_PF(bp)) {
  5704. *max_tx = bp->vf.max_tx_rings;
  5705. *max_rx = bp->vf.max_rx_rings;
  5706. *max_cp = min_t(int, bp->vf.max_irqs, bp->vf.max_cp_rings);
  5707. *max_cp = min_t(int, *max_cp, bp->vf.max_stat_ctxs);
  5708. max_ring_grps = bp->vf.max_hw_ring_grps;
  5709. } else
  5710. #endif
  5711. {
  5712. *max_tx = bp->pf.max_tx_rings;
  5713. *max_rx = bp->pf.max_rx_rings;
  5714. *max_cp = min_t(int, bp->pf.max_irqs, bp->pf.max_cp_rings);
  5715. *max_cp = min_t(int, *max_cp, bp->pf.max_stat_ctxs);
  5716. max_ring_grps = bp->pf.max_hw_ring_grps;
  5717. }
  5718. if (BNXT_CHIP_TYPE_NITRO_A0(bp) && BNXT_PF(bp)) {
  5719. *max_cp -= 1;
  5720. *max_rx -= 2;
  5721. }
  5722. if (bp->flags & BNXT_FLAG_AGG_RINGS)
  5723. *max_rx >>= 1;
  5724. *max_rx = min_t(int, *max_rx, max_ring_grps);
  5725. }
  5726. int bnxt_get_max_rings(struct bnxt *bp, int *max_rx, int *max_tx, bool shared)
  5727. {
  5728. int rx, tx, cp;
  5729. _bnxt_get_max_rings(bp, &rx, &tx, &cp);
  5730. if (!rx || !tx || !cp)
  5731. return -ENOMEM;
  5732. *max_rx = rx;
  5733. *max_tx = tx;
  5734. return bnxt_trim_rings(bp, max_rx, max_tx, cp, shared);
  5735. }
  5736. static int bnxt_get_dflt_rings(struct bnxt *bp, int *max_rx, int *max_tx,
  5737. bool shared)
  5738. {
  5739. int rc;
  5740. rc = bnxt_get_max_rings(bp, max_rx, max_tx, shared);
  5741. if (rc)
  5742. return rc;
  5743. if (bp->flags & BNXT_FLAG_ROCE_CAP) {
  5744. int max_cp, max_stat, max_irq;
  5745. /* Reserve minimum resources for RoCE */
  5746. max_cp = bnxt_get_max_func_cp_rings(bp);
  5747. max_stat = bnxt_get_max_func_stat_ctxs(bp);
  5748. max_irq = bnxt_get_max_func_irqs(bp);
  5749. if (max_cp <= BNXT_MIN_ROCE_CP_RINGS ||
  5750. max_irq <= BNXT_MIN_ROCE_CP_RINGS ||
  5751. max_stat <= BNXT_MIN_ROCE_STAT_CTXS)
  5752. return 0;
  5753. max_cp -= BNXT_MIN_ROCE_CP_RINGS;
  5754. max_irq -= BNXT_MIN_ROCE_CP_RINGS;
  5755. max_stat -= BNXT_MIN_ROCE_STAT_CTXS;
  5756. max_cp = min_t(int, max_cp, max_irq);
  5757. max_cp = min_t(int, max_cp, max_stat);
  5758. rc = bnxt_trim_rings(bp, max_rx, max_tx, max_cp, shared);
  5759. if (rc)
  5760. rc = 0;
  5761. }
  5762. return rc;
  5763. }
  5764. static int bnxt_set_dflt_rings(struct bnxt *bp)
  5765. {
  5766. int dflt_rings, max_rx_rings, max_tx_rings, rc;
  5767. bool sh = true;
  5768. if (sh)
  5769. bp->flags |= BNXT_FLAG_SHARED_RINGS;
  5770. dflt_rings = netif_get_num_default_rss_queues();
  5771. rc = bnxt_get_dflt_rings(bp, &max_rx_rings, &max_tx_rings, sh);
  5772. if (rc)
  5773. return rc;
  5774. bp->rx_nr_rings = min_t(int, dflt_rings, max_rx_rings);
  5775. bp->tx_nr_rings_per_tc = min_t(int, dflt_rings, max_tx_rings);
  5776. bp->tx_nr_rings = bp->tx_nr_rings_per_tc;
  5777. bp->cp_nr_rings = sh ? max_t(int, bp->tx_nr_rings, bp->rx_nr_rings) :
  5778. bp->tx_nr_rings + bp->rx_nr_rings;
  5779. bp->num_stat_ctxs = bp->cp_nr_rings;
  5780. if (BNXT_CHIP_TYPE_NITRO_A0(bp)) {
  5781. bp->rx_nr_rings++;
  5782. bp->cp_nr_rings++;
  5783. }
  5784. return rc;
  5785. }
  5786. void bnxt_restore_pf_fw_resources(struct bnxt *bp)
  5787. {
  5788. ASSERT_RTNL();
  5789. bnxt_hwrm_func_qcaps(bp);
  5790. bnxt_subtract_ulp_resources(bp, BNXT_ROCE_ULP);
  5791. }
  5792. static void bnxt_parse_log_pcie_link(struct bnxt *bp)
  5793. {
  5794. enum pcie_link_width width = PCIE_LNK_WIDTH_UNKNOWN;
  5795. enum pci_bus_speed speed = PCI_SPEED_UNKNOWN;
  5796. if (pcie_get_minimum_link(bp->pdev, &speed, &width) ||
  5797. speed == PCI_SPEED_UNKNOWN || width == PCIE_LNK_WIDTH_UNKNOWN)
  5798. netdev_info(bp->dev, "Failed to determine PCIe Link Info\n");
  5799. else
  5800. netdev_info(bp->dev, "PCIe: Speed %s Width x%d\n",
  5801. speed == PCIE_SPEED_2_5GT ? "2.5GT/s" :
  5802. speed == PCIE_SPEED_5_0GT ? "5.0GT/s" :
  5803. speed == PCIE_SPEED_8_0GT ? "8.0GT/s" :
  5804. "Unknown", width);
  5805. }
  5806. static int bnxt_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
  5807. {
  5808. static int version_printed;
  5809. struct net_device *dev;
  5810. struct bnxt *bp;
  5811. int rc, max_irqs;
  5812. if (pdev->device == 0x16cd && pci_is_bridge(pdev))
  5813. return -ENODEV;
  5814. if (version_printed++ == 0)
  5815. pr_info("%s", version);
  5816. max_irqs = bnxt_get_max_irq(pdev);
  5817. dev = alloc_etherdev_mq(sizeof(*bp), max_irqs);
  5818. if (!dev)
  5819. return -ENOMEM;
  5820. bp = netdev_priv(dev);
  5821. if (bnxt_vf_pciid(ent->driver_data))
  5822. bp->flags |= BNXT_FLAG_VF;
  5823. if (pdev->msix_cap)
  5824. bp->flags |= BNXT_FLAG_MSIX_CAP;
  5825. rc = bnxt_init_board(pdev, dev);
  5826. if (rc < 0)
  5827. goto init_err_free;
  5828. dev->netdev_ops = &bnxt_netdev_ops;
  5829. dev->watchdog_timeo = BNXT_TX_TIMEOUT;
  5830. dev->ethtool_ops = &bnxt_ethtool_ops;
  5831. pci_set_drvdata(pdev, dev);
  5832. rc = bnxt_alloc_hwrm_resources(bp);
  5833. if (rc)
  5834. goto init_err;
  5835. mutex_init(&bp->hwrm_cmd_lock);
  5836. rc = bnxt_hwrm_ver_get(bp);
  5837. if (rc)
  5838. goto init_err;
  5839. bnxt_hwrm_fw_set_time(bp);
  5840. dev->hw_features = NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
  5841. NETIF_F_TSO | NETIF_F_TSO6 |
  5842. NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
  5843. NETIF_F_GSO_IPXIP4 |
  5844. NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
  5845. NETIF_F_GSO_PARTIAL | NETIF_F_RXHASH |
  5846. NETIF_F_RXCSUM | NETIF_F_GRO;
  5847. if (!BNXT_CHIP_TYPE_NITRO_A0(bp))
  5848. dev->hw_features |= NETIF_F_LRO;
  5849. dev->hw_enc_features =
  5850. NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_SG |
  5851. NETIF_F_TSO | NETIF_F_TSO6 |
  5852. NETIF_F_GSO_UDP_TUNNEL | NETIF_F_GSO_GRE |
  5853. NETIF_F_GSO_UDP_TUNNEL_CSUM | NETIF_F_GSO_GRE_CSUM |
  5854. NETIF_F_GSO_IPXIP4 | NETIF_F_GSO_PARTIAL;
  5855. dev->gso_partial_features = NETIF_F_GSO_UDP_TUNNEL_CSUM |
  5856. NETIF_F_GSO_GRE_CSUM;
  5857. dev->vlan_features = dev->hw_features | NETIF_F_HIGHDMA;
  5858. dev->hw_features |= NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_CTAG_TX |
  5859. NETIF_F_HW_VLAN_STAG_RX | NETIF_F_HW_VLAN_STAG_TX;
  5860. dev->features |= dev->hw_features | NETIF_F_HIGHDMA;
  5861. dev->priv_flags |= IFF_UNICAST_FLT;
  5862. /* MTU range: 60 - 9500 */
  5863. dev->min_mtu = ETH_ZLEN;
  5864. dev->max_mtu = 9500;
  5865. bnxt_dcb_init(bp);
  5866. #ifdef CONFIG_BNXT_SRIOV
  5867. init_waitqueue_head(&bp->sriov_cfg_wait);
  5868. #endif
  5869. bp->gro_func = bnxt_gro_func_5730x;
  5870. if (BNXT_CHIP_NUM_57X1X(bp->chip_num))
  5871. bp->gro_func = bnxt_gro_func_5731x;
  5872. rc = bnxt_hwrm_func_drv_rgtr(bp);
  5873. if (rc)
  5874. goto init_err;
  5875. rc = bnxt_hwrm_func_rgtr_async_events(bp, NULL, 0);
  5876. if (rc)
  5877. goto init_err;
  5878. bp->ulp_probe = bnxt_ulp_probe;
  5879. /* Get the MAX capabilities for this function */
  5880. rc = bnxt_hwrm_func_qcaps(bp);
  5881. if (rc) {
  5882. netdev_err(bp->dev, "hwrm query capability failure rc: %x\n",
  5883. rc);
  5884. rc = -1;
  5885. goto init_err;
  5886. }
  5887. rc = bnxt_hwrm_queue_qportcfg(bp);
  5888. if (rc) {
  5889. netdev_err(bp->dev, "hwrm query qportcfg failure rc: %x\n",
  5890. rc);
  5891. rc = -1;
  5892. goto init_err;
  5893. }
  5894. bnxt_hwrm_func_qcfg(bp);
  5895. bnxt_set_tpa_flags(bp);
  5896. bnxt_set_ring_params(bp);
  5897. bnxt_set_max_func_irqs(bp, max_irqs);
  5898. bnxt_set_dflt_rings(bp);
  5899. /* Default RSS hash cfg. */
  5900. bp->rss_hash_cfg = VNIC_RSS_CFG_REQ_HASH_TYPE_IPV4 |
  5901. VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV4 |
  5902. VNIC_RSS_CFG_REQ_HASH_TYPE_IPV6 |
  5903. VNIC_RSS_CFG_REQ_HASH_TYPE_TCP_IPV6;
  5904. if (!BNXT_CHIP_NUM_57X0X(bp->chip_num) &&
  5905. !BNXT_CHIP_TYPE_NITRO_A0(bp) &&
  5906. bp->hwrm_spec_code >= 0x10501) {
  5907. bp->flags |= BNXT_FLAG_UDP_RSS_CAP;
  5908. bp->rss_hash_cfg |= VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV4 |
  5909. VNIC_RSS_CFG_REQ_HASH_TYPE_UDP_IPV6;
  5910. }
  5911. if (BNXT_PF(bp) && !BNXT_CHIP_TYPE_NITRO_A0(bp)) {
  5912. dev->hw_features |= NETIF_F_NTUPLE;
  5913. if (bnxt_rfs_capable(bp)) {
  5914. bp->flags |= BNXT_FLAG_RFS;
  5915. dev->features |= NETIF_F_NTUPLE;
  5916. }
  5917. }
  5918. if (dev->hw_features & NETIF_F_HW_VLAN_CTAG_RX)
  5919. bp->flags |= BNXT_FLAG_STRIP_VLAN;
  5920. rc = bnxt_probe_phy(bp);
  5921. if (rc)
  5922. goto init_err;
  5923. rc = bnxt_hwrm_func_reset(bp);
  5924. if (rc)
  5925. goto init_err;
  5926. rc = bnxt_init_int_mode(bp);
  5927. if (rc)
  5928. goto init_err;
  5929. rc = register_netdev(dev);
  5930. if (rc)
  5931. goto init_err_clr_int;
  5932. netdev_info(dev, "%s found at mem %lx, node addr %pM\n",
  5933. board_info[ent->driver_data].name,
  5934. (long)pci_resource_start(pdev, 0), dev->dev_addr);
  5935. bnxt_parse_log_pcie_link(bp);
  5936. return 0;
  5937. init_err_clr_int:
  5938. bnxt_clear_int_mode(bp);
  5939. init_err:
  5940. pci_iounmap(pdev, bp->bar0);
  5941. pci_release_regions(pdev);
  5942. pci_disable_device(pdev);
  5943. init_err_free:
  5944. free_netdev(dev);
  5945. return rc;
  5946. }
  5947. /**
  5948. * bnxt_io_error_detected - called when PCI error is detected
  5949. * @pdev: Pointer to PCI device
  5950. * @state: The current pci connection state
  5951. *
  5952. * This function is called after a PCI bus error affecting
  5953. * this device has been detected.
  5954. */
  5955. static pci_ers_result_t bnxt_io_error_detected(struct pci_dev *pdev,
  5956. pci_channel_state_t state)
  5957. {
  5958. struct net_device *netdev = pci_get_drvdata(pdev);
  5959. struct bnxt *bp = netdev_priv(netdev);
  5960. netdev_info(netdev, "PCI I/O error detected\n");
  5961. rtnl_lock();
  5962. netif_device_detach(netdev);
  5963. bnxt_ulp_stop(bp);
  5964. if (state == pci_channel_io_perm_failure) {
  5965. rtnl_unlock();
  5966. return PCI_ERS_RESULT_DISCONNECT;
  5967. }
  5968. if (netif_running(netdev))
  5969. bnxt_close(netdev);
  5970. pci_disable_device(pdev);
  5971. rtnl_unlock();
  5972. /* Request a slot slot reset. */
  5973. return PCI_ERS_RESULT_NEED_RESET;
  5974. }
  5975. /**
  5976. * bnxt_io_slot_reset - called after the pci bus has been reset.
  5977. * @pdev: Pointer to PCI device
  5978. *
  5979. * Restart the card from scratch, as if from a cold-boot.
  5980. * At this point, the card has exprienced a hard reset,
  5981. * followed by fixups by BIOS, and has its config space
  5982. * set up identically to what it was at cold boot.
  5983. */
  5984. static pci_ers_result_t bnxt_io_slot_reset(struct pci_dev *pdev)
  5985. {
  5986. struct net_device *netdev = pci_get_drvdata(pdev);
  5987. struct bnxt *bp = netdev_priv(netdev);
  5988. int err = 0;
  5989. pci_ers_result_t result = PCI_ERS_RESULT_DISCONNECT;
  5990. netdev_info(bp->dev, "PCI Slot Reset\n");
  5991. rtnl_lock();
  5992. if (pci_enable_device(pdev)) {
  5993. dev_err(&pdev->dev,
  5994. "Cannot re-enable PCI device after reset.\n");
  5995. } else {
  5996. pci_set_master(pdev);
  5997. err = bnxt_hwrm_func_reset(bp);
  5998. if (!err && netif_running(netdev))
  5999. err = bnxt_open(netdev);
  6000. if (!err) {
  6001. result = PCI_ERS_RESULT_RECOVERED;
  6002. bnxt_ulp_start(bp);
  6003. }
  6004. }
  6005. if (result != PCI_ERS_RESULT_RECOVERED && netif_running(netdev))
  6006. dev_close(netdev);
  6007. rtnl_unlock();
  6008. err = pci_cleanup_aer_uncorrect_error_status(pdev);
  6009. if (err) {
  6010. dev_err(&pdev->dev,
  6011. "pci_cleanup_aer_uncorrect_error_status failed 0x%0x\n",
  6012. err); /* non-fatal, continue */
  6013. }
  6014. return PCI_ERS_RESULT_RECOVERED;
  6015. }
  6016. /**
  6017. * bnxt_io_resume - called when traffic can start flowing again.
  6018. * @pdev: Pointer to PCI device
  6019. *
  6020. * This callback is called when the error recovery driver tells
  6021. * us that its OK to resume normal operation.
  6022. */
  6023. static void bnxt_io_resume(struct pci_dev *pdev)
  6024. {
  6025. struct net_device *netdev = pci_get_drvdata(pdev);
  6026. rtnl_lock();
  6027. netif_device_attach(netdev);
  6028. rtnl_unlock();
  6029. }
  6030. static const struct pci_error_handlers bnxt_err_handler = {
  6031. .error_detected = bnxt_io_error_detected,
  6032. .slot_reset = bnxt_io_slot_reset,
  6033. .resume = bnxt_io_resume
  6034. };
  6035. static struct pci_driver bnxt_pci_driver = {
  6036. .name = DRV_MODULE_NAME,
  6037. .id_table = bnxt_pci_tbl,
  6038. .probe = bnxt_init_one,
  6039. .remove = bnxt_remove_one,
  6040. .err_handler = &bnxt_err_handler,
  6041. #if defined(CONFIG_BNXT_SRIOV)
  6042. .sriov_configure = bnxt_sriov_configure,
  6043. #endif
  6044. };
  6045. module_pci_driver(bnxt_pci_driver);