uverbs_main.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039
  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. const char __user *buf, int in_len,
  73. int out_len) = {
  74. [IB_USER_VERBS_CMD_GET_CONTEXT] = ib_uverbs_get_context,
  75. [IB_USER_VERBS_CMD_QUERY_DEVICE] = ib_uverbs_query_device,
  76. [IB_USER_VERBS_CMD_QUERY_PORT] = ib_uverbs_query_port,
  77. [IB_USER_VERBS_CMD_ALLOC_PD] = ib_uverbs_alloc_pd,
  78. [IB_USER_VERBS_CMD_DEALLOC_PD] = ib_uverbs_dealloc_pd,
  79. [IB_USER_VERBS_CMD_REG_MR] = ib_uverbs_reg_mr,
  80. [IB_USER_VERBS_CMD_REREG_MR] = ib_uverbs_rereg_mr,
  81. [IB_USER_VERBS_CMD_DEREG_MR] = ib_uverbs_dereg_mr,
  82. [IB_USER_VERBS_CMD_ALLOC_MW] = ib_uverbs_alloc_mw,
  83. [IB_USER_VERBS_CMD_DEALLOC_MW] = ib_uverbs_dealloc_mw,
  84. [IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL] = ib_uverbs_create_comp_channel,
  85. [IB_USER_VERBS_CMD_CREATE_CQ] = ib_uverbs_create_cq,
  86. [IB_USER_VERBS_CMD_RESIZE_CQ] = ib_uverbs_resize_cq,
  87. [IB_USER_VERBS_CMD_POLL_CQ] = ib_uverbs_poll_cq,
  88. [IB_USER_VERBS_CMD_REQ_NOTIFY_CQ] = ib_uverbs_req_notify_cq,
  89. [IB_USER_VERBS_CMD_DESTROY_CQ] = ib_uverbs_destroy_cq,
  90. [IB_USER_VERBS_CMD_CREATE_QP] = ib_uverbs_create_qp,
  91. [IB_USER_VERBS_CMD_QUERY_QP] = ib_uverbs_query_qp,
  92. [IB_USER_VERBS_CMD_MODIFY_QP] = ib_uverbs_modify_qp,
  93. [IB_USER_VERBS_CMD_DESTROY_QP] = ib_uverbs_destroy_qp,
  94. [IB_USER_VERBS_CMD_POST_SEND] = ib_uverbs_post_send,
  95. [IB_USER_VERBS_CMD_POST_RECV] = ib_uverbs_post_recv,
  96. [IB_USER_VERBS_CMD_POST_SRQ_RECV] = ib_uverbs_post_srq_recv,
  97. [IB_USER_VERBS_CMD_CREATE_AH] = ib_uverbs_create_ah,
  98. [IB_USER_VERBS_CMD_DESTROY_AH] = ib_uverbs_destroy_ah,
  99. [IB_USER_VERBS_CMD_ATTACH_MCAST] = ib_uverbs_attach_mcast,
  100. [IB_USER_VERBS_CMD_DETACH_MCAST] = ib_uverbs_detach_mcast,
  101. [IB_USER_VERBS_CMD_CREATE_SRQ] = ib_uverbs_create_srq,
  102. [IB_USER_VERBS_CMD_MODIFY_SRQ] = ib_uverbs_modify_srq,
  103. [IB_USER_VERBS_CMD_QUERY_SRQ] = ib_uverbs_query_srq,
  104. [IB_USER_VERBS_CMD_DESTROY_SRQ] = ib_uverbs_destroy_srq,
  105. [IB_USER_VERBS_CMD_OPEN_XRCD] = ib_uverbs_open_xrcd,
  106. [IB_USER_VERBS_CMD_CLOSE_XRCD] = ib_uverbs_close_xrcd,
  107. [IB_USER_VERBS_CMD_CREATE_XSRQ] = ib_uverbs_create_xsrq,
  108. [IB_USER_VERBS_CMD_OPEN_QP] = ib_uverbs_open_qp,
  109. };
  110. static int (*uverbs_ex_cmd_table[])(struct ib_uverbs_file *file,
  111. struct ib_udata *ucore,
  112. struct ib_udata *uhw) = {
  113. [IB_USER_VERBS_EX_CMD_CREATE_FLOW] = ib_uverbs_ex_create_flow,
  114. [IB_USER_VERBS_EX_CMD_DESTROY_FLOW] = ib_uverbs_ex_destroy_flow,
  115. [IB_USER_VERBS_EX_CMD_QUERY_DEVICE] = ib_uverbs_ex_query_device,
  116. };
  117. static void ib_uverbs_add_one(struct ib_device *device);
  118. static void ib_uverbs_remove_one(struct ib_device *device);
  119. static void ib_uverbs_release_dev(struct kref *ref)
  120. {
  121. struct ib_uverbs_device *dev =
  122. container_of(ref, struct ib_uverbs_device, ref);
  123. complete(&dev->comp);
  124. }
  125. static void ib_uverbs_release_event_file(struct kref *ref)
  126. {
  127. struct ib_uverbs_event_file *file =
  128. container_of(ref, struct ib_uverbs_event_file, ref);
  129. kfree(file);
  130. }
  131. void ib_uverbs_release_ucq(struct ib_uverbs_file *file,
  132. struct ib_uverbs_event_file *ev_file,
  133. struct ib_ucq_object *uobj)
  134. {
  135. struct ib_uverbs_event *evt, *tmp;
  136. if (ev_file) {
  137. spin_lock_irq(&ev_file->lock);
  138. list_for_each_entry_safe(evt, tmp, &uobj->comp_list, obj_list) {
  139. list_del(&evt->list);
  140. kfree(evt);
  141. }
  142. spin_unlock_irq(&ev_file->lock);
  143. kref_put(&ev_file->ref, ib_uverbs_release_event_file);
  144. }
  145. spin_lock_irq(&file->async_file->lock);
  146. list_for_each_entry_safe(evt, tmp, &uobj->async_list, obj_list) {
  147. list_del(&evt->list);
  148. kfree(evt);
  149. }
  150. spin_unlock_irq(&file->async_file->lock);
  151. }
  152. void ib_uverbs_release_uevent(struct ib_uverbs_file *file,
  153. struct ib_uevent_object *uobj)
  154. {
  155. struct ib_uverbs_event *evt, *tmp;
  156. spin_lock_irq(&file->async_file->lock);
  157. list_for_each_entry_safe(evt, tmp, &uobj->event_list, obj_list) {
  158. list_del(&evt->list);
  159. kfree(evt);
  160. }
  161. spin_unlock_irq(&file->async_file->lock);
  162. }
  163. static void ib_uverbs_detach_umcast(struct ib_qp *qp,
  164. struct ib_uqp_object *uobj)
  165. {
  166. struct ib_uverbs_mcast_entry *mcast, *tmp;
  167. list_for_each_entry_safe(mcast, tmp, &uobj->mcast_list, list) {
  168. ib_detach_mcast(qp, &mcast->gid, mcast->lid);
  169. list_del(&mcast->list);
  170. kfree(mcast);
  171. }
  172. }
  173. static int ib_uverbs_cleanup_ucontext(struct ib_uverbs_file *file,
  174. struct ib_ucontext *context)
  175. {
  176. struct ib_uobject *uobj, *tmp;
  177. if (!context)
  178. return 0;
  179. context->closing = 1;
  180. list_for_each_entry_safe(uobj, tmp, &context->ah_list, list) {
  181. struct ib_ah *ah = uobj->object;
  182. idr_remove_uobj(&ib_uverbs_ah_idr, uobj);
  183. ib_destroy_ah(ah);
  184. kfree(uobj);
  185. }
  186. /* Remove MWs before QPs, in order to support type 2A MWs. */
  187. list_for_each_entry_safe(uobj, tmp, &context->mw_list, list) {
  188. struct ib_mw *mw = uobj->object;
  189. idr_remove_uobj(&ib_uverbs_mw_idr, uobj);
  190. ib_dealloc_mw(mw);
  191. kfree(uobj);
  192. }
  193. list_for_each_entry_safe(uobj, tmp, &context->rule_list, list) {
  194. struct ib_flow *flow_id = uobj->object;
  195. idr_remove_uobj(&ib_uverbs_rule_idr, uobj);
  196. ib_destroy_flow(flow_id);
  197. kfree(uobj);
  198. }
  199. list_for_each_entry_safe(uobj, tmp, &context->qp_list, list) {
  200. struct ib_qp *qp = uobj->object;
  201. struct ib_uqp_object *uqp =
  202. container_of(uobj, struct ib_uqp_object, uevent.uobject);
  203. idr_remove_uobj(&ib_uverbs_qp_idr, uobj);
  204. if (qp != qp->real_qp) {
  205. ib_close_qp(qp);
  206. } else {
  207. ib_uverbs_detach_umcast(qp, uqp);
  208. ib_destroy_qp(qp);
  209. }
  210. ib_uverbs_release_uevent(file, &uqp->uevent);
  211. kfree(uqp);
  212. }
  213. list_for_each_entry_safe(uobj, tmp, &context->srq_list, list) {
  214. struct ib_srq *srq = uobj->object;
  215. struct ib_uevent_object *uevent =
  216. container_of(uobj, struct ib_uevent_object, uobject);
  217. idr_remove_uobj(&ib_uverbs_srq_idr, uobj);
  218. ib_destroy_srq(srq);
  219. ib_uverbs_release_uevent(file, uevent);
  220. kfree(uevent);
  221. }
  222. list_for_each_entry_safe(uobj, tmp, &context->cq_list, list) {
  223. struct ib_cq *cq = uobj->object;
  224. struct ib_uverbs_event_file *ev_file = cq->cq_context;
  225. struct ib_ucq_object *ucq =
  226. container_of(uobj, struct ib_ucq_object, uobject);
  227. idr_remove_uobj(&ib_uverbs_cq_idr, uobj);
  228. ib_destroy_cq(cq);
  229. ib_uverbs_release_ucq(file, ev_file, ucq);
  230. kfree(ucq);
  231. }
  232. list_for_each_entry_safe(uobj, tmp, &context->mr_list, list) {
  233. struct ib_mr *mr = uobj->object;
  234. idr_remove_uobj(&ib_uverbs_mr_idr, uobj);
  235. ib_dereg_mr(mr);
  236. kfree(uobj);
  237. }
  238. mutex_lock(&file->device->xrcd_tree_mutex);
  239. list_for_each_entry_safe(uobj, tmp, &context->xrcd_list, list) {
  240. struct ib_xrcd *xrcd = uobj->object;
  241. struct ib_uxrcd_object *uxrcd =
  242. container_of(uobj, struct ib_uxrcd_object, uobject);
  243. idr_remove_uobj(&ib_uverbs_xrcd_idr, uobj);
  244. ib_uverbs_dealloc_xrcd(file->device, xrcd);
  245. kfree(uxrcd);
  246. }
  247. mutex_unlock(&file->device->xrcd_tree_mutex);
  248. list_for_each_entry_safe(uobj, tmp, &context->pd_list, list) {
  249. struct ib_pd *pd = uobj->object;
  250. idr_remove_uobj(&ib_uverbs_pd_idr, uobj);
  251. ib_dealloc_pd(pd);
  252. kfree(uobj);
  253. }
  254. put_pid(context->tgid);
  255. return context->device->dealloc_ucontext(context);
  256. }
  257. static void ib_uverbs_release_file(struct kref *ref)
  258. {
  259. struct ib_uverbs_file *file =
  260. container_of(ref, struct ib_uverbs_file, ref);
  261. module_put(file->device->ib_dev->owner);
  262. kref_put(&file->device->ref, ib_uverbs_release_dev);
  263. kfree(file);
  264. }
  265. static ssize_t ib_uverbs_event_read(struct file *filp, char __user *buf,
  266. size_t count, loff_t *pos)
  267. {
  268. struct ib_uverbs_event_file *file = filp->private_data;
  269. struct ib_uverbs_event *event;
  270. int eventsz;
  271. int ret = 0;
  272. spin_lock_irq(&file->lock);
  273. while (list_empty(&file->event_list)) {
  274. spin_unlock_irq(&file->lock);
  275. if (filp->f_flags & O_NONBLOCK)
  276. return -EAGAIN;
  277. if (wait_event_interruptible(file->poll_wait,
  278. !list_empty(&file->event_list)))
  279. return -ERESTARTSYS;
  280. spin_lock_irq(&file->lock);
  281. }
  282. event = list_entry(file->event_list.next, struct ib_uverbs_event, list);
  283. if (file->is_async)
  284. eventsz = sizeof (struct ib_uverbs_async_event_desc);
  285. else
  286. eventsz = sizeof (struct ib_uverbs_comp_event_desc);
  287. if (eventsz > count) {
  288. ret = -EINVAL;
  289. event = NULL;
  290. } else {
  291. list_del(file->event_list.next);
  292. if (event->counter) {
  293. ++(*event->counter);
  294. list_del(&event->obj_list);
  295. }
  296. }
  297. spin_unlock_irq(&file->lock);
  298. if (event) {
  299. if (copy_to_user(buf, event, eventsz))
  300. ret = -EFAULT;
  301. else
  302. ret = eventsz;
  303. }
  304. kfree(event);
  305. return ret;
  306. }
  307. static unsigned int ib_uverbs_event_poll(struct file *filp,
  308. struct poll_table_struct *wait)
  309. {
  310. unsigned int pollflags = 0;
  311. struct ib_uverbs_event_file *file = filp->private_data;
  312. poll_wait(filp, &file->poll_wait, wait);
  313. spin_lock_irq(&file->lock);
  314. if (!list_empty(&file->event_list))
  315. pollflags = POLLIN | POLLRDNORM;
  316. spin_unlock_irq(&file->lock);
  317. return pollflags;
  318. }
  319. static int ib_uverbs_event_fasync(int fd, struct file *filp, int on)
  320. {
  321. struct ib_uverbs_event_file *file = filp->private_data;
  322. return fasync_helper(fd, filp, on, &file->async_queue);
  323. }
  324. static int ib_uverbs_event_close(struct inode *inode, struct file *filp)
  325. {
  326. struct ib_uverbs_event_file *file = filp->private_data;
  327. struct ib_uverbs_event *entry, *tmp;
  328. spin_lock_irq(&file->lock);
  329. file->is_closed = 1;
  330. list_for_each_entry_safe(entry, tmp, &file->event_list, list) {
  331. if (entry->counter)
  332. list_del(&entry->obj_list);
  333. kfree(entry);
  334. }
  335. spin_unlock_irq(&file->lock);
  336. if (file->is_async) {
  337. ib_unregister_event_handler(&file->uverbs_file->event_handler);
  338. kref_put(&file->uverbs_file->ref, ib_uverbs_release_file);
  339. }
  340. kref_put(&file->ref, ib_uverbs_release_event_file);
  341. return 0;
  342. }
  343. static const struct file_operations uverbs_event_fops = {
  344. .owner = THIS_MODULE,
  345. .read = ib_uverbs_event_read,
  346. .poll = ib_uverbs_event_poll,
  347. .release = ib_uverbs_event_close,
  348. .fasync = ib_uverbs_event_fasync,
  349. .llseek = no_llseek,
  350. };
  351. void ib_uverbs_comp_handler(struct ib_cq *cq, void *cq_context)
  352. {
  353. struct ib_uverbs_event_file *file = cq_context;
  354. struct ib_ucq_object *uobj;
  355. struct ib_uverbs_event *entry;
  356. unsigned long flags;
  357. if (!file)
  358. return;
  359. spin_lock_irqsave(&file->lock, flags);
  360. if (file->is_closed) {
  361. spin_unlock_irqrestore(&file->lock, flags);
  362. return;
  363. }
  364. entry = kmalloc(sizeof *entry, GFP_ATOMIC);
  365. if (!entry) {
  366. spin_unlock_irqrestore(&file->lock, flags);
  367. return;
  368. }
  369. uobj = container_of(cq->uobject, struct ib_ucq_object, uobject);
  370. entry->desc.comp.cq_handle = cq->uobject->user_handle;
  371. entry->counter = &uobj->comp_events_reported;
  372. list_add_tail(&entry->list, &file->event_list);
  373. list_add_tail(&entry->obj_list, &uobj->comp_list);
  374. spin_unlock_irqrestore(&file->lock, flags);
  375. wake_up_interruptible(&file->poll_wait);
  376. kill_fasync(&file->async_queue, SIGIO, POLL_IN);
  377. }
  378. static void ib_uverbs_async_handler(struct ib_uverbs_file *file,
  379. __u64 element, __u64 event,
  380. struct list_head *obj_list,
  381. u32 *counter)
  382. {
  383. struct ib_uverbs_event *entry;
  384. unsigned long flags;
  385. spin_lock_irqsave(&file->async_file->lock, flags);
  386. if (file->async_file->is_closed) {
  387. spin_unlock_irqrestore(&file->async_file->lock, flags);
  388. return;
  389. }
  390. entry = kmalloc(sizeof *entry, GFP_ATOMIC);
  391. if (!entry) {
  392. spin_unlock_irqrestore(&file->async_file->lock, flags);
  393. return;
  394. }
  395. entry->desc.async.element = element;
  396. entry->desc.async.event_type = event;
  397. entry->desc.async.reserved = 0;
  398. entry->counter = counter;
  399. list_add_tail(&entry->list, &file->async_file->event_list);
  400. if (obj_list)
  401. list_add_tail(&entry->obj_list, obj_list);
  402. spin_unlock_irqrestore(&file->async_file->lock, flags);
  403. wake_up_interruptible(&file->async_file->poll_wait);
  404. kill_fasync(&file->async_file->async_queue, SIGIO, POLL_IN);
  405. }
  406. void ib_uverbs_cq_event_handler(struct ib_event *event, void *context_ptr)
  407. {
  408. struct ib_ucq_object *uobj = container_of(event->element.cq->uobject,
  409. struct ib_ucq_object, uobject);
  410. ib_uverbs_async_handler(uobj->uverbs_file, uobj->uobject.user_handle,
  411. event->event, &uobj->async_list,
  412. &uobj->async_events_reported);
  413. }
  414. void ib_uverbs_qp_event_handler(struct ib_event *event, void *context_ptr)
  415. {
  416. struct ib_uevent_object *uobj;
  417. /* for XRC target qp's, check that qp is live */
  418. if (!event->element.qp->uobject || !event->element.qp->uobject->live)
  419. return;
  420. uobj = container_of(event->element.qp->uobject,
  421. struct ib_uevent_object, uobject);
  422. ib_uverbs_async_handler(context_ptr, uobj->uobject.user_handle,
  423. event->event, &uobj->event_list,
  424. &uobj->events_reported);
  425. }
  426. void ib_uverbs_srq_event_handler(struct ib_event *event, void *context_ptr)
  427. {
  428. struct ib_uevent_object *uobj;
  429. uobj = container_of(event->element.srq->uobject,
  430. struct ib_uevent_object, uobject);
  431. ib_uverbs_async_handler(context_ptr, uobj->uobject.user_handle,
  432. event->event, &uobj->event_list,
  433. &uobj->events_reported);
  434. }
  435. void ib_uverbs_event_handler(struct ib_event_handler *handler,
  436. struct ib_event *event)
  437. {
  438. struct ib_uverbs_file *file =
  439. container_of(handler, struct ib_uverbs_file, event_handler);
  440. ib_uverbs_async_handler(file, event->element.port_num, event->event,
  441. NULL, NULL);
  442. }
  443. struct file *ib_uverbs_alloc_event_file(struct ib_uverbs_file *uverbs_file,
  444. int is_async)
  445. {
  446. struct ib_uverbs_event_file *ev_file;
  447. struct file *filp;
  448. ev_file = kmalloc(sizeof *ev_file, GFP_KERNEL);
  449. if (!ev_file)
  450. return ERR_PTR(-ENOMEM);
  451. kref_init(&ev_file->ref);
  452. spin_lock_init(&ev_file->lock);
  453. INIT_LIST_HEAD(&ev_file->event_list);
  454. init_waitqueue_head(&ev_file->poll_wait);
  455. ev_file->uverbs_file = uverbs_file;
  456. ev_file->async_queue = NULL;
  457. ev_file->is_async = is_async;
  458. ev_file->is_closed = 0;
  459. filp = anon_inode_getfile("[infinibandevent]", &uverbs_event_fops,
  460. ev_file, O_RDONLY);
  461. if (IS_ERR(filp))
  462. kfree(ev_file);
  463. return filp;
  464. }
  465. /*
  466. * Look up a completion event file by FD. If lookup is successful,
  467. * takes a ref to the event file struct that it returns; if
  468. * unsuccessful, returns NULL.
  469. */
  470. struct ib_uverbs_event_file *ib_uverbs_lookup_comp_file(int fd)
  471. {
  472. struct ib_uverbs_event_file *ev_file = NULL;
  473. struct fd f = fdget(fd);
  474. if (!f.file)
  475. return NULL;
  476. if (f.file->f_op != &uverbs_event_fops)
  477. goto out;
  478. ev_file = f.file->private_data;
  479. if (ev_file->is_async) {
  480. ev_file = NULL;
  481. goto out;
  482. }
  483. kref_get(&ev_file->ref);
  484. out:
  485. fdput(f);
  486. return ev_file;
  487. }
  488. static ssize_t ib_uverbs_write(struct file *filp, const char __user *buf,
  489. size_t count, loff_t *pos)
  490. {
  491. struct ib_uverbs_file *file = filp->private_data;
  492. struct ib_uverbs_cmd_hdr hdr;
  493. __u32 flags;
  494. if (count < sizeof hdr)
  495. return -EINVAL;
  496. if (copy_from_user(&hdr, buf, sizeof hdr))
  497. return -EFAULT;
  498. flags = (hdr.command &
  499. IB_USER_VERBS_CMD_FLAGS_MASK) >> IB_USER_VERBS_CMD_FLAGS_SHIFT;
  500. if (!flags) {
  501. __u32 command;
  502. if (hdr.command & ~(__u32)(IB_USER_VERBS_CMD_FLAGS_MASK |
  503. IB_USER_VERBS_CMD_COMMAND_MASK))
  504. return -EINVAL;
  505. command = hdr.command & IB_USER_VERBS_CMD_COMMAND_MASK;
  506. if (command >= ARRAY_SIZE(uverbs_cmd_table) ||
  507. !uverbs_cmd_table[command])
  508. return -EINVAL;
  509. if (!file->ucontext &&
  510. command != IB_USER_VERBS_CMD_GET_CONTEXT)
  511. return -EINVAL;
  512. if (!(file->device->ib_dev->uverbs_cmd_mask & (1ull << command)))
  513. return -ENOSYS;
  514. if (hdr.in_words * 4 != count)
  515. return -EINVAL;
  516. return uverbs_cmd_table[command](file,
  517. buf + sizeof(hdr),
  518. hdr.in_words * 4,
  519. hdr.out_words * 4);
  520. } else if (flags == IB_USER_VERBS_CMD_FLAG_EXTENDED) {
  521. __u32 command;
  522. struct ib_uverbs_ex_cmd_hdr ex_hdr;
  523. struct ib_udata ucore;
  524. struct ib_udata uhw;
  525. int err;
  526. size_t written_count = count;
  527. if (hdr.command & ~(__u32)(IB_USER_VERBS_CMD_FLAGS_MASK |
  528. IB_USER_VERBS_CMD_COMMAND_MASK))
  529. return -EINVAL;
  530. command = hdr.command & IB_USER_VERBS_CMD_COMMAND_MASK;
  531. if (command >= ARRAY_SIZE(uverbs_ex_cmd_table) ||
  532. !uverbs_ex_cmd_table[command])
  533. return -ENOSYS;
  534. if (!file->ucontext)
  535. return -EINVAL;
  536. if (!(file->device->ib_dev->uverbs_ex_cmd_mask & (1ull << command)))
  537. return -ENOSYS;
  538. if (count < (sizeof(hdr) + sizeof(ex_hdr)))
  539. return -EINVAL;
  540. if (copy_from_user(&ex_hdr, buf + sizeof(hdr), sizeof(ex_hdr)))
  541. return -EFAULT;
  542. count -= sizeof(hdr) + sizeof(ex_hdr);
  543. buf += sizeof(hdr) + sizeof(ex_hdr);
  544. if ((hdr.in_words + ex_hdr.provider_in_words) * 8 != count)
  545. return -EINVAL;
  546. if (ex_hdr.cmd_hdr_reserved)
  547. return -EINVAL;
  548. if (ex_hdr.response) {
  549. if (!hdr.out_words && !ex_hdr.provider_out_words)
  550. return -EINVAL;
  551. if (!access_ok(VERIFY_WRITE,
  552. (void __user *) (unsigned long) ex_hdr.response,
  553. (hdr.out_words + ex_hdr.provider_out_words) * 8))
  554. return -EFAULT;
  555. } else {
  556. if (hdr.out_words || ex_hdr.provider_out_words)
  557. return -EINVAL;
  558. }
  559. INIT_UDATA_BUF_OR_NULL(&ucore, buf, (unsigned long) ex_hdr.response,
  560. hdr.in_words * 8, hdr.out_words * 8);
  561. INIT_UDATA_BUF_OR_NULL(&uhw,
  562. buf + ucore.inlen,
  563. (unsigned long) ex_hdr.response + ucore.outlen,
  564. ex_hdr.provider_in_words * 8,
  565. ex_hdr.provider_out_words * 8);
  566. err = uverbs_ex_cmd_table[command](file,
  567. &ucore,
  568. &uhw);
  569. if (err)
  570. return err;
  571. return written_count;
  572. }
  573. return -ENOSYS;
  574. }
  575. static int ib_uverbs_mmap(struct file *filp, struct vm_area_struct *vma)
  576. {
  577. struct ib_uverbs_file *file = filp->private_data;
  578. if (!file->ucontext)
  579. return -ENODEV;
  580. else
  581. return file->device->ib_dev->mmap(file->ucontext, vma);
  582. }
  583. /*
  584. * ib_uverbs_open() does not need the BKL:
  585. *
  586. * - the ib_uverbs_device structures are properly reference counted and
  587. * everything else is purely local to the file being created, so
  588. * races against other open calls are not a problem;
  589. * - there is no ioctl method to race against;
  590. * - the open method will either immediately run -ENXIO, or all
  591. * required initialization will be done.
  592. */
  593. static int ib_uverbs_open(struct inode *inode, struct file *filp)
  594. {
  595. struct ib_uverbs_device *dev;
  596. struct ib_uverbs_file *file;
  597. int ret;
  598. dev = container_of(inode->i_cdev, struct ib_uverbs_device, cdev);
  599. if (dev)
  600. kref_get(&dev->ref);
  601. else
  602. return -ENXIO;
  603. if (!try_module_get(dev->ib_dev->owner)) {
  604. ret = -ENODEV;
  605. goto err;
  606. }
  607. file = kmalloc(sizeof *file, GFP_KERNEL);
  608. if (!file) {
  609. ret = -ENOMEM;
  610. goto err_module;
  611. }
  612. file->device = dev;
  613. file->ucontext = NULL;
  614. file->async_file = NULL;
  615. kref_init(&file->ref);
  616. mutex_init(&file->mutex);
  617. filp->private_data = file;
  618. return nonseekable_open(inode, filp);
  619. err_module:
  620. module_put(dev->ib_dev->owner);
  621. err:
  622. kref_put(&dev->ref, ib_uverbs_release_dev);
  623. return ret;
  624. }
  625. static int ib_uverbs_close(struct inode *inode, struct file *filp)
  626. {
  627. struct ib_uverbs_file *file = filp->private_data;
  628. ib_uverbs_cleanup_ucontext(file, file->ucontext);
  629. if (file->async_file)
  630. kref_put(&file->async_file->ref, ib_uverbs_release_event_file);
  631. kref_put(&file->ref, ib_uverbs_release_file);
  632. return 0;
  633. }
  634. static const struct file_operations uverbs_fops = {
  635. .owner = THIS_MODULE,
  636. .write = ib_uverbs_write,
  637. .open = ib_uverbs_open,
  638. .release = ib_uverbs_close,
  639. .llseek = no_llseek,
  640. };
  641. static const struct file_operations uverbs_mmap_fops = {
  642. .owner = THIS_MODULE,
  643. .write = ib_uverbs_write,
  644. .mmap = ib_uverbs_mmap,
  645. .open = ib_uverbs_open,
  646. .release = ib_uverbs_close,
  647. .llseek = no_llseek,
  648. };
  649. static struct ib_client uverbs_client = {
  650. .name = "uverbs",
  651. .add = ib_uverbs_add_one,
  652. .remove = ib_uverbs_remove_one
  653. };
  654. static ssize_t show_ibdev(struct device *device, struct device_attribute *attr,
  655. char *buf)
  656. {
  657. struct ib_uverbs_device *dev = dev_get_drvdata(device);
  658. if (!dev)
  659. return -ENODEV;
  660. return sprintf(buf, "%s\n", dev->ib_dev->name);
  661. }
  662. static DEVICE_ATTR(ibdev, S_IRUGO, show_ibdev, NULL);
  663. static ssize_t show_dev_abi_version(struct device *device,
  664. struct device_attribute *attr, char *buf)
  665. {
  666. struct ib_uverbs_device *dev = dev_get_drvdata(device);
  667. if (!dev)
  668. return -ENODEV;
  669. return sprintf(buf, "%d\n", dev->ib_dev->uverbs_abi_ver);
  670. }
  671. static DEVICE_ATTR(abi_version, S_IRUGO, show_dev_abi_version, NULL);
  672. static CLASS_ATTR_STRING(abi_version, S_IRUGO,
  673. __stringify(IB_USER_VERBS_ABI_VERSION));
  674. static dev_t overflow_maj;
  675. static DECLARE_BITMAP(overflow_map, IB_UVERBS_MAX_DEVICES);
  676. /*
  677. * If we have more than IB_UVERBS_MAX_DEVICES, dynamically overflow by
  678. * requesting a new major number and doubling the number of max devices we
  679. * support. It's stupid, but simple.
  680. */
  681. static int find_overflow_devnum(void)
  682. {
  683. int ret;
  684. if (!overflow_maj) {
  685. ret = alloc_chrdev_region(&overflow_maj, 0, IB_UVERBS_MAX_DEVICES,
  686. "infiniband_verbs");
  687. if (ret) {
  688. printk(KERN_ERR "user_verbs: couldn't register dynamic device number\n");
  689. return ret;
  690. }
  691. }
  692. ret = find_first_zero_bit(overflow_map, IB_UVERBS_MAX_DEVICES);
  693. if (ret >= IB_UVERBS_MAX_DEVICES)
  694. return -1;
  695. return ret;
  696. }
  697. static void ib_uverbs_add_one(struct ib_device *device)
  698. {
  699. int devnum;
  700. dev_t base;
  701. struct ib_uverbs_device *uverbs_dev;
  702. if (!device->alloc_ucontext)
  703. return;
  704. uverbs_dev = kzalloc(sizeof *uverbs_dev, GFP_KERNEL);
  705. if (!uverbs_dev)
  706. return;
  707. kref_init(&uverbs_dev->ref);
  708. init_completion(&uverbs_dev->comp);
  709. uverbs_dev->xrcd_tree = RB_ROOT;
  710. mutex_init(&uverbs_dev->xrcd_tree_mutex);
  711. spin_lock(&map_lock);
  712. devnum = find_first_zero_bit(dev_map, IB_UVERBS_MAX_DEVICES);
  713. if (devnum >= IB_UVERBS_MAX_DEVICES) {
  714. spin_unlock(&map_lock);
  715. devnum = find_overflow_devnum();
  716. if (devnum < 0)
  717. goto err;
  718. spin_lock(&map_lock);
  719. uverbs_dev->devnum = devnum + IB_UVERBS_MAX_DEVICES;
  720. base = devnum + overflow_maj;
  721. set_bit(devnum, overflow_map);
  722. } else {
  723. uverbs_dev->devnum = devnum;
  724. base = devnum + IB_UVERBS_BASE_DEV;
  725. set_bit(devnum, dev_map);
  726. }
  727. spin_unlock(&map_lock);
  728. uverbs_dev->ib_dev = device;
  729. uverbs_dev->num_comp_vectors = device->num_comp_vectors;
  730. cdev_init(&uverbs_dev->cdev, NULL);
  731. uverbs_dev->cdev.owner = THIS_MODULE;
  732. uverbs_dev->cdev.ops = device->mmap ? &uverbs_mmap_fops : &uverbs_fops;
  733. kobject_set_name(&uverbs_dev->cdev.kobj, "uverbs%d", uverbs_dev->devnum);
  734. if (cdev_add(&uverbs_dev->cdev, base, 1))
  735. goto err_cdev;
  736. uverbs_dev->dev = device_create(uverbs_class, device->dma_device,
  737. uverbs_dev->cdev.dev, uverbs_dev,
  738. "uverbs%d", uverbs_dev->devnum);
  739. if (IS_ERR(uverbs_dev->dev))
  740. goto err_cdev;
  741. if (device_create_file(uverbs_dev->dev, &dev_attr_ibdev))
  742. goto err_class;
  743. if (device_create_file(uverbs_dev->dev, &dev_attr_abi_version))
  744. goto err_class;
  745. ib_set_client_data(device, &uverbs_client, uverbs_dev);
  746. return;
  747. err_class:
  748. device_destroy(uverbs_class, uverbs_dev->cdev.dev);
  749. err_cdev:
  750. cdev_del(&uverbs_dev->cdev);
  751. if (uverbs_dev->devnum < IB_UVERBS_MAX_DEVICES)
  752. clear_bit(devnum, dev_map);
  753. else
  754. clear_bit(devnum, overflow_map);
  755. err:
  756. kref_put(&uverbs_dev->ref, ib_uverbs_release_dev);
  757. wait_for_completion(&uverbs_dev->comp);
  758. kfree(uverbs_dev);
  759. return;
  760. }
  761. static void ib_uverbs_remove_one(struct ib_device *device)
  762. {
  763. struct ib_uverbs_device *uverbs_dev = ib_get_client_data(device, &uverbs_client);
  764. if (!uverbs_dev)
  765. return;
  766. dev_set_drvdata(uverbs_dev->dev, NULL);
  767. device_destroy(uverbs_class, uverbs_dev->cdev.dev);
  768. cdev_del(&uverbs_dev->cdev);
  769. if (uverbs_dev->devnum < IB_UVERBS_MAX_DEVICES)
  770. clear_bit(uverbs_dev->devnum, dev_map);
  771. else
  772. clear_bit(uverbs_dev->devnum - IB_UVERBS_MAX_DEVICES, overflow_map);
  773. kref_put(&uverbs_dev->ref, ib_uverbs_release_dev);
  774. wait_for_completion(&uverbs_dev->comp);
  775. kfree(uverbs_dev);
  776. }
  777. static char *uverbs_devnode(struct device *dev, umode_t *mode)
  778. {
  779. if (mode)
  780. *mode = 0666;
  781. return kasprintf(GFP_KERNEL, "infiniband/%s", dev_name(dev));
  782. }
  783. static int __init ib_uverbs_init(void)
  784. {
  785. int ret;
  786. ret = register_chrdev_region(IB_UVERBS_BASE_DEV, IB_UVERBS_MAX_DEVICES,
  787. "infiniband_verbs");
  788. if (ret) {
  789. printk(KERN_ERR "user_verbs: couldn't register device number\n");
  790. goto out;
  791. }
  792. uverbs_class = class_create(THIS_MODULE, "infiniband_verbs");
  793. if (IS_ERR(uverbs_class)) {
  794. ret = PTR_ERR(uverbs_class);
  795. printk(KERN_ERR "user_verbs: couldn't create class infiniband_verbs\n");
  796. goto out_chrdev;
  797. }
  798. uverbs_class->devnode = uverbs_devnode;
  799. ret = class_create_file(uverbs_class, &class_attr_abi_version.attr);
  800. if (ret) {
  801. printk(KERN_ERR "user_verbs: couldn't create abi_version attribute\n");
  802. goto out_class;
  803. }
  804. ret = ib_register_client(&uverbs_client);
  805. if (ret) {
  806. printk(KERN_ERR "user_verbs: couldn't register client\n");
  807. goto out_class;
  808. }
  809. return 0;
  810. out_class:
  811. class_destroy(uverbs_class);
  812. out_chrdev:
  813. unregister_chrdev_region(IB_UVERBS_BASE_DEV, IB_UVERBS_MAX_DEVICES);
  814. out:
  815. return ret;
  816. }
  817. static void __exit ib_uverbs_cleanup(void)
  818. {
  819. ib_unregister_client(&uverbs_client);
  820. class_destroy(uverbs_class);
  821. unregister_chrdev_region(IB_UVERBS_BASE_DEV, IB_UVERBS_MAX_DEVICES);
  822. if (overflow_maj)
  823. unregister_chrdev_region(overflow_maj, IB_UVERBS_MAX_DEVICES);
  824. idr_destroy(&ib_uverbs_pd_idr);
  825. idr_destroy(&ib_uverbs_mr_idr);
  826. idr_destroy(&ib_uverbs_mw_idr);
  827. idr_destroy(&ib_uverbs_ah_idr);
  828. idr_destroy(&ib_uverbs_cq_idr);
  829. idr_destroy(&ib_uverbs_qp_idr);
  830. idr_destroy(&ib_uverbs_srq_idr);
  831. }
  832. module_init(ib_uverbs_init);
  833. module_exit(ib_uverbs_cleanup);