iflib.c 193 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180
  1. /*-
  2. * Copyright (c) 2014-2018, Matthew Macy <mmacy@mattmacy.io>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. *
  11. * 2. Neither the name of Matthew Macy nor the names of its
  12. * contributors may be used to endorse or promote products derived from
  13. * this software without specific prior written permission.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  18. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  19. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  20. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  21. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  22. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  23. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  24. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  25. * POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include <sys/cdefs.h>
  28. #include "opt_inet.h"
  29. #include "opt_inet6.h"
  30. #include "opt_acpi.h"
  31. #include "opt_sched.h"
  32. #include <sys/param.h>
  33. #include <sys/types.h>
  34. #include <sys/bus.h>
  35. #include <sys/eventhandler.h>
  36. #include <sys/kernel.h>
  37. #include <sys/lock.h>
  38. #include <sys/mutex.h>
  39. #include <sys/module.h>
  40. #include <sys/kobj.h>
  41. #include <sys/rman.h>
  42. #include <sys/sbuf.h>
  43. #include <sys/smp.h>
  44. #include <sys/socket.h>
  45. #include <sys/sockio.h>
  46. #include <sys/sysctl.h>
  47. #include <sys/syslog.h>
  48. #include <sys/taskqueue.h>
  49. #include <sys/limits.h>
  50. #include <net/if.h>
  51. #include <net/if_var.h>
  52. #include <net/if_private.h>
  53. #include <net/if_types.h>
  54. #include <net/if_media.h>
  55. #include <net/bpf.h>
  56. #include <net/ethernet.h>
  57. #include <net/mp_ring.h>
  58. #include <net/debugnet.h>
  59. #include <net/pfil.h>
  60. #include <net/vnet.h>
  61. #include <netinet/in.h>
  62. #include <netinet/in_pcb.h>
  63. #include <netinet/tcp_lro.h>
  64. #include <netinet/in_systm.h>
  65. #include <netinet/if_ether.h>
  66. #include <netinet/ip.h>
  67. #include <netinet/ip6.h>
  68. #include <netinet/tcp.h>
  69. #include <netinet/ip_var.h>
  70. #include <netinet6/ip6_var.h>
  71. #include <machine/bus.h>
  72. #include <machine/in_cksum.h>
  73. #include <vm/vm.h>
  74. #include <vm/pmap.h>
  75. #include <dev/led/led.h>
  76. #include <dev/pci/pcireg.h>
  77. #include <dev/pci/pcivar.h>
  78. #include <dev/pci/pci_private.h>
  79. #include <net/iflib.h>
  80. #include "ifdi_if.h"
  81. #ifdef PCI_IOV
  82. #include <dev/pci/pci_iov.h>
  83. #endif
  84. #include <sys/bitstring.h>
  85. /*
  86. * enable accounting of every mbuf as it comes in to and goes out of
  87. * iflib's software descriptor references
  88. */
  89. #define MEMORY_LOGGING 0
  90. /*
  91. * Enable mbuf vectors for compressing long mbuf chains
  92. */
  93. /*
  94. * NB:
  95. * - Prefetching in tx cleaning should perhaps be a tunable. The distance ahead
  96. * we prefetch needs to be determined by the time spent in m_free vis a vis
  97. * the cost of a prefetch. This will of course vary based on the workload:
  98. * - NFLX's m_free path is dominated by vm-based M_EXT manipulation which
  99. * is quite expensive, thus suggesting very little prefetch.
  100. * - small packet forwarding which is just returning a single mbuf to
  101. * UMA will typically be very fast vis a vis the cost of a memory
  102. * access.
  103. */
  104. /*
  105. * File organization:
  106. * - private structures
  107. * - iflib private utility functions
  108. * - ifnet functions
  109. * - vlan registry and other exported functions
  110. * - iflib public core functions
  111. *
  112. *
  113. */
  114. static MALLOC_DEFINE(M_IFLIB, "iflib", "ifnet library");
  115. #define IFLIB_RXEOF_MORE (1U << 0)
  116. #define IFLIB_RXEOF_EMPTY (2U << 0)
  117. struct iflib_txq;
  118. typedef struct iflib_txq *iflib_txq_t;
  119. struct iflib_rxq;
  120. typedef struct iflib_rxq *iflib_rxq_t;
  121. struct iflib_fl;
  122. typedef struct iflib_fl *iflib_fl_t;
  123. struct iflib_ctx;
  124. static void iru_init(if_rxd_update_t iru, iflib_rxq_t rxq, uint8_t flid);
  125. static void iflib_timer(void *arg);
  126. static void iflib_tqg_detach(if_ctx_t ctx);
  127. typedef struct iflib_filter_info {
  128. driver_filter_t *ifi_filter;
  129. void *ifi_filter_arg;
  130. struct grouptask *ifi_task;
  131. void *ifi_ctx;
  132. } *iflib_filter_info_t;
  133. struct iflib_ctx {
  134. KOBJ_FIELDS;
  135. /*
  136. * Pointer to hardware driver's softc
  137. */
  138. void *ifc_softc;
  139. device_t ifc_dev;
  140. if_t ifc_ifp;
  141. cpuset_t ifc_cpus;
  142. if_shared_ctx_t ifc_sctx;
  143. struct if_softc_ctx ifc_softc_ctx;
  144. struct sx ifc_ctx_sx;
  145. struct mtx ifc_state_mtx;
  146. iflib_txq_t ifc_txqs;
  147. iflib_rxq_t ifc_rxqs;
  148. uint32_t ifc_if_flags;
  149. uint32_t ifc_flags;
  150. uint32_t ifc_max_fl_buf_size;
  151. uint32_t ifc_rx_mbuf_sz;
  152. int ifc_link_state;
  153. int ifc_watchdog_events;
  154. struct cdev *ifc_led_dev;
  155. struct resource *ifc_msix_mem;
  156. struct if_irq ifc_legacy_irq;
  157. struct grouptask ifc_admin_task;
  158. struct grouptask ifc_vflr_task;
  159. struct iflib_filter_info ifc_filter_info;
  160. struct ifmedia ifc_media;
  161. struct ifmedia *ifc_mediap;
  162. struct sysctl_oid *ifc_sysctl_node;
  163. uint16_t ifc_sysctl_ntxqs;
  164. uint16_t ifc_sysctl_nrxqs;
  165. uint16_t ifc_sysctl_qs_eq_override;
  166. uint16_t ifc_sysctl_rx_budget;
  167. uint16_t ifc_sysctl_tx_abdicate;
  168. uint16_t ifc_sysctl_core_offset;
  169. #define CORE_OFFSET_UNSPECIFIED 0xffff
  170. uint8_t ifc_sysctl_separate_txrx;
  171. uint8_t ifc_sysctl_use_logical_cores;
  172. uint16_t ifc_sysctl_extra_msix_vectors;
  173. bool ifc_cpus_are_physical_cores;
  174. qidx_t ifc_sysctl_ntxds[8];
  175. qidx_t ifc_sysctl_nrxds[8];
  176. struct if_txrx ifc_txrx;
  177. #define isc_txd_encap ifc_txrx.ift_txd_encap
  178. #define isc_txd_flush ifc_txrx.ift_txd_flush
  179. #define isc_txd_credits_update ifc_txrx.ift_txd_credits_update
  180. #define isc_rxd_available ifc_txrx.ift_rxd_available
  181. #define isc_rxd_pkt_get ifc_txrx.ift_rxd_pkt_get
  182. #define isc_rxd_refill ifc_txrx.ift_rxd_refill
  183. #define isc_rxd_flush ifc_txrx.ift_rxd_flush
  184. #define isc_legacy_intr ifc_txrx.ift_legacy_intr
  185. #define isc_txq_select ifc_txrx.ift_txq_select
  186. #define isc_txq_select_v2 ifc_txrx.ift_txq_select_v2
  187. eventhandler_tag ifc_vlan_attach_event;
  188. eventhandler_tag ifc_vlan_detach_event;
  189. struct ether_addr ifc_mac;
  190. };
  191. void *
  192. iflib_get_softc(if_ctx_t ctx)
  193. {
  194. return (ctx->ifc_softc);
  195. }
  196. device_t
  197. iflib_get_dev(if_ctx_t ctx)
  198. {
  199. return (ctx->ifc_dev);
  200. }
  201. if_t
  202. iflib_get_ifp(if_ctx_t ctx)
  203. {
  204. return (ctx->ifc_ifp);
  205. }
  206. struct ifmedia *
  207. iflib_get_media(if_ctx_t ctx)
  208. {
  209. return (ctx->ifc_mediap);
  210. }
  211. void
  212. iflib_set_mac(if_ctx_t ctx, uint8_t mac[ETHER_ADDR_LEN])
  213. {
  214. bcopy(mac, ctx->ifc_mac.octet, ETHER_ADDR_LEN);
  215. }
  216. if_softc_ctx_t
  217. iflib_get_softc_ctx(if_ctx_t ctx)
  218. {
  219. return (&ctx->ifc_softc_ctx);
  220. }
  221. if_shared_ctx_t
  222. iflib_get_sctx(if_ctx_t ctx)
  223. {
  224. return (ctx->ifc_sctx);
  225. }
  226. uint16_t
  227. iflib_get_extra_msix_vectors_sysctl(if_ctx_t ctx)
  228. {
  229. return (ctx->ifc_sysctl_extra_msix_vectors);
  230. }
  231. #define IP_ALIGNED(m) ((((uintptr_t)(m)->m_data) & 0x3) == 0x2)
  232. #define CACHE_PTR_INCREMENT (CACHE_LINE_SIZE/sizeof(void*))
  233. #define CACHE_PTR_NEXT(ptr) ((void *)(((uintptr_t)(ptr)+CACHE_LINE_SIZE-1) & (CACHE_LINE_SIZE-1)))
  234. #define LINK_ACTIVE(ctx) ((ctx)->ifc_link_state == LINK_STATE_UP)
  235. #define CTX_IS_VF(ctx) ((ctx)->ifc_sctx->isc_flags & IFLIB_IS_VF)
  236. typedef struct iflib_sw_rx_desc_array {
  237. bus_dmamap_t *ifsd_map; /* bus_dma maps for packet */
  238. struct mbuf **ifsd_m; /* pkthdr mbufs */
  239. caddr_t *ifsd_cl; /* direct cluster pointer for rx */
  240. bus_addr_t *ifsd_ba; /* bus addr of cluster for rx */
  241. } iflib_rxsd_array_t;
  242. typedef struct iflib_sw_tx_desc_array {
  243. bus_dmamap_t *ifsd_map; /* bus_dma maps for packet */
  244. bus_dmamap_t *ifsd_tso_map; /* bus_dma maps for TSO packet */
  245. struct mbuf **ifsd_m; /* pkthdr mbufs */
  246. } if_txsd_vec_t;
  247. /* magic number that should be high enough for any hardware */
  248. #define IFLIB_MAX_TX_SEGS 128
  249. #define IFLIB_RX_COPY_THRESH 128
  250. #define IFLIB_MAX_RX_REFRESH 32
  251. /* The minimum descriptors per second before we start coalescing */
  252. #define IFLIB_MIN_DESC_SEC 16384
  253. #define IFLIB_DEFAULT_TX_UPDATE_FREQ 16
  254. #define IFLIB_QUEUE_IDLE 0
  255. #define IFLIB_QUEUE_HUNG 1
  256. #define IFLIB_QUEUE_WORKING 2
  257. /* maximum number of txqs that can share an rx interrupt */
  258. #define IFLIB_MAX_TX_SHARED_INTR 4
  259. /* this should really scale with ring size - this is a fairly arbitrary value */
  260. #define TX_BATCH_SIZE 32
  261. #define IFLIB_RESTART_BUDGET 8
  262. #define IFC_LEGACY 0x001
  263. #define IFC_QFLUSH 0x002
  264. #define IFC_MULTISEG 0x004
  265. #define IFC_SPARE1 0x008
  266. #define IFC_SC_ALLOCATED 0x010
  267. #define IFC_INIT_DONE 0x020
  268. #define IFC_PREFETCH 0x040
  269. #define IFC_DO_RESET 0x080
  270. #define IFC_DO_WATCHDOG 0x100
  271. #define IFC_SPARE0 0x200
  272. #define IFC_SPARE2 0x400
  273. #define IFC_IN_DETACH 0x800
  274. #define IFC_NETMAP_TX_IRQ 0x80000000
  275. #define CSUM_OFFLOAD (CSUM_IP_TSO|CSUM_IP6_TSO|CSUM_IP| \
  276. CSUM_IP_UDP|CSUM_IP_TCP|CSUM_IP_SCTP| \
  277. CSUM_IP6_UDP|CSUM_IP6_TCP|CSUM_IP6_SCTP)
  278. struct iflib_txq {
  279. qidx_t ift_in_use;
  280. qidx_t ift_cidx;
  281. qidx_t ift_cidx_processed;
  282. qidx_t ift_pidx;
  283. uint8_t ift_gen;
  284. uint8_t ift_br_offset;
  285. uint16_t ift_npending;
  286. uint16_t ift_db_pending;
  287. uint16_t ift_rs_pending;
  288. /* implicit pad */
  289. uint8_t ift_txd_size[8];
  290. uint64_t ift_processed;
  291. uint64_t ift_cleaned;
  292. uint64_t ift_cleaned_prev;
  293. #if MEMORY_LOGGING
  294. uint64_t ift_enqueued;
  295. uint64_t ift_dequeued;
  296. #endif
  297. uint64_t ift_no_tx_dma_setup;
  298. uint64_t ift_no_desc_avail;
  299. uint64_t ift_mbuf_defrag_failed;
  300. uint64_t ift_mbuf_defrag;
  301. uint64_t ift_map_failed;
  302. uint64_t ift_txd_encap_efbig;
  303. uint64_t ift_pullups;
  304. uint64_t ift_last_timer_tick;
  305. struct mtx ift_mtx;
  306. struct mtx ift_db_mtx;
  307. /* constant values */
  308. if_ctx_t ift_ctx;
  309. struct ifmp_ring *ift_br;
  310. struct grouptask ift_task;
  311. qidx_t ift_size;
  312. uint16_t ift_id;
  313. struct callout ift_timer;
  314. #ifdef DEV_NETMAP
  315. struct callout ift_netmap_timer;
  316. #endif /* DEV_NETMAP */
  317. if_txsd_vec_t ift_sds;
  318. uint8_t ift_qstatus;
  319. uint8_t ift_closed;
  320. uint8_t ift_update_freq;
  321. struct iflib_filter_info ift_filter_info;
  322. bus_dma_tag_t ift_buf_tag;
  323. bus_dma_tag_t ift_tso_buf_tag;
  324. iflib_dma_info_t ift_ifdi;
  325. #define MTX_NAME_LEN 32
  326. char ift_mtx_name[MTX_NAME_LEN];
  327. bus_dma_segment_t ift_segs[IFLIB_MAX_TX_SEGS] __aligned(CACHE_LINE_SIZE);
  328. #ifdef IFLIB_DIAGNOSTICS
  329. uint64_t ift_cpu_exec_count[256];
  330. #endif
  331. } __aligned(CACHE_LINE_SIZE);
  332. struct iflib_fl {
  333. qidx_t ifl_cidx;
  334. qidx_t ifl_pidx;
  335. qidx_t ifl_credits;
  336. uint8_t ifl_gen;
  337. uint8_t ifl_rxd_size;
  338. #if MEMORY_LOGGING
  339. uint64_t ifl_m_enqueued;
  340. uint64_t ifl_m_dequeued;
  341. uint64_t ifl_cl_enqueued;
  342. uint64_t ifl_cl_dequeued;
  343. #endif
  344. /* implicit pad */
  345. bitstr_t *ifl_rx_bitmap;
  346. qidx_t ifl_fragidx;
  347. /* constant */
  348. qidx_t ifl_size;
  349. uint16_t ifl_buf_size;
  350. uint16_t ifl_cltype;
  351. uma_zone_t ifl_zone;
  352. iflib_rxsd_array_t ifl_sds;
  353. iflib_rxq_t ifl_rxq;
  354. uint8_t ifl_id;
  355. bus_dma_tag_t ifl_buf_tag;
  356. iflib_dma_info_t ifl_ifdi;
  357. uint64_t ifl_bus_addrs[IFLIB_MAX_RX_REFRESH] __aligned(CACHE_LINE_SIZE);
  358. qidx_t ifl_rxd_idxs[IFLIB_MAX_RX_REFRESH];
  359. } __aligned(CACHE_LINE_SIZE);
  360. static inline qidx_t
  361. get_inuse(int size, qidx_t cidx, qidx_t pidx, uint8_t gen)
  362. {
  363. qidx_t used;
  364. if (pidx > cidx)
  365. used = pidx - cidx;
  366. else if (pidx < cidx)
  367. used = size - cidx + pidx;
  368. else if (gen == 0 && pidx == cidx)
  369. used = 0;
  370. else if (gen == 1 && pidx == cidx)
  371. used = size;
  372. else
  373. panic("bad state");
  374. return (used);
  375. }
  376. #define TXQ_AVAIL(txq) (txq->ift_size - get_inuse(txq->ift_size, txq->ift_cidx, txq->ift_pidx, txq->ift_gen))
  377. #define IDXDIFF(head, tail, wrap) \
  378. ((head) >= (tail) ? (head) - (tail) : (wrap) - (tail) + (head))
  379. struct iflib_rxq {
  380. if_ctx_t ifr_ctx;
  381. iflib_fl_t ifr_fl;
  382. uint64_t ifr_rx_irq;
  383. struct pfil_head *pfil;
  384. /*
  385. * If there is a separate completion queue (IFLIB_HAS_RXCQ), this is
  386. * the completion queue consumer index. Otherwise it's unused.
  387. */
  388. qidx_t ifr_cq_cidx;
  389. uint16_t ifr_id;
  390. uint8_t ifr_nfl;
  391. uint8_t ifr_ntxqirq;
  392. uint8_t ifr_txqid[IFLIB_MAX_TX_SHARED_INTR];
  393. uint8_t ifr_fl_offset;
  394. struct lro_ctrl ifr_lc;
  395. struct grouptask ifr_task;
  396. struct callout ifr_watchdog;
  397. struct iflib_filter_info ifr_filter_info;
  398. iflib_dma_info_t ifr_ifdi;
  399. /* dynamically allocate if any drivers need a value substantially larger than this */
  400. struct if_rxd_frag ifr_frags[IFLIB_MAX_RX_SEGS] __aligned(CACHE_LINE_SIZE);
  401. #ifdef IFLIB_DIAGNOSTICS
  402. uint64_t ifr_cpu_exec_count[256];
  403. #endif
  404. } __aligned(CACHE_LINE_SIZE);
  405. typedef struct if_rxsd {
  406. caddr_t *ifsd_cl;
  407. iflib_fl_t ifsd_fl;
  408. } *if_rxsd_t;
  409. /* multiple of word size */
  410. #ifdef __LP64__
  411. #define PKT_INFO_SIZE 6
  412. #define RXD_INFO_SIZE 5
  413. #define PKT_TYPE uint64_t
  414. #else
  415. #define PKT_INFO_SIZE 11
  416. #define RXD_INFO_SIZE 8
  417. #define PKT_TYPE uint32_t
  418. #endif
  419. #define PKT_LOOP_BOUND ((PKT_INFO_SIZE/3)*3)
  420. #define RXD_LOOP_BOUND ((RXD_INFO_SIZE/4)*4)
  421. typedef struct if_pkt_info_pad {
  422. PKT_TYPE pkt_val[PKT_INFO_SIZE];
  423. } *if_pkt_info_pad_t;
  424. typedef struct if_rxd_info_pad {
  425. PKT_TYPE rxd_val[RXD_INFO_SIZE];
  426. } *if_rxd_info_pad_t;
  427. CTASSERT(sizeof(struct if_pkt_info_pad) == sizeof(struct if_pkt_info));
  428. CTASSERT(sizeof(struct if_rxd_info_pad) == sizeof(struct if_rxd_info));
  429. static inline void
  430. pkt_info_zero(if_pkt_info_t pi)
  431. {
  432. if_pkt_info_pad_t pi_pad;
  433. pi_pad = (if_pkt_info_pad_t)pi;
  434. pi_pad->pkt_val[0] = 0; pi_pad->pkt_val[1] = 0; pi_pad->pkt_val[2] = 0;
  435. pi_pad->pkt_val[3] = 0; pi_pad->pkt_val[4] = 0; pi_pad->pkt_val[5] = 0;
  436. #ifndef __LP64__
  437. pi_pad->pkt_val[6] = 0; pi_pad->pkt_val[7] = 0; pi_pad->pkt_val[8] = 0;
  438. pi_pad->pkt_val[9] = 0; pi_pad->pkt_val[10] = 0;
  439. #endif
  440. }
  441. static inline void
  442. rxd_info_zero(if_rxd_info_t ri)
  443. {
  444. if_rxd_info_pad_t ri_pad;
  445. int i;
  446. ri_pad = (if_rxd_info_pad_t)ri;
  447. for (i = 0; i < RXD_LOOP_BOUND; i += 4) {
  448. ri_pad->rxd_val[i] = 0;
  449. ri_pad->rxd_val[i+1] = 0;
  450. ri_pad->rxd_val[i+2] = 0;
  451. ri_pad->rxd_val[i+3] = 0;
  452. }
  453. #ifdef __LP64__
  454. ri_pad->rxd_val[RXD_INFO_SIZE-1] = 0;
  455. #endif
  456. }
  457. /*
  458. * Only allow a single packet to take up most 1/nth of the tx ring
  459. */
  460. #define MAX_SINGLE_PACKET_FRACTION 12
  461. #define IF_BAD_DMA (bus_addr_t)-1
  462. #define CTX_ACTIVE(ctx) ((if_getdrvflags((ctx)->ifc_ifp) & IFF_DRV_RUNNING))
  463. #define CTX_LOCK_INIT(_sc) sx_init(&(_sc)->ifc_ctx_sx, "iflib ctx lock")
  464. #define CTX_LOCK(ctx) sx_xlock(&(ctx)->ifc_ctx_sx)
  465. #define CTX_UNLOCK(ctx) sx_xunlock(&(ctx)->ifc_ctx_sx)
  466. #define CTX_LOCK_DESTROY(ctx) sx_destroy(&(ctx)->ifc_ctx_sx)
  467. #define STATE_LOCK_INIT(_sc, _name) mtx_init(&(_sc)->ifc_state_mtx, _name, "iflib state lock", MTX_DEF)
  468. #define STATE_LOCK(ctx) mtx_lock(&(ctx)->ifc_state_mtx)
  469. #define STATE_UNLOCK(ctx) mtx_unlock(&(ctx)->ifc_state_mtx)
  470. #define STATE_LOCK_DESTROY(ctx) mtx_destroy(&(ctx)->ifc_state_mtx)
  471. #define CALLOUT_LOCK(txq) mtx_lock(&txq->ift_mtx)
  472. #define CALLOUT_UNLOCK(txq) mtx_unlock(&txq->ift_mtx)
  473. /* Our boot-time initialization hook */
  474. static int iflib_module_event_handler(module_t, int, void *);
  475. static moduledata_t iflib_moduledata = {
  476. "iflib",
  477. iflib_module_event_handler,
  478. NULL
  479. };
  480. DECLARE_MODULE(iflib, iflib_moduledata, SI_SUB_INIT_IF, SI_ORDER_ANY);
  481. MODULE_VERSION(iflib, 1);
  482. MODULE_DEPEND(iflib, pci, 1, 1, 1);
  483. MODULE_DEPEND(iflib, ether, 1, 1, 1);
  484. TASKQGROUP_DEFINE(if_io_tqg, mp_ncpus, 1);
  485. TASKQGROUP_DEFINE(if_config_tqg, 1, 1);
  486. #ifndef IFLIB_DEBUG_COUNTERS
  487. #ifdef INVARIANTS
  488. #define IFLIB_DEBUG_COUNTERS 1
  489. #else
  490. #define IFLIB_DEBUG_COUNTERS 0
  491. #endif /* !INVARIANTS */
  492. #endif
  493. static SYSCTL_NODE(_net, OID_AUTO, iflib, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
  494. "iflib driver parameters");
  495. /*
  496. * XXX need to ensure that this can't accidentally cause the head to be moved backwards
  497. */
  498. static int iflib_min_tx_latency = 0;
  499. SYSCTL_INT(_net_iflib, OID_AUTO, min_tx_latency, CTLFLAG_RW,
  500. &iflib_min_tx_latency, 0,
  501. "minimize transmit latency at the possible expense of throughput");
  502. static int iflib_no_tx_batch = 0;
  503. SYSCTL_INT(_net_iflib, OID_AUTO, no_tx_batch, CTLFLAG_RW,
  504. &iflib_no_tx_batch, 0,
  505. "minimize transmit latency at the possible expense of throughput");
  506. static int iflib_timer_default = 1000;
  507. SYSCTL_INT(_net_iflib, OID_AUTO, timer_default, CTLFLAG_RW,
  508. &iflib_timer_default, 0, "number of ticks between iflib_timer calls");
  509. #if IFLIB_DEBUG_COUNTERS
  510. static int iflib_tx_seen;
  511. static int iflib_tx_sent;
  512. static int iflib_tx_encap;
  513. static int iflib_rx_allocs;
  514. static int iflib_fl_refills;
  515. static int iflib_fl_refills_large;
  516. static int iflib_tx_frees;
  517. SYSCTL_INT(_net_iflib, OID_AUTO, tx_seen, CTLFLAG_RD, &iflib_tx_seen, 0,
  518. "# TX mbufs seen");
  519. SYSCTL_INT(_net_iflib, OID_AUTO, tx_sent, CTLFLAG_RD, &iflib_tx_sent, 0,
  520. "# TX mbufs sent");
  521. SYSCTL_INT(_net_iflib, OID_AUTO, tx_encap, CTLFLAG_RD, &iflib_tx_encap, 0,
  522. "# TX mbufs encapped");
  523. SYSCTL_INT(_net_iflib, OID_AUTO, tx_frees, CTLFLAG_RD, &iflib_tx_frees, 0,
  524. "# TX frees");
  525. SYSCTL_INT(_net_iflib, OID_AUTO, rx_allocs, CTLFLAG_RD, &iflib_rx_allocs, 0,
  526. "# RX allocations");
  527. SYSCTL_INT(_net_iflib, OID_AUTO, fl_refills, CTLFLAG_RD, &iflib_fl_refills, 0,
  528. "# refills");
  529. SYSCTL_INT(_net_iflib, OID_AUTO, fl_refills_large, CTLFLAG_RD,
  530. &iflib_fl_refills_large, 0, "# large refills");
  531. static int iflib_txq_drain_flushing;
  532. static int iflib_txq_drain_oactive;
  533. static int iflib_txq_drain_notready;
  534. SYSCTL_INT(_net_iflib, OID_AUTO, txq_drain_flushing, CTLFLAG_RD,
  535. &iflib_txq_drain_flushing, 0, "# drain flushes");
  536. SYSCTL_INT(_net_iflib, OID_AUTO, txq_drain_oactive, CTLFLAG_RD,
  537. &iflib_txq_drain_oactive, 0, "# drain oactives");
  538. SYSCTL_INT(_net_iflib, OID_AUTO, txq_drain_notready, CTLFLAG_RD,
  539. &iflib_txq_drain_notready, 0, "# drain notready");
  540. static int iflib_encap_load_mbuf_fail;
  541. static int iflib_encap_pad_mbuf_fail;
  542. static int iflib_encap_txq_avail_fail;
  543. static int iflib_encap_txd_encap_fail;
  544. SYSCTL_INT(_net_iflib, OID_AUTO, encap_load_mbuf_fail, CTLFLAG_RD,
  545. &iflib_encap_load_mbuf_fail, 0, "# busdma load failures");
  546. SYSCTL_INT(_net_iflib, OID_AUTO, encap_pad_mbuf_fail, CTLFLAG_RD,
  547. &iflib_encap_pad_mbuf_fail, 0, "# runt frame pad failures");
  548. SYSCTL_INT(_net_iflib, OID_AUTO, encap_txq_avail_fail, CTLFLAG_RD,
  549. &iflib_encap_txq_avail_fail, 0, "# txq avail failures");
  550. SYSCTL_INT(_net_iflib, OID_AUTO, encap_txd_encap_fail, CTLFLAG_RD,
  551. &iflib_encap_txd_encap_fail, 0, "# driver encap failures");
  552. static int iflib_task_fn_rxs;
  553. static int iflib_rx_intr_enables;
  554. static int iflib_fast_intrs;
  555. static int iflib_rx_unavail;
  556. static int iflib_rx_ctx_inactive;
  557. static int iflib_rx_if_input;
  558. static int iflib_rxd_flush;
  559. static int iflib_verbose_debug;
  560. SYSCTL_INT(_net_iflib, OID_AUTO, task_fn_rx, CTLFLAG_RD, &iflib_task_fn_rxs, 0,
  561. "# task_fn_rx calls");
  562. SYSCTL_INT(_net_iflib, OID_AUTO, rx_intr_enables, CTLFLAG_RD,
  563. &iflib_rx_intr_enables, 0, "# RX intr enables");
  564. SYSCTL_INT(_net_iflib, OID_AUTO, fast_intrs, CTLFLAG_RD, &iflib_fast_intrs, 0,
  565. "# fast_intr calls");
  566. SYSCTL_INT(_net_iflib, OID_AUTO, rx_unavail, CTLFLAG_RD, &iflib_rx_unavail, 0,
  567. "# times rxeof called with no available data");
  568. SYSCTL_INT(_net_iflib, OID_AUTO, rx_ctx_inactive, CTLFLAG_RD,
  569. &iflib_rx_ctx_inactive, 0, "# times rxeof called with inactive context");
  570. SYSCTL_INT(_net_iflib, OID_AUTO, rx_if_input, CTLFLAG_RD, &iflib_rx_if_input,
  571. 0, "# times rxeof called if_input");
  572. SYSCTL_INT(_net_iflib, OID_AUTO, rxd_flush, CTLFLAG_RD, &iflib_rxd_flush, 0,
  573. "# times rxd_flush called");
  574. SYSCTL_INT(_net_iflib, OID_AUTO, verbose_debug, CTLFLAG_RW,
  575. &iflib_verbose_debug, 0, "enable verbose debugging");
  576. #define DBG_COUNTER_INC(name) atomic_add_int(&(iflib_ ## name), 1)
  577. static void
  578. iflib_debug_reset(void)
  579. {
  580. iflib_tx_seen = iflib_tx_sent = iflib_tx_encap = iflib_rx_allocs =
  581. iflib_fl_refills = iflib_fl_refills_large = iflib_tx_frees =
  582. iflib_txq_drain_flushing = iflib_txq_drain_oactive =
  583. iflib_txq_drain_notready =
  584. iflib_encap_load_mbuf_fail = iflib_encap_pad_mbuf_fail =
  585. iflib_encap_txq_avail_fail = iflib_encap_txd_encap_fail =
  586. iflib_task_fn_rxs = iflib_rx_intr_enables = iflib_fast_intrs =
  587. iflib_rx_unavail =
  588. iflib_rx_ctx_inactive = iflib_rx_if_input =
  589. iflib_rxd_flush = 0;
  590. }
  591. #else
  592. #define DBG_COUNTER_INC(name)
  593. static void iflib_debug_reset(void) {}
  594. #endif
  595. #define IFLIB_DEBUG 0
  596. static void iflib_tx_structures_free(if_ctx_t ctx);
  597. static void iflib_rx_structures_free(if_ctx_t ctx);
  598. static int iflib_queues_alloc(if_ctx_t ctx);
  599. static int iflib_tx_credits_update(if_ctx_t ctx, iflib_txq_t txq);
  600. static int iflib_rxd_avail(if_ctx_t ctx, iflib_rxq_t rxq, qidx_t cidx, qidx_t budget);
  601. static int iflib_qset_structures_setup(if_ctx_t ctx);
  602. static int iflib_msix_init(if_ctx_t ctx);
  603. static int iflib_legacy_setup(if_ctx_t ctx, driver_filter_t filter, void *filterarg, int *rid, const char *str);
  604. static void iflib_txq_check_drain(iflib_txq_t txq, int budget);
  605. static uint32_t iflib_txq_can_drain(struct ifmp_ring *);
  606. #ifdef ALTQ
  607. static void iflib_altq_if_start(if_t ifp);
  608. static int iflib_altq_if_transmit(if_t ifp, struct mbuf *m);
  609. #endif
  610. static int iflib_register(if_ctx_t);
  611. static void iflib_deregister(if_ctx_t);
  612. static void iflib_unregister_vlan_handlers(if_ctx_t ctx);
  613. static uint16_t iflib_get_mbuf_size_for(unsigned int size);
  614. static void iflib_init_locked(if_ctx_t ctx);
  615. static void iflib_add_device_sysctl_pre(if_ctx_t ctx);
  616. static void iflib_add_device_sysctl_post(if_ctx_t ctx);
  617. static void iflib_ifmp_purge(iflib_txq_t txq);
  618. static void _iflib_pre_assert(if_softc_ctx_t scctx);
  619. static void iflib_stop(if_ctx_t ctx);
  620. static void iflib_if_init_locked(if_ctx_t ctx);
  621. static void iflib_free_intr_mem(if_ctx_t ctx);
  622. #ifndef __NO_STRICT_ALIGNMENT
  623. static struct mbuf * iflib_fixup_rx(struct mbuf *m);
  624. #endif
  625. static SLIST_HEAD(cpu_offset_list, cpu_offset) cpu_offsets =
  626. SLIST_HEAD_INITIALIZER(cpu_offsets);
  627. struct cpu_offset {
  628. SLIST_ENTRY(cpu_offset) entries;
  629. cpuset_t set;
  630. unsigned int refcount;
  631. uint16_t next_cpuid;
  632. };
  633. static struct mtx cpu_offset_mtx;
  634. MTX_SYSINIT(iflib_cpu_offset, &cpu_offset_mtx, "iflib_cpu_offset lock",
  635. MTX_DEF);
  636. DEBUGNET_DEFINE(iflib);
  637. static int
  638. iflib_num_rx_descs(if_ctx_t ctx)
  639. {
  640. if_softc_ctx_t scctx = &ctx->ifc_softc_ctx;
  641. if_shared_ctx_t sctx = ctx->ifc_sctx;
  642. uint16_t first_rxq = (sctx->isc_flags & IFLIB_HAS_RXCQ) ? 1 : 0;
  643. return scctx->isc_nrxd[first_rxq];
  644. }
  645. static int
  646. iflib_num_tx_descs(if_ctx_t ctx)
  647. {
  648. if_softc_ctx_t scctx = &ctx->ifc_softc_ctx;
  649. if_shared_ctx_t sctx = ctx->ifc_sctx;
  650. uint16_t first_txq = (sctx->isc_flags & IFLIB_HAS_TXCQ) ? 1 : 0;
  651. return scctx->isc_ntxd[first_txq];
  652. }
  653. #ifdef DEV_NETMAP
  654. #include <sys/selinfo.h>
  655. #include <net/netmap.h>
  656. #include <dev/netmap/netmap_kern.h>
  657. MODULE_DEPEND(iflib, netmap, 1, 1, 1);
  658. static int netmap_fl_refill(iflib_rxq_t rxq, struct netmap_kring *kring, bool init);
  659. static void iflib_netmap_timer(void *arg);
  660. /*
  661. * device-specific sysctl variables:
  662. *
  663. * iflib_crcstrip: 0: keep CRC in rx frames (default), 1: strip it.
  664. * During regular operations the CRC is stripped, but on some
  665. * hardware reception of frames not multiple of 64 is slower,
  666. * so using crcstrip=0 helps in benchmarks.
  667. *
  668. * iflib_rx_miss, iflib_rx_miss_bufs:
  669. * count packets that might be missed due to lost interrupts.
  670. */
  671. SYSCTL_DECL(_dev_netmap);
  672. /*
  673. * The xl driver by default strips CRCs and we do not override it.
  674. */
  675. int iflib_crcstrip = 1;
  676. SYSCTL_INT(_dev_netmap, OID_AUTO, iflib_crcstrip,
  677. CTLFLAG_RW, &iflib_crcstrip, 1, "strip CRC on RX frames");
  678. int iflib_rx_miss, iflib_rx_miss_bufs;
  679. SYSCTL_INT(_dev_netmap, OID_AUTO, iflib_rx_miss,
  680. CTLFLAG_RW, &iflib_rx_miss, 0, "potentially missed RX intr");
  681. SYSCTL_INT(_dev_netmap, OID_AUTO, iflib_rx_miss_bufs,
  682. CTLFLAG_RW, &iflib_rx_miss_bufs, 0, "potentially missed RX intr bufs");
  683. /*
  684. * Register/unregister. We are already under netmap lock.
  685. * Only called on the first register or the last unregister.
  686. */
  687. static int
  688. iflib_netmap_register(struct netmap_adapter *na, int onoff)
  689. {
  690. if_t ifp = na->ifp;
  691. if_ctx_t ctx = if_getsoftc(ifp);
  692. int status;
  693. CTX_LOCK(ctx);
  694. if (!CTX_IS_VF(ctx))
  695. IFDI_CRCSTRIP_SET(ctx, onoff, iflib_crcstrip);
  696. iflib_stop(ctx);
  697. /*
  698. * Enable (or disable) netmap flags, and intercept (or restore)
  699. * ifp->if_transmit. This is done once the device has been stopped
  700. * to prevent race conditions. Also, this must be done after
  701. * calling netmap_disable_all_rings() and before calling
  702. * netmap_enable_all_rings(), so that these two functions see the
  703. * updated state of the NAF_NETMAP_ON bit.
  704. */
  705. if (onoff) {
  706. nm_set_native_flags(na);
  707. } else {
  708. nm_clear_native_flags(na);
  709. }
  710. iflib_init_locked(ctx);
  711. IFDI_CRCSTRIP_SET(ctx, onoff, iflib_crcstrip); // XXX why twice ?
  712. status = if_getdrvflags(ifp) & IFF_DRV_RUNNING ? 0 : 1;
  713. if (status)
  714. nm_clear_native_flags(na);
  715. CTX_UNLOCK(ctx);
  716. return (status);
  717. }
  718. static int
  719. iflib_netmap_config(struct netmap_adapter *na, struct nm_config_info *info)
  720. {
  721. if_t ifp = na->ifp;
  722. if_ctx_t ctx = if_getsoftc(ifp);
  723. iflib_rxq_t rxq = &ctx->ifc_rxqs[0];
  724. iflib_fl_t fl = &rxq->ifr_fl[0];
  725. info->num_tx_rings = ctx->ifc_softc_ctx.isc_ntxqsets;
  726. info->num_rx_rings = ctx->ifc_softc_ctx.isc_nrxqsets;
  727. info->num_tx_descs = iflib_num_tx_descs(ctx);
  728. info->num_rx_descs = iflib_num_rx_descs(ctx);
  729. info->rx_buf_maxsize = fl->ifl_buf_size;
  730. nm_prinf("txr %u rxr %u txd %u rxd %u rbufsz %u",
  731. info->num_tx_rings, info->num_rx_rings, info->num_tx_descs,
  732. info->num_rx_descs, info->rx_buf_maxsize);
  733. return 0;
  734. }
  735. static int
  736. netmap_fl_refill(iflib_rxq_t rxq, struct netmap_kring *kring, bool init)
  737. {
  738. struct netmap_adapter *na = kring->na;
  739. u_int const lim = kring->nkr_num_slots - 1;
  740. struct netmap_ring *ring = kring->ring;
  741. bus_dmamap_t *map;
  742. struct if_rxd_update iru;
  743. if_ctx_t ctx = rxq->ifr_ctx;
  744. iflib_fl_t fl = &rxq->ifr_fl[0];
  745. u_int nic_i_first, nic_i;
  746. u_int nm_i;
  747. int i, n;
  748. #if IFLIB_DEBUG_COUNTERS
  749. int rf_count = 0;
  750. #endif
  751. /*
  752. * This function is used both at initialization and in rxsync.
  753. * At initialization we need to prepare (with isc_rxd_refill())
  754. * all the netmap buffers currently owned by the kernel, in
  755. * such a way to keep fl->ifl_pidx and kring->nr_hwcur in sync
  756. * (except for kring->nkr_hwofs). These may be less than
  757. * kring->nkr_num_slots if netmap_reset() was called while
  758. * an application using the kring that still owned some
  759. * buffers.
  760. * At rxsync time, both indexes point to the next buffer to be
  761. * refilled.
  762. * In any case we publish (with isc_rxd_flush()) up to
  763. * (fl->ifl_pidx - 1) % N (included), to avoid the NIC tail/prod
  764. * pointer to overrun the head/cons pointer, although this is
  765. * not necessary for some NICs (e.g. vmx).
  766. */
  767. if (__predict_false(init)) {
  768. n = kring->nkr_num_slots - nm_kr_rxspace(kring);
  769. } else {
  770. n = kring->rhead - kring->nr_hwcur;
  771. if (n == 0)
  772. return (0); /* Nothing to do. */
  773. if (n < 0)
  774. n += kring->nkr_num_slots;
  775. }
  776. iru_init(&iru, rxq, 0 /* flid */);
  777. map = fl->ifl_sds.ifsd_map;
  778. nic_i = fl->ifl_pidx;
  779. nm_i = netmap_idx_n2k(kring, nic_i);
  780. if (__predict_false(init)) {
  781. /*
  782. * On init/reset, nic_i must be 0, and we must
  783. * start to refill from hwtail (see netmap_reset()).
  784. */
  785. MPASS(nic_i == 0);
  786. MPASS(nm_i == kring->nr_hwtail);
  787. } else
  788. MPASS(nm_i == kring->nr_hwcur);
  789. DBG_COUNTER_INC(fl_refills);
  790. while (n > 0) {
  791. #if IFLIB_DEBUG_COUNTERS
  792. if (++rf_count == 9)
  793. DBG_COUNTER_INC(fl_refills_large);
  794. #endif
  795. nic_i_first = nic_i;
  796. for (i = 0; n > 0 && i < IFLIB_MAX_RX_REFRESH; n--, i++) {
  797. struct netmap_slot *slot = &ring->slot[nm_i];
  798. uint64_t paddr;
  799. void *addr = PNMB(na, slot, &paddr);
  800. MPASS(i < IFLIB_MAX_RX_REFRESH);
  801. if (addr == NETMAP_BUF_BASE(na)) /* bad buf */
  802. return netmap_ring_reinit(kring);
  803. fl->ifl_bus_addrs[i] = paddr +
  804. nm_get_offset(kring, slot);
  805. fl->ifl_rxd_idxs[i] = nic_i;
  806. if (__predict_false(init)) {
  807. netmap_load_map(na, fl->ifl_buf_tag,
  808. map[nic_i], addr);
  809. } else if (slot->flags & NS_BUF_CHANGED) {
  810. /* buffer has changed, reload map */
  811. netmap_reload_map(na, fl->ifl_buf_tag,
  812. map[nic_i], addr);
  813. }
  814. bus_dmamap_sync(fl->ifl_buf_tag, map[nic_i],
  815. BUS_DMASYNC_PREREAD);
  816. slot->flags &= ~NS_BUF_CHANGED;
  817. nm_i = nm_next(nm_i, lim);
  818. nic_i = nm_next(nic_i, lim);
  819. }
  820. iru.iru_pidx = nic_i_first;
  821. iru.iru_count = i;
  822. ctx->isc_rxd_refill(ctx->ifc_softc, &iru);
  823. }
  824. fl->ifl_pidx = nic_i;
  825. /*
  826. * At the end of the loop we must have refilled everything
  827. * we could possibly refill.
  828. */
  829. MPASS(nm_i == kring->rhead);
  830. kring->nr_hwcur = nm_i;
  831. bus_dmamap_sync(fl->ifl_ifdi->idi_tag, fl->ifl_ifdi->idi_map,
  832. BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
  833. ctx->isc_rxd_flush(ctx->ifc_softc, rxq->ifr_id, fl->ifl_id,
  834. nm_prev(nic_i, lim));
  835. DBG_COUNTER_INC(rxd_flush);
  836. return (0);
  837. }
  838. #define NETMAP_TX_TIMER_US 90
  839. /*
  840. * Reconcile kernel and user view of the transmit ring.
  841. *
  842. * All information is in the kring.
  843. * Userspace wants to send packets up to the one before kring->rhead,
  844. * kernel knows kring->nr_hwcur is the first unsent packet.
  845. *
  846. * Here we push packets out (as many as possible), and possibly
  847. * reclaim buffers from previously completed transmission.
  848. *
  849. * The caller (netmap) guarantees that there is only one instance
  850. * running at any time. Any interference with other driver
  851. * methods should be handled by the individual drivers.
  852. */
  853. static int
  854. iflib_netmap_txsync(struct netmap_kring *kring, int flags)
  855. {
  856. struct netmap_adapter *na = kring->na;
  857. if_t ifp = na->ifp;
  858. struct netmap_ring *ring = kring->ring;
  859. u_int nm_i; /* index into the netmap kring */
  860. u_int nic_i; /* index into the NIC ring */
  861. u_int const lim = kring->nkr_num_slots - 1;
  862. u_int const head = kring->rhead;
  863. struct if_pkt_info pi;
  864. int tx_pkts = 0, tx_bytes = 0;
  865. /*
  866. * interrupts on every tx packet are expensive so request
  867. * them every half ring, or where NS_REPORT is set
  868. */
  869. u_int report_frequency = kring->nkr_num_slots >> 1;
  870. /* device-specific */
  871. if_ctx_t ctx = if_getsoftc(ifp);
  872. iflib_txq_t txq = &ctx->ifc_txqs[kring->ring_id];
  873. bus_dmamap_sync(txq->ift_ifdi->idi_tag, txq->ift_ifdi->idi_map,
  874. BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
  875. /*
  876. * First part: process new packets to send.
  877. * nm_i is the current index in the netmap kring,
  878. * nic_i is the corresponding index in the NIC ring.
  879. *
  880. * If we have packets to send (nm_i != head)
  881. * iterate over the netmap ring, fetch length and update
  882. * the corresponding slot in the NIC ring. Some drivers also
  883. * need to update the buffer's physical address in the NIC slot
  884. * even NS_BUF_CHANGED is not set (PNMB computes the addresses).
  885. *
  886. * The netmap_reload_map() calls is especially expensive,
  887. * even when (as in this case) the tag is 0, so do only
  888. * when the buffer has actually changed.
  889. *
  890. * If possible do not set the report/intr bit on all slots,
  891. * but only a few times per ring or when NS_REPORT is set.
  892. *
  893. * Finally, on 10G and faster drivers, it might be useful
  894. * to prefetch the next slot and txr entry.
  895. */
  896. nm_i = kring->nr_hwcur;
  897. if (nm_i != head) { /* we have new packets to send */
  898. uint32_t pkt_len = 0, seg_idx = 0;
  899. int nic_i_start = -1, flags = 0;
  900. pkt_info_zero(&pi);
  901. pi.ipi_segs = txq->ift_segs;
  902. pi.ipi_qsidx = kring->ring_id;
  903. nic_i = netmap_idx_k2n(kring, nm_i);
  904. __builtin_prefetch(&ring->slot[nm_i]);
  905. __builtin_prefetch(&txq->ift_sds.ifsd_m[nic_i]);
  906. __builtin_prefetch(&txq->ift_sds.ifsd_map[nic_i]);
  907. while (nm_i != head) {
  908. struct netmap_slot *slot = &ring->slot[nm_i];
  909. uint64_t offset = nm_get_offset(kring, slot);
  910. u_int len = slot->len;
  911. uint64_t paddr;
  912. void *addr = PNMB(na, slot, &paddr);
  913. flags |= (slot->flags & NS_REPORT ||
  914. nic_i == 0 || nic_i == report_frequency) ?
  915. IPI_TX_INTR : 0;
  916. /*
  917. * If this is the first packet fragment, save the
  918. * index of the first NIC slot for later.
  919. */
  920. if (nic_i_start < 0)
  921. nic_i_start = nic_i;
  922. pi.ipi_segs[seg_idx].ds_addr = paddr + offset;
  923. pi.ipi_segs[seg_idx].ds_len = len;
  924. if (len) {
  925. pkt_len += len;
  926. seg_idx++;
  927. }
  928. if (!(slot->flags & NS_MOREFRAG)) {
  929. pi.ipi_len = pkt_len;
  930. pi.ipi_nsegs = seg_idx;
  931. pi.ipi_pidx = nic_i_start;
  932. pi.ipi_ndescs = 0;
  933. pi.ipi_flags = flags;
  934. /* Prepare the NIC TX ring. */
  935. ctx->isc_txd_encap(ctx->ifc_softc, &pi);
  936. DBG_COUNTER_INC(tx_encap);
  937. /* Update transmit counters */
  938. tx_bytes += pi.ipi_len;
  939. tx_pkts++;
  940. /* Reinit per-packet info for the next one. */
  941. flags = seg_idx = pkt_len = 0;
  942. nic_i_start = -1;
  943. }
  944. /* prefetch for next round */
  945. __builtin_prefetch(&ring->slot[nm_i + 1]);
  946. __builtin_prefetch(&txq->ift_sds.ifsd_m[nic_i + 1]);
  947. __builtin_prefetch(&txq->ift_sds.ifsd_map[nic_i + 1]);
  948. NM_CHECK_ADDR_LEN_OFF(na, len, offset);
  949. if (slot->flags & NS_BUF_CHANGED) {
  950. /* buffer has changed, reload map */
  951. netmap_reload_map(na, txq->ift_buf_tag,
  952. txq->ift_sds.ifsd_map[nic_i], addr);
  953. }
  954. /* make sure changes to the buffer are synced */
  955. bus_dmamap_sync(txq->ift_buf_tag,
  956. txq->ift_sds.ifsd_map[nic_i],
  957. BUS_DMASYNC_PREWRITE);
  958. slot->flags &= ~(NS_REPORT | NS_BUF_CHANGED | NS_MOREFRAG);
  959. nm_i = nm_next(nm_i, lim);
  960. nic_i = nm_next(nic_i, lim);
  961. }
  962. kring->nr_hwcur = nm_i;
  963. /* synchronize the NIC ring */
  964. bus_dmamap_sync(txq->ift_ifdi->idi_tag, txq->ift_ifdi->idi_map,
  965. BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
  966. /* (re)start the tx unit up to slot nic_i (excluded) */
  967. ctx->isc_txd_flush(ctx->ifc_softc, txq->ift_id, nic_i);
  968. }
  969. /*
  970. * Second part: reclaim buffers for completed transmissions.
  971. *
  972. * If there are unclaimed buffers, attempt to reclaim them.
  973. * If we don't manage to reclaim them all, and TX IRQs are not in use,
  974. * trigger a per-tx-queue timer to try again later.
  975. */
  976. if (kring->nr_hwtail != nm_prev(kring->nr_hwcur, lim)) {
  977. if (iflib_tx_credits_update(ctx, txq)) {
  978. /* some tx completed, increment avail */
  979. nic_i = txq->ift_cidx_processed;
  980. kring->nr_hwtail = nm_prev(netmap_idx_n2k(kring, nic_i), lim);
  981. }
  982. }
  983. if (!(ctx->ifc_flags & IFC_NETMAP_TX_IRQ))
  984. if (kring->nr_hwtail != nm_prev(kring->nr_hwcur, lim)) {
  985. callout_reset_sbt_on(&txq->ift_netmap_timer,
  986. NETMAP_TX_TIMER_US * SBT_1US, SBT_1US,
  987. iflib_netmap_timer, txq,
  988. txq->ift_netmap_timer.c_cpu, 0);
  989. }
  990. if_inc_counter(ifp, IFCOUNTER_OBYTES, tx_bytes);
  991. if_inc_counter(ifp, IFCOUNTER_OPACKETS, tx_pkts);
  992. return (0);
  993. }
  994. /*
  995. * Reconcile kernel and user view of the receive ring.
  996. * Same as for the txsync, this routine must be efficient.
  997. * The caller guarantees a single invocations, but races against
  998. * the rest of the driver should be handled here.
  999. *
  1000. * On call, kring->rhead is the first packet that userspace wants
  1001. * to keep, and kring->rcur is the wakeup point.
  1002. * The kernel has previously reported packets up to kring->rtail.
  1003. *
  1004. * If (flags & NAF_FORCE_READ) also check for incoming packets irrespective
  1005. * of whether or not we received an interrupt.
  1006. */
  1007. static int
  1008. iflib_netmap_rxsync(struct netmap_kring *kring, int flags)
  1009. {
  1010. struct netmap_adapter *na = kring->na;
  1011. struct netmap_ring *ring = kring->ring;
  1012. if_t ifp = na->ifp;
  1013. uint32_t nm_i; /* index into the netmap ring */
  1014. uint32_t nic_i; /* index into the NIC ring */
  1015. u_int n;
  1016. u_int const lim = kring->nkr_num_slots - 1;
  1017. int force_update = (flags & NAF_FORCE_READ) || kring->nr_kflags & NKR_PENDINTR;
  1018. int i = 0, rx_bytes = 0, rx_pkts = 0;
  1019. if_ctx_t ctx = if_getsoftc(ifp);
  1020. if_shared_ctx_t sctx = ctx->ifc_sctx;
  1021. if_softc_ctx_t scctx = &ctx->ifc_softc_ctx;
  1022. iflib_rxq_t rxq = &ctx->ifc_rxqs[kring->ring_id];
  1023. iflib_fl_t fl = &rxq->ifr_fl[0];
  1024. struct if_rxd_info ri;
  1025. qidx_t *cidxp;
  1026. /*
  1027. * netmap only uses free list 0, to avoid out of order consumption
  1028. * of receive buffers
  1029. */
  1030. bus_dmamap_sync(fl->ifl_ifdi->idi_tag, fl->ifl_ifdi->idi_map,
  1031. BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
  1032. /*
  1033. * First part: import newly received packets.
  1034. *
  1035. * nm_i is the index of the next free slot in the netmap ring,
  1036. * nic_i is the index of the next received packet in the NIC ring
  1037. * (or in the free list 0 if IFLIB_HAS_RXCQ is set), and they may
  1038. * differ in case if_init() has been called while
  1039. * in netmap mode. For the receive ring we have
  1040. *
  1041. * nic_i = fl->ifl_cidx;
  1042. * nm_i = kring->nr_hwtail (previous)
  1043. * and
  1044. * nm_i == (nic_i + kring->nkr_hwofs) % ring_size
  1045. *
  1046. * fl->ifl_cidx is set to 0 on a ring reinit
  1047. */
  1048. if (netmap_no_pendintr || force_update) {
  1049. uint32_t hwtail_lim = nm_prev(kring->nr_hwcur, lim);
  1050. bool have_rxcq = sctx->isc_flags & IFLIB_HAS_RXCQ;
  1051. int crclen = iflib_crcstrip ? 0 : 4;
  1052. int error, avail;
  1053. /*
  1054. * For the free list consumer index, we use the same
  1055. * logic as in iflib_rxeof().
  1056. */
  1057. if (have_rxcq)
  1058. cidxp = &rxq->ifr_cq_cidx;
  1059. else
  1060. cidxp = &fl->ifl_cidx;
  1061. avail = ctx->isc_rxd_available(ctx->ifc_softc,
  1062. rxq->ifr_id, *cidxp, USHRT_MAX);
  1063. nic_i = fl->ifl_cidx;
  1064. nm_i = netmap_idx_n2k(kring, nic_i);
  1065. MPASS(nm_i == kring->nr_hwtail);
  1066. for (n = 0; avail > 0 && nm_i != hwtail_lim; n++, avail--) {
  1067. rxd_info_zero(&ri);
  1068. ri.iri_frags = rxq->ifr_frags;
  1069. ri.iri_qsidx = kring->ring_id;
  1070. ri.iri_ifp = ctx->ifc_ifp;
  1071. ri.iri_cidx = *cidxp;
  1072. error = ctx->isc_rxd_pkt_get(ctx->ifc_softc, &ri);
  1073. for (i = 0; i < ri.iri_nfrags; i++) {
  1074. if (error) {
  1075. ring->slot[nm_i].len = 0;
  1076. ring->slot[nm_i].flags = 0;
  1077. } else {
  1078. ring->slot[nm_i].len = ri.iri_frags[i].irf_len;
  1079. if (i == (ri.iri_nfrags - 1)) {
  1080. ring->slot[nm_i].len -= crclen;
  1081. ring->slot[nm_i].flags = 0;
  1082. /* Update receive counters */
  1083. rx_bytes += ri.iri_len;
  1084. rx_pkts++;
  1085. } else
  1086. ring->slot[nm_i].flags = NS_MOREFRAG;
  1087. }
  1088. bus_dmamap_sync(fl->ifl_buf_tag,
  1089. fl->ifl_sds.ifsd_map[nic_i], BUS_DMASYNC_POSTREAD);
  1090. nm_i = nm_next(nm_i, lim);
  1091. fl->ifl_cidx = nic_i = nm_next(nic_i, lim);
  1092. }
  1093. if (have_rxcq) {
  1094. *cidxp = ri.iri_cidx;
  1095. while (*cidxp >= scctx->isc_nrxd[0])
  1096. *cidxp -= scctx->isc_nrxd[0];
  1097. }
  1098. }
  1099. if (n) { /* update the state variables */
  1100. if (netmap_no_pendintr && !force_update) {
  1101. /* diagnostics */
  1102. iflib_rx_miss ++;
  1103. iflib_rx_miss_bufs += n;
  1104. }
  1105. kring->nr_hwtail = nm_i;
  1106. }
  1107. kring->nr_kflags &= ~NKR_PENDINTR;
  1108. }
  1109. /*
  1110. * Second part: skip past packets that userspace has released.
  1111. * (kring->nr_hwcur to head excluded),
  1112. * and make the buffers available for reception.
  1113. * As usual nm_i is the index in the netmap ring,
  1114. * nic_i is the index in the NIC ring, and
  1115. * nm_i == (nic_i + kring->nkr_hwofs) % ring_size
  1116. */
  1117. netmap_fl_refill(rxq, kring, false);
  1118. if_inc_counter(ifp, IFCOUNTER_IBYTES, rx_bytes);
  1119. if_inc_counter(ifp, IFCOUNTER_IPACKETS, rx_pkts);
  1120. return (0);
  1121. }
  1122. static void
  1123. iflib_netmap_intr(struct netmap_adapter *na, int onoff)
  1124. {
  1125. if_ctx_t ctx = if_getsoftc(na->ifp);
  1126. CTX_LOCK(ctx);
  1127. if (onoff) {
  1128. IFDI_INTR_ENABLE(ctx);
  1129. } else {
  1130. IFDI_INTR_DISABLE(ctx);
  1131. }
  1132. CTX_UNLOCK(ctx);
  1133. }
  1134. static int
  1135. iflib_netmap_attach(if_ctx_t ctx)
  1136. {
  1137. struct netmap_adapter na;
  1138. bzero(&na, sizeof(na));
  1139. na.ifp = ctx->ifc_ifp;
  1140. na.na_flags = NAF_BDG_MAYSLEEP | NAF_MOREFRAG | NAF_OFFSETS;
  1141. MPASS(ctx->ifc_softc_ctx.isc_ntxqsets);
  1142. MPASS(ctx->ifc_softc_ctx.isc_nrxqsets);
  1143. na.num_tx_desc = iflib_num_tx_descs(ctx);
  1144. na.num_rx_desc = iflib_num_rx_descs(ctx);
  1145. na.nm_txsync = iflib_netmap_txsync;
  1146. na.nm_rxsync = iflib_netmap_rxsync;
  1147. na.nm_register = iflib_netmap_register;
  1148. na.nm_intr = iflib_netmap_intr;
  1149. na.nm_config = iflib_netmap_config;
  1150. na.num_tx_rings = ctx->ifc_softc_ctx.isc_ntxqsets;
  1151. na.num_rx_rings = ctx->ifc_softc_ctx.isc_nrxqsets;
  1152. return (netmap_attach(&na));
  1153. }
  1154. static int
  1155. iflib_netmap_txq_init(if_ctx_t ctx, iflib_txq_t txq)
  1156. {
  1157. struct netmap_adapter *na = NA(ctx->ifc_ifp);
  1158. struct netmap_slot *slot;
  1159. slot = netmap_reset(na, NR_TX, txq->ift_id, 0);
  1160. if (slot == NULL)
  1161. return (0);
  1162. for (int i = 0; i < ctx->ifc_softc_ctx.isc_ntxd[0]; i++) {
  1163. /*
  1164. * In netmap mode, set the map for the packet buffer.
  1165. * NOTE: Some drivers (not this one) also need to set
  1166. * the physical buffer address in the NIC ring.
  1167. * netmap_idx_n2k() maps a nic index, i, into the corresponding
  1168. * netmap slot index, si
  1169. */
  1170. int si = netmap_idx_n2k(na->tx_rings[txq->ift_id], i);
  1171. netmap_load_map(na, txq->ift_buf_tag, txq->ift_sds.ifsd_map[i],
  1172. NMB(na, slot + si));
  1173. }
  1174. return (1);
  1175. }
  1176. static int
  1177. iflib_netmap_rxq_init(if_ctx_t ctx, iflib_rxq_t rxq)
  1178. {
  1179. struct netmap_adapter *na = NA(ctx->ifc_ifp);
  1180. struct netmap_kring *kring;
  1181. struct netmap_slot *slot;
  1182. slot = netmap_reset(na, NR_RX, rxq->ifr_id, 0);
  1183. if (slot == NULL)
  1184. return (0);
  1185. kring = na->rx_rings[rxq->ifr_id];
  1186. netmap_fl_refill(rxq, kring, true);
  1187. return (1);
  1188. }
  1189. static void
  1190. iflib_netmap_timer(void *arg)
  1191. {
  1192. iflib_txq_t txq = arg;
  1193. if_ctx_t ctx = txq->ift_ctx;
  1194. /*
  1195. * Wake up the netmap application, to give it a chance to
  1196. * call txsync and reclaim more completed TX buffers.
  1197. */
  1198. netmap_tx_irq(ctx->ifc_ifp, txq->ift_id);
  1199. }
  1200. #define iflib_netmap_detach(ifp) netmap_detach(ifp)
  1201. #else
  1202. #define iflib_netmap_txq_init(ctx, txq) (0)
  1203. #define iflib_netmap_rxq_init(ctx, rxq) (0)
  1204. #define iflib_netmap_detach(ifp)
  1205. #define netmap_enable_all_rings(ifp)
  1206. #define netmap_disable_all_rings(ifp)
  1207. #define iflib_netmap_attach(ctx) (0)
  1208. #define netmap_rx_irq(ifp, qid, budget) (0)
  1209. #endif
  1210. #if defined(__i386__) || defined(__amd64__)
  1211. static __inline void
  1212. prefetch(void *x)
  1213. {
  1214. __asm volatile("prefetcht0 %0" :: "m" (*(unsigned long *)x));
  1215. }
  1216. static __inline void
  1217. prefetch2cachelines(void *x)
  1218. {
  1219. __asm volatile("prefetcht0 %0" :: "m" (*(unsigned long *)x));
  1220. #if (CACHE_LINE_SIZE < 128)
  1221. __asm volatile("prefetcht0 %0" :: "m" (*(((unsigned long *)x)+CACHE_LINE_SIZE/(sizeof(unsigned long)))));
  1222. #endif
  1223. }
  1224. #else
  1225. static __inline void
  1226. prefetch(void *x)
  1227. {
  1228. }
  1229. static __inline void
  1230. prefetch2cachelines(void *x)
  1231. {
  1232. }
  1233. #endif
  1234. static void
  1235. iru_init(if_rxd_update_t iru, iflib_rxq_t rxq, uint8_t flid)
  1236. {
  1237. iflib_fl_t fl;
  1238. fl = &rxq->ifr_fl[flid];
  1239. iru->iru_paddrs = fl->ifl_bus_addrs;
  1240. iru->iru_idxs = fl->ifl_rxd_idxs;
  1241. iru->iru_qsidx = rxq->ifr_id;
  1242. iru->iru_buf_size = fl->ifl_buf_size;
  1243. iru->iru_flidx = fl->ifl_id;
  1244. }
  1245. static void
  1246. _iflib_dmamap_cb(void *arg, bus_dma_segment_t *segs, int nseg, int err)
  1247. {
  1248. if (err)
  1249. return;
  1250. *(bus_addr_t *) arg = segs[0].ds_addr;
  1251. }
  1252. #define DMA_WIDTH_TO_BUS_LOWADDR(width) \
  1253. (((width) == 0) || (width) == flsll(BUS_SPACE_MAXADDR) ? \
  1254. BUS_SPACE_MAXADDR : (1ULL << (width)) - 1ULL)
  1255. int
  1256. iflib_dma_alloc_align(if_ctx_t ctx, int size, int align, iflib_dma_info_t dma, int mapflags)
  1257. {
  1258. int err;
  1259. device_t dev = ctx->ifc_dev;
  1260. bus_addr_t lowaddr;
  1261. lowaddr = DMA_WIDTH_TO_BUS_LOWADDR(ctx->ifc_softc_ctx.isc_dma_width);
  1262. err = bus_dma_tag_create(bus_get_dma_tag(dev), /* parent */
  1263. align, 0, /* alignment, bounds */
  1264. lowaddr, /* lowaddr */
  1265. BUS_SPACE_MAXADDR, /* highaddr */
  1266. NULL, NULL, /* filter, filterarg */
  1267. size, /* maxsize */
  1268. 1, /* nsegments */
  1269. size, /* maxsegsize */
  1270. BUS_DMA_ALLOCNOW, /* flags */
  1271. NULL, /* lockfunc */
  1272. NULL, /* lockarg */
  1273. &dma->idi_tag);
  1274. if (err) {
  1275. device_printf(dev,
  1276. "%s: bus_dma_tag_create failed: %d (size=%d, align=%d)\n",
  1277. __func__, err, size, align);
  1278. goto fail_0;
  1279. }
  1280. err = bus_dmamem_alloc(dma->idi_tag, (void**) &dma->idi_vaddr,
  1281. BUS_DMA_NOWAIT | BUS_DMA_COHERENT | BUS_DMA_ZERO, &dma->idi_map);
  1282. if (err) {
  1283. device_printf(dev,
  1284. "%s: bus_dmamem_alloc(%ju) failed: %d\n",
  1285. __func__, (uintmax_t)size, err);
  1286. goto fail_1;
  1287. }
  1288. dma->idi_paddr = IF_BAD_DMA;
  1289. err = bus_dmamap_load(dma->idi_tag, dma->idi_map, dma->idi_vaddr,
  1290. size, _iflib_dmamap_cb, &dma->idi_paddr, mapflags | BUS_DMA_NOWAIT);
  1291. if (err || dma->idi_paddr == IF_BAD_DMA) {
  1292. device_printf(dev,
  1293. "%s: bus_dmamap_load failed: %d\n",
  1294. __func__, err);
  1295. goto fail_2;
  1296. }
  1297. dma->idi_size = size;
  1298. return (0);
  1299. fail_2:
  1300. bus_dmamem_free(dma->idi_tag, dma->idi_vaddr, dma->idi_map);
  1301. fail_1:
  1302. bus_dma_tag_destroy(dma->idi_tag);
  1303. fail_0:
  1304. dma->idi_tag = NULL;
  1305. return (err);
  1306. }
  1307. int
  1308. iflib_dma_alloc(if_ctx_t ctx, int size, iflib_dma_info_t dma, int mapflags)
  1309. {
  1310. if_shared_ctx_t sctx = ctx->ifc_sctx;
  1311. KASSERT(sctx->isc_q_align != 0, ("alignment value not initialized"));
  1312. return (iflib_dma_alloc_align(ctx, size, sctx->isc_q_align, dma, mapflags));
  1313. }
  1314. int
  1315. iflib_dma_alloc_multi(if_ctx_t ctx, int *sizes, iflib_dma_info_t *dmalist, int mapflags, int count)
  1316. {
  1317. int i, err;
  1318. iflib_dma_info_t *dmaiter;
  1319. dmaiter = dmalist;
  1320. for (i = 0; i < count; i++, dmaiter++) {
  1321. if ((err = iflib_dma_alloc(ctx, sizes[i], *dmaiter, mapflags)) != 0)
  1322. break;
  1323. }
  1324. if (err)
  1325. iflib_dma_free_multi(dmalist, i);
  1326. return (err);
  1327. }
  1328. void
  1329. iflib_dma_free(iflib_dma_info_t dma)
  1330. {
  1331. if (dma->idi_tag == NULL)
  1332. return;
  1333. if (dma->idi_paddr != IF_BAD_DMA) {
  1334. bus_dmamap_sync(dma->idi_tag, dma->idi_map,
  1335. BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
  1336. bus_dmamap_unload(dma->idi_tag, dma->idi_map);
  1337. dma->idi_paddr = IF_BAD_DMA;
  1338. }
  1339. if (dma->idi_vaddr != NULL) {
  1340. bus_dmamem_free(dma->idi_tag, dma->idi_vaddr, dma->idi_map);
  1341. dma->idi_vaddr = NULL;
  1342. }
  1343. bus_dma_tag_destroy(dma->idi_tag);
  1344. dma->idi_tag = NULL;
  1345. }
  1346. void
  1347. iflib_dma_free_multi(iflib_dma_info_t *dmalist, int count)
  1348. {
  1349. int i;
  1350. iflib_dma_info_t *dmaiter = dmalist;
  1351. for (i = 0; i < count; i++, dmaiter++)
  1352. iflib_dma_free(*dmaiter);
  1353. }
  1354. static int
  1355. iflib_fast_intr(void *arg)
  1356. {
  1357. iflib_filter_info_t info = arg;
  1358. struct grouptask *gtask = info->ifi_task;
  1359. int result;
  1360. DBG_COUNTER_INC(fast_intrs);
  1361. if (info->ifi_filter != NULL) {
  1362. result = info->ifi_filter(info->ifi_filter_arg);
  1363. if ((result & FILTER_SCHEDULE_THREAD) == 0)
  1364. return (result);
  1365. }
  1366. GROUPTASK_ENQUEUE(gtask);
  1367. return (FILTER_HANDLED);
  1368. }
  1369. static int
  1370. iflib_fast_intr_rxtx(void *arg)
  1371. {
  1372. iflib_filter_info_t info = arg;
  1373. struct grouptask *gtask = info->ifi_task;
  1374. if_ctx_t ctx;
  1375. iflib_rxq_t rxq = (iflib_rxq_t)info->ifi_ctx;
  1376. iflib_txq_t txq;
  1377. void *sc;
  1378. int i, cidx, result;
  1379. qidx_t txqid;
  1380. bool intr_enable, intr_legacy;
  1381. DBG_COUNTER_INC(fast_intrs);
  1382. if (info->ifi_filter != NULL) {
  1383. result = info->ifi_filter(info->ifi_filter_arg);
  1384. if ((result & FILTER_SCHEDULE_THREAD) == 0)
  1385. return (result);
  1386. }
  1387. ctx = rxq->ifr_ctx;
  1388. sc = ctx->ifc_softc;
  1389. intr_enable = false;
  1390. intr_legacy = !!(ctx->ifc_flags & IFC_LEGACY);
  1391. MPASS(rxq->ifr_ntxqirq);
  1392. for (i = 0; i < rxq->ifr_ntxqirq; i++) {
  1393. txqid = rxq->ifr_txqid[i];
  1394. txq = &ctx->ifc_txqs[txqid];
  1395. bus_dmamap_sync(txq->ift_ifdi->idi_tag, txq->ift_ifdi->idi_map,
  1396. BUS_DMASYNC_POSTREAD);
  1397. if (!ctx->isc_txd_credits_update(sc, txqid, false)) {
  1398. if (intr_legacy)
  1399. intr_enable = true;
  1400. else
  1401. IFDI_TX_QUEUE_INTR_ENABLE(ctx, txqid);
  1402. continue;
  1403. }
  1404. GROUPTASK_ENQUEUE(&txq->ift_task);
  1405. }
  1406. if (ctx->ifc_sctx->isc_flags & IFLIB_HAS_RXCQ)
  1407. cidx = rxq->ifr_cq_cidx;
  1408. else
  1409. cidx = rxq->ifr_fl[0].ifl_cidx;
  1410. if (iflib_rxd_avail(ctx, rxq, cidx, 1))
  1411. GROUPTASK_ENQUEUE(gtask);
  1412. else {
  1413. if (intr_legacy)
  1414. intr_enable = true;
  1415. else
  1416. IFDI_RX_QUEUE_INTR_ENABLE(ctx, rxq->ifr_id);
  1417. DBG_COUNTER_INC(rx_intr_enables);
  1418. }
  1419. if (intr_enable)
  1420. IFDI_INTR_ENABLE(ctx);
  1421. return (FILTER_HANDLED);
  1422. }
  1423. static int
  1424. iflib_fast_intr_ctx(void *arg)
  1425. {
  1426. iflib_filter_info_t info = arg;
  1427. struct grouptask *gtask = info->ifi_task;
  1428. int result;
  1429. DBG_COUNTER_INC(fast_intrs);
  1430. if (info->ifi_filter != NULL) {
  1431. result = info->ifi_filter(info->ifi_filter_arg);
  1432. if ((result & FILTER_SCHEDULE_THREAD) == 0)
  1433. return (result);
  1434. }
  1435. if (gtask->gt_taskqueue != NULL)
  1436. GROUPTASK_ENQUEUE(gtask);
  1437. return (FILTER_HANDLED);
  1438. }
  1439. static int
  1440. _iflib_irq_alloc(if_ctx_t ctx, if_irq_t irq, int rid,
  1441. driver_filter_t filter, driver_intr_t handler, void *arg,
  1442. const char *name)
  1443. {
  1444. struct resource *res;
  1445. void *tag = NULL;
  1446. device_t dev = ctx->ifc_dev;
  1447. int flags, i, rc;
  1448. flags = RF_ACTIVE;
  1449. if (ctx->ifc_flags & IFC_LEGACY)
  1450. flags |= RF_SHAREABLE;
  1451. MPASS(rid < 512);
  1452. i = rid;
  1453. res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &i, flags);
  1454. if (res == NULL) {
  1455. device_printf(dev,
  1456. "failed to allocate IRQ for rid %d, name %s.\n", rid, name);
  1457. return (ENOMEM);
  1458. }
  1459. irq->ii_res = res;
  1460. KASSERT(filter == NULL || handler == NULL, ("filter and handler can't both be non-NULL"));
  1461. rc = bus_setup_intr(dev, res, INTR_MPSAFE | INTR_TYPE_NET,
  1462. filter, handler, arg, &tag);
  1463. if (rc != 0) {
  1464. device_printf(dev,
  1465. "failed to setup interrupt for rid %d, name %s: %d\n",
  1466. rid, name ? name : "unknown", rc);
  1467. return (rc);
  1468. } else if (name)
  1469. bus_describe_intr(dev, res, tag, "%s", name);
  1470. irq->ii_tag = tag;
  1471. return (0);
  1472. }
  1473. /*********************************************************************
  1474. *
  1475. * Allocate DMA resources for TX buffers as well as memory for the TX
  1476. * mbuf map. TX DMA maps (non-TSO/TSO) and TX mbuf map are kept in a
  1477. * iflib_sw_tx_desc_array structure, storing all the information that
  1478. * is needed to transmit a packet on the wire. This is called only
  1479. * once at attach, setup is done every reset.
  1480. *
  1481. **********************************************************************/
  1482. static int
  1483. iflib_txsd_alloc(iflib_txq_t txq)
  1484. {
  1485. if_ctx_t ctx = txq->ift_ctx;
  1486. if_shared_ctx_t sctx = ctx->ifc_sctx;
  1487. if_softc_ctx_t scctx = &ctx->ifc_softc_ctx;
  1488. device_t dev = ctx->ifc_dev;
  1489. bus_size_t tsomaxsize;
  1490. bus_addr_t lowaddr;
  1491. int err, nsegments, ntsosegments;
  1492. bool tso;
  1493. nsegments = scctx->isc_tx_nsegments;
  1494. ntsosegments = scctx->isc_tx_tso_segments_max;
  1495. tsomaxsize = scctx->isc_tx_tso_size_max;
  1496. if (if_getcapabilities(ctx->ifc_ifp) & IFCAP_VLAN_MTU)
  1497. tsomaxsize += sizeof(struct ether_vlan_header);
  1498. MPASS(scctx->isc_ntxd[0] > 0);
  1499. MPASS(scctx->isc_ntxd[txq->ift_br_offset] > 0);
  1500. MPASS(nsegments > 0);
  1501. if (if_getcapabilities(ctx->ifc_ifp) & IFCAP_TSO) {
  1502. MPASS(ntsosegments > 0);
  1503. MPASS(sctx->isc_tso_maxsize >= tsomaxsize);
  1504. }
  1505. lowaddr = DMA_WIDTH_TO_BUS_LOWADDR(scctx->isc_dma_width);
  1506. /*
  1507. * Set up DMA tags for TX buffers.
  1508. */
  1509. if ((err = bus_dma_tag_create(bus_get_dma_tag(dev),
  1510. 1, 0, /* alignment, bounds */
  1511. lowaddr, /* lowaddr */
  1512. BUS_SPACE_MAXADDR, /* highaddr */
  1513. NULL, NULL, /* filter, filterarg */
  1514. sctx->isc_tx_maxsize, /* maxsize */
  1515. nsegments, /* nsegments */
  1516. sctx->isc_tx_maxsegsize, /* maxsegsize */
  1517. 0, /* flags */
  1518. NULL, /* lockfunc */
  1519. NULL, /* lockfuncarg */
  1520. &txq->ift_buf_tag))) {
  1521. device_printf(dev,"Unable to allocate TX DMA tag: %d\n", err);
  1522. device_printf(dev,"maxsize: %ju nsegments: %d maxsegsize: %ju\n",
  1523. (uintmax_t)sctx->isc_tx_maxsize, nsegments, (uintmax_t)sctx->isc_tx_maxsegsize);
  1524. goto fail;
  1525. }
  1526. tso = (if_getcapabilities(ctx->ifc_ifp) & IFCAP_TSO) != 0;
  1527. if (tso && (err = bus_dma_tag_create(bus_get_dma_tag(dev),
  1528. 1, 0, /* alignment, bounds */
  1529. lowaddr, /* lowaddr */
  1530. BUS_SPACE_MAXADDR, /* highaddr */
  1531. NULL, NULL, /* filter, filterarg */
  1532. tsomaxsize, /* maxsize */
  1533. ntsosegments, /* nsegments */
  1534. sctx->isc_tso_maxsegsize,/* maxsegsize */
  1535. 0, /* flags */
  1536. NULL, /* lockfunc */
  1537. NULL, /* lockfuncarg */
  1538. &txq->ift_tso_buf_tag))) {
  1539. device_printf(dev, "Unable to allocate TSO TX DMA tag: %d\n",
  1540. err);
  1541. goto fail;
  1542. }
  1543. /* Allocate memory for the TX mbuf map. */
  1544. if (!(txq->ift_sds.ifsd_m =
  1545. (struct mbuf **) malloc(sizeof(struct mbuf *) *
  1546. scctx->isc_ntxd[txq->ift_br_offset], M_IFLIB, M_NOWAIT | M_ZERO))) {
  1547. device_printf(dev, "Unable to allocate TX mbuf map memory\n");
  1548. err = ENOMEM;
  1549. goto fail;
  1550. }
  1551. /*
  1552. * Create the DMA maps for TX buffers.
  1553. */
  1554. if ((txq->ift_sds.ifsd_map = (bus_dmamap_t *)malloc(
  1555. sizeof(bus_dmamap_t) * scctx->isc_ntxd[txq->ift_br_offset],
  1556. M_IFLIB, M_NOWAIT | M_ZERO)) == NULL) {
  1557. device_printf(dev,
  1558. "Unable to allocate TX buffer DMA map memory\n");
  1559. err = ENOMEM;
  1560. goto fail;
  1561. }
  1562. if (tso && (txq->ift_sds.ifsd_tso_map = (bus_dmamap_t *)malloc(
  1563. sizeof(bus_dmamap_t) * scctx->isc_ntxd[txq->ift_br_offset],
  1564. M_IFLIB, M_NOWAIT | M_ZERO)) == NULL) {
  1565. device_printf(dev,
  1566. "Unable to allocate TSO TX buffer map memory\n");
  1567. err = ENOMEM;
  1568. goto fail;
  1569. }
  1570. for (int i = 0; i < scctx->isc_ntxd[txq->ift_br_offset]; i++) {
  1571. err = bus_dmamap_create(txq->ift_buf_tag, 0,
  1572. &txq->ift_sds.ifsd_map[i]);
  1573. if (err != 0) {
  1574. device_printf(dev, "Unable to create TX DMA map\n");
  1575. goto fail;
  1576. }
  1577. if (!tso)
  1578. continue;
  1579. err = bus_dmamap_create(txq->ift_tso_buf_tag, 0,
  1580. &txq->ift_sds.ifsd_tso_map[i]);
  1581. if (err != 0) {
  1582. device_printf(dev, "Unable to create TSO TX DMA map\n");
  1583. goto fail;
  1584. }
  1585. }
  1586. return (0);
  1587. fail:
  1588. /* We free all, it handles case where we are in the middle */
  1589. iflib_tx_structures_free(ctx);
  1590. return (err);
  1591. }
  1592. static void
  1593. iflib_txsd_destroy(if_ctx_t ctx, iflib_txq_t txq, int i)
  1594. {
  1595. bus_dmamap_t map;
  1596. if (txq->ift_sds.ifsd_map != NULL) {
  1597. map = txq->ift_sds.ifsd_map[i];
  1598. bus_dmamap_sync(txq->ift_buf_tag, map, BUS_DMASYNC_POSTWRITE);
  1599. bus_dmamap_unload(txq->ift_buf_tag, map);
  1600. bus_dmamap_destroy(txq->ift_buf_tag, map);
  1601. txq->ift_sds.ifsd_map[i] = NULL;
  1602. }
  1603. if (txq->ift_sds.ifsd_tso_map != NULL) {
  1604. map = txq->ift_sds.ifsd_tso_map[i];
  1605. bus_dmamap_sync(txq->ift_tso_buf_tag, map,
  1606. BUS_DMASYNC_POSTWRITE);
  1607. bus_dmamap_unload(txq->ift_tso_buf_tag, map);
  1608. bus_dmamap_destroy(txq->ift_tso_buf_tag, map);
  1609. txq->ift_sds.ifsd_tso_map[i] = NULL;
  1610. }
  1611. }
  1612. static void
  1613. iflib_txq_destroy(iflib_txq_t txq)
  1614. {
  1615. if_ctx_t ctx = txq->ift_ctx;
  1616. for (int i = 0; i < txq->ift_size; i++)
  1617. iflib_txsd_destroy(ctx, txq, i);
  1618. if (txq->ift_br != NULL) {
  1619. ifmp_ring_free(txq->ift_br);
  1620. txq->ift_br = NULL;
  1621. }
  1622. mtx_destroy(&txq->ift_mtx);
  1623. if (txq->ift_sds.ifsd_map != NULL) {
  1624. free(txq->ift_sds.ifsd_map, M_IFLIB);
  1625. txq->ift_sds.ifsd_map = NULL;
  1626. }
  1627. if (txq->ift_sds.ifsd_tso_map != NULL) {
  1628. free(txq->ift_sds.ifsd_tso_map, M_IFLIB);
  1629. txq->ift_sds.ifsd_tso_map = NULL;
  1630. }
  1631. if (txq->ift_sds.ifsd_m != NULL) {
  1632. free(txq->ift_sds.ifsd_m, M_IFLIB);
  1633. txq->ift_sds.ifsd_m = NULL;
  1634. }
  1635. if (txq->ift_buf_tag != NULL) {
  1636. bus_dma_tag_destroy(txq->ift_buf_tag);
  1637. txq->ift_buf_tag = NULL;
  1638. }
  1639. if (txq->ift_tso_buf_tag != NULL) {
  1640. bus_dma_tag_destroy(txq->ift_tso_buf_tag);
  1641. txq->ift_tso_buf_tag = NULL;
  1642. }
  1643. if (txq->ift_ifdi != NULL) {
  1644. free(txq->ift_ifdi, M_IFLIB);
  1645. }
  1646. }
  1647. static void
  1648. iflib_txsd_free(if_ctx_t ctx, iflib_txq_t txq, int i)
  1649. {
  1650. struct mbuf **mp;
  1651. mp = &txq->ift_sds.ifsd_m[i];
  1652. if (*mp == NULL)
  1653. return;
  1654. if (txq->ift_sds.ifsd_map != NULL) {
  1655. bus_dmamap_sync(txq->ift_buf_tag,
  1656. txq->ift_sds.ifsd_map[i], BUS_DMASYNC_POSTWRITE);
  1657. bus_dmamap_unload(txq->ift_buf_tag, txq->ift_sds.ifsd_map[i]);
  1658. }
  1659. if (txq->ift_sds.ifsd_tso_map != NULL) {
  1660. bus_dmamap_sync(txq->ift_tso_buf_tag,
  1661. txq->ift_sds.ifsd_tso_map[i], BUS_DMASYNC_POSTWRITE);
  1662. bus_dmamap_unload(txq->ift_tso_buf_tag,
  1663. txq->ift_sds.ifsd_tso_map[i]);
  1664. }
  1665. m_freem(*mp);
  1666. DBG_COUNTER_INC(tx_frees);
  1667. *mp = NULL;
  1668. }
  1669. static int
  1670. iflib_txq_setup(iflib_txq_t txq)
  1671. {
  1672. if_ctx_t ctx = txq->ift_ctx;
  1673. if_softc_ctx_t scctx = &ctx->ifc_softc_ctx;
  1674. if_shared_ctx_t sctx = ctx->ifc_sctx;
  1675. iflib_dma_info_t di;
  1676. int i;
  1677. /* Set number of descriptors available */
  1678. txq->ift_qstatus = IFLIB_QUEUE_IDLE;
  1679. /* XXX make configurable */
  1680. txq->ift_update_freq = IFLIB_DEFAULT_TX_UPDATE_FREQ;
  1681. /* Reset indices */
  1682. txq->ift_cidx_processed = 0;
  1683. txq->ift_pidx = txq->ift_cidx = txq->ift_npending = 0;
  1684. txq->ift_size = scctx->isc_ntxd[txq->ift_br_offset];
  1685. for (i = 0, di = txq->ift_ifdi; i < sctx->isc_ntxqs; i++, di++)
  1686. bzero((void *)di->idi_vaddr, di->idi_size);
  1687. IFDI_TXQ_SETUP(ctx, txq->ift_id);
  1688. for (i = 0, di = txq->ift_ifdi; i < sctx->isc_ntxqs; i++, di++)
  1689. bus_dmamap_sync(di->idi_tag, di->idi_map,
  1690. BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
  1691. return (0);
  1692. }
  1693. /*********************************************************************
  1694. *
  1695. * Allocate DMA resources for RX buffers as well as memory for the RX
  1696. * mbuf map, direct RX cluster pointer map and RX cluster bus address
  1697. * map. RX DMA map, RX mbuf map, direct RX cluster pointer map and
  1698. * RX cluster map are kept in a iflib_sw_rx_desc_array structure.
  1699. * Since we use use one entry in iflib_sw_rx_desc_array per received
  1700. * packet, the maximum number of entries we'll need is equal to the
  1701. * number of hardware receive descriptors that we've allocated.
  1702. *
  1703. **********************************************************************/
  1704. static int
  1705. iflib_rxsd_alloc(iflib_rxq_t rxq)
  1706. {
  1707. if_ctx_t ctx = rxq->ifr_ctx;
  1708. if_shared_ctx_t sctx = ctx->ifc_sctx;
  1709. if_softc_ctx_t scctx = &ctx->ifc_softc_ctx;
  1710. device_t dev = ctx->ifc_dev;
  1711. iflib_fl_t fl;
  1712. bus_addr_t lowaddr;
  1713. int err;
  1714. MPASS(scctx->isc_nrxd[0] > 0);
  1715. MPASS(scctx->isc_nrxd[rxq->ifr_fl_offset] > 0);
  1716. lowaddr = DMA_WIDTH_TO_BUS_LOWADDR(scctx->isc_dma_width);
  1717. fl = rxq->ifr_fl;
  1718. for (int i = 0; i < rxq->ifr_nfl; i++, fl++) {
  1719. fl->ifl_size = scctx->isc_nrxd[rxq->ifr_fl_offset]; /* this isn't necessarily the same */
  1720. /* Set up DMA tag for RX buffers. */
  1721. err = bus_dma_tag_create(bus_get_dma_tag(dev), /* parent */
  1722. 1, 0, /* alignment, bounds */
  1723. lowaddr, /* lowaddr */
  1724. BUS_SPACE_MAXADDR, /* highaddr */
  1725. NULL, NULL, /* filter, filterarg */
  1726. sctx->isc_rx_maxsize, /* maxsize */
  1727. sctx->isc_rx_nsegments, /* nsegments */
  1728. sctx->isc_rx_maxsegsize, /* maxsegsize */
  1729. 0, /* flags */
  1730. NULL, /* lockfunc */
  1731. NULL, /* lockarg */
  1732. &fl->ifl_buf_tag);
  1733. if (err) {
  1734. device_printf(dev,
  1735. "Unable to allocate RX DMA tag: %d\n", err);
  1736. goto fail;
  1737. }
  1738. /* Allocate memory for the RX mbuf map. */
  1739. if (!(fl->ifl_sds.ifsd_m =
  1740. (struct mbuf **) malloc(sizeof(struct mbuf *) *
  1741. scctx->isc_nrxd[rxq->ifr_fl_offset], M_IFLIB, M_NOWAIT | M_ZERO))) {
  1742. device_printf(dev,
  1743. "Unable to allocate RX mbuf map memory\n");
  1744. err = ENOMEM;
  1745. goto fail;
  1746. }
  1747. /* Allocate memory for the direct RX cluster pointer map. */
  1748. if (!(fl->ifl_sds.ifsd_cl =
  1749. (caddr_t *) malloc(sizeof(caddr_t) *
  1750. scctx->isc_nrxd[rxq->ifr_fl_offset], M_IFLIB, M_NOWAIT | M_ZERO))) {
  1751. device_printf(dev,
  1752. "Unable to allocate RX cluster map memory\n");
  1753. err = ENOMEM;
  1754. goto fail;
  1755. }
  1756. /* Allocate memory for the RX cluster bus address map. */
  1757. if (!(fl->ifl_sds.ifsd_ba =
  1758. (bus_addr_t *) malloc(sizeof(bus_addr_t) *
  1759. scctx->isc_nrxd[rxq->ifr_fl_offset], M_IFLIB, M_NOWAIT | M_ZERO))) {
  1760. device_printf(dev,
  1761. "Unable to allocate RX bus address map memory\n");
  1762. err = ENOMEM;
  1763. goto fail;
  1764. }
  1765. /*
  1766. * Create the DMA maps for RX buffers.
  1767. */
  1768. if (!(fl->ifl_sds.ifsd_map =
  1769. (bus_dmamap_t *) malloc(sizeof(bus_dmamap_t) * scctx->isc_nrxd[rxq->ifr_fl_offset], M_IFLIB, M_NOWAIT | M_ZERO))) {
  1770. device_printf(dev,
  1771. "Unable to allocate RX buffer DMA map memory\n");
  1772. err = ENOMEM;
  1773. goto fail;
  1774. }
  1775. for (int i = 0; i < scctx->isc_nrxd[rxq->ifr_fl_offset]; i++) {
  1776. err = bus_dmamap_create(fl->ifl_buf_tag, 0,
  1777. &fl->ifl_sds.ifsd_map[i]);
  1778. if (err != 0) {
  1779. device_printf(dev, "Unable to create RX buffer DMA map\n");
  1780. goto fail;
  1781. }
  1782. }
  1783. }
  1784. return (0);
  1785. fail:
  1786. iflib_rx_structures_free(ctx);
  1787. return (err);
  1788. }
  1789. /*
  1790. * Internal service routines
  1791. */
  1792. struct rxq_refill_cb_arg {
  1793. int error;
  1794. bus_dma_segment_t seg;
  1795. int nseg;
  1796. };
  1797. static void
  1798. _rxq_refill_cb(void *arg, bus_dma_segment_t *segs, int nseg, int error)
  1799. {
  1800. struct rxq_refill_cb_arg *cb_arg = arg;
  1801. cb_arg->error = error;
  1802. cb_arg->seg = segs[0];
  1803. cb_arg->nseg = nseg;
  1804. }
  1805. /**
  1806. * iflib_fl_refill - refill an rxq free-buffer list
  1807. * @ctx: the iflib context
  1808. * @fl: the free list to refill
  1809. * @count: the number of new buffers to allocate
  1810. *
  1811. * (Re)populate an rxq free-buffer list with up to @count new packet buffers.
  1812. * The caller must assure that @count does not exceed the queue's capacity
  1813. * minus one (since we always leave a descriptor unavailable).
  1814. */
  1815. static uint8_t
  1816. iflib_fl_refill(if_ctx_t ctx, iflib_fl_t fl, int count)
  1817. {
  1818. struct if_rxd_update iru;
  1819. struct rxq_refill_cb_arg cb_arg;
  1820. struct mbuf *m;
  1821. caddr_t cl, *sd_cl;
  1822. struct mbuf **sd_m;
  1823. bus_dmamap_t *sd_map;
  1824. bus_addr_t bus_addr, *sd_ba;
  1825. int err, frag_idx, i, idx, n, pidx;
  1826. qidx_t credits;
  1827. MPASS(count <= fl->ifl_size - fl->ifl_credits - 1);
  1828. sd_m = fl->ifl_sds.ifsd_m;
  1829. sd_map = fl->ifl_sds.ifsd_map;
  1830. sd_cl = fl->ifl_sds.ifsd_cl;
  1831. sd_ba = fl->ifl_sds.ifsd_ba;
  1832. pidx = fl->ifl_pidx;
  1833. idx = pidx;
  1834. frag_idx = fl->ifl_fragidx;
  1835. credits = fl->ifl_credits;
  1836. i = 0;
  1837. n = count;
  1838. MPASS(n > 0);
  1839. MPASS(credits + n <= fl->ifl_size);
  1840. if (pidx < fl->ifl_cidx)
  1841. MPASS(pidx + n <= fl->ifl_cidx);
  1842. if (pidx == fl->ifl_cidx && (credits < fl->ifl_size))
  1843. MPASS(fl->ifl_gen == 0);
  1844. if (pidx > fl->ifl_cidx)
  1845. MPASS(n <= fl->ifl_size - pidx + fl->ifl_cidx);
  1846. DBG_COUNTER_INC(fl_refills);
  1847. if (n > 8)
  1848. DBG_COUNTER_INC(fl_refills_large);
  1849. iru_init(&iru, fl->ifl_rxq, fl->ifl_id);
  1850. while (n-- > 0) {
  1851. /*
  1852. * We allocate an uninitialized mbuf + cluster, mbuf is
  1853. * initialized after rx.
  1854. *
  1855. * If the cluster is still set then we know a minimum sized
  1856. * packet was received
  1857. */
  1858. bit_ffc_at(fl->ifl_rx_bitmap, frag_idx, fl->ifl_size,
  1859. &frag_idx);
  1860. if (frag_idx < 0)
  1861. bit_ffc(fl->ifl_rx_bitmap, fl->ifl_size, &frag_idx);
  1862. MPASS(frag_idx >= 0);
  1863. if ((cl = sd_cl[frag_idx]) == NULL) {
  1864. cl = uma_zalloc(fl->ifl_zone, M_NOWAIT);
  1865. if (__predict_false(cl == NULL))
  1866. break;
  1867. cb_arg.error = 0;
  1868. MPASS(sd_map != NULL);
  1869. err = bus_dmamap_load(fl->ifl_buf_tag, sd_map[frag_idx],
  1870. cl, fl->ifl_buf_size, _rxq_refill_cb, &cb_arg,
  1871. BUS_DMA_NOWAIT);
  1872. if (__predict_false(err != 0 || cb_arg.error)) {
  1873. uma_zfree(fl->ifl_zone, cl);
  1874. break;
  1875. }
  1876. sd_ba[frag_idx] = bus_addr = cb_arg.seg.ds_addr;
  1877. sd_cl[frag_idx] = cl;
  1878. #if MEMORY_LOGGING
  1879. fl->ifl_cl_enqueued++;
  1880. #endif
  1881. } else {
  1882. bus_addr = sd_ba[frag_idx];
  1883. }
  1884. bus_dmamap_sync(fl->ifl_buf_tag, sd_map[frag_idx],
  1885. BUS_DMASYNC_PREREAD);
  1886. if (sd_m[frag_idx] == NULL) {
  1887. m = m_gethdr_raw(M_NOWAIT, 0);
  1888. if (__predict_false(m == NULL))
  1889. break;
  1890. sd_m[frag_idx] = m;
  1891. }
  1892. bit_set(fl->ifl_rx_bitmap, frag_idx);
  1893. #if MEMORY_LOGGING
  1894. fl->ifl_m_enqueued++;
  1895. #endif
  1896. DBG_COUNTER_INC(rx_allocs);
  1897. fl->ifl_rxd_idxs[i] = frag_idx;
  1898. fl->ifl_bus_addrs[i] = bus_addr;
  1899. credits++;
  1900. i++;
  1901. MPASS(credits <= fl->ifl_size);
  1902. if (++idx == fl->ifl_size) {
  1903. #ifdef INVARIANTS
  1904. fl->ifl_gen = 1;
  1905. #endif
  1906. idx = 0;
  1907. }
  1908. if (n == 0 || i == IFLIB_MAX_RX_REFRESH) {
  1909. iru.iru_pidx = pidx;
  1910. iru.iru_count = i;
  1911. ctx->isc_rxd_refill(ctx->ifc_softc, &iru);
  1912. fl->ifl_pidx = idx;
  1913. fl->ifl_credits = credits;
  1914. pidx = idx;
  1915. i = 0;
  1916. }
  1917. }
  1918. if (n < count - 1) {
  1919. if (i != 0) {
  1920. iru.iru_pidx = pidx;
  1921. iru.iru_count = i;
  1922. ctx->isc_rxd_refill(ctx->ifc_softc, &iru);
  1923. fl->ifl_pidx = idx;
  1924. fl->ifl_credits = credits;
  1925. }
  1926. DBG_COUNTER_INC(rxd_flush);
  1927. bus_dmamap_sync(fl->ifl_ifdi->idi_tag, fl->ifl_ifdi->idi_map,
  1928. BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
  1929. ctx->isc_rxd_flush(ctx->ifc_softc, fl->ifl_rxq->ifr_id,
  1930. fl->ifl_id, fl->ifl_pidx);
  1931. if (__predict_true(bit_test(fl->ifl_rx_bitmap, frag_idx))) {
  1932. fl->ifl_fragidx = frag_idx + 1;
  1933. if (fl->ifl_fragidx == fl->ifl_size)
  1934. fl->ifl_fragidx = 0;
  1935. } else {
  1936. fl->ifl_fragidx = frag_idx;
  1937. }
  1938. }
  1939. return (n == -1 ? 0 : IFLIB_RXEOF_EMPTY);
  1940. }
  1941. static inline uint8_t
  1942. iflib_fl_refill_all(if_ctx_t ctx, iflib_fl_t fl)
  1943. {
  1944. /*
  1945. * We leave an unused descriptor to avoid pidx to catch up with cidx.
  1946. * This is important as it confuses most NICs. For instance,
  1947. * Intel NICs have (per receive ring) RDH and RDT registers, where
  1948. * RDH points to the next receive descriptor to be used by the NIC,
  1949. * and RDT for the next receive descriptor to be published by the
  1950. * driver to the NIC (RDT - 1 is thus the last valid one).
  1951. * The condition RDH == RDT means no descriptors are available to
  1952. * the NIC, and thus it would be ambiguous if it also meant that
  1953. * all the descriptors are available to the NIC.
  1954. */
  1955. int32_t reclaimable = fl->ifl_size - fl->ifl_credits - 1;
  1956. #ifdef INVARIANTS
  1957. int32_t delta = fl->ifl_size - get_inuse(fl->ifl_size, fl->ifl_cidx, fl->ifl_pidx, fl->ifl_gen) - 1;
  1958. #endif
  1959. MPASS(fl->ifl_credits <= fl->ifl_size);
  1960. MPASS(reclaimable == delta);
  1961. if (reclaimable > 0)
  1962. return (iflib_fl_refill(ctx, fl, reclaimable));
  1963. return (0);
  1964. }
  1965. uint8_t
  1966. iflib_in_detach(if_ctx_t ctx)
  1967. {
  1968. bool in_detach;
  1969. STATE_LOCK(ctx);
  1970. in_detach = !!(ctx->ifc_flags & IFC_IN_DETACH);
  1971. STATE_UNLOCK(ctx);
  1972. return (in_detach);
  1973. }
  1974. static void
  1975. iflib_fl_bufs_free(iflib_fl_t fl)
  1976. {
  1977. iflib_dma_info_t idi = fl->ifl_ifdi;
  1978. bus_dmamap_t sd_map;
  1979. uint32_t i;
  1980. for (i = 0; i < fl->ifl_size; i++) {
  1981. struct mbuf **sd_m = &fl->ifl_sds.ifsd_m[i];
  1982. caddr_t *sd_cl = &fl->ifl_sds.ifsd_cl[i];
  1983. if (*sd_cl != NULL) {
  1984. sd_map = fl->ifl_sds.ifsd_map[i];
  1985. bus_dmamap_sync(fl->ifl_buf_tag, sd_map,
  1986. BUS_DMASYNC_POSTREAD);
  1987. bus_dmamap_unload(fl->ifl_buf_tag, sd_map);
  1988. uma_zfree(fl->ifl_zone, *sd_cl);
  1989. *sd_cl = NULL;
  1990. if (*sd_m != NULL) {
  1991. m_init(*sd_m, M_NOWAIT, MT_DATA, 0);
  1992. m_free_raw(*sd_m);
  1993. *sd_m = NULL;
  1994. }
  1995. } else {
  1996. MPASS(*sd_m == NULL);
  1997. }
  1998. #if MEMORY_LOGGING
  1999. fl->ifl_m_dequeued++;
  2000. fl->ifl_cl_dequeued++;
  2001. #endif
  2002. }
  2003. #ifdef INVARIANTS
  2004. for (i = 0; i < fl->ifl_size; i++) {
  2005. MPASS(fl->ifl_sds.ifsd_cl[i] == NULL);
  2006. MPASS(fl->ifl_sds.ifsd_m[i] == NULL);
  2007. }
  2008. #endif
  2009. /*
  2010. * Reset free list values
  2011. */
  2012. fl->ifl_credits = fl->ifl_cidx = fl->ifl_pidx = fl->ifl_gen = fl->ifl_fragidx = 0;
  2013. bzero(idi->idi_vaddr, idi->idi_size);
  2014. }
  2015. /*********************************************************************
  2016. *
  2017. * Initialize a free list and its buffers.
  2018. *
  2019. **********************************************************************/
  2020. static int
  2021. iflib_fl_setup(iflib_fl_t fl)
  2022. {
  2023. iflib_rxq_t rxq = fl->ifl_rxq;
  2024. if_ctx_t ctx = rxq->ifr_ctx;
  2025. if_softc_ctx_t scctx = &ctx->ifc_softc_ctx;
  2026. int qidx;
  2027. bit_nclear(fl->ifl_rx_bitmap, 0, fl->ifl_size - 1);
  2028. /*
  2029. ** Free current RX buffer structs and their mbufs
  2030. */
  2031. iflib_fl_bufs_free(fl);
  2032. /* Now replenish the mbufs */
  2033. MPASS(fl->ifl_credits == 0);
  2034. qidx = rxq->ifr_fl_offset + fl->ifl_id;
  2035. if (scctx->isc_rxd_buf_size[qidx] != 0)
  2036. fl->ifl_buf_size = scctx->isc_rxd_buf_size[qidx];
  2037. else
  2038. fl->ifl_buf_size = ctx->ifc_rx_mbuf_sz;
  2039. /*
  2040. * ifl_buf_size may be a driver-supplied value, so pull it up
  2041. * to the selected mbuf size.
  2042. */
  2043. fl->ifl_buf_size = iflib_get_mbuf_size_for(fl->ifl_buf_size);
  2044. if (fl->ifl_buf_size > ctx->ifc_max_fl_buf_size)
  2045. ctx->ifc_max_fl_buf_size = fl->ifl_buf_size;
  2046. fl->ifl_cltype = m_gettype(fl->ifl_buf_size);
  2047. fl->ifl_zone = m_getzone(fl->ifl_buf_size);
  2048. /*
  2049. * Avoid pre-allocating zillions of clusters to an idle card
  2050. * potentially speeding up attach. In any case make sure
  2051. * to leave a descriptor unavailable. See the comment in
  2052. * iflib_fl_refill_all().
  2053. */
  2054. MPASS(fl->ifl_size > 0);
  2055. (void)iflib_fl_refill(ctx, fl, min(128, fl->ifl_size - 1));
  2056. if (min(128, fl->ifl_size - 1) != fl->ifl_credits)
  2057. return (ENOBUFS);
  2058. /*
  2059. * handle failure
  2060. */
  2061. MPASS(rxq != NULL);
  2062. MPASS(fl->ifl_ifdi != NULL);
  2063. bus_dmamap_sync(fl->ifl_ifdi->idi_tag, fl->ifl_ifdi->idi_map,
  2064. BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
  2065. return (0);
  2066. }
  2067. /*********************************************************************
  2068. *
  2069. * Free receive ring data structures
  2070. *
  2071. **********************************************************************/
  2072. static void
  2073. iflib_rx_sds_free(iflib_rxq_t rxq)
  2074. {
  2075. iflib_fl_t fl;
  2076. int i, j;
  2077. if (rxq->ifr_fl != NULL) {
  2078. for (i = 0; i < rxq->ifr_nfl; i++) {
  2079. fl = &rxq->ifr_fl[i];
  2080. if (fl->ifl_buf_tag != NULL) {
  2081. if (fl->ifl_sds.ifsd_map != NULL) {
  2082. for (j = 0; j < fl->ifl_size; j++) {
  2083. bus_dmamap_sync(
  2084. fl->ifl_buf_tag,
  2085. fl->ifl_sds.ifsd_map[j],
  2086. BUS_DMASYNC_POSTREAD);
  2087. bus_dmamap_unload(
  2088. fl->ifl_buf_tag,
  2089. fl->ifl_sds.ifsd_map[j]);
  2090. bus_dmamap_destroy(
  2091. fl->ifl_buf_tag,
  2092. fl->ifl_sds.ifsd_map[j]);
  2093. }
  2094. }
  2095. bus_dma_tag_destroy(fl->ifl_buf_tag);
  2096. fl->ifl_buf_tag = NULL;
  2097. }
  2098. free(fl->ifl_sds.ifsd_m, M_IFLIB);
  2099. free(fl->ifl_sds.ifsd_cl, M_IFLIB);
  2100. free(fl->ifl_sds.ifsd_ba, M_IFLIB);
  2101. free(fl->ifl_sds.ifsd_map, M_IFLIB);
  2102. free(fl->ifl_rx_bitmap, M_IFLIB);
  2103. fl->ifl_sds.ifsd_m = NULL;
  2104. fl->ifl_sds.ifsd_cl = NULL;
  2105. fl->ifl_sds.ifsd_ba = NULL;
  2106. fl->ifl_sds.ifsd_map = NULL;
  2107. fl->ifl_rx_bitmap = NULL;
  2108. }
  2109. free(rxq->ifr_fl, M_IFLIB);
  2110. rxq->ifr_fl = NULL;
  2111. free(rxq->ifr_ifdi, M_IFLIB);
  2112. rxq->ifr_ifdi = NULL;
  2113. rxq->ifr_cq_cidx = 0;
  2114. }
  2115. }
  2116. /*
  2117. * Timer routine
  2118. */
  2119. static void
  2120. iflib_timer(void *arg)
  2121. {
  2122. iflib_txq_t txq = arg;
  2123. if_ctx_t ctx = txq->ift_ctx;
  2124. if_softc_ctx_t sctx = &ctx->ifc_softc_ctx;
  2125. uint64_t this_tick = ticks;
  2126. if (!(if_getdrvflags(ctx->ifc_ifp) & IFF_DRV_RUNNING))
  2127. return;
  2128. /*
  2129. ** Check on the state of the TX queue(s), this
  2130. ** can be done without the lock because its RO
  2131. ** and the HUNG state will be static if set.
  2132. */
  2133. if (this_tick - txq->ift_last_timer_tick >= iflib_timer_default) {
  2134. txq->ift_last_timer_tick = this_tick;
  2135. IFDI_TIMER(ctx, txq->ift_id);
  2136. if ((txq->ift_qstatus == IFLIB_QUEUE_HUNG) &&
  2137. ((txq->ift_cleaned_prev == txq->ift_cleaned) ||
  2138. (sctx->isc_pause_frames == 0)))
  2139. goto hung;
  2140. if (txq->ift_qstatus != IFLIB_QUEUE_IDLE &&
  2141. ifmp_ring_is_stalled(txq->ift_br)) {
  2142. KASSERT(ctx->ifc_link_state == LINK_STATE_UP,
  2143. ("queue can't be marked as hung if interface is down"));
  2144. txq->ift_qstatus = IFLIB_QUEUE_HUNG;
  2145. }
  2146. txq->ift_cleaned_prev = txq->ift_cleaned;
  2147. }
  2148. /* handle any laggards */
  2149. if (txq->ift_db_pending)
  2150. GROUPTASK_ENQUEUE(&txq->ift_task);
  2151. sctx->isc_pause_frames = 0;
  2152. if (if_getdrvflags(ctx->ifc_ifp) & IFF_DRV_RUNNING)
  2153. callout_reset_on(&txq->ift_timer, iflib_timer_default, iflib_timer,
  2154. txq, txq->ift_timer.c_cpu);
  2155. return;
  2156. hung:
  2157. device_printf(ctx->ifc_dev,
  2158. "Watchdog timeout (TX: %d desc avail: %d pidx: %d) -- resetting\n",
  2159. txq->ift_id, TXQ_AVAIL(txq), txq->ift_pidx);
  2160. STATE_LOCK(ctx);
  2161. if_setdrvflagbits(ctx->ifc_ifp, IFF_DRV_OACTIVE, IFF_DRV_RUNNING);
  2162. ctx->ifc_flags |= (IFC_DO_WATCHDOG|IFC_DO_RESET);
  2163. iflib_admin_intr_deferred(ctx);
  2164. STATE_UNLOCK(ctx);
  2165. }
  2166. static uint16_t
  2167. iflib_get_mbuf_size_for(unsigned int size)
  2168. {
  2169. if (size <= MCLBYTES)
  2170. return (MCLBYTES);
  2171. else
  2172. return (MJUMPAGESIZE);
  2173. }
  2174. static void
  2175. iflib_calc_rx_mbuf_sz(if_ctx_t ctx)
  2176. {
  2177. if_softc_ctx_t sctx = &ctx->ifc_softc_ctx;
  2178. /*
  2179. * XXX don't set the max_frame_size to larger
  2180. * than the hardware can handle
  2181. */
  2182. ctx->ifc_rx_mbuf_sz =
  2183. iflib_get_mbuf_size_for(sctx->isc_max_frame_size);
  2184. }
  2185. uint32_t
  2186. iflib_get_rx_mbuf_sz(if_ctx_t ctx)
  2187. {
  2188. return (ctx->ifc_rx_mbuf_sz);
  2189. }
  2190. static void
  2191. iflib_init_locked(if_ctx_t ctx)
  2192. {
  2193. if_softc_ctx_t scctx = &ctx->ifc_softc_ctx;
  2194. if_t ifp = ctx->ifc_ifp;
  2195. iflib_fl_t fl;
  2196. iflib_txq_t txq;
  2197. iflib_rxq_t rxq;
  2198. int i, j, tx_ip_csum_flags, tx_ip6_csum_flags;
  2199. if_setdrvflagbits(ifp, IFF_DRV_OACTIVE, IFF_DRV_RUNNING);
  2200. IFDI_INTR_DISABLE(ctx);
  2201. /*
  2202. * See iflib_stop(). Useful in case iflib_init_locked() is
  2203. * called without first calling iflib_stop().
  2204. */
  2205. netmap_disable_all_rings(ifp);
  2206. tx_ip_csum_flags = scctx->isc_tx_csum_flags & (CSUM_IP | CSUM_TCP | CSUM_UDP | CSUM_SCTP);
  2207. tx_ip6_csum_flags = scctx->isc_tx_csum_flags & (CSUM_IP6_TCP | CSUM_IP6_UDP | CSUM_IP6_SCTP);
  2208. /* Set hardware offload abilities */
  2209. if_clearhwassist(ifp);
  2210. if (if_getcapenable(ifp) & IFCAP_TXCSUM)
  2211. if_sethwassistbits(ifp, tx_ip_csum_flags, 0);
  2212. if (if_getcapenable(ifp) & IFCAP_TXCSUM_IPV6)
  2213. if_sethwassistbits(ifp, tx_ip6_csum_flags, 0);
  2214. if (if_getcapenable(ifp) & IFCAP_TSO4)
  2215. if_sethwassistbits(ifp, CSUM_IP_TSO, 0);
  2216. if (if_getcapenable(ifp) & IFCAP_TSO6)
  2217. if_sethwassistbits(ifp, CSUM_IP6_TSO, 0);
  2218. for (i = 0, txq = ctx->ifc_txqs; i < scctx->isc_ntxqsets; i++, txq++) {
  2219. CALLOUT_LOCK(txq);
  2220. callout_stop(&txq->ift_timer);
  2221. #ifdef DEV_NETMAP
  2222. callout_stop(&txq->ift_netmap_timer);
  2223. #endif /* DEV_NETMAP */
  2224. CALLOUT_UNLOCK(txq);
  2225. (void)iflib_netmap_txq_init(ctx, txq);
  2226. }
  2227. /*
  2228. * Calculate a suitable Rx mbuf size prior to calling IFDI_INIT, so
  2229. * that drivers can use the value when setting up the hardware receive
  2230. * buffers.
  2231. */
  2232. iflib_calc_rx_mbuf_sz(ctx);
  2233. #ifdef INVARIANTS
  2234. i = if_getdrvflags(ifp);
  2235. #endif
  2236. IFDI_INIT(ctx);
  2237. MPASS(if_getdrvflags(ifp) == i);
  2238. for (i = 0, rxq = ctx->ifc_rxqs; i < scctx->isc_nrxqsets; i++, rxq++) {
  2239. if (iflib_netmap_rxq_init(ctx, rxq) > 0) {
  2240. /* This rxq is in netmap mode. Skip normal init. */
  2241. continue;
  2242. }
  2243. for (j = 0, fl = rxq->ifr_fl; j < rxq->ifr_nfl; j++, fl++) {
  2244. if (iflib_fl_setup(fl)) {
  2245. device_printf(ctx->ifc_dev,
  2246. "setting up free list %d failed - "
  2247. "check cluster settings\n", j);
  2248. goto done;
  2249. }
  2250. }
  2251. }
  2252. done:
  2253. if_setdrvflagbits(ctx->ifc_ifp, IFF_DRV_RUNNING, IFF_DRV_OACTIVE);
  2254. IFDI_INTR_ENABLE(ctx);
  2255. txq = ctx->ifc_txqs;
  2256. for (i = 0; i < scctx->isc_ntxqsets; i++, txq++)
  2257. callout_reset_on(&txq->ift_timer, iflib_timer_default, iflib_timer, txq,
  2258. txq->ift_timer.c_cpu);
  2259. /* Re-enable txsync/rxsync. */
  2260. netmap_enable_all_rings(ifp);
  2261. }
  2262. static int
  2263. iflib_media_change(if_t ifp)
  2264. {
  2265. if_ctx_t ctx = if_getsoftc(ifp);
  2266. int err;
  2267. CTX_LOCK(ctx);
  2268. if ((err = IFDI_MEDIA_CHANGE(ctx)) == 0)
  2269. iflib_if_init_locked(ctx);
  2270. CTX_UNLOCK(ctx);
  2271. return (err);
  2272. }
  2273. static void
  2274. iflib_media_status(if_t ifp, struct ifmediareq *ifmr)
  2275. {
  2276. if_ctx_t ctx = if_getsoftc(ifp);
  2277. CTX_LOCK(ctx);
  2278. IFDI_UPDATE_ADMIN_STATUS(ctx);
  2279. IFDI_MEDIA_STATUS(ctx, ifmr);
  2280. CTX_UNLOCK(ctx);
  2281. }
  2282. void
  2283. iflib_stop(if_ctx_t ctx)
  2284. {
  2285. iflib_txq_t txq = ctx->ifc_txqs;
  2286. iflib_rxq_t rxq = ctx->ifc_rxqs;
  2287. if_softc_ctx_t scctx = &ctx->ifc_softc_ctx;
  2288. if_shared_ctx_t sctx = ctx->ifc_sctx;
  2289. iflib_dma_info_t di;
  2290. iflib_fl_t fl;
  2291. int i, j;
  2292. /* Tell the stack that the interface is no longer active */
  2293. if_setdrvflagbits(ctx->ifc_ifp, IFF_DRV_OACTIVE, IFF_DRV_RUNNING);
  2294. IFDI_INTR_DISABLE(ctx);
  2295. DELAY(1000);
  2296. IFDI_STOP(ctx);
  2297. DELAY(1000);
  2298. /*
  2299. * Stop any pending txsync/rxsync and prevent new ones
  2300. * form starting. Processes blocked in poll() will get
  2301. * POLLERR.
  2302. */
  2303. netmap_disable_all_rings(ctx->ifc_ifp);
  2304. iflib_debug_reset();
  2305. /* Wait for current tx queue users to exit to disarm watchdog timer. */
  2306. for (i = 0; i < scctx->isc_ntxqsets; i++, txq++) {
  2307. /* make sure all transmitters have completed before proceeding XXX */
  2308. CALLOUT_LOCK(txq);
  2309. callout_stop(&txq->ift_timer);
  2310. #ifdef DEV_NETMAP
  2311. callout_stop(&txq->ift_netmap_timer);
  2312. #endif /* DEV_NETMAP */
  2313. CALLOUT_UNLOCK(txq);
  2314. /* clean any enqueued buffers */
  2315. iflib_ifmp_purge(txq);
  2316. /* Free any existing tx buffers. */
  2317. for (j = 0; j < txq->ift_size; j++) {
  2318. iflib_txsd_free(ctx, txq, j);
  2319. }
  2320. txq->ift_processed = txq->ift_cleaned = txq->ift_cidx_processed = 0;
  2321. txq->ift_in_use = txq->ift_gen = txq->ift_no_desc_avail = 0;
  2322. if (sctx->isc_flags & IFLIB_PRESERVE_TX_INDICES)
  2323. txq->ift_cidx = txq->ift_pidx;
  2324. else
  2325. txq->ift_cidx = txq->ift_pidx = 0;
  2326. txq->ift_closed = txq->ift_mbuf_defrag = txq->ift_mbuf_defrag_failed = 0;
  2327. txq->ift_no_tx_dma_setup = txq->ift_txd_encap_efbig = txq->ift_map_failed = 0;
  2328. txq->ift_pullups = 0;
  2329. ifmp_ring_reset_stats(txq->ift_br);
  2330. for (j = 0, di = txq->ift_ifdi; j < sctx->isc_ntxqs; j++, di++)
  2331. bzero((void *)di->idi_vaddr, di->idi_size);
  2332. }
  2333. for (i = 0; i < scctx->isc_nrxqsets; i++, rxq++) {
  2334. if (rxq->ifr_task.gt_taskqueue != NULL)
  2335. gtaskqueue_drain(rxq->ifr_task.gt_taskqueue,
  2336. &rxq->ifr_task.gt_task);
  2337. rxq->ifr_cq_cidx = 0;
  2338. for (j = 0, di = rxq->ifr_ifdi; j < sctx->isc_nrxqs; j++, di++)
  2339. bzero((void *)di->idi_vaddr, di->idi_size);
  2340. /* also resets the free lists pidx/cidx */
  2341. for (j = 0, fl = rxq->ifr_fl; j < rxq->ifr_nfl; j++, fl++)
  2342. iflib_fl_bufs_free(fl);
  2343. }
  2344. }
  2345. static inline caddr_t
  2346. calc_next_rxd(iflib_fl_t fl, int cidx)
  2347. {
  2348. qidx_t size;
  2349. int nrxd;
  2350. caddr_t start, end, cur, next;
  2351. nrxd = fl->ifl_size;
  2352. size = fl->ifl_rxd_size;
  2353. start = fl->ifl_ifdi->idi_vaddr;
  2354. if (__predict_false(size == 0))
  2355. return (start);
  2356. cur = start + size*cidx;
  2357. end = start + size*nrxd;
  2358. next = CACHE_PTR_NEXT(cur);
  2359. return (next < end ? next : start);
  2360. }
  2361. static inline void
  2362. prefetch_pkts(iflib_fl_t fl, int cidx)
  2363. {
  2364. int nextptr;
  2365. int nrxd = fl->ifl_size;
  2366. caddr_t next_rxd;
  2367. nextptr = (cidx + CACHE_PTR_INCREMENT) & (nrxd-1);
  2368. prefetch(&fl->ifl_sds.ifsd_m[nextptr]);
  2369. prefetch(&fl->ifl_sds.ifsd_cl[nextptr]);
  2370. next_rxd = calc_next_rxd(fl, cidx);
  2371. prefetch(next_rxd);
  2372. prefetch(fl->ifl_sds.ifsd_m[(cidx + 1) & (nrxd-1)]);
  2373. prefetch(fl->ifl_sds.ifsd_m[(cidx + 2) & (nrxd-1)]);
  2374. prefetch(fl->ifl_sds.ifsd_m[(cidx + 3) & (nrxd-1)]);
  2375. prefetch(fl->ifl_sds.ifsd_m[(cidx + 4) & (nrxd-1)]);
  2376. prefetch(fl->ifl_sds.ifsd_cl[(cidx + 1) & (nrxd-1)]);
  2377. prefetch(fl->ifl_sds.ifsd_cl[(cidx + 2) & (nrxd-1)]);
  2378. prefetch(fl->ifl_sds.ifsd_cl[(cidx + 3) & (nrxd-1)]);
  2379. prefetch(fl->ifl_sds.ifsd_cl[(cidx + 4) & (nrxd-1)]);
  2380. }
  2381. static struct mbuf *
  2382. rxd_frag_to_sd(iflib_rxq_t rxq, if_rxd_frag_t irf, bool unload, if_rxsd_t sd,
  2383. int *pf_rv, if_rxd_info_t ri)
  2384. {
  2385. bus_dmamap_t map;
  2386. iflib_fl_t fl;
  2387. caddr_t payload;
  2388. struct mbuf *m;
  2389. int flid, cidx, len, next;
  2390. map = NULL;
  2391. flid = irf->irf_flid;
  2392. cidx = irf->irf_idx;
  2393. fl = &rxq->ifr_fl[flid];
  2394. sd->ifsd_fl = fl;
  2395. sd->ifsd_cl = &fl->ifl_sds.ifsd_cl[cidx];
  2396. fl->ifl_credits--;
  2397. #if MEMORY_LOGGING
  2398. fl->ifl_m_dequeued++;
  2399. #endif
  2400. if (rxq->ifr_ctx->ifc_flags & IFC_PREFETCH)
  2401. prefetch_pkts(fl, cidx);
  2402. next = (cidx + CACHE_PTR_INCREMENT) & (fl->ifl_size-1);
  2403. prefetch(&fl->ifl_sds.ifsd_map[next]);
  2404. map = fl->ifl_sds.ifsd_map[cidx];
  2405. bus_dmamap_sync(fl->ifl_buf_tag, map, BUS_DMASYNC_POSTREAD);
  2406. if (rxq->pfil != NULL && PFIL_HOOKED_IN(rxq->pfil) && pf_rv != NULL &&
  2407. irf->irf_len != 0) {
  2408. payload = *sd->ifsd_cl;
  2409. payload += ri->iri_pad;
  2410. len = ri->iri_len - ri->iri_pad;
  2411. *pf_rv = pfil_mem_in(rxq->pfil, payload, len, ri->iri_ifp, &m);
  2412. switch (*pf_rv) {
  2413. case PFIL_DROPPED:
  2414. case PFIL_CONSUMED:
  2415. /*
  2416. * The filter ate it. Everything is recycled.
  2417. */
  2418. m = NULL;
  2419. unload = 0;
  2420. break;
  2421. case PFIL_REALLOCED:
  2422. /*
  2423. * The filter copied it. Everything is recycled.
  2424. * 'm' points at new mbuf.
  2425. */
  2426. unload = 0;
  2427. break;
  2428. case PFIL_PASS:
  2429. /*
  2430. * Filter said it was OK, so receive like
  2431. * normal
  2432. */
  2433. m = fl->ifl_sds.ifsd_m[cidx];
  2434. fl->ifl_sds.ifsd_m[cidx] = NULL;
  2435. break;
  2436. default:
  2437. MPASS(0);
  2438. }
  2439. } else {
  2440. m = fl->ifl_sds.ifsd_m[cidx];
  2441. fl->ifl_sds.ifsd_m[cidx] = NULL;
  2442. if (pf_rv != NULL)
  2443. *pf_rv = PFIL_PASS;
  2444. }
  2445. if (unload && irf->irf_len != 0)
  2446. bus_dmamap_unload(fl->ifl_buf_tag, map);
  2447. fl->ifl_cidx = (fl->ifl_cidx + 1) & (fl->ifl_size-1);
  2448. if (__predict_false(fl->ifl_cidx == 0))
  2449. fl->ifl_gen = 0;
  2450. bit_clear(fl->ifl_rx_bitmap, cidx);
  2451. return (m);
  2452. }
  2453. static struct mbuf *
  2454. assemble_segments(iflib_rxq_t rxq, if_rxd_info_t ri, if_rxsd_t sd, int *pf_rv)
  2455. {
  2456. struct mbuf *m, *mh, *mt;
  2457. caddr_t cl;
  2458. int *pf_rv_ptr, flags, i, padlen;
  2459. bool consumed;
  2460. i = 0;
  2461. mh = NULL;
  2462. consumed = false;
  2463. *pf_rv = PFIL_PASS;
  2464. pf_rv_ptr = pf_rv;
  2465. do {
  2466. m = rxd_frag_to_sd(rxq, &ri->iri_frags[i], !consumed, sd,
  2467. pf_rv_ptr, ri);
  2468. MPASS(*sd->ifsd_cl != NULL);
  2469. /*
  2470. * Exclude zero-length frags & frags from
  2471. * packets the filter has consumed or dropped
  2472. */
  2473. if (ri->iri_frags[i].irf_len == 0 || consumed ||
  2474. *pf_rv == PFIL_CONSUMED || *pf_rv == PFIL_DROPPED) {
  2475. if (mh == NULL) {
  2476. /* everything saved here */
  2477. consumed = true;
  2478. pf_rv_ptr = NULL;
  2479. continue;
  2480. }
  2481. /* XXX we can save the cluster here, but not the mbuf */
  2482. m_init(m, M_NOWAIT, MT_DATA, 0);
  2483. m_free(m);
  2484. continue;
  2485. }
  2486. if (mh == NULL) {
  2487. flags = M_PKTHDR|M_EXT;
  2488. mh = mt = m;
  2489. padlen = ri->iri_pad;
  2490. } else {
  2491. flags = M_EXT;
  2492. mt->m_next = m;
  2493. mt = m;
  2494. /* assuming padding is only on the first fragment */
  2495. padlen = 0;
  2496. }
  2497. cl = *sd->ifsd_cl;
  2498. *sd->ifsd_cl = NULL;
  2499. /* Can these two be made one ? */
  2500. m_init(m, M_NOWAIT, MT_DATA, flags);
  2501. m_cljset(m, cl, sd->ifsd_fl->ifl_cltype);
  2502. /*
  2503. * These must follow m_init and m_cljset
  2504. */
  2505. m->m_data += padlen;
  2506. ri->iri_len -= padlen;
  2507. m->m_len = ri->iri_frags[i].irf_len;
  2508. } while (++i < ri->iri_nfrags);
  2509. return (mh);
  2510. }
  2511. /*
  2512. * Process one software descriptor
  2513. */
  2514. static struct mbuf *
  2515. iflib_rxd_pkt_get(iflib_rxq_t rxq, if_rxd_info_t ri)
  2516. {
  2517. struct if_rxsd sd;
  2518. struct mbuf *m;
  2519. int pf_rv;
  2520. /* should I merge this back in now that the two paths are basically duplicated? */
  2521. if (ri->iri_nfrags == 1 &&
  2522. ri->iri_frags[0].irf_len != 0 &&
  2523. ri->iri_frags[0].irf_len <= MIN(IFLIB_RX_COPY_THRESH, MHLEN)) {
  2524. m = rxd_frag_to_sd(rxq, &ri->iri_frags[0], false, &sd,
  2525. &pf_rv, ri);
  2526. if (pf_rv != PFIL_PASS && pf_rv != PFIL_REALLOCED)
  2527. return (m);
  2528. if (pf_rv == PFIL_PASS) {
  2529. m_init(m, M_NOWAIT, MT_DATA, M_PKTHDR);
  2530. #ifndef __NO_STRICT_ALIGNMENT
  2531. if (!IP_ALIGNED(m) && ri->iri_pad == 0)
  2532. m->m_data += 2;
  2533. #endif
  2534. memcpy(m->m_data, *sd.ifsd_cl, ri->iri_len);
  2535. m->m_len = ri->iri_frags[0].irf_len;
  2536. m->m_data += ri->iri_pad;
  2537. ri->iri_len -= ri->iri_pad;
  2538. }
  2539. } else {
  2540. m = assemble_segments(rxq, ri, &sd, &pf_rv);
  2541. if (m == NULL)
  2542. return (NULL);
  2543. if (pf_rv != PFIL_PASS && pf_rv != PFIL_REALLOCED)
  2544. return (m);
  2545. }
  2546. m->m_pkthdr.len = ri->iri_len;
  2547. m->m_pkthdr.rcvif = ri->iri_ifp;
  2548. m->m_flags |= ri->iri_flags;
  2549. m->m_pkthdr.ether_vtag = ri->iri_vtag;
  2550. m->m_pkthdr.flowid = ri->iri_flowid;
  2551. M_HASHTYPE_SET(m, ri->iri_rsstype);
  2552. m->m_pkthdr.csum_flags = ri->iri_csum_flags;
  2553. m->m_pkthdr.csum_data = ri->iri_csum_data;
  2554. return (m);
  2555. }
  2556. #if defined(INET6) || defined(INET)
  2557. static void
  2558. iflib_get_ip_forwarding(struct lro_ctrl *lc, bool *v4, bool *v6)
  2559. {
  2560. CURVNET_SET(if_getvnet(lc->ifp));
  2561. #if defined(INET6)
  2562. *v6 = V_ip6_forwarding;
  2563. #endif
  2564. #if defined(INET)
  2565. *v4 = V_ipforwarding;
  2566. #endif
  2567. CURVNET_RESTORE();
  2568. }
  2569. /*
  2570. * Returns true if it's possible this packet could be LROed.
  2571. * if it returns false, it is guaranteed that tcp_lro_rx()
  2572. * would not return zero.
  2573. */
  2574. static bool
  2575. iflib_check_lro_possible(struct mbuf *m, bool v4_forwarding, bool v6_forwarding)
  2576. {
  2577. struct ether_header *eh;
  2578. eh = mtod(m, struct ether_header *);
  2579. switch (eh->ether_type) {
  2580. #if defined(INET6)
  2581. case htons(ETHERTYPE_IPV6):
  2582. return (!v6_forwarding);
  2583. #endif
  2584. #if defined (INET)
  2585. case htons(ETHERTYPE_IP):
  2586. return (!v4_forwarding);
  2587. #endif
  2588. }
  2589. return false;
  2590. }
  2591. #else
  2592. static void
  2593. iflib_get_ip_forwarding(struct lro_ctrl *lc __unused, bool *v4 __unused, bool *v6 __unused)
  2594. {
  2595. }
  2596. #endif
  2597. static void
  2598. _task_fn_rx_watchdog(void *context)
  2599. {
  2600. iflib_rxq_t rxq = context;
  2601. GROUPTASK_ENQUEUE(&rxq->ifr_task);
  2602. }
  2603. static uint8_t
  2604. iflib_rxeof(iflib_rxq_t rxq, qidx_t budget)
  2605. {
  2606. if_t ifp;
  2607. if_ctx_t ctx = rxq->ifr_ctx;
  2608. if_shared_ctx_t sctx = ctx->ifc_sctx;
  2609. if_softc_ctx_t scctx = &ctx->ifc_softc_ctx;
  2610. int avail, i;
  2611. qidx_t *cidxp;
  2612. struct if_rxd_info ri;
  2613. int err, budget_left, rx_bytes, rx_pkts;
  2614. iflib_fl_t fl;
  2615. int lro_enabled;
  2616. bool v4_forwarding, v6_forwarding, lro_possible;
  2617. uint8_t retval = 0;
  2618. /*
  2619. * XXX early demux data packets so that if_input processing only handles
  2620. * acks in interrupt context
  2621. */
  2622. struct mbuf *m, *mh, *mt, *mf;
  2623. NET_EPOCH_ASSERT();
  2624. lro_possible = v4_forwarding = v6_forwarding = false;
  2625. ifp = ctx->ifc_ifp;
  2626. mh = mt = NULL;
  2627. MPASS(budget > 0);
  2628. rx_pkts = rx_bytes = 0;
  2629. if (sctx->isc_flags & IFLIB_HAS_RXCQ)
  2630. cidxp = &rxq->ifr_cq_cidx;
  2631. else
  2632. cidxp = &rxq->ifr_fl[0].ifl_cidx;
  2633. if ((avail = iflib_rxd_avail(ctx, rxq, *cidxp, budget)) == 0) {
  2634. for (i = 0, fl = &rxq->ifr_fl[0]; i < sctx->isc_nfl; i++, fl++)
  2635. retval |= iflib_fl_refill_all(ctx, fl);
  2636. DBG_COUNTER_INC(rx_unavail);
  2637. return (retval);
  2638. }
  2639. /* pfil needs the vnet to be set */
  2640. CURVNET_SET_QUIET(if_getvnet(ifp));
  2641. for (budget_left = budget; budget_left > 0 && avail > 0;) {
  2642. if (__predict_false(!CTX_ACTIVE(ctx))) {
  2643. DBG_COUNTER_INC(rx_ctx_inactive);
  2644. break;
  2645. }
  2646. /*
  2647. * Reset client set fields to their default values
  2648. */
  2649. rxd_info_zero(&ri);
  2650. ri.iri_qsidx = rxq->ifr_id;
  2651. ri.iri_cidx = *cidxp;
  2652. ri.iri_ifp = ifp;
  2653. ri.iri_frags = rxq->ifr_frags;
  2654. err = ctx->isc_rxd_pkt_get(ctx->ifc_softc, &ri);
  2655. if (err)
  2656. goto err;
  2657. rx_pkts += 1;
  2658. rx_bytes += ri.iri_len;
  2659. if (sctx->isc_flags & IFLIB_HAS_RXCQ) {
  2660. *cidxp = ri.iri_cidx;
  2661. /* Update our consumer index */
  2662. /* XXX NB: shurd - check if this is still safe */
  2663. while (rxq->ifr_cq_cidx >= scctx->isc_nrxd[0])
  2664. rxq->ifr_cq_cidx -= scctx->isc_nrxd[0];
  2665. /* was this only a completion queue message? */
  2666. if (__predict_false(ri.iri_nfrags == 0))
  2667. continue;
  2668. }
  2669. MPASS(ri.iri_nfrags != 0);
  2670. MPASS(ri.iri_len != 0);
  2671. /* will advance the cidx on the corresponding free lists */
  2672. m = iflib_rxd_pkt_get(rxq, &ri);
  2673. avail--;
  2674. budget_left--;
  2675. if (avail == 0 && budget_left)
  2676. avail = iflib_rxd_avail(ctx, rxq, *cidxp, budget_left);
  2677. if (__predict_false(m == NULL))
  2678. continue;
  2679. /* imm_pkt: -- cxgb */
  2680. if (mh == NULL)
  2681. mh = mt = m;
  2682. else {
  2683. mt->m_nextpkt = m;
  2684. mt = m;
  2685. }
  2686. }
  2687. CURVNET_RESTORE();
  2688. /* make sure that we can refill faster than drain */
  2689. for (i = 0, fl = &rxq->ifr_fl[0]; i < sctx->isc_nfl; i++, fl++)
  2690. retval |= iflib_fl_refill_all(ctx, fl);
  2691. lro_enabled = (if_getcapenable(ifp) & IFCAP_LRO);
  2692. if (lro_enabled)
  2693. iflib_get_ip_forwarding(&rxq->ifr_lc, &v4_forwarding, &v6_forwarding);
  2694. mt = mf = NULL;
  2695. while (mh != NULL) {
  2696. m = mh;
  2697. mh = mh->m_nextpkt;
  2698. m->m_nextpkt = NULL;
  2699. #ifndef __NO_STRICT_ALIGNMENT
  2700. if (!IP_ALIGNED(m) && (m = iflib_fixup_rx(m)) == NULL)
  2701. continue;
  2702. #endif
  2703. #if defined(INET6) || defined(INET)
  2704. if (lro_enabled) {
  2705. if (!lro_possible) {
  2706. lro_possible = iflib_check_lro_possible(m, v4_forwarding, v6_forwarding);
  2707. if (lro_possible && mf != NULL) {
  2708. if_input(ifp, mf);
  2709. DBG_COUNTER_INC(rx_if_input);
  2710. mt = mf = NULL;
  2711. }
  2712. }
  2713. if ((m->m_pkthdr.csum_flags & (CSUM_L4_CALC|CSUM_L4_VALID)) ==
  2714. (CSUM_L4_CALC|CSUM_L4_VALID)) {
  2715. if (lro_possible && tcp_lro_rx(&rxq->ifr_lc, m, 0) == 0)
  2716. continue;
  2717. }
  2718. }
  2719. #endif
  2720. if (lro_possible) {
  2721. if_input(ifp, m);
  2722. DBG_COUNTER_INC(rx_if_input);
  2723. continue;
  2724. }
  2725. if (mf == NULL)
  2726. mf = m;
  2727. if (mt != NULL)
  2728. mt->m_nextpkt = m;
  2729. mt = m;
  2730. }
  2731. if (mf != NULL) {
  2732. if_input(ifp, mf);
  2733. DBG_COUNTER_INC(rx_if_input);
  2734. }
  2735. if_inc_counter(ifp, IFCOUNTER_IBYTES, rx_bytes);
  2736. if_inc_counter(ifp, IFCOUNTER_IPACKETS, rx_pkts);
  2737. /*
  2738. * Flush any outstanding LRO work
  2739. */
  2740. #if defined(INET6) || defined(INET)
  2741. tcp_lro_flush_all(&rxq->ifr_lc);
  2742. #endif
  2743. if (avail != 0 || iflib_rxd_avail(ctx, rxq, *cidxp, 1) != 0)
  2744. retval |= IFLIB_RXEOF_MORE;
  2745. return (retval);
  2746. err:
  2747. STATE_LOCK(ctx);
  2748. ctx->ifc_flags |= IFC_DO_RESET;
  2749. iflib_admin_intr_deferred(ctx);
  2750. STATE_UNLOCK(ctx);
  2751. return (0);
  2752. }
  2753. #define TXD_NOTIFY_COUNT(txq) (((txq)->ift_size / (txq)->ift_update_freq)-1)
  2754. static inline qidx_t
  2755. txq_max_db_deferred(iflib_txq_t txq, qidx_t in_use)
  2756. {
  2757. qidx_t notify_count = TXD_NOTIFY_COUNT(txq);
  2758. qidx_t minthresh = txq->ift_size / 8;
  2759. if (in_use > 4*minthresh)
  2760. return (notify_count);
  2761. if (in_use > 2*minthresh)
  2762. return (notify_count >> 1);
  2763. if (in_use > minthresh)
  2764. return (notify_count >> 3);
  2765. return (0);
  2766. }
  2767. static inline qidx_t
  2768. txq_max_rs_deferred(iflib_txq_t txq)
  2769. {
  2770. qidx_t notify_count = TXD_NOTIFY_COUNT(txq);
  2771. qidx_t minthresh = txq->ift_size / 8;
  2772. if (txq->ift_in_use > 4*minthresh)
  2773. return (notify_count);
  2774. if (txq->ift_in_use > 2*minthresh)
  2775. return (notify_count >> 1);
  2776. if (txq->ift_in_use > minthresh)
  2777. return (notify_count >> 2);
  2778. return (2);
  2779. }
  2780. #define M_CSUM_FLAGS(m) ((m)->m_pkthdr.csum_flags)
  2781. #define M_HAS_VLANTAG(m) (m->m_flags & M_VLANTAG)
  2782. #define TXQ_MAX_DB_DEFERRED(txq, in_use) txq_max_db_deferred((txq), (in_use))
  2783. #define TXQ_MAX_RS_DEFERRED(txq) txq_max_rs_deferred(txq)
  2784. #define TXQ_MAX_DB_CONSUMED(size) (size >> 4)
  2785. /* forward compatibility for cxgb */
  2786. #define FIRST_QSET(ctx) 0
  2787. #define NTXQSETS(ctx) ((ctx)->ifc_softc_ctx.isc_ntxqsets)
  2788. #define NRXQSETS(ctx) ((ctx)->ifc_softc_ctx.isc_nrxqsets)
  2789. #define QIDX(ctx, m) ((((m)->m_pkthdr.flowid & ctx->ifc_softc_ctx.isc_rss_table_mask) % NTXQSETS(ctx)) + FIRST_QSET(ctx))
  2790. #define DESC_RECLAIMABLE(q) ((int)((q)->ift_processed - (q)->ift_cleaned - (q)->ift_ctx->ifc_softc_ctx.isc_tx_nsegments))
  2791. /* XXX we should be setting this to something other than zero */
  2792. #define RECLAIM_THRESH(ctx) ((ctx)->ifc_sctx->isc_tx_reclaim_thresh)
  2793. #define MAX_TX_DESC(ctx) MAX((ctx)->ifc_softc_ctx.isc_tx_tso_segments_max, \
  2794. (ctx)->ifc_softc_ctx.isc_tx_nsegments)
  2795. static inline bool
  2796. iflib_txd_db_check(iflib_txq_t txq, int ring)
  2797. {
  2798. if_ctx_t ctx = txq->ift_ctx;
  2799. qidx_t dbval, max;
  2800. max = TXQ_MAX_DB_DEFERRED(txq, txq->ift_in_use);
  2801. /* force || threshold exceeded || at the edge of the ring */
  2802. if (ring || (txq->ift_db_pending >= max) || (TXQ_AVAIL(txq) <= MAX_TX_DESC(ctx) + 2)) {
  2803. /*
  2804. * 'npending' is used if the card's doorbell is in terms of the number of descriptors
  2805. * pending flush (BRCM). 'pidx' is used in cases where the card's doorbeel uses the
  2806. * producer index explicitly (INTC).
  2807. */
  2808. dbval = txq->ift_npending ? txq->ift_npending : txq->ift_pidx;
  2809. bus_dmamap_sync(txq->ift_ifdi->idi_tag, txq->ift_ifdi->idi_map,
  2810. BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
  2811. ctx->isc_txd_flush(ctx->ifc_softc, txq->ift_id, dbval);
  2812. /*
  2813. * Absent bugs there are zero packets pending so reset pending counts to zero.
  2814. */
  2815. txq->ift_db_pending = txq->ift_npending = 0;
  2816. return (true);
  2817. }
  2818. return (false);
  2819. }
  2820. #ifdef PKT_DEBUG
  2821. static void
  2822. print_pkt(if_pkt_info_t pi)
  2823. {
  2824. printf("pi len: %d qsidx: %d nsegs: %d ndescs: %d flags: %x pidx: %d\n",
  2825. pi->ipi_len, pi->ipi_qsidx, pi->ipi_nsegs, pi->ipi_ndescs, pi->ipi_flags, pi->ipi_pidx);
  2826. printf("pi new_pidx: %d csum_flags: %lx tso_segsz: %d mflags: %x vtag: %d\n",
  2827. pi->ipi_new_pidx, pi->ipi_csum_flags, pi->ipi_tso_segsz, pi->ipi_mflags, pi->ipi_vtag);
  2828. printf("pi etype: %d ehdrlen: %d ip_hlen: %d ipproto: %d\n",
  2829. pi->ipi_etype, pi->ipi_ehdrlen, pi->ipi_ip_hlen, pi->ipi_ipproto);
  2830. }
  2831. #endif
  2832. #define IS_TSO4(pi) ((pi)->ipi_csum_flags & CSUM_IP_TSO)
  2833. #define IS_TX_OFFLOAD4(pi) ((pi)->ipi_csum_flags & (CSUM_IP_TCP | CSUM_IP_TSO))
  2834. #define IS_TSO6(pi) ((pi)->ipi_csum_flags & CSUM_IP6_TSO)
  2835. #define IS_TX_OFFLOAD6(pi) ((pi)->ipi_csum_flags & (CSUM_IP6_TCP | CSUM_IP6_TSO))
  2836. /**
  2837. * Parses out ethernet header information in the given mbuf.
  2838. * Returns in pi: ipi_etype (EtherType) and ipi_ehdrlen (Ethernet header length)
  2839. *
  2840. * This will account for the VLAN header if present.
  2841. *
  2842. * XXX: This doesn't handle QinQ, which could prevent TX offloads for those
  2843. * types of packets.
  2844. */
  2845. static int
  2846. iflib_parse_ether_header(if_pkt_info_t pi, struct mbuf **mp, uint64_t *pullups)
  2847. {
  2848. struct ether_vlan_header *eh;
  2849. struct mbuf *m;
  2850. m = *mp;
  2851. if (__predict_false(m->m_len < sizeof(*eh))) {
  2852. (*pullups)++;
  2853. if (__predict_false((m = m_pullup(m, sizeof(*eh))) == NULL))
  2854. return (ENOMEM);
  2855. }
  2856. eh = mtod(m, struct ether_vlan_header *);
  2857. if (eh->evl_encap_proto == htons(ETHERTYPE_VLAN)) {
  2858. pi->ipi_etype = ntohs(eh->evl_proto);
  2859. pi->ipi_ehdrlen = ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN;
  2860. } else {
  2861. pi->ipi_etype = ntohs(eh->evl_encap_proto);
  2862. pi->ipi_ehdrlen = ETHER_HDR_LEN;
  2863. }
  2864. *mp = m;
  2865. return (0);
  2866. }
  2867. /**
  2868. * Parse up to the L3 header and extract IPv4/IPv6 header information into pi.
  2869. * Currently this information includes: IP ToS value, IP header version/presence
  2870. *
  2871. * This is missing some checks and doesn't edit the packet content as it goes,
  2872. * unlike iflib_parse_header(), in order to keep the amount of code here minimal.
  2873. */
  2874. static int
  2875. iflib_parse_header_partial(if_pkt_info_t pi, struct mbuf **mp, uint64_t *pullups)
  2876. {
  2877. struct mbuf *m;
  2878. int err;
  2879. *pullups = 0;
  2880. m = *mp;
  2881. if (!M_WRITABLE(m)) {
  2882. if ((m = m_dup(m, M_NOWAIT)) == NULL) {
  2883. return (ENOMEM);
  2884. } else {
  2885. m_freem(*mp);
  2886. DBG_COUNTER_INC(tx_frees);
  2887. *mp = m;
  2888. }
  2889. }
  2890. /* Fills out pi->ipi_etype */
  2891. err = iflib_parse_ether_header(pi, mp, pullups);
  2892. if (err)
  2893. return (err);
  2894. m = *mp;
  2895. switch (pi->ipi_etype) {
  2896. #ifdef INET
  2897. case ETHERTYPE_IP:
  2898. {
  2899. struct mbuf *n;
  2900. struct ip *ip = NULL;
  2901. int miniplen;
  2902. miniplen = min(m->m_pkthdr.len, pi->ipi_ehdrlen + sizeof(*ip));
  2903. if (__predict_false(m->m_len < miniplen)) {
  2904. /*
  2905. * Check for common case where the first mbuf only contains
  2906. * the Ethernet header
  2907. */
  2908. if (m->m_len == pi->ipi_ehdrlen) {
  2909. n = m->m_next;
  2910. MPASS(n);
  2911. /* If next mbuf contains at least the minimal IP header, then stop */
  2912. if (n->m_len >= sizeof(*ip)) {
  2913. ip = (struct ip *)n->m_data;
  2914. } else {
  2915. (*pullups)++;
  2916. if (__predict_false((m = m_pullup(m, miniplen)) == NULL))
  2917. return (ENOMEM);
  2918. ip = (struct ip *)(m->m_data + pi->ipi_ehdrlen);
  2919. }
  2920. } else {
  2921. (*pullups)++;
  2922. if (__predict_false((m = m_pullup(m, miniplen)) == NULL))
  2923. return (ENOMEM);
  2924. ip = (struct ip *)(m->m_data + pi->ipi_ehdrlen);
  2925. }
  2926. } else {
  2927. ip = (struct ip *)(m->m_data + pi->ipi_ehdrlen);
  2928. }
  2929. /* Have the IPv4 header w/ no options here */
  2930. pi->ipi_ip_hlen = ip->ip_hl << 2;
  2931. pi->ipi_ipproto = ip->ip_p;
  2932. pi->ipi_ip_tos = ip->ip_tos;
  2933. pi->ipi_flags |= IPI_TX_IPV4;
  2934. break;
  2935. }
  2936. #endif
  2937. #ifdef INET6
  2938. case ETHERTYPE_IPV6:
  2939. {
  2940. struct ip6_hdr *ip6;
  2941. if (__predict_false(m->m_len < pi->ipi_ehdrlen + sizeof(struct ip6_hdr))) {
  2942. (*pullups)++;
  2943. if (__predict_false((m = m_pullup(m, pi->ipi_ehdrlen + sizeof(struct ip6_hdr))) == NULL))
  2944. return (ENOMEM);
  2945. }
  2946. ip6 = (struct ip6_hdr *)(m->m_data + pi->ipi_ehdrlen);
  2947. /* Have the IPv6 fixed header here */
  2948. pi->ipi_ip_hlen = sizeof(struct ip6_hdr);
  2949. pi->ipi_ipproto = ip6->ip6_nxt;
  2950. pi->ipi_ip_tos = IPV6_TRAFFIC_CLASS(ip6);
  2951. pi->ipi_flags |= IPI_TX_IPV6;
  2952. break;
  2953. }
  2954. #endif
  2955. default:
  2956. pi->ipi_csum_flags &= ~CSUM_OFFLOAD;
  2957. pi->ipi_ip_hlen = 0;
  2958. break;
  2959. }
  2960. *mp = m;
  2961. return (0);
  2962. }
  2963. static int
  2964. iflib_parse_header(iflib_txq_t txq, if_pkt_info_t pi, struct mbuf **mp)
  2965. {
  2966. if_shared_ctx_t sctx = txq->ift_ctx->ifc_sctx;
  2967. struct mbuf *m;
  2968. int err;
  2969. m = *mp;
  2970. if ((sctx->isc_flags & IFLIB_NEED_SCRATCH) &&
  2971. M_WRITABLE(m) == 0) {
  2972. if ((m = m_dup(m, M_NOWAIT)) == NULL) {
  2973. return (ENOMEM);
  2974. } else {
  2975. m_freem(*mp);
  2976. DBG_COUNTER_INC(tx_frees);
  2977. *mp = m;
  2978. }
  2979. }
  2980. /* Fills out pi->ipi_etype */
  2981. err = iflib_parse_ether_header(pi, mp, &txq->ift_pullups);
  2982. if (__predict_false(err))
  2983. return (err);
  2984. m = *mp;
  2985. switch (pi->ipi_etype) {
  2986. #ifdef INET
  2987. case ETHERTYPE_IP:
  2988. {
  2989. struct mbuf *n;
  2990. struct ip *ip = NULL;
  2991. struct tcphdr *th = NULL;
  2992. int minthlen;
  2993. minthlen = min(m->m_pkthdr.len, pi->ipi_ehdrlen + sizeof(*ip) + sizeof(*th));
  2994. if (__predict_false(m->m_len < minthlen)) {
  2995. /*
  2996. * if this code bloat is causing too much of a hit
  2997. * move it to a separate function and mark it noinline
  2998. */
  2999. if (m->m_len == pi->ipi_ehdrlen) {
  3000. n = m->m_next;
  3001. MPASS(n);
  3002. if (n->m_len >= sizeof(*ip)) {
  3003. ip = (struct ip *)n->m_data;
  3004. if (n->m_len >= (ip->ip_hl << 2) + sizeof(*th))
  3005. th = (struct tcphdr *)((caddr_t)ip + (ip->ip_hl << 2));
  3006. } else {
  3007. txq->ift_pullups++;
  3008. if (__predict_false((m = m_pullup(m, minthlen)) == NULL))
  3009. return (ENOMEM);
  3010. ip = (struct ip *)(m->m_data + pi->ipi_ehdrlen);
  3011. }
  3012. } else {
  3013. txq->ift_pullups++;
  3014. if (__predict_false((m = m_pullup(m, minthlen)) == NULL))
  3015. return (ENOMEM);
  3016. ip = (struct ip *)(m->m_data + pi->ipi_ehdrlen);
  3017. if (m->m_len >= (ip->ip_hl << 2) + sizeof(*th))
  3018. th = (struct tcphdr *)((caddr_t)ip + (ip->ip_hl << 2));
  3019. }
  3020. } else {
  3021. ip = (struct ip *)(m->m_data + pi->ipi_ehdrlen);
  3022. if (m->m_len >= (ip->ip_hl << 2) + sizeof(*th))
  3023. th = (struct tcphdr *)((caddr_t)ip + (ip->ip_hl << 2));
  3024. }
  3025. pi->ipi_ip_hlen = ip->ip_hl << 2;
  3026. pi->ipi_ipproto = ip->ip_p;
  3027. pi->ipi_ip_tos = ip->ip_tos;
  3028. pi->ipi_flags |= IPI_TX_IPV4;
  3029. /* TCP checksum offload may require TCP header length */
  3030. if (IS_TX_OFFLOAD4(pi)) {
  3031. if (__predict_true(pi->ipi_ipproto == IPPROTO_TCP)) {
  3032. if (__predict_false(th == NULL)) {
  3033. txq->ift_pullups++;
  3034. if (__predict_false((m = m_pullup(m, (ip->ip_hl << 2) + sizeof(*th))) == NULL))
  3035. return (ENOMEM);
  3036. th = (struct tcphdr *)((caddr_t)ip + pi->ipi_ip_hlen);
  3037. }
  3038. pi->ipi_tcp_hflags = th->th_flags;
  3039. pi->ipi_tcp_hlen = th->th_off << 2;
  3040. pi->ipi_tcp_seq = th->th_seq;
  3041. }
  3042. if (IS_TSO4(pi)) {
  3043. if (__predict_false(ip->ip_p != IPPROTO_TCP))
  3044. return (ENXIO);
  3045. /*
  3046. * TSO always requires hardware checksum offload.
  3047. */
  3048. pi->ipi_csum_flags |= (CSUM_IP_TCP | CSUM_IP);
  3049. th->th_sum = in_pseudo(ip->ip_src.s_addr,
  3050. ip->ip_dst.s_addr, htons(IPPROTO_TCP));
  3051. pi->ipi_tso_segsz = m->m_pkthdr.tso_segsz;
  3052. if (sctx->isc_flags & IFLIB_TSO_INIT_IP) {
  3053. ip->ip_sum = 0;
  3054. ip->ip_len = htons(pi->ipi_ip_hlen + pi->ipi_tcp_hlen + pi->ipi_tso_segsz);
  3055. }
  3056. }
  3057. }
  3058. if ((sctx->isc_flags & IFLIB_NEED_ZERO_CSUM) && (pi->ipi_csum_flags & CSUM_IP))
  3059. ip->ip_sum = 0;
  3060. break;
  3061. }
  3062. #endif
  3063. #ifdef INET6
  3064. case ETHERTYPE_IPV6:
  3065. {
  3066. struct ip6_hdr *ip6 = (struct ip6_hdr *)(m->m_data + pi->ipi_ehdrlen);
  3067. struct tcphdr *th;
  3068. pi->ipi_ip_hlen = sizeof(struct ip6_hdr);
  3069. if (__predict_false(m->m_len < pi->ipi_ehdrlen + sizeof(struct ip6_hdr))) {
  3070. txq->ift_pullups++;
  3071. if (__predict_false((m = m_pullup(m, pi->ipi_ehdrlen + sizeof(struct ip6_hdr))) == NULL))
  3072. return (ENOMEM);
  3073. }
  3074. th = (struct tcphdr *)((caddr_t)ip6 + pi->ipi_ip_hlen);
  3075. /* XXX-BZ this will go badly in case of ext hdrs. */
  3076. pi->ipi_ipproto = ip6->ip6_nxt;
  3077. pi->ipi_ip_tos = IPV6_TRAFFIC_CLASS(ip6);
  3078. pi->ipi_flags |= IPI_TX_IPV6;
  3079. /* TCP checksum offload may require TCP header length */
  3080. if (IS_TX_OFFLOAD6(pi)) {
  3081. if (pi->ipi_ipproto == IPPROTO_TCP) {
  3082. if (__predict_false(m->m_len < pi->ipi_ehdrlen + sizeof(struct ip6_hdr) + sizeof(struct tcphdr))) {
  3083. txq->ift_pullups++;
  3084. if (__predict_false((m = m_pullup(m, pi->ipi_ehdrlen + sizeof(struct ip6_hdr) + sizeof(struct tcphdr))) == NULL))
  3085. return (ENOMEM);
  3086. }
  3087. pi->ipi_tcp_hflags = th->th_flags;
  3088. pi->ipi_tcp_hlen = th->th_off << 2;
  3089. pi->ipi_tcp_seq = th->th_seq;
  3090. }
  3091. if (IS_TSO6(pi)) {
  3092. if (__predict_false(ip6->ip6_nxt != IPPROTO_TCP))
  3093. return (ENXIO);
  3094. /*
  3095. * TSO always requires hardware checksum offload.
  3096. */
  3097. pi->ipi_csum_flags |= CSUM_IP6_TCP;
  3098. th->th_sum = in6_cksum_pseudo(ip6, 0, IPPROTO_TCP, 0);
  3099. pi->ipi_tso_segsz = m->m_pkthdr.tso_segsz;
  3100. }
  3101. }
  3102. break;
  3103. }
  3104. #endif
  3105. default:
  3106. pi->ipi_csum_flags &= ~CSUM_OFFLOAD;
  3107. pi->ipi_ip_hlen = 0;
  3108. break;
  3109. }
  3110. *mp = m;
  3111. return (0);
  3112. }
  3113. /*
  3114. * If dodgy hardware rejects the scatter gather chain we've handed it
  3115. * we'll need to remove the mbuf chain from ifsg_m[] before we can add the
  3116. * m_defrag'd mbufs
  3117. */
  3118. static __noinline struct mbuf *
  3119. iflib_remove_mbuf(iflib_txq_t txq)
  3120. {
  3121. int ntxd, pidx;
  3122. struct mbuf *m, **ifsd_m;
  3123. ifsd_m = txq->ift_sds.ifsd_m;
  3124. ntxd = txq->ift_size;
  3125. pidx = txq->ift_pidx & (ntxd - 1);
  3126. ifsd_m = txq->ift_sds.ifsd_m;
  3127. m = ifsd_m[pidx];
  3128. ifsd_m[pidx] = NULL;
  3129. bus_dmamap_unload(txq->ift_buf_tag, txq->ift_sds.ifsd_map[pidx]);
  3130. if (txq->ift_sds.ifsd_tso_map != NULL)
  3131. bus_dmamap_unload(txq->ift_tso_buf_tag,
  3132. txq->ift_sds.ifsd_tso_map[pidx]);
  3133. #if MEMORY_LOGGING
  3134. txq->ift_dequeued++;
  3135. #endif
  3136. return (m);
  3137. }
  3138. static inline caddr_t
  3139. calc_next_txd(iflib_txq_t txq, int cidx, uint8_t qid)
  3140. {
  3141. qidx_t size;
  3142. int ntxd;
  3143. caddr_t start, end, cur, next;
  3144. ntxd = txq->ift_size;
  3145. size = txq->ift_txd_size[qid];
  3146. start = txq->ift_ifdi[qid].idi_vaddr;
  3147. if (__predict_false(size == 0))
  3148. return (start);
  3149. cur = start + size*cidx;
  3150. end = start + size*ntxd;
  3151. next = CACHE_PTR_NEXT(cur);
  3152. return (next < end ? next : start);
  3153. }
  3154. /*
  3155. * Pad an mbuf to ensure a minimum ethernet frame size.
  3156. * min_frame_size is the frame size (less CRC) to pad the mbuf to
  3157. */
  3158. static __noinline int
  3159. iflib_ether_pad(device_t dev, struct mbuf **m_head, uint16_t min_frame_size)
  3160. {
  3161. /*
  3162. * 18 is enough bytes to pad an ARP packet to 46 bytes, and
  3163. * and ARP message is the smallest common payload I can think of
  3164. */
  3165. static char pad[18]; /* just zeros */
  3166. int n;
  3167. struct mbuf *new_head;
  3168. if (!M_WRITABLE(*m_head)) {
  3169. new_head = m_dup(*m_head, M_NOWAIT);
  3170. if (new_head == NULL) {
  3171. m_freem(*m_head);
  3172. device_printf(dev, "cannot pad short frame, m_dup() failed");
  3173. DBG_COUNTER_INC(encap_pad_mbuf_fail);
  3174. DBG_COUNTER_INC(tx_frees);
  3175. return ENOMEM;
  3176. }
  3177. m_freem(*m_head);
  3178. *m_head = new_head;
  3179. }
  3180. for (n = min_frame_size - (*m_head)->m_pkthdr.len;
  3181. n > 0; n -= sizeof(pad))
  3182. if (!m_append(*m_head, min(n, sizeof(pad)), pad))
  3183. break;
  3184. if (n > 0) {
  3185. m_freem(*m_head);
  3186. device_printf(dev, "cannot pad short frame\n");
  3187. DBG_COUNTER_INC(encap_pad_mbuf_fail);
  3188. DBG_COUNTER_INC(tx_frees);
  3189. return (ENOBUFS);
  3190. }
  3191. return 0;
  3192. }
  3193. static int
  3194. iflib_encap(iflib_txq_t txq, struct mbuf **m_headp)
  3195. {
  3196. if_ctx_t ctx;
  3197. if_shared_ctx_t sctx;
  3198. if_softc_ctx_t scctx;
  3199. bus_dma_tag_t buf_tag;
  3200. bus_dma_segment_t *segs;
  3201. struct mbuf *m_head, **ifsd_m;
  3202. void *next_txd;
  3203. bus_dmamap_t map;
  3204. struct if_pkt_info pi;
  3205. int remap = 0;
  3206. int err, nsegs, ndesc, max_segs, pidx, cidx, next, ntxd;
  3207. ctx = txq->ift_ctx;
  3208. sctx = ctx->ifc_sctx;
  3209. scctx = &ctx->ifc_softc_ctx;
  3210. segs = txq->ift_segs;
  3211. ntxd = txq->ift_size;
  3212. m_head = *m_headp;
  3213. map = NULL;
  3214. /*
  3215. * If we're doing TSO the next descriptor to clean may be quite far ahead
  3216. */
  3217. cidx = txq->ift_cidx;
  3218. pidx = txq->ift_pidx;
  3219. if (ctx->ifc_flags & IFC_PREFETCH) {
  3220. next = (cidx + CACHE_PTR_INCREMENT) & (ntxd-1);
  3221. if (!(ctx->ifc_flags & IFLIB_HAS_TXCQ)) {
  3222. next_txd = calc_next_txd(txq, cidx, 0);
  3223. prefetch(next_txd);
  3224. }
  3225. /* prefetch the next cache line of mbuf pointers and flags */
  3226. prefetch(&txq->ift_sds.ifsd_m[next]);
  3227. prefetch(&txq->ift_sds.ifsd_map[next]);
  3228. next = (cidx + CACHE_LINE_SIZE) & (ntxd-1);
  3229. }
  3230. map = txq->ift_sds.ifsd_map[pidx];
  3231. ifsd_m = txq->ift_sds.ifsd_m;
  3232. if (m_head->m_pkthdr.csum_flags & CSUM_TSO) {
  3233. buf_tag = txq->ift_tso_buf_tag;
  3234. max_segs = scctx->isc_tx_tso_segments_max;
  3235. map = txq->ift_sds.ifsd_tso_map[pidx];
  3236. MPASS(buf_tag != NULL);
  3237. MPASS(max_segs > 0);
  3238. } else {
  3239. buf_tag = txq->ift_buf_tag;
  3240. max_segs = scctx->isc_tx_nsegments;
  3241. map = txq->ift_sds.ifsd_map[pidx];
  3242. }
  3243. if ((sctx->isc_flags & IFLIB_NEED_ETHER_PAD) &&
  3244. __predict_false(m_head->m_pkthdr.len < scctx->isc_min_frame_size)) {
  3245. err = iflib_ether_pad(ctx->ifc_dev, m_headp, scctx->isc_min_frame_size);
  3246. if (err) {
  3247. DBG_COUNTER_INC(encap_txd_encap_fail);
  3248. return err;
  3249. }
  3250. }
  3251. m_head = *m_headp;
  3252. pkt_info_zero(&pi);
  3253. pi.ipi_mflags = (m_head->m_flags & (M_VLANTAG|M_BCAST|M_MCAST));
  3254. pi.ipi_pidx = pidx;
  3255. pi.ipi_qsidx = txq->ift_id;
  3256. pi.ipi_len = m_head->m_pkthdr.len;
  3257. pi.ipi_csum_flags = m_head->m_pkthdr.csum_flags;
  3258. pi.ipi_vtag = M_HAS_VLANTAG(m_head) ? m_head->m_pkthdr.ether_vtag : 0;
  3259. /* deliberate bitwise OR to make one condition */
  3260. if (__predict_true((pi.ipi_csum_flags | pi.ipi_vtag))) {
  3261. if (__predict_false((err = iflib_parse_header(txq, &pi, m_headp)) != 0)) {
  3262. DBG_COUNTER_INC(encap_txd_encap_fail);
  3263. return (err);
  3264. }
  3265. m_head = *m_headp;
  3266. }
  3267. retry:
  3268. err = bus_dmamap_load_mbuf_sg(buf_tag, map, m_head, segs, &nsegs,
  3269. BUS_DMA_NOWAIT);
  3270. defrag:
  3271. if (__predict_false(err)) {
  3272. switch (err) {
  3273. case EFBIG:
  3274. /* try collapse once and defrag once */
  3275. if (remap == 0) {
  3276. m_head = m_collapse(*m_headp, M_NOWAIT, max_segs);
  3277. /* try defrag if collapsing fails */
  3278. if (m_head == NULL)
  3279. remap++;
  3280. }
  3281. if (remap == 1) {
  3282. txq->ift_mbuf_defrag++;
  3283. m_head = m_defrag(*m_headp, M_NOWAIT);
  3284. }
  3285. /*
  3286. * remap should never be >1 unless bus_dmamap_load_mbuf_sg
  3287. * failed to map an mbuf that was run through m_defrag
  3288. */
  3289. MPASS(remap <= 1);
  3290. if (__predict_false(m_head == NULL || remap > 1))
  3291. goto defrag_failed;
  3292. remap++;
  3293. *m_headp = m_head;
  3294. goto retry;
  3295. break;
  3296. case ENOMEM:
  3297. txq->ift_no_tx_dma_setup++;
  3298. break;
  3299. default:
  3300. txq->ift_no_tx_dma_setup++;
  3301. m_freem(*m_headp);
  3302. DBG_COUNTER_INC(tx_frees);
  3303. *m_headp = NULL;
  3304. break;
  3305. }
  3306. txq->ift_map_failed++;
  3307. DBG_COUNTER_INC(encap_load_mbuf_fail);
  3308. DBG_COUNTER_INC(encap_txd_encap_fail);
  3309. return (err);
  3310. }
  3311. ifsd_m[pidx] = m_head;
  3312. /*
  3313. * XXX assumes a 1 to 1 relationship between segments and
  3314. * descriptors - this does not hold true on all drivers, e.g.
  3315. * cxgb
  3316. */
  3317. if (__predict_false(nsegs + 2 > TXQ_AVAIL(txq))) {
  3318. txq->ift_no_desc_avail++;
  3319. bus_dmamap_unload(buf_tag, map);
  3320. DBG_COUNTER_INC(encap_txq_avail_fail);
  3321. DBG_COUNTER_INC(encap_txd_encap_fail);
  3322. if ((txq->ift_task.gt_task.ta_flags & TASK_ENQUEUED) == 0)
  3323. GROUPTASK_ENQUEUE(&txq->ift_task);
  3324. return (ENOBUFS);
  3325. }
  3326. /*
  3327. * On Intel cards we can greatly reduce the number of TX interrupts
  3328. * we see by only setting report status on every Nth descriptor.
  3329. * However, this also means that the driver will need to keep track
  3330. * of the descriptors that RS was set on to check them for the DD bit.
  3331. */
  3332. txq->ift_rs_pending += nsegs + 1;
  3333. if (txq->ift_rs_pending > TXQ_MAX_RS_DEFERRED(txq) ||
  3334. iflib_no_tx_batch || (TXQ_AVAIL(txq) - nsegs) <= MAX_TX_DESC(ctx) + 2) {
  3335. pi.ipi_flags |= IPI_TX_INTR;
  3336. txq->ift_rs_pending = 0;
  3337. }
  3338. pi.ipi_segs = segs;
  3339. pi.ipi_nsegs = nsegs;
  3340. MPASS(pidx >= 0 && pidx < txq->ift_size);
  3341. #ifdef PKT_DEBUG
  3342. print_pkt(&pi);
  3343. #endif
  3344. if ((err = ctx->isc_txd_encap(ctx->ifc_softc, &pi)) == 0) {
  3345. bus_dmamap_sync(buf_tag, map, BUS_DMASYNC_PREWRITE);
  3346. DBG_COUNTER_INC(tx_encap);
  3347. MPASS(pi.ipi_new_pidx < txq->ift_size);
  3348. ndesc = pi.ipi_new_pidx - pi.ipi_pidx;
  3349. if (pi.ipi_new_pidx < pi.ipi_pidx) {
  3350. ndesc += txq->ift_size;
  3351. txq->ift_gen = 1;
  3352. }
  3353. /*
  3354. * drivers can need as many as
  3355. * two sentinels
  3356. */
  3357. MPASS(ndesc <= pi.ipi_nsegs + 2);
  3358. MPASS(pi.ipi_new_pidx != pidx);
  3359. MPASS(ndesc > 0);
  3360. txq->ift_in_use += ndesc;
  3361. txq->ift_db_pending += ndesc;
  3362. /*
  3363. * We update the last software descriptor again here because there may
  3364. * be a sentinel and/or there may be more mbufs than segments
  3365. */
  3366. txq->ift_pidx = pi.ipi_new_pidx;
  3367. txq->ift_npending += pi.ipi_ndescs;
  3368. } else {
  3369. *m_headp = m_head = iflib_remove_mbuf(txq);
  3370. if (err == EFBIG) {
  3371. txq->ift_txd_encap_efbig++;
  3372. if (remap < 2) {
  3373. remap = 1;
  3374. goto defrag;
  3375. }
  3376. }
  3377. goto defrag_failed;
  3378. }
  3379. /*
  3380. * err can't possibly be non-zero here, so we don't neet to test it
  3381. * to see if we need to DBG_COUNTER_INC(encap_txd_encap_fail).
  3382. */
  3383. return (err);
  3384. defrag_failed:
  3385. txq->ift_mbuf_defrag_failed++;
  3386. txq->ift_map_failed++;
  3387. m_freem(*m_headp);
  3388. DBG_COUNTER_INC(tx_frees);
  3389. *m_headp = NULL;
  3390. DBG_COUNTER_INC(encap_txd_encap_fail);
  3391. return (ENOMEM);
  3392. }
  3393. static void
  3394. iflib_tx_desc_free(iflib_txq_t txq, int n)
  3395. {
  3396. uint32_t qsize, cidx, mask, gen;
  3397. struct mbuf *m, **ifsd_m;
  3398. bool do_prefetch;
  3399. cidx = txq->ift_cidx;
  3400. gen = txq->ift_gen;
  3401. qsize = txq->ift_size;
  3402. mask = qsize-1;
  3403. ifsd_m = txq->ift_sds.ifsd_m;
  3404. do_prefetch = (txq->ift_ctx->ifc_flags & IFC_PREFETCH);
  3405. while (n-- > 0) {
  3406. if (do_prefetch) {
  3407. prefetch(ifsd_m[(cidx + 3) & mask]);
  3408. prefetch(ifsd_m[(cidx + 4) & mask]);
  3409. }
  3410. if ((m = ifsd_m[cidx]) != NULL) {
  3411. prefetch(&ifsd_m[(cidx + CACHE_PTR_INCREMENT) & mask]);
  3412. if (m->m_pkthdr.csum_flags & CSUM_TSO) {
  3413. bus_dmamap_sync(txq->ift_tso_buf_tag,
  3414. txq->ift_sds.ifsd_tso_map[cidx],
  3415. BUS_DMASYNC_POSTWRITE);
  3416. bus_dmamap_unload(txq->ift_tso_buf_tag,
  3417. txq->ift_sds.ifsd_tso_map[cidx]);
  3418. } else {
  3419. bus_dmamap_sync(txq->ift_buf_tag,
  3420. txq->ift_sds.ifsd_map[cidx],
  3421. BUS_DMASYNC_POSTWRITE);
  3422. bus_dmamap_unload(txq->ift_buf_tag,
  3423. txq->ift_sds.ifsd_map[cidx]);
  3424. }
  3425. /* XXX we don't support any drivers that batch packets yet */
  3426. MPASS(m->m_nextpkt == NULL);
  3427. m_freem(m);
  3428. ifsd_m[cidx] = NULL;
  3429. #if MEMORY_LOGGING
  3430. txq->ift_dequeued++;
  3431. #endif
  3432. DBG_COUNTER_INC(tx_frees);
  3433. }
  3434. if (__predict_false(++cidx == qsize)) {
  3435. cidx = 0;
  3436. gen = 0;
  3437. }
  3438. }
  3439. txq->ift_cidx = cidx;
  3440. txq->ift_gen = gen;
  3441. }
  3442. static __inline int
  3443. iflib_completed_tx_reclaim(iflib_txq_t txq, int thresh)
  3444. {
  3445. int reclaim;
  3446. if_ctx_t ctx = txq->ift_ctx;
  3447. KASSERT(thresh >= 0, ("invalid threshold to reclaim"));
  3448. MPASS(thresh /*+ MAX_TX_DESC(txq->ift_ctx) */ < txq->ift_size);
  3449. /*
  3450. * Need a rate-limiting check so that this isn't called every time
  3451. */
  3452. iflib_tx_credits_update(ctx, txq);
  3453. reclaim = DESC_RECLAIMABLE(txq);
  3454. if (reclaim <= thresh /* + MAX_TX_DESC(txq->ift_ctx) */) {
  3455. #ifdef INVARIANTS
  3456. if (iflib_verbose_debug) {
  3457. printf("%s processed=%ju cleaned=%ju tx_nsegments=%d reclaim=%d thresh=%d\n", __FUNCTION__,
  3458. txq->ift_processed, txq->ift_cleaned, txq->ift_ctx->ifc_softc_ctx.isc_tx_nsegments,
  3459. reclaim, thresh);
  3460. }
  3461. #endif
  3462. return (0);
  3463. }
  3464. iflib_tx_desc_free(txq, reclaim);
  3465. txq->ift_cleaned += reclaim;
  3466. txq->ift_in_use -= reclaim;
  3467. return (reclaim);
  3468. }
  3469. static struct mbuf **
  3470. _ring_peek_one(struct ifmp_ring *r, int cidx, int offset, int remaining)
  3471. {
  3472. int next, size;
  3473. struct mbuf **items;
  3474. size = r->size;
  3475. next = (cidx + CACHE_PTR_INCREMENT) & (size-1);
  3476. items = __DEVOLATILE(struct mbuf **, &r->items[0]);
  3477. prefetch(items[(cidx + offset) & (size-1)]);
  3478. if (remaining > 1) {
  3479. prefetch2cachelines(&items[next]);
  3480. prefetch2cachelines(items[(cidx + offset + 1) & (size-1)]);
  3481. prefetch2cachelines(items[(cidx + offset + 2) & (size-1)]);
  3482. prefetch2cachelines(items[(cidx + offset + 3) & (size-1)]);
  3483. }
  3484. return (__DEVOLATILE(struct mbuf **, &r->items[(cidx + offset) & (size-1)]));
  3485. }
  3486. static void
  3487. iflib_txq_check_drain(iflib_txq_t txq, int budget)
  3488. {
  3489. ifmp_ring_check_drainage(txq->ift_br, budget);
  3490. }
  3491. static uint32_t
  3492. iflib_txq_can_drain(struct ifmp_ring *r)
  3493. {
  3494. iflib_txq_t txq = r->cookie;
  3495. if_ctx_t ctx = txq->ift_ctx;
  3496. if (TXQ_AVAIL(txq) > MAX_TX_DESC(ctx) + 2)
  3497. return (1);
  3498. bus_dmamap_sync(txq->ift_ifdi->idi_tag, txq->ift_ifdi->idi_map,
  3499. BUS_DMASYNC_POSTREAD);
  3500. return (ctx->isc_txd_credits_update(ctx->ifc_softc, txq->ift_id,
  3501. false));
  3502. }
  3503. static uint32_t
  3504. iflib_txq_drain(struct ifmp_ring *r, uint32_t cidx, uint32_t pidx)
  3505. {
  3506. iflib_txq_t txq = r->cookie;
  3507. if_ctx_t ctx = txq->ift_ctx;
  3508. if_t ifp = ctx->ifc_ifp;
  3509. struct mbuf *m, **mp;
  3510. int avail, bytes_sent, skipped, count, err, i;
  3511. int mcast_sent, pkt_sent, reclaimed;
  3512. bool do_prefetch, rang, ring;
  3513. if (__predict_false(!(if_getdrvflags(ifp) & IFF_DRV_RUNNING) ||
  3514. !LINK_ACTIVE(ctx))) {
  3515. DBG_COUNTER_INC(txq_drain_notready);
  3516. return (0);
  3517. }
  3518. reclaimed = iflib_completed_tx_reclaim(txq, RECLAIM_THRESH(ctx));
  3519. rang = iflib_txd_db_check(txq, reclaimed && txq->ift_db_pending);
  3520. avail = IDXDIFF(pidx, cidx, r->size);
  3521. if (__predict_false(ctx->ifc_flags & IFC_QFLUSH)) {
  3522. /*
  3523. * The driver is unloading so we need to free all pending packets.
  3524. */
  3525. DBG_COUNTER_INC(txq_drain_flushing);
  3526. for (i = 0; i < avail; i++) {
  3527. if (__predict_true(r->items[(cidx + i) & (r->size-1)] != (void *)txq))
  3528. m_freem(r->items[(cidx + i) & (r->size-1)]);
  3529. r->items[(cidx + i) & (r->size-1)] = NULL;
  3530. }
  3531. return (avail);
  3532. }
  3533. if (__predict_false(if_getdrvflags(ctx->ifc_ifp) & IFF_DRV_OACTIVE)) {
  3534. txq->ift_qstatus = IFLIB_QUEUE_IDLE;
  3535. CALLOUT_LOCK(txq);
  3536. callout_stop(&txq->ift_timer);
  3537. CALLOUT_UNLOCK(txq);
  3538. DBG_COUNTER_INC(txq_drain_oactive);
  3539. return (0);
  3540. }
  3541. /*
  3542. * If we've reclaimed any packets this queue cannot be hung.
  3543. */
  3544. if (reclaimed)
  3545. txq->ift_qstatus = IFLIB_QUEUE_IDLE;
  3546. skipped = mcast_sent = bytes_sent = pkt_sent = 0;
  3547. count = MIN(avail, TX_BATCH_SIZE);
  3548. #ifdef INVARIANTS
  3549. if (iflib_verbose_debug)
  3550. printf("%s avail=%d ifc_flags=%x txq_avail=%d ", __FUNCTION__,
  3551. avail, ctx->ifc_flags, TXQ_AVAIL(txq));
  3552. #endif
  3553. do_prefetch = (ctx->ifc_flags & IFC_PREFETCH);
  3554. err = 0;
  3555. for (i = 0; i < count && TXQ_AVAIL(txq) >= MAX_TX_DESC(ctx) + 2; i++) {
  3556. int rem = do_prefetch ? count - i : 0;
  3557. mp = _ring_peek_one(r, cidx, i, rem);
  3558. MPASS(mp != NULL && *mp != NULL);
  3559. /*
  3560. * Completion interrupts will use the address of the txq
  3561. * as a sentinel to enqueue _something_ in order to acquire
  3562. * the lock on the mp_ring (there's no direct lock call).
  3563. * We obviously whave to check for these sentinel cases
  3564. * and skip them.
  3565. */
  3566. if (__predict_false(*mp == (struct mbuf *)txq)) {
  3567. skipped++;
  3568. continue;
  3569. }
  3570. err = iflib_encap(txq, mp);
  3571. if (__predict_false(err)) {
  3572. /* no room - bail out */
  3573. if (err == ENOBUFS)
  3574. break;
  3575. skipped++;
  3576. /* we can't send this packet - skip it */
  3577. continue;
  3578. }
  3579. pkt_sent++;
  3580. m = *mp;
  3581. DBG_COUNTER_INC(tx_sent);
  3582. bytes_sent += m->m_pkthdr.len;
  3583. mcast_sent += !!(m->m_flags & M_MCAST);
  3584. if (__predict_false(!(if_getdrvflags(ifp) & IFF_DRV_RUNNING)))
  3585. break;
  3586. ETHER_BPF_MTAP(ifp, m);
  3587. rang = iflib_txd_db_check(txq, false);
  3588. }
  3589. /* deliberate use of bitwise or to avoid gratuitous short-circuit */
  3590. ring = rang ? false : (iflib_min_tx_latency | err);
  3591. iflib_txd_db_check(txq, ring);
  3592. if_inc_counter(ifp, IFCOUNTER_OBYTES, bytes_sent);
  3593. if_inc_counter(ifp, IFCOUNTER_OPACKETS, pkt_sent);
  3594. if (mcast_sent)
  3595. if_inc_counter(ifp, IFCOUNTER_OMCASTS, mcast_sent);
  3596. #ifdef INVARIANTS
  3597. if (iflib_verbose_debug)
  3598. printf("consumed=%d\n", skipped + pkt_sent);
  3599. #endif
  3600. return (skipped + pkt_sent);
  3601. }
  3602. static uint32_t
  3603. iflib_txq_drain_always(struct ifmp_ring *r)
  3604. {
  3605. return (1);
  3606. }
  3607. static uint32_t
  3608. iflib_txq_drain_free(struct ifmp_ring *r, uint32_t cidx, uint32_t pidx)
  3609. {
  3610. int i, avail;
  3611. struct mbuf **mp;
  3612. iflib_txq_t txq;
  3613. txq = r->cookie;
  3614. txq->ift_qstatus = IFLIB_QUEUE_IDLE;
  3615. CALLOUT_LOCK(txq);
  3616. callout_stop(&txq->ift_timer);
  3617. CALLOUT_UNLOCK(txq);
  3618. avail = IDXDIFF(pidx, cidx, r->size);
  3619. for (i = 0; i < avail; i++) {
  3620. mp = _ring_peek_one(r, cidx, i, avail - i);
  3621. if (__predict_false(*mp == (struct mbuf *)txq))
  3622. continue;
  3623. m_freem(*mp);
  3624. DBG_COUNTER_INC(tx_frees);
  3625. }
  3626. MPASS(ifmp_ring_is_stalled(r) == 0);
  3627. return (avail);
  3628. }
  3629. static void
  3630. iflib_ifmp_purge(iflib_txq_t txq)
  3631. {
  3632. struct ifmp_ring *r;
  3633. r = txq->ift_br;
  3634. r->drain = iflib_txq_drain_free;
  3635. r->can_drain = iflib_txq_drain_always;
  3636. ifmp_ring_check_drainage(r, r->size);
  3637. r->drain = iflib_txq_drain;
  3638. r->can_drain = iflib_txq_can_drain;
  3639. }
  3640. static void
  3641. _task_fn_tx(void *context)
  3642. {
  3643. iflib_txq_t txq = context;
  3644. if_ctx_t ctx = txq->ift_ctx;
  3645. if_t ifp = ctx->ifc_ifp;
  3646. int abdicate = ctx->ifc_sysctl_tx_abdicate;
  3647. #ifdef IFLIB_DIAGNOSTICS
  3648. txq->ift_cpu_exec_count[curcpu]++;
  3649. #endif
  3650. if (!(if_getdrvflags(ifp) & IFF_DRV_RUNNING))
  3651. return;
  3652. #ifdef DEV_NETMAP
  3653. if ((if_getcapenable(ifp) & IFCAP_NETMAP) &&
  3654. netmap_tx_irq(ifp, txq->ift_id))
  3655. goto skip_ifmp;
  3656. #endif
  3657. #ifdef ALTQ
  3658. if (if_altq_is_enabled(ifp))
  3659. iflib_altq_if_start(ifp);
  3660. #endif
  3661. if (txq->ift_db_pending)
  3662. ifmp_ring_enqueue(txq->ift_br, (void **)&txq, 1, TX_BATCH_SIZE, abdicate);
  3663. else if (!abdicate)
  3664. ifmp_ring_check_drainage(txq->ift_br, TX_BATCH_SIZE);
  3665. /*
  3666. * When abdicating, we always need to check drainage, not just when we don't enqueue
  3667. */
  3668. if (abdicate)
  3669. ifmp_ring_check_drainage(txq->ift_br, TX_BATCH_SIZE);
  3670. #ifdef DEV_NETMAP
  3671. skip_ifmp:
  3672. #endif
  3673. if (ctx->ifc_flags & IFC_LEGACY)
  3674. IFDI_INTR_ENABLE(ctx);
  3675. else
  3676. IFDI_TX_QUEUE_INTR_ENABLE(ctx, txq->ift_id);
  3677. }
  3678. static void
  3679. _task_fn_rx(void *context)
  3680. {
  3681. iflib_rxq_t rxq = context;
  3682. if_ctx_t ctx = rxq->ifr_ctx;
  3683. uint8_t more;
  3684. uint16_t budget;
  3685. #ifdef DEV_NETMAP
  3686. u_int work = 0;
  3687. int nmirq;
  3688. #endif
  3689. #ifdef IFLIB_DIAGNOSTICS
  3690. rxq->ifr_cpu_exec_count[curcpu]++;
  3691. #endif
  3692. DBG_COUNTER_INC(task_fn_rxs);
  3693. if (__predict_false(!(if_getdrvflags(ctx->ifc_ifp) & IFF_DRV_RUNNING)))
  3694. return;
  3695. #ifdef DEV_NETMAP
  3696. nmirq = netmap_rx_irq(ctx->ifc_ifp, rxq->ifr_id, &work);
  3697. if (nmirq != NM_IRQ_PASS) {
  3698. more = (nmirq == NM_IRQ_RESCHED) ? IFLIB_RXEOF_MORE : 0;
  3699. goto skip_rxeof;
  3700. }
  3701. #endif
  3702. budget = ctx->ifc_sysctl_rx_budget;
  3703. if (budget == 0)
  3704. budget = 16; /* XXX */
  3705. more = iflib_rxeof(rxq, budget);
  3706. #ifdef DEV_NETMAP
  3707. skip_rxeof:
  3708. #endif
  3709. if ((more & IFLIB_RXEOF_MORE) == 0) {
  3710. if (ctx->ifc_flags & IFC_LEGACY)
  3711. IFDI_INTR_ENABLE(ctx);
  3712. else
  3713. IFDI_RX_QUEUE_INTR_ENABLE(ctx, rxq->ifr_id);
  3714. DBG_COUNTER_INC(rx_intr_enables);
  3715. }
  3716. if (__predict_false(!(if_getdrvflags(ctx->ifc_ifp) & IFF_DRV_RUNNING)))
  3717. return;
  3718. if (more & IFLIB_RXEOF_MORE)
  3719. GROUPTASK_ENQUEUE(&rxq->ifr_task);
  3720. else if (more & IFLIB_RXEOF_EMPTY)
  3721. callout_reset_curcpu(&rxq->ifr_watchdog, 1, &_task_fn_rx_watchdog, rxq);
  3722. }
  3723. static void
  3724. _task_fn_admin(void *context)
  3725. {
  3726. if_ctx_t ctx = context;
  3727. if_softc_ctx_t sctx = &ctx->ifc_softc_ctx;
  3728. iflib_txq_t txq;
  3729. int i;
  3730. bool oactive, running, do_reset, do_watchdog, in_detach;
  3731. STATE_LOCK(ctx);
  3732. running = (if_getdrvflags(ctx->ifc_ifp) & IFF_DRV_RUNNING);
  3733. oactive = (if_getdrvflags(ctx->ifc_ifp) & IFF_DRV_OACTIVE);
  3734. do_reset = (ctx->ifc_flags & IFC_DO_RESET);
  3735. do_watchdog = (ctx->ifc_flags & IFC_DO_WATCHDOG);
  3736. in_detach = (ctx->ifc_flags & IFC_IN_DETACH);
  3737. ctx->ifc_flags &= ~(IFC_DO_RESET|IFC_DO_WATCHDOG);
  3738. STATE_UNLOCK(ctx);
  3739. if ((!running && !oactive) && !(ctx->ifc_sctx->isc_flags & IFLIB_ADMIN_ALWAYS_RUN))
  3740. return;
  3741. if (in_detach)
  3742. return;
  3743. CTX_LOCK(ctx);
  3744. for (txq = ctx->ifc_txqs, i = 0; i < sctx->isc_ntxqsets; i++, txq++) {
  3745. CALLOUT_LOCK(txq);
  3746. callout_stop(&txq->ift_timer);
  3747. CALLOUT_UNLOCK(txq);
  3748. }
  3749. if (ctx->ifc_sctx->isc_flags & IFLIB_HAS_ADMINCQ)
  3750. IFDI_ADMIN_COMPLETION_HANDLE(ctx);
  3751. if (do_watchdog) {
  3752. ctx->ifc_watchdog_events++;
  3753. IFDI_WATCHDOG_RESET(ctx);
  3754. }
  3755. IFDI_UPDATE_ADMIN_STATUS(ctx);
  3756. for (txq = ctx->ifc_txqs, i = 0; i < sctx->isc_ntxqsets; i++, txq++) {
  3757. callout_reset_on(&txq->ift_timer, iflib_timer_default, iflib_timer, txq,
  3758. txq->ift_timer.c_cpu);
  3759. }
  3760. IFDI_LINK_INTR_ENABLE(ctx);
  3761. if (do_reset)
  3762. iflib_if_init_locked(ctx);
  3763. CTX_UNLOCK(ctx);
  3764. if (LINK_ACTIVE(ctx) == 0)
  3765. return;
  3766. for (txq = ctx->ifc_txqs, i = 0; i < sctx->isc_ntxqsets; i++, txq++)
  3767. iflib_txq_check_drain(txq, IFLIB_RESTART_BUDGET);
  3768. }
  3769. static void
  3770. _task_fn_iov(void *context)
  3771. {
  3772. if_ctx_t ctx = context;
  3773. if (!(if_getdrvflags(ctx->ifc_ifp) & IFF_DRV_RUNNING) &&
  3774. !(ctx->ifc_sctx->isc_flags & IFLIB_ADMIN_ALWAYS_RUN))
  3775. return;
  3776. CTX_LOCK(ctx);
  3777. IFDI_VFLR_HANDLE(ctx);
  3778. CTX_UNLOCK(ctx);
  3779. }
  3780. static int
  3781. iflib_sysctl_int_delay(SYSCTL_HANDLER_ARGS)
  3782. {
  3783. int err;
  3784. if_int_delay_info_t info;
  3785. if_ctx_t ctx;
  3786. info = (if_int_delay_info_t)arg1;
  3787. ctx = info->iidi_ctx;
  3788. info->iidi_req = req;
  3789. info->iidi_oidp = oidp;
  3790. CTX_LOCK(ctx);
  3791. err = IFDI_SYSCTL_INT_DELAY(ctx, info);
  3792. CTX_UNLOCK(ctx);
  3793. return (err);
  3794. }
  3795. /*********************************************************************
  3796. *
  3797. * IFNET FUNCTIONS
  3798. *
  3799. **********************************************************************/
  3800. static void
  3801. iflib_if_init_locked(if_ctx_t ctx)
  3802. {
  3803. iflib_stop(ctx);
  3804. iflib_init_locked(ctx);
  3805. }
  3806. static void
  3807. iflib_if_init(void *arg)
  3808. {
  3809. if_ctx_t ctx = arg;
  3810. CTX_LOCK(ctx);
  3811. iflib_if_init_locked(ctx);
  3812. CTX_UNLOCK(ctx);
  3813. }
  3814. static int
  3815. iflib_if_transmit(if_t ifp, struct mbuf *m)
  3816. {
  3817. if_ctx_t ctx = if_getsoftc(ifp);
  3818. iflib_txq_t txq;
  3819. int err, qidx;
  3820. int abdicate;
  3821. if (__predict_false((if_getdrvflags(ifp) & IFF_DRV_RUNNING) == 0 || !LINK_ACTIVE(ctx))) {
  3822. DBG_COUNTER_INC(tx_frees);
  3823. m_freem(m);
  3824. return (ENETDOWN);
  3825. }
  3826. MPASS(m->m_nextpkt == NULL);
  3827. /* ALTQ-enabled interfaces always use queue 0. */
  3828. qidx = 0;
  3829. /* Use driver-supplied queue selection method if it exists */
  3830. if (ctx->isc_txq_select_v2) {
  3831. struct if_pkt_info pi;
  3832. uint64_t early_pullups = 0;
  3833. pkt_info_zero(&pi);
  3834. err = iflib_parse_header_partial(&pi, &m, &early_pullups);
  3835. if (__predict_false(err != 0)) {
  3836. /* Assign pullups for bad pkts to default queue */
  3837. ctx->ifc_txqs[0].ift_pullups += early_pullups;
  3838. DBG_COUNTER_INC(encap_txd_encap_fail);
  3839. return (err);
  3840. }
  3841. /* Let driver make queueing decision */
  3842. qidx = ctx->isc_txq_select_v2(ctx->ifc_softc, m, &pi);
  3843. ctx->ifc_txqs[qidx].ift_pullups += early_pullups;
  3844. }
  3845. /* Backwards compatibility w/ simpler queue select */
  3846. else if (ctx->isc_txq_select)
  3847. qidx = ctx->isc_txq_select(ctx->ifc_softc, m);
  3848. /* If not, use iflib's standard method */
  3849. else if ((NTXQSETS(ctx) > 1) && M_HASHTYPE_GET(m) && !if_altq_is_enabled(ifp))
  3850. qidx = QIDX(ctx, m);
  3851. /* Set TX queue */
  3852. txq = &ctx->ifc_txqs[qidx];
  3853. #ifdef DRIVER_BACKPRESSURE
  3854. if (txq->ift_closed) {
  3855. while (m != NULL) {
  3856. next = m->m_nextpkt;
  3857. m->m_nextpkt = NULL;
  3858. m_freem(m);
  3859. DBG_COUNTER_INC(tx_frees);
  3860. m = next;
  3861. }
  3862. return (ENOBUFS);
  3863. }
  3864. #endif
  3865. #ifdef notyet
  3866. qidx = count = 0;
  3867. mp = marr;
  3868. next = m;
  3869. do {
  3870. count++;
  3871. next = next->m_nextpkt;
  3872. } while (next != NULL);
  3873. if (count > nitems(marr))
  3874. if ((mp = malloc(count*sizeof(struct mbuf *), M_IFLIB, M_NOWAIT)) == NULL) {
  3875. /* XXX check nextpkt */
  3876. m_freem(m);
  3877. /* XXX simplify for now */
  3878. DBG_COUNTER_INC(tx_frees);
  3879. return (ENOBUFS);
  3880. }
  3881. for (next = m, i = 0; next != NULL; i++) {
  3882. mp[i] = next;
  3883. next = next->m_nextpkt;
  3884. mp[i]->m_nextpkt = NULL;
  3885. }
  3886. #endif
  3887. DBG_COUNTER_INC(tx_seen);
  3888. abdicate = ctx->ifc_sysctl_tx_abdicate;
  3889. err = ifmp_ring_enqueue(txq->ift_br, (void **)&m, 1, TX_BATCH_SIZE, abdicate);
  3890. if (abdicate)
  3891. GROUPTASK_ENQUEUE(&txq->ift_task);
  3892. if (err) {
  3893. if (!abdicate)
  3894. GROUPTASK_ENQUEUE(&txq->ift_task);
  3895. /* support forthcoming later */
  3896. #ifdef DRIVER_BACKPRESSURE
  3897. txq->ift_closed = TRUE;
  3898. #endif
  3899. ifmp_ring_check_drainage(txq->ift_br, TX_BATCH_SIZE);
  3900. m_freem(m);
  3901. DBG_COUNTER_INC(tx_frees);
  3902. }
  3903. return (err);
  3904. }
  3905. #ifdef ALTQ
  3906. /*
  3907. * The overall approach to integrating iflib with ALTQ is to continue to use
  3908. * the iflib mp_ring machinery between the ALTQ queue(s) and the hardware
  3909. * ring. Technically, when using ALTQ, queueing to an intermediate mp_ring
  3910. * is redundant/unnecessary, but doing so minimizes the amount of
  3911. * ALTQ-specific code required in iflib. It is assumed that the overhead of
  3912. * redundantly queueing to an intermediate mp_ring is swamped by the
  3913. * performance limitations inherent in using ALTQ.
  3914. *
  3915. * When ALTQ support is compiled in, all iflib drivers will use a transmit
  3916. * routine, iflib_altq_if_transmit(), that checks if ALTQ is enabled for the
  3917. * given interface. If ALTQ is enabled for an interface, then all
  3918. * transmitted packets for that interface will be submitted to the ALTQ
  3919. * subsystem via IFQ_ENQUEUE(). We don't use the legacy if_transmit()
  3920. * implementation because it uses IFQ_HANDOFF(), which will duplicatively
  3921. * update stats that the iflib machinery handles, and which is sensitve to
  3922. * the disused IFF_DRV_OACTIVE flag. Additionally, iflib_altq_if_start()
  3923. * will be installed as the start routine for use by ALTQ facilities that
  3924. * need to trigger queue drains on a scheduled basis.
  3925. *
  3926. */
  3927. static void
  3928. iflib_altq_if_start(if_t ifp)
  3929. {
  3930. struct ifaltq *ifq = &ifp->if_snd; /* XXX - DRVAPI */
  3931. struct mbuf *m;
  3932. IFQ_LOCK(ifq);
  3933. IFQ_DEQUEUE_NOLOCK(ifq, m);
  3934. while (m != NULL) {
  3935. iflib_if_transmit(ifp, m);
  3936. IFQ_DEQUEUE_NOLOCK(ifq, m);
  3937. }
  3938. IFQ_UNLOCK(ifq);
  3939. }
  3940. static int
  3941. iflib_altq_if_transmit(if_t ifp, struct mbuf *m)
  3942. {
  3943. int err;
  3944. if (if_altq_is_enabled(ifp)) {
  3945. IFQ_ENQUEUE(&ifp->if_snd, m, err); /* XXX - DRVAPI */
  3946. if (err == 0)
  3947. iflib_altq_if_start(ifp);
  3948. } else
  3949. err = iflib_if_transmit(ifp, m);
  3950. return (err);
  3951. }
  3952. #endif /* ALTQ */
  3953. static void
  3954. iflib_if_qflush(if_t ifp)
  3955. {
  3956. if_ctx_t ctx = if_getsoftc(ifp);
  3957. iflib_txq_t txq = ctx->ifc_txqs;
  3958. int i;
  3959. STATE_LOCK(ctx);
  3960. ctx->ifc_flags |= IFC_QFLUSH;
  3961. STATE_UNLOCK(ctx);
  3962. for (i = 0; i < NTXQSETS(ctx); i++, txq++)
  3963. while (!(ifmp_ring_is_idle(txq->ift_br) || ifmp_ring_is_stalled(txq->ift_br)))
  3964. iflib_txq_check_drain(txq, 0);
  3965. STATE_LOCK(ctx);
  3966. ctx->ifc_flags &= ~IFC_QFLUSH;
  3967. STATE_UNLOCK(ctx);
  3968. /*
  3969. * When ALTQ is enabled, this will also take care of purging the
  3970. * ALTQ queue(s).
  3971. */
  3972. if_qflush(ifp);
  3973. }
  3974. #define IFCAP_FLAGS (IFCAP_HWCSUM_IPV6 | IFCAP_HWCSUM | IFCAP_LRO | \
  3975. IFCAP_TSO | IFCAP_VLAN_HWTAGGING | IFCAP_HWSTATS | \
  3976. IFCAP_VLAN_MTU | IFCAP_VLAN_HWFILTER | \
  3977. IFCAP_VLAN_HWTSO | IFCAP_VLAN_HWCSUM | IFCAP_MEXTPG)
  3978. static int
  3979. iflib_if_ioctl(if_t ifp, u_long command, caddr_t data)
  3980. {
  3981. if_ctx_t ctx = if_getsoftc(ifp);
  3982. struct ifreq *ifr = (struct ifreq *)data;
  3983. #if defined(INET) || defined(INET6)
  3984. struct ifaddr *ifa = (struct ifaddr *)data;
  3985. #endif
  3986. bool avoid_reset = false;
  3987. int err = 0, reinit = 0, bits;
  3988. switch (command) {
  3989. case SIOCSIFADDR:
  3990. #ifdef INET
  3991. if (ifa->ifa_addr->sa_family == AF_INET)
  3992. avoid_reset = true;
  3993. #endif
  3994. #ifdef INET6
  3995. if (ifa->ifa_addr->sa_family == AF_INET6)
  3996. avoid_reset = true;
  3997. #endif
  3998. /*
  3999. ** Calling init results in link renegotiation,
  4000. ** so we avoid doing it when possible.
  4001. */
  4002. if (avoid_reset) {
  4003. if_setflagbits(ifp, IFF_UP,0);
  4004. if (!(if_getdrvflags(ifp) & IFF_DRV_RUNNING))
  4005. reinit = 1;
  4006. #ifdef INET
  4007. if (!(if_getflags(ifp) & IFF_NOARP))
  4008. arp_ifinit(ifp, ifa);
  4009. #endif
  4010. } else
  4011. err = ether_ioctl(ifp, command, data);
  4012. break;
  4013. case SIOCSIFMTU:
  4014. CTX_LOCK(ctx);
  4015. if (ifr->ifr_mtu == if_getmtu(ifp)) {
  4016. CTX_UNLOCK(ctx);
  4017. break;
  4018. }
  4019. bits = if_getdrvflags(ifp);
  4020. /* stop the driver and free any clusters before proceeding */
  4021. iflib_stop(ctx);
  4022. if ((err = IFDI_MTU_SET(ctx, ifr->ifr_mtu)) == 0) {
  4023. STATE_LOCK(ctx);
  4024. if (ifr->ifr_mtu > ctx->ifc_max_fl_buf_size)
  4025. ctx->ifc_flags |= IFC_MULTISEG;
  4026. else
  4027. ctx->ifc_flags &= ~IFC_MULTISEG;
  4028. STATE_UNLOCK(ctx);
  4029. err = if_setmtu(ifp, ifr->ifr_mtu);
  4030. }
  4031. iflib_init_locked(ctx);
  4032. STATE_LOCK(ctx);
  4033. if_setdrvflags(ifp, bits);
  4034. STATE_UNLOCK(ctx);
  4035. CTX_UNLOCK(ctx);
  4036. break;
  4037. case SIOCSIFFLAGS:
  4038. CTX_LOCK(ctx);
  4039. if (if_getflags(ifp) & IFF_UP) {
  4040. if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) {
  4041. if ((if_getflags(ifp) ^ ctx->ifc_if_flags) &
  4042. (IFF_PROMISC | IFF_ALLMULTI)) {
  4043. CTX_UNLOCK(ctx);
  4044. err = IFDI_PROMISC_SET(ctx, if_getflags(ifp));
  4045. CTX_LOCK(ctx);
  4046. }
  4047. } else
  4048. reinit = 1;
  4049. } else if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) {
  4050. iflib_stop(ctx);
  4051. }
  4052. ctx->ifc_if_flags = if_getflags(ifp);
  4053. CTX_UNLOCK(ctx);
  4054. break;
  4055. case SIOCADDMULTI:
  4056. case SIOCDELMULTI:
  4057. if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) {
  4058. CTX_LOCK(ctx);
  4059. IFDI_INTR_DISABLE(ctx);
  4060. IFDI_MULTI_SET(ctx);
  4061. IFDI_INTR_ENABLE(ctx);
  4062. CTX_UNLOCK(ctx);
  4063. }
  4064. break;
  4065. case SIOCSIFMEDIA:
  4066. CTX_LOCK(ctx);
  4067. IFDI_MEDIA_SET(ctx);
  4068. CTX_UNLOCK(ctx);
  4069. /* FALLTHROUGH */
  4070. case SIOCGIFMEDIA:
  4071. case SIOCGIFXMEDIA:
  4072. err = ifmedia_ioctl(ifp, ifr, ctx->ifc_mediap, command);
  4073. break;
  4074. case SIOCGI2C:
  4075. {
  4076. struct ifi2creq i2c;
  4077. err = copyin(ifr_data_get_ptr(ifr), &i2c, sizeof(i2c));
  4078. if (err != 0)
  4079. break;
  4080. if (i2c.dev_addr != 0xA0 && i2c.dev_addr != 0xA2) {
  4081. err = EINVAL;
  4082. break;
  4083. }
  4084. if (i2c.len > sizeof(i2c.data)) {
  4085. err = EINVAL;
  4086. break;
  4087. }
  4088. if ((err = IFDI_I2C_REQ(ctx, &i2c)) == 0)
  4089. err = copyout(&i2c, ifr_data_get_ptr(ifr),
  4090. sizeof(i2c));
  4091. break;
  4092. }
  4093. case SIOCSIFCAP:
  4094. {
  4095. int mask, setmask, oldmask;
  4096. oldmask = if_getcapenable(ifp);
  4097. mask = ifr->ifr_reqcap ^ oldmask;
  4098. mask &= ctx->ifc_softc_ctx.isc_capabilities | IFCAP_MEXTPG;
  4099. setmask = 0;
  4100. #ifdef TCP_OFFLOAD
  4101. setmask |= mask & (IFCAP_TOE4|IFCAP_TOE6);
  4102. #endif
  4103. setmask |= (mask & IFCAP_FLAGS);
  4104. setmask |= (mask & IFCAP_WOL);
  4105. /*
  4106. * If any RX csum has changed, change all the ones that
  4107. * are supported by the driver.
  4108. */
  4109. if (setmask & (IFCAP_RXCSUM | IFCAP_RXCSUM_IPV6)) {
  4110. setmask |= ctx->ifc_softc_ctx.isc_capabilities &
  4111. (IFCAP_RXCSUM | IFCAP_RXCSUM_IPV6);
  4112. }
  4113. /*
  4114. * want to ensure that traffic has stopped before we change any of the flags
  4115. */
  4116. if (setmask) {
  4117. CTX_LOCK(ctx);
  4118. bits = if_getdrvflags(ifp);
  4119. if (bits & IFF_DRV_RUNNING && setmask & ~IFCAP_WOL)
  4120. iflib_stop(ctx);
  4121. STATE_LOCK(ctx);
  4122. if_togglecapenable(ifp, setmask);
  4123. ctx->ifc_softc_ctx.isc_capenable ^= setmask;
  4124. STATE_UNLOCK(ctx);
  4125. if (bits & IFF_DRV_RUNNING && setmask & ~IFCAP_WOL)
  4126. iflib_init_locked(ctx);
  4127. STATE_LOCK(ctx);
  4128. if_setdrvflags(ifp, bits);
  4129. STATE_UNLOCK(ctx);
  4130. CTX_UNLOCK(ctx);
  4131. }
  4132. if_vlancap(ifp);
  4133. break;
  4134. }
  4135. case SIOCGPRIVATE_0:
  4136. case SIOCSDRVSPEC:
  4137. case SIOCGDRVSPEC:
  4138. CTX_LOCK(ctx);
  4139. err = IFDI_PRIV_IOCTL(ctx, command, data);
  4140. CTX_UNLOCK(ctx);
  4141. break;
  4142. default:
  4143. err = ether_ioctl(ifp, command, data);
  4144. break;
  4145. }
  4146. if (reinit)
  4147. iflib_if_init(ctx);
  4148. return (err);
  4149. }
  4150. static uint64_t
  4151. iflib_if_get_counter(if_t ifp, ift_counter cnt)
  4152. {
  4153. if_ctx_t ctx = if_getsoftc(ifp);
  4154. return (IFDI_GET_COUNTER(ctx, cnt));
  4155. }
  4156. /*********************************************************************
  4157. *
  4158. * OTHER FUNCTIONS EXPORTED TO THE STACK
  4159. *
  4160. **********************************************************************/
  4161. static void
  4162. iflib_vlan_register(void *arg, if_t ifp, uint16_t vtag)
  4163. {
  4164. if_ctx_t ctx = if_getsoftc(ifp);
  4165. if ((void *)ctx != arg)
  4166. return;
  4167. if ((vtag == 0) || (vtag > 4095))
  4168. return;
  4169. if (iflib_in_detach(ctx))
  4170. return;
  4171. CTX_LOCK(ctx);
  4172. /* Driver may need all untagged packets to be flushed */
  4173. if (IFDI_NEEDS_RESTART(ctx, IFLIB_RESTART_VLAN_CONFIG))
  4174. iflib_stop(ctx);
  4175. IFDI_VLAN_REGISTER(ctx, vtag);
  4176. /* Re-init to load the changes, if required */
  4177. if (IFDI_NEEDS_RESTART(ctx, IFLIB_RESTART_VLAN_CONFIG))
  4178. iflib_init_locked(ctx);
  4179. CTX_UNLOCK(ctx);
  4180. }
  4181. static void
  4182. iflib_vlan_unregister(void *arg, if_t ifp, uint16_t vtag)
  4183. {
  4184. if_ctx_t ctx = if_getsoftc(ifp);
  4185. if ((void *)ctx != arg)
  4186. return;
  4187. if ((vtag == 0) || (vtag > 4095))
  4188. return;
  4189. CTX_LOCK(ctx);
  4190. /* Driver may need all tagged packets to be flushed */
  4191. if (IFDI_NEEDS_RESTART(ctx, IFLIB_RESTART_VLAN_CONFIG))
  4192. iflib_stop(ctx);
  4193. IFDI_VLAN_UNREGISTER(ctx, vtag);
  4194. /* Re-init to load the changes, if required */
  4195. if (IFDI_NEEDS_RESTART(ctx, IFLIB_RESTART_VLAN_CONFIG))
  4196. iflib_init_locked(ctx);
  4197. CTX_UNLOCK(ctx);
  4198. }
  4199. static void
  4200. iflib_led_func(void *arg, int onoff)
  4201. {
  4202. if_ctx_t ctx = arg;
  4203. CTX_LOCK(ctx);
  4204. IFDI_LED_FUNC(ctx, onoff);
  4205. CTX_UNLOCK(ctx);
  4206. }
  4207. /*********************************************************************
  4208. *
  4209. * BUS FUNCTION DEFINITIONS
  4210. *
  4211. **********************************************************************/
  4212. int
  4213. iflib_device_probe(device_t dev)
  4214. {
  4215. const pci_vendor_info_t *ent;
  4216. if_shared_ctx_t sctx;
  4217. uint16_t pci_device_id, pci_rev_id, pci_subdevice_id, pci_subvendor_id;
  4218. uint16_t pci_vendor_id;
  4219. if ((sctx = DEVICE_REGISTER(dev)) == NULL || sctx->isc_magic != IFLIB_MAGIC)
  4220. return (ENOTSUP);
  4221. pci_vendor_id = pci_get_vendor(dev);
  4222. pci_device_id = pci_get_device(dev);
  4223. pci_subvendor_id = pci_get_subvendor(dev);
  4224. pci_subdevice_id = pci_get_subdevice(dev);
  4225. pci_rev_id = pci_get_revid(dev);
  4226. if (sctx->isc_parse_devinfo != NULL)
  4227. sctx->isc_parse_devinfo(&pci_device_id, &pci_subvendor_id, &pci_subdevice_id, &pci_rev_id);
  4228. ent = sctx->isc_vendor_info;
  4229. while (ent->pvi_vendor_id != 0) {
  4230. if (pci_vendor_id != ent->pvi_vendor_id) {
  4231. ent++;
  4232. continue;
  4233. }
  4234. if ((pci_device_id == ent->pvi_device_id) &&
  4235. ((pci_subvendor_id == ent->pvi_subvendor_id) ||
  4236. (ent->pvi_subvendor_id == 0)) &&
  4237. ((pci_subdevice_id == ent->pvi_subdevice_id) ||
  4238. (ent->pvi_subdevice_id == 0)) &&
  4239. ((pci_rev_id == ent->pvi_rev_id) ||
  4240. (ent->pvi_rev_id == 0))) {
  4241. device_set_desc_copy(dev, ent->pvi_name);
  4242. /* this needs to be changed to zero if the bus probing code
  4243. * ever stops re-probing on best match because the sctx
  4244. * may have its values over written by register calls
  4245. * in subsequent probes
  4246. */
  4247. return (BUS_PROBE_DEFAULT);
  4248. }
  4249. ent++;
  4250. }
  4251. return (ENXIO);
  4252. }
  4253. int
  4254. iflib_device_probe_vendor(device_t dev)
  4255. {
  4256. int probe;
  4257. probe = iflib_device_probe(dev);
  4258. if (probe == BUS_PROBE_DEFAULT)
  4259. return (BUS_PROBE_VENDOR);
  4260. else
  4261. return (probe);
  4262. }
  4263. static void
  4264. iflib_reset_qvalues(if_ctx_t ctx)
  4265. {
  4266. if_softc_ctx_t scctx = &ctx->ifc_softc_ctx;
  4267. if_shared_ctx_t sctx = ctx->ifc_sctx;
  4268. device_t dev = ctx->ifc_dev;
  4269. int i;
  4270. if (ctx->ifc_sysctl_ntxqs != 0)
  4271. scctx->isc_ntxqsets = ctx->ifc_sysctl_ntxqs;
  4272. if (ctx->ifc_sysctl_nrxqs != 0)
  4273. scctx->isc_nrxqsets = ctx->ifc_sysctl_nrxqs;
  4274. for (i = 0; i < sctx->isc_ntxqs; i++) {
  4275. if (ctx->ifc_sysctl_ntxds[i] != 0)
  4276. scctx->isc_ntxd[i] = ctx->ifc_sysctl_ntxds[i];
  4277. else
  4278. scctx->isc_ntxd[i] = sctx->isc_ntxd_default[i];
  4279. }
  4280. for (i = 0; i < sctx->isc_nrxqs; i++) {
  4281. if (ctx->ifc_sysctl_nrxds[i] != 0)
  4282. scctx->isc_nrxd[i] = ctx->ifc_sysctl_nrxds[i];
  4283. else
  4284. scctx->isc_nrxd[i] = sctx->isc_nrxd_default[i];
  4285. }
  4286. for (i = 0; i < sctx->isc_nrxqs; i++) {
  4287. if (scctx->isc_nrxd[i] < sctx->isc_nrxd_min[i]) {
  4288. device_printf(dev, "nrxd%d: %d less than nrxd_min %d - resetting to min\n",
  4289. i, scctx->isc_nrxd[i], sctx->isc_nrxd_min[i]);
  4290. scctx->isc_nrxd[i] = sctx->isc_nrxd_min[i];
  4291. }
  4292. if (scctx->isc_nrxd[i] > sctx->isc_nrxd_max[i]) {
  4293. device_printf(dev, "nrxd%d: %d greater than nrxd_max %d - resetting to max\n",
  4294. i, scctx->isc_nrxd[i], sctx->isc_nrxd_max[i]);
  4295. scctx->isc_nrxd[i] = sctx->isc_nrxd_max[i];
  4296. }
  4297. if (!powerof2(scctx->isc_nrxd[i])) {
  4298. device_printf(dev, "nrxd%d: %d is not a power of 2 - using default value of %d\n",
  4299. i, scctx->isc_nrxd[i], sctx->isc_nrxd_default[i]);
  4300. scctx->isc_nrxd[i] = sctx->isc_nrxd_default[i];
  4301. }
  4302. }
  4303. for (i = 0; i < sctx->isc_ntxqs; i++) {
  4304. if (scctx->isc_ntxd[i] < sctx->isc_ntxd_min[i]) {
  4305. device_printf(dev, "ntxd%d: %d less than ntxd_min %d - resetting to min\n",
  4306. i, scctx->isc_ntxd[i], sctx->isc_ntxd_min[i]);
  4307. scctx->isc_ntxd[i] = sctx->isc_ntxd_min[i];
  4308. }
  4309. if (scctx->isc_ntxd[i] > sctx->isc_ntxd_max[i]) {
  4310. device_printf(dev, "ntxd%d: %d greater than ntxd_max %d - resetting to max\n",
  4311. i, scctx->isc_ntxd[i], sctx->isc_ntxd_max[i]);
  4312. scctx->isc_ntxd[i] = sctx->isc_ntxd_max[i];
  4313. }
  4314. if (!powerof2(scctx->isc_ntxd[i])) {
  4315. device_printf(dev, "ntxd%d: %d is not a power of 2 - using default value of %d\n",
  4316. i, scctx->isc_ntxd[i], sctx->isc_ntxd_default[i]);
  4317. scctx->isc_ntxd[i] = sctx->isc_ntxd_default[i];
  4318. }
  4319. }
  4320. }
  4321. static void
  4322. iflib_add_pfil(if_ctx_t ctx)
  4323. {
  4324. struct pfil_head *pfil;
  4325. struct pfil_head_args pa;
  4326. iflib_rxq_t rxq;
  4327. int i;
  4328. pa.pa_version = PFIL_VERSION;
  4329. pa.pa_flags = PFIL_IN;
  4330. pa.pa_type = PFIL_TYPE_ETHERNET;
  4331. pa.pa_headname = if_name(ctx->ifc_ifp);
  4332. pfil = pfil_head_register(&pa);
  4333. for (i = 0, rxq = ctx->ifc_rxqs; i < NRXQSETS(ctx); i++, rxq++) {
  4334. rxq->pfil = pfil;
  4335. }
  4336. }
  4337. static void
  4338. iflib_rem_pfil(if_ctx_t ctx)
  4339. {
  4340. struct pfil_head *pfil;
  4341. iflib_rxq_t rxq;
  4342. int i;
  4343. rxq = ctx->ifc_rxqs;
  4344. pfil = rxq->pfil;
  4345. for (i = 0; i < NRXQSETS(ctx); i++, rxq++) {
  4346. rxq->pfil = NULL;
  4347. }
  4348. pfil_head_unregister(pfil);
  4349. }
  4350. /*
  4351. * Advance forward by n members of the cpuset ctx->ifc_cpus starting from
  4352. * cpuid and wrapping as necessary.
  4353. */
  4354. static unsigned int
  4355. cpuid_advance(if_ctx_t ctx, unsigned int cpuid, unsigned int n)
  4356. {
  4357. unsigned int first_valid;
  4358. unsigned int last_valid;
  4359. /* cpuid should always be in the valid set */
  4360. MPASS(CPU_ISSET(cpuid, &ctx->ifc_cpus));
  4361. /* valid set should never be empty */
  4362. MPASS(!CPU_EMPTY(&ctx->ifc_cpus));
  4363. first_valid = CPU_FFS(&ctx->ifc_cpus) - 1;
  4364. last_valid = CPU_FLS(&ctx->ifc_cpus) - 1;
  4365. n = n % CPU_COUNT(&ctx->ifc_cpus);
  4366. while (n > 0) {
  4367. do {
  4368. cpuid++;
  4369. if (cpuid > last_valid)
  4370. cpuid = first_valid;
  4371. } while (!CPU_ISSET(cpuid, &ctx->ifc_cpus));
  4372. n--;
  4373. }
  4374. return (cpuid);
  4375. }
  4376. #if defined(SMP) && defined(SCHED_ULE)
  4377. extern struct cpu_group *cpu_top; /* CPU topology */
  4378. static int
  4379. find_child_with_core(int cpu, struct cpu_group *grp)
  4380. {
  4381. int i;
  4382. if (grp->cg_children == 0)
  4383. return -1;
  4384. MPASS(grp->cg_child);
  4385. for (i = 0; i < grp->cg_children; i++) {
  4386. if (CPU_ISSET(cpu, &grp->cg_child[i].cg_mask))
  4387. return i;
  4388. }
  4389. return -1;
  4390. }
  4391. /*
  4392. * Find an L2 neighbor of the given CPU or return -1 if none found. This
  4393. * does not distinguish among multiple L2 neighbors if the given CPU has
  4394. * more than one (it will always return the same result in that case).
  4395. */
  4396. static int
  4397. find_l2_neighbor(int cpu)
  4398. {
  4399. struct cpu_group *grp;
  4400. int i;
  4401. grp = cpu_top;
  4402. if (grp == NULL)
  4403. return -1;
  4404. /*
  4405. * Find the smallest CPU group that contains the given core.
  4406. */
  4407. i = 0;
  4408. while ((i = find_child_with_core(cpu, grp)) != -1) {
  4409. /*
  4410. * If the smallest group containing the given CPU has less
  4411. * than two members, we conclude the given CPU has no
  4412. * L2 neighbor.
  4413. */
  4414. if (grp->cg_child[i].cg_count <= 1)
  4415. return (-1);
  4416. grp = &grp->cg_child[i];
  4417. }
  4418. /* Must share L2. */
  4419. if (grp->cg_level > CG_SHARE_L2 || grp->cg_level == CG_SHARE_NONE)
  4420. return -1;
  4421. /*
  4422. * Select the first member of the set that isn't the reference
  4423. * CPU, which at this point is guaranteed to exist.
  4424. */
  4425. for (i = 0; i < CPU_SETSIZE; i++) {
  4426. if (CPU_ISSET(i, &grp->cg_mask) && i != cpu)
  4427. return (i);
  4428. }
  4429. /* Should never be reached */
  4430. return (-1);
  4431. }
  4432. #else
  4433. static int
  4434. find_l2_neighbor(int cpu)
  4435. {
  4436. return (-1);
  4437. }
  4438. #endif
  4439. /*
  4440. * CPU mapping behaviors
  4441. * ---------------------
  4442. * 'separate txrx' refers to the separate_txrx sysctl
  4443. * 'use logical' refers to the use_logical_cores sysctl
  4444. * 'INTR CPUS' indicates whether bus_get_cpus(INTR_CPUS) succeeded
  4445. *
  4446. * separate use INTR
  4447. * txrx logical CPUS result
  4448. * ---------- --------- ------ ------------------------------------------------
  4449. * - - X RX and TX queues mapped to consecutive physical
  4450. * cores with RX/TX pairs on same core and excess
  4451. * of either following
  4452. * - X X RX and TX queues mapped to consecutive cores
  4453. * of any type with RX/TX pairs on same core and
  4454. * excess of either following
  4455. * X - X RX and TX queues mapped to consecutive physical
  4456. * cores; all RX then all TX
  4457. * X X X RX queues mapped to consecutive physical cores
  4458. * first, then TX queues mapped to L2 neighbor of
  4459. * the corresponding RX queue if one exists,
  4460. * otherwise to consecutive physical cores
  4461. * - n/a - RX and TX queues mapped to consecutive cores of
  4462. * any type with RX/TX pairs on same core and excess
  4463. * of either following
  4464. * X n/a - RX and TX queues mapped to consecutive cores of
  4465. * any type; all RX then all TX
  4466. */
  4467. static unsigned int
  4468. get_cpuid_for_queue(if_ctx_t ctx, unsigned int base_cpuid, unsigned int qid,
  4469. bool is_tx)
  4470. {
  4471. if_softc_ctx_t scctx = &ctx->ifc_softc_ctx;
  4472. unsigned int core_index;
  4473. if (ctx->ifc_sysctl_separate_txrx) {
  4474. /*
  4475. * When using separate CPUs for TX and RX, the assignment
  4476. * will always be of a consecutive CPU out of the set of
  4477. * context CPUs, except for the specific case where the
  4478. * context CPUs are phsyical cores, the use of logical cores
  4479. * has been enabled, the assignment is for TX, the TX qid
  4480. * corresponds to an RX qid, and the CPU assigned to the
  4481. * corresponding RX queue has an L2 neighbor.
  4482. */
  4483. if (ctx->ifc_sysctl_use_logical_cores &&
  4484. ctx->ifc_cpus_are_physical_cores &&
  4485. is_tx && qid < scctx->isc_nrxqsets) {
  4486. int l2_neighbor;
  4487. unsigned int rx_cpuid;
  4488. rx_cpuid = cpuid_advance(ctx, base_cpuid, qid);
  4489. l2_neighbor = find_l2_neighbor(rx_cpuid);
  4490. if (l2_neighbor != -1) {
  4491. return (l2_neighbor);
  4492. }
  4493. /*
  4494. * ... else fall through to the normal
  4495. * consecutive-after-RX assignment scheme.
  4496. *
  4497. * Note that we are assuming that all RX queue CPUs
  4498. * have an L2 neighbor, or all do not. If a mixed
  4499. * scenario is possible, we will have to keep track
  4500. * separately of how many queues prior to this one
  4501. * were not able to be assigned to an L2 neighbor.
  4502. */
  4503. }
  4504. if (is_tx)
  4505. core_index = scctx->isc_nrxqsets + qid;
  4506. else
  4507. core_index = qid;
  4508. } else {
  4509. core_index = qid;
  4510. }
  4511. return (cpuid_advance(ctx, base_cpuid, core_index));
  4512. }
  4513. static uint16_t
  4514. get_ctx_core_offset(if_ctx_t ctx)
  4515. {
  4516. if_softc_ctx_t scctx = &ctx->ifc_softc_ctx;
  4517. struct cpu_offset *op;
  4518. cpuset_t assigned_cpus;
  4519. unsigned int cores_consumed;
  4520. unsigned int base_cpuid = ctx->ifc_sysctl_core_offset;
  4521. unsigned int first_valid;
  4522. unsigned int last_valid;
  4523. unsigned int i;
  4524. first_valid = CPU_FFS(&ctx->ifc_cpus) - 1;
  4525. last_valid = CPU_FLS(&ctx->ifc_cpus) - 1;
  4526. if (base_cpuid != CORE_OFFSET_UNSPECIFIED) {
  4527. /*
  4528. * Align the user-chosen base CPU ID to the next valid CPU
  4529. * for this device. If the chosen base CPU ID is smaller
  4530. * than the first valid CPU or larger than the last valid
  4531. * CPU, we assume the user does not know what the valid
  4532. * range is for this device and is thinking in terms of a
  4533. * zero-based reference frame, and so we shift the given
  4534. * value into the valid range (and wrap accordingly) so the
  4535. * intent is translated to the proper frame of reference.
  4536. * If the base CPU ID is within the valid first/last, but
  4537. * does not correspond to a valid CPU, it is advanced to the
  4538. * next valid CPU (wrapping if necessary).
  4539. */
  4540. if (base_cpuid < first_valid || base_cpuid > last_valid) {
  4541. /* shift from zero-based to first_valid-based */
  4542. base_cpuid += first_valid;
  4543. /* wrap to range [first_valid, last_valid] */
  4544. base_cpuid = (base_cpuid - first_valid) %
  4545. (last_valid - first_valid + 1);
  4546. }
  4547. if (!CPU_ISSET(base_cpuid, &ctx->ifc_cpus)) {
  4548. /*
  4549. * base_cpuid is in [first_valid, last_valid], but
  4550. * not a member of the valid set. In this case,
  4551. * there will always be a member of the valid set
  4552. * with a CPU ID that is greater than base_cpuid,
  4553. * and we simply advance to it.
  4554. */
  4555. while (!CPU_ISSET(base_cpuid, &ctx->ifc_cpus))
  4556. base_cpuid++;
  4557. }
  4558. return (base_cpuid);
  4559. }
  4560. /*
  4561. * Determine how many cores will be consumed by performing the CPU
  4562. * assignments and counting how many of the assigned CPUs correspond
  4563. * to CPUs in the set of context CPUs. This is done using the CPU
  4564. * ID first_valid as the base CPU ID, as the base CPU must be within
  4565. * the set of context CPUs.
  4566. *
  4567. * Note not all assigned CPUs will be in the set of context CPUs
  4568. * when separate CPUs are being allocated to TX and RX queues,
  4569. * assignment to logical cores has been enabled, the set of context
  4570. * CPUs contains only physical CPUs, and TX queues are mapped to L2
  4571. * neighbors of CPUs that RX queues have been mapped to - in this
  4572. * case we do only want to count how many CPUs in the set of context
  4573. * CPUs have been consumed, as that determines the next CPU in that
  4574. * set to start allocating at for the next device for which
  4575. * core_offset is not set.
  4576. */
  4577. CPU_ZERO(&assigned_cpus);
  4578. for (i = 0; i < scctx->isc_ntxqsets; i++)
  4579. CPU_SET(get_cpuid_for_queue(ctx, first_valid, i, true),
  4580. &assigned_cpus);
  4581. for (i = 0; i < scctx->isc_nrxqsets; i++)
  4582. CPU_SET(get_cpuid_for_queue(ctx, first_valid, i, false),
  4583. &assigned_cpus);
  4584. CPU_AND(&assigned_cpus, &assigned_cpus, &ctx->ifc_cpus);
  4585. cores_consumed = CPU_COUNT(&assigned_cpus);
  4586. mtx_lock(&cpu_offset_mtx);
  4587. SLIST_FOREACH(op, &cpu_offsets, entries) {
  4588. if (CPU_CMP(&ctx->ifc_cpus, &op->set) == 0) {
  4589. base_cpuid = op->next_cpuid;
  4590. op->next_cpuid = cpuid_advance(ctx, op->next_cpuid,
  4591. cores_consumed);
  4592. MPASS(op->refcount < UINT_MAX);
  4593. op->refcount++;
  4594. break;
  4595. }
  4596. }
  4597. if (base_cpuid == CORE_OFFSET_UNSPECIFIED) {
  4598. base_cpuid = first_valid;
  4599. op = malloc(sizeof(struct cpu_offset), M_IFLIB,
  4600. M_NOWAIT | M_ZERO);
  4601. if (op == NULL) {
  4602. device_printf(ctx->ifc_dev,
  4603. "allocation for cpu offset failed.\n");
  4604. } else {
  4605. op->next_cpuid = cpuid_advance(ctx, base_cpuid,
  4606. cores_consumed);
  4607. op->refcount = 1;
  4608. CPU_COPY(&ctx->ifc_cpus, &op->set);
  4609. SLIST_INSERT_HEAD(&cpu_offsets, op, entries);
  4610. }
  4611. }
  4612. mtx_unlock(&cpu_offset_mtx);
  4613. return (base_cpuid);
  4614. }
  4615. static void
  4616. unref_ctx_core_offset(if_ctx_t ctx)
  4617. {
  4618. struct cpu_offset *op, *top;
  4619. mtx_lock(&cpu_offset_mtx);
  4620. SLIST_FOREACH_SAFE(op, &cpu_offsets, entries, top) {
  4621. if (CPU_CMP(&ctx->ifc_cpus, &op->set) == 0) {
  4622. MPASS(op->refcount > 0);
  4623. op->refcount--;
  4624. if (op->refcount == 0) {
  4625. SLIST_REMOVE(&cpu_offsets, op, cpu_offset, entries);
  4626. free(op, M_IFLIB);
  4627. }
  4628. break;
  4629. }
  4630. }
  4631. mtx_unlock(&cpu_offset_mtx);
  4632. }
  4633. int
  4634. iflib_device_register(device_t dev, void *sc, if_shared_ctx_t sctx, if_ctx_t *ctxp)
  4635. {
  4636. if_ctx_t ctx;
  4637. if_t ifp;
  4638. if_softc_ctx_t scctx;
  4639. kobjop_desc_t kobj_desc;
  4640. kobj_method_t *kobj_method;
  4641. int err, msix, rid;
  4642. int num_txd, num_rxd;
  4643. ctx = malloc(sizeof(* ctx), M_IFLIB, M_WAITOK|M_ZERO);
  4644. if (sc == NULL) {
  4645. sc = malloc(sctx->isc_driver->size, M_IFLIB, M_WAITOK|M_ZERO);
  4646. device_set_softc(dev, ctx);
  4647. ctx->ifc_flags |= IFC_SC_ALLOCATED;
  4648. }
  4649. ctx->ifc_sctx = sctx;
  4650. ctx->ifc_dev = dev;
  4651. ctx->ifc_softc = sc;
  4652. if ((err = iflib_register(ctx)) != 0) {
  4653. device_printf(dev, "iflib_register failed %d\n", err);
  4654. goto fail_ctx_free;
  4655. }
  4656. iflib_add_device_sysctl_pre(ctx);
  4657. scctx = &ctx->ifc_softc_ctx;
  4658. ifp = ctx->ifc_ifp;
  4659. iflib_reset_qvalues(ctx);
  4660. IFNET_WLOCK();
  4661. CTX_LOCK(ctx);
  4662. if ((err = IFDI_ATTACH_PRE(ctx)) != 0) {
  4663. device_printf(dev, "IFDI_ATTACH_PRE failed %d\n", err);
  4664. goto fail_unlock;
  4665. }
  4666. _iflib_pre_assert(scctx);
  4667. ctx->ifc_txrx = *scctx->isc_txrx;
  4668. MPASS(scctx->isc_dma_width <= flsll(BUS_SPACE_MAXADDR));
  4669. if (sctx->isc_flags & IFLIB_DRIVER_MEDIA)
  4670. ctx->ifc_mediap = scctx->isc_media;
  4671. #ifdef INVARIANTS
  4672. if (scctx->isc_capabilities & IFCAP_TXCSUM)
  4673. MPASS(scctx->isc_tx_csum_flags);
  4674. #endif
  4675. if_setcapabilities(ifp,
  4676. scctx->isc_capabilities | IFCAP_HWSTATS | IFCAP_MEXTPG);
  4677. if_setcapenable(ifp,
  4678. scctx->isc_capenable | IFCAP_HWSTATS | IFCAP_MEXTPG);
  4679. if (scctx->isc_ntxqsets == 0 || (scctx->isc_ntxqsets_max && scctx->isc_ntxqsets_max < scctx->isc_ntxqsets))
  4680. scctx->isc_ntxqsets = scctx->isc_ntxqsets_max;
  4681. if (scctx->isc_nrxqsets == 0 || (scctx->isc_nrxqsets_max && scctx->isc_nrxqsets_max < scctx->isc_nrxqsets))
  4682. scctx->isc_nrxqsets = scctx->isc_nrxqsets_max;
  4683. num_txd = iflib_num_tx_descs(ctx);
  4684. num_rxd = iflib_num_rx_descs(ctx);
  4685. /* XXX change for per-queue sizes */
  4686. device_printf(dev, "Using %d TX descriptors and %d RX descriptors\n",
  4687. num_txd, num_rxd);
  4688. if (scctx->isc_tx_nsegments > num_txd / MAX_SINGLE_PACKET_FRACTION)
  4689. scctx->isc_tx_nsegments = max(1, num_txd /
  4690. MAX_SINGLE_PACKET_FRACTION);
  4691. if (scctx->isc_tx_tso_segments_max > num_txd /
  4692. MAX_SINGLE_PACKET_FRACTION)
  4693. scctx->isc_tx_tso_segments_max = max(1,
  4694. num_txd / MAX_SINGLE_PACKET_FRACTION);
  4695. /* TSO parameters - dig these out of the data sheet - simply correspond to tag setup */
  4696. if (if_getcapabilities(ifp) & IFCAP_TSO) {
  4697. /*
  4698. * The stack can't handle a TSO size larger than IP_MAXPACKET,
  4699. * but some MACs do.
  4700. */
  4701. if_sethwtsomax(ifp, min(scctx->isc_tx_tso_size_max,
  4702. IP_MAXPACKET));
  4703. /*
  4704. * Take maximum number of m_pullup(9)'s in iflib_parse_header()
  4705. * into account. In the worst case, each of these calls will
  4706. * add another mbuf and, thus, the requirement for another DMA
  4707. * segment. So for best performance, it doesn't make sense to
  4708. * advertize a maximum of TSO segments that typically will
  4709. * require defragmentation in iflib_encap().
  4710. */
  4711. if_sethwtsomaxsegcount(ifp, scctx->isc_tx_tso_segments_max - 3);
  4712. if_sethwtsomaxsegsize(ifp, scctx->isc_tx_tso_segsize_max);
  4713. }
  4714. if (scctx->isc_rss_table_size == 0)
  4715. scctx->isc_rss_table_size = 64;
  4716. scctx->isc_rss_table_mask = scctx->isc_rss_table_size-1;
  4717. GROUPTASK_INIT(&ctx->ifc_admin_task, 0, _task_fn_admin, ctx);
  4718. /* XXX format name */
  4719. taskqgroup_attach(qgroup_if_config_tqg, &ctx->ifc_admin_task, ctx,
  4720. NULL, NULL, "admin");
  4721. /* Set up cpu set. If it fails, use the set of all CPUs. */
  4722. if (bus_get_cpus(dev, INTR_CPUS, sizeof(ctx->ifc_cpus), &ctx->ifc_cpus) != 0) {
  4723. device_printf(dev, "Unable to fetch CPU list\n");
  4724. CPU_COPY(&all_cpus, &ctx->ifc_cpus);
  4725. ctx->ifc_cpus_are_physical_cores = false;
  4726. } else
  4727. ctx->ifc_cpus_are_physical_cores = true;
  4728. MPASS(CPU_COUNT(&ctx->ifc_cpus) > 0);
  4729. /*
  4730. ** Now set up MSI or MSI-X, should return us the number of supported
  4731. ** vectors (will be 1 for a legacy interrupt and MSI).
  4732. */
  4733. if (sctx->isc_flags & IFLIB_SKIP_MSIX) {
  4734. msix = scctx->isc_vectors;
  4735. } else if (scctx->isc_msix_bar != 0)
  4736. /*
  4737. * The simple fact that isc_msix_bar is not 0 does not mean we
  4738. * we have a good value there that is known to work.
  4739. */
  4740. msix = iflib_msix_init(ctx);
  4741. else {
  4742. scctx->isc_vectors = 1;
  4743. scctx->isc_ntxqsets = 1;
  4744. scctx->isc_nrxqsets = 1;
  4745. scctx->isc_intr = IFLIB_INTR_LEGACY;
  4746. msix = 0;
  4747. }
  4748. /* Get memory for the station queues */
  4749. if ((err = iflib_queues_alloc(ctx))) {
  4750. device_printf(dev, "Unable to allocate queue memory\n");
  4751. goto fail_intr_free;
  4752. }
  4753. if ((err = iflib_qset_structures_setup(ctx)))
  4754. goto fail_queues;
  4755. /*
  4756. * Now that we know how many queues there are, get the core offset.
  4757. */
  4758. ctx->ifc_sysctl_core_offset = get_ctx_core_offset(ctx);
  4759. if (msix > 1) {
  4760. /*
  4761. * When using MSI-X, ensure that ifdi_{r,t}x_queue_intr_enable
  4762. * aren't the default NULL implementation.
  4763. */
  4764. kobj_desc = &ifdi_rx_queue_intr_enable_desc;
  4765. kobj_method = kobj_lookup_method(((kobj_t)ctx)->ops->cls, NULL,
  4766. kobj_desc);
  4767. if (kobj_method == &kobj_desc->deflt) {
  4768. device_printf(dev,
  4769. "MSI-X requires ifdi_rx_queue_intr_enable method");
  4770. err = EOPNOTSUPP;
  4771. goto fail_queues;
  4772. }
  4773. kobj_desc = &ifdi_tx_queue_intr_enable_desc;
  4774. kobj_method = kobj_lookup_method(((kobj_t)ctx)->ops->cls, NULL,
  4775. kobj_desc);
  4776. if (kobj_method == &kobj_desc->deflt) {
  4777. device_printf(dev,
  4778. "MSI-X requires ifdi_tx_queue_intr_enable method");
  4779. err = EOPNOTSUPP;
  4780. goto fail_queues;
  4781. }
  4782. /*
  4783. * Assign the MSI-X vectors.
  4784. * Note that the default NULL ifdi_msix_intr_assign method will
  4785. * fail here, too.
  4786. */
  4787. err = IFDI_MSIX_INTR_ASSIGN(ctx, msix);
  4788. if (err != 0) {
  4789. device_printf(dev, "IFDI_MSIX_INTR_ASSIGN failed %d\n",
  4790. err);
  4791. goto fail_queues;
  4792. }
  4793. } else if (scctx->isc_intr != IFLIB_INTR_MSIX) {
  4794. rid = 0;
  4795. if (scctx->isc_intr == IFLIB_INTR_MSI) {
  4796. MPASS(msix == 1);
  4797. rid = 1;
  4798. }
  4799. if ((err = iflib_legacy_setup(ctx, ctx->isc_legacy_intr, ctx->ifc_softc, &rid, "irq0")) != 0) {
  4800. device_printf(dev, "iflib_legacy_setup failed %d\n", err);
  4801. goto fail_queues;
  4802. }
  4803. } else {
  4804. device_printf(dev,
  4805. "Cannot use iflib with only 1 MSI-X interrupt!\n");
  4806. err = ENODEV;
  4807. goto fail_queues;
  4808. }
  4809. /*
  4810. * It prevents a double-locking panic with iflib_media_status when
  4811. * the driver loads.
  4812. */
  4813. CTX_UNLOCK(ctx);
  4814. ether_ifattach(ctx->ifc_ifp, ctx->ifc_mac.octet);
  4815. CTX_LOCK(ctx);
  4816. if ((err = IFDI_ATTACH_POST(ctx)) != 0) {
  4817. device_printf(dev, "IFDI_ATTACH_POST failed %d\n", err);
  4818. goto fail_detach;
  4819. }
  4820. /*
  4821. * Tell the upper layer(s) if IFCAP_VLAN_MTU is supported.
  4822. * This must appear after the call to ether_ifattach() because
  4823. * ether_ifattach() sets if_hdrlen to the default value.
  4824. */
  4825. if (if_getcapabilities(ifp) & IFCAP_VLAN_MTU)
  4826. if_setifheaderlen(ifp, sizeof(struct ether_vlan_header));
  4827. if ((err = iflib_netmap_attach(ctx))) {
  4828. device_printf(ctx->ifc_dev, "netmap attach failed: %d\n", err);
  4829. goto fail_detach;
  4830. }
  4831. *ctxp = ctx;
  4832. DEBUGNET_SET(ctx->ifc_ifp, iflib);
  4833. if_setgetcounterfn(ctx->ifc_ifp, iflib_if_get_counter);
  4834. iflib_add_device_sysctl_post(ctx);
  4835. iflib_add_pfil(ctx);
  4836. ctx->ifc_flags |= IFC_INIT_DONE;
  4837. CTX_UNLOCK(ctx);
  4838. IFNET_WUNLOCK();
  4839. return (0);
  4840. fail_detach:
  4841. ether_ifdetach(ctx->ifc_ifp);
  4842. fail_queues:
  4843. iflib_tqg_detach(ctx);
  4844. iflib_tx_structures_free(ctx);
  4845. iflib_rx_structures_free(ctx);
  4846. IFDI_DETACH(ctx);
  4847. IFDI_QUEUES_FREE(ctx);
  4848. fail_intr_free:
  4849. iflib_free_intr_mem(ctx);
  4850. fail_unlock:
  4851. CTX_UNLOCK(ctx);
  4852. IFNET_WUNLOCK();
  4853. iflib_deregister(ctx);
  4854. fail_ctx_free:
  4855. device_set_softc(ctx->ifc_dev, NULL);
  4856. if (ctx->ifc_flags & IFC_SC_ALLOCATED)
  4857. free(ctx->ifc_softc, M_IFLIB);
  4858. free(ctx, M_IFLIB);
  4859. return (err);
  4860. }
  4861. int
  4862. iflib_device_attach(device_t dev)
  4863. {
  4864. if_ctx_t ctx;
  4865. if_shared_ctx_t sctx;
  4866. if ((sctx = DEVICE_REGISTER(dev)) == NULL || sctx->isc_magic != IFLIB_MAGIC)
  4867. return (ENOTSUP);
  4868. pci_enable_busmaster(dev);
  4869. return (iflib_device_register(dev, NULL, sctx, &ctx));
  4870. }
  4871. int
  4872. iflib_device_deregister(if_ctx_t ctx)
  4873. {
  4874. if_t ifp = ctx->ifc_ifp;
  4875. device_t dev = ctx->ifc_dev;
  4876. /* Make sure VLANS are not using driver */
  4877. if (if_vlantrunkinuse(ifp)) {
  4878. device_printf(dev, "Vlan in use, detach first\n");
  4879. return (EBUSY);
  4880. }
  4881. #ifdef PCI_IOV
  4882. if (!CTX_IS_VF(ctx) && pci_iov_detach(dev) != 0) {
  4883. device_printf(dev, "SR-IOV in use; detach first.\n");
  4884. return (EBUSY);
  4885. }
  4886. #endif
  4887. STATE_LOCK(ctx);
  4888. ctx->ifc_flags |= IFC_IN_DETACH;
  4889. STATE_UNLOCK(ctx);
  4890. /* Unregister VLAN handlers before calling iflib_stop() */
  4891. iflib_unregister_vlan_handlers(ctx);
  4892. iflib_netmap_detach(ifp);
  4893. ether_ifdetach(ifp);
  4894. CTX_LOCK(ctx);
  4895. iflib_stop(ctx);
  4896. CTX_UNLOCK(ctx);
  4897. iflib_rem_pfil(ctx);
  4898. if (ctx->ifc_led_dev != NULL)
  4899. led_destroy(ctx->ifc_led_dev);
  4900. iflib_tqg_detach(ctx);
  4901. iflib_tx_structures_free(ctx);
  4902. iflib_rx_structures_free(ctx);
  4903. CTX_LOCK(ctx);
  4904. IFDI_DETACH(ctx);
  4905. IFDI_QUEUES_FREE(ctx);
  4906. CTX_UNLOCK(ctx);
  4907. /* ether_ifdetach calls if_qflush - lock must be destroy afterwards*/
  4908. iflib_free_intr_mem(ctx);
  4909. bus_generic_detach(dev);
  4910. iflib_deregister(ctx);
  4911. device_set_softc(ctx->ifc_dev, NULL);
  4912. if (ctx->ifc_flags & IFC_SC_ALLOCATED)
  4913. free(ctx->ifc_softc, M_IFLIB);
  4914. unref_ctx_core_offset(ctx);
  4915. free(ctx, M_IFLIB);
  4916. return (0);
  4917. }
  4918. static void
  4919. iflib_tqg_detach(if_ctx_t ctx)
  4920. {
  4921. iflib_txq_t txq;
  4922. iflib_rxq_t rxq;
  4923. int i;
  4924. struct taskqgroup *tqg;
  4925. /* XXX drain any dependent tasks */
  4926. tqg = qgroup_if_io_tqg;
  4927. for (txq = ctx->ifc_txqs, i = 0; i < NTXQSETS(ctx); i++, txq++) {
  4928. callout_drain(&txq->ift_timer);
  4929. #ifdef DEV_NETMAP
  4930. callout_drain(&txq->ift_netmap_timer);
  4931. #endif /* DEV_NETMAP */
  4932. if (txq->ift_task.gt_uniq != NULL)
  4933. taskqgroup_detach(tqg, &txq->ift_task);
  4934. }
  4935. for (i = 0, rxq = ctx->ifc_rxqs; i < NRXQSETS(ctx); i++, rxq++) {
  4936. if (rxq->ifr_task.gt_uniq != NULL)
  4937. taskqgroup_detach(tqg, &rxq->ifr_task);
  4938. }
  4939. tqg = qgroup_if_config_tqg;
  4940. if (ctx->ifc_admin_task.gt_uniq != NULL)
  4941. taskqgroup_detach(tqg, &ctx->ifc_admin_task);
  4942. if (ctx->ifc_vflr_task.gt_uniq != NULL)
  4943. taskqgroup_detach(tqg, &ctx->ifc_vflr_task);
  4944. }
  4945. static void
  4946. iflib_free_intr_mem(if_ctx_t ctx)
  4947. {
  4948. if (ctx->ifc_softc_ctx.isc_intr != IFLIB_INTR_MSIX) {
  4949. iflib_irq_free(ctx, &ctx->ifc_legacy_irq);
  4950. }
  4951. if (ctx->ifc_softc_ctx.isc_intr != IFLIB_INTR_LEGACY) {
  4952. pci_release_msi(ctx->ifc_dev);
  4953. }
  4954. if (ctx->ifc_msix_mem != NULL) {
  4955. bus_release_resource(ctx->ifc_dev, SYS_RES_MEMORY,
  4956. rman_get_rid(ctx->ifc_msix_mem), ctx->ifc_msix_mem);
  4957. ctx->ifc_msix_mem = NULL;
  4958. }
  4959. }
  4960. int
  4961. iflib_device_detach(device_t dev)
  4962. {
  4963. if_ctx_t ctx = device_get_softc(dev);
  4964. return (iflib_device_deregister(ctx));
  4965. }
  4966. int
  4967. iflib_device_suspend(device_t dev)
  4968. {
  4969. if_ctx_t ctx = device_get_softc(dev);
  4970. CTX_LOCK(ctx);
  4971. IFDI_SUSPEND(ctx);
  4972. CTX_UNLOCK(ctx);
  4973. return bus_generic_suspend(dev);
  4974. }
  4975. int
  4976. iflib_device_shutdown(device_t dev)
  4977. {
  4978. if_ctx_t ctx = device_get_softc(dev);
  4979. CTX_LOCK(ctx);
  4980. IFDI_SHUTDOWN(ctx);
  4981. CTX_UNLOCK(ctx);
  4982. return bus_generic_suspend(dev);
  4983. }
  4984. int
  4985. iflib_device_resume(device_t dev)
  4986. {
  4987. if_ctx_t ctx = device_get_softc(dev);
  4988. iflib_txq_t txq = ctx->ifc_txqs;
  4989. CTX_LOCK(ctx);
  4990. IFDI_RESUME(ctx);
  4991. iflib_if_init_locked(ctx);
  4992. CTX_UNLOCK(ctx);
  4993. for (int i = 0; i < NTXQSETS(ctx); i++, txq++)
  4994. iflib_txq_check_drain(txq, IFLIB_RESTART_BUDGET);
  4995. return (bus_generic_resume(dev));
  4996. }
  4997. int
  4998. iflib_device_iov_init(device_t dev, uint16_t num_vfs, const nvlist_t *params)
  4999. {
  5000. int error;
  5001. if_ctx_t ctx = device_get_softc(dev);
  5002. CTX_LOCK(ctx);
  5003. error = IFDI_IOV_INIT(ctx, num_vfs, params);
  5004. CTX_UNLOCK(ctx);
  5005. return (error);
  5006. }
  5007. void
  5008. iflib_device_iov_uninit(device_t dev)
  5009. {
  5010. if_ctx_t ctx = device_get_softc(dev);
  5011. CTX_LOCK(ctx);
  5012. IFDI_IOV_UNINIT(ctx);
  5013. CTX_UNLOCK(ctx);
  5014. }
  5015. int
  5016. iflib_device_iov_add_vf(device_t dev, uint16_t vfnum, const nvlist_t *params)
  5017. {
  5018. int error;
  5019. if_ctx_t ctx = device_get_softc(dev);
  5020. CTX_LOCK(ctx);
  5021. error = IFDI_IOV_VF_ADD(ctx, vfnum, params);
  5022. CTX_UNLOCK(ctx);
  5023. return (error);
  5024. }
  5025. /*********************************************************************
  5026. *
  5027. * MODULE FUNCTION DEFINITIONS
  5028. *
  5029. **********************************************************************/
  5030. /*
  5031. * - Start a fast taskqueue thread for each core
  5032. * - Start a taskqueue for control operations
  5033. */
  5034. static int
  5035. iflib_module_init(void)
  5036. {
  5037. iflib_timer_default = hz / 2;
  5038. return (0);
  5039. }
  5040. static int
  5041. iflib_module_event_handler(module_t mod, int what, void *arg)
  5042. {
  5043. int err;
  5044. switch (what) {
  5045. case MOD_LOAD:
  5046. if ((err = iflib_module_init()) != 0)
  5047. return (err);
  5048. break;
  5049. case MOD_UNLOAD:
  5050. return (EBUSY);
  5051. default:
  5052. return (EOPNOTSUPP);
  5053. }
  5054. return (0);
  5055. }
  5056. /*********************************************************************
  5057. *
  5058. * PUBLIC FUNCTION DEFINITIONS
  5059. * ordered as in iflib.h
  5060. *
  5061. **********************************************************************/
  5062. static void
  5063. _iflib_assert(if_shared_ctx_t sctx)
  5064. {
  5065. int i;
  5066. MPASS(sctx->isc_tx_maxsize);
  5067. MPASS(sctx->isc_tx_maxsegsize);
  5068. MPASS(sctx->isc_rx_maxsize);
  5069. MPASS(sctx->isc_rx_nsegments);
  5070. MPASS(sctx->isc_rx_maxsegsize);
  5071. MPASS(sctx->isc_nrxqs >= 1 && sctx->isc_nrxqs <= 8);
  5072. for (i = 0; i < sctx->isc_nrxqs; i++) {
  5073. MPASS(sctx->isc_nrxd_min[i]);
  5074. MPASS(powerof2(sctx->isc_nrxd_min[i]));
  5075. MPASS(sctx->isc_nrxd_max[i]);
  5076. MPASS(powerof2(sctx->isc_nrxd_max[i]));
  5077. MPASS(sctx->isc_nrxd_default[i]);
  5078. MPASS(powerof2(sctx->isc_nrxd_default[i]));
  5079. }
  5080. MPASS(sctx->isc_ntxqs >= 1 && sctx->isc_ntxqs <= 8);
  5081. for (i = 0; i < sctx->isc_ntxqs; i++) {
  5082. MPASS(sctx->isc_ntxd_min[i]);
  5083. MPASS(powerof2(sctx->isc_ntxd_min[i]));
  5084. MPASS(sctx->isc_ntxd_max[i]);
  5085. MPASS(powerof2(sctx->isc_ntxd_max[i]));
  5086. MPASS(sctx->isc_ntxd_default[i]);
  5087. MPASS(powerof2(sctx->isc_ntxd_default[i]));
  5088. }
  5089. }
  5090. static void
  5091. _iflib_pre_assert(if_softc_ctx_t scctx)
  5092. {
  5093. MPASS(scctx->isc_txrx->ift_txd_encap);
  5094. MPASS(scctx->isc_txrx->ift_txd_flush);
  5095. MPASS(scctx->isc_txrx->ift_txd_credits_update);
  5096. MPASS(scctx->isc_txrx->ift_rxd_available);
  5097. MPASS(scctx->isc_txrx->ift_rxd_pkt_get);
  5098. MPASS(scctx->isc_txrx->ift_rxd_refill);
  5099. MPASS(scctx->isc_txrx->ift_rxd_flush);
  5100. }
  5101. static int
  5102. iflib_register(if_ctx_t ctx)
  5103. {
  5104. if_shared_ctx_t sctx = ctx->ifc_sctx;
  5105. driver_t *driver = sctx->isc_driver;
  5106. device_t dev = ctx->ifc_dev;
  5107. if_t ifp;
  5108. _iflib_assert(sctx);
  5109. CTX_LOCK_INIT(ctx);
  5110. STATE_LOCK_INIT(ctx, device_get_nameunit(ctx->ifc_dev));
  5111. ifp = ctx->ifc_ifp = if_alloc(IFT_ETHER);
  5112. /*
  5113. * Initialize our context's device specific methods
  5114. */
  5115. kobj_init((kobj_t) ctx, (kobj_class_t) driver);
  5116. kobj_class_compile((kobj_class_t) driver);
  5117. if_initname(ifp, device_get_name(dev), device_get_unit(dev));
  5118. if_setsoftc(ifp, ctx);
  5119. if_setdev(ifp, dev);
  5120. if_setinitfn(ifp, iflib_if_init);
  5121. if_setioctlfn(ifp, iflib_if_ioctl);
  5122. #ifdef ALTQ
  5123. if_setstartfn(ifp, iflib_altq_if_start);
  5124. if_settransmitfn(ifp, iflib_altq_if_transmit);
  5125. if_setsendqready(ifp);
  5126. #else
  5127. if_settransmitfn(ifp, iflib_if_transmit);
  5128. #endif
  5129. if_setqflushfn(ifp, iflib_if_qflush);
  5130. if_setflags(ifp, IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST);
  5131. ctx->ifc_vlan_attach_event =
  5132. EVENTHANDLER_REGISTER(vlan_config, iflib_vlan_register, ctx,
  5133. EVENTHANDLER_PRI_FIRST);
  5134. ctx->ifc_vlan_detach_event =
  5135. EVENTHANDLER_REGISTER(vlan_unconfig, iflib_vlan_unregister, ctx,
  5136. EVENTHANDLER_PRI_FIRST);
  5137. if ((sctx->isc_flags & IFLIB_DRIVER_MEDIA) == 0) {
  5138. ctx->ifc_mediap = &ctx->ifc_media;
  5139. ifmedia_init(ctx->ifc_mediap, IFM_IMASK,
  5140. iflib_media_change, iflib_media_status);
  5141. }
  5142. return (0);
  5143. }
  5144. static void
  5145. iflib_unregister_vlan_handlers(if_ctx_t ctx)
  5146. {
  5147. /* Unregister VLAN events */
  5148. if (ctx->ifc_vlan_attach_event != NULL) {
  5149. EVENTHANDLER_DEREGISTER(vlan_config, ctx->ifc_vlan_attach_event);
  5150. ctx->ifc_vlan_attach_event = NULL;
  5151. }
  5152. if (ctx->ifc_vlan_detach_event != NULL) {
  5153. EVENTHANDLER_DEREGISTER(vlan_unconfig, ctx->ifc_vlan_detach_event);
  5154. ctx->ifc_vlan_detach_event = NULL;
  5155. }
  5156. }
  5157. static void
  5158. iflib_deregister(if_ctx_t ctx)
  5159. {
  5160. if_t ifp = ctx->ifc_ifp;
  5161. /* Remove all media */
  5162. ifmedia_removeall(&ctx->ifc_media);
  5163. /* Ensure that VLAN event handlers are unregistered */
  5164. iflib_unregister_vlan_handlers(ctx);
  5165. /* Release kobject reference */
  5166. kobj_delete((kobj_t) ctx, NULL);
  5167. /* Free the ifnet structure */
  5168. if_free(ifp);
  5169. STATE_LOCK_DESTROY(ctx);
  5170. /* ether_ifdetach calls if_qflush - lock must be destroy afterwards*/
  5171. CTX_LOCK_DESTROY(ctx);
  5172. }
  5173. static int
  5174. iflib_queues_alloc(if_ctx_t ctx)
  5175. {
  5176. if_shared_ctx_t sctx = ctx->ifc_sctx;
  5177. if_softc_ctx_t scctx = &ctx->ifc_softc_ctx;
  5178. device_t dev = ctx->ifc_dev;
  5179. int nrxqsets = scctx->isc_nrxqsets;
  5180. int ntxqsets = scctx->isc_ntxqsets;
  5181. iflib_txq_t txq;
  5182. iflib_rxq_t rxq;
  5183. iflib_fl_t fl = NULL;
  5184. int i, j, cpu, err, txconf, rxconf;
  5185. iflib_dma_info_t ifdip;
  5186. uint32_t *rxqsizes = scctx->isc_rxqsizes;
  5187. uint32_t *txqsizes = scctx->isc_txqsizes;
  5188. uint8_t nrxqs = sctx->isc_nrxqs;
  5189. uint8_t ntxqs = sctx->isc_ntxqs;
  5190. int nfree_lists = sctx->isc_nfl ? sctx->isc_nfl : 1;
  5191. int fl_offset = (sctx->isc_flags & IFLIB_HAS_RXCQ ? 1 : 0);
  5192. caddr_t *vaddrs;
  5193. uint64_t *paddrs;
  5194. KASSERT(ntxqs > 0, ("number of queues per qset must be at least 1"));
  5195. KASSERT(nrxqs > 0, ("number of queues per qset must be at least 1"));
  5196. KASSERT(nrxqs >= fl_offset + nfree_lists,
  5197. ("there must be at least a rxq for each free list"));
  5198. /* Allocate the TX ring struct memory */
  5199. if (!(ctx->ifc_txqs =
  5200. (iflib_txq_t) malloc(sizeof(struct iflib_txq) *
  5201. ntxqsets, M_IFLIB, M_NOWAIT | M_ZERO))) {
  5202. device_printf(dev, "Unable to allocate TX ring memory\n");
  5203. err = ENOMEM;
  5204. goto fail;
  5205. }
  5206. /* Now allocate the RX */
  5207. if (!(ctx->ifc_rxqs =
  5208. (iflib_rxq_t) malloc(sizeof(struct iflib_rxq) *
  5209. nrxqsets, M_IFLIB, M_NOWAIT | M_ZERO))) {
  5210. device_printf(dev, "Unable to allocate RX ring memory\n");
  5211. err = ENOMEM;
  5212. goto rx_fail;
  5213. }
  5214. txq = ctx->ifc_txqs;
  5215. rxq = ctx->ifc_rxqs;
  5216. /*
  5217. * XXX handle allocation failure
  5218. */
  5219. for (txconf = i = 0, cpu = CPU_FIRST(); i < ntxqsets; i++, txconf++, txq++, cpu = CPU_NEXT(cpu)) {
  5220. /* Set up some basics */
  5221. if ((ifdip = malloc(sizeof(struct iflib_dma_info) * ntxqs,
  5222. M_IFLIB, M_NOWAIT | M_ZERO)) == NULL) {
  5223. device_printf(dev,
  5224. "Unable to allocate TX DMA info memory\n");
  5225. err = ENOMEM;
  5226. goto err_tx_desc;
  5227. }
  5228. txq->ift_ifdi = ifdip;
  5229. for (j = 0; j < ntxqs; j++, ifdip++) {
  5230. if (iflib_dma_alloc(ctx, txqsizes[j], ifdip, 0)) {
  5231. device_printf(dev,
  5232. "Unable to allocate TX descriptors\n");
  5233. err = ENOMEM;
  5234. goto err_tx_desc;
  5235. }
  5236. txq->ift_txd_size[j] = scctx->isc_txd_size[j];
  5237. bzero((void *)ifdip->idi_vaddr, txqsizes[j]);
  5238. }
  5239. txq->ift_ctx = ctx;
  5240. txq->ift_id = i;
  5241. if (sctx->isc_flags & IFLIB_HAS_TXCQ) {
  5242. txq->ift_br_offset = 1;
  5243. } else {
  5244. txq->ift_br_offset = 0;
  5245. }
  5246. if (iflib_txsd_alloc(txq)) {
  5247. device_printf(dev, "Critical Failure setting up TX buffers\n");
  5248. err = ENOMEM;
  5249. goto err_tx_desc;
  5250. }
  5251. /* Initialize the TX lock */
  5252. snprintf(txq->ift_mtx_name, MTX_NAME_LEN, "%s:TX(%d):callout",
  5253. device_get_nameunit(dev), txq->ift_id);
  5254. mtx_init(&txq->ift_mtx, txq->ift_mtx_name, NULL, MTX_DEF);
  5255. callout_init_mtx(&txq->ift_timer, &txq->ift_mtx, 0);
  5256. txq->ift_timer.c_cpu = cpu;
  5257. #ifdef DEV_NETMAP
  5258. callout_init_mtx(&txq->ift_netmap_timer, &txq->ift_mtx, 0);
  5259. txq->ift_netmap_timer.c_cpu = cpu;
  5260. #endif /* DEV_NETMAP */
  5261. err = ifmp_ring_alloc(&txq->ift_br, 2048, txq, iflib_txq_drain,
  5262. iflib_txq_can_drain, M_IFLIB, M_WAITOK);
  5263. if (err) {
  5264. /* XXX free any allocated rings */
  5265. device_printf(dev, "Unable to allocate buf_ring\n");
  5266. goto err_tx_desc;
  5267. }
  5268. }
  5269. for (rxconf = i = 0; i < nrxqsets; i++, rxconf++, rxq++) {
  5270. /* Set up some basics */
  5271. callout_init(&rxq->ifr_watchdog, 1);
  5272. if ((ifdip = malloc(sizeof(struct iflib_dma_info) * nrxqs,
  5273. M_IFLIB, M_NOWAIT | M_ZERO)) == NULL) {
  5274. device_printf(dev,
  5275. "Unable to allocate RX DMA info memory\n");
  5276. err = ENOMEM;
  5277. goto err_tx_desc;
  5278. }
  5279. rxq->ifr_ifdi = ifdip;
  5280. /* XXX this needs to be changed if #rx queues != #tx queues */
  5281. rxq->ifr_ntxqirq = 1;
  5282. rxq->ifr_txqid[0] = i;
  5283. for (j = 0; j < nrxqs; j++, ifdip++) {
  5284. if (iflib_dma_alloc(ctx, rxqsizes[j], ifdip, 0)) {
  5285. device_printf(dev,
  5286. "Unable to allocate RX descriptors\n");
  5287. err = ENOMEM;
  5288. goto err_tx_desc;
  5289. }
  5290. bzero((void *)ifdip->idi_vaddr, rxqsizes[j]);
  5291. }
  5292. rxq->ifr_ctx = ctx;
  5293. rxq->ifr_id = i;
  5294. rxq->ifr_fl_offset = fl_offset;
  5295. rxq->ifr_nfl = nfree_lists;
  5296. if (!(fl =
  5297. (iflib_fl_t) malloc(sizeof(struct iflib_fl) * nfree_lists, M_IFLIB, M_NOWAIT | M_ZERO))) {
  5298. device_printf(dev, "Unable to allocate free list memory\n");
  5299. err = ENOMEM;
  5300. goto err_tx_desc;
  5301. }
  5302. rxq->ifr_fl = fl;
  5303. for (j = 0; j < nfree_lists; j++) {
  5304. fl[j].ifl_rxq = rxq;
  5305. fl[j].ifl_id = j;
  5306. fl[j].ifl_ifdi = &rxq->ifr_ifdi[j + rxq->ifr_fl_offset];
  5307. fl[j].ifl_rxd_size = scctx->isc_rxd_size[j];
  5308. }
  5309. /* Allocate receive buffers for the ring */
  5310. if (iflib_rxsd_alloc(rxq)) {
  5311. device_printf(dev,
  5312. "Critical Failure setting up receive buffers\n");
  5313. err = ENOMEM;
  5314. goto err_rx_desc;
  5315. }
  5316. for (j = 0, fl = rxq->ifr_fl; j < rxq->ifr_nfl; j++, fl++)
  5317. fl->ifl_rx_bitmap = bit_alloc(fl->ifl_size, M_IFLIB,
  5318. M_WAITOK);
  5319. }
  5320. /* TXQs */
  5321. vaddrs = malloc(sizeof(caddr_t)*ntxqsets*ntxqs, M_IFLIB, M_WAITOK);
  5322. paddrs = malloc(sizeof(uint64_t)*ntxqsets*ntxqs, M_IFLIB, M_WAITOK);
  5323. for (i = 0; i < ntxqsets; i++) {
  5324. iflib_dma_info_t di = ctx->ifc_txqs[i].ift_ifdi;
  5325. for (j = 0; j < ntxqs; j++, di++) {
  5326. vaddrs[i*ntxqs + j] = di->idi_vaddr;
  5327. paddrs[i*ntxqs + j] = di->idi_paddr;
  5328. }
  5329. }
  5330. if ((err = IFDI_TX_QUEUES_ALLOC(ctx, vaddrs, paddrs, ntxqs, ntxqsets)) != 0) {
  5331. device_printf(ctx->ifc_dev,
  5332. "Unable to allocate device TX queue\n");
  5333. iflib_tx_structures_free(ctx);
  5334. free(vaddrs, M_IFLIB);
  5335. free(paddrs, M_IFLIB);
  5336. goto err_rx_desc;
  5337. }
  5338. free(vaddrs, M_IFLIB);
  5339. free(paddrs, M_IFLIB);
  5340. /* RXQs */
  5341. vaddrs = malloc(sizeof(caddr_t)*nrxqsets*nrxqs, M_IFLIB, M_WAITOK);
  5342. paddrs = malloc(sizeof(uint64_t)*nrxqsets*nrxqs, M_IFLIB, M_WAITOK);
  5343. for (i = 0; i < nrxqsets; i++) {
  5344. iflib_dma_info_t di = ctx->ifc_rxqs[i].ifr_ifdi;
  5345. for (j = 0; j < nrxqs; j++, di++) {
  5346. vaddrs[i*nrxqs + j] = di->idi_vaddr;
  5347. paddrs[i*nrxqs + j] = di->idi_paddr;
  5348. }
  5349. }
  5350. if ((err = IFDI_RX_QUEUES_ALLOC(ctx, vaddrs, paddrs, nrxqs, nrxqsets)) != 0) {
  5351. device_printf(ctx->ifc_dev,
  5352. "Unable to allocate device RX queue\n");
  5353. iflib_tx_structures_free(ctx);
  5354. free(vaddrs, M_IFLIB);
  5355. free(paddrs, M_IFLIB);
  5356. goto err_rx_desc;
  5357. }
  5358. free(vaddrs, M_IFLIB);
  5359. free(paddrs, M_IFLIB);
  5360. return (0);
  5361. /* XXX handle allocation failure changes */
  5362. err_rx_desc:
  5363. err_tx_desc:
  5364. rx_fail:
  5365. if (ctx->ifc_rxqs != NULL)
  5366. free(ctx->ifc_rxqs, M_IFLIB);
  5367. ctx->ifc_rxqs = NULL;
  5368. if (ctx->ifc_txqs != NULL)
  5369. free(ctx->ifc_txqs, M_IFLIB);
  5370. ctx->ifc_txqs = NULL;
  5371. fail:
  5372. return (err);
  5373. }
  5374. static int
  5375. iflib_tx_structures_setup(if_ctx_t ctx)
  5376. {
  5377. iflib_txq_t txq = ctx->ifc_txqs;
  5378. int i;
  5379. for (i = 0; i < NTXQSETS(ctx); i++, txq++)
  5380. iflib_txq_setup(txq);
  5381. return (0);
  5382. }
  5383. static void
  5384. iflib_tx_structures_free(if_ctx_t ctx)
  5385. {
  5386. iflib_txq_t txq = ctx->ifc_txqs;
  5387. if_shared_ctx_t sctx = ctx->ifc_sctx;
  5388. int i, j;
  5389. for (i = 0; i < NTXQSETS(ctx); i++, txq++) {
  5390. for (j = 0; j < sctx->isc_ntxqs; j++)
  5391. iflib_dma_free(&txq->ift_ifdi[j]);
  5392. iflib_txq_destroy(txq);
  5393. }
  5394. free(ctx->ifc_txqs, M_IFLIB);
  5395. ctx->ifc_txqs = NULL;
  5396. }
  5397. /*********************************************************************
  5398. *
  5399. * Initialize all receive rings.
  5400. *
  5401. **********************************************************************/
  5402. static int
  5403. iflib_rx_structures_setup(if_ctx_t ctx)
  5404. {
  5405. iflib_rxq_t rxq = ctx->ifc_rxqs;
  5406. int q;
  5407. #if defined(INET6) || defined(INET)
  5408. int err, i;
  5409. #endif
  5410. for (q = 0; q < ctx->ifc_softc_ctx.isc_nrxqsets; q++, rxq++) {
  5411. #if defined(INET6) || defined(INET)
  5412. err = tcp_lro_init_args(&rxq->ifr_lc, ctx->ifc_ifp,
  5413. TCP_LRO_ENTRIES, min(1024,
  5414. ctx->ifc_softc_ctx.isc_nrxd[rxq->ifr_fl_offset]));
  5415. if (err != 0) {
  5416. device_printf(ctx->ifc_dev,
  5417. "LRO Initialization failed!\n");
  5418. goto fail;
  5419. }
  5420. #endif
  5421. IFDI_RXQ_SETUP(ctx, rxq->ifr_id);
  5422. }
  5423. return (0);
  5424. #if defined(INET6) || defined(INET)
  5425. fail:
  5426. /*
  5427. * Free LRO resources allocated so far, we will only handle
  5428. * the rings that completed, the failing case will have
  5429. * cleaned up for itself. 'q' failed, so its the terminus.
  5430. */
  5431. rxq = ctx->ifc_rxqs;
  5432. for (i = 0; i < q; ++i, rxq++) {
  5433. tcp_lro_free(&rxq->ifr_lc);
  5434. }
  5435. return (err);
  5436. #endif
  5437. }
  5438. /*********************************************************************
  5439. *
  5440. * Free all receive rings.
  5441. *
  5442. **********************************************************************/
  5443. static void
  5444. iflib_rx_structures_free(if_ctx_t ctx)
  5445. {
  5446. iflib_rxq_t rxq = ctx->ifc_rxqs;
  5447. if_shared_ctx_t sctx = ctx->ifc_sctx;
  5448. int i, j;
  5449. for (i = 0; i < ctx->ifc_softc_ctx.isc_nrxqsets; i++, rxq++) {
  5450. for (j = 0; j < sctx->isc_nrxqs; j++)
  5451. iflib_dma_free(&rxq->ifr_ifdi[j]);
  5452. iflib_rx_sds_free(rxq);
  5453. #if defined(INET6) || defined(INET)
  5454. tcp_lro_free(&rxq->ifr_lc);
  5455. #endif
  5456. }
  5457. free(ctx->ifc_rxqs, M_IFLIB);
  5458. ctx->ifc_rxqs = NULL;
  5459. }
  5460. static int
  5461. iflib_qset_structures_setup(if_ctx_t ctx)
  5462. {
  5463. int err;
  5464. /*
  5465. * It is expected that the caller takes care of freeing queues if this
  5466. * fails.
  5467. */
  5468. if ((err = iflib_tx_structures_setup(ctx)) != 0) {
  5469. device_printf(ctx->ifc_dev, "iflib_tx_structures_setup failed: %d\n", err);
  5470. return (err);
  5471. }
  5472. if ((err = iflib_rx_structures_setup(ctx)) != 0)
  5473. device_printf(ctx->ifc_dev, "iflib_rx_structures_setup failed: %d\n", err);
  5474. return (err);
  5475. }
  5476. int
  5477. iflib_irq_alloc(if_ctx_t ctx, if_irq_t irq, int rid,
  5478. driver_filter_t filter, void *filter_arg, driver_intr_t handler, void *arg, const char *name)
  5479. {
  5480. return (_iflib_irq_alloc(ctx, irq, rid, filter, handler, arg, name));
  5481. }
  5482. /* Just to avoid copy/paste */
  5483. static inline int
  5484. iflib_irq_set_affinity(if_ctx_t ctx, if_irq_t irq, iflib_intr_type_t type,
  5485. int qid, struct grouptask *gtask, struct taskqgroup *tqg, void *uniq,
  5486. const char *name)
  5487. {
  5488. device_t dev;
  5489. unsigned int base_cpuid, cpuid;
  5490. int err;
  5491. dev = ctx->ifc_dev;
  5492. base_cpuid = ctx->ifc_sysctl_core_offset;
  5493. cpuid = get_cpuid_for_queue(ctx, base_cpuid, qid, type == IFLIB_INTR_TX);
  5494. err = taskqgroup_attach_cpu(tqg, gtask, uniq, cpuid, dev,
  5495. irq ? irq->ii_res : NULL, name);
  5496. if (err) {
  5497. device_printf(dev, "taskqgroup_attach_cpu failed %d\n", err);
  5498. return (err);
  5499. }
  5500. #ifdef notyet
  5501. if (cpuid > ctx->ifc_cpuid_highest)
  5502. ctx->ifc_cpuid_highest = cpuid;
  5503. #endif
  5504. return (0);
  5505. }
  5506. /*
  5507. * Allocate a hardware interrupt for subctx using the parent (ctx)'s hardware
  5508. * resources.
  5509. *
  5510. * Similar to iflib_irq_alloc_generic(), but for interrupt type IFLIB_INTR_RXTX
  5511. * only.
  5512. *
  5513. * XXX: Could be removed if subctx's dev has its intr resource allocation
  5514. * methods replaced with custom ones?
  5515. */
  5516. int
  5517. iflib_irq_alloc_generic_subctx(if_ctx_t ctx, if_ctx_t subctx, if_irq_t irq,
  5518. int rid, iflib_intr_type_t type,
  5519. driver_filter_t *filter, void *filter_arg,
  5520. int qid, const char *name)
  5521. {
  5522. device_t dev, subdev;
  5523. struct grouptask *gtask;
  5524. struct taskqgroup *tqg;
  5525. iflib_filter_info_t info;
  5526. gtask_fn_t *fn;
  5527. int tqrid, err;
  5528. driver_filter_t *intr_fast;
  5529. void *q;
  5530. MPASS(ctx != NULL);
  5531. MPASS(subctx != NULL);
  5532. tqrid = rid;
  5533. dev = ctx->ifc_dev;
  5534. subdev = subctx->ifc_dev;
  5535. switch (type) {
  5536. case IFLIB_INTR_RXTX:
  5537. q = &subctx->ifc_rxqs[qid];
  5538. info = &subctx->ifc_rxqs[qid].ifr_filter_info;
  5539. gtask = &subctx->ifc_rxqs[qid].ifr_task;
  5540. tqg = qgroup_if_io_tqg;
  5541. fn = _task_fn_rx;
  5542. intr_fast = iflib_fast_intr_rxtx;
  5543. NET_GROUPTASK_INIT(gtask, 0, fn, q);
  5544. break;
  5545. default:
  5546. device_printf(dev, "%s: unknown net intr type for subctx %s (%d)\n",
  5547. __func__, device_get_nameunit(subdev), type);
  5548. return (EINVAL);
  5549. }
  5550. info->ifi_filter = filter;
  5551. info->ifi_filter_arg = filter_arg;
  5552. info->ifi_task = gtask;
  5553. info->ifi_ctx = q;
  5554. NET_GROUPTASK_INIT(gtask, 0, fn, q);
  5555. /* Allocate interrupts from hardware using parent context */
  5556. err = _iflib_irq_alloc(ctx, irq, rid, intr_fast, NULL, info, name);
  5557. if (err != 0) {
  5558. device_printf(dev, "_iflib_irq_alloc failed for subctx %s: %d\n",
  5559. device_get_nameunit(subdev), err);
  5560. return (err);
  5561. }
  5562. if (tqrid != -1) {
  5563. err = iflib_irq_set_affinity(ctx, irq, type, qid, gtask, tqg, q,
  5564. name);
  5565. if (err)
  5566. return (err);
  5567. } else {
  5568. taskqgroup_attach(tqg, gtask, q, dev, irq->ii_res, name);
  5569. }
  5570. return (0);
  5571. }
  5572. int
  5573. iflib_irq_alloc_generic(if_ctx_t ctx, if_irq_t irq, int rid,
  5574. iflib_intr_type_t type, driver_filter_t *filter,
  5575. void *filter_arg, int qid, const char *name)
  5576. {
  5577. device_t dev;
  5578. struct grouptask *gtask;
  5579. struct taskqgroup *tqg;
  5580. iflib_filter_info_t info;
  5581. gtask_fn_t *fn;
  5582. int tqrid, err;
  5583. driver_filter_t *intr_fast;
  5584. void *q;
  5585. info = &ctx->ifc_filter_info;
  5586. tqrid = rid;
  5587. switch (type) {
  5588. /* XXX merge tx/rx for netmap? */
  5589. case IFLIB_INTR_TX:
  5590. q = &ctx->ifc_txqs[qid];
  5591. info = &ctx->ifc_txqs[qid].ift_filter_info;
  5592. gtask = &ctx->ifc_txqs[qid].ift_task;
  5593. tqg = qgroup_if_io_tqg;
  5594. fn = _task_fn_tx;
  5595. intr_fast = iflib_fast_intr;
  5596. GROUPTASK_INIT(gtask, 0, fn, q);
  5597. ctx->ifc_flags |= IFC_NETMAP_TX_IRQ;
  5598. break;
  5599. case IFLIB_INTR_RX:
  5600. q = &ctx->ifc_rxqs[qid];
  5601. info = &ctx->ifc_rxqs[qid].ifr_filter_info;
  5602. gtask = &ctx->ifc_rxqs[qid].ifr_task;
  5603. tqg = qgroup_if_io_tqg;
  5604. fn = _task_fn_rx;
  5605. intr_fast = iflib_fast_intr;
  5606. NET_GROUPTASK_INIT(gtask, 0, fn, q);
  5607. break;
  5608. case IFLIB_INTR_RXTX:
  5609. q = &ctx->ifc_rxqs[qid];
  5610. info = &ctx->ifc_rxqs[qid].ifr_filter_info;
  5611. gtask = &ctx->ifc_rxqs[qid].ifr_task;
  5612. tqg = qgroup_if_io_tqg;
  5613. fn = _task_fn_rx;
  5614. intr_fast = iflib_fast_intr_rxtx;
  5615. NET_GROUPTASK_INIT(gtask, 0, fn, q);
  5616. break;
  5617. case IFLIB_INTR_ADMIN:
  5618. q = ctx;
  5619. tqrid = -1;
  5620. info = &ctx->ifc_filter_info;
  5621. gtask = &ctx->ifc_admin_task;
  5622. tqg = qgroup_if_config_tqg;
  5623. fn = _task_fn_admin;
  5624. intr_fast = iflib_fast_intr_ctx;
  5625. break;
  5626. default:
  5627. device_printf(ctx->ifc_dev, "%s: unknown net intr type\n",
  5628. __func__);
  5629. return (EINVAL);
  5630. }
  5631. info->ifi_filter = filter;
  5632. info->ifi_filter_arg = filter_arg;
  5633. info->ifi_task = gtask;
  5634. info->ifi_ctx = q;
  5635. dev = ctx->ifc_dev;
  5636. err = _iflib_irq_alloc(ctx, irq, rid, intr_fast, NULL, info, name);
  5637. if (err != 0) {
  5638. device_printf(dev, "_iflib_irq_alloc failed %d\n", err);
  5639. return (err);
  5640. }
  5641. if (type == IFLIB_INTR_ADMIN)
  5642. return (0);
  5643. if (tqrid != -1) {
  5644. err = iflib_irq_set_affinity(ctx, irq, type, qid, gtask, tqg, q,
  5645. name);
  5646. if (err)
  5647. return (err);
  5648. } else {
  5649. taskqgroup_attach(tqg, gtask, q, dev, irq->ii_res, name);
  5650. }
  5651. return (0);
  5652. }
  5653. void
  5654. iflib_softirq_alloc_generic(if_ctx_t ctx, if_irq_t irq, iflib_intr_type_t type,
  5655. void *arg, int qid, const char *name)
  5656. {
  5657. device_t dev;
  5658. struct grouptask *gtask;
  5659. struct taskqgroup *tqg;
  5660. gtask_fn_t *fn;
  5661. void *q;
  5662. int err;
  5663. switch (type) {
  5664. case IFLIB_INTR_TX:
  5665. q = &ctx->ifc_txqs[qid];
  5666. gtask = &ctx->ifc_txqs[qid].ift_task;
  5667. tqg = qgroup_if_io_tqg;
  5668. fn = _task_fn_tx;
  5669. GROUPTASK_INIT(gtask, 0, fn, q);
  5670. break;
  5671. case IFLIB_INTR_RX:
  5672. q = &ctx->ifc_rxqs[qid];
  5673. gtask = &ctx->ifc_rxqs[qid].ifr_task;
  5674. tqg = qgroup_if_io_tqg;
  5675. fn = _task_fn_rx;
  5676. NET_GROUPTASK_INIT(gtask, 0, fn, q);
  5677. break;
  5678. case IFLIB_INTR_IOV:
  5679. q = ctx;
  5680. gtask = &ctx->ifc_vflr_task;
  5681. tqg = qgroup_if_config_tqg;
  5682. fn = _task_fn_iov;
  5683. GROUPTASK_INIT(gtask, 0, fn, q);
  5684. break;
  5685. default:
  5686. panic("unknown net intr type");
  5687. }
  5688. err = iflib_irq_set_affinity(ctx, irq, type, qid, gtask, tqg, q, name);
  5689. if (err) {
  5690. dev = ctx->ifc_dev;
  5691. taskqgroup_attach(tqg, gtask, q, dev, irq ? irq->ii_res : NULL,
  5692. name);
  5693. }
  5694. }
  5695. void
  5696. iflib_irq_free(if_ctx_t ctx, if_irq_t irq)
  5697. {
  5698. if (irq->ii_tag)
  5699. bus_teardown_intr(ctx->ifc_dev, irq->ii_res, irq->ii_tag);
  5700. if (irq->ii_res)
  5701. bus_release_resource(ctx->ifc_dev, SYS_RES_IRQ,
  5702. rman_get_rid(irq->ii_res), irq->ii_res);
  5703. }
  5704. static int
  5705. iflib_legacy_setup(if_ctx_t ctx, driver_filter_t filter, void *filter_arg, int *rid, const char *name)
  5706. {
  5707. iflib_txq_t txq = ctx->ifc_txqs;
  5708. iflib_rxq_t rxq = ctx->ifc_rxqs;
  5709. if_irq_t irq = &ctx->ifc_legacy_irq;
  5710. iflib_filter_info_t info;
  5711. device_t dev;
  5712. struct grouptask *gtask;
  5713. struct resource *res;
  5714. struct taskqgroup *tqg;
  5715. void *q;
  5716. int err, tqrid;
  5717. bool rx_only;
  5718. q = &ctx->ifc_rxqs[0];
  5719. info = &rxq[0].ifr_filter_info;
  5720. gtask = &rxq[0].ifr_task;
  5721. tqg = qgroup_if_io_tqg;
  5722. tqrid = *rid;
  5723. rx_only = (ctx->ifc_sctx->isc_flags & IFLIB_SINGLE_IRQ_RX_ONLY) != 0;
  5724. ctx->ifc_flags |= IFC_LEGACY;
  5725. info->ifi_filter = filter;
  5726. info->ifi_filter_arg = filter_arg;
  5727. info->ifi_task = gtask;
  5728. info->ifi_ctx = rx_only ? ctx : q;
  5729. dev = ctx->ifc_dev;
  5730. /* We allocate a single interrupt resource */
  5731. err = _iflib_irq_alloc(ctx, irq, tqrid, rx_only ? iflib_fast_intr_ctx :
  5732. iflib_fast_intr_rxtx, NULL, info, name);
  5733. if (err != 0)
  5734. return (err);
  5735. NET_GROUPTASK_INIT(gtask, 0, _task_fn_rx, q);
  5736. res = irq->ii_res;
  5737. taskqgroup_attach(tqg, gtask, q, dev, res, name);
  5738. GROUPTASK_INIT(&txq->ift_task, 0, _task_fn_tx, txq);
  5739. taskqgroup_attach(qgroup_if_io_tqg, &txq->ift_task, txq, dev, res,
  5740. "tx");
  5741. return (0);
  5742. }
  5743. void
  5744. iflib_led_create(if_ctx_t ctx)
  5745. {
  5746. ctx->ifc_led_dev = led_create(iflib_led_func, ctx,
  5747. device_get_nameunit(ctx->ifc_dev));
  5748. }
  5749. void
  5750. iflib_tx_intr_deferred(if_ctx_t ctx, int txqid)
  5751. {
  5752. GROUPTASK_ENQUEUE(&ctx->ifc_txqs[txqid].ift_task);
  5753. }
  5754. void
  5755. iflib_rx_intr_deferred(if_ctx_t ctx, int rxqid)
  5756. {
  5757. GROUPTASK_ENQUEUE(&ctx->ifc_rxqs[rxqid].ifr_task);
  5758. }
  5759. void
  5760. iflib_admin_intr_deferred(if_ctx_t ctx)
  5761. {
  5762. MPASS(ctx->ifc_admin_task.gt_taskqueue != NULL);
  5763. GROUPTASK_ENQUEUE(&ctx->ifc_admin_task);
  5764. }
  5765. void
  5766. iflib_iov_intr_deferred(if_ctx_t ctx)
  5767. {
  5768. GROUPTASK_ENQUEUE(&ctx->ifc_vflr_task);
  5769. }
  5770. void
  5771. iflib_io_tqg_attach(struct grouptask *gt, void *uniq, int cpu, const char *name)
  5772. {
  5773. taskqgroup_attach_cpu(qgroup_if_io_tqg, gt, uniq, cpu, NULL, NULL,
  5774. name);
  5775. }
  5776. void
  5777. iflib_config_gtask_init(void *ctx, struct grouptask *gtask, gtask_fn_t *fn,
  5778. const char *name)
  5779. {
  5780. GROUPTASK_INIT(gtask, 0, fn, ctx);
  5781. taskqgroup_attach(qgroup_if_config_tqg, gtask, gtask, NULL, NULL,
  5782. name);
  5783. }
  5784. void
  5785. iflib_config_gtask_deinit(struct grouptask *gtask)
  5786. {
  5787. taskqgroup_detach(qgroup_if_config_tqg, gtask);
  5788. }
  5789. void
  5790. iflib_link_state_change(if_ctx_t ctx, int link_state, uint64_t baudrate)
  5791. {
  5792. if_t ifp = ctx->ifc_ifp;
  5793. iflib_txq_t txq = ctx->ifc_txqs;
  5794. if_setbaudrate(ifp, baudrate);
  5795. if (baudrate >= IF_Gbps(10)) {
  5796. STATE_LOCK(ctx);
  5797. ctx->ifc_flags |= IFC_PREFETCH;
  5798. STATE_UNLOCK(ctx);
  5799. }
  5800. /* If link down, disable watchdog */
  5801. if ((ctx->ifc_link_state == LINK_STATE_UP) && (link_state == LINK_STATE_DOWN)) {
  5802. for (int i = 0; i < ctx->ifc_softc_ctx.isc_ntxqsets; i++, txq++)
  5803. txq->ift_qstatus = IFLIB_QUEUE_IDLE;
  5804. }
  5805. ctx->ifc_link_state = link_state;
  5806. if_link_state_change(ifp, link_state);
  5807. }
  5808. static int
  5809. iflib_tx_credits_update(if_ctx_t ctx, iflib_txq_t txq)
  5810. {
  5811. int credits;
  5812. #ifdef INVARIANTS
  5813. int credits_pre = txq->ift_cidx_processed;
  5814. #endif
  5815. bus_dmamap_sync(txq->ift_ifdi->idi_tag, txq->ift_ifdi->idi_map,
  5816. BUS_DMASYNC_POSTREAD);
  5817. if ((credits = ctx->isc_txd_credits_update(ctx->ifc_softc, txq->ift_id, true)) == 0)
  5818. return (0);
  5819. txq->ift_processed += credits;
  5820. txq->ift_cidx_processed += credits;
  5821. MPASS(credits_pre + credits == txq->ift_cidx_processed);
  5822. if (txq->ift_cidx_processed >= txq->ift_size)
  5823. txq->ift_cidx_processed -= txq->ift_size;
  5824. return (credits);
  5825. }
  5826. static int
  5827. iflib_rxd_avail(if_ctx_t ctx, iflib_rxq_t rxq, qidx_t cidx, qidx_t budget)
  5828. {
  5829. iflib_fl_t fl;
  5830. u_int i;
  5831. for (i = 0, fl = &rxq->ifr_fl[0]; i < rxq->ifr_nfl; i++, fl++)
  5832. bus_dmamap_sync(fl->ifl_ifdi->idi_tag, fl->ifl_ifdi->idi_map,
  5833. BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
  5834. return (ctx->isc_rxd_available(ctx->ifc_softc, rxq->ifr_id, cidx,
  5835. budget));
  5836. }
  5837. void
  5838. iflib_add_int_delay_sysctl(if_ctx_t ctx, const char *name,
  5839. const char *description, if_int_delay_info_t info,
  5840. int offset, int value)
  5841. {
  5842. info->iidi_ctx = ctx;
  5843. info->iidi_offset = offset;
  5844. info->iidi_value = value;
  5845. SYSCTL_ADD_PROC(device_get_sysctl_ctx(ctx->ifc_dev),
  5846. SYSCTL_CHILDREN(device_get_sysctl_tree(ctx->ifc_dev)),
  5847. OID_AUTO, name, CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
  5848. info, 0, iflib_sysctl_int_delay, "I", description);
  5849. }
  5850. struct sx *
  5851. iflib_ctx_lock_get(if_ctx_t ctx)
  5852. {
  5853. return (&ctx->ifc_ctx_sx);
  5854. }
  5855. static int
  5856. iflib_msix_init(if_ctx_t ctx)
  5857. {
  5858. device_t dev = ctx->ifc_dev;
  5859. if_shared_ctx_t sctx = ctx->ifc_sctx;
  5860. if_softc_ctx_t scctx = &ctx->ifc_softc_ctx;
  5861. int admincnt, bar, err, iflib_num_rx_queues, iflib_num_tx_queues;
  5862. int msgs, queuemsgs, queues, rx_queues, tx_queues, vectors;
  5863. iflib_num_tx_queues = ctx->ifc_sysctl_ntxqs;
  5864. iflib_num_rx_queues = ctx->ifc_sysctl_nrxqs;
  5865. if (bootverbose)
  5866. device_printf(dev, "msix_init qsets capped at %d\n",
  5867. imax(scctx->isc_ntxqsets, scctx->isc_nrxqsets));
  5868. /* Override by tuneable */
  5869. if (scctx->isc_disable_msix)
  5870. goto msi;
  5871. /* First try MSI-X */
  5872. if ((msgs = pci_msix_count(dev)) == 0) {
  5873. if (bootverbose)
  5874. device_printf(dev, "MSI-X not supported or disabled\n");
  5875. goto msi;
  5876. }
  5877. bar = ctx->ifc_softc_ctx.isc_msix_bar;
  5878. /*
  5879. * bar == -1 => "trust me I know what I'm doing"
  5880. * Some drivers are for hardware that is so shoddily
  5881. * documented that no one knows which bars are which
  5882. * so the developer has to map all bars. This hack
  5883. * allows shoddy garbage to use MSI-X in this framework.
  5884. */
  5885. if (bar != -1) {
  5886. ctx->ifc_msix_mem = bus_alloc_resource_any(dev,
  5887. SYS_RES_MEMORY, &bar, RF_ACTIVE);
  5888. if (ctx->ifc_msix_mem == NULL) {
  5889. device_printf(dev, "Unable to map MSI-X table\n");
  5890. goto msi;
  5891. }
  5892. }
  5893. admincnt = sctx->isc_admin_intrcnt;
  5894. #if IFLIB_DEBUG
  5895. /* use only 1 qset in debug mode */
  5896. queuemsgs = min(msgs - admincnt, 1);
  5897. #else
  5898. queuemsgs = msgs - admincnt;
  5899. #endif
  5900. #ifdef RSS
  5901. queues = imin(queuemsgs, rss_getnumbuckets());
  5902. #else
  5903. queues = queuemsgs;
  5904. #endif
  5905. queues = imin(CPU_COUNT(&ctx->ifc_cpus), queues);
  5906. if (bootverbose)
  5907. device_printf(dev,
  5908. "intr CPUs: %d queue msgs: %d admincnt: %d\n",
  5909. CPU_COUNT(&ctx->ifc_cpus), queuemsgs, admincnt);
  5910. #ifdef RSS
  5911. /* If we're doing RSS, clamp at the number of RSS buckets */
  5912. if (queues > rss_getnumbuckets())
  5913. queues = rss_getnumbuckets();
  5914. #endif
  5915. if (iflib_num_rx_queues > 0 && iflib_num_rx_queues < queuemsgs - admincnt)
  5916. rx_queues = iflib_num_rx_queues;
  5917. else
  5918. rx_queues = queues;
  5919. if (rx_queues > scctx->isc_nrxqsets)
  5920. rx_queues = scctx->isc_nrxqsets;
  5921. /*
  5922. * We want this to be all logical CPUs by default
  5923. */
  5924. if (iflib_num_tx_queues > 0 && iflib_num_tx_queues < queues)
  5925. tx_queues = iflib_num_tx_queues;
  5926. else
  5927. tx_queues = mp_ncpus;
  5928. if (tx_queues > scctx->isc_ntxqsets)
  5929. tx_queues = scctx->isc_ntxqsets;
  5930. if (ctx->ifc_sysctl_qs_eq_override == 0) {
  5931. #ifdef INVARIANTS
  5932. if (tx_queues != rx_queues)
  5933. device_printf(dev,
  5934. "queue equality override not set, capping rx_queues at %d and tx_queues at %d\n",
  5935. min(rx_queues, tx_queues), min(rx_queues, tx_queues));
  5936. #endif
  5937. tx_queues = min(rx_queues, tx_queues);
  5938. rx_queues = min(rx_queues, tx_queues);
  5939. }
  5940. vectors = rx_queues + admincnt;
  5941. if (msgs < vectors) {
  5942. device_printf(dev,
  5943. "insufficient number of MSI-X vectors "
  5944. "(supported %d, need %d)\n", msgs, vectors);
  5945. goto msi;
  5946. }
  5947. device_printf(dev, "Using %d RX queues %d TX queues\n", rx_queues,
  5948. tx_queues);
  5949. msgs = vectors;
  5950. if ((err = pci_alloc_msix(dev, &vectors)) == 0) {
  5951. if (vectors != msgs) {
  5952. device_printf(dev,
  5953. "Unable to allocate sufficient MSI-X vectors "
  5954. "(got %d, need %d)\n", vectors, msgs);
  5955. pci_release_msi(dev);
  5956. if (bar != -1) {
  5957. bus_release_resource(dev, SYS_RES_MEMORY, bar,
  5958. ctx->ifc_msix_mem);
  5959. ctx->ifc_msix_mem = NULL;
  5960. }
  5961. goto msi;
  5962. }
  5963. device_printf(dev, "Using MSI-X interrupts with %d vectors\n",
  5964. vectors);
  5965. scctx->isc_vectors = vectors;
  5966. scctx->isc_nrxqsets = rx_queues;
  5967. scctx->isc_ntxqsets = tx_queues;
  5968. scctx->isc_intr = IFLIB_INTR_MSIX;
  5969. return (vectors);
  5970. } else {
  5971. device_printf(dev,
  5972. "failed to allocate %d MSI-X vectors, err: %d\n", vectors,
  5973. err);
  5974. if (bar != -1) {
  5975. bus_release_resource(dev, SYS_RES_MEMORY, bar,
  5976. ctx->ifc_msix_mem);
  5977. ctx->ifc_msix_mem = NULL;
  5978. }
  5979. }
  5980. msi:
  5981. vectors = pci_msi_count(dev);
  5982. scctx->isc_nrxqsets = 1;
  5983. scctx->isc_ntxqsets = 1;
  5984. scctx->isc_vectors = vectors;
  5985. if (vectors == 1 && pci_alloc_msi(dev, &vectors) == 0) {
  5986. device_printf(dev,"Using an MSI interrupt\n");
  5987. scctx->isc_intr = IFLIB_INTR_MSI;
  5988. } else {
  5989. scctx->isc_vectors = 1;
  5990. device_printf(dev,"Using a Legacy interrupt\n");
  5991. scctx->isc_intr = IFLIB_INTR_LEGACY;
  5992. }
  5993. return (vectors);
  5994. }
  5995. static const char *ring_states[] = { "IDLE", "BUSY", "STALLED", "ABDICATED" };
  5996. static int
  5997. mp_ring_state_handler(SYSCTL_HANDLER_ARGS)
  5998. {
  5999. int rc;
  6000. uint16_t *state = ((uint16_t *)oidp->oid_arg1);
  6001. struct sbuf *sb;
  6002. const char *ring_state = "UNKNOWN";
  6003. /* XXX needed ? */
  6004. rc = sysctl_wire_old_buffer(req, 0);
  6005. MPASS(rc == 0);
  6006. if (rc != 0)
  6007. return (rc);
  6008. sb = sbuf_new_for_sysctl(NULL, NULL, 80, req);
  6009. MPASS(sb != NULL);
  6010. if (sb == NULL)
  6011. return (ENOMEM);
  6012. if (state[3] <= 3)
  6013. ring_state = ring_states[state[3]];
  6014. sbuf_printf(sb, "pidx_head: %04hd pidx_tail: %04hd cidx: %04hd state: %s",
  6015. state[0], state[1], state[2], ring_state);
  6016. rc = sbuf_finish(sb);
  6017. sbuf_delete(sb);
  6018. return(rc);
  6019. }
  6020. enum iflib_ndesc_handler {
  6021. IFLIB_NTXD_HANDLER,
  6022. IFLIB_NRXD_HANDLER,
  6023. };
  6024. static int
  6025. mp_ndesc_handler(SYSCTL_HANDLER_ARGS)
  6026. {
  6027. if_ctx_t ctx = (void *)arg1;
  6028. enum iflib_ndesc_handler type = arg2;
  6029. char buf[256] = {0};
  6030. qidx_t *ndesc;
  6031. char *p, *next;
  6032. int nqs, rc, i;
  6033. nqs = 8;
  6034. switch(type) {
  6035. case IFLIB_NTXD_HANDLER:
  6036. ndesc = ctx->ifc_sysctl_ntxds;
  6037. if (ctx->ifc_sctx)
  6038. nqs = ctx->ifc_sctx->isc_ntxqs;
  6039. break;
  6040. case IFLIB_NRXD_HANDLER:
  6041. ndesc = ctx->ifc_sysctl_nrxds;
  6042. if (ctx->ifc_sctx)
  6043. nqs = ctx->ifc_sctx->isc_nrxqs;
  6044. break;
  6045. default:
  6046. printf("%s: unhandled type\n", __func__);
  6047. return (EINVAL);
  6048. }
  6049. if (nqs == 0)
  6050. nqs = 8;
  6051. for (i=0; i<8; i++) {
  6052. if (i >= nqs)
  6053. break;
  6054. if (i)
  6055. strcat(buf, ",");
  6056. sprintf(strchr(buf, 0), "%d", ndesc[i]);
  6057. }
  6058. rc = sysctl_handle_string(oidp, buf, sizeof(buf), req);
  6059. if (rc || req->newptr == NULL)
  6060. return rc;
  6061. for (i = 0, next = buf, p = strsep(&next, " ,"); i < 8 && p;
  6062. i++, p = strsep(&next, " ,")) {
  6063. ndesc[i] = strtoul(p, NULL, 10);
  6064. }
  6065. return(rc);
  6066. }
  6067. #define NAME_BUFLEN 32
  6068. static void
  6069. iflib_add_device_sysctl_pre(if_ctx_t ctx)
  6070. {
  6071. device_t dev = iflib_get_dev(ctx);
  6072. struct sysctl_oid_list *child, *oid_list;
  6073. struct sysctl_ctx_list *ctx_list;
  6074. struct sysctl_oid *node;
  6075. ctx_list = device_get_sysctl_ctx(dev);
  6076. child = SYSCTL_CHILDREN(device_get_sysctl_tree(dev));
  6077. ctx->ifc_sysctl_node = node = SYSCTL_ADD_NODE(ctx_list, child,
  6078. OID_AUTO, "iflib", CTLFLAG_RD | CTLFLAG_MPSAFE, NULL,
  6079. "IFLIB fields");
  6080. oid_list = SYSCTL_CHILDREN(node);
  6081. SYSCTL_ADD_CONST_STRING(ctx_list, oid_list, OID_AUTO, "driver_version",
  6082. CTLFLAG_RD, ctx->ifc_sctx->isc_driver_version, "driver version");
  6083. SYSCTL_ADD_U16(ctx_list, oid_list, OID_AUTO, "override_ntxqs",
  6084. CTLFLAG_RWTUN, &ctx->ifc_sysctl_ntxqs, 0,
  6085. "# of txqs to use, 0 => use default #");
  6086. SYSCTL_ADD_U16(ctx_list, oid_list, OID_AUTO, "override_nrxqs",
  6087. CTLFLAG_RWTUN, &ctx->ifc_sysctl_nrxqs, 0,
  6088. "# of rxqs to use, 0 => use default #");
  6089. SYSCTL_ADD_U16(ctx_list, oid_list, OID_AUTO, "override_qs_enable",
  6090. CTLFLAG_RWTUN, &ctx->ifc_sysctl_qs_eq_override, 0,
  6091. "permit #txq != #rxq");
  6092. SYSCTL_ADD_INT(ctx_list, oid_list, OID_AUTO, "disable_msix",
  6093. CTLFLAG_RWTUN, &ctx->ifc_softc_ctx.isc_disable_msix, 0,
  6094. "disable MSI-X (default 0)");
  6095. SYSCTL_ADD_U16(ctx_list, oid_list, OID_AUTO, "rx_budget",
  6096. CTLFLAG_RWTUN, &ctx->ifc_sysctl_rx_budget, 0, "set the RX budget");
  6097. SYSCTL_ADD_U16(ctx_list, oid_list, OID_AUTO, "tx_abdicate",
  6098. CTLFLAG_RWTUN, &ctx->ifc_sysctl_tx_abdicate, 0,
  6099. "cause TX to abdicate instead of running to completion");
  6100. ctx->ifc_sysctl_core_offset = CORE_OFFSET_UNSPECIFIED;
  6101. SYSCTL_ADD_U16(ctx_list, oid_list, OID_AUTO, "core_offset",
  6102. CTLFLAG_RDTUN, &ctx->ifc_sysctl_core_offset, 0,
  6103. "offset to start using cores at");
  6104. SYSCTL_ADD_U8(ctx_list, oid_list, OID_AUTO, "separate_txrx",
  6105. CTLFLAG_RDTUN, &ctx->ifc_sysctl_separate_txrx, 0,
  6106. "use separate cores for TX and RX");
  6107. SYSCTL_ADD_U8(ctx_list, oid_list, OID_AUTO, "use_logical_cores",
  6108. CTLFLAG_RDTUN, &ctx->ifc_sysctl_use_logical_cores, 0,
  6109. "try to make use of logical cores for TX and RX");
  6110. SYSCTL_ADD_U16(ctx_list, oid_list, OID_AUTO, "use_extra_msix_vectors",
  6111. CTLFLAG_RDTUN, &ctx->ifc_sysctl_extra_msix_vectors, 0,
  6112. "attempt to reserve the given number of extra MSI-X vectors during driver load for the creation of additional interfaces later");
  6113. SYSCTL_ADD_INT(ctx_list, oid_list, OID_AUTO, "allocated_msix_vectors",
  6114. CTLFLAG_RDTUN, &ctx->ifc_softc_ctx.isc_vectors, 0,
  6115. "total # of MSI-X vectors allocated by driver");
  6116. /* XXX change for per-queue sizes */
  6117. SYSCTL_ADD_PROC(ctx_list, oid_list, OID_AUTO, "override_ntxds",
  6118. CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_NEEDGIANT, ctx,
  6119. IFLIB_NTXD_HANDLER, mp_ndesc_handler, "A",
  6120. "list of # of TX descriptors to use, 0 = use default #");
  6121. SYSCTL_ADD_PROC(ctx_list, oid_list, OID_AUTO, "override_nrxds",
  6122. CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_NEEDGIANT, ctx,
  6123. IFLIB_NRXD_HANDLER, mp_ndesc_handler, "A",
  6124. "list of # of RX descriptors to use, 0 = use default #");
  6125. }
  6126. static void
  6127. iflib_add_device_sysctl_post(if_ctx_t ctx)
  6128. {
  6129. if_shared_ctx_t sctx = ctx->ifc_sctx;
  6130. if_softc_ctx_t scctx = &ctx->ifc_softc_ctx;
  6131. device_t dev = iflib_get_dev(ctx);
  6132. struct sysctl_oid_list *child;
  6133. struct sysctl_ctx_list *ctx_list;
  6134. iflib_fl_t fl;
  6135. iflib_txq_t txq;
  6136. iflib_rxq_t rxq;
  6137. int i, j;
  6138. char namebuf[NAME_BUFLEN];
  6139. char *qfmt;
  6140. struct sysctl_oid *queue_node, *fl_node, *node;
  6141. struct sysctl_oid_list *queue_list, *fl_list;
  6142. ctx_list = device_get_sysctl_ctx(dev);
  6143. node = ctx->ifc_sysctl_node;
  6144. child = SYSCTL_CHILDREN(node);
  6145. if (scctx->isc_ntxqsets > 100)
  6146. qfmt = "txq%03d";
  6147. else if (scctx->isc_ntxqsets > 10)
  6148. qfmt = "txq%02d";
  6149. else
  6150. qfmt = "txq%d";
  6151. for (i = 0, txq = ctx->ifc_txqs; i < scctx->isc_ntxqsets; i++, txq++) {
  6152. snprintf(namebuf, NAME_BUFLEN, qfmt, i);
  6153. queue_node = SYSCTL_ADD_NODE(ctx_list, child, OID_AUTO, namebuf,
  6154. CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "Queue Name");
  6155. queue_list = SYSCTL_CHILDREN(queue_node);
  6156. SYSCTL_ADD_INT(ctx_list, queue_list, OID_AUTO, "cpu",
  6157. CTLFLAG_RD, &txq->ift_task.gt_cpu, 0,
  6158. "cpu this queue is bound to");
  6159. #if MEMORY_LOGGING
  6160. SYSCTL_ADD_UQUAD(ctx_list, queue_list, OID_AUTO, "txq_dequeued",
  6161. CTLFLAG_RD, &txq->ift_dequeued, "total mbufs freed");
  6162. SYSCTL_ADD_UQUAD(ctx_list, queue_list, OID_AUTO, "txq_enqueued",
  6163. CTLFLAG_RD, &txq->ift_enqueued, "total mbufs enqueued");
  6164. #endif
  6165. SYSCTL_ADD_UQUAD(ctx_list, queue_list, OID_AUTO, "mbuf_defrag",
  6166. CTLFLAG_RD, &txq->ift_mbuf_defrag,
  6167. "# of times m_defrag was called");
  6168. SYSCTL_ADD_UQUAD(ctx_list, queue_list, OID_AUTO, "m_pullups",
  6169. CTLFLAG_RD, &txq->ift_pullups,
  6170. "# of times m_pullup was called");
  6171. SYSCTL_ADD_UQUAD(ctx_list, queue_list, OID_AUTO,
  6172. "mbuf_defrag_failed", CTLFLAG_RD,
  6173. &txq->ift_mbuf_defrag_failed, "# of times m_defrag failed");
  6174. SYSCTL_ADD_UQUAD(ctx_list, queue_list, OID_AUTO,
  6175. "no_desc_avail", CTLFLAG_RD, &txq->ift_no_desc_avail,
  6176. "# of times no descriptors were available");
  6177. SYSCTL_ADD_UQUAD(ctx_list, queue_list, OID_AUTO,
  6178. "tx_map_failed", CTLFLAG_RD, &txq->ift_map_failed,
  6179. "# of times DMA map failed");
  6180. SYSCTL_ADD_UQUAD(ctx_list, queue_list, OID_AUTO,
  6181. "txd_encap_efbig", CTLFLAG_RD, &txq->ift_txd_encap_efbig,
  6182. "# of times txd_encap returned EFBIG");
  6183. SYSCTL_ADD_UQUAD(ctx_list, queue_list, OID_AUTO,
  6184. "no_tx_dma_setup", CTLFLAG_RD, &txq->ift_no_tx_dma_setup,
  6185. "# of times map failed for other than EFBIG");
  6186. SYSCTL_ADD_U16(ctx_list, queue_list, OID_AUTO, "txq_pidx",
  6187. CTLFLAG_RD, &txq->ift_pidx, 1, "Producer Index");
  6188. SYSCTL_ADD_U16(ctx_list, queue_list, OID_AUTO, "txq_cidx",
  6189. CTLFLAG_RD, &txq->ift_cidx, 1, "Consumer Index");
  6190. SYSCTL_ADD_U16(ctx_list, queue_list, OID_AUTO,
  6191. "txq_cidx_processed", CTLFLAG_RD, &txq->ift_cidx_processed,
  6192. 1, "Consumer Index seen by credit update");
  6193. SYSCTL_ADD_U16(ctx_list, queue_list, OID_AUTO, "txq_in_use",
  6194. CTLFLAG_RD, &txq->ift_in_use, 1, "descriptors in use");
  6195. SYSCTL_ADD_UQUAD(ctx_list, queue_list, OID_AUTO,
  6196. "txq_processed", CTLFLAG_RD, &txq->ift_processed,
  6197. "descriptors procesed for clean");
  6198. SYSCTL_ADD_UQUAD(ctx_list, queue_list, OID_AUTO, "txq_cleaned",
  6199. CTLFLAG_RD, &txq->ift_cleaned, "total cleaned");
  6200. SYSCTL_ADD_PROC(ctx_list, queue_list, OID_AUTO, "ring_state",
  6201. CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_NEEDGIANT,
  6202. __DEVOLATILE(uint64_t *, &txq->ift_br->state), 0,
  6203. mp_ring_state_handler, "A", "soft ring state");
  6204. SYSCTL_ADD_COUNTER_U64(ctx_list, queue_list, OID_AUTO,
  6205. "r_enqueues", CTLFLAG_RD, &txq->ift_br->enqueues,
  6206. "# of enqueues to the mp_ring for this queue");
  6207. SYSCTL_ADD_COUNTER_U64(ctx_list, queue_list, OID_AUTO,
  6208. "r_drops", CTLFLAG_RD, &txq->ift_br->drops,
  6209. "# of drops in the mp_ring for this queue");
  6210. SYSCTL_ADD_COUNTER_U64(ctx_list, queue_list, OID_AUTO,
  6211. "r_starts", CTLFLAG_RD, &txq->ift_br->starts,
  6212. "# of normal consumer starts in mp_ring for this queue");
  6213. SYSCTL_ADD_COUNTER_U64(ctx_list, queue_list, OID_AUTO,
  6214. "r_stalls", CTLFLAG_RD, &txq->ift_br->stalls,
  6215. "# of consumer stalls in the mp_ring for this queue");
  6216. SYSCTL_ADD_COUNTER_U64(ctx_list, queue_list, OID_AUTO,
  6217. "r_restarts", CTLFLAG_RD, &txq->ift_br->restarts,
  6218. "# of consumer restarts in the mp_ring for this queue");
  6219. SYSCTL_ADD_COUNTER_U64(ctx_list, queue_list, OID_AUTO,
  6220. "r_abdications", CTLFLAG_RD, &txq->ift_br->abdications,
  6221. "# of consumer abdications in the mp_ring for this queue");
  6222. }
  6223. if (scctx->isc_nrxqsets > 100)
  6224. qfmt = "rxq%03d";
  6225. else if (scctx->isc_nrxqsets > 10)
  6226. qfmt = "rxq%02d";
  6227. else
  6228. qfmt = "rxq%d";
  6229. for (i = 0, rxq = ctx->ifc_rxqs; i < scctx->isc_nrxqsets; i++, rxq++) {
  6230. snprintf(namebuf, NAME_BUFLEN, qfmt, i);
  6231. queue_node = SYSCTL_ADD_NODE(ctx_list, child, OID_AUTO, namebuf,
  6232. CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "Queue Name");
  6233. queue_list = SYSCTL_CHILDREN(queue_node);
  6234. SYSCTL_ADD_INT(ctx_list, queue_list, OID_AUTO, "cpu",
  6235. CTLFLAG_RD, &rxq->ifr_task.gt_cpu, 0,
  6236. "cpu this queue is bound to");
  6237. if (sctx->isc_flags & IFLIB_HAS_RXCQ) {
  6238. SYSCTL_ADD_U16(ctx_list, queue_list, OID_AUTO,
  6239. "rxq_cq_cidx", CTLFLAG_RD, &rxq->ifr_cq_cidx, 1,
  6240. "Consumer Index");
  6241. }
  6242. for (j = 0, fl = rxq->ifr_fl; j < rxq->ifr_nfl; j++, fl++) {
  6243. snprintf(namebuf, NAME_BUFLEN, "rxq_fl%d", j);
  6244. fl_node = SYSCTL_ADD_NODE(ctx_list, queue_list,
  6245. OID_AUTO, namebuf, CTLFLAG_RD | CTLFLAG_MPSAFE,
  6246. NULL, "freelist Name");
  6247. fl_list = SYSCTL_CHILDREN(fl_node);
  6248. SYSCTL_ADD_U16(ctx_list, fl_list, OID_AUTO, "pidx",
  6249. CTLFLAG_RD, &fl->ifl_pidx, 1, "Producer Index");
  6250. SYSCTL_ADD_U16(ctx_list, fl_list, OID_AUTO, "cidx",
  6251. CTLFLAG_RD, &fl->ifl_cidx, 1, "Consumer Index");
  6252. SYSCTL_ADD_U16(ctx_list, fl_list, OID_AUTO, "credits",
  6253. CTLFLAG_RD, &fl->ifl_credits, 1,
  6254. "credits available");
  6255. SYSCTL_ADD_U16(ctx_list, fl_list, OID_AUTO, "buf_size",
  6256. CTLFLAG_RD, &fl->ifl_buf_size, 1, "buffer size");
  6257. #if MEMORY_LOGGING
  6258. SYSCTL_ADD_UQUAD(ctx_list, fl_list, OID_AUTO,
  6259. "fl_m_enqueued", CTLFLAG_RD, &fl->ifl_m_enqueued,
  6260. "mbufs allocated");
  6261. SYSCTL_ADD_UQUAD(ctx_list, fl_list, OID_AUTO,
  6262. "fl_m_dequeued", CTLFLAG_RD, &fl->ifl_m_dequeued,
  6263. "mbufs freed");
  6264. SYSCTL_ADD_UQUAD(ctx_list, fl_list, OID_AUTO,
  6265. "fl_cl_enqueued", CTLFLAG_RD, &fl->ifl_cl_enqueued,
  6266. "clusters allocated");
  6267. SYSCTL_ADD_UQUAD(ctx_list, fl_list, OID_AUTO,
  6268. "fl_cl_dequeued", CTLFLAG_RD, &fl->ifl_cl_dequeued,
  6269. "clusters freed");
  6270. #endif
  6271. }
  6272. }
  6273. }
  6274. void
  6275. iflib_request_reset(if_ctx_t ctx)
  6276. {
  6277. STATE_LOCK(ctx);
  6278. ctx->ifc_flags |= IFC_DO_RESET;
  6279. STATE_UNLOCK(ctx);
  6280. }
  6281. #ifndef __NO_STRICT_ALIGNMENT
  6282. static struct mbuf *
  6283. iflib_fixup_rx(struct mbuf *m)
  6284. {
  6285. struct mbuf *n;
  6286. if (m->m_len <= (MCLBYTES - ETHER_HDR_LEN)) {
  6287. bcopy(m->m_data, m->m_data + ETHER_HDR_LEN, m->m_len);
  6288. m->m_data += ETHER_HDR_LEN;
  6289. n = m;
  6290. } else {
  6291. MGETHDR(n, M_NOWAIT, MT_DATA);
  6292. if (n == NULL) {
  6293. m_freem(m);
  6294. return (NULL);
  6295. }
  6296. bcopy(m->m_data, n->m_data, ETHER_HDR_LEN);
  6297. m->m_data += ETHER_HDR_LEN;
  6298. m->m_len -= ETHER_HDR_LEN;
  6299. n->m_len = ETHER_HDR_LEN;
  6300. M_MOVE_PKTHDR(n, m);
  6301. n->m_next = m;
  6302. }
  6303. return (n);
  6304. }
  6305. #endif
  6306. #ifdef DEBUGNET
  6307. static void
  6308. iflib_debugnet_init(if_t ifp, int *nrxr, int *ncl, int *clsize)
  6309. {
  6310. if_ctx_t ctx;
  6311. ctx = if_getsoftc(ifp);
  6312. CTX_LOCK(ctx);
  6313. *nrxr = NRXQSETS(ctx);
  6314. *ncl = ctx->ifc_rxqs[0].ifr_fl->ifl_size;
  6315. *clsize = ctx->ifc_rxqs[0].ifr_fl->ifl_buf_size;
  6316. CTX_UNLOCK(ctx);
  6317. }
  6318. static void
  6319. iflib_debugnet_event(if_t ifp, enum debugnet_ev event)
  6320. {
  6321. if_ctx_t ctx;
  6322. if_softc_ctx_t scctx;
  6323. iflib_fl_t fl;
  6324. iflib_rxq_t rxq;
  6325. int i, j;
  6326. ctx = if_getsoftc(ifp);
  6327. scctx = &ctx->ifc_softc_ctx;
  6328. switch (event) {
  6329. case DEBUGNET_START:
  6330. for (i = 0; i < scctx->isc_nrxqsets; i++) {
  6331. rxq = &ctx->ifc_rxqs[i];
  6332. for (j = 0; j < rxq->ifr_nfl; j++) {
  6333. fl = rxq->ifr_fl;
  6334. fl->ifl_zone = m_getzone(fl->ifl_buf_size);
  6335. }
  6336. }
  6337. iflib_no_tx_batch = 1;
  6338. break;
  6339. default:
  6340. break;
  6341. }
  6342. }
  6343. static int
  6344. iflib_debugnet_transmit(if_t ifp, struct mbuf *m)
  6345. {
  6346. if_ctx_t ctx;
  6347. iflib_txq_t txq;
  6348. int error;
  6349. ctx = if_getsoftc(ifp);
  6350. if ((if_getdrvflags(ifp) & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) !=
  6351. IFF_DRV_RUNNING)
  6352. return (EBUSY);
  6353. txq = &ctx->ifc_txqs[0];
  6354. error = iflib_encap(txq, &m);
  6355. if (error == 0)
  6356. (void)iflib_txd_db_check(txq, true);
  6357. return (error);
  6358. }
  6359. static int
  6360. iflib_debugnet_poll(if_t ifp, int count)
  6361. {
  6362. struct epoch_tracker et;
  6363. if_ctx_t ctx;
  6364. if_softc_ctx_t scctx;
  6365. iflib_txq_t txq;
  6366. int i;
  6367. ctx = if_getsoftc(ifp);
  6368. scctx = &ctx->ifc_softc_ctx;
  6369. if ((if_getdrvflags(ifp) & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) !=
  6370. IFF_DRV_RUNNING)
  6371. return (EBUSY);
  6372. txq = &ctx->ifc_txqs[0];
  6373. (void)iflib_completed_tx_reclaim(txq, RECLAIM_THRESH(ctx));
  6374. NET_EPOCH_ENTER(et);
  6375. for (i = 0; i < scctx->isc_nrxqsets; i++)
  6376. (void)iflib_rxeof(&ctx->ifc_rxqs[i], 16 /* XXX */);
  6377. NET_EPOCH_EXIT(et);
  6378. return (0);
  6379. }
  6380. #endif /* DEBUGNET */