main.c 172 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463
  1. /*
  2. * Copyright (c) 2013-2015, Mellanox Technologies. All rights reserved.
  3. *
  4. * This software is available to you under a choice of one of two
  5. * licenses. You may choose to be licensed under the terms of the GNU
  6. * General Public License (GPL) Version 2, available from the file
  7. * COPYING in the main directory of this source tree, or the
  8. * OpenIB.org BSD license below:
  9. *
  10. * Redistribution and use in source and binary forms, with or
  11. * without modification, are permitted provided that the following
  12. * conditions are met:
  13. *
  14. * - Redistributions of source code must retain the above
  15. * copyright notice, this list of conditions and the following
  16. * disclaimer.
  17. *
  18. * - Redistributions in binary form must reproduce the above
  19. * copyright notice, this list of conditions and the following
  20. * disclaimer in the documentation and/or other materials
  21. * provided with the distribution.
  22. *
  23. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  27. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  28. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  29. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  30. * SOFTWARE.
  31. */
  32. #include <linux/debugfs.h>
  33. #include <linux/highmem.h>
  34. #include <linux/module.h>
  35. #include <linux/init.h>
  36. #include <linux/errno.h>
  37. #include <linux/pci.h>
  38. #include <linux/dma-mapping.h>
  39. #include <linux/slab.h>
  40. #include <linux/bitmap.h>
  41. #if defined(CONFIG_X86)
  42. #include <asm/pat.h>
  43. #endif
  44. #include <linux/sched.h>
  45. #include <linux/sched/mm.h>
  46. #include <linux/sched/task.h>
  47. #include <linux/delay.h>
  48. #include <rdma/ib_user_verbs.h>
  49. #include <rdma/ib_addr.h>
  50. #include <rdma/ib_cache.h>
  51. #include <linux/mlx5/port.h>
  52. #include <linux/mlx5/vport.h>
  53. #include <linux/mlx5/fs.h>
  54. #include <linux/list.h>
  55. #include <rdma/ib_smi.h>
  56. #include <rdma/ib_umem.h>
  57. #include <linux/in.h>
  58. #include <linux/etherdevice.h>
  59. #include "mlx5_ib.h"
  60. #include "ib_rep.h"
  61. #include "cmd.h"
  62. #include <linux/mlx5/fs_helpers.h>
  63. #include <linux/mlx5/accel.h>
  64. #include <rdma/uverbs_std_types.h>
  65. #include <rdma/mlx5_user_ioctl_verbs.h>
  66. #include <rdma/mlx5_user_ioctl_cmds.h>
  67. #define UVERBS_MODULE_NAME mlx5_ib
  68. #include <rdma/uverbs_named_ioctl.h>
  69. #define DRIVER_NAME "mlx5_ib"
  70. #define DRIVER_VERSION "5.0-0"
  71. MODULE_AUTHOR("Eli Cohen <eli@mellanox.com>");
  72. MODULE_DESCRIPTION("Mellanox Connect-IB HCA IB driver");
  73. MODULE_LICENSE("Dual BSD/GPL");
  74. static char mlx5_version[] =
  75. DRIVER_NAME ": Mellanox Connect-IB Infiniband driver v"
  76. DRIVER_VERSION "\n";
  77. struct mlx5_ib_event_work {
  78. struct work_struct work;
  79. struct mlx5_core_dev *dev;
  80. void *context;
  81. enum mlx5_dev_event event;
  82. unsigned long param;
  83. };
  84. enum {
  85. MLX5_ATOMIC_SIZE_QP_8BYTES = 1 << 3,
  86. };
  87. static struct workqueue_struct *mlx5_ib_event_wq;
  88. static LIST_HEAD(mlx5_ib_unaffiliated_port_list);
  89. static LIST_HEAD(mlx5_ib_dev_list);
  90. /*
  91. * This mutex should be held when accessing either of the above lists
  92. */
  93. static DEFINE_MUTEX(mlx5_ib_multiport_mutex);
  94. /* We can't use an array for xlt_emergency_page because dma_map_single
  95. * doesn't work on kernel modules memory
  96. */
  97. static unsigned long xlt_emergency_page;
  98. static struct mutex xlt_emergency_page_mutex;
  99. struct mlx5_ib_dev *mlx5_ib_get_ibdev_from_mpi(struct mlx5_ib_multiport_info *mpi)
  100. {
  101. struct mlx5_ib_dev *dev;
  102. mutex_lock(&mlx5_ib_multiport_mutex);
  103. dev = mpi->ibdev;
  104. mutex_unlock(&mlx5_ib_multiport_mutex);
  105. return dev;
  106. }
  107. static enum rdma_link_layer
  108. mlx5_port_type_cap_to_rdma_ll(int port_type_cap)
  109. {
  110. switch (port_type_cap) {
  111. case MLX5_CAP_PORT_TYPE_IB:
  112. return IB_LINK_LAYER_INFINIBAND;
  113. case MLX5_CAP_PORT_TYPE_ETH:
  114. return IB_LINK_LAYER_ETHERNET;
  115. default:
  116. return IB_LINK_LAYER_UNSPECIFIED;
  117. }
  118. }
  119. static enum rdma_link_layer
  120. mlx5_ib_port_link_layer(struct ib_device *device, u8 port_num)
  121. {
  122. struct mlx5_ib_dev *dev = to_mdev(device);
  123. int port_type_cap = MLX5_CAP_GEN(dev->mdev, port_type);
  124. return mlx5_port_type_cap_to_rdma_ll(port_type_cap);
  125. }
  126. static int get_port_state(struct ib_device *ibdev,
  127. u8 port_num,
  128. enum ib_port_state *state)
  129. {
  130. struct ib_port_attr attr;
  131. int ret;
  132. memset(&attr, 0, sizeof(attr));
  133. ret = ibdev->query_port(ibdev, port_num, &attr);
  134. if (!ret)
  135. *state = attr.state;
  136. return ret;
  137. }
  138. static int mlx5_netdev_event(struct notifier_block *this,
  139. unsigned long event, void *ptr)
  140. {
  141. struct mlx5_roce *roce = container_of(this, struct mlx5_roce, nb);
  142. struct net_device *ndev = netdev_notifier_info_to_dev(ptr);
  143. u8 port_num = roce->native_port_num;
  144. struct mlx5_core_dev *mdev;
  145. struct mlx5_ib_dev *ibdev;
  146. ibdev = roce->dev;
  147. mdev = mlx5_ib_get_native_port_mdev(ibdev, port_num, NULL);
  148. if (!mdev)
  149. return NOTIFY_DONE;
  150. switch (event) {
  151. case NETDEV_REGISTER:
  152. case NETDEV_UNREGISTER:
  153. write_lock(&roce->netdev_lock);
  154. if (ibdev->rep) {
  155. struct mlx5_eswitch *esw = ibdev->mdev->priv.eswitch;
  156. struct net_device *rep_ndev;
  157. rep_ndev = mlx5_ib_get_rep_netdev(esw,
  158. ibdev->rep->vport);
  159. if (rep_ndev == ndev)
  160. roce->netdev = (event == NETDEV_UNREGISTER) ?
  161. NULL : ndev;
  162. } else if (ndev->dev.parent == &mdev->pdev->dev) {
  163. roce->netdev = (event == NETDEV_UNREGISTER) ?
  164. NULL : ndev;
  165. }
  166. write_unlock(&roce->netdev_lock);
  167. break;
  168. case NETDEV_CHANGE:
  169. case NETDEV_UP:
  170. case NETDEV_DOWN: {
  171. struct net_device *lag_ndev = mlx5_lag_get_roce_netdev(mdev);
  172. struct net_device *upper = NULL;
  173. if (lag_ndev) {
  174. upper = netdev_master_upper_dev_get(lag_ndev);
  175. dev_put(lag_ndev);
  176. }
  177. if ((upper == ndev || (!upper && ndev == roce->netdev))
  178. && ibdev->ib_active) {
  179. struct ib_event ibev = { };
  180. enum ib_port_state port_state;
  181. if (get_port_state(&ibdev->ib_dev, port_num,
  182. &port_state))
  183. goto done;
  184. if (roce->last_port_state == port_state)
  185. goto done;
  186. roce->last_port_state = port_state;
  187. ibev.device = &ibdev->ib_dev;
  188. if (port_state == IB_PORT_DOWN)
  189. ibev.event = IB_EVENT_PORT_ERR;
  190. else if (port_state == IB_PORT_ACTIVE)
  191. ibev.event = IB_EVENT_PORT_ACTIVE;
  192. else
  193. goto done;
  194. ibev.element.port_num = port_num;
  195. ib_dispatch_event(&ibev);
  196. }
  197. break;
  198. }
  199. default:
  200. break;
  201. }
  202. done:
  203. mlx5_ib_put_native_port_mdev(ibdev, port_num);
  204. return NOTIFY_DONE;
  205. }
  206. static struct net_device *mlx5_ib_get_netdev(struct ib_device *device,
  207. u8 port_num)
  208. {
  209. struct mlx5_ib_dev *ibdev = to_mdev(device);
  210. struct net_device *ndev;
  211. struct mlx5_core_dev *mdev;
  212. mdev = mlx5_ib_get_native_port_mdev(ibdev, port_num, NULL);
  213. if (!mdev)
  214. return NULL;
  215. ndev = mlx5_lag_get_roce_netdev(mdev);
  216. if (ndev)
  217. goto out;
  218. /* Ensure ndev does not disappear before we invoke dev_hold()
  219. */
  220. read_lock(&ibdev->roce[port_num - 1].netdev_lock);
  221. ndev = ibdev->roce[port_num - 1].netdev;
  222. if (ndev)
  223. dev_hold(ndev);
  224. read_unlock(&ibdev->roce[port_num - 1].netdev_lock);
  225. out:
  226. mlx5_ib_put_native_port_mdev(ibdev, port_num);
  227. return ndev;
  228. }
  229. struct mlx5_core_dev *mlx5_ib_get_native_port_mdev(struct mlx5_ib_dev *ibdev,
  230. u8 ib_port_num,
  231. u8 *native_port_num)
  232. {
  233. enum rdma_link_layer ll = mlx5_ib_port_link_layer(&ibdev->ib_dev,
  234. ib_port_num);
  235. struct mlx5_core_dev *mdev = NULL;
  236. struct mlx5_ib_multiport_info *mpi;
  237. struct mlx5_ib_port *port;
  238. if (!mlx5_core_mp_enabled(ibdev->mdev) ||
  239. ll != IB_LINK_LAYER_ETHERNET) {
  240. if (native_port_num)
  241. *native_port_num = ib_port_num;
  242. return ibdev->mdev;
  243. }
  244. if (native_port_num)
  245. *native_port_num = 1;
  246. port = &ibdev->port[ib_port_num - 1];
  247. if (!port)
  248. return NULL;
  249. spin_lock(&port->mp.mpi_lock);
  250. mpi = ibdev->port[ib_port_num - 1].mp.mpi;
  251. if (mpi && !mpi->unaffiliate) {
  252. mdev = mpi->mdev;
  253. /* If it's the master no need to refcount, it'll exist
  254. * as long as the ib_dev exists.
  255. */
  256. if (!mpi->is_master)
  257. mpi->mdev_refcnt++;
  258. }
  259. spin_unlock(&port->mp.mpi_lock);
  260. return mdev;
  261. }
  262. void mlx5_ib_put_native_port_mdev(struct mlx5_ib_dev *ibdev, u8 port_num)
  263. {
  264. enum rdma_link_layer ll = mlx5_ib_port_link_layer(&ibdev->ib_dev,
  265. port_num);
  266. struct mlx5_ib_multiport_info *mpi;
  267. struct mlx5_ib_port *port;
  268. if (!mlx5_core_mp_enabled(ibdev->mdev) || ll != IB_LINK_LAYER_ETHERNET)
  269. return;
  270. port = &ibdev->port[port_num - 1];
  271. spin_lock(&port->mp.mpi_lock);
  272. mpi = ibdev->port[port_num - 1].mp.mpi;
  273. if (mpi->is_master)
  274. goto out;
  275. mpi->mdev_refcnt--;
  276. if (mpi->unaffiliate)
  277. complete(&mpi->unref_comp);
  278. out:
  279. spin_unlock(&port->mp.mpi_lock);
  280. }
  281. static int translate_eth_proto_oper(u32 eth_proto_oper, u8 *active_speed,
  282. u8 *active_width)
  283. {
  284. switch (eth_proto_oper) {
  285. case MLX5E_PROT_MASK(MLX5E_1000BASE_CX_SGMII):
  286. case MLX5E_PROT_MASK(MLX5E_1000BASE_KX):
  287. case MLX5E_PROT_MASK(MLX5E_100BASE_TX):
  288. case MLX5E_PROT_MASK(MLX5E_1000BASE_T):
  289. *active_width = IB_WIDTH_1X;
  290. *active_speed = IB_SPEED_SDR;
  291. break;
  292. case MLX5E_PROT_MASK(MLX5E_10GBASE_T):
  293. case MLX5E_PROT_MASK(MLX5E_10GBASE_CX4):
  294. case MLX5E_PROT_MASK(MLX5E_10GBASE_KX4):
  295. case MLX5E_PROT_MASK(MLX5E_10GBASE_KR):
  296. case MLX5E_PROT_MASK(MLX5E_10GBASE_CR):
  297. case MLX5E_PROT_MASK(MLX5E_10GBASE_SR):
  298. case MLX5E_PROT_MASK(MLX5E_10GBASE_ER):
  299. *active_width = IB_WIDTH_1X;
  300. *active_speed = IB_SPEED_QDR;
  301. break;
  302. case MLX5E_PROT_MASK(MLX5E_25GBASE_CR):
  303. case MLX5E_PROT_MASK(MLX5E_25GBASE_KR):
  304. case MLX5E_PROT_MASK(MLX5E_25GBASE_SR):
  305. *active_width = IB_WIDTH_1X;
  306. *active_speed = IB_SPEED_EDR;
  307. break;
  308. case MLX5E_PROT_MASK(MLX5E_40GBASE_CR4):
  309. case MLX5E_PROT_MASK(MLX5E_40GBASE_KR4):
  310. case MLX5E_PROT_MASK(MLX5E_40GBASE_SR4):
  311. case MLX5E_PROT_MASK(MLX5E_40GBASE_LR4):
  312. *active_width = IB_WIDTH_4X;
  313. *active_speed = IB_SPEED_QDR;
  314. break;
  315. case MLX5E_PROT_MASK(MLX5E_50GBASE_CR2):
  316. case MLX5E_PROT_MASK(MLX5E_50GBASE_KR2):
  317. case MLX5E_PROT_MASK(MLX5E_50GBASE_SR2):
  318. *active_width = IB_WIDTH_1X;
  319. *active_speed = IB_SPEED_HDR;
  320. break;
  321. case MLX5E_PROT_MASK(MLX5E_56GBASE_R4):
  322. *active_width = IB_WIDTH_4X;
  323. *active_speed = IB_SPEED_FDR;
  324. break;
  325. case MLX5E_PROT_MASK(MLX5E_100GBASE_CR4):
  326. case MLX5E_PROT_MASK(MLX5E_100GBASE_SR4):
  327. case MLX5E_PROT_MASK(MLX5E_100GBASE_KR4):
  328. case MLX5E_PROT_MASK(MLX5E_100GBASE_LR4):
  329. *active_width = IB_WIDTH_4X;
  330. *active_speed = IB_SPEED_EDR;
  331. break;
  332. default:
  333. return -EINVAL;
  334. }
  335. return 0;
  336. }
  337. static int mlx5_query_port_roce(struct ib_device *device, u8 port_num,
  338. struct ib_port_attr *props)
  339. {
  340. struct mlx5_ib_dev *dev = to_mdev(device);
  341. struct mlx5_core_dev *mdev;
  342. struct net_device *ndev, *upper;
  343. enum ib_mtu ndev_ib_mtu;
  344. bool put_mdev = true;
  345. u16 qkey_viol_cntr;
  346. u32 eth_prot_oper;
  347. u8 mdev_port_num;
  348. int err;
  349. mdev = mlx5_ib_get_native_port_mdev(dev, port_num, &mdev_port_num);
  350. if (!mdev) {
  351. /* This means the port isn't affiliated yet. Get the
  352. * info for the master port instead.
  353. */
  354. put_mdev = false;
  355. mdev = dev->mdev;
  356. mdev_port_num = 1;
  357. port_num = 1;
  358. }
  359. /* Possible bad flows are checked before filling out props so in case
  360. * of an error it will still be zeroed out.
  361. */
  362. err = mlx5_query_port_eth_proto_oper(mdev, &eth_prot_oper,
  363. mdev_port_num);
  364. if (err)
  365. goto out;
  366. props->active_width = IB_WIDTH_4X;
  367. props->active_speed = IB_SPEED_QDR;
  368. translate_eth_proto_oper(eth_prot_oper, &props->active_speed,
  369. &props->active_width);
  370. props->port_cap_flags |= IB_PORT_CM_SUP;
  371. props->ip_gids = true;
  372. props->gid_tbl_len = MLX5_CAP_ROCE(dev->mdev,
  373. roce_address_table_size);
  374. props->max_mtu = IB_MTU_4096;
  375. props->max_msg_sz = 1 << MLX5_CAP_GEN(dev->mdev, log_max_msg);
  376. props->pkey_tbl_len = 1;
  377. props->state = IB_PORT_DOWN;
  378. props->phys_state = 3;
  379. mlx5_query_nic_vport_qkey_viol_cntr(mdev, &qkey_viol_cntr);
  380. props->qkey_viol_cntr = qkey_viol_cntr;
  381. /* If this is a stub query for an unaffiliated port stop here */
  382. if (!put_mdev)
  383. goto out;
  384. ndev = mlx5_ib_get_netdev(device, port_num);
  385. if (!ndev)
  386. goto out;
  387. if (mlx5_lag_is_active(dev->mdev)) {
  388. rcu_read_lock();
  389. upper = netdev_master_upper_dev_get_rcu(ndev);
  390. if (upper) {
  391. dev_put(ndev);
  392. ndev = upper;
  393. dev_hold(ndev);
  394. }
  395. rcu_read_unlock();
  396. }
  397. if (netif_running(ndev) && netif_carrier_ok(ndev)) {
  398. props->state = IB_PORT_ACTIVE;
  399. props->phys_state = 5;
  400. }
  401. ndev_ib_mtu = iboe_get_mtu(ndev->mtu);
  402. dev_put(ndev);
  403. props->active_mtu = min(props->max_mtu, ndev_ib_mtu);
  404. out:
  405. if (put_mdev)
  406. mlx5_ib_put_native_port_mdev(dev, port_num);
  407. return err;
  408. }
  409. static int set_roce_addr(struct mlx5_ib_dev *dev, u8 port_num,
  410. unsigned int index, const union ib_gid *gid,
  411. const struct ib_gid_attr *attr)
  412. {
  413. enum ib_gid_type gid_type = IB_GID_TYPE_IB;
  414. u8 roce_version = 0;
  415. u8 roce_l3_type = 0;
  416. bool vlan = false;
  417. u8 mac[ETH_ALEN];
  418. u16 vlan_id = 0;
  419. if (gid) {
  420. gid_type = attr->gid_type;
  421. ether_addr_copy(mac, attr->ndev->dev_addr);
  422. if (is_vlan_dev(attr->ndev)) {
  423. vlan = true;
  424. vlan_id = vlan_dev_vlan_id(attr->ndev);
  425. }
  426. }
  427. switch (gid_type) {
  428. case IB_GID_TYPE_IB:
  429. roce_version = MLX5_ROCE_VERSION_1;
  430. break;
  431. case IB_GID_TYPE_ROCE_UDP_ENCAP:
  432. roce_version = MLX5_ROCE_VERSION_2;
  433. if (ipv6_addr_v4mapped((void *)gid))
  434. roce_l3_type = MLX5_ROCE_L3_TYPE_IPV4;
  435. else
  436. roce_l3_type = MLX5_ROCE_L3_TYPE_IPV6;
  437. break;
  438. default:
  439. mlx5_ib_warn(dev, "Unexpected GID type %u\n", gid_type);
  440. }
  441. return mlx5_core_roce_gid_set(dev->mdev, index, roce_version,
  442. roce_l3_type, gid->raw, mac, vlan,
  443. vlan_id, port_num);
  444. }
  445. static int mlx5_ib_add_gid(const struct ib_gid_attr *attr,
  446. __always_unused void **context)
  447. {
  448. return set_roce_addr(to_mdev(attr->device), attr->port_num,
  449. attr->index, &attr->gid, attr);
  450. }
  451. static int mlx5_ib_del_gid(const struct ib_gid_attr *attr,
  452. __always_unused void **context)
  453. {
  454. return set_roce_addr(to_mdev(attr->device), attr->port_num,
  455. attr->index, NULL, NULL);
  456. }
  457. __be16 mlx5_get_roce_udp_sport(struct mlx5_ib_dev *dev,
  458. const struct ib_gid_attr *attr)
  459. {
  460. if (attr->gid_type != IB_GID_TYPE_ROCE_UDP_ENCAP)
  461. return 0;
  462. return cpu_to_be16(MLX5_CAP_ROCE(dev->mdev, r_roce_min_src_udp_port));
  463. }
  464. static int mlx5_use_mad_ifc(struct mlx5_ib_dev *dev)
  465. {
  466. if (MLX5_CAP_GEN(dev->mdev, port_type) == MLX5_CAP_PORT_TYPE_IB)
  467. return !MLX5_CAP_GEN(dev->mdev, ib_virt);
  468. return 0;
  469. }
  470. enum {
  471. MLX5_VPORT_ACCESS_METHOD_MAD,
  472. MLX5_VPORT_ACCESS_METHOD_HCA,
  473. MLX5_VPORT_ACCESS_METHOD_NIC,
  474. };
  475. static int mlx5_get_vport_access_method(struct ib_device *ibdev)
  476. {
  477. if (mlx5_use_mad_ifc(to_mdev(ibdev)))
  478. return MLX5_VPORT_ACCESS_METHOD_MAD;
  479. if (mlx5_ib_port_link_layer(ibdev, 1) ==
  480. IB_LINK_LAYER_ETHERNET)
  481. return MLX5_VPORT_ACCESS_METHOD_NIC;
  482. return MLX5_VPORT_ACCESS_METHOD_HCA;
  483. }
  484. static void get_atomic_caps(struct mlx5_ib_dev *dev,
  485. u8 atomic_size_qp,
  486. struct ib_device_attr *props)
  487. {
  488. u8 tmp;
  489. u8 atomic_operations = MLX5_CAP_ATOMIC(dev->mdev, atomic_operations);
  490. u8 atomic_req_8B_endianness_mode =
  491. MLX5_CAP_ATOMIC(dev->mdev, atomic_req_8B_endianness_mode);
  492. /* Check if HW supports 8 bytes standard atomic operations and capable
  493. * of host endianness respond
  494. */
  495. tmp = MLX5_ATOMIC_OPS_CMP_SWAP | MLX5_ATOMIC_OPS_FETCH_ADD;
  496. if (((atomic_operations & tmp) == tmp) &&
  497. (atomic_size_qp & MLX5_ATOMIC_SIZE_QP_8BYTES) &&
  498. (atomic_req_8B_endianness_mode)) {
  499. props->atomic_cap = IB_ATOMIC_HCA;
  500. } else {
  501. props->atomic_cap = IB_ATOMIC_NONE;
  502. }
  503. }
  504. static void get_atomic_caps_qp(struct mlx5_ib_dev *dev,
  505. struct ib_device_attr *props)
  506. {
  507. u8 atomic_size_qp = MLX5_CAP_ATOMIC(dev->mdev, atomic_size_qp);
  508. get_atomic_caps(dev, atomic_size_qp, props);
  509. }
  510. static void get_atomic_caps_dc(struct mlx5_ib_dev *dev,
  511. struct ib_device_attr *props)
  512. {
  513. u8 atomic_size_qp = MLX5_CAP_ATOMIC(dev->mdev, atomic_size_dc);
  514. get_atomic_caps(dev, atomic_size_qp, props);
  515. }
  516. bool mlx5_ib_dc_atomic_is_supported(struct mlx5_ib_dev *dev)
  517. {
  518. struct ib_device_attr props = {};
  519. get_atomic_caps_dc(dev, &props);
  520. return (props.atomic_cap == IB_ATOMIC_HCA) ? true : false;
  521. }
  522. static int mlx5_query_system_image_guid(struct ib_device *ibdev,
  523. __be64 *sys_image_guid)
  524. {
  525. struct mlx5_ib_dev *dev = to_mdev(ibdev);
  526. struct mlx5_core_dev *mdev = dev->mdev;
  527. u64 tmp;
  528. int err;
  529. switch (mlx5_get_vport_access_method(ibdev)) {
  530. case MLX5_VPORT_ACCESS_METHOD_MAD:
  531. return mlx5_query_mad_ifc_system_image_guid(ibdev,
  532. sys_image_guid);
  533. case MLX5_VPORT_ACCESS_METHOD_HCA:
  534. err = mlx5_query_hca_vport_system_image_guid(mdev, &tmp);
  535. break;
  536. case MLX5_VPORT_ACCESS_METHOD_NIC:
  537. err = mlx5_query_nic_vport_system_image_guid(mdev, &tmp);
  538. break;
  539. default:
  540. return -EINVAL;
  541. }
  542. if (!err)
  543. *sys_image_guid = cpu_to_be64(tmp);
  544. return err;
  545. }
  546. static int mlx5_query_max_pkeys(struct ib_device *ibdev,
  547. u16 *max_pkeys)
  548. {
  549. struct mlx5_ib_dev *dev = to_mdev(ibdev);
  550. struct mlx5_core_dev *mdev = dev->mdev;
  551. switch (mlx5_get_vport_access_method(ibdev)) {
  552. case MLX5_VPORT_ACCESS_METHOD_MAD:
  553. return mlx5_query_mad_ifc_max_pkeys(ibdev, max_pkeys);
  554. case MLX5_VPORT_ACCESS_METHOD_HCA:
  555. case MLX5_VPORT_ACCESS_METHOD_NIC:
  556. *max_pkeys = mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(mdev,
  557. pkey_table_size));
  558. return 0;
  559. default:
  560. return -EINVAL;
  561. }
  562. }
  563. static int mlx5_query_vendor_id(struct ib_device *ibdev,
  564. u32 *vendor_id)
  565. {
  566. struct mlx5_ib_dev *dev = to_mdev(ibdev);
  567. switch (mlx5_get_vport_access_method(ibdev)) {
  568. case MLX5_VPORT_ACCESS_METHOD_MAD:
  569. return mlx5_query_mad_ifc_vendor_id(ibdev, vendor_id);
  570. case MLX5_VPORT_ACCESS_METHOD_HCA:
  571. case MLX5_VPORT_ACCESS_METHOD_NIC:
  572. return mlx5_core_query_vendor_id(dev->mdev, vendor_id);
  573. default:
  574. return -EINVAL;
  575. }
  576. }
  577. static int mlx5_query_node_guid(struct mlx5_ib_dev *dev,
  578. __be64 *node_guid)
  579. {
  580. u64 tmp;
  581. int err;
  582. switch (mlx5_get_vport_access_method(&dev->ib_dev)) {
  583. case MLX5_VPORT_ACCESS_METHOD_MAD:
  584. return mlx5_query_mad_ifc_node_guid(dev, node_guid);
  585. case MLX5_VPORT_ACCESS_METHOD_HCA:
  586. err = mlx5_query_hca_vport_node_guid(dev->mdev, &tmp);
  587. break;
  588. case MLX5_VPORT_ACCESS_METHOD_NIC:
  589. err = mlx5_query_nic_vport_node_guid(dev->mdev, &tmp);
  590. break;
  591. default:
  592. return -EINVAL;
  593. }
  594. if (!err)
  595. *node_guid = cpu_to_be64(tmp);
  596. return err;
  597. }
  598. struct mlx5_reg_node_desc {
  599. u8 desc[IB_DEVICE_NODE_DESC_MAX];
  600. };
  601. static int mlx5_query_node_desc(struct mlx5_ib_dev *dev, char *node_desc)
  602. {
  603. struct mlx5_reg_node_desc in;
  604. if (mlx5_use_mad_ifc(dev))
  605. return mlx5_query_mad_ifc_node_desc(dev, node_desc);
  606. memset(&in, 0, sizeof(in));
  607. return mlx5_core_access_reg(dev->mdev, &in, sizeof(in), node_desc,
  608. sizeof(struct mlx5_reg_node_desc),
  609. MLX5_REG_NODE_DESC, 0, 0);
  610. }
  611. static int mlx5_ib_query_device(struct ib_device *ibdev,
  612. struct ib_device_attr *props,
  613. struct ib_udata *uhw)
  614. {
  615. struct mlx5_ib_dev *dev = to_mdev(ibdev);
  616. struct mlx5_core_dev *mdev = dev->mdev;
  617. int err = -ENOMEM;
  618. int max_sq_desc;
  619. int max_rq_sg;
  620. int max_sq_sg;
  621. u64 min_page_size = 1ull << MLX5_CAP_GEN(mdev, log_pg_sz);
  622. bool raw_support = !mlx5_core_mp_enabled(mdev);
  623. struct mlx5_ib_query_device_resp resp = {};
  624. size_t resp_len;
  625. u64 max_tso;
  626. resp_len = sizeof(resp.comp_mask) + sizeof(resp.response_length);
  627. if (uhw->outlen && uhw->outlen < resp_len)
  628. return -EINVAL;
  629. else
  630. resp.response_length = resp_len;
  631. if (uhw->inlen && !ib_is_udata_cleared(uhw, 0, uhw->inlen))
  632. return -EINVAL;
  633. memset(props, 0, sizeof(*props));
  634. err = mlx5_query_system_image_guid(ibdev,
  635. &props->sys_image_guid);
  636. if (err)
  637. return err;
  638. err = mlx5_query_max_pkeys(ibdev, &props->max_pkeys);
  639. if (err)
  640. return err;
  641. err = mlx5_query_vendor_id(ibdev, &props->vendor_id);
  642. if (err)
  643. return err;
  644. props->fw_ver = ((u64)fw_rev_maj(dev->mdev) << 32) |
  645. (fw_rev_min(dev->mdev) << 16) |
  646. fw_rev_sub(dev->mdev);
  647. props->device_cap_flags = IB_DEVICE_CHANGE_PHY_PORT |
  648. IB_DEVICE_PORT_ACTIVE_EVENT |
  649. IB_DEVICE_SYS_IMAGE_GUID |
  650. IB_DEVICE_RC_RNR_NAK_GEN;
  651. if (MLX5_CAP_GEN(mdev, pkv))
  652. props->device_cap_flags |= IB_DEVICE_BAD_PKEY_CNTR;
  653. if (MLX5_CAP_GEN(mdev, qkv))
  654. props->device_cap_flags |= IB_DEVICE_BAD_QKEY_CNTR;
  655. if (MLX5_CAP_GEN(mdev, apm))
  656. props->device_cap_flags |= IB_DEVICE_AUTO_PATH_MIG;
  657. if (MLX5_CAP_GEN(mdev, xrc))
  658. props->device_cap_flags |= IB_DEVICE_XRC;
  659. if (MLX5_CAP_GEN(mdev, imaicl)) {
  660. props->device_cap_flags |= IB_DEVICE_MEM_WINDOW |
  661. IB_DEVICE_MEM_WINDOW_TYPE_2B;
  662. props->max_mw = 1 << MLX5_CAP_GEN(mdev, log_max_mkey);
  663. /* We support 'Gappy' memory registration too */
  664. props->device_cap_flags |= IB_DEVICE_SG_GAPS_REG;
  665. }
  666. props->device_cap_flags |= IB_DEVICE_MEM_MGT_EXTENSIONS;
  667. if (MLX5_CAP_GEN(mdev, sho)) {
  668. props->device_cap_flags |= IB_DEVICE_SIGNATURE_HANDOVER;
  669. /* At this stage no support for signature handover */
  670. props->sig_prot_cap = IB_PROT_T10DIF_TYPE_1 |
  671. IB_PROT_T10DIF_TYPE_2 |
  672. IB_PROT_T10DIF_TYPE_3;
  673. props->sig_guard_cap = IB_GUARD_T10DIF_CRC |
  674. IB_GUARD_T10DIF_CSUM;
  675. }
  676. if (MLX5_CAP_GEN(mdev, block_lb_mc))
  677. props->device_cap_flags |= IB_DEVICE_BLOCK_MULTICAST_LOOPBACK;
  678. if (MLX5_CAP_GEN(dev->mdev, eth_net_offloads) && raw_support) {
  679. if (MLX5_CAP_ETH(mdev, csum_cap)) {
  680. /* Legacy bit to support old userspace libraries */
  681. props->device_cap_flags |= IB_DEVICE_RAW_IP_CSUM;
  682. props->raw_packet_caps |= IB_RAW_PACKET_CAP_IP_CSUM;
  683. }
  684. if (MLX5_CAP_ETH(dev->mdev, vlan_cap))
  685. props->raw_packet_caps |=
  686. IB_RAW_PACKET_CAP_CVLAN_STRIPPING;
  687. if (field_avail(typeof(resp), tso_caps, uhw->outlen)) {
  688. max_tso = MLX5_CAP_ETH(mdev, max_lso_cap);
  689. if (max_tso) {
  690. resp.tso_caps.max_tso = 1 << max_tso;
  691. resp.tso_caps.supported_qpts |=
  692. 1 << IB_QPT_RAW_PACKET;
  693. resp.response_length += sizeof(resp.tso_caps);
  694. }
  695. }
  696. if (field_avail(typeof(resp), rss_caps, uhw->outlen)) {
  697. resp.rss_caps.rx_hash_function =
  698. MLX5_RX_HASH_FUNC_TOEPLITZ;
  699. resp.rss_caps.rx_hash_fields_mask =
  700. MLX5_RX_HASH_SRC_IPV4 |
  701. MLX5_RX_HASH_DST_IPV4 |
  702. MLX5_RX_HASH_SRC_IPV6 |
  703. MLX5_RX_HASH_DST_IPV6 |
  704. MLX5_RX_HASH_SRC_PORT_TCP |
  705. MLX5_RX_HASH_DST_PORT_TCP |
  706. MLX5_RX_HASH_SRC_PORT_UDP |
  707. MLX5_RX_HASH_DST_PORT_UDP |
  708. MLX5_RX_HASH_INNER;
  709. if (mlx5_accel_ipsec_device_caps(dev->mdev) &
  710. MLX5_ACCEL_IPSEC_CAP_DEVICE)
  711. resp.rss_caps.rx_hash_fields_mask |=
  712. MLX5_RX_HASH_IPSEC_SPI;
  713. resp.response_length += sizeof(resp.rss_caps);
  714. }
  715. } else {
  716. if (field_avail(typeof(resp), tso_caps, uhw->outlen))
  717. resp.response_length += sizeof(resp.tso_caps);
  718. if (field_avail(typeof(resp), rss_caps, uhw->outlen))
  719. resp.response_length += sizeof(resp.rss_caps);
  720. }
  721. if (MLX5_CAP_GEN(mdev, ipoib_basic_offloads)) {
  722. props->device_cap_flags |= IB_DEVICE_UD_IP_CSUM;
  723. props->device_cap_flags |= IB_DEVICE_UD_TSO;
  724. }
  725. if (MLX5_CAP_GEN(dev->mdev, rq_delay_drop) &&
  726. MLX5_CAP_GEN(dev->mdev, general_notification_event) &&
  727. raw_support)
  728. props->raw_packet_caps |= IB_RAW_PACKET_CAP_DELAY_DROP;
  729. if (MLX5_CAP_GEN(mdev, ipoib_enhanced_offloads) &&
  730. MLX5_CAP_IPOIB_ENHANCED(mdev, csum_cap))
  731. props->device_cap_flags |= IB_DEVICE_UD_IP_CSUM;
  732. if (MLX5_CAP_GEN(dev->mdev, eth_net_offloads) &&
  733. MLX5_CAP_ETH(dev->mdev, scatter_fcs) &&
  734. raw_support) {
  735. /* Legacy bit to support old userspace libraries */
  736. props->device_cap_flags |= IB_DEVICE_RAW_SCATTER_FCS;
  737. props->raw_packet_caps |= IB_RAW_PACKET_CAP_SCATTER_FCS;
  738. }
  739. if (MLX5_CAP_DEV_MEM(mdev, memic)) {
  740. props->max_dm_size =
  741. MLX5_CAP_DEV_MEM(mdev, max_memic_size);
  742. }
  743. if (mlx5_get_flow_namespace(dev->mdev, MLX5_FLOW_NAMESPACE_BYPASS))
  744. props->device_cap_flags |= IB_DEVICE_MANAGED_FLOW_STEERING;
  745. if (MLX5_CAP_GEN(mdev, end_pad))
  746. props->device_cap_flags |= IB_DEVICE_PCI_WRITE_END_PADDING;
  747. props->vendor_part_id = mdev->pdev->device;
  748. props->hw_ver = mdev->pdev->revision;
  749. props->max_mr_size = ~0ull;
  750. props->page_size_cap = ~(min_page_size - 1);
  751. props->max_qp = 1 << MLX5_CAP_GEN(mdev, log_max_qp);
  752. props->max_qp_wr = 1 << MLX5_CAP_GEN(mdev, log_max_qp_sz);
  753. max_rq_sg = MLX5_CAP_GEN(mdev, max_wqe_sz_rq) /
  754. sizeof(struct mlx5_wqe_data_seg);
  755. max_sq_desc = min_t(int, MLX5_CAP_GEN(mdev, max_wqe_sz_sq), 512);
  756. max_sq_sg = (max_sq_desc - sizeof(struct mlx5_wqe_ctrl_seg) -
  757. sizeof(struct mlx5_wqe_raddr_seg)) /
  758. sizeof(struct mlx5_wqe_data_seg);
  759. props->max_send_sge = max_sq_sg;
  760. props->max_recv_sge = max_rq_sg;
  761. props->max_sge_rd = MLX5_MAX_SGE_RD;
  762. props->max_cq = 1 << MLX5_CAP_GEN(mdev, log_max_cq);
  763. props->max_cqe = (1 << MLX5_CAP_GEN(mdev, log_max_cq_sz)) - 1;
  764. props->max_mr = 1 << MLX5_CAP_GEN(mdev, log_max_mkey);
  765. props->max_pd = 1 << MLX5_CAP_GEN(mdev, log_max_pd);
  766. props->max_qp_rd_atom = 1 << MLX5_CAP_GEN(mdev, log_max_ra_req_qp);
  767. props->max_qp_init_rd_atom = 1 << MLX5_CAP_GEN(mdev, log_max_ra_res_qp);
  768. props->max_srq = 1 << MLX5_CAP_GEN(mdev, log_max_srq);
  769. props->max_srq_wr = (1 << MLX5_CAP_GEN(mdev, log_max_srq_sz)) - 1;
  770. props->local_ca_ack_delay = MLX5_CAP_GEN(mdev, local_ca_ack_delay);
  771. props->max_res_rd_atom = props->max_qp_rd_atom * props->max_qp;
  772. props->max_srq_sge = max_rq_sg - 1;
  773. props->max_fast_reg_page_list_len =
  774. 1 << MLX5_CAP_GEN(mdev, log_max_klm_list_size);
  775. get_atomic_caps_qp(dev, props);
  776. props->masked_atomic_cap = IB_ATOMIC_NONE;
  777. props->max_mcast_grp = 1 << MLX5_CAP_GEN(mdev, log_max_mcg);
  778. props->max_mcast_qp_attach = MLX5_CAP_GEN(mdev, max_qp_mcg);
  779. props->max_total_mcast_qp_attach = props->max_mcast_qp_attach *
  780. props->max_mcast_grp;
  781. props->max_map_per_fmr = INT_MAX; /* no limit in ConnectIB */
  782. props->max_ah = INT_MAX;
  783. props->hca_core_clock = MLX5_CAP_GEN(mdev, device_frequency_khz);
  784. props->timestamp_mask = 0x7FFFFFFFFFFFFFFFULL;
  785. #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
  786. if (MLX5_CAP_GEN(mdev, pg))
  787. props->device_cap_flags |= IB_DEVICE_ON_DEMAND_PAGING;
  788. props->odp_caps = dev->odp_caps;
  789. #endif
  790. if (MLX5_CAP_GEN(mdev, cd))
  791. props->device_cap_flags |= IB_DEVICE_CROSS_CHANNEL;
  792. if (!mlx5_core_is_pf(mdev))
  793. props->device_cap_flags |= IB_DEVICE_VIRTUAL_FUNCTION;
  794. if (mlx5_ib_port_link_layer(ibdev, 1) ==
  795. IB_LINK_LAYER_ETHERNET && raw_support) {
  796. props->rss_caps.max_rwq_indirection_tables =
  797. 1 << MLX5_CAP_GEN(dev->mdev, log_max_rqt);
  798. props->rss_caps.max_rwq_indirection_table_size =
  799. 1 << MLX5_CAP_GEN(dev->mdev, log_max_rqt_size);
  800. props->rss_caps.supported_qpts = 1 << IB_QPT_RAW_PACKET;
  801. props->max_wq_type_rq =
  802. 1 << MLX5_CAP_GEN(dev->mdev, log_max_rq);
  803. }
  804. if (MLX5_CAP_GEN(mdev, tag_matching)) {
  805. props->tm_caps.max_num_tags =
  806. (1 << MLX5_CAP_GEN(mdev, log_tag_matching_list_sz)) - 1;
  807. props->tm_caps.max_ops =
  808. 1 << MLX5_CAP_GEN(mdev, log_max_qp_sz);
  809. props->tm_caps.max_sge = MLX5_TM_MAX_SGE;
  810. }
  811. if (MLX5_CAP_GEN(mdev, tag_matching) &&
  812. MLX5_CAP_GEN(mdev, rndv_offload_rc)) {
  813. props->tm_caps.flags = IB_TM_CAP_RNDV_RC;
  814. props->tm_caps.max_rndv_hdr_size = MLX5_TM_MAX_RNDV_MSG_SIZE;
  815. }
  816. if (MLX5_CAP_GEN(dev->mdev, cq_moderation)) {
  817. props->cq_caps.max_cq_moderation_count =
  818. MLX5_MAX_CQ_COUNT;
  819. props->cq_caps.max_cq_moderation_period =
  820. MLX5_MAX_CQ_PERIOD;
  821. }
  822. if (field_avail(typeof(resp), cqe_comp_caps, uhw->outlen)) {
  823. resp.response_length += sizeof(resp.cqe_comp_caps);
  824. if (MLX5_CAP_GEN(dev->mdev, cqe_compression)) {
  825. resp.cqe_comp_caps.max_num =
  826. MLX5_CAP_GEN(dev->mdev,
  827. cqe_compression_max_num);
  828. resp.cqe_comp_caps.supported_format =
  829. MLX5_IB_CQE_RES_FORMAT_HASH |
  830. MLX5_IB_CQE_RES_FORMAT_CSUM;
  831. if (MLX5_CAP_GEN(dev->mdev, mini_cqe_resp_stride_index))
  832. resp.cqe_comp_caps.supported_format |=
  833. MLX5_IB_CQE_RES_FORMAT_CSUM_STRIDX;
  834. }
  835. }
  836. if (field_avail(typeof(resp), packet_pacing_caps, uhw->outlen) &&
  837. raw_support) {
  838. if (MLX5_CAP_QOS(mdev, packet_pacing) &&
  839. MLX5_CAP_GEN(mdev, qos)) {
  840. resp.packet_pacing_caps.qp_rate_limit_max =
  841. MLX5_CAP_QOS(mdev, packet_pacing_max_rate);
  842. resp.packet_pacing_caps.qp_rate_limit_min =
  843. MLX5_CAP_QOS(mdev, packet_pacing_min_rate);
  844. resp.packet_pacing_caps.supported_qpts |=
  845. 1 << IB_QPT_RAW_PACKET;
  846. if (MLX5_CAP_QOS(mdev, packet_pacing_burst_bound) &&
  847. MLX5_CAP_QOS(mdev, packet_pacing_typical_size))
  848. resp.packet_pacing_caps.cap_flags |=
  849. MLX5_IB_PP_SUPPORT_BURST;
  850. }
  851. resp.response_length += sizeof(resp.packet_pacing_caps);
  852. }
  853. if (field_avail(typeof(resp), mlx5_ib_support_multi_pkt_send_wqes,
  854. uhw->outlen)) {
  855. if (MLX5_CAP_ETH(mdev, multi_pkt_send_wqe))
  856. resp.mlx5_ib_support_multi_pkt_send_wqes =
  857. MLX5_IB_ALLOW_MPW;
  858. if (MLX5_CAP_ETH(mdev, enhanced_multi_pkt_send_wqe))
  859. resp.mlx5_ib_support_multi_pkt_send_wqes |=
  860. MLX5_IB_SUPPORT_EMPW;
  861. resp.response_length +=
  862. sizeof(resp.mlx5_ib_support_multi_pkt_send_wqes);
  863. }
  864. if (field_avail(typeof(resp), flags, uhw->outlen)) {
  865. resp.response_length += sizeof(resp.flags);
  866. if (MLX5_CAP_GEN(mdev, cqe_compression_128))
  867. resp.flags |=
  868. MLX5_IB_QUERY_DEV_RESP_FLAGS_CQE_128B_COMP;
  869. if (MLX5_CAP_GEN(mdev, cqe_128_always))
  870. resp.flags |= MLX5_IB_QUERY_DEV_RESP_FLAGS_CQE_128B_PAD;
  871. }
  872. if (field_avail(typeof(resp), sw_parsing_caps,
  873. uhw->outlen)) {
  874. resp.response_length += sizeof(resp.sw_parsing_caps);
  875. if (MLX5_CAP_ETH(mdev, swp)) {
  876. resp.sw_parsing_caps.sw_parsing_offloads |=
  877. MLX5_IB_SW_PARSING;
  878. if (MLX5_CAP_ETH(mdev, swp_csum))
  879. resp.sw_parsing_caps.sw_parsing_offloads |=
  880. MLX5_IB_SW_PARSING_CSUM;
  881. if (MLX5_CAP_ETH(mdev, swp_lso))
  882. resp.sw_parsing_caps.sw_parsing_offloads |=
  883. MLX5_IB_SW_PARSING_LSO;
  884. if (resp.sw_parsing_caps.sw_parsing_offloads)
  885. resp.sw_parsing_caps.supported_qpts =
  886. BIT(IB_QPT_RAW_PACKET);
  887. }
  888. }
  889. if (field_avail(typeof(resp), striding_rq_caps, uhw->outlen) &&
  890. raw_support) {
  891. resp.response_length += sizeof(resp.striding_rq_caps);
  892. if (MLX5_CAP_GEN(mdev, striding_rq)) {
  893. resp.striding_rq_caps.min_single_stride_log_num_of_bytes =
  894. MLX5_MIN_SINGLE_STRIDE_LOG_NUM_BYTES;
  895. resp.striding_rq_caps.max_single_stride_log_num_of_bytes =
  896. MLX5_MAX_SINGLE_STRIDE_LOG_NUM_BYTES;
  897. resp.striding_rq_caps.min_single_wqe_log_num_of_strides =
  898. MLX5_MIN_SINGLE_WQE_LOG_NUM_STRIDES;
  899. resp.striding_rq_caps.max_single_wqe_log_num_of_strides =
  900. MLX5_MAX_SINGLE_WQE_LOG_NUM_STRIDES;
  901. resp.striding_rq_caps.supported_qpts =
  902. BIT(IB_QPT_RAW_PACKET);
  903. }
  904. }
  905. if (field_avail(typeof(resp), tunnel_offloads_caps,
  906. uhw->outlen)) {
  907. resp.response_length += sizeof(resp.tunnel_offloads_caps);
  908. if (MLX5_CAP_ETH(mdev, tunnel_stateless_vxlan))
  909. resp.tunnel_offloads_caps |=
  910. MLX5_IB_TUNNELED_OFFLOADS_VXLAN;
  911. if (MLX5_CAP_ETH(mdev, tunnel_stateless_geneve_rx))
  912. resp.tunnel_offloads_caps |=
  913. MLX5_IB_TUNNELED_OFFLOADS_GENEVE;
  914. if (MLX5_CAP_ETH(mdev, tunnel_stateless_gre))
  915. resp.tunnel_offloads_caps |=
  916. MLX5_IB_TUNNELED_OFFLOADS_GRE;
  917. if (MLX5_CAP_GEN(mdev, flex_parser_protocols) &
  918. MLX5_FLEX_PROTO_CW_MPLS_GRE)
  919. resp.tunnel_offloads_caps |=
  920. MLX5_IB_TUNNELED_OFFLOADS_MPLS_GRE;
  921. if (MLX5_CAP_GEN(mdev, flex_parser_protocols) &
  922. MLX5_FLEX_PROTO_CW_MPLS_UDP)
  923. resp.tunnel_offloads_caps |=
  924. MLX5_IB_TUNNELED_OFFLOADS_MPLS_UDP;
  925. }
  926. if (uhw->outlen) {
  927. err = ib_copy_to_udata(uhw, &resp, resp.response_length);
  928. if (err)
  929. return err;
  930. }
  931. return 0;
  932. }
  933. enum mlx5_ib_width {
  934. MLX5_IB_WIDTH_1X = 1 << 0,
  935. MLX5_IB_WIDTH_2X = 1 << 1,
  936. MLX5_IB_WIDTH_4X = 1 << 2,
  937. MLX5_IB_WIDTH_8X = 1 << 3,
  938. MLX5_IB_WIDTH_12X = 1 << 4
  939. };
  940. static void translate_active_width(struct ib_device *ibdev, u8 active_width,
  941. u8 *ib_width)
  942. {
  943. struct mlx5_ib_dev *dev = to_mdev(ibdev);
  944. if (active_width & MLX5_IB_WIDTH_1X)
  945. *ib_width = IB_WIDTH_1X;
  946. else if (active_width & MLX5_IB_WIDTH_4X)
  947. *ib_width = IB_WIDTH_4X;
  948. else if (active_width & MLX5_IB_WIDTH_8X)
  949. *ib_width = IB_WIDTH_8X;
  950. else if (active_width & MLX5_IB_WIDTH_12X)
  951. *ib_width = IB_WIDTH_12X;
  952. else {
  953. mlx5_ib_dbg(dev, "Invalid active_width %d, setting width to default value: 4x\n",
  954. (int)active_width);
  955. *ib_width = IB_WIDTH_4X;
  956. }
  957. return;
  958. }
  959. static int mlx5_mtu_to_ib_mtu(int mtu)
  960. {
  961. switch (mtu) {
  962. case 256: return 1;
  963. case 512: return 2;
  964. case 1024: return 3;
  965. case 2048: return 4;
  966. case 4096: return 5;
  967. default:
  968. pr_warn("invalid mtu\n");
  969. return -1;
  970. }
  971. }
  972. enum ib_max_vl_num {
  973. __IB_MAX_VL_0 = 1,
  974. __IB_MAX_VL_0_1 = 2,
  975. __IB_MAX_VL_0_3 = 3,
  976. __IB_MAX_VL_0_7 = 4,
  977. __IB_MAX_VL_0_14 = 5,
  978. };
  979. enum mlx5_vl_hw_cap {
  980. MLX5_VL_HW_0 = 1,
  981. MLX5_VL_HW_0_1 = 2,
  982. MLX5_VL_HW_0_2 = 3,
  983. MLX5_VL_HW_0_3 = 4,
  984. MLX5_VL_HW_0_4 = 5,
  985. MLX5_VL_HW_0_5 = 6,
  986. MLX5_VL_HW_0_6 = 7,
  987. MLX5_VL_HW_0_7 = 8,
  988. MLX5_VL_HW_0_14 = 15
  989. };
  990. static int translate_max_vl_num(struct ib_device *ibdev, u8 vl_hw_cap,
  991. u8 *max_vl_num)
  992. {
  993. switch (vl_hw_cap) {
  994. case MLX5_VL_HW_0:
  995. *max_vl_num = __IB_MAX_VL_0;
  996. break;
  997. case MLX5_VL_HW_0_1:
  998. *max_vl_num = __IB_MAX_VL_0_1;
  999. break;
  1000. case MLX5_VL_HW_0_3:
  1001. *max_vl_num = __IB_MAX_VL_0_3;
  1002. break;
  1003. case MLX5_VL_HW_0_7:
  1004. *max_vl_num = __IB_MAX_VL_0_7;
  1005. break;
  1006. case MLX5_VL_HW_0_14:
  1007. *max_vl_num = __IB_MAX_VL_0_14;
  1008. break;
  1009. default:
  1010. return -EINVAL;
  1011. }
  1012. return 0;
  1013. }
  1014. static int mlx5_query_hca_port(struct ib_device *ibdev, u8 port,
  1015. struct ib_port_attr *props)
  1016. {
  1017. struct mlx5_ib_dev *dev = to_mdev(ibdev);
  1018. struct mlx5_core_dev *mdev = dev->mdev;
  1019. struct mlx5_hca_vport_context *rep;
  1020. u16 max_mtu;
  1021. u16 oper_mtu;
  1022. int err;
  1023. u8 ib_link_width_oper;
  1024. u8 vl_hw_cap;
  1025. rep = kzalloc(sizeof(*rep), GFP_KERNEL);
  1026. if (!rep) {
  1027. err = -ENOMEM;
  1028. goto out;
  1029. }
  1030. /* props being zeroed by the caller, avoid zeroing it here */
  1031. err = mlx5_query_hca_vport_context(mdev, 0, port, 0, rep);
  1032. if (err)
  1033. goto out;
  1034. props->lid = rep->lid;
  1035. props->lmc = rep->lmc;
  1036. props->sm_lid = rep->sm_lid;
  1037. props->sm_sl = rep->sm_sl;
  1038. props->state = rep->vport_state;
  1039. props->phys_state = rep->port_physical_state;
  1040. props->port_cap_flags = rep->cap_mask1;
  1041. props->gid_tbl_len = mlx5_get_gid_table_len(MLX5_CAP_GEN(mdev, gid_table_size));
  1042. props->max_msg_sz = 1 << MLX5_CAP_GEN(mdev, log_max_msg);
  1043. props->pkey_tbl_len = mlx5_to_sw_pkey_sz(MLX5_CAP_GEN(mdev, pkey_table_size));
  1044. props->bad_pkey_cntr = rep->pkey_violation_counter;
  1045. props->qkey_viol_cntr = rep->qkey_violation_counter;
  1046. props->subnet_timeout = rep->subnet_timeout;
  1047. props->init_type_reply = rep->init_type_reply;
  1048. err = mlx5_query_port_link_width_oper(mdev, &ib_link_width_oper, port);
  1049. if (err)
  1050. goto out;
  1051. translate_active_width(ibdev, ib_link_width_oper, &props->active_width);
  1052. err = mlx5_query_port_ib_proto_oper(mdev, &props->active_speed, port);
  1053. if (err)
  1054. goto out;
  1055. mlx5_query_port_max_mtu(mdev, &max_mtu, port);
  1056. props->max_mtu = mlx5_mtu_to_ib_mtu(max_mtu);
  1057. mlx5_query_port_oper_mtu(mdev, &oper_mtu, port);
  1058. props->active_mtu = mlx5_mtu_to_ib_mtu(oper_mtu);
  1059. err = mlx5_query_port_vl_hw_cap(mdev, &vl_hw_cap, port);
  1060. if (err)
  1061. goto out;
  1062. err = translate_max_vl_num(ibdev, vl_hw_cap,
  1063. &props->max_vl_num);
  1064. out:
  1065. kfree(rep);
  1066. return err;
  1067. }
  1068. int mlx5_ib_query_port(struct ib_device *ibdev, u8 port,
  1069. struct ib_port_attr *props)
  1070. {
  1071. unsigned int count;
  1072. int ret;
  1073. switch (mlx5_get_vport_access_method(ibdev)) {
  1074. case MLX5_VPORT_ACCESS_METHOD_MAD:
  1075. ret = mlx5_query_mad_ifc_port(ibdev, port, props);
  1076. break;
  1077. case MLX5_VPORT_ACCESS_METHOD_HCA:
  1078. ret = mlx5_query_hca_port(ibdev, port, props);
  1079. break;
  1080. case MLX5_VPORT_ACCESS_METHOD_NIC:
  1081. ret = mlx5_query_port_roce(ibdev, port, props);
  1082. break;
  1083. default:
  1084. ret = -EINVAL;
  1085. }
  1086. if (!ret && props) {
  1087. struct mlx5_ib_dev *dev = to_mdev(ibdev);
  1088. struct mlx5_core_dev *mdev;
  1089. bool put_mdev = true;
  1090. mdev = mlx5_ib_get_native_port_mdev(dev, port, NULL);
  1091. if (!mdev) {
  1092. /* If the port isn't affiliated yet query the master.
  1093. * The master and slave will have the same values.
  1094. */
  1095. mdev = dev->mdev;
  1096. port = 1;
  1097. put_mdev = false;
  1098. }
  1099. count = mlx5_core_reserved_gids_count(mdev);
  1100. if (put_mdev)
  1101. mlx5_ib_put_native_port_mdev(dev, port);
  1102. props->gid_tbl_len -= count;
  1103. }
  1104. return ret;
  1105. }
  1106. static int mlx5_ib_rep_query_port(struct ib_device *ibdev, u8 port,
  1107. struct ib_port_attr *props)
  1108. {
  1109. int ret;
  1110. /* Only link layer == ethernet is valid for representors */
  1111. ret = mlx5_query_port_roce(ibdev, port, props);
  1112. if (ret || !props)
  1113. return ret;
  1114. /* We don't support GIDS */
  1115. props->gid_tbl_len = 0;
  1116. return ret;
  1117. }
  1118. static int mlx5_ib_query_gid(struct ib_device *ibdev, u8 port, int index,
  1119. union ib_gid *gid)
  1120. {
  1121. struct mlx5_ib_dev *dev = to_mdev(ibdev);
  1122. struct mlx5_core_dev *mdev = dev->mdev;
  1123. switch (mlx5_get_vport_access_method(ibdev)) {
  1124. case MLX5_VPORT_ACCESS_METHOD_MAD:
  1125. return mlx5_query_mad_ifc_gids(ibdev, port, index, gid);
  1126. case MLX5_VPORT_ACCESS_METHOD_HCA:
  1127. return mlx5_query_hca_vport_gid(mdev, 0, port, 0, index, gid);
  1128. default:
  1129. return -EINVAL;
  1130. }
  1131. }
  1132. static int mlx5_query_hca_nic_pkey(struct ib_device *ibdev, u8 port,
  1133. u16 index, u16 *pkey)
  1134. {
  1135. struct mlx5_ib_dev *dev = to_mdev(ibdev);
  1136. struct mlx5_core_dev *mdev;
  1137. bool put_mdev = true;
  1138. u8 mdev_port_num;
  1139. int err;
  1140. mdev = mlx5_ib_get_native_port_mdev(dev, port, &mdev_port_num);
  1141. if (!mdev) {
  1142. /* The port isn't affiliated yet, get the PKey from the master
  1143. * port. For RoCE the PKey tables will be the same.
  1144. */
  1145. put_mdev = false;
  1146. mdev = dev->mdev;
  1147. mdev_port_num = 1;
  1148. }
  1149. err = mlx5_query_hca_vport_pkey(mdev, 0, mdev_port_num, 0,
  1150. index, pkey);
  1151. if (put_mdev)
  1152. mlx5_ib_put_native_port_mdev(dev, port);
  1153. return err;
  1154. }
  1155. static int mlx5_ib_query_pkey(struct ib_device *ibdev, u8 port, u16 index,
  1156. u16 *pkey)
  1157. {
  1158. switch (mlx5_get_vport_access_method(ibdev)) {
  1159. case MLX5_VPORT_ACCESS_METHOD_MAD:
  1160. return mlx5_query_mad_ifc_pkey(ibdev, port, index, pkey);
  1161. case MLX5_VPORT_ACCESS_METHOD_HCA:
  1162. case MLX5_VPORT_ACCESS_METHOD_NIC:
  1163. return mlx5_query_hca_nic_pkey(ibdev, port, index, pkey);
  1164. default:
  1165. return -EINVAL;
  1166. }
  1167. }
  1168. static int mlx5_ib_modify_device(struct ib_device *ibdev, int mask,
  1169. struct ib_device_modify *props)
  1170. {
  1171. struct mlx5_ib_dev *dev = to_mdev(ibdev);
  1172. struct mlx5_reg_node_desc in;
  1173. struct mlx5_reg_node_desc out;
  1174. int err;
  1175. if (mask & ~IB_DEVICE_MODIFY_NODE_DESC)
  1176. return -EOPNOTSUPP;
  1177. if (!(mask & IB_DEVICE_MODIFY_NODE_DESC))
  1178. return 0;
  1179. /*
  1180. * If possible, pass node desc to FW, so it can generate
  1181. * a 144 trap. If cmd fails, just ignore.
  1182. */
  1183. memcpy(&in, props->node_desc, IB_DEVICE_NODE_DESC_MAX);
  1184. err = mlx5_core_access_reg(dev->mdev, &in, sizeof(in), &out,
  1185. sizeof(out), MLX5_REG_NODE_DESC, 0, 1);
  1186. if (err)
  1187. return err;
  1188. memcpy(ibdev->node_desc, props->node_desc, IB_DEVICE_NODE_DESC_MAX);
  1189. return err;
  1190. }
  1191. static int set_port_caps_atomic(struct mlx5_ib_dev *dev, u8 port_num, u32 mask,
  1192. u32 value)
  1193. {
  1194. struct mlx5_hca_vport_context ctx = {};
  1195. struct mlx5_core_dev *mdev;
  1196. u8 mdev_port_num;
  1197. int err;
  1198. mdev = mlx5_ib_get_native_port_mdev(dev, port_num, &mdev_port_num);
  1199. if (!mdev)
  1200. return -ENODEV;
  1201. err = mlx5_query_hca_vport_context(mdev, 0, mdev_port_num, 0, &ctx);
  1202. if (err)
  1203. goto out;
  1204. if (~ctx.cap_mask1_perm & mask) {
  1205. mlx5_ib_warn(dev, "trying to change bitmask 0x%X but change supported 0x%X\n",
  1206. mask, ctx.cap_mask1_perm);
  1207. err = -EINVAL;
  1208. goto out;
  1209. }
  1210. ctx.cap_mask1 = value;
  1211. ctx.cap_mask1_perm = mask;
  1212. err = mlx5_core_modify_hca_vport_context(mdev, 0, mdev_port_num,
  1213. 0, &ctx);
  1214. out:
  1215. mlx5_ib_put_native_port_mdev(dev, port_num);
  1216. return err;
  1217. }
  1218. static int mlx5_ib_modify_port(struct ib_device *ibdev, u8 port, int mask,
  1219. struct ib_port_modify *props)
  1220. {
  1221. struct mlx5_ib_dev *dev = to_mdev(ibdev);
  1222. struct ib_port_attr attr;
  1223. u32 tmp;
  1224. int err;
  1225. u32 change_mask;
  1226. u32 value;
  1227. bool is_ib = (mlx5_ib_port_link_layer(ibdev, port) ==
  1228. IB_LINK_LAYER_INFINIBAND);
  1229. /* CM layer calls ib_modify_port() regardless of the link layer. For
  1230. * Ethernet ports, qkey violation and Port capabilities are meaningless.
  1231. */
  1232. if (!is_ib)
  1233. return 0;
  1234. if (MLX5_CAP_GEN(dev->mdev, ib_virt) && is_ib) {
  1235. change_mask = props->clr_port_cap_mask | props->set_port_cap_mask;
  1236. value = ~props->clr_port_cap_mask | props->set_port_cap_mask;
  1237. return set_port_caps_atomic(dev, port, change_mask, value);
  1238. }
  1239. mutex_lock(&dev->cap_mask_mutex);
  1240. err = ib_query_port(ibdev, port, &attr);
  1241. if (err)
  1242. goto out;
  1243. tmp = (attr.port_cap_flags | props->set_port_cap_mask) &
  1244. ~props->clr_port_cap_mask;
  1245. err = mlx5_set_port_caps(dev->mdev, port, tmp);
  1246. out:
  1247. mutex_unlock(&dev->cap_mask_mutex);
  1248. return err;
  1249. }
  1250. static void print_lib_caps(struct mlx5_ib_dev *dev, u64 caps)
  1251. {
  1252. mlx5_ib_dbg(dev, "MLX5_LIB_CAP_4K_UAR = %s\n",
  1253. caps & MLX5_LIB_CAP_4K_UAR ? "y" : "n");
  1254. }
  1255. static u16 calc_dynamic_bfregs(int uars_per_sys_page)
  1256. {
  1257. /* Large page with non 4k uar support might limit the dynamic size */
  1258. if (uars_per_sys_page == 1 && PAGE_SIZE > 4096)
  1259. return MLX5_MIN_DYN_BFREGS;
  1260. return MLX5_MAX_DYN_BFREGS;
  1261. }
  1262. static int calc_total_bfregs(struct mlx5_ib_dev *dev, bool lib_uar_4k,
  1263. struct mlx5_ib_alloc_ucontext_req_v2 *req,
  1264. struct mlx5_bfreg_info *bfregi)
  1265. {
  1266. int uars_per_sys_page;
  1267. int bfregs_per_sys_page;
  1268. int ref_bfregs = req->total_num_bfregs;
  1269. if (req->total_num_bfregs == 0)
  1270. return -EINVAL;
  1271. BUILD_BUG_ON(MLX5_MAX_BFREGS % MLX5_NON_FP_BFREGS_IN_PAGE);
  1272. BUILD_BUG_ON(MLX5_MAX_BFREGS < MLX5_NON_FP_BFREGS_IN_PAGE);
  1273. if (req->total_num_bfregs > MLX5_MAX_BFREGS)
  1274. return -ENOMEM;
  1275. uars_per_sys_page = get_uars_per_sys_page(dev, lib_uar_4k);
  1276. bfregs_per_sys_page = uars_per_sys_page * MLX5_NON_FP_BFREGS_PER_UAR;
  1277. /* This holds the required static allocation asked by the user */
  1278. req->total_num_bfregs = ALIGN(req->total_num_bfregs, bfregs_per_sys_page);
  1279. if (req->num_low_latency_bfregs > req->total_num_bfregs - 1)
  1280. return -EINVAL;
  1281. bfregi->num_static_sys_pages = req->total_num_bfregs / bfregs_per_sys_page;
  1282. bfregi->num_dyn_bfregs = ALIGN(calc_dynamic_bfregs(uars_per_sys_page), bfregs_per_sys_page);
  1283. bfregi->total_num_bfregs = req->total_num_bfregs + bfregi->num_dyn_bfregs;
  1284. bfregi->num_sys_pages = bfregi->total_num_bfregs / bfregs_per_sys_page;
  1285. mlx5_ib_dbg(dev, "uar_4k: fw support %s, lib support %s, user requested %d bfregs, allocated %d, total bfregs %d, using %d sys pages\n",
  1286. MLX5_CAP_GEN(dev->mdev, uar_4k) ? "yes" : "no",
  1287. lib_uar_4k ? "yes" : "no", ref_bfregs,
  1288. req->total_num_bfregs, bfregi->total_num_bfregs,
  1289. bfregi->num_sys_pages);
  1290. return 0;
  1291. }
  1292. static int allocate_uars(struct mlx5_ib_dev *dev, struct mlx5_ib_ucontext *context)
  1293. {
  1294. struct mlx5_bfreg_info *bfregi;
  1295. int err;
  1296. int i;
  1297. bfregi = &context->bfregi;
  1298. for (i = 0; i < bfregi->num_static_sys_pages; i++) {
  1299. err = mlx5_cmd_alloc_uar(dev->mdev, &bfregi->sys_pages[i]);
  1300. if (err)
  1301. goto error;
  1302. mlx5_ib_dbg(dev, "allocated uar %d\n", bfregi->sys_pages[i]);
  1303. }
  1304. for (i = bfregi->num_static_sys_pages; i < bfregi->num_sys_pages; i++)
  1305. bfregi->sys_pages[i] = MLX5_IB_INVALID_UAR_INDEX;
  1306. return 0;
  1307. error:
  1308. for (--i; i >= 0; i--)
  1309. if (mlx5_cmd_free_uar(dev->mdev, bfregi->sys_pages[i]))
  1310. mlx5_ib_warn(dev, "failed to free uar %d\n", i);
  1311. return err;
  1312. }
  1313. static void deallocate_uars(struct mlx5_ib_dev *dev,
  1314. struct mlx5_ib_ucontext *context)
  1315. {
  1316. struct mlx5_bfreg_info *bfregi;
  1317. int i;
  1318. bfregi = &context->bfregi;
  1319. for (i = 0; i < bfregi->num_sys_pages; i++)
  1320. if (i < bfregi->num_static_sys_pages ||
  1321. bfregi->sys_pages[i] != MLX5_IB_INVALID_UAR_INDEX)
  1322. mlx5_cmd_free_uar(dev->mdev, bfregi->sys_pages[i]);
  1323. }
  1324. static int mlx5_ib_alloc_transport_domain(struct mlx5_ib_dev *dev, u32 *tdn)
  1325. {
  1326. int err;
  1327. if (!MLX5_CAP_GEN(dev->mdev, log_max_transport_domain))
  1328. return 0;
  1329. err = mlx5_core_alloc_transport_domain(dev->mdev, tdn);
  1330. if (err)
  1331. return err;
  1332. if ((MLX5_CAP_GEN(dev->mdev, port_type) != MLX5_CAP_PORT_TYPE_ETH) ||
  1333. (!MLX5_CAP_GEN(dev->mdev, disable_local_lb_uc) &&
  1334. !MLX5_CAP_GEN(dev->mdev, disable_local_lb_mc)))
  1335. return err;
  1336. mutex_lock(&dev->lb_mutex);
  1337. dev->user_td++;
  1338. if (dev->user_td == 2)
  1339. err = mlx5_nic_vport_update_local_lb(dev->mdev, true);
  1340. mutex_unlock(&dev->lb_mutex);
  1341. return err;
  1342. }
  1343. static void mlx5_ib_dealloc_transport_domain(struct mlx5_ib_dev *dev, u32 tdn)
  1344. {
  1345. if (!MLX5_CAP_GEN(dev->mdev, log_max_transport_domain))
  1346. return;
  1347. mlx5_core_dealloc_transport_domain(dev->mdev, tdn);
  1348. if ((MLX5_CAP_GEN(dev->mdev, port_type) != MLX5_CAP_PORT_TYPE_ETH) ||
  1349. (!MLX5_CAP_GEN(dev->mdev, disable_local_lb_uc) &&
  1350. !MLX5_CAP_GEN(dev->mdev, disable_local_lb_mc)))
  1351. return;
  1352. mutex_lock(&dev->lb_mutex);
  1353. dev->user_td--;
  1354. if (dev->user_td < 2)
  1355. mlx5_nic_vport_update_local_lb(dev->mdev, false);
  1356. mutex_unlock(&dev->lb_mutex);
  1357. }
  1358. static struct ib_ucontext *mlx5_ib_alloc_ucontext(struct ib_device *ibdev,
  1359. struct ib_udata *udata)
  1360. {
  1361. struct mlx5_ib_dev *dev = to_mdev(ibdev);
  1362. struct mlx5_ib_alloc_ucontext_req_v2 req = {};
  1363. struct mlx5_ib_alloc_ucontext_resp resp = {};
  1364. struct mlx5_core_dev *mdev = dev->mdev;
  1365. struct mlx5_ib_ucontext *context;
  1366. struct mlx5_bfreg_info *bfregi;
  1367. int ver;
  1368. int err;
  1369. size_t min_req_v2 = offsetof(struct mlx5_ib_alloc_ucontext_req_v2,
  1370. max_cqe_version);
  1371. u32 dump_fill_mkey;
  1372. bool lib_uar_4k;
  1373. if (!dev->ib_active)
  1374. return ERR_PTR(-EAGAIN);
  1375. if (udata->inlen == sizeof(struct mlx5_ib_alloc_ucontext_req))
  1376. ver = 0;
  1377. else if (udata->inlen >= min_req_v2)
  1378. ver = 2;
  1379. else
  1380. return ERR_PTR(-EINVAL);
  1381. err = ib_copy_from_udata(&req, udata, min(udata->inlen, sizeof(req)));
  1382. if (err)
  1383. return ERR_PTR(err);
  1384. if (req.flags & ~MLX5_IB_ALLOC_UCTX_DEVX)
  1385. return ERR_PTR(-EOPNOTSUPP);
  1386. if (req.comp_mask || req.reserved0 || req.reserved1 || req.reserved2)
  1387. return ERR_PTR(-EOPNOTSUPP);
  1388. req.total_num_bfregs = ALIGN(req.total_num_bfregs,
  1389. MLX5_NON_FP_BFREGS_PER_UAR);
  1390. if (req.num_low_latency_bfregs > req.total_num_bfregs - 1)
  1391. return ERR_PTR(-EINVAL);
  1392. resp.qp_tab_size = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp);
  1393. if (mlx5_core_is_pf(dev->mdev) && MLX5_CAP_GEN(dev->mdev, bf))
  1394. resp.bf_reg_size = 1 << MLX5_CAP_GEN(dev->mdev, log_bf_reg_size);
  1395. resp.cache_line_size = cache_line_size();
  1396. resp.max_sq_desc_sz = MLX5_CAP_GEN(dev->mdev, max_wqe_sz_sq);
  1397. resp.max_rq_desc_sz = MLX5_CAP_GEN(dev->mdev, max_wqe_sz_rq);
  1398. resp.max_send_wqebb = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz);
  1399. resp.max_recv_wr = 1 << MLX5_CAP_GEN(dev->mdev, log_max_qp_sz);
  1400. resp.max_srq_recv_wr = 1 << MLX5_CAP_GEN(dev->mdev, log_max_srq_sz);
  1401. resp.cqe_version = min_t(__u8,
  1402. (__u8)MLX5_CAP_GEN(dev->mdev, cqe_version),
  1403. req.max_cqe_version);
  1404. resp.log_uar_size = MLX5_CAP_GEN(dev->mdev, uar_4k) ?
  1405. MLX5_ADAPTER_PAGE_SHIFT : PAGE_SHIFT;
  1406. resp.num_uars_per_page = MLX5_CAP_GEN(dev->mdev, uar_4k) ?
  1407. MLX5_CAP_GEN(dev->mdev, num_of_uars_per_page) : 1;
  1408. resp.response_length = min(offsetof(typeof(resp), response_length) +
  1409. sizeof(resp.response_length), udata->outlen);
  1410. if (mlx5_accel_ipsec_device_caps(dev->mdev) & MLX5_ACCEL_IPSEC_CAP_DEVICE) {
  1411. if (mlx5_get_flow_namespace(dev->mdev, MLX5_FLOW_NAMESPACE_EGRESS))
  1412. resp.flow_action_flags |= MLX5_USER_ALLOC_UCONTEXT_FLOW_ACTION_FLAGS_ESP_AES_GCM;
  1413. if (mlx5_accel_ipsec_device_caps(dev->mdev) & MLX5_ACCEL_IPSEC_CAP_REQUIRED_METADATA)
  1414. resp.flow_action_flags |= MLX5_USER_ALLOC_UCONTEXT_FLOW_ACTION_FLAGS_ESP_AES_GCM_REQ_METADATA;
  1415. if (MLX5_CAP_FLOWTABLE(dev->mdev, flow_table_properties_nic_receive.ft_field_support.outer_esp_spi))
  1416. resp.flow_action_flags |= MLX5_USER_ALLOC_UCONTEXT_FLOW_ACTION_FLAGS_ESP_AES_GCM_SPI_STEERING;
  1417. if (mlx5_accel_ipsec_device_caps(dev->mdev) & MLX5_ACCEL_IPSEC_CAP_TX_IV_IS_ESN)
  1418. resp.flow_action_flags |= MLX5_USER_ALLOC_UCONTEXT_FLOW_ACTION_FLAGS_ESP_AES_GCM_TX_IV_IS_ESN;
  1419. /* MLX5_USER_ALLOC_UCONTEXT_FLOW_ACTION_FLAGS_ESP_AES_GCM_FULL_OFFLOAD is currently always 0 */
  1420. }
  1421. context = kzalloc(sizeof(*context), GFP_KERNEL);
  1422. if (!context)
  1423. return ERR_PTR(-ENOMEM);
  1424. lib_uar_4k = req.lib_caps & MLX5_LIB_CAP_4K_UAR;
  1425. bfregi = &context->bfregi;
  1426. /* updates req->total_num_bfregs */
  1427. err = calc_total_bfregs(dev, lib_uar_4k, &req, bfregi);
  1428. if (err)
  1429. goto out_ctx;
  1430. mutex_init(&bfregi->lock);
  1431. bfregi->lib_uar_4k = lib_uar_4k;
  1432. bfregi->count = kcalloc(bfregi->total_num_bfregs, sizeof(*bfregi->count),
  1433. GFP_KERNEL);
  1434. if (!bfregi->count) {
  1435. err = -ENOMEM;
  1436. goto out_ctx;
  1437. }
  1438. bfregi->sys_pages = kcalloc(bfregi->num_sys_pages,
  1439. sizeof(*bfregi->sys_pages),
  1440. GFP_KERNEL);
  1441. if (!bfregi->sys_pages) {
  1442. err = -ENOMEM;
  1443. goto out_count;
  1444. }
  1445. err = allocate_uars(dev, context);
  1446. if (err)
  1447. goto out_sys_pages;
  1448. #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
  1449. context->ibucontext.invalidate_range = &mlx5_ib_invalidate_range;
  1450. #endif
  1451. err = mlx5_ib_alloc_transport_domain(dev, &context->tdn);
  1452. if (err)
  1453. goto out_uars;
  1454. if (req.flags & MLX5_IB_ALLOC_UCTX_DEVX) {
  1455. /* Block DEVX on Infiniband as of SELinux */
  1456. if (mlx5_ib_port_link_layer(ibdev, 1) != IB_LINK_LAYER_ETHERNET) {
  1457. err = -EPERM;
  1458. goto out_td;
  1459. }
  1460. err = mlx5_ib_devx_create(dev, context);
  1461. if (err)
  1462. goto out_td;
  1463. }
  1464. if (MLX5_CAP_GEN(dev->mdev, dump_fill_mkey)) {
  1465. err = mlx5_cmd_dump_fill_mkey(dev->mdev, &dump_fill_mkey);
  1466. if (err)
  1467. goto out_mdev;
  1468. }
  1469. INIT_LIST_HEAD(&context->vma_private_list);
  1470. mutex_init(&context->vma_private_list_mutex);
  1471. INIT_LIST_HEAD(&context->db_page_list);
  1472. mutex_init(&context->db_page_mutex);
  1473. resp.tot_bfregs = req.total_num_bfregs;
  1474. resp.num_ports = dev->num_ports;
  1475. if (field_avail(typeof(resp), cqe_version, udata->outlen))
  1476. resp.response_length += sizeof(resp.cqe_version);
  1477. if (field_avail(typeof(resp), cmds_supp_uhw, udata->outlen)) {
  1478. resp.cmds_supp_uhw |= MLX5_USER_CMDS_SUPP_UHW_QUERY_DEVICE |
  1479. MLX5_USER_CMDS_SUPP_UHW_CREATE_AH;
  1480. resp.response_length += sizeof(resp.cmds_supp_uhw);
  1481. }
  1482. if (field_avail(typeof(resp), eth_min_inline, udata->outlen)) {
  1483. if (mlx5_ib_port_link_layer(ibdev, 1) == IB_LINK_LAYER_ETHERNET) {
  1484. mlx5_query_min_inline(dev->mdev, &resp.eth_min_inline);
  1485. resp.eth_min_inline++;
  1486. }
  1487. resp.response_length += sizeof(resp.eth_min_inline);
  1488. }
  1489. if (field_avail(typeof(resp), clock_info_versions, udata->outlen)) {
  1490. if (mdev->clock_info)
  1491. resp.clock_info_versions = BIT(MLX5_IB_CLOCK_INFO_V1);
  1492. resp.response_length += sizeof(resp.clock_info_versions);
  1493. }
  1494. /*
  1495. * We don't want to expose information from the PCI bar that is located
  1496. * after 4096 bytes, so if the arch only supports larger pages, let's
  1497. * pretend we don't support reading the HCA's core clock. This is also
  1498. * forced by mmap function.
  1499. */
  1500. if (field_avail(typeof(resp), hca_core_clock_offset, udata->outlen)) {
  1501. if (PAGE_SIZE <= 4096) {
  1502. resp.comp_mask |=
  1503. MLX5_IB_ALLOC_UCONTEXT_RESP_MASK_CORE_CLOCK_OFFSET;
  1504. resp.hca_core_clock_offset =
  1505. offsetof(struct mlx5_init_seg, internal_timer_h) % PAGE_SIZE;
  1506. }
  1507. resp.response_length += sizeof(resp.hca_core_clock_offset);
  1508. }
  1509. if (field_avail(typeof(resp), log_uar_size, udata->outlen))
  1510. resp.response_length += sizeof(resp.log_uar_size);
  1511. if (field_avail(typeof(resp), num_uars_per_page, udata->outlen))
  1512. resp.response_length += sizeof(resp.num_uars_per_page);
  1513. if (field_avail(typeof(resp), num_dyn_bfregs, udata->outlen)) {
  1514. resp.num_dyn_bfregs = bfregi->num_dyn_bfregs;
  1515. resp.response_length += sizeof(resp.num_dyn_bfregs);
  1516. }
  1517. if (field_avail(typeof(resp), dump_fill_mkey, udata->outlen)) {
  1518. if (MLX5_CAP_GEN(dev->mdev, dump_fill_mkey)) {
  1519. resp.dump_fill_mkey = dump_fill_mkey;
  1520. resp.comp_mask |=
  1521. MLX5_IB_ALLOC_UCONTEXT_RESP_MASK_DUMP_FILL_MKEY;
  1522. }
  1523. resp.response_length += sizeof(resp.dump_fill_mkey);
  1524. }
  1525. err = ib_copy_to_udata(udata, &resp, resp.response_length);
  1526. if (err)
  1527. goto out_mdev;
  1528. bfregi->ver = ver;
  1529. bfregi->num_low_latency_bfregs = req.num_low_latency_bfregs;
  1530. context->cqe_version = resp.cqe_version;
  1531. context->lib_caps = req.lib_caps;
  1532. print_lib_caps(dev, context->lib_caps);
  1533. if (mlx5_lag_is_active(dev->mdev)) {
  1534. u8 port = mlx5_core_native_port_num(dev->mdev);
  1535. atomic_set(&context->tx_port_affinity,
  1536. atomic_add_return(
  1537. 1, &dev->roce[port].tx_port_affinity));
  1538. }
  1539. return &context->ibucontext;
  1540. out_mdev:
  1541. if (req.flags & MLX5_IB_ALLOC_UCTX_DEVX)
  1542. mlx5_ib_devx_destroy(dev, context);
  1543. out_td:
  1544. mlx5_ib_dealloc_transport_domain(dev, context->tdn);
  1545. out_uars:
  1546. deallocate_uars(dev, context);
  1547. out_sys_pages:
  1548. kfree(bfregi->sys_pages);
  1549. out_count:
  1550. kfree(bfregi->count);
  1551. out_ctx:
  1552. kfree(context);
  1553. return ERR_PTR(err);
  1554. }
  1555. static int mlx5_ib_dealloc_ucontext(struct ib_ucontext *ibcontext)
  1556. {
  1557. struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
  1558. struct mlx5_ib_dev *dev = to_mdev(ibcontext->device);
  1559. struct mlx5_bfreg_info *bfregi;
  1560. if (context->devx_uid)
  1561. mlx5_ib_devx_destroy(dev, context);
  1562. bfregi = &context->bfregi;
  1563. mlx5_ib_dealloc_transport_domain(dev, context->tdn);
  1564. deallocate_uars(dev, context);
  1565. kfree(bfregi->sys_pages);
  1566. kfree(bfregi->count);
  1567. kfree(context);
  1568. return 0;
  1569. }
  1570. static phys_addr_t uar_index2pfn(struct mlx5_ib_dev *dev,
  1571. int uar_idx)
  1572. {
  1573. int fw_uars_per_page;
  1574. fw_uars_per_page = MLX5_CAP_GEN(dev->mdev, uar_4k) ? MLX5_UARS_IN_PAGE : 1;
  1575. return (pci_resource_start(dev->mdev->pdev, 0) >> PAGE_SHIFT) + uar_idx / fw_uars_per_page;
  1576. }
  1577. static int get_command(unsigned long offset)
  1578. {
  1579. return (offset >> MLX5_IB_MMAP_CMD_SHIFT) & MLX5_IB_MMAP_CMD_MASK;
  1580. }
  1581. static int get_arg(unsigned long offset)
  1582. {
  1583. return offset & ((1 << MLX5_IB_MMAP_CMD_SHIFT) - 1);
  1584. }
  1585. static int get_index(unsigned long offset)
  1586. {
  1587. return get_arg(offset);
  1588. }
  1589. /* Index resides in an extra byte to enable larger values than 255 */
  1590. static int get_extended_index(unsigned long offset)
  1591. {
  1592. return get_arg(offset) | ((offset >> 16) & 0xff) << 8;
  1593. }
  1594. static void mlx5_ib_vma_open(struct vm_area_struct *area)
  1595. {
  1596. /* vma_open is called when a new VMA is created on top of our VMA. This
  1597. * is done through either mremap flow or split_vma (usually due to
  1598. * mlock, madvise, munmap, etc.) We do not support a clone of the VMA,
  1599. * as this VMA is strongly hardware related. Therefore we set the
  1600. * vm_ops of the newly created/cloned VMA to NULL, to prevent it from
  1601. * calling us again and trying to do incorrect actions. We assume that
  1602. * the original VMA size is exactly a single page, and therefore all
  1603. * "splitting" operation will not happen to it.
  1604. */
  1605. area->vm_ops = NULL;
  1606. }
  1607. static void mlx5_ib_vma_close(struct vm_area_struct *area)
  1608. {
  1609. struct mlx5_ib_vma_private_data *mlx5_ib_vma_priv_data;
  1610. /* It's guaranteed that all VMAs opened on a FD are closed before the
  1611. * file itself is closed, therefore no sync is needed with the regular
  1612. * closing flow. (e.g. mlx5 ib_dealloc_ucontext)
  1613. * However need a sync with accessing the vma as part of
  1614. * mlx5_ib_disassociate_ucontext.
  1615. * The close operation is usually called under mm->mmap_sem except when
  1616. * process is exiting.
  1617. * The exiting case is handled explicitly as part of
  1618. * mlx5_ib_disassociate_ucontext.
  1619. */
  1620. mlx5_ib_vma_priv_data = (struct mlx5_ib_vma_private_data *)area->vm_private_data;
  1621. /* setting the vma context pointer to null in the mlx5_ib driver's
  1622. * private data, to protect a race condition in
  1623. * mlx5_ib_disassociate_ucontext().
  1624. */
  1625. mlx5_ib_vma_priv_data->vma = NULL;
  1626. mutex_lock(mlx5_ib_vma_priv_data->vma_private_list_mutex);
  1627. list_del(&mlx5_ib_vma_priv_data->list);
  1628. mutex_unlock(mlx5_ib_vma_priv_data->vma_private_list_mutex);
  1629. kfree(mlx5_ib_vma_priv_data);
  1630. }
  1631. static const struct vm_operations_struct mlx5_ib_vm_ops = {
  1632. .open = mlx5_ib_vma_open,
  1633. .close = mlx5_ib_vma_close
  1634. };
  1635. static int mlx5_ib_set_vma_data(struct vm_area_struct *vma,
  1636. struct mlx5_ib_ucontext *ctx)
  1637. {
  1638. struct mlx5_ib_vma_private_data *vma_prv;
  1639. struct list_head *vma_head = &ctx->vma_private_list;
  1640. vma_prv = kzalloc(sizeof(*vma_prv), GFP_KERNEL);
  1641. if (!vma_prv)
  1642. return -ENOMEM;
  1643. vma_prv->vma = vma;
  1644. vma_prv->vma_private_list_mutex = &ctx->vma_private_list_mutex;
  1645. vma->vm_private_data = vma_prv;
  1646. vma->vm_ops = &mlx5_ib_vm_ops;
  1647. mutex_lock(&ctx->vma_private_list_mutex);
  1648. list_add(&vma_prv->list, vma_head);
  1649. mutex_unlock(&ctx->vma_private_list_mutex);
  1650. return 0;
  1651. }
  1652. static void mlx5_ib_disassociate_ucontext(struct ib_ucontext *ibcontext)
  1653. {
  1654. struct vm_area_struct *vma;
  1655. struct mlx5_ib_vma_private_data *vma_private, *n;
  1656. struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
  1657. mutex_lock(&context->vma_private_list_mutex);
  1658. list_for_each_entry_safe(vma_private, n, &context->vma_private_list,
  1659. list) {
  1660. vma = vma_private->vma;
  1661. zap_vma_ptes(vma, vma->vm_start, PAGE_SIZE);
  1662. /* context going to be destroyed, should
  1663. * not access ops any more.
  1664. */
  1665. vma->vm_flags &= ~(VM_SHARED | VM_MAYSHARE);
  1666. vma->vm_ops = NULL;
  1667. list_del(&vma_private->list);
  1668. kfree(vma_private);
  1669. }
  1670. mutex_unlock(&context->vma_private_list_mutex);
  1671. }
  1672. static inline char *mmap_cmd2str(enum mlx5_ib_mmap_cmd cmd)
  1673. {
  1674. switch (cmd) {
  1675. case MLX5_IB_MMAP_WC_PAGE:
  1676. return "WC";
  1677. case MLX5_IB_MMAP_REGULAR_PAGE:
  1678. return "best effort WC";
  1679. case MLX5_IB_MMAP_NC_PAGE:
  1680. return "NC";
  1681. case MLX5_IB_MMAP_DEVICE_MEM:
  1682. return "Device Memory";
  1683. default:
  1684. return NULL;
  1685. }
  1686. }
  1687. static int mlx5_ib_mmap_clock_info_page(struct mlx5_ib_dev *dev,
  1688. struct vm_area_struct *vma,
  1689. struct mlx5_ib_ucontext *context)
  1690. {
  1691. phys_addr_t pfn;
  1692. int err;
  1693. if (vma->vm_end - vma->vm_start != PAGE_SIZE)
  1694. return -EINVAL;
  1695. if (get_index(vma->vm_pgoff) != MLX5_IB_CLOCK_INFO_V1)
  1696. return -EOPNOTSUPP;
  1697. if (vma->vm_flags & VM_WRITE)
  1698. return -EPERM;
  1699. vma->vm_flags &= ~VM_MAYWRITE;
  1700. if (!dev->mdev->clock_info_page)
  1701. return -EOPNOTSUPP;
  1702. pfn = page_to_pfn(dev->mdev->clock_info_page);
  1703. err = remap_pfn_range(vma, vma->vm_start, pfn, PAGE_SIZE,
  1704. vma->vm_page_prot);
  1705. if (err)
  1706. return err;
  1707. return mlx5_ib_set_vma_data(vma, context);
  1708. }
  1709. static int uar_mmap(struct mlx5_ib_dev *dev, enum mlx5_ib_mmap_cmd cmd,
  1710. struct vm_area_struct *vma,
  1711. struct mlx5_ib_ucontext *context)
  1712. {
  1713. struct mlx5_bfreg_info *bfregi = &context->bfregi;
  1714. int err;
  1715. unsigned long idx;
  1716. phys_addr_t pfn;
  1717. pgprot_t prot;
  1718. u32 bfreg_dyn_idx = 0;
  1719. u32 uar_index;
  1720. int dyn_uar = (cmd == MLX5_IB_MMAP_ALLOC_WC);
  1721. int max_valid_idx = dyn_uar ? bfregi->num_sys_pages :
  1722. bfregi->num_static_sys_pages;
  1723. if (vma->vm_end - vma->vm_start != PAGE_SIZE)
  1724. return -EINVAL;
  1725. if (dyn_uar)
  1726. idx = get_extended_index(vma->vm_pgoff) + bfregi->num_static_sys_pages;
  1727. else
  1728. idx = get_index(vma->vm_pgoff);
  1729. if (idx >= max_valid_idx) {
  1730. mlx5_ib_warn(dev, "invalid uar index %lu, max=%d\n",
  1731. idx, max_valid_idx);
  1732. return -EINVAL;
  1733. }
  1734. switch (cmd) {
  1735. case MLX5_IB_MMAP_WC_PAGE:
  1736. case MLX5_IB_MMAP_ALLOC_WC:
  1737. /* Some architectures don't support WC memory */
  1738. #if defined(CONFIG_X86)
  1739. if (!pat_enabled())
  1740. return -EPERM;
  1741. #elif !(defined(CONFIG_PPC) || (defined(CONFIG_ARM) && defined(CONFIG_MMU)))
  1742. return -EPERM;
  1743. #endif
  1744. /* fall through */
  1745. case MLX5_IB_MMAP_REGULAR_PAGE:
  1746. /* For MLX5_IB_MMAP_REGULAR_PAGE do the best effort to get WC */
  1747. prot = pgprot_writecombine(vma->vm_page_prot);
  1748. break;
  1749. case MLX5_IB_MMAP_NC_PAGE:
  1750. prot = pgprot_noncached(vma->vm_page_prot);
  1751. break;
  1752. default:
  1753. return -EINVAL;
  1754. }
  1755. if (dyn_uar) {
  1756. int uars_per_page;
  1757. uars_per_page = get_uars_per_sys_page(dev, bfregi->lib_uar_4k);
  1758. bfreg_dyn_idx = idx * (uars_per_page * MLX5_NON_FP_BFREGS_PER_UAR);
  1759. if (bfreg_dyn_idx >= bfregi->total_num_bfregs) {
  1760. mlx5_ib_warn(dev, "invalid bfreg_dyn_idx %u, max=%u\n",
  1761. bfreg_dyn_idx, bfregi->total_num_bfregs);
  1762. return -EINVAL;
  1763. }
  1764. mutex_lock(&bfregi->lock);
  1765. /* Fail if uar already allocated, first bfreg index of each
  1766. * page holds its count.
  1767. */
  1768. if (bfregi->count[bfreg_dyn_idx]) {
  1769. mlx5_ib_warn(dev, "wrong offset, idx %lu is busy, bfregn=%u\n", idx, bfreg_dyn_idx);
  1770. mutex_unlock(&bfregi->lock);
  1771. return -EINVAL;
  1772. }
  1773. bfregi->count[bfreg_dyn_idx]++;
  1774. mutex_unlock(&bfregi->lock);
  1775. err = mlx5_cmd_alloc_uar(dev->mdev, &uar_index);
  1776. if (err) {
  1777. mlx5_ib_warn(dev, "UAR alloc failed\n");
  1778. goto free_bfreg;
  1779. }
  1780. } else {
  1781. uar_index = bfregi->sys_pages[idx];
  1782. }
  1783. pfn = uar_index2pfn(dev, uar_index);
  1784. mlx5_ib_dbg(dev, "uar idx 0x%lx, pfn %pa\n", idx, &pfn);
  1785. vma->vm_page_prot = prot;
  1786. err = io_remap_pfn_range(vma, vma->vm_start, pfn,
  1787. PAGE_SIZE, vma->vm_page_prot);
  1788. if (err) {
  1789. mlx5_ib_err(dev,
  1790. "io_remap_pfn_range failed with error=%d, mmap_cmd=%s\n",
  1791. err, mmap_cmd2str(cmd));
  1792. err = -EAGAIN;
  1793. goto err;
  1794. }
  1795. err = mlx5_ib_set_vma_data(vma, context);
  1796. if (err)
  1797. goto err;
  1798. if (dyn_uar)
  1799. bfregi->sys_pages[idx] = uar_index;
  1800. return 0;
  1801. err:
  1802. if (!dyn_uar)
  1803. return err;
  1804. mlx5_cmd_free_uar(dev->mdev, idx);
  1805. free_bfreg:
  1806. mlx5_ib_free_bfreg(dev, bfregi, bfreg_dyn_idx);
  1807. return err;
  1808. }
  1809. static int dm_mmap(struct ib_ucontext *context, struct vm_area_struct *vma)
  1810. {
  1811. struct mlx5_ib_ucontext *mctx = to_mucontext(context);
  1812. struct mlx5_ib_dev *dev = to_mdev(context->device);
  1813. u16 page_idx = get_extended_index(vma->vm_pgoff);
  1814. size_t map_size = vma->vm_end - vma->vm_start;
  1815. u32 npages = map_size >> PAGE_SHIFT;
  1816. phys_addr_t pfn;
  1817. pgprot_t prot;
  1818. if (find_next_zero_bit(mctx->dm_pages, page_idx + npages, page_idx) !=
  1819. page_idx + npages)
  1820. return -EINVAL;
  1821. pfn = ((pci_resource_start(dev->mdev->pdev, 0) +
  1822. MLX5_CAP64_DEV_MEM(dev->mdev, memic_bar_start_addr)) >>
  1823. PAGE_SHIFT) +
  1824. page_idx;
  1825. prot = pgprot_writecombine(vma->vm_page_prot);
  1826. vma->vm_page_prot = prot;
  1827. if (io_remap_pfn_range(vma, vma->vm_start, pfn, map_size,
  1828. vma->vm_page_prot))
  1829. return -EAGAIN;
  1830. return mlx5_ib_set_vma_data(vma, mctx);
  1831. }
  1832. static int mlx5_ib_mmap(struct ib_ucontext *ibcontext, struct vm_area_struct *vma)
  1833. {
  1834. struct mlx5_ib_ucontext *context = to_mucontext(ibcontext);
  1835. struct mlx5_ib_dev *dev = to_mdev(ibcontext->device);
  1836. unsigned long command;
  1837. phys_addr_t pfn;
  1838. command = get_command(vma->vm_pgoff);
  1839. switch (command) {
  1840. case MLX5_IB_MMAP_WC_PAGE:
  1841. case MLX5_IB_MMAP_NC_PAGE:
  1842. case MLX5_IB_MMAP_REGULAR_PAGE:
  1843. case MLX5_IB_MMAP_ALLOC_WC:
  1844. return uar_mmap(dev, command, vma, context);
  1845. case MLX5_IB_MMAP_GET_CONTIGUOUS_PAGES:
  1846. return -ENOSYS;
  1847. case MLX5_IB_MMAP_CORE_CLOCK:
  1848. if (vma->vm_end - vma->vm_start != PAGE_SIZE)
  1849. return -EINVAL;
  1850. if (vma->vm_flags & VM_WRITE)
  1851. return -EPERM;
  1852. vma->vm_flags &= ~VM_MAYWRITE;
  1853. /* Don't expose to user-space information it shouldn't have */
  1854. if (PAGE_SIZE > 4096)
  1855. return -EOPNOTSUPP;
  1856. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  1857. pfn = (dev->mdev->iseg_base +
  1858. offsetof(struct mlx5_init_seg, internal_timer_h)) >>
  1859. PAGE_SHIFT;
  1860. if (io_remap_pfn_range(vma, vma->vm_start, pfn,
  1861. PAGE_SIZE, vma->vm_page_prot))
  1862. return -EAGAIN;
  1863. break;
  1864. case MLX5_IB_MMAP_CLOCK_INFO:
  1865. return mlx5_ib_mmap_clock_info_page(dev, vma, context);
  1866. case MLX5_IB_MMAP_DEVICE_MEM:
  1867. return dm_mmap(ibcontext, vma);
  1868. default:
  1869. return -EINVAL;
  1870. }
  1871. return 0;
  1872. }
  1873. struct ib_dm *mlx5_ib_alloc_dm(struct ib_device *ibdev,
  1874. struct ib_ucontext *context,
  1875. struct ib_dm_alloc_attr *attr,
  1876. struct uverbs_attr_bundle *attrs)
  1877. {
  1878. u64 act_size = roundup(attr->length, MLX5_MEMIC_BASE_SIZE);
  1879. struct mlx5_memic *memic = &to_mdev(ibdev)->memic;
  1880. phys_addr_t memic_addr;
  1881. struct mlx5_ib_dm *dm;
  1882. u64 start_offset;
  1883. u32 page_idx;
  1884. int err;
  1885. dm = kzalloc(sizeof(*dm), GFP_KERNEL);
  1886. if (!dm)
  1887. return ERR_PTR(-ENOMEM);
  1888. mlx5_ib_dbg(to_mdev(ibdev), "alloc_memic req: user_length=0x%llx act_length=0x%llx log_alignment=%d\n",
  1889. attr->length, act_size, attr->alignment);
  1890. err = mlx5_cmd_alloc_memic(memic, &memic_addr,
  1891. act_size, attr->alignment);
  1892. if (err)
  1893. goto err_free;
  1894. start_offset = memic_addr & ~PAGE_MASK;
  1895. page_idx = (memic_addr - pci_resource_start(memic->dev->pdev, 0) -
  1896. MLX5_CAP64_DEV_MEM(memic->dev, memic_bar_start_addr)) >>
  1897. PAGE_SHIFT;
  1898. err = uverbs_copy_to(attrs,
  1899. MLX5_IB_ATTR_ALLOC_DM_RESP_START_OFFSET,
  1900. &start_offset, sizeof(start_offset));
  1901. if (err)
  1902. goto err_dealloc;
  1903. err = uverbs_copy_to(attrs,
  1904. MLX5_IB_ATTR_ALLOC_DM_RESP_PAGE_INDEX,
  1905. &page_idx, sizeof(page_idx));
  1906. if (err)
  1907. goto err_dealloc;
  1908. bitmap_set(to_mucontext(context)->dm_pages, page_idx,
  1909. DIV_ROUND_UP(act_size, PAGE_SIZE));
  1910. dm->dev_addr = memic_addr;
  1911. return &dm->ibdm;
  1912. err_dealloc:
  1913. mlx5_cmd_dealloc_memic(memic, memic_addr,
  1914. act_size);
  1915. err_free:
  1916. kfree(dm);
  1917. return ERR_PTR(err);
  1918. }
  1919. int mlx5_ib_dealloc_dm(struct ib_dm *ibdm)
  1920. {
  1921. struct mlx5_memic *memic = &to_mdev(ibdm->device)->memic;
  1922. struct mlx5_ib_dm *dm = to_mdm(ibdm);
  1923. u64 act_size = roundup(dm->ibdm.length, MLX5_MEMIC_BASE_SIZE);
  1924. u32 page_idx;
  1925. int ret;
  1926. ret = mlx5_cmd_dealloc_memic(memic, dm->dev_addr, act_size);
  1927. if (ret)
  1928. return ret;
  1929. page_idx = (dm->dev_addr - pci_resource_start(memic->dev->pdev, 0) -
  1930. MLX5_CAP64_DEV_MEM(memic->dev, memic_bar_start_addr)) >>
  1931. PAGE_SHIFT;
  1932. bitmap_clear(to_mucontext(ibdm->uobject->context)->dm_pages,
  1933. page_idx,
  1934. DIV_ROUND_UP(act_size, PAGE_SIZE));
  1935. kfree(dm);
  1936. return 0;
  1937. }
  1938. static struct ib_pd *mlx5_ib_alloc_pd(struct ib_device *ibdev,
  1939. struct ib_ucontext *context,
  1940. struct ib_udata *udata)
  1941. {
  1942. struct mlx5_ib_alloc_pd_resp resp;
  1943. struct mlx5_ib_pd *pd;
  1944. int err;
  1945. pd = kmalloc(sizeof(*pd), GFP_KERNEL);
  1946. if (!pd)
  1947. return ERR_PTR(-ENOMEM);
  1948. err = mlx5_core_alloc_pd(to_mdev(ibdev)->mdev, &pd->pdn);
  1949. if (err) {
  1950. kfree(pd);
  1951. return ERR_PTR(err);
  1952. }
  1953. if (context) {
  1954. resp.pdn = pd->pdn;
  1955. if (ib_copy_to_udata(udata, &resp, sizeof(resp))) {
  1956. mlx5_core_dealloc_pd(to_mdev(ibdev)->mdev, pd->pdn);
  1957. kfree(pd);
  1958. return ERR_PTR(-EFAULT);
  1959. }
  1960. }
  1961. return &pd->ibpd;
  1962. }
  1963. static int mlx5_ib_dealloc_pd(struct ib_pd *pd)
  1964. {
  1965. struct mlx5_ib_dev *mdev = to_mdev(pd->device);
  1966. struct mlx5_ib_pd *mpd = to_mpd(pd);
  1967. mlx5_core_dealloc_pd(mdev->mdev, mpd->pdn);
  1968. kfree(mpd);
  1969. return 0;
  1970. }
  1971. enum {
  1972. MATCH_CRITERIA_ENABLE_OUTER_BIT,
  1973. MATCH_CRITERIA_ENABLE_MISC_BIT,
  1974. MATCH_CRITERIA_ENABLE_INNER_BIT,
  1975. MATCH_CRITERIA_ENABLE_MISC2_BIT
  1976. };
  1977. #define HEADER_IS_ZERO(match_criteria, headers) \
  1978. !(memchr_inv(MLX5_ADDR_OF(fte_match_param, match_criteria, headers), \
  1979. 0, MLX5_FLD_SZ_BYTES(fte_match_param, headers))) \
  1980. static u8 get_match_criteria_enable(u32 *match_criteria)
  1981. {
  1982. u8 match_criteria_enable;
  1983. match_criteria_enable =
  1984. (!HEADER_IS_ZERO(match_criteria, outer_headers)) <<
  1985. MATCH_CRITERIA_ENABLE_OUTER_BIT;
  1986. match_criteria_enable |=
  1987. (!HEADER_IS_ZERO(match_criteria, misc_parameters)) <<
  1988. MATCH_CRITERIA_ENABLE_MISC_BIT;
  1989. match_criteria_enable |=
  1990. (!HEADER_IS_ZERO(match_criteria, inner_headers)) <<
  1991. MATCH_CRITERIA_ENABLE_INNER_BIT;
  1992. match_criteria_enable |=
  1993. (!HEADER_IS_ZERO(match_criteria, misc_parameters_2)) <<
  1994. MATCH_CRITERIA_ENABLE_MISC2_BIT;
  1995. return match_criteria_enable;
  1996. }
  1997. static int set_proto(void *outer_c, void *outer_v, u8 mask, u8 val)
  1998. {
  1999. u8 entry_mask;
  2000. u8 entry_val;
  2001. int err = 0;
  2002. if (!mask)
  2003. goto out;
  2004. entry_mask = MLX5_GET(fte_match_set_lyr_2_4, outer_c,
  2005. ip_protocol);
  2006. entry_val = MLX5_GET(fte_match_set_lyr_2_4, outer_v,
  2007. ip_protocol);
  2008. if (!entry_mask) {
  2009. MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_protocol, mask);
  2010. MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_protocol, val);
  2011. goto out;
  2012. }
  2013. /* Don't override existing ip protocol */
  2014. if (mask != entry_mask || val != entry_val)
  2015. err = -EINVAL;
  2016. out:
  2017. return err;
  2018. }
  2019. static void set_flow_label(void *misc_c, void *misc_v, u32 mask, u32 val,
  2020. bool inner)
  2021. {
  2022. if (inner) {
  2023. MLX5_SET(fte_match_set_misc,
  2024. misc_c, inner_ipv6_flow_label, mask);
  2025. MLX5_SET(fte_match_set_misc,
  2026. misc_v, inner_ipv6_flow_label, val);
  2027. } else {
  2028. MLX5_SET(fte_match_set_misc,
  2029. misc_c, outer_ipv6_flow_label, mask);
  2030. MLX5_SET(fte_match_set_misc,
  2031. misc_v, outer_ipv6_flow_label, val);
  2032. }
  2033. }
  2034. static void set_tos(void *outer_c, void *outer_v, u8 mask, u8 val)
  2035. {
  2036. MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_ecn, mask);
  2037. MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_ecn, val);
  2038. MLX5_SET(fte_match_set_lyr_2_4, outer_c, ip_dscp, mask >> 2);
  2039. MLX5_SET(fte_match_set_lyr_2_4, outer_v, ip_dscp, val >> 2);
  2040. }
  2041. static int check_mpls_supp_fields(u32 field_support, const __be32 *set_mask)
  2042. {
  2043. if (MLX5_GET(fte_match_mpls, set_mask, mpls_label) &&
  2044. !(field_support & MLX5_FIELD_SUPPORT_MPLS_LABEL))
  2045. return -EOPNOTSUPP;
  2046. if (MLX5_GET(fte_match_mpls, set_mask, mpls_exp) &&
  2047. !(field_support & MLX5_FIELD_SUPPORT_MPLS_EXP))
  2048. return -EOPNOTSUPP;
  2049. if (MLX5_GET(fte_match_mpls, set_mask, mpls_s_bos) &&
  2050. !(field_support & MLX5_FIELD_SUPPORT_MPLS_S_BOS))
  2051. return -EOPNOTSUPP;
  2052. if (MLX5_GET(fte_match_mpls, set_mask, mpls_ttl) &&
  2053. !(field_support & MLX5_FIELD_SUPPORT_MPLS_TTL))
  2054. return -EOPNOTSUPP;
  2055. return 0;
  2056. }
  2057. #define LAST_ETH_FIELD vlan_tag
  2058. #define LAST_IB_FIELD sl
  2059. #define LAST_IPV4_FIELD tos
  2060. #define LAST_IPV6_FIELD traffic_class
  2061. #define LAST_TCP_UDP_FIELD src_port
  2062. #define LAST_TUNNEL_FIELD tunnel_id
  2063. #define LAST_FLOW_TAG_FIELD tag_id
  2064. #define LAST_DROP_FIELD size
  2065. #define LAST_COUNTERS_FIELD counters
  2066. /* Field is the last supported field */
  2067. #define FIELDS_NOT_SUPPORTED(filter, field)\
  2068. memchr_inv((void *)&filter.field +\
  2069. sizeof(filter.field), 0,\
  2070. sizeof(filter) -\
  2071. offsetof(typeof(filter), field) -\
  2072. sizeof(filter.field))
  2073. static int parse_flow_flow_action(const union ib_flow_spec *ib_spec,
  2074. const struct ib_flow_attr *flow_attr,
  2075. struct mlx5_flow_act *action)
  2076. {
  2077. struct mlx5_ib_flow_action *maction = to_mflow_act(ib_spec->action.act);
  2078. switch (maction->ib_action.type) {
  2079. case IB_FLOW_ACTION_ESP:
  2080. /* Currently only AES_GCM keymat is supported by the driver */
  2081. action->esp_id = (uintptr_t)maction->esp_aes_gcm.ctx;
  2082. action->action |= flow_attr->flags & IB_FLOW_ATTR_FLAGS_EGRESS ?
  2083. MLX5_FLOW_CONTEXT_ACTION_ENCRYPT :
  2084. MLX5_FLOW_CONTEXT_ACTION_DECRYPT;
  2085. return 0;
  2086. default:
  2087. return -EOPNOTSUPP;
  2088. }
  2089. }
  2090. static int parse_flow_attr(struct mlx5_core_dev *mdev, u32 *match_c,
  2091. u32 *match_v, const union ib_flow_spec *ib_spec,
  2092. const struct ib_flow_attr *flow_attr,
  2093. struct mlx5_flow_act *action, u32 prev_type)
  2094. {
  2095. void *misc_params_c = MLX5_ADDR_OF(fte_match_param, match_c,
  2096. misc_parameters);
  2097. void *misc_params_v = MLX5_ADDR_OF(fte_match_param, match_v,
  2098. misc_parameters);
  2099. void *misc_params2_c = MLX5_ADDR_OF(fte_match_param, match_c,
  2100. misc_parameters_2);
  2101. void *misc_params2_v = MLX5_ADDR_OF(fte_match_param, match_v,
  2102. misc_parameters_2);
  2103. void *headers_c;
  2104. void *headers_v;
  2105. int match_ipv;
  2106. int ret;
  2107. if (ib_spec->type & IB_FLOW_SPEC_INNER) {
  2108. headers_c = MLX5_ADDR_OF(fte_match_param, match_c,
  2109. inner_headers);
  2110. headers_v = MLX5_ADDR_OF(fte_match_param, match_v,
  2111. inner_headers);
  2112. match_ipv = MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
  2113. ft_field_support.inner_ip_version);
  2114. } else {
  2115. headers_c = MLX5_ADDR_OF(fte_match_param, match_c,
  2116. outer_headers);
  2117. headers_v = MLX5_ADDR_OF(fte_match_param, match_v,
  2118. outer_headers);
  2119. match_ipv = MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
  2120. ft_field_support.outer_ip_version);
  2121. }
  2122. switch (ib_spec->type & ~IB_FLOW_SPEC_INNER) {
  2123. case IB_FLOW_SPEC_ETH:
  2124. if (FIELDS_NOT_SUPPORTED(ib_spec->eth.mask, LAST_ETH_FIELD))
  2125. return -EOPNOTSUPP;
  2126. ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
  2127. dmac_47_16),
  2128. ib_spec->eth.mask.dst_mac);
  2129. ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
  2130. dmac_47_16),
  2131. ib_spec->eth.val.dst_mac);
  2132. ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
  2133. smac_47_16),
  2134. ib_spec->eth.mask.src_mac);
  2135. ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
  2136. smac_47_16),
  2137. ib_spec->eth.val.src_mac);
  2138. if (ib_spec->eth.mask.vlan_tag) {
  2139. MLX5_SET(fte_match_set_lyr_2_4, headers_c,
  2140. cvlan_tag, 1);
  2141. MLX5_SET(fte_match_set_lyr_2_4, headers_v,
  2142. cvlan_tag, 1);
  2143. MLX5_SET(fte_match_set_lyr_2_4, headers_c,
  2144. first_vid, ntohs(ib_spec->eth.mask.vlan_tag));
  2145. MLX5_SET(fte_match_set_lyr_2_4, headers_v,
  2146. first_vid, ntohs(ib_spec->eth.val.vlan_tag));
  2147. MLX5_SET(fte_match_set_lyr_2_4, headers_c,
  2148. first_cfi,
  2149. ntohs(ib_spec->eth.mask.vlan_tag) >> 12);
  2150. MLX5_SET(fte_match_set_lyr_2_4, headers_v,
  2151. first_cfi,
  2152. ntohs(ib_spec->eth.val.vlan_tag) >> 12);
  2153. MLX5_SET(fte_match_set_lyr_2_4, headers_c,
  2154. first_prio,
  2155. ntohs(ib_spec->eth.mask.vlan_tag) >> 13);
  2156. MLX5_SET(fte_match_set_lyr_2_4, headers_v,
  2157. first_prio,
  2158. ntohs(ib_spec->eth.val.vlan_tag) >> 13);
  2159. }
  2160. MLX5_SET(fte_match_set_lyr_2_4, headers_c,
  2161. ethertype, ntohs(ib_spec->eth.mask.ether_type));
  2162. MLX5_SET(fte_match_set_lyr_2_4, headers_v,
  2163. ethertype, ntohs(ib_spec->eth.val.ether_type));
  2164. break;
  2165. case IB_FLOW_SPEC_IPV4:
  2166. if (FIELDS_NOT_SUPPORTED(ib_spec->ipv4.mask, LAST_IPV4_FIELD))
  2167. return -EOPNOTSUPP;
  2168. if (match_ipv) {
  2169. MLX5_SET(fte_match_set_lyr_2_4, headers_c,
  2170. ip_version, 0xf);
  2171. MLX5_SET(fte_match_set_lyr_2_4, headers_v,
  2172. ip_version, MLX5_FS_IPV4_VERSION);
  2173. } else {
  2174. MLX5_SET(fte_match_set_lyr_2_4, headers_c,
  2175. ethertype, 0xffff);
  2176. MLX5_SET(fte_match_set_lyr_2_4, headers_v,
  2177. ethertype, ETH_P_IP);
  2178. }
  2179. memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
  2180. src_ipv4_src_ipv6.ipv4_layout.ipv4),
  2181. &ib_spec->ipv4.mask.src_ip,
  2182. sizeof(ib_spec->ipv4.mask.src_ip));
  2183. memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
  2184. src_ipv4_src_ipv6.ipv4_layout.ipv4),
  2185. &ib_spec->ipv4.val.src_ip,
  2186. sizeof(ib_spec->ipv4.val.src_ip));
  2187. memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
  2188. dst_ipv4_dst_ipv6.ipv4_layout.ipv4),
  2189. &ib_spec->ipv4.mask.dst_ip,
  2190. sizeof(ib_spec->ipv4.mask.dst_ip));
  2191. memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
  2192. dst_ipv4_dst_ipv6.ipv4_layout.ipv4),
  2193. &ib_spec->ipv4.val.dst_ip,
  2194. sizeof(ib_spec->ipv4.val.dst_ip));
  2195. set_tos(headers_c, headers_v,
  2196. ib_spec->ipv4.mask.tos, ib_spec->ipv4.val.tos);
  2197. if (set_proto(headers_c, headers_v,
  2198. ib_spec->ipv4.mask.proto,
  2199. ib_spec->ipv4.val.proto))
  2200. return -EINVAL;
  2201. break;
  2202. case IB_FLOW_SPEC_IPV6:
  2203. if (FIELDS_NOT_SUPPORTED(ib_spec->ipv6.mask, LAST_IPV6_FIELD))
  2204. return -EOPNOTSUPP;
  2205. if (match_ipv) {
  2206. MLX5_SET(fte_match_set_lyr_2_4, headers_c,
  2207. ip_version, 0xf);
  2208. MLX5_SET(fte_match_set_lyr_2_4, headers_v,
  2209. ip_version, MLX5_FS_IPV6_VERSION);
  2210. } else {
  2211. MLX5_SET(fte_match_set_lyr_2_4, headers_c,
  2212. ethertype, 0xffff);
  2213. MLX5_SET(fte_match_set_lyr_2_4, headers_v,
  2214. ethertype, ETH_P_IPV6);
  2215. }
  2216. memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
  2217. src_ipv4_src_ipv6.ipv6_layout.ipv6),
  2218. &ib_spec->ipv6.mask.src_ip,
  2219. sizeof(ib_spec->ipv6.mask.src_ip));
  2220. memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
  2221. src_ipv4_src_ipv6.ipv6_layout.ipv6),
  2222. &ib_spec->ipv6.val.src_ip,
  2223. sizeof(ib_spec->ipv6.val.src_ip));
  2224. memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
  2225. dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
  2226. &ib_spec->ipv6.mask.dst_ip,
  2227. sizeof(ib_spec->ipv6.mask.dst_ip));
  2228. memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
  2229. dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
  2230. &ib_spec->ipv6.val.dst_ip,
  2231. sizeof(ib_spec->ipv6.val.dst_ip));
  2232. set_tos(headers_c, headers_v,
  2233. ib_spec->ipv6.mask.traffic_class,
  2234. ib_spec->ipv6.val.traffic_class);
  2235. if (set_proto(headers_c, headers_v,
  2236. ib_spec->ipv6.mask.next_hdr,
  2237. ib_spec->ipv6.val.next_hdr))
  2238. return -EINVAL;
  2239. set_flow_label(misc_params_c, misc_params_v,
  2240. ntohl(ib_spec->ipv6.mask.flow_label),
  2241. ntohl(ib_spec->ipv6.val.flow_label),
  2242. ib_spec->type & IB_FLOW_SPEC_INNER);
  2243. break;
  2244. case IB_FLOW_SPEC_ESP:
  2245. if (ib_spec->esp.mask.seq)
  2246. return -EOPNOTSUPP;
  2247. MLX5_SET(fte_match_set_misc, misc_params_c, outer_esp_spi,
  2248. ntohl(ib_spec->esp.mask.spi));
  2249. MLX5_SET(fte_match_set_misc, misc_params_v, outer_esp_spi,
  2250. ntohl(ib_spec->esp.val.spi));
  2251. break;
  2252. case IB_FLOW_SPEC_TCP:
  2253. if (FIELDS_NOT_SUPPORTED(ib_spec->tcp_udp.mask,
  2254. LAST_TCP_UDP_FIELD))
  2255. return -EOPNOTSUPP;
  2256. if (set_proto(headers_c, headers_v, 0xff, IPPROTO_TCP))
  2257. return -EINVAL;
  2258. MLX5_SET(fte_match_set_lyr_2_4, headers_c, tcp_sport,
  2259. ntohs(ib_spec->tcp_udp.mask.src_port));
  2260. MLX5_SET(fte_match_set_lyr_2_4, headers_v, tcp_sport,
  2261. ntohs(ib_spec->tcp_udp.val.src_port));
  2262. MLX5_SET(fte_match_set_lyr_2_4, headers_c, tcp_dport,
  2263. ntohs(ib_spec->tcp_udp.mask.dst_port));
  2264. MLX5_SET(fte_match_set_lyr_2_4, headers_v, tcp_dport,
  2265. ntohs(ib_spec->tcp_udp.val.dst_port));
  2266. break;
  2267. case IB_FLOW_SPEC_UDP:
  2268. if (FIELDS_NOT_SUPPORTED(ib_spec->tcp_udp.mask,
  2269. LAST_TCP_UDP_FIELD))
  2270. return -EOPNOTSUPP;
  2271. if (set_proto(headers_c, headers_v, 0xff, IPPROTO_UDP))
  2272. return -EINVAL;
  2273. MLX5_SET(fte_match_set_lyr_2_4, headers_c, udp_sport,
  2274. ntohs(ib_spec->tcp_udp.mask.src_port));
  2275. MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_sport,
  2276. ntohs(ib_spec->tcp_udp.val.src_port));
  2277. MLX5_SET(fte_match_set_lyr_2_4, headers_c, udp_dport,
  2278. ntohs(ib_spec->tcp_udp.mask.dst_port));
  2279. MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_dport,
  2280. ntohs(ib_spec->tcp_udp.val.dst_port));
  2281. break;
  2282. case IB_FLOW_SPEC_GRE:
  2283. if (ib_spec->gre.mask.c_ks_res0_ver)
  2284. return -EOPNOTSUPP;
  2285. if (set_proto(headers_c, headers_v, 0xff, IPPROTO_GRE))
  2286. return -EINVAL;
  2287. MLX5_SET(fte_match_set_lyr_2_4, headers_c, ip_protocol,
  2288. 0xff);
  2289. MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol,
  2290. IPPROTO_GRE);
  2291. MLX5_SET(fte_match_set_misc, misc_params_c, gre_protocol,
  2292. ntohs(ib_spec->gre.mask.protocol));
  2293. MLX5_SET(fte_match_set_misc, misc_params_v, gre_protocol,
  2294. ntohs(ib_spec->gre.val.protocol));
  2295. memcpy(MLX5_ADDR_OF(fte_match_set_misc, misc_params_c,
  2296. gre_key_h),
  2297. &ib_spec->gre.mask.key,
  2298. sizeof(ib_spec->gre.mask.key));
  2299. memcpy(MLX5_ADDR_OF(fte_match_set_misc, misc_params_v,
  2300. gre_key_h),
  2301. &ib_spec->gre.val.key,
  2302. sizeof(ib_spec->gre.val.key));
  2303. break;
  2304. case IB_FLOW_SPEC_MPLS:
  2305. switch (prev_type) {
  2306. case IB_FLOW_SPEC_UDP:
  2307. if (check_mpls_supp_fields(MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
  2308. ft_field_support.outer_first_mpls_over_udp),
  2309. &ib_spec->mpls.mask.tag))
  2310. return -EOPNOTSUPP;
  2311. memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_v,
  2312. outer_first_mpls_over_udp),
  2313. &ib_spec->mpls.val.tag,
  2314. sizeof(ib_spec->mpls.val.tag));
  2315. memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_c,
  2316. outer_first_mpls_over_udp),
  2317. &ib_spec->mpls.mask.tag,
  2318. sizeof(ib_spec->mpls.mask.tag));
  2319. break;
  2320. case IB_FLOW_SPEC_GRE:
  2321. if (check_mpls_supp_fields(MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
  2322. ft_field_support.outer_first_mpls_over_gre),
  2323. &ib_spec->mpls.mask.tag))
  2324. return -EOPNOTSUPP;
  2325. memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_v,
  2326. outer_first_mpls_over_gre),
  2327. &ib_spec->mpls.val.tag,
  2328. sizeof(ib_spec->mpls.val.tag));
  2329. memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_c,
  2330. outer_first_mpls_over_gre),
  2331. &ib_spec->mpls.mask.tag,
  2332. sizeof(ib_spec->mpls.mask.tag));
  2333. break;
  2334. default:
  2335. if (ib_spec->type & IB_FLOW_SPEC_INNER) {
  2336. if (check_mpls_supp_fields(MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
  2337. ft_field_support.inner_first_mpls),
  2338. &ib_spec->mpls.mask.tag))
  2339. return -EOPNOTSUPP;
  2340. memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_v,
  2341. inner_first_mpls),
  2342. &ib_spec->mpls.val.tag,
  2343. sizeof(ib_spec->mpls.val.tag));
  2344. memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_c,
  2345. inner_first_mpls),
  2346. &ib_spec->mpls.mask.tag,
  2347. sizeof(ib_spec->mpls.mask.tag));
  2348. } else {
  2349. if (check_mpls_supp_fields(MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
  2350. ft_field_support.outer_first_mpls),
  2351. &ib_spec->mpls.mask.tag))
  2352. return -EOPNOTSUPP;
  2353. memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_v,
  2354. outer_first_mpls),
  2355. &ib_spec->mpls.val.tag,
  2356. sizeof(ib_spec->mpls.val.tag));
  2357. memcpy(MLX5_ADDR_OF(fte_match_set_misc2, misc_params2_c,
  2358. outer_first_mpls),
  2359. &ib_spec->mpls.mask.tag,
  2360. sizeof(ib_spec->mpls.mask.tag));
  2361. }
  2362. }
  2363. break;
  2364. case IB_FLOW_SPEC_VXLAN_TUNNEL:
  2365. if (FIELDS_NOT_SUPPORTED(ib_spec->tunnel.mask,
  2366. LAST_TUNNEL_FIELD))
  2367. return -EOPNOTSUPP;
  2368. MLX5_SET(fte_match_set_misc, misc_params_c, vxlan_vni,
  2369. ntohl(ib_spec->tunnel.mask.tunnel_id));
  2370. MLX5_SET(fte_match_set_misc, misc_params_v, vxlan_vni,
  2371. ntohl(ib_spec->tunnel.val.tunnel_id));
  2372. break;
  2373. case IB_FLOW_SPEC_ACTION_TAG:
  2374. if (FIELDS_NOT_SUPPORTED(ib_spec->flow_tag,
  2375. LAST_FLOW_TAG_FIELD))
  2376. return -EOPNOTSUPP;
  2377. if (ib_spec->flow_tag.tag_id >= BIT(24))
  2378. return -EINVAL;
  2379. action->flow_tag = ib_spec->flow_tag.tag_id;
  2380. action->has_flow_tag = true;
  2381. break;
  2382. case IB_FLOW_SPEC_ACTION_DROP:
  2383. if (FIELDS_NOT_SUPPORTED(ib_spec->drop,
  2384. LAST_DROP_FIELD))
  2385. return -EOPNOTSUPP;
  2386. action->action |= MLX5_FLOW_CONTEXT_ACTION_DROP;
  2387. break;
  2388. case IB_FLOW_SPEC_ACTION_HANDLE:
  2389. ret = parse_flow_flow_action(ib_spec, flow_attr, action);
  2390. if (ret)
  2391. return ret;
  2392. break;
  2393. case IB_FLOW_SPEC_ACTION_COUNT:
  2394. if (FIELDS_NOT_SUPPORTED(ib_spec->flow_count,
  2395. LAST_COUNTERS_FIELD))
  2396. return -EOPNOTSUPP;
  2397. /* for now support only one counters spec per flow */
  2398. if (action->action & MLX5_FLOW_CONTEXT_ACTION_COUNT)
  2399. return -EINVAL;
  2400. action->counters = ib_spec->flow_count.counters;
  2401. action->action |= MLX5_FLOW_CONTEXT_ACTION_COUNT;
  2402. break;
  2403. default:
  2404. return -EINVAL;
  2405. }
  2406. return 0;
  2407. }
  2408. /* If a flow could catch both multicast and unicast packets,
  2409. * it won't fall into the multicast flow steering table and this rule
  2410. * could steal other multicast packets.
  2411. */
  2412. static bool flow_is_multicast_only(const struct ib_flow_attr *ib_attr)
  2413. {
  2414. union ib_flow_spec *flow_spec;
  2415. if (ib_attr->type != IB_FLOW_ATTR_NORMAL ||
  2416. ib_attr->num_of_specs < 1)
  2417. return false;
  2418. flow_spec = (union ib_flow_spec *)(ib_attr + 1);
  2419. if (flow_spec->type == IB_FLOW_SPEC_IPV4) {
  2420. struct ib_flow_spec_ipv4 *ipv4_spec;
  2421. ipv4_spec = (struct ib_flow_spec_ipv4 *)flow_spec;
  2422. if (ipv4_is_multicast(ipv4_spec->val.dst_ip))
  2423. return true;
  2424. return false;
  2425. }
  2426. if (flow_spec->type == IB_FLOW_SPEC_ETH) {
  2427. struct ib_flow_spec_eth *eth_spec;
  2428. eth_spec = (struct ib_flow_spec_eth *)flow_spec;
  2429. return is_multicast_ether_addr(eth_spec->mask.dst_mac) &&
  2430. is_multicast_ether_addr(eth_spec->val.dst_mac);
  2431. }
  2432. return false;
  2433. }
  2434. enum valid_spec {
  2435. VALID_SPEC_INVALID,
  2436. VALID_SPEC_VALID,
  2437. VALID_SPEC_NA,
  2438. };
  2439. static enum valid_spec
  2440. is_valid_esp_aes_gcm(struct mlx5_core_dev *mdev,
  2441. const struct mlx5_flow_spec *spec,
  2442. const struct mlx5_flow_act *flow_act,
  2443. bool egress)
  2444. {
  2445. const u32 *match_c = spec->match_criteria;
  2446. bool is_crypto =
  2447. (flow_act->action & (MLX5_FLOW_CONTEXT_ACTION_ENCRYPT |
  2448. MLX5_FLOW_CONTEXT_ACTION_DECRYPT));
  2449. bool is_ipsec = mlx5_fs_is_ipsec_flow(match_c);
  2450. bool is_drop = flow_act->action & MLX5_FLOW_CONTEXT_ACTION_DROP;
  2451. /*
  2452. * Currently only crypto is supported in egress, when regular egress
  2453. * rules would be supported, always return VALID_SPEC_NA.
  2454. */
  2455. if (!is_crypto)
  2456. return egress ? VALID_SPEC_INVALID : VALID_SPEC_NA;
  2457. return is_crypto && is_ipsec &&
  2458. (!egress || (!is_drop && !flow_act->has_flow_tag)) ?
  2459. VALID_SPEC_VALID : VALID_SPEC_INVALID;
  2460. }
  2461. static bool is_valid_spec(struct mlx5_core_dev *mdev,
  2462. const struct mlx5_flow_spec *spec,
  2463. const struct mlx5_flow_act *flow_act,
  2464. bool egress)
  2465. {
  2466. /* We curretly only support ipsec egress flow */
  2467. return is_valid_esp_aes_gcm(mdev, spec, flow_act, egress) != VALID_SPEC_INVALID;
  2468. }
  2469. static bool is_valid_ethertype(struct mlx5_core_dev *mdev,
  2470. const struct ib_flow_attr *flow_attr,
  2471. bool check_inner)
  2472. {
  2473. union ib_flow_spec *ib_spec = (union ib_flow_spec *)(flow_attr + 1);
  2474. int match_ipv = check_inner ?
  2475. MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
  2476. ft_field_support.inner_ip_version) :
  2477. MLX5_CAP_FLOWTABLE_NIC_RX(mdev,
  2478. ft_field_support.outer_ip_version);
  2479. int inner_bit = check_inner ? IB_FLOW_SPEC_INNER : 0;
  2480. bool ipv4_spec_valid, ipv6_spec_valid;
  2481. unsigned int ip_spec_type = 0;
  2482. bool has_ethertype = false;
  2483. unsigned int spec_index;
  2484. bool mask_valid = true;
  2485. u16 eth_type = 0;
  2486. bool type_valid;
  2487. /* Validate that ethertype is correct */
  2488. for (spec_index = 0; spec_index < flow_attr->num_of_specs; spec_index++) {
  2489. if ((ib_spec->type == (IB_FLOW_SPEC_ETH | inner_bit)) &&
  2490. ib_spec->eth.mask.ether_type) {
  2491. mask_valid = (ib_spec->eth.mask.ether_type ==
  2492. htons(0xffff));
  2493. has_ethertype = true;
  2494. eth_type = ntohs(ib_spec->eth.val.ether_type);
  2495. } else if ((ib_spec->type == (IB_FLOW_SPEC_IPV4 | inner_bit)) ||
  2496. (ib_spec->type == (IB_FLOW_SPEC_IPV6 | inner_bit))) {
  2497. ip_spec_type = ib_spec->type;
  2498. }
  2499. ib_spec = (void *)ib_spec + ib_spec->size;
  2500. }
  2501. type_valid = (!has_ethertype) || (!ip_spec_type);
  2502. if (!type_valid && mask_valid) {
  2503. ipv4_spec_valid = (eth_type == ETH_P_IP) &&
  2504. (ip_spec_type == (IB_FLOW_SPEC_IPV4 | inner_bit));
  2505. ipv6_spec_valid = (eth_type == ETH_P_IPV6) &&
  2506. (ip_spec_type == (IB_FLOW_SPEC_IPV6 | inner_bit));
  2507. type_valid = (ipv4_spec_valid) || (ipv6_spec_valid) ||
  2508. (((eth_type == ETH_P_MPLS_UC) ||
  2509. (eth_type == ETH_P_MPLS_MC)) && match_ipv);
  2510. }
  2511. return type_valid;
  2512. }
  2513. static bool is_valid_attr(struct mlx5_core_dev *mdev,
  2514. const struct ib_flow_attr *flow_attr)
  2515. {
  2516. return is_valid_ethertype(mdev, flow_attr, false) &&
  2517. is_valid_ethertype(mdev, flow_attr, true);
  2518. }
  2519. static void put_flow_table(struct mlx5_ib_dev *dev,
  2520. struct mlx5_ib_flow_prio *prio, bool ft_added)
  2521. {
  2522. prio->refcount -= !!ft_added;
  2523. if (!prio->refcount) {
  2524. mlx5_destroy_flow_table(prio->flow_table);
  2525. prio->flow_table = NULL;
  2526. }
  2527. }
  2528. static void counters_clear_description(struct ib_counters *counters)
  2529. {
  2530. struct mlx5_ib_mcounters *mcounters = to_mcounters(counters);
  2531. mutex_lock(&mcounters->mcntrs_mutex);
  2532. kfree(mcounters->counters_data);
  2533. mcounters->counters_data = NULL;
  2534. mcounters->cntrs_max_index = 0;
  2535. mutex_unlock(&mcounters->mcntrs_mutex);
  2536. }
  2537. static int mlx5_ib_destroy_flow(struct ib_flow *flow_id)
  2538. {
  2539. struct mlx5_ib_flow_handler *handler = container_of(flow_id,
  2540. struct mlx5_ib_flow_handler,
  2541. ibflow);
  2542. struct mlx5_ib_flow_handler *iter, *tmp;
  2543. struct mlx5_ib_dev *dev = handler->dev;
  2544. mutex_lock(&dev->flow_db->lock);
  2545. list_for_each_entry_safe(iter, tmp, &handler->list, list) {
  2546. mlx5_del_flow_rules(iter->rule);
  2547. put_flow_table(dev, iter->prio, true);
  2548. list_del(&iter->list);
  2549. kfree(iter);
  2550. }
  2551. mlx5_del_flow_rules(handler->rule);
  2552. put_flow_table(dev, handler->prio, true);
  2553. if (handler->ibcounters &&
  2554. atomic_read(&handler->ibcounters->usecnt) == 1)
  2555. counters_clear_description(handler->ibcounters);
  2556. mutex_unlock(&dev->flow_db->lock);
  2557. if (handler->flow_matcher)
  2558. atomic_dec(&handler->flow_matcher->usecnt);
  2559. kfree(handler);
  2560. return 0;
  2561. }
  2562. static int ib_prio_to_core_prio(unsigned int priority, bool dont_trap)
  2563. {
  2564. priority *= 2;
  2565. if (!dont_trap)
  2566. priority++;
  2567. return priority;
  2568. }
  2569. enum flow_table_type {
  2570. MLX5_IB_FT_RX,
  2571. MLX5_IB_FT_TX
  2572. };
  2573. #define MLX5_FS_MAX_TYPES 6
  2574. #define MLX5_FS_MAX_ENTRIES BIT(16)
  2575. static struct mlx5_ib_flow_prio *_get_prio(struct mlx5_flow_namespace *ns,
  2576. struct mlx5_ib_flow_prio *prio,
  2577. int priority,
  2578. int num_entries, int num_groups)
  2579. {
  2580. struct mlx5_flow_table *ft;
  2581. ft = mlx5_create_auto_grouped_flow_table(ns, priority,
  2582. num_entries,
  2583. num_groups,
  2584. 0, 0);
  2585. if (IS_ERR(ft))
  2586. return ERR_CAST(ft);
  2587. prio->flow_table = ft;
  2588. prio->refcount = 0;
  2589. return prio;
  2590. }
  2591. static struct mlx5_ib_flow_prio *get_flow_table(struct mlx5_ib_dev *dev,
  2592. struct ib_flow_attr *flow_attr,
  2593. enum flow_table_type ft_type)
  2594. {
  2595. bool dont_trap = flow_attr->flags & IB_FLOW_ATTR_FLAGS_DONT_TRAP;
  2596. struct mlx5_flow_namespace *ns = NULL;
  2597. struct mlx5_ib_flow_prio *prio;
  2598. struct mlx5_flow_table *ft;
  2599. int max_table_size;
  2600. int num_entries;
  2601. int num_groups;
  2602. int priority;
  2603. max_table_size = BIT(MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev,
  2604. log_max_ft_size));
  2605. if (flow_attr->type == IB_FLOW_ATTR_NORMAL) {
  2606. if (ft_type == MLX5_IB_FT_TX)
  2607. priority = 0;
  2608. else if (flow_is_multicast_only(flow_attr) &&
  2609. !dont_trap)
  2610. priority = MLX5_IB_FLOW_MCAST_PRIO;
  2611. else
  2612. priority = ib_prio_to_core_prio(flow_attr->priority,
  2613. dont_trap);
  2614. ns = mlx5_get_flow_namespace(dev->mdev,
  2615. ft_type == MLX5_IB_FT_TX ?
  2616. MLX5_FLOW_NAMESPACE_EGRESS :
  2617. MLX5_FLOW_NAMESPACE_BYPASS);
  2618. num_entries = MLX5_FS_MAX_ENTRIES;
  2619. num_groups = MLX5_FS_MAX_TYPES;
  2620. prio = &dev->flow_db->prios[priority];
  2621. } else if (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
  2622. flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT) {
  2623. ns = mlx5_get_flow_namespace(dev->mdev,
  2624. MLX5_FLOW_NAMESPACE_LEFTOVERS);
  2625. build_leftovers_ft_param(&priority,
  2626. &num_entries,
  2627. &num_groups);
  2628. prio = &dev->flow_db->prios[MLX5_IB_FLOW_LEFTOVERS_PRIO];
  2629. } else if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) {
  2630. if (!MLX5_CAP_FLOWTABLE(dev->mdev,
  2631. allow_sniffer_and_nic_rx_shared_tir))
  2632. return ERR_PTR(-ENOTSUPP);
  2633. ns = mlx5_get_flow_namespace(dev->mdev, ft_type == MLX5_IB_FT_RX ?
  2634. MLX5_FLOW_NAMESPACE_SNIFFER_RX :
  2635. MLX5_FLOW_NAMESPACE_SNIFFER_TX);
  2636. prio = &dev->flow_db->sniffer[ft_type];
  2637. priority = 0;
  2638. num_entries = 1;
  2639. num_groups = 1;
  2640. }
  2641. if (!ns)
  2642. return ERR_PTR(-ENOTSUPP);
  2643. if (num_entries > max_table_size)
  2644. return ERR_PTR(-ENOMEM);
  2645. ft = prio->flow_table;
  2646. if (!ft)
  2647. return _get_prio(ns, prio, priority, num_entries, num_groups);
  2648. return prio;
  2649. }
  2650. static void set_underlay_qp(struct mlx5_ib_dev *dev,
  2651. struct mlx5_flow_spec *spec,
  2652. u32 underlay_qpn)
  2653. {
  2654. void *misc_params_c = MLX5_ADDR_OF(fte_match_param,
  2655. spec->match_criteria,
  2656. misc_parameters);
  2657. void *misc_params_v = MLX5_ADDR_OF(fte_match_param, spec->match_value,
  2658. misc_parameters);
  2659. if (underlay_qpn &&
  2660. MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev,
  2661. ft_field_support.bth_dst_qp)) {
  2662. MLX5_SET(fte_match_set_misc,
  2663. misc_params_v, bth_dst_qp, underlay_qpn);
  2664. MLX5_SET(fte_match_set_misc,
  2665. misc_params_c, bth_dst_qp, 0xffffff);
  2666. }
  2667. }
  2668. static int read_flow_counters(struct ib_device *ibdev,
  2669. struct mlx5_read_counters_attr *read_attr)
  2670. {
  2671. struct mlx5_fc *fc = read_attr->hw_cntrs_hndl;
  2672. struct mlx5_ib_dev *dev = to_mdev(ibdev);
  2673. return mlx5_fc_query(dev->mdev, fc,
  2674. &read_attr->out[IB_COUNTER_PACKETS],
  2675. &read_attr->out[IB_COUNTER_BYTES]);
  2676. }
  2677. /* flow counters currently expose two counters packets and bytes */
  2678. #define FLOW_COUNTERS_NUM 2
  2679. static int counters_set_description(struct ib_counters *counters,
  2680. enum mlx5_ib_counters_type counters_type,
  2681. struct mlx5_ib_flow_counters_desc *desc_data,
  2682. u32 ncounters)
  2683. {
  2684. struct mlx5_ib_mcounters *mcounters = to_mcounters(counters);
  2685. u32 cntrs_max_index = 0;
  2686. int i;
  2687. if (counters_type != MLX5_IB_COUNTERS_FLOW)
  2688. return -EINVAL;
  2689. /* init the fields for the object */
  2690. mcounters->type = counters_type;
  2691. mcounters->read_counters = read_flow_counters;
  2692. mcounters->counters_num = FLOW_COUNTERS_NUM;
  2693. mcounters->ncounters = ncounters;
  2694. /* each counter entry have both description and index pair */
  2695. for (i = 0; i < ncounters; i++) {
  2696. if (desc_data[i].description > IB_COUNTER_BYTES)
  2697. return -EINVAL;
  2698. if (cntrs_max_index <= desc_data[i].index)
  2699. cntrs_max_index = desc_data[i].index + 1;
  2700. }
  2701. mutex_lock(&mcounters->mcntrs_mutex);
  2702. mcounters->counters_data = desc_data;
  2703. mcounters->cntrs_max_index = cntrs_max_index;
  2704. mutex_unlock(&mcounters->mcntrs_mutex);
  2705. return 0;
  2706. }
  2707. #define MAX_COUNTERS_NUM (USHRT_MAX / (sizeof(u32) * 2))
  2708. static int flow_counters_set_data(struct ib_counters *ibcounters,
  2709. struct mlx5_ib_create_flow *ucmd)
  2710. {
  2711. struct mlx5_ib_mcounters *mcounters = to_mcounters(ibcounters);
  2712. struct mlx5_ib_flow_counters_data *cntrs_data = NULL;
  2713. struct mlx5_ib_flow_counters_desc *desc_data = NULL;
  2714. bool hw_hndl = false;
  2715. int ret = 0;
  2716. if (ucmd && ucmd->ncounters_data != 0) {
  2717. cntrs_data = ucmd->data;
  2718. if (cntrs_data->ncounters > MAX_COUNTERS_NUM)
  2719. return -EINVAL;
  2720. desc_data = kcalloc(cntrs_data->ncounters,
  2721. sizeof(*desc_data),
  2722. GFP_KERNEL);
  2723. if (!desc_data)
  2724. return -ENOMEM;
  2725. if (copy_from_user(desc_data,
  2726. u64_to_user_ptr(cntrs_data->counters_data),
  2727. sizeof(*desc_data) * cntrs_data->ncounters)) {
  2728. ret = -EFAULT;
  2729. goto free;
  2730. }
  2731. }
  2732. if (!mcounters->hw_cntrs_hndl) {
  2733. mcounters->hw_cntrs_hndl = mlx5_fc_create(
  2734. to_mdev(ibcounters->device)->mdev, false);
  2735. if (IS_ERR(mcounters->hw_cntrs_hndl)) {
  2736. ret = PTR_ERR(mcounters->hw_cntrs_hndl);
  2737. goto free;
  2738. }
  2739. hw_hndl = true;
  2740. }
  2741. if (desc_data) {
  2742. /* counters already bound to at least one flow */
  2743. if (mcounters->cntrs_max_index) {
  2744. ret = -EINVAL;
  2745. goto free_hndl;
  2746. }
  2747. ret = counters_set_description(ibcounters,
  2748. MLX5_IB_COUNTERS_FLOW,
  2749. desc_data,
  2750. cntrs_data->ncounters);
  2751. if (ret)
  2752. goto free_hndl;
  2753. } else if (!mcounters->cntrs_max_index) {
  2754. /* counters not bound yet, must have udata passed */
  2755. ret = -EINVAL;
  2756. goto free_hndl;
  2757. }
  2758. return 0;
  2759. free_hndl:
  2760. if (hw_hndl) {
  2761. mlx5_fc_destroy(to_mdev(ibcounters->device)->mdev,
  2762. mcounters->hw_cntrs_hndl);
  2763. mcounters->hw_cntrs_hndl = NULL;
  2764. }
  2765. free:
  2766. kfree(desc_data);
  2767. return ret;
  2768. }
  2769. static struct mlx5_ib_flow_handler *_create_flow_rule(struct mlx5_ib_dev *dev,
  2770. struct mlx5_ib_flow_prio *ft_prio,
  2771. const struct ib_flow_attr *flow_attr,
  2772. struct mlx5_flow_destination *dst,
  2773. u32 underlay_qpn,
  2774. struct mlx5_ib_create_flow *ucmd)
  2775. {
  2776. struct mlx5_flow_table *ft = ft_prio->flow_table;
  2777. struct mlx5_ib_flow_handler *handler;
  2778. struct mlx5_flow_act flow_act = {.flow_tag = MLX5_FS_DEFAULT_FLOW_TAG};
  2779. struct mlx5_flow_spec *spec;
  2780. struct mlx5_flow_destination dest_arr[2] = {};
  2781. struct mlx5_flow_destination *rule_dst = dest_arr;
  2782. const void *ib_flow = (const void *)flow_attr + sizeof(*flow_attr);
  2783. unsigned int spec_index;
  2784. u32 prev_type = 0;
  2785. int err = 0;
  2786. int dest_num = 0;
  2787. bool is_egress = flow_attr->flags & IB_FLOW_ATTR_FLAGS_EGRESS;
  2788. if (!is_valid_attr(dev->mdev, flow_attr))
  2789. return ERR_PTR(-EINVAL);
  2790. spec = kvzalloc(sizeof(*spec), GFP_KERNEL);
  2791. handler = kzalloc(sizeof(*handler), GFP_KERNEL);
  2792. if (!handler || !spec) {
  2793. err = -ENOMEM;
  2794. goto free;
  2795. }
  2796. INIT_LIST_HEAD(&handler->list);
  2797. for (spec_index = 0; spec_index < flow_attr->num_of_specs; spec_index++) {
  2798. err = parse_flow_attr(dev->mdev, spec->match_criteria,
  2799. spec->match_value,
  2800. ib_flow, flow_attr, &flow_act,
  2801. prev_type);
  2802. if (err < 0)
  2803. goto free;
  2804. prev_type = ((union ib_flow_spec *)ib_flow)->type;
  2805. ib_flow += ((union ib_flow_spec *)ib_flow)->size;
  2806. }
  2807. if (dst && !(flow_act.action & MLX5_FLOW_CONTEXT_ACTION_DROP)) {
  2808. memcpy(&dest_arr[0], dst, sizeof(*dst));
  2809. dest_num++;
  2810. }
  2811. if (!flow_is_multicast_only(flow_attr))
  2812. set_underlay_qp(dev, spec, underlay_qpn);
  2813. if (dev->rep) {
  2814. void *misc;
  2815. misc = MLX5_ADDR_OF(fte_match_param, spec->match_value,
  2816. misc_parameters);
  2817. MLX5_SET(fte_match_set_misc, misc, source_port,
  2818. dev->rep->vport);
  2819. misc = MLX5_ADDR_OF(fte_match_param, spec->match_criteria,
  2820. misc_parameters);
  2821. MLX5_SET_TO_ONES(fte_match_set_misc, misc, source_port);
  2822. }
  2823. spec->match_criteria_enable = get_match_criteria_enable(spec->match_criteria);
  2824. if (is_egress &&
  2825. !is_valid_spec(dev->mdev, spec, &flow_act, is_egress)) {
  2826. err = -EINVAL;
  2827. goto free;
  2828. }
  2829. if (flow_act.action & MLX5_FLOW_CONTEXT_ACTION_COUNT) {
  2830. err = flow_counters_set_data(flow_act.counters, ucmd);
  2831. if (err)
  2832. goto free;
  2833. handler->ibcounters = flow_act.counters;
  2834. dest_arr[dest_num].type =
  2835. MLX5_FLOW_DESTINATION_TYPE_COUNTER;
  2836. dest_arr[dest_num].counter =
  2837. to_mcounters(flow_act.counters)->hw_cntrs_hndl;
  2838. dest_num++;
  2839. }
  2840. if (flow_act.action & MLX5_FLOW_CONTEXT_ACTION_DROP) {
  2841. if (!dest_num)
  2842. rule_dst = NULL;
  2843. } else {
  2844. if (is_egress)
  2845. flow_act.action |= MLX5_FLOW_CONTEXT_ACTION_ALLOW;
  2846. else
  2847. flow_act.action |=
  2848. dest_num ? MLX5_FLOW_CONTEXT_ACTION_FWD_DEST :
  2849. MLX5_FLOW_CONTEXT_ACTION_FWD_NEXT_PRIO;
  2850. }
  2851. if (flow_act.has_flow_tag &&
  2852. (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
  2853. flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT)) {
  2854. mlx5_ib_warn(dev, "Flow tag %u and attribute type %x isn't allowed in leftovers\n",
  2855. flow_act.flow_tag, flow_attr->type);
  2856. err = -EINVAL;
  2857. goto free;
  2858. }
  2859. handler->rule = mlx5_add_flow_rules(ft, spec,
  2860. &flow_act,
  2861. rule_dst, dest_num);
  2862. if (IS_ERR(handler->rule)) {
  2863. err = PTR_ERR(handler->rule);
  2864. goto free;
  2865. }
  2866. ft_prio->refcount++;
  2867. handler->prio = ft_prio;
  2868. handler->dev = dev;
  2869. ft_prio->flow_table = ft;
  2870. free:
  2871. if (err && handler) {
  2872. if (handler->ibcounters &&
  2873. atomic_read(&handler->ibcounters->usecnt) == 1)
  2874. counters_clear_description(handler->ibcounters);
  2875. kfree(handler);
  2876. }
  2877. kvfree(spec);
  2878. return err ? ERR_PTR(err) : handler;
  2879. }
  2880. static struct mlx5_ib_flow_handler *create_flow_rule(struct mlx5_ib_dev *dev,
  2881. struct mlx5_ib_flow_prio *ft_prio,
  2882. const struct ib_flow_attr *flow_attr,
  2883. struct mlx5_flow_destination *dst)
  2884. {
  2885. return _create_flow_rule(dev, ft_prio, flow_attr, dst, 0, NULL);
  2886. }
  2887. static struct mlx5_ib_flow_handler *create_dont_trap_rule(struct mlx5_ib_dev *dev,
  2888. struct mlx5_ib_flow_prio *ft_prio,
  2889. struct ib_flow_attr *flow_attr,
  2890. struct mlx5_flow_destination *dst)
  2891. {
  2892. struct mlx5_ib_flow_handler *handler_dst = NULL;
  2893. struct mlx5_ib_flow_handler *handler = NULL;
  2894. handler = create_flow_rule(dev, ft_prio, flow_attr, NULL);
  2895. if (!IS_ERR(handler)) {
  2896. handler_dst = create_flow_rule(dev, ft_prio,
  2897. flow_attr, dst);
  2898. if (IS_ERR(handler_dst)) {
  2899. mlx5_del_flow_rules(handler->rule);
  2900. ft_prio->refcount--;
  2901. kfree(handler);
  2902. handler = handler_dst;
  2903. } else {
  2904. list_add(&handler_dst->list, &handler->list);
  2905. }
  2906. }
  2907. return handler;
  2908. }
  2909. enum {
  2910. LEFTOVERS_MC,
  2911. LEFTOVERS_UC,
  2912. };
  2913. static struct mlx5_ib_flow_handler *create_leftovers_rule(struct mlx5_ib_dev *dev,
  2914. struct mlx5_ib_flow_prio *ft_prio,
  2915. struct ib_flow_attr *flow_attr,
  2916. struct mlx5_flow_destination *dst)
  2917. {
  2918. struct mlx5_ib_flow_handler *handler_ucast = NULL;
  2919. struct mlx5_ib_flow_handler *handler = NULL;
  2920. static struct {
  2921. struct ib_flow_attr flow_attr;
  2922. struct ib_flow_spec_eth eth_flow;
  2923. } leftovers_specs[] = {
  2924. [LEFTOVERS_MC] = {
  2925. .flow_attr = {
  2926. .num_of_specs = 1,
  2927. .size = sizeof(leftovers_specs[0])
  2928. },
  2929. .eth_flow = {
  2930. .type = IB_FLOW_SPEC_ETH,
  2931. .size = sizeof(struct ib_flow_spec_eth),
  2932. .mask = {.dst_mac = {0x1} },
  2933. .val = {.dst_mac = {0x1} }
  2934. }
  2935. },
  2936. [LEFTOVERS_UC] = {
  2937. .flow_attr = {
  2938. .num_of_specs = 1,
  2939. .size = sizeof(leftovers_specs[0])
  2940. },
  2941. .eth_flow = {
  2942. .type = IB_FLOW_SPEC_ETH,
  2943. .size = sizeof(struct ib_flow_spec_eth),
  2944. .mask = {.dst_mac = {0x1} },
  2945. .val = {.dst_mac = {} }
  2946. }
  2947. }
  2948. };
  2949. handler = create_flow_rule(dev, ft_prio,
  2950. &leftovers_specs[LEFTOVERS_MC].flow_attr,
  2951. dst);
  2952. if (!IS_ERR(handler) &&
  2953. flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT) {
  2954. handler_ucast = create_flow_rule(dev, ft_prio,
  2955. &leftovers_specs[LEFTOVERS_UC].flow_attr,
  2956. dst);
  2957. if (IS_ERR(handler_ucast)) {
  2958. mlx5_del_flow_rules(handler->rule);
  2959. ft_prio->refcount--;
  2960. kfree(handler);
  2961. handler = handler_ucast;
  2962. } else {
  2963. list_add(&handler_ucast->list, &handler->list);
  2964. }
  2965. }
  2966. return handler;
  2967. }
  2968. static struct mlx5_ib_flow_handler *create_sniffer_rule(struct mlx5_ib_dev *dev,
  2969. struct mlx5_ib_flow_prio *ft_rx,
  2970. struct mlx5_ib_flow_prio *ft_tx,
  2971. struct mlx5_flow_destination *dst)
  2972. {
  2973. struct mlx5_ib_flow_handler *handler_rx;
  2974. struct mlx5_ib_flow_handler *handler_tx;
  2975. int err;
  2976. static const struct ib_flow_attr flow_attr = {
  2977. .num_of_specs = 0,
  2978. .size = sizeof(flow_attr)
  2979. };
  2980. handler_rx = create_flow_rule(dev, ft_rx, &flow_attr, dst);
  2981. if (IS_ERR(handler_rx)) {
  2982. err = PTR_ERR(handler_rx);
  2983. goto err;
  2984. }
  2985. handler_tx = create_flow_rule(dev, ft_tx, &flow_attr, dst);
  2986. if (IS_ERR(handler_tx)) {
  2987. err = PTR_ERR(handler_tx);
  2988. goto err_tx;
  2989. }
  2990. list_add(&handler_tx->list, &handler_rx->list);
  2991. return handler_rx;
  2992. err_tx:
  2993. mlx5_del_flow_rules(handler_rx->rule);
  2994. ft_rx->refcount--;
  2995. kfree(handler_rx);
  2996. err:
  2997. return ERR_PTR(err);
  2998. }
  2999. static struct ib_flow *mlx5_ib_create_flow(struct ib_qp *qp,
  3000. struct ib_flow_attr *flow_attr,
  3001. int domain,
  3002. struct ib_udata *udata)
  3003. {
  3004. struct mlx5_ib_dev *dev = to_mdev(qp->device);
  3005. struct mlx5_ib_qp *mqp = to_mqp(qp);
  3006. struct mlx5_ib_flow_handler *handler = NULL;
  3007. struct mlx5_flow_destination *dst = NULL;
  3008. struct mlx5_ib_flow_prio *ft_prio_tx = NULL;
  3009. struct mlx5_ib_flow_prio *ft_prio;
  3010. bool is_egress = flow_attr->flags & IB_FLOW_ATTR_FLAGS_EGRESS;
  3011. struct mlx5_ib_create_flow *ucmd = NULL, ucmd_hdr;
  3012. size_t min_ucmd_sz, required_ucmd_sz;
  3013. int err;
  3014. int underlay_qpn;
  3015. if (udata && udata->inlen) {
  3016. min_ucmd_sz = offsetof(typeof(ucmd_hdr), reserved) +
  3017. sizeof(ucmd_hdr.reserved);
  3018. if (udata->inlen < min_ucmd_sz)
  3019. return ERR_PTR(-EOPNOTSUPP);
  3020. err = ib_copy_from_udata(&ucmd_hdr, udata, min_ucmd_sz);
  3021. if (err)
  3022. return ERR_PTR(err);
  3023. /* currently supports only one counters data */
  3024. if (ucmd_hdr.ncounters_data > 1)
  3025. return ERR_PTR(-EINVAL);
  3026. required_ucmd_sz = min_ucmd_sz +
  3027. sizeof(struct mlx5_ib_flow_counters_data) *
  3028. ucmd_hdr.ncounters_data;
  3029. if (udata->inlen > required_ucmd_sz &&
  3030. !ib_is_udata_cleared(udata, required_ucmd_sz,
  3031. udata->inlen - required_ucmd_sz))
  3032. return ERR_PTR(-EOPNOTSUPP);
  3033. ucmd = kzalloc(required_ucmd_sz, GFP_KERNEL);
  3034. if (!ucmd)
  3035. return ERR_PTR(-ENOMEM);
  3036. err = ib_copy_from_udata(ucmd, udata, required_ucmd_sz);
  3037. if (err)
  3038. goto free_ucmd;
  3039. }
  3040. if (flow_attr->priority > MLX5_IB_FLOW_LAST_PRIO) {
  3041. err = -ENOMEM;
  3042. goto free_ucmd;
  3043. }
  3044. if (domain != IB_FLOW_DOMAIN_USER ||
  3045. flow_attr->port > dev->num_ports ||
  3046. (flow_attr->flags & ~(IB_FLOW_ATTR_FLAGS_DONT_TRAP |
  3047. IB_FLOW_ATTR_FLAGS_EGRESS))) {
  3048. err = -EINVAL;
  3049. goto free_ucmd;
  3050. }
  3051. if (is_egress &&
  3052. (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
  3053. flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT)) {
  3054. err = -EINVAL;
  3055. goto free_ucmd;
  3056. }
  3057. dst = kzalloc(sizeof(*dst), GFP_KERNEL);
  3058. if (!dst) {
  3059. err = -ENOMEM;
  3060. goto free_ucmd;
  3061. }
  3062. mutex_lock(&dev->flow_db->lock);
  3063. ft_prio = get_flow_table(dev, flow_attr,
  3064. is_egress ? MLX5_IB_FT_TX : MLX5_IB_FT_RX);
  3065. if (IS_ERR(ft_prio)) {
  3066. err = PTR_ERR(ft_prio);
  3067. goto unlock;
  3068. }
  3069. if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) {
  3070. ft_prio_tx = get_flow_table(dev, flow_attr, MLX5_IB_FT_TX);
  3071. if (IS_ERR(ft_prio_tx)) {
  3072. err = PTR_ERR(ft_prio_tx);
  3073. ft_prio_tx = NULL;
  3074. goto destroy_ft;
  3075. }
  3076. }
  3077. if (is_egress) {
  3078. dst->type = MLX5_FLOW_DESTINATION_TYPE_PORT;
  3079. } else {
  3080. dst->type = MLX5_FLOW_DESTINATION_TYPE_TIR;
  3081. if (mqp->flags & MLX5_IB_QP_RSS)
  3082. dst->tir_num = mqp->rss_qp.tirn;
  3083. else
  3084. dst->tir_num = mqp->raw_packet_qp.rq.tirn;
  3085. }
  3086. if (flow_attr->type == IB_FLOW_ATTR_NORMAL) {
  3087. if (flow_attr->flags & IB_FLOW_ATTR_FLAGS_DONT_TRAP) {
  3088. handler = create_dont_trap_rule(dev, ft_prio,
  3089. flow_attr, dst);
  3090. } else {
  3091. underlay_qpn = (mqp->flags & MLX5_IB_QP_UNDERLAY) ?
  3092. mqp->underlay_qpn : 0;
  3093. handler = _create_flow_rule(dev, ft_prio, flow_attr,
  3094. dst, underlay_qpn, ucmd);
  3095. }
  3096. } else if (flow_attr->type == IB_FLOW_ATTR_ALL_DEFAULT ||
  3097. flow_attr->type == IB_FLOW_ATTR_MC_DEFAULT) {
  3098. handler = create_leftovers_rule(dev, ft_prio, flow_attr,
  3099. dst);
  3100. } else if (flow_attr->type == IB_FLOW_ATTR_SNIFFER) {
  3101. handler = create_sniffer_rule(dev, ft_prio, ft_prio_tx, dst);
  3102. } else {
  3103. err = -EINVAL;
  3104. goto destroy_ft;
  3105. }
  3106. if (IS_ERR(handler)) {
  3107. err = PTR_ERR(handler);
  3108. handler = NULL;
  3109. goto destroy_ft;
  3110. }
  3111. mutex_unlock(&dev->flow_db->lock);
  3112. kfree(dst);
  3113. kfree(ucmd);
  3114. return &handler->ibflow;
  3115. destroy_ft:
  3116. put_flow_table(dev, ft_prio, false);
  3117. if (ft_prio_tx)
  3118. put_flow_table(dev, ft_prio_tx, false);
  3119. unlock:
  3120. mutex_unlock(&dev->flow_db->lock);
  3121. kfree(dst);
  3122. free_ucmd:
  3123. kfree(ucmd);
  3124. return ERR_PTR(err);
  3125. }
  3126. static struct mlx5_ib_flow_prio *_get_flow_table(struct mlx5_ib_dev *dev,
  3127. int priority, bool mcast)
  3128. {
  3129. int max_table_size;
  3130. struct mlx5_flow_namespace *ns = NULL;
  3131. struct mlx5_ib_flow_prio *prio;
  3132. max_table_size = BIT(MLX5_CAP_FLOWTABLE_NIC_RX(dev->mdev,
  3133. log_max_ft_size));
  3134. if (max_table_size < MLX5_FS_MAX_ENTRIES)
  3135. return ERR_PTR(-ENOMEM);
  3136. if (mcast)
  3137. priority = MLX5_IB_FLOW_MCAST_PRIO;
  3138. else
  3139. priority = ib_prio_to_core_prio(priority, false);
  3140. ns = mlx5_get_flow_namespace(dev->mdev, MLX5_FLOW_NAMESPACE_BYPASS);
  3141. if (!ns)
  3142. return ERR_PTR(-ENOTSUPP);
  3143. prio = &dev->flow_db->prios[priority];
  3144. if (prio->flow_table)
  3145. return prio;
  3146. return _get_prio(ns, prio, priority, MLX5_FS_MAX_ENTRIES,
  3147. MLX5_FS_MAX_TYPES);
  3148. }
  3149. static struct mlx5_ib_flow_handler *
  3150. _create_raw_flow_rule(struct mlx5_ib_dev *dev,
  3151. struct mlx5_ib_flow_prio *ft_prio,
  3152. struct mlx5_flow_destination *dst,
  3153. struct mlx5_ib_flow_matcher *fs_matcher,
  3154. void *cmd_in, int inlen)
  3155. {
  3156. struct mlx5_ib_flow_handler *handler;
  3157. struct mlx5_flow_act flow_act = {.flow_tag = MLX5_FS_DEFAULT_FLOW_TAG};
  3158. struct mlx5_flow_spec *spec;
  3159. struct mlx5_flow_table *ft = ft_prio->flow_table;
  3160. int err = 0;
  3161. spec = kvzalloc(sizeof(*spec), GFP_KERNEL);
  3162. handler = kzalloc(sizeof(*handler), GFP_KERNEL);
  3163. if (!handler || !spec) {
  3164. err = -ENOMEM;
  3165. goto free;
  3166. }
  3167. INIT_LIST_HEAD(&handler->list);
  3168. memcpy(spec->match_value, cmd_in, inlen);
  3169. memcpy(spec->match_criteria, fs_matcher->matcher_mask.match_params,
  3170. fs_matcher->mask_len);
  3171. spec->match_criteria_enable = fs_matcher->match_criteria_enable;
  3172. flow_act.action |= MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
  3173. handler->rule = mlx5_add_flow_rules(ft, spec,
  3174. &flow_act, dst, 1);
  3175. if (IS_ERR(handler->rule)) {
  3176. err = PTR_ERR(handler->rule);
  3177. goto free;
  3178. }
  3179. ft_prio->refcount++;
  3180. handler->prio = ft_prio;
  3181. handler->dev = dev;
  3182. ft_prio->flow_table = ft;
  3183. free:
  3184. if (err)
  3185. kfree(handler);
  3186. kvfree(spec);
  3187. return err ? ERR_PTR(err) : handler;
  3188. }
  3189. static bool raw_fs_is_multicast(struct mlx5_ib_flow_matcher *fs_matcher,
  3190. void *match_v)
  3191. {
  3192. void *match_c;
  3193. void *match_v_set_lyr_2_4, *match_c_set_lyr_2_4;
  3194. void *dmac, *dmac_mask;
  3195. void *ipv4, *ipv4_mask;
  3196. if (!(fs_matcher->match_criteria_enable &
  3197. (1 << MATCH_CRITERIA_ENABLE_OUTER_BIT)))
  3198. return false;
  3199. match_c = fs_matcher->matcher_mask.match_params;
  3200. match_v_set_lyr_2_4 = MLX5_ADDR_OF(fte_match_param, match_v,
  3201. outer_headers);
  3202. match_c_set_lyr_2_4 = MLX5_ADDR_OF(fte_match_param, match_c,
  3203. outer_headers);
  3204. dmac = MLX5_ADDR_OF(fte_match_set_lyr_2_4, match_v_set_lyr_2_4,
  3205. dmac_47_16);
  3206. dmac_mask = MLX5_ADDR_OF(fte_match_set_lyr_2_4, match_c_set_lyr_2_4,
  3207. dmac_47_16);
  3208. if (is_multicast_ether_addr(dmac) &&
  3209. is_multicast_ether_addr(dmac_mask))
  3210. return true;
  3211. ipv4 = MLX5_ADDR_OF(fte_match_set_lyr_2_4, match_v_set_lyr_2_4,
  3212. dst_ipv4_dst_ipv6.ipv4_layout.ipv4);
  3213. ipv4_mask = MLX5_ADDR_OF(fte_match_set_lyr_2_4, match_c_set_lyr_2_4,
  3214. dst_ipv4_dst_ipv6.ipv4_layout.ipv4);
  3215. if (ipv4_is_multicast(*(__be32 *)(ipv4)) &&
  3216. ipv4_is_multicast(*(__be32 *)(ipv4_mask)))
  3217. return true;
  3218. return false;
  3219. }
  3220. struct mlx5_ib_flow_handler *
  3221. mlx5_ib_raw_fs_rule_add(struct mlx5_ib_dev *dev,
  3222. struct mlx5_ib_flow_matcher *fs_matcher,
  3223. void *cmd_in, int inlen, int dest_id,
  3224. int dest_type)
  3225. {
  3226. struct mlx5_flow_destination *dst;
  3227. struct mlx5_ib_flow_prio *ft_prio;
  3228. int priority = fs_matcher->priority;
  3229. struct mlx5_ib_flow_handler *handler;
  3230. bool mcast;
  3231. int err;
  3232. if (fs_matcher->flow_type != MLX5_IB_FLOW_TYPE_NORMAL)
  3233. return ERR_PTR(-EOPNOTSUPP);
  3234. if (fs_matcher->priority > MLX5_IB_FLOW_LAST_PRIO)
  3235. return ERR_PTR(-ENOMEM);
  3236. dst = kzalloc(sizeof(*dst), GFP_KERNEL);
  3237. if (!dst)
  3238. return ERR_PTR(-ENOMEM);
  3239. mcast = raw_fs_is_multicast(fs_matcher, cmd_in);
  3240. mutex_lock(&dev->flow_db->lock);
  3241. ft_prio = _get_flow_table(dev, priority, mcast);
  3242. if (IS_ERR(ft_prio)) {
  3243. err = PTR_ERR(ft_prio);
  3244. goto unlock;
  3245. }
  3246. if (dest_type == MLX5_FLOW_DESTINATION_TYPE_TIR) {
  3247. dst->type = dest_type;
  3248. dst->tir_num = dest_id;
  3249. } else {
  3250. dst->type = MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE_NUM;
  3251. dst->ft_num = dest_id;
  3252. }
  3253. handler = _create_raw_flow_rule(dev, ft_prio, dst, fs_matcher, cmd_in,
  3254. inlen);
  3255. if (IS_ERR(handler)) {
  3256. err = PTR_ERR(handler);
  3257. goto destroy_ft;
  3258. }
  3259. mutex_unlock(&dev->flow_db->lock);
  3260. atomic_inc(&fs_matcher->usecnt);
  3261. handler->flow_matcher = fs_matcher;
  3262. kfree(dst);
  3263. return handler;
  3264. destroy_ft:
  3265. put_flow_table(dev, ft_prio, false);
  3266. unlock:
  3267. mutex_unlock(&dev->flow_db->lock);
  3268. kfree(dst);
  3269. return ERR_PTR(err);
  3270. }
  3271. static u32 mlx5_ib_flow_action_flags_to_accel_xfrm_flags(u32 mlx5_flags)
  3272. {
  3273. u32 flags = 0;
  3274. if (mlx5_flags & MLX5_IB_UAPI_FLOW_ACTION_FLAGS_REQUIRE_METADATA)
  3275. flags |= MLX5_ACCEL_XFRM_FLAG_REQUIRE_METADATA;
  3276. return flags;
  3277. }
  3278. #define MLX5_FLOW_ACTION_ESP_CREATE_LAST_SUPPORTED MLX5_IB_UAPI_FLOW_ACTION_FLAGS_REQUIRE_METADATA
  3279. static struct ib_flow_action *
  3280. mlx5_ib_create_flow_action_esp(struct ib_device *device,
  3281. const struct ib_flow_action_attrs_esp *attr,
  3282. struct uverbs_attr_bundle *attrs)
  3283. {
  3284. struct mlx5_ib_dev *mdev = to_mdev(device);
  3285. struct ib_uverbs_flow_action_esp_keymat_aes_gcm *aes_gcm;
  3286. struct mlx5_accel_esp_xfrm_attrs accel_attrs = {};
  3287. struct mlx5_ib_flow_action *action;
  3288. u64 action_flags;
  3289. u64 flags;
  3290. int err = 0;
  3291. err = uverbs_get_flags64(
  3292. &action_flags, attrs, MLX5_IB_ATTR_CREATE_FLOW_ACTION_FLAGS,
  3293. ((MLX5_FLOW_ACTION_ESP_CREATE_LAST_SUPPORTED << 1) - 1));
  3294. if (err)
  3295. return ERR_PTR(err);
  3296. flags = mlx5_ib_flow_action_flags_to_accel_xfrm_flags(action_flags);
  3297. /* We current only support a subset of the standard features. Only a
  3298. * keymat of type AES_GCM, with icv_len == 16, iv_algo == SEQ and esn
  3299. * (with overlap). Full offload mode isn't supported.
  3300. */
  3301. if (!attr->keymat || attr->replay || attr->encap ||
  3302. attr->spi || attr->seq || attr->tfc_pad ||
  3303. attr->hard_limit_pkts ||
  3304. (attr->flags & ~(IB_FLOW_ACTION_ESP_FLAGS_ESN_TRIGGERED |
  3305. IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ENCRYPT)))
  3306. return ERR_PTR(-EOPNOTSUPP);
  3307. if (attr->keymat->protocol !=
  3308. IB_UVERBS_FLOW_ACTION_ESP_KEYMAT_AES_GCM)
  3309. return ERR_PTR(-EOPNOTSUPP);
  3310. aes_gcm = &attr->keymat->keymat.aes_gcm;
  3311. if (aes_gcm->icv_len != 16 ||
  3312. aes_gcm->iv_algo != IB_UVERBS_FLOW_ACTION_IV_ALGO_SEQ)
  3313. return ERR_PTR(-EOPNOTSUPP);
  3314. action = kmalloc(sizeof(*action), GFP_KERNEL);
  3315. if (!action)
  3316. return ERR_PTR(-ENOMEM);
  3317. action->esp_aes_gcm.ib_flags = attr->flags;
  3318. memcpy(&accel_attrs.keymat.aes_gcm.aes_key, &aes_gcm->aes_key,
  3319. sizeof(accel_attrs.keymat.aes_gcm.aes_key));
  3320. accel_attrs.keymat.aes_gcm.key_len = aes_gcm->key_len * 8;
  3321. memcpy(&accel_attrs.keymat.aes_gcm.salt, &aes_gcm->salt,
  3322. sizeof(accel_attrs.keymat.aes_gcm.salt));
  3323. memcpy(&accel_attrs.keymat.aes_gcm.seq_iv, &aes_gcm->iv,
  3324. sizeof(accel_attrs.keymat.aes_gcm.seq_iv));
  3325. accel_attrs.keymat.aes_gcm.icv_len = aes_gcm->icv_len * 8;
  3326. accel_attrs.keymat.aes_gcm.iv_algo = MLX5_ACCEL_ESP_AES_GCM_IV_ALGO_SEQ;
  3327. accel_attrs.keymat_type = MLX5_ACCEL_ESP_KEYMAT_AES_GCM;
  3328. accel_attrs.esn = attr->esn;
  3329. if (attr->flags & IB_FLOW_ACTION_ESP_FLAGS_ESN_TRIGGERED)
  3330. accel_attrs.flags |= MLX5_ACCEL_ESP_FLAGS_ESN_TRIGGERED;
  3331. if (attr->flags & IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW)
  3332. accel_attrs.flags |= MLX5_ACCEL_ESP_FLAGS_ESN_STATE_OVERLAP;
  3333. if (attr->flags & IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ENCRYPT)
  3334. accel_attrs.action |= MLX5_ACCEL_ESP_ACTION_ENCRYPT;
  3335. action->esp_aes_gcm.ctx =
  3336. mlx5_accel_esp_create_xfrm(mdev->mdev, &accel_attrs, flags);
  3337. if (IS_ERR(action->esp_aes_gcm.ctx)) {
  3338. err = PTR_ERR(action->esp_aes_gcm.ctx);
  3339. goto err_parse;
  3340. }
  3341. action->esp_aes_gcm.ib_flags = attr->flags;
  3342. return &action->ib_action;
  3343. err_parse:
  3344. kfree(action);
  3345. return ERR_PTR(err);
  3346. }
  3347. static int
  3348. mlx5_ib_modify_flow_action_esp(struct ib_flow_action *action,
  3349. const struct ib_flow_action_attrs_esp *attr,
  3350. struct uverbs_attr_bundle *attrs)
  3351. {
  3352. struct mlx5_ib_flow_action *maction = to_mflow_act(action);
  3353. struct mlx5_accel_esp_xfrm_attrs accel_attrs;
  3354. int err = 0;
  3355. if (attr->keymat || attr->replay || attr->encap ||
  3356. attr->spi || attr->seq || attr->tfc_pad ||
  3357. attr->hard_limit_pkts ||
  3358. (attr->flags & ~(IB_FLOW_ACTION_ESP_FLAGS_ESN_TRIGGERED |
  3359. IB_FLOW_ACTION_ESP_FLAGS_MOD_ESP_ATTRS |
  3360. IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW)))
  3361. return -EOPNOTSUPP;
  3362. /* Only the ESN value or the MLX5_ACCEL_ESP_FLAGS_ESN_STATE_OVERLAP can
  3363. * be modified.
  3364. */
  3365. if (!(maction->esp_aes_gcm.ib_flags &
  3366. IB_FLOW_ACTION_ESP_FLAGS_ESN_TRIGGERED) &&
  3367. attr->flags & (IB_FLOW_ACTION_ESP_FLAGS_ESN_TRIGGERED |
  3368. IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW))
  3369. return -EINVAL;
  3370. memcpy(&accel_attrs, &maction->esp_aes_gcm.ctx->attrs,
  3371. sizeof(accel_attrs));
  3372. accel_attrs.esn = attr->esn;
  3373. if (attr->flags & IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW)
  3374. accel_attrs.flags |= MLX5_ACCEL_ESP_FLAGS_ESN_STATE_OVERLAP;
  3375. else
  3376. accel_attrs.flags &= ~MLX5_ACCEL_ESP_FLAGS_ESN_STATE_OVERLAP;
  3377. err = mlx5_accel_esp_modify_xfrm(maction->esp_aes_gcm.ctx,
  3378. &accel_attrs);
  3379. if (err)
  3380. return err;
  3381. maction->esp_aes_gcm.ib_flags &=
  3382. ~IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW;
  3383. maction->esp_aes_gcm.ib_flags |=
  3384. attr->flags & IB_UVERBS_FLOW_ACTION_ESP_FLAGS_ESN_NEW_WINDOW;
  3385. return 0;
  3386. }
  3387. static int mlx5_ib_destroy_flow_action(struct ib_flow_action *action)
  3388. {
  3389. struct mlx5_ib_flow_action *maction = to_mflow_act(action);
  3390. switch (action->type) {
  3391. case IB_FLOW_ACTION_ESP:
  3392. /*
  3393. * We only support aes_gcm by now, so we implicitly know this is
  3394. * the underline crypto.
  3395. */
  3396. mlx5_accel_esp_destroy_xfrm(maction->esp_aes_gcm.ctx);
  3397. break;
  3398. default:
  3399. WARN_ON(true);
  3400. break;
  3401. }
  3402. kfree(maction);
  3403. return 0;
  3404. }
  3405. static int mlx5_ib_mcg_attach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
  3406. {
  3407. struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
  3408. struct mlx5_ib_qp *mqp = to_mqp(ibqp);
  3409. int err;
  3410. if (mqp->flags & MLX5_IB_QP_UNDERLAY) {
  3411. mlx5_ib_dbg(dev, "Attaching a multi cast group to underlay QP is not supported\n");
  3412. return -EOPNOTSUPP;
  3413. }
  3414. err = mlx5_core_attach_mcg(dev->mdev, gid, ibqp->qp_num);
  3415. if (err)
  3416. mlx5_ib_warn(dev, "failed attaching QPN 0x%x, MGID %pI6\n",
  3417. ibqp->qp_num, gid->raw);
  3418. return err;
  3419. }
  3420. static int mlx5_ib_mcg_detach(struct ib_qp *ibqp, union ib_gid *gid, u16 lid)
  3421. {
  3422. struct mlx5_ib_dev *dev = to_mdev(ibqp->device);
  3423. int err;
  3424. err = mlx5_core_detach_mcg(dev->mdev, gid, ibqp->qp_num);
  3425. if (err)
  3426. mlx5_ib_warn(dev, "failed detaching QPN 0x%x, MGID %pI6\n",
  3427. ibqp->qp_num, gid->raw);
  3428. return err;
  3429. }
  3430. static int init_node_data(struct mlx5_ib_dev *dev)
  3431. {
  3432. int err;
  3433. err = mlx5_query_node_desc(dev, dev->ib_dev.node_desc);
  3434. if (err)
  3435. return err;
  3436. dev->mdev->rev_id = dev->mdev->pdev->revision;
  3437. return mlx5_query_node_guid(dev, &dev->ib_dev.node_guid);
  3438. }
  3439. static ssize_t show_fw_pages(struct device *device, struct device_attribute *attr,
  3440. char *buf)
  3441. {
  3442. struct mlx5_ib_dev *dev =
  3443. container_of(device, struct mlx5_ib_dev, ib_dev.dev);
  3444. return sprintf(buf, "%d\n", dev->mdev->priv.fw_pages);
  3445. }
  3446. static ssize_t show_reg_pages(struct device *device,
  3447. struct device_attribute *attr, char *buf)
  3448. {
  3449. struct mlx5_ib_dev *dev =
  3450. container_of(device, struct mlx5_ib_dev, ib_dev.dev);
  3451. return sprintf(buf, "%d\n", atomic_read(&dev->mdev->priv.reg_pages));
  3452. }
  3453. static ssize_t show_hca(struct device *device, struct device_attribute *attr,
  3454. char *buf)
  3455. {
  3456. struct mlx5_ib_dev *dev =
  3457. container_of(device, struct mlx5_ib_dev, ib_dev.dev);
  3458. return sprintf(buf, "MT%d\n", dev->mdev->pdev->device);
  3459. }
  3460. static ssize_t show_rev(struct device *device, struct device_attribute *attr,
  3461. char *buf)
  3462. {
  3463. struct mlx5_ib_dev *dev =
  3464. container_of(device, struct mlx5_ib_dev, ib_dev.dev);
  3465. return sprintf(buf, "%x\n", dev->mdev->rev_id);
  3466. }
  3467. static ssize_t show_board(struct device *device, struct device_attribute *attr,
  3468. char *buf)
  3469. {
  3470. struct mlx5_ib_dev *dev =
  3471. container_of(device, struct mlx5_ib_dev, ib_dev.dev);
  3472. return sprintf(buf, "%.*s\n", MLX5_BOARD_ID_LEN,
  3473. dev->mdev->board_id);
  3474. }
  3475. static DEVICE_ATTR(hw_rev, S_IRUGO, show_rev, NULL);
  3476. static DEVICE_ATTR(hca_type, S_IRUGO, show_hca, NULL);
  3477. static DEVICE_ATTR(board_id, S_IRUGO, show_board, NULL);
  3478. static DEVICE_ATTR(fw_pages, S_IRUGO, show_fw_pages, NULL);
  3479. static DEVICE_ATTR(reg_pages, S_IRUGO, show_reg_pages, NULL);
  3480. static struct device_attribute *mlx5_class_attributes[] = {
  3481. &dev_attr_hw_rev,
  3482. &dev_attr_hca_type,
  3483. &dev_attr_board_id,
  3484. &dev_attr_fw_pages,
  3485. &dev_attr_reg_pages,
  3486. };
  3487. static void pkey_change_handler(struct work_struct *work)
  3488. {
  3489. struct mlx5_ib_port_resources *ports =
  3490. container_of(work, struct mlx5_ib_port_resources,
  3491. pkey_change_work);
  3492. mutex_lock(&ports->devr->mutex);
  3493. mlx5_ib_gsi_pkey_change(ports->gsi);
  3494. mutex_unlock(&ports->devr->mutex);
  3495. }
  3496. static void mlx5_ib_handle_internal_error(struct mlx5_ib_dev *ibdev)
  3497. {
  3498. struct mlx5_ib_qp *mqp;
  3499. struct mlx5_ib_cq *send_mcq, *recv_mcq;
  3500. struct mlx5_core_cq *mcq;
  3501. struct list_head cq_armed_list;
  3502. unsigned long flags_qp;
  3503. unsigned long flags_cq;
  3504. unsigned long flags;
  3505. INIT_LIST_HEAD(&cq_armed_list);
  3506. /* Go over qp list reside on that ibdev, sync with create/destroy qp.*/
  3507. spin_lock_irqsave(&ibdev->reset_flow_resource_lock, flags);
  3508. list_for_each_entry(mqp, &ibdev->qp_list, qps_list) {
  3509. spin_lock_irqsave(&mqp->sq.lock, flags_qp);
  3510. if (mqp->sq.tail != mqp->sq.head) {
  3511. send_mcq = to_mcq(mqp->ibqp.send_cq);
  3512. spin_lock_irqsave(&send_mcq->lock, flags_cq);
  3513. if (send_mcq->mcq.comp &&
  3514. mqp->ibqp.send_cq->comp_handler) {
  3515. if (!send_mcq->mcq.reset_notify_added) {
  3516. send_mcq->mcq.reset_notify_added = 1;
  3517. list_add_tail(&send_mcq->mcq.reset_notify,
  3518. &cq_armed_list);
  3519. }
  3520. }
  3521. spin_unlock_irqrestore(&send_mcq->lock, flags_cq);
  3522. }
  3523. spin_unlock_irqrestore(&mqp->sq.lock, flags_qp);
  3524. spin_lock_irqsave(&mqp->rq.lock, flags_qp);
  3525. /* no handling is needed for SRQ */
  3526. if (!mqp->ibqp.srq) {
  3527. if (mqp->rq.tail != mqp->rq.head) {
  3528. recv_mcq = to_mcq(mqp->ibqp.recv_cq);
  3529. spin_lock_irqsave(&recv_mcq->lock, flags_cq);
  3530. if (recv_mcq->mcq.comp &&
  3531. mqp->ibqp.recv_cq->comp_handler) {
  3532. if (!recv_mcq->mcq.reset_notify_added) {
  3533. recv_mcq->mcq.reset_notify_added = 1;
  3534. list_add_tail(&recv_mcq->mcq.reset_notify,
  3535. &cq_armed_list);
  3536. }
  3537. }
  3538. spin_unlock_irqrestore(&recv_mcq->lock,
  3539. flags_cq);
  3540. }
  3541. }
  3542. spin_unlock_irqrestore(&mqp->rq.lock, flags_qp);
  3543. }
  3544. /*At that point all inflight post send were put to be executed as of we
  3545. * lock/unlock above locks Now need to arm all involved CQs.
  3546. */
  3547. list_for_each_entry(mcq, &cq_armed_list, reset_notify) {
  3548. mcq->comp(mcq);
  3549. }
  3550. spin_unlock_irqrestore(&ibdev->reset_flow_resource_lock, flags);
  3551. }
  3552. static void delay_drop_handler(struct work_struct *work)
  3553. {
  3554. int err;
  3555. struct mlx5_ib_delay_drop *delay_drop =
  3556. container_of(work, struct mlx5_ib_delay_drop,
  3557. delay_drop_work);
  3558. atomic_inc(&delay_drop->events_cnt);
  3559. mutex_lock(&delay_drop->lock);
  3560. err = mlx5_core_set_delay_drop(delay_drop->dev->mdev,
  3561. delay_drop->timeout);
  3562. if (err) {
  3563. mlx5_ib_warn(delay_drop->dev, "Failed to set delay drop, timeout=%u\n",
  3564. delay_drop->timeout);
  3565. delay_drop->activate = false;
  3566. }
  3567. mutex_unlock(&delay_drop->lock);
  3568. }
  3569. static void mlx5_ib_handle_event(struct work_struct *_work)
  3570. {
  3571. struct mlx5_ib_event_work *work =
  3572. container_of(_work, struct mlx5_ib_event_work, work);
  3573. struct mlx5_ib_dev *ibdev;
  3574. struct ib_event ibev;
  3575. bool fatal = false;
  3576. u8 port = (u8)work->param;
  3577. if (mlx5_core_is_mp_slave(work->dev)) {
  3578. ibdev = mlx5_ib_get_ibdev_from_mpi(work->context);
  3579. if (!ibdev)
  3580. goto out;
  3581. } else {
  3582. ibdev = work->context;
  3583. }
  3584. switch (work->event) {
  3585. case MLX5_DEV_EVENT_SYS_ERROR:
  3586. ibev.event = IB_EVENT_DEVICE_FATAL;
  3587. mlx5_ib_handle_internal_error(ibdev);
  3588. fatal = true;
  3589. break;
  3590. case MLX5_DEV_EVENT_PORT_UP:
  3591. case MLX5_DEV_EVENT_PORT_DOWN:
  3592. case MLX5_DEV_EVENT_PORT_INITIALIZED:
  3593. /* In RoCE, port up/down events are handled in
  3594. * mlx5_netdev_event().
  3595. */
  3596. if (mlx5_ib_port_link_layer(&ibdev->ib_dev, port) ==
  3597. IB_LINK_LAYER_ETHERNET)
  3598. goto out;
  3599. ibev.event = (work->event == MLX5_DEV_EVENT_PORT_UP) ?
  3600. IB_EVENT_PORT_ACTIVE : IB_EVENT_PORT_ERR;
  3601. break;
  3602. case MLX5_DEV_EVENT_LID_CHANGE:
  3603. ibev.event = IB_EVENT_LID_CHANGE;
  3604. break;
  3605. case MLX5_DEV_EVENT_PKEY_CHANGE:
  3606. ibev.event = IB_EVENT_PKEY_CHANGE;
  3607. schedule_work(&ibdev->devr.ports[port - 1].pkey_change_work);
  3608. break;
  3609. case MLX5_DEV_EVENT_GUID_CHANGE:
  3610. ibev.event = IB_EVENT_GID_CHANGE;
  3611. break;
  3612. case MLX5_DEV_EVENT_CLIENT_REREG:
  3613. ibev.event = IB_EVENT_CLIENT_REREGISTER;
  3614. break;
  3615. case MLX5_DEV_EVENT_DELAY_DROP_TIMEOUT:
  3616. schedule_work(&ibdev->delay_drop.delay_drop_work);
  3617. goto out;
  3618. default:
  3619. goto out;
  3620. }
  3621. ibev.device = &ibdev->ib_dev;
  3622. ibev.element.port_num = port;
  3623. if (!rdma_is_port_valid(&ibdev->ib_dev, port)) {
  3624. mlx5_ib_warn(ibdev, "warning: event on port %d\n", port);
  3625. goto out;
  3626. }
  3627. if (ibdev->ib_active)
  3628. ib_dispatch_event(&ibev);
  3629. if (fatal)
  3630. ibdev->ib_active = false;
  3631. out:
  3632. kfree(work);
  3633. }
  3634. static void mlx5_ib_event(struct mlx5_core_dev *dev, void *context,
  3635. enum mlx5_dev_event event, unsigned long param)
  3636. {
  3637. struct mlx5_ib_event_work *work;
  3638. work = kmalloc(sizeof(*work), GFP_ATOMIC);
  3639. if (!work)
  3640. return;
  3641. INIT_WORK(&work->work, mlx5_ib_handle_event);
  3642. work->dev = dev;
  3643. work->param = param;
  3644. work->context = context;
  3645. work->event = event;
  3646. queue_work(mlx5_ib_event_wq, &work->work);
  3647. }
  3648. static int set_has_smi_cap(struct mlx5_ib_dev *dev)
  3649. {
  3650. struct mlx5_hca_vport_context vport_ctx;
  3651. int err;
  3652. int port;
  3653. for (port = 1; port <= dev->num_ports; port++) {
  3654. dev->mdev->port_caps[port - 1].has_smi = false;
  3655. if (MLX5_CAP_GEN(dev->mdev, port_type) ==
  3656. MLX5_CAP_PORT_TYPE_IB) {
  3657. if (MLX5_CAP_GEN(dev->mdev, ib_virt)) {
  3658. err = mlx5_query_hca_vport_context(dev->mdev, 0,
  3659. port, 0,
  3660. &vport_ctx);
  3661. if (err) {
  3662. mlx5_ib_err(dev, "query_hca_vport_context for port=%d failed %d\n",
  3663. port, err);
  3664. return err;
  3665. }
  3666. dev->mdev->port_caps[port - 1].has_smi =
  3667. vport_ctx.has_smi;
  3668. } else {
  3669. dev->mdev->port_caps[port - 1].has_smi = true;
  3670. }
  3671. }
  3672. }
  3673. return 0;
  3674. }
  3675. static void get_ext_port_caps(struct mlx5_ib_dev *dev)
  3676. {
  3677. int port;
  3678. for (port = 1; port <= dev->num_ports; port++)
  3679. mlx5_query_ext_port_caps(dev, port);
  3680. }
  3681. static int get_port_caps(struct mlx5_ib_dev *dev, u8 port)
  3682. {
  3683. struct ib_device_attr *dprops = NULL;
  3684. struct ib_port_attr *pprops = NULL;
  3685. int err = -ENOMEM;
  3686. struct ib_udata uhw = {.inlen = 0, .outlen = 0};
  3687. pprops = kmalloc(sizeof(*pprops), GFP_KERNEL);
  3688. if (!pprops)
  3689. goto out;
  3690. dprops = kmalloc(sizeof(*dprops), GFP_KERNEL);
  3691. if (!dprops)
  3692. goto out;
  3693. err = set_has_smi_cap(dev);
  3694. if (err)
  3695. goto out;
  3696. err = mlx5_ib_query_device(&dev->ib_dev, dprops, &uhw);
  3697. if (err) {
  3698. mlx5_ib_warn(dev, "query_device failed %d\n", err);
  3699. goto out;
  3700. }
  3701. memset(pprops, 0, sizeof(*pprops));
  3702. err = mlx5_ib_query_port(&dev->ib_dev, port, pprops);
  3703. if (err) {
  3704. mlx5_ib_warn(dev, "query_port %d failed %d\n",
  3705. port, err);
  3706. goto out;
  3707. }
  3708. dev->mdev->port_caps[port - 1].pkey_table_len =
  3709. dprops->max_pkeys;
  3710. dev->mdev->port_caps[port - 1].gid_table_len =
  3711. pprops->gid_tbl_len;
  3712. mlx5_ib_dbg(dev, "port %d: pkey_table_len %d, gid_table_len %d\n",
  3713. port, dprops->max_pkeys, pprops->gid_tbl_len);
  3714. out:
  3715. kfree(pprops);
  3716. kfree(dprops);
  3717. return err;
  3718. }
  3719. static void destroy_umrc_res(struct mlx5_ib_dev *dev)
  3720. {
  3721. int err;
  3722. err = mlx5_mr_cache_cleanup(dev);
  3723. if (err)
  3724. mlx5_ib_warn(dev, "mr cache cleanup failed\n");
  3725. if (dev->umrc.qp)
  3726. mlx5_ib_destroy_qp(dev->umrc.qp);
  3727. if (dev->umrc.cq)
  3728. ib_free_cq(dev->umrc.cq);
  3729. if (dev->umrc.pd)
  3730. ib_dealloc_pd(dev->umrc.pd);
  3731. }
  3732. enum {
  3733. MAX_UMR_WR = 128,
  3734. };
  3735. static int create_umr_res(struct mlx5_ib_dev *dev)
  3736. {
  3737. struct ib_qp_init_attr *init_attr = NULL;
  3738. struct ib_qp_attr *attr = NULL;
  3739. struct ib_pd *pd;
  3740. struct ib_cq *cq;
  3741. struct ib_qp *qp;
  3742. int ret;
  3743. attr = kzalloc(sizeof(*attr), GFP_KERNEL);
  3744. init_attr = kzalloc(sizeof(*init_attr), GFP_KERNEL);
  3745. if (!attr || !init_attr) {
  3746. ret = -ENOMEM;
  3747. goto error_0;
  3748. }
  3749. pd = ib_alloc_pd(&dev->ib_dev, 0);
  3750. if (IS_ERR(pd)) {
  3751. mlx5_ib_dbg(dev, "Couldn't create PD for sync UMR QP\n");
  3752. ret = PTR_ERR(pd);
  3753. goto error_0;
  3754. }
  3755. cq = ib_alloc_cq(&dev->ib_dev, NULL, 128, 0, IB_POLL_SOFTIRQ);
  3756. if (IS_ERR(cq)) {
  3757. mlx5_ib_dbg(dev, "Couldn't create CQ for sync UMR QP\n");
  3758. ret = PTR_ERR(cq);
  3759. goto error_2;
  3760. }
  3761. init_attr->send_cq = cq;
  3762. init_attr->recv_cq = cq;
  3763. init_attr->sq_sig_type = IB_SIGNAL_ALL_WR;
  3764. init_attr->cap.max_send_wr = MAX_UMR_WR;
  3765. init_attr->cap.max_send_sge = 1;
  3766. init_attr->qp_type = MLX5_IB_QPT_REG_UMR;
  3767. init_attr->port_num = 1;
  3768. qp = mlx5_ib_create_qp(pd, init_attr, NULL);
  3769. if (IS_ERR(qp)) {
  3770. mlx5_ib_dbg(dev, "Couldn't create sync UMR QP\n");
  3771. ret = PTR_ERR(qp);
  3772. goto error_3;
  3773. }
  3774. qp->device = &dev->ib_dev;
  3775. qp->real_qp = qp;
  3776. qp->uobject = NULL;
  3777. qp->qp_type = MLX5_IB_QPT_REG_UMR;
  3778. qp->send_cq = init_attr->send_cq;
  3779. qp->recv_cq = init_attr->recv_cq;
  3780. attr->qp_state = IB_QPS_INIT;
  3781. attr->port_num = 1;
  3782. ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE | IB_QP_PKEY_INDEX |
  3783. IB_QP_PORT, NULL);
  3784. if (ret) {
  3785. mlx5_ib_dbg(dev, "Couldn't modify UMR QP\n");
  3786. goto error_4;
  3787. }
  3788. memset(attr, 0, sizeof(*attr));
  3789. attr->qp_state = IB_QPS_RTR;
  3790. attr->path_mtu = IB_MTU_256;
  3791. ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE, NULL);
  3792. if (ret) {
  3793. mlx5_ib_dbg(dev, "Couldn't modify umr QP to rtr\n");
  3794. goto error_4;
  3795. }
  3796. memset(attr, 0, sizeof(*attr));
  3797. attr->qp_state = IB_QPS_RTS;
  3798. ret = mlx5_ib_modify_qp(qp, attr, IB_QP_STATE, NULL);
  3799. if (ret) {
  3800. mlx5_ib_dbg(dev, "Couldn't modify umr QP to rts\n");
  3801. goto error_4;
  3802. }
  3803. dev->umrc.qp = qp;
  3804. dev->umrc.cq = cq;
  3805. dev->umrc.pd = pd;
  3806. sema_init(&dev->umrc.sem, MAX_UMR_WR);
  3807. ret = mlx5_mr_cache_init(dev);
  3808. if (ret) {
  3809. mlx5_ib_warn(dev, "mr cache init failed %d\n", ret);
  3810. goto error_4;
  3811. }
  3812. kfree(attr);
  3813. kfree(init_attr);
  3814. return 0;
  3815. error_4:
  3816. mlx5_ib_destroy_qp(qp);
  3817. dev->umrc.qp = NULL;
  3818. error_3:
  3819. ib_free_cq(cq);
  3820. dev->umrc.cq = NULL;
  3821. error_2:
  3822. ib_dealloc_pd(pd);
  3823. dev->umrc.pd = NULL;
  3824. error_0:
  3825. kfree(attr);
  3826. kfree(init_attr);
  3827. return ret;
  3828. }
  3829. static u8 mlx5_get_umr_fence(u8 umr_fence_cap)
  3830. {
  3831. switch (umr_fence_cap) {
  3832. case MLX5_CAP_UMR_FENCE_NONE:
  3833. return MLX5_FENCE_MODE_NONE;
  3834. case MLX5_CAP_UMR_FENCE_SMALL:
  3835. return MLX5_FENCE_MODE_INITIATOR_SMALL;
  3836. default:
  3837. return MLX5_FENCE_MODE_STRONG_ORDERING;
  3838. }
  3839. }
  3840. static int create_dev_resources(struct mlx5_ib_resources *devr)
  3841. {
  3842. struct ib_srq_init_attr attr;
  3843. struct mlx5_ib_dev *dev;
  3844. struct ib_cq_init_attr cq_attr = {.cqe = 1};
  3845. int port;
  3846. int ret = 0;
  3847. dev = container_of(devr, struct mlx5_ib_dev, devr);
  3848. mutex_init(&devr->mutex);
  3849. devr->p0 = mlx5_ib_alloc_pd(&dev->ib_dev, NULL, NULL);
  3850. if (IS_ERR(devr->p0)) {
  3851. ret = PTR_ERR(devr->p0);
  3852. goto error0;
  3853. }
  3854. devr->p0->device = &dev->ib_dev;
  3855. devr->p0->uobject = NULL;
  3856. atomic_set(&devr->p0->usecnt, 0);
  3857. devr->c0 = mlx5_ib_create_cq(&dev->ib_dev, &cq_attr, NULL, NULL);
  3858. if (IS_ERR(devr->c0)) {
  3859. ret = PTR_ERR(devr->c0);
  3860. goto error1;
  3861. }
  3862. devr->c0->device = &dev->ib_dev;
  3863. devr->c0->uobject = NULL;
  3864. devr->c0->comp_handler = NULL;
  3865. devr->c0->event_handler = NULL;
  3866. devr->c0->cq_context = NULL;
  3867. atomic_set(&devr->c0->usecnt, 0);
  3868. devr->x0 = mlx5_ib_alloc_xrcd(&dev->ib_dev, NULL, NULL);
  3869. if (IS_ERR(devr->x0)) {
  3870. ret = PTR_ERR(devr->x0);
  3871. goto error2;
  3872. }
  3873. devr->x0->device = &dev->ib_dev;
  3874. devr->x0->inode = NULL;
  3875. atomic_set(&devr->x0->usecnt, 0);
  3876. mutex_init(&devr->x0->tgt_qp_mutex);
  3877. INIT_LIST_HEAD(&devr->x0->tgt_qp_list);
  3878. devr->x1 = mlx5_ib_alloc_xrcd(&dev->ib_dev, NULL, NULL);
  3879. if (IS_ERR(devr->x1)) {
  3880. ret = PTR_ERR(devr->x1);
  3881. goto error3;
  3882. }
  3883. devr->x1->device = &dev->ib_dev;
  3884. devr->x1->inode = NULL;
  3885. atomic_set(&devr->x1->usecnt, 0);
  3886. mutex_init(&devr->x1->tgt_qp_mutex);
  3887. INIT_LIST_HEAD(&devr->x1->tgt_qp_list);
  3888. memset(&attr, 0, sizeof(attr));
  3889. attr.attr.max_sge = 1;
  3890. attr.attr.max_wr = 1;
  3891. attr.srq_type = IB_SRQT_XRC;
  3892. attr.ext.cq = devr->c0;
  3893. attr.ext.xrc.xrcd = devr->x0;
  3894. devr->s0 = mlx5_ib_create_srq(devr->p0, &attr, NULL);
  3895. if (IS_ERR(devr->s0)) {
  3896. ret = PTR_ERR(devr->s0);
  3897. goto error4;
  3898. }
  3899. devr->s0->device = &dev->ib_dev;
  3900. devr->s0->pd = devr->p0;
  3901. devr->s0->uobject = NULL;
  3902. devr->s0->event_handler = NULL;
  3903. devr->s0->srq_context = NULL;
  3904. devr->s0->srq_type = IB_SRQT_XRC;
  3905. devr->s0->ext.xrc.xrcd = devr->x0;
  3906. devr->s0->ext.cq = devr->c0;
  3907. atomic_inc(&devr->s0->ext.xrc.xrcd->usecnt);
  3908. atomic_inc(&devr->s0->ext.cq->usecnt);
  3909. atomic_inc(&devr->p0->usecnt);
  3910. atomic_set(&devr->s0->usecnt, 0);
  3911. memset(&attr, 0, sizeof(attr));
  3912. attr.attr.max_sge = 1;
  3913. attr.attr.max_wr = 1;
  3914. attr.srq_type = IB_SRQT_BASIC;
  3915. devr->s1 = mlx5_ib_create_srq(devr->p0, &attr, NULL);
  3916. if (IS_ERR(devr->s1)) {
  3917. ret = PTR_ERR(devr->s1);
  3918. goto error5;
  3919. }
  3920. devr->s1->device = &dev->ib_dev;
  3921. devr->s1->pd = devr->p0;
  3922. devr->s1->uobject = NULL;
  3923. devr->s1->event_handler = NULL;
  3924. devr->s1->srq_context = NULL;
  3925. devr->s1->srq_type = IB_SRQT_BASIC;
  3926. devr->s1->ext.cq = devr->c0;
  3927. atomic_inc(&devr->p0->usecnt);
  3928. atomic_set(&devr->s1->usecnt, 0);
  3929. for (port = 0; port < ARRAY_SIZE(devr->ports); ++port) {
  3930. INIT_WORK(&devr->ports[port].pkey_change_work,
  3931. pkey_change_handler);
  3932. devr->ports[port].devr = devr;
  3933. }
  3934. return 0;
  3935. error5:
  3936. mlx5_ib_destroy_srq(devr->s0);
  3937. error4:
  3938. mlx5_ib_dealloc_xrcd(devr->x1);
  3939. error3:
  3940. mlx5_ib_dealloc_xrcd(devr->x0);
  3941. error2:
  3942. mlx5_ib_destroy_cq(devr->c0);
  3943. error1:
  3944. mlx5_ib_dealloc_pd(devr->p0);
  3945. error0:
  3946. return ret;
  3947. }
  3948. static void destroy_dev_resources(struct mlx5_ib_resources *devr)
  3949. {
  3950. struct mlx5_ib_dev *dev =
  3951. container_of(devr, struct mlx5_ib_dev, devr);
  3952. int port;
  3953. mlx5_ib_destroy_srq(devr->s1);
  3954. mlx5_ib_destroy_srq(devr->s0);
  3955. mlx5_ib_dealloc_xrcd(devr->x0);
  3956. mlx5_ib_dealloc_xrcd(devr->x1);
  3957. mlx5_ib_destroy_cq(devr->c0);
  3958. mlx5_ib_dealloc_pd(devr->p0);
  3959. /* Make sure no change P_Key work items are still executing */
  3960. for (port = 0; port < dev->num_ports; ++port)
  3961. cancel_work_sync(&devr->ports[port].pkey_change_work);
  3962. }
  3963. static u32 get_core_cap_flags(struct ib_device *ibdev,
  3964. struct mlx5_hca_vport_context *rep)
  3965. {
  3966. struct mlx5_ib_dev *dev = to_mdev(ibdev);
  3967. enum rdma_link_layer ll = mlx5_ib_port_link_layer(ibdev, 1);
  3968. u8 l3_type_cap = MLX5_CAP_ROCE(dev->mdev, l3_type);
  3969. u8 roce_version_cap = MLX5_CAP_ROCE(dev->mdev, roce_version);
  3970. bool raw_support = !mlx5_core_mp_enabled(dev->mdev);
  3971. u32 ret = 0;
  3972. if (rep->grh_required)
  3973. ret |= RDMA_CORE_CAP_IB_GRH_REQUIRED;
  3974. if (ll == IB_LINK_LAYER_INFINIBAND)
  3975. return ret | RDMA_CORE_PORT_IBA_IB;
  3976. if (raw_support)
  3977. ret |= RDMA_CORE_PORT_RAW_PACKET;
  3978. if (!(l3_type_cap & MLX5_ROCE_L3_TYPE_IPV4_CAP))
  3979. return ret;
  3980. if (!(l3_type_cap & MLX5_ROCE_L3_TYPE_IPV6_CAP))
  3981. return ret;
  3982. if (roce_version_cap & MLX5_ROCE_VERSION_1_CAP)
  3983. ret |= RDMA_CORE_PORT_IBA_ROCE;
  3984. if (roce_version_cap & MLX5_ROCE_VERSION_2_CAP)
  3985. ret |= RDMA_CORE_PORT_IBA_ROCE_UDP_ENCAP;
  3986. return ret;
  3987. }
  3988. static int mlx5_port_immutable(struct ib_device *ibdev, u8 port_num,
  3989. struct ib_port_immutable *immutable)
  3990. {
  3991. struct ib_port_attr attr;
  3992. struct mlx5_ib_dev *dev = to_mdev(ibdev);
  3993. enum rdma_link_layer ll = mlx5_ib_port_link_layer(ibdev, port_num);
  3994. struct mlx5_hca_vport_context rep = {0};
  3995. int err;
  3996. err = ib_query_port(ibdev, port_num, &attr);
  3997. if (err)
  3998. return err;
  3999. if (ll == IB_LINK_LAYER_INFINIBAND) {
  4000. err = mlx5_query_hca_vport_context(dev->mdev, 0, port_num, 0,
  4001. &rep);
  4002. if (err)
  4003. return err;
  4004. }
  4005. immutable->pkey_tbl_len = attr.pkey_tbl_len;
  4006. immutable->gid_tbl_len = attr.gid_tbl_len;
  4007. immutable->core_cap_flags = get_core_cap_flags(ibdev, &rep);
  4008. if ((ll == IB_LINK_LAYER_INFINIBAND) || MLX5_CAP_GEN(dev->mdev, roce))
  4009. immutable->max_mad_size = IB_MGMT_MAD_SIZE;
  4010. return 0;
  4011. }
  4012. static int mlx5_port_rep_immutable(struct ib_device *ibdev, u8 port_num,
  4013. struct ib_port_immutable *immutable)
  4014. {
  4015. struct ib_port_attr attr;
  4016. int err;
  4017. immutable->core_cap_flags = RDMA_CORE_PORT_RAW_PACKET;
  4018. err = ib_query_port(ibdev, port_num, &attr);
  4019. if (err)
  4020. return err;
  4021. immutable->pkey_tbl_len = attr.pkey_tbl_len;
  4022. immutable->gid_tbl_len = attr.gid_tbl_len;
  4023. immutable->core_cap_flags = RDMA_CORE_PORT_RAW_PACKET;
  4024. return 0;
  4025. }
  4026. static void get_dev_fw_str(struct ib_device *ibdev, char *str)
  4027. {
  4028. struct mlx5_ib_dev *dev =
  4029. container_of(ibdev, struct mlx5_ib_dev, ib_dev);
  4030. snprintf(str, IB_FW_VERSION_NAME_MAX, "%d.%d.%04d",
  4031. fw_rev_maj(dev->mdev), fw_rev_min(dev->mdev),
  4032. fw_rev_sub(dev->mdev));
  4033. }
  4034. static int mlx5_eth_lag_init(struct mlx5_ib_dev *dev)
  4035. {
  4036. struct mlx5_core_dev *mdev = dev->mdev;
  4037. struct mlx5_flow_namespace *ns = mlx5_get_flow_namespace(mdev,
  4038. MLX5_FLOW_NAMESPACE_LAG);
  4039. struct mlx5_flow_table *ft;
  4040. int err;
  4041. if (!ns || !mlx5_lag_is_active(mdev))
  4042. return 0;
  4043. err = mlx5_cmd_create_vport_lag(mdev);
  4044. if (err)
  4045. return err;
  4046. ft = mlx5_create_lag_demux_flow_table(ns, 0, 0);
  4047. if (IS_ERR(ft)) {
  4048. err = PTR_ERR(ft);
  4049. goto err_destroy_vport_lag;
  4050. }
  4051. dev->flow_db->lag_demux_ft = ft;
  4052. return 0;
  4053. err_destroy_vport_lag:
  4054. mlx5_cmd_destroy_vport_lag(mdev);
  4055. return err;
  4056. }
  4057. static void mlx5_eth_lag_cleanup(struct mlx5_ib_dev *dev)
  4058. {
  4059. struct mlx5_core_dev *mdev = dev->mdev;
  4060. if (dev->flow_db->lag_demux_ft) {
  4061. mlx5_destroy_flow_table(dev->flow_db->lag_demux_ft);
  4062. dev->flow_db->lag_demux_ft = NULL;
  4063. mlx5_cmd_destroy_vport_lag(mdev);
  4064. }
  4065. }
  4066. static int mlx5_add_netdev_notifier(struct mlx5_ib_dev *dev, u8 port_num)
  4067. {
  4068. int err;
  4069. dev->roce[port_num].nb.notifier_call = mlx5_netdev_event;
  4070. err = register_netdevice_notifier(&dev->roce[port_num].nb);
  4071. if (err) {
  4072. dev->roce[port_num].nb.notifier_call = NULL;
  4073. return err;
  4074. }
  4075. return 0;
  4076. }
  4077. static void mlx5_remove_netdev_notifier(struct mlx5_ib_dev *dev, u8 port_num)
  4078. {
  4079. if (dev->roce[port_num].nb.notifier_call) {
  4080. unregister_netdevice_notifier(&dev->roce[port_num].nb);
  4081. dev->roce[port_num].nb.notifier_call = NULL;
  4082. }
  4083. }
  4084. static int mlx5_enable_eth(struct mlx5_ib_dev *dev)
  4085. {
  4086. int err;
  4087. if (MLX5_CAP_GEN(dev->mdev, roce)) {
  4088. err = mlx5_nic_vport_enable_roce(dev->mdev);
  4089. if (err)
  4090. return err;
  4091. }
  4092. err = mlx5_eth_lag_init(dev);
  4093. if (err)
  4094. goto err_disable_roce;
  4095. return 0;
  4096. err_disable_roce:
  4097. if (MLX5_CAP_GEN(dev->mdev, roce))
  4098. mlx5_nic_vport_disable_roce(dev->mdev);
  4099. return err;
  4100. }
  4101. static void mlx5_disable_eth(struct mlx5_ib_dev *dev)
  4102. {
  4103. mlx5_eth_lag_cleanup(dev);
  4104. if (MLX5_CAP_GEN(dev->mdev, roce))
  4105. mlx5_nic_vport_disable_roce(dev->mdev);
  4106. }
  4107. struct mlx5_ib_counter {
  4108. const char *name;
  4109. size_t offset;
  4110. };
  4111. #define INIT_Q_COUNTER(_name) \
  4112. { .name = #_name, .offset = MLX5_BYTE_OFF(query_q_counter_out, _name)}
  4113. static const struct mlx5_ib_counter basic_q_cnts[] = {
  4114. INIT_Q_COUNTER(rx_write_requests),
  4115. INIT_Q_COUNTER(rx_read_requests),
  4116. INIT_Q_COUNTER(rx_atomic_requests),
  4117. INIT_Q_COUNTER(out_of_buffer),
  4118. };
  4119. static const struct mlx5_ib_counter out_of_seq_q_cnts[] = {
  4120. INIT_Q_COUNTER(out_of_sequence),
  4121. };
  4122. static const struct mlx5_ib_counter retrans_q_cnts[] = {
  4123. INIT_Q_COUNTER(duplicate_request),
  4124. INIT_Q_COUNTER(rnr_nak_retry_err),
  4125. INIT_Q_COUNTER(packet_seq_err),
  4126. INIT_Q_COUNTER(implied_nak_seq_err),
  4127. INIT_Q_COUNTER(local_ack_timeout_err),
  4128. };
  4129. #define INIT_CONG_COUNTER(_name) \
  4130. { .name = #_name, .offset = \
  4131. MLX5_BYTE_OFF(query_cong_statistics_out, _name ## _high)}
  4132. static const struct mlx5_ib_counter cong_cnts[] = {
  4133. INIT_CONG_COUNTER(rp_cnp_ignored),
  4134. INIT_CONG_COUNTER(rp_cnp_handled),
  4135. INIT_CONG_COUNTER(np_ecn_marked_roce_packets),
  4136. INIT_CONG_COUNTER(np_cnp_sent),
  4137. };
  4138. static const struct mlx5_ib_counter extended_err_cnts[] = {
  4139. INIT_Q_COUNTER(resp_local_length_error),
  4140. INIT_Q_COUNTER(resp_cqe_error),
  4141. INIT_Q_COUNTER(req_cqe_error),
  4142. INIT_Q_COUNTER(req_remote_invalid_request),
  4143. INIT_Q_COUNTER(req_remote_access_errors),
  4144. INIT_Q_COUNTER(resp_remote_access_errors),
  4145. INIT_Q_COUNTER(resp_cqe_flush_error),
  4146. INIT_Q_COUNTER(req_cqe_flush_error),
  4147. };
  4148. #define INIT_EXT_PPCNT_COUNTER(_name) \
  4149. { .name = #_name, .offset = \
  4150. MLX5_BYTE_OFF(ppcnt_reg, \
  4151. counter_set.eth_extended_cntrs_grp_data_layout._name##_high)}
  4152. static const struct mlx5_ib_counter ext_ppcnt_cnts[] = {
  4153. INIT_EXT_PPCNT_COUNTER(rx_icrc_encapsulated),
  4154. };
  4155. static void mlx5_ib_dealloc_counters(struct mlx5_ib_dev *dev)
  4156. {
  4157. int i;
  4158. for (i = 0; i < dev->num_ports; i++) {
  4159. if (dev->port[i].cnts.set_id_valid)
  4160. mlx5_core_dealloc_q_counter(dev->mdev,
  4161. dev->port[i].cnts.set_id);
  4162. kfree(dev->port[i].cnts.names);
  4163. kfree(dev->port[i].cnts.offsets);
  4164. }
  4165. }
  4166. static int __mlx5_ib_alloc_counters(struct mlx5_ib_dev *dev,
  4167. struct mlx5_ib_counters *cnts)
  4168. {
  4169. u32 num_counters;
  4170. num_counters = ARRAY_SIZE(basic_q_cnts);
  4171. if (MLX5_CAP_GEN(dev->mdev, out_of_seq_cnt))
  4172. num_counters += ARRAY_SIZE(out_of_seq_q_cnts);
  4173. if (MLX5_CAP_GEN(dev->mdev, retransmission_q_counters))
  4174. num_counters += ARRAY_SIZE(retrans_q_cnts);
  4175. if (MLX5_CAP_GEN(dev->mdev, enhanced_error_q_counters))
  4176. num_counters += ARRAY_SIZE(extended_err_cnts);
  4177. cnts->num_q_counters = num_counters;
  4178. if (MLX5_CAP_GEN(dev->mdev, cc_query_allowed)) {
  4179. cnts->num_cong_counters = ARRAY_SIZE(cong_cnts);
  4180. num_counters += ARRAY_SIZE(cong_cnts);
  4181. }
  4182. if (MLX5_CAP_PCAM_FEATURE(dev->mdev, rx_icrc_encapsulated_counter)) {
  4183. cnts->num_ext_ppcnt_counters = ARRAY_SIZE(ext_ppcnt_cnts);
  4184. num_counters += ARRAY_SIZE(ext_ppcnt_cnts);
  4185. }
  4186. cnts->names = kcalloc(num_counters, sizeof(cnts->names), GFP_KERNEL);
  4187. if (!cnts->names)
  4188. return -ENOMEM;
  4189. cnts->offsets = kcalloc(num_counters,
  4190. sizeof(cnts->offsets), GFP_KERNEL);
  4191. if (!cnts->offsets)
  4192. goto err_names;
  4193. return 0;
  4194. err_names:
  4195. kfree(cnts->names);
  4196. cnts->names = NULL;
  4197. return -ENOMEM;
  4198. }
  4199. static void mlx5_ib_fill_counters(struct mlx5_ib_dev *dev,
  4200. const char **names,
  4201. size_t *offsets)
  4202. {
  4203. int i;
  4204. int j = 0;
  4205. for (i = 0; i < ARRAY_SIZE(basic_q_cnts); i++, j++) {
  4206. names[j] = basic_q_cnts[i].name;
  4207. offsets[j] = basic_q_cnts[i].offset;
  4208. }
  4209. if (MLX5_CAP_GEN(dev->mdev, out_of_seq_cnt)) {
  4210. for (i = 0; i < ARRAY_SIZE(out_of_seq_q_cnts); i++, j++) {
  4211. names[j] = out_of_seq_q_cnts[i].name;
  4212. offsets[j] = out_of_seq_q_cnts[i].offset;
  4213. }
  4214. }
  4215. if (MLX5_CAP_GEN(dev->mdev, retransmission_q_counters)) {
  4216. for (i = 0; i < ARRAY_SIZE(retrans_q_cnts); i++, j++) {
  4217. names[j] = retrans_q_cnts[i].name;
  4218. offsets[j] = retrans_q_cnts[i].offset;
  4219. }
  4220. }
  4221. if (MLX5_CAP_GEN(dev->mdev, enhanced_error_q_counters)) {
  4222. for (i = 0; i < ARRAY_SIZE(extended_err_cnts); i++, j++) {
  4223. names[j] = extended_err_cnts[i].name;
  4224. offsets[j] = extended_err_cnts[i].offset;
  4225. }
  4226. }
  4227. if (MLX5_CAP_GEN(dev->mdev, cc_query_allowed)) {
  4228. for (i = 0; i < ARRAY_SIZE(cong_cnts); i++, j++) {
  4229. names[j] = cong_cnts[i].name;
  4230. offsets[j] = cong_cnts[i].offset;
  4231. }
  4232. }
  4233. if (MLX5_CAP_PCAM_FEATURE(dev->mdev, rx_icrc_encapsulated_counter)) {
  4234. for (i = 0; i < ARRAY_SIZE(ext_ppcnt_cnts); i++, j++) {
  4235. names[j] = ext_ppcnt_cnts[i].name;
  4236. offsets[j] = ext_ppcnt_cnts[i].offset;
  4237. }
  4238. }
  4239. }
  4240. static int mlx5_ib_alloc_counters(struct mlx5_ib_dev *dev)
  4241. {
  4242. int err = 0;
  4243. int i;
  4244. for (i = 0; i < dev->num_ports; i++) {
  4245. err = __mlx5_ib_alloc_counters(dev, &dev->port[i].cnts);
  4246. if (err)
  4247. goto err_alloc;
  4248. mlx5_ib_fill_counters(dev, dev->port[i].cnts.names,
  4249. dev->port[i].cnts.offsets);
  4250. err = mlx5_core_alloc_q_counter(dev->mdev,
  4251. &dev->port[i].cnts.set_id);
  4252. if (err) {
  4253. mlx5_ib_warn(dev,
  4254. "couldn't allocate queue counter for port %d, err %d\n",
  4255. i + 1, err);
  4256. goto err_alloc;
  4257. }
  4258. dev->port[i].cnts.set_id_valid = true;
  4259. }
  4260. return 0;
  4261. err_alloc:
  4262. mlx5_ib_dealloc_counters(dev);
  4263. return err;
  4264. }
  4265. static struct rdma_hw_stats *mlx5_ib_alloc_hw_stats(struct ib_device *ibdev,
  4266. u8 port_num)
  4267. {
  4268. struct mlx5_ib_dev *dev = to_mdev(ibdev);
  4269. struct mlx5_ib_port *port = &dev->port[port_num - 1];
  4270. /* We support only per port stats */
  4271. if (port_num == 0)
  4272. return NULL;
  4273. return rdma_alloc_hw_stats_struct(port->cnts.names,
  4274. port->cnts.num_q_counters +
  4275. port->cnts.num_cong_counters +
  4276. port->cnts.num_ext_ppcnt_counters,
  4277. RDMA_HW_STATS_DEFAULT_LIFESPAN);
  4278. }
  4279. static int mlx5_ib_query_q_counters(struct mlx5_core_dev *mdev,
  4280. struct mlx5_ib_port *port,
  4281. struct rdma_hw_stats *stats)
  4282. {
  4283. int outlen = MLX5_ST_SZ_BYTES(query_q_counter_out);
  4284. void *out;
  4285. __be32 val;
  4286. int ret, i;
  4287. out = kvzalloc(outlen, GFP_KERNEL);
  4288. if (!out)
  4289. return -ENOMEM;
  4290. ret = mlx5_core_query_q_counter(mdev,
  4291. port->cnts.set_id, 0,
  4292. out, outlen);
  4293. if (ret)
  4294. goto free;
  4295. for (i = 0; i < port->cnts.num_q_counters; i++) {
  4296. val = *(__be32 *)(out + port->cnts.offsets[i]);
  4297. stats->value[i] = (u64)be32_to_cpu(val);
  4298. }
  4299. free:
  4300. kvfree(out);
  4301. return ret;
  4302. }
  4303. static int mlx5_ib_query_ext_ppcnt_counters(struct mlx5_ib_dev *dev,
  4304. struct mlx5_ib_port *port,
  4305. struct rdma_hw_stats *stats)
  4306. {
  4307. int offset = port->cnts.num_q_counters + port->cnts.num_cong_counters;
  4308. int sz = MLX5_ST_SZ_BYTES(ppcnt_reg);
  4309. int ret, i;
  4310. void *out;
  4311. out = kvzalloc(sz, GFP_KERNEL);
  4312. if (!out)
  4313. return -ENOMEM;
  4314. ret = mlx5_cmd_query_ext_ppcnt_counters(dev->mdev, out);
  4315. if (ret)
  4316. goto free;
  4317. for (i = 0; i < port->cnts.num_ext_ppcnt_counters; i++) {
  4318. stats->value[i + offset] =
  4319. be64_to_cpup((__be64 *)(out +
  4320. port->cnts.offsets[i + offset]));
  4321. }
  4322. free:
  4323. kvfree(out);
  4324. return ret;
  4325. }
  4326. static int mlx5_ib_get_hw_stats(struct ib_device *ibdev,
  4327. struct rdma_hw_stats *stats,
  4328. u8 port_num, int index)
  4329. {
  4330. struct mlx5_ib_dev *dev = to_mdev(ibdev);
  4331. struct mlx5_ib_port *port = &dev->port[port_num - 1];
  4332. struct mlx5_core_dev *mdev;
  4333. int ret, num_counters;
  4334. u8 mdev_port_num;
  4335. if (!stats)
  4336. return -EINVAL;
  4337. num_counters = port->cnts.num_q_counters +
  4338. port->cnts.num_cong_counters +
  4339. port->cnts.num_ext_ppcnt_counters;
  4340. /* q_counters are per IB device, query the master mdev */
  4341. ret = mlx5_ib_query_q_counters(dev->mdev, port, stats);
  4342. if (ret)
  4343. return ret;
  4344. if (MLX5_CAP_PCAM_FEATURE(dev->mdev, rx_icrc_encapsulated_counter)) {
  4345. ret = mlx5_ib_query_ext_ppcnt_counters(dev, port, stats);
  4346. if (ret)
  4347. return ret;
  4348. }
  4349. if (MLX5_CAP_GEN(dev->mdev, cc_query_allowed)) {
  4350. mdev = mlx5_ib_get_native_port_mdev(dev, port_num,
  4351. &mdev_port_num);
  4352. if (!mdev) {
  4353. /* If port is not affiliated yet, its in down state
  4354. * which doesn't have any counters yet, so it would be
  4355. * zero. So no need to read from the HCA.
  4356. */
  4357. goto done;
  4358. }
  4359. ret = mlx5_lag_query_cong_counters(dev->mdev,
  4360. stats->value +
  4361. port->cnts.num_q_counters,
  4362. port->cnts.num_cong_counters,
  4363. port->cnts.offsets +
  4364. port->cnts.num_q_counters);
  4365. mlx5_ib_put_native_port_mdev(dev, port_num);
  4366. if (ret)
  4367. return ret;
  4368. }
  4369. done:
  4370. return num_counters;
  4371. }
  4372. static struct net_device*
  4373. mlx5_ib_alloc_rdma_netdev(struct ib_device *hca,
  4374. u8 port_num,
  4375. enum rdma_netdev_t type,
  4376. const char *name,
  4377. unsigned char name_assign_type,
  4378. void (*setup)(struct net_device *))
  4379. {
  4380. struct net_device *netdev;
  4381. if (type != RDMA_NETDEV_IPOIB)
  4382. return ERR_PTR(-EOPNOTSUPP);
  4383. netdev = mlx5_rdma_netdev_alloc(to_mdev(hca)->mdev, hca,
  4384. name, setup);
  4385. return netdev;
  4386. }
  4387. static void delay_drop_debugfs_cleanup(struct mlx5_ib_dev *dev)
  4388. {
  4389. if (!dev->delay_drop.dbg)
  4390. return;
  4391. debugfs_remove_recursive(dev->delay_drop.dbg->dir_debugfs);
  4392. kfree(dev->delay_drop.dbg);
  4393. dev->delay_drop.dbg = NULL;
  4394. }
  4395. static void cancel_delay_drop(struct mlx5_ib_dev *dev)
  4396. {
  4397. if (!(dev->ib_dev.attrs.raw_packet_caps & IB_RAW_PACKET_CAP_DELAY_DROP))
  4398. return;
  4399. cancel_work_sync(&dev->delay_drop.delay_drop_work);
  4400. delay_drop_debugfs_cleanup(dev);
  4401. }
  4402. static ssize_t delay_drop_timeout_read(struct file *filp, char __user *buf,
  4403. size_t count, loff_t *pos)
  4404. {
  4405. struct mlx5_ib_delay_drop *delay_drop = filp->private_data;
  4406. char lbuf[20];
  4407. int len;
  4408. len = snprintf(lbuf, sizeof(lbuf), "%u\n", delay_drop->timeout);
  4409. return simple_read_from_buffer(buf, count, pos, lbuf, len);
  4410. }
  4411. static ssize_t delay_drop_timeout_write(struct file *filp, const char __user *buf,
  4412. size_t count, loff_t *pos)
  4413. {
  4414. struct mlx5_ib_delay_drop *delay_drop = filp->private_data;
  4415. u32 timeout;
  4416. u32 var;
  4417. if (kstrtouint_from_user(buf, count, 0, &var))
  4418. return -EFAULT;
  4419. timeout = min_t(u32, roundup(var, 100), MLX5_MAX_DELAY_DROP_TIMEOUT_MS *
  4420. 1000);
  4421. if (timeout != var)
  4422. mlx5_ib_dbg(delay_drop->dev, "Round delay drop timeout to %u usec\n",
  4423. timeout);
  4424. delay_drop->timeout = timeout;
  4425. return count;
  4426. }
  4427. static const struct file_operations fops_delay_drop_timeout = {
  4428. .owner = THIS_MODULE,
  4429. .open = simple_open,
  4430. .write = delay_drop_timeout_write,
  4431. .read = delay_drop_timeout_read,
  4432. };
  4433. static int delay_drop_debugfs_init(struct mlx5_ib_dev *dev)
  4434. {
  4435. struct mlx5_ib_dbg_delay_drop *dbg;
  4436. if (!mlx5_debugfs_root)
  4437. return 0;
  4438. dbg = kzalloc(sizeof(*dbg), GFP_KERNEL);
  4439. if (!dbg)
  4440. return -ENOMEM;
  4441. dev->delay_drop.dbg = dbg;
  4442. dbg->dir_debugfs =
  4443. debugfs_create_dir("delay_drop",
  4444. dev->mdev->priv.dbg_root);
  4445. if (!dbg->dir_debugfs)
  4446. goto out_debugfs;
  4447. dbg->events_cnt_debugfs =
  4448. debugfs_create_atomic_t("num_timeout_events", 0400,
  4449. dbg->dir_debugfs,
  4450. &dev->delay_drop.events_cnt);
  4451. if (!dbg->events_cnt_debugfs)
  4452. goto out_debugfs;
  4453. dbg->rqs_cnt_debugfs =
  4454. debugfs_create_atomic_t("num_rqs", 0400,
  4455. dbg->dir_debugfs,
  4456. &dev->delay_drop.rqs_cnt);
  4457. if (!dbg->rqs_cnt_debugfs)
  4458. goto out_debugfs;
  4459. dbg->timeout_debugfs =
  4460. debugfs_create_file("timeout", 0600,
  4461. dbg->dir_debugfs,
  4462. &dev->delay_drop,
  4463. &fops_delay_drop_timeout);
  4464. if (!dbg->timeout_debugfs)
  4465. goto out_debugfs;
  4466. return 0;
  4467. out_debugfs:
  4468. delay_drop_debugfs_cleanup(dev);
  4469. return -ENOMEM;
  4470. }
  4471. static void init_delay_drop(struct mlx5_ib_dev *dev)
  4472. {
  4473. if (!(dev->ib_dev.attrs.raw_packet_caps & IB_RAW_PACKET_CAP_DELAY_DROP))
  4474. return;
  4475. mutex_init(&dev->delay_drop.lock);
  4476. dev->delay_drop.dev = dev;
  4477. dev->delay_drop.activate = false;
  4478. dev->delay_drop.timeout = MLX5_MAX_DELAY_DROP_TIMEOUT_MS * 1000;
  4479. INIT_WORK(&dev->delay_drop.delay_drop_work, delay_drop_handler);
  4480. atomic_set(&dev->delay_drop.rqs_cnt, 0);
  4481. atomic_set(&dev->delay_drop.events_cnt, 0);
  4482. if (delay_drop_debugfs_init(dev))
  4483. mlx5_ib_warn(dev, "Failed to init delay drop debugfs\n");
  4484. }
  4485. static const struct cpumask *
  4486. mlx5_ib_get_vector_affinity(struct ib_device *ibdev, int comp_vector)
  4487. {
  4488. struct mlx5_ib_dev *dev = to_mdev(ibdev);
  4489. return mlx5_get_vector_affinity_hint(dev->mdev, comp_vector);
  4490. }
  4491. /* The mlx5_ib_multiport_mutex should be held when calling this function */
  4492. static void mlx5_ib_unbind_slave_port(struct mlx5_ib_dev *ibdev,
  4493. struct mlx5_ib_multiport_info *mpi)
  4494. {
  4495. u8 port_num = mlx5_core_native_port_num(mpi->mdev) - 1;
  4496. struct mlx5_ib_port *port = &ibdev->port[port_num];
  4497. int comps;
  4498. int err;
  4499. int i;
  4500. mlx5_ib_cleanup_cong_debugfs(ibdev, port_num);
  4501. spin_lock(&port->mp.mpi_lock);
  4502. if (!mpi->ibdev) {
  4503. spin_unlock(&port->mp.mpi_lock);
  4504. return;
  4505. }
  4506. mpi->ibdev = NULL;
  4507. spin_unlock(&port->mp.mpi_lock);
  4508. mlx5_remove_netdev_notifier(ibdev, port_num);
  4509. spin_lock(&port->mp.mpi_lock);
  4510. comps = mpi->mdev_refcnt;
  4511. if (comps) {
  4512. mpi->unaffiliate = true;
  4513. init_completion(&mpi->unref_comp);
  4514. spin_unlock(&port->mp.mpi_lock);
  4515. for (i = 0; i < comps; i++)
  4516. wait_for_completion(&mpi->unref_comp);
  4517. spin_lock(&port->mp.mpi_lock);
  4518. mpi->unaffiliate = false;
  4519. }
  4520. port->mp.mpi = NULL;
  4521. list_add_tail(&mpi->list, &mlx5_ib_unaffiliated_port_list);
  4522. spin_unlock(&port->mp.mpi_lock);
  4523. err = mlx5_nic_vport_unaffiliate_multiport(mpi->mdev);
  4524. mlx5_ib_dbg(ibdev, "unaffiliated port %d\n", port_num + 1);
  4525. /* Log an error, still needed to cleanup the pointers and add
  4526. * it back to the list.
  4527. */
  4528. if (err)
  4529. mlx5_ib_err(ibdev, "Failed to unaffiliate port %u\n",
  4530. port_num + 1);
  4531. ibdev->roce[port_num].last_port_state = IB_PORT_DOWN;
  4532. }
  4533. /* The mlx5_ib_multiport_mutex should be held when calling this function */
  4534. static bool mlx5_ib_bind_slave_port(struct mlx5_ib_dev *ibdev,
  4535. struct mlx5_ib_multiport_info *mpi)
  4536. {
  4537. u8 port_num = mlx5_core_native_port_num(mpi->mdev) - 1;
  4538. int err;
  4539. spin_lock(&ibdev->port[port_num].mp.mpi_lock);
  4540. if (ibdev->port[port_num].mp.mpi) {
  4541. mlx5_ib_dbg(ibdev, "port %d already affiliated.\n",
  4542. port_num + 1);
  4543. spin_unlock(&ibdev->port[port_num].mp.mpi_lock);
  4544. return false;
  4545. }
  4546. ibdev->port[port_num].mp.mpi = mpi;
  4547. mpi->ibdev = ibdev;
  4548. spin_unlock(&ibdev->port[port_num].mp.mpi_lock);
  4549. err = mlx5_nic_vport_affiliate_multiport(ibdev->mdev, mpi->mdev);
  4550. if (err)
  4551. goto unbind;
  4552. err = get_port_caps(ibdev, mlx5_core_native_port_num(mpi->mdev));
  4553. if (err)
  4554. goto unbind;
  4555. err = mlx5_add_netdev_notifier(ibdev, port_num);
  4556. if (err) {
  4557. mlx5_ib_err(ibdev, "failed adding netdev notifier for port %u\n",
  4558. port_num + 1);
  4559. goto unbind;
  4560. }
  4561. err = mlx5_ib_init_cong_debugfs(ibdev, port_num);
  4562. if (err)
  4563. goto unbind;
  4564. return true;
  4565. unbind:
  4566. mlx5_ib_unbind_slave_port(ibdev, mpi);
  4567. return false;
  4568. }
  4569. static int mlx5_ib_init_multiport_master(struct mlx5_ib_dev *dev)
  4570. {
  4571. int port_num = mlx5_core_native_port_num(dev->mdev) - 1;
  4572. enum rdma_link_layer ll = mlx5_ib_port_link_layer(&dev->ib_dev,
  4573. port_num + 1);
  4574. struct mlx5_ib_multiport_info *mpi;
  4575. int err;
  4576. int i;
  4577. if (!mlx5_core_is_mp_master(dev->mdev) || ll != IB_LINK_LAYER_ETHERNET)
  4578. return 0;
  4579. err = mlx5_query_nic_vport_system_image_guid(dev->mdev,
  4580. &dev->sys_image_guid);
  4581. if (err)
  4582. return err;
  4583. err = mlx5_nic_vport_enable_roce(dev->mdev);
  4584. if (err)
  4585. return err;
  4586. mutex_lock(&mlx5_ib_multiport_mutex);
  4587. for (i = 0; i < dev->num_ports; i++) {
  4588. bool bound = false;
  4589. /* build a stub multiport info struct for the native port. */
  4590. if (i == port_num) {
  4591. mpi = kzalloc(sizeof(*mpi), GFP_KERNEL);
  4592. if (!mpi) {
  4593. mutex_unlock(&mlx5_ib_multiport_mutex);
  4594. mlx5_nic_vport_disable_roce(dev->mdev);
  4595. return -ENOMEM;
  4596. }
  4597. mpi->is_master = true;
  4598. mpi->mdev = dev->mdev;
  4599. mpi->sys_image_guid = dev->sys_image_guid;
  4600. dev->port[i].mp.mpi = mpi;
  4601. mpi->ibdev = dev;
  4602. mpi = NULL;
  4603. continue;
  4604. }
  4605. list_for_each_entry(mpi, &mlx5_ib_unaffiliated_port_list,
  4606. list) {
  4607. if (dev->sys_image_guid == mpi->sys_image_guid &&
  4608. (mlx5_core_native_port_num(mpi->mdev) - 1) == i) {
  4609. bound = mlx5_ib_bind_slave_port(dev, mpi);
  4610. }
  4611. if (bound) {
  4612. dev_dbg(&mpi->mdev->pdev->dev, "removing port from unaffiliated list.\n");
  4613. mlx5_ib_dbg(dev, "port %d bound\n", i + 1);
  4614. list_del(&mpi->list);
  4615. break;
  4616. }
  4617. }
  4618. if (!bound) {
  4619. get_port_caps(dev, i + 1);
  4620. mlx5_ib_dbg(dev, "no free port found for port %d\n",
  4621. i + 1);
  4622. }
  4623. }
  4624. list_add_tail(&dev->ib_dev_list, &mlx5_ib_dev_list);
  4625. mutex_unlock(&mlx5_ib_multiport_mutex);
  4626. return err;
  4627. }
  4628. static void mlx5_ib_cleanup_multiport_master(struct mlx5_ib_dev *dev)
  4629. {
  4630. int port_num = mlx5_core_native_port_num(dev->mdev) - 1;
  4631. enum rdma_link_layer ll = mlx5_ib_port_link_layer(&dev->ib_dev,
  4632. port_num + 1);
  4633. int i;
  4634. if (!mlx5_core_is_mp_master(dev->mdev) || ll != IB_LINK_LAYER_ETHERNET)
  4635. return;
  4636. mutex_lock(&mlx5_ib_multiport_mutex);
  4637. for (i = 0; i < dev->num_ports; i++) {
  4638. if (dev->port[i].mp.mpi) {
  4639. /* Destroy the native port stub */
  4640. if (i == port_num) {
  4641. kfree(dev->port[i].mp.mpi);
  4642. dev->port[i].mp.mpi = NULL;
  4643. } else {
  4644. mlx5_ib_dbg(dev, "unbinding port_num: %d\n", i + 1);
  4645. mlx5_ib_unbind_slave_port(dev, dev->port[i].mp.mpi);
  4646. }
  4647. }
  4648. }
  4649. mlx5_ib_dbg(dev, "removing from devlist\n");
  4650. list_del(&dev->ib_dev_list);
  4651. mutex_unlock(&mlx5_ib_multiport_mutex);
  4652. mlx5_nic_vport_disable_roce(dev->mdev);
  4653. }
  4654. ADD_UVERBS_ATTRIBUTES_SIMPLE(
  4655. mlx5_ib_dm,
  4656. UVERBS_OBJECT_DM,
  4657. UVERBS_METHOD_DM_ALLOC,
  4658. UVERBS_ATTR_PTR_OUT(MLX5_IB_ATTR_ALLOC_DM_RESP_START_OFFSET,
  4659. UVERBS_ATTR_TYPE(u64),
  4660. UA_MANDATORY),
  4661. UVERBS_ATTR_PTR_OUT(MLX5_IB_ATTR_ALLOC_DM_RESP_PAGE_INDEX,
  4662. UVERBS_ATTR_TYPE(u16),
  4663. UA_MANDATORY));
  4664. ADD_UVERBS_ATTRIBUTES_SIMPLE(
  4665. mlx5_ib_flow_action,
  4666. UVERBS_OBJECT_FLOW_ACTION,
  4667. UVERBS_METHOD_FLOW_ACTION_ESP_CREATE,
  4668. UVERBS_ATTR_FLAGS_IN(MLX5_IB_ATTR_CREATE_FLOW_ACTION_FLAGS,
  4669. enum mlx5_ib_uapi_flow_action_flags));
  4670. static int populate_specs_root(struct mlx5_ib_dev *dev)
  4671. {
  4672. const struct uverbs_object_tree_def **trees = dev->driver_trees;
  4673. size_t num_trees = 0;
  4674. if (mlx5_accel_ipsec_device_caps(dev->mdev) &
  4675. MLX5_ACCEL_IPSEC_CAP_DEVICE)
  4676. trees[num_trees++] = &mlx5_ib_flow_action;
  4677. if (MLX5_CAP_DEV_MEM(dev->mdev, memic))
  4678. trees[num_trees++] = &mlx5_ib_dm;
  4679. if (MLX5_CAP_GEN_64(dev->mdev, general_obj_types) &
  4680. MLX5_GENERAL_OBJ_TYPES_CAP_UCTX)
  4681. trees[num_trees++] = mlx5_ib_get_devx_tree();
  4682. num_trees += mlx5_ib_get_flow_trees(trees + num_trees);
  4683. WARN_ON(num_trees >= ARRAY_SIZE(dev->driver_trees));
  4684. trees[num_trees] = NULL;
  4685. dev->ib_dev.driver_specs = trees;
  4686. return 0;
  4687. }
  4688. static int mlx5_ib_read_counters(struct ib_counters *counters,
  4689. struct ib_counters_read_attr *read_attr,
  4690. struct uverbs_attr_bundle *attrs)
  4691. {
  4692. struct mlx5_ib_mcounters *mcounters = to_mcounters(counters);
  4693. struct mlx5_read_counters_attr mread_attr = {};
  4694. struct mlx5_ib_flow_counters_desc *desc;
  4695. int ret, i;
  4696. mutex_lock(&mcounters->mcntrs_mutex);
  4697. if (mcounters->cntrs_max_index > read_attr->ncounters) {
  4698. ret = -EINVAL;
  4699. goto err_bound;
  4700. }
  4701. mread_attr.out = kcalloc(mcounters->counters_num, sizeof(u64),
  4702. GFP_KERNEL);
  4703. if (!mread_attr.out) {
  4704. ret = -ENOMEM;
  4705. goto err_bound;
  4706. }
  4707. mread_attr.hw_cntrs_hndl = mcounters->hw_cntrs_hndl;
  4708. mread_attr.flags = read_attr->flags;
  4709. ret = mcounters->read_counters(counters->device, &mread_attr);
  4710. if (ret)
  4711. goto err_read;
  4712. /* do the pass over the counters data array to assign according to the
  4713. * descriptions and indexing pairs
  4714. */
  4715. desc = mcounters->counters_data;
  4716. for (i = 0; i < mcounters->ncounters; i++)
  4717. read_attr->counters_buff[desc[i].index] += mread_attr.out[desc[i].description];
  4718. err_read:
  4719. kfree(mread_attr.out);
  4720. err_bound:
  4721. mutex_unlock(&mcounters->mcntrs_mutex);
  4722. return ret;
  4723. }
  4724. static int mlx5_ib_destroy_counters(struct ib_counters *counters)
  4725. {
  4726. struct mlx5_ib_mcounters *mcounters = to_mcounters(counters);
  4727. counters_clear_description(counters);
  4728. if (mcounters->hw_cntrs_hndl)
  4729. mlx5_fc_destroy(to_mdev(counters->device)->mdev,
  4730. mcounters->hw_cntrs_hndl);
  4731. kfree(mcounters);
  4732. return 0;
  4733. }
  4734. static struct ib_counters *mlx5_ib_create_counters(struct ib_device *device,
  4735. struct uverbs_attr_bundle *attrs)
  4736. {
  4737. struct mlx5_ib_mcounters *mcounters;
  4738. mcounters = kzalloc(sizeof(*mcounters), GFP_KERNEL);
  4739. if (!mcounters)
  4740. return ERR_PTR(-ENOMEM);
  4741. mutex_init(&mcounters->mcntrs_mutex);
  4742. return &mcounters->ibcntrs;
  4743. }
  4744. void mlx5_ib_stage_init_cleanup(struct mlx5_ib_dev *dev)
  4745. {
  4746. mlx5_ib_cleanup_multiport_master(dev);
  4747. #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
  4748. cleanup_srcu_struct(&dev->mr_srcu);
  4749. #endif
  4750. kfree(dev->port);
  4751. }
  4752. int mlx5_ib_stage_init_init(struct mlx5_ib_dev *dev)
  4753. {
  4754. struct mlx5_core_dev *mdev = dev->mdev;
  4755. const char *name;
  4756. int err;
  4757. int i;
  4758. dev->port = kcalloc(dev->num_ports, sizeof(*dev->port),
  4759. GFP_KERNEL);
  4760. if (!dev->port)
  4761. return -ENOMEM;
  4762. for (i = 0; i < dev->num_ports; i++) {
  4763. spin_lock_init(&dev->port[i].mp.mpi_lock);
  4764. rwlock_init(&dev->roce[i].netdev_lock);
  4765. }
  4766. err = mlx5_ib_init_multiport_master(dev);
  4767. if (err)
  4768. goto err_free_port;
  4769. if (!mlx5_core_mp_enabled(mdev)) {
  4770. for (i = 1; i <= dev->num_ports; i++) {
  4771. err = get_port_caps(dev, i);
  4772. if (err)
  4773. break;
  4774. }
  4775. } else {
  4776. err = get_port_caps(dev, mlx5_core_native_port_num(mdev));
  4777. }
  4778. if (err)
  4779. goto err_mp;
  4780. if (mlx5_use_mad_ifc(dev))
  4781. get_ext_port_caps(dev);
  4782. if (!mlx5_lag_is_active(mdev))
  4783. name = "mlx5_%d";
  4784. else
  4785. name = "mlx5_bond_%d";
  4786. strlcpy(dev->ib_dev.name, name, IB_DEVICE_NAME_MAX);
  4787. dev->ib_dev.owner = THIS_MODULE;
  4788. dev->ib_dev.node_type = RDMA_NODE_IB_CA;
  4789. dev->ib_dev.local_dma_lkey = 0 /* not supported for now */;
  4790. dev->ib_dev.phys_port_cnt = dev->num_ports;
  4791. dev->ib_dev.num_comp_vectors =
  4792. dev->mdev->priv.eq_table.num_comp_vectors;
  4793. dev->ib_dev.dev.parent = &mdev->pdev->dev;
  4794. mutex_init(&dev->cap_mask_mutex);
  4795. INIT_LIST_HEAD(&dev->qp_list);
  4796. spin_lock_init(&dev->reset_flow_resource_lock);
  4797. spin_lock_init(&dev->memic.memic_lock);
  4798. dev->memic.dev = mdev;
  4799. #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
  4800. err = init_srcu_struct(&dev->mr_srcu);
  4801. if (err)
  4802. goto err_free_port;
  4803. #endif
  4804. return 0;
  4805. err_mp:
  4806. mlx5_ib_cleanup_multiport_master(dev);
  4807. err_free_port:
  4808. kfree(dev->port);
  4809. return -ENOMEM;
  4810. }
  4811. static int mlx5_ib_stage_flow_db_init(struct mlx5_ib_dev *dev)
  4812. {
  4813. dev->flow_db = kzalloc(sizeof(*dev->flow_db), GFP_KERNEL);
  4814. if (!dev->flow_db)
  4815. return -ENOMEM;
  4816. mutex_init(&dev->flow_db->lock);
  4817. return 0;
  4818. }
  4819. int mlx5_ib_stage_rep_flow_db_init(struct mlx5_ib_dev *dev)
  4820. {
  4821. struct mlx5_ib_dev *nic_dev;
  4822. nic_dev = mlx5_ib_get_uplink_ibdev(dev->mdev->priv.eswitch);
  4823. if (!nic_dev)
  4824. return -EINVAL;
  4825. dev->flow_db = nic_dev->flow_db;
  4826. return 0;
  4827. }
  4828. static void mlx5_ib_stage_flow_db_cleanup(struct mlx5_ib_dev *dev)
  4829. {
  4830. kfree(dev->flow_db);
  4831. }
  4832. int mlx5_ib_stage_caps_init(struct mlx5_ib_dev *dev)
  4833. {
  4834. struct mlx5_core_dev *mdev = dev->mdev;
  4835. int err;
  4836. dev->ib_dev.uverbs_abi_ver = MLX5_IB_UVERBS_ABI_VERSION;
  4837. dev->ib_dev.uverbs_cmd_mask =
  4838. (1ull << IB_USER_VERBS_CMD_GET_CONTEXT) |
  4839. (1ull << IB_USER_VERBS_CMD_QUERY_DEVICE) |
  4840. (1ull << IB_USER_VERBS_CMD_QUERY_PORT) |
  4841. (1ull << IB_USER_VERBS_CMD_ALLOC_PD) |
  4842. (1ull << IB_USER_VERBS_CMD_DEALLOC_PD) |
  4843. (1ull << IB_USER_VERBS_CMD_CREATE_AH) |
  4844. (1ull << IB_USER_VERBS_CMD_DESTROY_AH) |
  4845. (1ull << IB_USER_VERBS_CMD_REG_MR) |
  4846. (1ull << IB_USER_VERBS_CMD_REREG_MR) |
  4847. (1ull << IB_USER_VERBS_CMD_DEREG_MR) |
  4848. (1ull << IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL) |
  4849. (1ull << IB_USER_VERBS_CMD_CREATE_CQ) |
  4850. (1ull << IB_USER_VERBS_CMD_RESIZE_CQ) |
  4851. (1ull << IB_USER_VERBS_CMD_DESTROY_CQ) |
  4852. (1ull << IB_USER_VERBS_CMD_CREATE_QP) |
  4853. (1ull << IB_USER_VERBS_CMD_MODIFY_QP) |
  4854. (1ull << IB_USER_VERBS_CMD_QUERY_QP) |
  4855. (1ull << IB_USER_VERBS_CMD_DESTROY_QP) |
  4856. (1ull << IB_USER_VERBS_CMD_ATTACH_MCAST) |
  4857. (1ull << IB_USER_VERBS_CMD_DETACH_MCAST) |
  4858. (1ull << IB_USER_VERBS_CMD_CREATE_SRQ) |
  4859. (1ull << IB_USER_VERBS_CMD_MODIFY_SRQ) |
  4860. (1ull << IB_USER_VERBS_CMD_QUERY_SRQ) |
  4861. (1ull << IB_USER_VERBS_CMD_DESTROY_SRQ) |
  4862. (1ull << IB_USER_VERBS_CMD_CREATE_XSRQ) |
  4863. (1ull << IB_USER_VERBS_CMD_OPEN_QP);
  4864. dev->ib_dev.uverbs_ex_cmd_mask =
  4865. (1ull << IB_USER_VERBS_EX_CMD_QUERY_DEVICE) |
  4866. (1ull << IB_USER_VERBS_EX_CMD_CREATE_CQ) |
  4867. (1ull << IB_USER_VERBS_EX_CMD_CREATE_QP) |
  4868. (1ull << IB_USER_VERBS_EX_CMD_MODIFY_QP) |
  4869. (1ull << IB_USER_VERBS_EX_CMD_MODIFY_CQ);
  4870. dev->ib_dev.query_device = mlx5_ib_query_device;
  4871. dev->ib_dev.get_link_layer = mlx5_ib_port_link_layer;
  4872. dev->ib_dev.query_gid = mlx5_ib_query_gid;
  4873. dev->ib_dev.add_gid = mlx5_ib_add_gid;
  4874. dev->ib_dev.del_gid = mlx5_ib_del_gid;
  4875. dev->ib_dev.query_pkey = mlx5_ib_query_pkey;
  4876. dev->ib_dev.modify_device = mlx5_ib_modify_device;
  4877. dev->ib_dev.modify_port = mlx5_ib_modify_port;
  4878. dev->ib_dev.alloc_ucontext = mlx5_ib_alloc_ucontext;
  4879. dev->ib_dev.dealloc_ucontext = mlx5_ib_dealloc_ucontext;
  4880. dev->ib_dev.mmap = mlx5_ib_mmap;
  4881. dev->ib_dev.alloc_pd = mlx5_ib_alloc_pd;
  4882. dev->ib_dev.dealloc_pd = mlx5_ib_dealloc_pd;
  4883. dev->ib_dev.create_ah = mlx5_ib_create_ah;
  4884. dev->ib_dev.query_ah = mlx5_ib_query_ah;
  4885. dev->ib_dev.destroy_ah = mlx5_ib_destroy_ah;
  4886. dev->ib_dev.create_srq = mlx5_ib_create_srq;
  4887. dev->ib_dev.modify_srq = mlx5_ib_modify_srq;
  4888. dev->ib_dev.query_srq = mlx5_ib_query_srq;
  4889. dev->ib_dev.destroy_srq = mlx5_ib_destroy_srq;
  4890. dev->ib_dev.post_srq_recv = mlx5_ib_post_srq_recv;
  4891. dev->ib_dev.create_qp = mlx5_ib_create_qp;
  4892. dev->ib_dev.modify_qp = mlx5_ib_modify_qp;
  4893. dev->ib_dev.query_qp = mlx5_ib_query_qp;
  4894. dev->ib_dev.destroy_qp = mlx5_ib_destroy_qp;
  4895. dev->ib_dev.drain_sq = mlx5_ib_drain_sq;
  4896. dev->ib_dev.drain_rq = mlx5_ib_drain_rq;
  4897. dev->ib_dev.post_send = mlx5_ib_post_send;
  4898. dev->ib_dev.post_recv = mlx5_ib_post_recv;
  4899. dev->ib_dev.create_cq = mlx5_ib_create_cq;
  4900. dev->ib_dev.modify_cq = mlx5_ib_modify_cq;
  4901. dev->ib_dev.resize_cq = mlx5_ib_resize_cq;
  4902. dev->ib_dev.destroy_cq = mlx5_ib_destroy_cq;
  4903. dev->ib_dev.poll_cq = mlx5_ib_poll_cq;
  4904. dev->ib_dev.req_notify_cq = mlx5_ib_arm_cq;
  4905. dev->ib_dev.get_dma_mr = mlx5_ib_get_dma_mr;
  4906. dev->ib_dev.reg_user_mr = mlx5_ib_reg_user_mr;
  4907. dev->ib_dev.rereg_user_mr = mlx5_ib_rereg_user_mr;
  4908. dev->ib_dev.dereg_mr = mlx5_ib_dereg_mr;
  4909. dev->ib_dev.attach_mcast = mlx5_ib_mcg_attach;
  4910. dev->ib_dev.detach_mcast = mlx5_ib_mcg_detach;
  4911. dev->ib_dev.process_mad = mlx5_ib_process_mad;
  4912. dev->ib_dev.alloc_mr = mlx5_ib_alloc_mr;
  4913. dev->ib_dev.map_mr_sg = mlx5_ib_map_mr_sg;
  4914. dev->ib_dev.check_mr_status = mlx5_ib_check_mr_status;
  4915. dev->ib_dev.get_dev_fw_str = get_dev_fw_str;
  4916. dev->ib_dev.get_vector_affinity = mlx5_ib_get_vector_affinity;
  4917. if (MLX5_CAP_GEN(mdev, ipoib_enhanced_offloads))
  4918. dev->ib_dev.alloc_rdma_netdev = mlx5_ib_alloc_rdma_netdev;
  4919. if (mlx5_core_is_pf(mdev)) {
  4920. dev->ib_dev.get_vf_config = mlx5_ib_get_vf_config;
  4921. dev->ib_dev.set_vf_link_state = mlx5_ib_set_vf_link_state;
  4922. dev->ib_dev.get_vf_stats = mlx5_ib_get_vf_stats;
  4923. dev->ib_dev.set_vf_guid = mlx5_ib_set_vf_guid;
  4924. }
  4925. dev->ib_dev.disassociate_ucontext = mlx5_ib_disassociate_ucontext;
  4926. dev->umr_fence = mlx5_get_umr_fence(MLX5_CAP_GEN(mdev, umr_fence));
  4927. if (MLX5_CAP_GEN(mdev, imaicl)) {
  4928. dev->ib_dev.alloc_mw = mlx5_ib_alloc_mw;
  4929. dev->ib_dev.dealloc_mw = mlx5_ib_dealloc_mw;
  4930. dev->ib_dev.uverbs_cmd_mask |=
  4931. (1ull << IB_USER_VERBS_CMD_ALLOC_MW) |
  4932. (1ull << IB_USER_VERBS_CMD_DEALLOC_MW);
  4933. }
  4934. if (MLX5_CAP_GEN(mdev, xrc)) {
  4935. dev->ib_dev.alloc_xrcd = mlx5_ib_alloc_xrcd;
  4936. dev->ib_dev.dealloc_xrcd = mlx5_ib_dealloc_xrcd;
  4937. dev->ib_dev.uverbs_cmd_mask |=
  4938. (1ull << IB_USER_VERBS_CMD_OPEN_XRCD) |
  4939. (1ull << IB_USER_VERBS_CMD_CLOSE_XRCD);
  4940. }
  4941. if (MLX5_CAP_DEV_MEM(mdev, memic)) {
  4942. dev->ib_dev.alloc_dm = mlx5_ib_alloc_dm;
  4943. dev->ib_dev.dealloc_dm = mlx5_ib_dealloc_dm;
  4944. dev->ib_dev.reg_dm_mr = mlx5_ib_reg_dm_mr;
  4945. }
  4946. dev->ib_dev.create_flow = mlx5_ib_create_flow;
  4947. dev->ib_dev.destroy_flow = mlx5_ib_destroy_flow;
  4948. dev->ib_dev.uverbs_ex_cmd_mask |=
  4949. (1ull << IB_USER_VERBS_EX_CMD_CREATE_FLOW) |
  4950. (1ull << IB_USER_VERBS_EX_CMD_DESTROY_FLOW);
  4951. dev->ib_dev.create_flow_action_esp = mlx5_ib_create_flow_action_esp;
  4952. dev->ib_dev.destroy_flow_action = mlx5_ib_destroy_flow_action;
  4953. dev->ib_dev.modify_flow_action_esp = mlx5_ib_modify_flow_action_esp;
  4954. dev->ib_dev.driver_id = RDMA_DRIVER_MLX5;
  4955. dev->ib_dev.create_counters = mlx5_ib_create_counters;
  4956. dev->ib_dev.destroy_counters = mlx5_ib_destroy_counters;
  4957. dev->ib_dev.read_counters = mlx5_ib_read_counters;
  4958. err = init_node_data(dev);
  4959. if (err)
  4960. return err;
  4961. if ((MLX5_CAP_GEN(dev->mdev, port_type) == MLX5_CAP_PORT_TYPE_ETH) &&
  4962. (MLX5_CAP_GEN(dev->mdev, disable_local_lb_uc) ||
  4963. MLX5_CAP_GEN(dev->mdev, disable_local_lb_mc)))
  4964. mutex_init(&dev->lb_mutex);
  4965. return 0;
  4966. }
  4967. static int mlx5_ib_stage_non_default_cb(struct mlx5_ib_dev *dev)
  4968. {
  4969. dev->ib_dev.get_port_immutable = mlx5_port_immutable;
  4970. dev->ib_dev.query_port = mlx5_ib_query_port;
  4971. return 0;
  4972. }
  4973. int mlx5_ib_stage_rep_non_default_cb(struct mlx5_ib_dev *dev)
  4974. {
  4975. dev->ib_dev.get_port_immutable = mlx5_port_rep_immutable;
  4976. dev->ib_dev.query_port = mlx5_ib_rep_query_port;
  4977. return 0;
  4978. }
  4979. static int mlx5_ib_stage_common_roce_init(struct mlx5_ib_dev *dev)
  4980. {
  4981. u8 port_num;
  4982. int i;
  4983. for (i = 0; i < dev->num_ports; i++) {
  4984. dev->roce[i].dev = dev;
  4985. dev->roce[i].native_port_num = i + 1;
  4986. dev->roce[i].last_port_state = IB_PORT_DOWN;
  4987. }
  4988. dev->ib_dev.get_netdev = mlx5_ib_get_netdev;
  4989. dev->ib_dev.create_wq = mlx5_ib_create_wq;
  4990. dev->ib_dev.modify_wq = mlx5_ib_modify_wq;
  4991. dev->ib_dev.destroy_wq = mlx5_ib_destroy_wq;
  4992. dev->ib_dev.create_rwq_ind_table = mlx5_ib_create_rwq_ind_table;
  4993. dev->ib_dev.destroy_rwq_ind_table = mlx5_ib_destroy_rwq_ind_table;
  4994. dev->ib_dev.uverbs_ex_cmd_mask |=
  4995. (1ull << IB_USER_VERBS_EX_CMD_CREATE_WQ) |
  4996. (1ull << IB_USER_VERBS_EX_CMD_MODIFY_WQ) |
  4997. (1ull << IB_USER_VERBS_EX_CMD_DESTROY_WQ) |
  4998. (1ull << IB_USER_VERBS_EX_CMD_CREATE_RWQ_IND_TBL) |
  4999. (1ull << IB_USER_VERBS_EX_CMD_DESTROY_RWQ_IND_TBL);
  5000. port_num = mlx5_core_native_port_num(dev->mdev) - 1;
  5001. return mlx5_add_netdev_notifier(dev, port_num);
  5002. }
  5003. static void mlx5_ib_stage_common_roce_cleanup(struct mlx5_ib_dev *dev)
  5004. {
  5005. u8 port_num = mlx5_core_native_port_num(dev->mdev) - 1;
  5006. mlx5_remove_netdev_notifier(dev, port_num);
  5007. }
  5008. int mlx5_ib_stage_rep_roce_init(struct mlx5_ib_dev *dev)
  5009. {
  5010. struct mlx5_core_dev *mdev = dev->mdev;
  5011. enum rdma_link_layer ll;
  5012. int port_type_cap;
  5013. int err = 0;
  5014. port_type_cap = MLX5_CAP_GEN(mdev, port_type);
  5015. ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
  5016. if (ll == IB_LINK_LAYER_ETHERNET)
  5017. err = mlx5_ib_stage_common_roce_init(dev);
  5018. return err;
  5019. }
  5020. void mlx5_ib_stage_rep_roce_cleanup(struct mlx5_ib_dev *dev)
  5021. {
  5022. mlx5_ib_stage_common_roce_cleanup(dev);
  5023. }
  5024. static int mlx5_ib_stage_roce_init(struct mlx5_ib_dev *dev)
  5025. {
  5026. struct mlx5_core_dev *mdev = dev->mdev;
  5027. enum rdma_link_layer ll;
  5028. int port_type_cap;
  5029. int err;
  5030. port_type_cap = MLX5_CAP_GEN(mdev, port_type);
  5031. ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
  5032. if (ll == IB_LINK_LAYER_ETHERNET) {
  5033. err = mlx5_ib_stage_common_roce_init(dev);
  5034. if (err)
  5035. return err;
  5036. err = mlx5_enable_eth(dev);
  5037. if (err)
  5038. goto cleanup;
  5039. }
  5040. return 0;
  5041. cleanup:
  5042. mlx5_ib_stage_common_roce_cleanup(dev);
  5043. return err;
  5044. }
  5045. static void mlx5_ib_stage_roce_cleanup(struct mlx5_ib_dev *dev)
  5046. {
  5047. struct mlx5_core_dev *mdev = dev->mdev;
  5048. enum rdma_link_layer ll;
  5049. int port_type_cap;
  5050. port_type_cap = MLX5_CAP_GEN(mdev, port_type);
  5051. ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
  5052. if (ll == IB_LINK_LAYER_ETHERNET) {
  5053. mlx5_disable_eth(dev);
  5054. mlx5_ib_stage_common_roce_cleanup(dev);
  5055. }
  5056. }
  5057. int mlx5_ib_stage_dev_res_init(struct mlx5_ib_dev *dev)
  5058. {
  5059. return create_dev_resources(&dev->devr);
  5060. }
  5061. void mlx5_ib_stage_dev_res_cleanup(struct mlx5_ib_dev *dev)
  5062. {
  5063. destroy_dev_resources(&dev->devr);
  5064. }
  5065. static int mlx5_ib_stage_odp_init(struct mlx5_ib_dev *dev)
  5066. {
  5067. mlx5_ib_internal_fill_odp_caps(dev);
  5068. return mlx5_ib_odp_init_one(dev);
  5069. }
  5070. int mlx5_ib_stage_counters_init(struct mlx5_ib_dev *dev)
  5071. {
  5072. if (MLX5_CAP_GEN(dev->mdev, max_qp_cnt)) {
  5073. dev->ib_dev.get_hw_stats = mlx5_ib_get_hw_stats;
  5074. dev->ib_dev.alloc_hw_stats = mlx5_ib_alloc_hw_stats;
  5075. return mlx5_ib_alloc_counters(dev);
  5076. }
  5077. return 0;
  5078. }
  5079. void mlx5_ib_stage_counters_cleanup(struct mlx5_ib_dev *dev)
  5080. {
  5081. if (MLX5_CAP_GEN(dev->mdev, max_qp_cnt))
  5082. mlx5_ib_dealloc_counters(dev);
  5083. }
  5084. static int mlx5_ib_stage_cong_debugfs_init(struct mlx5_ib_dev *dev)
  5085. {
  5086. return mlx5_ib_init_cong_debugfs(dev,
  5087. mlx5_core_native_port_num(dev->mdev) - 1);
  5088. }
  5089. static void mlx5_ib_stage_cong_debugfs_cleanup(struct mlx5_ib_dev *dev)
  5090. {
  5091. mlx5_ib_cleanup_cong_debugfs(dev,
  5092. mlx5_core_native_port_num(dev->mdev) - 1);
  5093. }
  5094. static int mlx5_ib_stage_uar_init(struct mlx5_ib_dev *dev)
  5095. {
  5096. dev->mdev->priv.uar = mlx5_get_uars_page(dev->mdev);
  5097. return PTR_ERR_OR_ZERO(dev->mdev->priv.uar);
  5098. }
  5099. static void mlx5_ib_stage_uar_cleanup(struct mlx5_ib_dev *dev)
  5100. {
  5101. mlx5_put_uars_page(dev->mdev, dev->mdev->priv.uar);
  5102. }
  5103. int mlx5_ib_stage_bfrag_init(struct mlx5_ib_dev *dev)
  5104. {
  5105. int err;
  5106. err = mlx5_alloc_bfreg(dev->mdev, &dev->bfreg, false, false);
  5107. if (err)
  5108. return err;
  5109. err = mlx5_alloc_bfreg(dev->mdev, &dev->fp_bfreg, false, true);
  5110. if (err)
  5111. mlx5_free_bfreg(dev->mdev, &dev->fp_bfreg);
  5112. return err;
  5113. }
  5114. void mlx5_ib_stage_bfrag_cleanup(struct mlx5_ib_dev *dev)
  5115. {
  5116. mlx5_free_bfreg(dev->mdev, &dev->fp_bfreg);
  5117. mlx5_free_bfreg(dev->mdev, &dev->bfreg);
  5118. }
  5119. static int mlx5_ib_stage_populate_specs(struct mlx5_ib_dev *dev)
  5120. {
  5121. return populate_specs_root(dev);
  5122. }
  5123. int mlx5_ib_stage_ib_reg_init(struct mlx5_ib_dev *dev)
  5124. {
  5125. return ib_register_device(&dev->ib_dev, NULL);
  5126. }
  5127. void mlx5_ib_stage_pre_ib_reg_umr_cleanup(struct mlx5_ib_dev *dev)
  5128. {
  5129. destroy_umrc_res(dev);
  5130. }
  5131. void mlx5_ib_stage_ib_reg_cleanup(struct mlx5_ib_dev *dev)
  5132. {
  5133. ib_unregister_device(&dev->ib_dev);
  5134. }
  5135. int mlx5_ib_stage_post_ib_reg_umr_init(struct mlx5_ib_dev *dev)
  5136. {
  5137. return create_umr_res(dev);
  5138. }
  5139. static int mlx5_ib_stage_delay_drop_init(struct mlx5_ib_dev *dev)
  5140. {
  5141. init_delay_drop(dev);
  5142. return 0;
  5143. }
  5144. static void mlx5_ib_stage_delay_drop_cleanup(struct mlx5_ib_dev *dev)
  5145. {
  5146. cancel_delay_drop(dev);
  5147. }
  5148. int mlx5_ib_stage_class_attr_init(struct mlx5_ib_dev *dev)
  5149. {
  5150. int err;
  5151. int i;
  5152. for (i = 0; i < ARRAY_SIZE(mlx5_class_attributes); i++) {
  5153. err = device_create_file(&dev->ib_dev.dev,
  5154. mlx5_class_attributes[i]);
  5155. if (err)
  5156. return err;
  5157. }
  5158. return 0;
  5159. }
  5160. static int mlx5_ib_stage_rep_reg_init(struct mlx5_ib_dev *dev)
  5161. {
  5162. mlx5_ib_register_vport_reps(dev);
  5163. return 0;
  5164. }
  5165. static void mlx5_ib_stage_rep_reg_cleanup(struct mlx5_ib_dev *dev)
  5166. {
  5167. mlx5_ib_unregister_vport_reps(dev);
  5168. }
  5169. void __mlx5_ib_remove(struct mlx5_ib_dev *dev,
  5170. const struct mlx5_ib_profile *profile,
  5171. int stage)
  5172. {
  5173. /* Number of stages to cleanup */
  5174. while (stage) {
  5175. stage--;
  5176. if (profile->stage[stage].cleanup)
  5177. profile->stage[stage].cleanup(dev);
  5178. }
  5179. ib_dealloc_device((struct ib_device *)dev);
  5180. }
  5181. void *__mlx5_ib_add(struct mlx5_ib_dev *dev,
  5182. const struct mlx5_ib_profile *profile)
  5183. {
  5184. int err;
  5185. int i;
  5186. printk_once(KERN_INFO "%s", mlx5_version);
  5187. for (i = 0; i < MLX5_IB_STAGE_MAX; i++) {
  5188. if (profile->stage[i].init) {
  5189. err = profile->stage[i].init(dev);
  5190. if (err)
  5191. goto err_out;
  5192. }
  5193. }
  5194. dev->profile = profile;
  5195. dev->ib_active = true;
  5196. return dev;
  5197. err_out:
  5198. __mlx5_ib_remove(dev, profile, i);
  5199. return NULL;
  5200. }
  5201. static const struct mlx5_ib_profile pf_profile = {
  5202. STAGE_CREATE(MLX5_IB_STAGE_INIT,
  5203. mlx5_ib_stage_init_init,
  5204. mlx5_ib_stage_init_cleanup),
  5205. STAGE_CREATE(MLX5_IB_STAGE_FLOW_DB,
  5206. mlx5_ib_stage_flow_db_init,
  5207. mlx5_ib_stage_flow_db_cleanup),
  5208. STAGE_CREATE(MLX5_IB_STAGE_CAPS,
  5209. mlx5_ib_stage_caps_init,
  5210. NULL),
  5211. STAGE_CREATE(MLX5_IB_STAGE_NON_DEFAULT_CB,
  5212. mlx5_ib_stage_non_default_cb,
  5213. NULL),
  5214. STAGE_CREATE(MLX5_IB_STAGE_ROCE,
  5215. mlx5_ib_stage_roce_init,
  5216. mlx5_ib_stage_roce_cleanup),
  5217. STAGE_CREATE(MLX5_IB_STAGE_DEVICE_RESOURCES,
  5218. mlx5_ib_stage_dev_res_init,
  5219. mlx5_ib_stage_dev_res_cleanup),
  5220. STAGE_CREATE(MLX5_IB_STAGE_ODP,
  5221. mlx5_ib_stage_odp_init,
  5222. NULL),
  5223. STAGE_CREATE(MLX5_IB_STAGE_COUNTERS,
  5224. mlx5_ib_stage_counters_init,
  5225. mlx5_ib_stage_counters_cleanup),
  5226. STAGE_CREATE(MLX5_IB_STAGE_CONG_DEBUGFS,
  5227. mlx5_ib_stage_cong_debugfs_init,
  5228. mlx5_ib_stage_cong_debugfs_cleanup),
  5229. STAGE_CREATE(MLX5_IB_STAGE_UAR,
  5230. mlx5_ib_stage_uar_init,
  5231. mlx5_ib_stage_uar_cleanup),
  5232. STAGE_CREATE(MLX5_IB_STAGE_BFREG,
  5233. mlx5_ib_stage_bfrag_init,
  5234. mlx5_ib_stage_bfrag_cleanup),
  5235. STAGE_CREATE(MLX5_IB_STAGE_PRE_IB_REG_UMR,
  5236. NULL,
  5237. mlx5_ib_stage_pre_ib_reg_umr_cleanup),
  5238. STAGE_CREATE(MLX5_IB_STAGE_SPECS,
  5239. mlx5_ib_stage_populate_specs,
  5240. NULL),
  5241. STAGE_CREATE(MLX5_IB_STAGE_IB_REG,
  5242. mlx5_ib_stage_ib_reg_init,
  5243. mlx5_ib_stage_ib_reg_cleanup),
  5244. STAGE_CREATE(MLX5_IB_STAGE_POST_IB_REG_UMR,
  5245. mlx5_ib_stage_post_ib_reg_umr_init,
  5246. NULL),
  5247. STAGE_CREATE(MLX5_IB_STAGE_DELAY_DROP,
  5248. mlx5_ib_stage_delay_drop_init,
  5249. mlx5_ib_stage_delay_drop_cleanup),
  5250. STAGE_CREATE(MLX5_IB_STAGE_CLASS_ATTR,
  5251. mlx5_ib_stage_class_attr_init,
  5252. NULL),
  5253. };
  5254. static const struct mlx5_ib_profile nic_rep_profile = {
  5255. STAGE_CREATE(MLX5_IB_STAGE_INIT,
  5256. mlx5_ib_stage_init_init,
  5257. mlx5_ib_stage_init_cleanup),
  5258. STAGE_CREATE(MLX5_IB_STAGE_FLOW_DB,
  5259. mlx5_ib_stage_flow_db_init,
  5260. mlx5_ib_stage_flow_db_cleanup),
  5261. STAGE_CREATE(MLX5_IB_STAGE_CAPS,
  5262. mlx5_ib_stage_caps_init,
  5263. NULL),
  5264. STAGE_CREATE(MLX5_IB_STAGE_NON_DEFAULT_CB,
  5265. mlx5_ib_stage_rep_non_default_cb,
  5266. NULL),
  5267. STAGE_CREATE(MLX5_IB_STAGE_ROCE,
  5268. mlx5_ib_stage_rep_roce_init,
  5269. mlx5_ib_stage_rep_roce_cleanup),
  5270. STAGE_CREATE(MLX5_IB_STAGE_DEVICE_RESOURCES,
  5271. mlx5_ib_stage_dev_res_init,
  5272. mlx5_ib_stage_dev_res_cleanup),
  5273. STAGE_CREATE(MLX5_IB_STAGE_COUNTERS,
  5274. mlx5_ib_stage_counters_init,
  5275. mlx5_ib_stage_counters_cleanup),
  5276. STAGE_CREATE(MLX5_IB_STAGE_UAR,
  5277. mlx5_ib_stage_uar_init,
  5278. mlx5_ib_stage_uar_cleanup),
  5279. STAGE_CREATE(MLX5_IB_STAGE_BFREG,
  5280. mlx5_ib_stage_bfrag_init,
  5281. mlx5_ib_stage_bfrag_cleanup),
  5282. STAGE_CREATE(MLX5_IB_STAGE_PRE_IB_REG_UMR,
  5283. NULL,
  5284. mlx5_ib_stage_pre_ib_reg_umr_cleanup),
  5285. STAGE_CREATE(MLX5_IB_STAGE_SPECS,
  5286. mlx5_ib_stage_populate_specs,
  5287. NULL),
  5288. STAGE_CREATE(MLX5_IB_STAGE_IB_REG,
  5289. mlx5_ib_stage_ib_reg_init,
  5290. mlx5_ib_stage_ib_reg_cleanup),
  5291. STAGE_CREATE(MLX5_IB_STAGE_POST_IB_REG_UMR,
  5292. mlx5_ib_stage_post_ib_reg_umr_init,
  5293. NULL),
  5294. STAGE_CREATE(MLX5_IB_STAGE_CLASS_ATTR,
  5295. mlx5_ib_stage_class_attr_init,
  5296. NULL),
  5297. STAGE_CREATE(MLX5_IB_STAGE_REP_REG,
  5298. mlx5_ib_stage_rep_reg_init,
  5299. mlx5_ib_stage_rep_reg_cleanup),
  5300. };
  5301. static void *mlx5_ib_add_slave_port(struct mlx5_core_dev *mdev)
  5302. {
  5303. struct mlx5_ib_multiport_info *mpi;
  5304. struct mlx5_ib_dev *dev;
  5305. bool bound = false;
  5306. int err;
  5307. mpi = kzalloc(sizeof(*mpi), GFP_KERNEL);
  5308. if (!mpi)
  5309. return NULL;
  5310. mpi->mdev = mdev;
  5311. err = mlx5_query_nic_vport_system_image_guid(mdev,
  5312. &mpi->sys_image_guid);
  5313. if (err) {
  5314. kfree(mpi);
  5315. return NULL;
  5316. }
  5317. mutex_lock(&mlx5_ib_multiport_mutex);
  5318. list_for_each_entry(dev, &mlx5_ib_dev_list, ib_dev_list) {
  5319. if (dev->sys_image_guid == mpi->sys_image_guid)
  5320. bound = mlx5_ib_bind_slave_port(dev, mpi);
  5321. if (bound) {
  5322. rdma_roce_rescan_device(&dev->ib_dev);
  5323. break;
  5324. }
  5325. }
  5326. if (!bound) {
  5327. list_add_tail(&mpi->list, &mlx5_ib_unaffiliated_port_list);
  5328. dev_dbg(&mdev->pdev->dev, "no suitable IB device found to bind to, added to unaffiliated list.\n");
  5329. }
  5330. mutex_unlock(&mlx5_ib_multiport_mutex);
  5331. return mpi;
  5332. }
  5333. static void *mlx5_ib_add(struct mlx5_core_dev *mdev)
  5334. {
  5335. enum rdma_link_layer ll;
  5336. struct mlx5_ib_dev *dev;
  5337. int port_type_cap;
  5338. printk_once(KERN_INFO "%s", mlx5_version);
  5339. port_type_cap = MLX5_CAP_GEN(mdev, port_type);
  5340. ll = mlx5_port_type_cap_to_rdma_ll(port_type_cap);
  5341. if (mlx5_core_is_mp_slave(mdev) && ll == IB_LINK_LAYER_ETHERNET)
  5342. return mlx5_ib_add_slave_port(mdev);
  5343. dev = (struct mlx5_ib_dev *)ib_alloc_device(sizeof(*dev));
  5344. if (!dev)
  5345. return NULL;
  5346. dev->mdev = mdev;
  5347. dev->num_ports = max(MLX5_CAP_GEN(mdev, num_ports),
  5348. MLX5_CAP_GEN(mdev, num_vhca_ports));
  5349. if (MLX5_ESWITCH_MANAGER(mdev) &&
  5350. mlx5_ib_eswitch_mode(mdev->priv.eswitch) == SRIOV_OFFLOADS) {
  5351. dev->rep = mlx5_ib_vport_rep(mdev->priv.eswitch, 0);
  5352. return __mlx5_ib_add(dev, &nic_rep_profile);
  5353. }
  5354. return __mlx5_ib_add(dev, &pf_profile);
  5355. }
  5356. static void mlx5_ib_remove(struct mlx5_core_dev *mdev, void *context)
  5357. {
  5358. struct mlx5_ib_multiport_info *mpi;
  5359. struct mlx5_ib_dev *dev;
  5360. if (mlx5_core_is_mp_slave(mdev)) {
  5361. mpi = context;
  5362. mutex_lock(&mlx5_ib_multiport_mutex);
  5363. if (mpi->ibdev)
  5364. mlx5_ib_unbind_slave_port(mpi->ibdev, mpi);
  5365. list_del(&mpi->list);
  5366. mutex_unlock(&mlx5_ib_multiport_mutex);
  5367. kfree(mpi);
  5368. return;
  5369. }
  5370. dev = context;
  5371. __mlx5_ib_remove(dev, dev->profile, MLX5_IB_STAGE_MAX);
  5372. }
  5373. static struct mlx5_interface mlx5_ib_interface = {
  5374. .add = mlx5_ib_add,
  5375. .remove = mlx5_ib_remove,
  5376. .event = mlx5_ib_event,
  5377. #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
  5378. .pfault = mlx5_ib_pfault,
  5379. #endif
  5380. .protocol = MLX5_INTERFACE_PROTOCOL_IB,
  5381. };
  5382. unsigned long mlx5_ib_get_xlt_emergency_page(void)
  5383. {
  5384. mutex_lock(&xlt_emergency_page_mutex);
  5385. return xlt_emergency_page;
  5386. }
  5387. void mlx5_ib_put_xlt_emergency_page(void)
  5388. {
  5389. mutex_unlock(&xlt_emergency_page_mutex);
  5390. }
  5391. static int __init mlx5_ib_init(void)
  5392. {
  5393. int err;
  5394. xlt_emergency_page = __get_free_page(GFP_KERNEL);
  5395. if (!xlt_emergency_page)
  5396. return -ENOMEM;
  5397. mutex_init(&xlt_emergency_page_mutex);
  5398. mlx5_ib_event_wq = alloc_ordered_workqueue("mlx5_ib_event_wq", 0);
  5399. if (!mlx5_ib_event_wq) {
  5400. free_page(xlt_emergency_page);
  5401. return -ENOMEM;
  5402. }
  5403. mlx5_ib_odp_init();
  5404. err = mlx5_register_interface(&mlx5_ib_interface);
  5405. return err;
  5406. }
  5407. static void __exit mlx5_ib_cleanup(void)
  5408. {
  5409. mlx5_unregister_interface(&mlx5_ib_interface);
  5410. destroy_workqueue(mlx5_ib_event_wq);
  5411. mutex_destroy(&xlt_emergency_page_mutex);
  5412. free_page(xlt_emergency_page);
  5413. }
  5414. module_init(mlx5_ib_init);
  5415. module_exit(mlx5_ib_cleanup);