uverbs_main.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918
  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_MUTEX(ib_uverbs_idr_mutex);
  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. kfree(dev);
  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. mutex_lock(&ib_uverbs_idr_mutex);
  161. list_for_each_entry_safe(uobj, tmp, &context->ah_list, list) {
  162. struct ib_ah *ah = idr_find(&ib_uverbs_ah_idr, uobj->id);
  163. idr_remove(&ib_uverbs_ah_idr, uobj->id);
  164. ib_destroy_ah(ah);
  165. list_del(&uobj->list);
  166. kfree(uobj);
  167. }
  168. list_for_each_entry_safe(uobj, tmp, &context->qp_list, list) {
  169. struct ib_qp *qp = idr_find(&ib_uverbs_qp_idr, uobj->id);
  170. struct ib_uqp_object *uqp =
  171. container_of(uobj, struct ib_uqp_object, uevent.uobject);
  172. idr_remove(&ib_uverbs_qp_idr, uobj->id);
  173. ib_uverbs_detach_umcast(qp, uqp);
  174. ib_destroy_qp(qp);
  175. list_del(&uobj->list);
  176. ib_uverbs_release_uevent(file, &uqp->uevent);
  177. kfree(uqp);
  178. }
  179. list_for_each_entry_safe(uobj, tmp, &context->cq_list, list) {
  180. struct ib_cq *cq = idr_find(&ib_uverbs_cq_idr, uobj->id);
  181. struct ib_uverbs_event_file *ev_file = cq->cq_context;
  182. struct ib_ucq_object *ucq =
  183. container_of(uobj, struct ib_ucq_object, uobject);
  184. idr_remove(&ib_uverbs_cq_idr, uobj->id);
  185. ib_destroy_cq(cq);
  186. list_del(&uobj->list);
  187. ib_uverbs_release_ucq(file, ev_file, ucq);
  188. kfree(ucq);
  189. }
  190. list_for_each_entry_safe(uobj, tmp, &context->srq_list, list) {
  191. struct ib_srq *srq = idr_find(&ib_uverbs_srq_idr, uobj->id);
  192. struct ib_uevent_object *uevent =
  193. container_of(uobj, struct ib_uevent_object, uobject);
  194. idr_remove(&ib_uverbs_srq_idr, uobj->id);
  195. ib_destroy_srq(srq);
  196. list_del(&uobj->list);
  197. ib_uverbs_release_uevent(file, uevent);
  198. kfree(uevent);
  199. }
  200. /* XXX Free MWs */
  201. list_for_each_entry_safe(uobj, tmp, &context->mr_list, list) {
  202. struct ib_mr *mr = idr_find(&ib_uverbs_mr_idr, uobj->id);
  203. struct ib_device *mrdev = mr->device;
  204. struct ib_umem_object *memobj;
  205. idr_remove(&ib_uverbs_mr_idr, uobj->id);
  206. ib_dereg_mr(mr);
  207. memobj = container_of(uobj, struct ib_umem_object, uobject);
  208. ib_umem_release_on_close(mrdev, &memobj->umem);
  209. list_del(&uobj->list);
  210. kfree(memobj);
  211. }
  212. list_for_each_entry_safe(uobj, tmp, &context->pd_list, list) {
  213. struct ib_pd *pd = idr_find(&ib_uverbs_pd_idr, uobj->id);
  214. idr_remove(&ib_uverbs_pd_idr, uobj->id);
  215. ib_dealloc_pd(pd);
  216. list_del(&uobj->list);
  217. kfree(uobj);
  218. }
  219. mutex_unlock(&ib_uverbs_idr_mutex);
  220. return context->device->dealloc_ucontext(context);
  221. }
  222. static void ib_uverbs_release_file(struct kref *ref)
  223. {
  224. struct ib_uverbs_file *file =
  225. container_of(ref, struct ib_uverbs_file, ref);
  226. module_put(file->device->ib_dev->owner);
  227. kref_put(&file->device->ref, ib_uverbs_release_dev);
  228. kfree(file);
  229. }
  230. static ssize_t ib_uverbs_event_read(struct file *filp, char __user *buf,
  231. size_t count, loff_t *pos)
  232. {
  233. struct ib_uverbs_event_file *file = filp->private_data;
  234. struct ib_uverbs_event *event;
  235. int eventsz;
  236. int ret = 0;
  237. spin_lock_irq(&file->lock);
  238. while (list_empty(&file->event_list)) {
  239. spin_unlock_irq(&file->lock);
  240. if (filp->f_flags & O_NONBLOCK)
  241. return -EAGAIN;
  242. if (wait_event_interruptible(file->poll_wait,
  243. !list_empty(&file->event_list)))
  244. return -ERESTARTSYS;
  245. spin_lock_irq(&file->lock);
  246. }
  247. event = list_entry(file->event_list.next, struct ib_uverbs_event, list);
  248. if (file->is_async)
  249. eventsz = sizeof (struct ib_uverbs_async_event_desc);
  250. else
  251. eventsz = sizeof (struct ib_uverbs_comp_event_desc);
  252. if (eventsz > count) {
  253. ret = -EINVAL;
  254. event = NULL;
  255. } else {
  256. list_del(file->event_list.next);
  257. if (event->counter) {
  258. ++(*event->counter);
  259. list_del(&event->obj_list);
  260. }
  261. }
  262. spin_unlock_irq(&file->lock);
  263. if (event) {
  264. if (copy_to_user(buf, event, eventsz))
  265. ret = -EFAULT;
  266. else
  267. ret = eventsz;
  268. }
  269. kfree(event);
  270. return ret;
  271. }
  272. static unsigned int ib_uverbs_event_poll(struct file *filp,
  273. struct poll_table_struct *wait)
  274. {
  275. unsigned int pollflags = 0;
  276. struct ib_uverbs_event_file *file = filp->private_data;
  277. poll_wait(filp, &file->poll_wait, wait);
  278. spin_lock_irq(&file->lock);
  279. if (!list_empty(&file->event_list))
  280. pollflags = POLLIN | POLLRDNORM;
  281. spin_unlock_irq(&file->lock);
  282. return pollflags;
  283. }
  284. void ib_uverbs_release_event_file(struct kref *ref)
  285. {
  286. struct ib_uverbs_event_file *file =
  287. container_of(ref, struct ib_uverbs_event_file, ref);
  288. kfree(file);
  289. }
  290. static int ib_uverbs_event_fasync(int fd, struct file *filp, int on)
  291. {
  292. struct ib_uverbs_event_file *file = filp->private_data;
  293. return fasync_helper(fd, filp, on, &file->async_queue);
  294. }
  295. static int ib_uverbs_event_close(struct inode *inode, struct file *filp)
  296. {
  297. struct ib_uverbs_event_file *file = filp->private_data;
  298. struct ib_uverbs_event *entry, *tmp;
  299. spin_lock_irq(&file->lock);
  300. file->file = NULL;
  301. list_for_each_entry_safe(entry, tmp, &file->event_list, list) {
  302. if (entry->counter)
  303. list_del(&entry->obj_list);
  304. kfree(entry);
  305. }
  306. spin_unlock_irq(&file->lock);
  307. ib_uverbs_event_fasync(-1, filp, 0);
  308. if (file->is_async) {
  309. ib_unregister_event_handler(&file->uverbs_file->event_handler);
  310. kref_put(&file->uverbs_file->ref, ib_uverbs_release_file);
  311. }
  312. kref_put(&file->ref, ib_uverbs_release_event_file);
  313. return 0;
  314. }
  315. static struct file_operations uverbs_event_fops = {
  316. .owner = THIS_MODULE,
  317. .read = ib_uverbs_event_read,
  318. .poll = ib_uverbs_event_poll,
  319. .release = ib_uverbs_event_close,
  320. .fasync = ib_uverbs_event_fasync
  321. };
  322. void ib_uverbs_comp_handler(struct ib_cq *cq, void *cq_context)
  323. {
  324. struct ib_uverbs_event_file *file = cq_context;
  325. struct ib_ucq_object *uobj;
  326. struct ib_uverbs_event *entry;
  327. unsigned long flags;
  328. if (!file)
  329. return;
  330. spin_lock_irqsave(&file->lock, flags);
  331. if (!file->file) {
  332. spin_unlock_irqrestore(&file->lock, flags);
  333. return;
  334. }
  335. entry = kmalloc(sizeof *entry, GFP_ATOMIC);
  336. if (!entry) {
  337. spin_unlock_irqrestore(&file->lock, flags);
  338. return;
  339. }
  340. uobj = container_of(cq->uobject, struct ib_ucq_object, uobject);
  341. entry->desc.comp.cq_handle = cq->uobject->user_handle;
  342. entry->counter = &uobj->comp_events_reported;
  343. list_add_tail(&entry->list, &file->event_list);
  344. list_add_tail(&entry->obj_list, &uobj->comp_list);
  345. spin_unlock_irqrestore(&file->lock, flags);
  346. wake_up_interruptible(&file->poll_wait);
  347. kill_fasync(&file->async_queue, SIGIO, POLL_IN);
  348. }
  349. static void ib_uverbs_async_handler(struct ib_uverbs_file *file,
  350. __u64 element, __u64 event,
  351. struct list_head *obj_list,
  352. u32 *counter)
  353. {
  354. struct ib_uverbs_event *entry;
  355. unsigned long flags;
  356. spin_lock_irqsave(&file->async_file->lock, flags);
  357. if (!file->async_file->file) {
  358. spin_unlock_irqrestore(&file->async_file->lock, flags);
  359. return;
  360. }
  361. entry = kmalloc(sizeof *entry, GFP_ATOMIC);
  362. if (!entry) {
  363. spin_unlock_irqrestore(&file->async_file->lock, flags);
  364. return;
  365. }
  366. entry->desc.async.element = element;
  367. entry->desc.async.event_type = event;
  368. entry->counter = counter;
  369. list_add_tail(&entry->list, &file->async_file->event_list);
  370. if (obj_list)
  371. list_add_tail(&entry->obj_list, obj_list);
  372. spin_unlock_irqrestore(&file->async_file->lock, flags);
  373. wake_up_interruptible(&file->async_file->poll_wait);
  374. kill_fasync(&file->async_file->async_queue, SIGIO, POLL_IN);
  375. }
  376. void ib_uverbs_cq_event_handler(struct ib_event *event, void *context_ptr)
  377. {
  378. struct ib_ucq_object *uobj = container_of(event->element.cq->uobject,
  379. struct ib_ucq_object, uobject);
  380. ib_uverbs_async_handler(uobj->uverbs_file, uobj->uobject.user_handle,
  381. event->event, &uobj->async_list,
  382. &uobj->async_events_reported);
  383. }
  384. void ib_uverbs_qp_event_handler(struct ib_event *event, void *context_ptr)
  385. {
  386. struct ib_uevent_object *uobj;
  387. uobj = container_of(event->element.qp->uobject,
  388. struct ib_uevent_object, uobject);
  389. ib_uverbs_async_handler(context_ptr, uobj->uobject.user_handle,
  390. event->event, &uobj->event_list,
  391. &uobj->events_reported);
  392. }
  393. void ib_uverbs_srq_event_handler(struct ib_event *event, void *context_ptr)
  394. {
  395. struct ib_uevent_object *uobj;
  396. uobj = container_of(event->element.srq->uobject,
  397. struct ib_uevent_object, uobject);
  398. ib_uverbs_async_handler(context_ptr, uobj->uobject.user_handle,
  399. event->event, &uobj->event_list,
  400. &uobj->events_reported);
  401. }
  402. void ib_uverbs_event_handler(struct ib_event_handler *handler,
  403. struct ib_event *event)
  404. {
  405. struct ib_uverbs_file *file =
  406. container_of(handler, struct ib_uverbs_file, event_handler);
  407. ib_uverbs_async_handler(file, event->element.port_num, event->event,
  408. NULL, NULL);
  409. }
  410. struct file *ib_uverbs_alloc_event_file(struct ib_uverbs_file *uverbs_file,
  411. int is_async, int *fd)
  412. {
  413. struct ib_uverbs_event_file *ev_file;
  414. struct file *filp;
  415. int ret;
  416. ev_file = kmalloc(sizeof *ev_file, GFP_KERNEL);
  417. if (!ev_file)
  418. return ERR_PTR(-ENOMEM);
  419. kref_init(&ev_file->ref);
  420. spin_lock_init(&ev_file->lock);
  421. INIT_LIST_HEAD(&ev_file->event_list);
  422. init_waitqueue_head(&ev_file->poll_wait);
  423. ev_file->uverbs_file = uverbs_file;
  424. ev_file->async_queue = NULL;
  425. ev_file->is_async = is_async;
  426. *fd = get_unused_fd();
  427. if (*fd < 0) {
  428. ret = *fd;
  429. goto err;
  430. }
  431. filp = get_empty_filp();
  432. if (!filp) {
  433. ret = -ENFILE;
  434. goto err_fd;
  435. }
  436. ev_file->file = filp;
  437. /*
  438. * fops_get() can't fail here, because we're coming from a
  439. * system call on a uverbs file, which will already have a
  440. * module reference.
  441. */
  442. filp->f_op = fops_get(&uverbs_event_fops);
  443. filp->f_vfsmnt = mntget(uverbs_event_mnt);
  444. filp->f_dentry = dget(uverbs_event_mnt->mnt_root);
  445. filp->f_mapping = filp->f_dentry->d_inode->i_mapping;
  446. filp->f_flags = O_RDONLY;
  447. filp->f_mode = FMODE_READ;
  448. filp->private_data = ev_file;
  449. return filp;
  450. err_fd:
  451. put_unused_fd(*fd);
  452. err:
  453. kfree(ev_file);
  454. return ERR_PTR(ret);
  455. }
  456. /*
  457. * Look up a completion event file by FD. If lookup is successful,
  458. * takes a ref to the event file struct that it returns; if
  459. * unsuccessful, returns NULL.
  460. */
  461. struct ib_uverbs_event_file *ib_uverbs_lookup_comp_file(int fd)
  462. {
  463. struct ib_uverbs_event_file *ev_file = NULL;
  464. struct file *filp;
  465. filp = fget(fd);
  466. if (!filp)
  467. return NULL;
  468. if (filp->f_op != &uverbs_event_fops)
  469. goto out;
  470. ev_file = filp->private_data;
  471. if (ev_file->is_async) {
  472. ev_file = NULL;
  473. goto out;
  474. }
  475. kref_get(&ev_file->ref);
  476. out:
  477. fput(filp);
  478. return ev_file;
  479. }
  480. static ssize_t ib_uverbs_write(struct file *filp, const char __user *buf,
  481. size_t count, loff_t *pos)
  482. {
  483. struct ib_uverbs_file *file = filp->private_data;
  484. struct ib_uverbs_cmd_hdr hdr;
  485. if (count < sizeof hdr)
  486. return -EINVAL;
  487. if (copy_from_user(&hdr, buf, sizeof hdr))
  488. return -EFAULT;
  489. if (hdr.in_words * 4 != count)
  490. return -EINVAL;
  491. if (hdr.command < 0 ||
  492. hdr.command >= ARRAY_SIZE(uverbs_cmd_table) ||
  493. !uverbs_cmd_table[hdr.command] ||
  494. !(file->device->ib_dev->uverbs_cmd_mask & (1ull << hdr.command)))
  495. return -EINVAL;
  496. if (!file->ucontext &&
  497. hdr.command != IB_USER_VERBS_CMD_GET_CONTEXT)
  498. return -EINVAL;
  499. return uverbs_cmd_table[hdr.command](file, buf + sizeof hdr,
  500. hdr.in_words * 4, hdr.out_words * 4);
  501. }
  502. static int ib_uverbs_mmap(struct file *filp, struct vm_area_struct *vma)
  503. {
  504. struct ib_uverbs_file *file = filp->private_data;
  505. if (!file->ucontext)
  506. return -ENODEV;
  507. else
  508. return file->device->ib_dev->mmap(file->ucontext, vma);
  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 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 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 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, "%s\n", dev->ib_dev->name);
  577. }
  578. static CLASS_DEVICE_ATTR(ibdev, S_IRUGO, show_ibdev, NULL);
  579. static ssize_t show_dev_abi_version(struct class_device *class_dev, char *buf)
  580. {
  581. struct ib_uverbs_device *dev = class_get_devdata(class_dev);
  582. if (!dev)
  583. return -ENODEV;
  584. return sprintf(buf, "%d\n", dev->ib_dev->uverbs_abi_ver);
  585. }
  586. static CLASS_DEVICE_ATTR(abi_version, S_IRUGO, show_dev_abi_version, NULL);
  587. static ssize_t show_abi_version(struct class *class, char *buf)
  588. {
  589. return sprintf(buf, "%d\n", IB_USER_VERBS_ABI_VERSION);
  590. }
  591. static CLASS_ATTR(abi_version, S_IRUGO, show_abi_version, NULL);
  592. static void ib_uverbs_add_one(struct ib_device *device)
  593. {
  594. struct ib_uverbs_device *uverbs_dev;
  595. if (!device->alloc_ucontext)
  596. return;
  597. uverbs_dev = kzalloc(sizeof *uverbs_dev, GFP_KERNEL);
  598. if (!uverbs_dev)
  599. return;
  600. kref_init(&uverbs_dev->ref);
  601. spin_lock(&map_lock);
  602. uverbs_dev->devnum = find_first_zero_bit(dev_map, IB_UVERBS_MAX_DEVICES);
  603. if (uverbs_dev->devnum >= IB_UVERBS_MAX_DEVICES) {
  604. spin_unlock(&map_lock);
  605. goto err;
  606. }
  607. set_bit(uverbs_dev->devnum, dev_map);
  608. spin_unlock(&map_lock);
  609. uverbs_dev->ib_dev = device;
  610. uverbs_dev->num_comp_vectors = 1;
  611. uverbs_dev->dev = cdev_alloc();
  612. if (!uverbs_dev->dev)
  613. goto err;
  614. uverbs_dev->dev->owner = THIS_MODULE;
  615. uverbs_dev->dev->ops = device->mmap ? &uverbs_mmap_fops : &uverbs_fops;
  616. kobject_set_name(&uverbs_dev->dev->kobj, "uverbs%d", uverbs_dev->devnum);
  617. if (cdev_add(uverbs_dev->dev, IB_UVERBS_BASE_DEV + uverbs_dev->devnum, 1))
  618. goto err_cdev;
  619. uverbs_dev->class_dev = class_device_create(uverbs_class, NULL,
  620. uverbs_dev->dev->dev,
  621. device->dma_device,
  622. "uverbs%d", uverbs_dev->devnum);
  623. if (IS_ERR(uverbs_dev->class_dev))
  624. goto err_cdev;
  625. class_set_devdata(uverbs_dev->class_dev, uverbs_dev);
  626. if (class_device_create_file(uverbs_dev->class_dev, &class_device_attr_ibdev))
  627. goto err_class;
  628. if (class_device_create_file(uverbs_dev->class_dev, &class_device_attr_abi_version))
  629. goto err_class;
  630. spin_lock(&map_lock);
  631. dev_table[uverbs_dev->devnum] = uverbs_dev;
  632. spin_unlock(&map_lock);
  633. ib_set_client_data(device, &uverbs_client, uverbs_dev);
  634. return;
  635. err_class:
  636. class_device_destroy(uverbs_class, uverbs_dev->dev->dev);
  637. err_cdev:
  638. cdev_del(uverbs_dev->dev);
  639. clear_bit(uverbs_dev->devnum, dev_map);
  640. err:
  641. kref_put(&uverbs_dev->ref, ib_uverbs_release_dev);
  642. return;
  643. }
  644. static void ib_uverbs_remove_one(struct ib_device *device)
  645. {
  646. struct ib_uverbs_device *uverbs_dev = ib_get_client_data(device, &uverbs_client);
  647. if (!uverbs_dev)
  648. return;
  649. class_set_devdata(uverbs_dev->class_dev, NULL);
  650. class_device_destroy(uverbs_class, uverbs_dev->dev->dev);
  651. cdev_del(uverbs_dev->dev);
  652. spin_lock(&map_lock);
  653. dev_table[uverbs_dev->devnum] = NULL;
  654. spin_unlock(&map_lock);
  655. clear_bit(uverbs_dev->devnum, dev_map);
  656. kref_put(&uverbs_dev->ref, ib_uverbs_release_dev);
  657. }
  658. static struct super_block *uverbs_event_get_sb(struct file_system_type *fs_type, int flags,
  659. const char *dev_name, void *data)
  660. {
  661. return get_sb_pseudo(fs_type, "infinibandevent:", NULL,
  662. INFINIBANDEVENTFS_MAGIC);
  663. }
  664. static struct file_system_type uverbs_event_fs = {
  665. /* No owner field so module can be unloaded */
  666. .name = "infinibandeventfs",
  667. .get_sb = uverbs_event_get_sb,
  668. .kill_sb = kill_litter_super
  669. };
  670. static int __init ib_uverbs_init(void)
  671. {
  672. int ret;
  673. spin_lock_init(&map_lock);
  674. ret = register_chrdev_region(IB_UVERBS_BASE_DEV, IB_UVERBS_MAX_DEVICES,
  675. "infiniband_verbs");
  676. if (ret) {
  677. printk(KERN_ERR "user_verbs: couldn't register device number\n");
  678. goto out;
  679. }
  680. uverbs_class = class_create(THIS_MODULE, "infiniband_verbs");
  681. if (IS_ERR(uverbs_class)) {
  682. ret = PTR_ERR(uverbs_class);
  683. printk(KERN_ERR "user_verbs: couldn't create class infiniband_verbs\n");
  684. goto out_chrdev;
  685. }
  686. ret = class_create_file(uverbs_class, &class_attr_abi_version);
  687. if (ret) {
  688. printk(KERN_ERR "user_verbs: couldn't create abi_version attribute\n");
  689. goto out_class;
  690. }
  691. ret = register_filesystem(&uverbs_event_fs);
  692. if (ret) {
  693. printk(KERN_ERR "user_verbs: couldn't register infinibandeventfs\n");
  694. goto out_class;
  695. }
  696. uverbs_event_mnt = kern_mount(&uverbs_event_fs);
  697. if (IS_ERR(uverbs_event_mnt)) {
  698. ret = PTR_ERR(uverbs_event_mnt);
  699. printk(KERN_ERR "user_verbs: couldn't mount infinibandeventfs\n");
  700. goto out_fs;
  701. }
  702. ret = ib_register_client(&uverbs_client);
  703. if (ret) {
  704. printk(KERN_ERR "user_verbs: couldn't register client\n");
  705. goto out_mnt;
  706. }
  707. return 0;
  708. out_mnt:
  709. mntput(uverbs_event_mnt);
  710. out_fs:
  711. unregister_filesystem(&uverbs_event_fs);
  712. out_class:
  713. class_destroy(uverbs_class);
  714. out_chrdev:
  715. unregister_chrdev_region(IB_UVERBS_BASE_DEV, IB_UVERBS_MAX_DEVICES);
  716. out:
  717. return ret;
  718. }
  719. static void __exit ib_uverbs_cleanup(void)
  720. {
  721. ib_unregister_client(&uverbs_client);
  722. mntput(uverbs_event_mnt);
  723. unregister_filesystem(&uverbs_event_fs);
  724. class_destroy(uverbs_class);
  725. unregister_chrdev_region(IB_UVERBS_BASE_DEV, IB_UVERBS_MAX_DEVICES);
  726. flush_scheduled_work();
  727. idr_destroy(&ib_uverbs_pd_idr);
  728. idr_destroy(&ib_uverbs_mr_idr);
  729. idr_destroy(&ib_uverbs_mw_idr);
  730. idr_destroy(&ib_uverbs_ah_idr);
  731. idr_destroy(&ib_uverbs_cq_idr);
  732. idr_destroy(&ib_uverbs_qp_idr);
  733. idr_destroy(&ib_uverbs_srq_idr);
  734. }
  735. module_init(ib_uverbs_init);
  736. module_exit(ib_uverbs_cleanup);