uverbs_cmd.c 101 KB

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