uverbs_cmd.c 102 KB

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