uverbs_main.c 24 KB

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