uverbs_cmd.c 104 KB

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