devlink.c 122 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815
  1. /*
  2. * net/core/devlink.c - Network physical/parent device Netlink interface
  3. *
  4. * Heavily inspired by net/wireless/
  5. * Copyright (c) 2016 Mellanox Technologies. All rights reserved.
  6. * Copyright (c) 2016 Jiri Pirko <jiri@mellanox.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. */
  13. #include <linux/kernel.h>
  14. #include <linux/module.h>
  15. #include <linux/types.h>
  16. #include <linux/slab.h>
  17. #include <linux/gfp.h>
  18. #include <linux/device.h>
  19. #include <linux/list.h>
  20. #include <linux/netdevice.h>
  21. #include <rdma/ib_verbs.h>
  22. #include <net/netlink.h>
  23. #include <net/genetlink.h>
  24. #include <net/rtnetlink.h>
  25. #include <net/net_namespace.h>
  26. #include <net/sock.h>
  27. #include <net/devlink.h>
  28. #define CREATE_TRACE_POINTS
  29. #include <trace/events/devlink.h>
  30. static struct devlink_dpipe_field devlink_dpipe_fields_ethernet[] = {
  31. {
  32. .name = "destination mac",
  33. .id = DEVLINK_DPIPE_FIELD_ETHERNET_DST_MAC,
  34. .bitwidth = 48,
  35. },
  36. };
  37. struct devlink_dpipe_header devlink_dpipe_header_ethernet = {
  38. .name = "ethernet",
  39. .id = DEVLINK_DPIPE_HEADER_ETHERNET,
  40. .fields = devlink_dpipe_fields_ethernet,
  41. .fields_count = ARRAY_SIZE(devlink_dpipe_fields_ethernet),
  42. .global = true,
  43. };
  44. EXPORT_SYMBOL(devlink_dpipe_header_ethernet);
  45. static struct devlink_dpipe_field devlink_dpipe_fields_ipv4[] = {
  46. {
  47. .name = "destination ip",
  48. .id = DEVLINK_DPIPE_FIELD_IPV4_DST_IP,
  49. .bitwidth = 32,
  50. },
  51. };
  52. struct devlink_dpipe_header devlink_dpipe_header_ipv4 = {
  53. .name = "ipv4",
  54. .id = DEVLINK_DPIPE_HEADER_IPV4,
  55. .fields = devlink_dpipe_fields_ipv4,
  56. .fields_count = ARRAY_SIZE(devlink_dpipe_fields_ipv4),
  57. .global = true,
  58. };
  59. EXPORT_SYMBOL(devlink_dpipe_header_ipv4);
  60. static struct devlink_dpipe_field devlink_dpipe_fields_ipv6[] = {
  61. {
  62. .name = "destination ip",
  63. .id = DEVLINK_DPIPE_FIELD_IPV6_DST_IP,
  64. .bitwidth = 128,
  65. },
  66. };
  67. struct devlink_dpipe_header devlink_dpipe_header_ipv6 = {
  68. .name = "ipv6",
  69. .id = DEVLINK_DPIPE_HEADER_IPV6,
  70. .fields = devlink_dpipe_fields_ipv6,
  71. .fields_count = ARRAY_SIZE(devlink_dpipe_fields_ipv6),
  72. .global = true,
  73. };
  74. EXPORT_SYMBOL(devlink_dpipe_header_ipv6);
  75. EXPORT_TRACEPOINT_SYMBOL_GPL(devlink_hwmsg);
  76. static LIST_HEAD(devlink_list);
  77. /* devlink_mutex
  78. *
  79. * An overall lock guarding every operation coming from userspace.
  80. * It also guards devlink devices list and it is taken when
  81. * driver registers/unregisters it.
  82. */
  83. static DEFINE_MUTEX(devlink_mutex);
  84. static struct net *devlink_net(const struct devlink *devlink)
  85. {
  86. return read_pnet(&devlink->_net);
  87. }
  88. static void devlink_net_set(struct devlink *devlink, struct net *net)
  89. {
  90. write_pnet(&devlink->_net, net);
  91. }
  92. static struct devlink *devlink_get_from_attrs(struct net *net,
  93. struct nlattr **attrs)
  94. {
  95. struct devlink *devlink;
  96. char *busname;
  97. char *devname;
  98. if (!attrs[DEVLINK_ATTR_BUS_NAME] || !attrs[DEVLINK_ATTR_DEV_NAME])
  99. return ERR_PTR(-EINVAL);
  100. busname = nla_data(attrs[DEVLINK_ATTR_BUS_NAME]);
  101. devname = nla_data(attrs[DEVLINK_ATTR_DEV_NAME]);
  102. list_for_each_entry(devlink, &devlink_list, list) {
  103. if (strcmp(devlink->dev->bus->name, busname) == 0 &&
  104. strcmp(dev_name(devlink->dev), devname) == 0 &&
  105. net_eq(devlink_net(devlink), net))
  106. return devlink;
  107. }
  108. return ERR_PTR(-ENODEV);
  109. }
  110. static struct devlink *devlink_get_from_info(struct genl_info *info)
  111. {
  112. return devlink_get_from_attrs(genl_info_net(info), info->attrs);
  113. }
  114. static struct devlink_port *devlink_port_get_by_index(struct devlink *devlink,
  115. int port_index)
  116. {
  117. struct devlink_port *devlink_port;
  118. list_for_each_entry(devlink_port, &devlink->port_list, list) {
  119. if (devlink_port->index == port_index)
  120. return devlink_port;
  121. }
  122. return NULL;
  123. }
  124. static bool devlink_port_index_exists(struct devlink *devlink, int port_index)
  125. {
  126. return devlink_port_get_by_index(devlink, port_index);
  127. }
  128. static struct devlink_port *devlink_port_get_from_attrs(struct devlink *devlink,
  129. struct nlattr **attrs)
  130. {
  131. if (attrs[DEVLINK_ATTR_PORT_INDEX]) {
  132. u32 port_index = nla_get_u32(attrs[DEVLINK_ATTR_PORT_INDEX]);
  133. struct devlink_port *devlink_port;
  134. devlink_port = devlink_port_get_by_index(devlink, port_index);
  135. if (!devlink_port)
  136. return ERR_PTR(-ENODEV);
  137. return devlink_port;
  138. }
  139. return ERR_PTR(-EINVAL);
  140. }
  141. static struct devlink_port *devlink_port_get_from_info(struct devlink *devlink,
  142. struct genl_info *info)
  143. {
  144. return devlink_port_get_from_attrs(devlink, info->attrs);
  145. }
  146. struct devlink_sb {
  147. struct list_head list;
  148. unsigned int index;
  149. u32 size;
  150. u16 ingress_pools_count;
  151. u16 egress_pools_count;
  152. u16 ingress_tc_count;
  153. u16 egress_tc_count;
  154. };
  155. static u16 devlink_sb_pool_count(struct devlink_sb *devlink_sb)
  156. {
  157. return devlink_sb->ingress_pools_count + devlink_sb->egress_pools_count;
  158. }
  159. static struct devlink_sb *devlink_sb_get_by_index(struct devlink *devlink,
  160. unsigned int sb_index)
  161. {
  162. struct devlink_sb *devlink_sb;
  163. list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
  164. if (devlink_sb->index == sb_index)
  165. return devlink_sb;
  166. }
  167. return NULL;
  168. }
  169. static bool devlink_sb_index_exists(struct devlink *devlink,
  170. unsigned int sb_index)
  171. {
  172. return devlink_sb_get_by_index(devlink, sb_index);
  173. }
  174. static struct devlink_sb *devlink_sb_get_from_attrs(struct devlink *devlink,
  175. struct nlattr **attrs)
  176. {
  177. if (attrs[DEVLINK_ATTR_SB_INDEX]) {
  178. u32 sb_index = nla_get_u32(attrs[DEVLINK_ATTR_SB_INDEX]);
  179. struct devlink_sb *devlink_sb;
  180. devlink_sb = devlink_sb_get_by_index(devlink, sb_index);
  181. if (!devlink_sb)
  182. return ERR_PTR(-ENODEV);
  183. return devlink_sb;
  184. }
  185. return ERR_PTR(-EINVAL);
  186. }
  187. static struct devlink_sb *devlink_sb_get_from_info(struct devlink *devlink,
  188. struct genl_info *info)
  189. {
  190. return devlink_sb_get_from_attrs(devlink, info->attrs);
  191. }
  192. static int devlink_sb_pool_index_get_from_attrs(struct devlink_sb *devlink_sb,
  193. struct nlattr **attrs,
  194. u16 *p_pool_index)
  195. {
  196. u16 val;
  197. if (!attrs[DEVLINK_ATTR_SB_POOL_INDEX])
  198. return -EINVAL;
  199. val = nla_get_u16(attrs[DEVLINK_ATTR_SB_POOL_INDEX]);
  200. if (val >= devlink_sb_pool_count(devlink_sb))
  201. return -EINVAL;
  202. *p_pool_index = val;
  203. return 0;
  204. }
  205. static int devlink_sb_pool_index_get_from_info(struct devlink_sb *devlink_sb,
  206. struct genl_info *info,
  207. u16 *p_pool_index)
  208. {
  209. return devlink_sb_pool_index_get_from_attrs(devlink_sb, info->attrs,
  210. p_pool_index);
  211. }
  212. static int
  213. devlink_sb_pool_type_get_from_attrs(struct nlattr **attrs,
  214. enum devlink_sb_pool_type *p_pool_type)
  215. {
  216. u8 val;
  217. if (!attrs[DEVLINK_ATTR_SB_POOL_TYPE])
  218. return -EINVAL;
  219. val = nla_get_u8(attrs[DEVLINK_ATTR_SB_POOL_TYPE]);
  220. if (val != DEVLINK_SB_POOL_TYPE_INGRESS &&
  221. val != DEVLINK_SB_POOL_TYPE_EGRESS)
  222. return -EINVAL;
  223. *p_pool_type = val;
  224. return 0;
  225. }
  226. static int
  227. devlink_sb_pool_type_get_from_info(struct genl_info *info,
  228. enum devlink_sb_pool_type *p_pool_type)
  229. {
  230. return devlink_sb_pool_type_get_from_attrs(info->attrs, p_pool_type);
  231. }
  232. static int
  233. devlink_sb_th_type_get_from_attrs(struct nlattr **attrs,
  234. enum devlink_sb_threshold_type *p_th_type)
  235. {
  236. u8 val;
  237. if (!attrs[DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE])
  238. return -EINVAL;
  239. val = nla_get_u8(attrs[DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE]);
  240. if (val != DEVLINK_SB_THRESHOLD_TYPE_STATIC &&
  241. val != DEVLINK_SB_THRESHOLD_TYPE_DYNAMIC)
  242. return -EINVAL;
  243. *p_th_type = val;
  244. return 0;
  245. }
  246. static int
  247. devlink_sb_th_type_get_from_info(struct genl_info *info,
  248. enum devlink_sb_threshold_type *p_th_type)
  249. {
  250. return devlink_sb_th_type_get_from_attrs(info->attrs, p_th_type);
  251. }
  252. static int
  253. devlink_sb_tc_index_get_from_attrs(struct devlink_sb *devlink_sb,
  254. struct nlattr **attrs,
  255. enum devlink_sb_pool_type pool_type,
  256. u16 *p_tc_index)
  257. {
  258. u16 val;
  259. if (!attrs[DEVLINK_ATTR_SB_TC_INDEX])
  260. return -EINVAL;
  261. val = nla_get_u16(attrs[DEVLINK_ATTR_SB_TC_INDEX]);
  262. if (pool_type == DEVLINK_SB_POOL_TYPE_INGRESS &&
  263. val >= devlink_sb->ingress_tc_count)
  264. return -EINVAL;
  265. if (pool_type == DEVLINK_SB_POOL_TYPE_EGRESS &&
  266. val >= devlink_sb->egress_tc_count)
  267. return -EINVAL;
  268. *p_tc_index = val;
  269. return 0;
  270. }
  271. static int
  272. devlink_sb_tc_index_get_from_info(struct devlink_sb *devlink_sb,
  273. struct genl_info *info,
  274. enum devlink_sb_pool_type pool_type,
  275. u16 *p_tc_index)
  276. {
  277. return devlink_sb_tc_index_get_from_attrs(devlink_sb, info->attrs,
  278. pool_type, p_tc_index);
  279. }
  280. struct devlink_region {
  281. struct devlink *devlink;
  282. struct list_head list;
  283. const char *name;
  284. struct list_head snapshot_list;
  285. u32 max_snapshots;
  286. u32 cur_snapshots;
  287. u64 size;
  288. };
  289. struct devlink_snapshot {
  290. struct list_head list;
  291. struct devlink_region *region;
  292. devlink_snapshot_data_dest_t *data_destructor;
  293. u64 data_len;
  294. u8 *data;
  295. u32 id;
  296. };
  297. static struct devlink_region *
  298. devlink_region_get_by_name(struct devlink *devlink, const char *region_name)
  299. {
  300. struct devlink_region *region;
  301. list_for_each_entry(region, &devlink->region_list, list)
  302. if (!strcmp(region->name, region_name))
  303. return region;
  304. return NULL;
  305. }
  306. static struct devlink_snapshot *
  307. devlink_region_snapshot_get_by_id(struct devlink_region *region, u32 id)
  308. {
  309. struct devlink_snapshot *snapshot;
  310. list_for_each_entry(snapshot, &region->snapshot_list, list)
  311. if (snapshot->id == id)
  312. return snapshot;
  313. return NULL;
  314. }
  315. static void devlink_region_snapshot_del(struct devlink_snapshot *snapshot)
  316. {
  317. snapshot->region->cur_snapshots--;
  318. list_del(&snapshot->list);
  319. (*snapshot->data_destructor)(snapshot->data);
  320. kfree(snapshot);
  321. }
  322. #define DEVLINK_NL_FLAG_NEED_DEVLINK BIT(0)
  323. #define DEVLINK_NL_FLAG_NEED_PORT BIT(1)
  324. #define DEVLINK_NL_FLAG_NEED_SB BIT(2)
  325. /* The per devlink instance lock is taken by default in the pre-doit
  326. * operation, yet several commands do not require this. The global
  327. * devlink lock is taken and protects from disruption by user-calls.
  328. */
  329. #define DEVLINK_NL_FLAG_NO_LOCK BIT(3)
  330. static int devlink_nl_pre_doit(const struct genl_ops *ops,
  331. struct sk_buff *skb, struct genl_info *info)
  332. {
  333. struct devlink *devlink;
  334. int err;
  335. mutex_lock(&devlink_mutex);
  336. devlink = devlink_get_from_info(info);
  337. if (IS_ERR(devlink)) {
  338. mutex_unlock(&devlink_mutex);
  339. return PTR_ERR(devlink);
  340. }
  341. if (~ops->internal_flags & DEVLINK_NL_FLAG_NO_LOCK)
  342. mutex_lock(&devlink->lock);
  343. if (ops->internal_flags & DEVLINK_NL_FLAG_NEED_DEVLINK) {
  344. info->user_ptr[0] = devlink;
  345. } else if (ops->internal_flags & DEVLINK_NL_FLAG_NEED_PORT) {
  346. struct devlink_port *devlink_port;
  347. devlink_port = devlink_port_get_from_info(devlink, info);
  348. if (IS_ERR(devlink_port)) {
  349. err = PTR_ERR(devlink_port);
  350. goto unlock;
  351. }
  352. info->user_ptr[0] = devlink_port;
  353. }
  354. if (ops->internal_flags & DEVLINK_NL_FLAG_NEED_SB) {
  355. struct devlink_sb *devlink_sb;
  356. devlink_sb = devlink_sb_get_from_info(devlink, info);
  357. if (IS_ERR(devlink_sb)) {
  358. err = PTR_ERR(devlink_sb);
  359. goto unlock;
  360. }
  361. info->user_ptr[1] = devlink_sb;
  362. }
  363. return 0;
  364. unlock:
  365. if (~ops->internal_flags & DEVLINK_NL_FLAG_NO_LOCK)
  366. mutex_unlock(&devlink->lock);
  367. mutex_unlock(&devlink_mutex);
  368. return err;
  369. }
  370. static void devlink_nl_post_doit(const struct genl_ops *ops,
  371. struct sk_buff *skb, struct genl_info *info)
  372. {
  373. struct devlink *devlink;
  374. devlink = devlink_get_from_info(info);
  375. if (~ops->internal_flags & DEVLINK_NL_FLAG_NO_LOCK)
  376. mutex_unlock(&devlink->lock);
  377. mutex_unlock(&devlink_mutex);
  378. }
  379. static struct genl_family devlink_nl_family;
  380. enum devlink_multicast_groups {
  381. DEVLINK_MCGRP_CONFIG,
  382. };
  383. static const struct genl_multicast_group devlink_nl_mcgrps[] = {
  384. [DEVLINK_MCGRP_CONFIG] = { .name = DEVLINK_GENL_MCGRP_CONFIG_NAME },
  385. };
  386. static int devlink_nl_put_handle(struct sk_buff *msg, struct devlink *devlink)
  387. {
  388. if (nla_put_string(msg, DEVLINK_ATTR_BUS_NAME, devlink->dev->bus->name))
  389. return -EMSGSIZE;
  390. if (nla_put_string(msg, DEVLINK_ATTR_DEV_NAME, dev_name(devlink->dev)))
  391. return -EMSGSIZE;
  392. return 0;
  393. }
  394. static int devlink_nl_fill(struct sk_buff *msg, struct devlink *devlink,
  395. enum devlink_command cmd, u32 portid,
  396. u32 seq, int flags)
  397. {
  398. void *hdr;
  399. hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
  400. if (!hdr)
  401. return -EMSGSIZE;
  402. if (devlink_nl_put_handle(msg, devlink))
  403. goto nla_put_failure;
  404. genlmsg_end(msg, hdr);
  405. return 0;
  406. nla_put_failure:
  407. genlmsg_cancel(msg, hdr);
  408. return -EMSGSIZE;
  409. }
  410. static void devlink_notify(struct devlink *devlink, enum devlink_command cmd)
  411. {
  412. struct sk_buff *msg;
  413. int err;
  414. WARN_ON(cmd != DEVLINK_CMD_NEW && cmd != DEVLINK_CMD_DEL);
  415. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  416. if (!msg)
  417. return;
  418. err = devlink_nl_fill(msg, devlink, cmd, 0, 0, 0);
  419. if (err) {
  420. nlmsg_free(msg);
  421. return;
  422. }
  423. genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
  424. msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
  425. }
  426. static int devlink_nl_port_attrs_put(struct sk_buff *msg,
  427. struct devlink_port *devlink_port)
  428. {
  429. struct devlink_port_attrs *attrs = &devlink_port->attrs;
  430. if (!attrs->set)
  431. return 0;
  432. if (nla_put_u16(msg, DEVLINK_ATTR_PORT_FLAVOUR, attrs->flavour))
  433. return -EMSGSIZE;
  434. if (nla_put_u32(msg, DEVLINK_ATTR_PORT_NUMBER, attrs->port_number))
  435. return -EMSGSIZE;
  436. if (!attrs->split)
  437. return 0;
  438. if (nla_put_u32(msg, DEVLINK_ATTR_PORT_SPLIT_GROUP, attrs->port_number))
  439. return -EMSGSIZE;
  440. if (nla_put_u32(msg, DEVLINK_ATTR_PORT_SPLIT_SUBPORT_NUMBER,
  441. attrs->split_subport_number))
  442. return -EMSGSIZE;
  443. return 0;
  444. }
  445. static int devlink_nl_port_fill(struct sk_buff *msg, struct devlink *devlink,
  446. struct devlink_port *devlink_port,
  447. enum devlink_command cmd, u32 portid,
  448. u32 seq, int flags)
  449. {
  450. void *hdr;
  451. hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
  452. if (!hdr)
  453. return -EMSGSIZE;
  454. if (devlink_nl_put_handle(msg, devlink))
  455. goto nla_put_failure;
  456. if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, devlink_port->index))
  457. goto nla_put_failure;
  458. if (nla_put_u16(msg, DEVLINK_ATTR_PORT_TYPE, devlink_port->type))
  459. goto nla_put_failure;
  460. if (devlink_port->desired_type != DEVLINK_PORT_TYPE_NOTSET &&
  461. nla_put_u16(msg, DEVLINK_ATTR_PORT_DESIRED_TYPE,
  462. devlink_port->desired_type))
  463. goto nla_put_failure;
  464. if (devlink_port->type == DEVLINK_PORT_TYPE_ETH) {
  465. struct net_device *netdev = devlink_port->type_dev;
  466. if (netdev &&
  467. (nla_put_u32(msg, DEVLINK_ATTR_PORT_NETDEV_IFINDEX,
  468. netdev->ifindex) ||
  469. nla_put_string(msg, DEVLINK_ATTR_PORT_NETDEV_NAME,
  470. netdev->name)))
  471. goto nla_put_failure;
  472. }
  473. if (devlink_port->type == DEVLINK_PORT_TYPE_IB) {
  474. struct ib_device *ibdev = devlink_port->type_dev;
  475. if (ibdev &&
  476. nla_put_string(msg, DEVLINK_ATTR_PORT_IBDEV_NAME,
  477. ibdev->name))
  478. goto nla_put_failure;
  479. }
  480. if (devlink_nl_port_attrs_put(msg, devlink_port))
  481. goto nla_put_failure;
  482. genlmsg_end(msg, hdr);
  483. return 0;
  484. nla_put_failure:
  485. genlmsg_cancel(msg, hdr);
  486. return -EMSGSIZE;
  487. }
  488. static void devlink_port_notify(struct devlink_port *devlink_port,
  489. enum devlink_command cmd)
  490. {
  491. struct devlink *devlink = devlink_port->devlink;
  492. struct sk_buff *msg;
  493. int err;
  494. if (!devlink_port->registered)
  495. return;
  496. WARN_ON(cmd != DEVLINK_CMD_PORT_NEW && cmd != DEVLINK_CMD_PORT_DEL);
  497. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  498. if (!msg)
  499. return;
  500. err = devlink_nl_port_fill(msg, devlink, devlink_port, cmd, 0, 0, 0);
  501. if (err) {
  502. nlmsg_free(msg);
  503. return;
  504. }
  505. genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
  506. msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
  507. }
  508. static int devlink_nl_cmd_get_doit(struct sk_buff *skb, struct genl_info *info)
  509. {
  510. struct devlink *devlink = info->user_ptr[0];
  511. struct sk_buff *msg;
  512. int err;
  513. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  514. if (!msg)
  515. return -ENOMEM;
  516. err = devlink_nl_fill(msg, devlink, DEVLINK_CMD_NEW,
  517. info->snd_portid, info->snd_seq, 0);
  518. if (err) {
  519. nlmsg_free(msg);
  520. return err;
  521. }
  522. return genlmsg_reply(msg, info);
  523. }
  524. static int devlink_nl_cmd_get_dumpit(struct sk_buff *msg,
  525. struct netlink_callback *cb)
  526. {
  527. struct devlink *devlink;
  528. int start = cb->args[0];
  529. int idx = 0;
  530. int err;
  531. mutex_lock(&devlink_mutex);
  532. list_for_each_entry(devlink, &devlink_list, list) {
  533. if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
  534. continue;
  535. if (idx < start) {
  536. idx++;
  537. continue;
  538. }
  539. err = devlink_nl_fill(msg, devlink, DEVLINK_CMD_NEW,
  540. NETLINK_CB(cb->skb).portid,
  541. cb->nlh->nlmsg_seq, NLM_F_MULTI);
  542. if (err)
  543. goto out;
  544. idx++;
  545. }
  546. out:
  547. mutex_unlock(&devlink_mutex);
  548. cb->args[0] = idx;
  549. return msg->len;
  550. }
  551. static int devlink_nl_cmd_port_get_doit(struct sk_buff *skb,
  552. struct genl_info *info)
  553. {
  554. struct devlink_port *devlink_port = info->user_ptr[0];
  555. struct devlink *devlink = devlink_port->devlink;
  556. struct sk_buff *msg;
  557. int err;
  558. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  559. if (!msg)
  560. return -ENOMEM;
  561. err = devlink_nl_port_fill(msg, devlink, devlink_port,
  562. DEVLINK_CMD_PORT_NEW,
  563. info->snd_portid, info->snd_seq, 0);
  564. if (err) {
  565. nlmsg_free(msg);
  566. return err;
  567. }
  568. return genlmsg_reply(msg, info);
  569. }
  570. static int devlink_nl_cmd_port_get_dumpit(struct sk_buff *msg,
  571. struct netlink_callback *cb)
  572. {
  573. struct devlink *devlink;
  574. struct devlink_port *devlink_port;
  575. int start = cb->args[0];
  576. int idx = 0;
  577. int err;
  578. mutex_lock(&devlink_mutex);
  579. list_for_each_entry(devlink, &devlink_list, list) {
  580. if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
  581. continue;
  582. mutex_lock(&devlink->lock);
  583. list_for_each_entry(devlink_port, &devlink->port_list, list) {
  584. if (idx < start) {
  585. idx++;
  586. continue;
  587. }
  588. err = devlink_nl_port_fill(msg, devlink, devlink_port,
  589. DEVLINK_CMD_NEW,
  590. NETLINK_CB(cb->skb).portid,
  591. cb->nlh->nlmsg_seq,
  592. NLM_F_MULTI);
  593. if (err) {
  594. mutex_unlock(&devlink->lock);
  595. goto out;
  596. }
  597. idx++;
  598. }
  599. mutex_unlock(&devlink->lock);
  600. }
  601. out:
  602. mutex_unlock(&devlink_mutex);
  603. cb->args[0] = idx;
  604. return msg->len;
  605. }
  606. static int devlink_port_type_set(struct devlink *devlink,
  607. struct devlink_port *devlink_port,
  608. enum devlink_port_type port_type)
  609. {
  610. int err;
  611. if (devlink->ops && devlink->ops->port_type_set) {
  612. if (port_type == DEVLINK_PORT_TYPE_NOTSET)
  613. return -EINVAL;
  614. if (port_type == devlink_port->type)
  615. return 0;
  616. err = devlink->ops->port_type_set(devlink_port, port_type);
  617. if (err)
  618. return err;
  619. devlink_port->desired_type = port_type;
  620. devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW);
  621. return 0;
  622. }
  623. return -EOPNOTSUPP;
  624. }
  625. static int devlink_nl_cmd_port_set_doit(struct sk_buff *skb,
  626. struct genl_info *info)
  627. {
  628. struct devlink_port *devlink_port = info->user_ptr[0];
  629. struct devlink *devlink = devlink_port->devlink;
  630. int err;
  631. if (info->attrs[DEVLINK_ATTR_PORT_TYPE]) {
  632. enum devlink_port_type port_type;
  633. port_type = nla_get_u16(info->attrs[DEVLINK_ATTR_PORT_TYPE]);
  634. err = devlink_port_type_set(devlink, devlink_port, port_type);
  635. if (err)
  636. return err;
  637. }
  638. return 0;
  639. }
  640. static int devlink_port_split(struct devlink *devlink, u32 port_index,
  641. u32 count, struct netlink_ext_ack *extack)
  642. {
  643. if (devlink->ops && devlink->ops->port_split)
  644. return devlink->ops->port_split(devlink, port_index, count,
  645. extack);
  646. return -EOPNOTSUPP;
  647. }
  648. static int devlink_nl_cmd_port_split_doit(struct sk_buff *skb,
  649. struct genl_info *info)
  650. {
  651. struct devlink *devlink = info->user_ptr[0];
  652. u32 port_index;
  653. u32 count;
  654. if (!info->attrs[DEVLINK_ATTR_PORT_INDEX] ||
  655. !info->attrs[DEVLINK_ATTR_PORT_SPLIT_COUNT])
  656. return -EINVAL;
  657. port_index = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_INDEX]);
  658. count = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_SPLIT_COUNT]);
  659. return devlink_port_split(devlink, port_index, count, info->extack);
  660. }
  661. static int devlink_port_unsplit(struct devlink *devlink, u32 port_index,
  662. struct netlink_ext_ack *extack)
  663. {
  664. if (devlink->ops && devlink->ops->port_unsplit)
  665. return devlink->ops->port_unsplit(devlink, port_index, extack);
  666. return -EOPNOTSUPP;
  667. }
  668. static int devlink_nl_cmd_port_unsplit_doit(struct sk_buff *skb,
  669. struct genl_info *info)
  670. {
  671. struct devlink *devlink = info->user_ptr[0];
  672. u32 port_index;
  673. if (!info->attrs[DEVLINK_ATTR_PORT_INDEX])
  674. return -EINVAL;
  675. port_index = nla_get_u32(info->attrs[DEVLINK_ATTR_PORT_INDEX]);
  676. return devlink_port_unsplit(devlink, port_index, info->extack);
  677. }
  678. static int devlink_nl_sb_fill(struct sk_buff *msg, struct devlink *devlink,
  679. struct devlink_sb *devlink_sb,
  680. enum devlink_command cmd, u32 portid,
  681. u32 seq, int flags)
  682. {
  683. void *hdr;
  684. hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
  685. if (!hdr)
  686. return -EMSGSIZE;
  687. if (devlink_nl_put_handle(msg, devlink))
  688. goto nla_put_failure;
  689. if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index))
  690. goto nla_put_failure;
  691. if (nla_put_u32(msg, DEVLINK_ATTR_SB_SIZE, devlink_sb->size))
  692. goto nla_put_failure;
  693. if (nla_put_u16(msg, DEVLINK_ATTR_SB_INGRESS_POOL_COUNT,
  694. devlink_sb->ingress_pools_count))
  695. goto nla_put_failure;
  696. if (nla_put_u16(msg, DEVLINK_ATTR_SB_EGRESS_POOL_COUNT,
  697. devlink_sb->egress_pools_count))
  698. goto nla_put_failure;
  699. if (nla_put_u16(msg, DEVLINK_ATTR_SB_INGRESS_TC_COUNT,
  700. devlink_sb->ingress_tc_count))
  701. goto nla_put_failure;
  702. if (nla_put_u16(msg, DEVLINK_ATTR_SB_EGRESS_TC_COUNT,
  703. devlink_sb->egress_tc_count))
  704. goto nla_put_failure;
  705. genlmsg_end(msg, hdr);
  706. return 0;
  707. nla_put_failure:
  708. genlmsg_cancel(msg, hdr);
  709. return -EMSGSIZE;
  710. }
  711. static int devlink_nl_cmd_sb_get_doit(struct sk_buff *skb,
  712. struct genl_info *info)
  713. {
  714. struct devlink *devlink = info->user_ptr[0];
  715. struct devlink_sb *devlink_sb = info->user_ptr[1];
  716. struct sk_buff *msg;
  717. int err;
  718. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  719. if (!msg)
  720. return -ENOMEM;
  721. err = devlink_nl_sb_fill(msg, devlink, devlink_sb,
  722. DEVLINK_CMD_SB_NEW,
  723. info->snd_portid, info->snd_seq, 0);
  724. if (err) {
  725. nlmsg_free(msg);
  726. return err;
  727. }
  728. return genlmsg_reply(msg, info);
  729. }
  730. static int devlink_nl_cmd_sb_get_dumpit(struct sk_buff *msg,
  731. struct netlink_callback *cb)
  732. {
  733. struct devlink *devlink;
  734. struct devlink_sb *devlink_sb;
  735. int start = cb->args[0];
  736. int idx = 0;
  737. int err;
  738. mutex_lock(&devlink_mutex);
  739. list_for_each_entry(devlink, &devlink_list, list) {
  740. if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
  741. continue;
  742. mutex_lock(&devlink->lock);
  743. list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
  744. if (idx < start) {
  745. idx++;
  746. continue;
  747. }
  748. err = devlink_nl_sb_fill(msg, devlink, devlink_sb,
  749. DEVLINK_CMD_SB_NEW,
  750. NETLINK_CB(cb->skb).portid,
  751. cb->nlh->nlmsg_seq,
  752. NLM_F_MULTI);
  753. if (err) {
  754. mutex_unlock(&devlink->lock);
  755. goto out;
  756. }
  757. idx++;
  758. }
  759. mutex_unlock(&devlink->lock);
  760. }
  761. out:
  762. mutex_unlock(&devlink_mutex);
  763. cb->args[0] = idx;
  764. return msg->len;
  765. }
  766. static int devlink_nl_sb_pool_fill(struct sk_buff *msg, struct devlink *devlink,
  767. struct devlink_sb *devlink_sb,
  768. u16 pool_index, enum devlink_command cmd,
  769. u32 portid, u32 seq, int flags)
  770. {
  771. struct devlink_sb_pool_info pool_info;
  772. void *hdr;
  773. int err;
  774. err = devlink->ops->sb_pool_get(devlink, devlink_sb->index,
  775. pool_index, &pool_info);
  776. if (err)
  777. return err;
  778. hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
  779. if (!hdr)
  780. return -EMSGSIZE;
  781. if (devlink_nl_put_handle(msg, devlink))
  782. goto nla_put_failure;
  783. if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index))
  784. goto nla_put_failure;
  785. if (nla_put_u16(msg, DEVLINK_ATTR_SB_POOL_INDEX, pool_index))
  786. goto nla_put_failure;
  787. if (nla_put_u8(msg, DEVLINK_ATTR_SB_POOL_TYPE, pool_info.pool_type))
  788. goto nla_put_failure;
  789. if (nla_put_u32(msg, DEVLINK_ATTR_SB_POOL_SIZE, pool_info.size))
  790. goto nla_put_failure;
  791. if (nla_put_u8(msg, DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE,
  792. pool_info.threshold_type))
  793. goto nla_put_failure;
  794. genlmsg_end(msg, hdr);
  795. return 0;
  796. nla_put_failure:
  797. genlmsg_cancel(msg, hdr);
  798. return -EMSGSIZE;
  799. }
  800. static int devlink_nl_cmd_sb_pool_get_doit(struct sk_buff *skb,
  801. struct genl_info *info)
  802. {
  803. struct devlink *devlink = info->user_ptr[0];
  804. struct devlink_sb *devlink_sb = info->user_ptr[1];
  805. struct sk_buff *msg;
  806. u16 pool_index;
  807. int err;
  808. err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
  809. &pool_index);
  810. if (err)
  811. return err;
  812. if (!devlink->ops || !devlink->ops->sb_pool_get)
  813. return -EOPNOTSUPP;
  814. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  815. if (!msg)
  816. return -ENOMEM;
  817. err = devlink_nl_sb_pool_fill(msg, devlink, devlink_sb, pool_index,
  818. DEVLINK_CMD_SB_POOL_NEW,
  819. info->snd_portid, info->snd_seq, 0);
  820. if (err) {
  821. nlmsg_free(msg);
  822. return err;
  823. }
  824. return genlmsg_reply(msg, info);
  825. }
  826. static int __sb_pool_get_dumpit(struct sk_buff *msg, int start, int *p_idx,
  827. struct devlink *devlink,
  828. struct devlink_sb *devlink_sb,
  829. u32 portid, u32 seq)
  830. {
  831. u16 pool_count = devlink_sb_pool_count(devlink_sb);
  832. u16 pool_index;
  833. int err;
  834. for (pool_index = 0; pool_index < pool_count; pool_index++) {
  835. if (*p_idx < start) {
  836. (*p_idx)++;
  837. continue;
  838. }
  839. err = devlink_nl_sb_pool_fill(msg, devlink,
  840. devlink_sb,
  841. pool_index,
  842. DEVLINK_CMD_SB_POOL_NEW,
  843. portid, seq, NLM_F_MULTI);
  844. if (err)
  845. return err;
  846. (*p_idx)++;
  847. }
  848. return 0;
  849. }
  850. static int devlink_nl_cmd_sb_pool_get_dumpit(struct sk_buff *msg,
  851. struct netlink_callback *cb)
  852. {
  853. struct devlink *devlink;
  854. struct devlink_sb *devlink_sb;
  855. int start = cb->args[0];
  856. int idx = 0;
  857. int err;
  858. mutex_lock(&devlink_mutex);
  859. list_for_each_entry(devlink, &devlink_list, list) {
  860. if (!net_eq(devlink_net(devlink), sock_net(msg->sk)) ||
  861. !devlink->ops || !devlink->ops->sb_pool_get)
  862. continue;
  863. mutex_lock(&devlink->lock);
  864. list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
  865. err = __sb_pool_get_dumpit(msg, start, &idx, devlink,
  866. devlink_sb,
  867. NETLINK_CB(cb->skb).portid,
  868. cb->nlh->nlmsg_seq);
  869. if (err && err != -EOPNOTSUPP) {
  870. mutex_unlock(&devlink->lock);
  871. goto out;
  872. }
  873. }
  874. mutex_unlock(&devlink->lock);
  875. }
  876. out:
  877. mutex_unlock(&devlink_mutex);
  878. cb->args[0] = idx;
  879. return msg->len;
  880. }
  881. static int devlink_sb_pool_set(struct devlink *devlink, unsigned int sb_index,
  882. u16 pool_index, u32 size,
  883. enum devlink_sb_threshold_type threshold_type)
  884. {
  885. const struct devlink_ops *ops = devlink->ops;
  886. if (ops && ops->sb_pool_set)
  887. return ops->sb_pool_set(devlink, sb_index, pool_index,
  888. size, threshold_type);
  889. return -EOPNOTSUPP;
  890. }
  891. static int devlink_nl_cmd_sb_pool_set_doit(struct sk_buff *skb,
  892. struct genl_info *info)
  893. {
  894. struct devlink *devlink = info->user_ptr[0];
  895. struct devlink_sb *devlink_sb = info->user_ptr[1];
  896. enum devlink_sb_threshold_type threshold_type;
  897. u16 pool_index;
  898. u32 size;
  899. int err;
  900. err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
  901. &pool_index);
  902. if (err)
  903. return err;
  904. err = devlink_sb_th_type_get_from_info(info, &threshold_type);
  905. if (err)
  906. return err;
  907. if (!info->attrs[DEVLINK_ATTR_SB_POOL_SIZE])
  908. return -EINVAL;
  909. size = nla_get_u32(info->attrs[DEVLINK_ATTR_SB_POOL_SIZE]);
  910. return devlink_sb_pool_set(devlink, devlink_sb->index,
  911. pool_index, size, threshold_type);
  912. }
  913. static int devlink_nl_sb_port_pool_fill(struct sk_buff *msg,
  914. struct devlink *devlink,
  915. struct devlink_port *devlink_port,
  916. struct devlink_sb *devlink_sb,
  917. u16 pool_index,
  918. enum devlink_command cmd,
  919. u32 portid, u32 seq, int flags)
  920. {
  921. const struct devlink_ops *ops = devlink->ops;
  922. u32 threshold;
  923. void *hdr;
  924. int err;
  925. err = ops->sb_port_pool_get(devlink_port, devlink_sb->index,
  926. pool_index, &threshold);
  927. if (err)
  928. return err;
  929. hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
  930. if (!hdr)
  931. return -EMSGSIZE;
  932. if (devlink_nl_put_handle(msg, devlink))
  933. goto nla_put_failure;
  934. if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, devlink_port->index))
  935. goto nla_put_failure;
  936. if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index))
  937. goto nla_put_failure;
  938. if (nla_put_u16(msg, DEVLINK_ATTR_SB_POOL_INDEX, pool_index))
  939. goto nla_put_failure;
  940. if (nla_put_u32(msg, DEVLINK_ATTR_SB_THRESHOLD, threshold))
  941. goto nla_put_failure;
  942. if (ops->sb_occ_port_pool_get) {
  943. u32 cur;
  944. u32 max;
  945. err = ops->sb_occ_port_pool_get(devlink_port, devlink_sb->index,
  946. pool_index, &cur, &max);
  947. if (err && err != -EOPNOTSUPP)
  948. return err;
  949. if (!err) {
  950. if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_CUR, cur))
  951. goto nla_put_failure;
  952. if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_MAX, max))
  953. goto nla_put_failure;
  954. }
  955. }
  956. genlmsg_end(msg, hdr);
  957. return 0;
  958. nla_put_failure:
  959. genlmsg_cancel(msg, hdr);
  960. return -EMSGSIZE;
  961. }
  962. static int devlink_nl_cmd_sb_port_pool_get_doit(struct sk_buff *skb,
  963. struct genl_info *info)
  964. {
  965. struct devlink_port *devlink_port = info->user_ptr[0];
  966. struct devlink *devlink = devlink_port->devlink;
  967. struct devlink_sb *devlink_sb = info->user_ptr[1];
  968. struct sk_buff *msg;
  969. u16 pool_index;
  970. int err;
  971. err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
  972. &pool_index);
  973. if (err)
  974. return err;
  975. if (!devlink->ops || !devlink->ops->sb_port_pool_get)
  976. return -EOPNOTSUPP;
  977. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  978. if (!msg)
  979. return -ENOMEM;
  980. err = devlink_nl_sb_port_pool_fill(msg, devlink, devlink_port,
  981. devlink_sb, pool_index,
  982. DEVLINK_CMD_SB_PORT_POOL_NEW,
  983. info->snd_portid, info->snd_seq, 0);
  984. if (err) {
  985. nlmsg_free(msg);
  986. return err;
  987. }
  988. return genlmsg_reply(msg, info);
  989. }
  990. static int __sb_port_pool_get_dumpit(struct sk_buff *msg, int start, int *p_idx,
  991. struct devlink *devlink,
  992. struct devlink_sb *devlink_sb,
  993. u32 portid, u32 seq)
  994. {
  995. struct devlink_port *devlink_port;
  996. u16 pool_count = devlink_sb_pool_count(devlink_sb);
  997. u16 pool_index;
  998. int err;
  999. list_for_each_entry(devlink_port, &devlink->port_list, list) {
  1000. for (pool_index = 0; pool_index < pool_count; pool_index++) {
  1001. if (*p_idx < start) {
  1002. (*p_idx)++;
  1003. continue;
  1004. }
  1005. err = devlink_nl_sb_port_pool_fill(msg, devlink,
  1006. devlink_port,
  1007. devlink_sb,
  1008. pool_index,
  1009. DEVLINK_CMD_SB_PORT_POOL_NEW,
  1010. portid, seq,
  1011. NLM_F_MULTI);
  1012. if (err)
  1013. return err;
  1014. (*p_idx)++;
  1015. }
  1016. }
  1017. return 0;
  1018. }
  1019. static int devlink_nl_cmd_sb_port_pool_get_dumpit(struct sk_buff *msg,
  1020. struct netlink_callback *cb)
  1021. {
  1022. struct devlink *devlink;
  1023. struct devlink_sb *devlink_sb;
  1024. int start = cb->args[0];
  1025. int idx = 0;
  1026. int err;
  1027. mutex_lock(&devlink_mutex);
  1028. list_for_each_entry(devlink, &devlink_list, list) {
  1029. if (!net_eq(devlink_net(devlink), sock_net(msg->sk)) ||
  1030. !devlink->ops || !devlink->ops->sb_port_pool_get)
  1031. continue;
  1032. mutex_lock(&devlink->lock);
  1033. list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
  1034. err = __sb_port_pool_get_dumpit(msg, start, &idx,
  1035. devlink, devlink_sb,
  1036. NETLINK_CB(cb->skb).portid,
  1037. cb->nlh->nlmsg_seq);
  1038. if (err && err != -EOPNOTSUPP) {
  1039. mutex_unlock(&devlink->lock);
  1040. goto out;
  1041. }
  1042. }
  1043. mutex_unlock(&devlink->lock);
  1044. }
  1045. out:
  1046. mutex_unlock(&devlink_mutex);
  1047. cb->args[0] = idx;
  1048. return msg->len;
  1049. }
  1050. static int devlink_sb_port_pool_set(struct devlink_port *devlink_port,
  1051. unsigned int sb_index, u16 pool_index,
  1052. u32 threshold)
  1053. {
  1054. const struct devlink_ops *ops = devlink_port->devlink->ops;
  1055. if (ops && ops->sb_port_pool_set)
  1056. return ops->sb_port_pool_set(devlink_port, sb_index,
  1057. pool_index, threshold);
  1058. return -EOPNOTSUPP;
  1059. }
  1060. static int devlink_nl_cmd_sb_port_pool_set_doit(struct sk_buff *skb,
  1061. struct genl_info *info)
  1062. {
  1063. struct devlink_port *devlink_port = info->user_ptr[0];
  1064. struct devlink_sb *devlink_sb = info->user_ptr[1];
  1065. u16 pool_index;
  1066. u32 threshold;
  1067. int err;
  1068. err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
  1069. &pool_index);
  1070. if (err)
  1071. return err;
  1072. if (!info->attrs[DEVLINK_ATTR_SB_THRESHOLD])
  1073. return -EINVAL;
  1074. threshold = nla_get_u32(info->attrs[DEVLINK_ATTR_SB_THRESHOLD]);
  1075. return devlink_sb_port_pool_set(devlink_port, devlink_sb->index,
  1076. pool_index, threshold);
  1077. }
  1078. static int
  1079. devlink_nl_sb_tc_pool_bind_fill(struct sk_buff *msg, struct devlink *devlink,
  1080. struct devlink_port *devlink_port,
  1081. struct devlink_sb *devlink_sb, u16 tc_index,
  1082. enum devlink_sb_pool_type pool_type,
  1083. enum devlink_command cmd,
  1084. u32 portid, u32 seq, int flags)
  1085. {
  1086. const struct devlink_ops *ops = devlink->ops;
  1087. u16 pool_index;
  1088. u32 threshold;
  1089. void *hdr;
  1090. int err;
  1091. err = ops->sb_tc_pool_bind_get(devlink_port, devlink_sb->index,
  1092. tc_index, pool_type,
  1093. &pool_index, &threshold);
  1094. if (err)
  1095. return err;
  1096. hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
  1097. if (!hdr)
  1098. return -EMSGSIZE;
  1099. if (devlink_nl_put_handle(msg, devlink))
  1100. goto nla_put_failure;
  1101. if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, devlink_port->index))
  1102. goto nla_put_failure;
  1103. if (nla_put_u32(msg, DEVLINK_ATTR_SB_INDEX, devlink_sb->index))
  1104. goto nla_put_failure;
  1105. if (nla_put_u16(msg, DEVLINK_ATTR_SB_TC_INDEX, tc_index))
  1106. goto nla_put_failure;
  1107. if (nla_put_u8(msg, DEVLINK_ATTR_SB_POOL_TYPE, pool_type))
  1108. goto nla_put_failure;
  1109. if (nla_put_u16(msg, DEVLINK_ATTR_SB_POOL_INDEX, pool_index))
  1110. goto nla_put_failure;
  1111. if (nla_put_u32(msg, DEVLINK_ATTR_SB_THRESHOLD, threshold))
  1112. goto nla_put_failure;
  1113. if (ops->sb_occ_tc_port_bind_get) {
  1114. u32 cur;
  1115. u32 max;
  1116. err = ops->sb_occ_tc_port_bind_get(devlink_port,
  1117. devlink_sb->index,
  1118. tc_index, pool_type,
  1119. &cur, &max);
  1120. if (err && err != -EOPNOTSUPP)
  1121. return err;
  1122. if (!err) {
  1123. if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_CUR, cur))
  1124. goto nla_put_failure;
  1125. if (nla_put_u32(msg, DEVLINK_ATTR_SB_OCC_MAX, max))
  1126. goto nla_put_failure;
  1127. }
  1128. }
  1129. genlmsg_end(msg, hdr);
  1130. return 0;
  1131. nla_put_failure:
  1132. genlmsg_cancel(msg, hdr);
  1133. return -EMSGSIZE;
  1134. }
  1135. static int devlink_nl_cmd_sb_tc_pool_bind_get_doit(struct sk_buff *skb,
  1136. struct genl_info *info)
  1137. {
  1138. struct devlink_port *devlink_port = info->user_ptr[0];
  1139. struct devlink *devlink = devlink_port->devlink;
  1140. struct devlink_sb *devlink_sb = info->user_ptr[1];
  1141. struct sk_buff *msg;
  1142. enum devlink_sb_pool_type pool_type;
  1143. u16 tc_index;
  1144. int err;
  1145. err = devlink_sb_pool_type_get_from_info(info, &pool_type);
  1146. if (err)
  1147. return err;
  1148. err = devlink_sb_tc_index_get_from_info(devlink_sb, info,
  1149. pool_type, &tc_index);
  1150. if (err)
  1151. return err;
  1152. if (!devlink->ops || !devlink->ops->sb_tc_pool_bind_get)
  1153. return -EOPNOTSUPP;
  1154. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  1155. if (!msg)
  1156. return -ENOMEM;
  1157. err = devlink_nl_sb_tc_pool_bind_fill(msg, devlink, devlink_port,
  1158. devlink_sb, tc_index, pool_type,
  1159. DEVLINK_CMD_SB_TC_POOL_BIND_NEW,
  1160. info->snd_portid,
  1161. info->snd_seq, 0);
  1162. if (err) {
  1163. nlmsg_free(msg);
  1164. return err;
  1165. }
  1166. return genlmsg_reply(msg, info);
  1167. }
  1168. static int __sb_tc_pool_bind_get_dumpit(struct sk_buff *msg,
  1169. int start, int *p_idx,
  1170. struct devlink *devlink,
  1171. struct devlink_sb *devlink_sb,
  1172. u32 portid, u32 seq)
  1173. {
  1174. struct devlink_port *devlink_port;
  1175. u16 tc_index;
  1176. int err;
  1177. list_for_each_entry(devlink_port, &devlink->port_list, list) {
  1178. for (tc_index = 0;
  1179. tc_index < devlink_sb->ingress_tc_count; tc_index++) {
  1180. if (*p_idx < start) {
  1181. (*p_idx)++;
  1182. continue;
  1183. }
  1184. err = devlink_nl_sb_tc_pool_bind_fill(msg, devlink,
  1185. devlink_port,
  1186. devlink_sb,
  1187. tc_index,
  1188. DEVLINK_SB_POOL_TYPE_INGRESS,
  1189. DEVLINK_CMD_SB_TC_POOL_BIND_NEW,
  1190. portid, seq,
  1191. NLM_F_MULTI);
  1192. if (err)
  1193. return err;
  1194. (*p_idx)++;
  1195. }
  1196. for (tc_index = 0;
  1197. tc_index < devlink_sb->egress_tc_count; tc_index++) {
  1198. if (*p_idx < start) {
  1199. (*p_idx)++;
  1200. continue;
  1201. }
  1202. err = devlink_nl_sb_tc_pool_bind_fill(msg, devlink,
  1203. devlink_port,
  1204. devlink_sb,
  1205. tc_index,
  1206. DEVLINK_SB_POOL_TYPE_EGRESS,
  1207. DEVLINK_CMD_SB_TC_POOL_BIND_NEW,
  1208. portid, seq,
  1209. NLM_F_MULTI);
  1210. if (err)
  1211. return err;
  1212. (*p_idx)++;
  1213. }
  1214. }
  1215. return 0;
  1216. }
  1217. static int
  1218. devlink_nl_cmd_sb_tc_pool_bind_get_dumpit(struct sk_buff *msg,
  1219. struct netlink_callback *cb)
  1220. {
  1221. struct devlink *devlink;
  1222. struct devlink_sb *devlink_sb;
  1223. int start = cb->args[0];
  1224. int idx = 0;
  1225. int err;
  1226. mutex_lock(&devlink_mutex);
  1227. list_for_each_entry(devlink, &devlink_list, list) {
  1228. if (!net_eq(devlink_net(devlink), sock_net(msg->sk)) ||
  1229. !devlink->ops || !devlink->ops->sb_tc_pool_bind_get)
  1230. continue;
  1231. mutex_lock(&devlink->lock);
  1232. list_for_each_entry(devlink_sb, &devlink->sb_list, list) {
  1233. err = __sb_tc_pool_bind_get_dumpit(msg, start, &idx,
  1234. devlink,
  1235. devlink_sb,
  1236. NETLINK_CB(cb->skb).portid,
  1237. cb->nlh->nlmsg_seq);
  1238. if (err && err != -EOPNOTSUPP) {
  1239. mutex_unlock(&devlink->lock);
  1240. goto out;
  1241. }
  1242. }
  1243. mutex_unlock(&devlink->lock);
  1244. }
  1245. out:
  1246. mutex_unlock(&devlink_mutex);
  1247. cb->args[0] = idx;
  1248. return msg->len;
  1249. }
  1250. static int devlink_sb_tc_pool_bind_set(struct devlink_port *devlink_port,
  1251. unsigned int sb_index, u16 tc_index,
  1252. enum devlink_sb_pool_type pool_type,
  1253. u16 pool_index, u32 threshold)
  1254. {
  1255. const struct devlink_ops *ops = devlink_port->devlink->ops;
  1256. if (ops && ops->sb_tc_pool_bind_set)
  1257. return ops->sb_tc_pool_bind_set(devlink_port, sb_index,
  1258. tc_index, pool_type,
  1259. pool_index, threshold);
  1260. return -EOPNOTSUPP;
  1261. }
  1262. static int devlink_nl_cmd_sb_tc_pool_bind_set_doit(struct sk_buff *skb,
  1263. struct genl_info *info)
  1264. {
  1265. struct devlink_port *devlink_port = info->user_ptr[0];
  1266. struct devlink_sb *devlink_sb = info->user_ptr[1];
  1267. enum devlink_sb_pool_type pool_type;
  1268. u16 tc_index;
  1269. u16 pool_index;
  1270. u32 threshold;
  1271. int err;
  1272. err = devlink_sb_pool_type_get_from_info(info, &pool_type);
  1273. if (err)
  1274. return err;
  1275. err = devlink_sb_tc_index_get_from_info(devlink_sb, info,
  1276. pool_type, &tc_index);
  1277. if (err)
  1278. return err;
  1279. err = devlink_sb_pool_index_get_from_info(devlink_sb, info,
  1280. &pool_index);
  1281. if (err)
  1282. return err;
  1283. if (!info->attrs[DEVLINK_ATTR_SB_THRESHOLD])
  1284. return -EINVAL;
  1285. threshold = nla_get_u32(info->attrs[DEVLINK_ATTR_SB_THRESHOLD]);
  1286. return devlink_sb_tc_pool_bind_set(devlink_port, devlink_sb->index,
  1287. tc_index, pool_type,
  1288. pool_index, threshold);
  1289. }
  1290. static int devlink_nl_cmd_sb_occ_snapshot_doit(struct sk_buff *skb,
  1291. struct genl_info *info)
  1292. {
  1293. struct devlink *devlink = info->user_ptr[0];
  1294. struct devlink_sb *devlink_sb = info->user_ptr[1];
  1295. const struct devlink_ops *ops = devlink->ops;
  1296. if (ops && ops->sb_occ_snapshot)
  1297. return ops->sb_occ_snapshot(devlink, devlink_sb->index);
  1298. return -EOPNOTSUPP;
  1299. }
  1300. static int devlink_nl_cmd_sb_occ_max_clear_doit(struct sk_buff *skb,
  1301. struct genl_info *info)
  1302. {
  1303. struct devlink *devlink = info->user_ptr[0];
  1304. struct devlink_sb *devlink_sb = info->user_ptr[1];
  1305. const struct devlink_ops *ops = devlink->ops;
  1306. if (ops && ops->sb_occ_max_clear)
  1307. return ops->sb_occ_max_clear(devlink, devlink_sb->index);
  1308. return -EOPNOTSUPP;
  1309. }
  1310. static int devlink_nl_eswitch_fill(struct sk_buff *msg, struct devlink *devlink,
  1311. enum devlink_command cmd, u32 portid,
  1312. u32 seq, int flags)
  1313. {
  1314. const struct devlink_ops *ops = devlink->ops;
  1315. u8 inline_mode, encap_mode;
  1316. void *hdr;
  1317. int err = 0;
  1318. u16 mode;
  1319. hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
  1320. if (!hdr)
  1321. return -EMSGSIZE;
  1322. err = devlink_nl_put_handle(msg, devlink);
  1323. if (err)
  1324. goto nla_put_failure;
  1325. if (ops->eswitch_mode_get) {
  1326. err = ops->eswitch_mode_get(devlink, &mode);
  1327. if (err)
  1328. goto nla_put_failure;
  1329. err = nla_put_u16(msg, DEVLINK_ATTR_ESWITCH_MODE, mode);
  1330. if (err)
  1331. goto nla_put_failure;
  1332. }
  1333. if (ops->eswitch_inline_mode_get) {
  1334. err = ops->eswitch_inline_mode_get(devlink, &inline_mode);
  1335. if (err)
  1336. goto nla_put_failure;
  1337. err = nla_put_u8(msg, DEVLINK_ATTR_ESWITCH_INLINE_MODE,
  1338. inline_mode);
  1339. if (err)
  1340. goto nla_put_failure;
  1341. }
  1342. if (ops->eswitch_encap_mode_get) {
  1343. err = ops->eswitch_encap_mode_get(devlink, &encap_mode);
  1344. if (err)
  1345. goto nla_put_failure;
  1346. err = nla_put_u8(msg, DEVLINK_ATTR_ESWITCH_ENCAP_MODE, encap_mode);
  1347. if (err)
  1348. goto nla_put_failure;
  1349. }
  1350. genlmsg_end(msg, hdr);
  1351. return 0;
  1352. nla_put_failure:
  1353. genlmsg_cancel(msg, hdr);
  1354. return err;
  1355. }
  1356. static int devlink_nl_cmd_eswitch_get_doit(struct sk_buff *skb,
  1357. struct genl_info *info)
  1358. {
  1359. struct devlink *devlink = info->user_ptr[0];
  1360. const struct devlink_ops *ops = devlink->ops;
  1361. struct sk_buff *msg;
  1362. int err;
  1363. if (!ops)
  1364. return -EOPNOTSUPP;
  1365. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  1366. if (!msg)
  1367. return -ENOMEM;
  1368. err = devlink_nl_eswitch_fill(msg, devlink, DEVLINK_CMD_ESWITCH_GET,
  1369. info->snd_portid, info->snd_seq, 0);
  1370. if (err) {
  1371. nlmsg_free(msg);
  1372. return err;
  1373. }
  1374. return genlmsg_reply(msg, info);
  1375. }
  1376. static int devlink_nl_cmd_eswitch_set_doit(struct sk_buff *skb,
  1377. struct genl_info *info)
  1378. {
  1379. struct devlink *devlink = info->user_ptr[0];
  1380. const struct devlink_ops *ops = devlink->ops;
  1381. u8 inline_mode, encap_mode;
  1382. int err = 0;
  1383. u16 mode;
  1384. if (!ops)
  1385. return -EOPNOTSUPP;
  1386. if (info->attrs[DEVLINK_ATTR_ESWITCH_MODE]) {
  1387. if (!ops->eswitch_mode_set)
  1388. return -EOPNOTSUPP;
  1389. mode = nla_get_u16(info->attrs[DEVLINK_ATTR_ESWITCH_MODE]);
  1390. err = ops->eswitch_mode_set(devlink, mode);
  1391. if (err)
  1392. return err;
  1393. }
  1394. if (info->attrs[DEVLINK_ATTR_ESWITCH_INLINE_MODE]) {
  1395. if (!ops->eswitch_inline_mode_set)
  1396. return -EOPNOTSUPP;
  1397. inline_mode = nla_get_u8(
  1398. info->attrs[DEVLINK_ATTR_ESWITCH_INLINE_MODE]);
  1399. err = ops->eswitch_inline_mode_set(devlink, inline_mode);
  1400. if (err)
  1401. return err;
  1402. }
  1403. if (info->attrs[DEVLINK_ATTR_ESWITCH_ENCAP_MODE]) {
  1404. if (!ops->eswitch_encap_mode_set)
  1405. return -EOPNOTSUPP;
  1406. encap_mode = nla_get_u8(info->attrs[DEVLINK_ATTR_ESWITCH_ENCAP_MODE]);
  1407. err = ops->eswitch_encap_mode_set(devlink, encap_mode);
  1408. if (err)
  1409. return err;
  1410. }
  1411. return 0;
  1412. }
  1413. int devlink_dpipe_match_put(struct sk_buff *skb,
  1414. struct devlink_dpipe_match *match)
  1415. {
  1416. struct devlink_dpipe_header *header = match->header;
  1417. struct devlink_dpipe_field *field = &header->fields[match->field_id];
  1418. struct nlattr *match_attr;
  1419. match_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_MATCH);
  1420. if (!match_attr)
  1421. return -EMSGSIZE;
  1422. if (nla_put_u32(skb, DEVLINK_ATTR_DPIPE_MATCH_TYPE, match->type) ||
  1423. nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_INDEX, match->header_index) ||
  1424. nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_ID, header->id) ||
  1425. nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_ID, field->id) ||
  1426. nla_put_u8(skb, DEVLINK_ATTR_DPIPE_HEADER_GLOBAL, header->global))
  1427. goto nla_put_failure;
  1428. nla_nest_end(skb, match_attr);
  1429. return 0;
  1430. nla_put_failure:
  1431. nla_nest_cancel(skb, match_attr);
  1432. return -EMSGSIZE;
  1433. }
  1434. EXPORT_SYMBOL_GPL(devlink_dpipe_match_put);
  1435. static int devlink_dpipe_matches_put(struct devlink_dpipe_table *table,
  1436. struct sk_buff *skb)
  1437. {
  1438. struct nlattr *matches_attr;
  1439. matches_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_TABLE_MATCHES);
  1440. if (!matches_attr)
  1441. return -EMSGSIZE;
  1442. if (table->table_ops->matches_dump(table->priv, skb))
  1443. goto nla_put_failure;
  1444. nla_nest_end(skb, matches_attr);
  1445. return 0;
  1446. nla_put_failure:
  1447. nla_nest_cancel(skb, matches_attr);
  1448. return -EMSGSIZE;
  1449. }
  1450. int devlink_dpipe_action_put(struct sk_buff *skb,
  1451. struct devlink_dpipe_action *action)
  1452. {
  1453. struct devlink_dpipe_header *header = action->header;
  1454. struct devlink_dpipe_field *field = &header->fields[action->field_id];
  1455. struct nlattr *action_attr;
  1456. action_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_ACTION);
  1457. if (!action_attr)
  1458. return -EMSGSIZE;
  1459. if (nla_put_u32(skb, DEVLINK_ATTR_DPIPE_ACTION_TYPE, action->type) ||
  1460. nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_INDEX, action->header_index) ||
  1461. nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_ID, header->id) ||
  1462. nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_ID, field->id) ||
  1463. nla_put_u8(skb, DEVLINK_ATTR_DPIPE_HEADER_GLOBAL, header->global))
  1464. goto nla_put_failure;
  1465. nla_nest_end(skb, action_attr);
  1466. return 0;
  1467. nla_put_failure:
  1468. nla_nest_cancel(skb, action_attr);
  1469. return -EMSGSIZE;
  1470. }
  1471. EXPORT_SYMBOL_GPL(devlink_dpipe_action_put);
  1472. static int devlink_dpipe_actions_put(struct devlink_dpipe_table *table,
  1473. struct sk_buff *skb)
  1474. {
  1475. struct nlattr *actions_attr;
  1476. actions_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_TABLE_ACTIONS);
  1477. if (!actions_attr)
  1478. return -EMSGSIZE;
  1479. if (table->table_ops->actions_dump(table->priv, skb))
  1480. goto nla_put_failure;
  1481. nla_nest_end(skb, actions_attr);
  1482. return 0;
  1483. nla_put_failure:
  1484. nla_nest_cancel(skb, actions_attr);
  1485. return -EMSGSIZE;
  1486. }
  1487. static int devlink_dpipe_table_put(struct sk_buff *skb,
  1488. struct devlink_dpipe_table *table)
  1489. {
  1490. struct nlattr *table_attr;
  1491. u64 table_size;
  1492. table_size = table->table_ops->size_get(table->priv);
  1493. table_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_TABLE);
  1494. if (!table_attr)
  1495. return -EMSGSIZE;
  1496. if (nla_put_string(skb, DEVLINK_ATTR_DPIPE_TABLE_NAME, table->name) ||
  1497. nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_TABLE_SIZE, table_size,
  1498. DEVLINK_ATTR_PAD))
  1499. goto nla_put_failure;
  1500. if (nla_put_u8(skb, DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED,
  1501. table->counters_enabled))
  1502. goto nla_put_failure;
  1503. if (table->resource_valid) {
  1504. if (nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_ID,
  1505. table->resource_id, DEVLINK_ATTR_PAD) ||
  1506. nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_TABLE_RESOURCE_UNITS,
  1507. table->resource_units, DEVLINK_ATTR_PAD))
  1508. goto nla_put_failure;
  1509. }
  1510. if (devlink_dpipe_matches_put(table, skb))
  1511. goto nla_put_failure;
  1512. if (devlink_dpipe_actions_put(table, skb))
  1513. goto nla_put_failure;
  1514. nla_nest_end(skb, table_attr);
  1515. return 0;
  1516. nla_put_failure:
  1517. nla_nest_cancel(skb, table_attr);
  1518. return -EMSGSIZE;
  1519. }
  1520. static int devlink_dpipe_send_and_alloc_skb(struct sk_buff **pskb,
  1521. struct genl_info *info)
  1522. {
  1523. int err;
  1524. if (*pskb) {
  1525. err = genlmsg_reply(*pskb, info);
  1526. if (err)
  1527. return err;
  1528. }
  1529. *pskb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
  1530. if (!*pskb)
  1531. return -ENOMEM;
  1532. return 0;
  1533. }
  1534. static int devlink_dpipe_tables_fill(struct genl_info *info,
  1535. enum devlink_command cmd, int flags,
  1536. struct list_head *dpipe_tables,
  1537. const char *table_name)
  1538. {
  1539. struct devlink *devlink = info->user_ptr[0];
  1540. struct devlink_dpipe_table *table;
  1541. struct nlattr *tables_attr;
  1542. struct sk_buff *skb = NULL;
  1543. struct nlmsghdr *nlh;
  1544. bool incomplete;
  1545. void *hdr;
  1546. int i;
  1547. int err;
  1548. table = list_first_entry(dpipe_tables,
  1549. struct devlink_dpipe_table, list);
  1550. start_again:
  1551. err = devlink_dpipe_send_and_alloc_skb(&skb, info);
  1552. if (err)
  1553. return err;
  1554. hdr = genlmsg_put(skb, info->snd_portid, info->snd_seq,
  1555. &devlink_nl_family, NLM_F_MULTI, cmd);
  1556. if (!hdr) {
  1557. nlmsg_free(skb);
  1558. return -EMSGSIZE;
  1559. }
  1560. if (devlink_nl_put_handle(skb, devlink))
  1561. goto nla_put_failure;
  1562. tables_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_TABLES);
  1563. if (!tables_attr)
  1564. goto nla_put_failure;
  1565. i = 0;
  1566. incomplete = false;
  1567. list_for_each_entry_from(table, dpipe_tables, list) {
  1568. if (!table_name) {
  1569. err = devlink_dpipe_table_put(skb, table);
  1570. if (err) {
  1571. if (!i)
  1572. goto err_table_put;
  1573. incomplete = true;
  1574. break;
  1575. }
  1576. } else {
  1577. if (!strcmp(table->name, table_name)) {
  1578. err = devlink_dpipe_table_put(skb, table);
  1579. if (err)
  1580. break;
  1581. }
  1582. }
  1583. i++;
  1584. }
  1585. nla_nest_end(skb, tables_attr);
  1586. genlmsg_end(skb, hdr);
  1587. if (incomplete)
  1588. goto start_again;
  1589. send_done:
  1590. nlh = nlmsg_put(skb, info->snd_portid, info->snd_seq,
  1591. NLMSG_DONE, 0, flags | NLM_F_MULTI);
  1592. if (!nlh) {
  1593. err = devlink_dpipe_send_and_alloc_skb(&skb, info);
  1594. if (err)
  1595. return err;
  1596. goto send_done;
  1597. }
  1598. return genlmsg_reply(skb, info);
  1599. nla_put_failure:
  1600. err = -EMSGSIZE;
  1601. err_table_put:
  1602. nlmsg_free(skb);
  1603. return err;
  1604. }
  1605. static int devlink_nl_cmd_dpipe_table_get(struct sk_buff *skb,
  1606. struct genl_info *info)
  1607. {
  1608. struct devlink *devlink = info->user_ptr[0];
  1609. const char *table_name = NULL;
  1610. if (info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME])
  1611. table_name = nla_data(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME]);
  1612. return devlink_dpipe_tables_fill(info, DEVLINK_CMD_DPIPE_TABLE_GET, 0,
  1613. &devlink->dpipe_table_list,
  1614. table_name);
  1615. }
  1616. static int devlink_dpipe_value_put(struct sk_buff *skb,
  1617. struct devlink_dpipe_value *value)
  1618. {
  1619. if (nla_put(skb, DEVLINK_ATTR_DPIPE_VALUE,
  1620. value->value_size, value->value))
  1621. return -EMSGSIZE;
  1622. if (value->mask)
  1623. if (nla_put(skb, DEVLINK_ATTR_DPIPE_VALUE_MASK,
  1624. value->value_size, value->mask))
  1625. return -EMSGSIZE;
  1626. if (value->mapping_valid)
  1627. if (nla_put_u32(skb, DEVLINK_ATTR_DPIPE_VALUE_MAPPING,
  1628. value->mapping_value))
  1629. return -EMSGSIZE;
  1630. return 0;
  1631. }
  1632. static int devlink_dpipe_action_value_put(struct sk_buff *skb,
  1633. struct devlink_dpipe_value *value)
  1634. {
  1635. if (!value->action)
  1636. return -EINVAL;
  1637. if (devlink_dpipe_action_put(skb, value->action))
  1638. return -EMSGSIZE;
  1639. if (devlink_dpipe_value_put(skb, value))
  1640. return -EMSGSIZE;
  1641. return 0;
  1642. }
  1643. static int devlink_dpipe_action_values_put(struct sk_buff *skb,
  1644. struct devlink_dpipe_value *values,
  1645. unsigned int values_count)
  1646. {
  1647. struct nlattr *action_attr;
  1648. int i;
  1649. int err;
  1650. for (i = 0; i < values_count; i++) {
  1651. action_attr = nla_nest_start(skb,
  1652. DEVLINK_ATTR_DPIPE_ACTION_VALUE);
  1653. if (!action_attr)
  1654. return -EMSGSIZE;
  1655. err = devlink_dpipe_action_value_put(skb, &values[i]);
  1656. if (err)
  1657. goto err_action_value_put;
  1658. nla_nest_end(skb, action_attr);
  1659. }
  1660. return 0;
  1661. err_action_value_put:
  1662. nla_nest_cancel(skb, action_attr);
  1663. return err;
  1664. }
  1665. static int devlink_dpipe_match_value_put(struct sk_buff *skb,
  1666. struct devlink_dpipe_value *value)
  1667. {
  1668. if (!value->match)
  1669. return -EINVAL;
  1670. if (devlink_dpipe_match_put(skb, value->match))
  1671. return -EMSGSIZE;
  1672. if (devlink_dpipe_value_put(skb, value))
  1673. return -EMSGSIZE;
  1674. return 0;
  1675. }
  1676. static int devlink_dpipe_match_values_put(struct sk_buff *skb,
  1677. struct devlink_dpipe_value *values,
  1678. unsigned int values_count)
  1679. {
  1680. struct nlattr *match_attr;
  1681. int i;
  1682. int err;
  1683. for (i = 0; i < values_count; i++) {
  1684. match_attr = nla_nest_start(skb,
  1685. DEVLINK_ATTR_DPIPE_MATCH_VALUE);
  1686. if (!match_attr)
  1687. return -EMSGSIZE;
  1688. err = devlink_dpipe_match_value_put(skb, &values[i]);
  1689. if (err)
  1690. goto err_match_value_put;
  1691. nla_nest_end(skb, match_attr);
  1692. }
  1693. return 0;
  1694. err_match_value_put:
  1695. nla_nest_cancel(skb, match_attr);
  1696. return err;
  1697. }
  1698. static int devlink_dpipe_entry_put(struct sk_buff *skb,
  1699. struct devlink_dpipe_entry *entry)
  1700. {
  1701. struct nlattr *entry_attr, *matches_attr, *actions_attr;
  1702. int err;
  1703. entry_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_ENTRY);
  1704. if (!entry_attr)
  1705. return -EMSGSIZE;
  1706. if (nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_ENTRY_INDEX, entry->index,
  1707. DEVLINK_ATTR_PAD))
  1708. goto nla_put_failure;
  1709. if (entry->counter_valid)
  1710. if (nla_put_u64_64bit(skb, DEVLINK_ATTR_DPIPE_ENTRY_COUNTER,
  1711. entry->counter, DEVLINK_ATTR_PAD))
  1712. goto nla_put_failure;
  1713. matches_attr = nla_nest_start(skb,
  1714. DEVLINK_ATTR_DPIPE_ENTRY_MATCH_VALUES);
  1715. if (!matches_attr)
  1716. goto nla_put_failure;
  1717. err = devlink_dpipe_match_values_put(skb, entry->match_values,
  1718. entry->match_values_count);
  1719. if (err) {
  1720. nla_nest_cancel(skb, matches_attr);
  1721. goto err_match_values_put;
  1722. }
  1723. nla_nest_end(skb, matches_attr);
  1724. actions_attr = nla_nest_start(skb,
  1725. DEVLINK_ATTR_DPIPE_ENTRY_ACTION_VALUES);
  1726. if (!actions_attr)
  1727. goto nla_put_failure;
  1728. err = devlink_dpipe_action_values_put(skb, entry->action_values,
  1729. entry->action_values_count);
  1730. if (err) {
  1731. nla_nest_cancel(skb, actions_attr);
  1732. goto err_action_values_put;
  1733. }
  1734. nla_nest_end(skb, actions_attr);
  1735. nla_nest_end(skb, entry_attr);
  1736. return 0;
  1737. nla_put_failure:
  1738. err = -EMSGSIZE;
  1739. err_match_values_put:
  1740. err_action_values_put:
  1741. nla_nest_cancel(skb, entry_attr);
  1742. return err;
  1743. }
  1744. static struct devlink_dpipe_table *
  1745. devlink_dpipe_table_find(struct list_head *dpipe_tables,
  1746. const char *table_name)
  1747. {
  1748. struct devlink_dpipe_table *table;
  1749. list_for_each_entry_rcu(table, dpipe_tables, list) {
  1750. if (!strcmp(table->name, table_name))
  1751. return table;
  1752. }
  1753. return NULL;
  1754. }
  1755. int devlink_dpipe_entry_ctx_prepare(struct devlink_dpipe_dump_ctx *dump_ctx)
  1756. {
  1757. struct devlink *devlink;
  1758. int err;
  1759. err = devlink_dpipe_send_and_alloc_skb(&dump_ctx->skb,
  1760. dump_ctx->info);
  1761. if (err)
  1762. return err;
  1763. dump_ctx->hdr = genlmsg_put(dump_ctx->skb,
  1764. dump_ctx->info->snd_portid,
  1765. dump_ctx->info->snd_seq,
  1766. &devlink_nl_family, NLM_F_MULTI,
  1767. dump_ctx->cmd);
  1768. if (!dump_ctx->hdr)
  1769. goto nla_put_failure;
  1770. devlink = dump_ctx->info->user_ptr[0];
  1771. if (devlink_nl_put_handle(dump_ctx->skb, devlink))
  1772. goto nla_put_failure;
  1773. dump_ctx->nest = nla_nest_start(dump_ctx->skb,
  1774. DEVLINK_ATTR_DPIPE_ENTRIES);
  1775. if (!dump_ctx->nest)
  1776. goto nla_put_failure;
  1777. return 0;
  1778. nla_put_failure:
  1779. nlmsg_free(dump_ctx->skb);
  1780. return -EMSGSIZE;
  1781. }
  1782. EXPORT_SYMBOL_GPL(devlink_dpipe_entry_ctx_prepare);
  1783. int devlink_dpipe_entry_ctx_append(struct devlink_dpipe_dump_ctx *dump_ctx,
  1784. struct devlink_dpipe_entry *entry)
  1785. {
  1786. return devlink_dpipe_entry_put(dump_ctx->skb, entry);
  1787. }
  1788. EXPORT_SYMBOL_GPL(devlink_dpipe_entry_ctx_append);
  1789. int devlink_dpipe_entry_ctx_close(struct devlink_dpipe_dump_ctx *dump_ctx)
  1790. {
  1791. nla_nest_end(dump_ctx->skb, dump_ctx->nest);
  1792. genlmsg_end(dump_ctx->skb, dump_ctx->hdr);
  1793. return 0;
  1794. }
  1795. EXPORT_SYMBOL_GPL(devlink_dpipe_entry_ctx_close);
  1796. void devlink_dpipe_entry_clear(struct devlink_dpipe_entry *entry)
  1797. {
  1798. unsigned int value_count, value_index;
  1799. struct devlink_dpipe_value *value;
  1800. value = entry->action_values;
  1801. value_count = entry->action_values_count;
  1802. for (value_index = 0; value_index < value_count; value_index++) {
  1803. kfree(value[value_index].value);
  1804. kfree(value[value_index].mask);
  1805. }
  1806. value = entry->match_values;
  1807. value_count = entry->match_values_count;
  1808. for (value_index = 0; value_index < value_count; value_index++) {
  1809. kfree(value[value_index].value);
  1810. kfree(value[value_index].mask);
  1811. }
  1812. }
  1813. EXPORT_SYMBOL(devlink_dpipe_entry_clear);
  1814. static int devlink_dpipe_entries_fill(struct genl_info *info,
  1815. enum devlink_command cmd, int flags,
  1816. struct devlink_dpipe_table *table)
  1817. {
  1818. struct devlink_dpipe_dump_ctx dump_ctx;
  1819. struct nlmsghdr *nlh;
  1820. int err;
  1821. dump_ctx.skb = NULL;
  1822. dump_ctx.cmd = cmd;
  1823. dump_ctx.info = info;
  1824. err = table->table_ops->entries_dump(table->priv,
  1825. table->counters_enabled,
  1826. &dump_ctx);
  1827. if (err)
  1828. return err;
  1829. send_done:
  1830. nlh = nlmsg_put(dump_ctx.skb, info->snd_portid, info->snd_seq,
  1831. NLMSG_DONE, 0, flags | NLM_F_MULTI);
  1832. if (!nlh) {
  1833. err = devlink_dpipe_send_and_alloc_skb(&dump_ctx.skb, info);
  1834. if (err)
  1835. return err;
  1836. goto send_done;
  1837. }
  1838. return genlmsg_reply(dump_ctx.skb, info);
  1839. }
  1840. static int devlink_nl_cmd_dpipe_entries_get(struct sk_buff *skb,
  1841. struct genl_info *info)
  1842. {
  1843. struct devlink *devlink = info->user_ptr[0];
  1844. struct devlink_dpipe_table *table;
  1845. const char *table_name;
  1846. if (!info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME])
  1847. return -EINVAL;
  1848. table_name = nla_data(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME]);
  1849. table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
  1850. table_name);
  1851. if (!table)
  1852. return -EINVAL;
  1853. if (!table->table_ops->entries_dump)
  1854. return -EINVAL;
  1855. return devlink_dpipe_entries_fill(info, DEVLINK_CMD_DPIPE_ENTRIES_GET,
  1856. 0, table);
  1857. }
  1858. static int devlink_dpipe_fields_put(struct sk_buff *skb,
  1859. const struct devlink_dpipe_header *header)
  1860. {
  1861. struct devlink_dpipe_field *field;
  1862. struct nlattr *field_attr;
  1863. int i;
  1864. for (i = 0; i < header->fields_count; i++) {
  1865. field = &header->fields[i];
  1866. field_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_FIELD);
  1867. if (!field_attr)
  1868. return -EMSGSIZE;
  1869. if (nla_put_string(skb, DEVLINK_ATTR_DPIPE_FIELD_NAME, field->name) ||
  1870. nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_ID, field->id) ||
  1871. nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_BITWIDTH, field->bitwidth) ||
  1872. nla_put_u32(skb, DEVLINK_ATTR_DPIPE_FIELD_MAPPING_TYPE, field->mapping_type))
  1873. goto nla_put_failure;
  1874. nla_nest_end(skb, field_attr);
  1875. }
  1876. return 0;
  1877. nla_put_failure:
  1878. nla_nest_cancel(skb, field_attr);
  1879. return -EMSGSIZE;
  1880. }
  1881. static int devlink_dpipe_header_put(struct sk_buff *skb,
  1882. struct devlink_dpipe_header *header)
  1883. {
  1884. struct nlattr *fields_attr, *header_attr;
  1885. int err;
  1886. header_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_HEADER);
  1887. if (!header_attr)
  1888. return -EMSGSIZE;
  1889. if (nla_put_string(skb, DEVLINK_ATTR_DPIPE_HEADER_NAME, header->name) ||
  1890. nla_put_u32(skb, DEVLINK_ATTR_DPIPE_HEADER_ID, header->id) ||
  1891. nla_put_u8(skb, DEVLINK_ATTR_DPIPE_HEADER_GLOBAL, header->global))
  1892. goto nla_put_failure;
  1893. fields_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_HEADER_FIELDS);
  1894. if (!fields_attr)
  1895. goto nla_put_failure;
  1896. err = devlink_dpipe_fields_put(skb, header);
  1897. if (err) {
  1898. nla_nest_cancel(skb, fields_attr);
  1899. goto nla_put_failure;
  1900. }
  1901. nla_nest_end(skb, fields_attr);
  1902. nla_nest_end(skb, header_attr);
  1903. return 0;
  1904. nla_put_failure:
  1905. err = -EMSGSIZE;
  1906. nla_nest_cancel(skb, header_attr);
  1907. return err;
  1908. }
  1909. static int devlink_dpipe_headers_fill(struct genl_info *info,
  1910. enum devlink_command cmd, int flags,
  1911. struct devlink_dpipe_headers *
  1912. dpipe_headers)
  1913. {
  1914. struct devlink *devlink = info->user_ptr[0];
  1915. struct nlattr *headers_attr;
  1916. struct sk_buff *skb = NULL;
  1917. struct nlmsghdr *nlh;
  1918. void *hdr;
  1919. int i, j;
  1920. int err;
  1921. i = 0;
  1922. start_again:
  1923. err = devlink_dpipe_send_and_alloc_skb(&skb, info);
  1924. if (err)
  1925. return err;
  1926. hdr = genlmsg_put(skb, info->snd_portid, info->snd_seq,
  1927. &devlink_nl_family, NLM_F_MULTI, cmd);
  1928. if (!hdr) {
  1929. nlmsg_free(skb);
  1930. return -EMSGSIZE;
  1931. }
  1932. if (devlink_nl_put_handle(skb, devlink))
  1933. goto nla_put_failure;
  1934. headers_attr = nla_nest_start(skb, DEVLINK_ATTR_DPIPE_HEADERS);
  1935. if (!headers_attr)
  1936. goto nla_put_failure;
  1937. j = 0;
  1938. for (; i < dpipe_headers->headers_count; i++) {
  1939. err = devlink_dpipe_header_put(skb, dpipe_headers->headers[i]);
  1940. if (err) {
  1941. if (!j)
  1942. goto err_table_put;
  1943. break;
  1944. }
  1945. j++;
  1946. }
  1947. nla_nest_end(skb, headers_attr);
  1948. genlmsg_end(skb, hdr);
  1949. if (i != dpipe_headers->headers_count)
  1950. goto start_again;
  1951. send_done:
  1952. nlh = nlmsg_put(skb, info->snd_portid, info->snd_seq,
  1953. NLMSG_DONE, 0, flags | NLM_F_MULTI);
  1954. if (!nlh) {
  1955. err = devlink_dpipe_send_and_alloc_skb(&skb, info);
  1956. if (err)
  1957. return err;
  1958. goto send_done;
  1959. }
  1960. return genlmsg_reply(skb, info);
  1961. nla_put_failure:
  1962. err = -EMSGSIZE;
  1963. err_table_put:
  1964. nlmsg_free(skb);
  1965. return err;
  1966. }
  1967. static int devlink_nl_cmd_dpipe_headers_get(struct sk_buff *skb,
  1968. struct genl_info *info)
  1969. {
  1970. struct devlink *devlink = info->user_ptr[0];
  1971. if (!devlink->dpipe_headers)
  1972. return -EOPNOTSUPP;
  1973. return devlink_dpipe_headers_fill(info, DEVLINK_CMD_DPIPE_HEADERS_GET,
  1974. 0, devlink->dpipe_headers);
  1975. }
  1976. static int devlink_dpipe_table_counters_set(struct devlink *devlink,
  1977. const char *table_name,
  1978. bool enable)
  1979. {
  1980. struct devlink_dpipe_table *table;
  1981. table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
  1982. table_name);
  1983. if (!table)
  1984. return -EINVAL;
  1985. if (table->counter_control_extern)
  1986. return -EOPNOTSUPP;
  1987. if (!(table->counters_enabled ^ enable))
  1988. return 0;
  1989. table->counters_enabled = enable;
  1990. if (table->table_ops->counters_set_update)
  1991. table->table_ops->counters_set_update(table->priv, enable);
  1992. return 0;
  1993. }
  1994. static int devlink_nl_cmd_dpipe_table_counters_set(struct sk_buff *skb,
  1995. struct genl_info *info)
  1996. {
  1997. struct devlink *devlink = info->user_ptr[0];
  1998. const char *table_name;
  1999. bool counters_enable;
  2000. if (!info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME] ||
  2001. !info->attrs[DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED])
  2002. return -EINVAL;
  2003. table_name = nla_data(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_NAME]);
  2004. counters_enable = !!nla_get_u8(info->attrs[DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED]);
  2005. return devlink_dpipe_table_counters_set(devlink, table_name,
  2006. counters_enable);
  2007. }
  2008. static struct devlink_resource *
  2009. devlink_resource_find(struct devlink *devlink,
  2010. struct devlink_resource *resource, u64 resource_id)
  2011. {
  2012. struct list_head *resource_list;
  2013. if (resource)
  2014. resource_list = &resource->resource_list;
  2015. else
  2016. resource_list = &devlink->resource_list;
  2017. list_for_each_entry(resource, resource_list, list) {
  2018. struct devlink_resource *child_resource;
  2019. if (resource->id == resource_id)
  2020. return resource;
  2021. child_resource = devlink_resource_find(devlink, resource,
  2022. resource_id);
  2023. if (child_resource)
  2024. return child_resource;
  2025. }
  2026. return NULL;
  2027. }
  2028. static void
  2029. devlink_resource_validate_children(struct devlink_resource *resource)
  2030. {
  2031. struct devlink_resource *child_resource;
  2032. bool size_valid = true;
  2033. u64 parts_size = 0;
  2034. if (list_empty(&resource->resource_list))
  2035. goto out;
  2036. list_for_each_entry(child_resource, &resource->resource_list, list)
  2037. parts_size += child_resource->size_new;
  2038. if (parts_size > resource->size_new)
  2039. size_valid = false;
  2040. out:
  2041. resource->size_valid = size_valid;
  2042. }
  2043. static int
  2044. devlink_resource_validate_size(struct devlink_resource *resource, u64 size,
  2045. struct netlink_ext_ack *extack)
  2046. {
  2047. u64 reminder;
  2048. int err = 0;
  2049. if (size > resource->size_params.size_max) {
  2050. NL_SET_ERR_MSG_MOD(extack, "Size larger than maximum");
  2051. err = -EINVAL;
  2052. }
  2053. if (size < resource->size_params.size_min) {
  2054. NL_SET_ERR_MSG_MOD(extack, "Size smaller than minimum");
  2055. err = -EINVAL;
  2056. }
  2057. div64_u64_rem(size, resource->size_params.size_granularity, &reminder);
  2058. if (reminder) {
  2059. NL_SET_ERR_MSG_MOD(extack, "Wrong granularity");
  2060. err = -EINVAL;
  2061. }
  2062. return err;
  2063. }
  2064. static int devlink_nl_cmd_resource_set(struct sk_buff *skb,
  2065. struct genl_info *info)
  2066. {
  2067. struct devlink *devlink = info->user_ptr[0];
  2068. struct devlink_resource *resource;
  2069. u64 resource_id;
  2070. u64 size;
  2071. int err;
  2072. if (!info->attrs[DEVLINK_ATTR_RESOURCE_ID] ||
  2073. !info->attrs[DEVLINK_ATTR_RESOURCE_SIZE])
  2074. return -EINVAL;
  2075. resource_id = nla_get_u64(info->attrs[DEVLINK_ATTR_RESOURCE_ID]);
  2076. resource = devlink_resource_find(devlink, NULL, resource_id);
  2077. if (!resource)
  2078. return -EINVAL;
  2079. size = nla_get_u64(info->attrs[DEVLINK_ATTR_RESOURCE_SIZE]);
  2080. err = devlink_resource_validate_size(resource, size, info->extack);
  2081. if (err)
  2082. return err;
  2083. resource->size_new = size;
  2084. devlink_resource_validate_children(resource);
  2085. if (resource->parent)
  2086. devlink_resource_validate_children(resource->parent);
  2087. return 0;
  2088. }
  2089. static int
  2090. devlink_resource_size_params_put(struct devlink_resource *resource,
  2091. struct sk_buff *skb)
  2092. {
  2093. struct devlink_resource_size_params *size_params;
  2094. size_params = &resource->size_params;
  2095. if (nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE_GRAN,
  2096. size_params->size_granularity, DEVLINK_ATTR_PAD) ||
  2097. nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE_MAX,
  2098. size_params->size_max, DEVLINK_ATTR_PAD) ||
  2099. nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE_MIN,
  2100. size_params->size_min, DEVLINK_ATTR_PAD) ||
  2101. nla_put_u8(skb, DEVLINK_ATTR_RESOURCE_UNIT, size_params->unit))
  2102. return -EMSGSIZE;
  2103. return 0;
  2104. }
  2105. static int devlink_resource_occ_put(struct devlink_resource *resource,
  2106. struct sk_buff *skb)
  2107. {
  2108. if (!resource->occ_get)
  2109. return 0;
  2110. return nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_OCC,
  2111. resource->occ_get(resource->occ_get_priv),
  2112. DEVLINK_ATTR_PAD);
  2113. }
  2114. static int devlink_resource_put(struct devlink *devlink, struct sk_buff *skb,
  2115. struct devlink_resource *resource)
  2116. {
  2117. struct devlink_resource *child_resource;
  2118. struct nlattr *child_resource_attr;
  2119. struct nlattr *resource_attr;
  2120. resource_attr = nla_nest_start(skb, DEVLINK_ATTR_RESOURCE);
  2121. if (!resource_attr)
  2122. return -EMSGSIZE;
  2123. if (nla_put_string(skb, DEVLINK_ATTR_RESOURCE_NAME, resource->name) ||
  2124. nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE, resource->size,
  2125. DEVLINK_ATTR_PAD) ||
  2126. nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_ID, resource->id,
  2127. DEVLINK_ATTR_PAD))
  2128. goto nla_put_failure;
  2129. if (resource->size != resource->size_new)
  2130. nla_put_u64_64bit(skb, DEVLINK_ATTR_RESOURCE_SIZE_NEW,
  2131. resource->size_new, DEVLINK_ATTR_PAD);
  2132. if (devlink_resource_occ_put(resource, skb))
  2133. goto nla_put_failure;
  2134. if (devlink_resource_size_params_put(resource, skb))
  2135. goto nla_put_failure;
  2136. if (list_empty(&resource->resource_list))
  2137. goto out;
  2138. if (nla_put_u8(skb, DEVLINK_ATTR_RESOURCE_SIZE_VALID,
  2139. resource->size_valid))
  2140. goto nla_put_failure;
  2141. child_resource_attr = nla_nest_start(skb, DEVLINK_ATTR_RESOURCE_LIST);
  2142. if (!child_resource_attr)
  2143. goto nla_put_failure;
  2144. list_for_each_entry(child_resource, &resource->resource_list, list) {
  2145. if (devlink_resource_put(devlink, skb, child_resource))
  2146. goto resource_put_failure;
  2147. }
  2148. nla_nest_end(skb, child_resource_attr);
  2149. out:
  2150. nla_nest_end(skb, resource_attr);
  2151. return 0;
  2152. resource_put_failure:
  2153. nla_nest_cancel(skb, child_resource_attr);
  2154. nla_put_failure:
  2155. nla_nest_cancel(skb, resource_attr);
  2156. return -EMSGSIZE;
  2157. }
  2158. static int devlink_resource_fill(struct genl_info *info,
  2159. enum devlink_command cmd, int flags)
  2160. {
  2161. struct devlink *devlink = info->user_ptr[0];
  2162. struct devlink_resource *resource;
  2163. struct nlattr *resources_attr;
  2164. struct sk_buff *skb = NULL;
  2165. struct nlmsghdr *nlh;
  2166. bool incomplete;
  2167. void *hdr;
  2168. int i;
  2169. int err;
  2170. resource = list_first_entry(&devlink->resource_list,
  2171. struct devlink_resource, list);
  2172. start_again:
  2173. err = devlink_dpipe_send_and_alloc_skb(&skb, info);
  2174. if (err)
  2175. return err;
  2176. hdr = genlmsg_put(skb, info->snd_portid, info->snd_seq,
  2177. &devlink_nl_family, NLM_F_MULTI, cmd);
  2178. if (!hdr) {
  2179. nlmsg_free(skb);
  2180. return -EMSGSIZE;
  2181. }
  2182. if (devlink_nl_put_handle(skb, devlink))
  2183. goto nla_put_failure;
  2184. resources_attr = nla_nest_start(skb, DEVLINK_ATTR_RESOURCE_LIST);
  2185. if (!resources_attr)
  2186. goto nla_put_failure;
  2187. incomplete = false;
  2188. i = 0;
  2189. list_for_each_entry_from(resource, &devlink->resource_list, list) {
  2190. err = devlink_resource_put(devlink, skb, resource);
  2191. if (err) {
  2192. if (!i)
  2193. goto err_resource_put;
  2194. incomplete = true;
  2195. break;
  2196. }
  2197. i++;
  2198. }
  2199. nla_nest_end(skb, resources_attr);
  2200. genlmsg_end(skb, hdr);
  2201. if (incomplete)
  2202. goto start_again;
  2203. send_done:
  2204. nlh = nlmsg_put(skb, info->snd_portid, info->snd_seq,
  2205. NLMSG_DONE, 0, flags | NLM_F_MULTI);
  2206. if (!nlh) {
  2207. err = devlink_dpipe_send_and_alloc_skb(&skb, info);
  2208. if (err)
  2209. return err;
  2210. goto send_done;
  2211. }
  2212. return genlmsg_reply(skb, info);
  2213. nla_put_failure:
  2214. err = -EMSGSIZE;
  2215. err_resource_put:
  2216. nlmsg_free(skb);
  2217. return err;
  2218. }
  2219. static int devlink_nl_cmd_resource_dump(struct sk_buff *skb,
  2220. struct genl_info *info)
  2221. {
  2222. struct devlink *devlink = info->user_ptr[0];
  2223. if (list_empty(&devlink->resource_list))
  2224. return -EOPNOTSUPP;
  2225. return devlink_resource_fill(info, DEVLINK_CMD_RESOURCE_DUMP, 0);
  2226. }
  2227. static int
  2228. devlink_resources_validate(struct devlink *devlink,
  2229. struct devlink_resource *resource,
  2230. struct genl_info *info)
  2231. {
  2232. struct list_head *resource_list;
  2233. int err = 0;
  2234. if (resource)
  2235. resource_list = &resource->resource_list;
  2236. else
  2237. resource_list = &devlink->resource_list;
  2238. list_for_each_entry(resource, resource_list, list) {
  2239. if (!resource->size_valid)
  2240. return -EINVAL;
  2241. err = devlink_resources_validate(devlink, resource, info);
  2242. if (err)
  2243. return err;
  2244. }
  2245. return err;
  2246. }
  2247. static int devlink_nl_cmd_reload(struct sk_buff *skb, struct genl_info *info)
  2248. {
  2249. struct devlink *devlink = info->user_ptr[0];
  2250. int err;
  2251. if (!devlink->ops->reload)
  2252. return -EOPNOTSUPP;
  2253. err = devlink_resources_validate(devlink, NULL, info);
  2254. if (err) {
  2255. NL_SET_ERR_MSG_MOD(info->extack, "resources size validation failed");
  2256. return err;
  2257. }
  2258. return devlink->ops->reload(devlink, info->extack);
  2259. }
  2260. static const struct devlink_param devlink_param_generic[] = {
  2261. {
  2262. .id = DEVLINK_PARAM_GENERIC_ID_INT_ERR_RESET,
  2263. .name = DEVLINK_PARAM_GENERIC_INT_ERR_RESET_NAME,
  2264. .type = DEVLINK_PARAM_GENERIC_INT_ERR_RESET_TYPE,
  2265. },
  2266. {
  2267. .id = DEVLINK_PARAM_GENERIC_ID_MAX_MACS,
  2268. .name = DEVLINK_PARAM_GENERIC_MAX_MACS_NAME,
  2269. .type = DEVLINK_PARAM_GENERIC_MAX_MACS_TYPE,
  2270. },
  2271. {
  2272. .id = DEVLINK_PARAM_GENERIC_ID_ENABLE_SRIOV,
  2273. .name = DEVLINK_PARAM_GENERIC_ENABLE_SRIOV_NAME,
  2274. .type = DEVLINK_PARAM_GENERIC_ENABLE_SRIOV_TYPE,
  2275. },
  2276. {
  2277. .id = DEVLINK_PARAM_GENERIC_ID_REGION_SNAPSHOT,
  2278. .name = DEVLINK_PARAM_GENERIC_REGION_SNAPSHOT_NAME,
  2279. .type = DEVLINK_PARAM_GENERIC_REGION_SNAPSHOT_TYPE,
  2280. },
  2281. };
  2282. static int devlink_param_generic_verify(const struct devlink_param *param)
  2283. {
  2284. /* verify it match generic parameter by id and name */
  2285. if (param->id > DEVLINK_PARAM_GENERIC_ID_MAX)
  2286. return -EINVAL;
  2287. if (strcmp(param->name, devlink_param_generic[param->id].name))
  2288. return -ENOENT;
  2289. WARN_ON(param->type != devlink_param_generic[param->id].type);
  2290. return 0;
  2291. }
  2292. static int devlink_param_driver_verify(const struct devlink_param *param)
  2293. {
  2294. int i;
  2295. if (param->id <= DEVLINK_PARAM_GENERIC_ID_MAX)
  2296. return -EINVAL;
  2297. /* verify no such name in generic params */
  2298. for (i = 0; i <= DEVLINK_PARAM_GENERIC_ID_MAX; i++)
  2299. if (!strcmp(param->name, devlink_param_generic[i].name))
  2300. return -EEXIST;
  2301. return 0;
  2302. }
  2303. static struct devlink_param_item *
  2304. devlink_param_find_by_name(struct list_head *param_list,
  2305. const char *param_name)
  2306. {
  2307. struct devlink_param_item *param_item;
  2308. list_for_each_entry(param_item, param_list, list)
  2309. if (!strcmp(param_item->param->name, param_name))
  2310. return param_item;
  2311. return NULL;
  2312. }
  2313. static struct devlink_param_item *
  2314. devlink_param_find_by_id(struct list_head *param_list, u32 param_id)
  2315. {
  2316. struct devlink_param_item *param_item;
  2317. list_for_each_entry(param_item, param_list, list)
  2318. if (param_item->param->id == param_id)
  2319. return param_item;
  2320. return NULL;
  2321. }
  2322. static bool
  2323. devlink_param_cmode_is_supported(const struct devlink_param *param,
  2324. enum devlink_param_cmode cmode)
  2325. {
  2326. return test_bit(cmode, &param->supported_cmodes);
  2327. }
  2328. static int devlink_param_get(struct devlink *devlink,
  2329. const struct devlink_param *param,
  2330. struct devlink_param_gset_ctx *ctx)
  2331. {
  2332. if (!param->get)
  2333. return -EOPNOTSUPP;
  2334. return param->get(devlink, param->id, ctx);
  2335. }
  2336. static int devlink_param_set(struct devlink *devlink,
  2337. const struct devlink_param *param,
  2338. struct devlink_param_gset_ctx *ctx)
  2339. {
  2340. if (!param->set)
  2341. return -EOPNOTSUPP;
  2342. return param->set(devlink, param->id, ctx);
  2343. }
  2344. static int
  2345. devlink_param_type_to_nla_type(enum devlink_param_type param_type)
  2346. {
  2347. switch (param_type) {
  2348. case DEVLINK_PARAM_TYPE_U8:
  2349. return NLA_U8;
  2350. case DEVLINK_PARAM_TYPE_U16:
  2351. return NLA_U16;
  2352. case DEVLINK_PARAM_TYPE_U32:
  2353. return NLA_U32;
  2354. case DEVLINK_PARAM_TYPE_STRING:
  2355. return NLA_STRING;
  2356. case DEVLINK_PARAM_TYPE_BOOL:
  2357. return NLA_FLAG;
  2358. default:
  2359. return -EINVAL;
  2360. }
  2361. }
  2362. static int
  2363. devlink_nl_param_value_fill_one(struct sk_buff *msg,
  2364. enum devlink_param_type type,
  2365. enum devlink_param_cmode cmode,
  2366. union devlink_param_value val)
  2367. {
  2368. struct nlattr *param_value_attr;
  2369. param_value_attr = nla_nest_start(msg, DEVLINK_ATTR_PARAM_VALUE);
  2370. if (!param_value_attr)
  2371. goto nla_put_failure;
  2372. if (nla_put_u8(msg, DEVLINK_ATTR_PARAM_VALUE_CMODE, cmode))
  2373. goto value_nest_cancel;
  2374. switch (type) {
  2375. case DEVLINK_PARAM_TYPE_U8:
  2376. if (nla_put_u8(msg, DEVLINK_ATTR_PARAM_VALUE_DATA, val.vu8))
  2377. goto value_nest_cancel;
  2378. break;
  2379. case DEVLINK_PARAM_TYPE_U16:
  2380. if (nla_put_u16(msg, DEVLINK_ATTR_PARAM_VALUE_DATA, val.vu16))
  2381. goto value_nest_cancel;
  2382. break;
  2383. case DEVLINK_PARAM_TYPE_U32:
  2384. if (nla_put_u32(msg, DEVLINK_ATTR_PARAM_VALUE_DATA, val.vu32))
  2385. goto value_nest_cancel;
  2386. break;
  2387. case DEVLINK_PARAM_TYPE_STRING:
  2388. if (nla_put_string(msg, DEVLINK_ATTR_PARAM_VALUE_DATA,
  2389. val.vstr))
  2390. goto value_nest_cancel;
  2391. break;
  2392. case DEVLINK_PARAM_TYPE_BOOL:
  2393. if (val.vbool &&
  2394. nla_put_flag(msg, DEVLINK_ATTR_PARAM_VALUE_DATA))
  2395. goto value_nest_cancel;
  2396. break;
  2397. }
  2398. nla_nest_end(msg, param_value_attr);
  2399. return 0;
  2400. value_nest_cancel:
  2401. nla_nest_cancel(msg, param_value_attr);
  2402. nla_put_failure:
  2403. return -EMSGSIZE;
  2404. }
  2405. static int devlink_nl_param_fill(struct sk_buff *msg, struct devlink *devlink,
  2406. struct devlink_param_item *param_item,
  2407. enum devlink_command cmd,
  2408. u32 portid, u32 seq, int flags)
  2409. {
  2410. union devlink_param_value param_value[DEVLINK_PARAM_CMODE_MAX + 1];
  2411. const struct devlink_param *param = param_item->param;
  2412. struct devlink_param_gset_ctx ctx;
  2413. struct nlattr *param_values_list;
  2414. struct nlattr *param_attr;
  2415. int nla_type;
  2416. void *hdr;
  2417. int err;
  2418. int i;
  2419. /* Get value from driver part to driverinit configuration mode */
  2420. for (i = 0; i <= DEVLINK_PARAM_CMODE_MAX; i++) {
  2421. if (!devlink_param_cmode_is_supported(param, i))
  2422. continue;
  2423. if (i == DEVLINK_PARAM_CMODE_DRIVERINIT) {
  2424. if (!param_item->driverinit_value_valid)
  2425. return -EOPNOTSUPP;
  2426. param_value[i] = param_item->driverinit_value;
  2427. } else {
  2428. ctx.cmode = i;
  2429. err = devlink_param_get(devlink, param, &ctx);
  2430. if (err)
  2431. return err;
  2432. param_value[i] = ctx.val;
  2433. }
  2434. }
  2435. hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
  2436. if (!hdr)
  2437. return -EMSGSIZE;
  2438. if (devlink_nl_put_handle(msg, devlink))
  2439. goto genlmsg_cancel;
  2440. param_attr = nla_nest_start(msg, DEVLINK_ATTR_PARAM);
  2441. if (!param_attr)
  2442. goto genlmsg_cancel;
  2443. if (nla_put_string(msg, DEVLINK_ATTR_PARAM_NAME, param->name))
  2444. goto param_nest_cancel;
  2445. if (param->generic && nla_put_flag(msg, DEVLINK_ATTR_PARAM_GENERIC))
  2446. goto param_nest_cancel;
  2447. nla_type = devlink_param_type_to_nla_type(param->type);
  2448. if (nla_type < 0)
  2449. goto param_nest_cancel;
  2450. if (nla_put_u8(msg, DEVLINK_ATTR_PARAM_TYPE, nla_type))
  2451. goto param_nest_cancel;
  2452. param_values_list = nla_nest_start(msg, DEVLINK_ATTR_PARAM_VALUES_LIST);
  2453. if (!param_values_list)
  2454. goto param_nest_cancel;
  2455. for (i = 0; i <= DEVLINK_PARAM_CMODE_MAX; i++) {
  2456. if (!devlink_param_cmode_is_supported(param, i))
  2457. continue;
  2458. err = devlink_nl_param_value_fill_one(msg, param->type,
  2459. i, param_value[i]);
  2460. if (err)
  2461. goto values_list_nest_cancel;
  2462. }
  2463. nla_nest_end(msg, param_values_list);
  2464. nla_nest_end(msg, param_attr);
  2465. genlmsg_end(msg, hdr);
  2466. return 0;
  2467. values_list_nest_cancel:
  2468. nla_nest_end(msg, param_values_list);
  2469. param_nest_cancel:
  2470. nla_nest_cancel(msg, param_attr);
  2471. genlmsg_cancel:
  2472. genlmsg_cancel(msg, hdr);
  2473. return -EMSGSIZE;
  2474. }
  2475. static void devlink_param_notify(struct devlink *devlink,
  2476. struct devlink_param_item *param_item,
  2477. enum devlink_command cmd)
  2478. {
  2479. struct sk_buff *msg;
  2480. int err;
  2481. WARN_ON(cmd != DEVLINK_CMD_PARAM_NEW && cmd != DEVLINK_CMD_PARAM_DEL);
  2482. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  2483. if (!msg)
  2484. return;
  2485. err = devlink_nl_param_fill(msg, devlink, param_item, cmd, 0, 0, 0);
  2486. if (err) {
  2487. nlmsg_free(msg);
  2488. return;
  2489. }
  2490. genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
  2491. msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
  2492. }
  2493. static int devlink_nl_cmd_param_get_dumpit(struct sk_buff *msg,
  2494. struct netlink_callback *cb)
  2495. {
  2496. struct devlink_param_item *param_item;
  2497. struct devlink *devlink;
  2498. int start = cb->args[0];
  2499. int idx = 0;
  2500. int err;
  2501. mutex_lock(&devlink_mutex);
  2502. list_for_each_entry(devlink, &devlink_list, list) {
  2503. if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
  2504. continue;
  2505. mutex_lock(&devlink->lock);
  2506. list_for_each_entry(param_item, &devlink->param_list, list) {
  2507. if (idx < start) {
  2508. idx++;
  2509. continue;
  2510. }
  2511. err = devlink_nl_param_fill(msg, devlink, param_item,
  2512. DEVLINK_CMD_PARAM_GET,
  2513. NETLINK_CB(cb->skb).portid,
  2514. cb->nlh->nlmsg_seq,
  2515. NLM_F_MULTI);
  2516. if (err) {
  2517. mutex_unlock(&devlink->lock);
  2518. goto out;
  2519. }
  2520. idx++;
  2521. }
  2522. mutex_unlock(&devlink->lock);
  2523. }
  2524. out:
  2525. mutex_unlock(&devlink_mutex);
  2526. cb->args[0] = idx;
  2527. return msg->len;
  2528. }
  2529. static int
  2530. devlink_param_type_get_from_info(struct genl_info *info,
  2531. enum devlink_param_type *param_type)
  2532. {
  2533. if (!info->attrs[DEVLINK_ATTR_PARAM_TYPE])
  2534. return -EINVAL;
  2535. switch (nla_get_u8(info->attrs[DEVLINK_ATTR_PARAM_TYPE])) {
  2536. case NLA_U8:
  2537. *param_type = DEVLINK_PARAM_TYPE_U8;
  2538. break;
  2539. case NLA_U16:
  2540. *param_type = DEVLINK_PARAM_TYPE_U16;
  2541. break;
  2542. case NLA_U32:
  2543. *param_type = DEVLINK_PARAM_TYPE_U32;
  2544. break;
  2545. case NLA_STRING:
  2546. *param_type = DEVLINK_PARAM_TYPE_STRING;
  2547. break;
  2548. case NLA_FLAG:
  2549. *param_type = DEVLINK_PARAM_TYPE_BOOL;
  2550. break;
  2551. default:
  2552. return -EINVAL;
  2553. }
  2554. return 0;
  2555. }
  2556. static int
  2557. devlink_param_value_get_from_info(const struct devlink_param *param,
  2558. struct genl_info *info,
  2559. union devlink_param_value *value)
  2560. {
  2561. struct nlattr *param_data;
  2562. int len;
  2563. param_data = info->attrs[DEVLINK_ATTR_PARAM_VALUE_DATA];
  2564. if (param->type != DEVLINK_PARAM_TYPE_BOOL && !param_data)
  2565. return -EINVAL;
  2566. switch (param->type) {
  2567. case DEVLINK_PARAM_TYPE_U8:
  2568. if (nla_len(param_data) != sizeof(u8))
  2569. return -EINVAL;
  2570. value->vu8 = nla_get_u8(param_data);
  2571. break;
  2572. case DEVLINK_PARAM_TYPE_U16:
  2573. if (nla_len(param_data) != sizeof(u16))
  2574. return -EINVAL;
  2575. value->vu16 = nla_get_u16(param_data);
  2576. break;
  2577. case DEVLINK_PARAM_TYPE_U32:
  2578. if (nla_len(param_data) != sizeof(u32))
  2579. return -EINVAL;
  2580. value->vu32 = nla_get_u32(param_data);
  2581. break;
  2582. case DEVLINK_PARAM_TYPE_STRING:
  2583. len = strnlen(nla_data(param_data), nla_len(param_data));
  2584. if (len == nla_len(param_data) ||
  2585. len >= __DEVLINK_PARAM_MAX_STRING_VALUE)
  2586. return -EINVAL;
  2587. strcpy(value->vstr, nla_data(param_data));
  2588. break;
  2589. case DEVLINK_PARAM_TYPE_BOOL:
  2590. if (param_data && nla_len(param_data))
  2591. return -EINVAL;
  2592. value->vbool = nla_get_flag(param_data);
  2593. break;
  2594. }
  2595. return 0;
  2596. }
  2597. static struct devlink_param_item *
  2598. devlink_param_get_from_info(struct devlink *devlink,
  2599. struct genl_info *info)
  2600. {
  2601. char *param_name;
  2602. if (!info->attrs[DEVLINK_ATTR_PARAM_NAME])
  2603. return NULL;
  2604. param_name = nla_data(info->attrs[DEVLINK_ATTR_PARAM_NAME]);
  2605. return devlink_param_find_by_name(&devlink->param_list, param_name);
  2606. }
  2607. static int devlink_nl_cmd_param_get_doit(struct sk_buff *skb,
  2608. struct genl_info *info)
  2609. {
  2610. struct devlink *devlink = info->user_ptr[0];
  2611. struct devlink_param_item *param_item;
  2612. struct sk_buff *msg;
  2613. int err;
  2614. param_item = devlink_param_get_from_info(devlink, info);
  2615. if (!param_item)
  2616. return -EINVAL;
  2617. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  2618. if (!msg)
  2619. return -ENOMEM;
  2620. err = devlink_nl_param_fill(msg, devlink, param_item,
  2621. DEVLINK_CMD_PARAM_GET,
  2622. info->snd_portid, info->snd_seq, 0);
  2623. if (err) {
  2624. nlmsg_free(msg);
  2625. return err;
  2626. }
  2627. return genlmsg_reply(msg, info);
  2628. }
  2629. static int devlink_nl_cmd_param_set_doit(struct sk_buff *skb,
  2630. struct genl_info *info)
  2631. {
  2632. struct devlink *devlink = info->user_ptr[0];
  2633. enum devlink_param_type param_type;
  2634. struct devlink_param_gset_ctx ctx;
  2635. enum devlink_param_cmode cmode;
  2636. struct devlink_param_item *param_item;
  2637. const struct devlink_param *param;
  2638. union devlink_param_value value;
  2639. int err = 0;
  2640. param_item = devlink_param_get_from_info(devlink, info);
  2641. if (!param_item)
  2642. return -EINVAL;
  2643. param = param_item->param;
  2644. err = devlink_param_type_get_from_info(info, &param_type);
  2645. if (err)
  2646. return err;
  2647. if (param_type != param->type)
  2648. return -EINVAL;
  2649. err = devlink_param_value_get_from_info(param, info, &value);
  2650. if (err)
  2651. return err;
  2652. if (param->validate) {
  2653. err = param->validate(devlink, param->id, value, info->extack);
  2654. if (err)
  2655. return err;
  2656. }
  2657. if (!info->attrs[DEVLINK_ATTR_PARAM_VALUE_CMODE])
  2658. return -EINVAL;
  2659. cmode = nla_get_u8(info->attrs[DEVLINK_ATTR_PARAM_VALUE_CMODE]);
  2660. if (!devlink_param_cmode_is_supported(param, cmode))
  2661. return -EOPNOTSUPP;
  2662. if (cmode == DEVLINK_PARAM_CMODE_DRIVERINIT) {
  2663. if (param->type == DEVLINK_PARAM_TYPE_STRING)
  2664. strcpy(param_item->driverinit_value.vstr, value.vstr);
  2665. else
  2666. param_item->driverinit_value = value;
  2667. param_item->driverinit_value_valid = true;
  2668. } else {
  2669. if (!param->set)
  2670. return -EOPNOTSUPP;
  2671. ctx.val = value;
  2672. ctx.cmode = cmode;
  2673. err = devlink_param_set(devlink, param, &ctx);
  2674. if (err)
  2675. return err;
  2676. }
  2677. devlink_param_notify(devlink, param_item, DEVLINK_CMD_PARAM_NEW);
  2678. return 0;
  2679. }
  2680. static int devlink_param_register_one(struct devlink *devlink,
  2681. const struct devlink_param *param)
  2682. {
  2683. struct devlink_param_item *param_item;
  2684. if (devlink_param_find_by_name(&devlink->param_list,
  2685. param->name))
  2686. return -EEXIST;
  2687. if (param->supported_cmodes == BIT(DEVLINK_PARAM_CMODE_DRIVERINIT))
  2688. WARN_ON(param->get || param->set);
  2689. else
  2690. WARN_ON(!param->get || !param->set);
  2691. param_item = kzalloc(sizeof(*param_item), GFP_KERNEL);
  2692. if (!param_item)
  2693. return -ENOMEM;
  2694. param_item->param = param;
  2695. list_add_tail(&param_item->list, &devlink->param_list);
  2696. devlink_param_notify(devlink, param_item, DEVLINK_CMD_PARAM_NEW);
  2697. return 0;
  2698. }
  2699. static void devlink_param_unregister_one(struct devlink *devlink,
  2700. const struct devlink_param *param)
  2701. {
  2702. struct devlink_param_item *param_item;
  2703. param_item = devlink_param_find_by_name(&devlink->param_list,
  2704. param->name);
  2705. WARN_ON(!param_item);
  2706. devlink_param_notify(devlink, param_item, DEVLINK_CMD_PARAM_DEL);
  2707. list_del(&param_item->list);
  2708. kfree(param_item);
  2709. }
  2710. static int devlink_nl_region_snapshot_id_put(struct sk_buff *msg,
  2711. struct devlink *devlink,
  2712. struct devlink_snapshot *snapshot)
  2713. {
  2714. struct nlattr *snap_attr;
  2715. int err;
  2716. snap_attr = nla_nest_start(msg, DEVLINK_ATTR_REGION_SNAPSHOT);
  2717. if (!snap_attr)
  2718. return -EINVAL;
  2719. err = nla_put_u32(msg, DEVLINK_ATTR_REGION_SNAPSHOT_ID, snapshot->id);
  2720. if (err)
  2721. goto nla_put_failure;
  2722. nla_nest_end(msg, snap_attr);
  2723. return 0;
  2724. nla_put_failure:
  2725. nla_nest_cancel(msg, snap_attr);
  2726. return err;
  2727. }
  2728. static int devlink_nl_region_snapshots_id_put(struct sk_buff *msg,
  2729. struct devlink *devlink,
  2730. struct devlink_region *region)
  2731. {
  2732. struct devlink_snapshot *snapshot;
  2733. struct nlattr *snapshots_attr;
  2734. int err;
  2735. snapshots_attr = nla_nest_start(msg, DEVLINK_ATTR_REGION_SNAPSHOTS);
  2736. if (!snapshots_attr)
  2737. return -EINVAL;
  2738. list_for_each_entry(snapshot, &region->snapshot_list, list) {
  2739. err = devlink_nl_region_snapshot_id_put(msg, devlink, snapshot);
  2740. if (err)
  2741. goto nla_put_failure;
  2742. }
  2743. nla_nest_end(msg, snapshots_attr);
  2744. return 0;
  2745. nla_put_failure:
  2746. nla_nest_cancel(msg, snapshots_attr);
  2747. return err;
  2748. }
  2749. static int devlink_nl_region_fill(struct sk_buff *msg, struct devlink *devlink,
  2750. enum devlink_command cmd, u32 portid,
  2751. u32 seq, int flags,
  2752. struct devlink_region *region)
  2753. {
  2754. void *hdr;
  2755. int err;
  2756. hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
  2757. if (!hdr)
  2758. return -EMSGSIZE;
  2759. err = devlink_nl_put_handle(msg, devlink);
  2760. if (err)
  2761. goto nla_put_failure;
  2762. err = nla_put_string(msg, DEVLINK_ATTR_REGION_NAME, region->name);
  2763. if (err)
  2764. goto nla_put_failure;
  2765. err = nla_put_u64_64bit(msg, DEVLINK_ATTR_REGION_SIZE,
  2766. region->size,
  2767. DEVLINK_ATTR_PAD);
  2768. if (err)
  2769. goto nla_put_failure;
  2770. err = devlink_nl_region_snapshots_id_put(msg, devlink, region);
  2771. if (err)
  2772. goto nla_put_failure;
  2773. genlmsg_end(msg, hdr);
  2774. return 0;
  2775. nla_put_failure:
  2776. genlmsg_cancel(msg, hdr);
  2777. return err;
  2778. }
  2779. static void devlink_nl_region_notify(struct devlink_region *region,
  2780. struct devlink_snapshot *snapshot,
  2781. enum devlink_command cmd)
  2782. {
  2783. struct devlink *devlink = region->devlink;
  2784. struct sk_buff *msg;
  2785. void *hdr;
  2786. int err;
  2787. WARN_ON(cmd != DEVLINK_CMD_REGION_NEW && cmd != DEVLINK_CMD_REGION_DEL);
  2788. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  2789. if (!msg)
  2790. return;
  2791. hdr = genlmsg_put(msg, 0, 0, &devlink_nl_family, 0, cmd);
  2792. if (!hdr)
  2793. goto out_free_msg;
  2794. err = devlink_nl_put_handle(msg, devlink);
  2795. if (err)
  2796. goto out_cancel_msg;
  2797. err = nla_put_string(msg, DEVLINK_ATTR_REGION_NAME,
  2798. region->name);
  2799. if (err)
  2800. goto out_cancel_msg;
  2801. if (snapshot) {
  2802. err = nla_put_u32(msg, DEVLINK_ATTR_REGION_SNAPSHOT_ID,
  2803. snapshot->id);
  2804. if (err)
  2805. goto out_cancel_msg;
  2806. } else {
  2807. err = nla_put_u64_64bit(msg, DEVLINK_ATTR_REGION_SIZE,
  2808. region->size, DEVLINK_ATTR_PAD);
  2809. if (err)
  2810. goto out_cancel_msg;
  2811. }
  2812. genlmsg_end(msg, hdr);
  2813. genlmsg_multicast_netns(&devlink_nl_family, devlink_net(devlink),
  2814. msg, 0, DEVLINK_MCGRP_CONFIG, GFP_KERNEL);
  2815. return;
  2816. out_cancel_msg:
  2817. genlmsg_cancel(msg, hdr);
  2818. out_free_msg:
  2819. nlmsg_free(msg);
  2820. }
  2821. static int devlink_nl_cmd_region_get_doit(struct sk_buff *skb,
  2822. struct genl_info *info)
  2823. {
  2824. struct devlink *devlink = info->user_ptr[0];
  2825. struct devlink_region *region;
  2826. const char *region_name;
  2827. struct sk_buff *msg;
  2828. int err;
  2829. if (!info->attrs[DEVLINK_ATTR_REGION_NAME])
  2830. return -EINVAL;
  2831. region_name = nla_data(info->attrs[DEVLINK_ATTR_REGION_NAME]);
  2832. region = devlink_region_get_by_name(devlink, region_name);
  2833. if (!region)
  2834. return -EINVAL;
  2835. msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  2836. if (!msg)
  2837. return -ENOMEM;
  2838. err = devlink_nl_region_fill(msg, devlink, DEVLINK_CMD_REGION_GET,
  2839. info->snd_portid, info->snd_seq, 0,
  2840. region);
  2841. if (err) {
  2842. nlmsg_free(msg);
  2843. return err;
  2844. }
  2845. return genlmsg_reply(msg, info);
  2846. }
  2847. static int devlink_nl_cmd_region_get_dumpit(struct sk_buff *msg,
  2848. struct netlink_callback *cb)
  2849. {
  2850. struct devlink_region *region;
  2851. struct devlink *devlink;
  2852. int start = cb->args[0];
  2853. int idx = 0;
  2854. int err;
  2855. mutex_lock(&devlink_mutex);
  2856. list_for_each_entry(devlink, &devlink_list, list) {
  2857. if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
  2858. continue;
  2859. mutex_lock(&devlink->lock);
  2860. list_for_each_entry(region, &devlink->region_list, list) {
  2861. if (idx < start) {
  2862. idx++;
  2863. continue;
  2864. }
  2865. err = devlink_nl_region_fill(msg, devlink,
  2866. DEVLINK_CMD_REGION_GET,
  2867. NETLINK_CB(cb->skb).portid,
  2868. cb->nlh->nlmsg_seq,
  2869. NLM_F_MULTI, region);
  2870. if (err) {
  2871. mutex_unlock(&devlink->lock);
  2872. goto out;
  2873. }
  2874. idx++;
  2875. }
  2876. mutex_unlock(&devlink->lock);
  2877. }
  2878. out:
  2879. mutex_unlock(&devlink_mutex);
  2880. cb->args[0] = idx;
  2881. return msg->len;
  2882. }
  2883. static int devlink_nl_cmd_region_del(struct sk_buff *skb,
  2884. struct genl_info *info)
  2885. {
  2886. struct devlink *devlink = info->user_ptr[0];
  2887. struct devlink_snapshot *snapshot;
  2888. struct devlink_region *region;
  2889. const char *region_name;
  2890. u32 snapshot_id;
  2891. if (!info->attrs[DEVLINK_ATTR_REGION_NAME] ||
  2892. !info->attrs[DEVLINK_ATTR_REGION_SNAPSHOT_ID])
  2893. return -EINVAL;
  2894. region_name = nla_data(info->attrs[DEVLINK_ATTR_REGION_NAME]);
  2895. snapshot_id = nla_get_u32(info->attrs[DEVLINK_ATTR_REGION_SNAPSHOT_ID]);
  2896. region = devlink_region_get_by_name(devlink, region_name);
  2897. if (!region)
  2898. return -EINVAL;
  2899. snapshot = devlink_region_snapshot_get_by_id(region, snapshot_id);
  2900. if (!snapshot)
  2901. return -EINVAL;
  2902. devlink_nl_region_notify(region, snapshot, DEVLINK_CMD_REGION_DEL);
  2903. devlink_region_snapshot_del(snapshot);
  2904. return 0;
  2905. }
  2906. static int devlink_nl_cmd_region_read_chunk_fill(struct sk_buff *msg,
  2907. struct devlink *devlink,
  2908. u8 *chunk, u32 chunk_size,
  2909. u64 addr)
  2910. {
  2911. struct nlattr *chunk_attr;
  2912. int err;
  2913. chunk_attr = nla_nest_start(msg, DEVLINK_ATTR_REGION_CHUNK);
  2914. if (!chunk_attr)
  2915. return -EINVAL;
  2916. err = nla_put(msg, DEVLINK_ATTR_REGION_CHUNK_DATA, chunk_size, chunk);
  2917. if (err)
  2918. goto nla_put_failure;
  2919. err = nla_put_u64_64bit(msg, DEVLINK_ATTR_REGION_CHUNK_ADDR, addr,
  2920. DEVLINK_ATTR_PAD);
  2921. if (err)
  2922. goto nla_put_failure;
  2923. nla_nest_end(msg, chunk_attr);
  2924. return 0;
  2925. nla_put_failure:
  2926. nla_nest_cancel(msg, chunk_attr);
  2927. return err;
  2928. }
  2929. #define DEVLINK_REGION_READ_CHUNK_SIZE 256
  2930. static int devlink_nl_region_read_snapshot_fill(struct sk_buff *skb,
  2931. struct devlink *devlink,
  2932. struct devlink_region *region,
  2933. struct nlattr **attrs,
  2934. u64 start_offset,
  2935. u64 end_offset,
  2936. bool dump,
  2937. u64 *new_offset)
  2938. {
  2939. struct devlink_snapshot *snapshot;
  2940. u64 curr_offset = start_offset;
  2941. u32 snapshot_id;
  2942. int err = 0;
  2943. *new_offset = start_offset;
  2944. snapshot_id = nla_get_u32(attrs[DEVLINK_ATTR_REGION_SNAPSHOT_ID]);
  2945. snapshot = devlink_region_snapshot_get_by_id(region, snapshot_id);
  2946. if (!snapshot)
  2947. return -EINVAL;
  2948. if (end_offset > snapshot->data_len || dump)
  2949. end_offset = snapshot->data_len;
  2950. while (curr_offset < end_offset) {
  2951. u32 data_size;
  2952. u8 *data;
  2953. if (end_offset - curr_offset < DEVLINK_REGION_READ_CHUNK_SIZE)
  2954. data_size = end_offset - curr_offset;
  2955. else
  2956. data_size = DEVLINK_REGION_READ_CHUNK_SIZE;
  2957. data = &snapshot->data[curr_offset];
  2958. err = devlink_nl_cmd_region_read_chunk_fill(skb, devlink,
  2959. data, data_size,
  2960. curr_offset);
  2961. if (err)
  2962. break;
  2963. curr_offset += data_size;
  2964. }
  2965. *new_offset = curr_offset;
  2966. return err;
  2967. }
  2968. static int devlink_nl_cmd_region_read_dumpit(struct sk_buff *skb,
  2969. struct netlink_callback *cb)
  2970. {
  2971. u64 ret_offset, start_offset, end_offset = 0;
  2972. struct nlattr *attrs[DEVLINK_ATTR_MAX + 1];
  2973. const struct genl_ops *ops = cb->data;
  2974. struct devlink_region *region;
  2975. struct nlattr *chunks_attr;
  2976. const char *region_name;
  2977. struct devlink *devlink;
  2978. bool dump = true;
  2979. void *hdr;
  2980. int err;
  2981. start_offset = *((u64 *)&cb->args[0]);
  2982. err = nlmsg_parse(cb->nlh, GENL_HDRLEN + devlink_nl_family.hdrsize,
  2983. attrs, DEVLINK_ATTR_MAX, ops->policy, NULL);
  2984. if (err)
  2985. goto out;
  2986. devlink = devlink_get_from_attrs(sock_net(cb->skb->sk), attrs);
  2987. if (IS_ERR(devlink))
  2988. goto out;
  2989. mutex_lock(&devlink_mutex);
  2990. mutex_lock(&devlink->lock);
  2991. if (!attrs[DEVLINK_ATTR_REGION_NAME] ||
  2992. !attrs[DEVLINK_ATTR_REGION_SNAPSHOT_ID])
  2993. goto out_unlock;
  2994. region_name = nla_data(attrs[DEVLINK_ATTR_REGION_NAME]);
  2995. region = devlink_region_get_by_name(devlink, region_name);
  2996. if (!region)
  2997. goto out_unlock;
  2998. hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
  2999. &devlink_nl_family, NLM_F_ACK | NLM_F_MULTI,
  3000. DEVLINK_CMD_REGION_READ);
  3001. if (!hdr)
  3002. goto out_unlock;
  3003. err = devlink_nl_put_handle(skb, devlink);
  3004. if (err)
  3005. goto nla_put_failure;
  3006. err = nla_put_string(skb, DEVLINK_ATTR_REGION_NAME, region_name);
  3007. if (err)
  3008. goto nla_put_failure;
  3009. chunks_attr = nla_nest_start(skb, DEVLINK_ATTR_REGION_CHUNKS);
  3010. if (!chunks_attr)
  3011. goto nla_put_failure;
  3012. if (attrs[DEVLINK_ATTR_REGION_CHUNK_ADDR] &&
  3013. attrs[DEVLINK_ATTR_REGION_CHUNK_LEN]) {
  3014. if (!start_offset)
  3015. start_offset =
  3016. nla_get_u64(attrs[DEVLINK_ATTR_REGION_CHUNK_ADDR]);
  3017. end_offset = nla_get_u64(attrs[DEVLINK_ATTR_REGION_CHUNK_ADDR]);
  3018. end_offset += nla_get_u64(attrs[DEVLINK_ATTR_REGION_CHUNK_LEN]);
  3019. dump = false;
  3020. }
  3021. err = devlink_nl_region_read_snapshot_fill(skb, devlink,
  3022. region, attrs,
  3023. start_offset,
  3024. end_offset, dump,
  3025. &ret_offset);
  3026. if (err && err != -EMSGSIZE)
  3027. goto nla_put_failure;
  3028. /* Check if there was any progress done to prevent infinite loop */
  3029. if (ret_offset == start_offset)
  3030. goto nla_put_failure;
  3031. *((u64 *)&cb->args[0]) = ret_offset;
  3032. nla_nest_end(skb, chunks_attr);
  3033. genlmsg_end(skb, hdr);
  3034. mutex_unlock(&devlink->lock);
  3035. mutex_unlock(&devlink_mutex);
  3036. return skb->len;
  3037. nla_put_failure:
  3038. genlmsg_cancel(skb, hdr);
  3039. out_unlock:
  3040. mutex_unlock(&devlink->lock);
  3041. mutex_unlock(&devlink_mutex);
  3042. out:
  3043. return 0;
  3044. }
  3045. static const struct nla_policy devlink_nl_policy[DEVLINK_ATTR_MAX + 1] = {
  3046. [DEVLINK_ATTR_BUS_NAME] = { .type = NLA_NUL_STRING },
  3047. [DEVLINK_ATTR_DEV_NAME] = { .type = NLA_NUL_STRING },
  3048. [DEVLINK_ATTR_PORT_INDEX] = { .type = NLA_U32 },
  3049. [DEVLINK_ATTR_PORT_TYPE] = { .type = NLA_U16 },
  3050. [DEVLINK_ATTR_PORT_SPLIT_COUNT] = { .type = NLA_U32 },
  3051. [DEVLINK_ATTR_SB_INDEX] = { .type = NLA_U32 },
  3052. [DEVLINK_ATTR_SB_POOL_INDEX] = { .type = NLA_U16 },
  3053. [DEVLINK_ATTR_SB_POOL_TYPE] = { .type = NLA_U8 },
  3054. [DEVLINK_ATTR_SB_POOL_SIZE] = { .type = NLA_U32 },
  3055. [DEVLINK_ATTR_SB_POOL_THRESHOLD_TYPE] = { .type = NLA_U8 },
  3056. [DEVLINK_ATTR_SB_THRESHOLD] = { .type = NLA_U32 },
  3057. [DEVLINK_ATTR_SB_TC_INDEX] = { .type = NLA_U16 },
  3058. [DEVLINK_ATTR_ESWITCH_MODE] = { .type = NLA_U16 },
  3059. [DEVLINK_ATTR_ESWITCH_INLINE_MODE] = { .type = NLA_U8 },
  3060. [DEVLINK_ATTR_ESWITCH_ENCAP_MODE] = { .type = NLA_U8 },
  3061. [DEVLINK_ATTR_DPIPE_TABLE_NAME] = { .type = NLA_NUL_STRING },
  3062. [DEVLINK_ATTR_DPIPE_TABLE_COUNTERS_ENABLED] = { .type = NLA_U8 },
  3063. [DEVLINK_ATTR_RESOURCE_ID] = { .type = NLA_U64},
  3064. [DEVLINK_ATTR_RESOURCE_SIZE] = { .type = NLA_U64},
  3065. [DEVLINK_ATTR_PARAM_NAME] = { .type = NLA_NUL_STRING },
  3066. [DEVLINK_ATTR_PARAM_TYPE] = { .type = NLA_U8 },
  3067. [DEVLINK_ATTR_PARAM_VALUE_CMODE] = { .type = NLA_U8 },
  3068. [DEVLINK_ATTR_REGION_NAME] = { .type = NLA_NUL_STRING },
  3069. [DEVLINK_ATTR_REGION_SNAPSHOT_ID] = { .type = NLA_U32 },
  3070. [DEVLINK_ATTR_REGION_CHUNK_ADDR] = { .type = NLA_U64 },
  3071. [DEVLINK_ATTR_REGION_CHUNK_LEN] = { .type = NLA_U64 },
  3072. };
  3073. static const struct genl_ops devlink_nl_ops[] = {
  3074. {
  3075. .cmd = DEVLINK_CMD_GET,
  3076. .doit = devlink_nl_cmd_get_doit,
  3077. .dumpit = devlink_nl_cmd_get_dumpit,
  3078. .policy = devlink_nl_policy,
  3079. .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
  3080. /* can be retrieved by unprivileged users */
  3081. },
  3082. {
  3083. .cmd = DEVLINK_CMD_PORT_GET,
  3084. .doit = devlink_nl_cmd_port_get_doit,
  3085. .dumpit = devlink_nl_cmd_port_get_dumpit,
  3086. .policy = devlink_nl_policy,
  3087. .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
  3088. /* can be retrieved by unprivileged users */
  3089. },
  3090. {
  3091. .cmd = DEVLINK_CMD_PORT_SET,
  3092. .doit = devlink_nl_cmd_port_set_doit,
  3093. .policy = devlink_nl_policy,
  3094. .flags = GENL_ADMIN_PERM,
  3095. .internal_flags = DEVLINK_NL_FLAG_NEED_PORT,
  3096. },
  3097. {
  3098. .cmd = DEVLINK_CMD_PORT_SPLIT,
  3099. .doit = devlink_nl_cmd_port_split_doit,
  3100. .policy = devlink_nl_policy,
  3101. .flags = GENL_ADMIN_PERM,
  3102. .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
  3103. DEVLINK_NL_FLAG_NO_LOCK,
  3104. },
  3105. {
  3106. .cmd = DEVLINK_CMD_PORT_UNSPLIT,
  3107. .doit = devlink_nl_cmd_port_unsplit_doit,
  3108. .policy = devlink_nl_policy,
  3109. .flags = GENL_ADMIN_PERM,
  3110. .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
  3111. DEVLINK_NL_FLAG_NO_LOCK,
  3112. },
  3113. {
  3114. .cmd = DEVLINK_CMD_SB_GET,
  3115. .doit = devlink_nl_cmd_sb_get_doit,
  3116. .dumpit = devlink_nl_cmd_sb_get_dumpit,
  3117. .policy = devlink_nl_policy,
  3118. .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
  3119. DEVLINK_NL_FLAG_NEED_SB,
  3120. /* can be retrieved by unprivileged users */
  3121. },
  3122. {
  3123. .cmd = DEVLINK_CMD_SB_POOL_GET,
  3124. .doit = devlink_nl_cmd_sb_pool_get_doit,
  3125. .dumpit = devlink_nl_cmd_sb_pool_get_dumpit,
  3126. .policy = devlink_nl_policy,
  3127. .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
  3128. DEVLINK_NL_FLAG_NEED_SB,
  3129. /* can be retrieved by unprivileged users */
  3130. },
  3131. {
  3132. .cmd = DEVLINK_CMD_SB_POOL_SET,
  3133. .doit = devlink_nl_cmd_sb_pool_set_doit,
  3134. .policy = devlink_nl_policy,
  3135. .flags = GENL_ADMIN_PERM,
  3136. .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
  3137. DEVLINK_NL_FLAG_NEED_SB,
  3138. },
  3139. {
  3140. .cmd = DEVLINK_CMD_SB_PORT_POOL_GET,
  3141. .doit = devlink_nl_cmd_sb_port_pool_get_doit,
  3142. .dumpit = devlink_nl_cmd_sb_port_pool_get_dumpit,
  3143. .policy = devlink_nl_policy,
  3144. .internal_flags = DEVLINK_NL_FLAG_NEED_PORT |
  3145. DEVLINK_NL_FLAG_NEED_SB,
  3146. /* can be retrieved by unprivileged users */
  3147. },
  3148. {
  3149. .cmd = DEVLINK_CMD_SB_PORT_POOL_SET,
  3150. .doit = devlink_nl_cmd_sb_port_pool_set_doit,
  3151. .policy = devlink_nl_policy,
  3152. .flags = GENL_ADMIN_PERM,
  3153. .internal_flags = DEVLINK_NL_FLAG_NEED_PORT |
  3154. DEVLINK_NL_FLAG_NEED_SB,
  3155. },
  3156. {
  3157. .cmd = DEVLINK_CMD_SB_TC_POOL_BIND_GET,
  3158. .doit = devlink_nl_cmd_sb_tc_pool_bind_get_doit,
  3159. .dumpit = devlink_nl_cmd_sb_tc_pool_bind_get_dumpit,
  3160. .policy = devlink_nl_policy,
  3161. .internal_flags = DEVLINK_NL_FLAG_NEED_PORT |
  3162. DEVLINK_NL_FLAG_NEED_SB,
  3163. /* can be retrieved by unprivileged users */
  3164. },
  3165. {
  3166. .cmd = DEVLINK_CMD_SB_TC_POOL_BIND_SET,
  3167. .doit = devlink_nl_cmd_sb_tc_pool_bind_set_doit,
  3168. .policy = devlink_nl_policy,
  3169. .flags = GENL_ADMIN_PERM,
  3170. .internal_flags = DEVLINK_NL_FLAG_NEED_PORT |
  3171. DEVLINK_NL_FLAG_NEED_SB,
  3172. },
  3173. {
  3174. .cmd = DEVLINK_CMD_SB_OCC_SNAPSHOT,
  3175. .doit = devlink_nl_cmd_sb_occ_snapshot_doit,
  3176. .policy = devlink_nl_policy,
  3177. .flags = GENL_ADMIN_PERM,
  3178. .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
  3179. DEVLINK_NL_FLAG_NEED_SB,
  3180. },
  3181. {
  3182. .cmd = DEVLINK_CMD_SB_OCC_MAX_CLEAR,
  3183. .doit = devlink_nl_cmd_sb_occ_max_clear_doit,
  3184. .policy = devlink_nl_policy,
  3185. .flags = GENL_ADMIN_PERM,
  3186. .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
  3187. DEVLINK_NL_FLAG_NEED_SB,
  3188. },
  3189. {
  3190. .cmd = DEVLINK_CMD_ESWITCH_GET,
  3191. .doit = devlink_nl_cmd_eswitch_get_doit,
  3192. .policy = devlink_nl_policy,
  3193. .flags = GENL_ADMIN_PERM,
  3194. .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
  3195. },
  3196. {
  3197. .cmd = DEVLINK_CMD_ESWITCH_SET,
  3198. .doit = devlink_nl_cmd_eswitch_set_doit,
  3199. .policy = devlink_nl_policy,
  3200. .flags = GENL_ADMIN_PERM,
  3201. .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
  3202. DEVLINK_NL_FLAG_NO_LOCK,
  3203. },
  3204. {
  3205. .cmd = DEVLINK_CMD_DPIPE_TABLE_GET,
  3206. .doit = devlink_nl_cmd_dpipe_table_get,
  3207. .policy = devlink_nl_policy,
  3208. .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
  3209. /* can be retrieved by unprivileged users */
  3210. },
  3211. {
  3212. .cmd = DEVLINK_CMD_DPIPE_ENTRIES_GET,
  3213. .doit = devlink_nl_cmd_dpipe_entries_get,
  3214. .policy = devlink_nl_policy,
  3215. .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
  3216. /* can be retrieved by unprivileged users */
  3217. },
  3218. {
  3219. .cmd = DEVLINK_CMD_DPIPE_HEADERS_GET,
  3220. .doit = devlink_nl_cmd_dpipe_headers_get,
  3221. .policy = devlink_nl_policy,
  3222. .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
  3223. /* can be retrieved by unprivileged users */
  3224. },
  3225. {
  3226. .cmd = DEVLINK_CMD_DPIPE_TABLE_COUNTERS_SET,
  3227. .doit = devlink_nl_cmd_dpipe_table_counters_set,
  3228. .policy = devlink_nl_policy,
  3229. .flags = GENL_ADMIN_PERM,
  3230. .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
  3231. },
  3232. {
  3233. .cmd = DEVLINK_CMD_RESOURCE_SET,
  3234. .doit = devlink_nl_cmd_resource_set,
  3235. .policy = devlink_nl_policy,
  3236. .flags = GENL_ADMIN_PERM,
  3237. .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
  3238. },
  3239. {
  3240. .cmd = DEVLINK_CMD_RESOURCE_DUMP,
  3241. .doit = devlink_nl_cmd_resource_dump,
  3242. .policy = devlink_nl_policy,
  3243. .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
  3244. /* can be retrieved by unprivileged users */
  3245. },
  3246. {
  3247. .cmd = DEVLINK_CMD_RELOAD,
  3248. .doit = devlink_nl_cmd_reload,
  3249. .policy = devlink_nl_policy,
  3250. .flags = GENL_ADMIN_PERM,
  3251. .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK |
  3252. DEVLINK_NL_FLAG_NO_LOCK,
  3253. },
  3254. {
  3255. .cmd = DEVLINK_CMD_PARAM_GET,
  3256. .doit = devlink_nl_cmd_param_get_doit,
  3257. .dumpit = devlink_nl_cmd_param_get_dumpit,
  3258. .policy = devlink_nl_policy,
  3259. .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
  3260. /* can be retrieved by unprivileged users */
  3261. },
  3262. {
  3263. .cmd = DEVLINK_CMD_PARAM_SET,
  3264. .doit = devlink_nl_cmd_param_set_doit,
  3265. .policy = devlink_nl_policy,
  3266. .flags = GENL_ADMIN_PERM,
  3267. .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
  3268. },
  3269. {
  3270. .cmd = DEVLINK_CMD_REGION_GET,
  3271. .doit = devlink_nl_cmd_region_get_doit,
  3272. .dumpit = devlink_nl_cmd_region_get_dumpit,
  3273. .policy = devlink_nl_policy,
  3274. .flags = GENL_ADMIN_PERM,
  3275. .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
  3276. },
  3277. {
  3278. .cmd = DEVLINK_CMD_REGION_DEL,
  3279. .doit = devlink_nl_cmd_region_del,
  3280. .policy = devlink_nl_policy,
  3281. .flags = GENL_ADMIN_PERM,
  3282. .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
  3283. },
  3284. {
  3285. .cmd = DEVLINK_CMD_REGION_READ,
  3286. .dumpit = devlink_nl_cmd_region_read_dumpit,
  3287. .policy = devlink_nl_policy,
  3288. .flags = GENL_ADMIN_PERM,
  3289. .internal_flags = DEVLINK_NL_FLAG_NEED_DEVLINK,
  3290. },
  3291. };
  3292. static struct genl_family devlink_nl_family __ro_after_init = {
  3293. .name = DEVLINK_GENL_NAME,
  3294. .version = DEVLINK_GENL_VERSION,
  3295. .maxattr = DEVLINK_ATTR_MAX,
  3296. .netnsok = true,
  3297. .pre_doit = devlink_nl_pre_doit,
  3298. .post_doit = devlink_nl_post_doit,
  3299. .module = THIS_MODULE,
  3300. .ops = devlink_nl_ops,
  3301. .n_ops = ARRAY_SIZE(devlink_nl_ops),
  3302. .mcgrps = devlink_nl_mcgrps,
  3303. .n_mcgrps = ARRAY_SIZE(devlink_nl_mcgrps),
  3304. };
  3305. /**
  3306. * devlink_alloc - Allocate new devlink instance resources
  3307. *
  3308. * @ops: ops
  3309. * @priv_size: size of user private data
  3310. *
  3311. * Allocate new devlink instance resources, including devlink index
  3312. * and name.
  3313. */
  3314. struct devlink *devlink_alloc(const struct devlink_ops *ops, size_t priv_size)
  3315. {
  3316. struct devlink *devlink;
  3317. devlink = kzalloc(sizeof(*devlink) + priv_size, GFP_KERNEL);
  3318. if (!devlink)
  3319. return NULL;
  3320. devlink->ops = ops;
  3321. devlink_net_set(devlink, &init_net);
  3322. INIT_LIST_HEAD(&devlink->port_list);
  3323. INIT_LIST_HEAD(&devlink->sb_list);
  3324. INIT_LIST_HEAD_RCU(&devlink->dpipe_table_list);
  3325. INIT_LIST_HEAD(&devlink->resource_list);
  3326. INIT_LIST_HEAD(&devlink->param_list);
  3327. INIT_LIST_HEAD(&devlink->region_list);
  3328. mutex_init(&devlink->lock);
  3329. return devlink;
  3330. }
  3331. EXPORT_SYMBOL_GPL(devlink_alloc);
  3332. /**
  3333. * devlink_register - Register devlink instance
  3334. *
  3335. * @devlink: devlink
  3336. */
  3337. int devlink_register(struct devlink *devlink, struct device *dev)
  3338. {
  3339. mutex_lock(&devlink_mutex);
  3340. devlink->dev = dev;
  3341. list_add_tail(&devlink->list, &devlink_list);
  3342. devlink_notify(devlink, DEVLINK_CMD_NEW);
  3343. mutex_unlock(&devlink_mutex);
  3344. return 0;
  3345. }
  3346. EXPORT_SYMBOL_GPL(devlink_register);
  3347. /**
  3348. * devlink_unregister - Unregister devlink instance
  3349. *
  3350. * @devlink: devlink
  3351. */
  3352. void devlink_unregister(struct devlink *devlink)
  3353. {
  3354. mutex_lock(&devlink_mutex);
  3355. devlink_notify(devlink, DEVLINK_CMD_DEL);
  3356. list_del(&devlink->list);
  3357. mutex_unlock(&devlink_mutex);
  3358. }
  3359. EXPORT_SYMBOL_GPL(devlink_unregister);
  3360. /**
  3361. * devlink_free - Free devlink instance resources
  3362. *
  3363. * @devlink: devlink
  3364. */
  3365. void devlink_free(struct devlink *devlink)
  3366. {
  3367. kfree(devlink);
  3368. }
  3369. EXPORT_SYMBOL_GPL(devlink_free);
  3370. /**
  3371. * devlink_port_register - Register devlink port
  3372. *
  3373. * @devlink: devlink
  3374. * @devlink_port: devlink port
  3375. * @port_index
  3376. *
  3377. * Register devlink port with provided port index. User can use
  3378. * any indexing, even hw-related one. devlink_port structure
  3379. * is convenient to be embedded inside user driver private structure.
  3380. * Note that the caller should take care of zeroing the devlink_port
  3381. * structure.
  3382. */
  3383. int devlink_port_register(struct devlink *devlink,
  3384. struct devlink_port *devlink_port,
  3385. unsigned int port_index)
  3386. {
  3387. mutex_lock(&devlink->lock);
  3388. if (devlink_port_index_exists(devlink, port_index)) {
  3389. mutex_unlock(&devlink->lock);
  3390. return -EEXIST;
  3391. }
  3392. devlink_port->devlink = devlink;
  3393. devlink_port->index = port_index;
  3394. devlink_port->registered = true;
  3395. list_add_tail(&devlink_port->list, &devlink->port_list);
  3396. mutex_unlock(&devlink->lock);
  3397. devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW);
  3398. return 0;
  3399. }
  3400. EXPORT_SYMBOL_GPL(devlink_port_register);
  3401. /**
  3402. * devlink_port_unregister - Unregister devlink port
  3403. *
  3404. * @devlink_port: devlink port
  3405. */
  3406. void devlink_port_unregister(struct devlink_port *devlink_port)
  3407. {
  3408. struct devlink *devlink = devlink_port->devlink;
  3409. devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_DEL);
  3410. mutex_lock(&devlink->lock);
  3411. list_del(&devlink_port->list);
  3412. mutex_unlock(&devlink->lock);
  3413. }
  3414. EXPORT_SYMBOL_GPL(devlink_port_unregister);
  3415. static void __devlink_port_type_set(struct devlink_port *devlink_port,
  3416. enum devlink_port_type type,
  3417. void *type_dev)
  3418. {
  3419. devlink_port->type = type;
  3420. devlink_port->type_dev = type_dev;
  3421. devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW);
  3422. }
  3423. /**
  3424. * devlink_port_type_eth_set - Set port type to Ethernet
  3425. *
  3426. * @devlink_port: devlink port
  3427. * @netdev: related netdevice
  3428. */
  3429. void devlink_port_type_eth_set(struct devlink_port *devlink_port,
  3430. struct net_device *netdev)
  3431. {
  3432. return __devlink_port_type_set(devlink_port,
  3433. DEVLINK_PORT_TYPE_ETH, netdev);
  3434. }
  3435. EXPORT_SYMBOL_GPL(devlink_port_type_eth_set);
  3436. /**
  3437. * devlink_port_type_ib_set - Set port type to InfiniBand
  3438. *
  3439. * @devlink_port: devlink port
  3440. * @ibdev: related IB device
  3441. */
  3442. void devlink_port_type_ib_set(struct devlink_port *devlink_port,
  3443. struct ib_device *ibdev)
  3444. {
  3445. return __devlink_port_type_set(devlink_port,
  3446. DEVLINK_PORT_TYPE_IB, ibdev);
  3447. }
  3448. EXPORT_SYMBOL_GPL(devlink_port_type_ib_set);
  3449. /**
  3450. * devlink_port_type_clear - Clear port type
  3451. *
  3452. * @devlink_port: devlink port
  3453. */
  3454. void devlink_port_type_clear(struct devlink_port *devlink_port)
  3455. {
  3456. return __devlink_port_type_set(devlink_port,
  3457. DEVLINK_PORT_TYPE_NOTSET, NULL);
  3458. }
  3459. EXPORT_SYMBOL_GPL(devlink_port_type_clear);
  3460. /**
  3461. * devlink_port_attrs_set - Set port attributes
  3462. *
  3463. * @devlink_port: devlink port
  3464. * @flavour: flavour of the port
  3465. * @port_number: number of the port that is facing user, for example
  3466. * the front panel port number
  3467. * @split: indicates if this is split port
  3468. * @split_subport_number: if the port is split, this is the number
  3469. * of subport.
  3470. */
  3471. void devlink_port_attrs_set(struct devlink_port *devlink_port,
  3472. enum devlink_port_flavour flavour,
  3473. u32 port_number, bool split,
  3474. u32 split_subport_number)
  3475. {
  3476. struct devlink_port_attrs *attrs = &devlink_port->attrs;
  3477. attrs->set = true;
  3478. attrs->flavour = flavour;
  3479. attrs->port_number = port_number;
  3480. attrs->split = split;
  3481. attrs->split_subport_number = split_subport_number;
  3482. devlink_port_notify(devlink_port, DEVLINK_CMD_PORT_NEW);
  3483. }
  3484. EXPORT_SYMBOL_GPL(devlink_port_attrs_set);
  3485. int devlink_port_get_phys_port_name(struct devlink_port *devlink_port,
  3486. char *name, size_t len)
  3487. {
  3488. struct devlink_port_attrs *attrs = &devlink_port->attrs;
  3489. int n = 0;
  3490. if (!attrs->set)
  3491. return -EOPNOTSUPP;
  3492. switch (attrs->flavour) {
  3493. case DEVLINK_PORT_FLAVOUR_PHYSICAL:
  3494. if (!attrs->split)
  3495. n = snprintf(name, len, "p%u", attrs->port_number);
  3496. else
  3497. n = snprintf(name, len, "p%us%u", attrs->port_number,
  3498. attrs->split_subport_number);
  3499. break;
  3500. case DEVLINK_PORT_FLAVOUR_CPU:
  3501. case DEVLINK_PORT_FLAVOUR_DSA:
  3502. /* As CPU and DSA ports do not have a netdevice associated
  3503. * case should not ever happen.
  3504. */
  3505. WARN_ON(1);
  3506. return -EINVAL;
  3507. }
  3508. if (n >= len)
  3509. return -EINVAL;
  3510. return 0;
  3511. }
  3512. EXPORT_SYMBOL_GPL(devlink_port_get_phys_port_name);
  3513. int devlink_sb_register(struct devlink *devlink, unsigned int sb_index,
  3514. u32 size, u16 ingress_pools_count,
  3515. u16 egress_pools_count, u16 ingress_tc_count,
  3516. u16 egress_tc_count)
  3517. {
  3518. struct devlink_sb *devlink_sb;
  3519. int err = 0;
  3520. mutex_lock(&devlink->lock);
  3521. if (devlink_sb_index_exists(devlink, sb_index)) {
  3522. err = -EEXIST;
  3523. goto unlock;
  3524. }
  3525. devlink_sb = kzalloc(sizeof(*devlink_sb), GFP_KERNEL);
  3526. if (!devlink_sb) {
  3527. err = -ENOMEM;
  3528. goto unlock;
  3529. }
  3530. devlink_sb->index = sb_index;
  3531. devlink_sb->size = size;
  3532. devlink_sb->ingress_pools_count = ingress_pools_count;
  3533. devlink_sb->egress_pools_count = egress_pools_count;
  3534. devlink_sb->ingress_tc_count = ingress_tc_count;
  3535. devlink_sb->egress_tc_count = egress_tc_count;
  3536. list_add_tail(&devlink_sb->list, &devlink->sb_list);
  3537. unlock:
  3538. mutex_unlock(&devlink->lock);
  3539. return err;
  3540. }
  3541. EXPORT_SYMBOL_GPL(devlink_sb_register);
  3542. void devlink_sb_unregister(struct devlink *devlink, unsigned int sb_index)
  3543. {
  3544. struct devlink_sb *devlink_sb;
  3545. mutex_lock(&devlink->lock);
  3546. devlink_sb = devlink_sb_get_by_index(devlink, sb_index);
  3547. WARN_ON(!devlink_sb);
  3548. list_del(&devlink_sb->list);
  3549. mutex_unlock(&devlink->lock);
  3550. kfree(devlink_sb);
  3551. }
  3552. EXPORT_SYMBOL_GPL(devlink_sb_unregister);
  3553. /**
  3554. * devlink_dpipe_headers_register - register dpipe headers
  3555. *
  3556. * @devlink: devlink
  3557. * @dpipe_headers: dpipe header array
  3558. *
  3559. * Register the headers supported by hardware.
  3560. */
  3561. int devlink_dpipe_headers_register(struct devlink *devlink,
  3562. struct devlink_dpipe_headers *dpipe_headers)
  3563. {
  3564. mutex_lock(&devlink->lock);
  3565. devlink->dpipe_headers = dpipe_headers;
  3566. mutex_unlock(&devlink->lock);
  3567. return 0;
  3568. }
  3569. EXPORT_SYMBOL_GPL(devlink_dpipe_headers_register);
  3570. /**
  3571. * devlink_dpipe_headers_unregister - unregister dpipe headers
  3572. *
  3573. * @devlink: devlink
  3574. *
  3575. * Unregister the headers supported by hardware.
  3576. */
  3577. void devlink_dpipe_headers_unregister(struct devlink *devlink)
  3578. {
  3579. mutex_lock(&devlink->lock);
  3580. devlink->dpipe_headers = NULL;
  3581. mutex_unlock(&devlink->lock);
  3582. }
  3583. EXPORT_SYMBOL_GPL(devlink_dpipe_headers_unregister);
  3584. /**
  3585. * devlink_dpipe_table_counter_enabled - check if counter allocation
  3586. * required
  3587. * @devlink: devlink
  3588. * @table_name: tables name
  3589. *
  3590. * Used by driver to check if counter allocation is required.
  3591. * After counter allocation is turned on the table entries
  3592. * are updated to include counter statistics.
  3593. *
  3594. * After that point on the driver must respect the counter
  3595. * state so that each entry added to the table is added
  3596. * with a counter.
  3597. */
  3598. bool devlink_dpipe_table_counter_enabled(struct devlink *devlink,
  3599. const char *table_name)
  3600. {
  3601. struct devlink_dpipe_table *table;
  3602. bool enabled;
  3603. rcu_read_lock();
  3604. table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
  3605. table_name);
  3606. enabled = false;
  3607. if (table)
  3608. enabled = table->counters_enabled;
  3609. rcu_read_unlock();
  3610. return enabled;
  3611. }
  3612. EXPORT_SYMBOL_GPL(devlink_dpipe_table_counter_enabled);
  3613. /**
  3614. * devlink_dpipe_table_register - register dpipe table
  3615. *
  3616. * @devlink: devlink
  3617. * @table_name: table name
  3618. * @table_ops: table ops
  3619. * @priv: priv
  3620. * @counter_control_extern: external control for counters
  3621. */
  3622. int devlink_dpipe_table_register(struct devlink *devlink,
  3623. const char *table_name,
  3624. struct devlink_dpipe_table_ops *table_ops,
  3625. void *priv, bool counter_control_extern)
  3626. {
  3627. struct devlink_dpipe_table *table;
  3628. if (devlink_dpipe_table_find(&devlink->dpipe_table_list, table_name))
  3629. return -EEXIST;
  3630. if (WARN_ON(!table_ops->size_get))
  3631. return -EINVAL;
  3632. table = kzalloc(sizeof(*table), GFP_KERNEL);
  3633. if (!table)
  3634. return -ENOMEM;
  3635. table->name = table_name;
  3636. table->table_ops = table_ops;
  3637. table->priv = priv;
  3638. table->counter_control_extern = counter_control_extern;
  3639. mutex_lock(&devlink->lock);
  3640. list_add_tail_rcu(&table->list, &devlink->dpipe_table_list);
  3641. mutex_unlock(&devlink->lock);
  3642. return 0;
  3643. }
  3644. EXPORT_SYMBOL_GPL(devlink_dpipe_table_register);
  3645. /**
  3646. * devlink_dpipe_table_unregister - unregister dpipe table
  3647. *
  3648. * @devlink: devlink
  3649. * @table_name: table name
  3650. */
  3651. void devlink_dpipe_table_unregister(struct devlink *devlink,
  3652. const char *table_name)
  3653. {
  3654. struct devlink_dpipe_table *table;
  3655. mutex_lock(&devlink->lock);
  3656. table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
  3657. table_name);
  3658. if (!table)
  3659. goto unlock;
  3660. list_del_rcu(&table->list);
  3661. mutex_unlock(&devlink->lock);
  3662. kfree_rcu(table, rcu);
  3663. return;
  3664. unlock:
  3665. mutex_unlock(&devlink->lock);
  3666. }
  3667. EXPORT_SYMBOL_GPL(devlink_dpipe_table_unregister);
  3668. /**
  3669. * devlink_resource_register - devlink resource register
  3670. *
  3671. * @devlink: devlink
  3672. * @resource_name: resource's name
  3673. * @top_hierarchy: top hierarchy
  3674. * @reload_required: reload is required for new configuration to
  3675. * apply
  3676. * @resource_size: resource's size
  3677. * @resource_id: resource's id
  3678. * @parent_reosurce_id: resource's parent id
  3679. * @size params: size parameters
  3680. */
  3681. int devlink_resource_register(struct devlink *devlink,
  3682. const char *resource_name,
  3683. u64 resource_size,
  3684. u64 resource_id,
  3685. u64 parent_resource_id,
  3686. const struct devlink_resource_size_params *size_params)
  3687. {
  3688. struct devlink_resource *resource;
  3689. struct list_head *resource_list;
  3690. bool top_hierarchy;
  3691. int err = 0;
  3692. top_hierarchy = parent_resource_id == DEVLINK_RESOURCE_ID_PARENT_TOP;
  3693. mutex_lock(&devlink->lock);
  3694. resource = devlink_resource_find(devlink, NULL, resource_id);
  3695. if (resource) {
  3696. err = -EINVAL;
  3697. goto out;
  3698. }
  3699. resource = kzalloc(sizeof(*resource), GFP_KERNEL);
  3700. if (!resource) {
  3701. err = -ENOMEM;
  3702. goto out;
  3703. }
  3704. if (top_hierarchy) {
  3705. resource_list = &devlink->resource_list;
  3706. } else {
  3707. struct devlink_resource *parent_resource;
  3708. parent_resource = devlink_resource_find(devlink, NULL,
  3709. parent_resource_id);
  3710. if (parent_resource) {
  3711. resource_list = &parent_resource->resource_list;
  3712. resource->parent = parent_resource;
  3713. } else {
  3714. kfree(resource);
  3715. err = -EINVAL;
  3716. goto out;
  3717. }
  3718. }
  3719. resource->name = resource_name;
  3720. resource->size = resource_size;
  3721. resource->size_new = resource_size;
  3722. resource->id = resource_id;
  3723. resource->size_valid = true;
  3724. memcpy(&resource->size_params, size_params,
  3725. sizeof(resource->size_params));
  3726. INIT_LIST_HEAD(&resource->resource_list);
  3727. list_add_tail(&resource->list, resource_list);
  3728. out:
  3729. mutex_unlock(&devlink->lock);
  3730. return err;
  3731. }
  3732. EXPORT_SYMBOL_GPL(devlink_resource_register);
  3733. /**
  3734. * devlink_resources_unregister - free all resources
  3735. *
  3736. * @devlink: devlink
  3737. * @resource: resource
  3738. */
  3739. void devlink_resources_unregister(struct devlink *devlink,
  3740. struct devlink_resource *resource)
  3741. {
  3742. struct devlink_resource *tmp, *child_resource;
  3743. struct list_head *resource_list;
  3744. if (resource)
  3745. resource_list = &resource->resource_list;
  3746. else
  3747. resource_list = &devlink->resource_list;
  3748. if (!resource)
  3749. mutex_lock(&devlink->lock);
  3750. list_for_each_entry_safe(child_resource, tmp, resource_list, list) {
  3751. devlink_resources_unregister(devlink, child_resource);
  3752. list_del(&child_resource->list);
  3753. kfree(child_resource);
  3754. }
  3755. if (!resource)
  3756. mutex_unlock(&devlink->lock);
  3757. }
  3758. EXPORT_SYMBOL_GPL(devlink_resources_unregister);
  3759. /**
  3760. * devlink_resource_size_get - get and update size
  3761. *
  3762. * @devlink: devlink
  3763. * @resource_id: the requested resource id
  3764. * @p_resource_size: ptr to update
  3765. */
  3766. int devlink_resource_size_get(struct devlink *devlink,
  3767. u64 resource_id,
  3768. u64 *p_resource_size)
  3769. {
  3770. struct devlink_resource *resource;
  3771. int err = 0;
  3772. mutex_lock(&devlink->lock);
  3773. resource = devlink_resource_find(devlink, NULL, resource_id);
  3774. if (!resource) {
  3775. err = -EINVAL;
  3776. goto out;
  3777. }
  3778. *p_resource_size = resource->size_new;
  3779. resource->size = resource->size_new;
  3780. out:
  3781. mutex_unlock(&devlink->lock);
  3782. return err;
  3783. }
  3784. EXPORT_SYMBOL_GPL(devlink_resource_size_get);
  3785. /**
  3786. * devlink_dpipe_table_resource_set - set the resource id
  3787. *
  3788. * @devlink: devlink
  3789. * @table_name: table name
  3790. * @resource_id: resource id
  3791. * @resource_units: number of resource's units consumed per table's entry
  3792. */
  3793. int devlink_dpipe_table_resource_set(struct devlink *devlink,
  3794. const char *table_name, u64 resource_id,
  3795. u64 resource_units)
  3796. {
  3797. struct devlink_dpipe_table *table;
  3798. int err = 0;
  3799. mutex_lock(&devlink->lock);
  3800. table = devlink_dpipe_table_find(&devlink->dpipe_table_list,
  3801. table_name);
  3802. if (!table) {
  3803. err = -EINVAL;
  3804. goto out;
  3805. }
  3806. table->resource_id = resource_id;
  3807. table->resource_units = resource_units;
  3808. table->resource_valid = true;
  3809. out:
  3810. mutex_unlock(&devlink->lock);
  3811. return err;
  3812. }
  3813. EXPORT_SYMBOL_GPL(devlink_dpipe_table_resource_set);
  3814. /**
  3815. * devlink_resource_occ_get_register - register occupancy getter
  3816. *
  3817. * @devlink: devlink
  3818. * @resource_id: resource id
  3819. * @occ_get: occupancy getter callback
  3820. * @occ_get_priv: occupancy getter callback priv
  3821. */
  3822. void devlink_resource_occ_get_register(struct devlink *devlink,
  3823. u64 resource_id,
  3824. devlink_resource_occ_get_t *occ_get,
  3825. void *occ_get_priv)
  3826. {
  3827. struct devlink_resource *resource;
  3828. mutex_lock(&devlink->lock);
  3829. resource = devlink_resource_find(devlink, NULL, resource_id);
  3830. if (WARN_ON(!resource))
  3831. goto out;
  3832. WARN_ON(resource->occ_get);
  3833. resource->occ_get = occ_get;
  3834. resource->occ_get_priv = occ_get_priv;
  3835. out:
  3836. mutex_unlock(&devlink->lock);
  3837. }
  3838. EXPORT_SYMBOL_GPL(devlink_resource_occ_get_register);
  3839. /**
  3840. * devlink_resource_occ_get_unregister - unregister occupancy getter
  3841. *
  3842. * @devlink: devlink
  3843. * @resource_id: resource id
  3844. */
  3845. void devlink_resource_occ_get_unregister(struct devlink *devlink,
  3846. u64 resource_id)
  3847. {
  3848. struct devlink_resource *resource;
  3849. mutex_lock(&devlink->lock);
  3850. resource = devlink_resource_find(devlink, NULL, resource_id);
  3851. if (WARN_ON(!resource))
  3852. goto out;
  3853. WARN_ON(!resource->occ_get);
  3854. resource->occ_get = NULL;
  3855. resource->occ_get_priv = NULL;
  3856. out:
  3857. mutex_unlock(&devlink->lock);
  3858. }
  3859. EXPORT_SYMBOL_GPL(devlink_resource_occ_get_unregister);
  3860. /**
  3861. * devlink_params_register - register configuration parameters
  3862. *
  3863. * @devlink: devlink
  3864. * @params: configuration parameters array
  3865. * @params_count: number of parameters provided
  3866. *
  3867. * Register the configuration parameters supported by the driver.
  3868. */
  3869. int devlink_params_register(struct devlink *devlink,
  3870. const struct devlink_param *params,
  3871. size_t params_count)
  3872. {
  3873. const struct devlink_param *param = params;
  3874. int i;
  3875. int err;
  3876. mutex_lock(&devlink->lock);
  3877. for (i = 0; i < params_count; i++, param++) {
  3878. if (!param || !param->name || !param->supported_cmodes) {
  3879. err = -EINVAL;
  3880. goto rollback;
  3881. }
  3882. if (param->generic) {
  3883. err = devlink_param_generic_verify(param);
  3884. if (err)
  3885. goto rollback;
  3886. } else {
  3887. err = devlink_param_driver_verify(param);
  3888. if (err)
  3889. goto rollback;
  3890. }
  3891. err = devlink_param_register_one(devlink, param);
  3892. if (err)
  3893. goto rollback;
  3894. }
  3895. mutex_unlock(&devlink->lock);
  3896. return 0;
  3897. rollback:
  3898. if (!i)
  3899. goto unlock;
  3900. for (param--; i > 0; i--, param--)
  3901. devlink_param_unregister_one(devlink, param);
  3902. unlock:
  3903. mutex_unlock(&devlink->lock);
  3904. return err;
  3905. }
  3906. EXPORT_SYMBOL_GPL(devlink_params_register);
  3907. /**
  3908. * devlink_params_unregister - unregister configuration parameters
  3909. * @devlink: devlink
  3910. * @params: configuration parameters to unregister
  3911. * @params_count: number of parameters provided
  3912. */
  3913. void devlink_params_unregister(struct devlink *devlink,
  3914. const struct devlink_param *params,
  3915. size_t params_count)
  3916. {
  3917. const struct devlink_param *param = params;
  3918. int i;
  3919. mutex_lock(&devlink->lock);
  3920. for (i = 0; i < params_count; i++, param++)
  3921. devlink_param_unregister_one(devlink, param);
  3922. mutex_unlock(&devlink->lock);
  3923. }
  3924. EXPORT_SYMBOL_GPL(devlink_params_unregister);
  3925. /**
  3926. * devlink_param_driverinit_value_get - get configuration parameter
  3927. * value for driver initializing
  3928. *
  3929. * @devlink: devlink
  3930. * @param_id: parameter ID
  3931. * @init_val: value of parameter in driverinit configuration mode
  3932. *
  3933. * This function should be used by the driver to get driverinit
  3934. * configuration for initialization after reload command.
  3935. */
  3936. int devlink_param_driverinit_value_get(struct devlink *devlink, u32 param_id,
  3937. union devlink_param_value *init_val)
  3938. {
  3939. struct devlink_param_item *param_item;
  3940. if (!devlink->ops || !devlink->ops->reload)
  3941. return -EOPNOTSUPP;
  3942. param_item = devlink_param_find_by_id(&devlink->param_list, param_id);
  3943. if (!param_item)
  3944. return -EINVAL;
  3945. if (!param_item->driverinit_value_valid ||
  3946. !devlink_param_cmode_is_supported(param_item->param,
  3947. DEVLINK_PARAM_CMODE_DRIVERINIT))
  3948. return -EOPNOTSUPP;
  3949. if (param_item->param->type == DEVLINK_PARAM_TYPE_STRING)
  3950. strcpy(init_val->vstr, param_item->driverinit_value.vstr);
  3951. else
  3952. *init_val = param_item->driverinit_value;
  3953. return 0;
  3954. }
  3955. EXPORT_SYMBOL_GPL(devlink_param_driverinit_value_get);
  3956. /**
  3957. * devlink_param_driverinit_value_set - set value of configuration
  3958. * parameter for driverinit
  3959. * configuration mode
  3960. *
  3961. * @devlink: devlink
  3962. * @param_id: parameter ID
  3963. * @init_val: value of parameter to set for driverinit configuration mode
  3964. *
  3965. * This function should be used by the driver to set driverinit
  3966. * configuration mode default value.
  3967. */
  3968. int devlink_param_driverinit_value_set(struct devlink *devlink, u32 param_id,
  3969. union devlink_param_value init_val)
  3970. {
  3971. struct devlink_param_item *param_item;
  3972. param_item = devlink_param_find_by_id(&devlink->param_list, param_id);
  3973. if (!param_item)
  3974. return -EINVAL;
  3975. if (!devlink_param_cmode_is_supported(param_item->param,
  3976. DEVLINK_PARAM_CMODE_DRIVERINIT))
  3977. return -EOPNOTSUPP;
  3978. if (param_item->param->type == DEVLINK_PARAM_TYPE_STRING)
  3979. strcpy(param_item->driverinit_value.vstr, init_val.vstr);
  3980. else
  3981. param_item->driverinit_value = init_val;
  3982. param_item->driverinit_value_valid = true;
  3983. devlink_param_notify(devlink, param_item, DEVLINK_CMD_PARAM_NEW);
  3984. return 0;
  3985. }
  3986. EXPORT_SYMBOL_GPL(devlink_param_driverinit_value_set);
  3987. /**
  3988. * devlink_param_value_changed - notify devlink on a parameter's value
  3989. * change. Should be called by the driver
  3990. * right after the change.
  3991. *
  3992. * @devlink: devlink
  3993. * @param_id: parameter ID
  3994. *
  3995. * This function should be used by the driver to notify devlink on value
  3996. * change, excluding driverinit configuration mode.
  3997. * For driverinit configuration mode driver should use the function
  3998. * devlink_param_driverinit_value_set() instead.
  3999. */
  4000. void devlink_param_value_changed(struct devlink *devlink, u32 param_id)
  4001. {
  4002. struct devlink_param_item *param_item;
  4003. param_item = devlink_param_find_by_id(&devlink->param_list, param_id);
  4004. WARN_ON(!param_item);
  4005. devlink_param_notify(devlink, param_item, DEVLINK_CMD_PARAM_NEW);
  4006. }
  4007. EXPORT_SYMBOL_GPL(devlink_param_value_changed);
  4008. /**
  4009. * devlink_param_value_str_fill - Safely fill-up the string preventing
  4010. * from overflow of the preallocated buffer
  4011. *
  4012. * @dst_val: destination devlink_param_value
  4013. * @src: source buffer
  4014. */
  4015. void devlink_param_value_str_fill(union devlink_param_value *dst_val,
  4016. const char *src)
  4017. {
  4018. size_t len;
  4019. len = strlcpy(dst_val->vstr, src, __DEVLINK_PARAM_MAX_STRING_VALUE);
  4020. WARN_ON(len >= __DEVLINK_PARAM_MAX_STRING_VALUE);
  4021. }
  4022. EXPORT_SYMBOL_GPL(devlink_param_value_str_fill);
  4023. /**
  4024. * devlink_region_create - create a new address region
  4025. *
  4026. * @devlink: devlink
  4027. * @region_name: region name
  4028. * @region_max_snapshots: Maximum supported number of snapshots for region
  4029. * @region_size: size of region
  4030. */
  4031. struct devlink_region *devlink_region_create(struct devlink *devlink,
  4032. const char *region_name,
  4033. u32 region_max_snapshots,
  4034. u64 region_size)
  4035. {
  4036. struct devlink_region *region;
  4037. int err = 0;
  4038. mutex_lock(&devlink->lock);
  4039. if (devlink_region_get_by_name(devlink, region_name)) {
  4040. err = -EEXIST;
  4041. goto unlock;
  4042. }
  4043. region = kzalloc(sizeof(*region), GFP_KERNEL);
  4044. if (!region) {
  4045. err = -ENOMEM;
  4046. goto unlock;
  4047. }
  4048. region->devlink = devlink;
  4049. region->max_snapshots = region_max_snapshots;
  4050. region->name = region_name;
  4051. region->size = region_size;
  4052. INIT_LIST_HEAD(&region->snapshot_list);
  4053. list_add_tail(&region->list, &devlink->region_list);
  4054. devlink_nl_region_notify(region, NULL, DEVLINK_CMD_REGION_NEW);
  4055. mutex_unlock(&devlink->lock);
  4056. return region;
  4057. unlock:
  4058. mutex_unlock(&devlink->lock);
  4059. return ERR_PTR(err);
  4060. }
  4061. EXPORT_SYMBOL_GPL(devlink_region_create);
  4062. /**
  4063. * devlink_region_destroy - destroy address region
  4064. *
  4065. * @region: devlink region to destroy
  4066. */
  4067. void devlink_region_destroy(struct devlink_region *region)
  4068. {
  4069. struct devlink *devlink = region->devlink;
  4070. struct devlink_snapshot *snapshot, *ts;
  4071. mutex_lock(&devlink->lock);
  4072. /* Free all snapshots of region */
  4073. list_for_each_entry_safe(snapshot, ts, &region->snapshot_list, list)
  4074. devlink_region_snapshot_del(snapshot);
  4075. list_del(&region->list);
  4076. devlink_nl_region_notify(region, NULL, DEVLINK_CMD_REGION_DEL);
  4077. mutex_unlock(&devlink->lock);
  4078. kfree(region);
  4079. }
  4080. EXPORT_SYMBOL_GPL(devlink_region_destroy);
  4081. /**
  4082. * devlink_region_shapshot_id_get - get snapshot ID
  4083. *
  4084. * This callback should be called when adding a new snapshot,
  4085. * Driver should use the same id for multiple snapshots taken
  4086. * on multiple regions at the same time/by the same trigger.
  4087. *
  4088. * @devlink: devlink
  4089. */
  4090. u32 devlink_region_shapshot_id_get(struct devlink *devlink)
  4091. {
  4092. u32 id;
  4093. mutex_lock(&devlink->lock);
  4094. id = ++devlink->snapshot_id;
  4095. mutex_unlock(&devlink->lock);
  4096. return id;
  4097. }
  4098. EXPORT_SYMBOL_GPL(devlink_region_shapshot_id_get);
  4099. /**
  4100. * devlink_region_snapshot_create - create a new snapshot
  4101. * This will add a new snapshot of a region. The snapshot
  4102. * will be stored on the region struct and can be accessed
  4103. * from devlink. This is useful for future analyses of snapshots.
  4104. * Multiple snapshots can be created on a region.
  4105. * The @snapshot_id should be obtained using the getter function.
  4106. *
  4107. * @devlink_region: devlink region of the snapshot
  4108. * @data_len: size of snapshot data
  4109. * @data: snapshot data
  4110. * @snapshot_id: snapshot id to be created
  4111. * @data_destructor: pointer to destructor function to free data
  4112. */
  4113. int devlink_region_snapshot_create(struct devlink_region *region, u64 data_len,
  4114. u8 *data, u32 snapshot_id,
  4115. devlink_snapshot_data_dest_t *data_destructor)
  4116. {
  4117. struct devlink *devlink = region->devlink;
  4118. struct devlink_snapshot *snapshot;
  4119. int err;
  4120. mutex_lock(&devlink->lock);
  4121. /* check if region can hold one more snapshot */
  4122. if (region->cur_snapshots == region->max_snapshots) {
  4123. err = -ENOMEM;
  4124. goto unlock;
  4125. }
  4126. if (devlink_region_snapshot_get_by_id(region, snapshot_id)) {
  4127. err = -EEXIST;
  4128. goto unlock;
  4129. }
  4130. snapshot = kzalloc(sizeof(*snapshot), GFP_KERNEL);
  4131. if (!snapshot) {
  4132. err = -ENOMEM;
  4133. goto unlock;
  4134. }
  4135. snapshot->id = snapshot_id;
  4136. snapshot->region = region;
  4137. snapshot->data = data;
  4138. snapshot->data_len = data_len;
  4139. snapshot->data_destructor = data_destructor;
  4140. list_add_tail(&snapshot->list, &region->snapshot_list);
  4141. region->cur_snapshots++;
  4142. devlink_nl_region_notify(region, snapshot, DEVLINK_CMD_REGION_NEW);
  4143. mutex_unlock(&devlink->lock);
  4144. return 0;
  4145. unlock:
  4146. mutex_unlock(&devlink->lock);
  4147. return err;
  4148. }
  4149. EXPORT_SYMBOL_GPL(devlink_region_snapshot_create);
  4150. static int __init devlink_module_init(void)
  4151. {
  4152. return genl_register_family(&devlink_nl_family);
  4153. }
  4154. static void __exit devlink_module_exit(void)
  4155. {
  4156. genl_unregister_family(&devlink_nl_family);
  4157. }
  4158. module_init(devlink_module_init);
  4159. module_exit(devlink_module_exit);
  4160. MODULE_LICENSE("GPL v2");
  4161. MODULE_AUTHOR("Jiri Pirko <jiri@mellanox.com>");
  4162. MODULE_DESCRIPTION("Network physical device Netlink interface");
  4163. MODULE_ALIAS_GENL_FAMILY(DEVLINK_GENL_NAME);