uverbs_main.c 21 KB

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