uverbs_cmd.c 103 KB

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