uverbs_main.c 24 KB

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