uverbs_cmd.c 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197
  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. #define INIT_UDATA(udata, ibuf, obuf, ilen, olen) \
  47. do { \
  48. (udata)->inbuf = (void __user *) (ibuf); \
  49. (udata)->outbuf = (void __user *) (obuf); \
  50. (udata)->inlen = (ilen); \
  51. (udata)->outlen = (olen); \
  52. } while (0)
  53. /*
  54. * The ib_uobject locking scheme is as follows:
  55. *
  56. * - ib_uverbs_idr_lock protects the uverbs idrs themselves, so it
  57. * needs to be held during all idr operations. When an object is
  58. * looked up, a reference must be taken on the object's kref before
  59. * dropping this lock.
  60. *
  61. * - Each object also has an rwsem. This rwsem must be held for
  62. * reading while an operation that uses the object is performed.
  63. * For example, while registering an MR, the associated PD's
  64. * uobject.mutex must be held for reading. The rwsem must be held
  65. * for writing while initializing or destroying an object.
  66. *
  67. * - In addition, each object has a "live" flag. If this flag is not
  68. * set, then lookups of the object will fail even if it is found in
  69. * the idr. This handles a reader that blocks and does not acquire
  70. * the rwsem until after the object is destroyed. The destroy
  71. * operation will set the live flag to 0 and then drop the rwsem;
  72. * this will allow the reader to acquire the rwsem, see that the
  73. * live flag is 0, and then drop the rwsem and its reference to
  74. * object. The underlying storage will not be freed until the last
  75. * reference to the object is dropped.
  76. */
  77. static void init_uobj(struct ib_uobject *uobj, u64 user_handle,
  78. struct ib_ucontext *context, struct lock_class_key *key)
  79. {
  80. uobj->user_handle = user_handle;
  81. uobj->context = context;
  82. kref_init(&uobj->ref);
  83. init_rwsem(&uobj->mutex);
  84. lockdep_set_class(&uobj->mutex, key);
  85. uobj->live = 0;
  86. }
  87. static void release_uobj(struct kref *kref)
  88. {
  89. kfree(container_of(kref, struct ib_uobject, ref));
  90. }
  91. static void put_uobj(struct ib_uobject *uobj)
  92. {
  93. kref_put(&uobj->ref, release_uobj);
  94. }
  95. static void put_uobj_read(struct ib_uobject *uobj)
  96. {
  97. up_read(&uobj->mutex);
  98. put_uobj(uobj);
  99. }
  100. static void put_uobj_write(struct ib_uobject *uobj)
  101. {
  102. up_write(&uobj->mutex);
  103. put_uobj(uobj);
  104. }
  105. static int idr_add_uobj(struct idr *idr, struct ib_uobject *uobj)
  106. {
  107. int ret;
  108. retry:
  109. if (!idr_pre_get(idr, GFP_KERNEL))
  110. return -ENOMEM;
  111. spin_lock(&ib_uverbs_idr_lock);
  112. ret = idr_get_new(idr, uobj, &uobj->id);
  113. spin_unlock(&ib_uverbs_idr_lock);
  114. if (ret == -EAGAIN)
  115. goto retry;
  116. return ret;
  117. }
  118. void idr_remove_uobj(struct idr *idr, struct ib_uobject *uobj)
  119. {
  120. spin_lock(&ib_uverbs_idr_lock);
  121. idr_remove(idr, uobj->id);
  122. spin_unlock(&ib_uverbs_idr_lock);
  123. }
  124. static struct ib_uobject *__idr_get_uobj(struct idr *idr, int id,
  125. struct ib_ucontext *context)
  126. {
  127. struct ib_uobject *uobj;
  128. spin_lock(&ib_uverbs_idr_lock);
  129. uobj = idr_find(idr, id);
  130. if (uobj) {
  131. if (uobj->context == context)
  132. kref_get(&uobj->ref);
  133. else
  134. uobj = NULL;
  135. }
  136. spin_unlock(&ib_uverbs_idr_lock);
  137. return uobj;
  138. }
  139. static struct ib_uobject *idr_read_uobj(struct idr *idr, int id,
  140. struct ib_ucontext *context, int nested)
  141. {
  142. struct ib_uobject *uobj;
  143. uobj = __idr_get_uobj(idr, id, context);
  144. if (!uobj)
  145. return NULL;
  146. if (nested)
  147. down_read_nested(&uobj->mutex, SINGLE_DEPTH_NESTING);
  148. else
  149. down_read(&uobj->mutex);
  150. if (!uobj->live) {
  151. put_uobj_read(uobj);
  152. return NULL;
  153. }
  154. return uobj;
  155. }
  156. static struct ib_uobject *idr_write_uobj(struct idr *idr, int id,
  157. struct ib_ucontext *context)
  158. {
  159. struct ib_uobject *uobj;
  160. uobj = __idr_get_uobj(idr, id, context);
  161. if (!uobj)
  162. return NULL;
  163. down_write(&uobj->mutex);
  164. if (!uobj->live) {
  165. put_uobj_write(uobj);
  166. return NULL;
  167. }
  168. return uobj;
  169. }
  170. static void *idr_read_obj(struct idr *idr, int id, struct ib_ucontext *context,
  171. int nested)
  172. {
  173. struct ib_uobject *uobj;
  174. uobj = idr_read_uobj(idr, id, context, nested);
  175. return uobj ? uobj->object : NULL;
  176. }
  177. static struct ib_pd *idr_read_pd(int pd_handle, struct ib_ucontext *context)
  178. {
  179. return idr_read_obj(&ib_uverbs_pd_idr, pd_handle, context, 0);
  180. }
  181. static void put_pd_read(struct ib_pd *pd)
  182. {
  183. put_uobj_read(pd->uobject);
  184. }
  185. static struct ib_cq *idr_read_cq(int cq_handle, struct ib_ucontext *context, int nested)
  186. {
  187. return idr_read_obj(&ib_uverbs_cq_idr, cq_handle, context, nested);
  188. }
  189. static void put_cq_read(struct ib_cq *cq)
  190. {
  191. put_uobj_read(cq->uobject);
  192. }
  193. static struct ib_ah *idr_read_ah(int ah_handle, struct ib_ucontext *context)
  194. {
  195. return idr_read_obj(&ib_uverbs_ah_idr, ah_handle, context, 0);
  196. }
  197. static void put_ah_read(struct ib_ah *ah)
  198. {
  199. put_uobj_read(ah->uobject);
  200. }
  201. static struct ib_qp *idr_read_qp(int qp_handle, struct ib_ucontext *context)
  202. {
  203. return idr_read_obj(&ib_uverbs_qp_idr, qp_handle, context, 0);
  204. }
  205. static void put_qp_read(struct ib_qp *qp)
  206. {
  207. put_uobj_read(qp->uobject);
  208. }
  209. static struct ib_srq *idr_read_srq(int srq_handle, struct ib_ucontext *context)
  210. {
  211. return idr_read_obj(&ib_uverbs_srq_idr, srq_handle, context, 0);
  212. }
  213. static void put_srq_read(struct ib_srq *srq)
  214. {
  215. put_uobj_read(srq->uobject);
  216. }
  217. ssize_t ib_uverbs_get_context(struct ib_uverbs_file *file,
  218. const char __user *buf,
  219. int in_len, int out_len)
  220. {
  221. struct ib_uverbs_get_context cmd;
  222. struct ib_uverbs_get_context_resp resp;
  223. struct ib_udata udata;
  224. struct ib_device *ibdev = file->device->ib_dev;
  225. struct ib_ucontext *ucontext;
  226. struct file *filp;
  227. int ret;
  228. if (out_len < sizeof resp)
  229. return -ENOSPC;
  230. if (copy_from_user(&cmd, buf, sizeof cmd))
  231. return -EFAULT;
  232. mutex_lock(&file->mutex);
  233. if (file->ucontext) {
  234. ret = -EINVAL;
  235. goto err;
  236. }
  237. INIT_UDATA(&udata, buf + sizeof cmd,
  238. (unsigned long) cmd.response + sizeof resp,
  239. in_len - sizeof cmd, out_len - sizeof resp);
  240. ucontext = ibdev->alloc_ucontext(ibdev, &udata);
  241. if (IS_ERR(ucontext)) {
  242. ret = PTR_ERR(ucontext);
  243. goto err;
  244. }
  245. ucontext->device = ibdev;
  246. INIT_LIST_HEAD(&ucontext->pd_list);
  247. INIT_LIST_HEAD(&ucontext->mr_list);
  248. INIT_LIST_HEAD(&ucontext->mw_list);
  249. INIT_LIST_HEAD(&ucontext->cq_list);
  250. INIT_LIST_HEAD(&ucontext->qp_list);
  251. INIT_LIST_HEAD(&ucontext->srq_list);
  252. INIT_LIST_HEAD(&ucontext->ah_list);
  253. ucontext->closing = 0;
  254. resp.num_comp_vectors = file->device->num_comp_vectors;
  255. ret = get_unused_fd();
  256. if (ret < 0)
  257. goto err_free;
  258. resp.async_fd = ret;
  259. filp = ib_uverbs_alloc_event_file(file, 1);
  260. if (IS_ERR(filp)) {
  261. ret = PTR_ERR(filp);
  262. goto err_fd;
  263. }
  264. if (copy_to_user((void __user *) (unsigned long) cmd.response,
  265. &resp, sizeof resp)) {
  266. ret = -EFAULT;
  267. goto err_file;
  268. }
  269. file->async_file = filp->private_data;
  270. INIT_IB_EVENT_HANDLER(&file->event_handler, file->device->ib_dev,
  271. ib_uverbs_event_handler);
  272. ret = ib_register_event_handler(&file->event_handler);
  273. if (ret)
  274. goto err_file;
  275. kref_get(&file->async_file->ref);
  276. kref_get(&file->ref);
  277. file->ucontext = ucontext;
  278. fd_install(resp.async_fd, filp);
  279. mutex_unlock(&file->mutex);
  280. return in_len;
  281. err_file:
  282. fput(filp);
  283. err_fd:
  284. put_unused_fd(resp.async_fd);
  285. err_free:
  286. ibdev->dealloc_ucontext(ucontext);
  287. err:
  288. mutex_unlock(&file->mutex);
  289. return ret;
  290. }
  291. ssize_t ib_uverbs_query_device(struct ib_uverbs_file *file,
  292. const char __user *buf,
  293. int in_len, int out_len)
  294. {
  295. struct ib_uverbs_query_device cmd;
  296. struct ib_uverbs_query_device_resp resp;
  297. struct ib_device_attr attr;
  298. int ret;
  299. if (out_len < sizeof resp)
  300. return -ENOSPC;
  301. if (copy_from_user(&cmd, buf, sizeof cmd))
  302. return -EFAULT;
  303. ret = ib_query_device(file->device->ib_dev, &attr);
  304. if (ret)
  305. return ret;
  306. memset(&resp, 0, sizeof resp);
  307. resp.fw_ver = attr.fw_ver;
  308. resp.node_guid = file->device->ib_dev->node_guid;
  309. resp.sys_image_guid = attr.sys_image_guid;
  310. resp.max_mr_size = attr.max_mr_size;
  311. resp.page_size_cap = attr.page_size_cap;
  312. resp.vendor_id = attr.vendor_id;
  313. resp.vendor_part_id = attr.vendor_part_id;
  314. resp.hw_ver = attr.hw_ver;
  315. resp.max_qp = attr.max_qp;
  316. resp.max_qp_wr = attr.max_qp_wr;
  317. resp.device_cap_flags = attr.device_cap_flags;
  318. resp.max_sge = attr.max_sge;
  319. resp.max_sge_rd = attr.max_sge_rd;
  320. resp.max_cq = attr.max_cq;
  321. resp.max_cqe = attr.max_cqe;
  322. resp.max_mr = attr.max_mr;
  323. resp.max_pd = attr.max_pd;
  324. resp.max_qp_rd_atom = attr.max_qp_rd_atom;
  325. resp.max_ee_rd_atom = attr.max_ee_rd_atom;
  326. resp.max_res_rd_atom = attr.max_res_rd_atom;
  327. resp.max_qp_init_rd_atom = attr.max_qp_init_rd_atom;
  328. resp.max_ee_init_rd_atom = attr.max_ee_init_rd_atom;
  329. resp.atomic_cap = attr.atomic_cap;
  330. resp.max_ee = attr.max_ee;
  331. resp.max_rdd = attr.max_rdd;
  332. resp.max_mw = attr.max_mw;
  333. resp.max_raw_ipv6_qp = attr.max_raw_ipv6_qp;
  334. resp.max_raw_ethy_qp = attr.max_raw_ethy_qp;
  335. resp.max_mcast_grp = attr.max_mcast_grp;
  336. resp.max_mcast_qp_attach = attr.max_mcast_qp_attach;
  337. resp.max_total_mcast_qp_attach = attr.max_total_mcast_qp_attach;
  338. resp.max_ah = attr.max_ah;
  339. resp.max_fmr = attr.max_fmr;
  340. resp.max_map_per_fmr = attr.max_map_per_fmr;
  341. resp.max_srq = attr.max_srq;
  342. resp.max_srq_wr = attr.max_srq_wr;
  343. resp.max_srq_sge = attr.max_srq_sge;
  344. resp.max_pkeys = attr.max_pkeys;
  345. resp.local_ca_ack_delay = attr.local_ca_ack_delay;
  346. resp.phys_port_cnt = file->device->ib_dev->phys_port_cnt;
  347. if (copy_to_user((void __user *) (unsigned long) cmd.response,
  348. &resp, sizeof resp))
  349. return -EFAULT;
  350. return in_len;
  351. }
  352. ssize_t ib_uverbs_query_port(struct ib_uverbs_file *file,
  353. const char __user *buf,
  354. int in_len, int out_len)
  355. {
  356. struct ib_uverbs_query_port cmd;
  357. struct ib_uverbs_query_port_resp resp;
  358. struct ib_port_attr attr;
  359. int ret;
  360. if (out_len < sizeof resp)
  361. return -ENOSPC;
  362. if (copy_from_user(&cmd, buf, sizeof cmd))
  363. return -EFAULT;
  364. ret = ib_query_port(file->device->ib_dev, cmd.port_num, &attr);
  365. if (ret)
  366. return ret;
  367. memset(&resp, 0, sizeof resp);
  368. resp.state = attr.state;
  369. resp.max_mtu = attr.max_mtu;
  370. resp.active_mtu = attr.active_mtu;
  371. resp.gid_tbl_len = attr.gid_tbl_len;
  372. resp.port_cap_flags = attr.port_cap_flags;
  373. resp.max_msg_sz = attr.max_msg_sz;
  374. resp.bad_pkey_cntr = attr.bad_pkey_cntr;
  375. resp.qkey_viol_cntr = attr.qkey_viol_cntr;
  376. resp.pkey_tbl_len = attr.pkey_tbl_len;
  377. resp.lid = attr.lid;
  378. resp.sm_lid = attr.sm_lid;
  379. resp.lmc = attr.lmc;
  380. resp.max_vl_num = attr.max_vl_num;
  381. resp.sm_sl = attr.sm_sl;
  382. resp.subnet_timeout = attr.subnet_timeout;
  383. resp.init_type_reply = attr.init_type_reply;
  384. resp.active_width = attr.active_width;
  385. resp.active_speed = attr.active_speed;
  386. resp.phys_state = attr.phys_state;
  387. resp.link_layer = rdma_port_get_link_layer(file->device->ib_dev,
  388. cmd.port_num);
  389. if (copy_to_user((void __user *) (unsigned long) cmd.response,
  390. &resp, sizeof resp))
  391. return -EFAULT;
  392. return in_len;
  393. }
  394. ssize_t ib_uverbs_alloc_pd(struct ib_uverbs_file *file,
  395. const char __user *buf,
  396. int in_len, int out_len)
  397. {
  398. struct ib_uverbs_alloc_pd cmd;
  399. struct ib_uverbs_alloc_pd_resp resp;
  400. struct ib_udata udata;
  401. struct ib_uobject *uobj;
  402. struct ib_pd *pd;
  403. int ret;
  404. if (out_len < sizeof resp)
  405. return -ENOSPC;
  406. if (copy_from_user(&cmd, buf, sizeof cmd))
  407. return -EFAULT;
  408. INIT_UDATA(&udata, buf + sizeof cmd,
  409. (unsigned long) cmd.response + sizeof resp,
  410. in_len - sizeof cmd, out_len - sizeof resp);
  411. uobj = kmalloc(sizeof *uobj, GFP_KERNEL);
  412. if (!uobj)
  413. return -ENOMEM;
  414. init_uobj(uobj, 0, file->ucontext, &pd_lock_key);
  415. down_write(&uobj->mutex);
  416. pd = file->device->ib_dev->alloc_pd(file->device->ib_dev,
  417. file->ucontext, &udata);
  418. if (IS_ERR(pd)) {
  419. ret = PTR_ERR(pd);
  420. goto err;
  421. }
  422. pd->device = file->device->ib_dev;
  423. pd->uobject = uobj;
  424. atomic_set(&pd->usecnt, 0);
  425. uobj->object = pd;
  426. ret = idr_add_uobj(&ib_uverbs_pd_idr, uobj);
  427. if (ret)
  428. goto err_idr;
  429. memset(&resp, 0, sizeof resp);
  430. resp.pd_handle = uobj->id;
  431. if (copy_to_user((void __user *) (unsigned long) cmd.response,
  432. &resp, sizeof resp)) {
  433. ret = -EFAULT;
  434. goto err_copy;
  435. }
  436. mutex_lock(&file->mutex);
  437. list_add_tail(&uobj->list, &file->ucontext->pd_list);
  438. mutex_unlock(&file->mutex);
  439. uobj->live = 1;
  440. up_write(&uobj->mutex);
  441. return in_len;
  442. err_copy:
  443. idr_remove_uobj(&ib_uverbs_pd_idr, uobj);
  444. err_idr:
  445. ib_dealloc_pd(pd);
  446. err:
  447. put_uobj_write(uobj);
  448. return ret;
  449. }
  450. ssize_t ib_uverbs_dealloc_pd(struct ib_uverbs_file *file,
  451. const char __user *buf,
  452. int in_len, int out_len)
  453. {
  454. struct ib_uverbs_dealloc_pd cmd;
  455. struct ib_uobject *uobj;
  456. int ret;
  457. if (copy_from_user(&cmd, buf, sizeof cmd))
  458. return -EFAULT;
  459. uobj = idr_write_uobj(&ib_uverbs_pd_idr, cmd.pd_handle, file->ucontext);
  460. if (!uobj)
  461. return -EINVAL;
  462. ret = ib_dealloc_pd(uobj->object);
  463. if (!ret)
  464. uobj->live = 0;
  465. put_uobj_write(uobj);
  466. if (ret)
  467. return ret;
  468. idr_remove_uobj(&ib_uverbs_pd_idr, uobj);
  469. mutex_lock(&file->mutex);
  470. list_del(&uobj->list);
  471. mutex_unlock(&file->mutex);
  472. put_uobj(uobj);
  473. return in_len;
  474. }
  475. ssize_t ib_uverbs_reg_mr(struct ib_uverbs_file *file,
  476. const char __user *buf, int in_len,
  477. int out_len)
  478. {
  479. struct ib_uverbs_reg_mr cmd;
  480. struct ib_uverbs_reg_mr_resp resp;
  481. struct ib_udata udata;
  482. struct ib_uobject *uobj;
  483. struct ib_pd *pd;
  484. struct ib_mr *mr;
  485. int ret;
  486. if (out_len < sizeof resp)
  487. return -ENOSPC;
  488. if (copy_from_user(&cmd, buf, sizeof cmd))
  489. return -EFAULT;
  490. INIT_UDATA(&udata, buf + sizeof cmd,
  491. (unsigned long) cmd.response + sizeof resp,
  492. in_len - sizeof cmd, out_len - sizeof resp);
  493. if ((cmd.start & ~PAGE_MASK) != (cmd.hca_va & ~PAGE_MASK))
  494. return -EINVAL;
  495. /*
  496. * Local write permission is required if remote write or
  497. * remote atomic permission is also requested.
  498. */
  499. if (cmd.access_flags & (IB_ACCESS_REMOTE_ATOMIC | IB_ACCESS_REMOTE_WRITE) &&
  500. !(cmd.access_flags & IB_ACCESS_LOCAL_WRITE))
  501. return -EINVAL;
  502. uobj = kmalloc(sizeof *uobj, GFP_KERNEL);
  503. if (!uobj)
  504. return -ENOMEM;
  505. init_uobj(uobj, 0, file->ucontext, &mr_lock_key);
  506. down_write(&uobj->mutex);
  507. pd = idr_read_pd(cmd.pd_handle, file->ucontext);
  508. if (!pd) {
  509. ret = -EINVAL;
  510. goto err_free;
  511. }
  512. mr = pd->device->reg_user_mr(pd, cmd.start, cmd.length, cmd.hca_va,
  513. cmd.access_flags, &udata);
  514. if (IS_ERR(mr)) {
  515. ret = PTR_ERR(mr);
  516. goto err_put;
  517. }
  518. mr->device = pd->device;
  519. mr->pd = pd;
  520. mr->uobject = uobj;
  521. atomic_inc(&pd->usecnt);
  522. atomic_set(&mr->usecnt, 0);
  523. uobj->object = mr;
  524. ret = idr_add_uobj(&ib_uverbs_mr_idr, uobj);
  525. if (ret)
  526. goto err_unreg;
  527. memset(&resp, 0, sizeof resp);
  528. resp.lkey = mr->lkey;
  529. resp.rkey = mr->rkey;
  530. resp.mr_handle = uobj->id;
  531. if (copy_to_user((void __user *) (unsigned long) cmd.response,
  532. &resp, sizeof resp)) {
  533. ret = -EFAULT;
  534. goto err_copy;
  535. }
  536. put_pd_read(pd);
  537. mutex_lock(&file->mutex);
  538. list_add_tail(&uobj->list, &file->ucontext->mr_list);
  539. mutex_unlock(&file->mutex);
  540. uobj->live = 1;
  541. up_write(&uobj->mutex);
  542. return in_len;
  543. err_copy:
  544. idr_remove_uobj(&ib_uverbs_mr_idr, uobj);
  545. err_unreg:
  546. ib_dereg_mr(mr);
  547. err_put:
  548. put_pd_read(pd);
  549. err_free:
  550. put_uobj_write(uobj);
  551. return ret;
  552. }
  553. ssize_t ib_uverbs_dereg_mr(struct ib_uverbs_file *file,
  554. const char __user *buf, int in_len,
  555. int out_len)
  556. {
  557. struct ib_uverbs_dereg_mr cmd;
  558. struct ib_mr *mr;
  559. struct ib_uobject *uobj;
  560. int ret = -EINVAL;
  561. if (copy_from_user(&cmd, buf, sizeof cmd))
  562. return -EFAULT;
  563. uobj = idr_write_uobj(&ib_uverbs_mr_idr, cmd.mr_handle, file->ucontext);
  564. if (!uobj)
  565. return -EINVAL;
  566. mr = uobj->object;
  567. ret = ib_dereg_mr(mr);
  568. if (!ret)
  569. uobj->live = 0;
  570. put_uobj_write(uobj);
  571. if (ret)
  572. return ret;
  573. idr_remove_uobj(&ib_uverbs_mr_idr, uobj);
  574. mutex_lock(&file->mutex);
  575. list_del(&uobj->list);
  576. mutex_unlock(&file->mutex);
  577. put_uobj(uobj);
  578. return in_len;
  579. }
  580. ssize_t ib_uverbs_create_comp_channel(struct ib_uverbs_file *file,
  581. const char __user *buf, int in_len,
  582. int out_len)
  583. {
  584. struct ib_uverbs_create_comp_channel cmd;
  585. struct ib_uverbs_create_comp_channel_resp resp;
  586. struct file *filp;
  587. int ret;
  588. if (out_len < sizeof resp)
  589. return -ENOSPC;
  590. if (copy_from_user(&cmd, buf, sizeof cmd))
  591. return -EFAULT;
  592. ret = get_unused_fd();
  593. if (ret < 0)
  594. return ret;
  595. resp.fd = ret;
  596. filp = ib_uverbs_alloc_event_file(file, 0);
  597. if (IS_ERR(filp)) {
  598. put_unused_fd(resp.fd);
  599. return PTR_ERR(filp);
  600. }
  601. if (copy_to_user((void __user *) (unsigned long) cmd.response,
  602. &resp, sizeof resp)) {
  603. put_unused_fd(resp.fd);
  604. fput(filp);
  605. return -EFAULT;
  606. }
  607. fd_install(resp.fd, filp);
  608. return in_len;
  609. }
  610. ssize_t ib_uverbs_create_cq(struct ib_uverbs_file *file,
  611. const char __user *buf, int in_len,
  612. int out_len)
  613. {
  614. struct ib_uverbs_create_cq cmd;
  615. struct ib_uverbs_create_cq_resp resp;
  616. struct ib_udata udata;
  617. struct ib_ucq_object *obj;
  618. struct ib_uverbs_event_file *ev_file = NULL;
  619. struct ib_cq *cq;
  620. int ret;
  621. if (out_len < sizeof resp)
  622. return -ENOSPC;
  623. if (copy_from_user(&cmd, buf, sizeof cmd))
  624. return -EFAULT;
  625. INIT_UDATA(&udata, buf + sizeof cmd,
  626. (unsigned long) cmd.response + sizeof resp,
  627. in_len - sizeof cmd, out_len - sizeof resp);
  628. if (cmd.comp_vector >= file->device->num_comp_vectors)
  629. return -EINVAL;
  630. obj = kmalloc(sizeof *obj, GFP_KERNEL);
  631. if (!obj)
  632. return -ENOMEM;
  633. init_uobj(&obj->uobject, cmd.user_handle, file->ucontext, &cq_lock_key);
  634. down_write(&obj->uobject.mutex);
  635. if (cmd.comp_channel >= 0) {
  636. ev_file = ib_uverbs_lookup_comp_file(cmd.comp_channel);
  637. if (!ev_file) {
  638. ret = -EINVAL;
  639. goto err;
  640. }
  641. }
  642. obj->uverbs_file = file;
  643. obj->comp_events_reported = 0;
  644. obj->async_events_reported = 0;
  645. INIT_LIST_HEAD(&obj->comp_list);
  646. INIT_LIST_HEAD(&obj->async_list);
  647. cq = file->device->ib_dev->create_cq(file->device->ib_dev, cmd.cqe,
  648. cmd.comp_vector,
  649. file->ucontext, &udata);
  650. if (IS_ERR(cq)) {
  651. ret = PTR_ERR(cq);
  652. goto err_file;
  653. }
  654. cq->device = file->device->ib_dev;
  655. cq->uobject = &obj->uobject;
  656. cq->comp_handler = ib_uverbs_comp_handler;
  657. cq->event_handler = ib_uverbs_cq_event_handler;
  658. cq->cq_context = ev_file;
  659. atomic_set(&cq->usecnt, 0);
  660. obj->uobject.object = cq;
  661. ret = idr_add_uobj(&ib_uverbs_cq_idr, &obj->uobject);
  662. if (ret)
  663. goto err_free;
  664. memset(&resp, 0, sizeof resp);
  665. resp.cq_handle = obj->uobject.id;
  666. resp.cqe = cq->cqe;
  667. if (copy_to_user((void __user *) (unsigned long) cmd.response,
  668. &resp, sizeof resp)) {
  669. ret = -EFAULT;
  670. goto err_copy;
  671. }
  672. mutex_lock(&file->mutex);
  673. list_add_tail(&obj->uobject.list, &file->ucontext->cq_list);
  674. mutex_unlock(&file->mutex);
  675. obj->uobject.live = 1;
  676. up_write(&obj->uobject.mutex);
  677. return in_len;
  678. err_copy:
  679. idr_remove_uobj(&ib_uverbs_cq_idr, &obj->uobject);
  680. err_free:
  681. ib_destroy_cq(cq);
  682. err_file:
  683. if (ev_file)
  684. ib_uverbs_release_ucq(file, ev_file, obj);
  685. err:
  686. put_uobj_write(&obj->uobject);
  687. return ret;
  688. }
  689. ssize_t ib_uverbs_resize_cq(struct ib_uverbs_file *file,
  690. const char __user *buf, int in_len,
  691. int out_len)
  692. {
  693. struct ib_uverbs_resize_cq cmd;
  694. struct ib_uverbs_resize_cq_resp resp;
  695. struct ib_udata udata;
  696. struct ib_cq *cq;
  697. int ret = -EINVAL;
  698. if (copy_from_user(&cmd, buf, sizeof cmd))
  699. return -EFAULT;
  700. INIT_UDATA(&udata, buf + sizeof cmd,
  701. (unsigned long) cmd.response + sizeof resp,
  702. in_len - sizeof cmd, out_len - sizeof resp);
  703. cq = idr_read_cq(cmd.cq_handle, file->ucontext, 0);
  704. if (!cq)
  705. return -EINVAL;
  706. ret = cq->device->resize_cq(cq, cmd.cqe, &udata);
  707. if (ret)
  708. goto out;
  709. resp.cqe = cq->cqe;
  710. if (copy_to_user((void __user *) (unsigned long) cmd.response,
  711. &resp, sizeof resp.cqe))
  712. ret = -EFAULT;
  713. out:
  714. put_cq_read(cq);
  715. return ret ? ret : in_len;
  716. }
  717. static int copy_wc_to_user(void __user *dest, struct ib_wc *wc)
  718. {
  719. struct ib_uverbs_wc tmp;
  720. tmp.wr_id = wc->wr_id;
  721. tmp.status = wc->status;
  722. tmp.opcode = wc->opcode;
  723. tmp.vendor_err = wc->vendor_err;
  724. tmp.byte_len = wc->byte_len;
  725. tmp.ex.imm_data = (__u32 __force) wc->ex.imm_data;
  726. tmp.qp_num = wc->qp->qp_num;
  727. tmp.src_qp = wc->src_qp;
  728. tmp.wc_flags = wc->wc_flags;
  729. tmp.pkey_index = wc->pkey_index;
  730. tmp.slid = wc->slid;
  731. tmp.sl = wc->sl;
  732. tmp.dlid_path_bits = wc->dlid_path_bits;
  733. tmp.port_num = wc->port_num;
  734. tmp.reserved = 0;
  735. if (copy_to_user(dest, &tmp, sizeof tmp))
  736. return -EFAULT;
  737. return 0;
  738. }
  739. ssize_t ib_uverbs_poll_cq(struct ib_uverbs_file *file,
  740. const char __user *buf, int in_len,
  741. int out_len)
  742. {
  743. struct ib_uverbs_poll_cq cmd;
  744. struct ib_uverbs_poll_cq_resp resp;
  745. u8 __user *header_ptr;
  746. u8 __user *data_ptr;
  747. struct ib_cq *cq;
  748. struct ib_wc wc;
  749. int ret;
  750. if (copy_from_user(&cmd, buf, sizeof cmd))
  751. return -EFAULT;
  752. cq = idr_read_cq(cmd.cq_handle, file->ucontext, 0);
  753. if (!cq)
  754. return -EINVAL;
  755. /* we copy a struct ib_uverbs_poll_cq_resp to user space */
  756. header_ptr = (void __user *)(unsigned long) cmd.response;
  757. data_ptr = header_ptr + sizeof resp;
  758. memset(&resp, 0, sizeof resp);
  759. while (resp.count < cmd.ne) {
  760. ret = ib_poll_cq(cq, 1, &wc);
  761. if (ret < 0)
  762. goto out_put;
  763. if (!ret)
  764. break;
  765. ret = copy_wc_to_user(data_ptr, &wc);
  766. if (ret)
  767. goto out_put;
  768. data_ptr += sizeof(struct ib_uverbs_wc);
  769. ++resp.count;
  770. }
  771. if (copy_to_user(header_ptr, &resp, sizeof resp)) {
  772. ret = -EFAULT;
  773. goto out_put;
  774. }
  775. ret = in_len;
  776. out_put:
  777. put_cq_read(cq);
  778. return ret;
  779. }
  780. ssize_t ib_uverbs_req_notify_cq(struct ib_uverbs_file *file,
  781. const char __user *buf, int in_len,
  782. int out_len)
  783. {
  784. struct ib_uverbs_req_notify_cq cmd;
  785. struct ib_cq *cq;
  786. if (copy_from_user(&cmd, buf, sizeof cmd))
  787. return -EFAULT;
  788. cq = idr_read_cq(cmd.cq_handle, file->ucontext, 0);
  789. if (!cq)
  790. return -EINVAL;
  791. ib_req_notify_cq(cq, cmd.solicited_only ?
  792. IB_CQ_SOLICITED : IB_CQ_NEXT_COMP);
  793. put_cq_read(cq);
  794. return in_len;
  795. }
  796. ssize_t ib_uverbs_destroy_cq(struct ib_uverbs_file *file,
  797. const char __user *buf, int in_len,
  798. int out_len)
  799. {
  800. struct ib_uverbs_destroy_cq cmd;
  801. struct ib_uverbs_destroy_cq_resp resp;
  802. struct ib_uobject *uobj;
  803. struct ib_cq *cq;
  804. struct ib_ucq_object *obj;
  805. struct ib_uverbs_event_file *ev_file;
  806. int ret = -EINVAL;
  807. if (copy_from_user(&cmd, buf, sizeof cmd))
  808. return -EFAULT;
  809. uobj = idr_write_uobj(&ib_uverbs_cq_idr, cmd.cq_handle, file->ucontext);
  810. if (!uobj)
  811. return -EINVAL;
  812. cq = uobj->object;
  813. ev_file = cq->cq_context;
  814. obj = container_of(cq->uobject, struct ib_ucq_object, uobject);
  815. ret = ib_destroy_cq(cq);
  816. if (!ret)
  817. uobj->live = 0;
  818. put_uobj_write(uobj);
  819. if (ret)
  820. return ret;
  821. idr_remove_uobj(&ib_uverbs_cq_idr, uobj);
  822. mutex_lock(&file->mutex);
  823. list_del(&uobj->list);
  824. mutex_unlock(&file->mutex);
  825. ib_uverbs_release_ucq(file, ev_file, obj);
  826. memset(&resp, 0, sizeof resp);
  827. resp.comp_events_reported = obj->comp_events_reported;
  828. resp.async_events_reported = obj->async_events_reported;
  829. put_uobj(uobj);
  830. if (copy_to_user((void __user *) (unsigned long) cmd.response,
  831. &resp, sizeof resp))
  832. return -EFAULT;
  833. return in_len;
  834. }
  835. ssize_t ib_uverbs_create_qp(struct ib_uverbs_file *file,
  836. const char __user *buf, int in_len,
  837. int out_len)
  838. {
  839. struct ib_uverbs_create_qp cmd;
  840. struct ib_uverbs_create_qp_resp resp;
  841. struct ib_udata udata;
  842. struct ib_uqp_object *obj;
  843. struct ib_pd *pd;
  844. struct ib_cq *scq, *rcq;
  845. struct ib_srq *srq;
  846. struct ib_qp *qp;
  847. struct ib_qp_init_attr attr;
  848. int ret;
  849. if (out_len < sizeof resp)
  850. return -ENOSPC;
  851. if (copy_from_user(&cmd, buf, sizeof cmd))
  852. return -EFAULT;
  853. INIT_UDATA(&udata, buf + sizeof cmd,
  854. (unsigned long) cmd.response + sizeof resp,
  855. in_len - sizeof cmd, out_len - sizeof resp);
  856. obj = kmalloc(sizeof *obj, GFP_KERNEL);
  857. if (!obj)
  858. return -ENOMEM;
  859. init_uobj(&obj->uevent.uobject, cmd.user_handle, file->ucontext, &qp_lock_key);
  860. down_write(&obj->uevent.uobject.mutex);
  861. srq = cmd.is_srq ? idr_read_srq(cmd.srq_handle, file->ucontext) : NULL;
  862. pd = idr_read_pd(cmd.pd_handle, file->ucontext);
  863. scq = idr_read_cq(cmd.send_cq_handle, file->ucontext, 0);
  864. rcq = cmd.recv_cq_handle == cmd.send_cq_handle ?
  865. scq : idr_read_cq(cmd.recv_cq_handle, file->ucontext, 1);
  866. if (!pd || !scq || !rcq || (cmd.is_srq && !srq)) {
  867. ret = -EINVAL;
  868. goto err_put;
  869. }
  870. attr.event_handler = ib_uverbs_qp_event_handler;
  871. attr.qp_context = file;
  872. attr.send_cq = scq;
  873. attr.recv_cq = rcq;
  874. attr.srq = srq;
  875. attr.sq_sig_type = cmd.sq_sig_all ? IB_SIGNAL_ALL_WR : IB_SIGNAL_REQ_WR;
  876. attr.qp_type = cmd.qp_type;
  877. attr.create_flags = 0;
  878. attr.cap.max_send_wr = cmd.max_send_wr;
  879. attr.cap.max_recv_wr = cmd.max_recv_wr;
  880. attr.cap.max_send_sge = cmd.max_send_sge;
  881. attr.cap.max_recv_sge = cmd.max_recv_sge;
  882. attr.cap.max_inline_data = cmd.max_inline_data;
  883. obj->uevent.events_reported = 0;
  884. INIT_LIST_HEAD(&obj->uevent.event_list);
  885. INIT_LIST_HEAD(&obj->mcast_list);
  886. qp = pd->device->create_qp(pd, &attr, &udata);
  887. if (IS_ERR(qp)) {
  888. ret = PTR_ERR(qp);
  889. goto err_put;
  890. }
  891. qp->device = pd->device;
  892. qp->pd = pd;
  893. qp->send_cq = attr.send_cq;
  894. qp->recv_cq = attr.recv_cq;
  895. qp->srq = attr.srq;
  896. qp->uobject = &obj->uevent.uobject;
  897. qp->event_handler = attr.event_handler;
  898. qp->qp_context = attr.qp_context;
  899. qp->qp_type = attr.qp_type;
  900. atomic_inc(&pd->usecnt);
  901. atomic_inc(&attr.send_cq->usecnt);
  902. atomic_inc(&attr.recv_cq->usecnt);
  903. if (attr.srq)
  904. atomic_inc(&attr.srq->usecnt);
  905. obj->uevent.uobject.object = qp;
  906. ret = idr_add_uobj(&ib_uverbs_qp_idr, &obj->uevent.uobject);
  907. if (ret)
  908. goto err_destroy;
  909. memset(&resp, 0, sizeof resp);
  910. resp.qpn = qp->qp_num;
  911. resp.qp_handle = obj->uevent.uobject.id;
  912. resp.max_recv_sge = attr.cap.max_recv_sge;
  913. resp.max_send_sge = attr.cap.max_send_sge;
  914. resp.max_recv_wr = attr.cap.max_recv_wr;
  915. resp.max_send_wr = attr.cap.max_send_wr;
  916. resp.max_inline_data = attr.cap.max_inline_data;
  917. if (copy_to_user((void __user *) (unsigned long) cmd.response,
  918. &resp, sizeof resp)) {
  919. ret = -EFAULT;
  920. goto err_copy;
  921. }
  922. put_pd_read(pd);
  923. put_cq_read(scq);
  924. if (rcq != scq)
  925. put_cq_read(rcq);
  926. if (srq)
  927. put_srq_read(srq);
  928. mutex_lock(&file->mutex);
  929. list_add_tail(&obj->uevent.uobject.list, &file->ucontext->qp_list);
  930. mutex_unlock(&file->mutex);
  931. obj->uevent.uobject.live = 1;
  932. up_write(&obj->uevent.uobject.mutex);
  933. return in_len;
  934. err_copy:
  935. idr_remove_uobj(&ib_uverbs_qp_idr, &obj->uevent.uobject);
  936. err_destroy:
  937. ib_destroy_qp(qp);
  938. err_put:
  939. if (pd)
  940. put_pd_read(pd);
  941. if (scq)
  942. put_cq_read(scq);
  943. if (rcq && rcq != scq)
  944. put_cq_read(rcq);
  945. if (srq)
  946. put_srq_read(srq);
  947. put_uobj_write(&obj->uevent.uobject);
  948. return ret;
  949. }
  950. ssize_t ib_uverbs_query_qp(struct ib_uverbs_file *file,
  951. const char __user *buf, int in_len,
  952. int out_len)
  953. {
  954. struct ib_uverbs_query_qp cmd;
  955. struct ib_uverbs_query_qp_resp resp;
  956. struct ib_qp *qp;
  957. struct ib_qp_attr *attr;
  958. struct ib_qp_init_attr *init_attr;
  959. int ret;
  960. if (copy_from_user(&cmd, buf, sizeof cmd))
  961. return -EFAULT;
  962. attr = kmalloc(sizeof *attr, GFP_KERNEL);
  963. init_attr = kmalloc(sizeof *init_attr, GFP_KERNEL);
  964. if (!attr || !init_attr) {
  965. ret = -ENOMEM;
  966. goto out;
  967. }
  968. qp = idr_read_qp(cmd.qp_handle, file->ucontext);
  969. if (!qp) {
  970. ret = -EINVAL;
  971. goto out;
  972. }
  973. ret = ib_query_qp(qp, attr, cmd.attr_mask, init_attr);
  974. put_qp_read(qp);
  975. if (ret)
  976. goto out;
  977. memset(&resp, 0, sizeof resp);
  978. resp.qp_state = attr->qp_state;
  979. resp.cur_qp_state = attr->cur_qp_state;
  980. resp.path_mtu = attr->path_mtu;
  981. resp.path_mig_state = attr->path_mig_state;
  982. resp.qkey = attr->qkey;
  983. resp.rq_psn = attr->rq_psn;
  984. resp.sq_psn = attr->sq_psn;
  985. resp.dest_qp_num = attr->dest_qp_num;
  986. resp.qp_access_flags = attr->qp_access_flags;
  987. resp.pkey_index = attr->pkey_index;
  988. resp.alt_pkey_index = attr->alt_pkey_index;
  989. resp.sq_draining = attr->sq_draining;
  990. resp.max_rd_atomic = attr->max_rd_atomic;
  991. resp.max_dest_rd_atomic = attr->max_dest_rd_atomic;
  992. resp.min_rnr_timer = attr->min_rnr_timer;
  993. resp.port_num = attr->port_num;
  994. resp.timeout = attr->timeout;
  995. resp.retry_cnt = attr->retry_cnt;
  996. resp.rnr_retry = attr->rnr_retry;
  997. resp.alt_port_num = attr->alt_port_num;
  998. resp.alt_timeout = attr->alt_timeout;
  999. memcpy(resp.dest.dgid, attr->ah_attr.grh.dgid.raw, 16);
  1000. resp.dest.flow_label = attr->ah_attr.grh.flow_label;
  1001. resp.dest.sgid_index = attr->ah_attr.grh.sgid_index;
  1002. resp.dest.hop_limit = attr->ah_attr.grh.hop_limit;
  1003. resp.dest.traffic_class = attr->ah_attr.grh.traffic_class;
  1004. resp.dest.dlid = attr->ah_attr.dlid;
  1005. resp.dest.sl = attr->ah_attr.sl;
  1006. resp.dest.src_path_bits = attr->ah_attr.src_path_bits;
  1007. resp.dest.static_rate = attr->ah_attr.static_rate;
  1008. resp.dest.is_global = !!(attr->ah_attr.ah_flags & IB_AH_GRH);
  1009. resp.dest.port_num = attr->ah_attr.port_num;
  1010. memcpy(resp.alt_dest.dgid, attr->alt_ah_attr.grh.dgid.raw, 16);
  1011. resp.alt_dest.flow_label = attr->alt_ah_attr.grh.flow_label;
  1012. resp.alt_dest.sgid_index = attr->alt_ah_attr.grh.sgid_index;
  1013. resp.alt_dest.hop_limit = attr->alt_ah_attr.grh.hop_limit;
  1014. resp.alt_dest.traffic_class = attr->alt_ah_attr.grh.traffic_class;
  1015. resp.alt_dest.dlid = attr->alt_ah_attr.dlid;
  1016. resp.alt_dest.sl = attr->alt_ah_attr.sl;
  1017. resp.alt_dest.src_path_bits = attr->alt_ah_attr.src_path_bits;
  1018. resp.alt_dest.static_rate = attr->alt_ah_attr.static_rate;
  1019. resp.alt_dest.is_global = !!(attr->alt_ah_attr.ah_flags & IB_AH_GRH);
  1020. resp.alt_dest.port_num = attr->alt_ah_attr.port_num;
  1021. resp.max_send_wr = init_attr->cap.max_send_wr;
  1022. resp.max_recv_wr = init_attr->cap.max_recv_wr;
  1023. resp.max_send_sge = init_attr->cap.max_send_sge;
  1024. resp.max_recv_sge = init_attr->cap.max_recv_sge;
  1025. resp.max_inline_data = init_attr->cap.max_inline_data;
  1026. resp.sq_sig_all = init_attr->sq_sig_type == IB_SIGNAL_ALL_WR;
  1027. if (copy_to_user((void __user *) (unsigned long) cmd.response,
  1028. &resp, sizeof resp))
  1029. ret = -EFAULT;
  1030. out:
  1031. kfree(attr);
  1032. kfree(init_attr);
  1033. return ret ? ret : in_len;
  1034. }
  1035. ssize_t ib_uverbs_modify_qp(struct ib_uverbs_file *file,
  1036. const char __user *buf, int in_len,
  1037. int out_len)
  1038. {
  1039. struct ib_uverbs_modify_qp cmd;
  1040. struct ib_udata udata;
  1041. struct ib_qp *qp;
  1042. struct ib_qp_attr *attr;
  1043. int ret;
  1044. if (copy_from_user(&cmd, buf, sizeof cmd))
  1045. return -EFAULT;
  1046. INIT_UDATA(&udata, buf + sizeof cmd, NULL, in_len - sizeof cmd,
  1047. out_len);
  1048. attr = kmalloc(sizeof *attr, GFP_KERNEL);
  1049. if (!attr)
  1050. return -ENOMEM;
  1051. qp = idr_read_qp(cmd.qp_handle, file->ucontext);
  1052. if (!qp) {
  1053. ret = -EINVAL;
  1054. goto out;
  1055. }
  1056. attr->qp_state = cmd.qp_state;
  1057. attr->cur_qp_state = cmd.cur_qp_state;
  1058. attr->path_mtu = cmd.path_mtu;
  1059. attr->path_mig_state = cmd.path_mig_state;
  1060. attr->qkey = cmd.qkey;
  1061. attr->rq_psn = cmd.rq_psn;
  1062. attr->sq_psn = cmd.sq_psn;
  1063. attr->dest_qp_num = cmd.dest_qp_num;
  1064. attr->qp_access_flags = cmd.qp_access_flags;
  1065. attr->pkey_index = cmd.pkey_index;
  1066. attr->alt_pkey_index = cmd.alt_pkey_index;
  1067. attr->en_sqd_async_notify = cmd.en_sqd_async_notify;
  1068. attr->max_rd_atomic = cmd.max_rd_atomic;
  1069. attr->max_dest_rd_atomic = cmd.max_dest_rd_atomic;
  1070. attr->min_rnr_timer = cmd.min_rnr_timer;
  1071. attr->port_num = cmd.port_num;
  1072. attr->timeout = cmd.timeout;
  1073. attr->retry_cnt = cmd.retry_cnt;
  1074. attr->rnr_retry = cmd.rnr_retry;
  1075. attr->alt_port_num = cmd.alt_port_num;
  1076. attr->alt_timeout = cmd.alt_timeout;
  1077. memcpy(attr->ah_attr.grh.dgid.raw, cmd.dest.dgid, 16);
  1078. attr->ah_attr.grh.flow_label = cmd.dest.flow_label;
  1079. attr->ah_attr.grh.sgid_index = cmd.dest.sgid_index;
  1080. attr->ah_attr.grh.hop_limit = cmd.dest.hop_limit;
  1081. attr->ah_attr.grh.traffic_class = cmd.dest.traffic_class;
  1082. attr->ah_attr.dlid = cmd.dest.dlid;
  1083. attr->ah_attr.sl = cmd.dest.sl;
  1084. attr->ah_attr.src_path_bits = cmd.dest.src_path_bits;
  1085. attr->ah_attr.static_rate = cmd.dest.static_rate;
  1086. attr->ah_attr.ah_flags = cmd.dest.is_global ? IB_AH_GRH : 0;
  1087. attr->ah_attr.port_num = cmd.dest.port_num;
  1088. memcpy(attr->alt_ah_attr.grh.dgid.raw, cmd.alt_dest.dgid, 16);
  1089. attr->alt_ah_attr.grh.flow_label = cmd.alt_dest.flow_label;
  1090. attr->alt_ah_attr.grh.sgid_index = cmd.alt_dest.sgid_index;
  1091. attr->alt_ah_attr.grh.hop_limit = cmd.alt_dest.hop_limit;
  1092. attr->alt_ah_attr.grh.traffic_class = cmd.alt_dest.traffic_class;
  1093. attr->alt_ah_attr.dlid = cmd.alt_dest.dlid;
  1094. attr->alt_ah_attr.sl = cmd.alt_dest.sl;
  1095. attr->alt_ah_attr.src_path_bits = cmd.alt_dest.src_path_bits;
  1096. attr->alt_ah_attr.static_rate = cmd.alt_dest.static_rate;
  1097. attr->alt_ah_attr.ah_flags = cmd.alt_dest.is_global ? IB_AH_GRH : 0;
  1098. attr->alt_ah_attr.port_num = cmd.alt_dest.port_num;
  1099. ret = qp->device->modify_qp(qp, attr, cmd.attr_mask, &udata);
  1100. put_qp_read(qp);
  1101. if (ret)
  1102. goto out;
  1103. ret = in_len;
  1104. out:
  1105. kfree(attr);
  1106. return ret;
  1107. }
  1108. ssize_t ib_uverbs_destroy_qp(struct ib_uverbs_file *file,
  1109. const char __user *buf, int in_len,
  1110. int out_len)
  1111. {
  1112. struct ib_uverbs_destroy_qp cmd;
  1113. struct ib_uverbs_destroy_qp_resp resp;
  1114. struct ib_uobject *uobj;
  1115. struct ib_qp *qp;
  1116. struct ib_uqp_object *obj;
  1117. int ret = -EINVAL;
  1118. if (copy_from_user(&cmd, buf, sizeof cmd))
  1119. return -EFAULT;
  1120. memset(&resp, 0, sizeof resp);
  1121. uobj = idr_write_uobj(&ib_uverbs_qp_idr, cmd.qp_handle, file->ucontext);
  1122. if (!uobj)
  1123. return -EINVAL;
  1124. qp = uobj->object;
  1125. obj = container_of(uobj, struct ib_uqp_object, uevent.uobject);
  1126. if (!list_empty(&obj->mcast_list)) {
  1127. put_uobj_write(uobj);
  1128. return -EBUSY;
  1129. }
  1130. ret = ib_destroy_qp(qp);
  1131. if (!ret)
  1132. uobj->live = 0;
  1133. put_uobj_write(uobj);
  1134. if (ret)
  1135. return ret;
  1136. idr_remove_uobj(&ib_uverbs_qp_idr, uobj);
  1137. mutex_lock(&file->mutex);
  1138. list_del(&uobj->list);
  1139. mutex_unlock(&file->mutex);
  1140. ib_uverbs_release_uevent(file, &obj->uevent);
  1141. resp.events_reported = obj->uevent.events_reported;
  1142. put_uobj(uobj);
  1143. if (copy_to_user((void __user *) (unsigned long) cmd.response,
  1144. &resp, sizeof resp))
  1145. return -EFAULT;
  1146. return in_len;
  1147. }
  1148. ssize_t ib_uverbs_post_send(struct ib_uverbs_file *file,
  1149. const char __user *buf, int in_len,
  1150. int out_len)
  1151. {
  1152. struct ib_uverbs_post_send cmd;
  1153. struct ib_uverbs_post_send_resp resp;
  1154. struct ib_uverbs_send_wr *user_wr;
  1155. struct ib_send_wr *wr = NULL, *last, *next, *bad_wr;
  1156. struct ib_qp *qp;
  1157. int i, sg_ind;
  1158. int is_ud;
  1159. ssize_t ret = -EINVAL;
  1160. if (copy_from_user(&cmd, buf, sizeof cmd))
  1161. return -EFAULT;
  1162. if (in_len < sizeof cmd + cmd.wqe_size * cmd.wr_count +
  1163. cmd.sge_count * sizeof (struct ib_uverbs_sge))
  1164. return -EINVAL;
  1165. if (cmd.wqe_size < sizeof (struct ib_uverbs_send_wr))
  1166. return -EINVAL;
  1167. user_wr = kmalloc(cmd.wqe_size, GFP_KERNEL);
  1168. if (!user_wr)
  1169. return -ENOMEM;
  1170. qp = idr_read_qp(cmd.qp_handle, file->ucontext);
  1171. if (!qp)
  1172. goto out;
  1173. is_ud = qp->qp_type == IB_QPT_UD;
  1174. sg_ind = 0;
  1175. last = NULL;
  1176. for (i = 0; i < cmd.wr_count; ++i) {
  1177. if (copy_from_user(user_wr,
  1178. buf + sizeof cmd + i * cmd.wqe_size,
  1179. cmd.wqe_size)) {
  1180. ret = -EFAULT;
  1181. goto out_put;
  1182. }
  1183. if (user_wr->num_sge + sg_ind > cmd.sge_count) {
  1184. ret = -EINVAL;
  1185. goto out_put;
  1186. }
  1187. next = kmalloc(ALIGN(sizeof *next, sizeof (struct ib_sge)) +
  1188. user_wr->num_sge * sizeof (struct ib_sge),
  1189. GFP_KERNEL);
  1190. if (!next) {
  1191. ret = -ENOMEM;
  1192. goto out_put;
  1193. }
  1194. if (!last)
  1195. wr = next;
  1196. else
  1197. last->next = next;
  1198. last = next;
  1199. next->next = NULL;
  1200. next->wr_id = user_wr->wr_id;
  1201. next->num_sge = user_wr->num_sge;
  1202. next->opcode = user_wr->opcode;
  1203. next->send_flags = user_wr->send_flags;
  1204. if (is_ud) {
  1205. next->wr.ud.ah = idr_read_ah(user_wr->wr.ud.ah,
  1206. file->ucontext);
  1207. if (!next->wr.ud.ah) {
  1208. ret = -EINVAL;
  1209. goto out_put;
  1210. }
  1211. next->wr.ud.remote_qpn = user_wr->wr.ud.remote_qpn;
  1212. next->wr.ud.remote_qkey = user_wr->wr.ud.remote_qkey;
  1213. } else {
  1214. switch (next->opcode) {
  1215. case IB_WR_RDMA_WRITE_WITH_IMM:
  1216. next->ex.imm_data =
  1217. (__be32 __force) user_wr->ex.imm_data;
  1218. case IB_WR_RDMA_WRITE:
  1219. case IB_WR_RDMA_READ:
  1220. next->wr.rdma.remote_addr =
  1221. user_wr->wr.rdma.remote_addr;
  1222. next->wr.rdma.rkey =
  1223. user_wr->wr.rdma.rkey;
  1224. break;
  1225. case IB_WR_SEND_WITH_IMM:
  1226. next->ex.imm_data =
  1227. (__be32 __force) user_wr->ex.imm_data;
  1228. break;
  1229. case IB_WR_SEND_WITH_INV:
  1230. next->ex.invalidate_rkey =
  1231. user_wr->ex.invalidate_rkey;
  1232. break;
  1233. case IB_WR_ATOMIC_CMP_AND_SWP:
  1234. case IB_WR_ATOMIC_FETCH_AND_ADD:
  1235. next->wr.atomic.remote_addr =
  1236. user_wr->wr.atomic.remote_addr;
  1237. next->wr.atomic.compare_add =
  1238. user_wr->wr.atomic.compare_add;
  1239. next->wr.atomic.swap = user_wr->wr.atomic.swap;
  1240. next->wr.atomic.rkey = user_wr->wr.atomic.rkey;
  1241. break;
  1242. default:
  1243. break;
  1244. }
  1245. }
  1246. if (next->num_sge) {
  1247. next->sg_list = (void *) next +
  1248. ALIGN(sizeof *next, sizeof (struct ib_sge));
  1249. if (copy_from_user(next->sg_list,
  1250. buf + sizeof cmd +
  1251. cmd.wr_count * cmd.wqe_size +
  1252. sg_ind * sizeof (struct ib_sge),
  1253. next->num_sge * sizeof (struct ib_sge))) {
  1254. ret = -EFAULT;
  1255. goto out_put;
  1256. }
  1257. sg_ind += next->num_sge;
  1258. } else
  1259. next->sg_list = NULL;
  1260. }
  1261. resp.bad_wr = 0;
  1262. ret = qp->device->post_send(qp, wr, &bad_wr);
  1263. if (ret)
  1264. for (next = wr; next; next = next->next) {
  1265. ++resp.bad_wr;
  1266. if (next == bad_wr)
  1267. break;
  1268. }
  1269. if (copy_to_user((void __user *) (unsigned long) cmd.response,
  1270. &resp, sizeof resp))
  1271. ret = -EFAULT;
  1272. out_put:
  1273. put_qp_read(qp);
  1274. while (wr) {
  1275. if (is_ud && wr->wr.ud.ah)
  1276. put_ah_read(wr->wr.ud.ah);
  1277. next = wr->next;
  1278. kfree(wr);
  1279. wr = next;
  1280. }
  1281. out:
  1282. kfree(user_wr);
  1283. return ret ? ret : in_len;
  1284. }
  1285. static struct ib_recv_wr *ib_uverbs_unmarshall_recv(const char __user *buf,
  1286. int in_len,
  1287. u32 wr_count,
  1288. u32 sge_count,
  1289. u32 wqe_size)
  1290. {
  1291. struct ib_uverbs_recv_wr *user_wr;
  1292. struct ib_recv_wr *wr = NULL, *last, *next;
  1293. int sg_ind;
  1294. int i;
  1295. int ret;
  1296. if (in_len < wqe_size * wr_count +
  1297. sge_count * sizeof (struct ib_uverbs_sge))
  1298. return ERR_PTR(-EINVAL);
  1299. if (wqe_size < sizeof (struct ib_uverbs_recv_wr))
  1300. return ERR_PTR(-EINVAL);
  1301. user_wr = kmalloc(wqe_size, GFP_KERNEL);
  1302. if (!user_wr)
  1303. return ERR_PTR(-ENOMEM);
  1304. sg_ind = 0;
  1305. last = NULL;
  1306. for (i = 0; i < wr_count; ++i) {
  1307. if (copy_from_user(user_wr, buf + i * wqe_size,
  1308. wqe_size)) {
  1309. ret = -EFAULT;
  1310. goto err;
  1311. }
  1312. if (user_wr->num_sge + sg_ind > sge_count) {
  1313. ret = -EINVAL;
  1314. goto err;
  1315. }
  1316. next = kmalloc(ALIGN(sizeof *next, sizeof (struct ib_sge)) +
  1317. user_wr->num_sge * sizeof (struct ib_sge),
  1318. GFP_KERNEL);
  1319. if (!next) {
  1320. ret = -ENOMEM;
  1321. goto err;
  1322. }
  1323. if (!last)
  1324. wr = next;
  1325. else
  1326. last->next = next;
  1327. last = next;
  1328. next->next = NULL;
  1329. next->wr_id = user_wr->wr_id;
  1330. next->num_sge = user_wr->num_sge;
  1331. if (next->num_sge) {
  1332. next->sg_list = (void *) next +
  1333. ALIGN(sizeof *next, sizeof (struct ib_sge));
  1334. if (copy_from_user(next->sg_list,
  1335. buf + wr_count * wqe_size +
  1336. sg_ind * sizeof (struct ib_sge),
  1337. next->num_sge * sizeof (struct ib_sge))) {
  1338. ret = -EFAULT;
  1339. goto err;
  1340. }
  1341. sg_ind += next->num_sge;
  1342. } else
  1343. next->sg_list = NULL;
  1344. }
  1345. kfree(user_wr);
  1346. return wr;
  1347. err:
  1348. kfree(user_wr);
  1349. while (wr) {
  1350. next = wr->next;
  1351. kfree(wr);
  1352. wr = next;
  1353. }
  1354. return ERR_PTR(ret);
  1355. }
  1356. ssize_t ib_uverbs_post_recv(struct ib_uverbs_file *file,
  1357. const char __user *buf, int in_len,
  1358. int out_len)
  1359. {
  1360. struct ib_uverbs_post_recv cmd;
  1361. struct ib_uverbs_post_recv_resp resp;
  1362. struct ib_recv_wr *wr, *next, *bad_wr;
  1363. struct ib_qp *qp;
  1364. ssize_t ret = -EINVAL;
  1365. if (copy_from_user(&cmd, buf, sizeof cmd))
  1366. return -EFAULT;
  1367. wr = ib_uverbs_unmarshall_recv(buf + sizeof cmd,
  1368. in_len - sizeof cmd, cmd.wr_count,
  1369. cmd.sge_count, cmd.wqe_size);
  1370. if (IS_ERR(wr))
  1371. return PTR_ERR(wr);
  1372. qp = idr_read_qp(cmd.qp_handle, file->ucontext);
  1373. if (!qp)
  1374. goto out;
  1375. resp.bad_wr = 0;
  1376. ret = qp->device->post_recv(qp, wr, &bad_wr);
  1377. put_qp_read(qp);
  1378. if (ret)
  1379. for (next = wr; next; next = next->next) {
  1380. ++resp.bad_wr;
  1381. if (next == bad_wr)
  1382. break;
  1383. }
  1384. if (copy_to_user((void __user *) (unsigned long) cmd.response,
  1385. &resp, sizeof resp))
  1386. ret = -EFAULT;
  1387. out:
  1388. while (wr) {
  1389. next = wr->next;
  1390. kfree(wr);
  1391. wr = next;
  1392. }
  1393. return ret ? ret : in_len;
  1394. }
  1395. ssize_t ib_uverbs_post_srq_recv(struct ib_uverbs_file *file,
  1396. const char __user *buf, int in_len,
  1397. int out_len)
  1398. {
  1399. struct ib_uverbs_post_srq_recv cmd;
  1400. struct ib_uverbs_post_srq_recv_resp resp;
  1401. struct ib_recv_wr *wr, *next, *bad_wr;
  1402. struct ib_srq *srq;
  1403. ssize_t ret = -EINVAL;
  1404. if (copy_from_user(&cmd, buf, sizeof cmd))
  1405. return -EFAULT;
  1406. wr = ib_uverbs_unmarshall_recv(buf + sizeof cmd,
  1407. in_len - sizeof cmd, cmd.wr_count,
  1408. cmd.sge_count, cmd.wqe_size);
  1409. if (IS_ERR(wr))
  1410. return PTR_ERR(wr);
  1411. srq = idr_read_srq(cmd.srq_handle, file->ucontext);
  1412. if (!srq)
  1413. goto out;
  1414. resp.bad_wr = 0;
  1415. ret = srq->device->post_srq_recv(srq, wr, &bad_wr);
  1416. put_srq_read(srq);
  1417. if (ret)
  1418. for (next = wr; next; next = next->next) {
  1419. ++resp.bad_wr;
  1420. if (next == bad_wr)
  1421. break;
  1422. }
  1423. if (copy_to_user((void __user *) (unsigned long) cmd.response,
  1424. &resp, sizeof resp))
  1425. ret = -EFAULT;
  1426. out:
  1427. while (wr) {
  1428. next = wr->next;
  1429. kfree(wr);
  1430. wr = next;
  1431. }
  1432. return ret ? ret : in_len;
  1433. }
  1434. ssize_t ib_uverbs_create_ah(struct ib_uverbs_file *file,
  1435. const char __user *buf, int in_len,
  1436. int out_len)
  1437. {
  1438. struct ib_uverbs_create_ah cmd;
  1439. struct ib_uverbs_create_ah_resp resp;
  1440. struct ib_uobject *uobj;
  1441. struct ib_pd *pd;
  1442. struct ib_ah *ah;
  1443. struct ib_ah_attr attr;
  1444. int ret;
  1445. if (out_len < sizeof resp)
  1446. return -ENOSPC;
  1447. if (copy_from_user(&cmd, buf, sizeof cmd))
  1448. return -EFAULT;
  1449. uobj = kmalloc(sizeof *uobj, GFP_KERNEL);
  1450. if (!uobj)
  1451. return -ENOMEM;
  1452. init_uobj(uobj, cmd.user_handle, file->ucontext, &ah_lock_key);
  1453. down_write(&uobj->mutex);
  1454. pd = idr_read_pd(cmd.pd_handle, file->ucontext);
  1455. if (!pd) {
  1456. ret = -EINVAL;
  1457. goto err;
  1458. }
  1459. attr.dlid = cmd.attr.dlid;
  1460. attr.sl = cmd.attr.sl;
  1461. attr.src_path_bits = cmd.attr.src_path_bits;
  1462. attr.static_rate = cmd.attr.static_rate;
  1463. attr.ah_flags = cmd.attr.is_global ? IB_AH_GRH : 0;
  1464. attr.port_num = cmd.attr.port_num;
  1465. attr.grh.flow_label = cmd.attr.grh.flow_label;
  1466. attr.grh.sgid_index = cmd.attr.grh.sgid_index;
  1467. attr.grh.hop_limit = cmd.attr.grh.hop_limit;
  1468. attr.grh.traffic_class = cmd.attr.grh.traffic_class;
  1469. memcpy(attr.grh.dgid.raw, cmd.attr.grh.dgid, 16);
  1470. ah = ib_create_ah(pd, &attr);
  1471. if (IS_ERR(ah)) {
  1472. ret = PTR_ERR(ah);
  1473. goto err_put;
  1474. }
  1475. ah->uobject = uobj;
  1476. uobj->object = ah;
  1477. ret = idr_add_uobj(&ib_uverbs_ah_idr, uobj);
  1478. if (ret)
  1479. goto err_destroy;
  1480. resp.ah_handle = uobj->id;
  1481. if (copy_to_user((void __user *) (unsigned long) cmd.response,
  1482. &resp, sizeof resp)) {
  1483. ret = -EFAULT;
  1484. goto err_copy;
  1485. }
  1486. put_pd_read(pd);
  1487. mutex_lock(&file->mutex);
  1488. list_add_tail(&uobj->list, &file->ucontext->ah_list);
  1489. mutex_unlock(&file->mutex);
  1490. uobj->live = 1;
  1491. up_write(&uobj->mutex);
  1492. return in_len;
  1493. err_copy:
  1494. idr_remove_uobj(&ib_uverbs_ah_idr, uobj);
  1495. err_destroy:
  1496. ib_destroy_ah(ah);
  1497. err_put:
  1498. put_pd_read(pd);
  1499. err:
  1500. put_uobj_write(uobj);
  1501. return ret;
  1502. }
  1503. ssize_t ib_uverbs_destroy_ah(struct ib_uverbs_file *file,
  1504. const char __user *buf, int in_len, int out_len)
  1505. {
  1506. struct ib_uverbs_destroy_ah cmd;
  1507. struct ib_ah *ah;
  1508. struct ib_uobject *uobj;
  1509. int ret;
  1510. if (copy_from_user(&cmd, buf, sizeof cmd))
  1511. return -EFAULT;
  1512. uobj = idr_write_uobj(&ib_uverbs_ah_idr, cmd.ah_handle, file->ucontext);
  1513. if (!uobj)
  1514. return -EINVAL;
  1515. ah = uobj->object;
  1516. ret = ib_destroy_ah(ah);
  1517. if (!ret)
  1518. uobj->live = 0;
  1519. put_uobj_write(uobj);
  1520. if (ret)
  1521. return ret;
  1522. idr_remove_uobj(&ib_uverbs_ah_idr, uobj);
  1523. mutex_lock(&file->mutex);
  1524. list_del(&uobj->list);
  1525. mutex_unlock(&file->mutex);
  1526. put_uobj(uobj);
  1527. return in_len;
  1528. }
  1529. ssize_t ib_uverbs_attach_mcast(struct ib_uverbs_file *file,
  1530. const char __user *buf, int in_len,
  1531. int out_len)
  1532. {
  1533. struct ib_uverbs_attach_mcast cmd;
  1534. struct ib_qp *qp;
  1535. struct ib_uqp_object *obj;
  1536. struct ib_uverbs_mcast_entry *mcast;
  1537. int ret;
  1538. if (copy_from_user(&cmd, buf, sizeof cmd))
  1539. return -EFAULT;
  1540. qp = idr_read_qp(cmd.qp_handle, file->ucontext);
  1541. if (!qp)
  1542. return -EINVAL;
  1543. obj = container_of(qp->uobject, struct ib_uqp_object, uevent.uobject);
  1544. list_for_each_entry(mcast, &obj->mcast_list, list)
  1545. if (cmd.mlid == mcast->lid &&
  1546. !memcmp(cmd.gid, mcast->gid.raw, sizeof mcast->gid.raw)) {
  1547. ret = 0;
  1548. goto out_put;
  1549. }
  1550. mcast = kmalloc(sizeof *mcast, GFP_KERNEL);
  1551. if (!mcast) {
  1552. ret = -ENOMEM;
  1553. goto out_put;
  1554. }
  1555. mcast->lid = cmd.mlid;
  1556. memcpy(mcast->gid.raw, cmd.gid, sizeof mcast->gid.raw);
  1557. ret = ib_attach_mcast(qp, &mcast->gid, cmd.mlid);
  1558. if (!ret)
  1559. list_add_tail(&mcast->list, &obj->mcast_list);
  1560. else
  1561. kfree(mcast);
  1562. out_put:
  1563. put_qp_read(qp);
  1564. return ret ? ret : in_len;
  1565. }
  1566. ssize_t ib_uverbs_detach_mcast(struct ib_uverbs_file *file,
  1567. const char __user *buf, int in_len,
  1568. int out_len)
  1569. {
  1570. struct ib_uverbs_detach_mcast cmd;
  1571. struct ib_uqp_object *obj;
  1572. struct ib_qp *qp;
  1573. struct ib_uverbs_mcast_entry *mcast;
  1574. int ret = -EINVAL;
  1575. if (copy_from_user(&cmd, buf, sizeof cmd))
  1576. return -EFAULT;
  1577. qp = idr_read_qp(cmd.qp_handle, file->ucontext);
  1578. if (!qp)
  1579. return -EINVAL;
  1580. ret = ib_detach_mcast(qp, (union ib_gid *) cmd.gid, cmd.mlid);
  1581. if (ret)
  1582. goto out_put;
  1583. obj = container_of(qp->uobject, struct ib_uqp_object, uevent.uobject);
  1584. list_for_each_entry(mcast, &obj->mcast_list, list)
  1585. if (cmd.mlid == mcast->lid &&
  1586. !memcmp(cmd.gid, mcast->gid.raw, sizeof mcast->gid.raw)) {
  1587. list_del(&mcast->list);
  1588. kfree(mcast);
  1589. break;
  1590. }
  1591. out_put:
  1592. put_qp_read(qp);
  1593. return ret ? ret : in_len;
  1594. }
  1595. ssize_t ib_uverbs_create_srq(struct ib_uverbs_file *file,
  1596. const char __user *buf, int in_len,
  1597. int out_len)
  1598. {
  1599. struct ib_uverbs_create_srq cmd;
  1600. struct ib_uverbs_create_srq_resp resp;
  1601. struct ib_udata udata;
  1602. struct ib_uevent_object *obj;
  1603. struct ib_pd *pd;
  1604. struct ib_srq *srq;
  1605. struct ib_srq_init_attr attr;
  1606. int ret;
  1607. if (out_len < sizeof resp)
  1608. return -ENOSPC;
  1609. if (copy_from_user(&cmd, buf, sizeof cmd))
  1610. return -EFAULT;
  1611. INIT_UDATA(&udata, buf + sizeof cmd,
  1612. (unsigned long) cmd.response + sizeof resp,
  1613. in_len - sizeof cmd, out_len - sizeof resp);
  1614. obj = kmalloc(sizeof *obj, GFP_KERNEL);
  1615. if (!obj)
  1616. return -ENOMEM;
  1617. init_uobj(&obj->uobject, cmd.user_handle, file->ucontext, &srq_lock_key);
  1618. down_write(&obj->uobject.mutex);
  1619. pd = idr_read_pd(cmd.pd_handle, file->ucontext);
  1620. if (!pd) {
  1621. ret = -EINVAL;
  1622. goto err;
  1623. }
  1624. attr.event_handler = ib_uverbs_srq_event_handler;
  1625. attr.srq_context = file;
  1626. attr.attr.max_wr = cmd.max_wr;
  1627. attr.attr.max_sge = cmd.max_sge;
  1628. attr.attr.srq_limit = cmd.srq_limit;
  1629. obj->events_reported = 0;
  1630. INIT_LIST_HEAD(&obj->event_list);
  1631. srq = pd->device->create_srq(pd, &attr, &udata);
  1632. if (IS_ERR(srq)) {
  1633. ret = PTR_ERR(srq);
  1634. goto err_put;
  1635. }
  1636. srq->device = pd->device;
  1637. srq->pd = pd;
  1638. srq->uobject = &obj->uobject;
  1639. srq->event_handler = attr.event_handler;
  1640. srq->srq_context = attr.srq_context;
  1641. atomic_inc(&pd->usecnt);
  1642. atomic_set(&srq->usecnt, 0);
  1643. obj->uobject.object = srq;
  1644. ret = idr_add_uobj(&ib_uverbs_srq_idr, &obj->uobject);
  1645. if (ret)
  1646. goto err_destroy;
  1647. memset(&resp, 0, sizeof resp);
  1648. resp.srq_handle = obj->uobject.id;
  1649. resp.max_wr = attr.attr.max_wr;
  1650. resp.max_sge = attr.attr.max_sge;
  1651. if (copy_to_user((void __user *) (unsigned long) cmd.response,
  1652. &resp, sizeof resp)) {
  1653. ret = -EFAULT;
  1654. goto err_copy;
  1655. }
  1656. put_pd_read(pd);
  1657. mutex_lock(&file->mutex);
  1658. list_add_tail(&obj->uobject.list, &file->ucontext->srq_list);
  1659. mutex_unlock(&file->mutex);
  1660. obj->uobject.live = 1;
  1661. up_write(&obj->uobject.mutex);
  1662. return in_len;
  1663. err_copy:
  1664. idr_remove_uobj(&ib_uverbs_srq_idr, &obj->uobject);
  1665. err_destroy:
  1666. ib_destroy_srq(srq);
  1667. err_put:
  1668. put_pd_read(pd);
  1669. err:
  1670. put_uobj_write(&obj->uobject);
  1671. return ret;
  1672. }
  1673. ssize_t ib_uverbs_modify_srq(struct ib_uverbs_file *file,
  1674. const char __user *buf, int in_len,
  1675. int out_len)
  1676. {
  1677. struct ib_uverbs_modify_srq cmd;
  1678. struct ib_udata udata;
  1679. struct ib_srq *srq;
  1680. struct ib_srq_attr attr;
  1681. int ret;
  1682. if (copy_from_user(&cmd, buf, sizeof cmd))
  1683. return -EFAULT;
  1684. INIT_UDATA(&udata, buf + sizeof cmd, NULL, in_len - sizeof cmd,
  1685. out_len);
  1686. srq = idr_read_srq(cmd.srq_handle, file->ucontext);
  1687. if (!srq)
  1688. return -EINVAL;
  1689. attr.max_wr = cmd.max_wr;
  1690. attr.srq_limit = cmd.srq_limit;
  1691. ret = srq->device->modify_srq(srq, &attr, cmd.attr_mask, &udata);
  1692. put_srq_read(srq);
  1693. return ret ? ret : in_len;
  1694. }
  1695. ssize_t ib_uverbs_query_srq(struct ib_uverbs_file *file,
  1696. const char __user *buf,
  1697. int in_len, int out_len)
  1698. {
  1699. struct ib_uverbs_query_srq cmd;
  1700. struct ib_uverbs_query_srq_resp resp;
  1701. struct ib_srq_attr attr;
  1702. struct ib_srq *srq;
  1703. int ret;
  1704. if (out_len < sizeof resp)
  1705. return -ENOSPC;
  1706. if (copy_from_user(&cmd, buf, sizeof cmd))
  1707. return -EFAULT;
  1708. srq = idr_read_srq(cmd.srq_handle, file->ucontext);
  1709. if (!srq)
  1710. return -EINVAL;
  1711. ret = ib_query_srq(srq, &attr);
  1712. put_srq_read(srq);
  1713. if (ret)
  1714. return ret;
  1715. memset(&resp, 0, sizeof resp);
  1716. resp.max_wr = attr.max_wr;
  1717. resp.max_sge = attr.max_sge;
  1718. resp.srq_limit = attr.srq_limit;
  1719. if (copy_to_user((void __user *) (unsigned long) cmd.response,
  1720. &resp, sizeof resp))
  1721. return -EFAULT;
  1722. return in_len;
  1723. }
  1724. ssize_t ib_uverbs_destroy_srq(struct ib_uverbs_file *file,
  1725. const char __user *buf, int in_len,
  1726. int out_len)
  1727. {
  1728. struct ib_uverbs_destroy_srq cmd;
  1729. struct ib_uverbs_destroy_srq_resp resp;
  1730. struct ib_uobject *uobj;
  1731. struct ib_srq *srq;
  1732. struct ib_uevent_object *obj;
  1733. int ret = -EINVAL;
  1734. if (copy_from_user(&cmd, buf, sizeof cmd))
  1735. return -EFAULT;
  1736. uobj = idr_write_uobj(&ib_uverbs_srq_idr, cmd.srq_handle, file->ucontext);
  1737. if (!uobj)
  1738. return -EINVAL;
  1739. srq = uobj->object;
  1740. obj = container_of(uobj, struct ib_uevent_object, uobject);
  1741. ret = ib_destroy_srq(srq);
  1742. if (!ret)
  1743. uobj->live = 0;
  1744. put_uobj_write(uobj);
  1745. if (ret)
  1746. return ret;
  1747. idr_remove_uobj(&ib_uverbs_srq_idr, uobj);
  1748. mutex_lock(&file->mutex);
  1749. list_del(&uobj->list);
  1750. mutex_unlock(&file->mutex);
  1751. ib_uverbs_release_uevent(file, obj);
  1752. memset(&resp, 0, sizeof resp);
  1753. resp.events_reported = obj->events_reported;
  1754. put_uobj(uobj);
  1755. if (copy_to_user((void __user *) (unsigned long) cmd.response,
  1756. &resp, sizeof resp))
  1757. ret = -EFAULT;
  1758. return ret ? ret : in_len;
  1759. }