ib_verbs.h 149 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079
  1. /* SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB */
  2. /*
  3. * Copyright (c) 2004 Mellanox Technologies Ltd. All rights reserved.
  4. * Copyright (c) 2004 Infinicon Corporation. All rights reserved.
  5. * Copyright (c) 2004, 2020 Intel Corporation. All rights reserved.
  6. * Copyright (c) 2004 Topspin Corporation. All rights reserved.
  7. * Copyright (c) 2004 Voltaire Corporation. All rights reserved.
  8. * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
  9. * Copyright (c) 2005, 2006, 2007 Cisco Systems. All rights reserved.
  10. */
  11. #ifndef IB_VERBS_H
  12. #define IB_VERBS_H
  13. #include <linux/ethtool.h>
  14. #include <linux/types.h>
  15. #include <linux/device.h>
  16. #include <linux/bvec.h>
  17. #include <linux/dma-mapping.h>
  18. #include <linux/kref.h>
  19. #include <linux/list.h>
  20. #include <linux/rwsem.h>
  21. #include <linux/workqueue.h>
  22. #include <linux/irq_poll.h>
  23. #include <uapi/linux/if_ether.h>
  24. #include <net/ipv6.h>
  25. #include <net/ip.h>
  26. #include <linux/string.h>
  27. #include <linux/slab.h>
  28. #include <linux/netdevice.h>
  29. #include <linux/refcount.h>
  30. #include <linux/if_link.h>
  31. #include <linux/atomic.h>
  32. #include <linux/mmu_notifier.h>
  33. #include <linux/uaccess.h>
  34. #include <linux/cgroup_rdma.h>
  35. #include <linux/irqflags.h>
  36. #include <linux/preempt.h>
  37. #include <linux/dim.h>
  38. #include <uapi/rdma/ib_user_verbs.h>
  39. #include <rdma/rdma_counter.h>
  40. #include <rdma/restrack.h>
  41. #include <rdma/signature.h>
  42. #include <uapi/rdma/rdma_user_ioctl.h>
  43. #include <uapi/rdma/ib_user_ioctl_verbs.h>
  44. #include <linux/pci-tph.h>
  45. #include <linux/dma-buf.h>
  46. #define IB_FW_VERSION_NAME_MAX ETHTOOL_FWVERS_LEN
  47. struct ib_umem_odp;
  48. struct ib_uqp_object;
  49. struct ib_usrq_object;
  50. struct ib_uwq_object;
  51. struct rdma_cm_id;
  52. struct ib_port;
  53. struct hw_stats_device_data;
  54. extern struct workqueue_struct *ib_wq;
  55. extern struct workqueue_struct *ib_comp_wq;
  56. extern struct workqueue_struct *ib_comp_unbound_wq;
  57. struct ib_ucq_object;
  58. __printf(2, 3) __cold
  59. void ibdev_emerg(const struct ib_device *ibdev, const char *format, ...);
  60. __printf(2, 3) __cold
  61. void ibdev_alert(const struct ib_device *ibdev, const char *format, ...);
  62. __printf(2, 3) __cold
  63. void ibdev_crit(const struct ib_device *ibdev, const char *format, ...);
  64. __printf(2, 3) __cold
  65. void ibdev_err(const struct ib_device *ibdev, const char *format, ...);
  66. __printf(2, 3) __cold
  67. void ibdev_warn(const struct ib_device *ibdev, const char *format, ...);
  68. __printf(2, 3) __cold
  69. void ibdev_notice(const struct ib_device *ibdev, const char *format, ...);
  70. __printf(2, 3) __cold
  71. void ibdev_info(const struct ib_device *ibdev, const char *format, ...);
  72. #if defined(CONFIG_DYNAMIC_DEBUG) || \
  73. (defined(CONFIG_DYNAMIC_DEBUG_CORE) && defined(DYNAMIC_DEBUG_MODULE))
  74. #define ibdev_dbg(__dev, format, args...) \
  75. dynamic_ibdev_dbg(__dev, format, ##args)
  76. #else
  77. __printf(2, 3) __cold
  78. static inline
  79. void ibdev_dbg(const struct ib_device *ibdev, const char *format, ...) {}
  80. #endif
  81. #define ibdev_level_ratelimited(ibdev_level, ibdev, fmt, ...) \
  82. do { \
  83. static DEFINE_RATELIMIT_STATE(_rs, \
  84. DEFAULT_RATELIMIT_INTERVAL, \
  85. DEFAULT_RATELIMIT_BURST); \
  86. if (__ratelimit(&_rs)) \
  87. ibdev_level(ibdev, fmt, ##__VA_ARGS__); \
  88. } while (0)
  89. #define ibdev_emerg_ratelimited(ibdev, fmt, ...) \
  90. ibdev_level_ratelimited(ibdev_emerg, ibdev, fmt, ##__VA_ARGS__)
  91. #define ibdev_alert_ratelimited(ibdev, fmt, ...) \
  92. ibdev_level_ratelimited(ibdev_alert, ibdev, fmt, ##__VA_ARGS__)
  93. #define ibdev_crit_ratelimited(ibdev, fmt, ...) \
  94. ibdev_level_ratelimited(ibdev_crit, ibdev, fmt, ##__VA_ARGS__)
  95. #define ibdev_err_ratelimited(ibdev, fmt, ...) \
  96. ibdev_level_ratelimited(ibdev_err, ibdev, fmt, ##__VA_ARGS__)
  97. #define ibdev_warn_ratelimited(ibdev, fmt, ...) \
  98. ibdev_level_ratelimited(ibdev_warn, ibdev, fmt, ##__VA_ARGS__)
  99. #define ibdev_notice_ratelimited(ibdev, fmt, ...) \
  100. ibdev_level_ratelimited(ibdev_notice, ibdev, fmt, ##__VA_ARGS__)
  101. #define ibdev_info_ratelimited(ibdev, fmt, ...) \
  102. ibdev_level_ratelimited(ibdev_info, ibdev, fmt, ##__VA_ARGS__)
  103. #if defined(CONFIG_DYNAMIC_DEBUG) || \
  104. (defined(CONFIG_DYNAMIC_DEBUG_CORE) && defined(DYNAMIC_DEBUG_MODULE))
  105. /* descriptor check is first to prevent flooding with "callbacks suppressed" */
  106. #define ibdev_dbg_ratelimited(ibdev, fmt, ...) \
  107. do { \
  108. static DEFINE_RATELIMIT_STATE(_rs, \
  109. DEFAULT_RATELIMIT_INTERVAL, \
  110. DEFAULT_RATELIMIT_BURST); \
  111. DEFINE_DYNAMIC_DEBUG_METADATA(descriptor, fmt); \
  112. if (DYNAMIC_DEBUG_BRANCH(descriptor) && __ratelimit(&_rs)) \
  113. __dynamic_ibdev_dbg(&descriptor, ibdev, fmt, \
  114. ##__VA_ARGS__); \
  115. } while (0)
  116. #else
  117. __printf(2, 3) __cold
  118. static inline
  119. void ibdev_dbg_ratelimited(const struct ib_device *ibdev, const char *format, ...) {}
  120. #endif
  121. union ib_gid {
  122. u8 raw[16];
  123. struct {
  124. __be64 subnet_prefix;
  125. __be64 interface_id;
  126. } global;
  127. };
  128. extern union ib_gid zgid;
  129. enum ib_gid_type {
  130. IB_GID_TYPE_IB = IB_UVERBS_GID_TYPE_IB,
  131. IB_GID_TYPE_ROCE = IB_UVERBS_GID_TYPE_ROCE_V1,
  132. IB_GID_TYPE_ROCE_UDP_ENCAP = IB_UVERBS_GID_TYPE_ROCE_V2,
  133. IB_GID_TYPE_SIZE
  134. };
  135. #define ROCE_V2_UDP_DPORT 4791
  136. struct ib_gid_attr {
  137. struct net_device __rcu *ndev;
  138. struct ib_device *device;
  139. union ib_gid gid;
  140. enum ib_gid_type gid_type;
  141. u16 index;
  142. u32 port_num;
  143. };
  144. enum {
  145. /* set the local administered indication */
  146. IB_SA_WELL_KNOWN_GUID = BIT_ULL(57) | 2,
  147. };
  148. enum rdma_transport_type {
  149. RDMA_TRANSPORT_IB,
  150. RDMA_TRANSPORT_IWARP,
  151. RDMA_TRANSPORT_USNIC,
  152. RDMA_TRANSPORT_USNIC_UDP,
  153. RDMA_TRANSPORT_UNSPECIFIED,
  154. };
  155. enum rdma_protocol_type {
  156. RDMA_PROTOCOL_IB,
  157. RDMA_PROTOCOL_IBOE,
  158. RDMA_PROTOCOL_IWARP,
  159. RDMA_PROTOCOL_USNIC_UDP
  160. };
  161. __attribute_const__ enum rdma_transport_type
  162. rdma_node_get_transport(unsigned int node_type);
  163. enum rdma_network_type {
  164. RDMA_NETWORK_IB,
  165. RDMA_NETWORK_ROCE_V1,
  166. RDMA_NETWORK_IPV4,
  167. RDMA_NETWORK_IPV6
  168. };
  169. static inline enum ib_gid_type ib_network_to_gid_type(enum rdma_network_type network_type)
  170. {
  171. if (network_type == RDMA_NETWORK_IPV4 ||
  172. network_type == RDMA_NETWORK_IPV6)
  173. return IB_GID_TYPE_ROCE_UDP_ENCAP;
  174. else if (network_type == RDMA_NETWORK_ROCE_V1)
  175. return IB_GID_TYPE_ROCE;
  176. else
  177. return IB_GID_TYPE_IB;
  178. }
  179. static inline enum rdma_network_type
  180. rdma_gid_attr_network_type(const struct ib_gid_attr *attr)
  181. {
  182. if (attr->gid_type == IB_GID_TYPE_IB)
  183. return RDMA_NETWORK_IB;
  184. if (attr->gid_type == IB_GID_TYPE_ROCE)
  185. return RDMA_NETWORK_ROCE_V1;
  186. if (ipv6_addr_v4mapped((struct in6_addr *)&attr->gid))
  187. return RDMA_NETWORK_IPV4;
  188. else
  189. return RDMA_NETWORK_IPV6;
  190. }
  191. enum rdma_link_layer {
  192. IB_LINK_LAYER_UNSPECIFIED,
  193. IB_LINK_LAYER_INFINIBAND,
  194. IB_LINK_LAYER_ETHERNET,
  195. };
  196. enum ib_device_cap_flags {
  197. IB_DEVICE_RESIZE_MAX_WR = IB_UVERBS_DEVICE_RESIZE_MAX_WR,
  198. IB_DEVICE_BAD_PKEY_CNTR = IB_UVERBS_DEVICE_BAD_PKEY_CNTR,
  199. IB_DEVICE_BAD_QKEY_CNTR = IB_UVERBS_DEVICE_BAD_QKEY_CNTR,
  200. IB_DEVICE_RAW_MULTI = IB_UVERBS_DEVICE_RAW_MULTI,
  201. IB_DEVICE_AUTO_PATH_MIG = IB_UVERBS_DEVICE_AUTO_PATH_MIG,
  202. IB_DEVICE_CHANGE_PHY_PORT = IB_UVERBS_DEVICE_CHANGE_PHY_PORT,
  203. IB_DEVICE_UD_AV_PORT_ENFORCE = IB_UVERBS_DEVICE_UD_AV_PORT_ENFORCE,
  204. IB_DEVICE_CURR_QP_STATE_MOD = IB_UVERBS_DEVICE_CURR_QP_STATE_MOD,
  205. IB_DEVICE_SHUTDOWN_PORT = IB_UVERBS_DEVICE_SHUTDOWN_PORT,
  206. /* IB_DEVICE_INIT_TYPE = IB_UVERBS_DEVICE_INIT_TYPE, (not in use) */
  207. IB_DEVICE_PORT_ACTIVE_EVENT = IB_UVERBS_DEVICE_PORT_ACTIVE_EVENT,
  208. IB_DEVICE_SYS_IMAGE_GUID = IB_UVERBS_DEVICE_SYS_IMAGE_GUID,
  209. IB_DEVICE_RC_RNR_NAK_GEN = IB_UVERBS_DEVICE_RC_RNR_NAK_GEN,
  210. IB_DEVICE_SRQ_RESIZE = IB_UVERBS_DEVICE_SRQ_RESIZE,
  211. IB_DEVICE_N_NOTIFY_CQ = IB_UVERBS_DEVICE_N_NOTIFY_CQ,
  212. /* Reserved, old SEND_W_INV = 1 << 16,*/
  213. IB_DEVICE_MEM_WINDOW = IB_UVERBS_DEVICE_MEM_WINDOW,
  214. /*
  215. * Devices should set IB_DEVICE_UD_IP_SUM if they support
  216. * insertion of UDP and TCP checksum on outgoing UD IPoIB
  217. * messages and can verify the validity of checksum for
  218. * incoming messages. Setting this flag implies that the
  219. * IPoIB driver may set NETIF_F_IP_CSUM for datagram mode.
  220. */
  221. IB_DEVICE_UD_IP_CSUM = IB_UVERBS_DEVICE_UD_IP_CSUM,
  222. IB_DEVICE_XRC = IB_UVERBS_DEVICE_XRC,
  223. /*
  224. * This device supports the IB "base memory management extension",
  225. * which includes support for fast registrations (IB_WR_REG_MR,
  226. * IB_WR_LOCAL_INV and IB_WR_SEND_WITH_INV verbs). This flag should
  227. * also be set by any iWarp device which must support FRs to comply
  228. * to the iWarp verbs spec. iWarp devices also support the
  229. * IB_WR_RDMA_READ_WITH_INV verb for RDMA READs that invalidate the
  230. * stag.
  231. */
  232. IB_DEVICE_MEM_MGT_EXTENSIONS = IB_UVERBS_DEVICE_MEM_MGT_EXTENSIONS,
  233. IB_DEVICE_MEM_WINDOW_TYPE_2A = IB_UVERBS_DEVICE_MEM_WINDOW_TYPE_2A,
  234. IB_DEVICE_MEM_WINDOW_TYPE_2B = IB_UVERBS_DEVICE_MEM_WINDOW_TYPE_2B,
  235. IB_DEVICE_RC_IP_CSUM = IB_UVERBS_DEVICE_RC_IP_CSUM,
  236. /* Deprecated. Please use IB_RAW_PACKET_CAP_IP_CSUM. */
  237. IB_DEVICE_RAW_IP_CSUM = IB_UVERBS_DEVICE_RAW_IP_CSUM,
  238. IB_DEVICE_MANAGED_FLOW_STEERING =
  239. IB_UVERBS_DEVICE_MANAGED_FLOW_STEERING,
  240. /* Deprecated. Please use IB_RAW_PACKET_CAP_SCATTER_FCS. */
  241. IB_DEVICE_RAW_SCATTER_FCS = IB_UVERBS_DEVICE_RAW_SCATTER_FCS,
  242. /* The device supports padding incoming writes to cacheline. */
  243. IB_DEVICE_PCI_WRITE_END_PADDING =
  244. IB_UVERBS_DEVICE_PCI_WRITE_END_PADDING,
  245. /* Placement type attributes */
  246. IB_DEVICE_FLUSH_GLOBAL = IB_UVERBS_DEVICE_FLUSH_GLOBAL,
  247. IB_DEVICE_FLUSH_PERSISTENT = IB_UVERBS_DEVICE_FLUSH_PERSISTENT,
  248. IB_DEVICE_ATOMIC_WRITE = IB_UVERBS_DEVICE_ATOMIC_WRITE,
  249. };
  250. enum ib_kernel_cap_flags {
  251. /*
  252. * This device supports a per-device lkey or stag that can be
  253. * used without performing a memory registration for the local
  254. * memory. Note that ULPs should never check this flag, but
  255. * instead of use the local_dma_lkey flag in the ib_pd structure,
  256. * which will always contain a usable lkey.
  257. */
  258. IBK_LOCAL_DMA_LKEY = 1 << 0,
  259. /* IB_QP_CREATE_INTEGRITY_EN is supported to implement T10-PI */
  260. IBK_INTEGRITY_HANDOVER = 1 << 1,
  261. /* IB_ACCESS_ON_DEMAND is supported during reg_user_mr() */
  262. IBK_ON_DEMAND_PAGING = 1 << 2,
  263. /* IB_MR_TYPE_SG_GAPS is supported */
  264. IBK_SG_GAPS_REG = 1 << 3,
  265. /* Driver supports RDMA_NLDEV_CMD_DELLINK */
  266. IBK_ALLOW_USER_UNREG = 1 << 4,
  267. /* ipoib will use IB_QP_CREATE_BLOCK_MULTICAST_LOOPBACK */
  268. IBK_BLOCK_MULTICAST_LOOPBACK = 1 << 5,
  269. /* iopib will use IB_QP_CREATE_IPOIB_UD_LSO for its QPs */
  270. IBK_UD_TSO = 1 << 6,
  271. /* iopib will use the device ops:
  272. * get_vf_config
  273. * get_vf_guid
  274. * get_vf_stats
  275. * set_vf_guid
  276. * set_vf_link_state
  277. */
  278. IBK_VIRTUAL_FUNCTION = 1 << 7,
  279. /* ipoib will use IB_QP_CREATE_NETDEV_USE for its QPs */
  280. IBK_RDMA_NETDEV_OPA = 1 << 8,
  281. };
  282. enum ib_atomic_cap {
  283. IB_ATOMIC_NONE,
  284. IB_ATOMIC_HCA,
  285. IB_ATOMIC_GLOB
  286. };
  287. enum ib_odp_general_cap_bits {
  288. IB_ODP_SUPPORT = IB_UVERBS_ODP_SUPPORT,
  289. IB_ODP_SUPPORT_IMPLICIT = IB_UVERBS_ODP_SUPPORT_IMPLICIT,
  290. };
  291. enum ib_odp_transport_cap_bits {
  292. IB_ODP_SUPPORT_SEND = IB_UVERBS_ODP_SUPPORT_SEND,
  293. IB_ODP_SUPPORT_RECV = IB_UVERBS_ODP_SUPPORT_RECV,
  294. IB_ODP_SUPPORT_WRITE = IB_UVERBS_ODP_SUPPORT_WRITE,
  295. IB_ODP_SUPPORT_READ = IB_UVERBS_ODP_SUPPORT_READ,
  296. IB_ODP_SUPPORT_ATOMIC = IB_UVERBS_ODP_SUPPORT_ATOMIC,
  297. IB_ODP_SUPPORT_SRQ_RECV = IB_UVERBS_ODP_SUPPORT_SRQ_RECV,
  298. IB_ODP_SUPPORT_FLUSH = IB_UVERBS_ODP_SUPPORT_FLUSH,
  299. IB_ODP_SUPPORT_ATOMIC_WRITE = IB_UVERBS_ODP_SUPPORT_ATOMIC_WRITE,
  300. };
  301. struct ib_odp_caps {
  302. uint64_t general_caps;
  303. struct {
  304. uint32_t rc_odp_caps;
  305. uint32_t uc_odp_caps;
  306. uint32_t ud_odp_caps;
  307. uint32_t xrc_odp_caps;
  308. } per_transport_caps;
  309. };
  310. struct ib_rss_caps {
  311. /* Corresponding bit will be set if qp type from
  312. * 'enum ib_qp_type' is supported, e.g.
  313. * supported_qpts |= 1 << IB_QPT_UD
  314. */
  315. u32 supported_qpts;
  316. u32 max_rwq_indirection_tables;
  317. u32 max_rwq_indirection_table_size;
  318. };
  319. enum ib_tm_cap_flags {
  320. /* Support tag matching with rendezvous offload for RC transport */
  321. IB_TM_CAP_RNDV_RC = 1 << 0,
  322. };
  323. struct ib_tm_caps {
  324. /* Max size of RNDV header */
  325. u32 max_rndv_hdr_size;
  326. /* Max number of entries in tag matching list */
  327. u32 max_num_tags;
  328. /* From enum ib_tm_cap_flags */
  329. u32 flags;
  330. /* Max number of outstanding list operations */
  331. u32 max_ops;
  332. /* Max number of SGE in tag matching entry */
  333. u32 max_sge;
  334. };
  335. struct ib_cq_init_attr {
  336. unsigned int cqe;
  337. u32 comp_vector;
  338. u32 flags;
  339. };
  340. enum ib_cq_attr_mask {
  341. IB_CQ_MODERATE = 1 << 0,
  342. };
  343. struct ib_cq_caps {
  344. u16 max_cq_moderation_count;
  345. u16 max_cq_moderation_period;
  346. };
  347. struct ib_dm_mr_attr {
  348. u64 length;
  349. u64 offset;
  350. u32 access_flags;
  351. };
  352. struct ib_dm_alloc_attr {
  353. u64 length;
  354. u32 alignment;
  355. u32 flags;
  356. };
  357. struct ib_device_attr {
  358. u64 fw_ver;
  359. __be64 sys_image_guid;
  360. u64 max_mr_size;
  361. u64 page_size_cap;
  362. u32 vendor_id;
  363. u32 vendor_part_id;
  364. u32 hw_ver;
  365. int max_qp;
  366. int max_qp_wr;
  367. u64 device_cap_flags;
  368. u64 kernel_cap_flags;
  369. int max_send_sge;
  370. int max_recv_sge;
  371. int max_sge_rd;
  372. int max_cq;
  373. int max_cqe;
  374. int max_mr;
  375. int max_pd;
  376. int max_qp_rd_atom;
  377. int max_ee_rd_atom;
  378. int max_res_rd_atom;
  379. int max_qp_init_rd_atom;
  380. int max_ee_init_rd_atom;
  381. enum ib_atomic_cap atomic_cap;
  382. enum ib_atomic_cap masked_atomic_cap;
  383. int max_ee;
  384. int max_rdd;
  385. int max_mw;
  386. int max_raw_ipv6_qp;
  387. int max_raw_ethy_qp;
  388. int max_mcast_grp;
  389. int max_mcast_qp_attach;
  390. int max_total_mcast_qp_attach;
  391. int max_ah;
  392. int max_srq;
  393. int max_srq_wr;
  394. int max_srq_sge;
  395. unsigned int max_fast_reg_page_list_len;
  396. unsigned int max_pi_fast_reg_page_list_len;
  397. u16 max_pkeys;
  398. u8 local_ca_ack_delay;
  399. int sig_prot_cap;
  400. int sig_guard_cap;
  401. struct ib_odp_caps odp_caps;
  402. uint64_t timestamp_mask;
  403. uint64_t hca_core_clock; /* in KHZ */
  404. struct ib_rss_caps rss_caps;
  405. u32 max_wq_type_rq;
  406. u32 raw_packet_caps; /* Use ib_raw_packet_caps enum */
  407. struct ib_tm_caps tm_caps;
  408. struct ib_cq_caps cq_caps;
  409. u64 max_dm_size;
  410. /* Max entries for sgl for optimized performance per READ */
  411. u32 max_sgl_rd;
  412. };
  413. enum ib_mtu {
  414. IB_MTU_256 = 1,
  415. IB_MTU_512 = 2,
  416. IB_MTU_1024 = 3,
  417. IB_MTU_2048 = 4,
  418. IB_MTU_4096 = 5
  419. };
  420. enum opa_mtu {
  421. OPA_MTU_8192 = 6,
  422. OPA_MTU_10240 = 7
  423. };
  424. static inline int ib_mtu_enum_to_int(enum ib_mtu mtu)
  425. {
  426. switch (mtu) {
  427. case IB_MTU_256: return 256;
  428. case IB_MTU_512: return 512;
  429. case IB_MTU_1024: return 1024;
  430. case IB_MTU_2048: return 2048;
  431. case IB_MTU_4096: return 4096;
  432. default: return -1;
  433. }
  434. }
  435. static inline enum ib_mtu ib_mtu_int_to_enum(int mtu)
  436. {
  437. if (mtu >= 4096)
  438. return IB_MTU_4096;
  439. else if (mtu >= 2048)
  440. return IB_MTU_2048;
  441. else if (mtu >= 1024)
  442. return IB_MTU_1024;
  443. else if (mtu >= 512)
  444. return IB_MTU_512;
  445. else
  446. return IB_MTU_256;
  447. }
  448. static inline int opa_mtu_enum_to_int(enum opa_mtu mtu)
  449. {
  450. switch (mtu) {
  451. case OPA_MTU_8192:
  452. return 8192;
  453. case OPA_MTU_10240:
  454. return 10240;
  455. default:
  456. return(ib_mtu_enum_to_int((enum ib_mtu)mtu));
  457. }
  458. }
  459. static inline enum opa_mtu opa_mtu_int_to_enum(int mtu)
  460. {
  461. if (mtu >= 10240)
  462. return OPA_MTU_10240;
  463. else if (mtu >= 8192)
  464. return OPA_MTU_8192;
  465. else
  466. return ((enum opa_mtu)ib_mtu_int_to_enum(mtu));
  467. }
  468. enum ib_port_state {
  469. IB_PORT_NOP = 0,
  470. IB_PORT_DOWN = 1,
  471. IB_PORT_INIT = 2,
  472. IB_PORT_ARMED = 3,
  473. IB_PORT_ACTIVE = 4,
  474. IB_PORT_ACTIVE_DEFER = 5
  475. };
  476. static inline const char *__attribute_const__
  477. ib_port_state_to_str(enum ib_port_state state)
  478. {
  479. const char * const states[] = {
  480. [IB_PORT_NOP] = "NOP",
  481. [IB_PORT_DOWN] = "DOWN",
  482. [IB_PORT_INIT] = "INIT",
  483. [IB_PORT_ARMED] = "ARMED",
  484. [IB_PORT_ACTIVE] = "ACTIVE",
  485. [IB_PORT_ACTIVE_DEFER] = "ACTIVE_DEFER",
  486. };
  487. if (state < ARRAY_SIZE(states))
  488. return states[state];
  489. return "UNKNOWN";
  490. }
  491. enum ib_port_phys_state {
  492. IB_PORT_PHYS_STATE_SLEEP = 1,
  493. IB_PORT_PHYS_STATE_POLLING = 2,
  494. IB_PORT_PHYS_STATE_DISABLED = 3,
  495. IB_PORT_PHYS_STATE_PORT_CONFIGURATION_TRAINING = 4,
  496. IB_PORT_PHYS_STATE_LINK_UP = 5,
  497. IB_PORT_PHYS_STATE_LINK_ERROR_RECOVERY = 6,
  498. IB_PORT_PHYS_STATE_PHY_TEST = 7,
  499. };
  500. enum ib_port_width {
  501. IB_WIDTH_1X = 1,
  502. IB_WIDTH_2X = 16,
  503. IB_WIDTH_4X = 2,
  504. IB_WIDTH_8X = 4,
  505. IB_WIDTH_12X = 8
  506. };
  507. static inline int ib_width_enum_to_int(enum ib_port_width width)
  508. {
  509. switch (width) {
  510. case IB_WIDTH_1X: return 1;
  511. case IB_WIDTH_2X: return 2;
  512. case IB_WIDTH_4X: return 4;
  513. case IB_WIDTH_8X: return 8;
  514. case IB_WIDTH_12X: return 12;
  515. default: return -1;
  516. }
  517. }
  518. enum ib_port_speed {
  519. IB_SPEED_SDR = 1,
  520. IB_SPEED_DDR = 2,
  521. IB_SPEED_QDR = 4,
  522. IB_SPEED_FDR10 = 8,
  523. IB_SPEED_FDR = 16,
  524. IB_SPEED_EDR = 32,
  525. IB_SPEED_HDR = 64,
  526. IB_SPEED_NDR = 128,
  527. IB_SPEED_XDR = 256,
  528. };
  529. enum ib_stat_flag {
  530. IB_STAT_FLAG_OPTIONAL = 1 << 0,
  531. };
  532. /**
  533. * struct rdma_stat_desc - description of one rdma stat/counter
  534. * @name: The name of the counter
  535. * @flags: Flags of the counter; For example, IB_STAT_FLAG_OPTIONAL
  536. * @priv: Driver private information; Core code should not use
  537. */
  538. struct rdma_stat_desc {
  539. const char *name;
  540. unsigned int flags;
  541. const void *priv;
  542. };
  543. /**
  544. * struct rdma_hw_stats - collection of hardware stats and their management
  545. * @lock: Mutex to protect parallel write access to lifespan and values
  546. * of counters, which are 64bits and not guaranteed to be written
  547. * atomicaly on 32bits systems.
  548. * @timestamp: Used by the core code to track when the last update was
  549. * @lifespan: Used by the core code to determine how old the counters
  550. * should be before being updated again. Stored in jiffies, defaults
  551. * to 10 milliseconds, drivers can override the default be specifying
  552. * their own value during their allocation routine.
  553. * @descs: Array of pointers to static descriptors used for the counters
  554. * in directory.
  555. * @is_disabled: A bitmap to indicate each counter is currently disabled
  556. * or not.
  557. * @num_counters: How many hardware counters there are. If name is
  558. * shorter than this number, a kernel oops will result. Driver authors
  559. * are encouraged to leave BUILD_BUG_ON(ARRAY_SIZE(@name) < num_counters)
  560. * in their code to prevent this.
  561. * @value: Array of u64 counters that are accessed by the sysfs code and
  562. * filled in by the drivers get_stats routine
  563. */
  564. struct rdma_hw_stats {
  565. struct mutex lock; /* Protect lifespan and values[] */
  566. unsigned long timestamp;
  567. unsigned long lifespan;
  568. const struct rdma_stat_desc *descs;
  569. unsigned long *is_disabled;
  570. int num_counters;
  571. u64 value[] __counted_by(num_counters);
  572. };
  573. #define RDMA_HW_STATS_DEFAULT_LIFESPAN 10
  574. struct rdma_hw_stats *rdma_alloc_hw_stats_struct(
  575. const struct rdma_stat_desc *descs, int num_counters,
  576. unsigned long lifespan);
  577. void rdma_free_hw_stats_struct(struct rdma_hw_stats *stats);
  578. /* Define bits for the various functionality this port needs to be supported by
  579. * the core.
  580. */
  581. /* Management 0x00000FFF */
  582. #define RDMA_CORE_CAP_IB_MAD 0x00000001
  583. #define RDMA_CORE_CAP_IB_SMI 0x00000002
  584. #define RDMA_CORE_CAP_IB_CM 0x00000004
  585. #define RDMA_CORE_CAP_IW_CM 0x00000008
  586. #define RDMA_CORE_CAP_IB_SA 0x00000010
  587. #define RDMA_CORE_CAP_OPA_MAD 0x00000020
  588. /* Address format 0x000FF000 */
  589. #define RDMA_CORE_CAP_AF_IB 0x00001000
  590. #define RDMA_CORE_CAP_ETH_AH 0x00002000
  591. #define RDMA_CORE_CAP_OPA_AH 0x00004000
  592. #define RDMA_CORE_CAP_IB_GRH_REQUIRED 0x00008000
  593. /* Protocol 0xFFF00000 */
  594. #define RDMA_CORE_CAP_PROT_IB 0x00100000
  595. #define RDMA_CORE_CAP_PROT_ROCE 0x00200000
  596. #define RDMA_CORE_CAP_PROT_IWARP 0x00400000
  597. #define RDMA_CORE_CAP_PROT_ROCE_UDP_ENCAP 0x00800000
  598. #define RDMA_CORE_CAP_PROT_RAW_PACKET 0x01000000
  599. #define RDMA_CORE_CAP_PROT_USNIC 0x02000000
  600. #define RDMA_CORE_PORT_IB_GRH_REQUIRED (RDMA_CORE_CAP_IB_GRH_REQUIRED \
  601. | RDMA_CORE_CAP_PROT_ROCE \
  602. | RDMA_CORE_CAP_PROT_ROCE_UDP_ENCAP)
  603. #define RDMA_CORE_PORT_IBA_IB (RDMA_CORE_CAP_PROT_IB \
  604. | RDMA_CORE_CAP_IB_MAD \
  605. | RDMA_CORE_CAP_IB_SMI \
  606. | RDMA_CORE_CAP_IB_CM \
  607. | RDMA_CORE_CAP_IB_SA \
  608. | RDMA_CORE_CAP_AF_IB)
  609. #define RDMA_CORE_PORT_IBA_ROCE (RDMA_CORE_CAP_PROT_ROCE \
  610. | RDMA_CORE_CAP_IB_MAD \
  611. | RDMA_CORE_CAP_IB_CM \
  612. | RDMA_CORE_CAP_AF_IB \
  613. | RDMA_CORE_CAP_ETH_AH)
  614. #define RDMA_CORE_PORT_IBA_ROCE_UDP_ENCAP \
  615. (RDMA_CORE_CAP_PROT_ROCE_UDP_ENCAP \
  616. | RDMA_CORE_CAP_IB_MAD \
  617. | RDMA_CORE_CAP_IB_CM \
  618. | RDMA_CORE_CAP_AF_IB \
  619. | RDMA_CORE_CAP_ETH_AH)
  620. #define RDMA_CORE_PORT_IWARP (RDMA_CORE_CAP_PROT_IWARP \
  621. | RDMA_CORE_CAP_IW_CM)
  622. #define RDMA_CORE_PORT_INTEL_OPA (RDMA_CORE_PORT_IBA_IB \
  623. | RDMA_CORE_CAP_OPA_MAD)
  624. #define RDMA_CORE_PORT_RAW_PACKET (RDMA_CORE_CAP_PROT_RAW_PACKET)
  625. #define RDMA_CORE_PORT_USNIC (RDMA_CORE_CAP_PROT_USNIC)
  626. struct ib_port_attr {
  627. u64 subnet_prefix;
  628. enum ib_port_state state;
  629. enum ib_mtu max_mtu;
  630. enum ib_mtu active_mtu;
  631. u32 phys_mtu;
  632. int gid_tbl_len;
  633. unsigned int ip_gids:1;
  634. /* This is the value from PortInfo CapabilityMask, defined by IBA */
  635. u32 port_cap_flags;
  636. u32 max_msg_sz;
  637. u32 bad_pkey_cntr;
  638. u32 qkey_viol_cntr;
  639. u16 pkey_tbl_len;
  640. u32 sm_lid;
  641. u32 lid;
  642. u8 lmc;
  643. u8 max_vl_num;
  644. u8 sm_sl;
  645. u8 subnet_timeout;
  646. u8 init_type_reply;
  647. u8 active_width;
  648. u16 active_speed;
  649. u8 phys_state;
  650. u16 port_cap_flags2;
  651. };
  652. enum ib_device_modify_flags {
  653. IB_DEVICE_MODIFY_SYS_IMAGE_GUID = 1 << 0,
  654. IB_DEVICE_MODIFY_NODE_DESC = 1 << 1
  655. };
  656. #define IB_DEVICE_NODE_DESC_MAX 64
  657. struct ib_device_modify {
  658. u64 sys_image_guid;
  659. char node_desc[IB_DEVICE_NODE_DESC_MAX];
  660. };
  661. enum ib_port_modify_flags {
  662. IB_PORT_SHUTDOWN = 1,
  663. IB_PORT_INIT_TYPE = (1<<2),
  664. IB_PORT_RESET_QKEY_CNTR = (1<<3),
  665. IB_PORT_OPA_MASK_CHG = (1<<4)
  666. };
  667. struct ib_port_modify {
  668. u32 set_port_cap_mask;
  669. u32 clr_port_cap_mask;
  670. u8 init_type;
  671. };
  672. enum ib_event_type {
  673. IB_EVENT_CQ_ERR,
  674. IB_EVENT_QP_FATAL,
  675. IB_EVENT_QP_REQ_ERR,
  676. IB_EVENT_QP_ACCESS_ERR,
  677. IB_EVENT_COMM_EST,
  678. IB_EVENT_SQ_DRAINED,
  679. IB_EVENT_PATH_MIG,
  680. IB_EVENT_PATH_MIG_ERR,
  681. IB_EVENT_DEVICE_FATAL,
  682. IB_EVENT_PORT_ACTIVE,
  683. IB_EVENT_PORT_ERR,
  684. IB_EVENT_LID_CHANGE,
  685. IB_EVENT_PKEY_CHANGE,
  686. IB_EVENT_SM_CHANGE,
  687. IB_EVENT_SRQ_ERR,
  688. IB_EVENT_SRQ_LIMIT_REACHED,
  689. IB_EVENT_QP_LAST_WQE_REACHED,
  690. IB_EVENT_CLIENT_REREGISTER,
  691. IB_EVENT_GID_CHANGE,
  692. IB_EVENT_WQ_FATAL,
  693. IB_EVENT_DEVICE_SPEED_CHANGE,
  694. };
  695. const char *__attribute_const__ ib_event_msg(enum ib_event_type event);
  696. struct ib_event {
  697. struct ib_device *device;
  698. union {
  699. struct ib_cq *cq;
  700. struct ib_qp *qp;
  701. struct ib_srq *srq;
  702. struct ib_wq *wq;
  703. u32 port_num;
  704. } element;
  705. enum ib_event_type event;
  706. };
  707. struct ib_event_handler {
  708. struct ib_device *device;
  709. void (*handler)(struct ib_event_handler *, struct ib_event *);
  710. struct list_head list;
  711. };
  712. #define INIT_IB_EVENT_HANDLER(_ptr, _device, _handler) \
  713. do { \
  714. (_ptr)->device = _device; \
  715. (_ptr)->handler = _handler; \
  716. INIT_LIST_HEAD(&(_ptr)->list); \
  717. } while (0)
  718. struct ib_global_route {
  719. const struct ib_gid_attr *sgid_attr;
  720. union ib_gid dgid;
  721. u32 flow_label;
  722. u8 sgid_index;
  723. u8 hop_limit;
  724. u8 traffic_class;
  725. };
  726. struct ib_grh {
  727. __be32 version_tclass_flow;
  728. __be16 paylen;
  729. u8 next_hdr;
  730. u8 hop_limit;
  731. union ib_gid sgid;
  732. union ib_gid dgid;
  733. };
  734. union rdma_network_hdr {
  735. struct ib_grh ibgrh;
  736. struct {
  737. /* The IB spec states that if it's IPv4, the header
  738. * is located in the last 20 bytes of the header.
  739. */
  740. u8 reserved[20];
  741. struct iphdr roce4grh;
  742. };
  743. };
  744. #define IB_QPN_MASK 0xFFFFFF
  745. enum {
  746. IB_MULTICAST_QPN = 0xffffff
  747. };
  748. #define IB_LID_PERMISSIVE cpu_to_be16(0xFFFF)
  749. #define IB_MULTICAST_LID_BASE cpu_to_be16(0xC000)
  750. enum ib_ah_flags {
  751. IB_AH_GRH = 1
  752. };
  753. enum ib_rate {
  754. IB_RATE_PORT_CURRENT = 0,
  755. IB_RATE_2_5_GBPS = 2,
  756. IB_RATE_5_GBPS = 5,
  757. IB_RATE_10_GBPS = 3,
  758. IB_RATE_20_GBPS = 6,
  759. IB_RATE_30_GBPS = 4,
  760. IB_RATE_40_GBPS = 7,
  761. IB_RATE_60_GBPS = 8,
  762. IB_RATE_80_GBPS = 9,
  763. IB_RATE_120_GBPS = 10,
  764. IB_RATE_14_GBPS = 11,
  765. IB_RATE_56_GBPS = 12,
  766. IB_RATE_112_GBPS = 13,
  767. IB_RATE_168_GBPS = 14,
  768. IB_RATE_25_GBPS = 15,
  769. IB_RATE_100_GBPS = 16,
  770. IB_RATE_200_GBPS = 17,
  771. IB_RATE_300_GBPS = 18,
  772. IB_RATE_28_GBPS = 19,
  773. IB_RATE_50_GBPS = 20,
  774. IB_RATE_400_GBPS = 21,
  775. IB_RATE_600_GBPS = 22,
  776. IB_RATE_800_GBPS = 23,
  777. IB_RATE_1600_GBPS = 25,
  778. };
  779. /**
  780. * ib_rate_to_mult - Convert the IB rate enum to a multiple of the
  781. * base rate of 2.5 Gbit/sec. For example, IB_RATE_5_GBPS will be
  782. * converted to 2, since 5 Gbit/sec is 2 * 2.5 Gbit/sec.
  783. * @rate: rate to convert.
  784. */
  785. __attribute_const__ int ib_rate_to_mult(enum ib_rate rate);
  786. /**
  787. * ib_rate_to_mbps - Convert the IB rate enum to Mbps.
  788. * For example, IB_RATE_2_5_GBPS will be converted to 2500.
  789. * @rate: rate to convert.
  790. */
  791. __attribute_const__ int ib_rate_to_mbps(enum ib_rate rate);
  792. struct ib_port_speed_info {
  793. const char *str;
  794. int rate; /* in deci-Gb/sec (100 MBps units) */
  795. };
  796. /**
  797. * ib_port_attr_to_speed_info - Convert port attributes to speed information
  798. * @attr: Port attributes containing active_speed and active_width
  799. * @speed_info: Speed information to return
  800. *
  801. * Returns 0 on success, -EINVAL on error.
  802. */
  803. int ib_port_attr_to_speed_info(struct ib_port_attr *attr,
  804. struct ib_port_speed_info *speed_info);
  805. /**
  806. * enum ib_mr_type - memory region type
  807. * @IB_MR_TYPE_MEM_REG: memory region that is used for
  808. * normal registration
  809. * @IB_MR_TYPE_SG_GAPS: memory region that is capable to
  810. * register any arbitrary sg lists (without
  811. * the normal mr constraints - see
  812. * ib_map_mr_sg)
  813. * @IB_MR_TYPE_DM: memory region that is used for device
  814. * memory registration
  815. * @IB_MR_TYPE_USER: memory region that is used for the user-space
  816. * application
  817. * @IB_MR_TYPE_DMA: memory region that is used for DMA operations
  818. * without address translations (VA=PA)
  819. * @IB_MR_TYPE_INTEGRITY: memory region that is used for
  820. * data integrity operations
  821. */
  822. enum ib_mr_type {
  823. IB_MR_TYPE_MEM_REG,
  824. IB_MR_TYPE_SG_GAPS,
  825. IB_MR_TYPE_DM,
  826. IB_MR_TYPE_USER,
  827. IB_MR_TYPE_DMA,
  828. IB_MR_TYPE_INTEGRITY,
  829. };
  830. enum ib_mr_status_check {
  831. IB_MR_CHECK_SIG_STATUS = 1,
  832. };
  833. /**
  834. * struct ib_mr_status - Memory region status container
  835. *
  836. * @fail_status: Bitmask of MR checks status. For each
  837. * failed check a corresponding status bit is set.
  838. * @sig_err: Additional info for IB_MR_CEHCK_SIG_STATUS
  839. * failure.
  840. */
  841. struct ib_mr_status {
  842. u32 fail_status;
  843. struct ib_sig_err sig_err;
  844. };
  845. /**
  846. * mult_to_ib_rate - Convert a multiple of 2.5 Gbit/sec to an IB rate
  847. * enum.
  848. * @mult: multiple to convert.
  849. */
  850. __attribute_const__ enum ib_rate mult_to_ib_rate(int mult);
  851. struct rdma_ah_init_attr {
  852. struct rdma_ah_attr *ah_attr;
  853. u32 flags;
  854. struct net_device *xmit_slave;
  855. };
  856. enum rdma_ah_attr_type {
  857. RDMA_AH_ATTR_TYPE_UNDEFINED,
  858. RDMA_AH_ATTR_TYPE_IB,
  859. RDMA_AH_ATTR_TYPE_ROCE,
  860. RDMA_AH_ATTR_TYPE_OPA,
  861. };
  862. struct ib_ah_attr {
  863. u16 dlid;
  864. u8 src_path_bits;
  865. };
  866. struct roce_ah_attr {
  867. u8 dmac[ETH_ALEN];
  868. };
  869. struct opa_ah_attr {
  870. u32 dlid;
  871. u8 src_path_bits;
  872. bool make_grd;
  873. };
  874. struct rdma_ah_attr {
  875. struct ib_global_route grh;
  876. u8 sl;
  877. u8 static_rate;
  878. u32 port_num;
  879. u8 ah_flags;
  880. enum rdma_ah_attr_type type;
  881. union {
  882. struct ib_ah_attr ib;
  883. struct roce_ah_attr roce;
  884. struct opa_ah_attr opa;
  885. };
  886. };
  887. enum ib_wc_status {
  888. IB_WC_SUCCESS,
  889. IB_WC_LOC_LEN_ERR,
  890. IB_WC_LOC_QP_OP_ERR,
  891. IB_WC_LOC_EEC_OP_ERR,
  892. IB_WC_LOC_PROT_ERR,
  893. IB_WC_WR_FLUSH_ERR,
  894. IB_WC_MW_BIND_ERR,
  895. IB_WC_BAD_RESP_ERR,
  896. IB_WC_LOC_ACCESS_ERR,
  897. IB_WC_REM_INV_REQ_ERR,
  898. IB_WC_REM_ACCESS_ERR,
  899. IB_WC_REM_OP_ERR,
  900. IB_WC_RETRY_EXC_ERR,
  901. IB_WC_RNR_RETRY_EXC_ERR,
  902. IB_WC_LOC_RDD_VIOL_ERR,
  903. IB_WC_REM_INV_RD_REQ_ERR,
  904. IB_WC_REM_ABORT_ERR,
  905. IB_WC_INV_EECN_ERR,
  906. IB_WC_INV_EEC_STATE_ERR,
  907. IB_WC_FATAL_ERR,
  908. IB_WC_RESP_TIMEOUT_ERR,
  909. IB_WC_GENERAL_ERR
  910. };
  911. const char *__attribute_const__ ib_wc_status_msg(enum ib_wc_status status);
  912. enum ib_wc_opcode {
  913. IB_WC_SEND = IB_UVERBS_WC_SEND,
  914. IB_WC_RDMA_WRITE = IB_UVERBS_WC_RDMA_WRITE,
  915. IB_WC_RDMA_READ = IB_UVERBS_WC_RDMA_READ,
  916. IB_WC_COMP_SWAP = IB_UVERBS_WC_COMP_SWAP,
  917. IB_WC_FETCH_ADD = IB_UVERBS_WC_FETCH_ADD,
  918. IB_WC_BIND_MW = IB_UVERBS_WC_BIND_MW,
  919. IB_WC_LOCAL_INV = IB_UVERBS_WC_LOCAL_INV,
  920. IB_WC_LSO = IB_UVERBS_WC_TSO,
  921. IB_WC_ATOMIC_WRITE = IB_UVERBS_WC_ATOMIC_WRITE,
  922. IB_WC_REG_MR,
  923. IB_WC_MASKED_COMP_SWAP,
  924. IB_WC_MASKED_FETCH_ADD,
  925. IB_WC_FLUSH = IB_UVERBS_WC_FLUSH,
  926. /*
  927. * Set value of IB_WC_RECV so consumers can test if a completion is a
  928. * receive by testing (opcode & IB_WC_RECV).
  929. */
  930. IB_WC_RECV = 1 << 7,
  931. IB_WC_RECV_RDMA_WITH_IMM
  932. };
  933. enum ib_wc_flags {
  934. IB_WC_GRH = 1,
  935. IB_WC_WITH_IMM = (1<<1),
  936. IB_WC_WITH_INVALIDATE = (1<<2),
  937. IB_WC_IP_CSUM_OK = (1<<3),
  938. IB_WC_WITH_SMAC = (1<<4),
  939. IB_WC_WITH_VLAN = (1<<5),
  940. IB_WC_WITH_NETWORK_HDR_TYPE = (1<<6),
  941. };
  942. struct ib_wc {
  943. union {
  944. u64 wr_id;
  945. struct ib_cqe *wr_cqe;
  946. };
  947. enum ib_wc_status status;
  948. enum ib_wc_opcode opcode;
  949. u32 vendor_err;
  950. u32 byte_len;
  951. struct ib_qp *qp;
  952. union {
  953. __be32 imm_data;
  954. u32 invalidate_rkey;
  955. } ex;
  956. u32 src_qp;
  957. u32 slid;
  958. int wc_flags;
  959. u16 pkey_index;
  960. u8 sl;
  961. u8 dlid_path_bits;
  962. u32 port_num; /* valid only for DR SMPs on switches */
  963. u8 smac[ETH_ALEN];
  964. u16 vlan_id;
  965. u8 network_hdr_type;
  966. };
  967. enum ib_cq_notify_flags {
  968. IB_CQ_SOLICITED = 1 << 0,
  969. IB_CQ_NEXT_COMP = 1 << 1,
  970. IB_CQ_SOLICITED_MASK = IB_CQ_SOLICITED | IB_CQ_NEXT_COMP,
  971. IB_CQ_REPORT_MISSED_EVENTS = 1 << 2,
  972. };
  973. enum ib_srq_type {
  974. IB_SRQT_BASIC = IB_UVERBS_SRQT_BASIC,
  975. IB_SRQT_XRC = IB_UVERBS_SRQT_XRC,
  976. IB_SRQT_TM = IB_UVERBS_SRQT_TM,
  977. };
  978. static inline bool ib_srq_has_cq(enum ib_srq_type srq_type)
  979. {
  980. return srq_type == IB_SRQT_XRC ||
  981. srq_type == IB_SRQT_TM;
  982. }
  983. enum ib_srq_attr_mask {
  984. IB_SRQ_MAX_WR = 1 << 0,
  985. IB_SRQ_LIMIT = 1 << 1,
  986. };
  987. struct ib_srq_attr {
  988. u32 max_wr;
  989. u32 max_sge;
  990. u32 srq_limit;
  991. };
  992. struct ib_srq_init_attr {
  993. void (*event_handler)(struct ib_event *, void *);
  994. void *srq_context;
  995. struct ib_srq_attr attr;
  996. enum ib_srq_type srq_type;
  997. struct {
  998. struct ib_cq *cq;
  999. union {
  1000. struct {
  1001. struct ib_xrcd *xrcd;
  1002. } xrc;
  1003. struct {
  1004. u32 max_num_tags;
  1005. } tag_matching;
  1006. };
  1007. } ext;
  1008. };
  1009. struct ib_qp_cap {
  1010. u32 max_send_wr;
  1011. u32 max_recv_wr;
  1012. u32 max_send_sge;
  1013. u32 max_recv_sge;
  1014. u32 max_inline_data;
  1015. /*
  1016. * Maximum number of rdma_rw_ctx structures in flight at a time.
  1017. * ib_create_qp() will calculate the right amount of needed WRs
  1018. * and MRs based on this.
  1019. */
  1020. u32 max_rdma_ctxs;
  1021. };
  1022. enum ib_sig_type {
  1023. IB_SIGNAL_ALL_WR,
  1024. IB_SIGNAL_REQ_WR
  1025. };
  1026. enum ib_qp_type {
  1027. /*
  1028. * IB_QPT_SMI and IB_QPT_GSI have to be the first two entries
  1029. * here (and in that order) since the MAD layer uses them as
  1030. * indices into a 2-entry table.
  1031. */
  1032. IB_QPT_SMI,
  1033. IB_QPT_GSI,
  1034. IB_QPT_RC = IB_UVERBS_QPT_RC,
  1035. IB_QPT_UC = IB_UVERBS_QPT_UC,
  1036. IB_QPT_UD = IB_UVERBS_QPT_UD,
  1037. IB_QPT_RAW_IPV6,
  1038. IB_QPT_RAW_ETHERTYPE,
  1039. IB_QPT_RAW_PACKET = IB_UVERBS_QPT_RAW_PACKET,
  1040. IB_QPT_XRC_INI = IB_UVERBS_QPT_XRC_INI,
  1041. IB_QPT_XRC_TGT = IB_UVERBS_QPT_XRC_TGT,
  1042. IB_QPT_MAX,
  1043. IB_QPT_DRIVER = IB_UVERBS_QPT_DRIVER,
  1044. /* Reserve a range for qp types internal to the low level driver.
  1045. * These qp types will not be visible at the IB core layer, so the
  1046. * IB_QPT_MAX usages should not be affected in the core layer
  1047. */
  1048. IB_QPT_RESERVED1 = 0x1000,
  1049. IB_QPT_RESERVED2,
  1050. IB_QPT_RESERVED3,
  1051. IB_QPT_RESERVED4,
  1052. IB_QPT_RESERVED5,
  1053. IB_QPT_RESERVED6,
  1054. IB_QPT_RESERVED7,
  1055. IB_QPT_RESERVED8,
  1056. IB_QPT_RESERVED9,
  1057. IB_QPT_RESERVED10,
  1058. };
  1059. enum ib_qp_create_flags {
  1060. IB_QP_CREATE_IPOIB_UD_LSO = 1 << 0,
  1061. IB_QP_CREATE_BLOCK_MULTICAST_LOOPBACK =
  1062. IB_UVERBS_QP_CREATE_BLOCK_MULTICAST_LOOPBACK,
  1063. IB_QP_CREATE_CROSS_CHANNEL = 1 << 2,
  1064. IB_QP_CREATE_MANAGED_SEND = 1 << 3,
  1065. IB_QP_CREATE_MANAGED_RECV = 1 << 4,
  1066. IB_QP_CREATE_NETIF_QP = 1 << 5,
  1067. IB_QP_CREATE_INTEGRITY_EN = 1 << 6,
  1068. IB_QP_CREATE_NETDEV_USE = 1 << 7,
  1069. IB_QP_CREATE_SCATTER_FCS =
  1070. IB_UVERBS_QP_CREATE_SCATTER_FCS,
  1071. IB_QP_CREATE_CVLAN_STRIPPING =
  1072. IB_UVERBS_QP_CREATE_CVLAN_STRIPPING,
  1073. IB_QP_CREATE_SOURCE_QPN = 1 << 10,
  1074. IB_QP_CREATE_PCI_WRITE_END_PADDING =
  1075. IB_UVERBS_QP_CREATE_PCI_WRITE_END_PADDING,
  1076. /* reserve bits 26-31 for low level drivers' internal use */
  1077. IB_QP_CREATE_RESERVED_START = 1 << 26,
  1078. IB_QP_CREATE_RESERVED_END = 1 << 31,
  1079. };
  1080. /*
  1081. * Note: users may not call ib_close_qp or ib_destroy_qp from the event_handler
  1082. * callback to destroy the passed in QP.
  1083. */
  1084. struct ib_qp_init_attr {
  1085. /* This callback occurs in workqueue context */
  1086. void (*event_handler)(struct ib_event *, void *);
  1087. void *qp_context;
  1088. struct ib_cq *send_cq;
  1089. struct ib_cq *recv_cq;
  1090. struct ib_srq *srq;
  1091. struct ib_xrcd *xrcd; /* XRC TGT QPs only */
  1092. struct ib_qp_cap cap;
  1093. enum ib_sig_type sq_sig_type;
  1094. enum ib_qp_type qp_type;
  1095. u32 create_flags;
  1096. /*
  1097. * Only needed for special QP types, or when using the RW API.
  1098. */
  1099. u32 port_num;
  1100. struct ib_rwq_ind_table *rwq_ind_tbl;
  1101. u32 source_qpn;
  1102. };
  1103. struct ib_qp_open_attr {
  1104. void (*event_handler)(struct ib_event *, void *);
  1105. void *qp_context;
  1106. u32 qp_num;
  1107. enum ib_qp_type qp_type;
  1108. };
  1109. enum ib_rnr_timeout {
  1110. IB_RNR_TIMER_655_36 = 0,
  1111. IB_RNR_TIMER_000_01 = 1,
  1112. IB_RNR_TIMER_000_02 = 2,
  1113. IB_RNR_TIMER_000_03 = 3,
  1114. IB_RNR_TIMER_000_04 = 4,
  1115. IB_RNR_TIMER_000_06 = 5,
  1116. IB_RNR_TIMER_000_08 = 6,
  1117. IB_RNR_TIMER_000_12 = 7,
  1118. IB_RNR_TIMER_000_16 = 8,
  1119. IB_RNR_TIMER_000_24 = 9,
  1120. IB_RNR_TIMER_000_32 = 10,
  1121. IB_RNR_TIMER_000_48 = 11,
  1122. IB_RNR_TIMER_000_64 = 12,
  1123. IB_RNR_TIMER_000_96 = 13,
  1124. IB_RNR_TIMER_001_28 = 14,
  1125. IB_RNR_TIMER_001_92 = 15,
  1126. IB_RNR_TIMER_002_56 = 16,
  1127. IB_RNR_TIMER_003_84 = 17,
  1128. IB_RNR_TIMER_005_12 = 18,
  1129. IB_RNR_TIMER_007_68 = 19,
  1130. IB_RNR_TIMER_010_24 = 20,
  1131. IB_RNR_TIMER_015_36 = 21,
  1132. IB_RNR_TIMER_020_48 = 22,
  1133. IB_RNR_TIMER_030_72 = 23,
  1134. IB_RNR_TIMER_040_96 = 24,
  1135. IB_RNR_TIMER_061_44 = 25,
  1136. IB_RNR_TIMER_081_92 = 26,
  1137. IB_RNR_TIMER_122_88 = 27,
  1138. IB_RNR_TIMER_163_84 = 28,
  1139. IB_RNR_TIMER_245_76 = 29,
  1140. IB_RNR_TIMER_327_68 = 30,
  1141. IB_RNR_TIMER_491_52 = 31
  1142. };
  1143. enum ib_qp_attr_mask {
  1144. IB_QP_STATE = 1,
  1145. IB_QP_CUR_STATE = (1<<1),
  1146. IB_QP_EN_SQD_ASYNC_NOTIFY = (1<<2),
  1147. IB_QP_ACCESS_FLAGS = (1<<3),
  1148. IB_QP_PKEY_INDEX = (1<<4),
  1149. IB_QP_PORT = (1<<5),
  1150. IB_QP_QKEY = (1<<6),
  1151. IB_QP_AV = (1<<7),
  1152. IB_QP_PATH_MTU = (1<<8),
  1153. IB_QP_TIMEOUT = (1<<9),
  1154. IB_QP_RETRY_CNT = (1<<10),
  1155. IB_QP_RNR_RETRY = (1<<11),
  1156. IB_QP_RQ_PSN = (1<<12),
  1157. IB_QP_MAX_QP_RD_ATOMIC = (1<<13),
  1158. IB_QP_ALT_PATH = (1<<14),
  1159. IB_QP_MIN_RNR_TIMER = (1<<15),
  1160. IB_QP_SQ_PSN = (1<<16),
  1161. IB_QP_MAX_DEST_RD_ATOMIC = (1<<17),
  1162. IB_QP_PATH_MIG_STATE = (1<<18),
  1163. IB_QP_CAP = (1<<19),
  1164. IB_QP_DEST_QPN = (1<<20),
  1165. IB_QP_RESERVED1 = (1<<21),
  1166. IB_QP_RESERVED2 = (1<<22),
  1167. IB_QP_RESERVED3 = (1<<23),
  1168. IB_QP_RESERVED4 = (1<<24),
  1169. IB_QP_RATE_LIMIT = (1<<25),
  1170. IB_QP_ATTR_STANDARD_BITS = GENMASK(20, 0),
  1171. };
  1172. enum ib_qp_state {
  1173. IB_QPS_RESET,
  1174. IB_QPS_INIT,
  1175. IB_QPS_RTR,
  1176. IB_QPS_RTS,
  1177. IB_QPS_SQD,
  1178. IB_QPS_SQE,
  1179. IB_QPS_ERR
  1180. };
  1181. enum ib_mig_state {
  1182. IB_MIG_MIGRATED,
  1183. IB_MIG_REARM,
  1184. IB_MIG_ARMED
  1185. };
  1186. enum ib_mw_type {
  1187. IB_MW_TYPE_1 = 1,
  1188. IB_MW_TYPE_2 = 2
  1189. };
  1190. struct ib_qp_attr {
  1191. enum ib_qp_state qp_state;
  1192. enum ib_qp_state cur_qp_state;
  1193. enum ib_mtu path_mtu;
  1194. enum ib_mig_state path_mig_state;
  1195. u32 qkey;
  1196. u32 rq_psn;
  1197. u32 sq_psn;
  1198. u32 dest_qp_num;
  1199. int qp_access_flags;
  1200. struct ib_qp_cap cap;
  1201. struct rdma_ah_attr ah_attr;
  1202. struct rdma_ah_attr alt_ah_attr;
  1203. u16 pkey_index;
  1204. u16 alt_pkey_index;
  1205. u8 en_sqd_async_notify;
  1206. u8 sq_draining;
  1207. u8 max_rd_atomic;
  1208. u8 max_dest_rd_atomic;
  1209. u8 min_rnr_timer;
  1210. u32 port_num;
  1211. u8 timeout;
  1212. u8 retry_cnt;
  1213. u8 rnr_retry;
  1214. u32 alt_port_num;
  1215. u8 alt_timeout;
  1216. u32 rate_limit;
  1217. struct net_device *xmit_slave;
  1218. };
  1219. enum ib_wr_opcode {
  1220. /* These are shared with userspace */
  1221. IB_WR_RDMA_WRITE = IB_UVERBS_WR_RDMA_WRITE,
  1222. IB_WR_RDMA_WRITE_WITH_IMM = IB_UVERBS_WR_RDMA_WRITE_WITH_IMM,
  1223. IB_WR_SEND = IB_UVERBS_WR_SEND,
  1224. IB_WR_SEND_WITH_IMM = IB_UVERBS_WR_SEND_WITH_IMM,
  1225. IB_WR_RDMA_READ = IB_UVERBS_WR_RDMA_READ,
  1226. IB_WR_ATOMIC_CMP_AND_SWP = IB_UVERBS_WR_ATOMIC_CMP_AND_SWP,
  1227. IB_WR_ATOMIC_FETCH_AND_ADD = IB_UVERBS_WR_ATOMIC_FETCH_AND_ADD,
  1228. IB_WR_BIND_MW = IB_UVERBS_WR_BIND_MW,
  1229. IB_WR_LSO = IB_UVERBS_WR_TSO,
  1230. IB_WR_SEND_WITH_INV = IB_UVERBS_WR_SEND_WITH_INV,
  1231. IB_WR_RDMA_READ_WITH_INV = IB_UVERBS_WR_RDMA_READ_WITH_INV,
  1232. IB_WR_LOCAL_INV = IB_UVERBS_WR_LOCAL_INV,
  1233. IB_WR_MASKED_ATOMIC_CMP_AND_SWP =
  1234. IB_UVERBS_WR_MASKED_ATOMIC_CMP_AND_SWP,
  1235. IB_WR_MASKED_ATOMIC_FETCH_AND_ADD =
  1236. IB_UVERBS_WR_MASKED_ATOMIC_FETCH_AND_ADD,
  1237. IB_WR_FLUSH = IB_UVERBS_WR_FLUSH,
  1238. IB_WR_ATOMIC_WRITE = IB_UVERBS_WR_ATOMIC_WRITE,
  1239. /* These are kernel only and can not be issued by userspace */
  1240. IB_WR_REG_MR = 0x20,
  1241. IB_WR_REG_MR_INTEGRITY,
  1242. /* reserve values for low level drivers' internal use.
  1243. * These values will not be used at all in the ib core layer.
  1244. */
  1245. IB_WR_RESERVED1 = 0xf0,
  1246. IB_WR_RESERVED2,
  1247. IB_WR_RESERVED3,
  1248. IB_WR_RESERVED4,
  1249. IB_WR_RESERVED5,
  1250. IB_WR_RESERVED6,
  1251. IB_WR_RESERVED7,
  1252. IB_WR_RESERVED8,
  1253. IB_WR_RESERVED9,
  1254. IB_WR_RESERVED10,
  1255. };
  1256. enum ib_send_flags {
  1257. IB_SEND_FENCE = 1,
  1258. IB_SEND_SIGNALED = (1<<1),
  1259. IB_SEND_SOLICITED = (1<<2),
  1260. IB_SEND_INLINE = (1<<3),
  1261. IB_SEND_IP_CSUM = (1<<4),
  1262. /* reserve bits 26-31 for low level drivers' internal use */
  1263. IB_SEND_RESERVED_START = (1 << 26),
  1264. IB_SEND_RESERVED_END = (1 << 31),
  1265. };
  1266. struct ib_sge {
  1267. u64 addr;
  1268. u32 length;
  1269. u32 lkey;
  1270. };
  1271. struct ib_cqe {
  1272. void (*done)(struct ib_cq *cq, struct ib_wc *wc);
  1273. };
  1274. struct ib_send_wr {
  1275. struct ib_send_wr *next;
  1276. union {
  1277. u64 wr_id;
  1278. struct ib_cqe *wr_cqe;
  1279. };
  1280. struct ib_sge *sg_list;
  1281. int num_sge;
  1282. enum ib_wr_opcode opcode;
  1283. int send_flags;
  1284. union {
  1285. __be32 imm_data;
  1286. u32 invalidate_rkey;
  1287. } ex;
  1288. };
  1289. struct ib_rdma_wr {
  1290. struct ib_send_wr wr;
  1291. u64 remote_addr;
  1292. u32 rkey;
  1293. };
  1294. static inline const struct ib_rdma_wr *rdma_wr(const struct ib_send_wr *wr)
  1295. {
  1296. return container_of(wr, struct ib_rdma_wr, wr);
  1297. }
  1298. struct ib_atomic_wr {
  1299. struct ib_send_wr wr;
  1300. u64 remote_addr;
  1301. u64 compare_add;
  1302. u64 swap;
  1303. u64 compare_add_mask;
  1304. u64 swap_mask;
  1305. u32 rkey;
  1306. };
  1307. static inline const struct ib_atomic_wr *atomic_wr(const struct ib_send_wr *wr)
  1308. {
  1309. return container_of(wr, struct ib_atomic_wr, wr);
  1310. }
  1311. struct ib_ud_wr {
  1312. struct ib_send_wr wr;
  1313. struct ib_ah *ah;
  1314. void *header;
  1315. int hlen;
  1316. int mss;
  1317. u32 remote_qpn;
  1318. u32 remote_qkey;
  1319. u16 pkey_index; /* valid for GSI only */
  1320. u32 port_num; /* valid for DR SMPs on switch only */
  1321. };
  1322. static inline const struct ib_ud_wr *ud_wr(const struct ib_send_wr *wr)
  1323. {
  1324. return container_of(wr, struct ib_ud_wr, wr);
  1325. }
  1326. struct ib_reg_wr {
  1327. struct ib_send_wr wr;
  1328. struct ib_mr *mr;
  1329. u32 key;
  1330. int access;
  1331. };
  1332. static inline const struct ib_reg_wr *reg_wr(const struct ib_send_wr *wr)
  1333. {
  1334. return container_of(wr, struct ib_reg_wr, wr);
  1335. }
  1336. struct ib_recv_wr {
  1337. struct ib_recv_wr *next;
  1338. union {
  1339. u64 wr_id;
  1340. struct ib_cqe *wr_cqe;
  1341. };
  1342. struct ib_sge *sg_list;
  1343. int num_sge;
  1344. };
  1345. enum ib_access_flags {
  1346. IB_ACCESS_LOCAL_WRITE = IB_UVERBS_ACCESS_LOCAL_WRITE,
  1347. IB_ACCESS_REMOTE_WRITE = IB_UVERBS_ACCESS_REMOTE_WRITE,
  1348. IB_ACCESS_REMOTE_READ = IB_UVERBS_ACCESS_REMOTE_READ,
  1349. IB_ACCESS_REMOTE_ATOMIC = IB_UVERBS_ACCESS_REMOTE_ATOMIC,
  1350. IB_ACCESS_MW_BIND = IB_UVERBS_ACCESS_MW_BIND,
  1351. IB_ZERO_BASED = IB_UVERBS_ACCESS_ZERO_BASED,
  1352. IB_ACCESS_ON_DEMAND = IB_UVERBS_ACCESS_ON_DEMAND,
  1353. IB_ACCESS_HUGETLB = IB_UVERBS_ACCESS_HUGETLB,
  1354. IB_ACCESS_RELAXED_ORDERING = IB_UVERBS_ACCESS_RELAXED_ORDERING,
  1355. IB_ACCESS_FLUSH_GLOBAL = IB_UVERBS_ACCESS_FLUSH_GLOBAL,
  1356. IB_ACCESS_FLUSH_PERSISTENT = IB_UVERBS_ACCESS_FLUSH_PERSISTENT,
  1357. IB_ACCESS_OPTIONAL = IB_UVERBS_ACCESS_OPTIONAL_RANGE,
  1358. IB_ACCESS_SUPPORTED =
  1359. ((IB_ACCESS_FLUSH_PERSISTENT << 1) - 1) | IB_ACCESS_OPTIONAL,
  1360. };
  1361. /*
  1362. * XXX: these are apparently used for ->rereg_user_mr, no idea why they
  1363. * are hidden here instead of a uapi header!
  1364. */
  1365. enum ib_mr_rereg_flags {
  1366. IB_MR_REREG_TRANS = 1,
  1367. IB_MR_REREG_PD = (1<<1),
  1368. IB_MR_REREG_ACCESS = (1<<2),
  1369. IB_MR_REREG_SUPPORTED = ((IB_MR_REREG_ACCESS << 1) - 1)
  1370. };
  1371. struct ib_umem;
  1372. enum rdma_remove_reason {
  1373. /*
  1374. * Userspace requested uobject deletion or initial try
  1375. * to remove uobject via cleanup. Call could fail
  1376. */
  1377. RDMA_REMOVE_DESTROY,
  1378. /* Context deletion. This call should delete the actual object itself */
  1379. RDMA_REMOVE_CLOSE,
  1380. /* Driver is being hot-unplugged. This call should delete the actual object itself */
  1381. RDMA_REMOVE_DRIVER_REMOVE,
  1382. /* uobj is being cleaned-up before being committed */
  1383. RDMA_REMOVE_ABORT,
  1384. /* The driver failed to destroy the uobject and is being disconnected */
  1385. RDMA_REMOVE_DRIVER_FAILURE,
  1386. };
  1387. struct ib_rdmacg_object {
  1388. #ifdef CONFIG_CGROUP_RDMA
  1389. struct rdma_cgroup *cg; /* owner rdma cgroup */
  1390. #endif
  1391. };
  1392. struct ib_ucontext {
  1393. struct ib_device *device;
  1394. struct ib_uverbs_file *ufile;
  1395. struct ib_rdmacg_object cg_obj;
  1396. u64 enabled_caps;
  1397. /*
  1398. * Implementation details of the RDMA core, don't use in drivers:
  1399. */
  1400. struct rdma_restrack_entry res;
  1401. struct xarray mmap_xa;
  1402. };
  1403. struct ib_uobject {
  1404. u64 user_handle; /* handle given to us by userspace */
  1405. /* ufile & ucontext owning this object */
  1406. struct ib_uverbs_file *ufile;
  1407. /* FIXME, save memory: ufile->context == context */
  1408. struct ib_ucontext *context; /* associated user context */
  1409. void *object; /* containing object */
  1410. struct list_head list; /* link to context's list */
  1411. struct ib_rdmacg_object cg_obj; /* rdmacg object */
  1412. int id; /* index into kernel idr */
  1413. struct kref ref;
  1414. atomic_t usecnt; /* protects exclusive access */
  1415. struct rcu_head rcu; /* kfree_rcu() overhead */
  1416. const struct uverbs_api_object *uapi_object;
  1417. };
  1418. struct ib_udata {
  1419. const void __user *inbuf;
  1420. void __user *outbuf;
  1421. size_t inlen;
  1422. size_t outlen;
  1423. };
  1424. struct ib_pd {
  1425. u32 local_dma_lkey;
  1426. u32 flags;
  1427. struct ib_device *device;
  1428. struct ib_uobject *uobject;
  1429. atomic_t usecnt; /* count all resources */
  1430. u32 unsafe_global_rkey;
  1431. /*
  1432. * Implementation details of the RDMA core, don't use in drivers:
  1433. */
  1434. struct ib_mr *__internal_mr;
  1435. struct rdma_restrack_entry res;
  1436. };
  1437. struct ib_xrcd {
  1438. struct ib_device *device;
  1439. atomic_t usecnt; /* count all exposed resources */
  1440. struct inode *inode;
  1441. struct rw_semaphore tgt_qps_rwsem;
  1442. struct xarray tgt_qps;
  1443. };
  1444. struct ib_ah {
  1445. struct ib_device *device;
  1446. struct ib_pd *pd;
  1447. struct ib_uobject *uobject;
  1448. const struct ib_gid_attr *sgid_attr;
  1449. enum rdma_ah_attr_type type;
  1450. };
  1451. typedef void (*ib_comp_handler)(struct ib_cq *cq, void *cq_context);
  1452. enum ib_poll_context {
  1453. IB_POLL_SOFTIRQ, /* poll from softirq context */
  1454. IB_POLL_WORKQUEUE, /* poll from workqueue */
  1455. IB_POLL_UNBOUND_WORKQUEUE, /* poll from unbound workqueue */
  1456. IB_POLL_LAST_POOL_TYPE = IB_POLL_UNBOUND_WORKQUEUE,
  1457. IB_POLL_DIRECT, /* caller context, no hw completions */
  1458. };
  1459. struct ib_cq {
  1460. struct ib_device *device;
  1461. struct ib_ucq_object *uobject;
  1462. ib_comp_handler comp_handler;
  1463. void (*event_handler)(struct ib_event *, void *);
  1464. void *cq_context;
  1465. int cqe;
  1466. unsigned int cqe_used;
  1467. atomic_t usecnt; /* count number of work queues */
  1468. enum ib_poll_context poll_ctx;
  1469. struct ib_wc *wc;
  1470. struct list_head pool_entry;
  1471. union {
  1472. struct irq_poll iop;
  1473. struct work_struct work;
  1474. };
  1475. struct workqueue_struct *comp_wq;
  1476. struct dim *dim;
  1477. /* updated only by trace points */
  1478. ktime_t timestamp;
  1479. u8 interrupt:1;
  1480. u8 shared:1;
  1481. unsigned int comp_vector;
  1482. /*
  1483. * Implementation details of the RDMA core, don't use in drivers:
  1484. */
  1485. struct rdma_restrack_entry res;
  1486. };
  1487. struct ib_srq {
  1488. struct ib_device *device;
  1489. struct ib_pd *pd;
  1490. struct ib_usrq_object *uobject;
  1491. void (*event_handler)(struct ib_event *, void *);
  1492. void *srq_context;
  1493. enum ib_srq_type srq_type;
  1494. atomic_t usecnt;
  1495. struct {
  1496. struct ib_cq *cq;
  1497. union {
  1498. struct {
  1499. struct ib_xrcd *xrcd;
  1500. u32 srq_num;
  1501. } xrc;
  1502. };
  1503. } ext;
  1504. /*
  1505. * Implementation details of the RDMA core, don't use in drivers:
  1506. */
  1507. struct rdma_restrack_entry res;
  1508. };
  1509. enum ib_raw_packet_caps {
  1510. /*
  1511. * Strip cvlan from incoming packet and report it in the matching work
  1512. * completion is supported.
  1513. */
  1514. IB_RAW_PACKET_CAP_CVLAN_STRIPPING =
  1515. IB_UVERBS_RAW_PACKET_CAP_CVLAN_STRIPPING,
  1516. /*
  1517. * Scatter FCS field of an incoming packet to host memory is supported.
  1518. */
  1519. IB_RAW_PACKET_CAP_SCATTER_FCS = IB_UVERBS_RAW_PACKET_CAP_SCATTER_FCS,
  1520. /* Checksum offloads are supported (for both send and receive). */
  1521. IB_RAW_PACKET_CAP_IP_CSUM = IB_UVERBS_RAW_PACKET_CAP_IP_CSUM,
  1522. /*
  1523. * When a packet is received for an RQ with no receive WQEs, the
  1524. * packet processing is delayed.
  1525. */
  1526. IB_RAW_PACKET_CAP_DELAY_DROP = IB_UVERBS_RAW_PACKET_CAP_DELAY_DROP,
  1527. };
  1528. enum ib_wq_type {
  1529. IB_WQT_RQ = IB_UVERBS_WQT_RQ,
  1530. };
  1531. enum ib_wq_state {
  1532. IB_WQS_RESET,
  1533. IB_WQS_RDY,
  1534. IB_WQS_ERR
  1535. };
  1536. struct ib_wq {
  1537. struct ib_device *device;
  1538. struct ib_uwq_object *uobject;
  1539. void *wq_context;
  1540. void (*event_handler)(struct ib_event *, void *);
  1541. struct ib_pd *pd;
  1542. struct ib_cq *cq;
  1543. u32 wq_num;
  1544. enum ib_wq_state state;
  1545. enum ib_wq_type wq_type;
  1546. atomic_t usecnt;
  1547. };
  1548. enum ib_wq_flags {
  1549. IB_WQ_FLAGS_CVLAN_STRIPPING = IB_UVERBS_WQ_FLAGS_CVLAN_STRIPPING,
  1550. IB_WQ_FLAGS_SCATTER_FCS = IB_UVERBS_WQ_FLAGS_SCATTER_FCS,
  1551. IB_WQ_FLAGS_DELAY_DROP = IB_UVERBS_WQ_FLAGS_DELAY_DROP,
  1552. IB_WQ_FLAGS_PCI_WRITE_END_PADDING =
  1553. IB_UVERBS_WQ_FLAGS_PCI_WRITE_END_PADDING,
  1554. };
  1555. struct ib_wq_init_attr {
  1556. void *wq_context;
  1557. enum ib_wq_type wq_type;
  1558. u32 max_wr;
  1559. u32 max_sge;
  1560. struct ib_cq *cq;
  1561. void (*event_handler)(struct ib_event *, void *);
  1562. u32 create_flags; /* Use enum ib_wq_flags */
  1563. };
  1564. enum ib_wq_attr_mask {
  1565. IB_WQ_STATE = 1 << 0,
  1566. IB_WQ_CUR_STATE = 1 << 1,
  1567. IB_WQ_FLAGS = 1 << 2,
  1568. };
  1569. struct ib_wq_attr {
  1570. enum ib_wq_state wq_state;
  1571. enum ib_wq_state curr_wq_state;
  1572. u32 flags; /* Use enum ib_wq_flags */
  1573. u32 flags_mask; /* Use enum ib_wq_flags */
  1574. };
  1575. struct ib_rwq_ind_table {
  1576. struct ib_device *device;
  1577. struct ib_uobject *uobject;
  1578. atomic_t usecnt;
  1579. u32 ind_tbl_num;
  1580. u32 log_ind_tbl_size;
  1581. struct ib_wq **ind_tbl;
  1582. };
  1583. struct ib_rwq_ind_table_init_attr {
  1584. u32 log_ind_tbl_size;
  1585. /* Each entry is a pointer to Receive Work Queue */
  1586. struct ib_wq **ind_tbl;
  1587. };
  1588. enum port_pkey_state {
  1589. IB_PORT_PKEY_NOT_VALID = 0,
  1590. IB_PORT_PKEY_VALID = 1,
  1591. IB_PORT_PKEY_LISTED = 2,
  1592. };
  1593. struct ib_qp_security;
  1594. struct ib_port_pkey {
  1595. enum port_pkey_state state;
  1596. u16 pkey_index;
  1597. u32 port_num;
  1598. struct list_head qp_list;
  1599. struct list_head to_error_list;
  1600. struct ib_qp_security *sec;
  1601. };
  1602. struct ib_ports_pkeys {
  1603. struct ib_port_pkey main;
  1604. struct ib_port_pkey alt;
  1605. };
  1606. struct ib_qp_security {
  1607. struct ib_qp *qp;
  1608. struct ib_device *dev;
  1609. /* Hold this mutex when changing port and pkey settings. */
  1610. struct mutex mutex;
  1611. struct ib_ports_pkeys *ports_pkeys;
  1612. /* A list of all open shared QP handles. Required to enforce security
  1613. * properly for all users of a shared QP.
  1614. */
  1615. struct list_head shared_qp_list;
  1616. void *security;
  1617. bool destroying;
  1618. atomic_t error_list_count;
  1619. struct completion error_complete;
  1620. int error_comps_pending;
  1621. };
  1622. /*
  1623. * @max_write_sge: Maximum SGE elements per RDMA WRITE request.
  1624. * @max_read_sge: Maximum SGE elements per RDMA READ request.
  1625. */
  1626. struct ib_qp {
  1627. struct ib_device *device;
  1628. struct ib_pd *pd;
  1629. struct ib_cq *send_cq;
  1630. struct ib_cq *recv_cq;
  1631. spinlock_t mr_lock;
  1632. int mrs_used;
  1633. struct list_head rdma_mrs;
  1634. struct list_head sig_mrs;
  1635. struct ib_srq *srq;
  1636. struct completion srq_completion;
  1637. struct ib_xrcd *xrcd; /* XRC TGT QPs only */
  1638. struct list_head xrcd_list;
  1639. /* count times opened, mcast attaches, flow attaches */
  1640. atomic_t usecnt;
  1641. struct list_head open_list;
  1642. struct ib_qp *real_qp;
  1643. struct ib_uqp_object *uobject;
  1644. void (*event_handler)(struct ib_event *, void *);
  1645. void (*registered_event_handler)(struct ib_event *, void *);
  1646. void *qp_context;
  1647. /* sgid_attrs associated with the AV's */
  1648. const struct ib_gid_attr *av_sgid_attr;
  1649. const struct ib_gid_attr *alt_path_sgid_attr;
  1650. u32 qp_num;
  1651. u32 max_write_sge;
  1652. u32 max_read_sge;
  1653. enum ib_qp_type qp_type;
  1654. struct ib_rwq_ind_table *rwq_ind_tbl;
  1655. struct ib_qp_security *qp_sec;
  1656. u32 port;
  1657. bool integrity_en;
  1658. /*
  1659. * Implementation details of the RDMA core, don't use in drivers:
  1660. */
  1661. struct rdma_restrack_entry res;
  1662. /* The counter the qp is bind to */
  1663. struct rdma_counter *counter;
  1664. };
  1665. struct ib_dm {
  1666. struct ib_device *device;
  1667. u32 length;
  1668. u32 flags;
  1669. struct ib_uobject *uobject;
  1670. atomic_t usecnt;
  1671. };
  1672. /* bit values to mark existence of ib_dmah fields */
  1673. enum {
  1674. IB_DMAH_CPU_ID_EXISTS,
  1675. IB_DMAH_MEM_TYPE_EXISTS,
  1676. IB_DMAH_PH_EXISTS,
  1677. };
  1678. struct ib_dmah {
  1679. struct ib_device *device;
  1680. struct ib_uobject *uobject;
  1681. /*
  1682. * Implementation details of the RDMA core, don't use in drivers:
  1683. */
  1684. struct rdma_restrack_entry res;
  1685. u32 cpu_id;
  1686. enum tph_mem_type mem_type;
  1687. atomic_t usecnt;
  1688. u8 ph;
  1689. u8 valid_fields; /* use IB_DMAH_XXX_EXISTS */
  1690. };
  1691. struct ib_mr {
  1692. struct ib_device *device;
  1693. struct ib_pd *pd;
  1694. u32 lkey;
  1695. u32 rkey;
  1696. u64 iova;
  1697. u64 length;
  1698. unsigned int page_size;
  1699. enum ib_mr_type type;
  1700. bool need_inval;
  1701. union {
  1702. struct ib_uobject *uobject; /* user */
  1703. struct list_head qp_entry; /* FR */
  1704. };
  1705. struct ib_dm *dm;
  1706. struct ib_sig_attrs *sig_attrs; /* only for IB_MR_TYPE_INTEGRITY MRs */
  1707. struct ib_dmah *dmah;
  1708. /*
  1709. * Implementation details of the RDMA core, don't use in drivers:
  1710. */
  1711. struct rdma_restrack_entry res;
  1712. };
  1713. struct ib_mw {
  1714. struct ib_device *device;
  1715. struct ib_pd *pd;
  1716. struct ib_uobject *uobject;
  1717. u32 rkey;
  1718. enum ib_mw_type type;
  1719. };
  1720. /* Supported steering options */
  1721. enum ib_flow_attr_type {
  1722. /* steering according to rule specifications */
  1723. IB_FLOW_ATTR_NORMAL = 0x0,
  1724. /* default unicast and multicast rule -
  1725. * receive all Eth traffic which isn't steered to any QP
  1726. */
  1727. IB_FLOW_ATTR_ALL_DEFAULT = 0x1,
  1728. /* default multicast rule -
  1729. * receive all Eth multicast traffic which isn't steered to any QP
  1730. */
  1731. IB_FLOW_ATTR_MC_DEFAULT = 0x2,
  1732. /* sniffer rule - receive all port traffic */
  1733. IB_FLOW_ATTR_SNIFFER = 0x3
  1734. };
  1735. /* Supported steering header types */
  1736. enum ib_flow_spec_type {
  1737. /* L2 headers*/
  1738. IB_FLOW_SPEC_ETH = 0x20,
  1739. IB_FLOW_SPEC_IB = 0x22,
  1740. /* L3 header*/
  1741. IB_FLOW_SPEC_IPV4 = 0x30,
  1742. IB_FLOW_SPEC_IPV6 = 0x31,
  1743. IB_FLOW_SPEC_ESP = 0x34,
  1744. /* L4 headers*/
  1745. IB_FLOW_SPEC_TCP = 0x40,
  1746. IB_FLOW_SPEC_UDP = 0x41,
  1747. IB_FLOW_SPEC_VXLAN_TUNNEL = 0x50,
  1748. IB_FLOW_SPEC_GRE = 0x51,
  1749. IB_FLOW_SPEC_MPLS = 0x60,
  1750. IB_FLOW_SPEC_INNER = 0x100,
  1751. /* Actions */
  1752. IB_FLOW_SPEC_ACTION_TAG = 0x1000,
  1753. IB_FLOW_SPEC_ACTION_DROP = 0x1001,
  1754. IB_FLOW_SPEC_ACTION_HANDLE = 0x1002,
  1755. IB_FLOW_SPEC_ACTION_COUNT = 0x1003,
  1756. };
  1757. #define IB_FLOW_SPEC_LAYER_MASK 0xF0
  1758. #define IB_FLOW_SPEC_SUPPORT_LAYERS 10
  1759. enum ib_flow_flags {
  1760. IB_FLOW_ATTR_FLAGS_DONT_TRAP = 1UL << 1, /* Continue match, no steal */
  1761. IB_FLOW_ATTR_FLAGS_EGRESS = 1UL << 2, /* Egress flow */
  1762. IB_FLOW_ATTR_FLAGS_RESERVED = 1UL << 3 /* Must be last */
  1763. };
  1764. struct ib_flow_eth_filter {
  1765. u8 dst_mac[6];
  1766. u8 src_mac[6];
  1767. __be16 ether_type;
  1768. __be16 vlan_tag;
  1769. };
  1770. struct ib_flow_spec_eth {
  1771. u32 type;
  1772. u16 size;
  1773. struct ib_flow_eth_filter val;
  1774. struct ib_flow_eth_filter mask;
  1775. };
  1776. struct ib_flow_ib_filter {
  1777. __be16 dlid;
  1778. __u8 sl;
  1779. };
  1780. struct ib_flow_spec_ib {
  1781. u32 type;
  1782. u16 size;
  1783. struct ib_flow_ib_filter val;
  1784. struct ib_flow_ib_filter mask;
  1785. };
  1786. /* IPv4 header flags */
  1787. enum ib_ipv4_flags {
  1788. IB_IPV4_DONT_FRAG = 0x2, /* Don't enable packet fragmentation */
  1789. IB_IPV4_MORE_FRAG = 0X4 /* For All fragmented packets except the
  1790. last have this flag set */
  1791. };
  1792. struct ib_flow_ipv4_filter {
  1793. __be32 src_ip;
  1794. __be32 dst_ip;
  1795. u8 proto;
  1796. u8 tos;
  1797. u8 ttl;
  1798. u8 flags;
  1799. };
  1800. struct ib_flow_spec_ipv4 {
  1801. u32 type;
  1802. u16 size;
  1803. struct ib_flow_ipv4_filter val;
  1804. struct ib_flow_ipv4_filter mask;
  1805. };
  1806. struct ib_flow_ipv6_filter {
  1807. u8 src_ip[16];
  1808. u8 dst_ip[16];
  1809. __be32 flow_label;
  1810. u8 next_hdr;
  1811. u8 traffic_class;
  1812. u8 hop_limit;
  1813. } __packed;
  1814. struct ib_flow_spec_ipv6 {
  1815. u32 type;
  1816. u16 size;
  1817. struct ib_flow_ipv6_filter val;
  1818. struct ib_flow_ipv6_filter mask;
  1819. };
  1820. struct ib_flow_tcp_udp_filter {
  1821. __be16 dst_port;
  1822. __be16 src_port;
  1823. };
  1824. struct ib_flow_spec_tcp_udp {
  1825. u32 type;
  1826. u16 size;
  1827. struct ib_flow_tcp_udp_filter val;
  1828. struct ib_flow_tcp_udp_filter mask;
  1829. };
  1830. struct ib_flow_tunnel_filter {
  1831. __be32 tunnel_id;
  1832. };
  1833. /* ib_flow_spec_tunnel describes the Vxlan tunnel
  1834. * the tunnel_id from val has the vni value
  1835. */
  1836. struct ib_flow_spec_tunnel {
  1837. u32 type;
  1838. u16 size;
  1839. struct ib_flow_tunnel_filter val;
  1840. struct ib_flow_tunnel_filter mask;
  1841. };
  1842. struct ib_flow_esp_filter {
  1843. __be32 spi;
  1844. __be32 seq;
  1845. };
  1846. struct ib_flow_spec_esp {
  1847. u32 type;
  1848. u16 size;
  1849. struct ib_flow_esp_filter val;
  1850. struct ib_flow_esp_filter mask;
  1851. };
  1852. struct ib_flow_gre_filter {
  1853. __be16 c_ks_res0_ver;
  1854. __be16 protocol;
  1855. __be32 key;
  1856. };
  1857. struct ib_flow_spec_gre {
  1858. u32 type;
  1859. u16 size;
  1860. struct ib_flow_gre_filter val;
  1861. struct ib_flow_gre_filter mask;
  1862. };
  1863. struct ib_flow_mpls_filter {
  1864. __be32 tag;
  1865. };
  1866. struct ib_flow_spec_mpls {
  1867. u32 type;
  1868. u16 size;
  1869. struct ib_flow_mpls_filter val;
  1870. struct ib_flow_mpls_filter mask;
  1871. };
  1872. struct ib_flow_spec_action_tag {
  1873. enum ib_flow_spec_type type;
  1874. u16 size;
  1875. u32 tag_id;
  1876. };
  1877. struct ib_flow_spec_action_drop {
  1878. enum ib_flow_spec_type type;
  1879. u16 size;
  1880. };
  1881. struct ib_flow_spec_action_handle {
  1882. enum ib_flow_spec_type type;
  1883. u16 size;
  1884. struct ib_flow_action *act;
  1885. };
  1886. enum ib_counters_description {
  1887. IB_COUNTER_PACKETS,
  1888. IB_COUNTER_BYTES,
  1889. };
  1890. struct ib_flow_spec_action_count {
  1891. enum ib_flow_spec_type type;
  1892. u16 size;
  1893. struct ib_counters *counters;
  1894. };
  1895. union ib_flow_spec {
  1896. struct {
  1897. u32 type;
  1898. u16 size;
  1899. };
  1900. struct ib_flow_spec_eth eth;
  1901. struct ib_flow_spec_ib ib;
  1902. struct ib_flow_spec_ipv4 ipv4;
  1903. struct ib_flow_spec_tcp_udp tcp_udp;
  1904. struct ib_flow_spec_ipv6 ipv6;
  1905. struct ib_flow_spec_tunnel tunnel;
  1906. struct ib_flow_spec_esp esp;
  1907. struct ib_flow_spec_gre gre;
  1908. struct ib_flow_spec_mpls mpls;
  1909. struct ib_flow_spec_action_tag flow_tag;
  1910. struct ib_flow_spec_action_drop drop;
  1911. struct ib_flow_spec_action_handle action;
  1912. struct ib_flow_spec_action_count flow_count;
  1913. };
  1914. struct ib_flow_attr {
  1915. enum ib_flow_attr_type type;
  1916. u16 size;
  1917. u16 priority;
  1918. u32 flags;
  1919. u8 num_of_specs;
  1920. u32 port;
  1921. union ib_flow_spec flows[];
  1922. };
  1923. struct ib_flow {
  1924. struct ib_qp *qp;
  1925. struct ib_device *device;
  1926. struct ib_uobject *uobject;
  1927. };
  1928. enum ib_flow_action_type {
  1929. IB_FLOW_ACTION_UNSPECIFIED,
  1930. IB_FLOW_ACTION_ESP = 1,
  1931. };
  1932. struct ib_flow_action_attrs_esp_keymats {
  1933. enum ib_uverbs_flow_action_esp_keymat protocol;
  1934. union {
  1935. struct ib_uverbs_flow_action_esp_keymat_aes_gcm aes_gcm;
  1936. } keymat;
  1937. };
  1938. struct ib_flow_action_attrs_esp_replays {
  1939. enum ib_uverbs_flow_action_esp_replay protocol;
  1940. union {
  1941. struct ib_uverbs_flow_action_esp_replay_bmp bmp;
  1942. } replay;
  1943. };
  1944. enum ib_flow_action_attrs_esp_flags {
  1945. /* All user-space flags at the top: Use enum ib_uverbs_flow_action_esp_flags
  1946. * This is done in order to share the same flags between user-space and
  1947. * kernel and spare an unnecessary translation.
  1948. */
  1949. /* Kernel flags */
  1950. IB_FLOW_ACTION_ESP_FLAGS_ESN_TRIGGERED = 1ULL << 32,
  1951. IB_FLOW_ACTION_ESP_FLAGS_MOD_ESP_ATTRS = 1ULL << 33,
  1952. };
  1953. struct ib_flow_spec_list {
  1954. struct ib_flow_spec_list *next;
  1955. union ib_flow_spec spec;
  1956. };
  1957. struct ib_flow_action_attrs_esp {
  1958. struct ib_flow_action_attrs_esp_keymats *keymat;
  1959. struct ib_flow_action_attrs_esp_replays *replay;
  1960. struct ib_flow_spec_list *encap;
  1961. /* Used only if IB_FLOW_ACTION_ESP_FLAGS_ESN_TRIGGERED is enabled.
  1962. * Value of 0 is a valid value.
  1963. */
  1964. u32 esn;
  1965. u32 spi;
  1966. u32 seq;
  1967. u32 tfc_pad;
  1968. /* Use enum ib_flow_action_attrs_esp_flags */
  1969. u64 flags;
  1970. u64 hard_limit_pkts;
  1971. };
  1972. struct ib_flow_action {
  1973. struct ib_device *device;
  1974. struct ib_uobject *uobject;
  1975. enum ib_flow_action_type type;
  1976. atomic_t usecnt;
  1977. };
  1978. struct ib_mad;
  1979. enum ib_process_mad_flags {
  1980. IB_MAD_IGNORE_MKEY = 1,
  1981. IB_MAD_IGNORE_BKEY = 2,
  1982. IB_MAD_IGNORE_ALL = IB_MAD_IGNORE_MKEY | IB_MAD_IGNORE_BKEY
  1983. };
  1984. enum ib_mad_result {
  1985. IB_MAD_RESULT_FAILURE = 0, /* (!SUCCESS is the important flag) */
  1986. IB_MAD_RESULT_SUCCESS = 1 << 0, /* MAD was successfully processed */
  1987. IB_MAD_RESULT_REPLY = 1 << 1, /* Reply packet needs to be sent */
  1988. IB_MAD_RESULT_CONSUMED = 1 << 2 /* Packet consumed: stop processing */
  1989. };
  1990. struct ib_port_cache {
  1991. u64 subnet_prefix;
  1992. struct ib_pkey_cache *pkey;
  1993. struct ib_gid_table *gid;
  1994. u8 lmc;
  1995. enum ib_port_state port_state;
  1996. enum ib_port_state last_port_state;
  1997. };
  1998. struct ib_port_immutable {
  1999. int pkey_tbl_len;
  2000. int gid_tbl_len;
  2001. u32 core_cap_flags;
  2002. u32 max_mad_size;
  2003. };
  2004. struct ib_port_data {
  2005. struct ib_device *ib_dev;
  2006. struct ib_port_immutable immutable;
  2007. spinlock_t pkey_list_lock;
  2008. spinlock_t netdev_lock;
  2009. struct list_head pkey_list;
  2010. struct ib_port_cache cache;
  2011. struct net_device __rcu *netdev;
  2012. netdevice_tracker netdev_tracker;
  2013. struct hlist_node ndev_hash_link;
  2014. struct rdma_port_counter port_counter;
  2015. struct ib_port *sysfs;
  2016. };
  2017. /* rdma netdev type - specifies protocol type */
  2018. enum rdma_netdev_t {
  2019. RDMA_NETDEV_OPA_VNIC,
  2020. RDMA_NETDEV_IPOIB,
  2021. };
  2022. /**
  2023. * struct rdma_netdev - rdma netdev
  2024. * For cases where netstack interfacing is required.
  2025. */
  2026. struct rdma_netdev {
  2027. void *clnt_priv;
  2028. struct ib_device *hca;
  2029. u32 port_num;
  2030. int mtu;
  2031. /*
  2032. * cleanup function must be specified.
  2033. * FIXME: This is only used for OPA_VNIC and that usage should be
  2034. * removed too.
  2035. */
  2036. void (*free_rdma_netdev)(struct net_device *netdev);
  2037. /* control functions */
  2038. void (*set_id)(struct net_device *netdev, int id);
  2039. /* send packet */
  2040. int (*send)(struct net_device *dev, struct sk_buff *skb,
  2041. struct ib_ah *address, u32 dqpn);
  2042. /* multicast */
  2043. int (*attach_mcast)(struct net_device *dev, struct ib_device *hca,
  2044. union ib_gid *gid, u16 mlid,
  2045. int set_qkey, u32 qkey);
  2046. int (*detach_mcast)(struct net_device *dev, struct ib_device *hca,
  2047. union ib_gid *gid, u16 mlid);
  2048. /* timeout */
  2049. void (*tx_timeout)(struct net_device *dev, unsigned int txqueue);
  2050. };
  2051. struct rdma_netdev_alloc_params {
  2052. size_t sizeof_priv;
  2053. unsigned int txqs;
  2054. unsigned int rxqs;
  2055. void *param;
  2056. int (*initialize_rdma_netdev)(struct ib_device *device, u32 port_num,
  2057. struct net_device *netdev, void *param);
  2058. };
  2059. struct ib_odp_counters {
  2060. atomic64_t faults;
  2061. atomic64_t faults_handled;
  2062. atomic64_t invalidations;
  2063. atomic64_t invalidations_handled;
  2064. atomic64_t prefetch;
  2065. };
  2066. struct ib_counters {
  2067. struct ib_device *device;
  2068. struct ib_uobject *uobject;
  2069. /* num of objects attached */
  2070. atomic_t usecnt;
  2071. };
  2072. struct ib_counters_read_attr {
  2073. u64 *counters_buff;
  2074. u32 ncounters;
  2075. u32 flags; /* use enum ib_read_counters_flags */
  2076. };
  2077. struct uverbs_attr_bundle;
  2078. struct iw_cm_id;
  2079. struct iw_cm_conn_param;
  2080. #define INIT_RDMA_OBJ_SIZE(ib_struct, drv_struct, member) \
  2081. .size_##ib_struct = \
  2082. (sizeof(struct drv_struct) + \
  2083. BUILD_BUG_ON_ZERO(offsetof(struct drv_struct, member)) + \
  2084. BUILD_BUG_ON_ZERO( \
  2085. !__same_type(((struct drv_struct *)NULL)->member, \
  2086. struct ib_struct)))
  2087. #define rdma_zalloc_drv_obj_gfp(ib_dev, ib_type, gfp) \
  2088. ((struct ib_type *)rdma_zalloc_obj(ib_dev, ib_dev->ops.size_##ib_type, \
  2089. gfp, false))
  2090. #define rdma_zalloc_drv_obj_numa(ib_dev, ib_type) \
  2091. ((struct ib_type *)rdma_zalloc_obj(ib_dev, ib_dev->ops.size_##ib_type, \
  2092. GFP_KERNEL, true))
  2093. #define rdma_zalloc_drv_obj(ib_dev, ib_type) \
  2094. rdma_zalloc_drv_obj_gfp(ib_dev, ib_type, GFP_KERNEL)
  2095. #define DECLARE_RDMA_OBJ_SIZE(ib_struct) size_t size_##ib_struct
  2096. struct rdma_user_mmap_entry {
  2097. struct kref ref;
  2098. struct ib_ucontext *ucontext;
  2099. unsigned long start_pgoff;
  2100. size_t npages;
  2101. bool driver_removed;
  2102. /* protects access to dmabufs */
  2103. struct mutex dmabufs_lock;
  2104. struct list_head dmabufs;
  2105. };
  2106. /* Return the offset (in bytes) the user should pass to libc's mmap() */
  2107. static inline u64
  2108. rdma_user_mmap_get_offset(const struct rdma_user_mmap_entry *entry)
  2109. {
  2110. return (u64)entry->start_pgoff << PAGE_SHIFT;
  2111. }
  2112. /**
  2113. * struct ib_device_ops - InfiniBand device operations
  2114. * This structure defines all the InfiniBand device operations, providers will
  2115. * need to define the supported operations, otherwise they will be set to null.
  2116. */
  2117. struct ib_device_ops {
  2118. struct module *owner;
  2119. enum rdma_driver_id driver_id;
  2120. u32 uverbs_abi_ver;
  2121. unsigned int uverbs_no_driver_id_binding:1;
  2122. /*
  2123. * NOTE: New drivers should not make use of device_group; instead new
  2124. * device parameter should be exposed via netlink command. This
  2125. * mechanism exists only for existing drivers.
  2126. */
  2127. const struct attribute_group *device_group;
  2128. const struct attribute_group **port_groups;
  2129. int (*post_send)(struct ib_qp *qp, const struct ib_send_wr *send_wr,
  2130. const struct ib_send_wr **bad_send_wr);
  2131. int (*post_recv)(struct ib_qp *qp, const struct ib_recv_wr *recv_wr,
  2132. const struct ib_recv_wr **bad_recv_wr);
  2133. void (*drain_rq)(struct ib_qp *qp);
  2134. void (*drain_sq)(struct ib_qp *qp);
  2135. int (*poll_cq)(struct ib_cq *cq, int num_entries, struct ib_wc *wc);
  2136. int (*peek_cq)(struct ib_cq *cq, int wc_cnt);
  2137. int (*req_notify_cq)(struct ib_cq *cq, enum ib_cq_notify_flags flags);
  2138. int (*post_srq_recv)(struct ib_srq *srq,
  2139. const struct ib_recv_wr *recv_wr,
  2140. const struct ib_recv_wr **bad_recv_wr);
  2141. int (*process_mad)(struct ib_device *device, int process_mad_flags,
  2142. u32 port_num, const struct ib_wc *in_wc,
  2143. const struct ib_grh *in_grh,
  2144. const struct ib_mad *in_mad, struct ib_mad *out_mad,
  2145. size_t *out_mad_size, u16 *out_mad_pkey_index);
  2146. int (*query_device)(struct ib_device *device,
  2147. struct ib_device_attr *device_attr,
  2148. struct ib_udata *udata);
  2149. int (*modify_device)(struct ib_device *device, int device_modify_mask,
  2150. struct ib_device_modify *device_modify);
  2151. void (*get_dev_fw_str)(struct ib_device *device, char *str);
  2152. const struct cpumask *(*get_vector_affinity)(struct ib_device *ibdev,
  2153. int comp_vector);
  2154. int (*query_port)(struct ib_device *device, u32 port_num,
  2155. struct ib_port_attr *port_attr);
  2156. int (*query_port_speed)(struct ib_device *device, u32 port_num,
  2157. u64 *speed);
  2158. int (*modify_port)(struct ib_device *device, u32 port_num,
  2159. int port_modify_mask,
  2160. struct ib_port_modify *port_modify);
  2161. /*
  2162. * The following mandatory functions are used only at device
  2163. * registration. Keep functions such as these at the end of this
  2164. * structure to avoid cache line misses when accessing struct ib_device
  2165. * in fast paths.
  2166. */
  2167. int (*get_port_immutable)(struct ib_device *device, u32 port_num,
  2168. struct ib_port_immutable *immutable);
  2169. enum rdma_link_layer (*get_link_layer)(struct ib_device *device,
  2170. u32 port_num);
  2171. /*
  2172. * When calling get_netdev, the HW vendor's driver should return the
  2173. * net device of device @device at port @port_num or NULL if such
  2174. * a net device doesn't exist. The vendor driver should call dev_hold
  2175. * on this net device. The HW vendor's device driver must guarantee
  2176. * that this function returns NULL before the net device has finished
  2177. * NETDEV_UNREGISTER state.
  2178. */
  2179. struct net_device *(*get_netdev)(struct ib_device *device,
  2180. u32 port_num);
  2181. /*
  2182. * rdma netdev operation
  2183. *
  2184. * Driver implementing alloc_rdma_netdev or rdma_netdev_get_params
  2185. * must return -EOPNOTSUPP if it doesn't support the specified type.
  2186. */
  2187. struct net_device *(*alloc_rdma_netdev)(
  2188. struct ib_device *device, u32 port_num, enum rdma_netdev_t type,
  2189. const char *name, unsigned char name_assign_type,
  2190. void (*setup)(struct net_device *));
  2191. int (*rdma_netdev_get_params)(struct ib_device *device, u32 port_num,
  2192. enum rdma_netdev_t type,
  2193. struct rdma_netdev_alloc_params *params);
  2194. /*
  2195. * query_gid should be return GID value for @device, when @port_num
  2196. * link layer is either IB or iWarp. It is no-op if @port_num port
  2197. * is RoCE link layer.
  2198. */
  2199. int (*query_gid)(struct ib_device *device, u32 port_num, int index,
  2200. union ib_gid *gid);
  2201. /*
  2202. * When calling add_gid, the HW vendor's driver should add the gid
  2203. * of device of port at gid index available at @attr. Meta-info of
  2204. * that gid (for example, the network device related to this gid) is
  2205. * available at @attr. @context allows the HW vendor driver to store
  2206. * extra information together with a GID entry. The HW vendor driver may
  2207. * allocate memory to contain this information and store it in @context
  2208. * when a new GID entry is written to. Params are consistent until the
  2209. * next call of add_gid or delete_gid. The function should return 0 on
  2210. * success or error otherwise. The function could be called
  2211. * concurrently for different ports. This function is only called when
  2212. * roce_gid_table is used.
  2213. */
  2214. int (*add_gid)(const struct ib_gid_attr *attr, void **context);
  2215. /*
  2216. * When calling del_gid, the HW vendor's driver should delete the
  2217. * gid of device @device at gid index gid_index of port port_num
  2218. * available in @attr.
  2219. * Upon the deletion of a GID entry, the HW vendor must free any
  2220. * allocated memory. The caller will clear @context afterwards.
  2221. * This function is only called when roce_gid_table is used.
  2222. */
  2223. int (*del_gid)(const struct ib_gid_attr *attr, void **context);
  2224. int (*query_pkey)(struct ib_device *device, u32 port_num, u16 index,
  2225. u16 *pkey);
  2226. int (*alloc_ucontext)(struct ib_ucontext *context,
  2227. struct ib_udata *udata);
  2228. void (*dealloc_ucontext)(struct ib_ucontext *context);
  2229. int (*mmap)(struct ib_ucontext *context, struct vm_area_struct *vma);
  2230. /*
  2231. * This will be called once refcount of an entry in mmap_xa reaches
  2232. * zero. The type of the memory that was mapped may differ between
  2233. * entries and is opaque to the rdma_user_mmap interface.
  2234. * Therefore needs to be implemented by the driver in mmap_free.
  2235. */
  2236. void (*mmap_free)(struct rdma_user_mmap_entry *entry);
  2237. int (*mmap_get_pfns)(struct rdma_user_mmap_entry *entry,
  2238. struct phys_vec *phys_vec,
  2239. struct p2pdma_provider **provider);
  2240. struct rdma_user_mmap_entry *(*pgoff_to_mmap_entry)(struct ib_ucontext *ucontext,
  2241. off_t pg_off);
  2242. void (*disassociate_ucontext)(struct ib_ucontext *ibcontext);
  2243. int (*alloc_pd)(struct ib_pd *pd, struct ib_udata *udata);
  2244. int (*dealloc_pd)(struct ib_pd *pd, struct ib_udata *udata);
  2245. int (*create_ah)(struct ib_ah *ah, struct rdma_ah_init_attr *attr,
  2246. struct ib_udata *udata);
  2247. int (*create_user_ah)(struct ib_ah *ah, struct rdma_ah_init_attr *attr,
  2248. struct ib_udata *udata);
  2249. int (*modify_ah)(struct ib_ah *ah, struct rdma_ah_attr *ah_attr);
  2250. int (*query_ah)(struct ib_ah *ah, struct rdma_ah_attr *ah_attr);
  2251. int (*destroy_ah)(struct ib_ah *ah, u32 flags);
  2252. int (*create_srq)(struct ib_srq *srq,
  2253. struct ib_srq_init_attr *srq_init_attr,
  2254. struct ib_udata *udata);
  2255. int (*modify_srq)(struct ib_srq *srq, struct ib_srq_attr *srq_attr,
  2256. enum ib_srq_attr_mask srq_attr_mask,
  2257. struct ib_udata *udata);
  2258. int (*query_srq)(struct ib_srq *srq, struct ib_srq_attr *srq_attr);
  2259. int (*destroy_srq)(struct ib_srq *srq, struct ib_udata *udata);
  2260. int (*create_qp)(struct ib_qp *qp, struct ib_qp_init_attr *qp_init_attr,
  2261. struct ib_udata *udata);
  2262. int (*modify_qp)(struct ib_qp *qp, struct ib_qp_attr *qp_attr,
  2263. int qp_attr_mask, struct ib_udata *udata);
  2264. int (*query_qp)(struct ib_qp *qp, struct ib_qp_attr *qp_attr,
  2265. int qp_attr_mask, struct ib_qp_init_attr *qp_init_attr);
  2266. int (*destroy_qp)(struct ib_qp *qp, struct ib_udata *udata);
  2267. int (*create_cq)(struct ib_cq *cq, const struct ib_cq_init_attr *attr,
  2268. struct uverbs_attr_bundle *attrs);
  2269. int (*create_cq_umem)(struct ib_cq *cq,
  2270. const struct ib_cq_init_attr *attr,
  2271. struct ib_umem *umem,
  2272. struct uverbs_attr_bundle *attrs);
  2273. int (*modify_cq)(struct ib_cq *cq, u16 cq_count, u16 cq_period);
  2274. int (*destroy_cq)(struct ib_cq *cq, struct ib_udata *udata);
  2275. int (*resize_cq)(struct ib_cq *cq, int cqe, struct ib_udata *udata);
  2276. /*
  2277. * pre_destroy_cq - Prevent a cq from generating any new work
  2278. * completions, but not free any kernel resources
  2279. */
  2280. int (*pre_destroy_cq)(struct ib_cq *cq);
  2281. /*
  2282. * post_destroy_cq - Free all kernel resources
  2283. */
  2284. void (*post_destroy_cq)(struct ib_cq *cq);
  2285. struct ib_mr *(*get_dma_mr)(struct ib_pd *pd, int mr_access_flags);
  2286. struct ib_mr *(*reg_user_mr)(struct ib_pd *pd, u64 start, u64 length,
  2287. u64 virt_addr, int mr_access_flags,
  2288. struct ib_dmah *dmah,
  2289. struct ib_udata *udata);
  2290. struct ib_mr *(*reg_user_mr_dmabuf)(struct ib_pd *pd, u64 offset,
  2291. u64 length, u64 virt_addr, int fd,
  2292. int mr_access_flags,
  2293. struct ib_dmah *dmah,
  2294. struct uverbs_attr_bundle *attrs);
  2295. struct ib_mr *(*rereg_user_mr)(struct ib_mr *mr, int flags, u64 start,
  2296. u64 length, u64 virt_addr,
  2297. int mr_access_flags, struct ib_pd *pd,
  2298. struct ib_udata *udata);
  2299. int (*dereg_mr)(struct ib_mr *mr, struct ib_udata *udata);
  2300. struct ib_mr *(*alloc_mr)(struct ib_pd *pd, enum ib_mr_type mr_type,
  2301. u32 max_num_sg);
  2302. struct ib_mr *(*alloc_mr_integrity)(struct ib_pd *pd,
  2303. u32 max_num_data_sg,
  2304. u32 max_num_meta_sg);
  2305. int (*advise_mr)(struct ib_pd *pd,
  2306. enum ib_uverbs_advise_mr_advice advice, u32 flags,
  2307. struct ib_sge *sg_list, u32 num_sge,
  2308. struct uverbs_attr_bundle *attrs);
  2309. /*
  2310. * Kernel users should universally support relaxed ordering (RO), as
  2311. * they are designed to read data only after observing the CQE and use
  2312. * the DMA API correctly.
  2313. *
  2314. * Some drivers implicitly enable RO if platform supports it.
  2315. */
  2316. int (*map_mr_sg)(struct ib_mr *mr, struct scatterlist *sg, int sg_nents,
  2317. unsigned int *sg_offset);
  2318. int (*check_mr_status)(struct ib_mr *mr, u32 check_mask,
  2319. struct ib_mr_status *mr_status);
  2320. int (*alloc_mw)(struct ib_mw *mw, struct ib_udata *udata);
  2321. int (*dealloc_mw)(struct ib_mw *mw);
  2322. int (*attach_mcast)(struct ib_qp *qp, union ib_gid *gid, u16 lid);
  2323. int (*detach_mcast)(struct ib_qp *qp, union ib_gid *gid, u16 lid);
  2324. int (*alloc_xrcd)(struct ib_xrcd *xrcd, struct ib_udata *udata);
  2325. int (*dealloc_xrcd)(struct ib_xrcd *xrcd, struct ib_udata *udata);
  2326. struct ib_flow *(*create_flow)(struct ib_qp *qp,
  2327. struct ib_flow_attr *flow_attr,
  2328. struct ib_udata *udata);
  2329. int (*destroy_flow)(struct ib_flow *flow_id);
  2330. int (*destroy_flow_action)(struct ib_flow_action *action);
  2331. int (*set_vf_link_state)(struct ib_device *device, int vf, u32 port,
  2332. int state);
  2333. int (*get_vf_config)(struct ib_device *device, int vf, u32 port,
  2334. struct ifla_vf_info *ivf);
  2335. int (*get_vf_stats)(struct ib_device *device, int vf, u32 port,
  2336. struct ifla_vf_stats *stats);
  2337. int (*get_vf_guid)(struct ib_device *device, int vf, u32 port,
  2338. struct ifla_vf_guid *node_guid,
  2339. struct ifla_vf_guid *port_guid);
  2340. int (*set_vf_guid)(struct ib_device *device, int vf, u32 port, u64 guid,
  2341. int type);
  2342. struct ib_wq *(*create_wq)(struct ib_pd *pd,
  2343. struct ib_wq_init_attr *init_attr,
  2344. struct ib_udata *udata);
  2345. int (*destroy_wq)(struct ib_wq *wq, struct ib_udata *udata);
  2346. int (*modify_wq)(struct ib_wq *wq, struct ib_wq_attr *attr,
  2347. u32 wq_attr_mask, struct ib_udata *udata);
  2348. int (*create_rwq_ind_table)(struct ib_rwq_ind_table *ib_rwq_ind_table,
  2349. struct ib_rwq_ind_table_init_attr *init_attr,
  2350. struct ib_udata *udata);
  2351. int (*destroy_rwq_ind_table)(struct ib_rwq_ind_table *wq_ind_table);
  2352. struct ib_dm *(*alloc_dm)(struct ib_device *device,
  2353. struct ib_ucontext *context,
  2354. struct ib_dm_alloc_attr *attr,
  2355. struct uverbs_attr_bundle *attrs);
  2356. int (*dealloc_dm)(struct ib_dm *dm, struct uverbs_attr_bundle *attrs);
  2357. int (*alloc_dmah)(struct ib_dmah *ibdmah,
  2358. struct uverbs_attr_bundle *attrs);
  2359. int (*dealloc_dmah)(struct ib_dmah *dmah, struct uverbs_attr_bundle *attrs);
  2360. struct ib_mr *(*reg_dm_mr)(struct ib_pd *pd, struct ib_dm *dm,
  2361. struct ib_dm_mr_attr *attr,
  2362. struct uverbs_attr_bundle *attrs);
  2363. int (*create_counters)(struct ib_counters *counters,
  2364. struct uverbs_attr_bundle *attrs);
  2365. int (*destroy_counters)(struct ib_counters *counters);
  2366. int (*read_counters)(struct ib_counters *counters,
  2367. struct ib_counters_read_attr *counters_read_attr,
  2368. struct uverbs_attr_bundle *attrs);
  2369. int (*map_mr_sg_pi)(struct ib_mr *mr, struct scatterlist *data_sg,
  2370. int data_sg_nents, unsigned int *data_sg_offset,
  2371. struct scatterlist *meta_sg, int meta_sg_nents,
  2372. unsigned int *meta_sg_offset);
  2373. /*
  2374. * alloc_hw_[device,port]_stats - Allocate a struct rdma_hw_stats and
  2375. * fill in the driver initialized data. The struct is kfree()'ed by
  2376. * the sysfs core when the device is removed. A lifespan of -1 in the
  2377. * return struct tells the core to set a default lifespan.
  2378. */
  2379. struct rdma_hw_stats *(*alloc_hw_device_stats)(struct ib_device *device);
  2380. struct rdma_hw_stats *(*alloc_hw_port_stats)(struct ib_device *device,
  2381. u32 port_num);
  2382. /*
  2383. * get_hw_stats - Fill in the counter value(s) in the stats struct.
  2384. * @index - The index in the value array we wish to have updated, or
  2385. * num_counters if we want all stats updated
  2386. * Return codes -
  2387. * < 0 - Error, no counters updated
  2388. * index - Updated the single counter pointed to by index
  2389. * num_counters - Updated all counters (will reset the timestamp
  2390. * and prevent further calls for lifespan milliseconds)
  2391. * Drivers are allowed to update all counters in leiu of just the
  2392. * one given in index at their option
  2393. */
  2394. int (*get_hw_stats)(struct ib_device *device,
  2395. struct rdma_hw_stats *stats, u32 port, int index);
  2396. /*
  2397. * modify_hw_stat - Modify the counter configuration
  2398. * @enable: true/false when enable/disable a counter
  2399. * Return codes - 0 on success or error code otherwise.
  2400. */
  2401. int (*modify_hw_stat)(struct ib_device *device, u32 port,
  2402. unsigned int counter_index, bool enable);
  2403. /*
  2404. * Allows rdma drivers to add their own restrack attributes.
  2405. */
  2406. int (*fill_res_mr_entry)(struct sk_buff *msg, struct ib_mr *ibmr);
  2407. int (*fill_res_mr_entry_raw)(struct sk_buff *msg, struct ib_mr *ibmr);
  2408. int (*fill_res_cq_entry)(struct sk_buff *msg, struct ib_cq *ibcq);
  2409. int (*fill_res_cq_entry_raw)(struct sk_buff *msg, struct ib_cq *ibcq);
  2410. int (*fill_res_qp_entry)(struct sk_buff *msg, struct ib_qp *ibqp);
  2411. int (*fill_res_qp_entry_raw)(struct sk_buff *msg, struct ib_qp *ibqp);
  2412. int (*fill_res_cm_id_entry)(struct sk_buff *msg, struct rdma_cm_id *id);
  2413. int (*fill_res_srq_entry)(struct sk_buff *msg, struct ib_srq *ib_srq);
  2414. int (*fill_res_srq_entry_raw)(struct sk_buff *msg, struct ib_srq *ib_srq);
  2415. /* Device lifecycle callbacks */
  2416. /*
  2417. * Called after the device becomes registered, before clients are
  2418. * attached
  2419. */
  2420. int (*enable_driver)(struct ib_device *dev);
  2421. /*
  2422. * This is called as part of ib_dealloc_device().
  2423. */
  2424. void (*dealloc_driver)(struct ib_device *dev);
  2425. /* iWarp CM callbacks */
  2426. void (*iw_add_ref)(struct ib_qp *qp);
  2427. void (*iw_rem_ref)(struct ib_qp *qp);
  2428. struct ib_qp *(*iw_get_qp)(struct ib_device *device, int qpn);
  2429. int (*iw_connect)(struct iw_cm_id *cm_id,
  2430. struct iw_cm_conn_param *conn_param);
  2431. int (*iw_accept)(struct iw_cm_id *cm_id,
  2432. struct iw_cm_conn_param *conn_param);
  2433. int (*iw_reject)(struct iw_cm_id *cm_id, const void *pdata,
  2434. u8 pdata_len);
  2435. int (*iw_create_listen)(struct iw_cm_id *cm_id, int backlog);
  2436. int (*iw_destroy_listen)(struct iw_cm_id *cm_id);
  2437. /*
  2438. * counter_bind_qp - Bind a QP to a counter.
  2439. * @counter - The counter to be bound. If counter->id is zero then
  2440. * the driver needs to allocate a new counter and set counter->id
  2441. */
  2442. int (*counter_bind_qp)(struct rdma_counter *counter, struct ib_qp *qp,
  2443. u32 port);
  2444. /*
  2445. * counter_unbind_qp - Unbind the qp from the dynamically-allocated
  2446. * counter and bind it onto the default one
  2447. */
  2448. int (*counter_unbind_qp)(struct ib_qp *qp, u32 port);
  2449. /*
  2450. * counter_dealloc -De-allocate the hw counter
  2451. */
  2452. int (*counter_dealloc)(struct rdma_counter *counter);
  2453. /*
  2454. * counter_alloc_stats - Allocate a struct rdma_hw_stats and fill in
  2455. * the driver initialized data.
  2456. */
  2457. struct rdma_hw_stats *(*counter_alloc_stats)(
  2458. struct rdma_counter *counter);
  2459. /*
  2460. * counter_update_stats - Query the stats value of this counter
  2461. */
  2462. int (*counter_update_stats)(struct rdma_counter *counter);
  2463. /*
  2464. * counter_init - Initialize the driver specific rdma counter struct.
  2465. */
  2466. void (*counter_init)(struct rdma_counter *counter);
  2467. /*
  2468. * Allows rdma drivers to add their own restrack attributes
  2469. * dumped via 'rdma stat' iproute2 command.
  2470. */
  2471. int (*fill_stat_mr_entry)(struct sk_buff *msg, struct ib_mr *ibmr);
  2472. /* query driver for its ucontext properties */
  2473. int (*query_ucontext)(struct ib_ucontext *context,
  2474. struct uverbs_attr_bundle *attrs);
  2475. /*
  2476. * Provide NUMA node. This API exists for rdmavt/hfi1 only.
  2477. * Everyone else relies on Linux memory management model.
  2478. */
  2479. int (*get_numa_node)(struct ib_device *dev);
  2480. /*
  2481. * add_sub_dev - Add a sub IB device
  2482. */
  2483. struct ib_device *(*add_sub_dev)(struct ib_device *parent,
  2484. enum rdma_nl_dev_type type,
  2485. const char *name);
  2486. /*
  2487. * del_sub_dev - Delete a sub IB device
  2488. */
  2489. void (*del_sub_dev)(struct ib_device *sub_dev);
  2490. /*
  2491. * ufile_cleanup - Attempt to cleanup ubojects HW resources inside
  2492. * the ufile.
  2493. */
  2494. void (*ufile_hw_cleanup)(struct ib_uverbs_file *ufile);
  2495. /*
  2496. * report_port_event - Drivers need to implement this if they have
  2497. * some private stuff to handle when link status changes.
  2498. */
  2499. void (*report_port_event)(struct ib_device *ibdev,
  2500. struct net_device *ndev, unsigned long event);
  2501. DECLARE_RDMA_OBJ_SIZE(ib_ah);
  2502. DECLARE_RDMA_OBJ_SIZE(ib_counters);
  2503. DECLARE_RDMA_OBJ_SIZE(ib_cq);
  2504. DECLARE_RDMA_OBJ_SIZE(ib_dmah);
  2505. DECLARE_RDMA_OBJ_SIZE(ib_mw);
  2506. DECLARE_RDMA_OBJ_SIZE(ib_pd);
  2507. DECLARE_RDMA_OBJ_SIZE(ib_qp);
  2508. DECLARE_RDMA_OBJ_SIZE(ib_rwq_ind_table);
  2509. DECLARE_RDMA_OBJ_SIZE(ib_srq);
  2510. DECLARE_RDMA_OBJ_SIZE(ib_ucontext);
  2511. DECLARE_RDMA_OBJ_SIZE(ib_xrcd);
  2512. DECLARE_RDMA_OBJ_SIZE(rdma_counter);
  2513. };
  2514. struct ib_core_device {
  2515. /* device must be the first element in structure until,
  2516. * union of ib_core_device and device exists in ib_device.
  2517. */
  2518. struct device dev;
  2519. possible_net_t rdma_net;
  2520. struct kobject *ports_kobj;
  2521. struct list_head port_list;
  2522. struct ib_device *owner; /* reach back to owner ib_device */
  2523. };
  2524. struct rdma_restrack_root;
  2525. struct ib_device {
  2526. /* Do not access @dma_device directly from ULP nor from HW drivers. */
  2527. struct device *dma_device;
  2528. struct ib_device_ops ops;
  2529. char name[IB_DEVICE_NAME_MAX];
  2530. struct rcu_head rcu_head;
  2531. struct list_head event_handler_list;
  2532. /* Protects event_handler_list */
  2533. struct rw_semaphore event_handler_rwsem;
  2534. /* Protects QP's event_handler calls and open_qp list */
  2535. spinlock_t qp_open_list_lock;
  2536. struct rw_semaphore client_data_rwsem;
  2537. struct xarray client_data;
  2538. struct mutex unregistration_lock;
  2539. /* Synchronize GID, Pkey cache entries, subnet prefix, LMC */
  2540. rwlock_t cache_lock;
  2541. /**
  2542. * port_data is indexed by port number
  2543. */
  2544. struct ib_port_data *port_data;
  2545. int num_comp_vectors;
  2546. union {
  2547. struct device dev;
  2548. struct ib_core_device coredev;
  2549. };
  2550. /* First group is for device attributes,
  2551. * Second group is for driver provided attributes (optional).
  2552. * Third group is for the hw_stats
  2553. * It is a NULL terminated array.
  2554. */
  2555. const struct attribute_group *groups[4];
  2556. u8 hw_stats_attr_index;
  2557. u64 uverbs_cmd_mask;
  2558. char node_desc[IB_DEVICE_NODE_DESC_MAX];
  2559. __be64 node_guid;
  2560. u32 local_dma_lkey;
  2561. u16 is_switch:1;
  2562. /* Indicates kernel verbs support, should not be used in drivers */
  2563. u16 kverbs_provider:1;
  2564. /* CQ adaptive moderation (RDMA DIM) */
  2565. u16 use_cq_dim:1;
  2566. u8 node_type;
  2567. u32 phys_port_cnt;
  2568. struct ib_device_attr attrs;
  2569. struct hw_stats_device_data *hw_stats_data;
  2570. #ifdef CONFIG_CGROUP_RDMA
  2571. struct rdmacg_device cg_device;
  2572. #endif
  2573. u32 index;
  2574. spinlock_t cq_pools_lock;
  2575. struct list_head cq_pools[IB_POLL_LAST_POOL_TYPE + 1];
  2576. struct rdma_restrack_root *res;
  2577. const struct uapi_definition *driver_def;
  2578. /*
  2579. * Positive refcount indicates that the device is currently
  2580. * registered and cannot be unregistered.
  2581. */
  2582. refcount_t refcount;
  2583. struct completion unreg_completion;
  2584. struct work_struct unregistration_work;
  2585. const struct rdma_link_ops *link_ops;
  2586. /* Protects compat_devs xarray modifications */
  2587. struct mutex compat_devs_mutex;
  2588. /* Maintains compat devices for each net namespace */
  2589. struct xarray compat_devs;
  2590. /* Used by iWarp CM */
  2591. char iw_ifname[IFNAMSIZ];
  2592. u32 iw_driver_flags;
  2593. u32 lag_flags;
  2594. /* A parent device has a list of sub-devices */
  2595. struct mutex subdev_lock;
  2596. struct list_head subdev_list_head;
  2597. /* A sub device has a type and a parent */
  2598. enum rdma_nl_dev_type type;
  2599. struct ib_device *parent;
  2600. struct list_head subdev_list;
  2601. enum rdma_nl_name_assign_type name_assign_type;
  2602. };
  2603. static inline void *rdma_zalloc_obj(struct ib_device *dev, size_t size,
  2604. gfp_t gfp, bool is_numa_aware)
  2605. {
  2606. if (is_numa_aware && dev->ops.get_numa_node)
  2607. return kzalloc_node(size, gfp, dev->ops.get_numa_node(dev));
  2608. return kzalloc(size, gfp);
  2609. }
  2610. struct ib_client_nl_info;
  2611. struct ib_client {
  2612. const char *name;
  2613. int (*add)(struct ib_device *ibdev);
  2614. void (*remove)(struct ib_device *, void *client_data);
  2615. void (*rename)(struct ib_device *dev, void *client_data);
  2616. int (*get_nl_info)(struct ib_device *ibdev, void *client_data,
  2617. struct ib_client_nl_info *res);
  2618. int (*get_global_nl_info)(struct ib_client_nl_info *res);
  2619. /* Returns the net_dev belonging to this ib_client and matching the
  2620. * given parameters.
  2621. * @dev: An RDMA device that the net_dev use for communication.
  2622. * @port: A physical port number on the RDMA device.
  2623. * @pkey: P_Key that the net_dev uses if applicable.
  2624. * @gid: A GID that the net_dev uses to communicate.
  2625. * @addr: An IP address the net_dev is configured with.
  2626. * @client_data: The device's client data set by ib_set_client_data().
  2627. *
  2628. * An ib_client that implements a net_dev on top of RDMA devices
  2629. * (such as IP over IB) should implement this callback, allowing the
  2630. * rdma_cm module to find the right net_dev for a given request.
  2631. *
  2632. * The caller is responsible for calling dev_put on the returned
  2633. * netdev. */
  2634. struct net_device *(*get_net_dev_by_params)(
  2635. struct ib_device *dev,
  2636. u32 port,
  2637. u16 pkey,
  2638. const union ib_gid *gid,
  2639. const struct sockaddr *addr,
  2640. void *client_data);
  2641. refcount_t uses;
  2642. struct completion uses_zero;
  2643. u32 client_id;
  2644. /* kverbs are not required by the client */
  2645. u8 no_kverbs_req:1;
  2646. };
  2647. /*
  2648. * IB block DMA iterator
  2649. *
  2650. * Iterates the DMA-mapped SGL in contiguous memory blocks aligned
  2651. * to a HW supported page size.
  2652. */
  2653. struct ib_block_iter {
  2654. /* internal states */
  2655. struct scatterlist *__sg; /* sg holding the current aligned block */
  2656. dma_addr_t __dma_addr; /* unaligned DMA address of this block */
  2657. size_t __sg_numblocks; /* ib_umem_num_dma_blocks() */
  2658. unsigned int __sg_nents; /* number of SG entries */
  2659. unsigned int __sg_advance; /* number of bytes to advance in sg in next step */
  2660. unsigned int __pg_bit; /* alignment of current block */
  2661. };
  2662. struct ib_device *_ib_alloc_device(size_t size, struct net *net);
  2663. #define ib_alloc_device(drv_struct, member) \
  2664. container_of(_ib_alloc_device(sizeof(struct drv_struct) + \
  2665. BUILD_BUG_ON_ZERO(offsetof( \
  2666. struct drv_struct, member)), \
  2667. &init_net), \
  2668. struct drv_struct, member)
  2669. #define ib_alloc_device_with_net(drv_struct, member, net) \
  2670. container_of(_ib_alloc_device(sizeof(struct drv_struct) + \
  2671. BUILD_BUG_ON_ZERO(offsetof( \
  2672. struct drv_struct, member)), net), \
  2673. struct drv_struct, member)
  2674. void ib_dealloc_device(struct ib_device *device);
  2675. void ib_get_device_fw_str(struct ib_device *device, char *str);
  2676. int ib_register_device(struct ib_device *device, const char *name,
  2677. struct device *dma_device);
  2678. void ib_unregister_device(struct ib_device *device);
  2679. void ib_unregister_driver(enum rdma_driver_id driver_id);
  2680. void ib_unregister_device_and_put(struct ib_device *device);
  2681. void ib_unregister_device_queued(struct ib_device *ib_dev);
  2682. int ib_register_client (struct ib_client *client);
  2683. void ib_unregister_client(struct ib_client *client);
  2684. void __rdma_block_iter_start(struct ib_block_iter *biter,
  2685. struct scatterlist *sglist,
  2686. unsigned int nents,
  2687. unsigned long pgsz);
  2688. bool __rdma_block_iter_next(struct ib_block_iter *biter);
  2689. /**
  2690. * rdma_block_iter_dma_address - get the aligned dma address of the current
  2691. * block held by the block iterator.
  2692. * @biter: block iterator holding the memory block
  2693. */
  2694. static inline dma_addr_t
  2695. rdma_block_iter_dma_address(struct ib_block_iter *biter)
  2696. {
  2697. return biter->__dma_addr & ~(BIT_ULL(biter->__pg_bit) - 1);
  2698. }
  2699. /**
  2700. * rdma_for_each_block - iterate over contiguous memory blocks of the sg list
  2701. * @sglist: sglist to iterate over
  2702. * @biter: block iterator holding the memory block
  2703. * @nents: maximum number of sg entries to iterate over
  2704. * @pgsz: best HW supported page size to use
  2705. *
  2706. * Callers may use rdma_block_iter_dma_address() to get each
  2707. * blocks aligned DMA address.
  2708. */
  2709. #define rdma_for_each_block(sglist, biter, nents, pgsz) \
  2710. for (__rdma_block_iter_start(biter, sglist, nents, \
  2711. pgsz); \
  2712. __rdma_block_iter_next(biter);)
  2713. /**
  2714. * ib_get_client_data - Get IB client context
  2715. * @device:Device to get context for
  2716. * @client:Client to get context for
  2717. *
  2718. * ib_get_client_data() returns the client context data set with
  2719. * ib_set_client_data(). This can only be called while the client is
  2720. * registered to the device, once the ib_client remove() callback returns this
  2721. * cannot be called.
  2722. */
  2723. static inline void *ib_get_client_data(struct ib_device *device,
  2724. struct ib_client *client)
  2725. {
  2726. return xa_load(&device->client_data, client->client_id);
  2727. }
  2728. void ib_set_client_data(struct ib_device *device, struct ib_client *client,
  2729. void *data);
  2730. void ib_set_device_ops(struct ib_device *device,
  2731. const struct ib_device_ops *ops);
  2732. int rdma_user_mmap_io(struct ib_ucontext *ucontext, struct vm_area_struct *vma,
  2733. unsigned long pfn, unsigned long size, pgprot_t prot,
  2734. struct rdma_user_mmap_entry *entry);
  2735. int rdma_user_mmap_entry_insert(struct ib_ucontext *ucontext,
  2736. struct rdma_user_mmap_entry *entry,
  2737. size_t length);
  2738. int rdma_user_mmap_entry_insert_range(struct ib_ucontext *ucontext,
  2739. struct rdma_user_mmap_entry *entry,
  2740. size_t length, u32 min_pgoff,
  2741. u32 max_pgoff);
  2742. #if IS_ENABLED(CONFIG_INFINIBAND_USER_ACCESS)
  2743. void rdma_user_mmap_disassociate(struct ib_device *device);
  2744. #else
  2745. static inline void rdma_user_mmap_disassociate(struct ib_device *device)
  2746. {
  2747. }
  2748. #endif
  2749. static inline int
  2750. rdma_user_mmap_entry_insert_exact(struct ib_ucontext *ucontext,
  2751. struct rdma_user_mmap_entry *entry,
  2752. size_t length, u32 pgoff)
  2753. {
  2754. return rdma_user_mmap_entry_insert_range(ucontext, entry, length, pgoff,
  2755. pgoff);
  2756. }
  2757. struct rdma_user_mmap_entry *
  2758. rdma_user_mmap_entry_get_pgoff(struct ib_ucontext *ucontext,
  2759. unsigned long pgoff);
  2760. struct rdma_user_mmap_entry *
  2761. rdma_user_mmap_entry_get(struct ib_ucontext *ucontext,
  2762. struct vm_area_struct *vma);
  2763. void rdma_user_mmap_entry_put(struct rdma_user_mmap_entry *entry);
  2764. void rdma_user_mmap_entry_remove(struct rdma_user_mmap_entry *entry);
  2765. static inline int ib_copy_from_udata(void *dest, struct ib_udata *udata, size_t len)
  2766. {
  2767. return copy_from_user(dest, udata->inbuf, len) ? -EFAULT : 0;
  2768. }
  2769. static inline int ib_copy_to_udata(struct ib_udata *udata, void *src, size_t len)
  2770. {
  2771. return copy_to_user(udata->outbuf, src, len) ? -EFAULT : 0;
  2772. }
  2773. static inline bool ib_is_buffer_cleared(const void __user *p,
  2774. size_t len)
  2775. {
  2776. bool ret;
  2777. u8 *buf;
  2778. if (len > USHRT_MAX)
  2779. return false;
  2780. buf = memdup_user(p, len);
  2781. if (IS_ERR(buf))
  2782. return false;
  2783. ret = !memchr_inv(buf, 0, len);
  2784. kfree(buf);
  2785. return ret;
  2786. }
  2787. static inline bool ib_is_udata_cleared(struct ib_udata *udata,
  2788. size_t offset,
  2789. size_t len)
  2790. {
  2791. return ib_is_buffer_cleared(udata->inbuf + offset, len);
  2792. }
  2793. /**
  2794. * ib_modify_qp_is_ok - Check that the supplied attribute mask
  2795. * contains all required attributes and no attributes not allowed for
  2796. * the given QP state transition.
  2797. * @cur_state: Current QP state
  2798. * @next_state: Next QP state
  2799. * @type: QP type
  2800. * @mask: Mask of supplied QP attributes
  2801. *
  2802. * This function is a helper function that a low-level driver's
  2803. * modify_qp method can use to validate the consumer's input. It
  2804. * checks that cur_state and next_state are valid QP states, that a
  2805. * transition from cur_state to next_state is allowed by the IB spec,
  2806. * and that the attribute mask supplied is allowed for the transition.
  2807. */
  2808. bool ib_modify_qp_is_ok(enum ib_qp_state cur_state, enum ib_qp_state next_state,
  2809. enum ib_qp_type type, enum ib_qp_attr_mask mask);
  2810. void ib_register_event_handler(struct ib_event_handler *event_handler);
  2811. void ib_unregister_event_handler(struct ib_event_handler *event_handler);
  2812. void ib_dispatch_event(const struct ib_event *event);
  2813. int ib_query_port(struct ib_device *device,
  2814. u32 port_num, struct ib_port_attr *port_attr);
  2815. enum rdma_link_layer rdma_port_get_link_layer(struct ib_device *device,
  2816. u32 port_num);
  2817. /**
  2818. * rdma_cap_ib_switch - Check if the device is IB switch
  2819. * @device: Device to check
  2820. *
  2821. * Device driver is responsible for setting is_switch bit on
  2822. * in ib_device structure at init time.
  2823. *
  2824. * Return: true if the device is IB switch.
  2825. */
  2826. static inline bool rdma_cap_ib_switch(const struct ib_device *device)
  2827. {
  2828. return device->is_switch;
  2829. }
  2830. /**
  2831. * rdma_start_port - Return the first valid port number for the device
  2832. * specified
  2833. *
  2834. * @device: Device to be checked
  2835. *
  2836. * Return start port number
  2837. */
  2838. static inline u32 rdma_start_port(const struct ib_device *device)
  2839. {
  2840. return rdma_cap_ib_switch(device) ? 0 : 1;
  2841. }
  2842. /**
  2843. * rdma_for_each_port - Iterate over all valid port numbers of the IB device
  2844. * @device: The struct ib_device * to iterate over
  2845. * @iter: The unsigned int to store the port number
  2846. */
  2847. #define rdma_for_each_port(device, iter) \
  2848. for (iter = rdma_start_port(device + \
  2849. BUILD_BUG_ON_ZERO(!__same_type(u32, \
  2850. iter))); \
  2851. iter <= rdma_end_port(device); iter++)
  2852. /**
  2853. * rdma_end_port - Return the last valid port number for the device
  2854. * specified
  2855. *
  2856. * @device: Device to be checked
  2857. *
  2858. * Return last port number
  2859. */
  2860. static inline u32 rdma_end_port(const struct ib_device *device)
  2861. {
  2862. return rdma_cap_ib_switch(device) ? 0 : device->phys_port_cnt;
  2863. }
  2864. static inline int rdma_is_port_valid(const struct ib_device *device,
  2865. unsigned int port)
  2866. {
  2867. return (port >= rdma_start_port(device) &&
  2868. port <= rdma_end_port(device));
  2869. }
  2870. static inline bool rdma_is_grh_required(const struct ib_device *device,
  2871. u32 port_num)
  2872. {
  2873. return device->port_data[port_num].immutable.core_cap_flags &
  2874. RDMA_CORE_PORT_IB_GRH_REQUIRED;
  2875. }
  2876. static inline bool rdma_protocol_ib(const struct ib_device *device,
  2877. u32 port_num)
  2878. {
  2879. return device->port_data[port_num].immutable.core_cap_flags &
  2880. RDMA_CORE_CAP_PROT_IB;
  2881. }
  2882. static inline bool rdma_protocol_roce(const struct ib_device *device,
  2883. u32 port_num)
  2884. {
  2885. return device->port_data[port_num].immutable.core_cap_flags &
  2886. (RDMA_CORE_CAP_PROT_ROCE | RDMA_CORE_CAP_PROT_ROCE_UDP_ENCAP);
  2887. }
  2888. static inline bool rdma_protocol_roce_udp_encap(const struct ib_device *device,
  2889. u32 port_num)
  2890. {
  2891. return device->port_data[port_num].immutable.core_cap_flags &
  2892. RDMA_CORE_CAP_PROT_ROCE_UDP_ENCAP;
  2893. }
  2894. static inline bool rdma_protocol_roce_eth_encap(const struct ib_device *device,
  2895. u32 port_num)
  2896. {
  2897. return device->port_data[port_num].immutable.core_cap_flags &
  2898. RDMA_CORE_CAP_PROT_ROCE;
  2899. }
  2900. static inline bool rdma_protocol_iwarp(const struct ib_device *device,
  2901. u32 port_num)
  2902. {
  2903. return device->port_data[port_num].immutable.core_cap_flags &
  2904. RDMA_CORE_CAP_PROT_IWARP;
  2905. }
  2906. static inline bool rdma_ib_or_roce(const struct ib_device *device,
  2907. u32 port_num)
  2908. {
  2909. return rdma_protocol_ib(device, port_num) ||
  2910. rdma_protocol_roce(device, port_num);
  2911. }
  2912. static inline bool rdma_protocol_raw_packet(const struct ib_device *device,
  2913. u32 port_num)
  2914. {
  2915. return device->port_data[port_num].immutable.core_cap_flags &
  2916. RDMA_CORE_CAP_PROT_RAW_PACKET;
  2917. }
  2918. static inline bool rdma_protocol_usnic(const struct ib_device *device,
  2919. u32 port_num)
  2920. {
  2921. return device->port_data[port_num].immutable.core_cap_flags &
  2922. RDMA_CORE_CAP_PROT_USNIC;
  2923. }
  2924. /**
  2925. * rdma_cap_ib_mad - Check if the port of a device supports Infiniband
  2926. * Management Datagrams.
  2927. * @device: Device to check
  2928. * @port_num: Port number to check
  2929. *
  2930. * Management Datagrams (MAD) are a required part of the InfiniBand
  2931. * specification and are supported on all InfiniBand devices. A slightly
  2932. * extended version are also supported on OPA interfaces.
  2933. *
  2934. * Return: true if the port supports sending/receiving of MAD packets.
  2935. */
  2936. static inline bool rdma_cap_ib_mad(const struct ib_device *device, u32 port_num)
  2937. {
  2938. return device->port_data[port_num].immutable.core_cap_flags &
  2939. RDMA_CORE_CAP_IB_MAD;
  2940. }
  2941. /**
  2942. * rdma_cap_opa_mad - Check if the port of device provides support for OPA
  2943. * Management Datagrams.
  2944. * @device: Device to check
  2945. * @port_num: Port number to check
  2946. *
  2947. * Intel OmniPath devices extend and/or replace the InfiniBand Management
  2948. * datagrams with their own versions. These OPA MADs share many but not all of
  2949. * the characteristics of InfiniBand MADs.
  2950. *
  2951. * OPA MADs differ in the following ways:
  2952. *
  2953. * 1) MADs are variable size up to 2K
  2954. * IBTA defined MADs remain fixed at 256 bytes
  2955. * 2) OPA SMPs must carry valid PKeys
  2956. * 3) OPA SMP packets are a different format
  2957. *
  2958. * Return: true if the port supports OPA MAD packet formats.
  2959. */
  2960. static inline bool rdma_cap_opa_mad(struct ib_device *device, u32 port_num)
  2961. {
  2962. return device->port_data[port_num].immutable.core_cap_flags &
  2963. RDMA_CORE_CAP_OPA_MAD;
  2964. }
  2965. /**
  2966. * rdma_cap_ib_smi - Check if the port of a device provides an Infiniband
  2967. * Subnet Management Agent (SMA) on the Subnet Management Interface (SMI).
  2968. * @device: Device to check
  2969. * @port_num: Port number to check
  2970. *
  2971. * Each InfiniBand node is required to provide a Subnet Management Agent
  2972. * that the subnet manager can access. Prior to the fabric being fully
  2973. * configured by the subnet manager, the SMA is accessed via a well known
  2974. * interface called the Subnet Management Interface (SMI). This interface
  2975. * uses directed route packets to communicate with the SM to get around the
  2976. * chicken and egg problem of the SM needing to know what's on the fabric
  2977. * in order to configure the fabric, and needing to configure the fabric in
  2978. * order to send packets to the devices on the fabric. These directed
  2979. * route packets do not need the fabric fully configured in order to reach
  2980. * their destination. The SMI is the only method allowed to send
  2981. * directed route packets on an InfiniBand fabric.
  2982. *
  2983. * Return: true if the port provides an SMI.
  2984. */
  2985. static inline bool rdma_cap_ib_smi(const struct ib_device *device, u32 port_num)
  2986. {
  2987. return device->port_data[port_num].immutable.core_cap_flags &
  2988. RDMA_CORE_CAP_IB_SMI;
  2989. }
  2990. /**
  2991. * rdma_cap_ib_cm - Check if the port of device has the capability Infiniband
  2992. * Communication Manager.
  2993. * @device: Device to check
  2994. * @port_num: Port number to check
  2995. *
  2996. * The InfiniBand Communication Manager is one of many pre-defined General
  2997. * Service Agents (GSA) that are accessed via the General Service
  2998. * Interface (GSI). It's role is to facilitate establishment of connections
  2999. * between nodes as well as other management related tasks for established
  3000. * connections.
  3001. *
  3002. * Return: true if the port supports an IB CM (this does not guarantee that
  3003. * a CM is actually running however).
  3004. */
  3005. static inline bool rdma_cap_ib_cm(const struct ib_device *device, u32 port_num)
  3006. {
  3007. return device->port_data[port_num].immutable.core_cap_flags &
  3008. RDMA_CORE_CAP_IB_CM;
  3009. }
  3010. /**
  3011. * rdma_cap_iw_cm - Check if the port of device has the capability IWARP
  3012. * Communication Manager.
  3013. * @device: Device to check
  3014. * @port_num: Port number to check
  3015. *
  3016. * Similar to above, but specific to iWARP connections which have a different
  3017. * managment protocol than InfiniBand.
  3018. *
  3019. * Return: true if the port supports an iWARP CM (this does not guarantee that
  3020. * a CM is actually running however).
  3021. */
  3022. static inline bool rdma_cap_iw_cm(const struct ib_device *device, u32 port_num)
  3023. {
  3024. return device->port_data[port_num].immutable.core_cap_flags &
  3025. RDMA_CORE_CAP_IW_CM;
  3026. }
  3027. /**
  3028. * rdma_cap_ib_sa - Check if the port of device has the capability Infiniband
  3029. * Subnet Administration.
  3030. * @device: Device to check
  3031. * @port_num: Port number to check
  3032. *
  3033. * An InfiniBand Subnet Administration (SA) service is a pre-defined General
  3034. * Service Agent (GSA) provided by the Subnet Manager (SM). On InfiniBand
  3035. * fabrics, devices should resolve routes to other hosts by contacting the
  3036. * SA to query the proper route.
  3037. *
  3038. * Return: true if the port should act as a client to the fabric Subnet
  3039. * Administration interface. This does not imply that the SA service is
  3040. * running locally.
  3041. */
  3042. static inline bool rdma_cap_ib_sa(const struct ib_device *device, u32 port_num)
  3043. {
  3044. return device->port_data[port_num].immutable.core_cap_flags &
  3045. RDMA_CORE_CAP_IB_SA;
  3046. }
  3047. /**
  3048. * rdma_cap_ib_mcast - Check if the port of device has the capability Infiniband
  3049. * Multicast.
  3050. * @device: Device to check
  3051. * @port_num: Port number to check
  3052. *
  3053. * InfiniBand multicast registration is more complex than normal IPv4 or
  3054. * IPv6 multicast registration. Each Host Channel Adapter must register
  3055. * with the Subnet Manager when it wishes to join a multicast group. It
  3056. * should do so only once regardless of how many queue pairs it subscribes
  3057. * to this group. And it should leave the group only after all queue pairs
  3058. * attached to the group have been detached.
  3059. *
  3060. * Return: true if the port must undertake the additional adminstrative
  3061. * overhead of registering/unregistering with the SM and tracking of the
  3062. * total number of queue pairs attached to the multicast group.
  3063. */
  3064. static inline bool rdma_cap_ib_mcast(const struct ib_device *device,
  3065. u32 port_num)
  3066. {
  3067. return rdma_cap_ib_sa(device, port_num);
  3068. }
  3069. /**
  3070. * rdma_cap_af_ib - Check if the port of device has the capability
  3071. * Native Infiniband Address.
  3072. * @device: Device to check
  3073. * @port_num: Port number to check
  3074. *
  3075. * InfiniBand addressing uses a port's GUID + Subnet Prefix to make a default
  3076. * GID. RoCE uses a different mechanism, but still generates a GID via
  3077. * a prescribed mechanism and port specific data.
  3078. *
  3079. * Return: true if the port uses a GID address to identify devices on the
  3080. * network.
  3081. */
  3082. static inline bool rdma_cap_af_ib(const struct ib_device *device, u32 port_num)
  3083. {
  3084. return device->port_data[port_num].immutable.core_cap_flags &
  3085. RDMA_CORE_CAP_AF_IB;
  3086. }
  3087. /**
  3088. * rdma_cap_eth_ah - Check if the port of device has the capability
  3089. * Ethernet Address Handle.
  3090. * @device: Device to check
  3091. * @port_num: Port number to check
  3092. *
  3093. * RoCE is InfiniBand over Ethernet, and it uses a well defined technique
  3094. * to fabricate GIDs over Ethernet/IP specific addresses native to the
  3095. * port. Normally, packet headers are generated by the sending host
  3096. * adapter, but when sending connectionless datagrams, we must manually
  3097. * inject the proper headers for the fabric we are communicating over.
  3098. *
  3099. * Return: true if we are running as a RoCE port and must force the
  3100. * addition of a Global Route Header built from our Ethernet Address
  3101. * Handle into our header list for connectionless packets.
  3102. */
  3103. static inline bool rdma_cap_eth_ah(const struct ib_device *device, u32 port_num)
  3104. {
  3105. return device->port_data[port_num].immutable.core_cap_flags &
  3106. RDMA_CORE_CAP_ETH_AH;
  3107. }
  3108. /**
  3109. * rdma_cap_opa_ah - Check if the port of device supports
  3110. * OPA Address handles
  3111. * @device: Device to check
  3112. * @port_num: Port number to check
  3113. *
  3114. * Return: true if we are running on an OPA device which supports
  3115. * the extended OPA addressing.
  3116. */
  3117. static inline bool rdma_cap_opa_ah(struct ib_device *device, u32 port_num)
  3118. {
  3119. return (device->port_data[port_num].immutable.core_cap_flags &
  3120. RDMA_CORE_CAP_OPA_AH) == RDMA_CORE_CAP_OPA_AH;
  3121. }
  3122. /**
  3123. * rdma_max_mad_size - Return the max MAD size required by this RDMA Port.
  3124. *
  3125. * @device: Device
  3126. * @port_num: Port number
  3127. *
  3128. * This MAD size includes the MAD headers and MAD payload. No other headers
  3129. * are included.
  3130. *
  3131. * Return the max MAD size required by the Port. Will return 0 if the port
  3132. * does not support MADs
  3133. */
  3134. static inline size_t rdma_max_mad_size(const struct ib_device *device,
  3135. u32 port_num)
  3136. {
  3137. return device->port_data[port_num].immutable.max_mad_size;
  3138. }
  3139. /**
  3140. * rdma_cap_roce_gid_table - Check if the port of device uses roce_gid_table
  3141. * @device: Device to check
  3142. * @port_num: Port number to check
  3143. *
  3144. * RoCE GID table mechanism manages the various GIDs for a device.
  3145. *
  3146. * NOTE: if allocating the port's GID table has failed, this call will still
  3147. * return true, but any RoCE GID table API will fail.
  3148. *
  3149. * Return: true if the port uses RoCE GID table mechanism in order to manage
  3150. * its GIDs.
  3151. */
  3152. static inline bool rdma_cap_roce_gid_table(const struct ib_device *device,
  3153. u32 port_num)
  3154. {
  3155. return rdma_protocol_roce(device, port_num) &&
  3156. device->ops.add_gid && device->ops.del_gid;
  3157. }
  3158. /*
  3159. * Check if the device supports READ W/ INVALIDATE.
  3160. */
  3161. static inline bool rdma_cap_read_inv(struct ib_device *dev, u32 port_num)
  3162. {
  3163. /*
  3164. * iWarp drivers must support READ W/ INVALIDATE. No other protocol
  3165. * has support for it yet.
  3166. */
  3167. return rdma_protocol_iwarp(dev, port_num);
  3168. }
  3169. /**
  3170. * rdma_core_cap_opa_port - Return whether the RDMA Port is OPA or not.
  3171. * @device: Device
  3172. * @port_num: 1 based Port number
  3173. *
  3174. * Return true if port is an Intel OPA port , false if not
  3175. */
  3176. static inline bool rdma_core_cap_opa_port(struct ib_device *device,
  3177. u32 port_num)
  3178. {
  3179. return (device->port_data[port_num].immutable.core_cap_flags &
  3180. RDMA_CORE_PORT_INTEL_OPA) == RDMA_CORE_PORT_INTEL_OPA;
  3181. }
  3182. /**
  3183. * rdma_mtu_enum_to_int - Return the mtu of the port as an integer value.
  3184. * @device: Device
  3185. * @port: Port number
  3186. * @mtu: enum value of MTU
  3187. *
  3188. * Return the MTU size supported by the port as an integer value. Will return
  3189. * -1 if enum value of mtu is not supported.
  3190. */
  3191. static inline int rdma_mtu_enum_to_int(struct ib_device *device, u32 port,
  3192. int mtu)
  3193. {
  3194. if (rdma_core_cap_opa_port(device, port))
  3195. return opa_mtu_enum_to_int((enum opa_mtu)mtu);
  3196. else
  3197. return ib_mtu_enum_to_int((enum ib_mtu)mtu);
  3198. }
  3199. /**
  3200. * rdma_mtu_from_attr - Return the mtu of the port from the port attribute.
  3201. * @device: Device
  3202. * @port: Port number
  3203. * @attr: port attribute
  3204. *
  3205. * Return the MTU size supported by the port as an integer value.
  3206. */
  3207. static inline int rdma_mtu_from_attr(struct ib_device *device, u32 port,
  3208. struct ib_port_attr *attr)
  3209. {
  3210. if (rdma_core_cap_opa_port(device, port))
  3211. return attr->phys_mtu;
  3212. else
  3213. return ib_mtu_enum_to_int(attr->max_mtu);
  3214. }
  3215. int ib_set_vf_link_state(struct ib_device *device, int vf, u32 port,
  3216. int state);
  3217. int ib_get_vf_config(struct ib_device *device, int vf, u32 port,
  3218. struct ifla_vf_info *info);
  3219. int ib_get_vf_stats(struct ib_device *device, int vf, u32 port,
  3220. struct ifla_vf_stats *stats);
  3221. int ib_get_vf_guid(struct ib_device *device, int vf, u32 port,
  3222. struct ifla_vf_guid *node_guid,
  3223. struct ifla_vf_guid *port_guid);
  3224. int ib_set_vf_guid(struct ib_device *device, int vf, u32 port, u64 guid,
  3225. int type);
  3226. int ib_query_pkey(struct ib_device *device,
  3227. u32 port_num, u16 index, u16 *pkey);
  3228. int ib_modify_device(struct ib_device *device,
  3229. int device_modify_mask,
  3230. struct ib_device_modify *device_modify);
  3231. int ib_modify_port(struct ib_device *device,
  3232. u32 port_num, int port_modify_mask,
  3233. struct ib_port_modify *port_modify);
  3234. int ib_find_gid(struct ib_device *device, union ib_gid *gid,
  3235. u32 *port_num, u16 *index);
  3236. int ib_find_pkey(struct ib_device *device,
  3237. u32 port_num, u16 pkey, u16 *index);
  3238. enum ib_pd_flags {
  3239. /*
  3240. * Create a memory registration for all memory in the system and place
  3241. * the rkey for it into pd->unsafe_global_rkey. This can be used by
  3242. * ULPs to avoid the overhead of dynamic MRs.
  3243. *
  3244. * This flag is generally considered unsafe and must only be used in
  3245. * extremly trusted environments. Every use of it will log a warning
  3246. * in the kernel log.
  3247. */
  3248. IB_PD_UNSAFE_GLOBAL_RKEY = 0x01,
  3249. };
  3250. struct ib_pd *__ib_alloc_pd(struct ib_device *device, unsigned int flags,
  3251. const char *caller);
  3252. /**
  3253. * ib_alloc_pd - Allocates an unused protection domain.
  3254. * @device: The device on which to allocate the protection domain.
  3255. * @flags: protection domain flags
  3256. *
  3257. * A protection domain object provides an association between QPs, shared
  3258. * receive queues, address handles, memory regions, and memory windows.
  3259. *
  3260. * Every PD has a local_dma_lkey which can be used as the lkey value for local
  3261. * memory operations.
  3262. */
  3263. #define ib_alloc_pd(device, flags) \
  3264. __ib_alloc_pd((device), (flags), KBUILD_MODNAME)
  3265. int ib_dealloc_pd_user(struct ib_pd *pd, struct ib_udata *udata);
  3266. /**
  3267. * ib_dealloc_pd - Deallocate kernel PD
  3268. * @pd: The protection domain
  3269. *
  3270. * NOTE: for user PD use ib_dealloc_pd_user with valid udata!
  3271. */
  3272. static inline void ib_dealloc_pd(struct ib_pd *pd)
  3273. {
  3274. int ret = ib_dealloc_pd_user(pd, NULL);
  3275. WARN_ONCE(ret, "Destroy of kernel PD shouldn't fail");
  3276. }
  3277. enum rdma_create_ah_flags {
  3278. /* In a sleepable context */
  3279. RDMA_CREATE_AH_SLEEPABLE = BIT(0),
  3280. };
  3281. /**
  3282. * rdma_create_ah - Creates an address handle for the given address vector.
  3283. * @pd: The protection domain associated with the address handle.
  3284. * @ah_attr: The attributes of the address vector.
  3285. * @flags: Create address handle flags (see enum rdma_create_ah_flags).
  3286. *
  3287. * The address handle is used to reference a local or global destination
  3288. * in all UD QP post sends.
  3289. */
  3290. struct ib_ah *rdma_create_ah(struct ib_pd *pd, struct rdma_ah_attr *ah_attr,
  3291. u32 flags);
  3292. /**
  3293. * rdma_create_user_ah - Creates an address handle for the given address vector.
  3294. * It resolves destination mac address for ah attribute of RoCE type.
  3295. * @pd: The protection domain associated with the address handle.
  3296. * @ah_attr: The attributes of the address vector.
  3297. * @udata: pointer to user's input output buffer information need by
  3298. * provider driver.
  3299. *
  3300. * It returns 0 on success and returns appropriate error code on error.
  3301. * The address handle is used to reference a local or global destination
  3302. * in all UD QP post sends.
  3303. */
  3304. struct ib_ah *rdma_create_user_ah(struct ib_pd *pd,
  3305. struct rdma_ah_attr *ah_attr,
  3306. struct ib_udata *udata);
  3307. /**
  3308. * ib_get_gids_from_rdma_hdr - Get sgid and dgid from GRH or IPv4 header
  3309. * work completion.
  3310. * @hdr: the L3 header to parse
  3311. * @net_type: type of header to parse
  3312. * @sgid: place to store source gid
  3313. * @dgid: place to store destination gid
  3314. */
  3315. int ib_get_gids_from_rdma_hdr(const union rdma_network_hdr *hdr,
  3316. enum rdma_network_type net_type,
  3317. union ib_gid *sgid, union ib_gid *dgid);
  3318. /**
  3319. * ib_get_rdma_header_version - Get the header version
  3320. * @hdr: the L3 header to parse
  3321. */
  3322. int ib_get_rdma_header_version(const union rdma_network_hdr *hdr);
  3323. /**
  3324. * ib_init_ah_attr_from_wc - Initializes address handle attributes from a
  3325. * work completion.
  3326. * @device: Device on which the received message arrived.
  3327. * @port_num: Port on which the received message arrived.
  3328. * @wc: Work completion associated with the received message.
  3329. * @grh: References the received global route header. This parameter is
  3330. * ignored unless the work completion indicates that the GRH is valid.
  3331. * @ah_attr: Returned attributes that can be used when creating an address
  3332. * handle for replying to the message.
  3333. * When ib_init_ah_attr_from_wc() returns success,
  3334. * (a) for IB link layer it optionally contains a reference to SGID attribute
  3335. * when GRH is present for IB link layer.
  3336. * (b) for RoCE link layer it contains a reference to SGID attribute.
  3337. * User must invoke rdma_cleanup_ah_attr_gid_attr() to release reference to SGID
  3338. * attributes which are initialized using ib_init_ah_attr_from_wc().
  3339. *
  3340. */
  3341. int ib_init_ah_attr_from_wc(struct ib_device *device, u32 port_num,
  3342. const struct ib_wc *wc, const struct ib_grh *grh,
  3343. struct rdma_ah_attr *ah_attr);
  3344. /**
  3345. * ib_create_ah_from_wc - Creates an address handle associated with the
  3346. * sender of the specified work completion.
  3347. * @pd: The protection domain associated with the address handle.
  3348. * @wc: Work completion information associated with a received message.
  3349. * @grh: References the received global route header. This parameter is
  3350. * ignored unless the work completion indicates that the GRH is valid.
  3351. * @port_num: The outbound port number to associate with the address.
  3352. *
  3353. * The address handle is used to reference a local or global destination
  3354. * in all UD QP post sends.
  3355. */
  3356. struct ib_ah *ib_create_ah_from_wc(struct ib_pd *pd, const struct ib_wc *wc,
  3357. const struct ib_grh *grh, u32 port_num);
  3358. /**
  3359. * rdma_modify_ah - Modifies the address vector associated with an address
  3360. * handle.
  3361. * @ah: The address handle to modify.
  3362. * @ah_attr: The new address vector attributes to associate with the
  3363. * address handle.
  3364. */
  3365. int rdma_modify_ah(struct ib_ah *ah, struct rdma_ah_attr *ah_attr);
  3366. /**
  3367. * rdma_query_ah - Queries the address vector associated with an address
  3368. * handle.
  3369. * @ah: The address handle to query.
  3370. * @ah_attr: The address vector attributes associated with the address
  3371. * handle.
  3372. */
  3373. int rdma_query_ah(struct ib_ah *ah, struct rdma_ah_attr *ah_attr);
  3374. enum rdma_destroy_ah_flags {
  3375. /* In a sleepable context */
  3376. RDMA_DESTROY_AH_SLEEPABLE = BIT(0),
  3377. };
  3378. /**
  3379. * rdma_destroy_ah_user - Destroys an address handle.
  3380. * @ah: The address handle to destroy.
  3381. * @flags: Destroy address handle flags (see enum rdma_destroy_ah_flags).
  3382. * @udata: Valid user data or NULL for kernel objects
  3383. */
  3384. int rdma_destroy_ah_user(struct ib_ah *ah, u32 flags, struct ib_udata *udata);
  3385. /**
  3386. * rdma_destroy_ah - Destroys an kernel address handle.
  3387. * @ah: The address handle to destroy.
  3388. * @flags: Destroy address handle flags (see enum rdma_destroy_ah_flags).
  3389. *
  3390. * NOTE: for user ah use rdma_destroy_ah_user with valid udata!
  3391. */
  3392. static inline void rdma_destroy_ah(struct ib_ah *ah, u32 flags)
  3393. {
  3394. int ret = rdma_destroy_ah_user(ah, flags, NULL);
  3395. WARN_ONCE(ret, "Destroy of kernel AH shouldn't fail");
  3396. }
  3397. struct ib_srq *ib_create_srq_user(struct ib_pd *pd,
  3398. struct ib_srq_init_attr *srq_init_attr,
  3399. struct ib_usrq_object *uobject,
  3400. struct ib_udata *udata);
  3401. static inline struct ib_srq *
  3402. ib_create_srq(struct ib_pd *pd, struct ib_srq_init_attr *srq_init_attr)
  3403. {
  3404. if (!pd->device->ops.create_srq)
  3405. return ERR_PTR(-EOPNOTSUPP);
  3406. return ib_create_srq_user(pd, srq_init_attr, NULL, NULL);
  3407. }
  3408. /**
  3409. * ib_modify_srq - Modifies the attributes for the specified SRQ.
  3410. * @srq: The SRQ to modify.
  3411. * @srq_attr: On input, specifies the SRQ attributes to modify. On output,
  3412. * the current values of selected SRQ attributes are returned.
  3413. * @srq_attr_mask: A bit-mask used to specify which attributes of the SRQ
  3414. * are being modified.
  3415. *
  3416. * The mask may contain IB_SRQ_MAX_WR to resize the SRQ and/or
  3417. * IB_SRQ_LIMIT to set the SRQ's limit and request notification when
  3418. * the number of receives queued drops below the limit.
  3419. */
  3420. int ib_modify_srq(struct ib_srq *srq,
  3421. struct ib_srq_attr *srq_attr,
  3422. enum ib_srq_attr_mask srq_attr_mask);
  3423. /**
  3424. * ib_query_srq - Returns the attribute list and current values for the
  3425. * specified SRQ.
  3426. * @srq: The SRQ to query.
  3427. * @srq_attr: The attributes of the specified SRQ.
  3428. */
  3429. int ib_query_srq(struct ib_srq *srq,
  3430. struct ib_srq_attr *srq_attr);
  3431. /**
  3432. * ib_destroy_srq_user - Destroys the specified SRQ.
  3433. * @srq: The SRQ to destroy.
  3434. * @udata: Valid user data or NULL for kernel objects
  3435. */
  3436. int ib_destroy_srq_user(struct ib_srq *srq, struct ib_udata *udata);
  3437. /**
  3438. * ib_destroy_srq - Destroys the specified kernel SRQ.
  3439. * @srq: The SRQ to destroy.
  3440. *
  3441. * NOTE: for user srq use ib_destroy_srq_user with valid udata!
  3442. */
  3443. static inline void ib_destroy_srq(struct ib_srq *srq)
  3444. {
  3445. int ret = ib_destroy_srq_user(srq, NULL);
  3446. WARN_ONCE(ret, "Destroy of kernel SRQ shouldn't fail");
  3447. }
  3448. /**
  3449. * ib_post_srq_recv - Posts a list of work requests to the specified SRQ.
  3450. * @srq: The SRQ to post the work request on.
  3451. * @recv_wr: A list of work requests to post on the receive queue.
  3452. * @bad_recv_wr: On an immediate failure, this parameter will reference
  3453. * the work request that failed to be posted on the QP.
  3454. */
  3455. static inline int ib_post_srq_recv(struct ib_srq *srq,
  3456. const struct ib_recv_wr *recv_wr,
  3457. const struct ib_recv_wr **bad_recv_wr)
  3458. {
  3459. const struct ib_recv_wr *dummy;
  3460. return srq->device->ops.post_srq_recv(srq, recv_wr,
  3461. bad_recv_wr ? : &dummy);
  3462. }
  3463. struct ib_qp *ib_create_qp_kernel(struct ib_pd *pd,
  3464. struct ib_qp_init_attr *qp_init_attr,
  3465. const char *caller);
  3466. /**
  3467. * ib_create_qp - Creates a kernel QP associated with the specific protection
  3468. * domain.
  3469. * @pd: The protection domain associated with the QP.
  3470. * @init_attr: A list of initial attributes required to create the
  3471. * QP. If QP creation succeeds, then the attributes are updated to
  3472. * the actual capabilities of the created QP.
  3473. */
  3474. static inline struct ib_qp *ib_create_qp(struct ib_pd *pd,
  3475. struct ib_qp_init_attr *init_attr)
  3476. {
  3477. return ib_create_qp_kernel(pd, init_attr, KBUILD_MODNAME);
  3478. }
  3479. /**
  3480. * ib_modify_qp_with_udata - Modifies the attributes for the specified QP.
  3481. * @qp: The QP to modify.
  3482. * @attr: On input, specifies the QP attributes to modify. On output,
  3483. * the current values of selected QP attributes are returned.
  3484. * @attr_mask: A bit-mask used to specify which attributes of the QP
  3485. * are being modified.
  3486. * @udata: pointer to user's input output buffer information
  3487. * are being modified.
  3488. * It returns 0 on success and returns appropriate error code on error.
  3489. */
  3490. int ib_modify_qp_with_udata(struct ib_qp *qp,
  3491. struct ib_qp_attr *attr,
  3492. int attr_mask,
  3493. struct ib_udata *udata);
  3494. /**
  3495. * ib_modify_qp - Modifies the attributes for the specified QP and then
  3496. * transitions the QP to the given state.
  3497. * @qp: The QP to modify.
  3498. * @qp_attr: On input, specifies the QP attributes to modify. On output,
  3499. * the current values of selected QP attributes are returned.
  3500. * @qp_attr_mask: A bit-mask used to specify which attributes of the QP
  3501. * are being modified.
  3502. */
  3503. int ib_modify_qp(struct ib_qp *qp,
  3504. struct ib_qp_attr *qp_attr,
  3505. int qp_attr_mask);
  3506. /**
  3507. * ib_query_qp - Returns the attribute list and current values for the
  3508. * specified QP.
  3509. * @qp: The QP to query.
  3510. * @qp_attr: The attributes of the specified QP.
  3511. * @qp_attr_mask: A bit-mask used to select specific attributes to query.
  3512. * @qp_init_attr: Additional attributes of the selected QP.
  3513. *
  3514. * The qp_attr_mask may be used to limit the query to gathering only the
  3515. * selected attributes.
  3516. */
  3517. int ib_query_qp(struct ib_qp *qp,
  3518. struct ib_qp_attr *qp_attr,
  3519. int qp_attr_mask,
  3520. struct ib_qp_init_attr *qp_init_attr);
  3521. /**
  3522. * ib_destroy_qp - Destroys the specified QP.
  3523. * @qp: The QP to destroy.
  3524. * @udata: Valid udata or NULL for kernel objects
  3525. */
  3526. int ib_destroy_qp_user(struct ib_qp *qp, struct ib_udata *udata);
  3527. /**
  3528. * ib_destroy_qp - Destroys the specified kernel QP.
  3529. * @qp: The QP to destroy.
  3530. *
  3531. * NOTE: for user qp use ib_destroy_qp_user with valid udata!
  3532. */
  3533. static inline int ib_destroy_qp(struct ib_qp *qp)
  3534. {
  3535. return ib_destroy_qp_user(qp, NULL);
  3536. }
  3537. /**
  3538. * ib_open_qp - Obtain a reference to an existing sharable QP.
  3539. * @xrcd: XRC domain
  3540. * @qp_open_attr: Attributes identifying the QP to open.
  3541. *
  3542. * Returns a reference to a sharable QP.
  3543. */
  3544. struct ib_qp *ib_open_qp(struct ib_xrcd *xrcd,
  3545. struct ib_qp_open_attr *qp_open_attr);
  3546. /**
  3547. * ib_close_qp - Release an external reference to a QP.
  3548. * @qp: The QP handle to release
  3549. *
  3550. * The opened QP handle is released by the caller. The underlying
  3551. * shared QP is not destroyed until all internal references are released.
  3552. */
  3553. int ib_close_qp(struct ib_qp *qp);
  3554. /**
  3555. * ib_post_send - Posts a list of work requests to the send queue of
  3556. * the specified QP.
  3557. * @qp: The QP to post the work request on.
  3558. * @send_wr: A list of work requests to post on the send queue.
  3559. * @bad_send_wr: On an immediate failure, this parameter will reference
  3560. * the work request that failed to be posted on the QP.
  3561. *
  3562. * While IBA Vol. 1 section 11.4.1.1 specifies that if an immediate
  3563. * error is returned, the QP state shall not be affected,
  3564. * ib_post_send() will return an immediate error after queueing any
  3565. * earlier work requests in the list.
  3566. */
  3567. static inline int ib_post_send(struct ib_qp *qp,
  3568. const struct ib_send_wr *send_wr,
  3569. const struct ib_send_wr **bad_send_wr)
  3570. {
  3571. const struct ib_send_wr *dummy;
  3572. return qp->device->ops.post_send(qp, send_wr, bad_send_wr ? : &dummy);
  3573. }
  3574. /**
  3575. * ib_post_recv - Posts a list of work requests to the receive queue of
  3576. * the specified QP.
  3577. * @qp: The QP to post the work request on.
  3578. * @recv_wr: A list of work requests to post on the receive queue.
  3579. * @bad_recv_wr: On an immediate failure, this parameter will reference
  3580. * the work request that failed to be posted on the QP.
  3581. */
  3582. static inline int ib_post_recv(struct ib_qp *qp,
  3583. const struct ib_recv_wr *recv_wr,
  3584. const struct ib_recv_wr **bad_recv_wr)
  3585. {
  3586. const struct ib_recv_wr *dummy;
  3587. return qp->device->ops.post_recv(qp, recv_wr, bad_recv_wr ? : &dummy);
  3588. }
  3589. struct ib_cq *__ib_alloc_cq(struct ib_device *dev, void *private, int nr_cqe,
  3590. int comp_vector, enum ib_poll_context poll_ctx,
  3591. const char *caller);
  3592. static inline struct ib_cq *ib_alloc_cq(struct ib_device *dev, void *private,
  3593. int nr_cqe, int comp_vector,
  3594. enum ib_poll_context poll_ctx)
  3595. {
  3596. return __ib_alloc_cq(dev, private, nr_cqe, comp_vector, poll_ctx,
  3597. KBUILD_MODNAME);
  3598. }
  3599. struct ib_cq *__ib_alloc_cq_any(struct ib_device *dev, void *private,
  3600. int nr_cqe, enum ib_poll_context poll_ctx,
  3601. const char *caller);
  3602. /**
  3603. * ib_alloc_cq_any: Allocate kernel CQ
  3604. * @dev: The IB device
  3605. * @private: Private data attached to the CQE
  3606. * @nr_cqe: Number of CQEs in the CQ
  3607. * @poll_ctx: Context used for polling the CQ
  3608. */
  3609. static inline struct ib_cq *ib_alloc_cq_any(struct ib_device *dev,
  3610. void *private, int nr_cqe,
  3611. enum ib_poll_context poll_ctx)
  3612. {
  3613. return __ib_alloc_cq_any(dev, private, nr_cqe, poll_ctx,
  3614. KBUILD_MODNAME);
  3615. }
  3616. void ib_free_cq(struct ib_cq *cq);
  3617. int ib_process_cq_direct(struct ib_cq *cq, int budget);
  3618. /**
  3619. * ib_create_cq - Creates a CQ on the specified device.
  3620. * @device: The device on which to create the CQ.
  3621. * @comp_handler: A user-specified callback that is invoked when a
  3622. * completion event occurs on the CQ.
  3623. * @event_handler: A user-specified callback that is invoked when an
  3624. * asynchronous event not associated with a completion occurs on the CQ.
  3625. * @cq_context: Context associated with the CQ returned to the user via
  3626. * the associated completion and event handlers.
  3627. * @cq_attr: The attributes the CQ should be created upon.
  3628. *
  3629. * Users can examine the cq structure to determine the actual CQ size.
  3630. */
  3631. struct ib_cq *__ib_create_cq(struct ib_device *device,
  3632. ib_comp_handler comp_handler,
  3633. void (*event_handler)(struct ib_event *, void *),
  3634. void *cq_context,
  3635. const struct ib_cq_init_attr *cq_attr,
  3636. const char *caller);
  3637. #define ib_create_cq(device, cmp_hndlr, evt_hndlr, cq_ctxt, cq_attr) \
  3638. __ib_create_cq((device), (cmp_hndlr), (evt_hndlr), (cq_ctxt), (cq_attr), KBUILD_MODNAME)
  3639. /**
  3640. * ib_resize_cq - Modifies the capacity of the CQ.
  3641. * @cq: The CQ to resize.
  3642. * @cqe: The minimum size of the CQ.
  3643. *
  3644. * Users can examine the cq structure to determine the actual CQ size.
  3645. */
  3646. int ib_resize_cq(struct ib_cq *cq, int cqe);
  3647. /**
  3648. * rdma_set_cq_moderation - Modifies moderation params of the CQ
  3649. * @cq: The CQ to modify.
  3650. * @cq_count: number of CQEs that will trigger an event
  3651. * @cq_period: max period of time in usec before triggering an event
  3652. *
  3653. */
  3654. int rdma_set_cq_moderation(struct ib_cq *cq, u16 cq_count, u16 cq_period);
  3655. /**
  3656. * ib_destroy_cq_user - Destroys the specified CQ.
  3657. * @cq: The CQ to destroy.
  3658. * @udata: Valid user data or NULL for kernel objects
  3659. */
  3660. int ib_destroy_cq_user(struct ib_cq *cq, struct ib_udata *udata);
  3661. /**
  3662. * ib_destroy_cq - Destroys the specified kernel CQ.
  3663. * @cq: The CQ to destroy.
  3664. *
  3665. * NOTE: for user cq use ib_destroy_cq_user with valid udata!
  3666. */
  3667. static inline void ib_destroy_cq(struct ib_cq *cq)
  3668. {
  3669. int ret = ib_destroy_cq_user(cq, NULL);
  3670. WARN_ONCE(ret, "Destroy of kernel CQ shouldn't fail");
  3671. }
  3672. /**
  3673. * ib_poll_cq - poll a CQ for completion(s)
  3674. * @cq:the CQ being polled
  3675. * @num_entries:maximum number of completions to return
  3676. * @wc:array of at least @num_entries &struct ib_wc where completions
  3677. * will be returned
  3678. *
  3679. * Poll a CQ for (possibly multiple) completions. If the return value
  3680. * is < 0, an error occurred. If the return value is >= 0, it is the
  3681. * number of completions returned. If the return value is
  3682. * non-negative and < num_entries, then the CQ was emptied.
  3683. */
  3684. static inline int ib_poll_cq(struct ib_cq *cq, int num_entries,
  3685. struct ib_wc *wc)
  3686. {
  3687. return cq->device->ops.poll_cq(cq, num_entries, wc);
  3688. }
  3689. /**
  3690. * ib_req_notify_cq - Request completion notification on a CQ.
  3691. * @cq: The CQ to generate an event for.
  3692. * @flags:
  3693. * Must contain exactly one of %IB_CQ_SOLICITED or %IB_CQ_NEXT_COMP
  3694. * to request an event on the next solicited event or next work
  3695. * completion at any type, respectively. %IB_CQ_REPORT_MISSED_EVENTS
  3696. * may also be |ed in to request a hint about missed events, as
  3697. * described below.
  3698. *
  3699. * Return Value:
  3700. * < 0 means an error occurred while requesting notification
  3701. * == 0 means notification was requested successfully, and if
  3702. * IB_CQ_REPORT_MISSED_EVENTS was passed in, then no events
  3703. * were missed and it is safe to wait for another event. In
  3704. * this case is it guaranteed that any work completions added
  3705. * to the CQ since the last CQ poll will trigger a completion
  3706. * notification event.
  3707. * > 0 is only returned if IB_CQ_REPORT_MISSED_EVENTS was passed
  3708. * in. It means that the consumer must poll the CQ again to
  3709. * make sure it is empty to avoid missing an event because of a
  3710. * race between requesting notification and an entry being
  3711. * added to the CQ. This return value means it is possible
  3712. * (but not guaranteed) that a work completion has been added
  3713. * to the CQ since the last poll without triggering a
  3714. * completion notification event.
  3715. */
  3716. static inline int ib_req_notify_cq(struct ib_cq *cq,
  3717. enum ib_cq_notify_flags flags)
  3718. {
  3719. return cq->device->ops.req_notify_cq(cq, flags);
  3720. }
  3721. struct ib_cq *ib_cq_pool_get(struct ib_device *dev, unsigned int nr_cqe,
  3722. int comp_vector_hint,
  3723. enum ib_poll_context poll_ctx);
  3724. void ib_cq_pool_put(struct ib_cq *cq, unsigned int nr_cqe);
  3725. /*
  3726. * Drivers that don't need a DMA mapping at the RDMA layer, set dma_device to
  3727. * NULL. This causes the ib_dma* helpers to just stash the kernel virtual
  3728. * address into the dma address.
  3729. */
  3730. static inline bool ib_uses_virt_dma(struct ib_device *dev)
  3731. {
  3732. return IS_ENABLED(CONFIG_INFINIBAND_VIRT_DMA) && !dev->dma_device;
  3733. }
  3734. /*
  3735. * Check if a IB device's underlying DMA mapping supports P2PDMA transfers.
  3736. */
  3737. static inline bool ib_dma_pci_p2p_dma_supported(struct ib_device *dev)
  3738. {
  3739. if (ib_uses_virt_dma(dev))
  3740. return false;
  3741. return dma_pci_p2pdma_supported(dev->dma_device);
  3742. }
  3743. /**
  3744. * ib_virt_dma_to_ptr - Convert a dma_addr to a kernel pointer
  3745. * @dma_addr: The DMA address
  3746. *
  3747. * Used by ib_uses_virt_dma() devices to get back to the kernel pointer after
  3748. * going through the dma_addr marshalling.
  3749. */
  3750. static inline void *ib_virt_dma_to_ptr(u64 dma_addr)
  3751. {
  3752. /* virt_dma mode maps the kvs's directly into the dma addr */
  3753. return (void *)(uintptr_t)dma_addr;
  3754. }
  3755. /**
  3756. * ib_virt_dma_to_page - Convert a dma_addr to a struct page
  3757. * @dma_addr: The DMA address
  3758. *
  3759. * Used by ib_uses_virt_dma() device to get back to the struct page after going
  3760. * through the dma_addr marshalling.
  3761. */
  3762. static inline struct page *ib_virt_dma_to_page(u64 dma_addr)
  3763. {
  3764. return virt_to_page(ib_virt_dma_to_ptr(dma_addr));
  3765. }
  3766. /**
  3767. * ib_dma_mapping_error - check a DMA addr for error
  3768. * @dev: The device for which the dma_addr was created
  3769. * @dma_addr: The DMA address to check
  3770. */
  3771. static inline int ib_dma_mapping_error(struct ib_device *dev, u64 dma_addr)
  3772. {
  3773. if (ib_uses_virt_dma(dev))
  3774. return 0;
  3775. return dma_mapping_error(dev->dma_device, dma_addr);
  3776. }
  3777. /**
  3778. * ib_dma_map_single - Map a kernel virtual address to DMA address
  3779. * @dev: The device for which the dma_addr is to be created
  3780. * @cpu_addr: The kernel virtual address
  3781. * @size: The size of the region in bytes
  3782. * @direction: The direction of the DMA
  3783. */
  3784. static inline u64 ib_dma_map_single(struct ib_device *dev,
  3785. void *cpu_addr, size_t size,
  3786. enum dma_data_direction direction)
  3787. {
  3788. if (ib_uses_virt_dma(dev))
  3789. return (uintptr_t)cpu_addr;
  3790. return dma_map_single(dev->dma_device, cpu_addr, size, direction);
  3791. }
  3792. /**
  3793. * ib_dma_unmap_single - Destroy a mapping created by ib_dma_map_single()
  3794. * @dev: The device for which the DMA address was created
  3795. * @addr: The DMA address
  3796. * @size: The size of the region in bytes
  3797. * @direction: The direction of the DMA
  3798. */
  3799. static inline void ib_dma_unmap_single(struct ib_device *dev,
  3800. u64 addr, size_t size,
  3801. enum dma_data_direction direction)
  3802. {
  3803. if (!ib_uses_virt_dma(dev))
  3804. dma_unmap_single(dev->dma_device, addr, size, direction);
  3805. }
  3806. /**
  3807. * ib_dma_map_page - Map a physical page to DMA address
  3808. * @dev: The device for which the dma_addr is to be created
  3809. * @page: The page to be mapped
  3810. * @offset: The offset within the page
  3811. * @size: The size of the region in bytes
  3812. * @direction: The direction of the DMA
  3813. */
  3814. static inline u64 ib_dma_map_page(struct ib_device *dev,
  3815. struct page *page,
  3816. unsigned long offset,
  3817. size_t size,
  3818. enum dma_data_direction direction)
  3819. {
  3820. if (ib_uses_virt_dma(dev))
  3821. return (uintptr_t)(page_address(page) + offset);
  3822. return dma_map_page(dev->dma_device, page, offset, size, direction);
  3823. }
  3824. /**
  3825. * ib_dma_unmap_page - Destroy a mapping created by ib_dma_map_page()
  3826. * @dev: The device for which the DMA address was created
  3827. * @addr: The DMA address
  3828. * @size: The size of the region in bytes
  3829. * @direction: The direction of the DMA
  3830. */
  3831. static inline void ib_dma_unmap_page(struct ib_device *dev,
  3832. u64 addr, size_t size,
  3833. enum dma_data_direction direction)
  3834. {
  3835. if (!ib_uses_virt_dma(dev))
  3836. dma_unmap_page(dev->dma_device, addr, size, direction);
  3837. }
  3838. /**
  3839. * ib_dma_map_bvec - Map a bio_vec to DMA address
  3840. * @dev: The device for which the dma_addr is to be created
  3841. * @bvec: The bio_vec to map
  3842. * @direction: The direction of the DMA
  3843. *
  3844. * Returns a DMA address for the bio_vec. The caller must check the
  3845. * result with ib_dma_mapping_error() before use; a failed mapping
  3846. * must not be passed to ib_dma_unmap_bvec().
  3847. *
  3848. * For software RDMA devices (rxe, siw), returns a virtual address
  3849. * and no actual DMA mapping occurs.
  3850. */
  3851. static inline u64 ib_dma_map_bvec(struct ib_device *dev,
  3852. struct bio_vec *bvec,
  3853. enum dma_data_direction direction)
  3854. {
  3855. if (ib_uses_virt_dma(dev))
  3856. return (uintptr_t)bvec_virt(bvec);
  3857. return dma_map_phys(dev->dma_device, bvec_phys(bvec),
  3858. bvec->bv_len, direction, 0);
  3859. }
  3860. /**
  3861. * ib_dma_unmap_bvec - Unmap a bio_vec DMA mapping
  3862. * @dev: The device for which the DMA address was created
  3863. * @addr: The DMA address returned by ib_dma_map_bvec()
  3864. * @size: The size of the region in bytes
  3865. * @direction: The direction of the DMA
  3866. *
  3867. * Releases a DMA mapping created by ib_dma_map_bvec(). For software
  3868. * RDMA devices this is a no-op since no actual mapping occurred.
  3869. */
  3870. static inline void ib_dma_unmap_bvec(struct ib_device *dev,
  3871. u64 addr, size_t size,
  3872. enum dma_data_direction direction)
  3873. {
  3874. if (!ib_uses_virt_dma(dev))
  3875. dma_unmap_phys(dev->dma_device, addr, size, direction, 0);
  3876. }
  3877. int ib_dma_virt_map_sg(struct ib_device *dev, struct scatterlist *sg, int nents);
  3878. static inline int ib_dma_map_sg_attrs(struct ib_device *dev,
  3879. struct scatterlist *sg, int nents,
  3880. enum dma_data_direction direction,
  3881. unsigned long dma_attrs)
  3882. {
  3883. if (ib_uses_virt_dma(dev))
  3884. return ib_dma_virt_map_sg(dev, sg, nents);
  3885. return dma_map_sg_attrs(dev->dma_device, sg, nents, direction,
  3886. dma_attrs);
  3887. }
  3888. static inline void ib_dma_unmap_sg_attrs(struct ib_device *dev,
  3889. struct scatterlist *sg, int nents,
  3890. enum dma_data_direction direction,
  3891. unsigned long dma_attrs)
  3892. {
  3893. if (!ib_uses_virt_dma(dev))
  3894. dma_unmap_sg_attrs(dev->dma_device, sg, nents, direction,
  3895. dma_attrs);
  3896. }
  3897. /**
  3898. * ib_dma_map_sgtable_attrs - Map a scatter/gather table to DMA addresses
  3899. * @dev: The device for which the DMA addresses are to be created
  3900. * @sgt: The sg_table object describing the buffer
  3901. * @direction: The direction of the DMA
  3902. * @dma_attrs: Optional DMA attributes for the map operation
  3903. */
  3904. static inline int ib_dma_map_sgtable_attrs(struct ib_device *dev,
  3905. struct sg_table *sgt,
  3906. enum dma_data_direction direction,
  3907. unsigned long dma_attrs)
  3908. {
  3909. int nents;
  3910. if (ib_uses_virt_dma(dev)) {
  3911. nents = ib_dma_virt_map_sg(dev, sgt->sgl, sgt->orig_nents);
  3912. if (!nents)
  3913. return -EIO;
  3914. sgt->nents = nents;
  3915. return 0;
  3916. }
  3917. return dma_map_sgtable(dev->dma_device, sgt, direction, dma_attrs);
  3918. }
  3919. static inline void ib_dma_unmap_sgtable_attrs(struct ib_device *dev,
  3920. struct sg_table *sgt,
  3921. enum dma_data_direction direction,
  3922. unsigned long dma_attrs)
  3923. {
  3924. if (!ib_uses_virt_dma(dev))
  3925. dma_unmap_sgtable(dev->dma_device, sgt, direction, dma_attrs);
  3926. }
  3927. /**
  3928. * ib_dma_map_sg - Map a scatter/gather list to DMA addresses
  3929. * @dev: The device for which the DMA addresses are to be created
  3930. * @sg: The array of scatter/gather entries
  3931. * @nents: The number of scatter/gather entries
  3932. * @direction: The direction of the DMA
  3933. */
  3934. static inline int ib_dma_map_sg(struct ib_device *dev,
  3935. struct scatterlist *sg, int nents,
  3936. enum dma_data_direction direction)
  3937. {
  3938. return ib_dma_map_sg_attrs(dev, sg, nents, direction, 0);
  3939. }
  3940. /**
  3941. * ib_dma_unmap_sg - Unmap a scatter/gather list of DMA addresses
  3942. * @dev: The device for which the DMA addresses were created
  3943. * @sg: The array of scatter/gather entries
  3944. * @nents: The number of scatter/gather entries
  3945. * @direction: The direction of the DMA
  3946. */
  3947. static inline void ib_dma_unmap_sg(struct ib_device *dev,
  3948. struct scatterlist *sg, int nents,
  3949. enum dma_data_direction direction)
  3950. {
  3951. ib_dma_unmap_sg_attrs(dev, sg, nents, direction, 0);
  3952. }
  3953. /**
  3954. * ib_dma_max_seg_size - Return the size limit of a single DMA transfer
  3955. * @dev: The device to query
  3956. *
  3957. * The returned value represents a size in bytes.
  3958. */
  3959. static inline unsigned int ib_dma_max_seg_size(struct ib_device *dev)
  3960. {
  3961. if (ib_uses_virt_dma(dev))
  3962. return UINT_MAX;
  3963. return dma_get_max_seg_size(dev->dma_device);
  3964. }
  3965. /**
  3966. * ib_dma_sync_single_for_cpu - Prepare DMA region to be accessed by CPU
  3967. * @dev: The device for which the DMA address was created
  3968. * @addr: The DMA address
  3969. * @size: The size of the region in bytes
  3970. * @dir: The direction of the DMA
  3971. */
  3972. static inline void ib_dma_sync_single_for_cpu(struct ib_device *dev,
  3973. u64 addr,
  3974. size_t size,
  3975. enum dma_data_direction dir)
  3976. {
  3977. if (!ib_uses_virt_dma(dev))
  3978. dma_sync_single_for_cpu(dev->dma_device, addr, size, dir);
  3979. }
  3980. /**
  3981. * ib_dma_sync_single_for_device - Prepare DMA region to be accessed by device
  3982. * @dev: The device for which the DMA address was created
  3983. * @addr: The DMA address
  3984. * @size: The size of the region in bytes
  3985. * @dir: The direction of the DMA
  3986. */
  3987. static inline void ib_dma_sync_single_for_device(struct ib_device *dev,
  3988. u64 addr,
  3989. size_t size,
  3990. enum dma_data_direction dir)
  3991. {
  3992. if (!ib_uses_virt_dma(dev))
  3993. dma_sync_single_for_device(dev->dma_device, addr, size, dir);
  3994. }
  3995. /* ib_reg_user_mr - register a memory region for virtual addresses from kernel
  3996. * space. This function should be called when 'current' is the owning MM.
  3997. */
  3998. struct ib_mr *ib_reg_user_mr(struct ib_pd *pd, u64 start, u64 length,
  3999. u64 virt_addr, int mr_access_flags);
  4000. /* ib_advise_mr - give an advice about an address range in a memory region */
  4001. int ib_advise_mr(struct ib_pd *pd, enum ib_uverbs_advise_mr_advice advice,
  4002. u32 flags, struct ib_sge *sg_list, u32 num_sge);
  4003. /**
  4004. * ib_dereg_mr_user - Deregisters a memory region and removes it from the
  4005. * HCA translation table.
  4006. * @mr: The memory region to deregister.
  4007. * @udata: Valid user data or NULL for kernel object
  4008. *
  4009. * This function can fail, if the memory region has memory windows bound to it.
  4010. */
  4011. int ib_dereg_mr_user(struct ib_mr *mr, struct ib_udata *udata);
  4012. /**
  4013. * ib_dereg_mr - Deregisters a kernel memory region and removes it from the
  4014. * HCA translation table.
  4015. * @mr: The memory region to deregister.
  4016. *
  4017. * This function can fail, if the memory region has memory windows bound to it.
  4018. *
  4019. * NOTE: for user mr use ib_dereg_mr_user with valid udata!
  4020. */
  4021. static inline int ib_dereg_mr(struct ib_mr *mr)
  4022. {
  4023. return ib_dereg_mr_user(mr, NULL);
  4024. }
  4025. struct ib_mr *ib_alloc_mr(struct ib_pd *pd, enum ib_mr_type mr_type,
  4026. u32 max_num_sg);
  4027. struct ib_mr *ib_alloc_mr_integrity(struct ib_pd *pd,
  4028. u32 max_num_data_sg,
  4029. u32 max_num_meta_sg);
  4030. /**
  4031. * ib_update_fast_reg_key - updates the key portion of the fast_reg MR
  4032. * R_Key and L_Key.
  4033. * @mr: struct ib_mr pointer to be updated.
  4034. * @newkey: new key to be used.
  4035. */
  4036. static inline void ib_update_fast_reg_key(struct ib_mr *mr, u8 newkey)
  4037. {
  4038. mr->lkey = (mr->lkey & 0xffffff00) | newkey;
  4039. mr->rkey = (mr->rkey & 0xffffff00) | newkey;
  4040. }
  4041. /**
  4042. * ib_inc_rkey - increments the key portion of the given rkey. Can be used
  4043. * for calculating a new rkey for type 2 memory windows.
  4044. * @rkey: the rkey to increment.
  4045. */
  4046. static inline u32 ib_inc_rkey(u32 rkey)
  4047. {
  4048. const u32 mask = 0x000000ff;
  4049. return ((rkey + 1) & mask) | (rkey & ~mask);
  4050. }
  4051. /**
  4052. * ib_attach_mcast - Attaches the specified QP to a multicast group.
  4053. * @qp: QP to attach to the multicast group. The QP must be type
  4054. * IB_QPT_UD.
  4055. * @gid: Multicast group GID.
  4056. * @lid: Multicast group LID in host byte order.
  4057. *
  4058. * In order to send and receive multicast packets, subnet
  4059. * administration must have created the multicast group and configured
  4060. * the fabric appropriately. The port associated with the specified
  4061. * QP must also be a member of the multicast group.
  4062. */
  4063. int ib_attach_mcast(struct ib_qp *qp, union ib_gid *gid, u16 lid);
  4064. /**
  4065. * ib_detach_mcast - Detaches the specified QP from a multicast group.
  4066. * @qp: QP to detach from the multicast group.
  4067. * @gid: Multicast group GID.
  4068. * @lid: Multicast group LID in host byte order.
  4069. */
  4070. int ib_detach_mcast(struct ib_qp *qp, union ib_gid *gid, u16 lid);
  4071. struct ib_xrcd *ib_alloc_xrcd_user(struct ib_device *device,
  4072. struct inode *inode, struct ib_udata *udata);
  4073. int ib_dealloc_xrcd_user(struct ib_xrcd *xrcd, struct ib_udata *udata);
  4074. static inline int ib_check_mr_access(struct ib_device *ib_dev,
  4075. unsigned int flags)
  4076. {
  4077. u64 device_cap = ib_dev->attrs.device_cap_flags;
  4078. /*
  4079. * Local write permission is required if remote write or
  4080. * remote atomic permission is also requested.
  4081. */
  4082. if (flags & (IB_ACCESS_REMOTE_ATOMIC | IB_ACCESS_REMOTE_WRITE) &&
  4083. !(flags & IB_ACCESS_LOCAL_WRITE))
  4084. return -EINVAL;
  4085. if (flags & ~IB_ACCESS_SUPPORTED)
  4086. return -EINVAL;
  4087. if (flags & IB_ACCESS_ON_DEMAND &&
  4088. !(ib_dev->attrs.kernel_cap_flags & IBK_ON_DEMAND_PAGING))
  4089. return -EOPNOTSUPP;
  4090. if ((flags & IB_ACCESS_FLUSH_GLOBAL &&
  4091. !(device_cap & IB_DEVICE_FLUSH_GLOBAL)) ||
  4092. (flags & IB_ACCESS_FLUSH_PERSISTENT &&
  4093. !(device_cap & IB_DEVICE_FLUSH_PERSISTENT)))
  4094. return -EOPNOTSUPP;
  4095. return 0;
  4096. }
  4097. static inline bool ib_access_writable(int access_flags)
  4098. {
  4099. /*
  4100. * We have writable memory backing the MR if any of the following
  4101. * access flags are set. "Local write" and "remote write" obviously
  4102. * require write access. "Remote atomic" can do things like fetch and
  4103. * add, which will modify memory, and "MW bind" can change permissions
  4104. * by binding a window.
  4105. */
  4106. return access_flags &
  4107. (IB_ACCESS_LOCAL_WRITE | IB_ACCESS_REMOTE_WRITE |
  4108. IB_ACCESS_REMOTE_ATOMIC | IB_ACCESS_MW_BIND);
  4109. }
  4110. /**
  4111. * ib_check_mr_status: lightweight check of MR status.
  4112. * This routine may provide status checks on a selected
  4113. * ib_mr. first use is for signature status check.
  4114. *
  4115. * @mr: A memory region.
  4116. * @check_mask: Bitmask of which checks to perform from
  4117. * ib_mr_status_check enumeration.
  4118. * @mr_status: The container of relevant status checks.
  4119. * failed checks will be indicated in the status bitmask
  4120. * and the relevant info shall be in the error item.
  4121. */
  4122. int ib_check_mr_status(struct ib_mr *mr, u32 check_mask,
  4123. struct ib_mr_status *mr_status);
  4124. /**
  4125. * ib_device_try_get: Hold a registration lock
  4126. * @dev: The device to lock
  4127. *
  4128. * A device under an active registration lock cannot become unregistered. It
  4129. * is only possible to obtain a registration lock on a device that is fully
  4130. * registered, otherwise this function returns false.
  4131. *
  4132. * The registration lock is only necessary for actions which require the
  4133. * device to still be registered. Uses that only require the device pointer to
  4134. * be valid should use get_device(&ibdev->dev) to hold the memory.
  4135. *
  4136. */
  4137. static inline bool ib_device_try_get(struct ib_device *dev)
  4138. {
  4139. return refcount_inc_not_zero(&dev->refcount);
  4140. }
  4141. void ib_device_put(struct ib_device *device);
  4142. struct ib_device *ib_device_get_by_netdev(struct net_device *ndev,
  4143. enum rdma_driver_id driver_id);
  4144. struct net_device *ib_get_net_dev_by_params(struct ib_device *dev, u32 port,
  4145. u16 pkey, const union ib_gid *gid,
  4146. const struct sockaddr *addr);
  4147. int ib_device_set_netdev(struct ib_device *ib_dev, struct net_device *ndev,
  4148. unsigned int port);
  4149. struct net_device *ib_device_get_netdev(struct ib_device *ib_dev,
  4150. u32 port);
  4151. int ib_query_netdev_port(struct ib_device *ibdev, struct net_device *ndev,
  4152. u32 *port);
  4153. static inline enum ib_port_state ib_get_curr_port_state(struct net_device *net_dev)
  4154. {
  4155. return (netif_running(net_dev) && netif_carrier_ok(net_dev)) ?
  4156. IB_PORT_ACTIVE : IB_PORT_DOWN;
  4157. }
  4158. void ib_dispatch_port_state_event(struct ib_device *ibdev,
  4159. struct net_device *ndev);
  4160. struct ib_wq *ib_create_wq(struct ib_pd *pd,
  4161. struct ib_wq_init_attr *init_attr);
  4162. int ib_destroy_wq_user(struct ib_wq *wq, struct ib_udata *udata);
  4163. int ib_map_mr_sg(struct ib_mr *mr, struct scatterlist *sg, int sg_nents,
  4164. unsigned int *sg_offset, unsigned int page_size);
  4165. int ib_map_mr_sg_pi(struct ib_mr *mr, struct scatterlist *data_sg,
  4166. int data_sg_nents, unsigned int *data_sg_offset,
  4167. struct scatterlist *meta_sg, int meta_sg_nents,
  4168. unsigned int *meta_sg_offset, unsigned int page_size);
  4169. static inline int
  4170. ib_map_mr_sg_zbva(struct ib_mr *mr, struct scatterlist *sg, int sg_nents,
  4171. unsigned int *sg_offset, unsigned int page_size)
  4172. {
  4173. int n;
  4174. n = ib_map_mr_sg(mr, sg, sg_nents, sg_offset, page_size);
  4175. mr->iova = 0;
  4176. return n;
  4177. }
  4178. int ib_sg_to_pages(struct ib_mr *mr, struct scatterlist *sgl, int sg_nents,
  4179. unsigned int *sg_offset, int (*set_page)(struct ib_mr *, u64));
  4180. void ib_drain_rq(struct ib_qp *qp);
  4181. void ib_drain_sq(struct ib_qp *qp);
  4182. void ib_drain_qp(struct ib_qp *qp);
  4183. int ib_get_eth_speed(struct ib_device *dev, u32 port_num, u16 *speed,
  4184. u8 *width);
  4185. static inline u8 *rdma_ah_retrieve_dmac(struct rdma_ah_attr *attr)
  4186. {
  4187. if (attr->type == RDMA_AH_ATTR_TYPE_ROCE)
  4188. return attr->roce.dmac;
  4189. return NULL;
  4190. }
  4191. static inline void rdma_ah_set_dlid(struct rdma_ah_attr *attr, u32 dlid)
  4192. {
  4193. if (attr->type == RDMA_AH_ATTR_TYPE_IB)
  4194. attr->ib.dlid = (u16)dlid;
  4195. else if (attr->type == RDMA_AH_ATTR_TYPE_OPA)
  4196. attr->opa.dlid = dlid;
  4197. }
  4198. static inline u32 rdma_ah_get_dlid(const struct rdma_ah_attr *attr)
  4199. {
  4200. if (attr->type == RDMA_AH_ATTR_TYPE_IB)
  4201. return attr->ib.dlid;
  4202. else if (attr->type == RDMA_AH_ATTR_TYPE_OPA)
  4203. return attr->opa.dlid;
  4204. return 0;
  4205. }
  4206. static inline void rdma_ah_set_sl(struct rdma_ah_attr *attr, u8 sl)
  4207. {
  4208. attr->sl = sl;
  4209. }
  4210. static inline u8 rdma_ah_get_sl(const struct rdma_ah_attr *attr)
  4211. {
  4212. return attr->sl;
  4213. }
  4214. static inline void rdma_ah_set_path_bits(struct rdma_ah_attr *attr,
  4215. u8 src_path_bits)
  4216. {
  4217. if (attr->type == RDMA_AH_ATTR_TYPE_IB)
  4218. attr->ib.src_path_bits = src_path_bits;
  4219. else if (attr->type == RDMA_AH_ATTR_TYPE_OPA)
  4220. attr->opa.src_path_bits = src_path_bits;
  4221. }
  4222. static inline u8 rdma_ah_get_path_bits(const struct rdma_ah_attr *attr)
  4223. {
  4224. if (attr->type == RDMA_AH_ATTR_TYPE_IB)
  4225. return attr->ib.src_path_bits;
  4226. else if (attr->type == RDMA_AH_ATTR_TYPE_OPA)
  4227. return attr->opa.src_path_bits;
  4228. return 0;
  4229. }
  4230. static inline void rdma_ah_set_make_grd(struct rdma_ah_attr *attr,
  4231. bool make_grd)
  4232. {
  4233. if (attr->type == RDMA_AH_ATTR_TYPE_OPA)
  4234. attr->opa.make_grd = make_grd;
  4235. }
  4236. static inline bool rdma_ah_get_make_grd(const struct rdma_ah_attr *attr)
  4237. {
  4238. if (attr->type == RDMA_AH_ATTR_TYPE_OPA)
  4239. return attr->opa.make_grd;
  4240. return false;
  4241. }
  4242. static inline void rdma_ah_set_port_num(struct rdma_ah_attr *attr, u32 port_num)
  4243. {
  4244. attr->port_num = port_num;
  4245. }
  4246. static inline u32 rdma_ah_get_port_num(const struct rdma_ah_attr *attr)
  4247. {
  4248. return attr->port_num;
  4249. }
  4250. static inline void rdma_ah_set_static_rate(struct rdma_ah_attr *attr,
  4251. u8 static_rate)
  4252. {
  4253. attr->static_rate = static_rate;
  4254. }
  4255. static inline u8 rdma_ah_get_static_rate(const struct rdma_ah_attr *attr)
  4256. {
  4257. return attr->static_rate;
  4258. }
  4259. static inline void rdma_ah_set_ah_flags(struct rdma_ah_attr *attr,
  4260. enum ib_ah_flags flag)
  4261. {
  4262. attr->ah_flags = flag;
  4263. }
  4264. static inline enum ib_ah_flags
  4265. rdma_ah_get_ah_flags(const struct rdma_ah_attr *attr)
  4266. {
  4267. return attr->ah_flags;
  4268. }
  4269. static inline const struct ib_global_route
  4270. *rdma_ah_read_grh(const struct rdma_ah_attr *attr)
  4271. {
  4272. return &attr->grh;
  4273. }
  4274. /*To retrieve and modify the grh */
  4275. static inline struct ib_global_route
  4276. *rdma_ah_retrieve_grh(struct rdma_ah_attr *attr)
  4277. {
  4278. return &attr->grh;
  4279. }
  4280. static inline void rdma_ah_set_dgid_raw(struct rdma_ah_attr *attr, void *dgid)
  4281. {
  4282. struct ib_global_route *grh = rdma_ah_retrieve_grh(attr);
  4283. memcpy(grh->dgid.raw, dgid, sizeof(grh->dgid));
  4284. }
  4285. static inline void rdma_ah_set_subnet_prefix(struct rdma_ah_attr *attr,
  4286. __be64 prefix)
  4287. {
  4288. struct ib_global_route *grh = rdma_ah_retrieve_grh(attr);
  4289. grh->dgid.global.subnet_prefix = prefix;
  4290. }
  4291. static inline void rdma_ah_set_interface_id(struct rdma_ah_attr *attr,
  4292. __be64 if_id)
  4293. {
  4294. struct ib_global_route *grh = rdma_ah_retrieve_grh(attr);
  4295. grh->dgid.global.interface_id = if_id;
  4296. }
  4297. static inline void rdma_ah_set_grh(struct rdma_ah_attr *attr,
  4298. union ib_gid *dgid, u32 flow_label,
  4299. u8 sgid_index, u8 hop_limit,
  4300. u8 traffic_class)
  4301. {
  4302. struct ib_global_route *grh = rdma_ah_retrieve_grh(attr);
  4303. attr->ah_flags = IB_AH_GRH;
  4304. if (dgid)
  4305. grh->dgid = *dgid;
  4306. grh->flow_label = flow_label;
  4307. grh->sgid_index = sgid_index;
  4308. grh->hop_limit = hop_limit;
  4309. grh->traffic_class = traffic_class;
  4310. grh->sgid_attr = NULL;
  4311. }
  4312. void rdma_destroy_ah_attr(struct rdma_ah_attr *ah_attr);
  4313. void rdma_move_grh_sgid_attr(struct rdma_ah_attr *attr, union ib_gid *dgid,
  4314. u32 flow_label, u8 hop_limit, u8 traffic_class,
  4315. const struct ib_gid_attr *sgid_attr);
  4316. void rdma_copy_ah_attr(struct rdma_ah_attr *dest,
  4317. const struct rdma_ah_attr *src);
  4318. void rdma_replace_ah_attr(struct rdma_ah_attr *old,
  4319. const struct rdma_ah_attr *new);
  4320. void rdma_move_ah_attr(struct rdma_ah_attr *dest, struct rdma_ah_attr *src);
  4321. /**
  4322. * rdma_ah_find_type - Return address handle type.
  4323. *
  4324. * @dev: Device to be checked
  4325. * @port_num: Port number
  4326. */
  4327. static inline enum rdma_ah_attr_type rdma_ah_find_type(struct ib_device *dev,
  4328. u32 port_num)
  4329. {
  4330. if (rdma_protocol_roce(dev, port_num))
  4331. return RDMA_AH_ATTR_TYPE_ROCE;
  4332. if (rdma_protocol_ib(dev, port_num)) {
  4333. if (rdma_cap_opa_ah(dev, port_num))
  4334. return RDMA_AH_ATTR_TYPE_OPA;
  4335. return RDMA_AH_ATTR_TYPE_IB;
  4336. }
  4337. if (dev->type == RDMA_DEVICE_TYPE_SMI)
  4338. return RDMA_AH_ATTR_TYPE_IB;
  4339. return RDMA_AH_ATTR_TYPE_UNDEFINED;
  4340. }
  4341. /**
  4342. * ib_lid_cpu16 - Return lid in 16bit CPU encoding.
  4343. * In the current implementation the only way to
  4344. * get the 32bit lid is from other sources for OPA.
  4345. * For IB, lids will always be 16bits so cast the
  4346. * value accordingly.
  4347. *
  4348. * @lid: A 32bit LID
  4349. */
  4350. static inline u16 ib_lid_cpu16(u32 lid)
  4351. {
  4352. WARN_ON_ONCE(lid & 0xFFFF0000);
  4353. return (u16)lid;
  4354. }
  4355. /**
  4356. * ib_lid_be16 - Return lid in 16bit BE encoding.
  4357. *
  4358. * @lid: A 32bit LID
  4359. */
  4360. static inline __be16 ib_lid_be16(u32 lid)
  4361. {
  4362. WARN_ON_ONCE(lid & 0xFFFF0000);
  4363. return cpu_to_be16((u16)lid);
  4364. }
  4365. /**
  4366. * ib_get_vector_affinity - Get the affinity mappings of a given completion
  4367. * vector
  4368. * @device: the rdma device
  4369. * @comp_vector: index of completion vector
  4370. *
  4371. * Returns NULL on failure, otherwise a corresponding cpu map of the
  4372. * completion vector (returns all-cpus map if the device driver doesn't
  4373. * implement get_vector_affinity).
  4374. */
  4375. static inline const struct cpumask *
  4376. ib_get_vector_affinity(struct ib_device *device, int comp_vector)
  4377. {
  4378. if (comp_vector < 0 || comp_vector >= device->num_comp_vectors ||
  4379. !device->ops.get_vector_affinity)
  4380. return NULL;
  4381. return device->ops.get_vector_affinity(device, comp_vector);
  4382. }
  4383. /**
  4384. * rdma_roce_rescan_device - Rescan all of the network devices in the system
  4385. * and add their gids, as needed, to the relevant RoCE devices.
  4386. *
  4387. * @ibdev: the rdma device
  4388. */
  4389. void rdma_roce_rescan_device(struct ib_device *ibdev);
  4390. void rdma_roce_rescan_port(struct ib_device *ib_dev, u32 port);
  4391. void roce_del_all_netdev_gids(struct ib_device *ib_dev,
  4392. u32 port, struct net_device *ndev);
  4393. struct ib_ucontext *ib_uverbs_get_ucontext_file(struct ib_uverbs_file *ufile);
  4394. #if IS_ENABLED(CONFIG_INFINIBAND_USER_ACCESS)
  4395. int uverbs_destroy_def_handler(struct uverbs_attr_bundle *attrs);
  4396. bool rdma_uattrs_has_raw_cap(const struct uverbs_attr_bundle *attrs);
  4397. #else
  4398. static inline int uverbs_destroy_def_handler(struct uverbs_attr_bundle *attrs)
  4399. {
  4400. return 0;
  4401. }
  4402. static inline bool
  4403. rdma_uattrs_has_raw_cap(const struct uverbs_attr_bundle *attrs)
  4404. {
  4405. return false;
  4406. }
  4407. #endif
  4408. struct net_device *rdma_alloc_netdev(struct ib_device *device, u32 port_num,
  4409. enum rdma_netdev_t type, const char *name,
  4410. unsigned char name_assign_type,
  4411. void (*setup)(struct net_device *));
  4412. int rdma_init_netdev(struct ib_device *device, u32 port_num,
  4413. enum rdma_netdev_t type, const char *name,
  4414. unsigned char name_assign_type,
  4415. void (*setup)(struct net_device *),
  4416. struct net_device *netdev);
  4417. /**
  4418. * rdma_device_to_ibdev - Get ib_device pointer from device pointer
  4419. *
  4420. * @device: device pointer for which ib_device pointer to retrieve
  4421. *
  4422. * rdma_device_to_ibdev() retrieves ib_device pointer from device.
  4423. *
  4424. */
  4425. static inline struct ib_device *rdma_device_to_ibdev(struct device *device)
  4426. {
  4427. struct ib_core_device *coredev =
  4428. container_of(device, struct ib_core_device, dev);
  4429. return coredev->owner;
  4430. }
  4431. /**
  4432. * ibdev_to_node - return the NUMA node for a given ib_device
  4433. * @ibdev: device to get the NUMA node for.
  4434. */
  4435. static inline int ibdev_to_node(struct ib_device *ibdev)
  4436. {
  4437. struct device *parent = ibdev->dev.parent;
  4438. if (!parent)
  4439. return NUMA_NO_NODE;
  4440. return dev_to_node(parent);
  4441. }
  4442. /**
  4443. * rdma_device_to_drv_device - Helper macro to reach back to driver's
  4444. * ib_device holder structure from device pointer.
  4445. *
  4446. * NOTE: New drivers should not make use of this API; This API is only for
  4447. * existing drivers who have exposed sysfs entries using
  4448. * ops->device_group.
  4449. */
  4450. #define rdma_device_to_drv_device(dev, drv_dev_struct, ibdev_member) \
  4451. container_of(rdma_device_to_ibdev(dev), drv_dev_struct, ibdev_member)
  4452. bool rdma_dev_access_netns(const struct ib_device *device,
  4453. const struct net *net);
  4454. bool rdma_dev_has_raw_cap(const struct ib_device *dev);
  4455. static inline struct net *rdma_dev_net(struct ib_device *device)
  4456. {
  4457. return read_pnet(&device->coredev.rdma_net);
  4458. }
  4459. #define IB_ROCE_UDP_ENCAP_VALID_PORT_MIN (0xC000)
  4460. #define IB_ROCE_UDP_ENCAP_VALID_PORT_MAX (0xFFFF)
  4461. #define IB_GRH_FLOWLABEL_MASK (0x000FFFFF)
  4462. /**
  4463. * rdma_flow_label_to_udp_sport - generate a RoCE v2 UDP src port value based
  4464. * on the flow_label
  4465. * @fl: flow_label value
  4466. *
  4467. * This function will convert the 20 bit flow_label input to a valid RoCE v2
  4468. * UDP src port 14 bit value. All RoCE V2 drivers should use this same
  4469. * convention.
  4470. */
  4471. static inline u16 rdma_flow_label_to_udp_sport(u32 fl)
  4472. {
  4473. u32 fl_low = fl & 0x03fff, fl_high = fl & 0xFC000;
  4474. fl_low ^= fl_high >> 14;
  4475. return (u16)(fl_low | IB_ROCE_UDP_ENCAP_VALID_PORT_MIN);
  4476. }
  4477. /**
  4478. * rdma_calc_flow_label - generate a RDMA symmetric flow label value based on
  4479. * local and remote qpn values
  4480. *
  4481. * This function folded the multiplication results of two qpns, 24 bit each,
  4482. * fields, and converts it to a 20 bit results.
  4483. *
  4484. * This function will create symmetric flow_label value based on the local
  4485. * and remote qpn values. this will allow both the requester and responder
  4486. * to calculate the same flow_label for a given connection.
  4487. *
  4488. * This helper function should be used by driver in case the upper layer
  4489. * provide a zero flow_label value. This is to improve entropy of RDMA
  4490. * traffic in the network.
  4491. */
  4492. static inline u32 rdma_calc_flow_label(u32 lqpn, u32 rqpn)
  4493. {
  4494. u64 v = (u64)lqpn * rqpn;
  4495. v ^= v >> 20;
  4496. v ^= v >> 40;
  4497. return (u32)(v & IB_GRH_FLOWLABEL_MASK);
  4498. }
  4499. /**
  4500. * rdma_get_udp_sport - Calculate and set UDP source port based on the flow
  4501. * label. If flow label is not defined in GRH then
  4502. * calculate it based on lqpn/rqpn.
  4503. *
  4504. * @fl: flow label from GRH
  4505. * @lqpn: local qp number
  4506. * @rqpn: remote qp number
  4507. */
  4508. static inline u16 rdma_get_udp_sport(u32 fl, u32 lqpn, u32 rqpn)
  4509. {
  4510. if (!fl)
  4511. fl = rdma_calc_flow_label(lqpn, rqpn);
  4512. return rdma_flow_label_to_udp_sport(fl);
  4513. }
  4514. const struct ib_port_immutable*
  4515. ib_port_immutable_read(struct ib_device *dev, unsigned int port);
  4516. /** ib_add_sub_device - Add a sub IB device on an existing one
  4517. *
  4518. * @parent: The IB device that needs to add a sub device
  4519. * @type: The type of the new sub device
  4520. * @name: The name of the new sub device
  4521. *
  4522. *
  4523. * Return 0 on success, an error code otherwise
  4524. */
  4525. int ib_add_sub_device(struct ib_device *parent,
  4526. enum rdma_nl_dev_type type,
  4527. const char *name);
  4528. /** ib_del_sub_device_and_put - Delect an IB sub device while holding a 'get'
  4529. *
  4530. * @sub: The sub device that is going to be deleted
  4531. *
  4532. * Return 0 on success, an error code otherwise
  4533. */
  4534. int ib_del_sub_device_and_put(struct ib_device *sub);
  4535. static inline void ib_mark_name_assigned_by_user(struct ib_device *ibdev)
  4536. {
  4537. ibdev->name_assign_type = RDMA_NAME_ASSIGN_TYPE_USER;
  4538. }
  4539. #endif /* IB_VERBS_H */