uverbs_main.c 24 KB

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