uverbs_cmd.c 66 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782
  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 <asm/uaccess.h>
  39. #include "uverbs.h"
  40. static struct lock_class_key pd_lock_key;
  41. static struct lock_class_key mr_lock_key;
  42. static struct lock_class_key cq_lock_key;
  43. static struct lock_class_key qp_lock_key;
  44. static struct lock_class_key ah_lock_key;
  45. static struct lock_class_key srq_lock_key;
  46. static struct lock_class_key xrcd_lock_key;
  47. #define INIT_UDATA(udata, ibuf, obuf, ilen, olen) \
  48. do { \
  49. (udata)->inbuf = (void __user *) (ibuf); \
  50. (udata)->outbuf = (void __user *) (obuf); \
  51. (udata)->inlen = (ilen); \
  52. (udata)->outlen = (olen); \
  53. } while (0)
  54. /*
  55. * The ib_uobject locking scheme is as follows:
  56. *
  57. * - ib_uverbs_idr_lock protects the uverbs idrs themselves, so it
  58. * needs to be held during all idr operations. When an object is
  59. * looked up, a reference must be taken on the object's kref before
  60. * dropping this lock.
  61. *
  62. * - Each object also has an rwsem. This rwsem must be held for
  63. * reading while an operation that uses the object is performed.
  64. * For example, while registering an MR, the associated PD's
  65. * uobject.mutex must be held for reading. The rwsem must be held
  66. * for writing while initializing or destroying an object.
  67. *
  68. * - In addition, each object has a "live" flag. If this flag is not
  69. * set, then lookups of the object will fail even if it is found in
  70. * the idr. This handles a reader that blocks and does not acquire
  71. * the rwsem until after the object is destroyed. The destroy
  72. * operation will set the live flag to 0 and then drop the rwsem;
  73. * this will allow the reader to acquire the rwsem, see that the
  74. * live flag is 0, and then drop the rwsem and its reference to
  75. * object. The underlying storage will not be freed until the last
  76. * reference to the object is dropped.
  77. */
  78. static void init_uobj(struct ib_uobject *uobj, u64 user_handle,
  79. struct ib_ucontext *context, struct lock_class_key *key)
  80. {
  81. uobj->user_handle = user_handle;
  82. uobj->context = context;
  83. kref_init(&uobj->ref);
  84. init_rwsem(&uobj->mutex);
  85. lockdep_set_class(&uobj->mutex, key);
  86. uobj->live = 0;
  87. }
  88. static void release_uobj(struct kref *kref)
  89. {
  90. kfree(container_of(kref, struct ib_uobject, ref));
  91. }
  92. static void put_uobj(struct ib_uobject *uobj)
  93. {
  94. kref_put(&uobj->ref, release_uobj);
  95. }
  96. static void put_uobj_read(struct ib_uobject *uobj)
  97. {
  98. up_read(&uobj->mutex);
  99. put_uobj(uobj);
  100. }
  101. static void put_uobj_write(struct ib_uobject *uobj)
  102. {
  103. up_write(&uobj->mutex);
  104. put_uobj(uobj);
  105. }
  106. static int idr_add_uobj(struct idr *idr, struct ib_uobject *uobj)
  107. {
  108. int ret;
  109. retry:
  110. if (!idr_pre_get(idr, GFP_KERNEL))
  111. return -ENOMEM;
  112. spin_lock(&ib_uverbs_idr_lock);
  113. ret = idr_get_new(idr, uobj, &uobj->id);
  114. spin_unlock(&ib_uverbs_idr_lock);
  115. if (ret == -EAGAIN)
  116. goto retry;
  117. return ret;
  118. }
  119. void idr_remove_uobj(struct idr *idr, struct ib_uobject *uobj)
  120. {
  121. spin_lock(&ib_uverbs_idr_lock);
  122. idr_remove(idr, uobj->id);
  123. spin_unlock(&ib_uverbs_idr_lock);
  124. }
  125. static struct ib_uobject *__idr_get_uobj(struct idr *idr, int id,
  126. struct ib_ucontext *context)
  127. {
  128. struct ib_uobject *uobj;
  129. spin_lock(&ib_uverbs_idr_lock);
  130. uobj = idr_find(idr, id);
  131. if (uobj) {
  132. if (uobj->context == context)
  133. kref_get(&uobj->ref);
  134. else
  135. uobj = NULL;
  136. }
  137. spin_unlock(&ib_uverbs_idr_lock);
  138. return uobj;
  139. }
  140. static struct ib_uobject *idr_read_uobj(struct idr *idr, int id,
  141. struct ib_ucontext *context, int nested)
  142. {
  143. struct ib_uobject *uobj;
  144. uobj = __idr_get_uobj(idr, id, context);
  145. if (!uobj)
  146. return NULL;
  147. if (nested)
  148. down_read_nested(&uobj->mutex, SINGLE_DEPTH_NESTING);
  149. else
  150. down_read(&uobj->mutex);
  151. if (!uobj->live) {
  152. put_uobj_read(uobj);
  153. return NULL;
  154. }
  155. return uobj;
  156. }
  157. static struct ib_uobject *idr_write_uobj(struct idr *idr, int id,
  158. struct ib_ucontext *context)
  159. {
  160. struct ib_uobject *uobj;
  161. uobj = __idr_get_uobj(idr, id, context);
  162. if (!uobj)
  163. return NULL;
  164. down_write(&uobj->mutex);
  165. if (!uobj->live) {
  166. put_uobj_write(uobj);
  167. return NULL;
  168. }
  169. return uobj;
  170. }
  171. static void *idr_read_obj(struct idr *idr, int id, struct ib_ucontext *context,
  172. int nested)
  173. {
  174. struct ib_uobject *uobj;
  175. uobj = idr_read_uobj(idr, id, context, nested);
  176. return uobj ? uobj->object : NULL;
  177. }
  178. static struct ib_pd *idr_read_pd(int pd_handle, struct ib_ucontext *context)
  179. {
  180. return idr_read_obj(&ib_uverbs_pd_idr, pd_handle, context, 0);
  181. }
  182. static void put_pd_read(struct ib_pd *pd)
  183. {
  184. put_uobj_read(pd->uobject);
  185. }
  186. static struct ib_cq *idr_read_cq(int cq_handle, struct ib_ucontext *context, int nested)
  187. {
  188. return idr_read_obj(&ib_uverbs_cq_idr, cq_handle, context, nested);
  189. }
  190. static void put_cq_read(struct ib_cq *cq)
  191. {
  192. put_uobj_read(cq->uobject);
  193. }
  194. static struct ib_ah *idr_read_ah(int ah_handle, struct ib_ucontext *context)
  195. {
  196. return idr_read_obj(&ib_uverbs_ah_idr, ah_handle, context, 0);
  197. }
  198. static void put_ah_read(struct ib_ah *ah)
  199. {
  200. put_uobj_read(ah->uobject);
  201. }
  202. static struct ib_qp *idr_read_qp(int qp_handle, struct ib_ucontext *context)
  203. {
  204. return idr_read_obj(&ib_uverbs_qp_idr, qp_handle, context, 0);
  205. }
  206. static struct ib_qp *idr_write_qp(int qp_handle, struct ib_ucontext *context)
  207. {
  208. struct ib_uobject *uobj;
  209. uobj = idr_write_uobj(&ib_uverbs_qp_idr, qp_handle, context);
  210. return uobj ? uobj->object : NULL;
  211. }
  212. static void put_qp_read(struct ib_qp *qp)
  213. {
  214. put_uobj_read(qp->uobject);
  215. }
  216. static void put_qp_write(struct ib_qp *qp)
  217. {
  218. put_uobj_write(qp->uobject);
  219. }
  220. static struct ib_srq *idr_read_srq(int srq_handle, struct ib_ucontext *context)
  221. {
  222. return idr_read_obj(&ib_uverbs_srq_idr, srq_handle, context, 0);
  223. }
  224. static void put_srq_read(struct ib_srq *srq)
  225. {
  226. put_uobj_read(srq->uobject);
  227. }
  228. static struct ib_xrcd *idr_read_xrcd(int xrcd_handle, struct ib_ucontext *context,
  229. struct ib_uobject **uobj)
  230. {
  231. *uobj = idr_read_uobj(&ib_uverbs_xrcd_idr, xrcd_handle, context, 0);
  232. return *uobj ? (*uobj)->object : NULL;
  233. }
  234. static void put_xrcd_read(struct ib_uobject *uobj)
  235. {
  236. put_uobj_read(uobj);
  237. }
  238. ssize_t ib_uverbs_get_context(struct ib_uverbs_file *file,
  239. const char __user *buf,
  240. int in_len, int out_len)
  241. {
  242. struct ib_uverbs_get_context cmd;
  243. struct ib_uverbs_get_context_resp resp;
  244. struct ib_udata udata;
  245. struct ib_device *ibdev = file->device->ib_dev;
  246. struct ib_ucontext *ucontext;
  247. struct file *filp;
  248. int ret;
  249. if (out_len < sizeof resp)
  250. return -ENOSPC;
  251. if (copy_from_user(&cmd, buf, sizeof cmd))
  252. return -EFAULT;
  253. mutex_lock(&file->mutex);
  254. if (file->ucontext) {
  255. ret = -EINVAL;
  256. goto err;
  257. }
  258. INIT_UDATA(&udata, buf + sizeof cmd,
  259. (unsigned long) cmd.response + sizeof resp,
  260. in_len - sizeof cmd, out_len - sizeof resp);
  261. ucontext = ibdev->alloc_ucontext(ibdev, &udata);
  262. if (IS_ERR(ucontext)) {
  263. ret = PTR_ERR(ucontext);
  264. goto err;
  265. }
  266. ucontext->device = ibdev;
  267. INIT_LIST_HEAD(&ucontext->pd_list);
  268. INIT_LIST_HEAD(&ucontext->mr_list);
  269. INIT_LIST_HEAD(&ucontext->mw_list);
  270. INIT_LIST_HEAD(&ucontext->cq_list);
  271. INIT_LIST_HEAD(&ucontext->qp_list);
  272. INIT_LIST_HEAD(&ucontext->srq_list);
  273. INIT_LIST_HEAD(&ucontext->ah_list);
  274. INIT_LIST_HEAD(&ucontext->xrcd_list);
  275. ucontext->closing = 0;
  276. resp.num_comp_vectors = file->device->num_comp_vectors;
  277. ret = get_unused_fd();
  278. if (ret < 0)
  279. goto err_free;
  280. resp.async_fd = ret;
  281. filp = ib_uverbs_alloc_event_file(file, 1);
  282. if (IS_ERR(filp)) {
  283. ret = PTR_ERR(filp);
  284. goto err_fd;
  285. }
  286. if (copy_to_user((void __user *) (unsigned long) cmd.response,
  287. &resp, sizeof resp)) {
  288. ret = -EFAULT;
  289. goto err_file;
  290. }
  291. file->async_file = filp->private_data;
  292. INIT_IB_EVENT_HANDLER(&file->event_handler, file->device->ib_dev,
  293. ib_uverbs_event_handler);
  294. ret = ib_register_event_handler(&file->event_handler);
  295. if (ret)
  296. goto err_file;
  297. kref_get(&file->async_file->ref);
  298. kref_get(&file->ref);
  299. file->ucontext = ucontext;
  300. fd_install(resp.async_fd, filp);
  301. mutex_unlock(&file->mutex);
  302. return in_len;
  303. err_file:
  304. fput(filp);
  305. err_fd:
  306. put_unused_fd(resp.async_fd);
  307. err_free:
  308. ibdev->dealloc_ucontext(ucontext);
  309. err:
  310. mutex_unlock(&file->mutex);
  311. return ret;
  312. }
  313. ssize_t ib_uverbs_query_device(struct ib_uverbs_file *file,
  314. const char __user *buf,
  315. int in_len, int out_len)
  316. {
  317. struct ib_uverbs_query_device cmd;
  318. struct ib_uverbs_query_device_resp resp;
  319. struct ib_device_attr attr;
  320. int ret;
  321. if (out_len < sizeof resp)
  322. return -ENOSPC;
  323. if (copy_from_user(&cmd, buf, sizeof cmd))
  324. return -EFAULT;
  325. ret = ib_query_device(file->device->ib_dev, &attr);
  326. if (ret)
  327. return ret;
  328. memset(&resp, 0, sizeof resp);
  329. resp.fw_ver = attr.fw_ver;
  330. resp.node_guid = file->device->ib_dev->node_guid;
  331. resp.sys_image_guid = attr.sys_image_guid;
  332. resp.max_mr_size = attr.max_mr_size;
  333. resp.page_size_cap = attr.page_size_cap;
  334. resp.vendor_id = attr.vendor_id;
  335. resp.vendor_part_id = attr.vendor_part_id;
  336. resp.hw_ver = attr.hw_ver;
  337. resp.max_qp = attr.max_qp;
  338. resp.max_qp_wr = attr.max_qp_wr;
  339. resp.device_cap_flags = attr.device_cap_flags;
  340. resp.max_sge = attr.max_sge;
  341. resp.max_sge_rd = attr.max_sge_rd;
  342. resp.max_cq = attr.max_cq;
  343. resp.max_cqe = attr.max_cqe;
  344. resp.max_mr = attr.max_mr;
  345. resp.max_pd = attr.max_pd;
  346. resp.max_qp_rd_atom = attr.max_qp_rd_atom;
  347. resp.max_ee_rd_atom = attr.max_ee_rd_atom;
  348. resp.max_res_rd_atom = attr.max_res_rd_atom;
  349. resp.max_qp_init_rd_atom = attr.max_qp_init_rd_atom;
  350. resp.max_ee_init_rd_atom = attr.max_ee_init_rd_atom;
  351. resp.atomic_cap = attr.atomic_cap;
  352. resp.max_ee = attr.max_ee;
  353. resp.max_rdd = attr.max_rdd;
  354. resp.max_mw = attr.max_mw;
  355. resp.max_raw_ipv6_qp = attr.max_raw_ipv6_qp;
  356. resp.max_raw_ethy_qp = attr.max_raw_ethy_qp;
  357. resp.max_mcast_grp = attr.max_mcast_grp;
  358. resp.max_mcast_qp_attach = attr.max_mcast_qp_attach;
  359. resp.max_total_mcast_qp_attach = attr.max_total_mcast_qp_attach;
  360. resp.max_ah = attr.max_ah;
  361. resp.max_fmr = attr.max_fmr;
  362. resp.max_map_per_fmr = attr.max_map_per_fmr;
  363. resp.max_srq = attr.max_srq;
  364. resp.max_srq_wr = attr.max_srq_wr;
  365. resp.max_srq_sge = attr.max_srq_sge;
  366. resp.max_pkeys = attr.max_pkeys;
  367. resp.local_ca_ack_delay = attr.local_ca_ack_delay;
  368. resp.phys_port_cnt = file->device->ib_dev->phys_port_cnt;
  369. if (copy_to_user((void __user *) (unsigned long) cmd.response,
  370. &resp, sizeof resp))
  371. return -EFAULT;
  372. return in_len;
  373. }
  374. ssize_t ib_uverbs_query_port(struct ib_uverbs_file *file,
  375. const char __user *buf,
  376. int in_len, int out_len)
  377. {
  378. struct ib_uverbs_query_port cmd;
  379. struct ib_uverbs_query_port_resp resp;
  380. struct ib_port_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_port(file->device->ib_dev, cmd.port_num, &attr);
  387. if (ret)
  388. return ret;
  389. memset(&resp, 0, sizeof resp);
  390. resp.state = attr.state;
  391. resp.max_mtu = attr.max_mtu;
  392. resp.active_mtu = attr.active_mtu;
  393. resp.gid_tbl_len = attr.gid_tbl_len;
  394. resp.port_cap_flags = attr.port_cap_flags;
  395. resp.max_msg_sz = attr.max_msg_sz;
  396. resp.bad_pkey_cntr = attr.bad_pkey_cntr;
  397. resp.qkey_viol_cntr = attr.qkey_viol_cntr;
  398. resp.pkey_tbl_len = attr.pkey_tbl_len;
  399. resp.lid = attr.lid;
  400. resp.sm_lid = attr.sm_lid;
  401. resp.lmc = attr.lmc;
  402. resp.max_vl_num = attr.max_vl_num;
  403. resp.sm_sl = attr.sm_sl;
  404. resp.subnet_timeout = attr.subnet_timeout;
  405. resp.init_type_reply = attr.init_type_reply;
  406. resp.active_width = attr.active_width;
  407. resp.active_speed = attr.active_speed;
  408. resp.phys_state = attr.phys_state;
  409. resp.link_layer = rdma_port_get_link_layer(file->device->ib_dev,
  410. cmd.port_num);
  411. if (copy_to_user((void __user *) (unsigned long) cmd.response,
  412. &resp, sizeof resp))
  413. return -EFAULT;
  414. return in_len;
  415. }
  416. ssize_t ib_uverbs_alloc_pd(struct ib_uverbs_file *file,
  417. const char __user *buf,
  418. int in_len, int out_len)
  419. {
  420. struct ib_uverbs_alloc_pd cmd;
  421. struct ib_uverbs_alloc_pd_resp resp;
  422. struct ib_udata udata;
  423. struct ib_uobject *uobj;
  424. struct ib_pd *pd;
  425. int ret;
  426. if (out_len < sizeof resp)
  427. return -ENOSPC;
  428. if (copy_from_user(&cmd, buf, sizeof cmd))
  429. return -EFAULT;
  430. INIT_UDATA(&udata, buf + sizeof cmd,
  431. (unsigned long) cmd.response + sizeof resp,
  432. in_len - sizeof cmd, out_len - sizeof resp);
  433. uobj = kmalloc(sizeof *uobj, GFP_KERNEL);
  434. if (!uobj)
  435. return -ENOMEM;
  436. init_uobj(uobj, 0, file->ucontext, &pd_lock_key);
  437. down_write(&uobj->mutex);
  438. pd = file->device->ib_dev->alloc_pd(file->device->ib_dev,
  439. file->ucontext, &udata);
  440. if (IS_ERR(pd)) {
  441. ret = PTR_ERR(pd);
  442. goto err;
  443. }
  444. pd->device = file->device->ib_dev;
  445. pd->uobject = uobj;
  446. atomic_set(&pd->usecnt, 0);
  447. uobj->object = pd;
  448. ret = idr_add_uobj(&ib_uverbs_pd_idr, uobj);
  449. if (ret)
  450. goto err_idr;
  451. memset(&resp, 0, sizeof resp);
  452. resp.pd_handle = uobj->id;
  453. if (copy_to_user((void __user *) (unsigned long) cmd.response,
  454. &resp, sizeof resp)) {
  455. ret = -EFAULT;
  456. goto err_copy;
  457. }
  458. mutex_lock(&file->mutex);
  459. list_add_tail(&uobj->list, &file->ucontext->pd_list);
  460. mutex_unlock(&file->mutex);
  461. uobj->live = 1;
  462. up_write(&uobj->mutex);
  463. return in_len;
  464. err_copy:
  465. idr_remove_uobj(&ib_uverbs_pd_idr, uobj);
  466. err_idr:
  467. ib_dealloc_pd(pd);
  468. err:
  469. put_uobj_write(uobj);
  470. return ret;
  471. }
  472. ssize_t ib_uverbs_dealloc_pd(struct ib_uverbs_file *file,
  473. const char __user *buf,
  474. int in_len, int out_len)
  475. {
  476. struct ib_uverbs_dealloc_pd cmd;
  477. struct ib_uobject *uobj;
  478. int ret;
  479. if (copy_from_user(&cmd, buf, sizeof cmd))
  480. return -EFAULT;
  481. uobj = idr_write_uobj(&ib_uverbs_pd_idr, cmd.pd_handle, file->ucontext);
  482. if (!uobj)
  483. return -EINVAL;
  484. ret = ib_dealloc_pd(uobj->object);
  485. if (!ret)
  486. uobj->live = 0;
  487. put_uobj_write(uobj);
  488. if (ret)
  489. return ret;
  490. idr_remove_uobj(&ib_uverbs_pd_idr, uobj);
  491. mutex_lock(&file->mutex);
  492. list_del(&uobj->list);
  493. mutex_unlock(&file->mutex);
  494. put_uobj(uobj);
  495. return in_len;
  496. }
  497. struct xrcd_table_entry {
  498. struct rb_node node;
  499. struct ib_xrcd *xrcd;
  500. struct inode *inode;
  501. };
  502. static int xrcd_table_insert(struct ib_uverbs_device *dev,
  503. struct inode *inode,
  504. struct ib_xrcd *xrcd)
  505. {
  506. struct xrcd_table_entry *entry, *scan;
  507. struct rb_node **p = &dev->xrcd_tree.rb_node;
  508. struct rb_node *parent = NULL;
  509. entry = kmalloc(sizeof *entry, GFP_KERNEL);
  510. if (!entry)
  511. return -ENOMEM;
  512. entry->xrcd = xrcd;
  513. entry->inode = inode;
  514. while (*p) {
  515. parent = *p;
  516. scan = rb_entry(parent, struct xrcd_table_entry, node);
  517. if (inode < scan->inode) {
  518. p = &(*p)->rb_left;
  519. } else if (inode > scan->inode) {
  520. p = &(*p)->rb_right;
  521. } else {
  522. kfree(entry);
  523. return -EEXIST;
  524. }
  525. }
  526. rb_link_node(&entry->node, parent, p);
  527. rb_insert_color(&entry->node, &dev->xrcd_tree);
  528. igrab(inode);
  529. return 0;
  530. }
  531. static struct xrcd_table_entry *xrcd_table_search(struct ib_uverbs_device *dev,
  532. struct inode *inode)
  533. {
  534. struct xrcd_table_entry *entry;
  535. struct rb_node *p = dev->xrcd_tree.rb_node;
  536. while (p) {
  537. entry = rb_entry(p, struct xrcd_table_entry, node);
  538. if (inode < entry->inode)
  539. p = p->rb_left;
  540. else if (inode > entry->inode)
  541. p = p->rb_right;
  542. else
  543. return entry;
  544. }
  545. return NULL;
  546. }
  547. static struct ib_xrcd *find_xrcd(struct ib_uverbs_device *dev, struct inode *inode)
  548. {
  549. struct xrcd_table_entry *entry;
  550. entry = xrcd_table_search(dev, inode);
  551. if (!entry)
  552. return NULL;
  553. return entry->xrcd;
  554. }
  555. static void xrcd_table_delete(struct ib_uverbs_device *dev,
  556. struct inode *inode)
  557. {
  558. struct xrcd_table_entry *entry;
  559. entry = xrcd_table_search(dev, inode);
  560. if (entry) {
  561. iput(inode);
  562. rb_erase(&entry->node, &dev->xrcd_tree);
  563. kfree(entry);
  564. }
  565. }
  566. ssize_t ib_uverbs_open_xrcd(struct ib_uverbs_file *file,
  567. const char __user *buf, int in_len,
  568. int out_len)
  569. {
  570. struct ib_uverbs_open_xrcd cmd;
  571. struct ib_uverbs_open_xrcd_resp resp;
  572. struct ib_udata udata;
  573. struct ib_uxrcd_object *obj;
  574. struct ib_xrcd *xrcd = NULL;
  575. struct file *f = NULL;
  576. struct inode *inode = NULL;
  577. int ret = 0;
  578. int new_xrcd = 0;
  579. if (out_len < sizeof resp)
  580. return -ENOSPC;
  581. if (copy_from_user(&cmd, buf, sizeof cmd))
  582. return -EFAULT;
  583. INIT_UDATA(&udata, buf + sizeof cmd,
  584. (unsigned long) cmd.response + sizeof resp,
  585. in_len - sizeof cmd, out_len - sizeof resp);
  586. mutex_lock(&file->device->xrcd_tree_mutex);
  587. if (cmd.fd != -1) {
  588. /* search for file descriptor */
  589. f = fget(cmd.fd);
  590. if (!f) {
  591. ret = -EBADF;
  592. goto err_tree_mutex_unlock;
  593. }
  594. inode = f->f_dentry->d_inode;
  595. if (!inode) {
  596. ret = -EBADF;
  597. goto err_tree_mutex_unlock;
  598. }
  599. xrcd = find_xrcd(file->device, inode);
  600. if (!xrcd && !(cmd.oflags & O_CREAT)) {
  601. /* no file descriptor. Need CREATE flag */
  602. ret = -EAGAIN;
  603. goto err_tree_mutex_unlock;
  604. }
  605. if (xrcd && cmd.oflags & O_EXCL) {
  606. ret = -EINVAL;
  607. goto err_tree_mutex_unlock;
  608. }
  609. }
  610. obj = kmalloc(sizeof *obj, GFP_KERNEL);
  611. if (!obj) {
  612. ret = -ENOMEM;
  613. goto err_tree_mutex_unlock;
  614. }
  615. init_uobj(&obj->uobject, 0, file->ucontext, &xrcd_lock_key);
  616. down_write(&obj->uobject.mutex);
  617. if (!xrcd) {
  618. xrcd = file->device->ib_dev->alloc_xrcd(file->device->ib_dev,
  619. file->ucontext, &udata);
  620. if (IS_ERR(xrcd)) {
  621. ret = PTR_ERR(xrcd);
  622. goto err;
  623. }
  624. xrcd->inode = inode;
  625. xrcd->device = file->device->ib_dev;
  626. atomic_set(&xrcd->usecnt, 0);
  627. mutex_init(&xrcd->tgt_qp_mutex);
  628. INIT_LIST_HEAD(&xrcd->tgt_qp_list);
  629. new_xrcd = 1;
  630. }
  631. atomic_set(&obj->refcnt, 0);
  632. obj->uobject.object = xrcd;
  633. ret = idr_add_uobj(&ib_uverbs_xrcd_idr, &obj->uobject);
  634. if (ret)
  635. goto err_idr;
  636. memset(&resp, 0, sizeof resp);
  637. resp.xrcd_handle = obj->uobject.id;
  638. if (inode) {
  639. if (new_xrcd) {
  640. /* create new inode/xrcd table entry */
  641. ret = xrcd_table_insert(file->device, inode, xrcd);
  642. if (ret)
  643. goto err_insert_xrcd;
  644. }
  645. atomic_inc(&xrcd->usecnt);
  646. }
  647. if (copy_to_user((void __user *) (unsigned long) cmd.response,
  648. &resp, sizeof resp)) {
  649. ret = -EFAULT;
  650. goto err_copy;
  651. }
  652. if (f)
  653. fput(f);
  654. mutex_lock(&file->mutex);
  655. list_add_tail(&obj->uobject.list, &file->ucontext->xrcd_list);
  656. mutex_unlock(&file->mutex);
  657. obj->uobject.live = 1;
  658. up_write(&obj->uobject.mutex);
  659. mutex_unlock(&file->device->xrcd_tree_mutex);
  660. return in_len;
  661. err_copy:
  662. if (inode) {
  663. if (new_xrcd)
  664. xrcd_table_delete(file->device, inode);
  665. atomic_dec(&xrcd->usecnt);
  666. }
  667. err_insert_xrcd:
  668. idr_remove_uobj(&ib_uverbs_xrcd_idr, &obj->uobject);
  669. err_idr:
  670. ib_dealloc_xrcd(xrcd);
  671. err:
  672. put_uobj_write(&obj->uobject);
  673. err_tree_mutex_unlock:
  674. if (f)
  675. fput(f);
  676. mutex_unlock(&file->device->xrcd_tree_mutex);
  677. return ret;
  678. }
  679. ssize_t ib_uverbs_close_xrcd(struct ib_uverbs_file *file,
  680. const char __user *buf, int in_len,
  681. int out_len)
  682. {
  683. struct ib_uverbs_close_xrcd cmd;
  684. struct ib_uobject *uobj;
  685. struct ib_xrcd *xrcd = NULL;
  686. struct inode *inode = NULL;
  687. struct ib_uxrcd_object *obj;
  688. int live;
  689. int ret = 0;
  690. if (copy_from_user(&cmd, buf, sizeof cmd))
  691. return -EFAULT;
  692. mutex_lock(&file->device->xrcd_tree_mutex);
  693. uobj = idr_write_uobj(&ib_uverbs_xrcd_idr, cmd.xrcd_handle, file->ucontext);
  694. if (!uobj) {
  695. ret = -EINVAL;
  696. goto out;
  697. }
  698. xrcd = uobj->object;
  699. inode = xrcd->inode;
  700. obj = container_of(uobj, struct ib_uxrcd_object, uobject);
  701. if (atomic_read(&obj->refcnt)) {
  702. put_uobj_write(uobj);
  703. ret = -EBUSY;
  704. goto out;
  705. }
  706. if (!inode || atomic_dec_and_test(&xrcd->usecnt)) {
  707. ret = ib_dealloc_xrcd(uobj->object);
  708. if (!ret)
  709. uobj->live = 0;
  710. }
  711. live = uobj->live;
  712. if (inode && ret)
  713. atomic_inc(&xrcd->usecnt);
  714. put_uobj_write(uobj);
  715. if (ret)
  716. goto out;
  717. if (inode && !live)
  718. xrcd_table_delete(file->device, inode);
  719. idr_remove_uobj(&ib_uverbs_xrcd_idr, uobj);
  720. mutex_lock(&file->mutex);
  721. list_del(&uobj->list);
  722. mutex_unlock(&file->mutex);
  723. put_uobj(uobj);
  724. ret = in_len;
  725. out:
  726. mutex_unlock(&file->device->xrcd_tree_mutex);
  727. return ret;
  728. }
  729. void ib_uverbs_dealloc_xrcd(struct ib_uverbs_device *dev,
  730. struct ib_xrcd *xrcd)
  731. {
  732. struct inode *inode;
  733. inode = xrcd->inode;
  734. if (inode && !atomic_dec_and_test(&xrcd->usecnt))
  735. return;
  736. ib_dealloc_xrcd(xrcd);
  737. if (inode)
  738. xrcd_table_delete(dev, inode);
  739. }
  740. ssize_t ib_uverbs_reg_mr(struct ib_uverbs_file *file,
  741. const char __user *buf, int in_len,
  742. int out_len)
  743. {
  744. struct ib_uverbs_reg_mr cmd;
  745. struct ib_uverbs_reg_mr_resp resp;
  746. struct ib_udata udata;
  747. struct ib_uobject *uobj;
  748. struct ib_pd *pd;
  749. struct ib_mr *mr;
  750. int ret;
  751. if (out_len < sizeof resp)
  752. return -ENOSPC;
  753. if (copy_from_user(&cmd, buf, sizeof cmd))
  754. return -EFAULT;
  755. INIT_UDATA(&udata, buf + sizeof cmd,
  756. (unsigned long) cmd.response + sizeof resp,
  757. in_len - sizeof cmd, out_len - sizeof resp);
  758. if ((cmd.start & ~PAGE_MASK) != (cmd.hca_va & ~PAGE_MASK))
  759. return -EINVAL;
  760. /*
  761. * Local write permission is required if remote write or
  762. * remote atomic permission is also requested.
  763. */
  764. if (cmd.access_flags & (IB_ACCESS_REMOTE_ATOMIC | IB_ACCESS_REMOTE_WRITE) &&
  765. !(cmd.access_flags & IB_ACCESS_LOCAL_WRITE))
  766. return -EINVAL;
  767. uobj = kmalloc(sizeof *uobj, GFP_KERNEL);
  768. if (!uobj)
  769. return -ENOMEM;
  770. init_uobj(uobj, 0, file->ucontext, &mr_lock_key);
  771. down_write(&uobj->mutex);
  772. pd = idr_read_pd(cmd.pd_handle, file->ucontext);
  773. if (!pd) {
  774. ret = -EINVAL;
  775. goto err_free;
  776. }
  777. mr = pd->device->reg_user_mr(pd, cmd.start, cmd.length, cmd.hca_va,
  778. cmd.access_flags, &udata);
  779. if (IS_ERR(mr)) {
  780. ret = PTR_ERR(mr);
  781. goto err_put;
  782. }
  783. mr->device = pd->device;
  784. mr->pd = pd;
  785. mr->uobject = uobj;
  786. atomic_inc(&pd->usecnt);
  787. atomic_set(&mr->usecnt, 0);
  788. uobj->object = mr;
  789. ret = idr_add_uobj(&ib_uverbs_mr_idr, uobj);
  790. if (ret)
  791. goto err_unreg;
  792. memset(&resp, 0, sizeof resp);
  793. resp.lkey = mr->lkey;
  794. resp.rkey = mr->rkey;
  795. resp.mr_handle = uobj->id;
  796. if (copy_to_user((void __user *) (unsigned long) cmd.response,
  797. &resp, sizeof resp)) {
  798. ret = -EFAULT;
  799. goto err_copy;
  800. }
  801. put_pd_read(pd);
  802. mutex_lock(&file->mutex);
  803. list_add_tail(&uobj->list, &file->ucontext->mr_list);
  804. mutex_unlock(&file->mutex);
  805. uobj->live = 1;
  806. up_write(&uobj->mutex);
  807. return in_len;
  808. err_copy:
  809. idr_remove_uobj(&ib_uverbs_mr_idr, uobj);
  810. err_unreg:
  811. ib_dereg_mr(mr);
  812. err_put:
  813. put_pd_read(pd);
  814. err_free:
  815. put_uobj_write(uobj);
  816. return ret;
  817. }
  818. ssize_t ib_uverbs_dereg_mr(struct ib_uverbs_file *file,
  819. const char __user *buf, int in_len,
  820. int out_len)
  821. {
  822. struct ib_uverbs_dereg_mr cmd;
  823. struct ib_mr *mr;
  824. struct ib_uobject *uobj;
  825. int ret = -EINVAL;
  826. if (copy_from_user(&cmd, buf, sizeof cmd))
  827. return -EFAULT;
  828. uobj = idr_write_uobj(&ib_uverbs_mr_idr, cmd.mr_handle, file->ucontext);
  829. if (!uobj)
  830. return -EINVAL;
  831. mr = uobj->object;
  832. ret = ib_dereg_mr(mr);
  833. if (!ret)
  834. uobj->live = 0;
  835. put_uobj_write(uobj);
  836. if (ret)
  837. return ret;
  838. idr_remove_uobj(&ib_uverbs_mr_idr, uobj);
  839. mutex_lock(&file->mutex);
  840. list_del(&uobj->list);
  841. mutex_unlock(&file->mutex);
  842. put_uobj(uobj);
  843. return in_len;
  844. }
  845. ssize_t ib_uverbs_create_comp_channel(struct ib_uverbs_file *file,
  846. const char __user *buf, int in_len,
  847. int out_len)
  848. {
  849. struct ib_uverbs_create_comp_channel cmd;
  850. struct ib_uverbs_create_comp_channel_resp resp;
  851. struct file *filp;
  852. int ret;
  853. if (out_len < sizeof resp)
  854. return -ENOSPC;
  855. if (copy_from_user(&cmd, buf, sizeof cmd))
  856. return -EFAULT;
  857. ret = get_unused_fd();
  858. if (ret < 0)
  859. return ret;
  860. resp.fd = ret;
  861. filp = ib_uverbs_alloc_event_file(file, 0);
  862. if (IS_ERR(filp)) {
  863. put_unused_fd(resp.fd);
  864. return PTR_ERR(filp);
  865. }
  866. if (copy_to_user((void __user *) (unsigned long) cmd.response,
  867. &resp, sizeof resp)) {
  868. put_unused_fd(resp.fd);
  869. fput(filp);
  870. return -EFAULT;
  871. }
  872. fd_install(resp.fd, filp);
  873. return in_len;
  874. }
  875. ssize_t ib_uverbs_create_cq(struct ib_uverbs_file *file,
  876. const char __user *buf, int in_len,
  877. int out_len)
  878. {
  879. struct ib_uverbs_create_cq cmd;
  880. struct ib_uverbs_create_cq_resp resp;
  881. struct ib_udata udata;
  882. struct ib_ucq_object *obj;
  883. struct ib_uverbs_event_file *ev_file = NULL;
  884. struct ib_cq *cq;
  885. int ret;
  886. if (out_len < sizeof resp)
  887. return -ENOSPC;
  888. if (copy_from_user(&cmd, buf, sizeof cmd))
  889. return -EFAULT;
  890. INIT_UDATA(&udata, buf + sizeof cmd,
  891. (unsigned long) cmd.response + sizeof resp,
  892. in_len - sizeof cmd, out_len - sizeof resp);
  893. if (cmd.comp_vector >= file->device->num_comp_vectors)
  894. return -EINVAL;
  895. obj = kmalloc(sizeof *obj, GFP_KERNEL);
  896. if (!obj)
  897. return -ENOMEM;
  898. init_uobj(&obj->uobject, cmd.user_handle, file->ucontext, &cq_lock_key);
  899. down_write(&obj->uobject.mutex);
  900. if (cmd.comp_channel >= 0) {
  901. ev_file = ib_uverbs_lookup_comp_file(cmd.comp_channel);
  902. if (!ev_file) {
  903. ret = -EINVAL;
  904. goto err;
  905. }
  906. }
  907. obj->uverbs_file = file;
  908. obj->comp_events_reported = 0;
  909. obj->async_events_reported = 0;
  910. INIT_LIST_HEAD(&obj->comp_list);
  911. INIT_LIST_HEAD(&obj->async_list);
  912. cq = file->device->ib_dev->create_cq(file->device->ib_dev, cmd.cqe,
  913. cmd.comp_vector,
  914. file->ucontext, &udata);
  915. if (IS_ERR(cq)) {
  916. ret = PTR_ERR(cq);
  917. goto err_file;
  918. }
  919. cq->device = file->device->ib_dev;
  920. cq->uobject = &obj->uobject;
  921. cq->comp_handler = ib_uverbs_comp_handler;
  922. cq->event_handler = ib_uverbs_cq_event_handler;
  923. cq->cq_context = ev_file;
  924. atomic_set(&cq->usecnt, 0);
  925. obj->uobject.object = cq;
  926. ret = idr_add_uobj(&ib_uverbs_cq_idr, &obj->uobject);
  927. if (ret)
  928. goto err_free;
  929. memset(&resp, 0, sizeof resp);
  930. resp.cq_handle = obj->uobject.id;
  931. resp.cqe = cq->cqe;
  932. if (copy_to_user((void __user *) (unsigned long) cmd.response,
  933. &resp, sizeof resp)) {
  934. ret = -EFAULT;
  935. goto err_copy;
  936. }
  937. mutex_lock(&file->mutex);
  938. list_add_tail(&obj->uobject.list, &file->ucontext->cq_list);
  939. mutex_unlock(&file->mutex);
  940. obj->uobject.live = 1;
  941. up_write(&obj->uobject.mutex);
  942. return in_len;
  943. err_copy:
  944. idr_remove_uobj(&ib_uverbs_cq_idr, &obj->uobject);
  945. err_free:
  946. ib_destroy_cq(cq);
  947. err_file:
  948. if (ev_file)
  949. ib_uverbs_release_ucq(file, ev_file, obj);
  950. err:
  951. put_uobj_write(&obj->uobject);
  952. return ret;
  953. }
  954. ssize_t ib_uverbs_resize_cq(struct ib_uverbs_file *file,
  955. const char __user *buf, int in_len,
  956. int out_len)
  957. {
  958. struct ib_uverbs_resize_cq cmd;
  959. struct ib_uverbs_resize_cq_resp resp;
  960. struct ib_udata udata;
  961. struct ib_cq *cq;
  962. int ret = -EINVAL;
  963. if (copy_from_user(&cmd, buf, sizeof cmd))
  964. return -EFAULT;
  965. INIT_UDATA(&udata, buf + sizeof cmd,
  966. (unsigned long) cmd.response + sizeof resp,
  967. in_len - sizeof cmd, out_len - sizeof resp);
  968. cq = idr_read_cq(cmd.cq_handle, file->ucontext, 0);
  969. if (!cq)
  970. return -EINVAL;
  971. ret = cq->device->resize_cq(cq, cmd.cqe, &udata);
  972. if (ret)
  973. goto out;
  974. resp.cqe = cq->cqe;
  975. if (copy_to_user((void __user *) (unsigned long) cmd.response,
  976. &resp, sizeof resp.cqe))
  977. ret = -EFAULT;
  978. out:
  979. put_cq_read(cq);
  980. return ret ? ret : in_len;
  981. }
  982. static int copy_wc_to_user(void __user *dest, struct ib_wc *wc)
  983. {
  984. struct ib_uverbs_wc tmp;
  985. tmp.wr_id = wc->wr_id;
  986. tmp.status = wc->status;
  987. tmp.opcode = wc->opcode;
  988. tmp.vendor_err = wc->vendor_err;
  989. tmp.byte_len = wc->byte_len;
  990. tmp.ex.imm_data = (__u32 __force) wc->ex.imm_data;
  991. tmp.qp_num = wc->qp->qp_num;
  992. tmp.src_qp = wc->src_qp;
  993. tmp.wc_flags = wc->wc_flags;
  994. tmp.pkey_index = wc->pkey_index;
  995. tmp.slid = wc->slid;
  996. tmp.sl = wc->sl;
  997. tmp.dlid_path_bits = wc->dlid_path_bits;
  998. tmp.port_num = wc->port_num;
  999. tmp.reserved = 0;
  1000. if (copy_to_user(dest, &tmp, sizeof tmp))
  1001. return -EFAULT;
  1002. return 0;
  1003. }
  1004. ssize_t ib_uverbs_poll_cq(struct ib_uverbs_file *file,
  1005. const char __user *buf, int in_len,
  1006. int out_len)
  1007. {
  1008. struct ib_uverbs_poll_cq cmd;
  1009. struct ib_uverbs_poll_cq_resp resp;
  1010. u8 __user *header_ptr;
  1011. u8 __user *data_ptr;
  1012. struct ib_cq *cq;
  1013. struct ib_wc wc;
  1014. int ret;
  1015. if (copy_from_user(&cmd, buf, sizeof cmd))
  1016. return -EFAULT;
  1017. cq = idr_read_cq(cmd.cq_handle, file->ucontext, 0);
  1018. if (!cq)
  1019. return -EINVAL;
  1020. /* we copy a struct ib_uverbs_poll_cq_resp to user space */
  1021. header_ptr = (void __user *)(unsigned long) cmd.response;
  1022. data_ptr = header_ptr + sizeof resp;
  1023. memset(&resp, 0, sizeof resp);
  1024. while (resp.count < cmd.ne) {
  1025. ret = ib_poll_cq(cq, 1, &wc);
  1026. if (ret < 0)
  1027. goto out_put;
  1028. if (!ret)
  1029. break;
  1030. ret = copy_wc_to_user(data_ptr, &wc);
  1031. if (ret)
  1032. goto out_put;
  1033. data_ptr += sizeof(struct ib_uverbs_wc);
  1034. ++resp.count;
  1035. }
  1036. if (copy_to_user(header_ptr, &resp, sizeof resp)) {
  1037. ret = -EFAULT;
  1038. goto out_put;
  1039. }
  1040. ret = in_len;
  1041. out_put:
  1042. put_cq_read(cq);
  1043. return ret;
  1044. }
  1045. ssize_t ib_uverbs_req_notify_cq(struct ib_uverbs_file *file,
  1046. const char __user *buf, int in_len,
  1047. int out_len)
  1048. {
  1049. struct ib_uverbs_req_notify_cq cmd;
  1050. struct ib_cq *cq;
  1051. if (copy_from_user(&cmd, buf, sizeof cmd))
  1052. return -EFAULT;
  1053. cq = idr_read_cq(cmd.cq_handle, file->ucontext, 0);
  1054. if (!cq)
  1055. return -EINVAL;
  1056. ib_req_notify_cq(cq, cmd.solicited_only ?
  1057. IB_CQ_SOLICITED : IB_CQ_NEXT_COMP);
  1058. put_cq_read(cq);
  1059. return in_len;
  1060. }
  1061. ssize_t ib_uverbs_destroy_cq(struct ib_uverbs_file *file,
  1062. const char __user *buf, int in_len,
  1063. int out_len)
  1064. {
  1065. struct ib_uverbs_destroy_cq cmd;
  1066. struct ib_uverbs_destroy_cq_resp resp;
  1067. struct ib_uobject *uobj;
  1068. struct ib_cq *cq;
  1069. struct ib_ucq_object *obj;
  1070. struct ib_uverbs_event_file *ev_file;
  1071. int ret = -EINVAL;
  1072. if (copy_from_user(&cmd, buf, sizeof cmd))
  1073. return -EFAULT;
  1074. uobj = idr_write_uobj(&ib_uverbs_cq_idr, cmd.cq_handle, file->ucontext);
  1075. if (!uobj)
  1076. return -EINVAL;
  1077. cq = uobj->object;
  1078. ev_file = cq->cq_context;
  1079. obj = container_of(cq->uobject, struct ib_ucq_object, uobject);
  1080. ret = ib_destroy_cq(cq);
  1081. if (!ret)
  1082. uobj->live = 0;
  1083. put_uobj_write(uobj);
  1084. if (ret)
  1085. return ret;
  1086. idr_remove_uobj(&ib_uverbs_cq_idr, uobj);
  1087. mutex_lock(&file->mutex);
  1088. list_del(&uobj->list);
  1089. mutex_unlock(&file->mutex);
  1090. ib_uverbs_release_ucq(file, ev_file, obj);
  1091. memset(&resp, 0, sizeof resp);
  1092. resp.comp_events_reported = obj->comp_events_reported;
  1093. resp.async_events_reported = obj->async_events_reported;
  1094. put_uobj(uobj);
  1095. if (copy_to_user((void __user *) (unsigned long) cmd.response,
  1096. &resp, sizeof resp))
  1097. return -EFAULT;
  1098. return in_len;
  1099. }
  1100. ssize_t ib_uverbs_create_qp(struct ib_uverbs_file *file,
  1101. const char __user *buf, int in_len,
  1102. int out_len)
  1103. {
  1104. struct ib_uverbs_create_qp cmd;
  1105. struct ib_uverbs_create_qp_resp resp;
  1106. struct ib_udata udata;
  1107. struct ib_uqp_object *obj;
  1108. struct ib_device *device;
  1109. struct ib_pd *pd = NULL;
  1110. struct ib_xrcd *xrcd = NULL;
  1111. struct ib_uobject *uninitialized_var(xrcd_uobj);
  1112. struct ib_cq *scq = NULL, *rcq = NULL;
  1113. struct ib_srq *srq = NULL;
  1114. struct ib_qp *qp;
  1115. struct ib_qp_init_attr attr;
  1116. int ret;
  1117. if (out_len < sizeof resp)
  1118. return -ENOSPC;
  1119. if (copy_from_user(&cmd, buf, sizeof cmd))
  1120. return -EFAULT;
  1121. INIT_UDATA(&udata, buf + sizeof cmd,
  1122. (unsigned long) cmd.response + sizeof resp,
  1123. in_len - sizeof cmd, out_len - sizeof resp);
  1124. obj = kmalloc(sizeof *obj, GFP_KERNEL);
  1125. if (!obj)
  1126. return -ENOMEM;
  1127. init_uobj(&obj->uevent.uobject, cmd.user_handle, file->ucontext, &qp_lock_key);
  1128. down_write(&obj->uevent.uobject.mutex);
  1129. if (cmd.qp_type == IB_QPT_XRC_TGT) {
  1130. xrcd = idr_read_xrcd(cmd.pd_handle, file->ucontext, &xrcd_uobj);
  1131. if (!xrcd) {
  1132. ret = -EINVAL;
  1133. goto err_put;
  1134. }
  1135. device = xrcd->device;
  1136. } else {
  1137. pd = idr_read_pd(cmd.pd_handle, file->ucontext);
  1138. scq = idr_read_cq(cmd.send_cq_handle, file->ucontext, 0);
  1139. if (!pd || !scq) {
  1140. ret = -EINVAL;
  1141. goto err_put;
  1142. }
  1143. if (cmd.qp_type == IB_QPT_XRC_INI) {
  1144. cmd.max_recv_wr = cmd.max_recv_sge = 0;
  1145. } else {
  1146. if (cmd.is_srq) {
  1147. srq = idr_read_srq(cmd.srq_handle, file->ucontext);
  1148. if (!srq || srq->srq_type != IB_SRQT_BASIC) {
  1149. ret = -EINVAL;
  1150. goto err_put;
  1151. }
  1152. }
  1153. rcq = (cmd.recv_cq_handle == cmd.send_cq_handle) ?
  1154. scq : idr_read_cq(cmd.recv_cq_handle, file->ucontext, 1);
  1155. if (!rcq) {
  1156. ret = -EINVAL;
  1157. goto err_put;
  1158. }
  1159. }
  1160. device = pd->device;
  1161. }
  1162. attr.event_handler = ib_uverbs_qp_event_handler;
  1163. attr.qp_context = file;
  1164. attr.send_cq = scq;
  1165. attr.recv_cq = rcq;
  1166. attr.srq = srq;
  1167. attr.xrcd = xrcd;
  1168. attr.sq_sig_type = cmd.sq_sig_all ? IB_SIGNAL_ALL_WR : IB_SIGNAL_REQ_WR;
  1169. attr.qp_type = cmd.qp_type;
  1170. attr.create_flags = 0;
  1171. attr.cap.max_send_wr = cmd.max_send_wr;
  1172. attr.cap.max_recv_wr = cmd.max_recv_wr;
  1173. attr.cap.max_send_sge = cmd.max_send_sge;
  1174. attr.cap.max_recv_sge = cmd.max_recv_sge;
  1175. attr.cap.max_inline_data = cmd.max_inline_data;
  1176. obj->uevent.events_reported = 0;
  1177. INIT_LIST_HEAD(&obj->uevent.event_list);
  1178. INIT_LIST_HEAD(&obj->mcast_list);
  1179. if (cmd.qp_type == IB_QPT_XRC_TGT)
  1180. qp = ib_create_qp(pd, &attr);
  1181. else
  1182. qp = device->create_qp(pd, &attr, &udata);
  1183. if (IS_ERR(qp)) {
  1184. ret = PTR_ERR(qp);
  1185. goto err_put;
  1186. }
  1187. if (cmd.qp_type != IB_QPT_XRC_TGT) {
  1188. qp->real_qp = qp;
  1189. qp->device = device;
  1190. qp->pd = pd;
  1191. qp->send_cq = attr.send_cq;
  1192. qp->recv_cq = attr.recv_cq;
  1193. qp->srq = attr.srq;
  1194. qp->event_handler = attr.event_handler;
  1195. qp->qp_context = attr.qp_context;
  1196. qp->qp_type = attr.qp_type;
  1197. atomic_set(&qp->usecnt, 0);
  1198. atomic_inc(&pd->usecnt);
  1199. atomic_inc(&attr.send_cq->usecnt);
  1200. if (attr.recv_cq)
  1201. atomic_inc(&attr.recv_cq->usecnt);
  1202. if (attr.srq)
  1203. atomic_inc(&attr.srq->usecnt);
  1204. }
  1205. qp->uobject = &obj->uevent.uobject;
  1206. obj->uevent.uobject.object = qp;
  1207. ret = idr_add_uobj(&ib_uverbs_qp_idr, &obj->uevent.uobject);
  1208. if (ret)
  1209. goto err_destroy;
  1210. memset(&resp, 0, sizeof resp);
  1211. resp.qpn = qp->qp_num;
  1212. resp.qp_handle = obj->uevent.uobject.id;
  1213. resp.max_recv_sge = attr.cap.max_recv_sge;
  1214. resp.max_send_sge = attr.cap.max_send_sge;
  1215. resp.max_recv_wr = attr.cap.max_recv_wr;
  1216. resp.max_send_wr = attr.cap.max_send_wr;
  1217. resp.max_inline_data = attr.cap.max_inline_data;
  1218. if (copy_to_user((void __user *) (unsigned long) cmd.response,
  1219. &resp, sizeof resp)) {
  1220. ret = -EFAULT;
  1221. goto err_copy;
  1222. }
  1223. if (xrcd)
  1224. put_xrcd_read(xrcd_uobj);
  1225. if (pd)
  1226. put_pd_read(pd);
  1227. if (scq)
  1228. put_cq_read(scq);
  1229. if (rcq && rcq != scq)
  1230. put_cq_read(rcq);
  1231. if (srq)
  1232. put_srq_read(srq);
  1233. mutex_lock(&file->mutex);
  1234. list_add_tail(&obj->uevent.uobject.list, &file->ucontext->qp_list);
  1235. mutex_unlock(&file->mutex);
  1236. obj->uevent.uobject.live = 1;
  1237. up_write(&obj->uevent.uobject.mutex);
  1238. return in_len;
  1239. err_copy:
  1240. idr_remove_uobj(&ib_uverbs_qp_idr, &obj->uevent.uobject);
  1241. err_destroy:
  1242. ib_destroy_qp(qp);
  1243. err_put:
  1244. if (xrcd)
  1245. put_xrcd_read(xrcd_uobj);
  1246. if (pd)
  1247. put_pd_read(pd);
  1248. if (scq)
  1249. put_cq_read(scq);
  1250. if (rcq && rcq != scq)
  1251. put_cq_read(rcq);
  1252. if (srq)
  1253. put_srq_read(srq);
  1254. put_uobj_write(&obj->uevent.uobject);
  1255. return ret;
  1256. }
  1257. ssize_t ib_uverbs_open_qp(struct ib_uverbs_file *file,
  1258. const char __user *buf, int in_len, int out_len)
  1259. {
  1260. struct ib_uverbs_open_qp cmd;
  1261. struct ib_uverbs_create_qp_resp resp;
  1262. struct ib_udata udata;
  1263. struct ib_uqp_object *obj;
  1264. struct ib_xrcd *xrcd;
  1265. struct ib_uobject *uninitialized_var(xrcd_uobj);
  1266. struct ib_qp *qp;
  1267. struct ib_qp_open_attr attr;
  1268. int ret;
  1269. if (out_len < sizeof resp)
  1270. return -ENOSPC;
  1271. if (copy_from_user(&cmd, buf, sizeof cmd))
  1272. return -EFAULT;
  1273. INIT_UDATA(&udata, buf + sizeof cmd,
  1274. (unsigned long) cmd.response + sizeof resp,
  1275. in_len - sizeof cmd, out_len - sizeof resp);
  1276. obj = kmalloc(sizeof *obj, GFP_KERNEL);
  1277. if (!obj)
  1278. return -ENOMEM;
  1279. init_uobj(&obj->uevent.uobject, cmd.user_handle, file->ucontext, &qp_lock_key);
  1280. down_write(&obj->uevent.uobject.mutex);
  1281. xrcd = idr_read_xrcd(cmd.pd_handle, file->ucontext, &xrcd_uobj);
  1282. if (!xrcd) {
  1283. ret = -EINVAL;
  1284. goto err_put;
  1285. }
  1286. attr.event_handler = ib_uverbs_qp_event_handler;
  1287. attr.qp_context = file;
  1288. attr.qp_num = cmd.qpn;
  1289. attr.qp_type = cmd.qp_type;
  1290. obj->uevent.events_reported = 0;
  1291. INIT_LIST_HEAD(&obj->uevent.event_list);
  1292. INIT_LIST_HEAD(&obj->mcast_list);
  1293. qp = ib_open_qp(xrcd, &attr);
  1294. if (IS_ERR(qp)) {
  1295. ret = PTR_ERR(qp);
  1296. goto err_put;
  1297. }
  1298. qp->uobject = &obj->uevent.uobject;
  1299. obj->uevent.uobject.object = qp;
  1300. ret = idr_add_uobj(&ib_uverbs_qp_idr, &obj->uevent.uobject);
  1301. if (ret)
  1302. goto err_destroy;
  1303. memset(&resp, 0, sizeof resp);
  1304. resp.qpn = qp->qp_num;
  1305. resp.qp_handle = obj->uevent.uobject.id;
  1306. if (copy_to_user((void __user *) (unsigned long) cmd.response,
  1307. &resp, sizeof resp)) {
  1308. ret = -EFAULT;
  1309. goto err_remove;
  1310. }
  1311. put_xrcd_read(xrcd_uobj);
  1312. mutex_lock(&file->mutex);
  1313. list_add_tail(&obj->uevent.uobject.list, &file->ucontext->qp_list);
  1314. mutex_unlock(&file->mutex);
  1315. obj->uevent.uobject.live = 1;
  1316. up_write(&obj->uevent.uobject.mutex);
  1317. return in_len;
  1318. err_remove:
  1319. idr_remove_uobj(&ib_uverbs_qp_idr, &obj->uevent.uobject);
  1320. err_destroy:
  1321. ib_destroy_qp(qp);
  1322. err_put:
  1323. put_xrcd_read(xrcd_uobj);
  1324. put_uobj_write(&obj->uevent.uobject);
  1325. return ret;
  1326. }
  1327. ssize_t ib_uverbs_query_qp(struct ib_uverbs_file *file,
  1328. const char __user *buf, int in_len,
  1329. int out_len)
  1330. {
  1331. struct ib_uverbs_query_qp cmd;
  1332. struct ib_uverbs_query_qp_resp resp;
  1333. struct ib_qp *qp;
  1334. struct ib_qp_attr *attr;
  1335. struct ib_qp_init_attr *init_attr;
  1336. int ret;
  1337. if (copy_from_user(&cmd, buf, sizeof cmd))
  1338. return -EFAULT;
  1339. attr = kmalloc(sizeof *attr, GFP_KERNEL);
  1340. init_attr = kmalloc(sizeof *init_attr, GFP_KERNEL);
  1341. if (!attr || !init_attr) {
  1342. ret = -ENOMEM;
  1343. goto out;
  1344. }
  1345. qp = idr_read_qp(cmd.qp_handle, file->ucontext);
  1346. if (!qp) {
  1347. ret = -EINVAL;
  1348. goto out;
  1349. }
  1350. ret = ib_query_qp(qp, attr, cmd.attr_mask, init_attr);
  1351. put_qp_read(qp);
  1352. if (ret)
  1353. goto out;
  1354. memset(&resp, 0, sizeof resp);
  1355. resp.qp_state = attr->qp_state;
  1356. resp.cur_qp_state = attr->cur_qp_state;
  1357. resp.path_mtu = attr->path_mtu;
  1358. resp.path_mig_state = attr->path_mig_state;
  1359. resp.qkey = attr->qkey;
  1360. resp.rq_psn = attr->rq_psn;
  1361. resp.sq_psn = attr->sq_psn;
  1362. resp.dest_qp_num = attr->dest_qp_num;
  1363. resp.qp_access_flags = attr->qp_access_flags;
  1364. resp.pkey_index = attr->pkey_index;
  1365. resp.alt_pkey_index = attr->alt_pkey_index;
  1366. resp.sq_draining = attr->sq_draining;
  1367. resp.max_rd_atomic = attr->max_rd_atomic;
  1368. resp.max_dest_rd_atomic = attr->max_dest_rd_atomic;
  1369. resp.min_rnr_timer = attr->min_rnr_timer;
  1370. resp.port_num = attr->port_num;
  1371. resp.timeout = attr->timeout;
  1372. resp.retry_cnt = attr->retry_cnt;
  1373. resp.rnr_retry = attr->rnr_retry;
  1374. resp.alt_port_num = attr->alt_port_num;
  1375. resp.alt_timeout = attr->alt_timeout;
  1376. memcpy(resp.dest.dgid, attr->ah_attr.grh.dgid.raw, 16);
  1377. resp.dest.flow_label = attr->ah_attr.grh.flow_label;
  1378. resp.dest.sgid_index = attr->ah_attr.grh.sgid_index;
  1379. resp.dest.hop_limit = attr->ah_attr.grh.hop_limit;
  1380. resp.dest.traffic_class = attr->ah_attr.grh.traffic_class;
  1381. resp.dest.dlid = attr->ah_attr.dlid;
  1382. resp.dest.sl = attr->ah_attr.sl;
  1383. resp.dest.src_path_bits = attr->ah_attr.src_path_bits;
  1384. resp.dest.static_rate = attr->ah_attr.static_rate;
  1385. resp.dest.is_global = !!(attr->ah_attr.ah_flags & IB_AH_GRH);
  1386. resp.dest.port_num = attr->ah_attr.port_num;
  1387. memcpy(resp.alt_dest.dgid, attr->alt_ah_attr.grh.dgid.raw, 16);
  1388. resp.alt_dest.flow_label = attr->alt_ah_attr.grh.flow_label;
  1389. resp.alt_dest.sgid_index = attr->alt_ah_attr.grh.sgid_index;
  1390. resp.alt_dest.hop_limit = attr->alt_ah_attr.grh.hop_limit;
  1391. resp.alt_dest.traffic_class = attr->alt_ah_attr.grh.traffic_class;
  1392. resp.alt_dest.dlid = attr->alt_ah_attr.dlid;
  1393. resp.alt_dest.sl = attr->alt_ah_attr.sl;
  1394. resp.alt_dest.src_path_bits = attr->alt_ah_attr.src_path_bits;
  1395. resp.alt_dest.static_rate = attr->alt_ah_attr.static_rate;
  1396. resp.alt_dest.is_global = !!(attr->alt_ah_attr.ah_flags & IB_AH_GRH);
  1397. resp.alt_dest.port_num = attr->alt_ah_attr.port_num;
  1398. resp.max_send_wr = init_attr->cap.max_send_wr;
  1399. resp.max_recv_wr = init_attr->cap.max_recv_wr;
  1400. resp.max_send_sge = init_attr->cap.max_send_sge;
  1401. resp.max_recv_sge = init_attr->cap.max_recv_sge;
  1402. resp.max_inline_data = init_attr->cap.max_inline_data;
  1403. resp.sq_sig_all = init_attr->sq_sig_type == IB_SIGNAL_ALL_WR;
  1404. if (copy_to_user((void __user *) (unsigned long) cmd.response,
  1405. &resp, sizeof resp))
  1406. ret = -EFAULT;
  1407. out:
  1408. kfree(attr);
  1409. kfree(init_attr);
  1410. return ret ? ret : in_len;
  1411. }
  1412. /* Remove ignored fields set in the attribute mask */
  1413. static int modify_qp_mask(enum ib_qp_type qp_type, int mask)
  1414. {
  1415. switch (qp_type) {
  1416. case IB_QPT_XRC_INI:
  1417. return mask & ~(IB_QP_MAX_DEST_RD_ATOMIC | IB_QP_MIN_RNR_TIMER);
  1418. case IB_QPT_XRC_TGT:
  1419. return mask & ~(IB_QP_MAX_QP_RD_ATOMIC | IB_QP_RETRY_CNT |
  1420. IB_QP_RNR_RETRY);
  1421. default:
  1422. return mask;
  1423. }
  1424. }
  1425. ssize_t ib_uverbs_modify_qp(struct ib_uverbs_file *file,
  1426. const char __user *buf, int in_len,
  1427. int out_len)
  1428. {
  1429. struct ib_uverbs_modify_qp cmd;
  1430. struct ib_udata udata;
  1431. struct ib_qp *qp;
  1432. struct ib_qp_attr *attr;
  1433. int ret;
  1434. if (copy_from_user(&cmd, buf, sizeof cmd))
  1435. return -EFAULT;
  1436. INIT_UDATA(&udata, buf + sizeof cmd, NULL, in_len - sizeof cmd,
  1437. out_len);
  1438. attr = kmalloc(sizeof *attr, GFP_KERNEL);
  1439. if (!attr)
  1440. return -ENOMEM;
  1441. qp = idr_read_qp(cmd.qp_handle, file->ucontext);
  1442. if (!qp) {
  1443. ret = -EINVAL;
  1444. goto out;
  1445. }
  1446. attr->qp_state = cmd.qp_state;
  1447. attr->cur_qp_state = cmd.cur_qp_state;
  1448. attr->path_mtu = cmd.path_mtu;
  1449. attr->path_mig_state = cmd.path_mig_state;
  1450. attr->qkey = cmd.qkey;
  1451. attr->rq_psn = cmd.rq_psn;
  1452. attr->sq_psn = cmd.sq_psn;
  1453. attr->dest_qp_num = cmd.dest_qp_num;
  1454. attr->qp_access_flags = cmd.qp_access_flags;
  1455. attr->pkey_index = cmd.pkey_index;
  1456. attr->alt_pkey_index = cmd.alt_pkey_index;
  1457. attr->en_sqd_async_notify = cmd.en_sqd_async_notify;
  1458. attr->max_rd_atomic = cmd.max_rd_atomic;
  1459. attr->max_dest_rd_atomic = cmd.max_dest_rd_atomic;
  1460. attr->min_rnr_timer = cmd.min_rnr_timer;
  1461. attr->port_num = cmd.port_num;
  1462. attr->timeout = cmd.timeout;
  1463. attr->retry_cnt = cmd.retry_cnt;
  1464. attr->rnr_retry = cmd.rnr_retry;
  1465. attr->alt_port_num = cmd.alt_port_num;
  1466. attr->alt_timeout = cmd.alt_timeout;
  1467. memcpy(attr->ah_attr.grh.dgid.raw, cmd.dest.dgid, 16);
  1468. attr->ah_attr.grh.flow_label = cmd.dest.flow_label;
  1469. attr->ah_attr.grh.sgid_index = cmd.dest.sgid_index;
  1470. attr->ah_attr.grh.hop_limit = cmd.dest.hop_limit;
  1471. attr->ah_attr.grh.traffic_class = cmd.dest.traffic_class;
  1472. attr->ah_attr.dlid = cmd.dest.dlid;
  1473. attr->ah_attr.sl = cmd.dest.sl;
  1474. attr->ah_attr.src_path_bits = cmd.dest.src_path_bits;
  1475. attr->ah_attr.static_rate = cmd.dest.static_rate;
  1476. attr->ah_attr.ah_flags = cmd.dest.is_global ? IB_AH_GRH : 0;
  1477. attr->ah_attr.port_num = cmd.dest.port_num;
  1478. memcpy(attr->alt_ah_attr.grh.dgid.raw, cmd.alt_dest.dgid, 16);
  1479. attr->alt_ah_attr.grh.flow_label = cmd.alt_dest.flow_label;
  1480. attr->alt_ah_attr.grh.sgid_index = cmd.alt_dest.sgid_index;
  1481. attr->alt_ah_attr.grh.hop_limit = cmd.alt_dest.hop_limit;
  1482. attr->alt_ah_attr.grh.traffic_class = cmd.alt_dest.traffic_class;
  1483. attr->alt_ah_attr.dlid = cmd.alt_dest.dlid;
  1484. attr->alt_ah_attr.sl = cmd.alt_dest.sl;
  1485. attr->alt_ah_attr.src_path_bits = cmd.alt_dest.src_path_bits;
  1486. attr->alt_ah_attr.static_rate = cmd.alt_dest.static_rate;
  1487. attr->alt_ah_attr.ah_flags = cmd.alt_dest.is_global ? IB_AH_GRH : 0;
  1488. attr->alt_ah_attr.port_num = cmd.alt_dest.port_num;
  1489. if (qp->real_qp == qp) {
  1490. ret = qp->device->modify_qp(qp, attr,
  1491. modify_qp_mask(qp->qp_type, cmd.attr_mask), &udata);
  1492. } else {
  1493. ret = ib_modify_qp(qp, attr, modify_qp_mask(qp->qp_type, cmd.attr_mask));
  1494. }
  1495. put_qp_read(qp);
  1496. if (ret)
  1497. goto out;
  1498. ret = in_len;
  1499. out:
  1500. kfree(attr);
  1501. return ret;
  1502. }
  1503. ssize_t ib_uverbs_destroy_qp(struct ib_uverbs_file *file,
  1504. const char __user *buf, int in_len,
  1505. int out_len)
  1506. {
  1507. struct ib_uverbs_destroy_qp cmd;
  1508. struct ib_uverbs_destroy_qp_resp resp;
  1509. struct ib_uobject *uobj;
  1510. struct ib_qp *qp;
  1511. struct ib_uqp_object *obj;
  1512. int ret = -EINVAL;
  1513. if (copy_from_user(&cmd, buf, sizeof cmd))
  1514. return -EFAULT;
  1515. memset(&resp, 0, sizeof resp);
  1516. uobj = idr_write_uobj(&ib_uverbs_qp_idr, cmd.qp_handle, file->ucontext);
  1517. if (!uobj)
  1518. return -EINVAL;
  1519. qp = uobj->object;
  1520. obj = container_of(uobj, struct ib_uqp_object, uevent.uobject);
  1521. if (!list_empty(&obj->mcast_list)) {
  1522. put_uobj_write(uobj);
  1523. return -EBUSY;
  1524. }
  1525. ret = ib_destroy_qp(qp);
  1526. if (!ret)
  1527. uobj->live = 0;
  1528. put_uobj_write(uobj);
  1529. if (ret)
  1530. return ret;
  1531. idr_remove_uobj(&ib_uverbs_qp_idr, uobj);
  1532. mutex_lock(&file->mutex);
  1533. list_del(&uobj->list);
  1534. mutex_unlock(&file->mutex);
  1535. ib_uverbs_release_uevent(file, &obj->uevent);
  1536. resp.events_reported = obj->uevent.events_reported;
  1537. put_uobj(uobj);
  1538. if (copy_to_user((void __user *) (unsigned long) cmd.response,
  1539. &resp, sizeof resp))
  1540. return -EFAULT;
  1541. return in_len;
  1542. }
  1543. ssize_t ib_uverbs_post_send(struct ib_uverbs_file *file,
  1544. const char __user *buf, int in_len,
  1545. int out_len)
  1546. {
  1547. struct ib_uverbs_post_send cmd;
  1548. struct ib_uverbs_post_send_resp resp;
  1549. struct ib_uverbs_send_wr *user_wr;
  1550. struct ib_send_wr *wr = NULL, *last, *next, *bad_wr;
  1551. struct ib_qp *qp;
  1552. int i, sg_ind;
  1553. int is_ud;
  1554. ssize_t ret = -EINVAL;
  1555. if (copy_from_user(&cmd, buf, sizeof cmd))
  1556. return -EFAULT;
  1557. if (in_len < sizeof cmd + cmd.wqe_size * cmd.wr_count +
  1558. cmd.sge_count * sizeof (struct ib_uverbs_sge))
  1559. return -EINVAL;
  1560. if (cmd.wqe_size < sizeof (struct ib_uverbs_send_wr))
  1561. return -EINVAL;
  1562. user_wr = kmalloc(cmd.wqe_size, GFP_KERNEL);
  1563. if (!user_wr)
  1564. return -ENOMEM;
  1565. qp = idr_read_qp(cmd.qp_handle, file->ucontext);
  1566. if (!qp)
  1567. goto out;
  1568. is_ud = qp->qp_type == IB_QPT_UD;
  1569. sg_ind = 0;
  1570. last = NULL;
  1571. for (i = 0; i < cmd.wr_count; ++i) {
  1572. if (copy_from_user(user_wr,
  1573. buf + sizeof cmd + i * cmd.wqe_size,
  1574. cmd.wqe_size)) {
  1575. ret = -EFAULT;
  1576. goto out_put;
  1577. }
  1578. if (user_wr->num_sge + sg_ind > cmd.sge_count) {
  1579. ret = -EINVAL;
  1580. goto out_put;
  1581. }
  1582. next = kmalloc(ALIGN(sizeof *next, sizeof (struct ib_sge)) +
  1583. user_wr->num_sge * sizeof (struct ib_sge),
  1584. GFP_KERNEL);
  1585. if (!next) {
  1586. ret = -ENOMEM;
  1587. goto out_put;
  1588. }
  1589. if (!last)
  1590. wr = next;
  1591. else
  1592. last->next = next;
  1593. last = next;
  1594. next->next = NULL;
  1595. next->wr_id = user_wr->wr_id;
  1596. next->num_sge = user_wr->num_sge;
  1597. next->opcode = user_wr->opcode;
  1598. next->send_flags = user_wr->send_flags;
  1599. if (is_ud) {
  1600. if (next->opcode != IB_WR_SEND &&
  1601. next->opcode != IB_WR_SEND_WITH_IMM) {
  1602. ret = -EINVAL;
  1603. goto out_put;
  1604. }
  1605. next->wr.ud.ah = idr_read_ah(user_wr->wr.ud.ah,
  1606. file->ucontext);
  1607. if (!next->wr.ud.ah) {
  1608. ret = -EINVAL;
  1609. goto out_put;
  1610. }
  1611. next->wr.ud.remote_qpn = user_wr->wr.ud.remote_qpn;
  1612. next->wr.ud.remote_qkey = user_wr->wr.ud.remote_qkey;
  1613. } else {
  1614. switch (next->opcode) {
  1615. case IB_WR_RDMA_WRITE_WITH_IMM:
  1616. next->ex.imm_data =
  1617. (__be32 __force) user_wr->ex.imm_data;
  1618. case IB_WR_RDMA_WRITE:
  1619. case IB_WR_RDMA_READ:
  1620. next->wr.rdma.remote_addr =
  1621. user_wr->wr.rdma.remote_addr;
  1622. next->wr.rdma.rkey =
  1623. user_wr->wr.rdma.rkey;
  1624. break;
  1625. case IB_WR_SEND_WITH_IMM:
  1626. next->ex.imm_data =
  1627. (__be32 __force) user_wr->ex.imm_data;
  1628. break;
  1629. case IB_WR_SEND_WITH_INV:
  1630. next->ex.invalidate_rkey =
  1631. user_wr->ex.invalidate_rkey;
  1632. break;
  1633. case IB_WR_ATOMIC_CMP_AND_SWP:
  1634. case IB_WR_ATOMIC_FETCH_AND_ADD:
  1635. next->wr.atomic.remote_addr =
  1636. user_wr->wr.atomic.remote_addr;
  1637. next->wr.atomic.compare_add =
  1638. user_wr->wr.atomic.compare_add;
  1639. next->wr.atomic.swap = user_wr->wr.atomic.swap;
  1640. next->wr.atomic.rkey = user_wr->wr.atomic.rkey;
  1641. case IB_WR_SEND:
  1642. break;
  1643. default:
  1644. ret = -EINVAL;
  1645. goto out_put;
  1646. }
  1647. }
  1648. if (next->num_sge) {
  1649. next->sg_list = (void *) next +
  1650. ALIGN(sizeof *next, sizeof (struct ib_sge));
  1651. if (copy_from_user(next->sg_list,
  1652. buf + sizeof cmd +
  1653. cmd.wr_count * cmd.wqe_size +
  1654. sg_ind * sizeof (struct ib_sge),
  1655. next->num_sge * sizeof (struct ib_sge))) {
  1656. ret = -EFAULT;
  1657. goto out_put;
  1658. }
  1659. sg_ind += next->num_sge;
  1660. } else
  1661. next->sg_list = NULL;
  1662. }
  1663. resp.bad_wr = 0;
  1664. ret = qp->device->post_send(qp->real_qp, wr, &bad_wr);
  1665. if (ret)
  1666. for (next = wr; next; next = next->next) {
  1667. ++resp.bad_wr;
  1668. if (next == bad_wr)
  1669. break;
  1670. }
  1671. if (copy_to_user((void __user *) (unsigned long) cmd.response,
  1672. &resp, sizeof resp))
  1673. ret = -EFAULT;
  1674. out_put:
  1675. put_qp_read(qp);
  1676. while (wr) {
  1677. if (is_ud && wr->wr.ud.ah)
  1678. put_ah_read(wr->wr.ud.ah);
  1679. next = wr->next;
  1680. kfree(wr);
  1681. wr = next;
  1682. }
  1683. out:
  1684. kfree(user_wr);
  1685. return ret ? ret : in_len;
  1686. }
  1687. static struct ib_recv_wr *ib_uverbs_unmarshall_recv(const char __user *buf,
  1688. int in_len,
  1689. u32 wr_count,
  1690. u32 sge_count,
  1691. u32 wqe_size)
  1692. {
  1693. struct ib_uverbs_recv_wr *user_wr;
  1694. struct ib_recv_wr *wr = NULL, *last, *next;
  1695. int sg_ind;
  1696. int i;
  1697. int ret;
  1698. if (in_len < wqe_size * wr_count +
  1699. sge_count * sizeof (struct ib_uverbs_sge))
  1700. return ERR_PTR(-EINVAL);
  1701. if (wqe_size < sizeof (struct ib_uverbs_recv_wr))
  1702. return ERR_PTR(-EINVAL);
  1703. user_wr = kmalloc(wqe_size, GFP_KERNEL);
  1704. if (!user_wr)
  1705. return ERR_PTR(-ENOMEM);
  1706. sg_ind = 0;
  1707. last = NULL;
  1708. for (i = 0; i < wr_count; ++i) {
  1709. if (copy_from_user(user_wr, buf + i * wqe_size,
  1710. wqe_size)) {
  1711. ret = -EFAULT;
  1712. goto err;
  1713. }
  1714. if (user_wr->num_sge + sg_ind > sge_count) {
  1715. ret = -EINVAL;
  1716. goto err;
  1717. }
  1718. next = kmalloc(ALIGN(sizeof *next, sizeof (struct ib_sge)) +
  1719. user_wr->num_sge * sizeof (struct ib_sge),
  1720. GFP_KERNEL);
  1721. if (!next) {
  1722. ret = -ENOMEM;
  1723. goto err;
  1724. }
  1725. if (!last)
  1726. wr = next;
  1727. else
  1728. last->next = next;
  1729. last = next;
  1730. next->next = NULL;
  1731. next->wr_id = user_wr->wr_id;
  1732. next->num_sge = user_wr->num_sge;
  1733. if (next->num_sge) {
  1734. next->sg_list = (void *) next +
  1735. ALIGN(sizeof *next, sizeof (struct ib_sge));
  1736. if (copy_from_user(next->sg_list,
  1737. buf + wr_count * wqe_size +
  1738. sg_ind * sizeof (struct ib_sge),
  1739. next->num_sge * sizeof (struct ib_sge))) {
  1740. ret = -EFAULT;
  1741. goto err;
  1742. }
  1743. sg_ind += next->num_sge;
  1744. } else
  1745. next->sg_list = NULL;
  1746. }
  1747. kfree(user_wr);
  1748. return wr;
  1749. err:
  1750. kfree(user_wr);
  1751. while (wr) {
  1752. next = wr->next;
  1753. kfree(wr);
  1754. wr = next;
  1755. }
  1756. return ERR_PTR(ret);
  1757. }
  1758. ssize_t ib_uverbs_post_recv(struct ib_uverbs_file *file,
  1759. const char __user *buf, int in_len,
  1760. int out_len)
  1761. {
  1762. struct ib_uverbs_post_recv cmd;
  1763. struct ib_uverbs_post_recv_resp resp;
  1764. struct ib_recv_wr *wr, *next, *bad_wr;
  1765. struct ib_qp *qp;
  1766. ssize_t ret = -EINVAL;
  1767. if (copy_from_user(&cmd, buf, sizeof cmd))
  1768. return -EFAULT;
  1769. wr = ib_uverbs_unmarshall_recv(buf + sizeof cmd,
  1770. in_len - sizeof cmd, cmd.wr_count,
  1771. cmd.sge_count, cmd.wqe_size);
  1772. if (IS_ERR(wr))
  1773. return PTR_ERR(wr);
  1774. qp = idr_read_qp(cmd.qp_handle, file->ucontext);
  1775. if (!qp)
  1776. goto out;
  1777. resp.bad_wr = 0;
  1778. ret = qp->device->post_recv(qp->real_qp, wr, &bad_wr);
  1779. put_qp_read(qp);
  1780. if (ret)
  1781. for (next = wr; next; next = next->next) {
  1782. ++resp.bad_wr;
  1783. if (next == bad_wr)
  1784. break;
  1785. }
  1786. if (copy_to_user((void __user *) (unsigned long) cmd.response,
  1787. &resp, sizeof resp))
  1788. ret = -EFAULT;
  1789. out:
  1790. while (wr) {
  1791. next = wr->next;
  1792. kfree(wr);
  1793. wr = next;
  1794. }
  1795. return ret ? ret : in_len;
  1796. }
  1797. ssize_t ib_uverbs_post_srq_recv(struct ib_uverbs_file *file,
  1798. const char __user *buf, int in_len,
  1799. int out_len)
  1800. {
  1801. struct ib_uverbs_post_srq_recv cmd;
  1802. struct ib_uverbs_post_srq_recv_resp resp;
  1803. struct ib_recv_wr *wr, *next, *bad_wr;
  1804. struct ib_srq *srq;
  1805. ssize_t ret = -EINVAL;
  1806. if (copy_from_user(&cmd, buf, sizeof cmd))
  1807. return -EFAULT;
  1808. wr = ib_uverbs_unmarshall_recv(buf + sizeof cmd,
  1809. in_len - sizeof cmd, cmd.wr_count,
  1810. cmd.sge_count, cmd.wqe_size);
  1811. if (IS_ERR(wr))
  1812. return PTR_ERR(wr);
  1813. srq = idr_read_srq(cmd.srq_handle, file->ucontext);
  1814. if (!srq)
  1815. goto out;
  1816. resp.bad_wr = 0;
  1817. ret = srq->device->post_srq_recv(srq, wr, &bad_wr);
  1818. put_srq_read(srq);
  1819. if (ret)
  1820. for (next = wr; next; next = next->next) {
  1821. ++resp.bad_wr;
  1822. if (next == bad_wr)
  1823. break;
  1824. }
  1825. if (copy_to_user((void __user *) (unsigned long) cmd.response,
  1826. &resp, sizeof resp))
  1827. ret = -EFAULT;
  1828. out:
  1829. while (wr) {
  1830. next = wr->next;
  1831. kfree(wr);
  1832. wr = next;
  1833. }
  1834. return ret ? ret : in_len;
  1835. }
  1836. ssize_t ib_uverbs_create_ah(struct ib_uverbs_file *file,
  1837. const char __user *buf, int in_len,
  1838. int out_len)
  1839. {
  1840. struct ib_uverbs_create_ah cmd;
  1841. struct ib_uverbs_create_ah_resp resp;
  1842. struct ib_uobject *uobj;
  1843. struct ib_pd *pd;
  1844. struct ib_ah *ah;
  1845. struct ib_ah_attr attr;
  1846. int ret;
  1847. if (out_len < sizeof resp)
  1848. return -ENOSPC;
  1849. if (copy_from_user(&cmd, buf, sizeof cmd))
  1850. return -EFAULT;
  1851. uobj = kmalloc(sizeof *uobj, GFP_KERNEL);
  1852. if (!uobj)
  1853. return -ENOMEM;
  1854. init_uobj(uobj, cmd.user_handle, file->ucontext, &ah_lock_key);
  1855. down_write(&uobj->mutex);
  1856. pd = idr_read_pd(cmd.pd_handle, file->ucontext);
  1857. if (!pd) {
  1858. ret = -EINVAL;
  1859. goto err;
  1860. }
  1861. attr.dlid = cmd.attr.dlid;
  1862. attr.sl = cmd.attr.sl;
  1863. attr.src_path_bits = cmd.attr.src_path_bits;
  1864. attr.static_rate = cmd.attr.static_rate;
  1865. attr.ah_flags = cmd.attr.is_global ? IB_AH_GRH : 0;
  1866. attr.port_num = cmd.attr.port_num;
  1867. attr.grh.flow_label = cmd.attr.grh.flow_label;
  1868. attr.grh.sgid_index = cmd.attr.grh.sgid_index;
  1869. attr.grh.hop_limit = cmd.attr.grh.hop_limit;
  1870. attr.grh.traffic_class = cmd.attr.grh.traffic_class;
  1871. memcpy(attr.grh.dgid.raw, cmd.attr.grh.dgid, 16);
  1872. ah = ib_create_ah(pd, &attr);
  1873. if (IS_ERR(ah)) {
  1874. ret = PTR_ERR(ah);
  1875. goto err_put;
  1876. }
  1877. ah->uobject = uobj;
  1878. uobj->object = ah;
  1879. ret = idr_add_uobj(&ib_uverbs_ah_idr, uobj);
  1880. if (ret)
  1881. goto err_destroy;
  1882. resp.ah_handle = uobj->id;
  1883. if (copy_to_user((void __user *) (unsigned long) cmd.response,
  1884. &resp, sizeof resp)) {
  1885. ret = -EFAULT;
  1886. goto err_copy;
  1887. }
  1888. put_pd_read(pd);
  1889. mutex_lock(&file->mutex);
  1890. list_add_tail(&uobj->list, &file->ucontext->ah_list);
  1891. mutex_unlock(&file->mutex);
  1892. uobj->live = 1;
  1893. up_write(&uobj->mutex);
  1894. return in_len;
  1895. err_copy:
  1896. idr_remove_uobj(&ib_uverbs_ah_idr, uobj);
  1897. err_destroy:
  1898. ib_destroy_ah(ah);
  1899. err_put:
  1900. put_pd_read(pd);
  1901. err:
  1902. put_uobj_write(uobj);
  1903. return ret;
  1904. }
  1905. ssize_t ib_uverbs_destroy_ah(struct ib_uverbs_file *file,
  1906. const char __user *buf, int in_len, int out_len)
  1907. {
  1908. struct ib_uverbs_destroy_ah cmd;
  1909. struct ib_ah *ah;
  1910. struct ib_uobject *uobj;
  1911. int ret;
  1912. if (copy_from_user(&cmd, buf, sizeof cmd))
  1913. return -EFAULT;
  1914. uobj = idr_write_uobj(&ib_uverbs_ah_idr, cmd.ah_handle, file->ucontext);
  1915. if (!uobj)
  1916. return -EINVAL;
  1917. ah = uobj->object;
  1918. ret = ib_destroy_ah(ah);
  1919. if (!ret)
  1920. uobj->live = 0;
  1921. put_uobj_write(uobj);
  1922. if (ret)
  1923. return ret;
  1924. idr_remove_uobj(&ib_uverbs_ah_idr, uobj);
  1925. mutex_lock(&file->mutex);
  1926. list_del(&uobj->list);
  1927. mutex_unlock(&file->mutex);
  1928. put_uobj(uobj);
  1929. return in_len;
  1930. }
  1931. ssize_t ib_uverbs_attach_mcast(struct ib_uverbs_file *file,
  1932. const char __user *buf, int in_len,
  1933. int out_len)
  1934. {
  1935. struct ib_uverbs_attach_mcast cmd;
  1936. struct ib_qp *qp;
  1937. struct ib_uqp_object *obj;
  1938. struct ib_uverbs_mcast_entry *mcast;
  1939. int ret;
  1940. if (copy_from_user(&cmd, buf, sizeof cmd))
  1941. return -EFAULT;
  1942. qp = idr_write_qp(cmd.qp_handle, file->ucontext);
  1943. if (!qp)
  1944. return -EINVAL;
  1945. obj = container_of(qp->uobject, struct ib_uqp_object, uevent.uobject);
  1946. list_for_each_entry(mcast, &obj->mcast_list, list)
  1947. if (cmd.mlid == mcast->lid &&
  1948. !memcmp(cmd.gid, mcast->gid.raw, sizeof mcast->gid.raw)) {
  1949. ret = 0;
  1950. goto out_put;
  1951. }
  1952. mcast = kmalloc(sizeof *mcast, GFP_KERNEL);
  1953. if (!mcast) {
  1954. ret = -ENOMEM;
  1955. goto out_put;
  1956. }
  1957. mcast->lid = cmd.mlid;
  1958. memcpy(mcast->gid.raw, cmd.gid, sizeof mcast->gid.raw);
  1959. ret = ib_attach_mcast(qp, &mcast->gid, cmd.mlid);
  1960. if (!ret)
  1961. list_add_tail(&mcast->list, &obj->mcast_list);
  1962. else
  1963. kfree(mcast);
  1964. out_put:
  1965. put_qp_write(qp);
  1966. return ret ? ret : in_len;
  1967. }
  1968. ssize_t ib_uverbs_detach_mcast(struct ib_uverbs_file *file,
  1969. const char __user *buf, int in_len,
  1970. int out_len)
  1971. {
  1972. struct ib_uverbs_detach_mcast cmd;
  1973. struct ib_uqp_object *obj;
  1974. struct ib_qp *qp;
  1975. struct ib_uverbs_mcast_entry *mcast;
  1976. int ret = -EINVAL;
  1977. if (copy_from_user(&cmd, buf, sizeof cmd))
  1978. return -EFAULT;
  1979. qp = idr_write_qp(cmd.qp_handle, file->ucontext);
  1980. if (!qp)
  1981. return -EINVAL;
  1982. ret = ib_detach_mcast(qp, (union ib_gid *) cmd.gid, cmd.mlid);
  1983. if (ret)
  1984. goto out_put;
  1985. obj = container_of(qp->uobject, struct ib_uqp_object, uevent.uobject);
  1986. list_for_each_entry(mcast, &obj->mcast_list, list)
  1987. if (cmd.mlid == mcast->lid &&
  1988. !memcmp(cmd.gid, mcast->gid.raw, sizeof mcast->gid.raw)) {
  1989. list_del(&mcast->list);
  1990. kfree(mcast);
  1991. break;
  1992. }
  1993. out_put:
  1994. put_qp_write(qp);
  1995. return ret ? ret : in_len;
  1996. }
  1997. static int __uverbs_create_xsrq(struct ib_uverbs_file *file,
  1998. struct ib_uverbs_create_xsrq *cmd,
  1999. struct ib_udata *udata)
  2000. {
  2001. struct ib_uverbs_create_srq_resp resp;
  2002. struct ib_usrq_object *obj;
  2003. struct ib_pd *pd;
  2004. struct ib_srq *srq;
  2005. struct ib_uobject *uninitialized_var(xrcd_uobj);
  2006. struct ib_srq_init_attr attr;
  2007. int ret;
  2008. obj = kmalloc(sizeof *obj, GFP_KERNEL);
  2009. if (!obj)
  2010. return -ENOMEM;
  2011. init_uobj(&obj->uevent.uobject, cmd->user_handle, file->ucontext, &srq_lock_key);
  2012. down_write(&obj->uevent.uobject.mutex);
  2013. pd = idr_read_pd(cmd->pd_handle, file->ucontext);
  2014. if (!pd) {
  2015. ret = -EINVAL;
  2016. goto err;
  2017. }
  2018. if (cmd->srq_type == IB_SRQT_XRC) {
  2019. attr.ext.xrc.cq = idr_read_cq(cmd->cq_handle, file->ucontext, 0);
  2020. if (!attr.ext.xrc.cq) {
  2021. ret = -EINVAL;
  2022. goto err_put_pd;
  2023. }
  2024. attr.ext.xrc.xrcd = idr_read_xrcd(cmd->xrcd_handle, file->ucontext, &xrcd_uobj);
  2025. if (!attr.ext.xrc.xrcd) {
  2026. ret = -EINVAL;
  2027. goto err_put_cq;
  2028. }
  2029. obj->uxrcd = container_of(xrcd_uobj, struct ib_uxrcd_object, uobject);
  2030. atomic_inc(&obj->uxrcd->refcnt);
  2031. }
  2032. attr.event_handler = ib_uverbs_srq_event_handler;
  2033. attr.srq_context = file;
  2034. attr.srq_type = cmd->srq_type;
  2035. attr.attr.max_wr = cmd->max_wr;
  2036. attr.attr.max_sge = cmd->max_sge;
  2037. attr.attr.srq_limit = cmd->srq_limit;
  2038. obj->uevent.events_reported = 0;
  2039. INIT_LIST_HEAD(&obj->uevent.event_list);
  2040. srq = pd->device->create_srq(pd, &attr, udata);
  2041. if (IS_ERR(srq)) {
  2042. ret = PTR_ERR(srq);
  2043. goto err_put;
  2044. }
  2045. srq->device = pd->device;
  2046. srq->pd = pd;
  2047. srq->srq_type = cmd->srq_type;
  2048. srq->uobject = &obj->uevent.uobject;
  2049. srq->event_handler = attr.event_handler;
  2050. srq->srq_context = attr.srq_context;
  2051. if (cmd->srq_type == IB_SRQT_XRC) {
  2052. srq->ext.xrc.cq = attr.ext.xrc.cq;
  2053. srq->ext.xrc.xrcd = attr.ext.xrc.xrcd;
  2054. atomic_inc(&attr.ext.xrc.cq->usecnt);
  2055. atomic_inc(&attr.ext.xrc.xrcd->usecnt);
  2056. }
  2057. atomic_inc(&pd->usecnt);
  2058. atomic_set(&srq->usecnt, 0);
  2059. obj->uevent.uobject.object = srq;
  2060. ret = idr_add_uobj(&ib_uverbs_srq_idr, &obj->uevent.uobject);
  2061. if (ret)
  2062. goto err_destroy;
  2063. memset(&resp, 0, sizeof resp);
  2064. resp.srq_handle = obj->uevent.uobject.id;
  2065. resp.max_wr = attr.attr.max_wr;
  2066. resp.max_sge = attr.attr.max_sge;
  2067. if (cmd->srq_type == IB_SRQT_XRC)
  2068. resp.srqn = srq->ext.xrc.srq_num;
  2069. if (copy_to_user((void __user *) (unsigned long) cmd->response,
  2070. &resp, sizeof resp)) {
  2071. ret = -EFAULT;
  2072. goto err_copy;
  2073. }
  2074. if (cmd->srq_type == IB_SRQT_XRC) {
  2075. put_uobj_read(xrcd_uobj);
  2076. put_cq_read(attr.ext.xrc.cq);
  2077. }
  2078. put_pd_read(pd);
  2079. mutex_lock(&file->mutex);
  2080. list_add_tail(&obj->uevent.uobject.list, &file->ucontext->srq_list);
  2081. mutex_unlock(&file->mutex);
  2082. obj->uevent.uobject.live = 1;
  2083. up_write(&obj->uevent.uobject.mutex);
  2084. return 0;
  2085. err_copy:
  2086. idr_remove_uobj(&ib_uverbs_srq_idr, &obj->uevent.uobject);
  2087. err_destroy:
  2088. ib_destroy_srq(srq);
  2089. err_put:
  2090. if (cmd->srq_type == IB_SRQT_XRC) {
  2091. atomic_dec(&obj->uxrcd->refcnt);
  2092. put_uobj_read(xrcd_uobj);
  2093. }
  2094. err_put_cq:
  2095. if (cmd->srq_type == IB_SRQT_XRC)
  2096. put_cq_read(attr.ext.xrc.cq);
  2097. err_put_pd:
  2098. put_pd_read(pd);
  2099. err:
  2100. put_uobj_write(&obj->uevent.uobject);
  2101. return ret;
  2102. }
  2103. ssize_t ib_uverbs_create_srq(struct ib_uverbs_file *file,
  2104. const char __user *buf, int in_len,
  2105. int out_len)
  2106. {
  2107. struct ib_uverbs_create_srq cmd;
  2108. struct ib_uverbs_create_xsrq xcmd;
  2109. struct ib_uverbs_create_srq_resp resp;
  2110. struct ib_udata udata;
  2111. int ret;
  2112. if (out_len < sizeof resp)
  2113. return -ENOSPC;
  2114. if (copy_from_user(&cmd, buf, sizeof cmd))
  2115. return -EFAULT;
  2116. xcmd.response = cmd.response;
  2117. xcmd.user_handle = cmd.user_handle;
  2118. xcmd.srq_type = IB_SRQT_BASIC;
  2119. xcmd.pd_handle = cmd.pd_handle;
  2120. xcmd.max_wr = cmd.max_wr;
  2121. xcmd.max_sge = cmd.max_sge;
  2122. xcmd.srq_limit = cmd.srq_limit;
  2123. INIT_UDATA(&udata, buf + sizeof cmd,
  2124. (unsigned long) cmd.response + sizeof resp,
  2125. in_len - sizeof cmd, out_len - sizeof resp);
  2126. ret = __uverbs_create_xsrq(file, &xcmd, &udata);
  2127. if (ret)
  2128. return ret;
  2129. return in_len;
  2130. }
  2131. ssize_t ib_uverbs_create_xsrq(struct ib_uverbs_file *file,
  2132. const char __user *buf, int in_len, int out_len)
  2133. {
  2134. struct ib_uverbs_create_xsrq cmd;
  2135. struct ib_uverbs_create_srq_resp resp;
  2136. struct ib_udata udata;
  2137. int ret;
  2138. if (out_len < sizeof resp)
  2139. return -ENOSPC;
  2140. if (copy_from_user(&cmd, buf, sizeof cmd))
  2141. return -EFAULT;
  2142. INIT_UDATA(&udata, buf + sizeof cmd,
  2143. (unsigned long) cmd.response + sizeof resp,
  2144. in_len - sizeof cmd, out_len - sizeof resp);
  2145. ret = __uverbs_create_xsrq(file, &cmd, &udata);
  2146. if (ret)
  2147. return ret;
  2148. return in_len;
  2149. }
  2150. ssize_t ib_uverbs_modify_srq(struct ib_uverbs_file *file,
  2151. const char __user *buf, int in_len,
  2152. int out_len)
  2153. {
  2154. struct ib_uverbs_modify_srq cmd;
  2155. struct ib_udata udata;
  2156. struct ib_srq *srq;
  2157. struct ib_srq_attr attr;
  2158. int ret;
  2159. if (copy_from_user(&cmd, buf, sizeof cmd))
  2160. return -EFAULT;
  2161. INIT_UDATA(&udata, buf + sizeof cmd, NULL, in_len - sizeof cmd,
  2162. out_len);
  2163. srq = idr_read_srq(cmd.srq_handle, file->ucontext);
  2164. if (!srq)
  2165. return -EINVAL;
  2166. attr.max_wr = cmd.max_wr;
  2167. attr.srq_limit = cmd.srq_limit;
  2168. ret = srq->device->modify_srq(srq, &attr, cmd.attr_mask, &udata);
  2169. put_srq_read(srq);
  2170. return ret ? ret : in_len;
  2171. }
  2172. ssize_t ib_uverbs_query_srq(struct ib_uverbs_file *file,
  2173. const char __user *buf,
  2174. int in_len, int out_len)
  2175. {
  2176. struct ib_uverbs_query_srq cmd;
  2177. struct ib_uverbs_query_srq_resp resp;
  2178. struct ib_srq_attr attr;
  2179. struct ib_srq *srq;
  2180. int ret;
  2181. if (out_len < sizeof resp)
  2182. return -ENOSPC;
  2183. if (copy_from_user(&cmd, buf, sizeof cmd))
  2184. return -EFAULT;
  2185. srq = idr_read_srq(cmd.srq_handle, file->ucontext);
  2186. if (!srq)
  2187. return -EINVAL;
  2188. ret = ib_query_srq(srq, &attr);
  2189. put_srq_read(srq);
  2190. if (ret)
  2191. return ret;
  2192. memset(&resp, 0, sizeof resp);
  2193. resp.max_wr = attr.max_wr;
  2194. resp.max_sge = attr.max_sge;
  2195. resp.srq_limit = attr.srq_limit;
  2196. if (copy_to_user((void __user *) (unsigned long) cmd.response,
  2197. &resp, sizeof resp))
  2198. return -EFAULT;
  2199. return in_len;
  2200. }
  2201. ssize_t ib_uverbs_destroy_srq(struct ib_uverbs_file *file,
  2202. const char __user *buf, int in_len,
  2203. int out_len)
  2204. {
  2205. struct ib_uverbs_destroy_srq cmd;
  2206. struct ib_uverbs_destroy_srq_resp resp;
  2207. struct ib_uobject *uobj;
  2208. struct ib_srq *srq;
  2209. struct ib_uevent_object *obj;
  2210. int ret = -EINVAL;
  2211. if (copy_from_user(&cmd, buf, sizeof cmd))
  2212. return -EFAULT;
  2213. uobj = idr_write_uobj(&ib_uverbs_srq_idr, cmd.srq_handle, file->ucontext);
  2214. if (!uobj)
  2215. return -EINVAL;
  2216. srq = uobj->object;
  2217. obj = container_of(uobj, struct ib_uevent_object, uobject);
  2218. ret = ib_destroy_srq(srq);
  2219. if (!ret)
  2220. uobj->live = 0;
  2221. put_uobj_write(uobj);
  2222. if (ret)
  2223. return ret;
  2224. idr_remove_uobj(&ib_uverbs_srq_idr, uobj);
  2225. mutex_lock(&file->mutex);
  2226. list_del(&uobj->list);
  2227. mutex_unlock(&file->mutex);
  2228. ib_uverbs_release_uevent(file, obj);
  2229. memset(&resp, 0, sizeof resp);
  2230. resp.events_reported = obj->events_reported;
  2231. put_uobj(uobj);
  2232. if (copy_to_user((void __user *) (unsigned long) cmd.response,
  2233. &resp, sizeof resp))
  2234. ret = -EFAULT;
  2235. return ret ? ret : in_len;
  2236. }