uverbs_cmd.c 101 KB

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