uverbs_main.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324
  1. /*
  2. * Copyright (c) 2005 Topspin Communications. All rights reserved.
  3. * Copyright (c) 2005, 2006 Cisco Systems. All rights reserved.
  4. * Copyright (c) 2005 Mellanox Technologies. All rights reserved.
  5. * Copyright (c) 2005 Voltaire, Inc. All rights reserved.
  6. * Copyright (c) 2005 PathScale, Inc. All rights reserved.
  7. *
  8. * This software is available to you under a choice of one of two
  9. * licenses. You may choose to be licensed under the terms of the GNU
  10. * General Public License (GPL) Version 2, available from the file
  11. * COPYING in the main directory of this source tree, or the
  12. * OpenIB.org BSD license below:
  13. *
  14. * Redistribution and use in source and binary forms, with or
  15. * without modification, are permitted provided that the following
  16. * conditions are met:
  17. *
  18. * - Redistributions of source code must retain the above
  19. * copyright notice, this list of conditions and the following
  20. * disclaimer.
  21. *
  22. * - Redistributions in binary form must reproduce the above
  23. * copyright notice, this list of conditions and the following
  24. * disclaimer in the documentation and/or other materials
  25. * provided with the distribution.
  26. *
  27. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  28. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  29. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  30. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  31. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  32. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  33. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  34. * SOFTWARE.
  35. */
  36. #include <linux/module.h>
  37. #include <linux/init.h>
  38. #include <linux/device.h>
  39. #include <linux/err.h>
  40. #include <linux/fs.h>
  41. #include <linux/poll.h>
  42. #include <linux/sched.h>
  43. #include <linux/file.h>
  44. #include <linux/cdev.h>
  45. #include <linux/anon_inodes.h>
  46. #include <linux/slab.h>
  47. #include <asm/uaccess.h>
  48. #include "uverbs.h"
  49. MODULE_AUTHOR("Roland Dreier");
  50. MODULE_DESCRIPTION("InfiniBand userspace verbs access");
  51. MODULE_LICENSE("Dual BSD/GPL");
  52. enum {
  53. IB_UVERBS_MAJOR = 231,
  54. IB_UVERBS_BASE_MINOR = 192,
  55. IB_UVERBS_MAX_DEVICES = 32
  56. };
  57. #define IB_UVERBS_BASE_DEV MKDEV(IB_UVERBS_MAJOR, IB_UVERBS_BASE_MINOR)
  58. static struct class *uverbs_class;
  59. DEFINE_SPINLOCK(ib_uverbs_idr_lock);
  60. DEFINE_IDR(ib_uverbs_pd_idr);
  61. DEFINE_IDR(ib_uverbs_mr_idr);
  62. DEFINE_IDR(ib_uverbs_mw_idr);
  63. DEFINE_IDR(ib_uverbs_ah_idr);
  64. DEFINE_IDR(ib_uverbs_cq_idr);
  65. DEFINE_IDR(ib_uverbs_qp_idr);
  66. DEFINE_IDR(ib_uverbs_srq_idr);
  67. DEFINE_IDR(ib_uverbs_xrcd_idr);
  68. DEFINE_IDR(ib_uverbs_rule_idr);
  69. static DEFINE_SPINLOCK(map_lock);
  70. static DECLARE_BITMAP(dev_map, IB_UVERBS_MAX_DEVICES);
  71. static ssize_t (*uverbs_cmd_table[])(struct ib_uverbs_file *file,
  72. struct ib_device *ib_dev,
  73. const char __user *buf, int in_len,
  74. int out_len) = {
  75. [IB_USER_VERBS_CMD_GET_CONTEXT] = ib_uverbs_get_context,
  76. [IB_USER_VERBS_CMD_QUERY_DEVICE] = ib_uverbs_query_device,
  77. [IB_USER_VERBS_CMD_QUERY_PORT] = ib_uverbs_query_port,
  78. [IB_USER_VERBS_CMD_ALLOC_PD] = ib_uverbs_alloc_pd,
  79. [IB_USER_VERBS_CMD_DEALLOC_PD] = ib_uverbs_dealloc_pd,
  80. [IB_USER_VERBS_CMD_REG_MR] = ib_uverbs_reg_mr,
  81. [IB_USER_VERBS_CMD_REREG_MR] = ib_uverbs_rereg_mr,
  82. [IB_USER_VERBS_CMD_DEREG_MR] = ib_uverbs_dereg_mr,
  83. [IB_USER_VERBS_CMD_ALLOC_MW] = ib_uverbs_alloc_mw,
  84. [IB_USER_VERBS_CMD_DEALLOC_MW] = ib_uverbs_dealloc_mw,
  85. [IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL] = ib_uverbs_create_comp_channel,
  86. [IB_USER_VERBS_CMD_CREATE_CQ] = ib_uverbs_create_cq,
  87. [IB_USER_VERBS_CMD_RESIZE_CQ] = ib_uverbs_resize_cq,
  88. [IB_USER_VERBS_CMD_POLL_CQ] = ib_uverbs_poll_cq,
  89. [IB_USER_VERBS_CMD_REQ_NOTIFY_CQ] = ib_uverbs_req_notify_cq,
  90. [IB_USER_VERBS_CMD_DESTROY_CQ] = ib_uverbs_destroy_cq,
  91. [IB_USER_VERBS_CMD_CREATE_QP] = ib_uverbs_create_qp,
  92. [IB_USER_VERBS_CMD_QUERY_QP] = ib_uverbs_query_qp,
  93. [IB_USER_VERBS_CMD_MODIFY_QP] = ib_uverbs_modify_qp,
  94. [IB_USER_VERBS_CMD_DESTROY_QP] = ib_uverbs_destroy_qp,
  95. [IB_USER_VERBS_CMD_POST_SEND] = ib_uverbs_post_send,
  96. [IB_USER_VERBS_CMD_POST_RECV] = ib_uverbs_post_recv,
  97. [IB_USER_VERBS_CMD_POST_SRQ_RECV] = ib_uverbs_post_srq_recv,
  98. [IB_USER_VERBS_CMD_CREATE_AH] = ib_uverbs_create_ah,
  99. [IB_USER_VERBS_CMD_DESTROY_AH] = ib_uverbs_destroy_ah,
  100. [IB_USER_VERBS_CMD_ATTACH_MCAST] = ib_uverbs_attach_mcast,
  101. [IB_USER_VERBS_CMD_DETACH_MCAST] = ib_uverbs_detach_mcast,
  102. [IB_USER_VERBS_CMD_CREATE_SRQ] = ib_uverbs_create_srq,
  103. [IB_USER_VERBS_CMD_MODIFY_SRQ] = ib_uverbs_modify_srq,
  104. [IB_USER_VERBS_CMD_QUERY_SRQ] = ib_uverbs_query_srq,
  105. [IB_USER_VERBS_CMD_DESTROY_SRQ] = ib_uverbs_destroy_srq,
  106. [IB_USER_VERBS_CMD_OPEN_XRCD] = ib_uverbs_open_xrcd,
  107. [IB_USER_VERBS_CMD_CLOSE_XRCD] = ib_uverbs_close_xrcd,
  108. [IB_USER_VERBS_CMD_CREATE_XSRQ] = ib_uverbs_create_xsrq,
  109. [IB_USER_VERBS_CMD_OPEN_QP] = ib_uverbs_open_qp,
  110. };
  111. static int (*uverbs_ex_cmd_table[])(struct ib_uverbs_file *file,
  112. struct ib_device *ib_dev,
  113. struct ib_udata *ucore,
  114. struct ib_udata *uhw) = {
  115. [IB_USER_VERBS_EX_CMD_CREATE_FLOW] = ib_uverbs_ex_create_flow,
  116. [IB_USER_VERBS_EX_CMD_DESTROY_FLOW] = ib_uverbs_ex_destroy_flow,
  117. [IB_USER_VERBS_EX_CMD_QUERY_DEVICE] = ib_uverbs_ex_query_device,
  118. [IB_USER_VERBS_EX_CMD_CREATE_CQ] = ib_uverbs_ex_create_cq,
  119. };
  120. static void ib_uverbs_add_one(struct ib_device *device);
  121. static void ib_uverbs_remove_one(struct ib_device *device, void *client_data);
  122. static void ib_uverbs_release_dev(struct kobject *kobj)
  123. {
  124. struct ib_uverbs_device *dev =
  125. container_of(kobj, struct ib_uverbs_device, kobj);
  126. cleanup_srcu_struct(&dev->disassociate_srcu);
  127. kfree(dev);
  128. }
  129. static struct kobj_type ib_uverbs_dev_ktype = {
  130. .release = ib_uverbs_release_dev,
  131. };
  132. static void ib_uverbs_release_event_file(struct kref *ref)
  133. {
  134. struct ib_uverbs_event_file *file =
  135. container_of(ref, struct ib_uverbs_event_file, ref);
  136. kfree(file);
  137. }
  138. void ib_uverbs_release_ucq(struct ib_uverbs_file *file,
  139. struct ib_uverbs_event_file *ev_file,
  140. struct ib_ucq_object *uobj)
  141. {
  142. struct ib_uverbs_event *evt, *tmp;
  143. if (ev_file) {
  144. spin_lock_irq(&ev_file->lock);
  145. list_for_each_entry_safe(evt, tmp, &uobj->comp_list, obj_list) {
  146. list_del(&evt->list);
  147. kfree(evt);
  148. }
  149. spin_unlock_irq(&ev_file->lock);
  150. kref_put(&ev_file->ref, ib_uverbs_release_event_file);
  151. }
  152. spin_lock_irq(&file->async_file->lock);
  153. list_for_each_entry_safe(evt, tmp, &uobj->async_list, obj_list) {
  154. list_del(&evt->list);
  155. kfree(evt);
  156. }
  157. spin_unlock_irq(&file->async_file->lock);
  158. }
  159. void ib_uverbs_release_uevent(struct ib_uverbs_file *file,
  160. struct ib_uevent_object *uobj)
  161. {
  162. struct ib_uverbs_event *evt, *tmp;
  163. spin_lock_irq(&file->async_file->lock);
  164. list_for_each_entry_safe(evt, tmp, &uobj->event_list, obj_list) {
  165. list_del(&evt->list);
  166. kfree(evt);
  167. }
  168. spin_unlock_irq(&file->async_file->lock);
  169. }
  170. static void ib_uverbs_detach_umcast(struct ib_qp *qp,
  171. struct ib_uqp_object *uobj)
  172. {
  173. struct ib_uverbs_mcast_entry *mcast, *tmp;
  174. list_for_each_entry_safe(mcast, tmp, &uobj->mcast_list, list) {
  175. ib_detach_mcast(qp, &mcast->gid, mcast->lid);
  176. list_del(&mcast->list);
  177. kfree(mcast);
  178. }
  179. }
  180. static int ib_uverbs_cleanup_ucontext(struct ib_uverbs_file *file,
  181. struct ib_ucontext *context)
  182. {
  183. struct ib_uobject *uobj, *tmp;
  184. context->closing = 1;
  185. list_for_each_entry_safe(uobj, tmp, &context->ah_list, list) {
  186. struct ib_ah *ah = uobj->object;
  187. idr_remove_uobj(&ib_uverbs_ah_idr, uobj);
  188. ib_destroy_ah(ah);
  189. kfree(uobj);
  190. }
  191. /* Remove MWs before QPs, in order to support type 2A MWs. */
  192. list_for_each_entry_safe(uobj, tmp, &context->mw_list, list) {
  193. struct ib_mw *mw = uobj->object;
  194. idr_remove_uobj(&ib_uverbs_mw_idr, uobj);
  195. ib_dealloc_mw(mw);
  196. kfree(uobj);
  197. }
  198. list_for_each_entry_safe(uobj, tmp, &context->rule_list, list) {
  199. struct ib_flow *flow_id = uobj->object;
  200. idr_remove_uobj(&ib_uverbs_rule_idr, uobj);
  201. ib_destroy_flow(flow_id);
  202. kfree(uobj);
  203. }
  204. list_for_each_entry_safe(uobj, tmp, &context->qp_list, list) {
  205. struct ib_qp *qp = uobj->object;
  206. struct ib_uqp_object *uqp =
  207. container_of(uobj, struct ib_uqp_object, uevent.uobject);
  208. idr_remove_uobj(&ib_uverbs_qp_idr, uobj);
  209. if (qp != qp->real_qp) {
  210. ib_close_qp(qp);
  211. } else {
  212. ib_uverbs_detach_umcast(qp, uqp);
  213. ib_destroy_qp(qp);
  214. }
  215. ib_uverbs_release_uevent(file, &uqp->uevent);
  216. kfree(uqp);
  217. }
  218. list_for_each_entry_safe(uobj, tmp, &context->srq_list, list) {
  219. struct ib_srq *srq = uobj->object;
  220. struct ib_uevent_object *uevent =
  221. container_of(uobj, struct ib_uevent_object, uobject);
  222. idr_remove_uobj(&ib_uverbs_srq_idr, uobj);
  223. ib_destroy_srq(srq);
  224. ib_uverbs_release_uevent(file, uevent);
  225. kfree(uevent);
  226. }
  227. list_for_each_entry_safe(uobj, tmp, &context->cq_list, list) {
  228. struct ib_cq *cq = uobj->object;
  229. struct ib_uverbs_event_file *ev_file = cq->cq_context;
  230. struct ib_ucq_object *ucq =
  231. container_of(uobj, struct ib_ucq_object, uobject);
  232. idr_remove_uobj(&ib_uverbs_cq_idr, uobj);
  233. ib_destroy_cq(cq);
  234. ib_uverbs_release_ucq(file, ev_file, ucq);
  235. kfree(ucq);
  236. }
  237. list_for_each_entry_safe(uobj, tmp, &context->mr_list, list) {
  238. struct ib_mr *mr = uobj->object;
  239. idr_remove_uobj(&ib_uverbs_mr_idr, uobj);
  240. ib_dereg_mr(mr);
  241. kfree(uobj);
  242. }
  243. mutex_lock(&file->device->xrcd_tree_mutex);
  244. list_for_each_entry_safe(uobj, tmp, &context->xrcd_list, list) {
  245. struct ib_xrcd *xrcd = uobj->object;
  246. struct ib_uxrcd_object *uxrcd =
  247. container_of(uobj, struct ib_uxrcd_object, uobject);
  248. idr_remove_uobj(&ib_uverbs_xrcd_idr, uobj);
  249. ib_uverbs_dealloc_xrcd(file->device, xrcd);
  250. kfree(uxrcd);
  251. }
  252. mutex_unlock(&file->device->xrcd_tree_mutex);
  253. list_for_each_entry_safe(uobj, tmp, &context->pd_list, list) {
  254. struct ib_pd *pd = uobj->object;
  255. idr_remove_uobj(&ib_uverbs_pd_idr, uobj);
  256. ib_dealloc_pd(pd);
  257. kfree(uobj);
  258. }
  259. put_pid(context->tgid);
  260. return context->device->dealloc_ucontext(context);
  261. }
  262. static void ib_uverbs_comp_dev(struct ib_uverbs_device *dev)
  263. {
  264. complete(&dev->comp);
  265. }
  266. static void ib_uverbs_release_file(struct kref *ref)
  267. {
  268. struct ib_uverbs_file *file =
  269. container_of(ref, struct ib_uverbs_file, ref);
  270. struct ib_device *ib_dev;
  271. int srcu_key;
  272. srcu_key = srcu_read_lock(&file->device->disassociate_srcu);
  273. ib_dev = srcu_dereference(file->device->ib_dev,
  274. &file->device->disassociate_srcu);
  275. if (ib_dev && !ib_dev->disassociate_ucontext)
  276. module_put(ib_dev->owner);
  277. srcu_read_unlock(&file->device->disassociate_srcu, srcu_key);
  278. if (atomic_dec_and_test(&file->device->refcount))
  279. ib_uverbs_comp_dev(file->device);
  280. kfree(file);
  281. }
  282. static ssize_t ib_uverbs_event_read(struct file *filp, char __user *buf,
  283. size_t count, loff_t *pos)
  284. {
  285. struct ib_uverbs_event_file *file = filp->private_data;
  286. struct ib_uverbs_event *event;
  287. int eventsz;
  288. int ret = 0;
  289. spin_lock_irq(&file->lock);
  290. while (list_empty(&file->event_list)) {
  291. spin_unlock_irq(&file->lock);
  292. if (filp->f_flags & O_NONBLOCK)
  293. return -EAGAIN;
  294. if (wait_event_interruptible(file->poll_wait,
  295. (!list_empty(&file->event_list) ||
  296. /* The barriers built into wait_event_interruptible()
  297. * and wake_up() guarentee this will see the null set
  298. * without using RCU
  299. */
  300. !file->uverbs_file->device->ib_dev)))
  301. return -ERESTARTSYS;
  302. /* If device was disassociated and no event exists set an error */
  303. if (list_empty(&file->event_list) &&
  304. !file->uverbs_file->device->ib_dev)
  305. return -EIO;
  306. spin_lock_irq(&file->lock);
  307. }
  308. event = list_entry(file->event_list.next, struct ib_uverbs_event, list);
  309. if (file->is_async)
  310. eventsz = sizeof (struct ib_uverbs_async_event_desc);
  311. else
  312. eventsz = sizeof (struct ib_uverbs_comp_event_desc);
  313. if (eventsz > count) {
  314. ret = -EINVAL;
  315. event = NULL;
  316. } else {
  317. list_del(file->event_list.next);
  318. if (event->counter) {
  319. ++(*event->counter);
  320. list_del(&event->obj_list);
  321. }
  322. }
  323. spin_unlock_irq(&file->lock);
  324. if (event) {
  325. if (copy_to_user(buf, event, eventsz))
  326. ret = -EFAULT;
  327. else
  328. ret = eventsz;
  329. }
  330. kfree(event);
  331. return ret;
  332. }
  333. static unsigned int ib_uverbs_event_poll(struct file *filp,
  334. struct poll_table_struct *wait)
  335. {
  336. unsigned int pollflags = 0;
  337. struct ib_uverbs_event_file *file = filp->private_data;
  338. poll_wait(filp, &file->poll_wait, wait);
  339. spin_lock_irq(&file->lock);
  340. if (!list_empty(&file->event_list))
  341. pollflags = POLLIN | POLLRDNORM;
  342. spin_unlock_irq(&file->lock);
  343. return pollflags;
  344. }
  345. static int ib_uverbs_event_fasync(int fd, struct file *filp, int on)
  346. {
  347. struct ib_uverbs_event_file *file = filp->private_data;
  348. return fasync_helper(fd, filp, on, &file->async_queue);
  349. }
  350. static int ib_uverbs_event_close(struct inode *inode, struct file *filp)
  351. {
  352. struct ib_uverbs_event_file *file = filp->private_data;
  353. struct ib_uverbs_event *entry, *tmp;
  354. int closed_already = 0;
  355. mutex_lock(&file->uverbs_file->device->lists_mutex);
  356. spin_lock_irq(&file->lock);
  357. closed_already = file->is_closed;
  358. file->is_closed = 1;
  359. list_for_each_entry_safe(entry, tmp, &file->event_list, list) {
  360. if (entry->counter)
  361. list_del(&entry->obj_list);
  362. kfree(entry);
  363. }
  364. spin_unlock_irq(&file->lock);
  365. if (!closed_already) {
  366. list_del(&file->list);
  367. if (file->is_async)
  368. ib_unregister_event_handler(&file->uverbs_file->
  369. event_handler);
  370. }
  371. mutex_unlock(&file->uverbs_file->device->lists_mutex);
  372. kref_put(&file->uverbs_file->ref, ib_uverbs_release_file);
  373. kref_put(&file->ref, ib_uverbs_release_event_file);
  374. return 0;
  375. }
  376. static const struct file_operations uverbs_event_fops = {
  377. .owner = THIS_MODULE,
  378. .read = ib_uverbs_event_read,
  379. .poll = ib_uverbs_event_poll,
  380. .release = ib_uverbs_event_close,
  381. .fasync = ib_uverbs_event_fasync,
  382. .llseek = no_llseek,
  383. };
  384. void ib_uverbs_comp_handler(struct ib_cq *cq, void *cq_context)
  385. {
  386. struct ib_uverbs_event_file *file = cq_context;
  387. struct ib_ucq_object *uobj;
  388. struct ib_uverbs_event *entry;
  389. unsigned long flags;
  390. if (!file)
  391. return;
  392. spin_lock_irqsave(&file->lock, flags);
  393. if (file->is_closed) {
  394. spin_unlock_irqrestore(&file->lock, flags);
  395. return;
  396. }
  397. entry = kmalloc(sizeof *entry, GFP_ATOMIC);
  398. if (!entry) {
  399. spin_unlock_irqrestore(&file->lock, flags);
  400. return;
  401. }
  402. uobj = container_of(cq->uobject, struct ib_ucq_object, uobject);
  403. entry->desc.comp.cq_handle = cq->uobject->user_handle;
  404. entry->counter = &uobj->comp_events_reported;
  405. list_add_tail(&entry->list, &file->event_list);
  406. list_add_tail(&entry->obj_list, &uobj->comp_list);
  407. spin_unlock_irqrestore(&file->lock, flags);
  408. wake_up_interruptible(&file->poll_wait);
  409. kill_fasync(&file->async_queue, SIGIO, POLL_IN);
  410. }
  411. static void ib_uverbs_async_handler(struct ib_uverbs_file *file,
  412. __u64 element, __u64 event,
  413. struct list_head *obj_list,
  414. u32 *counter)
  415. {
  416. struct ib_uverbs_event *entry;
  417. unsigned long flags;
  418. spin_lock_irqsave(&file->async_file->lock, flags);
  419. if (file->async_file->is_closed) {
  420. spin_unlock_irqrestore(&file->async_file->lock, flags);
  421. return;
  422. }
  423. entry = kmalloc(sizeof *entry, GFP_ATOMIC);
  424. if (!entry) {
  425. spin_unlock_irqrestore(&file->async_file->lock, flags);
  426. return;
  427. }
  428. entry->desc.async.element = element;
  429. entry->desc.async.event_type = event;
  430. entry->desc.async.reserved = 0;
  431. entry->counter = counter;
  432. list_add_tail(&entry->list, &file->async_file->event_list);
  433. if (obj_list)
  434. list_add_tail(&entry->obj_list, obj_list);
  435. spin_unlock_irqrestore(&file->async_file->lock, flags);
  436. wake_up_interruptible(&file->async_file->poll_wait);
  437. kill_fasync(&file->async_file->async_queue, SIGIO, POLL_IN);
  438. }
  439. void ib_uverbs_cq_event_handler(struct ib_event *event, void *context_ptr)
  440. {
  441. struct ib_ucq_object *uobj = container_of(event->element.cq->uobject,
  442. struct ib_ucq_object, uobject);
  443. ib_uverbs_async_handler(uobj->uverbs_file, uobj->uobject.user_handle,
  444. event->event, &uobj->async_list,
  445. &uobj->async_events_reported);
  446. }
  447. void ib_uverbs_qp_event_handler(struct ib_event *event, void *context_ptr)
  448. {
  449. struct ib_uevent_object *uobj;
  450. /* for XRC target qp's, check that qp is live */
  451. if (!event->element.qp->uobject || !event->element.qp->uobject->live)
  452. return;
  453. uobj = container_of(event->element.qp->uobject,
  454. struct ib_uevent_object, uobject);
  455. ib_uverbs_async_handler(context_ptr, uobj->uobject.user_handle,
  456. event->event, &uobj->event_list,
  457. &uobj->events_reported);
  458. }
  459. void ib_uverbs_srq_event_handler(struct ib_event *event, void *context_ptr)
  460. {
  461. struct ib_uevent_object *uobj;
  462. uobj = container_of(event->element.srq->uobject,
  463. struct ib_uevent_object, uobject);
  464. ib_uverbs_async_handler(context_ptr, uobj->uobject.user_handle,
  465. event->event, &uobj->event_list,
  466. &uobj->events_reported);
  467. }
  468. void ib_uverbs_event_handler(struct ib_event_handler *handler,
  469. struct ib_event *event)
  470. {
  471. struct ib_uverbs_file *file =
  472. container_of(handler, struct ib_uverbs_file, event_handler);
  473. ib_uverbs_async_handler(file, event->element.port_num, event->event,
  474. NULL, NULL);
  475. }
  476. void ib_uverbs_free_async_event_file(struct ib_uverbs_file *file)
  477. {
  478. kref_put(&file->async_file->ref, ib_uverbs_release_event_file);
  479. file->async_file = NULL;
  480. }
  481. struct file *ib_uverbs_alloc_event_file(struct ib_uverbs_file *uverbs_file,
  482. struct ib_device *ib_dev,
  483. int is_async)
  484. {
  485. struct ib_uverbs_event_file *ev_file;
  486. struct file *filp;
  487. int ret;
  488. ev_file = kzalloc(sizeof(*ev_file), GFP_KERNEL);
  489. if (!ev_file)
  490. return ERR_PTR(-ENOMEM);
  491. kref_init(&ev_file->ref);
  492. spin_lock_init(&ev_file->lock);
  493. INIT_LIST_HEAD(&ev_file->event_list);
  494. init_waitqueue_head(&ev_file->poll_wait);
  495. ev_file->uverbs_file = uverbs_file;
  496. kref_get(&ev_file->uverbs_file->ref);
  497. ev_file->async_queue = NULL;
  498. ev_file->is_closed = 0;
  499. filp = anon_inode_getfile("[infinibandevent]", &uverbs_event_fops,
  500. ev_file, O_RDONLY);
  501. if (IS_ERR(filp))
  502. goto err_put_refs;
  503. mutex_lock(&uverbs_file->device->lists_mutex);
  504. list_add_tail(&ev_file->list,
  505. &uverbs_file->device->uverbs_events_file_list);
  506. mutex_unlock(&uverbs_file->device->lists_mutex);
  507. if (is_async) {
  508. WARN_ON(uverbs_file->async_file);
  509. uverbs_file->async_file = ev_file;
  510. kref_get(&uverbs_file->async_file->ref);
  511. INIT_IB_EVENT_HANDLER(&uverbs_file->event_handler,
  512. ib_dev,
  513. ib_uverbs_event_handler);
  514. ret = ib_register_event_handler(&uverbs_file->event_handler);
  515. if (ret)
  516. goto err_put_file;
  517. /* At that point async file stuff was fully set */
  518. ev_file->is_async = 1;
  519. }
  520. return filp;
  521. err_put_file:
  522. fput(filp);
  523. kref_put(&uverbs_file->async_file->ref, ib_uverbs_release_event_file);
  524. uverbs_file->async_file = NULL;
  525. return ERR_PTR(ret);
  526. err_put_refs:
  527. kref_put(&ev_file->uverbs_file->ref, ib_uverbs_release_file);
  528. kref_put(&ev_file->ref, ib_uverbs_release_event_file);
  529. return filp;
  530. }
  531. /*
  532. * Look up a completion event file by FD. If lookup is successful,
  533. * takes a ref to the event file struct that it returns; if
  534. * unsuccessful, returns NULL.
  535. */
  536. struct ib_uverbs_event_file *ib_uverbs_lookup_comp_file(int fd)
  537. {
  538. struct ib_uverbs_event_file *ev_file = NULL;
  539. struct fd f = fdget(fd);
  540. if (!f.file)
  541. return NULL;
  542. if (f.file->f_op != &uverbs_event_fops)
  543. goto out;
  544. ev_file = f.file->private_data;
  545. if (ev_file->is_async) {
  546. ev_file = NULL;
  547. goto out;
  548. }
  549. kref_get(&ev_file->ref);
  550. out:
  551. fdput(f);
  552. return ev_file;
  553. }
  554. static ssize_t ib_uverbs_write(struct file *filp, const char __user *buf,
  555. size_t count, loff_t *pos)
  556. {
  557. struct ib_uverbs_file *file = filp->private_data;
  558. struct ib_device *ib_dev;
  559. struct ib_uverbs_cmd_hdr hdr;
  560. __u32 flags;
  561. int srcu_key;
  562. ssize_t ret;
  563. if (count < sizeof hdr)
  564. return -EINVAL;
  565. if (copy_from_user(&hdr, buf, sizeof hdr))
  566. return -EFAULT;
  567. srcu_key = srcu_read_lock(&file->device->disassociate_srcu);
  568. ib_dev = srcu_dereference(file->device->ib_dev,
  569. &file->device->disassociate_srcu);
  570. if (!ib_dev) {
  571. ret = -EIO;
  572. goto out;
  573. }
  574. flags = (hdr.command &
  575. IB_USER_VERBS_CMD_FLAGS_MASK) >> IB_USER_VERBS_CMD_FLAGS_SHIFT;
  576. if (!flags) {
  577. __u32 command;
  578. if (hdr.command & ~(__u32)(IB_USER_VERBS_CMD_FLAGS_MASK |
  579. IB_USER_VERBS_CMD_COMMAND_MASK)) {
  580. ret = -EINVAL;
  581. goto out;
  582. }
  583. command = hdr.command & IB_USER_VERBS_CMD_COMMAND_MASK;
  584. if (command >= ARRAY_SIZE(uverbs_cmd_table) ||
  585. !uverbs_cmd_table[command]) {
  586. ret = -EINVAL;
  587. goto out;
  588. }
  589. if (!file->ucontext &&
  590. command != IB_USER_VERBS_CMD_GET_CONTEXT) {
  591. ret = -EINVAL;
  592. goto out;
  593. }
  594. if (!(ib_dev->uverbs_cmd_mask & (1ull << command))) {
  595. ret = -ENOSYS;
  596. goto out;
  597. }
  598. if (hdr.in_words * 4 != count) {
  599. ret = -EINVAL;
  600. goto out;
  601. }
  602. ret = uverbs_cmd_table[command](file, ib_dev,
  603. buf + sizeof(hdr),
  604. hdr.in_words * 4,
  605. hdr.out_words * 4);
  606. } else if (flags == IB_USER_VERBS_CMD_FLAG_EXTENDED) {
  607. __u32 command;
  608. struct ib_uverbs_ex_cmd_hdr ex_hdr;
  609. struct ib_udata ucore;
  610. struct ib_udata uhw;
  611. size_t written_count = count;
  612. if (hdr.command & ~(__u32)(IB_USER_VERBS_CMD_FLAGS_MASK |
  613. IB_USER_VERBS_CMD_COMMAND_MASK)) {
  614. ret = -EINVAL;
  615. goto out;
  616. }
  617. command = hdr.command & IB_USER_VERBS_CMD_COMMAND_MASK;
  618. if (command >= ARRAY_SIZE(uverbs_ex_cmd_table) ||
  619. !uverbs_ex_cmd_table[command]) {
  620. ret = -ENOSYS;
  621. goto out;
  622. }
  623. if (!file->ucontext) {
  624. ret = -EINVAL;
  625. goto out;
  626. }
  627. if (!(ib_dev->uverbs_ex_cmd_mask & (1ull << command))) {
  628. ret = -ENOSYS;
  629. goto out;
  630. }
  631. if (count < (sizeof(hdr) + sizeof(ex_hdr))) {
  632. ret = -EINVAL;
  633. goto out;
  634. }
  635. if (copy_from_user(&ex_hdr, buf + sizeof(hdr), sizeof(ex_hdr))) {
  636. ret = -EFAULT;
  637. goto out;
  638. }
  639. count -= sizeof(hdr) + sizeof(ex_hdr);
  640. buf += sizeof(hdr) + sizeof(ex_hdr);
  641. if ((hdr.in_words + ex_hdr.provider_in_words) * 8 != count) {
  642. ret = -EINVAL;
  643. goto out;
  644. }
  645. if (ex_hdr.cmd_hdr_reserved) {
  646. ret = -EINVAL;
  647. goto out;
  648. }
  649. if (ex_hdr.response) {
  650. if (!hdr.out_words && !ex_hdr.provider_out_words) {
  651. ret = -EINVAL;
  652. goto out;
  653. }
  654. if (!access_ok(VERIFY_WRITE,
  655. (void __user *) (unsigned long) ex_hdr.response,
  656. (hdr.out_words + ex_hdr.provider_out_words) * 8)) {
  657. ret = -EFAULT;
  658. goto out;
  659. }
  660. } else {
  661. if (hdr.out_words || ex_hdr.provider_out_words) {
  662. ret = -EINVAL;
  663. goto out;
  664. }
  665. }
  666. INIT_UDATA_BUF_OR_NULL(&ucore, buf, (unsigned long) ex_hdr.response,
  667. hdr.in_words * 8, hdr.out_words * 8);
  668. INIT_UDATA_BUF_OR_NULL(&uhw,
  669. buf + ucore.inlen,
  670. (unsigned long) ex_hdr.response + ucore.outlen,
  671. ex_hdr.provider_in_words * 8,
  672. ex_hdr.provider_out_words * 8);
  673. ret = uverbs_ex_cmd_table[command](file,
  674. ib_dev,
  675. &ucore,
  676. &uhw);
  677. if (!ret)
  678. ret = written_count;
  679. } else {
  680. ret = -ENOSYS;
  681. }
  682. out:
  683. srcu_read_unlock(&file->device->disassociate_srcu, srcu_key);
  684. return ret;
  685. }
  686. static int ib_uverbs_mmap(struct file *filp, struct vm_area_struct *vma)
  687. {
  688. struct ib_uverbs_file *file = filp->private_data;
  689. struct ib_device *ib_dev;
  690. int ret = 0;
  691. int srcu_key;
  692. srcu_key = srcu_read_lock(&file->device->disassociate_srcu);
  693. ib_dev = srcu_dereference(file->device->ib_dev,
  694. &file->device->disassociate_srcu);
  695. if (!ib_dev) {
  696. ret = -EIO;
  697. goto out;
  698. }
  699. if (!file->ucontext)
  700. ret = -ENODEV;
  701. else
  702. ret = ib_dev->mmap(file->ucontext, vma);
  703. out:
  704. srcu_read_unlock(&file->device->disassociate_srcu, srcu_key);
  705. return ret;
  706. }
  707. /*
  708. * ib_uverbs_open() does not need the BKL:
  709. *
  710. * - the ib_uverbs_device structures are properly reference counted and
  711. * everything else is purely local to the file being created, so
  712. * races against other open calls are not a problem;
  713. * - there is no ioctl method to race against;
  714. * - the open method will either immediately run -ENXIO, or all
  715. * required initialization will be done.
  716. */
  717. static int ib_uverbs_open(struct inode *inode, struct file *filp)
  718. {
  719. struct ib_uverbs_device *dev;
  720. struct ib_uverbs_file *file;
  721. struct ib_device *ib_dev;
  722. int ret;
  723. int module_dependent;
  724. int srcu_key;
  725. dev = container_of(inode->i_cdev, struct ib_uverbs_device, cdev);
  726. if (!atomic_inc_not_zero(&dev->refcount))
  727. return -ENXIO;
  728. srcu_key = srcu_read_lock(&dev->disassociate_srcu);
  729. mutex_lock(&dev->lists_mutex);
  730. ib_dev = srcu_dereference(dev->ib_dev,
  731. &dev->disassociate_srcu);
  732. if (!ib_dev) {
  733. ret = -EIO;
  734. goto err;
  735. }
  736. /* In case IB device supports disassociate ucontext, there is no hard
  737. * dependency between uverbs device and its low level device.
  738. */
  739. module_dependent = !(ib_dev->disassociate_ucontext);
  740. if (module_dependent) {
  741. if (!try_module_get(ib_dev->owner)) {
  742. ret = -ENODEV;
  743. goto err;
  744. }
  745. }
  746. file = kzalloc(sizeof(*file), GFP_KERNEL);
  747. if (!file) {
  748. ret = -ENOMEM;
  749. if (module_dependent)
  750. goto err_module;
  751. goto err;
  752. }
  753. file->device = dev;
  754. file->ucontext = NULL;
  755. file->async_file = NULL;
  756. kref_init(&file->ref);
  757. mutex_init(&file->mutex);
  758. filp->private_data = file;
  759. kobject_get(&dev->kobj);
  760. list_add_tail(&file->list, &dev->uverbs_file_list);
  761. mutex_unlock(&dev->lists_mutex);
  762. srcu_read_unlock(&dev->disassociate_srcu, srcu_key);
  763. return nonseekable_open(inode, filp);
  764. err_module:
  765. module_put(ib_dev->owner);
  766. err:
  767. mutex_unlock(&dev->lists_mutex);
  768. srcu_read_unlock(&dev->disassociate_srcu, srcu_key);
  769. if (atomic_dec_and_test(&dev->refcount))
  770. ib_uverbs_comp_dev(dev);
  771. return ret;
  772. }
  773. static int ib_uverbs_close(struct inode *inode, struct file *filp)
  774. {
  775. struct ib_uverbs_file *file = filp->private_data;
  776. struct ib_uverbs_device *dev = file->device;
  777. struct ib_ucontext *ucontext = NULL;
  778. mutex_lock(&file->device->lists_mutex);
  779. ucontext = file->ucontext;
  780. file->ucontext = NULL;
  781. if (!file->is_closed) {
  782. list_del(&file->list);
  783. file->is_closed = 1;
  784. }
  785. mutex_unlock(&file->device->lists_mutex);
  786. if (ucontext)
  787. ib_uverbs_cleanup_ucontext(file, ucontext);
  788. if (file->async_file)
  789. kref_put(&file->async_file->ref, ib_uverbs_release_event_file);
  790. kref_put(&file->ref, ib_uverbs_release_file);
  791. kobject_put(&dev->kobj);
  792. return 0;
  793. }
  794. static const struct file_operations uverbs_fops = {
  795. .owner = THIS_MODULE,
  796. .write = ib_uverbs_write,
  797. .open = ib_uverbs_open,
  798. .release = ib_uverbs_close,
  799. .llseek = no_llseek,
  800. };
  801. static const struct file_operations uverbs_mmap_fops = {
  802. .owner = THIS_MODULE,
  803. .write = ib_uverbs_write,
  804. .mmap = ib_uverbs_mmap,
  805. .open = ib_uverbs_open,
  806. .release = ib_uverbs_close,
  807. .llseek = no_llseek,
  808. };
  809. static struct ib_client uverbs_client = {
  810. .name = "uverbs",
  811. .add = ib_uverbs_add_one,
  812. .remove = ib_uverbs_remove_one
  813. };
  814. static ssize_t show_ibdev(struct device *device, struct device_attribute *attr,
  815. char *buf)
  816. {
  817. int ret = -ENODEV;
  818. int srcu_key;
  819. struct ib_uverbs_device *dev = dev_get_drvdata(device);
  820. struct ib_device *ib_dev;
  821. if (!dev)
  822. return -ENODEV;
  823. srcu_key = srcu_read_lock(&dev->disassociate_srcu);
  824. ib_dev = srcu_dereference(dev->ib_dev, &dev->disassociate_srcu);
  825. if (ib_dev)
  826. ret = sprintf(buf, "%s\n", ib_dev->name);
  827. srcu_read_unlock(&dev->disassociate_srcu, srcu_key);
  828. return ret;
  829. }
  830. static DEVICE_ATTR(ibdev, S_IRUGO, show_ibdev, NULL);
  831. static ssize_t show_dev_abi_version(struct device *device,
  832. struct device_attribute *attr, char *buf)
  833. {
  834. struct ib_uverbs_device *dev = dev_get_drvdata(device);
  835. int ret = -ENODEV;
  836. int srcu_key;
  837. struct ib_device *ib_dev;
  838. if (!dev)
  839. return -ENODEV;
  840. srcu_key = srcu_read_lock(&dev->disassociate_srcu);
  841. ib_dev = srcu_dereference(dev->ib_dev, &dev->disassociate_srcu);
  842. if (ib_dev)
  843. ret = sprintf(buf, "%d\n", ib_dev->uverbs_abi_ver);
  844. srcu_read_unlock(&dev->disassociate_srcu, srcu_key);
  845. return ret;
  846. }
  847. static DEVICE_ATTR(abi_version, S_IRUGO, show_dev_abi_version, NULL);
  848. static CLASS_ATTR_STRING(abi_version, S_IRUGO,
  849. __stringify(IB_USER_VERBS_ABI_VERSION));
  850. static dev_t overflow_maj;
  851. static DECLARE_BITMAP(overflow_map, IB_UVERBS_MAX_DEVICES);
  852. /*
  853. * If we have more than IB_UVERBS_MAX_DEVICES, dynamically overflow by
  854. * requesting a new major number and doubling the number of max devices we
  855. * support. It's stupid, but simple.
  856. */
  857. static int find_overflow_devnum(void)
  858. {
  859. int ret;
  860. if (!overflow_maj) {
  861. ret = alloc_chrdev_region(&overflow_maj, 0, IB_UVERBS_MAX_DEVICES,
  862. "infiniband_verbs");
  863. if (ret) {
  864. printk(KERN_ERR "user_verbs: couldn't register dynamic device number\n");
  865. return ret;
  866. }
  867. }
  868. ret = find_first_zero_bit(overflow_map, IB_UVERBS_MAX_DEVICES);
  869. if (ret >= IB_UVERBS_MAX_DEVICES)
  870. return -1;
  871. return ret;
  872. }
  873. static void ib_uverbs_add_one(struct ib_device *device)
  874. {
  875. int devnum;
  876. dev_t base;
  877. struct ib_uverbs_device *uverbs_dev;
  878. int ret;
  879. if (!device->alloc_ucontext)
  880. return;
  881. uverbs_dev = kzalloc(sizeof *uverbs_dev, GFP_KERNEL);
  882. if (!uverbs_dev)
  883. return;
  884. ret = init_srcu_struct(&uverbs_dev->disassociate_srcu);
  885. if (ret) {
  886. kfree(uverbs_dev);
  887. return;
  888. }
  889. atomic_set(&uverbs_dev->refcount, 1);
  890. init_completion(&uverbs_dev->comp);
  891. uverbs_dev->xrcd_tree = RB_ROOT;
  892. mutex_init(&uverbs_dev->xrcd_tree_mutex);
  893. kobject_init(&uverbs_dev->kobj, &ib_uverbs_dev_ktype);
  894. mutex_init(&uverbs_dev->lists_mutex);
  895. INIT_LIST_HEAD(&uverbs_dev->uverbs_file_list);
  896. INIT_LIST_HEAD(&uverbs_dev->uverbs_events_file_list);
  897. spin_lock(&map_lock);
  898. devnum = find_first_zero_bit(dev_map, IB_UVERBS_MAX_DEVICES);
  899. if (devnum >= IB_UVERBS_MAX_DEVICES) {
  900. spin_unlock(&map_lock);
  901. devnum = find_overflow_devnum();
  902. if (devnum < 0)
  903. goto err;
  904. spin_lock(&map_lock);
  905. uverbs_dev->devnum = devnum + IB_UVERBS_MAX_DEVICES;
  906. base = devnum + overflow_maj;
  907. set_bit(devnum, overflow_map);
  908. } else {
  909. uverbs_dev->devnum = devnum;
  910. base = devnum + IB_UVERBS_BASE_DEV;
  911. set_bit(devnum, dev_map);
  912. }
  913. spin_unlock(&map_lock);
  914. rcu_assign_pointer(uverbs_dev->ib_dev, device);
  915. uverbs_dev->num_comp_vectors = device->num_comp_vectors;
  916. cdev_init(&uverbs_dev->cdev, NULL);
  917. uverbs_dev->cdev.owner = THIS_MODULE;
  918. uverbs_dev->cdev.ops = device->mmap ? &uverbs_mmap_fops : &uverbs_fops;
  919. uverbs_dev->cdev.kobj.parent = &uverbs_dev->kobj;
  920. kobject_set_name(&uverbs_dev->cdev.kobj, "uverbs%d", uverbs_dev->devnum);
  921. if (cdev_add(&uverbs_dev->cdev, base, 1))
  922. goto err_cdev;
  923. uverbs_dev->dev = device_create(uverbs_class, device->dma_device,
  924. uverbs_dev->cdev.dev, uverbs_dev,
  925. "uverbs%d", uverbs_dev->devnum);
  926. if (IS_ERR(uverbs_dev->dev))
  927. goto err_cdev;
  928. if (device_create_file(uverbs_dev->dev, &dev_attr_ibdev))
  929. goto err_class;
  930. if (device_create_file(uverbs_dev->dev, &dev_attr_abi_version))
  931. goto err_class;
  932. ib_set_client_data(device, &uverbs_client, uverbs_dev);
  933. return;
  934. err_class:
  935. device_destroy(uverbs_class, uverbs_dev->cdev.dev);
  936. err_cdev:
  937. cdev_del(&uverbs_dev->cdev);
  938. if (uverbs_dev->devnum < IB_UVERBS_MAX_DEVICES)
  939. clear_bit(devnum, dev_map);
  940. else
  941. clear_bit(devnum, overflow_map);
  942. err:
  943. if (atomic_dec_and_test(&uverbs_dev->refcount))
  944. ib_uverbs_comp_dev(uverbs_dev);
  945. wait_for_completion(&uverbs_dev->comp);
  946. kobject_put(&uverbs_dev->kobj);
  947. return;
  948. }
  949. static void ib_uverbs_free_hw_resources(struct ib_uverbs_device *uverbs_dev,
  950. struct ib_device *ib_dev)
  951. {
  952. struct ib_uverbs_file *file;
  953. struct ib_uverbs_event_file *event_file;
  954. struct ib_event event;
  955. /* Pending running commands to terminate */
  956. synchronize_srcu(&uverbs_dev->disassociate_srcu);
  957. event.event = IB_EVENT_DEVICE_FATAL;
  958. event.element.port_num = 0;
  959. event.device = ib_dev;
  960. mutex_lock(&uverbs_dev->lists_mutex);
  961. while (!list_empty(&uverbs_dev->uverbs_file_list)) {
  962. struct ib_ucontext *ucontext;
  963. file = list_first_entry(&uverbs_dev->uverbs_file_list,
  964. struct ib_uverbs_file, list);
  965. file->is_closed = 1;
  966. ucontext = file->ucontext;
  967. list_del(&file->list);
  968. file->ucontext = NULL;
  969. kref_get(&file->ref);
  970. mutex_unlock(&uverbs_dev->lists_mutex);
  971. /* We must release the mutex before going ahead and calling
  972. * disassociate_ucontext. disassociate_ucontext might end up
  973. * indirectly calling uverbs_close, for example due to freeing
  974. * the resources (e.g mmput).
  975. */
  976. ib_uverbs_event_handler(&file->event_handler, &event);
  977. if (ucontext) {
  978. ib_dev->disassociate_ucontext(ucontext);
  979. ib_uverbs_cleanup_ucontext(file, ucontext);
  980. }
  981. mutex_lock(&uverbs_dev->lists_mutex);
  982. kref_put(&file->ref, ib_uverbs_release_file);
  983. }
  984. while (!list_empty(&uverbs_dev->uverbs_events_file_list)) {
  985. event_file = list_first_entry(&uverbs_dev->
  986. uverbs_events_file_list,
  987. struct ib_uverbs_event_file,
  988. list);
  989. spin_lock_irq(&event_file->lock);
  990. event_file->is_closed = 1;
  991. spin_unlock_irq(&event_file->lock);
  992. list_del(&event_file->list);
  993. if (event_file->is_async) {
  994. ib_unregister_event_handler(&event_file->uverbs_file->
  995. event_handler);
  996. event_file->uverbs_file->event_handler.device = NULL;
  997. }
  998. wake_up_interruptible(&event_file->poll_wait);
  999. kill_fasync(&event_file->async_queue, SIGIO, POLL_IN);
  1000. }
  1001. mutex_unlock(&uverbs_dev->lists_mutex);
  1002. }
  1003. static void ib_uverbs_remove_one(struct ib_device *device, void *client_data)
  1004. {
  1005. struct ib_uverbs_device *uverbs_dev = client_data;
  1006. int wait_clients = 1;
  1007. if (!uverbs_dev)
  1008. return;
  1009. dev_set_drvdata(uverbs_dev->dev, NULL);
  1010. device_destroy(uverbs_class, uverbs_dev->cdev.dev);
  1011. cdev_del(&uverbs_dev->cdev);
  1012. if (uverbs_dev->devnum < IB_UVERBS_MAX_DEVICES)
  1013. clear_bit(uverbs_dev->devnum, dev_map);
  1014. else
  1015. clear_bit(uverbs_dev->devnum - IB_UVERBS_MAX_DEVICES, overflow_map);
  1016. if (device->disassociate_ucontext) {
  1017. /* We disassociate HW resources and immediately return.
  1018. * Userspace will see a EIO errno for all future access.
  1019. * Upon returning, ib_device may be freed internally and is not
  1020. * valid any more.
  1021. * uverbs_device is still available until all clients close
  1022. * their files, then the uverbs device ref count will be zero
  1023. * and its resources will be freed.
  1024. * Note: At this point no more files can be opened since the
  1025. * cdev was deleted, however active clients can still issue
  1026. * commands and close their open files.
  1027. */
  1028. rcu_assign_pointer(uverbs_dev->ib_dev, NULL);
  1029. ib_uverbs_free_hw_resources(uverbs_dev, device);
  1030. wait_clients = 0;
  1031. }
  1032. if (atomic_dec_and_test(&uverbs_dev->refcount))
  1033. ib_uverbs_comp_dev(uverbs_dev);
  1034. if (wait_clients)
  1035. wait_for_completion(&uverbs_dev->comp);
  1036. kobject_put(&uverbs_dev->kobj);
  1037. }
  1038. static char *uverbs_devnode(struct device *dev, umode_t *mode)
  1039. {
  1040. if (mode)
  1041. *mode = 0666;
  1042. return kasprintf(GFP_KERNEL, "infiniband/%s", dev_name(dev));
  1043. }
  1044. static int __init ib_uverbs_init(void)
  1045. {
  1046. int ret;
  1047. ret = register_chrdev_region(IB_UVERBS_BASE_DEV, IB_UVERBS_MAX_DEVICES,
  1048. "infiniband_verbs");
  1049. if (ret) {
  1050. printk(KERN_ERR "user_verbs: couldn't register device number\n");
  1051. goto out;
  1052. }
  1053. uverbs_class = class_create(THIS_MODULE, "infiniband_verbs");
  1054. if (IS_ERR(uverbs_class)) {
  1055. ret = PTR_ERR(uverbs_class);
  1056. printk(KERN_ERR "user_verbs: couldn't create class infiniband_verbs\n");
  1057. goto out_chrdev;
  1058. }
  1059. uverbs_class->devnode = uverbs_devnode;
  1060. ret = class_create_file(uverbs_class, &class_attr_abi_version.attr);
  1061. if (ret) {
  1062. printk(KERN_ERR "user_verbs: couldn't create abi_version attribute\n");
  1063. goto out_class;
  1064. }
  1065. ret = ib_register_client(&uverbs_client);
  1066. if (ret) {
  1067. printk(KERN_ERR "user_verbs: couldn't register client\n");
  1068. goto out_class;
  1069. }
  1070. return 0;
  1071. out_class:
  1072. class_destroy(uverbs_class);
  1073. out_chrdev:
  1074. unregister_chrdev_region(IB_UVERBS_BASE_DEV, IB_UVERBS_MAX_DEVICES);
  1075. out:
  1076. return ret;
  1077. }
  1078. static void __exit ib_uverbs_cleanup(void)
  1079. {
  1080. ib_unregister_client(&uverbs_client);
  1081. class_destroy(uverbs_class);
  1082. unregister_chrdev_region(IB_UVERBS_BASE_DEV, IB_UVERBS_MAX_DEVICES);
  1083. if (overflow_maj)
  1084. unregister_chrdev_region(overflow_maj, IB_UVERBS_MAX_DEVICES);
  1085. idr_destroy(&ib_uverbs_pd_idr);
  1086. idr_destroy(&ib_uverbs_mr_idr);
  1087. idr_destroy(&ib_uverbs_mw_idr);
  1088. idr_destroy(&ib_uverbs_ah_idr);
  1089. idr_destroy(&ib_uverbs_cq_idr);
  1090. idr_destroy(&ib_uverbs_qp_idr);
  1091. idr_destroy(&ib_uverbs_srq_idr);
  1092. }
  1093. module_init(ib_uverbs_init);
  1094. module_exit(ib_uverbs_cleanup);