uverbs_main.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921
  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/mount.h>
  45. #include <linux/cdev.h>
  46. #include <asm/uaccess.h>
  47. #include "uverbs.h"
  48. MODULE_AUTHOR("Roland Dreier");
  49. MODULE_DESCRIPTION("InfiniBand userspace verbs access");
  50. MODULE_LICENSE("Dual BSD/GPL");
  51. #define INFINIBANDEVENTFS_MAGIC 0x49426576 /* "IBev" */
  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. static DEFINE_SPINLOCK(map_lock);
  68. static struct ib_uverbs_device *dev_table[IB_UVERBS_MAX_DEVICES];
  69. static DECLARE_BITMAP(dev_map, IB_UVERBS_MAX_DEVICES);
  70. static ssize_t (*uverbs_cmd_table[])(struct ib_uverbs_file *file,
  71. const char __user *buf, int in_len,
  72. int out_len) = {
  73. [IB_USER_VERBS_CMD_GET_CONTEXT] = ib_uverbs_get_context,
  74. [IB_USER_VERBS_CMD_QUERY_DEVICE] = ib_uverbs_query_device,
  75. [IB_USER_VERBS_CMD_QUERY_PORT] = ib_uverbs_query_port,
  76. [IB_USER_VERBS_CMD_ALLOC_PD] = ib_uverbs_alloc_pd,
  77. [IB_USER_VERBS_CMD_DEALLOC_PD] = ib_uverbs_dealloc_pd,
  78. [IB_USER_VERBS_CMD_REG_MR] = ib_uverbs_reg_mr,
  79. [IB_USER_VERBS_CMD_DEREG_MR] = ib_uverbs_dereg_mr,
  80. [IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL] = ib_uverbs_create_comp_channel,
  81. [IB_USER_VERBS_CMD_CREATE_CQ] = ib_uverbs_create_cq,
  82. [IB_USER_VERBS_CMD_RESIZE_CQ] = ib_uverbs_resize_cq,
  83. [IB_USER_VERBS_CMD_POLL_CQ] = ib_uverbs_poll_cq,
  84. [IB_USER_VERBS_CMD_REQ_NOTIFY_CQ] = ib_uverbs_req_notify_cq,
  85. [IB_USER_VERBS_CMD_DESTROY_CQ] = ib_uverbs_destroy_cq,
  86. [IB_USER_VERBS_CMD_CREATE_QP] = ib_uverbs_create_qp,
  87. [IB_USER_VERBS_CMD_QUERY_QP] = ib_uverbs_query_qp,
  88. [IB_USER_VERBS_CMD_MODIFY_QP] = ib_uverbs_modify_qp,
  89. [IB_USER_VERBS_CMD_DESTROY_QP] = ib_uverbs_destroy_qp,
  90. [IB_USER_VERBS_CMD_POST_SEND] = ib_uverbs_post_send,
  91. [IB_USER_VERBS_CMD_POST_RECV] = ib_uverbs_post_recv,
  92. [IB_USER_VERBS_CMD_POST_SRQ_RECV] = ib_uverbs_post_srq_recv,
  93. [IB_USER_VERBS_CMD_CREATE_AH] = ib_uverbs_create_ah,
  94. [IB_USER_VERBS_CMD_DESTROY_AH] = ib_uverbs_destroy_ah,
  95. [IB_USER_VERBS_CMD_ATTACH_MCAST] = ib_uverbs_attach_mcast,
  96. [IB_USER_VERBS_CMD_DETACH_MCAST] = ib_uverbs_detach_mcast,
  97. [IB_USER_VERBS_CMD_CREATE_SRQ] = ib_uverbs_create_srq,
  98. [IB_USER_VERBS_CMD_MODIFY_SRQ] = ib_uverbs_modify_srq,
  99. [IB_USER_VERBS_CMD_QUERY_SRQ] = ib_uverbs_query_srq,
  100. [IB_USER_VERBS_CMD_DESTROY_SRQ] = ib_uverbs_destroy_srq,
  101. };
  102. static struct vfsmount *uverbs_event_mnt;
  103. static void ib_uverbs_add_one(struct ib_device *device);
  104. static void ib_uverbs_remove_one(struct ib_device *device);
  105. static void ib_uverbs_release_dev(struct kref *ref)
  106. {
  107. struct ib_uverbs_device *dev =
  108. container_of(ref, struct ib_uverbs_device, ref);
  109. complete(&dev->comp);
  110. }
  111. static void ib_uverbs_release_event_file(struct kref *ref)
  112. {
  113. struct ib_uverbs_event_file *file =
  114. container_of(ref, struct ib_uverbs_event_file, ref);
  115. kfree(file);
  116. }
  117. void ib_uverbs_release_ucq(struct ib_uverbs_file *file,
  118. struct ib_uverbs_event_file *ev_file,
  119. struct ib_ucq_object *uobj)
  120. {
  121. struct ib_uverbs_event *evt, *tmp;
  122. if (ev_file) {
  123. spin_lock_irq(&ev_file->lock);
  124. list_for_each_entry_safe(evt, tmp, &uobj->comp_list, obj_list) {
  125. list_del(&evt->list);
  126. kfree(evt);
  127. }
  128. spin_unlock_irq(&ev_file->lock);
  129. kref_put(&ev_file->ref, ib_uverbs_release_event_file);
  130. }
  131. spin_lock_irq(&file->async_file->lock);
  132. list_for_each_entry_safe(evt, tmp, &uobj->async_list, obj_list) {
  133. list_del(&evt->list);
  134. kfree(evt);
  135. }
  136. spin_unlock_irq(&file->async_file->lock);
  137. }
  138. void ib_uverbs_release_uevent(struct ib_uverbs_file *file,
  139. struct ib_uevent_object *uobj)
  140. {
  141. struct ib_uverbs_event *evt, *tmp;
  142. spin_lock_irq(&file->async_file->lock);
  143. list_for_each_entry_safe(evt, tmp, &uobj->event_list, obj_list) {
  144. list_del(&evt->list);
  145. kfree(evt);
  146. }
  147. spin_unlock_irq(&file->async_file->lock);
  148. }
  149. static void ib_uverbs_detach_umcast(struct ib_qp *qp,
  150. struct ib_uqp_object *uobj)
  151. {
  152. struct ib_uverbs_mcast_entry *mcast, *tmp;
  153. list_for_each_entry_safe(mcast, tmp, &uobj->mcast_list, list) {
  154. ib_detach_mcast(qp, &mcast->gid, mcast->lid);
  155. list_del(&mcast->list);
  156. kfree(mcast);
  157. }
  158. }
  159. static int ib_uverbs_cleanup_ucontext(struct ib_uverbs_file *file,
  160. struct ib_ucontext *context)
  161. {
  162. struct ib_uobject *uobj, *tmp;
  163. if (!context)
  164. return 0;
  165. context->closing = 1;
  166. list_for_each_entry_safe(uobj, tmp, &context->ah_list, list) {
  167. struct ib_ah *ah = uobj->object;
  168. idr_remove_uobj(&ib_uverbs_ah_idr, uobj);
  169. ib_destroy_ah(ah);
  170. kfree(uobj);
  171. }
  172. list_for_each_entry_safe(uobj, tmp, &context->qp_list, list) {
  173. struct ib_qp *qp = uobj->object;
  174. struct ib_uqp_object *uqp =
  175. container_of(uobj, struct ib_uqp_object, uevent.uobject);
  176. idr_remove_uobj(&ib_uverbs_qp_idr, uobj);
  177. ib_uverbs_detach_umcast(qp, uqp);
  178. ib_destroy_qp(qp);
  179. ib_uverbs_release_uevent(file, &uqp->uevent);
  180. kfree(uqp);
  181. }
  182. list_for_each_entry_safe(uobj, tmp, &context->cq_list, list) {
  183. struct ib_cq *cq = uobj->object;
  184. struct ib_uverbs_event_file *ev_file = cq->cq_context;
  185. struct ib_ucq_object *ucq =
  186. container_of(uobj, struct ib_ucq_object, uobject);
  187. idr_remove_uobj(&ib_uverbs_cq_idr, uobj);
  188. ib_destroy_cq(cq);
  189. ib_uverbs_release_ucq(file, ev_file, ucq);
  190. kfree(ucq);
  191. }
  192. list_for_each_entry_safe(uobj, tmp, &context->srq_list, list) {
  193. struct ib_srq *srq = uobj->object;
  194. struct ib_uevent_object *uevent =
  195. container_of(uobj, struct ib_uevent_object, uobject);
  196. idr_remove_uobj(&ib_uverbs_srq_idr, uobj);
  197. ib_destroy_srq(srq);
  198. ib_uverbs_release_uevent(file, uevent);
  199. kfree(uevent);
  200. }
  201. /* XXX Free MWs */
  202. list_for_each_entry_safe(uobj, tmp, &context->mr_list, list) {
  203. struct ib_mr *mr = uobj->object;
  204. idr_remove_uobj(&ib_uverbs_mr_idr, uobj);
  205. ib_dereg_mr(mr);
  206. kfree(uobj);
  207. }
  208. list_for_each_entry_safe(uobj, tmp, &context->pd_list, list) {
  209. struct ib_pd *pd = uobj->object;
  210. idr_remove_uobj(&ib_uverbs_pd_idr, uobj);
  211. ib_dealloc_pd(pd);
  212. kfree(uobj);
  213. }
  214. return context->device->dealloc_ucontext(context);
  215. }
  216. static void ib_uverbs_release_file(struct kref *ref)
  217. {
  218. struct ib_uverbs_file *file =
  219. container_of(ref, struct ib_uverbs_file, ref);
  220. module_put(file->device->ib_dev->owner);
  221. kref_put(&file->device->ref, ib_uverbs_release_dev);
  222. kfree(file);
  223. }
  224. static ssize_t ib_uverbs_event_read(struct file *filp, char __user *buf,
  225. size_t count, loff_t *pos)
  226. {
  227. struct ib_uverbs_event_file *file = filp->private_data;
  228. struct ib_uverbs_event *event;
  229. int eventsz;
  230. int ret = 0;
  231. spin_lock_irq(&file->lock);
  232. while (list_empty(&file->event_list)) {
  233. spin_unlock_irq(&file->lock);
  234. if (filp->f_flags & O_NONBLOCK)
  235. return -EAGAIN;
  236. if (wait_event_interruptible(file->poll_wait,
  237. !list_empty(&file->event_list)))
  238. return -ERESTARTSYS;
  239. spin_lock_irq(&file->lock);
  240. }
  241. event = list_entry(file->event_list.next, struct ib_uverbs_event, list);
  242. if (file->is_async)
  243. eventsz = sizeof (struct ib_uverbs_async_event_desc);
  244. else
  245. eventsz = sizeof (struct ib_uverbs_comp_event_desc);
  246. if (eventsz > count) {
  247. ret = -EINVAL;
  248. event = NULL;
  249. } else {
  250. list_del(file->event_list.next);
  251. if (event->counter) {
  252. ++(*event->counter);
  253. list_del(&event->obj_list);
  254. }
  255. }
  256. spin_unlock_irq(&file->lock);
  257. if (event) {
  258. if (copy_to_user(buf, event, eventsz))
  259. ret = -EFAULT;
  260. else
  261. ret = eventsz;
  262. }
  263. kfree(event);
  264. return ret;
  265. }
  266. static unsigned int ib_uverbs_event_poll(struct file *filp,
  267. struct poll_table_struct *wait)
  268. {
  269. unsigned int pollflags = 0;
  270. struct ib_uverbs_event_file *file = filp->private_data;
  271. poll_wait(filp, &file->poll_wait, wait);
  272. spin_lock_irq(&file->lock);
  273. if (!list_empty(&file->event_list))
  274. pollflags = POLLIN | POLLRDNORM;
  275. spin_unlock_irq(&file->lock);
  276. return pollflags;
  277. }
  278. static int ib_uverbs_event_fasync(int fd, struct file *filp, int on)
  279. {
  280. struct ib_uverbs_event_file *file = filp->private_data;
  281. return fasync_helper(fd, filp, on, &file->async_queue);
  282. }
  283. static int ib_uverbs_event_close(struct inode *inode, struct file *filp)
  284. {
  285. struct ib_uverbs_event_file *file = filp->private_data;
  286. struct ib_uverbs_event *entry, *tmp;
  287. spin_lock_irq(&file->lock);
  288. file->is_closed = 1;
  289. list_for_each_entry_safe(entry, tmp, &file->event_list, list) {
  290. if (entry->counter)
  291. list_del(&entry->obj_list);
  292. kfree(entry);
  293. }
  294. spin_unlock_irq(&file->lock);
  295. if (file->is_async) {
  296. ib_unregister_event_handler(&file->uverbs_file->event_handler);
  297. kref_put(&file->uverbs_file->ref, ib_uverbs_release_file);
  298. }
  299. kref_put(&file->ref, ib_uverbs_release_event_file);
  300. return 0;
  301. }
  302. static const struct file_operations uverbs_event_fops = {
  303. .owner = THIS_MODULE,
  304. .read = ib_uverbs_event_read,
  305. .poll = ib_uverbs_event_poll,
  306. .release = ib_uverbs_event_close,
  307. .fasync = ib_uverbs_event_fasync
  308. };
  309. void ib_uverbs_comp_handler(struct ib_cq *cq, void *cq_context)
  310. {
  311. struct ib_uverbs_event_file *file = cq_context;
  312. struct ib_ucq_object *uobj;
  313. struct ib_uverbs_event *entry;
  314. unsigned long flags;
  315. if (!file)
  316. return;
  317. spin_lock_irqsave(&file->lock, flags);
  318. if (file->is_closed) {
  319. spin_unlock_irqrestore(&file->lock, flags);
  320. return;
  321. }
  322. entry = kmalloc(sizeof *entry, GFP_ATOMIC);
  323. if (!entry) {
  324. spin_unlock_irqrestore(&file->lock, flags);
  325. return;
  326. }
  327. uobj = container_of(cq->uobject, struct ib_ucq_object, uobject);
  328. entry->desc.comp.cq_handle = cq->uobject->user_handle;
  329. entry->counter = &uobj->comp_events_reported;
  330. list_add_tail(&entry->list, &file->event_list);
  331. list_add_tail(&entry->obj_list, &uobj->comp_list);
  332. spin_unlock_irqrestore(&file->lock, flags);
  333. wake_up_interruptible(&file->poll_wait);
  334. kill_fasync(&file->async_queue, SIGIO, POLL_IN);
  335. }
  336. static void ib_uverbs_async_handler(struct ib_uverbs_file *file,
  337. __u64 element, __u64 event,
  338. struct list_head *obj_list,
  339. u32 *counter)
  340. {
  341. struct ib_uverbs_event *entry;
  342. unsigned long flags;
  343. spin_lock_irqsave(&file->async_file->lock, flags);
  344. if (file->async_file->is_closed) {
  345. spin_unlock_irqrestore(&file->async_file->lock, flags);
  346. return;
  347. }
  348. entry = kmalloc(sizeof *entry, GFP_ATOMIC);
  349. if (!entry) {
  350. spin_unlock_irqrestore(&file->async_file->lock, flags);
  351. return;
  352. }
  353. entry->desc.async.element = element;
  354. entry->desc.async.event_type = event;
  355. entry->counter = counter;
  356. list_add_tail(&entry->list, &file->async_file->event_list);
  357. if (obj_list)
  358. list_add_tail(&entry->obj_list, obj_list);
  359. spin_unlock_irqrestore(&file->async_file->lock, flags);
  360. wake_up_interruptible(&file->async_file->poll_wait);
  361. kill_fasync(&file->async_file->async_queue, SIGIO, POLL_IN);
  362. }
  363. void ib_uverbs_cq_event_handler(struct ib_event *event, void *context_ptr)
  364. {
  365. struct ib_ucq_object *uobj = container_of(event->element.cq->uobject,
  366. struct ib_ucq_object, uobject);
  367. ib_uverbs_async_handler(uobj->uverbs_file, uobj->uobject.user_handle,
  368. event->event, &uobj->async_list,
  369. &uobj->async_events_reported);
  370. }
  371. void ib_uverbs_qp_event_handler(struct ib_event *event, void *context_ptr)
  372. {
  373. struct ib_uevent_object *uobj;
  374. uobj = container_of(event->element.qp->uobject,
  375. struct ib_uevent_object, uobject);
  376. ib_uverbs_async_handler(context_ptr, uobj->uobject.user_handle,
  377. event->event, &uobj->event_list,
  378. &uobj->events_reported);
  379. }
  380. void ib_uverbs_srq_event_handler(struct ib_event *event, void *context_ptr)
  381. {
  382. struct ib_uevent_object *uobj;
  383. uobj = container_of(event->element.srq->uobject,
  384. struct ib_uevent_object, uobject);
  385. ib_uverbs_async_handler(context_ptr, uobj->uobject.user_handle,
  386. event->event, &uobj->event_list,
  387. &uobj->events_reported);
  388. }
  389. void ib_uverbs_event_handler(struct ib_event_handler *handler,
  390. struct ib_event *event)
  391. {
  392. struct ib_uverbs_file *file =
  393. container_of(handler, struct ib_uverbs_file, event_handler);
  394. ib_uverbs_async_handler(file, event->element.port_num, event->event,
  395. NULL, NULL);
  396. }
  397. struct file *ib_uverbs_alloc_event_file(struct ib_uverbs_file *uverbs_file,
  398. int is_async, int *fd)
  399. {
  400. struct ib_uverbs_event_file *ev_file;
  401. struct path path;
  402. struct file *filp;
  403. int ret;
  404. ev_file = kmalloc(sizeof *ev_file, GFP_KERNEL);
  405. if (!ev_file)
  406. return ERR_PTR(-ENOMEM);
  407. kref_init(&ev_file->ref);
  408. spin_lock_init(&ev_file->lock);
  409. INIT_LIST_HEAD(&ev_file->event_list);
  410. init_waitqueue_head(&ev_file->poll_wait);
  411. ev_file->uverbs_file = uverbs_file;
  412. ev_file->async_queue = NULL;
  413. ev_file->is_async = is_async;
  414. ev_file->is_closed = 0;
  415. *fd = get_unused_fd();
  416. if (*fd < 0) {
  417. ret = *fd;
  418. goto err;
  419. }
  420. /*
  421. * fops_get() can't fail here, because we're coming from a
  422. * system call on a uverbs file, which will already have a
  423. * module reference.
  424. */
  425. path.mnt = uverbs_event_mnt;
  426. path.dentry = uverbs_event_mnt->mnt_root;
  427. path_get(&path);
  428. filp = alloc_file(&path, FMODE_READ, fops_get(&uverbs_event_fops));
  429. if (!filp) {
  430. ret = -ENFILE;
  431. goto err_fd;
  432. }
  433. filp->private_data = ev_file;
  434. return filp;
  435. err_fd:
  436. fops_put(&uverbs_event_fops);
  437. path_put(&path);
  438. put_unused_fd(*fd);
  439. err:
  440. kfree(ev_file);
  441. return ERR_PTR(ret);
  442. }
  443. /*
  444. * Look up a completion event file by FD. If lookup is successful,
  445. * takes a ref to the event file struct that it returns; if
  446. * unsuccessful, returns NULL.
  447. */
  448. struct ib_uverbs_event_file *ib_uverbs_lookup_comp_file(int fd)
  449. {
  450. struct ib_uverbs_event_file *ev_file = NULL;
  451. struct file *filp;
  452. filp = fget(fd);
  453. if (!filp)
  454. return NULL;
  455. if (filp->f_op != &uverbs_event_fops)
  456. goto out;
  457. ev_file = filp->private_data;
  458. if (ev_file->is_async) {
  459. ev_file = NULL;
  460. goto out;
  461. }
  462. kref_get(&ev_file->ref);
  463. out:
  464. fput(filp);
  465. return ev_file;
  466. }
  467. static ssize_t ib_uverbs_write(struct file *filp, const char __user *buf,
  468. size_t count, loff_t *pos)
  469. {
  470. struct ib_uverbs_file *file = filp->private_data;
  471. struct ib_uverbs_cmd_hdr hdr;
  472. if (count < sizeof hdr)
  473. return -EINVAL;
  474. if (copy_from_user(&hdr, buf, sizeof hdr))
  475. return -EFAULT;
  476. if (hdr.in_words * 4 != count)
  477. return -EINVAL;
  478. if (hdr.command < 0 ||
  479. hdr.command >= ARRAY_SIZE(uverbs_cmd_table) ||
  480. !uverbs_cmd_table[hdr.command])
  481. return -EINVAL;
  482. if (!file->ucontext &&
  483. hdr.command != IB_USER_VERBS_CMD_GET_CONTEXT)
  484. return -EINVAL;
  485. if (!(file->device->ib_dev->uverbs_cmd_mask & (1ull << hdr.command)))
  486. return -ENOSYS;
  487. return uverbs_cmd_table[hdr.command](file, buf + sizeof hdr,
  488. hdr.in_words * 4, hdr.out_words * 4);
  489. }
  490. static int ib_uverbs_mmap(struct file *filp, struct vm_area_struct *vma)
  491. {
  492. struct ib_uverbs_file *file = filp->private_data;
  493. if (!file->ucontext)
  494. return -ENODEV;
  495. else
  496. return file->device->ib_dev->mmap(file->ucontext, vma);
  497. }
  498. /*
  499. * ib_uverbs_open() does not need the BKL:
  500. *
  501. * - dev_table[] accesses are protected by map_lock, the
  502. * ib_uverbs_device structures are properly reference counted, and
  503. * everything else is purely local to the file being created, so
  504. * races against other open calls are not a problem;
  505. * - there is no ioctl method to race against;
  506. * - the device is added to dev_table[] as the last part of module
  507. * initialization, the open method will either immediately run
  508. * -ENXIO, or all required initialization will be done.
  509. */
  510. static int ib_uverbs_open(struct inode *inode, struct file *filp)
  511. {
  512. struct ib_uverbs_device *dev;
  513. struct ib_uverbs_file *file;
  514. int ret;
  515. spin_lock(&map_lock);
  516. dev = dev_table[iminor(inode) - IB_UVERBS_BASE_MINOR];
  517. if (dev)
  518. kref_get(&dev->ref);
  519. spin_unlock(&map_lock);
  520. if (!dev)
  521. return -ENXIO;
  522. if (!try_module_get(dev->ib_dev->owner)) {
  523. ret = -ENODEV;
  524. goto err;
  525. }
  526. file = kmalloc(sizeof *file, GFP_KERNEL);
  527. if (!file) {
  528. ret = -ENOMEM;
  529. goto err_module;
  530. }
  531. file->device = dev;
  532. file->ucontext = NULL;
  533. file->async_file = NULL;
  534. kref_init(&file->ref);
  535. mutex_init(&file->mutex);
  536. filp->private_data = file;
  537. return 0;
  538. err_module:
  539. module_put(dev->ib_dev->owner);
  540. err:
  541. kref_put(&dev->ref, ib_uverbs_release_dev);
  542. return ret;
  543. }
  544. static int ib_uverbs_close(struct inode *inode, struct file *filp)
  545. {
  546. struct ib_uverbs_file *file = filp->private_data;
  547. ib_uverbs_cleanup_ucontext(file, file->ucontext);
  548. if (file->async_file)
  549. kref_put(&file->async_file->ref, ib_uverbs_release_event_file);
  550. kref_put(&file->ref, ib_uverbs_release_file);
  551. return 0;
  552. }
  553. static const struct file_operations uverbs_fops = {
  554. .owner = THIS_MODULE,
  555. .write = ib_uverbs_write,
  556. .open = ib_uverbs_open,
  557. .release = ib_uverbs_close
  558. };
  559. static const struct file_operations uverbs_mmap_fops = {
  560. .owner = THIS_MODULE,
  561. .write = ib_uverbs_write,
  562. .mmap = ib_uverbs_mmap,
  563. .open = ib_uverbs_open,
  564. .release = ib_uverbs_close
  565. };
  566. static struct ib_client uverbs_client = {
  567. .name = "uverbs",
  568. .add = ib_uverbs_add_one,
  569. .remove = ib_uverbs_remove_one
  570. };
  571. static ssize_t show_ibdev(struct device *device, struct device_attribute *attr,
  572. char *buf)
  573. {
  574. struct ib_uverbs_device *dev = dev_get_drvdata(device);
  575. if (!dev)
  576. return -ENODEV;
  577. return sprintf(buf, "%s\n", dev->ib_dev->name);
  578. }
  579. static DEVICE_ATTR(ibdev, S_IRUGO, show_ibdev, NULL);
  580. static ssize_t show_dev_abi_version(struct device *device,
  581. struct device_attribute *attr, char *buf)
  582. {
  583. struct ib_uverbs_device *dev = dev_get_drvdata(device);
  584. if (!dev)
  585. return -ENODEV;
  586. return sprintf(buf, "%d\n", dev->ib_dev->uverbs_abi_ver);
  587. }
  588. static DEVICE_ATTR(abi_version, S_IRUGO, show_dev_abi_version, NULL);
  589. static ssize_t show_abi_version(struct class *class, char *buf)
  590. {
  591. return sprintf(buf, "%d\n", IB_USER_VERBS_ABI_VERSION);
  592. }
  593. static CLASS_ATTR(abi_version, S_IRUGO, show_abi_version, NULL);
  594. static void ib_uverbs_add_one(struct ib_device *device)
  595. {
  596. struct ib_uverbs_device *uverbs_dev;
  597. if (!device->alloc_ucontext)
  598. return;
  599. uverbs_dev = kzalloc(sizeof *uverbs_dev, GFP_KERNEL);
  600. if (!uverbs_dev)
  601. return;
  602. kref_init(&uverbs_dev->ref);
  603. init_completion(&uverbs_dev->comp);
  604. spin_lock(&map_lock);
  605. uverbs_dev->devnum = find_first_zero_bit(dev_map, IB_UVERBS_MAX_DEVICES);
  606. if (uverbs_dev->devnum >= IB_UVERBS_MAX_DEVICES) {
  607. spin_unlock(&map_lock);
  608. goto err;
  609. }
  610. set_bit(uverbs_dev->devnum, dev_map);
  611. spin_unlock(&map_lock);
  612. uverbs_dev->ib_dev = device;
  613. uverbs_dev->num_comp_vectors = device->num_comp_vectors;
  614. uverbs_dev->cdev = cdev_alloc();
  615. if (!uverbs_dev->cdev)
  616. goto err;
  617. uverbs_dev->cdev->owner = THIS_MODULE;
  618. uverbs_dev->cdev->ops = device->mmap ? &uverbs_mmap_fops : &uverbs_fops;
  619. kobject_set_name(&uverbs_dev->cdev->kobj, "uverbs%d", uverbs_dev->devnum);
  620. if (cdev_add(uverbs_dev->cdev, IB_UVERBS_BASE_DEV + uverbs_dev->devnum, 1))
  621. goto err_cdev;
  622. uverbs_dev->dev = device_create(uverbs_class, device->dma_device,
  623. uverbs_dev->cdev->dev, uverbs_dev,
  624. "uverbs%d", uverbs_dev->devnum);
  625. if (IS_ERR(uverbs_dev->dev))
  626. goto err_cdev;
  627. if (device_create_file(uverbs_dev->dev, &dev_attr_ibdev))
  628. goto err_class;
  629. if (device_create_file(uverbs_dev->dev, &dev_attr_abi_version))
  630. goto err_class;
  631. spin_lock(&map_lock);
  632. dev_table[uverbs_dev->devnum] = uverbs_dev;
  633. spin_unlock(&map_lock);
  634. ib_set_client_data(device, &uverbs_client, uverbs_dev);
  635. return;
  636. err_class:
  637. device_destroy(uverbs_class, uverbs_dev->cdev->dev);
  638. err_cdev:
  639. cdev_del(uverbs_dev->cdev);
  640. clear_bit(uverbs_dev->devnum, dev_map);
  641. err:
  642. kref_put(&uverbs_dev->ref, ib_uverbs_release_dev);
  643. wait_for_completion(&uverbs_dev->comp);
  644. kfree(uverbs_dev);
  645. return;
  646. }
  647. static void ib_uverbs_remove_one(struct ib_device *device)
  648. {
  649. struct ib_uverbs_device *uverbs_dev = ib_get_client_data(device, &uverbs_client);
  650. if (!uverbs_dev)
  651. return;
  652. dev_set_drvdata(uverbs_dev->dev, NULL);
  653. device_destroy(uverbs_class, uverbs_dev->cdev->dev);
  654. cdev_del(uverbs_dev->cdev);
  655. spin_lock(&map_lock);
  656. dev_table[uverbs_dev->devnum] = NULL;
  657. spin_unlock(&map_lock);
  658. clear_bit(uverbs_dev->devnum, dev_map);
  659. kref_put(&uverbs_dev->ref, ib_uverbs_release_dev);
  660. wait_for_completion(&uverbs_dev->comp);
  661. kfree(uverbs_dev);
  662. }
  663. static int uverbs_event_get_sb(struct file_system_type *fs_type, int flags,
  664. const char *dev_name, void *data,
  665. struct vfsmount *mnt)
  666. {
  667. return get_sb_pseudo(fs_type, "infinibandevent:", NULL,
  668. INFINIBANDEVENTFS_MAGIC, mnt);
  669. }
  670. static struct file_system_type uverbs_event_fs = {
  671. /* No owner field so module can be unloaded */
  672. .name = "infinibandeventfs",
  673. .get_sb = uverbs_event_get_sb,
  674. .kill_sb = kill_litter_super
  675. };
  676. static int __init ib_uverbs_init(void)
  677. {
  678. int ret;
  679. ret = register_chrdev_region(IB_UVERBS_BASE_DEV, IB_UVERBS_MAX_DEVICES,
  680. "infiniband_verbs");
  681. if (ret) {
  682. printk(KERN_ERR "user_verbs: couldn't register device number\n");
  683. goto out;
  684. }
  685. uverbs_class = class_create(THIS_MODULE, "infiniband_verbs");
  686. if (IS_ERR(uverbs_class)) {
  687. ret = PTR_ERR(uverbs_class);
  688. printk(KERN_ERR "user_verbs: couldn't create class infiniband_verbs\n");
  689. goto out_chrdev;
  690. }
  691. ret = class_create_file(uverbs_class, &class_attr_abi_version);
  692. if (ret) {
  693. printk(KERN_ERR "user_verbs: couldn't create abi_version attribute\n");
  694. goto out_class;
  695. }
  696. ret = register_filesystem(&uverbs_event_fs);
  697. if (ret) {
  698. printk(KERN_ERR "user_verbs: couldn't register infinibandeventfs\n");
  699. goto out_class;
  700. }
  701. uverbs_event_mnt = kern_mount(&uverbs_event_fs);
  702. if (IS_ERR(uverbs_event_mnt)) {
  703. ret = PTR_ERR(uverbs_event_mnt);
  704. printk(KERN_ERR "user_verbs: couldn't mount infinibandeventfs\n");
  705. goto out_fs;
  706. }
  707. ret = ib_register_client(&uverbs_client);
  708. if (ret) {
  709. printk(KERN_ERR "user_verbs: couldn't register client\n");
  710. goto out_mnt;
  711. }
  712. return 0;
  713. out_mnt:
  714. mntput(uverbs_event_mnt);
  715. out_fs:
  716. unregister_filesystem(&uverbs_event_fs);
  717. out_class:
  718. class_destroy(uverbs_class);
  719. out_chrdev:
  720. unregister_chrdev_region(IB_UVERBS_BASE_DEV, IB_UVERBS_MAX_DEVICES);
  721. out:
  722. return ret;
  723. }
  724. static void __exit ib_uverbs_cleanup(void)
  725. {
  726. ib_unregister_client(&uverbs_client);
  727. mntput(uverbs_event_mnt);
  728. unregister_filesystem(&uverbs_event_fs);
  729. class_destroy(uverbs_class);
  730. unregister_chrdev_region(IB_UVERBS_BASE_DEV, IB_UVERBS_MAX_DEVICES);
  731. idr_destroy(&ib_uverbs_pd_idr);
  732. idr_destroy(&ib_uverbs_mr_idr);
  733. idr_destroy(&ib_uverbs_mw_idr);
  734. idr_destroy(&ib_uverbs_ah_idr);
  735. idr_destroy(&ib_uverbs_cq_idr);
  736. idr_destroy(&ib_uverbs_qp_idr);
  737. idr_destroy(&ib_uverbs_srq_idr);
  738. }
  739. module_init(ib_uverbs_init);
  740. module_exit(ib_uverbs_cleanup);