uverbs_cmd.c 104 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146
  1. /*
  2. * Copyright (c) 2005 Topspin Communications. All rights reserved.
  3. * Copyright (c) 2005, 2006, 2007 Cisco Systems. All rights reserved.
  4. * Copyright (c) 2005 PathScale, Inc. All rights reserved.
  5. * Copyright (c) 2006 Mellanox Technologies. All rights reserved.
  6. *
  7. * This software is available to you under a choice of one of two
  8. * licenses. You may choose to be licensed under the terms of the GNU
  9. * General Public License (GPL) Version 2, available from the file
  10. * COPYING in the main directory of this source tree, or the
  11. * OpenIB.org BSD license below:
  12. *
  13. * Redistribution and use in source and binary forms, with or
  14. * without modification, are permitted provided that the following
  15. * conditions are met:
  16. *
  17. * - Redistributions of source code must retain the above
  18. * copyright notice, this list of conditions and the following
  19. * disclaimer.
  20. *
  21. * - Redistributions in binary form must reproduce the above
  22. * copyright notice, this list of conditions and the following
  23. * disclaimer in the documentation and/or other materials
  24. * provided with the distribution.
  25. *
  26. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  27. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  28. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  29. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  30. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  31. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  32. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  33. * SOFTWARE.
  34. */
  35. #include <linux/file.h>
  36. #include <linux/fs.h>
  37. #include <linux/slab.h>
  38. #include <linux/sched.h>
  39. #include <linux/uaccess.h>
  40. #include <rdma/uverbs_types.h>
  41. #include <rdma/uverbs_std_types.h>
  42. #include "rdma_core.h"
  43. #include "uverbs.h"
  44. #include "core_priv.h"
  45. static struct ib_uverbs_completion_event_file *
  46. _ib_uverbs_lookup_comp_file(s32 fd, struct ib_uverbs_file *ufile)
  47. {
  48. struct ib_uobject *uobj = ufd_get_read(UVERBS_OBJECT_COMP_CHANNEL,
  49. fd, ufile);
  50. if (IS_ERR(uobj))
  51. return (void *)uobj;
  52. uverbs_uobject_get(uobj);
  53. uobj_put_read(uobj);
  54. return container_of(uobj, struct ib_uverbs_completion_event_file,
  55. uobj);
  56. }
  57. #define ib_uverbs_lookup_comp_file(_fd, _ufile) \
  58. _ib_uverbs_lookup_comp_file((_fd)*typecheck(s32, _fd), _ufile)
  59. ssize_t ib_uverbs_get_context(struct ib_uverbs_file *file,
  60. const char __user *buf,
  61. int in_len, int out_len)
  62. {
  63. struct ib_uverbs_get_context cmd;
  64. struct ib_uverbs_get_context_resp resp;
  65. struct ib_udata udata;
  66. struct ib_ucontext *ucontext;
  67. struct file *filp;
  68. struct ib_rdmacg_object cg_obj;
  69. struct ib_device *ib_dev;
  70. int ret;
  71. if (out_len < sizeof resp)
  72. return -ENOSPC;
  73. if (copy_from_user(&cmd, buf, sizeof cmd))
  74. return -EFAULT;
  75. mutex_lock(&file->ucontext_lock);
  76. ib_dev = srcu_dereference(file->device->ib_dev,
  77. &file->device->disassociate_srcu);
  78. if (!ib_dev) {
  79. ret = -EIO;
  80. goto err;
  81. }
  82. if (file->ucontext) {
  83. ret = -EINVAL;
  84. goto err;
  85. }
  86. ib_uverbs_init_udata(&udata, buf + sizeof(cmd),
  87. u64_to_user_ptr(cmd.response) + sizeof(resp),
  88. in_len - sizeof(cmd) - sizeof(struct ib_uverbs_cmd_hdr),
  89. out_len - sizeof(resp));
  90. ret = ib_rdmacg_try_charge(&cg_obj, ib_dev, RDMACG_RESOURCE_HCA_HANDLE);
  91. if (ret)
  92. goto err;
  93. ucontext = ib_dev->alloc_ucontext(ib_dev, &udata);
  94. if (IS_ERR(ucontext)) {
  95. ret = PTR_ERR(ucontext);
  96. goto err_alloc;
  97. }
  98. ucontext->device = ib_dev;
  99. ucontext->cg_obj = cg_obj;
  100. /* ufile is required when some objects are released */
  101. ucontext->ufile = file;
  102. rcu_read_lock();
  103. ucontext->tgid = get_task_pid(current->group_leader, PIDTYPE_PID);
  104. rcu_read_unlock();
  105. ucontext->closing = 0;
  106. ucontext->cleanup_retryable = false;
  107. #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
  108. ucontext->umem_tree = RB_ROOT_CACHED;
  109. init_rwsem(&ucontext->umem_rwsem);
  110. ucontext->odp_mrs_count = 0;
  111. INIT_LIST_HEAD(&ucontext->no_private_counters);
  112. if (!(ib_dev->attrs.device_cap_flags & IB_DEVICE_ON_DEMAND_PAGING))
  113. ucontext->invalidate_range = NULL;
  114. #endif
  115. resp.num_comp_vectors = file->device->num_comp_vectors;
  116. ret = get_unused_fd_flags(O_CLOEXEC);
  117. if (ret < 0)
  118. goto err_free;
  119. resp.async_fd = ret;
  120. filp = ib_uverbs_alloc_async_event_file(file, ib_dev);
  121. if (IS_ERR(filp)) {
  122. ret = PTR_ERR(filp);
  123. goto err_fd;
  124. }
  125. if (copy_to_user(u64_to_user_ptr(cmd.response), &resp, sizeof resp)) {
  126. ret = -EFAULT;
  127. goto err_file;
  128. }
  129. fd_install(resp.async_fd, filp);
  130. /*
  131. * Make sure that ib_uverbs_get_ucontext() sees the pointer update
  132. * only after all writes to setup the ucontext have completed
  133. */
  134. smp_store_release(&file->ucontext, ucontext);
  135. mutex_unlock(&file->ucontext_lock);
  136. return in_len;
  137. err_file:
  138. ib_uverbs_free_async_event_file(file);
  139. fput(filp);
  140. err_fd:
  141. put_unused_fd(resp.async_fd);
  142. err_free:
  143. put_pid(ucontext->tgid);
  144. ib_dev->dealloc_ucontext(ucontext);
  145. err_alloc:
  146. ib_rdmacg_uncharge(&cg_obj, ib_dev, RDMACG_RESOURCE_HCA_HANDLE);
  147. err:
  148. mutex_unlock(&file->ucontext_lock);
  149. return ret;
  150. }
  151. static void copy_query_dev_fields(struct ib_ucontext *ucontext,
  152. struct ib_uverbs_query_device_resp *resp,
  153. struct ib_device_attr *attr)
  154. {
  155. struct ib_device *ib_dev = ucontext->device;
  156. resp->fw_ver = attr->fw_ver;
  157. resp->node_guid = ib_dev->node_guid;
  158. resp->sys_image_guid = attr->sys_image_guid;
  159. resp->max_mr_size = attr->max_mr_size;
  160. resp->page_size_cap = attr->page_size_cap;
  161. resp->vendor_id = attr->vendor_id;
  162. resp->vendor_part_id = attr->vendor_part_id;
  163. resp->hw_ver = attr->hw_ver;
  164. resp->max_qp = attr->max_qp;
  165. resp->max_qp_wr = attr->max_qp_wr;
  166. resp->device_cap_flags = lower_32_bits(attr->device_cap_flags);
  167. resp->max_sge = min(attr->max_send_sge, attr->max_recv_sge);
  168. resp->max_sge_rd = attr->max_sge_rd;
  169. resp->max_cq = attr->max_cq;
  170. resp->max_cqe = attr->max_cqe;
  171. resp->max_mr = attr->max_mr;
  172. resp->max_pd = attr->max_pd;
  173. resp->max_qp_rd_atom = attr->max_qp_rd_atom;
  174. resp->max_ee_rd_atom = attr->max_ee_rd_atom;
  175. resp->max_res_rd_atom = attr->max_res_rd_atom;
  176. resp->max_qp_init_rd_atom = attr->max_qp_init_rd_atom;
  177. resp->max_ee_init_rd_atom = attr->max_ee_init_rd_atom;
  178. resp->atomic_cap = attr->atomic_cap;
  179. resp->max_ee = attr->max_ee;
  180. resp->max_rdd = attr->max_rdd;
  181. resp->max_mw = attr->max_mw;
  182. resp->max_raw_ipv6_qp = attr->max_raw_ipv6_qp;
  183. resp->max_raw_ethy_qp = attr->max_raw_ethy_qp;
  184. resp->max_mcast_grp = attr->max_mcast_grp;
  185. resp->max_mcast_qp_attach = attr->max_mcast_qp_attach;
  186. resp->max_total_mcast_qp_attach = attr->max_total_mcast_qp_attach;
  187. resp->max_ah = attr->max_ah;
  188. resp->max_fmr = attr->max_fmr;
  189. resp->max_map_per_fmr = attr->max_map_per_fmr;
  190. resp->max_srq = attr->max_srq;
  191. resp->max_srq_wr = attr->max_srq_wr;
  192. resp->max_srq_sge = attr->max_srq_sge;
  193. resp->max_pkeys = attr->max_pkeys;
  194. resp->local_ca_ack_delay = attr->local_ca_ack_delay;
  195. resp->phys_port_cnt = ib_dev->phys_port_cnt;
  196. }
  197. ssize_t ib_uverbs_query_device(struct ib_uverbs_file *file,
  198. const char __user *buf,
  199. int in_len, int out_len)
  200. {
  201. struct ib_uverbs_query_device cmd;
  202. struct ib_uverbs_query_device_resp resp;
  203. struct ib_ucontext *ucontext;
  204. ucontext = ib_uverbs_get_ucontext(file);
  205. if (IS_ERR(ucontext))
  206. return PTR_ERR(ucontext);
  207. if (out_len < sizeof resp)
  208. return -ENOSPC;
  209. if (copy_from_user(&cmd, buf, sizeof cmd))
  210. return -EFAULT;
  211. memset(&resp, 0, sizeof resp);
  212. copy_query_dev_fields(ucontext, &resp, &ucontext->device->attrs);
  213. if (copy_to_user(u64_to_user_ptr(cmd.response), &resp, sizeof resp))
  214. return -EFAULT;
  215. return in_len;
  216. }
  217. /*
  218. * ib_uverbs_query_port_resp.port_cap_flags started out as just a copy of the
  219. * PortInfo CapabilityMask, but was extended with unique bits.
  220. */
  221. static u32 make_port_cap_flags(const struct ib_port_attr *attr)
  222. {
  223. u32 res;
  224. /* All IBA CapabilityMask bits are passed through here, except bit 26,
  225. * which is overridden with IP_BASED_GIDS. This is due to a historical
  226. * mistake in the implementation of IP_BASED_GIDS. Otherwise all other
  227. * bits match the IBA definition across all kernel versions.
  228. */
  229. res = attr->port_cap_flags & ~(u32)IB_UVERBS_PCF_IP_BASED_GIDS;
  230. if (attr->ip_gids)
  231. res |= IB_UVERBS_PCF_IP_BASED_GIDS;
  232. return res;
  233. }
  234. ssize_t ib_uverbs_query_port(struct ib_uverbs_file *file,
  235. const char __user *buf,
  236. int in_len, int out_len)
  237. {
  238. struct ib_uverbs_query_port cmd;
  239. struct ib_uverbs_query_port_resp resp;
  240. struct ib_port_attr attr;
  241. int ret;
  242. struct ib_ucontext *ucontext;
  243. struct ib_device *ib_dev;
  244. ucontext = ib_uverbs_get_ucontext(file);
  245. if (IS_ERR(ucontext))
  246. return PTR_ERR(ucontext);
  247. ib_dev = ucontext->device;
  248. if (out_len < sizeof resp)
  249. return -ENOSPC;
  250. if (copy_from_user(&cmd, buf, sizeof cmd))
  251. return -EFAULT;
  252. ret = ib_query_port(ib_dev, cmd.port_num, &attr);
  253. if (ret)
  254. return ret;
  255. memset(&resp, 0, sizeof resp);
  256. resp.state = attr.state;
  257. resp.max_mtu = attr.max_mtu;
  258. resp.active_mtu = attr.active_mtu;
  259. resp.gid_tbl_len = attr.gid_tbl_len;
  260. resp.port_cap_flags = make_port_cap_flags(&attr);
  261. resp.max_msg_sz = attr.max_msg_sz;
  262. resp.bad_pkey_cntr = attr.bad_pkey_cntr;
  263. resp.qkey_viol_cntr = attr.qkey_viol_cntr;
  264. resp.pkey_tbl_len = attr.pkey_tbl_len;
  265. if (rdma_is_grh_required(ib_dev, cmd.port_num))
  266. resp.flags |= IB_UVERBS_QPF_GRH_REQUIRED;
  267. if (rdma_cap_opa_ah(ib_dev, cmd.port_num)) {
  268. resp.lid = OPA_TO_IB_UCAST_LID(attr.lid);
  269. resp.sm_lid = OPA_TO_IB_UCAST_LID(attr.sm_lid);
  270. } else {
  271. resp.lid = ib_lid_cpu16(attr.lid);
  272. resp.sm_lid = ib_lid_cpu16(attr.sm_lid);
  273. }
  274. resp.lmc = attr.lmc;
  275. resp.max_vl_num = attr.max_vl_num;
  276. resp.sm_sl = attr.sm_sl;
  277. resp.subnet_timeout = attr.subnet_timeout;
  278. resp.init_type_reply = attr.init_type_reply;
  279. resp.active_width = attr.active_width;
  280. resp.active_speed = attr.active_speed;
  281. resp.phys_state = attr.phys_state;
  282. resp.link_layer = rdma_port_get_link_layer(ib_dev,
  283. cmd.port_num);
  284. if (copy_to_user(u64_to_user_ptr(cmd.response), &resp, sizeof resp))
  285. return -EFAULT;
  286. return in_len;
  287. }
  288. ssize_t ib_uverbs_alloc_pd(struct ib_uverbs_file *file,
  289. const char __user *buf,
  290. int in_len, int out_len)
  291. {
  292. struct ib_uverbs_alloc_pd cmd;
  293. struct ib_uverbs_alloc_pd_resp resp;
  294. struct ib_udata udata;
  295. struct ib_uobject *uobj;
  296. struct ib_pd *pd;
  297. int ret;
  298. struct ib_device *ib_dev;
  299. if (out_len < sizeof resp)
  300. return -ENOSPC;
  301. if (copy_from_user(&cmd, buf, sizeof cmd))
  302. return -EFAULT;
  303. ib_uverbs_init_udata(&udata, buf + sizeof(cmd),
  304. u64_to_user_ptr(cmd.response) + sizeof(resp),
  305. in_len - sizeof(cmd) - sizeof(struct ib_uverbs_cmd_hdr),
  306. out_len - sizeof(resp));
  307. uobj = uobj_alloc(UVERBS_OBJECT_PD, file, &ib_dev);
  308. if (IS_ERR(uobj))
  309. return PTR_ERR(uobj);
  310. pd = ib_dev->alloc_pd(ib_dev, uobj->context, &udata);
  311. if (IS_ERR(pd)) {
  312. ret = PTR_ERR(pd);
  313. goto err;
  314. }
  315. pd->device = ib_dev;
  316. pd->uobject = uobj;
  317. pd->__internal_mr = NULL;
  318. atomic_set(&pd->usecnt, 0);
  319. uobj->object = pd;
  320. memset(&resp, 0, sizeof resp);
  321. resp.pd_handle = uobj->id;
  322. pd->res.type = RDMA_RESTRACK_PD;
  323. rdma_restrack_add(&pd->res);
  324. if (copy_to_user(u64_to_user_ptr(cmd.response), &resp, sizeof resp)) {
  325. ret = -EFAULT;
  326. goto err_copy;
  327. }
  328. return uobj_alloc_commit(uobj, in_len);
  329. err_copy:
  330. ib_dealloc_pd(pd);
  331. err:
  332. uobj_alloc_abort(uobj);
  333. return ret;
  334. }
  335. ssize_t ib_uverbs_dealloc_pd(struct ib_uverbs_file *file,
  336. const char __user *buf,
  337. int in_len, int out_len)
  338. {
  339. struct ib_uverbs_dealloc_pd cmd;
  340. if (copy_from_user(&cmd, buf, sizeof cmd))
  341. return -EFAULT;
  342. return uobj_perform_destroy(UVERBS_OBJECT_PD, cmd.pd_handle, file,
  343. in_len);
  344. }
  345. struct xrcd_table_entry {
  346. struct rb_node node;
  347. struct ib_xrcd *xrcd;
  348. struct inode *inode;
  349. };
  350. static int xrcd_table_insert(struct ib_uverbs_device *dev,
  351. struct inode *inode,
  352. struct ib_xrcd *xrcd)
  353. {
  354. struct xrcd_table_entry *entry, *scan;
  355. struct rb_node **p = &dev->xrcd_tree.rb_node;
  356. struct rb_node *parent = NULL;
  357. entry = kmalloc(sizeof *entry, GFP_KERNEL);
  358. if (!entry)
  359. return -ENOMEM;
  360. entry->xrcd = xrcd;
  361. entry->inode = inode;
  362. while (*p) {
  363. parent = *p;
  364. scan = rb_entry(parent, struct xrcd_table_entry, node);
  365. if (inode < scan->inode) {
  366. p = &(*p)->rb_left;
  367. } else if (inode > scan->inode) {
  368. p = &(*p)->rb_right;
  369. } else {
  370. kfree(entry);
  371. return -EEXIST;
  372. }
  373. }
  374. rb_link_node(&entry->node, parent, p);
  375. rb_insert_color(&entry->node, &dev->xrcd_tree);
  376. igrab(inode);
  377. return 0;
  378. }
  379. static struct xrcd_table_entry *xrcd_table_search(struct ib_uverbs_device *dev,
  380. struct inode *inode)
  381. {
  382. struct xrcd_table_entry *entry;
  383. struct rb_node *p = dev->xrcd_tree.rb_node;
  384. while (p) {
  385. entry = rb_entry(p, struct xrcd_table_entry, node);
  386. if (inode < entry->inode)
  387. p = p->rb_left;
  388. else if (inode > entry->inode)
  389. p = p->rb_right;
  390. else
  391. return entry;
  392. }
  393. return NULL;
  394. }
  395. static struct ib_xrcd *find_xrcd(struct ib_uverbs_device *dev, struct inode *inode)
  396. {
  397. struct xrcd_table_entry *entry;
  398. entry = xrcd_table_search(dev, inode);
  399. if (!entry)
  400. return NULL;
  401. return entry->xrcd;
  402. }
  403. static void xrcd_table_delete(struct ib_uverbs_device *dev,
  404. struct inode *inode)
  405. {
  406. struct xrcd_table_entry *entry;
  407. entry = xrcd_table_search(dev, inode);
  408. if (entry) {
  409. iput(inode);
  410. rb_erase(&entry->node, &dev->xrcd_tree);
  411. kfree(entry);
  412. }
  413. }
  414. ssize_t ib_uverbs_open_xrcd(struct ib_uverbs_file *file,
  415. const char __user *buf, int in_len,
  416. int out_len)
  417. {
  418. struct ib_uverbs_open_xrcd cmd;
  419. struct ib_uverbs_open_xrcd_resp resp;
  420. struct ib_udata udata;
  421. struct ib_uxrcd_object *obj;
  422. struct ib_xrcd *xrcd = NULL;
  423. struct fd f = {NULL, 0};
  424. struct inode *inode = NULL;
  425. int ret = 0;
  426. int new_xrcd = 0;
  427. struct ib_device *ib_dev;
  428. if (out_len < sizeof resp)
  429. return -ENOSPC;
  430. if (copy_from_user(&cmd, buf, sizeof cmd))
  431. return -EFAULT;
  432. ib_uverbs_init_udata(&udata, buf + sizeof(cmd),
  433. u64_to_user_ptr(cmd.response) + sizeof(resp),
  434. in_len - sizeof(cmd) - sizeof(struct ib_uverbs_cmd_hdr),
  435. out_len - sizeof(resp));
  436. mutex_lock(&file->device->xrcd_tree_mutex);
  437. if (cmd.fd != -1) {
  438. /* search for file descriptor */
  439. f = fdget(cmd.fd);
  440. if (!f.file) {
  441. ret = -EBADF;
  442. goto err_tree_mutex_unlock;
  443. }
  444. inode = file_inode(f.file);
  445. xrcd = find_xrcd(file->device, inode);
  446. if (!xrcd && !(cmd.oflags & O_CREAT)) {
  447. /* no file descriptor. Need CREATE flag */
  448. ret = -EAGAIN;
  449. goto err_tree_mutex_unlock;
  450. }
  451. if (xrcd && cmd.oflags & O_EXCL) {
  452. ret = -EINVAL;
  453. goto err_tree_mutex_unlock;
  454. }
  455. }
  456. obj = (struct ib_uxrcd_object *)uobj_alloc(UVERBS_OBJECT_XRCD, file,
  457. &ib_dev);
  458. if (IS_ERR(obj)) {
  459. ret = PTR_ERR(obj);
  460. goto err_tree_mutex_unlock;
  461. }
  462. if (!xrcd) {
  463. xrcd = ib_dev->alloc_xrcd(ib_dev, obj->uobject.context, &udata);
  464. if (IS_ERR(xrcd)) {
  465. ret = PTR_ERR(xrcd);
  466. goto err;
  467. }
  468. xrcd->inode = inode;
  469. xrcd->device = ib_dev;
  470. atomic_set(&xrcd->usecnt, 0);
  471. mutex_init(&xrcd->tgt_qp_mutex);
  472. INIT_LIST_HEAD(&xrcd->tgt_qp_list);
  473. new_xrcd = 1;
  474. }
  475. atomic_set(&obj->refcnt, 0);
  476. obj->uobject.object = xrcd;
  477. memset(&resp, 0, sizeof resp);
  478. resp.xrcd_handle = obj->uobject.id;
  479. if (inode) {
  480. if (new_xrcd) {
  481. /* create new inode/xrcd table entry */
  482. ret = xrcd_table_insert(file->device, inode, xrcd);
  483. if (ret)
  484. goto err_dealloc_xrcd;
  485. }
  486. atomic_inc(&xrcd->usecnt);
  487. }
  488. if (copy_to_user(u64_to_user_ptr(cmd.response), &resp, sizeof resp)) {
  489. ret = -EFAULT;
  490. goto err_copy;
  491. }
  492. if (f.file)
  493. fdput(f);
  494. mutex_unlock(&file->device->xrcd_tree_mutex);
  495. return uobj_alloc_commit(&obj->uobject, in_len);
  496. err_copy:
  497. if (inode) {
  498. if (new_xrcd)
  499. xrcd_table_delete(file->device, inode);
  500. atomic_dec(&xrcd->usecnt);
  501. }
  502. err_dealloc_xrcd:
  503. ib_dealloc_xrcd(xrcd);
  504. err:
  505. uobj_alloc_abort(&obj->uobject);
  506. err_tree_mutex_unlock:
  507. if (f.file)
  508. fdput(f);
  509. mutex_unlock(&file->device->xrcd_tree_mutex);
  510. return ret;
  511. }
  512. ssize_t ib_uverbs_close_xrcd(struct ib_uverbs_file *file,
  513. const char __user *buf, int in_len,
  514. int out_len)
  515. {
  516. struct ib_uverbs_close_xrcd cmd;
  517. if (copy_from_user(&cmd, buf, sizeof cmd))
  518. return -EFAULT;
  519. return uobj_perform_destroy(UVERBS_OBJECT_XRCD, cmd.xrcd_handle, file,
  520. in_len);
  521. }
  522. int ib_uverbs_dealloc_xrcd(struct ib_uobject *uobject,
  523. struct ib_xrcd *xrcd,
  524. enum rdma_remove_reason why)
  525. {
  526. struct inode *inode;
  527. int ret;
  528. struct ib_uverbs_device *dev = uobject->context->ufile->device;
  529. inode = xrcd->inode;
  530. if (inode && !atomic_dec_and_test(&xrcd->usecnt))
  531. return 0;
  532. ret = ib_dealloc_xrcd(xrcd);
  533. if (ib_is_destroy_retryable(ret, why, uobject)) {
  534. atomic_inc(&xrcd->usecnt);
  535. return ret;
  536. }
  537. if (inode)
  538. xrcd_table_delete(dev, inode);
  539. return ret;
  540. }
  541. ssize_t ib_uverbs_reg_mr(struct ib_uverbs_file *file,
  542. const char __user *buf, int in_len,
  543. int out_len)
  544. {
  545. struct ib_uverbs_reg_mr cmd;
  546. struct ib_uverbs_reg_mr_resp resp;
  547. struct ib_udata udata;
  548. struct ib_uobject *uobj;
  549. struct ib_pd *pd;
  550. struct ib_mr *mr;
  551. int ret;
  552. struct ib_device *ib_dev;
  553. if (out_len < sizeof resp)
  554. return -ENOSPC;
  555. if (copy_from_user(&cmd, buf, sizeof cmd))
  556. return -EFAULT;
  557. ib_uverbs_init_udata(&udata, buf + sizeof(cmd),
  558. u64_to_user_ptr(cmd.response) + sizeof(resp),
  559. in_len - sizeof(cmd) - sizeof(struct ib_uverbs_cmd_hdr),
  560. out_len - sizeof(resp));
  561. if ((cmd.start & ~PAGE_MASK) != (cmd.hca_va & ~PAGE_MASK))
  562. return -EINVAL;
  563. ret = ib_check_mr_access(cmd.access_flags);
  564. if (ret)
  565. return ret;
  566. uobj = uobj_alloc(UVERBS_OBJECT_MR, file, &ib_dev);
  567. if (IS_ERR(uobj))
  568. return PTR_ERR(uobj);
  569. pd = uobj_get_obj_read(pd, UVERBS_OBJECT_PD, cmd.pd_handle, file);
  570. if (!pd) {
  571. ret = -EINVAL;
  572. goto err_free;
  573. }
  574. if (cmd.access_flags & IB_ACCESS_ON_DEMAND) {
  575. if (!(pd->device->attrs.device_cap_flags &
  576. IB_DEVICE_ON_DEMAND_PAGING)) {
  577. pr_debug("ODP support not available\n");
  578. ret = -EINVAL;
  579. goto err_put;
  580. }
  581. }
  582. mr = pd->device->reg_user_mr(pd, cmd.start, cmd.length, cmd.hca_va,
  583. cmd.access_flags, &udata);
  584. if (IS_ERR(mr)) {
  585. ret = PTR_ERR(mr);
  586. goto err_put;
  587. }
  588. mr->device = pd->device;
  589. mr->pd = pd;
  590. mr->dm = NULL;
  591. mr->uobject = uobj;
  592. atomic_inc(&pd->usecnt);
  593. mr->res.type = RDMA_RESTRACK_MR;
  594. rdma_restrack_add(&mr->res);
  595. uobj->object = mr;
  596. memset(&resp, 0, sizeof resp);
  597. resp.lkey = mr->lkey;
  598. resp.rkey = mr->rkey;
  599. resp.mr_handle = uobj->id;
  600. if (copy_to_user(u64_to_user_ptr(cmd.response), &resp, sizeof resp)) {
  601. ret = -EFAULT;
  602. goto err_copy;
  603. }
  604. uobj_put_obj_read(pd);
  605. return uobj_alloc_commit(uobj, in_len);
  606. err_copy:
  607. ib_dereg_mr(mr);
  608. err_put:
  609. uobj_put_obj_read(pd);
  610. err_free:
  611. uobj_alloc_abort(uobj);
  612. return ret;
  613. }
  614. ssize_t ib_uverbs_rereg_mr(struct ib_uverbs_file *file,
  615. const char __user *buf, int in_len,
  616. int out_len)
  617. {
  618. struct ib_uverbs_rereg_mr cmd;
  619. struct ib_uverbs_rereg_mr_resp resp;
  620. struct ib_udata udata;
  621. struct ib_pd *pd = NULL;
  622. struct ib_mr *mr;
  623. struct ib_pd *old_pd;
  624. int ret;
  625. struct ib_uobject *uobj;
  626. if (out_len < sizeof(resp))
  627. return -ENOSPC;
  628. if (copy_from_user(&cmd, buf, sizeof(cmd)))
  629. return -EFAULT;
  630. ib_uverbs_init_udata(&udata, buf + sizeof(cmd),
  631. u64_to_user_ptr(cmd.response) + sizeof(resp),
  632. in_len - sizeof(cmd) - sizeof(struct ib_uverbs_cmd_hdr),
  633. out_len - sizeof(resp));
  634. if (cmd.flags & ~IB_MR_REREG_SUPPORTED || !cmd.flags)
  635. return -EINVAL;
  636. if ((cmd.flags & IB_MR_REREG_TRANS) &&
  637. (!cmd.start || !cmd.hca_va || 0 >= cmd.length ||
  638. (cmd.start & ~PAGE_MASK) != (cmd.hca_va & ~PAGE_MASK)))
  639. return -EINVAL;
  640. uobj = uobj_get_write(UVERBS_OBJECT_MR, cmd.mr_handle, file);
  641. if (IS_ERR(uobj))
  642. return PTR_ERR(uobj);
  643. mr = uobj->object;
  644. if (mr->dm) {
  645. ret = -EINVAL;
  646. goto put_uobjs;
  647. }
  648. if (cmd.flags & IB_MR_REREG_ACCESS) {
  649. ret = ib_check_mr_access(cmd.access_flags);
  650. if (ret)
  651. goto put_uobjs;
  652. }
  653. if (cmd.flags & IB_MR_REREG_PD) {
  654. pd = uobj_get_obj_read(pd, UVERBS_OBJECT_PD, cmd.pd_handle,
  655. file);
  656. if (!pd) {
  657. ret = -EINVAL;
  658. goto put_uobjs;
  659. }
  660. }
  661. old_pd = mr->pd;
  662. ret = mr->device->rereg_user_mr(mr, cmd.flags, cmd.start,
  663. cmd.length, cmd.hca_va,
  664. cmd.access_flags, pd, &udata);
  665. if (!ret) {
  666. if (cmd.flags & IB_MR_REREG_PD) {
  667. atomic_inc(&pd->usecnt);
  668. mr->pd = pd;
  669. atomic_dec(&old_pd->usecnt);
  670. }
  671. } else {
  672. goto put_uobj_pd;
  673. }
  674. memset(&resp, 0, sizeof(resp));
  675. resp.lkey = mr->lkey;
  676. resp.rkey = mr->rkey;
  677. if (copy_to_user(u64_to_user_ptr(cmd.response), &resp, sizeof(resp)))
  678. ret = -EFAULT;
  679. else
  680. ret = in_len;
  681. put_uobj_pd:
  682. if (cmd.flags & IB_MR_REREG_PD)
  683. uobj_put_obj_read(pd);
  684. put_uobjs:
  685. uobj_put_write(uobj);
  686. return ret;
  687. }
  688. ssize_t ib_uverbs_dereg_mr(struct ib_uverbs_file *file,
  689. const char __user *buf, int in_len,
  690. int out_len)
  691. {
  692. struct ib_uverbs_dereg_mr cmd;
  693. if (copy_from_user(&cmd, buf, sizeof cmd))
  694. return -EFAULT;
  695. return uobj_perform_destroy(UVERBS_OBJECT_MR, cmd.mr_handle, file,
  696. in_len);
  697. }
  698. ssize_t ib_uverbs_alloc_mw(struct ib_uverbs_file *file,
  699. const char __user *buf, int in_len,
  700. int out_len)
  701. {
  702. struct ib_uverbs_alloc_mw cmd;
  703. struct ib_uverbs_alloc_mw_resp resp;
  704. struct ib_uobject *uobj;
  705. struct ib_pd *pd;
  706. struct ib_mw *mw;
  707. struct ib_udata udata;
  708. int ret;
  709. struct ib_device *ib_dev;
  710. if (out_len < sizeof(resp))
  711. return -ENOSPC;
  712. if (copy_from_user(&cmd, buf, sizeof(cmd)))
  713. return -EFAULT;
  714. uobj = uobj_alloc(UVERBS_OBJECT_MW, file, &ib_dev);
  715. if (IS_ERR(uobj))
  716. return PTR_ERR(uobj);
  717. pd = uobj_get_obj_read(pd, UVERBS_OBJECT_PD, cmd.pd_handle, file);
  718. if (!pd) {
  719. ret = -EINVAL;
  720. goto err_free;
  721. }
  722. ib_uverbs_init_udata(&udata, buf + sizeof(cmd),
  723. u64_to_user_ptr(cmd.response) + sizeof(resp),
  724. in_len - sizeof(cmd) - sizeof(struct ib_uverbs_cmd_hdr),
  725. out_len - sizeof(resp));
  726. mw = pd->device->alloc_mw(pd, cmd.mw_type, &udata);
  727. if (IS_ERR(mw)) {
  728. ret = PTR_ERR(mw);
  729. goto err_put;
  730. }
  731. mw->device = pd->device;
  732. mw->pd = pd;
  733. mw->uobject = uobj;
  734. atomic_inc(&pd->usecnt);
  735. uobj->object = mw;
  736. memset(&resp, 0, sizeof(resp));
  737. resp.rkey = mw->rkey;
  738. resp.mw_handle = uobj->id;
  739. if (copy_to_user(u64_to_user_ptr(cmd.response), &resp, sizeof(resp))) {
  740. ret = -EFAULT;
  741. goto err_copy;
  742. }
  743. uobj_put_obj_read(pd);
  744. return uobj_alloc_commit(uobj, in_len);
  745. err_copy:
  746. uverbs_dealloc_mw(mw);
  747. err_put:
  748. uobj_put_obj_read(pd);
  749. err_free:
  750. uobj_alloc_abort(uobj);
  751. return ret;
  752. }
  753. ssize_t ib_uverbs_dealloc_mw(struct ib_uverbs_file *file,
  754. const char __user *buf, int in_len,
  755. int out_len)
  756. {
  757. struct ib_uverbs_dealloc_mw cmd;
  758. if (copy_from_user(&cmd, buf, sizeof(cmd)))
  759. return -EFAULT;
  760. return uobj_perform_destroy(UVERBS_OBJECT_MW, cmd.mw_handle, file,
  761. in_len);
  762. }
  763. ssize_t ib_uverbs_create_comp_channel(struct ib_uverbs_file *file,
  764. const char __user *buf, int in_len,
  765. int out_len)
  766. {
  767. struct ib_uverbs_create_comp_channel cmd;
  768. struct ib_uverbs_create_comp_channel_resp resp;
  769. struct ib_uobject *uobj;
  770. struct ib_uverbs_completion_event_file *ev_file;
  771. struct ib_device *ib_dev;
  772. if (out_len < sizeof resp)
  773. return -ENOSPC;
  774. if (copy_from_user(&cmd, buf, sizeof cmd))
  775. return -EFAULT;
  776. uobj = uobj_alloc(UVERBS_OBJECT_COMP_CHANNEL, file, &ib_dev);
  777. if (IS_ERR(uobj))
  778. return PTR_ERR(uobj);
  779. resp.fd = uobj->id;
  780. ev_file = container_of(uobj, struct ib_uverbs_completion_event_file,
  781. uobj);
  782. ib_uverbs_init_event_queue(&ev_file->ev_queue);
  783. if (copy_to_user(u64_to_user_ptr(cmd.response), &resp, sizeof resp)) {
  784. uobj_alloc_abort(uobj);
  785. return -EFAULT;
  786. }
  787. return uobj_alloc_commit(uobj, in_len);
  788. }
  789. static struct ib_ucq_object *create_cq(struct ib_uverbs_file *file,
  790. struct ib_udata *ucore,
  791. struct ib_udata *uhw,
  792. struct ib_uverbs_ex_create_cq *cmd,
  793. size_t cmd_sz,
  794. int (*cb)(struct ib_uverbs_file *file,
  795. struct ib_ucq_object *obj,
  796. struct ib_uverbs_ex_create_cq_resp *resp,
  797. struct ib_udata *udata,
  798. void *context),
  799. void *context)
  800. {
  801. struct ib_ucq_object *obj;
  802. struct ib_uverbs_completion_event_file *ev_file = NULL;
  803. struct ib_cq *cq;
  804. int ret;
  805. struct ib_uverbs_ex_create_cq_resp resp;
  806. struct ib_cq_init_attr attr = {};
  807. struct ib_device *ib_dev;
  808. if (cmd->comp_vector >= file->device->num_comp_vectors)
  809. return ERR_PTR(-EINVAL);
  810. obj = (struct ib_ucq_object *)uobj_alloc(UVERBS_OBJECT_CQ, file,
  811. &ib_dev);
  812. if (IS_ERR(obj))
  813. return obj;
  814. if (!ib_dev->create_cq) {
  815. ret = -EOPNOTSUPP;
  816. goto err;
  817. }
  818. if (cmd->comp_channel >= 0) {
  819. ev_file = ib_uverbs_lookup_comp_file(cmd->comp_channel, file);
  820. if (IS_ERR(ev_file)) {
  821. ret = PTR_ERR(ev_file);
  822. goto err;
  823. }
  824. }
  825. obj->uobject.user_handle = cmd->user_handle;
  826. obj->comp_events_reported = 0;
  827. obj->async_events_reported = 0;
  828. INIT_LIST_HEAD(&obj->comp_list);
  829. INIT_LIST_HEAD(&obj->async_list);
  830. attr.cqe = cmd->cqe;
  831. attr.comp_vector = cmd->comp_vector;
  832. if (cmd_sz > offsetof(typeof(*cmd), flags) + sizeof(cmd->flags))
  833. attr.flags = cmd->flags;
  834. cq = ib_dev->create_cq(ib_dev, &attr, obj->uobject.context, uhw);
  835. if (IS_ERR(cq)) {
  836. ret = PTR_ERR(cq);
  837. goto err_file;
  838. }
  839. cq->device = ib_dev;
  840. cq->uobject = &obj->uobject;
  841. cq->comp_handler = ib_uverbs_comp_handler;
  842. cq->event_handler = ib_uverbs_cq_event_handler;
  843. cq->cq_context = ev_file ? &ev_file->ev_queue : NULL;
  844. atomic_set(&cq->usecnt, 0);
  845. obj->uobject.object = cq;
  846. memset(&resp, 0, sizeof resp);
  847. resp.base.cq_handle = obj->uobject.id;
  848. resp.base.cqe = cq->cqe;
  849. resp.response_length = offsetof(typeof(resp), response_length) +
  850. sizeof(resp.response_length);
  851. cq->res.type = RDMA_RESTRACK_CQ;
  852. rdma_restrack_add(&cq->res);
  853. ret = cb(file, obj, &resp, ucore, context);
  854. if (ret)
  855. goto err_cb;
  856. ret = uobj_alloc_commit(&obj->uobject, 0);
  857. if (ret)
  858. return ERR_PTR(ret);
  859. return obj;
  860. err_cb:
  861. ib_destroy_cq(cq);
  862. err_file:
  863. if (ev_file)
  864. ib_uverbs_release_ucq(file, ev_file, obj);
  865. err:
  866. uobj_alloc_abort(&obj->uobject);
  867. return ERR_PTR(ret);
  868. }
  869. static int ib_uverbs_create_cq_cb(struct ib_uverbs_file *file,
  870. struct ib_ucq_object *obj,
  871. struct ib_uverbs_ex_create_cq_resp *resp,
  872. struct ib_udata *ucore, void *context)
  873. {
  874. if (ib_copy_to_udata(ucore, &resp->base, sizeof(resp->base)))
  875. return -EFAULT;
  876. return 0;
  877. }
  878. ssize_t ib_uverbs_create_cq(struct ib_uverbs_file *file,
  879. const char __user *buf, int in_len,
  880. int out_len)
  881. {
  882. struct ib_uverbs_create_cq cmd;
  883. struct ib_uverbs_ex_create_cq cmd_ex;
  884. struct ib_uverbs_create_cq_resp resp;
  885. struct ib_udata ucore;
  886. struct ib_udata uhw;
  887. struct ib_ucq_object *obj;
  888. if (out_len < sizeof(resp))
  889. return -ENOSPC;
  890. if (copy_from_user(&cmd, buf, sizeof(cmd)))
  891. return -EFAULT;
  892. ib_uverbs_init_udata(&ucore, buf, u64_to_user_ptr(cmd.response),
  893. sizeof(cmd), sizeof(resp));
  894. ib_uverbs_init_udata(&uhw, buf + sizeof(cmd),
  895. u64_to_user_ptr(cmd.response) + sizeof(resp),
  896. in_len - sizeof(cmd) - sizeof(struct ib_uverbs_cmd_hdr),
  897. out_len - sizeof(resp));
  898. memset(&cmd_ex, 0, sizeof(cmd_ex));
  899. cmd_ex.user_handle = cmd.user_handle;
  900. cmd_ex.cqe = cmd.cqe;
  901. cmd_ex.comp_vector = cmd.comp_vector;
  902. cmd_ex.comp_channel = cmd.comp_channel;
  903. obj = create_cq(file, &ucore, &uhw, &cmd_ex,
  904. offsetof(typeof(cmd_ex), comp_channel) +
  905. sizeof(cmd.comp_channel), ib_uverbs_create_cq_cb,
  906. NULL);
  907. if (IS_ERR(obj))
  908. return PTR_ERR(obj);
  909. return in_len;
  910. }
  911. static int ib_uverbs_ex_create_cq_cb(struct ib_uverbs_file *file,
  912. struct ib_ucq_object *obj,
  913. struct ib_uverbs_ex_create_cq_resp *resp,
  914. struct ib_udata *ucore, void *context)
  915. {
  916. if (ib_copy_to_udata(ucore, resp, resp->response_length))
  917. return -EFAULT;
  918. return 0;
  919. }
  920. int ib_uverbs_ex_create_cq(struct ib_uverbs_file *file,
  921. struct ib_udata *ucore,
  922. struct ib_udata *uhw)
  923. {
  924. struct ib_uverbs_ex_create_cq_resp resp;
  925. struct ib_uverbs_ex_create_cq cmd;
  926. struct ib_ucq_object *obj;
  927. int err;
  928. if (ucore->inlen < sizeof(cmd))
  929. return -EINVAL;
  930. err = ib_copy_from_udata(&cmd, ucore, sizeof(cmd));
  931. if (err)
  932. return err;
  933. if (cmd.comp_mask)
  934. return -EINVAL;
  935. if (cmd.reserved)
  936. return -EINVAL;
  937. if (ucore->outlen < (offsetof(typeof(resp), response_length) +
  938. sizeof(resp.response_length)))
  939. return -ENOSPC;
  940. obj = create_cq(file, ucore, uhw, &cmd,
  941. min(ucore->inlen, sizeof(cmd)),
  942. ib_uverbs_ex_create_cq_cb, NULL);
  943. return PTR_ERR_OR_ZERO(obj);
  944. }
  945. ssize_t ib_uverbs_resize_cq(struct ib_uverbs_file *file,
  946. const char __user *buf, int in_len,
  947. int out_len)
  948. {
  949. struct ib_uverbs_resize_cq cmd;
  950. struct ib_uverbs_resize_cq_resp resp = {};
  951. struct ib_udata udata;
  952. struct ib_cq *cq;
  953. int ret = -EINVAL;
  954. if (copy_from_user(&cmd, buf, sizeof cmd))
  955. return -EFAULT;
  956. ib_uverbs_init_udata(&udata, buf + sizeof(cmd),
  957. u64_to_user_ptr(cmd.response) + sizeof(resp),
  958. in_len - sizeof(cmd) - sizeof(struct ib_uverbs_cmd_hdr),
  959. out_len - sizeof(resp));
  960. cq = uobj_get_obj_read(cq, UVERBS_OBJECT_CQ, cmd.cq_handle, file);
  961. if (!cq)
  962. return -EINVAL;
  963. ret = cq->device->resize_cq(cq, cmd.cqe, &udata);
  964. if (ret)
  965. goto out;
  966. resp.cqe = cq->cqe;
  967. if (copy_to_user(u64_to_user_ptr(cmd.response), &resp, sizeof resp.cqe))
  968. ret = -EFAULT;
  969. out:
  970. uobj_put_obj_read(cq);
  971. return ret ? ret : in_len;
  972. }
  973. static int copy_wc_to_user(struct ib_device *ib_dev, void __user *dest,
  974. struct ib_wc *wc)
  975. {
  976. struct ib_uverbs_wc tmp;
  977. tmp.wr_id = wc->wr_id;
  978. tmp.status = wc->status;
  979. tmp.opcode = wc->opcode;
  980. tmp.vendor_err = wc->vendor_err;
  981. tmp.byte_len = wc->byte_len;
  982. tmp.ex.imm_data = wc->ex.imm_data;
  983. tmp.qp_num = wc->qp->qp_num;
  984. tmp.src_qp = wc->src_qp;
  985. tmp.wc_flags = wc->wc_flags;
  986. tmp.pkey_index = wc->pkey_index;
  987. if (rdma_cap_opa_ah(ib_dev, wc->port_num))
  988. tmp.slid = OPA_TO_IB_UCAST_LID(wc->slid);
  989. else
  990. tmp.slid = ib_lid_cpu16(wc->slid);
  991. tmp.sl = wc->sl;
  992. tmp.dlid_path_bits = wc->dlid_path_bits;
  993. tmp.port_num = wc->port_num;
  994. tmp.reserved = 0;
  995. if (copy_to_user(dest, &tmp, sizeof tmp))
  996. return -EFAULT;
  997. return 0;
  998. }
  999. ssize_t ib_uverbs_poll_cq(struct ib_uverbs_file *file,
  1000. const char __user *buf, int in_len,
  1001. int out_len)
  1002. {
  1003. struct ib_uverbs_poll_cq cmd;
  1004. struct ib_uverbs_poll_cq_resp resp;
  1005. u8 __user *header_ptr;
  1006. u8 __user *data_ptr;
  1007. struct ib_cq *cq;
  1008. struct ib_wc wc;
  1009. int ret;
  1010. if (copy_from_user(&cmd, buf, sizeof cmd))
  1011. return -EFAULT;
  1012. cq = uobj_get_obj_read(cq, UVERBS_OBJECT_CQ, cmd.cq_handle, file);
  1013. if (!cq)
  1014. return -EINVAL;
  1015. /* we copy a struct ib_uverbs_poll_cq_resp to user space */
  1016. header_ptr = u64_to_user_ptr(cmd.response);
  1017. data_ptr = header_ptr + sizeof resp;
  1018. memset(&resp, 0, sizeof resp);
  1019. while (resp.count < cmd.ne) {
  1020. ret = ib_poll_cq(cq, 1, &wc);
  1021. if (ret < 0)
  1022. goto out_put;
  1023. if (!ret)
  1024. break;
  1025. ret = copy_wc_to_user(cq->device, data_ptr, &wc);
  1026. if (ret)
  1027. goto out_put;
  1028. data_ptr += sizeof(struct ib_uverbs_wc);
  1029. ++resp.count;
  1030. }
  1031. if (copy_to_user(header_ptr, &resp, sizeof resp)) {
  1032. ret = -EFAULT;
  1033. goto out_put;
  1034. }
  1035. ret = in_len;
  1036. out_put:
  1037. uobj_put_obj_read(cq);
  1038. return ret;
  1039. }
  1040. ssize_t ib_uverbs_req_notify_cq(struct ib_uverbs_file *file,
  1041. const char __user *buf, int in_len,
  1042. int out_len)
  1043. {
  1044. struct ib_uverbs_req_notify_cq cmd;
  1045. struct ib_cq *cq;
  1046. if (copy_from_user(&cmd, buf, sizeof cmd))
  1047. return -EFAULT;
  1048. cq = uobj_get_obj_read(cq, UVERBS_OBJECT_CQ, cmd.cq_handle, file);
  1049. if (!cq)
  1050. return -EINVAL;
  1051. ib_req_notify_cq(cq, cmd.solicited_only ?
  1052. IB_CQ_SOLICITED : IB_CQ_NEXT_COMP);
  1053. uobj_put_obj_read(cq);
  1054. return in_len;
  1055. }
  1056. ssize_t ib_uverbs_destroy_cq(struct ib_uverbs_file *file,
  1057. const char __user *buf, int in_len,
  1058. int out_len)
  1059. {
  1060. struct ib_uverbs_destroy_cq cmd;
  1061. struct ib_uverbs_destroy_cq_resp resp;
  1062. struct ib_uobject *uobj;
  1063. struct ib_ucq_object *obj;
  1064. if (copy_from_user(&cmd, buf, sizeof cmd))
  1065. return -EFAULT;
  1066. uobj = uobj_get_destroy(UVERBS_OBJECT_CQ, cmd.cq_handle, file);
  1067. if (IS_ERR(uobj))
  1068. return PTR_ERR(uobj);
  1069. obj = container_of(uobj, struct ib_ucq_object, uobject);
  1070. memset(&resp, 0, sizeof(resp));
  1071. resp.comp_events_reported = obj->comp_events_reported;
  1072. resp.async_events_reported = obj->async_events_reported;
  1073. uobj_put_destroy(uobj);
  1074. if (copy_to_user(u64_to_user_ptr(cmd.response), &resp, sizeof resp))
  1075. return -EFAULT;
  1076. return in_len;
  1077. }
  1078. static int create_qp(struct ib_uverbs_file *file,
  1079. struct ib_udata *ucore,
  1080. struct ib_udata *uhw,
  1081. struct ib_uverbs_ex_create_qp *cmd,
  1082. size_t cmd_sz,
  1083. int (*cb)(struct ib_uverbs_file *file,
  1084. struct ib_uverbs_ex_create_qp_resp *resp,
  1085. struct ib_udata *udata),
  1086. void *context)
  1087. {
  1088. struct ib_uqp_object *obj;
  1089. struct ib_device *device;
  1090. struct ib_pd *pd = NULL;
  1091. struct ib_xrcd *xrcd = NULL;
  1092. struct ib_uobject *xrcd_uobj = ERR_PTR(-ENOENT);
  1093. struct ib_cq *scq = NULL, *rcq = NULL;
  1094. struct ib_srq *srq = NULL;
  1095. struct ib_qp *qp;
  1096. char *buf;
  1097. struct ib_qp_init_attr attr = {};
  1098. struct ib_uverbs_ex_create_qp_resp resp;
  1099. int ret;
  1100. struct ib_rwq_ind_table *ind_tbl = NULL;
  1101. bool has_sq = true;
  1102. struct ib_device *ib_dev;
  1103. if (cmd->qp_type == IB_QPT_RAW_PACKET && !capable(CAP_NET_RAW))
  1104. return -EPERM;
  1105. obj = (struct ib_uqp_object *)uobj_alloc(UVERBS_OBJECT_QP, file,
  1106. &ib_dev);
  1107. if (IS_ERR(obj))
  1108. return PTR_ERR(obj);
  1109. obj->uxrcd = NULL;
  1110. obj->uevent.uobject.user_handle = cmd->user_handle;
  1111. mutex_init(&obj->mcast_lock);
  1112. if (cmd_sz >= offsetof(typeof(*cmd), rwq_ind_tbl_handle) +
  1113. sizeof(cmd->rwq_ind_tbl_handle) &&
  1114. (cmd->comp_mask & IB_UVERBS_CREATE_QP_MASK_IND_TABLE)) {
  1115. ind_tbl = uobj_get_obj_read(rwq_ind_table,
  1116. UVERBS_OBJECT_RWQ_IND_TBL,
  1117. cmd->rwq_ind_tbl_handle, file);
  1118. if (!ind_tbl) {
  1119. ret = -EINVAL;
  1120. goto err_put;
  1121. }
  1122. attr.rwq_ind_tbl = ind_tbl;
  1123. }
  1124. if (cmd_sz > sizeof(*cmd) &&
  1125. !ib_is_udata_cleared(ucore, sizeof(*cmd),
  1126. cmd_sz - sizeof(*cmd))) {
  1127. ret = -EOPNOTSUPP;
  1128. goto err_put;
  1129. }
  1130. if (ind_tbl && (cmd->max_recv_wr || cmd->max_recv_sge || cmd->is_srq)) {
  1131. ret = -EINVAL;
  1132. goto err_put;
  1133. }
  1134. if (ind_tbl && !cmd->max_send_wr)
  1135. has_sq = false;
  1136. if (cmd->qp_type == IB_QPT_XRC_TGT) {
  1137. xrcd_uobj = uobj_get_read(UVERBS_OBJECT_XRCD, cmd->pd_handle,
  1138. file);
  1139. if (IS_ERR(xrcd_uobj)) {
  1140. ret = -EINVAL;
  1141. goto err_put;
  1142. }
  1143. xrcd = (struct ib_xrcd *)xrcd_uobj->object;
  1144. if (!xrcd) {
  1145. ret = -EINVAL;
  1146. goto err_put;
  1147. }
  1148. device = xrcd->device;
  1149. } else {
  1150. if (cmd->qp_type == IB_QPT_XRC_INI) {
  1151. cmd->max_recv_wr = 0;
  1152. cmd->max_recv_sge = 0;
  1153. } else {
  1154. if (cmd->is_srq) {
  1155. srq = uobj_get_obj_read(srq, UVERBS_OBJECT_SRQ,
  1156. cmd->srq_handle, file);
  1157. if (!srq || srq->srq_type == IB_SRQT_XRC) {
  1158. ret = -EINVAL;
  1159. goto err_put;
  1160. }
  1161. }
  1162. if (!ind_tbl) {
  1163. if (cmd->recv_cq_handle != cmd->send_cq_handle) {
  1164. rcq = uobj_get_obj_read(
  1165. cq, UVERBS_OBJECT_CQ,
  1166. cmd->recv_cq_handle, file);
  1167. if (!rcq) {
  1168. ret = -EINVAL;
  1169. goto err_put;
  1170. }
  1171. }
  1172. }
  1173. }
  1174. if (has_sq)
  1175. scq = uobj_get_obj_read(cq, UVERBS_OBJECT_CQ,
  1176. cmd->send_cq_handle, file);
  1177. if (!ind_tbl)
  1178. rcq = rcq ?: scq;
  1179. pd = uobj_get_obj_read(pd, UVERBS_OBJECT_PD, cmd->pd_handle,
  1180. file);
  1181. if (!pd || (!scq && has_sq)) {
  1182. ret = -EINVAL;
  1183. goto err_put;
  1184. }
  1185. device = pd->device;
  1186. }
  1187. attr.event_handler = ib_uverbs_qp_event_handler;
  1188. attr.qp_context = file;
  1189. attr.send_cq = scq;
  1190. attr.recv_cq = rcq;
  1191. attr.srq = srq;
  1192. attr.xrcd = xrcd;
  1193. attr.sq_sig_type = cmd->sq_sig_all ? IB_SIGNAL_ALL_WR :
  1194. IB_SIGNAL_REQ_WR;
  1195. attr.qp_type = cmd->qp_type;
  1196. attr.create_flags = 0;
  1197. attr.cap.max_send_wr = cmd->max_send_wr;
  1198. attr.cap.max_recv_wr = cmd->max_recv_wr;
  1199. attr.cap.max_send_sge = cmd->max_send_sge;
  1200. attr.cap.max_recv_sge = cmd->max_recv_sge;
  1201. attr.cap.max_inline_data = cmd->max_inline_data;
  1202. obj->uevent.events_reported = 0;
  1203. INIT_LIST_HEAD(&obj->uevent.event_list);
  1204. INIT_LIST_HEAD(&obj->mcast_list);
  1205. if (cmd_sz >= offsetof(typeof(*cmd), create_flags) +
  1206. sizeof(cmd->create_flags))
  1207. attr.create_flags = cmd->create_flags;
  1208. if (attr.create_flags & ~(IB_QP_CREATE_BLOCK_MULTICAST_LOOPBACK |
  1209. IB_QP_CREATE_CROSS_CHANNEL |
  1210. IB_QP_CREATE_MANAGED_SEND |
  1211. IB_QP_CREATE_MANAGED_RECV |
  1212. IB_QP_CREATE_SCATTER_FCS |
  1213. IB_QP_CREATE_CVLAN_STRIPPING |
  1214. IB_QP_CREATE_SOURCE_QPN |
  1215. IB_QP_CREATE_PCI_WRITE_END_PADDING)) {
  1216. ret = -EINVAL;
  1217. goto err_put;
  1218. }
  1219. if (attr.create_flags & IB_QP_CREATE_SOURCE_QPN) {
  1220. if (!capable(CAP_NET_RAW)) {
  1221. ret = -EPERM;
  1222. goto err_put;
  1223. }
  1224. attr.source_qpn = cmd->source_qpn;
  1225. }
  1226. buf = (void *)cmd + sizeof(*cmd);
  1227. if (cmd_sz > sizeof(*cmd))
  1228. if (!(buf[0] == 0 && !memcmp(buf, buf + 1,
  1229. cmd_sz - sizeof(*cmd) - 1))) {
  1230. ret = -EINVAL;
  1231. goto err_put;
  1232. }
  1233. if (cmd->qp_type == IB_QPT_XRC_TGT)
  1234. qp = ib_create_qp(pd, &attr);
  1235. else
  1236. qp = _ib_create_qp(device, pd, &attr, uhw,
  1237. &obj->uevent.uobject);
  1238. if (IS_ERR(qp)) {
  1239. ret = PTR_ERR(qp);
  1240. goto err_put;
  1241. }
  1242. if (cmd->qp_type != IB_QPT_XRC_TGT) {
  1243. ret = ib_create_qp_security(qp, device);
  1244. if (ret)
  1245. goto err_cb;
  1246. qp->real_qp = qp;
  1247. qp->pd = pd;
  1248. qp->send_cq = attr.send_cq;
  1249. qp->recv_cq = attr.recv_cq;
  1250. qp->srq = attr.srq;
  1251. qp->rwq_ind_tbl = ind_tbl;
  1252. qp->event_handler = attr.event_handler;
  1253. qp->qp_context = attr.qp_context;
  1254. qp->qp_type = attr.qp_type;
  1255. atomic_set(&qp->usecnt, 0);
  1256. atomic_inc(&pd->usecnt);
  1257. qp->port = 0;
  1258. if (attr.send_cq)
  1259. atomic_inc(&attr.send_cq->usecnt);
  1260. if (attr.recv_cq)
  1261. atomic_inc(&attr.recv_cq->usecnt);
  1262. if (attr.srq)
  1263. atomic_inc(&attr.srq->usecnt);
  1264. if (ind_tbl)
  1265. atomic_inc(&ind_tbl->usecnt);
  1266. } else {
  1267. /* It is done in _ib_create_qp for other QP types */
  1268. qp->uobject = &obj->uevent.uobject;
  1269. }
  1270. obj->uevent.uobject.object = qp;
  1271. memset(&resp, 0, sizeof resp);
  1272. resp.base.qpn = qp->qp_num;
  1273. resp.base.qp_handle = obj->uevent.uobject.id;
  1274. resp.base.max_recv_sge = attr.cap.max_recv_sge;
  1275. resp.base.max_send_sge = attr.cap.max_send_sge;
  1276. resp.base.max_recv_wr = attr.cap.max_recv_wr;
  1277. resp.base.max_send_wr = attr.cap.max_send_wr;
  1278. resp.base.max_inline_data = attr.cap.max_inline_data;
  1279. resp.response_length = offsetof(typeof(resp), response_length) +
  1280. sizeof(resp.response_length);
  1281. ret = cb(file, &resp, ucore);
  1282. if (ret)
  1283. goto err_cb;
  1284. if (xrcd) {
  1285. obj->uxrcd = container_of(xrcd_uobj, struct ib_uxrcd_object,
  1286. uobject);
  1287. atomic_inc(&obj->uxrcd->refcnt);
  1288. uobj_put_read(xrcd_uobj);
  1289. }
  1290. if (pd)
  1291. uobj_put_obj_read(pd);
  1292. if (scq)
  1293. uobj_put_obj_read(scq);
  1294. if (rcq && rcq != scq)
  1295. uobj_put_obj_read(rcq);
  1296. if (srq)
  1297. uobj_put_obj_read(srq);
  1298. if (ind_tbl)
  1299. uobj_put_obj_read(ind_tbl);
  1300. return uobj_alloc_commit(&obj->uevent.uobject, 0);
  1301. err_cb:
  1302. ib_destroy_qp(qp);
  1303. err_put:
  1304. if (!IS_ERR(xrcd_uobj))
  1305. uobj_put_read(xrcd_uobj);
  1306. if (pd)
  1307. uobj_put_obj_read(pd);
  1308. if (scq)
  1309. uobj_put_obj_read(scq);
  1310. if (rcq && rcq != scq)
  1311. uobj_put_obj_read(rcq);
  1312. if (srq)
  1313. uobj_put_obj_read(srq);
  1314. if (ind_tbl)
  1315. uobj_put_obj_read(ind_tbl);
  1316. uobj_alloc_abort(&obj->uevent.uobject);
  1317. return ret;
  1318. }
  1319. static int ib_uverbs_create_qp_cb(struct ib_uverbs_file *file,
  1320. struct ib_uverbs_ex_create_qp_resp *resp,
  1321. struct ib_udata *ucore)
  1322. {
  1323. if (ib_copy_to_udata(ucore, &resp->base, sizeof(resp->base)))
  1324. return -EFAULT;
  1325. return 0;
  1326. }
  1327. ssize_t ib_uverbs_create_qp(struct ib_uverbs_file *file,
  1328. const char __user *buf, int in_len,
  1329. int out_len)
  1330. {
  1331. struct ib_uverbs_create_qp cmd;
  1332. struct ib_uverbs_ex_create_qp cmd_ex;
  1333. struct ib_udata ucore;
  1334. struct ib_udata uhw;
  1335. ssize_t resp_size = sizeof(struct ib_uverbs_create_qp_resp);
  1336. int err;
  1337. if (out_len < resp_size)
  1338. return -ENOSPC;
  1339. if (copy_from_user(&cmd, buf, sizeof(cmd)))
  1340. return -EFAULT;
  1341. ib_uverbs_init_udata(&ucore, buf, u64_to_user_ptr(cmd.response),
  1342. sizeof(cmd), resp_size);
  1343. ib_uverbs_init_udata(&uhw, buf + sizeof(cmd),
  1344. u64_to_user_ptr(cmd.response) + resp_size,
  1345. in_len - sizeof(cmd) - sizeof(struct ib_uverbs_cmd_hdr),
  1346. out_len - resp_size);
  1347. memset(&cmd_ex, 0, sizeof(cmd_ex));
  1348. cmd_ex.user_handle = cmd.user_handle;
  1349. cmd_ex.pd_handle = cmd.pd_handle;
  1350. cmd_ex.send_cq_handle = cmd.send_cq_handle;
  1351. cmd_ex.recv_cq_handle = cmd.recv_cq_handle;
  1352. cmd_ex.srq_handle = cmd.srq_handle;
  1353. cmd_ex.max_send_wr = cmd.max_send_wr;
  1354. cmd_ex.max_recv_wr = cmd.max_recv_wr;
  1355. cmd_ex.max_send_sge = cmd.max_send_sge;
  1356. cmd_ex.max_recv_sge = cmd.max_recv_sge;
  1357. cmd_ex.max_inline_data = cmd.max_inline_data;
  1358. cmd_ex.sq_sig_all = cmd.sq_sig_all;
  1359. cmd_ex.qp_type = cmd.qp_type;
  1360. cmd_ex.is_srq = cmd.is_srq;
  1361. err = create_qp(file, &ucore, &uhw, &cmd_ex,
  1362. offsetof(typeof(cmd_ex), is_srq) +
  1363. sizeof(cmd.is_srq), ib_uverbs_create_qp_cb,
  1364. NULL);
  1365. if (err)
  1366. return err;
  1367. return in_len;
  1368. }
  1369. static int ib_uverbs_ex_create_qp_cb(struct ib_uverbs_file *file,
  1370. struct ib_uverbs_ex_create_qp_resp *resp,
  1371. struct ib_udata *ucore)
  1372. {
  1373. if (ib_copy_to_udata(ucore, resp, resp->response_length))
  1374. return -EFAULT;
  1375. return 0;
  1376. }
  1377. int ib_uverbs_ex_create_qp(struct ib_uverbs_file *file,
  1378. struct ib_udata *ucore,
  1379. struct ib_udata *uhw)
  1380. {
  1381. struct ib_uverbs_ex_create_qp_resp resp;
  1382. struct ib_uverbs_ex_create_qp cmd = {0};
  1383. int err;
  1384. if (ucore->inlen < (offsetof(typeof(cmd), comp_mask) +
  1385. sizeof(cmd.comp_mask)))
  1386. return -EINVAL;
  1387. err = ib_copy_from_udata(&cmd, ucore, min(sizeof(cmd), ucore->inlen));
  1388. if (err)
  1389. return err;
  1390. if (cmd.comp_mask & ~IB_UVERBS_CREATE_QP_SUP_COMP_MASK)
  1391. return -EINVAL;
  1392. if (cmd.reserved)
  1393. return -EINVAL;
  1394. if (ucore->outlen < (offsetof(typeof(resp), response_length) +
  1395. sizeof(resp.response_length)))
  1396. return -ENOSPC;
  1397. err = create_qp(file, ucore, uhw, &cmd,
  1398. min(ucore->inlen, sizeof(cmd)),
  1399. ib_uverbs_ex_create_qp_cb, NULL);
  1400. if (err)
  1401. return err;
  1402. return 0;
  1403. }
  1404. ssize_t ib_uverbs_open_qp(struct ib_uverbs_file *file,
  1405. const char __user *buf, int in_len, int out_len)
  1406. {
  1407. struct ib_uverbs_open_qp cmd;
  1408. struct ib_uverbs_create_qp_resp resp;
  1409. struct ib_udata udata;
  1410. struct ib_uqp_object *obj;
  1411. struct ib_xrcd *xrcd;
  1412. struct ib_uobject *uninitialized_var(xrcd_uobj);
  1413. struct ib_qp *qp;
  1414. struct ib_qp_open_attr attr;
  1415. int ret;
  1416. struct ib_device *ib_dev;
  1417. if (out_len < sizeof resp)
  1418. return -ENOSPC;
  1419. if (copy_from_user(&cmd, buf, sizeof cmd))
  1420. return -EFAULT;
  1421. ib_uverbs_init_udata(&udata, buf + sizeof(cmd),
  1422. u64_to_user_ptr(cmd.response) + sizeof(resp),
  1423. in_len - sizeof(cmd) - sizeof(struct ib_uverbs_cmd_hdr),
  1424. out_len - sizeof(resp));
  1425. obj = (struct ib_uqp_object *)uobj_alloc(UVERBS_OBJECT_QP, file,
  1426. &ib_dev);
  1427. if (IS_ERR(obj))
  1428. return PTR_ERR(obj);
  1429. xrcd_uobj = uobj_get_read(UVERBS_OBJECT_XRCD, cmd.pd_handle, file);
  1430. if (IS_ERR(xrcd_uobj)) {
  1431. ret = -EINVAL;
  1432. goto err_put;
  1433. }
  1434. xrcd = (struct ib_xrcd *)xrcd_uobj->object;
  1435. if (!xrcd) {
  1436. ret = -EINVAL;
  1437. goto err_xrcd;
  1438. }
  1439. attr.event_handler = ib_uverbs_qp_event_handler;
  1440. attr.qp_context = file;
  1441. attr.qp_num = cmd.qpn;
  1442. attr.qp_type = cmd.qp_type;
  1443. obj->uevent.events_reported = 0;
  1444. INIT_LIST_HEAD(&obj->uevent.event_list);
  1445. INIT_LIST_HEAD(&obj->mcast_list);
  1446. qp = ib_open_qp(xrcd, &attr);
  1447. if (IS_ERR(qp)) {
  1448. ret = PTR_ERR(qp);
  1449. goto err_xrcd;
  1450. }
  1451. obj->uevent.uobject.object = qp;
  1452. obj->uevent.uobject.user_handle = cmd.user_handle;
  1453. memset(&resp, 0, sizeof resp);
  1454. resp.qpn = qp->qp_num;
  1455. resp.qp_handle = obj->uevent.uobject.id;
  1456. if (copy_to_user(u64_to_user_ptr(cmd.response), &resp, sizeof resp)) {
  1457. ret = -EFAULT;
  1458. goto err_destroy;
  1459. }
  1460. obj->uxrcd = container_of(xrcd_uobj, struct ib_uxrcd_object, uobject);
  1461. atomic_inc(&obj->uxrcd->refcnt);
  1462. qp->uobject = &obj->uevent.uobject;
  1463. uobj_put_read(xrcd_uobj);
  1464. return uobj_alloc_commit(&obj->uevent.uobject, in_len);
  1465. err_destroy:
  1466. ib_destroy_qp(qp);
  1467. err_xrcd:
  1468. uobj_put_read(xrcd_uobj);
  1469. err_put:
  1470. uobj_alloc_abort(&obj->uevent.uobject);
  1471. return ret;
  1472. }
  1473. static void copy_ah_attr_to_uverbs(struct ib_uverbs_qp_dest *uverb_attr,
  1474. struct rdma_ah_attr *rdma_attr)
  1475. {
  1476. const struct ib_global_route *grh;
  1477. uverb_attr->dlid = rdma_ah_get_dlid(rdma_attr);
  1478. uverb_attr->sl = rdma_ah_get_sl(rdma_attr);
  1479. uverb_attr->src_path_bits = rdma_ah_get_path_bits(rdma_attr);
  1480. uverb_attr->static_rate = rdma_ah_get_static_rate(rdma_attr);
  1481. uverb_attr->is_global = !!(rdma_ah_get_ah_flags(rdma_attr) &
  1482. IB_AH_GRH);
  1483. if (uverb_attr->is_global) {
  1484. grh = rdma_ah_read_grh(rdma_attr);
  1485. memcpy(uverb_attr->dgid, grh->dgid.raw, 16);
  1486. uverb_attr->flow_label = grh->flow_label;
  1487. uverb_attr->sgid_index = grh->sgid_index;
  1488. uverb_attr->hop_limit = grh->hop_limit;
  1489. uverb_attr->traffic_class = grh->traffic_class;
  1490. }
  1491. uverb_attr->port_num = rdma_ah_get_port_num(rdma_attr);
  1492. }
  1493. ssize_t ib_uverbs_query_qp(struct ib_uverbs_file *file,
  1494. const char __user *buf, int in_len,
  1495. int out_len)
  1496. {
  1497. struct ib_uverbs_query_qp cmd;
  1498. struct ib_uverbs_query_qp_resp resp;
  1499. struct ib_qp *qp;
  1500. struct ib_qp_attr *attr;
  1501. struct ib_qp_init_attr *init_attr;
  1502. int ret;
  1503. if (copy_from_user(&cmd, buf, sizeof cmd))
  1504. return -EFAULT;
  1505. attr = kmalloc(sizeof *attr, GFP_KERNEL);
  1506. init_attr = kmalloc(sizeof *init_attr, GFP_KERNEL);
  1507. if (!attr || !init_attr) {
  1508. ret = -ENOMEM;
  1509. goto out;
  1510. }
  1511. qp = uobj_get_obj_read(qp, UVERBS_OBJECT_QP, cmd.qp_handle, file);
  1512. if (!qp) {
  1513. ret = -EINVAL;
  1514. goto out;
  1515. }
  1516. ret = ib_query_qp(qp, attr, cmd.attr_mask, init_attr);
  1517. uobj_put_obj_read(qp);
  1518. if (ret)
  1519. goto out;
  1520. memset(&resp, 0, sizeof resp);
  1521. resp.qp_state = attr->qp_state;
  1522. resp.cur_qp_state = attr->cur_qp_state;
  1523. resp.path_mtu = attr->path_mtu;
  1524. resp.path_mig_state = attr->path_mig_state;
  1525. resp.qkey = attr->qkey;
  1526. resp.rq_psn = attr->rq_psn;
  1527. resp.sq_psn = attr->sq_psn;
  1528. resp.dest_qp_num = attr->dest_qp_num;
  1529. resp.qp_access_flags = attr->qp_access_flags;
  1530. resp.pkey_index = attr->pkey_index;
  1531. resp.alt_pkey_index = attr->alt_pkey_index;
  1532. resp.sq_draining = attr->sq_draining;
  1533. resp.max_rd_atomic = attr->max_rd_atomic;
  1534. resp.max_dest_rd_atomic = attr->max_dest_rd_atomic;
  1535. resp.min_rnr_timer = attr->min_rnr_timer;
  1536. resp.port_num = attr->port_num;
  1537. resp.timeout = attr->timeout;
  1538. resp.retry_cnt = attr->retry_cnt;
  1539. resp.rnr_retry = attr->rnr_retry;
  1540. resp.alt_port_num = attr->alt_port_num;
  1541. resp.alt_timeout = attr->alt_timeout;
  1542. copy_ah_attr_to_uverbs(&resp.dest, &attr->ah_attr);
  1543. copy_ah_attr_to_uverbs(&resp.alt_dest, &attr->alt_ah_attr);
  1544. resp.max_send_wr = init_attr->cap.max_send_wr;
  1545. resp.max_recv_wr = init_attr->cap.max_recv_wr;
  1546. resp.max_send_sge = init_attr->cap.max_send_sge;
  1547. resp.max_recv_sge = init_attr->cap.max_recv_sge;
  1548. resp.max_inline_data = init_attr->cap.max_inline_data;
  1549. resp.sq_sig_all = init_attr->sq_sig_type == IB_SIGNAL_ALL_WR;
  1550. if (copy_to_user(u64_to_user_ptr(cmd.response), &resp, sizeof resp))
  1551. ret = -EFAULT;
  1552. out:
  1553. kfree(attr);
  1554. kfree(init_attr);
  1555. return ret ? ret : in_len;
  1556. }
  1557. /* Remove ignored fields set in the attribute mask */
  1558. static int modify_qp_mask(enum ib_qp_type qp_type, int mask)
  1559. {
  1560. switch (qp_type) {
  1561. case IB_QPT_XRC_INI:
  1562. return mask & ~(IB_QP_MAX_DEST_RD_ATOMIC | IB_QP_MIN_RNR_TIMER);
  1563. case IB_QPT_XRC_TGT:
  1564. return mask & ~(IB_QP_MAX_QP_RD_ATOMIC | IB_QP_RETRY_CNT |
  1565. IB_QP_RNR_RETRY);
  1566. default:
  1567. return mask;
  1568. }
  1569. }
  1570. static void copy_ah_attr_from_uverbs(struct ib_device *dev,
  1571. struct rdma_ah_attr *rdma_attr,
  1572. struct ib_uverbs_qp_dest *uverb_attr)
  1573. {
  1574. rdma_attr->type = rdma_ah_find_type(dev, uverb_attr->port_num);
  1575. if (uverb_attr->is_global) {
  1576. rdma_ah_set_grh(rdma_attr, NULL,
  1577. uverb_attr->flow_label,
  1578. uverb_attr->sgid_index,
  1579. uverb_attr->hop_limit,
  1580. uverb_attr->traffic_class);
  1581. rdma_ah_set_dgid_raw(rdma_attr, uverb_attr->dgid);
  1582. } else {
  1583. rdma_ah_set_ah_flags(rdma_attr, 0);
  1584. }
  1585. rdma_ah_set_dlid(rdma_attr, uverb_attr->dlid);
  1586. rdma_ah_set_sl(rdma_attr, uverb_attr->sl);
  1587. rdma_ah_set_path_bits(rdma_attr, uverb_attr->src_path_bits);
  1588. rdma_ah_set_static_rate(rdma_attr, uverb_attr->static_rate);
  1589. rdma_ah_set_port_num(rdma_attr, uverb_attr->port_num);
  1590. rdma_ah_set_make_grd(rdma_attr, false);
  1591. }
  1592. static int modify_qp(struct ib_uverbs_file *file,
  1593. struct ib_uverbs_ex_modify_qp *cmd, struct ib_udata *udata)
  1594. {
  1595. struct ib_qp_attr *attr;
  1596. struct ib_qp *qp;
  1597. int ret;
  1598. attr = kzalloc(sizeof(*attr), GFP_KERNEL);
  1599. if (!attr)
  1600. return -ENOMEM;
  1601. qp = uobj_get_obj_read(qp, UVERBS_OBJECT_QP, cmd->base.qp_handle, file);
  1602. if (!qp) {
  1603. ret = -EINVAL;
  1604. goto out;
  1605. }
  1606. if ((cmd->base.attr_mask & IB_QP_PORT) &&
  1607. !rdma_is_port_valid(qp->device, cmd->base.port_num)) {
  1608. ret = -EINVAL;
  1609. goto release_qp;
  1610. }
  1611. if ((cmd->base.attr_mask & IB_QP_AV)) {
  1612. if (!rdma_is_port_valid(qp->device, cmd->base.dest.port_num)) {
  1613. ret = -EINVAL;
  1614. goto release_qp;
  1615. }
  1616. if (cmd->base.attr_mask & IB_QP_STATE &&
  1617. cmd->base.qp_state == IB_QPS_RTR) {
  1618. /* We are in INIT->RTR TRANSITION (if we are not,
  1619. * this transition will be rejected in subsequent checks).
  1620. * In the INIT->RTR transition, we cannot have IB_QP_PORT set,
  1621. * but the IB_QP_STATE flag is required.
  1622. *
  1623. * Since kernel 3.14 (commit dbf727de7440), the uverbs driver,
  1624. * when IB_QP_AV is set, has required inclusion of a valid
  1625. * port number in the primary AV. (AVs are created and handled
  1626. * differently for infiniband and ethernet (RoCE) ports).
  1627. *
  1628. * Check the port number included in the primary AV against
  1629. * the port number in the qp struct, which was set (and saved)
  1630. * in the RST->INIT transition.
  1631. */
  1632. if (cmd->base.dest.port_num != qp->real_qp->port) {
  1633. ret = -EINVAL;
  1634. goto release_qp;
  1635. }
  1636. } else {
  1637. /* We are in SQD->SQD. (If we are not, this transition will
  1638. * be rejected later in the verbs layer checks).
  1639. * Check for both IB_QP_PORT and IB_QP_AV, these can be set
  1640. * together in the SQD->SQD transition.
  1641. *
  1642. * If only IP_QP_AV was set, add in IB_QP_PORT as well (the
  1643. * verbs layer driver does not track primary port changes
  1644. * resulting from path migration. Thus, in SQD, if the primary
  1645. * AV is modified, the primary port should also be modified).
  1646. *
  1647. * Note that in this transition, the IB_QP_STATE flag
  1648. * is not allowed.
  1649. */
  1650. if (((cmd->base.attr_mask & (IB_QP_AV | IB_QP_PORT))
  1651. == (IB_QP_AV | IB_QP_PORT)) &&
  1652. cmd->base.port_num != cmd->base.dest.port_num) {
  1653. ret = -EINVAL;
  1654. goto release_qp;
  1655. }
  1656. if ((cmd->base.attr_mask & (IB_QP_AV | IB_QP_PORT))
  1657. == IB_QP_AV) {
  1658. cmd->base.attr_mask |= IB_QP_PORT;
  1659. cmd->base.port_num = cmd->base.dest.port_num;
  1660. }
  1661. }
  1662. }
  1663. if ((cmd->base.attr_mask & IB_QP_ALT_PATH) &&
  1664. (!rdma_is_port_valid(qp->device, cmd->base.alt_port_num) ||
  1665. !rdma_is_port_valid(qp->device, cmd->base.alt_dest.port_num) ||
  1666. cmd->base.alt_port_num != cmd->base.alt_dest.port_num)) {
  1667. ret = -EINVAL;
  1668. goto release_qp;
  1669. }
  1670. if ((cmd->base.attr_mask & IB_QP_CUR_STATE &&
  1671. cmd->base.cur_qp_state > IB_QPS_ERR) ||
  1672. (cmd->base.attr_mask & IB_QP_STATE &&
  1673. cmd->base.qp_state > IB_QPS_ERR)) {
  1674. ret = -EINVAL;
  1675. goto release_qp;
  1676. }
  1677. if (cmd->base.attr_mask & IB_QP_STATE)
  1678. attr->qp_state = cmd->base.qp_state;
  1679. if (cmd->base.attr_mask & IB_QP_CUR_STATE)
  1680. attr->cur_qp_state = cmd->base.cur_qp_state;
  1681. if (cmd->base.attr_mask & IB_QP_PATH_MTU)
  1682. attr->path_mtu = cmd->base.path_mtu;
  1683. if (cmd->base.attr_mask & IB_QP_PATH_MIG_STATE)
  1684. attr->path_mig_state = cmd->base.path_mig_state;
  1685. if (cmd->base.attr_mask & IB_QP_QKEY)
  1686. attr->qkey = cmd->base.qkey;
  1687. if (cmd->base.attr_mask & IB_QP_RQ_PSN)
  1688. attr->rq_psn = cmd->base.rq_psn;
  1689. if (cmd->base.attr_mask & IB_QP_SQ_PSN)
  1690. attr->sq_psn = cmd->base.sq_psn;
  1691. if (cmd->base.attr_mask & IB_QP_DEST_QPN)
  1692. attr->dest_qp_num = cmd->base.dest_qp_num;
  1693. if (cmd->base.attr_mask & IB_QP_ACCESS_FLAGS)
  1694. attr->qp_access_flags = cmd->base.qp_access_flags;
  1695. if (cmd->base.attr_mask & IB_QP_PKEY_INDEX)
  1696. attr->pkey_index = cmd->base.pkey_index;
  1697. if (cmd->base.attr_mask & IB_QP_EN_SQD_ASYNC_NOTIFY)
  1698. attr->en_sqd_async_notify = cmd->base.en_sqd_async_notify;
  1699. if (cmd->base.attr_mask & IB_QP_MAX_QP_RD_ATOMIC)
  1700. attr->max_rd_atomic = cmd->base.max_rd_atomic;
  1701. if (cmd->base.attr_mask & IB_QP_MAX_DEST_RD_ATOMIC)
  1702. attr->max_dest_rd_atomic = cmd->base.max_dest_rd_atomic;
  1703. if (cmd->base.attr_mask & IB_QP_MIN_RNR_TIMER)
  1704. attr->min_rnr_timer = cmd->base.min_rnr_timer;
  1705. if (cmd->base.attr_mask & IB_QP_PORT)
  1706. attr->port_num = cmd->base.port_num;
  1707. if (cmd->base.attr_mask & IB_QP_TIMEOUT)
  1708. attr->timeout = cmd->base.timeout;
  1709. if (cmd->base.attr_mask & IB_QP_RETRY_CNT)
  1710. attr->retry_cnt = cmd->base.retry_cnt;
  1711. if (cmd->base.attr_mask & IB_QP_RNR_RETRY)
  1712. attr->rnr_retry = cmd->base.rnr_retry;
  1713. if (cmd->base.attr_mask & IB_QP_ALT_PATH) {
  1714. attr->alt_port_num = cmd->base.alt_port_num;
  1715. attr->alt_timeout = cmd->base.alt_timeout;
  1716. attr->alt_pkey_index = cmd->base.alt_pkey_index;
  1717. }
  1718. if (cmd->base.attr_mask & IB_QP_RATE_LIMIT)
  1719. attr->rate_limit = cmd->rate_limit;
  1720. if (cmd->base.attr_mask & IB_QP_AV)
  1721. copy_ah_attr_from_uverbs(qp->device, &attr->ah_attr,
  1722. &cmd->base.dest);
  1723. if (cmd->base.attr_mask & IB_QP_ALT_PATH)
  1724. copy_ah_attr_from_uverbs(qp->device, &attr->alt_ah_attr,
  1725. &cmd->base.alt_dest);
  1726. ret = ib_modify_qp_with_udata(qp, attr,
  1727. modify_qp_mask(qp->qp_type,
  1728. cmd->base.attr_mask),
  1729. udata);
  1730. release_qp:
  1731. uobj_put_obj_read(qp);
  1732. out:
  1733. kfree(attr);
  1734. return ret;
  1735. }
  1736. ssize_t ib_uverbs_modify_qp(struct ib_uverbs_file *file,
  1737. const char __user *buf, int in_len,
  1738. int out_len)
  1739. {
  1740. struct ib_uverbs_ex_modify_qp cmd = {};
  1741. struct ib_udata udata;
  1742. int ret;
  1743. if (copy_from_user(&cmd.base, buf, sizeof(cmd.base)))
  1744. return -EFAULT;
  1745. if (cmd.base.attr_mask &
  1746. ~((IB_USER_LEGACY_LAST_QP_ATTR_MASK << 1) - 1))
  1747. return -EOPNOTSUPP;
  1748. ib_uverbs_init_udata(&udata, buf + sizeof(cmd.base), NULL,
  1749. in_len - sizeof(cmd.base) - sizeof(struct ib_uverbs_cmd_hdr),
  1750. out_len);
  1751. ret = modify_qp(file, &cmd, &udata);
  1752. if (ret)
  1753. return ret;
  1754. return in_len;
  1755. }
  1756. int ib_uverbs_ex_modify_qp(struct ib_uverbs_file *file,
  1757. struct ib_udata *ucore,
  1758. struct ib_udata *uhw)
  1759. {
  1760. struct ib_uverbs_ex_modify_qp cmd = {};
  1761. int ret;
  1762. /*
  1763. * Last bit is reserved for extending the attr_mask by
  1764. * using another field.
  1765. */
  1766. BUILD_BUG_ON(IB_USER_LAST_QP_ATTR_MASK == (1 << 31));
  1767. if (ucore->inlen < sizeof(cmd.base))
  1768. return -EINVAL;
  1769. ret = ib_copy_from_udata(&cmd, ucore, min(sizeof(cmd), ucore->inlen));
  1770. if (ret)
  1771. return ret;
  1772. if (cmd.base.attr_mask &
  1773. ~((IB_USER_LAST_QP_ATTR_MASK << 1) - 1))
  1774. return -EOPNOTSUPP;
  1775. if (ucore->inlen > sizeof(cmd)) {
  1776. if (!ib_is_udata_cleared(ucore, sizeof(cmd),
  1777. ucore->inlen - sizeof(cmd)))
  1778. return -EOPNOTSUPP;
  1779. }
  1780. ret = modify_qp(file, &cmd, uhw);
  1781. return ret;
  1782. }
  1783. ssize_t ib_uverbs_destroy_qp(struct ib_uverbs_file *file,
  1784. const char __user *buf, int in_len,
  1785. int out_len)
  1786. {
  1787. struct ib_uverbs_destroy_qp cmd;
  1788. struct ib_uverbs_destroy_qp_resp resp;
  1789. struct ib_uobject *uobj;
  1790. struct ib_uqp_object *obj;
  1791. if (copy_from_user(&cmd, buf, sizeof cmd))
  1792. return -EFAULT;
  1793. uobj = uobj_get_destroy(UVERBS_OBJECT_QP, cmd.qp_handle, file);
  1794. if (IS_ERR(uobj))
  1795. return PTR_ERR(uobj);
  1796. obj = container_of(uobj, struct ib_uqp_object, uevent.uobject);
  1797. memset(&resp, 0, sizeof(resp));
  1798. resp.events_reported = obj->uevent.events_reported;
  1799. uobj_put_destroy(uobj);
  1800. if (copy_to_user(u64_to_user_ptr(cmd.response), &resp, sizeof resp))
  1801. return -EFAULT;
  1802. return in_len;
  1803. }
  1804. static void *alloc_wr(size_t wr_size, __u32 num_sge)
  1805. {
  1806. if (num_sge >= (U32_MAX - ALIGN(wr_size, sizeof (struct ib_sge))) /
  1807. sizeof (struct ib_sge))
  1808. return NULL;
  1809. return kmalloc(ALIGN(wr_size, sizeof (struct ib_sge)) +
  1810. num_sge * sizeof (struct ib_sge), GFP_KERNEL);
  1811. }
  1812. ssize_t ib_uverbs_post_send(struct ib_uverbs_file *file,
  1813. const char __user *buf, int in_len,
  1814. int out_len)
  1815. {
  1816. struct ib_uverbs_post_send cmd;
  1817. struct ib_uverbs_post_send_resp resp;
  1818. struct ib_uverbs_send_wr *user_wr;
  1819. struct ib_send_wr *wr = NULL, *last, *next;
  1820. const struct ib_send_wr *bad_wr;
  1821. struct ib_qp *qp;
  1822. int i, sg_ind;
  1823. int is_ud;
  1824. ssize_t ret = -EINVAL;
  1825. size_t next_size;
  1826. if (copy_from_user(&cmd, buf, sizeof cmd))
  1827. return -EFAULT;
  1828. if (in_len < sizeof cmd + cmd.wqe_size * cmd.wr_count +
  1829. cmd.sge_count * sizeof (struct ib_uverbs_sge))
  1830. return -EINVAL;
  1831. if (cmd.wqe_size < sizeof (struct ib_uverbs_send_wr))
  1832. return -EINVAL;
  1833. user_wr = kmalloc(cmd.wqe_size, GFP_KERNEL);
  1834. if (!user_wr)
  1835. return -ENOMEM;
  1836. qp = uobj_get_obj_read(qp, UVERBS_OBJECT_QP, cmd.qp_handle, file);
  1837. if (!qp)
  1838. goto out;
  1839. is_ud = qp->qp_type == IB_QPT_UD;
  1840. sg_ind = 0;
  1841. last = NULL;
  1842. for (i = 0; i < cmd.wr_count; ++i) {
  1843. if (copy_from_user(user_wr,
  1844. buf + sizeof cmd + i * cmd.wqe_size,
  1845. cmd.wqe_size)) {
  1846. ret = -EFAULT;
  1847. goto out_put;
  1848. }
  1849. if (user_wr->num_sge + sg_ind > cmd.sge_count) {
  1850. ret = -EINVAL;
  1851. goto out_put;
  1852. }
  1853. if (is_ud) {
  1854. struct ib_ud_wr *ud;
  1855. if (user_wr->opcode != IB_WR_SEND &&
  1856. user_wr->opcode != IB_WR_SEND_WITH_IMM) {
  1857. ret = -EINVAL;
  1858. goto out_put;
  1859. }
  1860. next_size = sizeof(*ud);
  1861. ud = alloc_wr(next_size, user_wr->num_sge);
  1862. if (!ud) {
  1863. ret = -ENOMEM;
  1864. goto out_put;
  1865. }
  1866. ud->ah = uobj_get_obj_read(ah, UVERBS_OBJECT_AH,
  1867. user_wr->wr.ud.ah, file);
  1868. if (!ud->ah) {
  1869. kfree(ud);
  1870. ret = -EINVAL;
  1871. goto out_put;
  1872. }
  1873. ud->remote_qpn = user_wr->wr.ud.remote_qpn;
  1874. ud->remote_qkey = user_wr->wr.ud.remote_qkey;
  1875. next = &ud->wr;
  1876. } else if (user_wr->opcode == IB_WR_RDMA_WRITE_WITH_IMM ||
  1877. user_wr->opcode == IB_WR_RDMA_WRITE ||
  1878. user_wr->opcode == IB_WR_RDMA_READ) {
  1879. struct ib_rdma_wr *rdma;
  1880. next_size = sizeof(*rdma);
  1881. rdma = alloc_wr(next_size, user_wr->num_sge);
  1882. if (!rdma) {
  1883. ret = -ENOMEM;
  1884. goto out_put;
  1885. }
  1886. rdma->remote_addr = user_wr->wr.rdma.remote_addr;
  1887. rdma->rkey = user_wr->wr.rdma.rkey;
  1888. next = &rdma->wr;
  1889. } else if (user_wr->opcode == IB_WR_ATOMIC_CMP_AND_SWP ||
  1890. user_wr->opcode == IB_WR_ATOMIC_FETCH_AND_ADD) {
  1891. struct ib_atomic_wr *atomic;
  1892. next_size = sizeof(*atomic);
  1893. atomic = alloc_wr(next_size, user_wr->num_sge);
  1894. if (!atomic) {
  1895. ret = -ENOMEM;
  1896. goto out_put;
  1897. }
  1898. atomic->remote_addr = user_wr->wr.atomic.remote_addr;
  1899. atomic->compare_add = user_wr->wr.atomic.compare_add;
  1900. atomic->swap = user_wr->wr.atomic.swap;
  1901. atomic->rkey = user_wr->wr.atomic.rkey;
  1902. next = &atomic->wr;
  1903. } else if (user_wr->opcode == IB_WR_SEND ||
  1904. user_wr->opcode == IB_WR_SEND_WITH_IMM ||
  1905. user_wr->opcode == IB_WR_SEND_WITH_INV) {
  1906. next_size = sizeof(*next);
  1907. next = alloc_wr(next_size, user_wr->num_sge);
  1908. if (!next) {
  1909. ret = -ENOMEM;
  1910. goto out_put;
  1911. }
  1912. } else {
  1913. ret = -EINVAL;
  1914. goto out_put;
  1915. }
  1916. if (user_wr->opcode == IB_WR_SEND_WITH_IMM ||
  1917. user_wr->opcode == IB_WR_RDMA_WRITE_WITH_IMM) {
  1918. next->ex.imm_data =
  1919. (__be32 __force) user_wr->ex.imm_data;
  1920. } else if (user_wr->opcode == IB_WR_SEND_WITH_INV) {
  1921. next->ex.invalidate_rkey = user_wr->ex.invalidate_rkey;
  1922. }
  1923. if (!last)
  1924. wr = next;
  1925. else
  1926. last->next = next;
  1927. last = next;
  1928. next->next = NULL;
  1929. next->wr_id = user_wr->wr_id;
  1930. next->num_sge = user_wr->num_sge;
  1931. next->opcode = user_wr->opcode;
  1932. next->send_flags = user_wr->send_flags;
  1933. if (next->num_sge) {
  1934. next->sg_list = (void *) next +
  1935. ALIGN(next_size, sizeof(struct ib_sge));
  1936. if (copy_from_user(next->sg_list,
  1937. buf + sizeof cmd +
  1938. cmd.wr_count * cmd.wqe_size +
  1939. sg_ind * sizeof (struct ib_sge),
  1940. next->num_sge * sizeof (struct ib_sge))) {
  1941. ret = -EFAULT;
  1942. goto out_put;
  1943. }
  1944. sg_ind += next->num_sge;
  1945. } else
  1946. next->sg_list = NULL;
  1947. }
  1948. resp.bad_wr = 0;
  1949. ret = qp->device->post_send(qp->real_qp, wr, &bad_wr);
  1950. if (ret)
  1951. for (next = wr; next; next = next->next) {
  1952. ++resp.bad_wr;
  1953. if (next == bad_wr)
  1954. break;
  1955. }
  1956. if (copy_to_user(u64_to_user_ptr(cmd.response), &resp, sizeof resp))
  1957. ret = -EFAULT;
  1958. out_put:
  1959. uobj_put_obj_read(qp);
  1960. while (wr) {
  1961. if (is_ud && ud_wr(wr)->ah)
  1962. uobj_put_obj_read(ud_wr(wr)->ah);
  1963. next = wr->next;
  1964. kfree(wr);
  1965. wr = next;
  1966. }
  1967. out:
  1968. kfree(user_wr);
  1969. return ret ? ret : in_len;
  1970. }
  1971. static struct ib_recv_wr *ib_uverbs_unmarshall_recv(const char __user *buf,
  1972. int in_len,
  1973. u32 wr_count,
  1974. u32 sge_count,
  1975. u32 wqe_size)
  1976. {
  1977. struct ib_uverbs_recv_wr *user_wr;
  1978. struct ib_recv_wr *wr = NULL, *last, *next;
  1979. int sg_ind;
  1980. int i;
  1981. int ret;
  1982. if (in_len < wqe_size * wr_count +
  1983. sge_count * sizeof (struct ib_uverbs_sge))
  1984. return ERR_PTR(-EINVAL);
  1985. if (wqe_size < sizeof (struct ib_uverbs_recv_wr))
  1986. return ERR_PTR(-EINVAL);
  1987. user_wr = kmalloc(wqe_size, GFP_KERNEL);
  1988. if (!user_wr)
  1989. return ERR_PTR(-ENOMEM);
  1990. sg_ind = 0;
  1991. last = NULL;
  1992. for (i = 0; i < wr_count; ++i) {
  1993. if (copy_from_user(user_wr, buf + i * wqe_size,
  1994. wqe_size)) {
  1995. ret = -EFAULT;
  1996. goto err;
  1997. }
  1998. if (user_wr->num_sge + sg_ind > sge_count) {
  1999. ret = -EINVAL;
  2000. goto err;
  2001. }
  2002. if (user_wr->num_sge >=
  2003. (U32_MAX - ALIGN(sizeof *next, sizeof (struct ib_sge))) /
  2004. sizeof (struct ib_sge)) {
  2005. ret = -EINVAL;
  2006. goto err;
  2007. }
  2008. next = kmalloc(ALIGN(sizeof *next, sizeof (struct ib_sge)) +
  2009. user_wr->num_sge * sizeof (struct ib_sge),
  2010. GFP_KERNEL);
  2011. if (!next) {
  2012. ret = -ENOMEM;
  2013. goto err;
  2014. }
  2015. if (!last)
  2016. wr = next;
  2017. else
  2018. last->next = next;
  2019. last = next;
  2020. next->next = NULL;
  2021. next->wr_id = user_wr->wr_id;
  2022. next->num_sge = user_wr->num_sge;
  2023. if (next->num_sge) {
  2024. next->sg_list = (void *) next +
  2025. ALIGN(sizeof *next, sizeof (struct ib_sge));
  2026. if (copy_from_user(next->sg_list,
  2027. buf + wr_count * wqe_size +
  2028. sg_ind * sizeof (struct ib_sge),
  2029. next->num_sge * sizeof (struct ib_sge))) {
  2030. ret = -EFAULT;
  2031. goto err;
  2032. }
  2033. sg_ind += next->num_sge;
  2034. } else
  2035. next->sg_list = NULL;
  2036. }
  2037. kfree(user_wr);
  2038. return wr;
  2039. err:
  2040. kfree(user_wr);
  2041. while (wr) {
  2042. next = wr->next;
  2043. kfree(wr);
  2044. wr = next;
  2045. }
  2046. return ERR_PTR(ret);
  2047. }
  2048. ssize_t ib_uverbs_post_recv(struct ib_uverbs_file *file,
  2049. const char __user *buf, int in_len,
  2050. int out_len)
  2051. {
  2052. struct ib_uverbs_post_recv cmd;
  2053. struct ib_uverbs_post_recv_resp resp;
  2054. struct ib_recv_wr *wr, *next;
  2055. const struct ib_recv_wr *bad_wr;
  2056. struct ib_qp *qp;
  2057. ssize_t ret = -EINVAL;
  2058. if (copy_from_user(&cmd, buf, sizeof cmd))
  2059. return -EFAULT;
  2060. wr = ib_uverbs_unmarshall_recv(buf + sizeof cmd,
  2061. in_len - sizeof cmd, cmd.wr_count,
  2062. cmd.sge_count, cmd.wqe_size);
  2063. if (IS_ERR(wr))
  2064. return PTR_ERR(wr);
  2065. qp = uobj_get_obj_read(qp, UVERBS_OBJECT_QP, cmd.qp_handle, file);
  2066. if (!qp)
  2067. goto out;
  2068. resp.bad_wr = 0;
  2069. ret = qp->device->post_recv(qp->real_qp, wr, &bad_wr);
  2070. uobj_put_obj_read(qp);
  2071. if (ret) {
  2072. for (next = wr; next; next = next->next) {
  2073. ++resp.bad_wr;
  2074. if (next == bad_wr)
  2075. break;
  2076. }
  2077. }
  2078. if (copy_to_user(u64_to_user_ptr(cmd.response), &resp, sizeof resp))
  2079. ret = -EFAULT;
  2080. out:
  2081. while (wr) {
  2082. next = wr->next;
  2083. kfree(wr);
  2084. wr = next;
  2085. }
  2086. return ret ? ret : in_len;
  2087. }
  2088. ssize_t ib_uverbs_post_srq_recv(struct ib_uverbs_file *file,
  2089. const char __user *buf, int in_len,
  2090. int out_len)
  2091. {
  2092. struct ib_uverbs_post_srq_recv cmd;
  2093. struct ib_uverbs_post_srq_recv_resp resp;
  2094. struct ib_recv_wr *wr, *next;
  2095. const struct ib_recv_wr *bad_wr;
  2096. struct ib_srq *srq;
  2097. ssize_t ret = -EINVAL;
  2098. if (copy_from_user(&cmd, buf, sizeof cmd))
  2099. return -EFAULT;
  2100. wr = ib_uverbs_unmarshall_recv(buf + sizeof cmd,
  2101. in_len - sizeof cmd, cmd.wr_count,
  2102. cmd.sge_count, cmd.wqe_size);
  2103. if (IS_ERR(wr))
  2104. return PTR_ERR(wr);
  2105. srq = uobj_get_obj_read(srq, UVERBS_OBJECT_SRQ, cmd.srq_handle, file);
  2106. if (!srq)
  2107. goto out;
  2108. resp.bad_wr = 0;
  2109. ret = srq->device->post_srq_recv ?
  2110. srq->device->post_srq_recv(srq, wr, &bad_wr) : -EOPNOTSUPP;
  2111. uobj_put_obj_read(srq);
  2112. if (ret)
  2113. for (next = wr; next; next = next->next) {
  2114. ++resp.bad_wr;
  2115. if (next == bad_wr)
  2116. break;
  2117. }
  2118. if (copy_to_user(u64_to_user_ptr(cmd.response), &resp, sizeof resp))
  2119. ret = -EFAULT;
  2120. out:
  2121. while (wr) {
  2122. next = wr->next;
  2123. kfree(wr);
  2124. wr = next;
  2125. }
  2126. return ret ? ret : in_len;
  2127. }
  2128. ssize_t ib_uverbs_create_ah(struct ib_uverbs_file *file,
  2129. const char __user *buf, int in_len,
  2130. int out_len)
  2131. {
  2132. struct ib_uverbs_create_ah cmd;
  2133. struct ib_uverbs_create_ah_resp resp;
  2134. struct ib_uobject *uobj;
  2135. struct ib_pd *pd;
  2136. struct ib_ah *ah;
  2137. struct rdma_ah_attr attr = {};
  2138. int ret;
  2139. struct ib_udata udata;
  2140. struct ib_device *ib_dev;
  2141. if (out_len < sizeof resp)
  2142. return -ENOSPC;
  2143. if (copy_from_user(&cmd, buf, sizeof cmd))
  2144. return -EFAULT;
  2145. ib_uverbs_init_udata(&udata, buf + sizeof(cmd),
  2146. u64_to_user_ptr(cmd.response) + sizeof(resp),
  2147. in_len - sizeof(cmd) - sizeof(struct ib_uverbs_cmd_hdr),
  2148. out_len - sizeof(resp));
  2149. uobj = uobj_alloc(UVERBS_OBJECT_AH, file, &ib_dev);
  2150. if (IS_ERR(uobj))
  2151. return PTR_ERR(uobj);
  2152. if (!rdma_is_port_valid(ib_dev, cmd.attr.port_num)) {
  2153. ret = -EINVAL;
  2154. goto err;
  2155. }
  2156. pd = uobj_get_obj_read(pd, UVERBS_OBJECT_PD, cmd.pd_handle, file);
  2157. if (!pd) {
  2158. ret = -EINVAL;
  2159. goto err;
  2160. }
  2161. attr.type = rdma_ah_find_type(ib_dev, cmd.attr.port_num);
  2162. rdma_ah_set_make_grd(&attr, false);
  2163. rdma_ah_set_dlid(&attr, cmd.attr.dlid);
  2164. rdma_ah_set_sl(&attr, cmd.attr.sl);
  2165. rdma_ah_set_path_bits(&attr, cmd.attr.src_path_bits);
  2166. rdma_ah_set_static_rate(&attr, cmd.attr.static_rate);
  2167. rdma_ah_set_port_num(&attr, cmd.attr.port_num);
  2168. if (cmd.attr.is_global) {
  2169. rdma_ah_set_grh(&attr, NULL, cmd.attr.grh.flow_label,
  2170. cmd.attr.grh.sgid_index,
  2171. cmd.attr.grh.hop_limit,
  2172. cmd.attr.grh.traffic_class);
  2173. rdma_ah_set_dgid_raw(&attr, cmd.attr.grh.dgid);
  2174. } else {
  2175. rdma_ah_set_ah_flags(&attr, 0);
  2176. }
  2177. ah = rdma_create_user_ah(pd, &attr, &udata);
  2178. if (IS_ERR(ah)) {
  2179. ret = PTR_ERR(ah);
  2180. goto err_put;
  2181. }
  2182. ah->uobject = uobj;
  2183. uobj->user_handle = cmd.user_handle;
  2184. uobj->object = ah;
  2185. resp.ah_handle = uobj->id;
  2186. if (copy_to_user(u64_to_user_ptr(cmd.response), &resp, sizeof resp)) {
  2187. ret = -EFAULT;
  2188. goto err_copy;
  2189. }
  2190. uobj_put_obj_read(pd);
  2191. return uobj_alloc_commit(uobj, in_len);
  2192. err_copy:
  2193. rdma_destroy_ah(ah);
  2194. err_put:
  2195. uobj_put_obj_read(pd);
  2196. err:
  2197. uobj_alloc_abort(uobj);
  2198. return ret;
  2199. }
  2200. ssize_t ib_uverbs_destroy_ah(struct ib_uverbs_file *file,
  2201. const char __user *buf, int in_len, int out_len)
  2202. {
  2203. struct ib_uverbs_destroy_ah cmd;
  2204. if (copy_from_user(&cmd, buf, sizeof cmd))
  2205. return -EFAULT;
  2206. return uobj_perform_destroy(UVERBS_OBJECT_AH, cmd.ah_handle, file,
  2207. in_len);
  2208. }
  2209. ssize_t ib_uverbs_attach_mcast(struct ib_uverbs_file *file,
  2210. const char __user *buf, int in_len,
  2211. int out_len)
  2212. {
  2213. struct ib_uverbs_attach_mcast cmd;
  2214. struct ib_qp *qp;
  2215. struct ib_uqp_object *obj;
  2216. struct ib_uverbs_mcast_entry *mcast;
  2217. int ret;
  2218. if (copy_from_user(&cmd, buf, sizeof cmd))
  2219. return -EFAULT;
  2220. qp = uobj_get_obj_read(qp, UVERBS_OBJECT_QP, cmd.qp_handle, file);
  2221. if (!qp)
  2222. return -EINVAL;
  2223. obj = container_of(qp->uobject, struct ib_uqp_object, uevent.uobject);
  2224. mutex_lock(&obj->mcast_lock);
  2225. list_for_each_entry(mcast, &obj->mcast_list, list)
  2226. if (cmd.mlid == mcast->lid &&
  2227. !memcmp(cmd.gid, mcast->gid.raw, sizeof mcast->gid.raw)) {
  2228. ret = 0;
  2229. goto out_put;
  2230. }
  2231. mcast = kmalloc(sizeof *mcast, GFP_KERNEL);
  2232. if (!mcast) {
  2233. ret = -ENOMEM;
  2234. goto out_put;
  2235. }
  2236. mcast->lid = cmd.mlid;
  2237. memcpy(mcast->gid.raw, cmd.gid, sizeof mcast->gid.raw);
  2238. ret = ib_attach_mcast(qp, &mcast->gid, cmd.mlid);
  2239. if (!ret)
  2240. list_add_tail(&mcast->list, &obj->mcast_list);
  2241. else
  2242. kfree(mcast);
  2243. out_put:
  2244. mutex_unlock(&obj->mcast_lock);
  2245. uobj_put_obj_read(qp);
  2246. return ret ? ret : in_len;
  2247. }
  2248. ssize_t ib_uverbs_detach_mcast(struct ib_uverbs_file *file,
  2249. const char __user *buf, int in_len,
  2250. int out_len)
  2251. {
  2252. struct ib_uverbs_detach_mcast cmd;
  2253. struct ib_uqp_object *obj;
  2254. struct ib_qp *qp;
  2255. struct ib_uverbs_mcast_entry *mcast;
  2256. int ret = -EINVAL;
  2257. bool found = false;
  2258. if (copy_from_user(&cmd, buf, sizeof cmd))
  2259. return -EFAULT;
  2260. qp = uobj_get_obj_read(qp, UVERBS_OBJECT_QP, cmd.qp_handle, file);
  2261. if (!qp)
  2262. return -EINVAL;
  2263. obj = container_of(qp->uobject, struct ib_uqp_object, uevent.uobject);
  2264. mutex_lock(&obj->mcast_lock);
  2265. list_for_each_entry(mcast, &obj->mcast_list, list)
  2266. if (cmd.mlid == mcast->lid &&
  2267. !memcmp(cmd.gid, mcast->gid.raw, sizeof mcast->gid.raw)) {
  2268. list_del(&mcast->list);
  2269. kfree(mcast);
  2270. found = true;
  2271. break;
  2272. }
  2273. if (!found) {
  2274. ret = -EINVAL;
  2275. goto out_put;
  2276. }
  2277. ret = ib_detach_mcast(qp, (union ib_gid *)cmd.gid, cmd.mlid);
  2278. out_put:
  2279. mutex_unlock(&obj->mcast_lock);
  2280. uobj_put_obj_read(qp);
  2281. return ret ? ret : in_len;
  2282. }
  2283. struct ib_uflow_resources {
  2284. size_t max;
  2285. size_t num;
  2286. size_t collection_num;
  2287. size_t counters_num;
  2288. struct ib_counters **counters;
  2289. struct ib_flow_action **collection;
  2290. };
  2291. static struct ib_uflow_resources *flow_resources_alloc(size_t num_specs)
  2292. {
  2293. struct ib_uflow_resources *resources;
  2294. resources = kzalloc(sizeof(*resources), GFP_KERNEL);
  2295. if (!resources)
  2296. return NULL;
  2297. if (!num_specs)
  2298. goto out;
  2299. resources->counters =
  2300. kcalloc(num_specs, sizeof(*resources->counters), GFP_KERNEL);
  2301. resources->collection =
  2302. kcalloc(num_specs, sizeof(*resources->collection), GFP_KERNEL);
  2303. if (!resources->counters || !resources->collection)
  2304. goto err;
  2305. out:
  2306. resources->max = num_specs;
  2307. return resources;
  2308. err:
  2309. kfree(resources->counters);
  2310. kfree(resources);
  2311. return NULL;
  2312. }
  2313. void ib_uverbs_flow_resources_free(struct ib_uflow_resources *uflow_res)
  2314. {
  2315. unsigned int i;
  2316. if (!uflow_res)
  2317. return;
  2318. for (i = 0; i < uflow_res->collection_num; i++)
  2319. atomic_dec(&uflow_res->collection[i]->usecnt);
  2320. for (i = 0; i < uflow_res->counters_num; i++)
  2321. atomic_dec(&uflow_res->counters[i]->usecnt);
  2322. kfree(uflow_res->collection);
  2323. kfree(uflow_res->counters);
  2324. kfree(uflow_res);
  2325. }
  2326. static void flow_resources_add(struct ib_uflow_resources *uflow_res,
  2327. enum ib_flow_spec_type type,
  2328. void *ibobj)
  2329. {
  2330. WARN_ON(uflow_res->num >= uflow_res->max);
  2331. switch (type) {
  2332. case IB_FLOW_SPEC_ACTION_HANDLE:
  2333. atomic_inc(&((struct ib_flow_action *)ibobj)->usecnt);
  2334. uflow_res->collection[uflow_res->collection_num++] =
  2335. (struct ib_flow_action *)ibobj;
  2336. break;
  2337. case IB_FLOW_SPEC_ACTION_COUNT:
  2338. atomic_inc(&((struct ib_counters *)ibobj)->usecnt);
  2339. uflow_res->counters[uflow_res->counters_num++] =
  2340. (struct ib_counters *)ibobj;
  2341. break;
  2342. default:
  2343. WARN_ON(1);
  2344. }
  2345. uflow_res->num++;
  2346. }
  2347. static int kern_spec_to_ib_spec_action(struct ib_uverbs_file *ufile,
  2348. struct ib_uverbs_flow_spec *kern_spec,
  2349. union ib_flow_spec *ib_spec,
  2350. struct ib_uflow_resources *uflow_res)
  2351. {
  2352. ib_spec->type = kern_spec->type;
  2353. switch (ib_spec->type) {
  2354. case IB_FLOW_SPEC_ACTION_TAG:
  2355. if (kern_spec->flow_tag.size !=
  2356. sizeof(struct ib_uverbs_flow_spec_action_tag))
  2357. return -EINVAL;
  2358. ib_spec->flow_tag.size = sizeof(struct ib_flow_spec_action_tag);
  2359. ib_spec->flow_tag.tag_id = kern_spec->flow_tag.tag_id;
  2360. break;
  2361. case IB_FLOW_SPEC_ACTION_DROP:
  2362. if (kern_spec->drop.size !=
  2363. sizeof(struct ib_uverbs_flow_spec_action_drop))
  2364. return -EINVAL;
  2365. ib_spec->drop.size = sizeof(struct ib_flow_spec_action_drop);
  2366. break;
  2367. case IB_FLOW_SPEC_ACTION_HANDLE:
  2368. if (kern_spec->action.size !=
  2369. sizeof(struct ib_uverbs_flow_spec_action_handle))
  2370. return -EOPNOTSUPP;
  2371. ib_spec->action.act = uobj_get_obj_read(flow_action,
  2372. UVERBS_OBJECT_FLOW_ACTION,
  2373. kern_spec->action.handle,
  2374. ufile);
  2375. if (!ib_spec->action.act)
  2376. return -EINVAL;
  2377. ib_spec->action.size =
  2378. sizeof(struct ib_flow_spec_action_handle);
  2379. flow_resources_add(uflow_res,
  2380. IB_FLOW_SPEC_ACTION_HANDLE,
  2381. ib_spec->action.act);
  2382. uobj_put_obj_read(ib_spec->action.act);
  2383. break;
  2384. case IB_FLOW_SPEC_ACTION_COUNT:
  2385. if (kern_spec->flow_count.size !=
  2386. sizeof(struct ib_uverbs_flow_spec_action_count))
  2387. return -EINVAL;
  2388. ib_spec->flow_count.counters =
  2389. uobj_get_obj_read(counters,
  2390. UVERBS_OBJECT_COUNTERS,
  2391. kern_spec->flow_count.handle,
  2392. ufile);
  2393. if (!ib_spec->flow_count.counters)
  2394. return -EINVAL;
  2395. ib_spec->flow_count.size =
  2396. sizeof(struct ib_flow_spec_action_count);
  2397. flow_resources_add(uflow_res,
  2398. IB_FLOW_SPEC_ACTION_COUNT,
  2399. ib_spec->flow_count.counters);
  2400. uobj_put_obj_read(ib_spec->flow_count.counters);
  2401. break;
  2402. default:
  2403. return -EINVAL;
  2404. }
  2405. return 0;
  2406. }
  2407. static ssize_t spec_filter_size(const void *kern_spec_filter, u16 kern_filter_size,
  2408. u16 ib_real_filter_sz)
  2409. {
  2410. /*
  2411. * User space filter structures must be 64 bit aligned, otherwise this
  2412. * may pass, but we won't handle additional new attributes.
  2413. */
  2414. if (kern_filter_size > ib_real_filter_sz) {
  2415. if (memchr_inv(kern_spec_filter +
  2416. ib_real_filter_sz, 0,
  2417. kern_filter_size - ib_real_filter_sz))
  2418. return -EINVAL;
  2419. return ib_real_filter_sz;
  2420. }
  2421. return kern_filter_size;
  2422. }
  2423. int ib_uverbs_kern_spec_to_ib_spec_filter(enum ib_flow_spec_type type,
  2424. const void *kern_spec_mask,
  2425. const void *kern_spec_val,
  2426. size_t kern_filter_sz,
  2427. union ib_flow_spec *ib_spec)
  2428. {
  2429. ssize_t actual_filter_sz;
  2430. ssize_t ib_filter_sz;
  2431. /* User flow spec size must be aligned to 4 bytes */
  2432. if (kern_filter_sz != ALIGN(kern_filter_sz, 4))
  2433. return -EINVAL;
  2434. ib_spec->type = type;
  2435. if (ib_spec->type == (IB_FLOW_SPEC_INNER | IB_FLOW_SPEC_VXLAN_TUNNEL))
  2436. return -EINVAL;
  2437. switch (ib_spec->type & ~IB_FLOW_SPEC_INNER) {
  2438. case IB_FLOW_SPEC_ETH:
  2439. ib_filter_sz = offsetof(struct ib_flow_eth_filter, real_sz);
  2440. actual_filter_sz = spec_filter_size(kern_spec_mask,
  2441. kern_filter_sz,
  2442. ib_filter_sz);
  2443. if (actual_filter_sz <= 0)
  2444. return -EINVAL;
  2445. ib_spec->size = sizeof(struct ib_flow_spec_eth);
  2446. memcpy(&ib_spec->eth.val, kern_spec_val, actual_filter_sz);
  2447. memcpy(&ib_spec->eth.mask, kern_spec_mask, actual_filter_sz);
  2448. break;
  2449. case IB_FLOW_SPEC_IPV4:
  2450. ib_filter_sz = offsetof(struct ib_flow_ipv4_filter, real_sz);
  2451. actual_filter_sz = spec_filter_size(kern_spec_mask,
  2452. kern_filter_sz,
  2453. ib_filter_sz);
  2454. if (actual_filter_sz <= 0)
  2455. return -EINVAL;
  2456. ib_spec->size = sizeof(struct ib_flow_spec_ipv4);
  2457. memcpy(&ib_spec->ipv4.val, kern_spec_val, actual_filter_sz);
  2458. memcpy(&ib_spec->ipv4.mask, kern_spec_mask, actual_filter_sz);
  2459. break;
  2460. case IB_FLOW_SPEC_IPV6:
  2461. ib_filter_sz = offsetof(struct ib_flow_ipv6_filter, real_sz);
  2462. actual_filter_sz = spec_filter_size(kern_spec_mask,
  2463. kern_filter_sz,
  2464. ib_filter_sz);
  2465. if (actual_filter_sz <= 0)
  2466. return -EINVAL;
  2467. ib_spec->size = sizeof(struct ib_flow_spec_ipv6);
  2468. memcpy(&ib_spec->ipv6.val, kern_spec_val, actual_filter_sz);
  2469. memcpy(&ib_spec->ipv6.mask, kern_spec_mask, actual_filter_sz);
  2470. if ((ntohl(ib_spec->ipv6.mask.flow_label)) >= BIT(20) ||
  2471. (ntohl(ib_spec->ipv6.val.flow_label)) >= BIT(20))
  2472. return -EINVAL;
  2473. break;
  2474. case IB_FLOW_SPEC_TCP:
  2475. case IB_FLOW_SPEC_UDP:
  2476. ib_filter_sz = offsetof(struct ib_flow_tcp_udp_filter, real_sz);
  2477. actual_filter_sz = spec_filter_size(kern_spec_mask,
  2478. kern_filter_sz,
  2479. ib_filter_sz);
  2480. if (actual_filter_sz <= 0)
  2481. return -EINVAL;
  2482. ib_spec->size = sizeof(struct ib_flow_spec_tcp_udp);
  2483. memcpy(&ib_spec->tcp_udp.val, kern_spec_val, actual_filter_sz);
  2484. memcpy(&ib_spec->tcp_udp.mask, kern_spec_mask, actual_filter_sz);
  2485. break;
  2486. case IB_FLOW_SPEC_VXLAN_TUNNEL:
  2487. ib_filter_sz = offsetof(struct ib_flow_tunnel_filter, real_sz);
  2488. actual_filter_sz = spec_filter_size(kern_spec_mask,
  2489. kern_filter_sz,
  2490. ib_filter_sz);
  2491. if (actual_filter_sz <= 0)
  2492. return -EINVAL;
  2493. ib_spec->tunnel.size = sizeof(struct ib_flow_spec_tunnel);
  2494. memcpy(&ib_spec->tunnel.val, kern_spec_val, actual_filter_sz);
  2495. memcpy(&ib_spec->tunnel.mask, kern_spec_mask, actual_filter_sz);
  2496. if ((ntohl(ib_spec->tunnel.mask.tunnel_id)) >= BIT(24) ||
  2497. (ntohl(ib_spec->tunnel.val.tunnel_id)) >= BIT(24))
  2498. return -EINVAL;
  2499. break;
  2500. case IB_FLOW_SPEC_ESP:
  2501. ib_filter_sz = offsetof(struct ib_flow_esp_filter, real_sz);
  2502. actual_filter_sz = spec_filter_size(kern_spec_mask,
  2503. kern_filter_sz,
  2504. ib_filter_sz);
  2505. if (actual_filter_sz <= 0)
  2506. return -EINVAL;
  2507. ib_spec->esp.size = sizeof(struct ib_flow_spec_esp);
  2508. memcpy(&ib_spec->esp.val, kern_spec_val, actual_filter_sz);
  2509. memcpy(&ib_spec->esp.mask, kern_spec_mask, actual_filter_sz);
  2510. break;
  2511. case IB_FLOW_SPEC_GRE:
  2512. ib_filter_sz = offsetof(struct ib_flow_gre_filter, real_sz);
  2513. actual_filter_sz = spec_filter_size(kern_spec_mask,
  2514. kern_filter_sz,
  2515. ib_filter_sz);
  2516. if (actual_filter_sz <= 0)
  2517. return -EINVAL;
  2518. ib_spec->gre.size = sizeof(struct ib_flow_spec_gre);
  2519. memcpy(&ib_spec->gre.val, kern_spec_val, actual_filter_sz);
  2520. memcpy(&ib_spec->gre.mask, kern_spec_mask, actual_filter_sz);
  2521. break;
  2522. case IB_FLOW_SPEC_MPLS:
  2523. ib_filter_sz = offsetof(struct ib_flow_mpls_filter, real_sz);
  2524. actual_filter_sz = spec_filter_size(kern_spec_mask,
  2525. kern_filter_sz,
  2526. ib_filter_sz);
  2527. if (actual_filter_sz <= 0)
  2528. return -EINVAL;
  2529. ib_spec->mpls.size = sizeof(struct ib_flow_spec_mpls);
  2530. memcpy(&ib_spec->mpls.val, kern_spec_val, actual_filter_sz);
  2531. memcpy(&ib_spec->mpls.mask, kern_spec_mask, actual_filter_sz);
  2532. break;
  2533. default:
  2534. return -EINVAL;
  2535. }
  2536. return 0;
  2537. }
  2538. static int kern_spec_to_ib_spec_filter(struct ib_uverbs_flow_spec *kern_spec,
  2539. union ib_flow_spec *ib_spec)
  2540. {
  2541. size_t kern_filter_sz;
  2542. void *kern_spec_mask;
  2543. void *kern_spec_val;
  2544. if (check_sub_overflow((size_t)kern_spec->hdr.size,
  2545. sizeof(struct ib_uverbs_flow_spec_hdr),
  2546. &kern_filter_sz))
  2547. return -EINVAL;
  2548. kern_filter_sz /= 2;
  2549. kern_spec_val = (void *)kern_spec +
  2550. sizeof(struct ib_uverbs_flow_spec_hdr);
  2551. kern_spec_mask = kern_spec_val + kern_filter_sz;
  2552. return ib_uverbs_kern_spec_to_ib_spec_filter(kern_spec->type,
  2553. kern_spec_mask,
  2554. kern_spec_val,
  2555. kern_filter_sz, ib_spec);
  2556. }
  2557. static int kern_spec_to_ib_spec(struct ib_uverbs_file *ufile,
  2558. struct ib_uverbs_flow_spec *kern_spec,
  2559. union ib_flow_spec *ib_spec,
  2560. struct ib_uflow_resources *uflow_res)
  2561. {
  2562. if (kern_spec->reserved)
  2563. return -EINVAL;
  2564. if (kern_spec->type >= IB_FLOW_SPEC_ACTION_TAG)
  2565. return kern_spec_to_ib_spec_action(ufile, kern_spec, ib_spec,
  2566. uflow_res);
  2567. else
  2568. return kern_spec_to_ib_spec_filter(kern_spec, ib_spec);
  2569. }
  2570. int ib_uverbs_ex_create_wq(struct ib_uverbs_file *file,
  2571. struct ib_udata *ucore,
  2572. struct ib_udata *uhw)
  2573. {
  2574. struct ib_uverbs_ex_create_wq cmd = {};
  2575. struct ib_uverbs_ex_create_wq_resp resp = {};
  2576. struct ib_uwq_object *obj;
  2577. int err = 0;
  2578. struct ib_cq *cq;
  2579. struct ib_pd *pd;
  2580. struct ib_wq *wq;
  2581. struct ib_wq_init_attr wq_init_attr = {};
  2582. size_t required_cmd_sz;
  2583. size_t required_resp_len;
  2584. struct ib_device *ib_dev;
  2585. required_cmd_sz = offsetof(typeof(cmd), max_sge) + sizeof(cmd.max_sge);
  2586. required_resp_len = offsetof(typeof(resp), wqn) + sizeof(resp.wqn);
  2587. if (ucore->inlen < required_cmd_sz)
  2588. return -EINVAL;
  2589. if (ucore->outlen < required_resp_len)
  2590. return -ENOSPC;
  2591. if (ucore->inlen > sizeof(cmd) &&
  2592. !ib_is_udata_cleared(ucore, sizeof(cmd),
  2593. ucore->inlen - sizeof(cmd)))
  2594. return -EOPNOTSUPP;
  2595. err = ib_copy_from_udata(&cmd, ucore, min(sizeof(cmd), ucore->inlen));
  2596. if (err)
  2597. return err;
  2598. if (cmd.comp_mask)
  2599. return -EOPNOTSUPP;
  2600. obj = (struct ib_uwq_object *)uobj_alloc(UVERBS_OBJECT_WQ, file,
  2601. &ib_dev);
  2602. if (IS_ERR(obj))
  2603. return PTR_ERR(obj);
  2604. pd = uobj_get_obj_read(pd, UVERBS_OBJECT_PD, cmd.pd_handle, file);
  2605. if (!pd) {
  2606. err = -EINVAL;
  2607. goto err_uobj;
  2608. }
  2609. cq = uobj_get_obj_read(cq, UVERBS_OBJECT_CQ, cmd.cq_handle, file);
  2610. if (!cq) {
  2611. err = -EINVAL;
  2612. goto err_put_pd;
  2613. }
  2614. wq_init_attr.cq = cq;
  2615. wq_init_attr.max_sge = cmd.max_sge;
  2616. wq_init_attr.max_wr = cmd.max_wr;
  2617. wq_init_attr.wq_context = file;
  2618. wq_init_attr.wq_type = cmd.wq_type;
  2619. wq_init_attr.event_handler = ib_uverbs_wq_event_handler;
  2620. if (ucore->inlen >= (offsetof(typeof(cmd), create_flags) +
  2621. sizeof(cmd.create_flags)))
  2622. wq_init_attr.create_flags = cmd.create_flags;
  2623. obj->uevent.events_reported = 0;
  2624. INIT_LIST_HEAD(&obj->uevent.event_list);
  2625. if (!pd->device->create_wq) {
  2626. err = -EOPNOTSUPP;
  2627. goto err_put_cq;
  2628. }
  2629. wq = pd->device->create_wq(pd, &wq_init_attr, uhw);
  2630. if (IS_ERR(wq)) {
  2631. err = PTR_ERR(wq);
  2632. goto err_put_cq;
  2633. }
  2634. wq->uobject = &obj->uevent.uobject;
  2635. obj->uevent.uobject.object = wq;
  2636. wq->wq_type = wq_init_attr.wq_type;
  2637. wq->cq = cq;
  2638. wq->pd = pd;
  2639. wq->device = pd->device;
  2640. wq->wq_context = wq_init_attr.wq_context;
  2641. atomic_set(&wq->usecnt, 0);
  2642. atomic_inc(&pd->usecnt);
  2643. atomic_inc(&cq->usecnt);
  2644. wq->uobject = &obj->uevent.uobject;
  2645. obj->uevent.uobject.object = wq;
  2646. memset(&resp, 0, sizeof(resp));
  2647. resp.wq_handle = obj->uevent.uobject.id;
  2648. resp.max_sge = wq_init_attr.max_sge;
  2649. resp.max_wr = wq_init_attr.max_wr;
  2650. resp.wqn = wq->wq_num;
  2651. resp.response_length = required_resp_len;
  2652. err = ib_copy_to_udata(ucore,
  2653. &resp, resp.response_length);
  2654. if (err)
  2655. goto err_copy;
  2656. uobj_put_obj_read(pd);
  2657. uobj_put_obj_read(cq);
  2658. return uobj_alloc_commit(&obj->uevent.uobject, 0);
  2659. err_copy:
  2660. ib_destroy_wq(wq);
  2661. err_put_cq:
  2662. uobj_put_obj_read(cq);
  2663. err_put_pd:
  2664. uobj_put_obj_read(pd);
  2665. err_uobj:
  2666. uobj_alloc_abort(&obj->uevent.uobject);
  2667. return err;
  2668. }
  2669. int ib_uverbs_ex_destroy_wq(struct ib_uverbs_file *file,
  2670. struct ib_udata *ucore,
  2671. struct ib_udata *uhw)
  2672. {
  2673. struct ib_uverbs_ex_destroy_wq cmd = {};
  2674. struct ib_uverbs_ex_destroy_wq_resp resp = {};
  2675. struct ib_uobject *uobj;
  2676. struct ib_uwq_object *obj;
  2677. size_t required_cmd_sz;
  2678. size_t required_resp_len;
  2679. int ret;
  2680. required_cmd_sz = offsetof(typeof(cmd), wq_handle) + sizeof(cmd.wq_handle);
  2681. required_resp_len = offsetof(typeof(resp), reserved) + sizeof(resp.reserved);
  2682. if (ucore->inlen < required_cmd_sz)
  2683. return -EINVAL;
  2684. if (ucore->outlen < required_resp_len)
  2685. return -ENOSPC;
  2686. if (ucore->inlen > sizeof(cmd) &&
  2687. !ib_is_udata_cleared(ucore, sizeof(cmd),
  2688. ucore->inlen - sizeof(cmd)))
  2689. return -EOPNOTSUPP;
  2690. ret = ib_copy_from_udata(&cmd, ucore, min(sizeof(cmd), ucore->inlen));
  2691. if (ret)
  2692. return ret;
  2693. if (cmd.comp_mask)
  2694. return -EOPNOTSUPP;
  2695. resp.response_length = required_resp_len;
  2696. uobj = uobj_get_destroy(UVERBS_OBJECT_WQ, cmd.wq_handle, file);
  2697. if (IS_ERR(uobj))
  2698. return PTR_ERR(uobj);
  2699. obj = container_of(uobj, struct ib_uwq_object, uevent.uobject);
  2700. resp.events_reported = obj->uevent.events_reported;
  2701. uobj_put_destroy(uobj);
  2702. return ib_copy_to_udata(ucore, &resp, resp.response_length);
  2703. }
  2704. int ib_uverbs_ex_modify_wq(struct ib_uverbs_file *file,
  2705. struct ib_udata *ucore,
  2706. struct ib_udata *uhw)
  2707. {
  2708. struct ib_uverbs_ex_modify_wq cmd = {};
  2709. struct ib_wq *wq;
  2710. struct ib_wq_attr wq_attr = {};
  2711. size_t required_cmd_sz;
  2712. int ret;
  2713. required_cmd_sz = offsetof(typeof(cmd), curr_wq_state) + sizeof(cmd.curr_wq_state);
  2714. if (ucore->inlen < required_cmd_sz)
  2715. return -EINVAL;
  2716. if (ucore->inlen > sizeof(cmd) &&
  2717. !ib_is_udata_cleared(ucore, sizeof(cmd),
  2718. ucore->inlen - sizeof(cmd)))
  2719. return -EOPNOTSUPP;
  2720. ret = ib_copy_from_udata(&cmd, ucore, min(sizeof(cmd), ucore->inlen));
  2721. if (ret)
  2722. return ret;
  2723. if (!cmd.attr_mask)
  2724. return -EINVAL;
  2725. if (cmd.attr_mask > (IB_WQ_STATE | IB_WQ_CUR_STATE | IB_WQ_FLAGS))
  2726. return -EINVAL;
  2727. wq = uobj_get_obj_read(wq, UVERBS_OBJECT_WQ, cmd.wq_handle, file);
  2728. if (!wq)
  2729. return -EINVAL;
  2730. wq_attr.curr_wq_state = cmd.curr_wq_state;
  2731. wq_attr.wq_state = cmd.wq_state;
  2732. if (cmd.attr_mask & IB_WQ_FLAGS) {
  2733. wq_attr.flags = cmd.flags;
  2734. wq_attr.flags_mask = cmd.flags_mask;
  2735. }
  2736. if (!wq->device->modify_wq) {
  2737. ret = -EOPNOTSUPP;
  2738. goto out;
  2739. }
  2740. ret = wq->device->modify_wq(wq, &wq_attr, cmd.attr_mask, uhw);
  2741. out:
  2742. uobj_put_obj_read(wq);
  2743. return ret;
  2744. }
  2745. int ib_uverbs_ex_create_rwq_ind_table(struct ib_uverbs_file *file,
  2746. struct ib_udata *ucore,
  2747. struct ib_udata *uhw)
  2748. {
  2749. struct ib_uverbs_ex_create_rwq_ind_table cmd = {};
  2750. struct ib_uverbs_ex_create_rwq_ind_table_resp resp = {};
  2751. struct ib_uobject *uobj;
  2752. int err = 0;
  2753. struct ib_rwq_ind_table_init_attr init_attr = {};
  2754. struct ib_rwq_ind_table *rwq_ind_tbl;
  2755. struct ib_wq **wqs = NULL;
  2756. u32 *wqs_handles = NULL;
  2757. struct ib_wq *wq = NULL;
  2758. int i, j, num_read_wqs;
  2759. u32 num_wq_handles;
  2760. u32 expected_in_size;
  2761. size_t required_cmd_sz_header;
  2762. size_t required_resp_len;
  2763. struct ib_device *ib_dev;
  2764. required_cmd_sz_header = offsetof(typeof(cmd), log_ind_tbl_size) + sizeof(cmd.log_ind_tbl_size);
  2765. required_resp_len = offsetof(typeof(resp), ind_tbl_num) + sizeof(resp.ind_tbl_num);
  2766. if (ucore->inlen < required_cmd_sz_header)
  2767. return -EINVAL;
  2768. if (ucore->outlen < required_resp_len)
  2769. return -ENOSPC;
  2770. err = ib_copy_from_udata(&cmd, ucore, required_cmd_sz_header);
  2771. if (err)
  2772. return err;
  2773. ucore->inbuf += required_cmd_sz_header;
  2774. ucore->inlen -= required_cmd_sz_header;
  2775. if (cmd.comp_mask)
  2776. return -EOPNOTSUPP;
  2777. if (cmd.log_ind_tbl_size > IB_USER_VERBS_MAX_LOG_IND_TBL_SIZE)
  2778. return -EINVAL;
  2779. num_wq_handles = 1 << cmd.log_ind_tbl_size;
  2780. expected_in_size = num_wq_handles * sizeof(__u32);
  2781. if (num_wq_handles == 1)
  2782. /* input size for wq handles is u64 aligned */
  2783. expected_in_size += sizeof(__u32);
  2784. if (ucore->inlen < expected_in_size)
  2785. return -EINVAL;
  2786. if (ucore->inlen > expected_in_size &&
  2787. !ib_is_udata_cleared(ucore, expected_in_size,
  2788. ucore->inlen - expected_in_size))
  2789. return -EOPNOTSUPP;
  2790. wqs_handles = kcalloc(num_wq_handles, sizeof(*wqs_handles),
  2791. GFP_KERNEL);
  2792. if (!wqs_handles)
  2793. return -ENOMEM;
  2794. err = ib_copy_from_udata(wqs_handles, ucore,
  2795. num_wq_handles * sizeof(__u32));
  2796. if (err)
  2797. goto err_free;
  2798. wqs = kcalloc(num_wq_handles, sizeof(*wqs), GFP_KERNEL);
  2799. if (!wqs) {
  2800. err = -ENOMEM;
  2801. goto err_free;
  2802. }
  2803. for (num_read_wqs = 0; num_read_wqs < num_wq_handles;
  2804. num_read_wqs++) {
  2805. wq = uobj_get_obj_read(wq, UVERBS_OBJECT_WQ,
  2806. wqs_handles[num_read_wqs], file);
  2807. if (!wq) {
  2808. err = -EINVAL;
  2809. goto put_wqs;
  2810. }
  2811. wqs[num_read_wqs] = wq;
  2812. }
  2813. uobj = uobj_alloc(UVERBS_OBJECT_RWQ_IND_TBL, file, &ib_dev);
  2814. if (IS_ERR(uobj)) {
  2815. err = PTR_ERR(uobj);
  2816. goto put_wqs;
  2817. }
  2818. init_attr.log_ind_tbl_size = cmd.log_ind_tbl_size;
  2819. init_attr.ind_tbl = wqs;
  2820. if (!ib_dev->create_rwq_ind_table) {
  2821. err = -EOPNOTSUPP;
  2822. goto err_uobj;
  2823. }
  2824. rwq_ind_tbl = ib_dev->create_rwq_ind_table(ib_dev, &init_attr, uhw);
  2825. if (IS_ERR(rwq_ind_tbl)) {
  2826. err = PTR_ERR(rwq_ind_tbl);
  2827. goto err_uobj;
  2828. }
  2829. rwq_ind_tbl->ind_tbl = wqs;
  2830. rwq_ind_tbl->log_ind_tbl_size = init_attr.log_ind_tbl_size;
  2831. rwq_ind_tbl->uobject = uobj;
  2832. uobj->object = rwq_ind_tbl;
  2833. rwq_ind_tbl->device = ib_dev;
  2834. atomic_set(&rwq_ind_tbl->usecnt, 0);
  2835. for (i = 0; i < num_wq_handles; i++)
  2836. atomic_inc(&wqs[i]->usecnt);
  2837. resp.ind_tbl_handle = uobj->id;
  2838. resp.ind_tbl_num = rwq_ind_tbl->ind_tbl_num;
  2839. resp.response_length = required_resp_len;
  2840. err = ib_copy_to_udata(ucore,
  2841. &resp, resp.response_length);
  2842. if (err)
  2843. goto err_copy;
  2844. kfree(wqs_handles);
  2845. for (j = 0; j < num_read_wqs; j++)
  2846. uobj_put_obj_read(wqs[j]);
  2847. return uobj_alloc_commit(uobj, 0);
  2848. err_copy:
  2849. ib_destroy_rwq_ind_table(rwq_ind_tbl);
  2850. err_uobj:
  2851. uobj_alloc_abort(uobj);
  2852. put_wqs:
  2853. for (j = 0; j < num_read_wqs; j++)
  2854. uobj_put_obj_read(wqs[j]);
  2855. err_free:
  2856. kfree(wqs_handles);
  2857. kfree(wqs);
  2858. return err;
  2859. }
  2860. int ib_uverbs_ex_destroy_rwq_ind_table(struct ib_uverbs_file *file,
  2861. struct ib_udata *ucore,
  2862. struct ib_udata *uhw)
  2863. {
  2864. struct ib_uverbs_ex_destroy_rwq_ind_table cmd = {};
  2865. int ret;
  2866. size_t required_cmd_sz;
  2867. required_cmd_sz = offsetof(typeof(cmd), ind_tbl_handle) + sizeof(cmd.ind_tbl_handle);
  2868. if (ucore->inlen < required_cmd_sz)
  2869. return -EINVAL;
  2870. if (ucore->inlen > sizeof(cmd) &&
  2871. !ib_is_udata_cleared(ucore, sizeof(cmd),
  2872. ucore->inlen - sizeof(cmd)))
  2873. return -EOPNOTSUPP;
  2874. ret = ib_copy_from_udata(&cmd, ucore, min(sizeof(cmd), ucore->inlen));
  2875. if (ret)
  2876. return ret;
  2877. if (cmd.comp_mask)
  2878. return -EOPNOTSUPP;
  2879. return uobj_perform_destroy(UVERBS_OBJECT_RWQ_IND_TBL,
  2880. cmd.ind_tbl_handle, file, 0);
  2881. }
  2882. int ib_uverbs_ex_create_flow(struct ib_uverbs_file *file,
  2883. struct ib_udata *ucore,
  2884. struct ib_udata *uhw)
  2885. {
  2886. struct ib_uverbs_create_flow cmd;
  2887. struct ib_uverbs_create_flow_resp resp;
  2888. struct ib_uobject *uobj;
  2889. struct ib_uflow_object *uflow;
  2890. struct ib_flow *flow_id;
  2891. struct ib_uverbs_flow_attr *kern_flow_attr;
  2892. struct ib_flow_attr *flow_attr;
  2893. struct ib_qp *qp;
  2894. struct ib_uflow_resources *uflow_res;
  2895. struct ib_uverbs_flow_spec_hdr *kern_spec;
  2896. int err = 0;
  2897. void *ib_spec;
  2898. int i;
  2899. struct ib_device *ib_dev;
  2900. if (ucore->inlen < sizeof(cmd))
  2901. return -EINVAL;
  2902. if (ucore->outlen < sizeof(resp))
  2903. return -ENOSPC;
  2904. err = ib_copy_from_udata(&cmd, ucore, sizeof(cmd));
  2905. if (err)
  2906. return err;
  2907. ucore->inbuf += sizeof(cmd);
  2908. ucore->inlen -= sizeof(cmd);
  2909. if (cmd.comp_mask)
  2910. return -EINVAL;
  2911. if (!capable(CAP_NET_RAW))
  2912. return -EPERM;
  2913. if (cmd.flow_attr.flags >= IB_FLOW_ATTR_FLAGS_RESERVED)
  2914. return -EINVAL;
  2915. if ((cmd.flow_attr.flags & IB_FLOW_ATTR_FLAGS_DONT_TRAP) &&
  2916. ((cmd.flow_attr.type == IB_FLOW_ATTR_ALL_DEFAULT) ||
  2917. (cmd.flow_attr.type == IB_FLOW_ATTR_MC_DEFAULT)))
  2918. return -EINVAL;
  2919. if (cmd.flow_attr.num_of_specs > IB_FLOW_SPEC_SUPPORT_LAYERS)
  2920. return -EINVAL;
  2921. if (cmd.flow_attr.size > ucore->inlen ||
  2922. cmd.flow_attr.size >
  2923. (cmd.flow_attr.num_of_specs * sizeof(struct ib_uverbs_flow_spec)))
  2924. return -EINVAL;
  2925. if (cmd.flow_attr.reserved[0] ||
  2926. cmd.flow_attr.reserved[1])
  2927. return -EINVAL;
  2928. if (cmd.flow_attr.num_of_specs) {
  2929. kern_flow_attr = kmalloc(sizeof(*kern_flow_attr) + cmd.flow_attr.size,
  2930. GFP_KERNEL);
  2931. if (!kern_flow_attr)
  2932. return -ENOMEM;
  2933. *kern_flow_attr = cmd.flow_attr;
  2934. err = ib_copy_from_udata(&kern_flow_attr->flow_specs, ucore,
  2935. cmd.flow_attr.size);
  2936. if (err)
  2937. goto err_free_attr;
  2938. } else {
  2939. kern_flow_attr = &cmd.flow_attr;
  2940. }
  2941. uobj = uobj_alloc(UVERBS_OBJECT_FLOW, file, &ib_dev);
  2942. if (IS_ERR(uobj)) {
  2943. err = PTR_ERR(uobj);
  2944. goto err_free_attr;
  2945. }
  2946. qp = uobj_get_obj_read(qp, UVERBS_OBJECT_QP, cmd.qp_handle, file);
  2947. if (!qp) {
  2948. err = -EINVAL;
  2949. goto err_uobj;
  2950. }
  2951. if (qp->qp_type != IB_QPT_UD && qp->qp_type != IB_QPT_RAW_PACKET) {
  2952. err = -EINVAL;
  2953. goto err_put;
  2954. }
  2955. if (!qp->device->create_flow) {
  2956. err = -EOPNOTSUPP;
  2957. goto err_put;
  2958. }
  2959. flow_attr = kzalloc(struct_size(flow_attr, flows,
  2960. cmd.flow_attr.num_of_specs), GFP_KERNEL);
  2961. if (!flow_attr) {
  2962. err = -ENOMEM;
  2963. goto err_put;
  2964. }
  2965. uflow_res = flow_resources_alloc(cmd.flow_attr.num_of_specs);
  2966. if (!uflow_res) {
  2967. err = -ENOMEM;
  2968. goto err_free_flow_attr;
  2969. }
  2970. flow_attr->type = kern_flow_attr->type;
  2971. flow_attr->priority = kern_flow_attr->priority;
  2972. flow_attr->num_of_specs = kern_flow_attr->num_of_specs;
  2973. flow_attr->port = kern_flow_attr->port;
  2974. flow_attr->flags = kern_flow_attr->flags;
  2975. flow_attr->size = sizeof(*flow_attr);
  2976. kern_spec = kern_flow_attr->flow_specs;
  2977. ib_spec = flow_attr + 1;
  2978. for (i = 0; i < flow_attr->num_of_specs &&
  2979. cmd.flow_attr.size >= sizeof(*kern_spec) &&
  2980. cmd.flow_attr.size >= kern_spec->size;
  2981. i++) {
  2982. err = kern_spec_to_ib_spec(
  2983. file, (struct ib_uverbs_flow_spec *)kern_spec,
  2984. ib_spec, uflow_res);
  2985. if (err)
  2986. goto err_free;
  2987. flow_attr->size +=
  2988. ((union ib_flow_spec *) ib_spec)->size;
  2989. cmd.flow_attr.size -= kern_spec->size;
  2990. kern_spec = ((void *)kern_spec) + kern_spec->size;
  2991. ib_spec += ((union ib_flow_spec *) ib_spec)->size;
  2992. }
  2993. if (cmd.flow_attr.size || (i != flow_attr->num_of_specs)) {
  2994. pr_warn("create flow failed, flow %d: %d bytes left from uverb cmd\n",
  2995. i, cmd.flow_attr.size);
  2996. err = -EINVAL;
  2997. goto err_free;
  2998. }
  2999. flow_id = qp->device->create_flow(qp, flow_attr,
  3000. IB_FLOW_DOMAIN_USER, uhw);
  3001. if (IS_ERR(flow_id)) {
  3002. err = PTR_ERR(flow_id);
  3003. goto err_free;
  3004. }
  3005. atomic_inc(&qp->usecnt);
  3006. flow_id->qp = qp;
  3007. flow_id->device = qp->device;
  3008. flow_id->uobject = uobj;
  3009. uobj->object = flow_id;
  3010. uflow = container_of(uobj, typeof(*uflow), uobject);
  3011. uflow->resources = uflow_res;
  3012. memset(&resp, 0, sizeof(resp));
  3013. resp.flow_handle = uobj->id;
  3014. err = ib_copy_to_udata(ucore,
  3015. &resp, sizeof(resp));
  3016. if (err)
  3017. goto err_copy;
  3018. uobj_put_obj_read(qp);
  3019. kfree(flow_attr);
  3020. if (cmd.flow_attr.num_of_specs)
  3021. kfree(kern_flow_attr);
  3022. return uobj_alloc_commit(uobj, 0);
  3023. err_copy:
  3024. if (!qp->device->destroy_flow(flow_id))
  3025. atomic_dec(&qp->usecnt);
  3026. err_free:
  3027. ib_uverbs_flow_resources_free(uflow_res);
  3028. err_free_flow_attr:
  3029. kfree(flow_attr);
  3030. err_put:
  3031. uobj_put_obj_read(qp);
  3032. err_uobj:
  3033. uobj_alloc_abort(uobj);
  3034. err_free_attr:
  3035. if (cmd.flow_attr.num_of_specs)
  3036. kfree(kern_flow_attr);
  3037. return err;
  3038. }
  3039. int ib_uverbs_ex_destroy_flow(struct ib_uverbs_file *file,
  3040. struct ib_udata *ucore,
  3041. struct ib_udata *uhw)
  3042. {
  3043. struct ib_uverbs_destroy_flow cmd;
  3044. int ret;
  3045. if (ucore->inlen < sizeof(cmd))
  3046. return -EINVAL;
  3047. ret = ib_copy_from_udata(&cmd, ucore, sizeof(cmd));
  3048. if (ret)
  3049. return ret;
  3050. if (cmd.comp_mask)
  3051. return -EINVAL;
  3052. return uobj_perform_destroy(UVERBS_OBJECT_FLOW, cmd.flow_handle, file,
  3053. 0);
  3054. }
  3055. static int __uverbs_create_xsrq(struct ib_uverbs_file *file,
  3056. struct ib_uverbs_create_xsrq *cmd,
  3057. struct ib_udata *udata)
  3058. {
  3059. struct ib_uverbs_create_srq_resp resp;
  3060. struct ib_usrq_object *obj;
  3061. struct ib_pd *pd;
  3062. struct ib_srq *srq;
  3063. struct ib_uobject *uninitialized_var(xrcd_uobj);
  3064. struct ib_srq_init_attr attr;
  3065. int ret;
  3066. struct ib_device *ib_dev;
  3067. obj = (struct ib_usrq_object *)uobj_alloc(UVERBS_OBJECT_SRQ, file,
  3068. &ib_dev);
  3069. if (IS_ERR(obj))
  3070. return PTR_ERR(obj);
  3071. if (cmd->srq_type == IB_SRQT_TM)
  3072. attr.ext.tag_matching.max_num_tags = cmd->max_num_tags;
  3073. if (cmd->srq_type == IB_SRQT_XRC) {
  3074. xrcd_uobj = uobj_get_read(UVERBS_OBJECT_XRCD, cmd->xrcd_handle,
  3075. file);
  3076. if (IS_ERR(xrcd_uobj)) {
  3077. ret = -EINVAL;
  3078. goto err;
  3079. }
  3080. attr.ext.xrc.xrcd = (struct ib_xrcd *)xrcd_uobj->object;
  3081. if (!attr.ext.xrc.xrcd) {
  3082. ret = -EINVAL;
  3083. goto err_put_xrcd;
  3084. }
  3085. obj->uxrcd = container_of(xrcd_uobj, struct ib_uxrcd_object, uobject);
  3086. atomic_inc(&obj->uxrcd->refcnt);
  3087. }
  3088. if (ib_srq_has_cq(cmd->srq_type)) {
  3089. attr.ext.cq = uobj_get_obj_read(cq, UVERBS_OBJECT_CQ,
  3090. cmd->cq_handle, file);
  3091. if (!attr.ext.cq) {
  3092. ret = -EINVAL;
  3093. goto err_put_xrcd;
  3094. }
  3095. }
  3096. pd = uobj_get_obj_read(pd, UVERBS_OBJECT_PD, cmd->pd_handle, file);
  3097. if (!pd) {
  3098. ret = -EINVAL;
  3099. goto err_put_cq;
  3100. }
  3101. attr.event_handler = ib_uverbs_srq_event_handler;
  3102. attr.srq_context = file;
  3103. attr.srq_type = cmd->srq_type;
  3104. attr.attr.max_wr = cmd->max_wr;
  3105. attr.attr.max_sge = cmd->max_sge;
  3106. attr.attr.srq_limit = cmd->srq_limit;
  3107. obj->uevent.events_reported = 0;
  3108. INIT_LIST_HEAD(&obj->uevent.event_list);
  3109. srq = pd->device->create_srq(pd, &attr, udata);
  3110. if (IS_ERR(srq)) {
  3111. ret = PTR_ERR(srq);
  3112. goto err_put;
  3113. }
  3114. srq->device = pd->device;
  3115. srq->pd = pd;
  3116. srq->srq_type = cmd->srq_type;
  3117. srq->uobject = &obj->uevent.uobject;
  3118. srq->event_handler = attr.event_handler;
  3119. srq->srq_context = attr.srq_context;
  3120. if (ib_srq_has_cq(cmd->srq_type)) {
  3121. srq->ext.cq = attr.ext.cq;
  3122. atomic_inc(&attr.ext.cq->usecnt);
  3123. }
  3124. if (cmd->srq_type == IB_SRQT_XRC) {
  3125. srq->ext.xrc.xrcd = attr.ext.xrc.xrcd;
  3126. atomic_inc(&attr.ext.xrc.xrcd->usecnt);
  3127. }
  3128. atomic_inc(&pd->usecnt);
  3129. atomic_set(&srq->usecnt, 0);
  3130. obj->uevent.uobject.object = srq;
  3131. obj->uevent.uobject.user_handle = cmd->user_handle;
  3132. memset(&resp, 0, sizeof resp);
  3133. resp.srq_handle = obj->uevent.uobject.id;
  3134. resp.max_wr = attr.attr.max_wr;
  3135. resp.max_sge = attr.attr.max_sge;
  3136. if (cmd->srq_type == IB_SRQT_XRC)
  3137. resp.srqn = srq->ext.xrc.srq_num;
  3138. if (copy_to_user(u64_to_user_ptr(cmd->response),
  3139. &resp, sizeof resp)) {
  3140. ret = -EFAULT;
  3141. goto err_copy;
  3142. }
  3143. if (cmd->srq_type == IB_SRQT_XRC)
  3144. uobj_put_read(xrcd_uobj);
  3145. if (ib_srq_has_cq(cmd->srq_type))
  3146. uobj_put_obj_read(attr.ext.cq);
  3147. uobj_put_obj_read(pd);
  3148. return uobj_alloc_commit(&obj->uevent.uobject, 0);
  3149. err_copy:
  3150. ib_destroy_srq(srq);
  3151. err_put:
  3152. uobj_put_obj_read(pd);
  3153. err_put_cq:
  3154. if (ib_srq_has_cq(cmd->srq_type))
  3155. uobj_put_obj_read(attr.ext.cq);
  3156. err_put_xrcd:
  3157. if (cmd->srq_type == IB_SRQT_XRC) {
  3158. atomic_dec(&obj->uxrcd->refcnt);
  3159. uobj_put_read(xrcd_uobj);
  3160. }
  3161. err:
  3162. uobj_alloc_abort(&obj->uevent.uobject);
  3163. return ret;
  3164. }
  3165. ssize_t ib_uverbs_create_srq(struct ib_uverbs_file *file,
  3166. const char __user *buf, int in_len,
  3167. int out_len)
  3168. {
  3169. struct ib_uverbs_create_srq cmd;
  3170. struct ib_uverbs_create_xsrq xcmd;
  3171. struct ib_uverbs_create_srq_resp resp;
  3172. struct ib_udata udata;
  3173. int ret;
  3174. if (out_len < sizeof resp)
  3175. return -ENOSPC;
  3176. if (copy_from_user(&cmd, buf, sizeof cmd))
  3177. return -EFAULT;
  3178. memset(&xcmd, 0, sizeof(xcmd));
  3179. xcmd.response = cmd.response;
  3180. xcmd.user_handle = cmd.user_handle;
  3181. xcmd.srq_type = IB_SRQT_BASIC;
  3182. xcmd.pd_handle = cmd.pd_handle;
  3183. xcmd.max_wr = cmd.max_wr;
  3184. xcmd.max_sge = cmd.max_sge;
  3185. xcmd.srq_limit = cmd.srq_limit;
  3186. ib_uverbs_init_udata(&udata, buf + sizeof(cmd),
  3187. u64_to_user_ptr(cmd.response) + sizeof(resp),
  3188. in_len - sizeof(cmd) - sizeof(struct ib_uverbs_cmd_hdr),
  3189. out_len - sizeof(resp));
  3190. ret = __uverbs_create_xsrq(file, &xcmd, &udata);
  3191. if (ret)
  3192. return ret;
  3193. return in_len;
  3194. }
  3195. ssize_t ib_uverbs_create_xsrq(struct ib_uverbs_file *file,
  3196. const char __user *buf, int in_len, int out_len)
  3197. {
  3198. struct ib_uverbs_create_xsrq cmd;
  3199. struct ib_uverbs_create_srq_resp resp;
  3200. struct ib_udata udata;
  3201. int ret;
  3202. if (out_len < sizeof resp)
  3203. return -ENOSPC;
  3204. if (copy_from_user(&cmd, buf, sizeof cmd))
  3205. return -EFAULT;
  3206. ib_uverbs_init_udata(&udata, buf + sizeof(cmd),
  3207. u64_to_user_ptr(cmd.response) + sizeof(resp),
  3208. in_len - sizeof(cmd) - sizeof(struct ib_uverbs_cmd_hdr),
  3209. out_len - sizeof(resp));
  3210. ret = __uverbs_create_xsrq(file, &cmd, &udata);
  3211. if (ret)
  3212. return ret;
  3213. return in_len;
  3214. }
  3215. ssize_t ib_uverbs_modify_srq(struct ib_uverbs_file *file,
  3216. const char __user *buf, int in_len,
  3217. int out_len)
  3218. {
  3219. struct ib_uverbs_modify_srq cmd;
  3220. struct ib_udata udata;
  3221. struct ib_srq *srq;
  3222. struct ib_srq_attr attr;
  3223. int ret;
  3224. if (copy_from_user(&cmd, buf, sizeof cmd))
  3225. return -EFAULT;
  3226. ib_uverbs_init_udata(&udata, buf + sizeof cmd, NULL, in_len - sizeof cmd,
  3227. out_len);
  3228. srq = uobj_get_obj_read(srq, UVERBS_OBJECT_SRQ, cmd.srq_handle, file);
  3229. if (!srq)
  3230. return -EINVAL;
  3231. attr.max_wr = cmd.max_wr;
  3232. attr.srq_limit = cmd.srq_limit;
  3233. ret = srq->device->modify_srq(srq, &attr, cmd.attr_mask, &udata);
  3234. uobj_put_obj_read(srq);
  3235. return ret ? ret : in_len;
  3236. }
  3237. ssize_t ib_uverbs_query_srq(struct ib_uverbs_file *file,
  3238. const char __user *buf,
  3239. int in_len, int out_len)
  3240. {
  3241. struct ib_uverbs_query_srq cmd;
  3242. struct ib_uverbs_query_srq_resp resp;
  3243. struct ib_srq_attr attr;
  3244. struct ib_srq *srq;
  3245. int ret;
  3246. if (out_len < sizeof resp)
  3247. return -ENOSPC;
  3248. if (copy_from_user(&cmd, buf, sizeof cmd))
  3249. return -EFAULT;
  3250. srq = uobj_get_obj_read(srq, UVERBS_OBJECT_SRQ, cmd.srq_handle, file);
  3251. if (!srq)
  3252. return -EINVAL;
  3253. ret = ib_query_srq(srq, &attr);
  3254. uobj_put_obj_read(srq);
  3255. if (ret)
  3256. return ret;
  3257. memset(&resp, 0, sizeof resp);
  3258. resp.max_wr = attr.max_wr;
  3259. resp.max_sge = attr.max_sge;
  3260. resp.srq_limit = attr.srq_limit;
  3261. if (copy_to_user(u64_to_user_ptr(cmd.response), &resp, sizeof resp))
  3262. return -EFAULT;
  3263. return in_len;
  3264. }
  3265. ssize_t ib_uverbs_destroy_srq(struct ib_uverbs_file *file,
  3266. const char __user *buf, int in_len,
  3267. int out_len)
  3268. {
  3269. struct ib_uverbs_destroy_srq cmd;
  3270. struct ib_uverbs_destroy_srq_resp resp;
  3271. struct ib_uobject *uobj;
  3272. struct ib_uevent_object *obj;
  3273. if (copy_from_user(&cmd, buf, sizeof cmd))
  3274. return -EFAULT;
  3275. uobj = uobj_get_destroy(UVERBS_OBJECT_SRQ, cmd.srq_handle, file);
  3276. if (IS_ERR(uobj))
  3277. return PTR_ERR(uobj);
  3278. obj = container_of(uobj, struct ib_uevent_object, uobject);
  3279. memset(&resp, 0, sizeof(resp));
  3280. resp.events_reported = obj->events_reported;
  3281. uobj_put_destroy(uobj);
  3282. if (copy_to_user(u64_to_user_ptr(cmd.response), &resp, sizeof(resp)))
  3283. return -EFAULT;
  3284. return in_len;
  3285. }
  3286. int ib_uverbs_ex_query_device(struct ib_uverbs_file *file,
  3287. struct ib_udata *ucore,
  3288. struct ib_udata *uhw)
  3289. {
  3290. struct ib_uverbs_ex_query_device_resp resp = { {0} };
  3291. struct ib_uverbs_ex_query_device cmd;
  3292. struct ib_device_attr attr = {0};
  3293. struct ib_ucontext *ucontext;
  3294. struct ib_device *ib_dev;
  3295. int err;
  3296. ucontext = ib_uverbs_get_ucontext(file);
  3297. if (IS_ERR(ucontext))
  3298. return PTR_ERR(ucontext);
  3299. ib_dev = ucontext->device;
  3300. if (!ib_dev->query_device)
  3301. return -EOPNOTSUPP;
  3302. if (ucore->inlen < sizeof(cmd))
  3303. return -EINVAL;
  3304. err = ib_copy_from_udata(&cmd, ucore, sizeof(cmd));
  3305. if (err)
  3306. return err;
  3307. if (cmd.comp_mask)
  3308. return -EINVAL;
  3309. if (cmd.reserved)
  3310. return -EINVAL;
  3311. resp.response_length = offsetof(typeof(resp), odp_caps);
  3312. if (ucore->outlen < resp.response_length)
  3313. return -ENOSPC;
  3314. err = ib_dev->query_device(ib_dev, &attr, uhw);
  3315. if (err)
  3316. return err;
  3317. copy_query_dev_fields(ucontext, &resp.base, &attr);
  3318. if (ucore->outlen < resp.response_length + sizeof(resp.odp_caps))
  3319. goto end;
  3320. #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
  3321. resp.odp_caps.general_caps = attr.odp_caps.general_caps;
  3322. resp.odp_caps.per_transport_caps.rc_odp_caps =
  3323. attr.odp_caps.per_transport_caps.rc_odp_caps;
  3324. resp.odp_caps.per_transport_caps.uc_odp_caps =
  3325. attr.odp_caps.per_transport_caps.uc_odp_caps;
  3326. resp.odp_caps.per_transport_caps.ud_odp_caps =
  3327. attr.odp_caps.per_transport_caps.ud_odp_caps;
  3328. #endif
  3329. resp.response_length += sizeof(resp.odp_caps);
  3330. if (ucore->outlen < resp.response_length + sizeof(resp.timestamp_mask))
  3331. goto end;
  3332. resp.timestamp_mask = attr.timestamp_mask;
  3333. resp.response_length += sizeof(resp.timestamp_mask);
  3334. if (ucore->outlen < resp.response_length + sizeof(resp.hca_core_clock))
  3335. goto end;
  3336. resp.hca_core_clock = attr.hca_core_clock;
  3337. resp.response_length += sizeof(resp.hca_core_clock);
  3338. if (ucore->outlen < resp.response_length + sizeof(resp.device_cap_flags_ex))
  3339. goto end;
  3340. resp.device_cap_flags_ex = attr.device_cap_flags;
  3341. resp.response_length += sizeof(resp.device_cap_flags_ex);
  3342. if (ucore->outlen < resp.response_length + sizeof(resp.rss_caps))
  3343. goto end;
  3344. resp.rss_caps.supported_qpts = attr.rss_caps.supported_qpts;
  3345. resp.rss_caps.max_rwq_indirection_tables =
  3346. attr.rss_caps.max_rwq_indirection_tables;
  3347. resp.rss_caps.max_rwq_indirection_table_size =
  3348. attr.rss_caps.max_rwq_indirection_table_size;
  3349. resp.response_length += sizeof(resp.rss_caps);
  3350. if (ucore->outlen < resp.response_length + sizeof(resp.max_wq_type_rq))
  3351. goto end;
  3352. resp.max_wq_type_rq = attr.max_wq_type_rq;
  3353. resp.response_length += sizeof(resp.max_wq_type_rq);
  3354. if (ucore->outlen < resp.response_length + sizeof(resp.raw_packet_caps))
  3355. goto end;
  3356. resp.raw_packet_caps = attr.raw_packet_caps;
  3357. resp.response_length += sizeof(resp.raw_packet_caps);
  3358. if (ucore->outlen < resp.response_length + sizeof(resp.tm_caps))
  3359. goto end;
  3360. resp.tm_caps.max_rndv_hdr_size = attr.tm_caps.max_rndv_hdr_size;
  3361. resp.tm_caps.max_num_tags = attr.tm_caps.max_num_tags;
  3362. resp.tm_caps.max_ops = attr.tm_caps.max_ops;
  3363. resp.tm_caps.max_sge = attr.tm_caps.max_sge;
  3364. resp.tm_caps.flags = attr.tm_caps.flags;
  3365. resp.response_length += sizeof(resp.tm_caps);
  3366. if (ucore->outlen < resp.response_length + sizeof(resp.cq_moderation_caps))
  3367. goto end;
  3368. resp.cq_moderation_caps.max_cq_moderation_count =
  3369. attr.cq_caps.max_cq_moderation_count;
  3370. resp.cq_moderation_caps.max_cq_moderation_period =
  3371. attr.cq_caps.max_cq_moderation_period;
  3372. resp.response_length += sizeof(resp.cq_moderation_caps);
  3373. if (ucore->outlen < resp.response_length + sizeof(resp.max_dm_size))
  3374. goto end;
  3375. resp.max_dm_size = attr.max_dm_size;
  3376. resp.response_length += sizeof(resp.max_dm_size);
  3377. end:
  3378. err = ib_copy_to_udata(ucore, &resp, resp.response_length);
  3379. return err;
  3380. }
  3381. int ib_uverbs_ex_modify_cq(struct ib_uverbs_file *file,
  3382. struct ib_udata *ucore,
  3383. struct ib_udata *uhw)
  3384. {
  3385. struct ib_uverbs_ex_modify_cq cmd = {};
  3386. struct ib_cq *cq;
  3387. size_t required_cmd_sz;
  3388. int ret;
  3389. required_cmd_sz = offsetof(typeof(cmd), reserved) +
  3390. sizeof(cmd.reserved);
  3391. if (ucore->inlen < required_cmd_sz)
  3392. return -EINVAL;
  3393. /* sanity checks */
  3394. if (ucore->inlen > sizeof(cmd) &&
  3395. !ib_is_udata_cleared(ucore, sizeof(cmd),
  3396. ucore->inlen - sizeof(cmd)))
  3397. return -EOPNOTSUPP;
  3398. ret = ib_copy_from_udata(&cmd, ucore, min(sizeof(cmd), ucore->inlen));
  3399. if (ret)
  3400. return ret;
  3401. if (!cmd.attr_mask || cmd.reserved)
  3402. return -EINVAL;
  3403. if (cmd.attr_mask > IB_CQ_MODERATE)
  3404. return -EOPNOTSUPP;
  3405. cq = uobj_get_obj_read(cq, UVERBS_OBJECT_CQ, cmd.cq_handle, file);
  3406. if (!cq)
  3407. return -EINVAL;
  3408. ret = rdma_set_cq_moderation(cq, cmd.attr.cq_count, cmd.attr.cq_period);
  3409. uobj_put_obj_read(cq);
  3410. return ret;
  3411. }