uverbs_cmd.c 103 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113
  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 = false;
  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.qp_state > IB_QPS_ERR) {
  1673. ret = -EINVAL;
  1674. goto release_qp;
  1675. }
  1676. attr->qp_state = cmd->base.qp_state;
  1677. attr->cur_qp_state = cmd->base.cur_qp_state;
  1678. attr->path_mtu = cmd->base.path_mtu;
  1679. attr->path_mig_state = cmd->base.path_mig_state;
  1680. attr->qkey = cmd->base.qkey;
  1681. attr->rq_psn = cmd->base.rq_psn;
  1682. attr->sq_psn = cmd->base.sq_psn;
  1683. attr->dest_qp_num = cmd->base.dest_qp_num;
  1684. attr->qp_access_flags = cmd->base.qp_access_flags;
  1685. attr->pkey_index = cmd->base.pkey_index;
  1686. attr->alt_pkey_index = cmd->base.alt_pkey_index;
  1687. attr->en_sqd_async_notify = cmd->base.en_sqd_async_notify;
  1688. attr->max_rd_atomic = cmd->base.max_rd_atomic;
  1689. attr->max_dest_rd_atomic = cmd->base.max_dest_rd_atomic;
  1690. attr->min_rnr_timer = cmd->base.min_rnr_timer;
  1691. attr->port_num = cmd->base.port_num;
  1692. attr->timeout = cmd->base.timeout;
  1693. attr->retry_cnt = cmd->base.retry_cnt;
  1694. attr->rnr_retry = cmd->base.rnr_retry;
  1695. attr->alt_port_num = cmd->base.alt_port_num;
  1696. attr->alt_timeout = cmd->base.alt_timeout;
  1697. attr->rate_limit = cmd->rate_limit;
  1698. if (cmd->base.attr_mask & IB_QP_AV)
  1699. copy_ah_attr_from_uverbs(qp->device, &attr->ah_attr,
  1700. &cmd->base.dest);
  1701. if (cmd->base.attr_mask & IB_QP_ALT_PATH)
  1702. copy_ah_attr_from_uverbs(qp->device, &attr->alt_ah_attr,
  1703. &cmd->base.alt_dest);
  1704. ret = ib_modify_qp_with_udata(qp, attr,
  1705. modify_qp_mask(qp->qp_type,
  1706. cmd->base.attr_mask),
  1707. udata);
  1708. release_qp:
  1709. uobj_put_obj_read(qp);
  1710. out:
  1711. kfree(attr);
  1712. return ret;
  1713. }
  1714. ssize_t ib_uverbs_modify_qp(struct ib_uverbs_file *file,
  1715. const char __user *buf, int in_len,
  1716. int out_len)
  1717. {
  1718. struct ib_uverbs_ex_modify_qp cmd = {};
  1719. struct ib_udata udata;
  1720. int ret;
  1721. if (copy_from_user(&cmd.base, buf, sizeof(cmd.base)))
  1722. return -EFAULT;
  1723. if (cmd.base.attr_mask &
  1724. ~((IB_USER_LEGACY_LAST_QP_ATTR_MASK << 1) - 1))
  1725. return -EOPNOTSUPP;
  1726. ib_uverbs_init_udata(&udata, buf + sizeof(cmd.base), NULL,
  1727. in_len - sizeof(cmd.base) - sizeof(struct ib_uverbs_cmd_hdr),
  1728. out_len);
  1729. ret = modify_qp(file, &cmd, &udata);
  1730. if (ret)
  1731. return ret;
  1732. return in_len;
  1733. }
  1734. int ib_uverbs_ex_modify_qp(struct ib_uverbs_file *file,
  1735. struct ib_udata *ucore,
  1736. struct ib_udata *uhw)
  1737. {
  1738. struct ib_uverbs_ex_modify_qp cmd = {};
  1739. int ret;
  1740. /*
  1741. * Last bit is reserved for extending the attr_mask by
  1742. * using another field.
  1743. */
  1744. BUILD_BUG_ON(IB_USER_LAST_QP_ATTR_MASK == (1 << 31));
  1745. if (ucore->inlen < sizeof(cmd.base))
  1746. return -EINVAL;
  1747. ret = ib_copy_from_udata(&cmd, ucore, min(sizeof(cmd), ucore->inlen));
  1748. if (ret)
  1749. return ret;
  1750. if (cmd.base.attr_mask &
  1751. ~((IB_USER_LAST_QP_ATTR_MASK << 1) - 1))
  1752. return -EOPNOTSUPP;
  1753. if (ucore->inlen > sizeof(cmd)) {
  1754. if (!ib_is_udata_cleared(ucore, sizeof(cmd),
  1755. ucore->inlen - sizeof(cmd)))
  1756. return -EOPNOTSUPP;
  1757. }
  1758. ret = modify_qp(file, &cmd, uhw);
  1759. return ret;
  1760. }
  1761. ssize_t ib_uverbs_destroy_qp(struct ib_uverbs_file *file,
  1762. const char __user *buf, int in_len,
  1763. int out_len)
  1764. {
  1765. struct ib_uverbs_destroy_qp cmd;
  1766. struct ib_uverbs_destroy_qp_resp resp;
  1767. struct ib_uobject *uobj;
  1768. struct ib_uqp_object *obj;
  1769. if (copy_from_user(&cmd, buf, sizeof cmd))
  1770. return -EFAULT;
  1771. uobj = uobj_get_destroy(UVERBS_OBJECT_QP, cmd.qp_handle, file);
  1772. if (IS_ERR(uobj))
  1773. return PTR_ERR(uobj);
  1774. obj = container_of(uobj, struct ib_uqp_object, uevent.uobject);
  1775. memset(&resp, 0, sizeof(resp));
  1776. resp.events_reported = obj->uevent.events_reported;
  1777. uobj_put_destroy(uobj);
  1778. if (copy_to_user(u64_to_user_ptr(cmd.response), &resp, sizeof resp))
  1779. return -EFAULT;
  1780. return in_len;
  1781. }
  1782. static void *alloc_wr(size_t wr_size, __u32 num_sge)
  1783. {
  1784. if (num_sge >= (U32_MAX - ALIGN(wr_size, sizeof (struct ib_sge))) /
  1785. sizeof (struct ib_sge))
  1786. return NULL;
  1787. return kmalloc(ALIGN(wr_size, sizeof (struct ib_sge)) +
  1788. num_sge * sizeof (struct ib_sge), GFP_KERNEL);
  1789. }
  1790. ssize_t ib_uverbs_post_send(struct ib_uverbs_file *file,
  1791. const char __user *buf, int in_len,
  1792. int out_len)
  1793. {
  1794. struct ib_uverbs_post_send cmd;
  1795. struct ib_uverbs_post_send_resp resp;
  1796. struct ib_uverbs_send_wr *user_wr;
  1797. struct ib_send_wr *wr = NULL, *last, *next;
  1798. const struct ib_send_wr *bad_wr;
  1799. struct ib_qp *qp;
  1800. int i, sg_ind;
  1801. int is_ud;
  1802. ssize_t ret = -EINVAL;
  1803. size_t next_size;
  1804. if (copy_from_user(&cmd, buf, sizeof cmd))
  1805. return -EFAULT;
  1806. if (in_len < sizeof cmd + cmd.wqe_size * cmd.wr_count +
  1807. cmd.sge_count * sizeof (struct ib_uverbs_sge))
  1808. return -EINVAL;
  1809. if (cmd.wqe_size < sizeof (struct ib_uverbs_send_wr))
  1810. return -EINVAL;
  1811. user_wr = kmalloc(cmd.wqe_size, GFP_KERNEL);
  1812. if (!user_wr)
  1813. return -ENOMEM;
  1814. qp = uobj_get_obj_read(qp, UVERBS_OBJECT_QP, cmd.qp_handle, file);
  1815. if (!qp)
  1816. goto out;
  1817. is_ud = qp->qp_type == IB_QPT_UD;
  1818. sg_ind = 0;
  1819. last = NULL;
  1820. for (i = 0; i < cmd.wr_count; ++i) {
  1821. if (copy_from_user(user_wr,
  1822. buf + sizeof cmd + i * cmd.wqe_size,
  1823. cmd.wqe_size)) {
  1824. ret = -EFAULT;
  1825. goto out_put;
  1826. }
  1827. if (user_wr->num_sge + sg_ind > cmd.sge_count) {
  1828. ret = -EINVAL;
  1829. goto out_put;
  1830. }
  1831. if (is_ud) {
  1832. struct ib_ud_wr *ud;
  1833. if (user_wr->opcode != IB_WR_SEND &&
  1834. user_wr->opcode != IB_WR_SEND_WITH_IMM) {
  1835. ret = -EINVAL;
  1836. goto out_put;
  1837. }
  1838. next_size = sizeof(*ud);
  1839. ud = alloc_wr(next_size, user_wr->num_sge);
  1840. if (!ud) {
  1841. ret = -ENOMEM;
  1842. goto out_put;
  1843. }
  1844. ud->ah = uobj_get_obj_read(ah, UVERBS_OBJECT_AH,
  1845. user_wr->wr.ud.ah, file);
  1846. if (!ud->ah) {
  1847. kfree(ud);
  1848. ret = -EINVAL;
  1849. goto out_put;
  1850. }
  1851. ud->remote_qpn = user_wr->wr.ud.remote_qpn;
  1852. ud->remote_qkey = user_wr->wr.ud.remote_qkey;
  1853. next = &ud->wr;
  1854. } else if (user_wr->opcode == IB_WR_RDMA_WRITE_WITH_IMM ||
  1855. user_wr->opcode == IB_WR_RDMA_WRITE ||
  1856. user_wr->opcode == IB_WR_RDMA_READ) {
  1857. struct ib_rdma_wr *rdma;
  1858. next_size = sizeof(*rdma);
  1859. rdma = alloc_wr(next_size, user_wr->num_sge);
  1860. if (!rdma) {
  1861. ret = -ENOMEM;
  1862. goto out_put;
  1863. }
  1864. rdma->remote_addr = user_wr->wr.rdma.remote_addr;
  1865. rdma->rkey = user_wr->wr.rdma.rkey;
  1866. next = &rdma->wr;
  1867. } else if (user_wr->opcode == IB_WR_ATOMIC_CMP_AND_SWP ||
  1868. user_wr->opcode == IB_WR_ATOMIC_FETCH_AND_ADD) {
  1869. struct ib_atomic_wr *atomic;
  1870. next_size = sizeof(*atomic);
  1871. atomic = alloc_wr(next_size, user_wr->num_sge);
  1872. if (!atomic) {
  1873. ret = -ENOMEM;
  1874. goto out_put;
  1875. }
  1876. atomic->remote_addr = user_wr->wr.atomic.remote_addr;
  1877. atomic->compare_add = user_wr->wr.atomic.compare_add;
  1878. atomic->swap = user_wr->wr.atomic.swap;
  1879. atomic->rkey = user_wr->wr.atomic.rkey;
  1880. next = &atomic->wr;
  1881. } else if (user_wr->opcode == IB_WR_SEND ||
  1882. user_wr->opcode == IB_WR_SEND_WITH_IMM ||
  1883. user_wr->opcode == IB_WR_SEND_WITH_INV) {
  1884. next_size = sizeof(*next);
  1885. next = alloc_wr(next_size, user_wr->num_sge);
  1886. if (!next) {
  1887. ret = -ENOMEM;
  1888. goto out_put;
  1889. }
  1890. } else {
  1891. ret = -EINVAL;
  1892. goto out_put;
  1893. }
  1894. if (user_wr->opcode == IB_WR_SEND_WITH_IMM ||
  1895. user_wr->opcode == IB_WR_RDMA_WRITE_WITH_IMM) {
  1896. next->ex.imm_data =
  1897. (__be32 __force) user_wr->ex.imm_data;
  1898. } else if (user_wr->opcode == IB_WR_SEND_WITH_INV) {
  1899. next->ex.invalidate_rkey = user_wr->ex.invalidate_rkey;
  1900. }
  1901. if (!last)
  1902. wr = next;
  1903. else
  1904. last->next = next;
  1905. last = next;
  1906. next->next = NULL;
  1907. next->wr_id = user_wr->wr_id;
  1908. next->num_sge = user_wr->num_sge;
  1909. next->opcode = user_wr->opcode;
  1910. next->send_flags = user_wr->send_flags;
  1911. if (next->num_sge) {
  1912. next->sg_list = (void *) next +
  1913. ALIGN(next_size, sizeof(struct ib_sge));
  1914. if (copy_from_user(next->sg_list,
  1915. buf + sizeof cmd +
  1916. cmd.wr_count * cmd.wqe_size +
  1917. sg_ind * sizeof (struct ib_sge),
  1918. next->num_sge * sizeof (struct ib_sge))) {
  1919. ret = -EFAULT;
  1920. goto out_put;
  1921. }
  1922. sg_ind += next->num_sge;
  1923. } else
  1924. next->sg_list = NULL;
  1925. }
  1926. resp.bad_wr = 0;
  1927. ret = qp->device->post_send(qp->real_qp, wr, &bad_wr);
  1928. if (ret)
  1929. for (next = wr; next; next = next->next) {
  1930. ++resp.bad_wr;
  1931. if (next == bad_wr)
  1932. break;
  1933. }
  1934. if (copy_to_user(u64_to_user_ptr(cmd.response), &resp, sizeof resp))
  1935. ret = -EFAULT;
  1936. out_put:
  1937. uobj_put_obj_read(qp);
  1938. while (wr) {
  1939. if (is_ud && ud_wr(wr)->ah)
  1940. uobj_put_obj_read(ud_wr(wr)->ah);
  1941. next = wr->next;
  1942. kfree(wr);
  1943. wr = next;
  1944. }
  1945. out:
  1946. kfree(user_wr);
  1947. return ret ? ret : in_len;
  1948. }
  1949. static struct ib_recv_wr *ib_uverbs_unmarshall_recv(const char __user *buf,
  1950. int in_len,
  1951. u32 wr_count,
  1952. u32 sge_count,
  1953. u32 wqe_size)
  1954. {
  1955. struct ib_uverbs_recv_wr *user_wr;
  1956. struct ib_recv_wr *wr = NULL, *last, *next;
  1957. int sg_ind;
  1958. int i;
  1959. int ret;
  1960. if (in_len < wqe_size * wr_count +
  1961. sge_count * sizeof (struct ib_uverbs_sge))
  1962. return ERR_PTR(-EINVAL);
  1963. if (wqe_size < sizeof (struct ib_uverbs_recv_wr))
  1964. return ERR_PTR(-EINVAL);
  1965. user_wr = kmalloc(wqe_size, GFP_KERNEL);
  1966. if (!user_wr)
  1967. return ERR_PTR(-ENOMEM);
  1968. sg_ind = 0;
  1969. last = NULL;
  1970. for (i = 0; i < wr_count; ++i) {
  1971. if (copy_from_user(user_wr, buf + i * wqe_size,
  1972. wqe_size)) {
  1973. ret = -EFAULT;
  1974. goto err;
  1975. }
  1976. if (user_wr->num_sge + sg_ind > sge_count) {
  1977. ret = -EINVAL;
  1978. goto err;
  1979. }
  1980. if (user_wr->num_sge >=
  1981. (U32_MAX - ALIGN(sizeof *next, sizeof (struct ib_sge))) /
  1982. sizeof (struct ib_sge)) {
  1983. ret = -EINVAL;
  1984. goto err;
  1985. }
  1986. next = kmalloc(ALIGN(sizeof *next, sizeof (struct ib_sge)) +
  1987. user_wr->num_sge * sizeof (struct ib_sge),
  1988. GFP_KERNEL);
  1989. if (!next) {
  1990. ret = -ENOMEM;
  1991. goto err;
  1992. }
  1993. if (!last)
  1994. wr = next;
  1995. else
  1996. last->next = next;
  1997. last = next;
  1998. next->next = NULL;
  1999. next->wr_id = user_wr->wr_id;
  2000. next->num_sge = user_wr->num_sge;
  2001. if (next->num_sge) {
  2002. next->sg_list = (void *) next +
  2003. ALIGN(sizeof *next, sizeof (struct ib_sge));
  2004. if (copy_from_user(next->sg_list,
  2005. buf + wr_count * wqe_size +
  2006. sg_ind * sizeof (struct ib_sge),
  2007. next->num_sge * sizeof (struct ib_sge))) {
  2008. ret = -EFAULT;
  2009. goto err;
  2010. }
  2011. sg_ind += next->num_sge;
  2012. } else
  2013. next->sg_list = NULL;
  2014. }
  2015. kfree(user_wr);
  2016. return wr;
  2017. err:
  2018. kfree(user_wr);
  2019. while (wr) {
  2020. next = wr->next;
  2021. kfree(wr);
  2022. wr = next;
  2023. }
  2024. return ERR_PTR(ret);
  2025. }
  2026. ssize_t ib_uverbs_post_recv(struct ib_uverbs_file *file,
  2027. const char __user *buf, int in_len,
  2028. int out_len)
  2029. {
  2030. struct ib_uverbs_post_recv cmd;
  2031. struct ib_uverbs_post_recv_resp resp;
  2032. struct ib_recv_wr *wr, *next;
  2033. const struct ib_recv_wr *bad_wr;
  2034. struct ib_qp *qp;
  2035. ssize_t ret = -EINVAL;
  2036. if (copy_from_user(&cmd, buf, sizeof cmd))
  2037. return -EFAULT;
  2038. wr = ib_uverbs_unmarshall_recv(buf + sizeof cmd,
  2039. in_len - sizeof cmd, cmd.wr_count,
  2040. cmd.sge_count, cmd.wqe_size);
  2041. if (IS_ERR(wr))
  2042. return PTR_ERR(wr);
  2043. qp = uobj_get_obj_read(qp, UVERBS_OBJECT_QP, cmd.qp_handle, file);
  2044. if (!qp)
  2045. goto out;
  2046. resp.bad_wr = 0;
  2047. ret = qp->device->post_recv(qp->real_qp, wr, &bad_wr);
  2048. uobj_put_obj_read(qp);
  2049. if (ret) {
  2050. for (next = wr; next; next = next->next) {
  2051. ++resp.bad_wr;
  2052. if (next == bad_wr)
  2053. break;
  2054. }
  2055. }
  2056. if (copy_to_user(u64_to_user_ptr(cmd.response), &resp, sizeof resp))
  2057. ret = -EFAULT;
  2058. out:
  2059. while (wr) {
  2060. next = wr->next;
  2061. kfree(wr);
  2062. wr = next;
  2063. }
  2064. return ret ? ret : in_len;
  2065. }
  2066. ssize_t ib_uverbs_post_srq_recv(struct ib_uverbs_file *file,
  2067. const char __user *buf, int in_len,
  2068. int out_len)
  2069. {
  2070. struct ib_uverbs_post_srq_recv cmd;
  2071. struct ib_uverbs_post_srq_recv_resp resp;
  2072. struct ib_recv_wr *wr, *next;
  2073. const struct ib_recv_wr *bad_wr;
  2074. struct ib_srq *srq;
  2075. ssize_t ret = -EINVAL;
  2076. if (copy_from_user(&cmd, buf, sizeof cmd))
  2077. return -EFAULT;
  2078. wr = ib_uverbs_unmarshall_recv(buf + sizeof cmd,
  2079. in_len - sizeof cmd, cmd.wr_count,
  2080. cmd.sge_count, cmd.wqe_size);
  2081. if (IS_ERR(wr))
  2082. return PTR_ERR(wr);
  2083. srq = uobj_get_obj_read(srq, UVERBS_OBJECT_SRQ, cmd.srq_handle, file);
  2084. if (!srq)
  2085. goto out;
  2086. resp.bad_wr = 0;
  2087. ret = srq->device->post_srq_recv ?
  2088. srq->device->post_srq_recv(srq, wr, &bad_wr) : -EOPNOTSUPP;
  2089. uobj_put_obj_read(srq);
  2090. if (ret)
  2091. for (next = wr; next; next = next->next) {
  2092. ++resp.bad_wr;
  2093. if (next == bad_wr)
  2094. break;
  2095. }
  2096. if (copy_to_user(u64_to_user_ptr(cmd.response), &resp, sizeof resp))
  2097. ret = -EFAULT;
  2098. out:
  2099. while (wr) {
  2100. next = wr->next;
  2101. kfree(wr);
  2102. wr = next;
  2103. }
  2104. return ret ? ret : in_len;
  2105. }
  2106. ssize_t ib_uverbs_create_ah(struct ib_uverbs_file *file,
  2107. const char __user *buf, int in_len,
  2108. int out_len)
  2109. {
  2110. struct ib_uverbs_create_ah cmd;
  2111. struct ib_uverbs_create_ah_resp resp;
  2112. struct ib_uobject *uobj;
  2113. struct ib_pd *pd;
  2114. struct ib_ah *ah;
  2115. struct rdma_ah_attr attr = {};
  2116. int ret;
  2117. struct ib_udata udata;
  2118. struct ib_device *ib_dev;
  2119. if (out_len < sizeof resp)
  2120. return -ENOSPC;
  2121. if (copy_from_user(&cmd, buf, sizeof cmd))
  2122. return -EFAULT;
  2123. ib_uverbs_init_udata(&udata, buf + sizeof(cmd),
  2124. u64_to_user_ptr(cmd.response) + sizeof(resp),
  2125. in_len - sizeof(cmd) - sizeof(struct ib_uverbs_cmd_hdr),
  2126. out_len - sizeof(resp));
  2127. uobj = uobj_alloc(UVERBS_OBJECT_AH, file, &ib_dev);
  2128. if (IS_ERR(uobj))
  2129. return PTR_ERR(uobj);
  2130. if (!rdma_is_port_valid(ib_dev, cmd.attr.port_num)) {
  2131. ret = -EINVAL;
  2132. goto err;
  2133. }
  2134. pd = uobj_get_obj_read(pd, UVERBS_OBJECT_PD, cmd.pd_handle, file);
  2135. if (!pd) {
  2136. ret = -EINVAL;
  2137. goto err;
  2138. }
  2139. attr.type = rdma_ah_find_type(ib_dev, cmd.attr.port_num);
  2140. rdma_ah_set_make_grd(&attr, false);
  2141. rdma_ah_set_dlid(&attr, cmd.attr.dlid);
  2142. rdma_ah_set_sl(&attr, cmd.attr.sl);
  2143. rdma_ah_set_path_bits(&attr, cmd.attr.src_path_bits);
  2144. rdma_ah_set_static_rate(&attr, cmd.attr.static_rate);
  2145. rdma_ah_set_port_num(&attr, cmd.attr.port_num);
  2146. if (cmd.attr.is_global) {
  2147. rdma_ah_set_grh(&attr, NULL, cmd.attr.grh.flow_label,
  2148. cmd.attr.grh.sgid_index,
  2149. cmd.attr.grh.hop_limit,
  2150. cmd.attr.grh.traffic_class);
  2151. rdma_ah_set_dgid_raw(&attr, cmd.attr.grh.dgid);
  2152. } else {
  2153. rdma_ah_set_ah_flags(&attr, 0);
  2154. }
  2155. ah = rdma_create_user_ah(pd, &attr, &udata);
  2156. if (IS_ERR(ah)) {
  2157. ret = PTR_ERR(ah);
  2158. goto err_put;
  2159. }
  2160. ah->uobject = uobj;
  2161. uobj->user_handle = cmd.user_handle;
  2162. uobj->object = ah;
  2163. resp.ah_handle = uobj->id;
  2164. if (copy_to_user(u64_to_user_ptr(cmd.response), &resp, sizeof resp)) {
  2165. ret = -EFAULT;
  2166. goto err_copy;
  2167. }
  2168. uobj_put_obj_read(pd);
  2169. return uobj_alloc_commit(uobj, in_len);
  2170. err_copy:
  2171. rdma_destroy_ah(ah);
  2172. err_put:
  2173. uobj_put_obj_read(pd);
  2174. err:
  2175. uobj_alloc_abort(uobj);
  2176. return ret;
  2177. }
  2178. ssize_t ib_uverbs_destroy_ah(struct ib_uverbs_file *file,
  2179. const char __user *buf, int in_len, int out_len)
  2180. {
  2181. struct ib_uverbs_destroy_ah cmd;
  2182. if (copy_from_user(&cmd, buf, sizeof cmd))
  2183. return -EFAULT;
  2184. return uobj_perform_destroy(UVERBS_OBJECT_AH, cmd.ah_handle, file,
  2185. in_len);
  2186. }
  2187. ssize_t ib_uverbs_attach_mcast(struct ib_uverbs_file *file,
  2188. const char __user *buf, int in_len,
  2189. int out_len)
  2190. {
  2191. struct ib_uverbs_attach_mcast cmd;
  2192. struct ib_qp *qp;
  2193. struct ib_uqp_object *obj;
  2194. struct ib_uverbs_mcast_entry *mcast;
  2195. int ret;
  2196. if (copy_from_user(&cmd, buf, sizeof cmd))
  2197. return -EFAULT;
  2198. qp = uobj_get_obj_read(qp, UVERBS_OBJECT_QP, cmd.qp_handle, file);
  2199. if (!qp)
  2200. return -EINVAL;
  2201. obj = container_of(qp->uobject, struct ib_uqp_object, uevent.uobject);
  2202. mutex_lock(&obj->mcast_lock);
  2203. list_for_each_entry(mcast, &obj->mcast_list, list)
  2204. if (cmd.mlid == mcast->lid &&
  2205. !memcmp(cmd.gid, mcast->gid.raw, sizeof mcast->gid.raw)) {
  2206. ret = 0;
  2207. goto out_put;
  2208. }
  2209. mcast = kmalloc(sizeof *mcast, GFP_KERNEL);
  2210. if (!mcast) {
  2211. ret = -ENOMEM;
  2212. goto out_put;
  2213. }
  2214. mcast->lid = cmd.mlid;
  2215. memcpy(mcast->gid.raw, cmd.gid, sizeof mcast->gid.raw);
  2216. ret = ib_attach_mcast(qp, &mcast->gid, cmd.mlid);
  2217. if (!ret)
  2218. list_add_tail(&mcast->list, &obj->mcast_list);
  2219. else
  2220. kfree(mcast);
  2221. out_put:
  2222. mutex_unlock(&obj->mcast_lock);
  2223. uobj_put_obj_read(qp);
  2224. return ret ? ret : in_len;
  2225. }
  2226. ssize_t ib_uverbs_detach_mcast(struct ib_uverbs_file *file,
  2227. const char __user *buf, int in_len,
  2228. int out_len)
  2229. {
  2230. struct ib_uverbs_detach_mcast cmd;
  2231. struct ib_uqp_object *obj;
  2232. struct ib_qp *qp;
  2233. struct ib_uverbs_mcast_entry *mcast;
  2234. int ret = -EINVAL;
  2235. bool found = false;
  2236. if (copy_from_user(&cmd, buf, sizeof cmd))
  2237. return -EFAULT;
  2238. qp = uobj_get_obj_read(qp, UVERBS_OBJECT_QP, cmd.qp_handle, file);
  2239. if (!qp)
  2240. return -EINVAL;
  2241. obj = container_of(qp->uobject, struct ib_uqp_object, uevent.uobject);
  2242. mutex_lock(&obj->mcast_lock);
  2243. list_for_each_entry(mcast, &obj->mcast_list, list)
  2244. if (cmd.mlid == mcast->lid &&
  2245. !memcmp(cmd.gid, mcast->gid.raw, sizeof mcast->gid.raw)) {
  2246. list_del(&mcast->list);
  2247. kfree(mcast);
  2248. found = true;
  2249. break;
  2250. }
  2251. if (!found) {
  2252. ret = -EINVAL;
  2253. goto out_put;
  2254. }
  2255. ret = ib_detach_mcast(qp, (union ib_gid *)cmd.gid, cmd.mlid);
  2256. out_put:
  2257. mutex_unlock(&obj->mcast_lock);
  2258. uobj_put_obj_read(qp);
  2259. return ret ? ret : in_len;
  2260. }
  2261. struct ib_uflow_resources *flow_resources_alloc(size_t num_specs)
  2262. {
  2263. struct ib_uflow_resources *resources;
  2264. resources = kzalloc(sizeof(*resources), GFP_KERNEL);
  2265. if (!resources)
  2266. return NULL;
  2267. if (!num_specs)
  2268. goto out;
  2269. resources->counters =
  2270. kcalloc(num_specs, sizeof(*resources->counters), GFP_KERNEL);
  2271. resources->collection =
  2272. kcalloc(num_specs, sizeof(*resources->collection), GFP_KERNEL);
  2273. if (!resources->counters || !resources->collection)
  2274. goto err;
  2275. out:
  2276. resources->max = num_specs;
  2277. return resources;
  2278. err:
  2279. kfree(resources->counters);
  2280. kfree(resources);
  2281. return NULL;
  2282. }
  2283. EXPORT_SYMBOL(flow_resources_alloc);
  2284. void ib_uverbs_flow_resources_free(struct ib_uflow_resources *uflow_res)
  2285. {
  2286. unsigned int i;
  2287. if (!uflow_res)
  2288. return;
  2289. for (i = 0; i < uflow_res->collection_num; i++)
  2290. atomic_dec(&uflow_res->collection[i]->usecnt);
  2291. for (i = 0; i < uflow_res->counters_num; i++)
  2292. atomic_dec(&uflow_res->counters[i]->usecnt);
  2293. kfree(uflow_res->collection);
  2294. kfree(uflow_res->counters);
  2295. kfree(uflow_res);
  2296. }
  2297. EXPORT_SYMBOL(ib_uverbs_flow_resources_free);
  2298. void flow_resources_add(struct ib_uflow_resources *uflow_res,
  2299. enum ib_flow_spec_type type,
  2300. void *ibobj)
  2301. {
  2302. WARN_ON(uflow_res->num >= uflow_res->max);
  2303. switch (type) {
  2304. case IB_FLOW_SPEC_ACTION_HANDLE:
  2305. atomic_inc(&((struct ib_flow_action *)ibobj)->usecnt);
  2306. uflow_res->collection[uflow_res->collection_num++] =
  2307. (struct ib_flow_action *)ibobj;
  2308. break;
  2309. case IB_FLOW_SPEC_ACTION_COUNT:
  2310. atomic_inc(&((struct ib_counters *)ibobj)->usecnt);
  2311. uflow_res->counters[uflow_res->counters_num++] =
  2312. (struct ib_counters *)ibobj;
  2313. break;
  2314. default:
  2315. WARN_ON(1);
  2316. }
  2317. uflow_res->num++;
  2318. }
  2319. EXPORT_SYMBOL(flow_resources_add);
  2320. static int kern_spec_to_ib_spec_action(struct ib_uverbs_file *ufile,
  2321. struct ib_uverbs_flow_spec *kern_spec,
  2322. union ib_flow_spec *ib_spec,
  2323. struct ib_uflow_resources *uflow_res)
  2324. {
  2325. ib_spec->type = kern_spec->type;
  2326. switch (ib_spec->type) {
  2327. case IB_FLOW_SPEC_ACTION_TAG:
  2328. if (kern_spec->flow_tag.size !=
  2329. sizeof(struct ib_uverbs_flow_spec_action_tag))
  2330. return -EINVAL;
  2331. ib_spec->flow_tag.size = sizeof(struct ib_flow_spec_action_tag);
  2332. ib_spec->flow_tag.tag_id = kern_spec->flow_tag.tag_id;
  2333. break;
  2334. case IB_FLOW_SPEC_ACTION_DROP:
  2335. if (kern_spec->drop.size !=
  2336. sizeof(struct ib_uverbs_flow_spec_action_drop))
  2337. return -EINVAL;
  2338. ib_spec->drop.size = sizeof(struct ib_flow_spec_action_drop);
  2339. break;
  2340. case IB_FLOW_SPEC_ACTION_HANDLE:
  2341. if (kern_spec->action.size !=
  2342. sizeof(struct ib_uverbs_flow_spec_action_handle))
  2343. return -EOPNOTSUPP;
  2344. ib_spec->action.act = uobj_get_obj_read(flow_action,
  2345. UVERBS_OBJECT_FLOW_ACTION,
  2346. kern_spec->action.handle,
  2347. ufile);
  2348. if (!ib_spec->action.act)
  2349. return -EINVAL;
  2350. ib_spec->action.size =
  2351. sizeof(struct ib_flow_spec_action_handle);
  2352. flow_resources_add(uflow_res,
  2353. IB_FLOW_SPEC_ACTION_HANDLE,
  2354. ib_spec->action.act);
  2355. uobj_put_obj_read(ib_spec->action.act);
  2356. break;
  2357. case IB_FLOW_SPEC_ACTION_COUNT:
  2358. if (kern_spec->flow_count.size !=
  2359. sizeof(struct ib_uverbs_flow_spec_action_count))
  2360. return -EINVAL;
  2361. ib_spec->flow_count.counters =
  2362. uobj_get_obj_read(counters,
  2363. UVERBS_OBJECT_COUNTERS,
  2364. kern_spec->flow_count.handle,
  2365. ufile);
  2366. if (!ib_spec->flow_count.counters)
  2367. return -EINVAL;
  2368. ib_spec->flow_count.size =
  2369. sizeof(struct ib_flow_spec_action_count);
  2370. flow_resources_add(uflow_res,
  2371. IB_FLOW_SPEC_ACTION_COUNT,
  2372. ib_spec->flow_count.counters);
  2373. uobj_put_obj_read(ib_spec->flow_count.counters);
  2374. break;
  2375. default:
  2376. return -EINVAL;
  2377. }
  2378. return 0;
  2379. }
  2380. static size_t kern_spec_filter_sz(const struct ib_uverbs_flow_spec_hdr *spec)
  2381. {
  2382. /* Returns user space filter size, includes padding */
  2383. return (spec->size - sizeof(struct ib_uverbs_flow_spec_hdr)) / 2;
  2384. }
  2385. static ssize_t spec_filter_size(const void *kern_spec_filter, u16 kern_filter_size,
  2386. u16 ib_real_filter_sz)
  2387. {
  2388. /*
  2389. * User space filter structures must be 64 bit aligned, otherwise this
  2390. * may pass, but we won't handle additional new attributes.
  2391. */
  2392. if (kern_filter_size > ib_real_filter_sz) {
  2393. if (memchr_inv(kern_spec_filter +
  2394. ib_real_filter_sz, 0,
  2395. kern_filter_size - ib_real_filter_sz))
  2396. return -EINVAL;
  2397. return ib_real_filter_sz;
  2398. }
  2399. return kern_filter_size;
  2400. }
  2401. int ib_uverbs_kern_spec_to_ib_spec_filter(enum ib_flow_spec_type type,
  2402. const void *kern_spec_mask,
  2403. const void *kern_spec_val,
  2404. size_t kern_filter_sz,
  2405. union ib_flow_spec *ib_spec)
  2406. {
  2407. ssize_t actual_filter_sz;
  2408. ssize_t ib_filter_sz;
  2409. /* User flow spec size must be aligned to 4 bytes */
  2410. if (kern_filter_sz != ALIGN(kern_filter_sz, 4))
  2411. return -EINVAL;
  2412. ib_spec->type = type;
  2413. if (ib_spec->type == (IB_FLOW_SPEC_INNER | IB_FLOW_SPEC_VXLAN_TUNNEL))
  2414. return -EINVAL;
  2415. switch (ib_spec->type & ~IB_FLOW_SPEC_INNER) {
  2416. case IB_FLOW_SPEC_ETH:
  2417. ib_filter_sz = offsetof(struct ib_flow_eth_filter, real_sz);
  2418. actual_filter_sz = spec_filter_size(kern_spec_mask,
  2419. kern_filter_sz,
  2420. ib_filter_sz);
  2421. if (actual_filter_sz <= 0)
  2422. return -EINVAL;
  2423. ib_spec->size = sizeof(struct ib_flow_spec_eth);
  2424. memcpy(&ib_spec->eth.val, kern_spec_val, actual_filter_sz);
  2425. memcpy(&ib_spec->eth.mask, kern_spec_mask, actual_filter_sz);
  2426. break;
  2427. case IB_FLOW_SPEC_IPV4:
  2428. ib_filter_sz = offsetof(struct ib_flow_ipv4_filter, real_sz);
  2429. actual_filter_sz = spec_filter_size(kern_spec_mask,
  2430. kern_filter_sz,
  2431. ib_filter_sz);
  2432. if (actual_filter_sz <= 0)
  2433. return -EINVAL;
  2434. ib_spec->size = sizeof(struct ib_flow_spec_ipv4);
  2435. memcpy(&ib_spec->ipv4.val, kern_spec_val, actual_filter_sz);
  2436. memcpy(&ib_spec->ipv4.mask, kern_spec_mask, actual_filter_sz);
  2437. break;
  2438. case IB_FLOW_SPEC_IPV6:
  2439. ib_filter_sz = offsetof(struct ib_flow_ipv6_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_ipv6);
  2446. memcpy(&ib_spec->ipv6.val, kern_spec_val, actual_filter_sz);
  2447. memcpy(&ib_spec->ipv6.mask, kern_spec_mask, actual_filter_sz);
  2448. if ((ntohl(ib_spec->ipv6.mask.flow_label)) >= BIT(20) ||
  2449. (ntohl(ib_spec->ipv6.val.flow_label)) >= BIT(20))
  2450. return -EINVAL;
  2451. break;
  2452. case IB_FLOW_SPEC_TCP:
  2453. case IB_FLOW_SPEC_UDP:
  2454. ib_filter_sz = offsetof(struct ib_flow_tcp_udp_filter, real_sz);
  2455. actual_filter_sz = spec_filter_size(kern_spec_mask,
  2456. kern_filter_sz,
  2457. ib_filter_sz);
  2458. if (actual_filter_sz <= 0)
  2459. return -EINVAL;
  2460. ib_spec->size = sizeof(struct ib_flow_spec_tcp_udp);
  2461. memcpy(&ib_spec->tcp_udp.val, kern_spec_val, actual_filter_sz);
  2462. memcpy(&ib_spec->tcp_udp.mask, kern_spec_mask, actual_filter_sz);
  2463. break;
  2464. case IB_FLOW_SPEC_VXLAN_TUNNEL:
  2465. ib_filter_sz = offsetof(struct ib_flow_tunnel_filter, real_sz);
  2466. actual_filter_sz = spec_filter_size(kern_spec_mask,
  2467. kern_filter_sz,
  2468. ib_filter_sz);
  2469. if (actual_filter_sz <= 0)
  2470. return -EINVAL;
  2471. ib_spec->tunnel.size = sizeof(struct ib_flow_spec_tunnel);
  2472. memcpy(&ib_spec->tunnel.val, kern_spec_val, actual_filter_sz);
  2473. memcpy(&ib_spec->tunnel.mask, kern_spec_mask, actual_filter_sz);
  2474. if ((ntohl(ib_spec->tunnel.mask.tunnel_id)) >= BIT(24) ||
  2475. (ntohl(ib_spec->tunnel.val.tunnel_id)) >= BIT(24))
  2476. return -EINVAL;
  2477. break;
  2478. case IB_FLOW_SPEC_ESP:
  2479. ib_filter_sz = offsetof(struct ib_flow_esp_filter, real_sz);
  2480. actual_filter_sz = spec_filter_size(kern_spec_mask,
  2481. kern_filter_sz,
  2482. ib_filter_sz);
  2483. if (actual_filter_sz <= 0)
  2484. return -EINVAL;
  2485. ib_spec->esp.size = sizeof(struct ib_flow_spec_esp);
  2486. memcpy(&ib_spec->esp.val, kern_spec_val, actual_filter_sz);
  2487. memcpy(&ib_spec->esp.mask, kern_spec_mask, actual_filter_sz);
  2488. break;
  2489. case IB_FLOW_SPEC_GRE:
  2490. ib_filter_sz = offsetof(struct ib_flow_gre_filter, real_sz);
  2491. actual_filter_sz = spec_filter_size(kern_spec_mask,
  2492. kern_filter_sz,
  2493. ib_filter_sz);
  2494. if (actual_filter_sz <= 0)
  2495. return -EINVAL;
  2496. ib_spec->gre.size = sizeof(struct ib_flow_spec_gre);
  2497. memcpy(&ib_spec->gre.val, kern_spec_val, actual_filter_sz);
  2498. memcpy(&ib_spec->gre.mask, kern_spec_mask, actual_filter_sz);
  2499. break;
  2500. case IB_FLOW_SPEC_MPLS:
  2501. ib_filter_sz = offsetof(struct ib_flow_mpls_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->mpls.size = sizeof(struct ib_flow_spec_mpls);
  2508. memcpy(&ib_spec->mpls.val, kern_spec_val, actual_filter_sz);
  2509. memcpy(&ib_spec->mpls.mask, kern_spec_mask, actual_filter_sz);
  2510. break;
  2511. default:
  2512. return -EINVAL;
  2513. }
  2514. return 0;
  2515. }
  2516. static int kern_spec_to_ib_spec_filter(struct ib_uverbs_flow_spec *kern_spec,
  2517. union ib_flow_spec *ib_spec)
  2518. {
  2519. ssize_t kern_filter_sz;
  2520. void *kern_spec_mask;
  2521. void *kern_spec_val;
  2522. kern_filter_sz = kern_spec_filter_sz(&kern_spec->hdr);
  2523. kern_spec_val = (void *)kern_spec +
  2524. sizeof(struct ib_uverbs_flow_spec_hdr);
  2525. kern_spec_mask = kern_spec_val + kern_filter_sz;
  2526. return ib_uverbs_kern_spec_to_ib_spec_filter(kern_spec->type,
  2527. kern_spec_mask,
  2528. kern_spec_val,
  2529. kern_filter_sz, ib_spec);
  2530. }
  2531. static int kern_spec_to_ib_spec(struct ib_uverbs_file *ufile,
  2532. struct ib_uverbs_flow_spec *kern_spec,
  2533. union ib_flow_spec *ib_spec,
  2534. struct ib_uflow_resources *uflow_res)
  2535. {
  2536. if (kern_spec->reserved)
  2537. return -EINVAL;
  2538. if (kern_spec->type >= IB_FLOW_SPEC_ACTION_TAG)
  2539. return kern_spec_to_ib_spec_action(ufile, kern_spec, ib_spec,
  2540. uflow_res);
  2541. else
  2542. return kern_spec_to_ib_spec_filter(kern_spec, ib_spec);
  2543. }
  2544. int ib_uverbs_ex_create_wq(struct ib_uverbs_file *file,
  2545. struct ib_udata *ucore,
  2546. struct ib_udata *uhw)
  2547. {
  2548. struct ib_uverbs_ex_create_wq cmd = {};
  2549. struct ib_uverbs_ex_create_wq_resp resp = {};
  2550. struct ib_uwq_object *obj;
  2551. int err = 0;
  2552. struct ib_cq *cq;
  2553. struct ib_pd *pd;
  2554. struct ib_wq *wq;
  2555. struct ib_wq_init_attr wq_init_attr = {};
  2556. size_t required_cmd_sz;
  2557. size_t required_resp_len;
  2558. struct ib_device *ib_dev;
  2559. required_cmd_sz = offsetof(typeof(cmd), max_sge) + sizeof(cmd.max_sge);
  2560. required_resp_len = offsetof(typeof(resp), wqn) + sizeof(resp.wqn);
  2561. if (ucore->inlen < required_cmd_sz)
  2562. return -EINVAL;
  2563. if (ucore->outlen < required_resp_len)
  2564. return -ENOSPC;
  2565. if (ucore->inlen > sizeof(cmd) &&
  2566. !ib_is_udata_cleared(ucore, sizeof(cmd),
  2567. ucore->inlen - sizeof(cmd)))
  2568. return -EOPNOTSUPP;
  2569. err = ib_copy_from_udata(&cmd, ucore, min(sizeof(cmd), ucore->inlen));
  2570. if (err)
  2571. return err;
  2572. if (cmd.comp_mask)
  2573. return -EOPNOTSUPP;
  2574. obj = (struct ib_uwq_object *)uobj_alloc(UVERBS_OBJECT_WQ, file,
  2575. &ib_dev);
  2576. if (IS_ERR(obj))
  2577. return PTR_ERR(obj);
  2578. pd = uobj_get_obj_read(pd, UVERBS_OBJECT_PD, cmd.pd_handle, file);
  2579. if (!pd) {
  2580. err = -EINVAL;
  2581. goto err_uobj;
  2582. }
  2583. cq = uobj_get_obj_read(cq, UVERBS_OBJECT_CQ, cmd.cq_handle, file);
  2584. if (!cq) {
  2585. err = -EINVAL;
  2586. goto err_put_pd;
  2587. }
  2588. wq_init_attr.cq = cq;
  2589. wq_init_attr.max_sge = cmd.max_sge;
  2590. wq_init_attr.max_wr = cmd.max_wr;
  2591. wq_init_attr.wq_context = file;
  2592. wq_init_attr.wq_type = cmd.wq_type;
  2593. wq_init_attr.event_handler = ib_uverbs_wq_event_handler;
  2594. if (ucore->inlen >= (offsetof(typeof(cmd), create_flags) +
  2595. sizeof(cmd.create_flags)))
  2596. wq_init_attr.create_flags = cmd.create_flags;
  2597. obj->uevent.events_reported = 0;
  2598. INIT_LIST_HEAD(&obj->uevent.event_list);
  2599. if (!pd->device->create_wq) {
  2600. err = -EOPNOTSUPP;
  2601. goto err_put_cq;
  2602. }
  2603. wq = pd->device->create_wq(pd, &wq_init_attr, uhw);
  2604. if (IS_ERR(wq)) {
  2605. err = PTR_ERR(wq);
  2606. goto err_put_cq;
  2607. }
  2608. wq->uobject = &obj->uevent.uobject;
  2609. obj->uevent.uobject.object = wq;
  2610. wq->wq_type = wq_init_attr.wq_type;
  2611. wq->cq = cq;
  2612. wq->pd = pd;
  2613. wq->device = pd->device;
  2614. wq->wq_context = wq_init_attr.wq_context;
  2615. atomic_set(&wq->usecnt, 0);
  2616. atomic_inc(&pd->usecnt);
  2617. atomic_inc(&cq->usecnt);
  2618. wq->uobject = &obj->uevent.uobject;
  2619. obj->uevent.uobject.object = wq;
  2620. memset(&resp, 0, sizeof(resp));
  2621. resp.wq_handle = obj->uevent.uobject.id;
  2622. resp.max_sge = wq_init_attr.max_sge;
  2623. resp.max_wr = wq_init_attr.max_wr;
  2624. resp.wqn = wq->wq_num;
  2625. resp.response_length = required_resp_len;
  2626. err = ib_copy_to_udata(ucore,
  2627. &resp, resp.response_length);
  2628. if (err)
  2629. goto err_copy;
  2630. uobj_put_obj_read(pd);
  2631. uobj_put_obj_read(cq);
  2632. return uobj_alloc_commit(&obj->uevent.uobject, 0);
  2633. err_copy:
  2634. ib_destroy_wq(wq);
  2635. err_put_cq:
  2636. uobj_put_obj_read(cq);
  2637. err_put_pd:
  2638. uobj_put_obj_read(pd);
  2639. err_uobj:
  2640. uobj_alloc_abort(&obj->uevent.uobject);
  2641. return err;
  2642. }
  2643. int ib_uverbs_ex_destroy_wq(struct ib_uverbs_file *file,
  2644. struct ib_udata *ucore,
  2645. struct ib_udata *uhw)
  2646. {
  2647. struct ib_uverbs_ex_destroy_wq cmd = {};
  2648. struct ib_uverbs_ex_destroy_wq_resp resp = {};
  2649. struct ib_uobject *uobj;
  2650. struct ib_uwq_object *obj;
  2651. size_t required_cmd_sz;
  2652. size_t required_resp_len;
  2653. int ret;
  2654. required_cmd_sz = offsetof(typeof(cmd), wq_handle) + sizeof(cmd.wq_handle);
  2655. required_resp_len = offsetof(typeof(resp), reserved) + sizeof(resp.reserved);
  2656. if (ucore->inlen < required_cmd_sz)
  2657. return -EINVAL;
  2658. if (ucore->outlen < required_resp_len)
  2659. return -ENOSPC;
  2660. if (ucore->inlen > sizeof(cmd) &&
  2661. !ib_is_udata_cleared(ucore, sizeof(cmd),
  2662. ucore->inlen - sizeof(cmd)))
  2663. return -EOPNOTSUPP;
  2664. ret = ib_copy_from_udata(&cmd, ucore, min(sizeof(cmd), ucore->inlen));
  2665. if (ret)
  2666. return ret;
  2667. if (cmd.comp_mask)
  2668. return -EOPNOTSUPP;
  2669. resp.response_length = required_resp_len;
  2670. uobj = uobj_get_destroy(UVERBS_OBJECT_WQ, cmd.wq_handle, file);
  2671. if (IS_ERR(uobj))
  2672. return PTR_ERR(uobj);
  2673. obj = container_of(uobj, struct ib_uwq_object, uevent.uobject);
  2674. resp.events_reported = obj->uevent.events_reported;
  2675. uobj_put_destroy(uobj);
  2676. return ib_copy_to_udata(ucore, &resp, resp.response_length);
  2677. }
  2678. int ib_uverbs_ex_modify_wq(struct ib_uverbs_file *file,
  2679. struct ib_udata *ucore,
  2680. struct ib_udata *uhw)
  2681. {
  2682. struct ib_uverbs_ex_modify_wq cmd = {};
  2683. struct ib_wq *wq;
  2684. struct ib_wq_attr wq_attr = {};
  2685. size_t required_cmd_sz;
  2686. int ret;
  2687. required_cmd_sz = offsetof(typeof(cmd), curr_wq_state) + sizeof(cmd.curr_wq_state);
  2688. if (ucore->inlen < required_cmd_sz)
  2689. return -EINVAL;
  2690. if (ucore->inlen > sizeof(cmd) &&
  2691. !ib_is_udata_cleared(ucore, sizeof(cmd),
  2692. ucore->inlen - sizeof(cmd)))
  2693. return -EOPNOTSUPP;
  2694. ret = ib_copy_from_udata(&cmd, ucore, min(sizeof(cmd), ucore->inlen));
  2695. if (ret)
  2696. return ret;
  2697. if (!cmd.attr_mask)
  2698. return -EINVAL;
  2699. if (cmd.attr_mask > (IB_WQ_STATE | IB_WQ_CUR_STATE | IB_WQ_FLAGS))
  2700. return -EINVAL;
  2701. wq = uobj_get_obj_read(wq, UVERBS_OBJECT_WQ, cmd.wq_handle, file);
  2702. if (!wq)
  2703. return -EINVAL;
  2704. wq_attr.curr_wq_state = cmd.curr_wq_state;
  2705. wq_attr.wq_state = cmd.wq_state;
  2706. if (cmd.attr_mask & IB_WQ_FLAGS) {
  2707. wq_attr.flags = cmd.flags;
  2708. wq_attr.flags_mask = cmd.flags_mask;
  2709. }
  2710. if (!wq->device->modify_wq) {
  2711. ret = -EOPNOTSUPP;
  2712. goto out;
  2713. }
  2714. ret = wq->device->modify_wq(wq, &wq_attr, cmd.attr_mask, uhw);
  2715. out:
  2716. uobj_put_obj_read(wq);
  2717. return ret;
  2718. }
  2719. int ib_uverbs_ex_create_rwq_ind_table(struct ib_uverbs_file *file,
  2720. struct ib_udata *ucore,
  2721. struct ib_udata *uhw)
  2722. {
  2723. struct ib_uverbs_ex_create_rwq_ind_table cmd = {};
  2724. struct ib_uverbs_ex_create_rwq_ind_table_resp resp = {};
  2725. struct ib_uobject *uobj;
  2726. int err = 0;
  2727. struct ib_rwq_ind_table_init_attr init_attr = {};
  2728. struct ib_rwq_ind_table *rwq_ind_tbl;
  2729. struct ib_wq **wqs = NULL;
  2730. u32 *wqs_handles = NULL;
  2731. struct ib_wq *wq = NULL;
  2732. int i, j, num_read_wqs;
  2733. u32 num_wq_handles;
  2734. u32 expected_in_size;
  2735. size_t required_cmd_sz_header;
  2736. size_t required_resp_len;
  2737. struct ib_device *ib_dev;
  2738. required_cmd_sz_header = offsetof(typeof(cmd), log_ind_tbl_size) + sizeof(cmd.log_ind_tbl_size);
  2739. required_resp_len = offsetof(typeof(resp), ind_tbl_num) + sizeof(resp.ind_tbl_num);
  2740. if (ucore->inlen < required_cmd_sz_header)
  2741. return -EINVAL;
  2742. if (ucore->outlen < required_resp_len)
  2743. return -ENOSPC;
  2744. err = ib_copy_from_udata(&cmd, ucore, required_cmd_sz_header);
  2745. if (err)
  2746. return err;
  2747. ucore->inbuf += required_cmd_sz_header;
  2748. ucore->inlen -= required_cmd_sz_header;
  2749. if (cmd.comp_mask)
  2750. return -EOPNOTSUPP;
  2751. if (cmd.log_ind_tbl_size > IB_USER_VERBS_MAX_LOG_IND_TBL_SIZE)
  2752. return -EINVAL;
  2753. num_wq_handles = 1 << cmd.log_ind_tbl_size;
  2754. expected_in_size = num_wq_handles * sizeof(__u32);
  2755. if (num_wq_handles == 1)
  2756. /* input size for wq handles is u64 aligned */
  2757. expected_in_size += sizeof(__u32);
  2758. if (ucore->inlen < expected_in_size)
  2759. return -EINVAL;
  2760. if (ucore->inlen > expected_in_size &&
  2761. !ib_is_udata_cleared(ucore, expected_in_size,
  2762. ucore->inlen - expected_in_size))
  2763. return -EOPNOTSUPP;
  2764. wqs_handles = kcalloc(num_wq_handles, sizeof(*wqs_handles),
  2765. GFP_KERNEL);
  2766. if (!wqs_handles)
  2767. return -ENOMEM;
  2768. err = ib_copy_from_udata(wqs_handles, ucore,
  2769. num_wq_handles * sizeof(__u32));
  2770. if (err)
  2771. goto err_free;
  2772. wqs = kcalloc(num_wq_handles, sizeof(*wqs), GFP_KERNEL);
  2773. if (!wqs) {
  2774. err = -ENOMEM;
  2775. goto err_free;
  2776. }
  2777. for (num_read_wqs = 0; num_read_wqs < num_wq_handles;
  2778. num_read_wqs++) {
  2779. wq = uobj_get_obj_read(wq, UVERBS_OBJECT_WQ,
  2780. wqs_handles[num_read_wqs], file);
  2781. if (!wq) {
  2782. err = -EINVAL;
  2783. goto put_wqs;
  2784. }
  2785. wqs[num_read_wqs] = wq;
  2786. }
  2787. uobj = uobj_alloc(UVERBS_OBJECT_RWQ_IND_TBL, file, &ib_dev);
  2788. if (IS_ERR(uobj)) {
  2789. err = PTR_ERR(uobj);
  2790. goto put_wqs;
  2791. }
  2792. init_attr.log_ind_tbl_size = cmd.log_ind_tbl_size;
  2793. init_attr.ind_tbl = wqs;
  2794. if (!ib_dev->create_rwq_ind_table) {
  2795. err = -EOPNOTSUPP;
  2796. goto err_uobj;
  2797. }
  2798. rwq_ind_tbl = ib_dev->create_rwq_ind_table(ib_dev, &init_attr, uhw);
  2799. if (IS_ERR(rwq_ind_tbl)) {
  2800. err = PTR_ERR(rwq_ind_tbl);
  2801. goto err_uobj;
  2802. }
  2803. rwq_ind_tbl->ind_tbl = wqs;
  2804. rwq_ind_tbl->log_ind_tbl_size = init_attr.log_ind_tbl_size;
  2805. rwq_ind_tbl->uobject = uobj;
  2806. uobj->object = rwq_ind_tbl;
  2807. rwq_ind_tbl->device = ib_dev;
  2808. atomic_set(&rwq_ind_tbl->usecnt, 0);
  2809. for (i = 0; i < num_wq_handles; i++)
  2810. atomic_inc(&wqs[i]->usecnt);
  2811. resp.ind_tbl_handle = uobj->id;
  2812. resp.ind_tbl_num = rwq_ind_tbl->ind_tbl_num;
  2813. resp.response_length = required_resp_len;
  2814. err = ib_copy_to_udata(ucore,
  2815. &resp, resp.response_length);
  2816. if (err)
  2817. goto err_copy;
  2818. kfree(wqs_handles);
  2819. for (j = 0; j < num_read_wqs; j++)
  2820. uobj_put_obj_read(wqs[j]);
  2821. return uobj_alloc_commit(uobj, 0);
  2822. err_copy:
  2823. ib_destroy_rwq_ind_table(rwq_ind_tbl);
  2824. err_uobj:
  2825. uobj_alloc_abort(uobj);
  2826. put_wqs:
  2827. for (j = 0; j < num_read_wqs; j++)
  2828. uobj_put_obj_read(wqs[j]);
  2829. err_free:
  2830. kfree(wqs_handles);
  2831. kfree(wqs);
  2832. return err;
  2833. }
  2834. int ib_uverbs_ex_destroy_rwq_ind_table(struct ib_uverbs_file *file,
  2835. struct ib_udata *ucore,
  2836. struct ib_udata *uhw)
  2837. {
  2838. struct ib_uverbs_ex_destroy_rwq_ind_table cmd = {};
  2839. int ret;
  2840. size_t required_cmd_sz;
  2841. required_cmd_sz = offsetof(typeof(cmd), ind_tbl_handle) + sizeof(cmd.ind_tbl_handle);
  2842. if (ucore->inlen < required_cmd_sz)
  2843. return -EINVAL;
  2844. if (ucore->inlen > sizeof(cmd) &&
  2845. !ib_is_udata_cleared(ucore, sizeof(cmd),
  2846. ucore->inlen - sizeof(cmd)))
  2847. return -EOPNOTSUPP;
  2848. ret = ib_copy_from_udata(&cmd, ucore, min(sizeof(cmd), ucore->inlen));
  2849. if (ret)
  2850. return ret;
  2851. if (cmd.comp_mask)
  2852. return -EOPNOTSUPP;
  2853. return uobj_perform_destroy(UVERBS_OBJECT_RWQ_IND_TBL,
  2854. cmd.ind_tbl_handle, file, 0);
  2855. }
  2856. int ib_uverbs_ex_create_flow(struct ib_uverbs_file *file,
  2857. struct ib_udata *ucore,
  2858. struct ib_udata *uhw)
  2859. {
  2860. struct ib_uverbs_create_flow cmd;
  2861. struct ib_uverbs_create_flow_resp resp;
  2862. struct ib_uobject *uobj;
  2863. struct ib_flow *flow_id;
  2864. struct ib_uverbs_flow_attr *kern_flow_attr;
  2865. struct ib_flow_attr *flow_attr;
  2866. struct ib_qp *qp;
  2867. struct ib_uflow_resources *uflow_res;
  2868. struct ib_uverbs_flow_spec_hdr *kern_spec;
  2869. int err = 0;
  2870. void *ib_spec;
  2871. int i;
  2872. struct ib_device *ib_dev;
  2873. if (ucore->inlen < sizeof(cmd))
  2874. return -EINVAL;
  2875. if (ucore->outlen < sizeof(resp))
  2876. return -ENOSPC;
  2877. err = ib_copy_from_udata(&cmd, ucore, sizeof(cmd));
  2878. if (err)
  2879. return err;
  2880. ucore->inbuf += sizeof(cmd);
  2881. ucore->inlen -= sizeof(cmd);
  2882. if (cmd.comp_mask)
  2883. return -EINVAL;
  2884. if (!capable(CAP_NET_RAW))
  2885. return -EPERM;
  2886. if (cmd.flow_attr.flags >= IB_FLOW_ATTR_FLAGS_RESERVED)
  2887. return -EINVAL;
  2888. if ((cmd.flow_attr.flags & IB_FLOW_ATTR_FLAGS_DONT_TRAP) &&
  2889. ((cmd.flow_attr.type == IB_FLOW_ATTR_ALL_DEFAULT) ||
  2890. (cmd.flow_attr.type == IB_FLOW_ATTR_MC_DEFAULT)))
  2891. return -EINVAL;
  2892. if (cmd.flow_attr.num_of_specs > IB_FLOW_SPEC_SUPPORT_LAYERS)
  2893. return -EINVAL;
  2894. if (cmd.flow_attr.size > ucore->inlen ||
  2895. cmd.flow_attr.size >
  2896. (cmd.flow_attr.num_of_specs * sizeof(struct ib_uverbs_flow_spec)))
  2897. return -EINVAL;
  2898. if (cmd.flow_attr.reserved[0] ||
  2899. cmd.flow_attr.reserved[1])
  2900. return -EINVAL;
  2901. if (cmd.flow_attr.num_of_specs) {
  2902. kern_flow_attr = kmalloc(sizeof(*kern_flow_attr) + cmd.flow_attr.size,
  2903. GFP_KERNEL);
  2904. if (!kern_flow_attr)
  2905. return -ENOMEM;
  2906. *kern_flow_attr = cmd.flow_attr;
  2907. err = ib_copy_from_udata(&kern_flow_attr->flow_specs, ucore,
  2908. cmd.flow_attr.size);
  2909. if (err)
  2910. goto err_free_attr;
  2911. } else {
  2912. kern_flow_attr = &cmd.flow_attr;
  2913. }
  2914. uobj = uobj_alloc(UVERBS_OBJECT_FLOW, file, &ib_dev);
  2915. if (IS_ERR(uobj)) {
  2916. err = PTR_ERR(uobj);
  2917. goto err_free_attr;
  2918. }
  2919. qp = uobj_get_obj_read(qp, UVERBS_OBJECT_QP, cmd.qp_handle, file);
  2920. if (!qp) {
  2921. err = -EINVAL;
  2922. goto err_uobj;
  2923. }
  2924. if (qp->qp_type != IB_QPT_UD && qp->qp_type != IB_QPT_RAW_PACKET) {
  2925. err = -EINVAL;
  2926. goto err_put;
  2927. }
  2928. if (!qp->device->create_flow) {
  2929. err = -EOPNOTSUPP;
  2930. goto err_put;
  2931. }
  2932. flow_attr = kzalloc(struct_size(flow_attr, flows,
  2933. cmd.flow_attr.num_of_specs), GFP_KERNEL);
  2934. if (!flow_attr) {
  2935. err = -ENOMEM;
  2936. goto err_put;
  2937. }
  2938. uflow_res = flow_resources_alloc(cmd.flow_attr.num_of_specs);
  2939. if (!uflow_res) {
  2940. err = -ENOMEM;
  2941. goto err_free_flow_attr;
  2942. }
  2943. flow_attr->type = kern_flow_attr->type;
  2944. flow_attr->priority = kern_flow_attr->priority;
  2945. flow_attr->num_of_specs = kern_flow_attr->num_of_specs;
  2946. flow_attr->port = kern_flow_attr->port;
  2947. flow_attr->flags = kern_flow_attr->flags;
  2948. flow_attr->size = sizeof(*flow_attr);
  2949. kern_spec = kern_flow_attr->flow_specs;
  2950. ib_spec = flow_attr + 1;
  2951. for (i = 0; i < flow_attr->num_of_specs &&
  2952. cmd.flow_attr.size >= sizeof(*kern_spec) &&
  2953. cmd.flow_attr.size >= kern_spec->size;
  2954. i++) {
  2955. err = kern_spec_to_ib_spec(
  2956. file, (struct ib_uverbs_flow_spec *)kern_spec,
  2957. ib_spec, uflow_res);
  2958. if (err)
  2959. goto err_free;
  2960. flow_attr->size +=
  2961. ((union ib_flow_spec *) ib_spec)->size;
  2962. cmd.flow_attr.size -= kern_spec->size;
  2963. kern_spec = ((void *)kern_spec) + kern_spec->size;
  2964. ib_spec += ((union ib_flow_spec *) ib_spec)->size;
  2965. }
  2966. if (cmd.flow_attr.size || (i != flow_attr->num_of_specs)) {
  2967. pr_warn("create flow failed, flow %d: %d bytes left from uverb cmd\n",
  2968. i, cmd.flow_attr.size);
  2969. err = -EINVAL;
  2970. goto err_free;
  2971. }
  2972. flow_id = qp->device->create_flow(qp, flow_attr,
  2973. IB_FLOW_DOMAIN_USER, uhw);
  2974. if (IS_ERR(flow_id)) {
  2975. err = PTR_ERR(flow_id);
  2976. goto err_free;
  2977. }
  2978. ib_set_flow(uobj, flow_id, qp, qp->device, uflow_res);
  2979. memset(&resp, 0, sizeof(resp));
  2980. resp.flow_handle = uobj->id;
  2981. err = ib_copy_to_udata(ucore,
  2982. &resp, sizeof(resp));
  2983. if (err)
  2984. goto err_copy;
  2985. uobj_put_obj_read(qp);
  2986. kfree(flow_attr);
  2987. if (cmd.flow_attr.num_of_specs)
  2988. kfree(kern_flow_attr);
  2989. return uobj_alloc_commit(uobj, 0);
  2990. err_copy:
  2991. if (!qp->device->destroy_flow(flow_id))
  2992. atomic_dec(&qp->usecnt);
  2993. err_free:
  2994. ib_uverbs_flow_resources_free(uflow_res);
  2995. err_free_flow_attr:
  2996. kfree(flow_attr);
  2997. err_put:
  2998. uobj_put_obj_read(qp);
  2999. err_uobj:
  3000. uobj_alloc_abort(uobj);
  3001. err_free_attr:
  3002. if (cmd.flow_attr.num_of_specs)
  3003. kfree(kern_flow_attr);
  3004. return err;
  3005. }
  3006. int ib_uverbs_ex_destroy_flow(struct ib_uverbs_file *file,
  3007. struct ib_udata *ucore,
  3008. struct ib_udata *uhw)
  3009. {
  3010. struct ib_uverbs_destroy_flow cmd;
  3011. int ret;
  3012. if (ucore->inlen < sizeof(cmd))
  3013. return -EINVAL;
  3014. ret = ib_copy_from_udata(&cmd, ucore, sizeof(cmd));
  3015. if (ret)
  3016. return ret;
  3017. if (cmd.comp_mask)
  3018. return -EINVAL;
  3019. return uobj_perform_destroy(UVERBS_OBJECT_FLOW, cmd.flow_handle, file,
  3020. 0);
  3021. }
  3022. static int __uverbs_create_xsrq(struct ib_uverbs_file *file,
  3023. struct ib_uverbs_create_xsrq *cmd,
  3024. struct ib_udata *udata)
  3025. {
  3026. struct ib_uverbs_create_srq_resp resp;
  3027. struct ib_usrq_object *obj;
  3028. struct ib_pd *pd;
  3029. struct ib_srq *srq;
  3030. struct ib_uobject *uninitialized_var(xrcd_uobj);
  3031. struct ib_srq_init_attr attr;
  3032. int ret;
  3033. struct ib_device *ib_dev;
  3034. obj = (struct ib_usrq_object *)uobj_alloc(UVERBS_OBJECT_SRQ, file,
  3035. &ib_dev);
  3036. if (IS_ERR(obj))
  3037. return PTR_ERR(obj);
  3038. if (cmd->srq_type == IB_SRQT_TM)
  3039. attr.ext.tag_matching.max_num_tags = cmd->max_num_tags;
  3040. if (cmd->srq_type == IB_SRQT_XRC) {
  3041. xrcd_uobj = uobj_get_read(UVERBS_OBJECT_XRCD, cmd->xrcd_handle,
  3042. file);
  3043. if (IS_ERR(xrcd_uobj)) {
  3044. ret = -EINVAL;
  3045. goto err;
  3046. }
  3047. attr.ext.xrc.xrcd = (struct ib_xrcd *)xrcd_uobj->object;
  3048. if (!attr.ext.xrc.xrcd) {
  3049. ret = -EINVAL;
  3050. goto err_put_xrcd;
  3051. }
  3052. obj->uxrcd = container_of(xrcd_uobj, struct ib_uxrcd_object, uobject);
  3053. atomic_inc(&obj->uxrcd->refcnt);
  3054. }
  3055. if (ib_srq_has_cq(cmd->srq_type)) {
  3056. attr.ext.cq = uobj_get_obj_read(cq, UVERBS_OBJECT_CQ,
  3057. cmd->cq_handle, file);
  3058. if (!attr.ext.cq) {
  3059. ret = -EINVAL;
  3060. goto err_put_xrcd;
  3061. }
  3062. }
  3063. pd = uobj_get_obj_read(pd, UVERBS_OBJECT_PD, cmd->pd_handle, file);
  3064. if (!pd) {
  3065. ret = -EINVAL;
  3066. goto err_put_cq;
  3067. }
  3068. attr.event_handler = ib_uverbs_srq_event_handler;
  3069. attr.srq_context = file;
  3070. attr.srq_type = cmd->srq_type;
  3071. attr.attr.max_wr = cmd->max_wr;
  3072. attr.attr.max_sge = cmd->max_sge;
  3073. attr.attr.srq_limit = cmd->srq_limit;
  3074. obj->uevent.events_reported = 0;
  3075. INIT_LIST_HEAD(&obj->uevent.event_list);
  3076. srq = pd->device->create_srq(pd, &attr, udata);
  3077. if (IS_ERR(srq)) {
  3078. ret = PTR_ERR(srq);
  3079. goto err_put;
  3080. }
  3081. srq->device = pd->device;
  3082. srq->pd = pd;
  3083. srq->srq_type = cmd->srq_type;
  3084. srq->uobject = &obj->uevent.uobject;
  3085. srq->event_handler = attr.event_handler;
  3086. srq->srq_context = attr.srq_context;
  3087. if (ib_srq_has_cq(cmd->srq_type)) {
  3088. srq->ext.cq = attr.ext.cq;
  3089. atomic_inc(&attr.ext.cq->usecnt);
  3090. }
  3091. if (cmd->srq_type == IB_SRQT_XRC) {
  3092. srq->ext.xrc.xrcd = attr.ext.xrc.xrcd;
  3093. atomic_inc(&attr.ext.xrc.xrcd->usecnt);
  3094. }
  3095. atomic_inc(&pd->usecnt);
  3096. atomic_set(&srq->usecnt, 0);
  3097. obj->uevent.uobject.object = srq;
  3098. obj->uevent.uobject.user_handle = cmd->user_handle;
  3099. memset(&resp, 0, sizeof resp);
  3100. resp.srq_handle = obj->uevent.uobject.id;
  3101. resp.max_wr = attr.attr.max_wr;
  3102. resp.max_sge = attr.attr.max_sge;
  3103. if (cmd->srq_type == IB_SRQT_XRC)
  3104. resp.srqn = srq->ext.xrc.srq_num;
  3105. if (copy_to_user(u64_to_user_ptr(cmd->response),
  3106. &resp, sizeof resp)) {
  3107. ret = -EFAULT;
  3108. goto err_copy;
  3109. }
  3110. if (cmd->srq_type == IB_SRQT_XRC)
  3111. uobj_put_read(xrcd_uobj);
  3112. if (ib_srq_has_cq(cmd->srq_type))
  3113. uobj_put_obj_read(attr.ext.cq);
  3114. uobj_put_obj_read(pd);
  3115. return uobj_alloc_commit(&obj->uevent.uobject, 0);
  3116. err_copy:
  3117. ib_destroy_srq(srq);
  3118. err_put:
  3119. uobj_put_obj_read(pd);
  3120. err_put_cq:
  3121. if (ib_srq_has_cq(cmd->srq_type))
  3122. uobj_put_obj_read(attr.ext.cq);
  3123. err_put_xrcd:
  3124. if (cmd->srq_type == IB_SRQT_XRC) {
  3125. atomic_dec(&obj->uxrcd->refcnt);
  3126. uobj_put_read(xrcd_uobj);
  3127. }
  3128. err:
  3129. uobj_alloc_abort(&obj->uevent.uobject);
  3130. return ret;
  3131. }
  3132. ssize_t ib_uverbs_create_srq(struct ib_uverbs_file *file,
  3133. const char __user *buf, int in_len,
  3134. int out_len)
  3135. {
  3136. struct ib_uverbs_create_srq cmd;
  3137. struct ib_uverbs_create_xsrq xcmd;
  3138. struct ib_uverbs_create_srq_resp resp;
  3139. struct ib_udata udata;
  3140. int ret;
  3141. if (out_len < sizeof resp)
  3142. return -ENOSPC;
  3143. if (copy_from_user(&cmd, buf, sizeof cmd))
  3144. return -EFAULT;
  3145. memset(&xcmd, 0, sizeof(xcmd));
  3146. xcmd.response = cmd.response;
  3147. xcmd.user_handle = cmd.user_handle;
  3148. xcmd.srq_type = IB_SRQT_BASIC;
  3149. xcmd.pd_handle = cmd.pd_handle;
  3150. xcmd.max_wr = cmd.max_wr;
  3151. xcmd.max_sge = cmd.max_sge;
  3152. xcmd.srq_limit = cmd.srq_limit;
  3153. ib_uverbs_init_udata(&udata, buf + sizeof(cmd),
  3154. u64_to_user_ptr(cmd.response) + sizeof(resp),
  3155. in_len - sizeof(cmd) - sizeof(struct ib_uverbs_cmd_hdr),
  3156. out_len - sizeof(resp));
  3157. ret = __uverbs_create_xsrq(file, &xcmd, &udata);
  3158. if (ret)
  3159. return ret;
  3160. return in_len;
  3161. }
  3162. ssize_t ib_uverbs_create_xsrq(struct ib_uverbs_file *file,
  3163. const char __user *buf, int in_len, int out_len)
  3164. {
  3165. struct ib_uverbs_create_xsrq cmd;
  3166. struct ib_uverbs_create_srq_resp resp;
  3167. struct ib_udata udata;
  3168. int ret;
  3169. if (out_len < sizeof resp)
  3170. return -ENOSPC;
  3171. if (copy_from_user(&cmd, buf, sizeof cmd))
  3172. return -EFAULT;
  3173. ib_uverbs_init_udata(&udata, buf + sizeof(cmd),
  3174. u64_to_user_ptr(cmd.response) + sizeof(resp),
  3175. in_len - sizeof(cmd) - sizeof(struct ib_uverbs_cmd_hdr),
  3176. out_len - sizeof(resp));
  3177. ret = __uverbs_create_xsrq(file, &cmd, &udata);
  3178. if (ret)
  3179. return ret;
  3180. return in_len;
  3181. }
  3182. ssize_t ib_uverbs_modify_srq(struct ib_uverbs_file *file,
  3183. const char __user *buf, int in_len,
  3184. int out_len)
  3185. {
  3186. struct ib_uverbs_modify_srq cmd;
  3187. struct ib_udata udata;
  3188. struct ib_srq *srq;
  3189. struct ib_srq_attr attr;
  3190. int ret;
  3191. if (copy_from_user(&cmd, buf, sizeof cmd))
  3192. return -EFAULT;
  3193. ib_uverbs_init_udata(&udata, buf + sizeof cmd, NULL, in_len - sizeof cmd,
  3194. out_len);
  3195. srq = uobj_get_obj_read(srq, UVERBS_OBJECT_SRQ, cmd.srq_handle, file);
  3196. if (!srq)
  3197. return -EINVAL;
  3198. attr.max_wr = cmd.max_wr;
  3199. attr.srq_limit = cmd.srq_limit;
  3200. ret = srq->device->modify_srq(srq, &attr, cmd.attr_mask, &udata);
  3201. uobj_put_obj_read(srq);
  3202. return ret ? ret : in_len;
  3203. }
  3204. ssize_t ib_uverbs_query_srq(struct ib_uverbs_file *file,
  3205. const char __user *buf,
  3206. int in_len, int out_len)
  3207. {
  3208. struct ib_uverbs_query_srq cmd;
  3209. struct ib_uverbs_query_srq_resp resp;
  3210. struct ib_srq_attr attr;
  3211. struct ib_srq *srq;
  3212. int ret;
  3213. if (out_len < sizeof resp)
  3214. return -ENOSPC;
  3215. if (copy_from_user(&cmd, buf, sizeof cmd))
  3216. return -EFAULT;
  3217. srq = uobj_get_obj_read(srq, UVERBS_OBJECT_SRQ, cmd.srq_handle, file);
  3218. if (!srq)
  3219. return -EINVAL;
  3220. ret = ib_query_srq(srq, &attr);
  3221. uobj_put_obj_read(srq);
  3222. if (ret)
  3223. return ret;
  3224. memset(&resp, 0, sizeof resp);
  3225. resp.max_wr = attr.max_wr;
  3226. resp.max_sge = attr.max_sge;
  3227. resp.srq_limit = attr.srq_limit;
  3228. if (copy_to_user(u64_to_user_ptr(cmd.response), &resp, sizeof resp))
  3229. return -EFAULT;
  3230. return in_len;
  3231. }
  3232. ssize_t ib_uverbs_destroy_srq(struct ib_uverbs_file *file,
  3233. const char __user *buf, int in_len,
  3234. int out_len)
  3235. {
  3236. struct ib_uverbs_destroy_srq cmd;
  3237. struct ib_uverbs_destroy_srq_resp resp;
  3238. struct ib_uobject *uobj;
  3239. struct ib_uevent_object *obj;
  3240. if (copy_from_user(&cmd, buf, sizeof cmd))
  3241. return -EFAULT;
  3242. uobj = uobj_get_destroy(UVERBS_OBJECT_SRQ, cmd.srq_handle, file);
  3243. if (IS_ERR(uobj))
  3244. return PTR_ERR(uobj);
  3245. obj = container_of(uobj, struct ib_uevent_object, uobject);
  3246. memset(&resp, 0, sizeof(resp));
  3247. resp.events_reported = obj->events_reported;
  3248. uobj_put_destroy(uobj);
  3249. if (copy_to_user(u64_to_user_ptr(cmd.response), &resp, sizeof(resp)))
  3250. return -EFAULT;
  3251. return in_len;
  3252. }
  3253. int ib_uverbs_ex_query_device(struct ib_uverbs_file *file,
  3254. struct ib_udata *ucore,
  3255. struct ib_udata *uhw)
  3256. {
  3257. struct ib_uverbs_ex_query_device_resp resp = { {0} };
  3258. struct ib_uverbs_ex_query_device cmd;
  3259. struct ib_device_attr attr = {0};
  3260. struct ib_ucontext *ucontext;
  3261. struct ib_device *ib_dev;
  3262. int err;
  3263. ucontext = ib_uverbs_get_ucontext(file);
  3264. if (IS_ERR(ucontext))
  3265. return PTR_ERR(ucontext);
  3266. ib_dev = ucontext->device;
  3267. if (!ib_dev->query_device)
  3268. return -EOPNOTSUPP;
  3269. if (ucore->inlen < sizeof(cmd))
  3270. return -EINVAL;
  3271. err = ib_copy_from_udata(&cmd, ucore, sizeof(cmd));
  3272. if (err)
  3273. return err;
  3274. if (cmd.comp_mask)
  3275. return -EINVAL;
  3276. if (cmd.reserved)
  3277. return -EINVAL;
  3278. resp.response_length = offsetof(typeof(resp), odp_caps);
  3279. if (ucore->outlen < resp.response_length)
  3280. return -ENOSPC;
  3281. err = ib_dev->query_device(ib_dev, &attr, uhw);
  3282. if (err)
  3283. return err;
  3284. copy_query_dev_fields(ucontext, &resp.base, &attr);
  3285. if (ucore->outlen < resp.response_length + sizeof(resp.odp_caps))
  3286. goto end;
  3287. #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
  3288. resp.odp_caps.general_caps = attr.odp_caps.general_caps;
  3289. resp.odp_caps.per_transport_caps.rc_odp_caps =
  3290. attr.odp_caps.per_transport_caps.rc_odp_caps;
  3291. resp.odp_caps.per_transport_caps.uc_odp_caps =
  3292. attr.odp_caps.per_transport_caps.uc_odp_caps;
  3293. resp.odp_caps.per_transport_caps.ud_odp_caps =
  3294. attr.odp_caps.per_transport_caps.ud_odp_caps;
  3295. #endif
  3296. resp.response_length += sizeof(resp.odp_caps);
  3297. if (ucore->outlen < resp.response_length + sizeof(resp.timestamp_mask))
  3298. goto end;
  3299. resp.timestamp_mask = attr.timestamp_mask;
  3300. resp.response_length += sizeof(resp.timestamp_mask);
  3301. if (ucore->outlen < resp.response_length + sizeof(resp.hca_core_clock))
  3302. goto end;
  3303. resp.hca_core_clock = attr.hca_core_clock;
  3304. resp.response_length += sizeof(resp.hca_core_clock);
  3305. if (ucore->outlen < resp.response_length + sizeof(resp.device_cap_flags_ex))
  3306. goto end;
  3307. resp.device_cap_flags_ex = attr.device_cap_flags;
  3308. resp.response_length += sizeof(resp.device_cap_flags_ex);
  3309. if (ucore->outlen < resp.response_length + sizeof(resp.rss_caps))
  3310. goto end;
  3311. resp.rss_caps.supported_qpts = attr.rss_caps.supported_qpts;
  3312. resp.rss_caps.max_rwq_indirection_tables =
  3313. attr.rss_caps.max_rwq_indirection_tables;
  3314. resp.rss_caps.max_rwq_indirection_table_size =
  3315. attr.rss_caps.max_rwq_indirection_table_size;
  3316. resp.response_length += sizeof(resp.rss_caps);
  3317. if (ucore->outlen < resp.response_length + sizeof(resp.max_wq_type_rq))
  3318. goto end;
  3319. resp.max_wq_type_rq = attr.max_wq_type_rq;
  3320. resp.response_length += sizeof(resp.max_wq_type_rq);
  3321. if (ucore->outlen < resp.response_length + sizeof(resp.raw_packet_caps))
  3322. goto end;
  3323. resp.raw_packet_caps = attr.raw_packet_caps;
  3324. resp.response_length += sizeof(resp.raw_packet_caps);
  3325. if (ucore->outlen < resp.response_length + sizeof(resp.tm_caps))
  3326. goto end;
  3327. resp.tm_caps.max_rndv_hdr_size = attr.tm_caps.max_rndv_hdr_size;
  3328. resp.tm_caps.max_num_tags = attr.tm_caps.max_num_tags;
  3329. resp.tm_caps.max_ops = attr.tm_caps.max_ops;
  3330. resp.tm_caps.max_sge = attr.tm_caps.max_sge;
  3331. resp.tm_caps.flags = attr.tm_caps.flags;
  3332. resp.response_length += sizeof(resp.tm_caps);
  3333. if (ucore->outlen < resp.response_length + sizeof(resp.cq_moderation_caps))
  3334. goto end;
  3335. resp.cq_moderation_caps.max_cq_moderation_count =
  3336. attr.cq_caps.max_cq_moderation_count;
  3337. resp.cq_moderation_caps.max_cq_moderation_period =
  3338. attr.cq_caps.max_cq_moderation_period;
  3339. resp.response_length += sizeof(resp.cq_moderation_caps);
  3340. if (ucore->outlen < resp.response_length + sizeof(resp.max_dm_size))
  3341. goto end;
  3342. resp.max_dm_size = attr.max_dm_size;
  3343. resp.response_length += sizeof(resp.max_dm_size);
  3344. end:
  3345. err = ib_copy_to_udata(ucore, &resp, resp.response_length);
  3346. return err;
  3347. }
  3348. int ib_uverbs_ex_modify_cq(struct ib_uverbs_file *file,
  3349. struct ib_udata *ucore,
  3350. struct ib_udata *uhw)
  3351. {
  3352. struct ib_uverbs_ex_modify_cq cmd = {};
  3353. struct ib_cq *cq;
  3354. size_t required_cmd_sz;
  3355. int ret;
  3356. required_cmd_sz = offsetof(typeof(cmd), reserved) +
  3357. sizeof(cmd.reserved);
  3358. if (ucore->inlen < required_cmd_sz)
  3359. return -EINVAL;
  3360. /* sanity checks */
  3361. if (ucore->inlen > sizeof(cmd) &&
  3362. !ib_is_udata_cleared(ucore, sizeof(cmd),
  3363. ucore->inlen - sizeof(cmd)))
  3364. return -EOPNOTSUPP;
  3365. ret = ib_copy_from_udata(&cmd, ucore, min(sizeof(cmd), ucore->inlen));
  3366. if (ret)
  3367. return ret;
  3368. if (!cmd.attr_mask || cmd.reserved)
  3369. return -EINVAL;
  3370. if (cmd.attr_mask > IB_CQ_MODERATE)
  3371. return -EOPNOTSUPP;
  3372. cq = uobj_get_obj_read(cq, UVERBS_OBJECT_CQ, cmd.cq_handle, file);
  3373. if (!cq)
  3374. return -EINVAL;
  3375. ret = rdma_set_cq_moderation(cq, cmd.attr.cq_count, cmd.attr.cq_period);
  3376. uobj_put_obj_read(cq);
  3377. return ret;
  3378. }