uverbs_cmd.c 103 KB

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