uverbs_cmd.c 88 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691
  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 <asm/uaccess.h>
  40. #include "uverbs.h"
  41. #include "core_priv.h"
  42. struct uverbs_lock_class {
  43. struct lock_class_key key;
  44. char name[16];
  45. };
  46. static struct uverbs_lock_class pd_lock_class = { .name = "PD-uobj" };
  47. static struct uverbs_lock_class mr_lock_class = { .name = "MR-uobj" };
  48. static struct uverbs_lock_class mw_lock_class = { .name = "MW-uobj" };
  49. static struct uverbs_lock_class cq_lock_class = { .name = "CQ-uobj" };
  50. static struct uverbs_lock_class qp_lock_class = { .name = "QP-uobj" };
  51. static struct uverbs_lock_class ah_lock_class = { .name = "AH-uobj" };
  52. static struct uverbs_lock_class srq_lock_class = { .name = "SRQ-uobj" };
  53. static struct uverbs_lock_class xrcd_lock_class = { .name = "XRCD-uobj" };
  54. static struct uverbs_lock_class rule_lock_class = { .name = "RULE-uobj" };
  55. /*
  56. * The ib_uobject locking scheme is as follows:
  57. *
  58. * - ib_uverbs_idr_lock protects the uverbs idrs themselves, so it
  59. * needs to be held during all idr write operations. When an object is
  60. * looked up, a reference must be taken on the object's kref before
  61. * dropping this lock. For read operations, the rcu_read_lock()
  62. * and rcu_write_lock() but similarly the kref reference is grabbed
  63. * before the rcu_read_unlock().
  64. *
  65. * - Each object also has an rwsem. This rwsem must be held for
  66. * reading while an operation that uses the object is performed.
  67. * For example, while registering an MR, the associated PD's
  68. * uobject.mutex must be held for reading. The rwsem must be held
  69. * for writing while initializing or destroying an object.
  70. *
  71. * - In addition, each object has a "live" flag. If this flag is not
  72. * set, then lookups of the object will fail even if it is found in
  73. * the idr. This handles a reader that blocks and does not acquire
  74. * the rwsem until after the object is destroyed. The destroy
  75. * operation will set the live flag to 0 and then drop the rwsem;
  76. * this will allow the reader to acquire the rwsem, see that the
  77. * live flag is 0, and then drop the rwsem and its reference to
  78. * object. The underlying storage will not be freed until the last
  79. * reference to the object is dropped.
  80. */
  81. static void init_uobj(struct ib_uobject *uobj, u64 user_handle,
  82. struct ib_ucontext *context, struct uverbs_lock_class *c)
  83. {
  84. uobj->user_handle = user_handle;
  85. uobj->context = context;
  86. kref_init(&uobj->ref);
  87. init_rwsem(&uobj->mutex);
  88. lockdep_set_class_and_name(&uobj->mutex, &c->key, c->name);
  89. uobj->live = 0;
  90. }
  91. static void release_uobj(struct kref *kref)
  92. {
  93. kfree_rcu(container_of(kref, struct ib_uobject, ref), rcu);
  94. }
  95. static void put_uobj(struct ib_uobject *uobj)
  96. {
  97. kref_put(&uobj->ref, release_uobj);
  98. }
  99. static void put_uobj_read(struct ib_uobject *uobj)
  100. {
  101. up_read(&uobj->mutex);
  102. put_uobj(uobj);
  103. }
  104. static void put_uobj_write(struct ib_uobject *uobj)
  105. {
  106. up_write(&uobj->mutex);
  107. put_uobj(uobj);
  108. }
  109. static int idr_add_uobj(struct idr *idr, struct ib_uobject *uobj)
  110. {
  111. int ret;
  112. idr_preload(GFP_KERNEL);
  113. spin_lock(&ib_uverbs_idr_lock);
  114. ret = idr_alloc(idr, uobj, 0, 0, GFP_NOWAIT);
  115. if (ret >= 0)
  116. uobj->id = ret;
  117. spin_unlock(&ib_uverbs_idr_lock);
  118. idr_preload_end();
  119. return ret < 0 ? ret : 0;
  120. }
  121. void idr_remove_uobj(struct idr *idr, struct ib_uobject *uobj)
  122. {
  123. spin_lock(&ib_uverbs_idr_lock);
  124. idr_remove(idr, uobj->id);
  125. spin_unlock(&ib_uverbs_idr_lock);
  126. }
  127. static struct ib_uobject *__idr_get_uobj(struct idr *idr, int id,
  128. struct ib_ucontext *context)
  129. {
  130. struct ib_uobject *uobj;
  131. rcu_read_lock();
  132. uobj = idr_find(idr, id);
  133. if (uobj) {
  134. if (uobj->context == context)
  135. kref_get(&uobj->ref);
  136. else
  137. uobj = NULL;
  138. }
  139. rcu_read_unlock();
  140. return uobj;
  141. }
  142. static struct ib_uobject *idr_read_uobj(struct idr *idr, int id,
  143. struct ib_ucontext *context, int nested)
  144. {
  145. struct ib_uobject *uobj;
  146. uobj = __idr_get_uobj(idr, id, context);
  147. if (!uobj)
  148. return NULL;
  149. if (nested)
  150. down_read_nested(&uobj->mutex, SINGLE_DEPTH_NESTING);
  151. else
  152. down_read(&uobj->mutex);
  153. if (!uobj->live) {
  154. put_uobj_read(uobj);
  155. return NULL;
  156. }
  157. return uobj;
  158. }
  159. static struct ib_uobject *idr_write_uobj(struct idr *idr, int id,
  160. struct ib_ucontext *context)
  161. {
  162. struct ib_uobject *uobj;
  163. uobj = __idr_get_uobj(idr, id, context);
  164. if (!uobj)
  165. return NULL;
  166. down_write(&uobj->mutex);
  167. if (!uobj->live) {
  168. put_uobj_write(uobj);
  169. return NULL;
  170. }
  171. return uobj;
  172. }
  173. static void *idr_read_obj(struct idr *idr, int id, struct ib_ucontext *context,
  174. int nested)
  175. {
  176. struct ib_uobject *uobj;
  177. uobj = idr_read_uobj(idr, id, context, nested);
  178. return uobj ? uobj->object : NULL;
  179. }
  180. static struct ib_pd *idr_read_pd(int pd_handle, struct ib_ucontext *context)
  181. {
  182. return idr_read_obj(&ib_uverbs_pd_idr, pd_handle, context, 0);
  183. }
  184. static void put_pd_read(struct ib_pd *pd)
  185. {
  186. put_uobj_read(pd->uobject);
  187. }
  188. static struct ib_cq *idr_read_cq(int cq_handle, struct ib_ucontext *context, int nested)
  189. {
  190. return idr_read_obj(&ib_uverbs_cq_idr, cq_handle, context, nested);
  191. }
  192. static void put_cq_read(struct ib_cq *cq)
  193. {
  194. put_uobj_read(cq->uobject);
  195. }
  196. static struct ib_ah *idr_read_ah(int ah_handle, struct ib_ucontext *context)
  197. {
  198. return idr_read_obj(&ib_uverbs_ah_idr, ah_handle, context, 0);
  199. }
  200. static void put_ah_read(struct ib_ah *ah)
  201. {
  202. put_uobj_read(ah->uobject);
  203. }
  204. static struct ib_qp *idr_read_qp(int qp_handle, struct ib_ucontext *context)
  205. {
  206. return idr_read_obj(&ib_uverbs_qp_idr, qp_handle, context, 0);
  207. }
  208. static struct ib_qp *idr_write_qp(int qp_handle, struct ib_ucontext *context)
  209. {
  210. struct ib_uobject *uobj;
  211. uobj = idr_write_uobj(&ib_uverbs_qp_idr, qp_handle, context);
  212. return uobj ? uobj->object : NULL;
  213. }
  214. static void put_qp_read(struct ib_qp *qp)
  215. {
  216. put_uobj_read(qp->uobject);
  217. }
  218. static void put_qp_write(struct ib_qp *qp)
  219. {
  220. put_uobj_write(qp->uobject);
  221. }
  222. static struct ib_srq *idr_read_srq(int srq_handle, struct ib_ucontext *context)
  223. {
  224. return idr_read_obj(&ib_uverbs_srq_idr, srq_handle, context, 0);
  225. }
  226. static void put_srq_read(struct ib_srq *srq)
  227. {
  228. put_uobj_read(srq->uobject);
  229. }
  230. static struct ib_xrcd *idr_read_xrcd(int xrcd_handle, struct ib_ucontext *context,
  231. struct ib_uobject **uobj)
  232. {
  233. *uobj = idr_read_uobj(&ib_uverbs_xrcd_idr, xrcd_handle, context, 0);
  234. return *uobj ? (*uobj)->object : NULL;
  235. }
  236. static void put_xrcd_read(struct ib_uobject *uobj)
  237. {
  238. put_uobj_read(uobj);
  239. }
  240. ssize_t ib_uverbs_get_context(struct ib_uverbs_file *file,
  241. struct ib_device *ib_dev,
  242. const char __user *buf,
  243. int in_len, int out_len)
  244. {
  245. struct ib_uverbs_get_context cmd;
  246. struct ib_uverbs_get_context_resp resp;
  247. struct ib_udata udata;
  248. #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
  249. struct ib_device_attr dev_attr;
  250. #endif
  251. struct ib_ucontext *ucontext;
  252. struct file *filp;
  253. int ret;
  254. if (out_len < sizeof resp)
  255. return -ENOSPC;
  256. if (copy_from_user(&cmd, buf, sizeof cmd))
  257. return -EFAULT;
  258. mutex_lock(&file->mutex);
  259. if (file->ucontext) {
  260. ret = -EINVAL;
  261. goto err;
  262. }
  263. INIT_UDATA(&udata, buf + sizeof cmd,
  264. (unsigned long) cmd.response + sizeof resp,
  265. in_len - sizeof cmd, out_len - sizeof resp);
  266. ucontext = ib_dev->alloc_ucontext(ib_dev, &udata);
  267. if (IS_ERR(ucontext)) {
  268. ret = PTR_ERR(ucontext);
  269. goto err;
  270. }
  271. ucontext->device = ib_dev;
  272. INIT_LIST_HEAD(&ucontext->pd_list);
  273. INIT_LIST_HEAD(&ucontext->mr_list);
  274. INIT_LIST_HEAD(&ucontext->mw_list);
  275. INIT_LIST_HEAD(&ucontext->cq_list);
  276. INIT_LIST_HEAD(&ucontext->qp_list);
  277. INIT_LIST_HEAD(&ucontext->srq_list);
  278. INIT_LIST_HEAD(&ucontext->ah_list);
  279. INIT_LIST_HEAD(&ucontext->xrcd_list);
  280. INIT_LIST_HEAD(&ucontext->rule_list);
  281. rcu_read_lock();
  282. ucontext->tgid = get_task_pid(current->group_leader, PIDTYPE_PID);
  283. rcu_read_unlock();
  284. ucontext->closing = 0;
  285. #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
  286. ucontext->umem_tree = RB_ROOT;
  287. init_rwsem(&ucontext->umem_rwsem);
  288. ucontext->odp_mrs_count = 0;
  289. INIT_LIST_HEAD(&ucontext->no_private_counters);
  290. ret = ib_query_device(ib_dev, &dev_attr);
  291. if (ret)
  292. goto err_free;
  293. if (!(dev_attr.device_cap_flags & IB_DEVICE_ON_DEMAND_PAGING))
  294. ucontext->invalidate_range = NULL;
  295. #endif
  296. resp.num_comp_vectors = file->device->num_comp_vectors;
  297. ret = get_unused_fd_flags(O_CLOEXEC);
  298. if (ret < 0)
  299. goto err_free;
  300. resp.async_fd = ret;
  301. filp = ib_uverbs_alloc_event_file(file, ib_dev, 1);
  302. if (IS_ERR(filp)) {
  303. ret = PTR_ERR(filp);
  304. goto err_fd;
  305. }
  306. if (copy_to_user((void __user *) (unsigned long) cmd.response,
  307. &resp, sizeof resp)) {
  308. ret = -EFAULT;
  309. goto err_file;
  310. }
  311. file->ucontext = ucontext;
  312. fd_install(resp.async_fd, filp);
  313. mutex_unlock(&file->mutex);
  314. return in_len;
  315. err_file:
  316. ib_uverbs_free_async_event_file(file);
  317. fput(filp);
  318. err_fd:
  319. put_unused_fd(resp.async_fd);
  320. err_free:
  321. put_pid(ucontext->tgid);
  322. ib_dev->dealloc_ucontext(ucontext);
  323. err:
  324. mutex_unlock(&file->mutex);
  325. return ret;
  326. }
  327. static void copy_query_dev_fields(struct ib_uverbs_file *file,
  328. struct ib_device *ib_dev,
  329. struct ib_uverbs_query_device_resp *resp,
  330. struct ib_device_attr *attr)
  331. {
  332. resp->fw_ver = attr->fw_ver;
  333. resp->node_guid = ib_dev->node_guid;
  334. resp->sys_image_guid = attr->sys_image_guid;
  335. resp->max_mr_size = attr->max_mr_size;
  336. resp->page_size_cap = attr->page_size_cap;
  337. resp->vendor_id = attr->vendor_id;
  338. resp->vendor_part_id = attr->vendor_part_id;
  339. resp->hw_ver = attr->hw_ver;
  340. resp->max_qp = attr->max_qp;
  341. resp->max_qp_wr = attr->max_qp_wr;
  342. resp->device_cap_flags = attr->device_cap_flags;
  343. resp->max_sge = attr->max_sge;
  344. resp->max_sge_rd = attr->max_sge_rd;
  345. resp->max_cq = attr->max_cq;
  346. resp->max_cqe = attr->max_cqe;
  347. resp->max_mr = attr->max_mr;
  348. resp->max_pd = attr->max_pd;
  349. resp->max_qp_rd_atom = attr->max_qp_rd_atom;
  350. resp->max_ee_rd_atom = attr->max_ee_rd_atom;
  351. resp->max_res_rd_atom = attr->max_res_rd_atom;
  352. resp->max_qp_init_rd_atom = attr->max_qp_init_rd_atom;
  353. resp->max_ee_init_rd_atom = attr->max_ee_init_rd_atom;
  354. resp->atomic_cap = attr->atomic_cap;
  355. resp->max_ee = attr->max_ee;
  356. resp->max_rdd = attr->max_rdd;
  357. resp->max_mw = attr->max_mw;
  358. resp->max_raw_ipv6_qp = attr->max_raw_ipv6_qp;
  359. resp->max_raw_ethy_qp = attr->max_raw_ethy_qp;
  360. resp->max_mcast_grp = attr->max_mcast_grp;
  361. resp->max_mcast_qp_attach = attr->max_mcast_qp_attach;
  362. resp->max_total_mcast_qp_attach = attr->max_total_mcast_qp_attach;
  363. resp->max_ah = attr->max_ah;
  364. resp->max_fmr = attr->max_fmr;
  365. resp->max_map_per_fmr = attr->max_map_per_fmr;
  366. resp->max_srq = attr->max_srq;
  367. resp->max_srq_wr = attr->max_srq_wr;
  368. resp->max_srq_sge = attr->max_srq_sge;
  369. resp->max_pkeys = attr->max_pkeys;
  370. resp->local_ca_ack_delay = attr->local_ca_ack_delay;
  371. resp->phys_port_cnt = ib_dev->phys_port_cnt;
  372. }
  373. ssize_t ib_uverbs_query_device(struct ib_uverbs_file *file,
  374. struct ib_device *ib_dev,
  375. const char __user *buf,
  376. int in_len, int out_len)
  377. {
  378. struct ib_uverbs_query_device cmd;
  379. struct ib_uverbs_query_device_resp resp;
  380. struct ib_device_attr attr;
  381. int ret;
  382. if (out_len < sizeof resp)
  383. return -ENOSPC;
  384. if (copy_from_user(&cmd, buf, sizeof cmd))
  385. return -EFAULT;
  386. ret = ib_query_device(ib_dev, &attr);
  387. if (ret)
  388. return ret;
  389. memset(&resp, 0, sizeof resp);
  390. copy_query_dev_fields(file, ib_dev, &resp, &attr);
  391. if (copy_to_user((void __user *) (unsigned long) cmd.response,
  392. &resp, sizeof resp))
  393. return -EFAULT;
  394. return in_len;
  395. }
  396. ssize_t ib_uverbs_query_port(struct ib_uverbs_file *file,
  397. struct ib_device *ib_dev,
  398. const char __user *buf,
  399. int in_len, int out_len)
  400. {
  401. struct ib_uverbs_query_port cmd;
  402. struct ib_uverbs_query_port_resp resp;
  403. struct ib_port_attr attr;
  404. int ret;
  405. if (out_len < sizeof resp)
  406. return -ENOSPC;
  407. if (copy_from_user(&cmd, buf, sizeof cmd))
  408. return -EFAULT;
  409. ret = ib_query_port(ib_dev, cmd.port_num, &attr);
  410. if (ret)
  411. return ret;
  412. memset(&resp, 0, sizeof resp);
  413. resp.state = attr.state;
  414. resp.max_mtu = attr.max_mtu;
  415. resp.active_mtu = attr.active_mtu;
  416. resp.gid_tbl_len = attr.gid_tbl_len;
  417. resp.port_cap_flags = attr.port_cap_flags;
  418. resp.max_msg_sz = attr.max_msg_sz;
  419. resp.bad_pkey_cntr = attr.bad_pkey_cntr;
  420. resp.qkey_viol_cntr = attr.qkey_viol_cntr;
  421. resp.pkey_tbl_len = attr.pkey_tbl_len;
  422. resp.lid = attr.lid;
  423. resp.sm_lid = attr.sm_lid;
  424. resp.lmc = attr.lmc;
  425. resp.max_vl_num = attr.max_vl_num;
  426. resp.sm_sl = attr.sm_sl;
  427. resp.subnet_timeout = attr.subnet_timeout;
  428. resp.init_type_reply = attr.init_type_reply;
  429. resp.active_width = attr.active_width;
  430. resp.active_speed = attr.active_speed;
  431. resp.phys_state = attr.phys_state;
  432. resp.link_layer = rdma_port_get_link_layer(ib_dev,
  433. cmd.port_num);
  434. if (copy_to_user((void __user *) (unsigned long) cmd.response,
  435. &resp, sizeof resp))
  436. return -EFAULT;
  437. return in_len;
  438. }
  439. ssize_t ib_uverbs_alloc_pd(struct ib_uverbs_file *file,
  440. struct ib_device *ib_dev,
  441. const char __user *buf,
  442. int in_len, int out_len)
  443. {
  444. struct ib_uverbs_alloc_pd cmd;
  445. struct ib_uverbs_alloc_pd_resp resp;
  446. struct ib_udata udata;
  447. struct ib_uobject *uobj;
  448. struct ib_pd *pd;
  449. int ret;
  450. if (out_len < sizeof resp)
  451. return -ENOSPC;
  452. if (copy_from_user(&cmd, buf, sizeof cmd))
  453. return -EFAULT;
  454. INIT_UDATA(&udata, buf + sizeof cmd,
  455. (unsigned long) cmd.response + sizeof resp,
  456. in_len - sizeof cmd, out_len - sizeof resp);
  457. uobj = kmalloc(sizeof *uobj, GFP_KERNEL);
  458. if (!uobj)
  459. return -ENOMEM;
  460. init_uobj(uobj, 0, file->ucontext, &pd_lock_class);
  461. down_write(&uobj->mutex);
  462. pd = ib_dev->alloc_pd(ib_dev, file->ucontext, &udata);
  463. if (IS_ERR(pd)) {
  464. ret = PTR_ERR(pd);
  465. goto err;
  466. }
  467. pd->device = ib_dev;
  468. pd->uobject = uobj;
  469. pd->local_mr = NULL;
  470. atomic_set(&pd->usecnt, 0);
  471. uobj->object = pd;
  472. ret = idr_add_uobj(&ib_uverbs_pd_idr, uobj);
  473. if (ret)
  474. goto err_idr;
  475. memset(&resp, 0, sizeof resp);
  476. resp.pd_handle = uobj->id;
  477. if (copy_to_user((void __user *) (unsigned long) cmd.response,
  478. &resp, sizeof resp)) {
  479. ret = -EFAULT;
  480. goto err_copy;
  481. }
  482. mutex_lock(&file->mutex);
  483. list_add_tail(&uobj->list, &file->ucontext->pd_list);
  484. mutex_unlock(&file->mutex);
  485. uobj->live = 1;
  486. up_write(&uobj->mutex);
  487. return in_len;
  488. err_copy:
  489. idr_remove_uobj(&ib_uverbs_pd_idr, uobj);
  490. err_idr:
  491. ib_dealloc_pd(pd);
  492. err:
  493. put_uobj_write(uobj);
  494. return ret;
  495. }
  496. ssize_t ib_uverbs_dealloc_pd(struct ib_uverbs_file *file,
  497. struct ib_device *ib_dev,
  498. const char __user *buf,
  499. int in_len, int out_len)
  500. {
  501. struct ib_uverbs_dealloc_pd cmd;
  502. struct ib_uobject *uobj;
  503. struct ib_pd *pd;
  504. int ret;
  505. if (copy_from_user(&cmd, buf, sizeof cmd))
  506. return -EFAULT;
  507. uobj = idr_write_uobj(&ib_uverbs_pd_idr, cmd.pd_handle, file->ucontext);
  508. if (!uobj)
  509. return -EINVAL;
  510. pd = uobj->object;
  511. if (atomic_read(&pd->usecnt)) {
  512. ret = -EBUSY;
  513. goto err_put;
  514. }
  515. ret = pd->device->dealloc_pd(uobj->object);
  516. WARN_ONCE(ret, "Infiniband HW driver failed dealloc_pd");
  517. if (ret)
  518. goto err_put;
  519. uobj->live = 0;
  520. put_uobj_write(uobj);
  521. idr_remove_uobj(&ib_uverbs_pd_idr, uobj);
  522. mutex_lock(&file->mutex);
  523. list_del(&uobj->list);
  524. mutex_unlock(&file->mutex);
  525. put_uobj(uobj);
  526. return in_len;
  527. err_put:
  528. put_uobj_write(uobj);
  529. return ret;
  530. }
  531. struct xrcd_table_entry {
  532. struct rb_node node;
  533. struct ib_xrcd *xrcd;
  534. struct inode *inode;
  535. };
  536. static int xrcd_table_insert(struct ib_uverbs_device *dev,
  537. struct inode *inode,
  538. struct ib_xrcd *xrcd)
  539. {
  540. struct xrcd_table_entry *entry, *scan;
  541. struct rb_node **p = &dev->xrcd_tree.rb_node;
  542. struct rb_node *parent = NULL;
  543. entry = kmalloc(sizeof *entry, GFP_KERNEL);
  544. if (!entry)
  545. return -ENOMEM;
  546. entry->xrcd = xrcd;
  547. entry->inode = inode;
  548. while (*p) {
  549. parent = *p;
  550. scan = rb_entry(parent, struct xrcd_table_entry, node);
  551. if (inode < scan->inode) {
  552. p = &(*p)->rb_left;
  553. } else if (inode > scan->inode) {
  554. p = &(*p)->rb_right;
  555. } else {
  556. kfree(entry);
  557. return -EEXIST;
  558. }
  559. }
  560. rb_link_node(&entry->node, parent, p);
  561. rb_insert_color(&entry->node, &dev->xrcd_tree);
  562. igrab(inode);
  563. return 0;
  564. }
  565. static struct xrcd_table_entry *xrcd_table_search(struct ib_uverbs_device *dev,
  566. struct inode *inode)
  567. {
  568. struct xrcd_table_entry *entry;
  569. struct rb_node *p = dev->xrcd_tree.rb_node;
  570. while (p) {
  571. entry = rb_entry(p, struct xrcd_table_entry, node);
  572. if (inode < entry->inode)
  573. p = p->rb_left;
  574. else if (inode > entry->inode)
  575. p = p->rb_right;
  576. else
  577. return entry;
  578. }
  579. return NULL;
  580. }
  581. static struct ib_xrcd *find_xrcd(struct ib_uverbs_device *dev, struct inode *inode)
  582. {
  583. struct xrcd_table_entry *entry;
  584. entry = xrcd_table_search(dev, inode);
  585. if (!entry)
  586. return NULL;
  587. return entry->xrcd;
  588. }
  589. static void xrcd_table_delete(struct ib_uverbs_device *dev,
  590. struct inode *inode)
  591. {
  592. struct xrcd_table_entry *entry;
  593. entry = xrcd_table_search(dev, inode);
  594. if (entry) {
  595. iput(inode);
  596. rb_erase(&entry->node, &dev->xrcd_tree);
  597. kfree(entry);
  598. }
  599. }
  600. ssize_t ib_uverbs_open_xrcd(struct ib_uverbs_file *file,
  601. struct ib_device *ib_dev,
  602. const char __user *buf, int in_len,
  603. int out_len)
  604. {
  605. struct ib_uverbs_open_xrcd cmd;
  606. struct ib_uverbs_open_xrcd_resp resp;
  607. struct ib_udata udata;
  608. struct ib_uxrcd_object *obj;
  609. struct ib_xrcd *xrcd = NULL;
  610. struct fd f = {NULL, 0};
  611. struct inode *inode = NULL;
  612. int ret = 0;
  613. int new_xrcd = 0;
  614. if (out_len < sizeof resp)
  615. return -ENOSPC;
  616. if (copy_from_user(&cmd, buf, sizeof cmd))
  617. return -EFAULT;
  618. INIT_UDATA(&udata, buf + sizeof cmd,
  619. (unsigned long) cmd.response + sizeof resp,
  620. in_len - sizeof cmd, out_len - sizeof resp);
  621. mutex_lock(&file->device->xrcd_tree_mutex);
  622. if (cmd.fd != -1) {
  623. /* search for file descriptor */
  624. f = fdget(cmd.fd);
  625. if (!f.file) {
  626. ret = -EBADF;
  627. goto err_tree_mutex_unlock;
  628. }
  629. inode = file_inode(f.file);
  630. xrcd = find_xrcd(file->device, inode);
  631. if (!xrcd && !(cmd.oflags & O_CREAT)) {
  632. /* no file descriptor. Need CREATE flag */
  633. ret = -EAGAIN;
  634. goto err_tree_mutex_unlock;
  635. }
  636. if (xrcd && cmd.oflags & O_EXCL) {
  637. ret = -EINVAL;
  638. goto err_tree_mutex_unlock;
  639. }
  640. }
  641. obj = kmalloc(sizeof *obj, GFP_KERNEL);
  642. if (!obj) {
  643. ret = -ENOMEM;
  644. goto err_tree_mutex_unlock;
  645. }
  646. init_uobj(&obj->uobject, 0, file->ucontext, &xrcd_lock_class);
  647. down_write(&obj->uobject.mutex);
  648. if (!xrcd) {
  649. xrcd = ib_dev->alloc_xrcd(ib_dev, file->ucontext, &udata);
  650. if (IS_ERR(xrcd)) {
  651. ret = PTR_ERR(xrcd);
  652. goto err;
  653. }
  654. xrcd->inode = inode;
  655. xrcd->device = ib_dev;
  656. atomic_set(&xrcd->usecnt, 0);
  657. mutex_init(&xrcd->tgt_qp_mutex);
  658. INIT_LIST_HEAD(&xrcd->tgt_qp_list);
  659. new_xrcd = 1;
  660. }
  661. atomic_set(&obj->refcnt, 0);
  662. obj->uobject.object = xrcd;
  663. ret = idr_add_uobj(&ib_uverbs_xrcd_idr, &obj->uobject);
  664. if (ret)
  665. goto err_idr;
  666. memset(&resp, 0, sizeof resp);
  667. resp.xrcd_handle = obj->uobject.id;
  668. if (inode) {
  669. if (new_xrcd) {
  670. /* create new inode/xrcd table entry */
  671. ret = xrcd_table_insert(file->device, inode, xrcd);
  672. if (ret)
  673. goto err_insert_xrcd;
  674. }
  675. atomic_inc(&xrcd->usecnt);
  676. }
  677. if (copy_to_user((void __user *) (unsigned long) cmd.response,
  678. &resp, sizeof resp)) {
  679. ret = -EFAULT;
  680. goto err_copy;
  681. }
  682. if (f.file)
  683. fdput(f);
  684. mutex_lock(&file->mutex);
  685. list_add_tail(&obj->uobject.list, &file->ucontext->xrcd_list);
  686. mutex_unlock(&file->mutex);
  687. obj->uobject.live = 1;
  688. up_write(&obj->uobject.mutex);
  689. mutex_unlock(&file->device->xrcd_tree_mutex);
  690. return in_len;
  691. err_copy:
  692. if (inode) {
  693. if (new_xrcd)
  694. xrcd_table_delete(file->device, inode);
  695. atomic_dec(&xrcd->usecnt);
  696. }
  697. err_insert_xrcd:
  698. idr_remove_uobj(&ib_uverbs_xrcd_idr, &obj->uobject);
  699. err_idr:
  700. ib_dealloc_xrcd(xrcd);
  701. err:
  702. put_uobj_write(&obj->uobject);
  703. err_tree_mutex_unlock:
  704. if (f.file)
  705. fdput(f);
  706. mutex_unlock(&file->device->xrcd_tree_mutex);
  707. return ret;
  708. }
  709. ssize_t ib_uverbs_close_xrcd(struct ib_uverbs_file *file,
  710. struct ib_device *ib_dev,
  711. const char __user *buf, int in_len,
  712. int out_len)
  713. {
  714. struct ib_uverbs_close_xrcd cmd;
  715. struct ib_uobject *uobj;
  716. struct ib_xrcd *xrcd = NULL;
  717. struct inode *inode = NULL;
  718. struct ib_uxrcd_object *obj;
  719. int live;
  720. int ret = 0;
  721. if (copy_from_user(&cmd, buf, sizeof cmd))
  722. return -EFAULT;
  723. mutex_lock(&file->device->xrcd_tree_mutex);
  724. uobj = idr_write_uobj(&ib_uverbs_xrcd_idr, cmd.xrcd_handle, file->ucontext);
  725. if (!uobj) {
  726. ret = -EINVAL;
  727. goto out;
  728. }
  729. xrcd = uobj->object;
  730. inode = xrcd->inode;
  731. obj = container_of(uobj, struct ib_uxrcd_object, uobject);
  732. if (atomic_read(&obj->refcnt)) {
  733. put_uobj_write(uobj);
  734. ret = -EBUSY;
  735. goto out;
  736. }
  737. if (!inode || atomic_dec_and_test(&xrcd->usecnt)) {
  738. ret = ib_dealloc_xrcd(uobj->object);
  739. if (!ret)
  740. uobj->live = 0;
  741. }
  742. live = uobj->live;
  743. if (inode && ret)
  744. atomic_inc(&xrcd->usecnt);
  745. put_uobj_write(uobj);
  746. if (ret)
  747. goto out;
  748. if (inode && !live)
  749. xrcd_table_delete(file->device, inode);
  750. idr_remove_uobj(&ib_uverbs_xrcd_idr, uobj);
  751. mutex_lock(&file->mutex);
  752. list_del(&uobj->list);
  753. mutex_unlock(&file->mutex);
  754. put_uobj(uobj);
  755. ret = in_len;
  756. out:
  757. mutex_unlock(&file->device->xrcd_tree_mutex);
  758. return ret;
  759. }
  760. void ib_uverbs_dealloc_xrcd(struct ib_uverbs_device *dev,
  761. struct ib_xrcd *xrcd)
  762. {
  763. struct inode *inode;
  764. inode = xrcd->inode;
  765. if (inode && !atomic_dec_and_test(&xrcd->usecnt))
  766. return;
  767. ib_dealloc_xrcd(xrcd);
  768. if (inode)
  769. xrcd_table_delete(dev, inode);
  770. }
  771. ssize_t ib_uverbs_reg_mr(struct ib_uverbs_file *file,
  772. struct ib_device *ib_dev,
  773. const char __user *buf, int in_len,
  774. int out_len)
  775. {
  776. struct ib_uverbs_reg_mr cmd;
  777. struct ib_uverbs_reg_mr_resp resp;
  778. struct ib_udata udata;
  779. struct ib_uobject *uobj;
  780. struct ib_pd *pd;
  781. struct ib_mr *mr;
  782. int ret;
  783. if (out_len < sizeof resp)
  784. return -ENOSPC;
  785. if (copy_from_user(&cmd, buf, sizeof cmd))
  786. return -EFAULT;
  787. INIT_UDATA(&udata, buf + sizeof cmd,
  788. (unsigned long) cmd.response + sizeof resp,
  789. in_len - sizeof cmd, out_len - sizeof resp);
  790. if ((cmd.start & ~PAGE_MASK) != (cmd.hca_va & ~PAGE_MASK))
  791. return -EINVAL;
  792. ret = ib_check_mr_access(cmd.access_flags);
  793. if (ret)
  794. return ret;
  795. uobj = kmalloc(sizeof *uobj, GFP_KERNEL);
  796. if (!uobj)
  797. return -ENOMEM;
  798. init_uobj(uobj, 0, file->ucontext, &mr_lock_class);
  799. down_write(&uobj->mutex);
  800. pd = idr_read_pd(cmd.pd_handle, file->ucontext);
  801. if (!pd) {
  802. ret = -EINVAL;
  803. goto err_free;
  804. }
  805. if (cmd.access_flags & IB_ACCESS_ON_DEMAND) {
  806. struct ib_device_attr attr;
  807. ret = ib_query_device(pd->device, &attr);
  808. if (ret || !(attr.device_cap_flags &
  809. IB_DEVICE_ON_DEMAND_PAGING)) {
  810. pr_debug("ODP support not available\n");
  811. ret = -EINVAL;
  812. goto err_put;
  813. }
  814. }
  815. mr = pd->device->reg_user_mr(pd, cmd.start, cmd.length, cmd.hca_va,
  816. cmd.access_flags, &udata);
  817. if (IS_ERR(mr)) {
  818. ret = PTR_ERR(mr);
  819. goto err_put;
  820. }
  821. mr->device = pd->device;
  822. mr->pd = pd;
  823. mr->uobject = uobj;
  824. atomic_inc(&pd->usecnt);
  825. atomic_set(&mr->usecnt, 0);
  826. uobj->object = mr;
  827. ret = idr_add_uobj(&ib_uverbs_mr_idr, uobj);
  828. if (ret)
  829. goto err_unreg;
  830. memset(&resp, 0, sizeof resp);
  831. resp.lkey = mr->lkey;
  832. resp.rkey = mr->rkey;
  833. resp.mr_handle = uobj->id;
  834. if (copy_to_user((void __user *) (unsigned long) cmd.response,
  835. &resp, sizeof resp)) {
  836. ret = -EFAULT;
  837. goto err_copy;
  838. }
  839. put_pd_read(pd);
  840. mutex_lock(&file->mutex);
  841. list_add_tail(&uobj->list, &file->ucontext->mr_list);
  842. mutex_unlock(&file->mutex);
  843. uobj->live = 1;
  844. up_write(&uobj->mutex);
  845. return in_len;
  846. err_copy:
  847. idr_remove_uobj(&ib_uverbs_mr_idr, uobj);
  848. err_unreg:
  849. ib_dereg_mr(mr);
  850. err_put:
  851. put_pd_read(pd);
  852. err_free:
  853. put_uobj_write(uobj);
  854. return ret;
  855. }
  856. ssize_t ib_uverbs_rereg_mr(struct ib_uverbs_file *file,
  857. struct ib_device *ib_dev,
  858. const char __user *buf, int in_len,
  859. int out_len)
  860. {
  861. struct ib_uverbs_rereg_mr cmd;
  862. struct ib_uverbs_rereg_mr_resp resp;
  863. struct ib_udata udata;
  864. struct ib_pd *pd = NULL;
  865. struct ib_mr *mr;
  866. struct ib_pd *old_pd;
  867. int ret;
  868. struct ib_uobject *uobj;
  869. if (out_len < sizeof(resp))
  870. return -ENOSPC;
  871. if (copy_from_user(&cmd, buf, sizeof(cmd)))
  872. return -EFAULT;
  873. INIT_UDATA(&udata, buf + sizeof(cmd),
  874. (unsigned long) cmd.response + sizeof(resp),
  875. in_len - sizeof(cmd), out_len - sizeof(resp));
  876. if (cmd.flags & ~IB_MR_REREG_SUPPORTED || !cmd.flags)
  877. return -EINVAL;
  878. if ((cmd.flags & IB_MR_REREG_TRANS) &&
  879. (!cmd.start || !cmd.hca_va || 0 >= cmd.length ||
  880. (cmd.start & ~PAGE_MASK) != (cmd.hca_va & ~PAGE_MASK)))
  881. return -EINVAL;
  882. uobj = idr_write_uobj(&ib_uverbs_mr_idr, cmd.mr_handle,
  883. file->ucontext);
  884. if (!uobj)
  885. return -EINVAL;
  886. mr = uobj->object;
  887. if (cmd.flags & IB_MR_REREG_ACCESS) {
  888. ret = ib_check_mr_access(cmd.access_flags);
  889. if (ret)
  890. goto put_uobjs;
  891. }
  892. if (cmd.flags & IB_MR_REREG_PD) {
  893. pd = idr_read_pd(cmd.pd_handle, file->ucontext);
  894. if (!pd) {
  895. ret = -EINVAL;
  896. goto put_uobjs;
  897. }
  898. }
  899. if (atomic_read(&mr->usecnt)) {
  900. ret = -EBUSY;
  901. goto put_uobj_pd;
  902. }
  903. old_pd = mr->pd;
  904. ret = mr->device->rereg_user_mr(mr, cmd.flags, cmd.start,
  905. cmd.length, cmd.hca_va,
  906. cmd.access_flags, pd, &udata);
  907. if (!ret) {
  908. if (cmd.flags & IB_MR_REREG_PD) {
  909. atomic_inc(&pd->usecnt);
  910. mr->pd = pd;
  911. atomic_dec(&old_pd->usecnt);
  912. }
  913. } else {
  914. goto put_uobj_pd;
  915. }
  916. memset(&resp, 0, sizeof(resp));
  917. resp.lkey = mr->lkey;
  918. resp.rkey = mr->rkey;
  919. if (copy_to_user((void __user *)(unsigned long)cmd.response,
  920. &resp, sizeof(resp)))
  921. ret = -EFAULT;
  922. else
  923. ret = in_len;
  924. put_uobj_pd:
  925. if (cmd.flags & IB_MR_REREG_PD)
  926. put_pd_read(pd);
  927. put_uobjs:
  928. put_uobj_write(mr->uobject);
  929. return ret;
  930. }
  931. ssize_t ib_uverbs_dereg_mr(struct ib_uverbs_file *file,
  932. struct ib_device *ib_dev,
  933. const char __user *buf, int in_len,
  934. int out_len)
  935. {
  936. struct ib_uverbs_dereg_mr cmd;
  937. struct ib_mr *mr;
  938. struct ib_uobject *uobj;
  939. int ret = -EINVAL;
  940. if (copy_from_user(&cmd, buf, sizeof cmd))
  941. return -EFAULT;
  942. uobj = idr_write_uobj(&ib_uverbs_mr_idr, cmd.mr_handle, file->ucontext);
  943. if (!uobj)
  944. return -EINVAL;
  945. mr = uobj->object;
  946. ret = ib_dereg_mr(mr);
  947. if (!ret)
  948. uobj->live = 0;
  949. put_uobj_write(uobj);
  950. if (ret)
  951. return ret;
  952. idr_remove_uobj(&ib_uverbs_mr_idr, uobj);
  953. mutex_lock(&file->mutex);
  954. list_del(&uobj->list);
  955. mutex_unlock(&file->mutex);
  956. put_uobj(uobj);
  957. return in_len;
  958. }
  959. ssize_t ib_uverbs_alloc_mw(struct ib_uverbs_file *file,
  960. struct ib_device *ib_dev,
  961. const char __user *buf, int in_len,
  962. int out_len)
  963. {
  964. struct ib_uverbs_alloc_mw cmd;
  965. struct ib_uverbs_alloc_mw_resp resp;
  966. struct ib_uobject *uobj;
  967. struct ib_pd *pd;
  968. struct ib_mw *mw;
  969. int ret;
  970. if (out_len < sizeof(resp))
  971. return -ENOSPC;
  972. if (copy_from_user(&cmd, buf, sizeof(cmd)))
  973. return -EFAULT;
  974. uobj = kmalloc(sizeof(*uobj), GFP_KERNEL);
  975. if (!uobj)
  976. return -ENOMEM;
  977. init_uobj(uobj, 0, file->ucontext, &mw_lock_class);
  978. down_write(&uobj->mutex);
  979. pd = idr_read_pd(cmd.pd_handle, file->ucontext);
  980. if (!pd) {
  981. ret = -EINVAL;
  982. goto err_free;
  983. }
  984. mw = pd->device->alloc_mw(pd, cmd.mw_type);
  985. if (IS_ERR(mw)) {
  986. ret = PTR_ERR(mw);
  987. goto err_put;
  988. }
  989. mw->device = pd->device;
  990. mw->pd = pd;
  991. mw->uobject = uobj;
  992. atomic_inc(&pd->usecnt);
  993. uobj->object = mw;
  994. ret = idr_add_uobj(&ib_uverbs_mw_idr, uobj);
  995. if (ret)
  996. goto err_unalloc;
  997. memset(&resp, 0, sizeof(resp));
  998. resp.rkey = mw->rkey;
  999. resp.mw_handle = uobj->id;
  1000. if (copy_to_user((void __user *)(unsigned long)cmd.response,
  1001. &resp, sizeof(resp))) {
  1002. ret = -EFAULT;
  1003. goto err_copy;
  1004. }
  1005. put_pd_read(pd);
  1006. mutex_lock(&file->mutex);
  1007. list_add_tail(&uobj->list, &file->ucontext->mw_list);
  1008. mutex_unlock(&file->mutex);
  1009. uobj->live = 1;
  1010. up_write(&uobj->mutex);
  1011. return in_len;
  1012. err_copy:
  1013. idr_remove_uobj(&ib_uverbs_mw_idr, uobj);
  1014. err_unalloc:
  1015. ib_dealloc_mw(mw);
  1016. err_put:
  1017. put_pd_read(pd);
  1018. err_free:
  1019. put_uobj_write(uobj);
  1020. return ret;
  1021. }
  1022. ssize_t ib_uverbs_dealloc_mw(struct ib_uverbs_file *file,
  1023. struct ib_device *ib_dev,
  1024. const char __user *buf, int in_len,
  1025. int out_len)
  1026. {
  1027. struct ib_uverbs_dealloc_mw cmd;
  1028. struct ib_mw *mw;
  1029. struct ib_uobject *uobj;
  1030. int ret = -EINVAL;
  1031. if (copy_from_user(&cmd, buf, sizeof(cmd)))
  1032. return -EFAULT;
  1033. uobj = idr_write_uobj(&ib_uverbs_mw_idr, cmd.mw_handle, file->ucontext);
  1034. if (!uobj)
  1035. return -EINVAL;
  1036. mw = uobj->object;
  1037. ret = ib_dealloc_mw(mw);
  1038. if (!ret)
  1039. uobj->live = 0;
  1040. put_uobj_write(uobj);
  1041. if (ret)
  1042. return ret;
  1043. idr_remove_uobj(&ib_uverbs_mw_idr, uobj);
  1044. mutex_lock(&file->mutex);
  1045. list_del(&uobj->list);
  1046. mutex_unlock(&file->mutex);
  1047. put_uobj(uobj);
  1048. return in_len;
  1049. }
  1050. ssize_t ib_uverbs_create_comp_channel(struct ib_uverbs_file *file,
  1051. struct ib_device *ib_dev,
  1052. const char __user *buf, int in_len,
  1053. int out_len)
  1054. {
  1055. struct ib_uverbs_create_comp_channel cmd;
  1056. struct ib_uverbs_create_comp_channel_resp resp;
  1057. struct file *filp;
  1058. int ret;
  1059. if (out_len < sizeof resp)
  1060. return -ENOSPC;
  1061. if (copy_from_user(&cmd, buf, sizeof cmd))
  1062. return -EFAULT;
  1063. ret = get_unused_fd_flags(O_CLOEXEC);
  1064. if (ret < 0)
  1065. return ret;
  1066. resp.fd = ret;
  1067. filp = ib_uverbs_alloc_event_file(file, ib_dev, 0);
  1068. if (IS_ERR(filp)) {
  1069. put_unused_fd(resp.fd);
  1070. return PTR_ERR(filp);
  1071. }
  1072. if (copy_to_user((void __user *) (unsigned long) cmd.response,
  1073. &resp, sizeof resp)) {
  1074. put_unused_fd(resp.fd);
  1075. fput(filp);
  1076. return -EFAULT;
  1077. }
  1078. fd_install(resp.fd, filp);
  1079. return in_len;
  1080. }
  1081. static struct ib_ucq_object *create_cq(struct ib_uverbs_file *file,
  1082. struct ib_device *ib_dev,
  1083. struct ib_udata *ucore,
  1084. struct ib_udata *uhw,
  1085. struct ib_uverbs_ex_create_cq *cmd,
  1086. size_t cmd_sz,
  1087. int (*cb)(struct ib_uverbs_file *file,
  1088. struct ib_ucq_object *obj,
  1089. struct ib_uverbs_ex_create_cq_resp *resp,
  1090. struct ib_udata *udata,
  1091. void *context),
  1092. void *context)
  1093. {
  1094. struct ib_ucq_object *obj;
  1095. struct ib_uverbs_event_file *ev_file = NULL;
  1096. struct ib_cq *cq;
  1097. int ret;
  1098. struct ib_uverbs_ex_create_cq_resp resp;
  1099. struct ib_cq_init_attr attr = {};
  1100. if (cmd->comp_vector >= file->device->num_comp_vectors)
  1101. return ERR_PTR(-EINVAL);
  1102. obj = kmalloc(sizeof *obj, GFP_KERNEL);
  1103. if (!obj)
  1104. return ERR_PTR(-ENOMEM);
  1105. init_uobj(&obj->uobject, cmd->user_handle, file->ucontext, &cq_lock_class);
  1106. down_write(&obj->uobject.mutex);
  1107. if (cmd->comp_channel >= 0) {
  1108. ev_file = ib_uverbs_lookup_comp_file(cmd->comp_channel);
  1109. if (!ev_file) {
  1110. ret = -EINVAL;
  1111. goto err;
  1112. }
  1113. }
  1114. obj->uverbs_file = file;
  1115. obj->comp_events_reported = 0;
  1116. obj->async_events_reported = 0;
  1117. INIT_LIST_HEAD(&obj->comp_list);
  1118. INIT_LIST_HEAD(&obj->async_list);
  1119. attr.cqe = cmd->cqe;
  1120. attr.comp_vector = cmd->comp_vector;
  1121. if (cmd_sz > offsetof(typeof(*cmd), flags) + sizeof(cmd->flags))
  1122. attr.flags = cmd->flags;
  1123. cq = ib_dev->create_cq(ib_dev, &attr,
  1124. file->ucontext, uhw);
  1125. if (IS_ERR(cq)) {
  1126. ret = PTR_ERR(cq);
  1127. goto err_file;
  1128. }
  1129. cq->device = ib_dev;
  1130. cq->uobject = &obj->uobject;
  1131. cq->comp_handler = ib_uverbs_comp_handler;
  1132. cq->event_handler = ib_uverbs_cq_event_handler;
  1133. cq->cq_context = ev_file;
  1134. atomic_set(&cq->usecnt, 0);
  1135. obj->uobject.object = cq;
  1136. ret = idr_add_uobj(&ib_uverbs_cq_idr, &obj->uobject);
  1137. if (ret)
  1138. goto err_free;
  1139. memset(&resp, 0, sizeof resp);
  1140. resp.base.cq_handle = obj->uobject.id;
  1141. resp.base.cqe = cq->cqe;
  1142. resp.response_length = offsetof(typeof(resp), response_length) +
  1143. sizeof(resp.response_length);
  1144. ret = cb(file, obj, &resp, ucore, context);
  1145. if (ret)
  1146. goto err_cb;
  1147. mutex_lock(&file->mutex);
  1148. list_add_tail(&obj->uobject.list, &file->ucontext->cq_list);
  1149. mutex_unlock(&file->mutex);
  1150. obj->uobject.live = 1;
  1151. up_write(&obj->uobject.mutex);
  1152. return obj;
  1153. err_cb:
  1154. idr_remove_uobj(&ib_uverbs_cq_idr, &obj->uobject);
  1155. err_free:
  1156. ib_destroy_cq(cq);
  1157. err_file:
  1158. if (ev_file)
  1159. ib_uverbs_release_ucq(file, ev_file, obj);
  1160. err:
  1161. put_uobj_write(&obj->uobject);
  1162. return ERR_PTR(ret);
  1163. }
  1164. static int ib_uverbs_create_cq_cb(struct ib_uverbs_file *file,
  1165. struct ib_ucq_object *obj,
  1166. struct ib_uverbs_ex_create_cq_resp *resp,
  1167. struct ib_udata *ucore, void *context)
  1168. {
  1169. if (ib_copy_to_udata(ucore, &resp->base, sizeof(resp->base)))
  1170. return -EFAULT;
  1171. return 0;
  1172. }
  1173. ssize_t ib_uverbs_create_cq(struct ib_uverbs_file *file,
  1174. struct ib_device *ib_dev,
  1175. const char __user *buf, int in_len,
  1176. int out_len)
  1177. {
  1178. struct ib_uverbs_create_cq cmd;
  1179. struct ib_uverbs_ex_create_cq cmd_ex;
  1180. struct ib_uverbs_create_cq_resp resp;
  1181. struct ib_udata ucore;
  1182. struct ib_udata uhw;
  1183. struct ib_ucq_object *obj;
  1184. if (out_len < sizeof(resp))
  1185. return -ENOSPC;
  1186. if (copy_from_user(&cmd, buf, sizeof(cmd)))
  1187. return -EFAULT;
  1188. INIT_UDATA(&ucore, buf, (unsigned long)cmd.response, sizeof(cmd), sizeof(resp));
  1189. INIT_UDATA(&uhw, buf + sizeof(cmd),
  1190. (unsigned long)cmd.response + sizeof(resp),
  1191. in_len - sizeof(cmd), out_len - sizeof(resp));
  1192. memset(&cmd_ex, 0, sizeof(cmd_ex));
  1193. cmd_ex.user_handle = cmd.user_handle;
  1194. cmd_ex.cqe = cmd.cqe;
  1195. cmd_ex.comp_vector = cmd.comp_vector;
  1196. cmd_ex.comp_channel = cmd.comp_channel;
  1197. obj = create_cq(file, ib_dev, &ucore, &uhw, &cmd_ex,
  1198. offsetof(typeof(cmd_ex), comp_channel) +
  1199. sizeof(cmd.comp_channel), ib_uverbs_create_cq_cb,
  1200. NULL);
  1201. if (IS_ERR(obj))
  1202. return PTR_ERR(obj);
  1203. return in_len;
  1204. }
  1205. static int ib_uverbs_ex_create_cq_cb(struct ib_uverbs_file *file,
  1206. struct ib_ucq_object *obj,
  1207. struct ib_uverbs_ex_create_cq_resp *resp,
  1208. struct ib_udata *ucore, void *context)
  1209. {
  1210. if (ib_copy_to_udata(ucore, resp, resp->response_length))
  1211. return -EFAULT;
  1212. return 0;
  1213. }
  1214. int ib_uverbs_ex_create_cq(struct ib_uverbs_file *file,
  1215. struct ib_device *ib_dev,
  1216. struct ib_udata *ucore,
  1217. struct ib_udata *uhw)
  1218. {
  1219. struct ib_uverbs_ex_create_cq_resp resp;
  1220. struct ib_uverbs_ex_create_cq cmd;
  1221. struct ib_ucq_object *obj;
  1222. int err;
  1223. if (ucore->inlen < sizeof(cmd))
  1224. return -EINVAL;
  1225. err = ib_copy_from_udata(&cmd, ucore, sizeof(cmd));
  1226. if (err)
  1227. return err;
  1228. if (cmd.comp_mask)
  1229. return -EINVAL;
  1230. if (cmd.reserved)
  1231. return -EINVAL;
  1232. if (ucore->outlen < (offsetof(typeof(resp), response_length) +
  1233. sizeof(resp.response_length)))
  1234. return -ENOSPC;
  1235. obj = create_cq(file, ib_dev, ucore, uhw, &cmd,
  1236. min(ucore->inlen, sizeof(cmd)),
  1237. ib_uverbs_ex_create_cq_cb, NULL);
  1238. if (IS_ERR(obj))
  1239. return PTR_ERR(obj);
  1240. return 0;
  1241. }
  1242. ssize_t ib_uverbs_resize_cq(struct ib_uverbs_file *file,
  1243. struct ib_device *ib_dev,
  1244. const char __user *buf, int in_len,
  1245. int out_len)
  1246. {
  1247. struct ib_uverbs_resize_cq cmd;
  1248. struct ib_uverbs_resize_cq_resp resp;
  1249. struct ib_udata udata;
  1250. struct ib_cq *cq;
  1251. int ret = -EINVAL;
  1252. if (copy_from_user(&cmd, buf, sizeof cmd))
  1253. return -EFAULT;
  1254. INIT_UDATA(&udata, buf + sizeof cmd,
  1255. (unsigned long) cmd.response + sizeof resp,
  1256. in_len - sizeof cmd, out_len - sizeof resp);
  1257. cq = idr_read_cq(cmd.cq_handle, file->ucontext, 0);
  1258. if (!cq)
  1259. return -EINVAL;
  1260. ret = cq->device->resize_cq(cq, cmd.cqe, &udata);
  1261. if (ret)
  1262. goto out;
  1263. resp.cqe = cq->cqe;
  1264. if (copy_to_user((void __user *) (unsigned long) cmd.response,
  1265. &resp, sizeof resp.cqe))
  1266. ret = -EFAULT;
  1267. out:
  1268. put_cq_read(cq);
  1269. return ret ? ret : in_len;
  1270. }
  1271. static int copy_wc_to_user(void __user *dest, struct ib_wc *wc)
  1272. {
  1273. struct ib_uverbs_wc tmp;
  1274. tmp.wr_id = wc->wr_id;
  1275. tmp.status = wc->status;
  1276. tmp.opcode = wc->opcode;
  1277. tmp.vendor_err = wc->vendor_err;
  1278. tmp.byte_len = wc->byte_len;
  1279. tmp.ex.imm_data = (__u32 __force) wc->ex.imm_data;
  1280. tmp.qp_num = wc->qp->qp_num;
  1281. tmp.src_qp = wc->src_qp;
  1282. tmp.wc_flags = wc->wc_flags;
  1283. tmp.pkey_index = wc->pkey_index;
  1284. tmp.slid = wc->slid;
  1285. tmp.sl = wc->sl;
  1286. tmp.dlid_path_bits = wc->dlid_path_bits;
  1287. tmp.port_num = wc->port_num;
  1288. tmp.reserved = 0;
  1289. if (copy_to_user(dest, &tmp, sizeof tmp))
  1290. return -EFAULT;
  1291. return 0;
  1292. }
  1293. ssize_t ib_uverbs_poll_cq(struct ib_uverbs_file *file,
  1294. struct ib_device *ib_dev,
  1295. const char __user *buf, int in_len,
  1296. int out_len)
  1297. {
  1298. struct ib_uverbs_poll_cq cmd;
  1299. struct ib_uverbs_poll_cq_resp resp;
  1300. u8 __user *header_ptr;
  1301. u8 __user *data_ptr;
  1302. struct ib_cq *cq;
  1303. struct ib_wc wc;
  1304. int ret;
  1305. if (copy_from_user(&cmd, buf, sizeof cmd))
  1306. return -EFAULT;
  1307. cq = idr_read_cq(cmd.cq_handle, file->ucontext, 0);
  1308. if (!cq)
  1309. return -EINVAL;
  1310. /* we copy a struct ib_uverbs_poll_cq_resp to user space */
  1311. header_ptr = (void __user *)(unsigned long) cmd.response;
  1312. data_ptr = header_ptr + sizeof resp;
  1313. memset(&resp, 0, sizeof resp);
  1314. while (resp.count < cmd.ne) {
  1315. ret = ib_poll_cq(cq, 1, &wc);
  1316. if (ret < 0)
  1317. goto out_put;
  1318. if (!ret)
  1319. break;
  1320. ret = copy_wc_to_user(data_ptr, &wc);
  1321. if (ret)
  1322. goto out_put;
  1323. data_ptr += sizeof(struct ib_uverbs_wc);
  1324. ++resp.count;
  1325. }
  1326. if (copy_to_user(header_ptr, &resp, sizeof resp)) {
  1327. ret = -EFAULT;
  1328. goto out_put;
  1329. }
  1330. ret = in_len;
  1331. out_put:
  1332. put_cq_read(cq);
  1333. return ret;
  1334. }
  1335. ssize_t ib_uverbs_req_notify_cq(struct ib_uverbs_file *file,
  1336. struct ib_device *ib_dev,
  1337. const char __user *buf, int in_len,
  1338. int out_len)
  1339. {
  1340. struct ib_uverbs_req_notify_cq cmd;
  1341. struct ib_cq *cq;
  1342. if (copy_from_user(&cmd, buf, sizeof cmd))
  1343. return -EFAULT;
  1344. cq = idr_read_cq(cmd.cq_handle, file->ucontext, 0);
  1345. if (!cq)
  1346. return -EINVAL;
  1347. ib_req_notify_cq(cq, cmd.solicited_only ?
  1348. IB_CQ_SOLICITED : IB_CQ_NEXT_COMP);
  1349. put_cq_read(cq);
  1350. return in_len;
  1351. }
  1352. ssize_t ib_uverbs_destroy_cq(struct ib_uverbs_file *file,
  1353. struct ib_device *ib_dev,
  1354. const char __user *buf, int in_len,
  1355. int out_len)
  1356. {
  1357. struct ib_uverbs_destroy_cq cmd;
  1358. struct ib_uverbs_destroy_cq_resp resp;
  1359. struct ib_uobject *uobj;
  1360. struct ib_cq *cq;
  1361. struct ib_ucq_object *obj;
  1362. struct ib_uverbs_event_file *ev_file;
  1363. int ret = -EINVAL;
  1364. if (copy_from_user(&cmd, buf, sizeof cmd))
  1365. return -EFAULT;
  1366. uobj = idr_write_uobj(&ib_uverbs_cq_idr, cmd.cq_handle, file->ucontext);
  1367. if (!uobj)
  1368. return -EINVAL;
  1369. cq = uobj->object;
  1370. ev_file = cq->cq_context;
  1371. obj = container_of(cq->uobject, struct ib_ucq_object, uobject);
  1372. ret = ib_destroy_cq(cq);
  1373. if (!ret)
  1374. uobj->live = 0;
  1375. put_uobj_write(uobj);
  1376. if (ret)
  1377. return ret;
  1378. idr_remove_uobj(&ib_uverbs_cq_idr, uobj);
  1379. mutex_lock(&file->mutex);
  1380. list_del(&uobj->list);
  1381. mutex_unlock(&file->mutex);
  1382. ib_uverbs_release_ucq(file, ev_file, obj);
  1383. memset(&resp, 0, sizeof resp);
  1384. resp.comp_events_reported = obj->comp_events_reported;
  1385. resp.async_events_reported = obj->async_events_reported;
  1386. put_uobj(uobj);
  1387. if (copy_to_user((void __user *) (unsigned long) cmd.response,
  1388. &resp, sizeof resp))
  1389. return -EFAULT;
  1390. return in_len;
  1391. }
  1392. static int create_qp(struct ib_uverbs_file *file,
  1393. struct ib_udata *ucore,
  1394. struct ib_udata *uhw,
  1395. struct ib_uverbs_ex_create_qp *cmd,
  1396. size_t cmd_sz,
  1397. int (*cb)(struct ib_uverbs_file *file,
  1398. struct ib_uverbs_ex_create_qp_resp *resp,
  1399. struct ib_udata *udata),
  1400. void *context)
  1401. {
  1402. struct ib_uqp_object *obj;
  1403. struct ib_device *device;
  1404. struct ib_pd *pd = NULL;
  1405. struct ib_xrcd *xrcd = NULL;
  1406. struct ib_uobject *uninitialized_var(xrcd_uobj);
  1407. struct ib_cq *scq = NULL, *rcq = NULL;
  1408. struct ib_srq *srq = NULL;
  1409. struct ib_qp *qp;
  1410. char *buf;
  1411. struct ib_qp_init_attr attr;
  1412. struct ib_uverbs_ex_create_qp_resp resp;
  1413. int ret;
  1414. if (cmd->qp_type == IB_QPT_RAW_PACKET && !capable(CAP_NET_RAW))
  1415. return -EPERM;
  1416. obj = kzalloc(sizeof *obj, GFP_KERNEL);
  1417. if (!obj)
  1418. return -ENOMEM;
  1419. init_uobj(&obj->uevent.uobject, cmd->user_handle, file->ucontext,
  1420. &qp_lock_class);
  1421. down_write(&obj->uevent.uobject.mutex);
  1422. if (cmd->qp_type == IB_QPT_XRC_TGT) {
  1423. xrcd = idr_read_xrcd(cmd->pd_handle, file->ucontext,
  1424. &xrcd_uobj);
  1425. if (!xrcd) {
  1426. ret = -EINVAL;
  1427. goto err_put;
  1428. }
  1429. device = xrcd->device;
  1430. } else {
  1431. if (cmd->qp_type == IB_QPT_XRC_INI) {
  1432. cmd->max_recv_wr = 0;
  1433. cmd->max_recv_sge = 0;
  1434. } else {
  1435. if (cmd->is_srq) {
  1436. srq = idr_read_srq(cmd->srq_handle,
  1437. file->ucontext);
  1438. if (!srq || srq->srq_type != IB_SRQT_BASIC) {
  1439. ret = -EINVAL;
  1440. goto err_put;
  1441. }
  1442. }
  1443. if (cmd->recv_cq_handle != cmd->send_cq_handle) {
  1444. rcq = idr_read_cq(cmd->recv_cq_handle,
  1445. file->ucontext, 0);
  1446. if (!rcq) {
  1447. ret = -EINVAL;
  1448. goto err_put;
  1449. }
  1450. }
  1451. }
  1452. scq = idr_read_cq(cmd->send_cq_handle, file->ucontext, !!rcq);
  1453. rcq = rcq ?: scq;
  1454. pd = idr_read_pd(cmd->pd_handle, file->ucontext);
  1455. if (!pd || !scq) {
  1456. ret = -EINVAL;
  1457. goto err_put;
  1458. }
  1459. device = pd->device;
  1460. }
  1461. attr.event_handler = ib_uverbs_qp_event_handler;
  1462. attr.qp_context = file;
  1463. attr.send_cq = scq;
  1464. attr.recv_cq = rcq;
  1465. attr.srq = srq;
  1466. attr.xrcd = xrcd;
  1467. attr.sq_sig_type = cmd->sq_sig_all ? IB_SIGNAL_ALL_WR :
  1468. IB_SIGNAL_REQ_WR;
  1469. attr.qp_type = cmd->qp_type;
  1470. attr.create_flags = 0;
  1471. attr.cap.max_send_wr = cmd->max_send_wr;
  1472. attr.cap.max_recv_wr = cmd->max_recv_wr;
  1473. attr.cap.max_send_sge = cmd->max_send_sge;
  1474. attr.cap.max_recv_sge = cmd->max_recv_sge;
  1475. attr.cap.max_inline_data = cmd->max_inline_data;
  1476. obj->uevent.events_reported = 0;
  1477. INIT_LIST_HEAD(&obj->uevent.event_list);
  1478. INIT_LIST_HEAD(&obj->mcast_list);
  1479. if (cmd_sz >= offsetof(typeof(*cmd), create_flags) +
  1480. sizeof(cmd->create_flags))
  1481. attr.create_flags = cmd->create_flags;
  1482. if (attr.create_flags & ~IB_QP_CREATE_BLOCK_MULTICAST_LOOPBACK) {
  1483. ret = -EINVAL;
  1484. goto err_put;
  1485. }
  1486. buf = (void *)cmd + sizeof(*cmd);
  1487. if (cmd_sz > sizeof(*cmd))
  1488. if (!(buf[0] == 0 && !memcmp(buf, buf + 1,
  1489. cmd_sz - sizeof(*cmd) - 1))) {
  1490. ret = -EINVAL;
  1491. goto err_put;
  1492. }
  1493. if (cmd->qp_type == IB_QPT_XRC_TGT)
  1494. qp = ib_create_qp(pd, &attr);
  1495. else
  1496. qp = device->create_qp(pd, &attr, uhw);
  1497. if (IS_ERR(qp)) {
  1498. ret = PTR_ERR(qp);
  1499. goto err_put;
  1500. }
  1501. if (cmd->qp_type != IB_QPT_XRC_TGT) {
  1502. qp->real_qp = qp;
  1503. qp->device = device;
  1504. qp->pd = pd;
  1505. qp->send_cq = attr.send_cq;
  1506. qp->recv_cq = attr.recv_cq;
  1507. qp->srq = attr.srq;
  1508. qp->event_handler = attr.event_handler;
  1509. qp->qp_context = attr.qp_context;
  1510. qp->qp_type = attr.qp_type;
  1511. atomic_set(&qp->usecnt, 0);
  1512. atomic_inc(&pd->usecnt);
  1513. atomic_inc(&attr.send_cq->usecnt);
  1514. if (attr.recv_cq)
  1515. atomic_inc(&attr.recv_cq->usecnt);
  1516. if (attr.srq)
  1517. atomic_inc(&attr.srq->usecnt);
  1518. }
  1519. qp->uobject = &obj->uevent.uobject;
  1520. obj->uevent.uobject.object = qp;
  1521. ret = idr_add_uobj(&ib_uverbs_qp_idr, &obj->uevent.uobject);
  1522. if (ret)
  1523. goto err_destroy;
  1524. memset(&resp, 0, sizeof resp);
  1525. resp.base.qpn = qp->qp_num;
  1526. resp.base.qp_handle = obj->uevent.uobject.id;
  1527. resp.base.max_recv_sge = attr.cap.max_recv_sge;
  1528. resp.base.max_send_sge = attr.cap.max_send_sge;
  1529. resp.base.max_recv_wr = attr.cap.max_recv_wr;
  1530. resp.base.max_send_wr = attr.cap.max_send_wr;
  1531. resp.base.max_inline_data = attr.cap.max_inline_data;
  1532. resp.response_length = offsetof(typeof(resp), response_length) +
  1533. sizeof(resp.response_length);
  1534. ret = cb(file, &resp, ucore);
  1535. if (ret)
  1536. goto err_cb;
  1537. if (xrcd) {
  1538. obj->uxrcd = container_of(xrcd_uobj, struct ib_uxrcd_object,
  1539. uobject);
  1540. atomic_inc(&obj->uxrcd->refcnt);
  1541. put_xrcd_read(xrcd_uobj);
  1542. }
  1543. if (pd)
  1544. put_pd_read(pd);
  1545. if (scq)
  1546. put_cq_read(scq);
  1547. if (rcq && rcq != scq)
  1548. put_cq_read(rcq);
  1549. if (srq)
  1550. put_srq_read(srq);
  1551. mutex_lock(&file->mutex);
  1552. list_add_tail(&obj->uevent.uobject.list, &file->ucontext->qp_list);
  1553. mutex_unlock(&file->mutex);
  1554. obj->uevent.uobject.live = 1;
  1555. up_write(&obj->uevent.uobject.mutex);
  1556. return 0;
  1557. err_cb:
  1558. idr_remove_uobj(&ib_uverbs_qp_idr, &obj->uevent.uobject);
  1559. err_destroy:
  1560. ib_destroy_qp(qp);
  1561. err_put:
  1562. if (xrcd)
  1563. put_xrcd_read(xrcd_uobj);
  1564. if (pd)
  1565. put_pd_read(pd);
  1566. if (scq)
  1567. put_cq_read(scq);
  1568. if (rcq && rcq != scq)
  1569. put_cq_read(rcq);
  1570. if (srq)
  1571. put_srq_read(srq);
  1572. put_uobj_write(&obj->uevent.uobject);
  1573. return ret;
  1574. }
  1575. static int ib_uverbs_create_qp_cb(struct ib_uverbs_file *file,
  1576. struct ib_uverbs_ex_create_qp_resp *resp,
  1577. struct ib_udata *ucore)
  1578. {
  1579. if (ib_copy_to_udata(ucore, &resp->base, sizeof(resp->base)))
  1580. return -EFAULT;
  1581. return 0;
  1582. }
  1583. ssize_t ib_uverbs_create_qp(struct ib_uverbs_file *file,
  1584. struct ib_device *ib_dev,
  1585. const char __user *buf, int in_len,
  1586. int out_len)
  1587. {
  1588. struct ib_uverbs_create_qp cmd;
  1589. struct ib_uverbs_ex_create_qp cmd_ex;
  1590. struct ib_udata ucore;
  1591. struct ib_udata uhw;
  1592. ssize_t resp_size = sizeof(struct ib_uverbs_create_qp_resp);
  1593. int err;
  1594. if (out_len < resp_size)
  1595. return -ENOSPC;
  1596. if (copy_from_user(&cmd, buf, sizeof(cmd)))
  1597. return -EFAULT;
  1598. INIT_UDATA(&ucore, buf, (unsigned long)cmd.response, sizeof(cmd),
  1599. resp_size);
  1600. INIT_UDATA(&uhw, buf + sizeof(cmd),
  1601. (unsigned long)cmd.response + resp_size,
  1602. in_len - sizeof(cmd), out_len - resp_size);
  1603. memset(&cmd_ex, 0, sizeof(cmd_ex));
  1604. cmd_ex.user_handle = cmd.user_handle;
  1605. cmd_ex.pd_handle = cmd.pd_handle;
  1606. cmd_ex.send_cq_handle = cmd.send_cq_handle;
  1607. cmd_ex.recv_cq_handle = cmd.recv_cq_handle;
  1608. cmd_ex.srq_handle = cmd.srq_handle;
  1609. cmd_ex.max_send_wr = cmd.max_send_wr;
  1610. cmd_ex.max_recv_wr = cmd.max_recv_wr;
  1611. cmd_ex.max_send_sge = cmd.max_send_sge;
  1612. cmd_ex.max_recv_sge = cmd.max_recv_sge;
  1613. cmd_ex.max_inline_data = cmd.max_inline_data;
  1614. cmd_ex.sq_sig_all = cmd.sq_sig_all;
  1615. cmd_ex.qp_type = cmd.qp_type;
  1616. cmd_ex.is_srq = cmd.is_srq;
  1617. err = create_qp(file, &ucore, &uhw, &cmd_ex,
  1618. offsetof(typeof(cmd_ex), is_srq) +
  1619. sizeof(cmd.is_srq), ib_uverbs_create_qp_cb,
  1620. NULL);
  1621. if (err)
  1622. return err;
  1623. return in_len;
  1624. }
  1625. static int ib_uverbs_ex_create_qp_cb(struct ib_uverbs_file *file,
  1626. struct ib_uverbs_ex_create_qp_resp *resp,
  1627. struct ib_udata *ucore)
  1628. {
  1629. if (ib_copy_to_udata(ucore, resp, resp->response_length))
  1630. return -EFAULT;
  1631. return 0;
  1632. }
  1633. int ib_uverbs_ex_create_qp(struct ib_uverbs_file *file,
  1634. struct ib_device *ib_dev,
  1635. struct ib_udata *ucore,
  1636. struct ib_udata *uhw)
  1637. {
  1638. struct ib_uverbs_ex_create_qp_resp resp;
  1639. struct ib_uverbs_ex_create_qp cmd = {0};
  1640. int err;
  1641. if (ucore->inlen < (offsetof(typeof(cmd), comp_mask) +
  1642. sizeof(cmd.comp_mask)))
  1643. return -EINVAL;
  1644. err = ib_copy_from_udata(&cmd, ucore, min(sizeof(cmd), ucore->inlen));
  1645. if (err)
  1646. return err;
  1647. if (cmd.comp_mask)
  1648. return -EINVAL;
  1649. if (cmd.reserved)
  1650. return -EINVAL;
  1651. if (ucore->outlen < (offsetof(typeof(resp), response_length) +
  1652. sizeof(resp.response_length)))
  1653. return -ENOSPC;
  1654. err = create_qp(file, ucore, uhw, &cmd,
  1655. min(ucore->inlen, sizeof(cmd)),
  1656. ib_uverbs_ex_create_qp_cb, NULL);
  1657. if (err)
  1658. return err;
  1659. return 0;
  1660. }
  1661. ssize_t ib_uverbs_open_qp(struct ib_uverbs_file *file,
  1662. struct ib_device *ib_dev,
  1663. const char __user *buf, int in_len, int out_len)
  1664. {
  1665. struct ib_uverbs_open_qp cmd;
  1666. struct ib_uverbs_create_qp_resp resp;
  1667. struct ib_udata udata;
  1668. struct ib_uqp_object *obj;
  1669. struct ib_xrcd *xrcd;
  1670. struct ib_uobject *uninitialized_var(xrcd_uobj);
  1671. struct ib_qp *qp;
  1672. struct ib_qp_open_attr attr;
  1673. int ret;
  1674. if (out_len < sizeof resp)
  1675. return -ENOSPC;
  1676. if (copy_from_user(&cmd, buf, sizeof cmd))
  1677. return -EFAULT;
  1678. INIT_UDATA(&udata, buf + sizeof cmd,
  1679. (unsigned long) cmd.response + sizeof resp,
  1680. in_len - sizeof cmd, out_len - sizeof resp);
  1681. obj = kmalloc(sizeof *obj, GFP_KERNEL);
  1682. if (!obj)
  1683. return -ENOMEM;
  1684. init_uobj(&obj->uevent.uobject, cmd.user_handle, file->ucontext, &qp_lock_class);
  1685. down_write(&obj->uevent.uobject.mutex);
  1686. xrcd = idr_read_xrcd(cmd.pd_handle, file->ucontext, &xrcd_uobj);
  1687. if (!xrcd) {
  1688. ret = -EINVAL;
  1689. goto err_put;
  1690. }
  1691. attr.event_handler = ib_uverbs_qp_event_handler;
  1692. attr.qp_context = file;
  1693. attr.qp_num = cmd.qpn;
  1694. attr.qp_type = cmd.qp_type;
  1695. obj->uevent.events_reported = 0;
  1696. INIT_LIST_HEAD(&obj->uevent.event_list);
  1697. INIT_LIST_HEAD(&obj->mcast_list);
  1698. qp = ib_open_qp(xrcd, &attr);
  1699. if (IS_ERR(qp)) {
  1700. ret = PTR_ERR(qp);
  1701. goto err_put;
  1702. }
  1703. qp->uobject = &obj->uevent.uobject;
  1704. obj->uevent.uobject.object = qp;
  1705. ret = idr_add_uobj(&ib_uverbs_qp_idr, &obj->uevent.uobject);
  1706. if (ret)
  1707. goto err_destroy;
  1708. memset(&resp, 0, sizeof resp);
  1709. resp.qpn = qp->qp_num;
  1710. resp.qp_handle = obj->uevent.uobject.id;
  1711. if (copy_to_user((void __user *) (unsigned long) cmd.response,
  1712. &resp, sizeof resp)) {
  1713. ret = -EFAULT;
  1714. goto err_remove;
  1715. }
  1716. obj->uxrcd = container_of(xrcd_uobj, struct ib_uxrcd_object, uobject);
  1717. atomic_inc(&obj->uxrcd->refcnt);
  1718. put_xrcd_read(xrcd_uobj);
  1719. mutex_lock(&file->mutex);
  1720. list_add_tail(&obj->uevent.uobject.list, &file->ucontext->qp_list);
  1721. mutex_unlock(&file->mutex);
  1722. obj->uevent.uobject.live = 1;
  1723. up_write(&obj->uevent.uobject.mutex);
  1724. return in_len;
  1725. err_remove:
  1726. idr_remove_uobj(&ib_uverbs_qp_idr, &obj->uevent.uobject);
  1727. err_destroy:
  1728. ib_destroy_qp(qp);
  1729. err_put:
  1730. put_xrcd_read(xrcd_uobj);
  1731. put_uobj_write(&obj->uevent.uobject);
  1732. return ret;
  1733. }
  1734. ssize_t ib_uverbs_query_qp(struct ib_uverbs_file *file,
  1735. struct ib_device *ib_dev,
  1736. const char __user *buf, int in_len,
  1737. int out_len)
  1738. {
  1739. struct ib_uverbs_query_qp cmd;
  1740. struct ib_uverbs_query_qp_resp resp;
  1741. struct ib_qp *qp;
  1742. struct ib_qp_attr *attr;
  1743. struct ib_qp_init_attr *init_attr;
  1744. int ret;
  1745. if (copy_from_user(&cmd, buf, sizeof cmd))
  1746. return -EFAULT;
  1747. attr = kmalloc(sizeof *attr, GFP_KERNEL);
  1748. init_attr = kmalloc(sizeof *init_attr, GFP_KERNEL);
  1749. if (!attr || !init_attr) {
  1750. ret = -ENOMEM;
  1751. goto out;
  1752. }
  1753. qp = idr_read_qp(cmd.qp_handle, file->ucontext);
  1754. if (!qp) {
  1755. ret = -EINVAL;
  1756. goto out;
  1757. }
  1758. ret = ib_query_qp(qp, attr, cmd.attr_mask, init_attr);
  1759. put_qp_read(qp);
  1760. if (ret)
  1761. goto out;
  1762. memset(&resp, 0, sizeof resp);
  1763. resp.qp_state = attr->qp_state;
  1764. resp.cur_qp_state = attr->cur_qp_state;
  1765. resp.path_mtu = attr->path_mtu;
  1766. resp.path_mig_state = attr->path_mig_state;
  1767. resp.qkey = attr->qkey;
  1768. resp.rq_psn = attr->rq_psn;
  1769. resp.sq_psn = attr->sq_psn;
  1770. resp.dest_qp_num = attr->dest_qp_num;
  1771. resp.qp_access_flags = attr->qp_access_flags;
  1772. resp.pkey_index = attr->pkey_index;
  1773. resp.alt_pkey_index = attr->alt_pkey_index;
  1774. resp.sq_draining = attr->sq_draining;
  1775. resp.max_rd_atomic = attr->max_rd_atomic;
  1776. resp.max_dest_rd_atomic = attr->max_dest_rd_atomic;
  1777. resp.min_rnr_timer = attr->min_rnr_timer;
  1778. resp.port_num = attr->port_num;
  1779. resp.timeout = attr->timeout;
  1780. resp.retry_cnt = attr->retry_cnt;
  1781. resp.rnr_retry = attr->rnr_retry;
  1782. resp.alt_port_num = attr->alt_port_num;
  1783. resp.alt_timeout = attr->alt_timeout;
  1784. memcpy(resp.dest.dgid, attr->ah_attr.grh.dgid.raw, 16);
  1785. resp.dest.flow_label = attr->ah_attr.grh.flow_label;
  1786. resp.dest.sgid_index = attr->ah_attr.grh.sgid_index;
  1787. resp.dest.hop_limit = attr->ah_attr.grh.hop_limit;
  1788. resp.dest.traffic_class = attr->ah_attr.grh.traffic_class;
  1789. resp.dest.dlid = attr->ah_attr.dlid;
  1790. resp.dest.sl = attr->ah_attr.sl;
  1791. resp.dest.src_path_bits = attr->ah_attr.src_path_bits;
  1792. resp.dest.static_rate = attr->ah_attr.static_rate;
  1793. resp.dest.is_global = !!(attr->ah_attr.ah_flags & IB_AH_GRH);
  1794. resp.dest.port_num = attr->ah_attr.port_num;
  1795. memcpy(resp.alt_dest.dgid, attr->alt_ah_attr.grh.dgid.raw, 16);
  1796. resp.alt_dest.flow_label = attr->alt_ah_attr.grh.flow_label;
  1797. resp.alt_dest.sgid_index = attr->alt_ah_attr.grh.sgid_index;
  1798. resp.alt_dest.hop_limit = attr->alt_ah_attr.grh.hop_limit;
  1799. resp.alt_dest.traffic_class = attr->alt_ah_attr.grh.traffic_class;
  1800. resp.alt_dest.dlid = attr->alt_ah_attr.dlid;
  1801. resp.alt_dest.sl = attr->alt_ah_attr.sl;
  1802. resp.alt_dest.src_path_bits = attr->alt_ah_attr.src_path_bits;
  1803. resp.alt_dest.static_rate = attr->alt_ah_attr.static_rate;
  1804. resp.alt_dest.is_global = !!(attr->alt_ah_attr.ah_flags & IB_AH_GRH);
  1805. resp.alt_dest.port_num = attr->alt_ah_attr.port_num;
  1806. resp.max_send_wr = init_attr->cap.max_send_wr;
  1807. resp.max_recv_wr = init_attr->cap.max_recv_wr;
  1808. resp.max_send_sge = init_attr->cap.max_send_sge;
  1809. resp.max_recv_sge = init_attr->cap.max_recv_sge;
  1810. resp.max_inline_data = init_attr->cap.max_inline_data;
  1811. resp.sq_sig_all = init_attr->sq_sig_type == IB_SIGNAL_ALL_WR;
  1812. if (copy_to_user((void __user *) (unsigned long) cmd.response,
  1813. &resp, sizeof resp))
  1814. ret = -EFAULT;
  1815. out:
  1816. kfree(attr);
  1817. kfree(init_attr);
  1818. return ret ? ret : in_len;
  1819. }
  1820. /* Remove ignored fields set in the attribute mask */
  1821. static int modify_qp_mask(enum ib_qp_type qp_type, int mask)
  1822. {
  1823. switch (qp_type) {
  1824. case IB_QPT_XRC_INI:
  1825. return mask & ~(IB_QP_MAX_DEST_RD_ATOMIC | IB_QP_MIN_RNR_TIMER);
  1826. case IB_QPT_XRC_TGT:
  1827. return mask & ~(IB_QP_MAX_QP_RD_ATOMIC | IB_QP_RETRY_CNT |
  1828. IB_QP_RNR_RETRY);
  1829. default:
  1830. return mask;
  1831. }
  1832. }
  1833. ssize_t ib_uverbs_modify_qp(struct ib_uverbs_file *file,
  1834. struct ib_device *ib_dev,
  1835. const char __user *buf, int in_len,
  1836. int out_len)
  1837. {
  1838. struct ib_uverbs_modify_qp cmd;
  1839. struct ib_udata udata;
  1840. struct ib_qp *qp;
  1841. struct ib_qp_attr *attr;
  1842. int ret;
  1843. if (copy_from_user(&cmd, buf, sizeof cmd))
  1844. return -EFAULT;
  1845. if ((cmd.attr_mask & IB_QP_PORT) &&
  1846. (cmd.port_num < rdma_start_port(ib_dev) ||
  1847. cmd.port_num > rdma_end_port(ib_dev)))
  1848. return -EINVAL;
  1849. INIT_UDATA(&udata, buf + sizeof cmd, NULL, in_len - sizeof cmd,
  1850. out_len);
  1851. attr = kmalloc(sizeof *attr, GFP_KERNEL);
  1852. if (!attr)
  1853. return -ENOMEM;
  1854. qp = idr_read_qp(cmd.qp_handle, file->ucontext);
  1855. if (!qp) {
  1856. ret = -EINVAL;
  1857. goto out;
  1858. }
  1859. attr->qp_state = cmd.qp_state;
  1860. attr->cur_qp_state = cmd.cur_qp_state;
  1861. attr->path_mtu = cmd.path_mtu;
  1862. attr->path_mig_state = cmd.path_mig_state;
  1863. attr->qkey = cmd.qkey;
  1864. attr->rq_psn = cmd.rq_psn;
  1865. attr->sq_psn = cmd.sq_psn;
  1866. attr->dest_qp_num = cmd.dest_qp_num;
  1867. attr->qp_access_flags = cmd.qp_access_flags;
  1868. attr->pkey_index = cmd.pkey_index;
  1869. attr->alt_pkey_index = cmd.alt_pkey_index;
  1870. attr->en_sqd_async_notify = cmd.en_sqd_async_notify;
  1871. attr->max_rd_atomic = cmd.max_rd_atomic;
  1872. attr->max_dest_rd_atomic = cmd.max_dest_rd_atomic;
  1873. attr->min_rnr_timer = cmd.min_rnr_timer;
  1874. attr->port_num = cmd.port_num;
  1875. attr->timeout = cmd.timeout;
  1876. attr->retry_cnt = cmd.retry_cnt;
  1877. attr->rnr_retry = cmd.rnr_retry;
  1878. attr->alt_port_num = cmd.alt_port_num;
  1879. attr->alt_timeout = cmd.alt_timeout;
  1880. memcpy(attr->ah_attr.grh.dgid.raw, cmd.dest.dgid, 16);
  1881. attr->ah_attr.grh.flow_label = cmd.dest.flow_label;
  1882. attr->ah_attr.grh.sgid_index = cmd.dest.sgid_index;
  1883. attr->ah_attr.grh.hop_limit = cmd.dest.hop_limit;
  1884. attr->ah_attr.grh.traffic_class = cmd.dest.traffic_class;
  1885. attr->ah_attr.dlid = cmd.dest.dlid;
  1886. attr->ah_attr.sl = cmd.dest.sl;
  1887. attr->ah_attr.src_path_bits = cmd.dest.src_path_bits;
  1888. attr->ah_attr.static_rate = cmd.dest.static_rate;
  1889. attr->ah_attr.ah_flags = cmd.dest.is_global ? IB_AH_GRH : 0;
  1890. attr->ah_attr.port_num = cmd.dest.port_num;
  1891. memcpy(attr->alt_ah_attr.grh.dgid.raw, cmd.alt_dest.dgid, 16);
  1892. attr->alt_ah_attr.grh.flow_label = cmd.alt_dest.flow_label;
  1893. attr->alt_ah_attr.grh.sgid_index = cmd.alt_dest.sgid_index;
  1894. attr->alt_ah_attr.grh.hop_limit = cmd.alt_dest.hop_limit;
  1895. attr->alt_ah_attr.grh.traffic_class = cmd.alt_dest.traffic_class;
  1896. attr->alt_ah_attr.dlid = cmd.alt_dest.dlid;
  1897. attr->alt_ah_attr.sl = cmd.alt_dest.sl;
  1898. attr->alt_ah_attr.src_path_bits = cmd.alt_dest.src_path_bits;
  1899. attr->alt_ah_attr.static_rate = cmd.alt_dest.static_rate;
  1900. attr->alt_ah_attr.ah_flags = cmd.alt_dest.is_global ? IB_AH_GRH : 0;
  1901. attr->alt_ah_attr.port_num = cmd.alt_dest.port_num;
  1902. if (qp->real_qp == qp) {
  1903. ret = ib_resolve_eth_dmac(qp, attr, &cmd.attr_mask);
  1904. if (ret)
  1905. goto release_qp;
  1906. ret = qp->device->modify_qp(qp, attr,
  1907. modify_qp_mask(qp->qp_type, cmd.attr_mask), &udata);
  1908. } else {
  1909. ret = ib_modify_qp(qp, attr, modify_qp_mask(qp->qp_type, cmd.attr_mask));
  1910. }
  1911. if (ret)
  1912. goto release_qp;
  1913. ret = in_len;
  1914. release_qp:
  1915. put_qp_read(qp);
  1916. out:
  1917. kfree(attr);
  1918. return ret;
  1919. }
  1920. ssize_t ib_uverbs_destroy_qp(struct ib_uverbs_file *file,
  1921. struct ib_device *ib_dev,
  1922. const char __user *buf, int in_len,
  1923. int out_len)
  1924. {
  1925. struct ib_uverbs_destroy_qp cmd;
  1926. struct ib_uverbs_destroy_qp_resp resp;
  1927. struct ib_uobject *uobj;
  1928. struct ib_qp *qp;
  1929. struct ib_uqp_object *obj;
  1930. int ret = -EINVAL;
  1931. if (copy_from_user(&cmd, buf, sizeof cmd))
  1932. return -EFAULT;
  1933. memset(&resp, 0, sizeof resp);
  1934. uobj = idr_write_uobj(&ib_uverbs_qp_idr, cmd.qp_handle, file->ucontext);
  1935. if (!uobj)
  1936. return -EINVAL;
  1937. qp = uobj->object;
  1938. obj = container_of(uobj, struct ib_uqp_object, uevent.uobject);
  1939. if (!list_empty(&obj->mcast_list)) {
  1940. put_uobj_write(uobj);
  1941. return -EBUSY;
  1942. }
  1943. ret = ib_destroy_qp(qp);
  1944. if (!ret)
  1945. uobj->live = 0;
  1946. put_uobj_write(uobj);
  1947. if (ret)
  1948. return ret;
  1949. if (obj->uxrcd)
  1950. atomic_dec(&obj->uxrcd->refcnt);
  1951. idr_remove_uobj(&ib_uverbs_qp_idr, uobj);
  1952. mutex_lock(&file->mutex);
  1953. list_del(&uobj->list);
  1954. mutex_unlock(&file->mutex);
  1955. ib_uverbs_release_uevent(file, &obj->uevent);
  1956. resp.events_reported = obj->uevent.events_reported;
  1957. put_uobj(uobj);
  1958. if (copy_to_user((void __user *) (unsigned long) cmd.response,
  1959. &resp, sizeof resp))
  1960. return -EFAULT;
  1961. return in_len;
  1962. }
  1963. static void *alloc_wr(size_t wr_size, __u32 num_sge)
  1964. {
  1965. if (num_sge >= (U32_MAX - ALIGN(wr_size, sizeof (struct ib_sge))) /
  1966. sizeof (struct ib_sge))
  1967. return NULL;
  1968. return kmalloc(ALIGN(wr_size, sizeof (struct ib_sge)) +
  1969. num_sge * sizeof (struct ib_sge), GFP_KERNEL);
  1970. }
  1971. ssize_t ib_uverbs_post_send(struct ib_uverbs_file *file,
  1972. struct ib_device *ib_dev,
  1973. const char __user *buf, int in_len,
  1974. int out_len)
  1975. {
  1976. struct ib_uverbs_post_send cmd;
  1977. struct ib_uverbs_post_send_resp resp;
  1978. struct ib_uverbs_send_wr *user_wr;
  1979. struct ib_send_wr *wr = NULL, *last, *next, *bad_wr;
  1980. struct ib_qp *qp;
  1981. int i, sg_ind;
  1982. int is_ud;
  1983. ssize_t ret = -EINVAL;
  1984. size_t next_size;
  1985. if (copy_from_user(&cmd, buf, sizeof cmd))
  1986. return -EFAULT;
  1987. if (in_len < sizeof cmd + cmd.wqe_size * cmd.wr_count +
  1988. cmd.sge_count * sizeof (struct ib_uverbs_sge))
  1989. return -EINVAL;
  1990. if (cmd.wqe_size < sizeof (struct ib_uverbs_send_wr))
  1991. return -EINVAL;
  1992. user_wr = kmalloc(cmd.wqe_size, GFP_KERNEL);
  1993. if (!user_wr)
  1994. return -ENOMEM;
  1995. qp = idr_read_qp(cmd.qp_handle, file->ucontext);
  1996. if (!qp)
  1997. goto out;
  1998. is_ud = qp->qp_type == IB_QPT_UD;
  1999. sg_ind = 0;
  2000. last = NULL;
  2001. for (i = 0; i < cmd.wr_count; ++i) {
  2002. if (copy_from_user(user_wr,
  2003. buf + sizeof cmd + i * cmd.wqe_size,
  2004. cmd.wqe_size)) {
  2005. ret = -EFAULT;
  2006. goto out_put;
  2007. }
  2008. if (user_wr->num_sge + sg_ind > cmd.sge_count) {
  2009. ret = -EINVAL;
  2010. goto out_put;
  2011. }
  2012. if (is_ud) {
  2013. struct ib_ud_wr *ud;
  2014. if (user_wr->opcode != IB_WR_SEND &&
  2015. user_wr->opcode != IB_WR_SEND_WITH_IMM) {
  2016. ret = -EINVAL;
  2017. goto out_put;
  2018. }
  2019. next_size = sizeof(*ud);
  2020. ud = alloc_wr(next_size, user_wr->num_sge);
  2021. if (!ud) {
  2022. ret = -ENOMEM;
  2023. goto out_put;
  2024. }
  2025. ud->ah = idr_read_ah(user_wr->wr.ud.ah, file->ucontext);
  2026. if (!ud->ah) {
  2027. kfree(ud);
  2028. ret = -EINVAL;
  2029. goto out_put;
  2030. }
  2031. ud->remote_qpn = user_wr->wr.ud.remote_qpn;
  2032. ud->remote_qkey = user_wr->wr.ud.remote_qkey;
  2033. next = &ud->wr;
  2034. } else if (user_wr->opcode == IB_WR_RDMA_WRITE_WITH_IMM ||
  2035. user_wr->opcode == IB_WR_RDMA_WRITE ||
  2036. user_wr->opcode == IB_WR_RDMA_READ) {
  2037. struct ib_rdma_wr *rdma;
  2038. next_size = sizeof(*rdma);
  2039. rdma = alloc_wr(next_size, user_wr->num_sge);
  2040. if (!rdma) {
  2041. ret = -ENOMEM;
  2042. goto out_put;
  2043. }
  2044. rdma->remote_addr = user_wr->wr.rdma.remote_addr;
  2045. rdma->rkey = user_wr->wr.rdma.rkey;
  2046. next = &rdma->wr;
  2047. } else if (user_wr->opcode == IB_WR_ATOMIC_CMP_AND_SWP ||
  2048. user_wr->opcode == IB_WR_ATOMIC_FETCH_AND_ADD) {
  2049. struct ib_atomic_wr *atomic;
  2050. next_size = sizeof(*atomic);
  2051. atomic = alloc_wr(next_size, user_wr->num_sge);
  2052. if (!atomic) {
  2053. ret = -ENOMEM;
  2054. goto out_put;
  2055. }
  2056. atomic->remote_addr = user_wr->wr.atomic.remote_addr;
  2057. atomic->compare_add = user_wr->wr.atomic.compare_add;
  2058. atomic->swap = user_wr->wr.atomic.swap;
  2059. atomic->rkey = user_wr->wr.atomic.rkey;
  2060. next = &atomic->wr;
  2061. } else if (user_wr->opcode == IB_WR_SEND ||
  2062. user_wr->opcode == IB_WR_SEND_WITH_IMM ||
  2063. user_wr->opcode == IB_WR_SEND_WITH_INV) {
  2064. next_size = sizeof(*next);
  2065. next = alloc_wr(next_size, user_wr->num_sge);
  2066. if (!next) {
  2067. ret = -ENOMEM;
  2068. goto out_put;
  2069. }
  2070. } else {
  2071. ret = -EINVAL;
  2072. goto out_put;
  2073. }
  2074. if (user_wr->opcode == IB_WR_SEND_WITH_IMM ||
  2075. user_wr->opcode == IB_WR_RDMA_WRITE_WITH_IMM) {
  2076. next->ex.imm_data =
  2077. (__be32 __force) user_wr->ex.imm_data;
  2078. } else if (user_wr->opcode == IB_WR_SEND_WITH_INV) {
  2079. next->ex.invalidate_rkey = user_wr->ex.invalidate_rkey;
  2080. }
  2081. if (!last)
  2082. wr = next;
  2083. else
  2084. last->next = next;
  2085. last = next;
  2086. next->next = NULL;
  2087. next->wr_id = user_wr->wr_id;
  2088. next->num_sge = user_wr->num_sge;
  2089. next->opcode = user_wr->opcode;
  2090. next->send_flags = user_wr->send_flags;
  2091. if (next->num_sge) {
  2092. next->sg_list = (void *) next +
  2093. ALIGN(next_size, sizeof(struct ib_sge));
  2094. if (copy_from_user(next->sg_list,
  2095. buf + sizeof cmd +
  2096. cmd.wr_count * cmd.wqe_size +
  2097. sg_ind * sizeof (struct ib_sge),
  2098. next->num_sge * sizeof (struct ib_sge))) {
  2099. ret = -EFAULT;
  2100. goto out_put;
  2101. }
  2102. sg_ind += next->num_sge;
  2103. } else
  2104. next->sg_list = NULL;
  2105. }
  2106. resp.bad_wr = 0;
  2107. ret = qp->device->post_send(qp->real_qp, wr, &bad_wr);
  2108. if (ret)
  2109. for (next = wr; next; next = next->next) {
  2110. ++resp.bad_wr;
  2111. if (next == bad_wr)
  2112. break;
  2113. }
  2114. if (copy_to_user((void __user *) (unsigned long) cmd.response,
  2115. &resp, sizeof resp))
  2116. ret = -EFAULT;
  2117. out_put:
  2118. put_qp_read(qp);
  2119. while (wr) {
  2120. if (is_ud && ud_wr(wr)->ah)
  2121. put_ah_read(ud_wr(wr)->ah);
  2122. next = wr->next;
  2123. kfree(wr);
  2124. wr = next;
  2125. }
  2126. out:
  2127. kfree(user_wr);
  2128. return ret ? ret : in_len;
  2129. }
  2130. static struct ib_recv_wr *ib_uverbs_unmarshall_recv(const char __user *buf,
  2131. int in_len,
  2132. u32 wr_count,
  2133. u32 sge_count,
  2134. u32 wqe_size)
  2135. {
  2136. struct ib_uverbs_recv_wr *user_wr;
  2137. struct ib_recv_wr *wr = NULL, *last, *next;
  2138. int sg_ind;
  2139. int i;
  2140. int ret;
  2141. if (in_len < wqe_size * wr_count +
  2142. sge_count * sizeof (struct ib_uverbs_sge))
  2143. return ERR_PTR(-EINVAL);
  2144. if (wqe_size < sizeof (struct ib_uverbs_recv_wr))
  2145. return ERR_PTR(-EINVAL);
  2146. user_wr = kmalloc(wqe_size, GFP_KERNEL);
  2147. if (!user_wr)
  2148. return ERR_PTR(-ENOMEM);
  2149. sg_ind = 0;
  2150. last = NULL;
  2151. for (i = 0; i < wr_count; ++i) {
  2152. if (copy_from_user(user_wr, buf + i * wqe_size,
  2153. wqe_size)) {
  2154. ret = -EFAULT;
  2155. goto err;
  2156. }
  2157. if (user_wr->num_sge + sg_ind > sge_count) {
  2158. ret = -EINVAL;
  2159. goto err;
  2160. }
  2161. if (user_wr->num_sge >=
  2162. (U32_MAX - ALIGN(sizeof *next, sizeof (struct ib_sge))) /
  2163. sizeof (struct ib_sge)) {
  2164. ret = -EINVAL;
  2165. goto err;
  2166. }
  2167. next = kmalloc(ALIGN(sizeof *next, sizeof (struct ib_sge)) +
  2168. user_wr->num_sge * sizeof (struct ib_sge),
  2169. GFP_KERNEL);
  2170. if (!next) {
  2171. ret = -ENOMEM;
  2172. goto err;
  2173. }
  2174. if (!last)
  2175. wr = next;
  2176. else
  2177. last->next = next;
  2178. last = next;
  2179. next->next = NULL;
  2180. next->wr_id = user_wr->wr_id;
  2181. next->num_sge = user_wr->num_sge;
  2182. if (next->num_sge) {
  2183. next->sg_list = (void *) next +
  2184. ALIGN(sizeof *next, sizeof (struct ib_sge));
  2185. if (copy_from_user(next->sg_list,
  2186. buf + wr_count * wqe_size +
  2187. sg_ind * sizeof (struct ib_sge),
  2188. next->num_sge * sizeof (struct ib_sge))) {
  2189. ret = -EFAULT;
  2190. goto err;
  2191. }
  2192. sg_ind += next->num_sge;
  2193. } else
  2194. next->sg_list = NULL;
  2195. }
  2196. kfree(user_wr);
  2197. return wr;
  2198. err:
  2199. kfree(user_wr);
  2200. while (wr) {
  2201. next = wr->next;
  2202. kfree(wr);
  2203. wr = next;
  2204. }
  2205. return ERR_PTR(ret);
  2206. }
  2207. ssize_t ib_uverbs_post_recv(struct ib_uverbs_file *file,
  2208. struct ib_device *ib_dev,
  2209. const char __user *buf, int in_len,
  2210. int out_len)
  2211. {
  2212. struct ib_uverbs_post_recv cmd;
  2213. struct ib_uverbs_post_recv_resp resp;
  2214. struct ib_recv_wr *wr, *next, *bad_wr;
  2215. struct ib_qp *qp;
  2216. ssize_t ret = -EINVAL;
  2217. if (copy_from_user(&cmd, buf, sizeof cmd))
  2218. return -EFAULT;
  2219. wr = ib_uverbs_unmarshall_recv(buf + sizeof cmd,
  2220. in_len - sizeof cmd, cmd.wr_count,
  2221. cmd.sge_count, cmd.wqe_size);
  2222. if (IS_ERR(wr))
  2223. return PTR_ERR(wr);
  2224. qp = idr_read_qp(cmd.qp_handle, file->ucontext);
  2225. if (!qp)
  2226. goto out;
  2227. resp.bad_wr = 0;
  2228. ret = qp->device->post_recv(qp->real_qp, wr, &bad_wr);
  2229. put_qp_read(qp);
  2230. if (ret)
  2231. for (next = wr; next; next = next->next) {
  2232. ++resp.bad_wr;
  2233. if (next == bad_wr)
  2234. break;
  2235. }
  2236. if (copy_to_user((void __user *) (unsigned long) cmd.response,
  2237. &resp, sizeof resp))
  2238. ret = -EFAULT;
  2239. out:
  2240. while (wr) {
  2241. next = wr->next;
  2242. kfree(wr);
  2243. wr = next;
  2244. }
  2245. return ret ? ret : in_len;
  2246. }
  2247. ssize_t ib_uverbs_post_srq_recv(struct ib_uverbs_file *file,
  2248. struct ib_device *ib_dev,
  2249. const char __user *buf, int in_len,
  2250. int out_len)
  2251. {
  2252. struct ib_uverbs_post_srq_recv cmd;
  2253. struct ib_uverbs_post_srq_recv_resp resp;
  2254. struct ib_recv_wr *wr, *next, *bad_wr;
  2255. struct ib_srq *srq;
  2256. ssize_t ret = -EINVAL;
  2257. if (copy_from_user(&cmd, buf, sizeof cmd))
  2258. return -EFAULT;
  2259. wr = ib_uverbs_unmarshall_recv(buf + sizeof cmd,
  2260. in_len - sizeof cmd, cmd.wr_count,
  2261. cmd.sge_count, cmd.wqe_size);
  2262. if (IS_ERR(wr))
  2263. return PTR_ERR(wr);
  2264. srq = idr_read_srq(cmd.srq_handle, file->ucontext);
  2265. if (!srq)
  2266. goto out;
  2267. resp.bad_wr = 0;
  2268. ret = srq->device->post_srq_recv(srq, wr, &bad_wr);
  2269. put_srq_read(srq);
  2270. if (ret)
  2271. for (next = wr; next; next = next->next) {
  2272. ++resp.bad_wr;
  2273. if (next == bad_wr)
  2274. break;
  2275. }
  2276. if (copy_to_user((void __user *) (unsigned long) cmd.response,
  2277. &resp, sizeof resp))
  2278. ret = -EFAULT;
  2279. out:
  2280. while (wr) {
  2281. next = wr->next;
  2282. kfree(wr);
  2283. wr = next;
  2284. }
  2285. return ret ? ret : in_len;
  2286. }
  2287. ssize_t ib_uverbs_create_ah(struct ib_uverbs_file *file,
  2288. struct ib_device *ib_dev,
  2289. const char __user *buf, int in_len,
  2290. int out_len)
  2291. {
  2292. struct ib_uverbs_create_ah cmd;
  2293. struct ib_uverbs_create_ah_resp resp;
  2294. struct ib_uobject *uobj;
  2295. struct ib_pd *pd;
  2296. struct ib_ah *ah;
  2297. struct ib_ah_attr attr;
  2298. int ret;
  2299. if (out_len < sizeof resp)
  2300. return -ENOSPC;
  2301. if (copy_from_user(&cmd, buf, sizeof cmd))
  2302. return -EFAULT;
  2303. if (cmd.attr.port_num < rdma_start_port(ib_dev) ||
  2304. cmd.attr.port_num > rdma_end_port(ib_dev))
  2305. return -EINVAL;
  2306. uobj = kmalloc(sizeof *uobj, GFP_KERNEL);
  2307. if (!uobj)
  2308. return -ENOMEM;
  2309. init_uobj(uobj, cmd.user_handle, file->ucontext, &ah_lock_class);
  2310. down_write(&uobj->mutex);
  2311. pd = idr_read_pd(cmd.pd_handle, file->ucontext);
  2312. if (!pd) {
  2313. ret = -EINVAL;
  2314. goto err;
  2315. }
  2316. attr.dlid = cmd.attr.dlid;
  2317. attr.sl = cmd.attr.sl;
  2318. attr.src_path_bits = cmd.attr.src_path_bits;
  2319. attr.static_rate = cmd.attr.static_rate;
  2320. attr.ah_flags = cmd.attr.is_global ? IB_AH_GRH : 0;
  2321. attr.port_num = cmd.attr.port_num;
  2322. attr.grh.flow_label = cmd.attr.grh.flow_label;
  2323. attr.grh.sgid_index = cmd.attr.grh.sgid_index;
  2324. attr.grh.hop_limit = cmd.attr.grh.hop_limit;
  2325. attr.grh.traffic_class = cmd.attr.grh.traffic_class;
  2326. memset(&attr.dmac, 0, sizeof(attr.dmac));
  2327. memcpy(attr.grh.dgid.raw, cmd.attr.grh.dgid, 16);
  2328. ah = ib_create_ah(pd, &attr);
  2329. if (IS_ERR(ah)) {
  2330. ret = PTR_ERR(ah);
  2331. goto err_put;
  2332. }
  2333. ah->uobject = uobj;
  2334. uobj->object = ah;
  2335. ret = idr_add_uobj(&ib_uverbs_ah_idr, uobj);
  2336. if (ret)
  2337. goto err_destroy;
  2338. resp.ah_handle = uobj->id;
  2339. if (copy_to_user((void __user *) (unsigned long) cmd.response,
  2340. &resp, sizeof resp)) {
  2341. ret = -EFAULT;
  2342. goto err_copy;
  2343. }
  2344. put_pd_read(pd);
  2345. mutex_lock(&file->mutex);
  2346. list_add_tail(&uobj->list, &file->ucontext->ah_list);
  2347. mutex_unlock(&file->mutex);
  2348. uobj->live = 1;
  2349. up_write(&uobj->mutex);
  2350. return in_len;
  2351. err_copy:
  2352. idr_remove_uobj(&ib_uverbs_ah_idr, uobj);
  2353. err_destroy:
  2354. ib_destroy_ah(ah);
  2355. err_put:
  2356. put_pd_read(pd);
  2357. err:
  2358. put_uobj_write(uobj);
  2359. return ret;
  2360. }
  2361. ssize_t ib_uverbs_destroy_ah(struct ib_uverbs_file *file,
  2362. struct ib_device *ib_dev,
  2363. const char __user *buf, int in_len, int out_len)
  2364. {
  2365. struct ib_uverbs_destroy_ah cmd;
  2366. struct ib_ah *ah;
  2367. struct ib_uobject *uobj;
  2368. int ret;
  2369. if (copy_from_user(&cmd, buf, sizeof cmd))
  2370. return -EFAULT;
  2371. uobj = idr_write_uobj(&ib_uverbs_ah_idr, cmd.ah_handle, file->ucontext);
  2372. if (!uobj)
  2373. return -EINVAL;
  2374. ah = uobj->object;
  2375. ret = ib_destroy_ah(ah);
  2376. if (!ret)
  2377. uobj->live = 0;
  2378. put_uobj_write(uobj);
  2379. if (ret)
  2380. return ret;
  2381. idr_remove_uobj(&ib_uverbs_ah_idr, uobj);
  2382. mutex_lock(&file->mutex);
  2383. list_del(&uobj->list);
  2384. mutex_unlock(&file->mutex);
  2385. put_uobj(uobj);
  2386. return in_len;
  2387. }
  2388. ssize_t ib_uverbs_attach_mcast(struct ib_uverbs_file *file,
  2389. struct ib_device *ib_dev,
  2390. const char __user *buf, int in_len,
  2391. int out_len)
  2392. {
  2393. struct ib_uverbs_attach_mcast cmd;
  2394. struct ib_qp *qp;
  2395. struct ib_uqp_object *obj;
  2396. struct ib_uverbs_mcast_entry *mcast;
  2397. int ret;
  2398. if (copy_from_user(&cmd, buf, sizeof cmd))
  2399. return -EFAULT;
  2400. qp = idr_write_qp(cmd.qp_handle, file->ucontext);
  2401. if (!qp)
  2402. return -EINVAL;
  2403. obj = container_of(qp->uobject, struct ib_uqp_object, uevent.uobject);
  2404. list_for_each_entry(mcast, &obj->mcast_list, list)
  2405. if (cmd.mlid == mcast->lid &&
  2406. !memcmp(cmd.gid, mcast->gid.raw, sizeof mcast->gid.raw)) {
  2407. ret = 0;
  2408. goto out_put;
  2409. }
  2410. mcast = kmalloc(sizeof *mcast, GFP_KERNEL);
  2411. if (!mcast) {
  2412. ret = -ENOMEM;
  2413. goto out_put;
  2414. }
  2415. mcast->lid = cmd.mlid;
  2416. memcpy(mcast->gid.raw, cmd.gid, sizeof mcast->gid.raw);
  2417. ret = ib_attach_mcast(qp, &mcast->gid, cmd.mlid);
  2418. if (!ret)
  2419. list_add_tail(&mcast->list, &obj->mcast_list);
  2420. else
  2421. kfree(mcast);
  2422. out_put:
  2423. put_qp_write(qp);
  2424. return ret ? ret : in_len;
  2425. }
  2426. ssize_t ib_uverbs_detach_mcast(struct ib_uverbs_file *file,
  2427. struct ib_device *ib_dev,
  2428. const char __user *buf, int in_len,
  2429. int out_len)
  2430. {
  2431. struct ib_uverbs_detach_mcast cmd;
  2432. struct ib_uqp_object *obj;
  2433. struct ib_qp *qp;
  2434. struct ib_uverbs_mcast_entry *mcast;
  2435. int ret = -EINVAL;
  2436. if (copy_from_user(&cmd, buf, sizeof cmd))
  2437. return -EFAULT;
  2438. qp = idr_write_qp(cmd.qp_handle, file->ucontext);
  2439. if (!qp)
  2440. return -EINVAL;
  2441. ret = ib_detach_mcast(qp, (union ib_gid *) cmd.gid, cmd.mlid);
  2442. if (ret)
  2443. goto out_put;
  2444. obj = container_of(qp->uobject, struct ib_uqp_object, uevent.uobject);
  2445. list_for_each_entry(mcast, &obj->mcast_list, list)
  2446. if (cmd.mlid == mcast->lid &&
  2447. !memcmp(cmd.gid, mcast->gid.raw, sizeof mcast->gid.raw)) {
  2448. list_del(&mcast->list);
  2449. kfree(mcast);
  2450. break;
  2451. }
  2452. out_put:
  2453. put_qp_write(qp);
  2454. return ret ? ret : in_len;
  2455. }
  2456. static int kern_spec_to_ib_spec(struct ib_uverbs_flow_spec *kern_spec,
  2457. union ib_flow_spec *ib_spec)
  2458. {
  2459. if (kern_spec->reserved)
  2460. return -EINVAL;
  2461. ib_spec->type = kern_spec->type;
  2462. switch (ib_spec->type) {
  2463. case IB_FLOW_SPEC_ETH:
  2464. ib_spec->eth.size = sizeof(struct ib_flow_spec_eth);
  2465. if (ib_spec->eth.size != kern_spec->eth.size)
  2466. return -EINVAL;
  2467. memcpy(&ib_spec->eth.val, &kern_spec->eth.val,
  2468. sizeof(struct ib_flow_eth_filter));
  2469. memcpy(&ib_spec->eth.mask, &kern_spec->eth.mask,
  2470. sizeof(struct ib_flow_eth_filter));
  2471. break;
  2472. case IB_FLOW_SPEC_IPV4:
  2473. ib_spec->ipv4.size = sizeof(struct ib_flow_spec_ipv4);
  2474. if (ib_spec->ipv4.size != kern_spec->ipv4.size)
  2475. return -EINVAL;
  2476. memcpy(&ib_spec->ipv4.val, &kern_spec->ipv4.val,
  2477. sizeof(struct ib_flow_ipv4_filter));
  2478. memcpy(&ib_spec->ipv4.mask, &kern_spec->ipv4.mask,
  2479. sizeof(struct ib_flow_ipv4_filter));
  2480. break;
  2481. case IB_FLOW_SPEC_TCP:
  2482. case IB_FLOW_SPEC_UDP:
  2483. ib_spec->tcp_udp.size = sizeof(struct ib_flow_spec_tcp_udp);
  2484. if (ib_spec->tcp_udp.size != kern_spec->tcp_udp.size)
  2485. return -EINVAL;
  2486. memcpy(&ib_spec->tcp_udp.val, &kern_spec->tcp_udp.val,
  2487. sizeof(struct ib_flow_tcp_udp_filter));
  2488. memcpy(&ib_spec->tcp_udp.mask, &kern_spec->tcp_udp.mask,
  2489. sizeof(struct ib_flow_tcp_udp_filter));
  2490. break;
  2491. default:
  2492. return -EINVAL;
  2493. }
  2494. return 0;
  2495. }
  2496. int ib_uverbs_ex_create_flow(struct ib_uverbs_file *file,
  2497. struct ib_device *ib_dev,
  2498. struct ib_udata *ucore,
  2499. struct ib_udata *uhw)
  2500. {
  2501. struct ib_uverbs_create_flow cmd;
  2502. struct ib_uverbs_create_flow_resp resp;
  2503. struct ib_uobject *uobj;
  2504. struct ib_flow *flow_id;
  2505. struct ib_uverbs_flow_attr *kern_flow_attr;
  2506. struct ib_flow_attr *flow_attr;
  2507. struct ib_qp *qp;
  2508. int err = 0;
  2509. void *kern_spec;
  2510. void *ib_spec;
  2511. int i;
  2512. if (ucore->inlen < sizeof(cmd))
  2513. return -EINVAL;
  2514. if (ucore->outlen < sizeof(resp))
  2515. return -ENOSPC;
  2516. err = ib_copy_from_udata(&cmd, ucore, sizeof(cmd));
  2517. if (err)
  2518. return err;
  2519. ucore->inbuf += sizeof(cmd);
  2520. ucore->inlen -= sizeof(cmd);
  2521. if (cmd.comp_mask)
  2522. return -EINVAL;
  2523. if ((cmd.flow_attr.type == IB_FLOW_ATTR_SNIFFER &&
  2524. !capable(CAP_NET_ADMIN)) || !capable(CAP_NET_RAW))
  2525. return -EPERM;
  2526. if (cmd.flow_attr.num_of_specs > IB_FLOW_SPEC_SUPPORT_LAYERS)
  2527. return -EINVAL;
  2528. if (cmd.flow_attr.size > ucore->inlen ||
  2529. cmd.flow_attr.size >
  2530. (cmd.flow_attr.num_of_specs * sizeof(struct ib_uverbs_flow_spec)))
  2531. return -EINVAL;
  2532. if (cmd.flow_attr.reserved[0] ||
  2533. cmd.flow_attr.reserved[1])
  2534. return -EINVAL;
  2535. if (cmd.flow_attr.num_of_specs) {
  2536. kern_flow_attr = kmalloc(sizeof(*kern_flow_attr) + cmd.flow_attr.size,
  2537. GFP_KERNEL);
  2538. if (!kern_flow_attr)
  2539. return -ENOMEM;
  2540. memcpy(kern_flow_attr, &cmd.flow_attr, sizeof(*kern_flow_attr));
  2541. err = ib_copy_from_udata(kern_flow_attr + 1, ucore,
  2542. cmd.flow_attr.size);
  2543. if (err)
  2544. goto err_free_attr;
  2545. } else {
  2546. kern_flow_attr = &cmd.flow_attr;
  2547. }
  2548. uobj = kmalloc(sizeof(*uobj), GFP_KERNEL);
  2549. if (!uobj) {
  2550. err = -ENOMEM;
  2551. goto err_free_attr;
  2552. }
  2553. init_uobj(uobj, 0, file->ucontext, &rule_lock_class);
  2554. down_write(&uobj->mutex);
  2555. qp = idr_read_qp(cmd.qp_handle, file->ucontext);
  2556. if (!qp) {
  2557. err = -EINVAL;
  2558. goto err_uobj;
  2559. }
  2560. flow_attr = kmalloc(sizeof(*flow_attr) + cmd.flow_attr.size, GFP_KERNEL);
  2561. if (!flow_attr) {
  2562. err = -ENOMEM;
  2563. goto err_put;
  2564. }
  2565. flow_attr->type = kern_flow_attr->type;
  2566. flow_attr->priority = kern_flow_attr->priority;
  2567. flow_attr->num_of_specs = kern_flow_attr->num_of_specs;
  2568. flow_attr->port = kern_flow_attr->port;
  2569. flow_attr->flags = kern_flow_attr->flags;
  2570. flow_attr->size = sizeof(*flow_attr);
  2571. kern_spec = kern_flow_attr + 1;
  2572. ib_spec = flow_attr + 1;
  2573. for (i = 0; i < flow_attr->num_of_specs &&
  2574. cmd.flow_attr.size > offsetof(struct ib_uverbs_flow_spec, reserved) &&
  2575. cmd.flow_attr.size >=
  2576. ((struct ib_uverbs_flow_spec *)kern_spec)->size; i++) {
  2577. err = kern_spec_to_ib_spec(kern_spec, ib_spec);
  2578. if (err)
  2579. goto err_free;
  2580. flow_attr->size +=
  2581. ((union ib_flow_spec *) ib_spec)->size;
  2582. cmd.flow_attr.size -= ((struct ib_uverbs_flow_spec *)kern_spec)->size;
  2583. kern_spec += ((struct ib_uverbs_flow_spec *) kern_spec)->size;
  2584. ib_spec += ((union ib_flow_spec *) ib_spec)->size;
  2585. }
  2586. if (cmd.flow_attr.size || (i != flow_attr->num_of_specs)) {
  2587. pr_warn("create flow failed, flow %d: %d bytes left from uverb cmd\n",
  2588. i, cmd.flow_attr.size);
  2589. err = -EINVAL;
  2590. goto err_free;
  2591. }
  2592. flow_id = ib_create_flow(qp, flow_attr, IB_FLOW_DOMAIN_USER);
  2593. if (IS_ERR(flow_id)) {
  2594. err = PTR_ERR(flow_id);
  2595. goto err_free;
  2596. }
  2597. flow_id->qp = qp;
  2598. flow_id->uobject = uobj;
  2599. uobj->object = flow_id;
  2600. err = idr_add_uobj(&ib_uverbs_rule_idr, uobj);
  2601. if (err)
  2602. goto destroy_flow;
  2603. memset(&resp, 0, sizeof(resp));
  2604. resp.flow_handle = uobj->id;
  2605. err = ib_copy_to_udata(ucore,
  2606. &resp, sizeof(resp));
  2607. if (err)
  2608. goto err_copy;
  2609. put_qp_read(qp);
  2610. mutex_lock(&file->mutex);
  2611. list_add_tail(&uobj->list, &file->ucontext->rule_list);
  2612. mutex_unlock(&file->mutex);
  2613. uobj->live = 1;
  2614. up_write(&uobj->mutex);
  2615. kfree(flow_attr);
  2616. if (cmd.flow_attr.num_of_specs)
  2617. kfree(kern_flow_attr);
  2618. return 0;
  2619. err_copy:
  2620. idr_remove_uobj(&ib_uverbs_rule_idr, uobj);
  2621. destroy_flow:
  2622. ib_destroy_flow(flow_id);
  2623. err_free:
  2624. kfree(flow_attr);
  2625. err_put:
  2626. put_qp_read(qp);
  2627. err_uobj:
  2628. put_uobj_write(uobj);
  2629. err_free_attr:
  2630. if (cmd.flow_attr.num_of_specs)
  2631. kfree(kern_flow_attr);
  2632. return err;
  2633. }
  2634. int ib_uverbs_ex_destroy_flow(struct ib_uverbs_file *file,
  2635. struct ib_device *ib_dev,
  2636. struct ib_udata *ucore,
  2637. struct ib_udata *uhw)
  2638. {
  2639. struct ib_uverbs_destroy_flow cmd;
  2640. struct ib_flow *flow_id;
  2641. struct ib_uobject *uobj;
  2642. int ret;
  2643. if (ucore->inlen < sizeof(cmd))
  2644. return -EINVAL;
  2645. ret = ib_copy_from_udata(&cmd, ucore, sizeof(cmd));
  2646. if (ret)
  2647. return ret;
  2648. if (cmd.comp_mask)
  2649. return -EINVAL;
  2650. uobj = idr_write_uobj(&ib_uverbs_rule_idr, cmd.flow_handle,
  2651. file->ucontext);
  2652. if (!uobj)
  2653. return -EINVAL;
  2654. flow_id = uobj->object;
  2655. ret = ib_destroy_flow(flow_id);
  2656. if (!ret)
  2657. uobj->live = 0;
  2658. put_uobj_write(uobj);
  2659. idr_remove_uobj(&ib_uverbs_rule_idr, uobj);
  2660. mutex_lock(&file->mutex);
  2661. list_del(&uobj->list);
  2662. mutex_unlock(&file->mutex);
  2663. put_uobj(uobj);
  2664. return ret;
  2665. }
  2666. static int __uverbs_create_xsrq(struct ib_uverbs_file *file,
  2667. struct ib_device *ib_dev,
  2668. struct ib_uverbs_create_xsrq *cmd,
  2669. struct ib_udata *udata)
  2670. {
  2671. struct ib_uverbs_create_srq_resp resp;
  2672. struct ib_usrq_object *obj;
  2673. struct ib_pd *pd;
  2674. struct ib_srq *srq;
  2675. struct ib_uobject *uninitialized_var(xrcd_uobj);
  2676. struct ib_srq_init_attr attr;
  2677. int ret;
  2678. obj = kmalloc(sizeof *obj, GFP_KERNEL);
  2679. if (!obj)
  2680. return -ENOMEM;
  2681. init_uobj(&obj->uevent.uobject, cmd->user_handle, file->ucontext, &srq_lock_class);
  2682. down_write(&obj->uevent.uobject.mutex);
  2683. if (cmd->srq_type == IB_SRQT_XRC) {
  2684. attr.ext.xrc.xrcd = idr_read_xrcd(cmd->xrcd_handle, file->ucontext, &xrcd_uobj);
  2685. if (!attr.ext.xrc.xrcd) {
  2686. ret = -EINVAL;
  2687. goto err;
  2688. }
  2689. obj->uxrcd = container_of(xrcd_uobj, struct ib_uxrcd_object, uobject);
  2690. atomic_inc(&obj->uxrcd->refcnt);
  2691. attr.ext.xrc.cq = idr_read_cq(cmd->cq_handle, file->ucontext, 0);
  2692. if (!attr.ext.xrc.cq) {
  2693. ret = -EINVAL;
  2694. goto err_put_xrcd;
  2695. }
  2696. }
  2697. pd = idr_read_pd(cmd->pd_handle, file->ucontext);
  2698. if (!pd) {
  2699. ret = -EINVAL;
  2700. goto err_put_cq;
  2701. }
  2702. attr.event_handler = ib_uverbs_srq_event_handler;
  2703. attr.srq_context = file;
  2704. attr.srq_type = cmd->srq_type;
  2705. attr.attr.max_wr = cmd->max_wr;
  2706. attr.attr.max_sge = cmd->max_sge;
  2707. attr.attr.srq_limit = cmd->srq_limit;
  2708. obj->uevent.events_reported = 0;
  2709. INIT_LIST_HEAD(&obj->uevent.event_list);
  2710. srq = pd->device->create_srq(pd, &attr, udata);
  2711. if (IS_ERR(srq)) {
  2712. ret = PTR_ERR(srq);
  2713. goto err_put;
  2714. }
  2715. srq->device = pd->device;
  2716. srq->pd = pd;
  2717. srq->srq_type = cmd->srq_type;
  2718. srq->uobject = &obj->uevent.uobject;
  2719. srq->event_handler = attr.event_handler;
  2720. srq->srq_context = attr.srq_context;
  2721. if (cmd->srq_type == IB_SRQT_XRC) {
  2722. srq->ext.xrc.cq = attr.ext.xrc.cq;
  2723. srq->ext.xrc.xrcd = attr.ext.xrc.xrcd;
  2724. atomic_inc(&attr.ext.xrc.cq->usecnt);
  2725. atomic_inc(&attr.ext.xrc.xrcd->usecnt);
  2726. }
  2727. atomic_inc(&pd->usecnt);
  2728. atomic_set(&srq->usecnt, 0);
  2729. obj->uevent.uobject.object = srq;
  2730. ret = idr_add_uobj(&ib_uverbs_srq_idr, &obj->uevent.uobject);
  2731. if (ret)
  2732. goto err_destroy;
  2733. memset(&resp, 0, sizeof resp);
  2734. resp.srq_handle = obj->uevent.uobject.id;
  2735. resp.max_wr = attr.attr.max_wr;
  2736. resp.max_sge = attr.attr.max_sge;
  2737. if (cmd->srq_type == IB_SRQT_XRC)
  2738. resp.srqn = srq->ext.xrc.srq_num;
  2739. if (copy_to_user((void __user *) (unsigned long) cmd->response,
  2740. &resp, sizeof resp)) {
  2741. ret = -EFAULT;
  2742. goto err_copy;
  2743. }
  2744. if (cmd->srq_type == IB_SRQT_XRC) {
  2745. put_uobj_read(xrcd_uobj);
  2746. put_cq_read(attr.ext.xrc.cq);
  2747. }
  2748. put_pd_read(pd);
  2749. mutex_lock(&file->mutex);
  2750. list_add_tail(&obj->uevent.uobject.list, &file->ucontext->srq_list);
  2751. mutex_unlock(&file->mutex);
  2752. obj->uevent.uobject.live = 1;
  2753. up_write(&obj->uevent.uobject.mutex);
  2754. return 0;
  2755. err_copy:
  2756. idr_remove_uobj(&ib_uverbs_srq_idr, &obj->uevent.uobject);
  2757. err_destroy:
  2758. ib_destroy_srq(srq);
  2759. err_put:
  2760. put_pd_read(pd);
  2761. err_put_cq:
  2762. if (cmd->srq_type == IB_SRQT_XRC)
  2763. put_cq_read(attr.ext.xrc.cq);
  2764. err_put_xrcd:
  2765. if (cmd->srq_type == IB_SRQT_XRC) {
  2766. atomic_dec(&obj->uxrcd->refcnt);
  2767. put_uobj_read(xrcd_uobj);
  2768. }
  2769. err:
  2770. put_uobj_write(&obj->uevent.uobject);
  2771. return ret;
  2772. }
  2773. ssize_t ib_uverbs_create_srq(struct ib_uverbs_file *file,
  2774. struct ib_device *ib_dev,
  2775. const char __user *buf, int in_len,
  2776. int out_len)
  2777. {
  2778. struct ib_uverbs_create_srq cmd;
  2779. struct ib_uverbs_create_xsrq xcmd;
  2780. struct ib_uverbs_create_srq_resp resp;
  2781. struct ib_udata udata;
  2782. int ret;
  2783. if (out_len < sizeof resp)
  2784. return -ENOSPC;
  2785. if (copy_from_user(&cmd, buf, sizeof cmd))
  2786. return -EFAULT;
  2787. xcmd.response = cmd.response;
  2788. xcmd.user_handle = cmd.user_handle;
  2789. xcmd.srq_type = IB_SRQT_BASIC;
  2790. xcmd.pd_handle = cmd.pd_handle;
  2791. xcmd.max_wr = cmd.max_wr;
  2792. xcmd.max_sge = cmd.max_sge;
  2793. xcmd.srq_limit = cmd.srq_limit;
  2794. INIT_UDATA(&udata, buf + sizeof cmd,
  2795. (unsigned long) cmd.response + sizeof resp,
  2796. in_len - sizeof cmd, out_len - sizeof resp);
  2797. ret = __uverbs_create_xsrq(file, ib_dev, &xcmd, &udata);
  2798. if (ret)
  2799. return ret;
  2800. return in_len;
  2801. }
  2802. ssize_t ib_uverbs_create_xsrq(struct ib_uverbs_file *file,
  2803. struct ib_device *ib_dev,
  2804. const char __user *buf, int in_len, int out_len)
  2805. {
  2806. struct ib_uverbs_create_xsrq cmd;
  2807. struct ib_uverbs_create_srq_resp resp;
  2808. struct ib_udata udata;
  2809. int ret;
  2810. if (out_len < sizeof resp)
  2811. return -ENOSPC;
  2812. if (copy_from_user(&cmd, buf, sizeof cmd))
  2813. return -EFAULT;
  2814. INIT_UDATA(&udata, buf + sizeof cmd,
  2815. (unsigned long) cmd.response + sizeof resp,
  2816. in_len - sizeof cmd, out_len - sizeof resp);
  2817. ret = __uverbs_create_xsrq(file, ib_dev, &cmd, &udata);
  2818. if (ret)
  2819. return ret;
  2820. return in_len;
  2821. }
  2822. ssize_t ib_uverbs_modify_srq(struct ib_uverbs_file *file,
  2823. struct ib_device *ib_dev,
  2824. const char __user *buf, int in_len,
  2825. int out_len)
  2826. {
  2827. struct ib_uverbs_modify_srq cmd;
  2828. struct ib_udata udata;
  2829. struct ib_srq *srq;
  2830. struct ib_srq_attr attr;
  2831. int ret;
  2832. if (copy_from_user(&cmd, buf, sizeof cmd))
  2833. return -EFAULT;
  2834. INIT_UDATA(&udata, buf + sizeof cmd, NULL, in_len - sizeof cmd,
  2835. out_len);
  2836. srq = idr_read_srq(cmd.srq_handle, file->ucontext);
  2837. if (!srq)
  2838. return -EINVAL;
  2839. attr.max_wr = cmd.max_wr;
  2840. attr.srq_limit = cmd.srq_limit;
  2841. ret = srq->device->modify_srq(srq, &attr, cmd.attr_mask, &udata);
  2842. put_srq_read(srq);
  2843. return ret ? ret : in_len;
  2844. }
  2845. ssize_t ib_uverbs_query_srq(struct ib_uverbs_file *file,
  2846. struct ib_device *ib_dev,
  2847. const char __user *buf,
  2848. int in_len, int out_len)
  2849. {
  2850. struct ib_uverbs_query_srq cmd;
  2851. struct ib_uverbs_query_srq_resp resp;
  2852. struct ib_srq_attr attr;
  2853. struct ib_srq *srq;
  2854. int ret;
  2855. if (out_len < sizeof resp)
  2856. return -ENOSPC;
  2857. if (copy_from_user(&cmd, buf, sizeof cmd))
  2858. return -EFAULT;
  2859. srq = idr_read_srq(cmd.srq_handle, file->ucontext);
  2860. if (!srq)
  2861. return -EINVAL;
  2862. ret = ib_query_srq(srq, &attr);
  2863. put_srq_read(srq);
  2864. if (ret)
  2865. return ret;
  2866. memset(&resp, 0, sizeof resp);
  2867. resp.max_wr = attr.max_wr;
  2868. resp.max_sge = attr.max_sge;
  2869. resp.srq_limit = attr.srq_limit;
  2870. if (copy_to_user((void __user *) (unsigned long) cmd.response,
  2871. &resp, sizeof resp))
  2872. return -EFAULT;
  2873. return in_len;
  2874. }
  2875. ssize_t ib_uverbs_destroy_srq(struct ib_uverbs_file *file,
  2876. struct ib_device *ib_dev,
  2877. const char __user *buf, int in_len,
  2878. int out_len)
  2879. {
  2880. struct ib_uverbs_destroy_srq cmd;
  2881. struct ib_uverbs_destroy_srq_resp resp;
  2882. struct ib_uobject *uobj;
  2883. struct ib_srq *srq;
  2884. struct ib_uevent_object *obj;
  2885. int ret = -EINVAL;
  2886. struct ib_usrq_object *us;
  2887. enum ib_srq_type srq_type;
  2888. if (copy_from_user(&cmd, buf, sizeof cmd))
  2889. return -EFAULT;
  2890. uobj = idr_write_uobj(&ib_uverbs_srq_idr, cmd.srq_handle, file->ucontext);
  2891. if (!uobj)
  2892. return -EINVAL;
  2893. srq = uobj->object;
  2894. obj = container_of(uobj, struct ib_uevent_object, uobject);
  2895. srq_type = srq->srq_type;
  2896. ret = ib_destroy_srq(srq);
  2897. if (!ret)
  2898. uobj->live = 0;
  2899. put_uobj_write(uobj);
  2900. if (ret)
  2901. return ret;
  2902. if (srq_type == IB_SRQT_XRC) {
  2903. us = container_of(obj, struct ib_usrq_object, uevent);
  2904. atomic_dec(&us->uxrcd->refcnt);
  2905. }
  2906. idr_remove_uobj(&ib_uverbs_srq_idr, uobj);
  2907. mutex_lock(&file->mutex);
  2908. list_del(&uobj->list);
  2909. mutex_unlock(&file->mutex);
  2910. ib_uverbs_release_uevent(file, obj);
  2911. memset(&resp, 0, sizeof resp);
  2912. resp.events_reported = obj->events_reported;
  2913. put_uobj(uobj);
  2914. if (copy_to_user((void __user *) (unsigned long) cmd.response,
  2915. &resp, sizeof resp))
  2916. ret = -EFAULT;
  2917. return ret ? ret : in_len;
  2918. }
  2919. int ib_uverbs_ex_query_device(struct ib_uverbs_file *file,
  2920. struct ib_device *ib_dev,
  2921. struct ib_udata *ucore,
  2922. struct ib_udata *uhw)
  2923. {
  2924. struct ib_uverbs_ex_query_device_resp resp;
  2925. struct ib_uverbs_ex_query_device cmd;
  2926. struct ib_device_attr attr;
  2927. int err;
  2928. if (ucore->inlen < sizeof(cmd))
  2929. return -EINVAL;
  2930. err = ib_copy_from_udata(&cmd, ucore, sizeof(cmd));
  2931. if (err)
  2932. return err;
  2933. if (cmd.comp_mask)
  2934. return -EINVAL;
  2935. if (cmd.reserved)
  2936. return -EINVAL;
  2937. resp.response_length = offsetof(typeof(resp), odp_caps);
  2938. if (ucore->outlen < resp.response_length)
  2939. return -ENOSPC;
  2940. memset(&attr, 0, sizeof(attr));
  2941. err = ib_dev->query_device(ib_dev, &attr, uhw);
  2942. if (err)
  2943. return err;
  2944. copy_query_dev_fields(file, ib_dev, &resp.base, &attr);
  2945. resp.comp_mask = 0;
  2946. if (ucore->outlen < resp.response_length + sizeof(resp.odp_caps))
  2947. goto end;
  2948. #ifdef CONFIG_INFINIBAND_ON_DEMAND_PAGING
  2949. resp.odp_caps.general_caps = attr.odp_caps.general_caps;
  2950. resp.odp_caps.per_transport_caps.rc_odp_caps =
  2951. attr.odp_caps.per_transport_caps.rc_odp_caps;
  2952. resp.odp_caps.per_transport_caps.uc_odp_caps =
  2953. attr.odp_caps.per_transport_caps.uc_odp_caps;
  2954. resp.odp_caps.per_transport_caps.ud_odp_caps =
  2955. attr.odp_caps.per_transport_caps.ud_odp_caps;
  2956. resp.odp_caps.reserved = 0;
  2957. #else
  2958. memset(&resp.odp_caps, 0, sizeof(resp.odp_caps));
  2959. #endif
  2960. resp.response_length += sizeof(resp.odp_caps);
  2961. if (ucore->outlen < resp.response_length + sizeof(resp.timestamp_mask))
  2962. goto end;
  2963. resp.timestamp_mask = attr.timestamp_mask;
  2964. resp.response_length += sizeof(resp.timestamp_mask);
  2965. if (ucore->outlen < resp.response_length + sizeof(resp.hca_core_clock))
  2966. goto end;
  2967. resp.hca_core_clock = attr.hca_core_clock;
  2968. resp.response_length += sizeof(resp.hca_core_clock);
  2969. end:
  2970. err = ib_copy_to_udata(ucore, &resp, resp.response_length);
  2971. if (err)
  2972. return err;
  2973. return 0;
  2974. }