uverbs_main.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698
  1. /*
  2. * Copyright (c) 2005 Topspin Communications. All rights reserved.
  3. * Copyright (c) 2005 Cisco Systems. All rights reserved.
  4. *
  5. * This software is available to you under a choice of one of two
  6. * licenses. You may choose to be licensed under the terms of the GNU
  7. * General Public License (GPL) Version 2, available from the file
  8. * COPYING in the main directory of this source tree, or the
  9. * OpenIB.org BSD license below:
  10. *
  11. * Redistribution and use in source and binary forms, with or
  12. * without modification, are permitted provided that the following
  13. * conditions are met:
  14. *
  15. * - Redistributions of source code must retain the above
  16. * copyright notice, this list of conditions and the following
  17. * disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials
  22. * provided with the distribution.
  23. *
  24. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  25. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  26. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  27. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  28. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  29. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  30. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  31. * SOFTWARE.
  32. *
  33. * $Id: uverbs_main.c 2733 2005-06-28 19:14:34Z roland $
  34. */
  35. #include <linux/module.h>
  36. #include <linux/init.h>
  37. #include <linux/device.h>
  38. #include <linux/err.h>
  39. #include <linux/fs.h>
  40. #include <linux/poll.h>
  41. #include <linux/file.h>
  42. #include <linux/mount.h>
  43. #include <asm/uaccess.h>
  44. #include "uverbs.h"
  45. MODULE_AUTHOR("Roland Dreier");
  46. MODULE_DESCRIPTION("InfiniBand userspace verbs access");
  47. MODULE_LICENSE("Dual BSD/GPL");
  48. #define INFINIBANDEVENTFS_MAGIC 0x49426576 /* "IBev" */
  49. enum {
  50. IB_UVERBS_MAJOR = 231,
  51. IB_UVERBS_BASE_MINOR = 192,
  52. IB_UVERBS_MAX_DEVICES = 32
  53. };
  54. #define IB_UVERBS_BASE_DEV MKDEV(IB_UVERBS_MAJOR, IB_UVERBS_BASE_MINOR)
  55. DECLARE_MUTEX(ib_uverbs_idr_mutex);
  56. DEFINE_IDR(ib_uverbs_pd_idr);
  57. DEFINE_IDR(ib_uverbs_mr_idr);
  58. DEFINE_IDR(ib_uverbs_mw_idr);
  59. DEFINE_IDR(ib_uverbs_ah_idr);
  60. DEFINE_IDR(ib_uverbs_cq_idr);
  61. DEFINE_IDR(ib_uverbs_qp_idr);
  62. static spinlock_t map_lock;
  63. static DECLARE_BITMAP(dev_map, IB_UVERBS_MAX_DEVICES);
  64. static ssize_t (*uverbs_cmd_table[])(struct ib_uverbs_file *file,
  65. const char __user *buf, int in_len,
  66. int out_len) = {
  67. [IB_USER_VERBS_CMD_QUERY_PARAMS] = ib_uverbs_query_params,
  68. [IB_USER_VERBS_CMD_GET_CONTEXT] = ib_uverbs_get_context,
  69. [IB_USER_VERBS_CMD_QUERY_DEVICE] = ib_uverbs_query_device,
  70. [IB_USER_VERBS_CMD_QUERY_PORT] = ib_uverbs_query_port,
  71. [IB_USER_VERBS_CMD_QUERY_GID] = ib_uverbs_query_gid,
  72. [IB_USER_VERBS_CMD_QUERY_PKEY] = ib_uverbs_query_pkey,
  73. [IB_USER_VERBS_CMD_ALLOC_PD] = ib_uverbs_alloc_pd,
  74. [IB_USER_VERBS_CMD_DEALLOC_PD] = ib_uverbs_dealloc_pd,
  75. [IB_USER_VERBS_CMD_REG_MR] = ib_uverbs_reg_mr,
  76. [IB_USER_VERBS_CMD_DEREG_MR] = ib_uverbs_dereg_mr,
  77. [IB_USER_VERBS_CMD_CREATE_CQ] = ib_uverbs_create_cq,
  78. [IB_USER_VERBS_CMD_DESTROY_CQ] = ib_uverbs_destroy_cq,
  79. [IB_USER_VERBS_CMD_CREATE_QP] = ib_uverbs_create_qp,
  80. [IB_USER_VERBS_CMD_MODIFY_QP] = ib_uverbs_modify_qp,
  81. [IB_USER_VERBS_CMD_DESTROY_QP] = ib_uverbs_destroy_qp,
  82. [IB_USER_VERBS_CMD_ATTACH_MCAST] = ib_uverbs_attach_mcast,
  83. [IB_USER_VERBS_CMD_DETACH_MCAST] = ib_uverbs_detach_mcast,
  84. };
  85. static struct vfsmount *uverbs_event_mnt;
  86. static void ib_uverbs_add_one(struct ib_device *device);
  87. static void ib_uverbs_remove_one(struct ib_device *device);
  88. static int ib_dealloc_ucontext(struct ib_ucontext *context)
  89. {
  90. struct ib_uobject *uobj, *tmp;
  91. if (!context)
  92. return 0;
  93. down(&ib_uverbs_idr_mutex);
  94. /* XXX Free AHs */
  95. list_for_each_entry_safe(uobj, tmp, &context->qp_list, list) {
  96. struct ib_qp *qp = idr_find(&ib_uverbs_qp_idr, uobj->id);
  97. idr_remove(&ib_uverbs_qp_idr, uobj->id);
  98. ib_destroy_qp(qp);
  99. list_del(&uobj->list);
  100. kfree(uobj);
  101. }
  102. list_for_each_entry_safe(uobj, tmp, &context->cq_list, list) {
  103. struct ib_cq *cq = idr_find(&ib_uverbs_cq_idr, uobj->id);
  104. idr_remove(&ib_uverbs_cq_idr, uobj->id);
  105. ib_destroy_cq(cq);
  106. list_del(&uobj->list);
  107. kfree(uobj);
  108. }
  109. /* XXX Free SRQs */
  110. /* XXX Free MWs */
  111. list_for_each_entry_safe(uobj, tmp, &context->mr_list, list) {
  112. struct ib_mr *mr = idr_find(&ib_uverbs_mr_idr, uobj->id);
  113. struct ib_umem_object *memobj;
  114. idr_remove(&ib_uverbs_mr_idr, uobj->id);
  115. ib_dereg_mr(mr);
  116. memobj = container_of(uobj, struct ib_umem_object, uobject);
  117. ib_umem_release_on_close(mr->device, &memobj->umem);
  118. list_del(&uobj->list);
  119. kfree(memobj);
  120. }
  121. list_for_each_entry_safe(uobj, tmp, &context->pd_list, list) {
  122. struct ib_pd *pd = idr_find(&ib_uverbs_pd_idr, uobj->id);
  123. idr_remove(&ib_uverbs_pd_idr, uobj->id);
  124. ib_dealloc_pd(pd);
  125. list_del(&uobj->list);
  126. kfree(uobj);
  127. }
  128. up(&ib_uverbs_idr_mutex);
  129. return context->device->dealloc_ucontext(context);
  130. }
  131. static void ib_uverbs_release_file(struct kref *ref)
  132. {
  133. struct ib_uverbs_file *file =
  134. container_of(ref, struct ib_uverbs_file, ref);
  135. module_put(file->device->ib_dev->owner);
  136. kfree(file);
  137. }
  138. static ssize_t ib_uverbs_event_read(struct file *filp, char __user *buf,
  139. size_t count, loff_t *pos)
  140. {
  141. struct ib_uverbs_event_file *file = filp->private_data;
  142. void *event;
  143. int eventsz;
  144. int ret = 0;
  145. spin_lock_irq(&file->lock);
  146. while (list_empty(&file->event_list) && file->fd >= 0) {
  147. spin_unlock_irq(&file->lock);
  148. if (filp->f_flags & O_NONBLOCK)
  149. return -EAGAIN;
  150. if (wait_event_interruptible(file->poll_wait,
  151. !list_empty(&file->event_list) ||
  152. file->fd < 0))
  153. return -ERESTARTSYS;
  154. spin_lock_irq(&file->lock);
  155. }
  156. if (file->fd < 0) {
  157. spin_unlock_irq(&file->lock);
  158. return -ENODEV;
  159. }
  160. if (file->is_async) {
  161. event = list_entry(file->event_list.next,
  162. struct ib_uverbs_async_event, list);
  163. eventsz = sizeof (struct ib_uverbs_async_event_desc);
  164. } else {
  165. event = list_entry(file->event_list.next,
  166. struct ib_uverbs_comp_event, list);
  167. eventsz = sizeof (struct ib_uverbs_comp_event_desc);
  168. }
  169. if (eventsz > count) {
  170. ret = -EINVAL;
  171. event = NULL;
  172. } else
  173. list_del(file->event_list.next);
  174. spin_unlock_irq(&file->lock);
  175. if (event) {
  176. if (copy_to_user(buf, event, eventsz))
  177. ret = -EFAULT;
  178. else
  179. ret = eventsz;
  180. }
  181. kfree(event);
  182. return ret;
  183. }
  184. static unsigned int ib_uverbs_event_poll(struct file *filp,
  185. struct poll_table_struct *wait)
  186. {
  187. unsigned int pollflags = 0;
  188. struct ib_uverbs_event_file *file = filp->private_data;
  189. poll_wait(filp, &file->poll_wait, wait);
  190. spin_lock_irq(&file->lock);
  191. if (file->fd < 0)
  192. pollflags = POLLERR;
  193. else if (!list_empty(&file->event_list))
  194. pollflags = POLLIN | POLLRDNORM;
  195. spin_unlock_irq(&file->lock);
  196. return pollflags;
  197. }
  198. static void ib_uverbs_event_release(struct ib_uverbs_event_file *file)
  199. {
  200. struct list_head *entry, *tmp;
  201. spin_lock_irq(&file->lock);
  202. if (file->fd != -1) {
  203. file->fd = -1;
  204. list_for_each_safe(entry, tmp, &file->event_list)
  205. if (file->is_async)
  206. kfree(list_entry(entry, struct ib_uverbs_async_event, list));
  207. else
  208. kfree(list_entry(entry, struct ib_uverbs_comp_event, list));
  209. }
  210. spin_unlock_irq(&file->lock);
  211. }
  212. static int ib_uverbs_event_close(struct inode *inode, struct file *filp)
  213. {
  214. struct ib_uverbs_event_file *file = filp->private_data;
  215. ib_uverbs_event_release(file);
  216. kref_put(&file->uverbs_file->ref, ib_uverbs_release_file);
  217. return 0;
  218. }
  219. static struct file_operations uverbs_event_fops = {
  220. /*
  221. * No .owner field since we artificially create event files,
  222. * so there is no increment to the module reference count in
  223. * the open path. All event files come from a uverbs command
  224. * file, which already takes a module reference, so this is OK.
  225. */
  226. .read = ib_uverbs_event_read,
  227. .poll = ib_uverbs_event_poll,
  228. .release = ib_uverbs_event_close
  229. };
  230. void ib_uverbs_comp_handler(struct ib_cq *cq, void *cq_context)
  231. {
  232. struct ib_uverbs_file *file = cq_context;
  233. struct ib_uverbs_comp_event *entry;
  234. unsigned long flags;
  235. entry = kmalloc(sizeof *entry, GFP_ATOMIC);
  236. if (!entry)
  237. return;
  238. entry->desc.cq_handle = cq->uobject->user_handle;
  239. spin_lock_irqsave(&file->comp_file[0].lock, flags);
  240. list_add_tail(&entry->list, &file->comp_file[0].event_list);
  241. spin_unlock_irqrestore(&file->comp_file[0].lock, flags);
  242. wake_up_interruptible(&file->comp_file[0].poll_wait);
  243. }
  244. static void ib_uverbs_async_handler(struct ib_uverbs_file *file,
  245. __u64 element, __u64 event)
  246. {
  247. struct ib_uverbs_async_event *entry;
  248. unsigned long flags;
  249. entry = kmalloc(sizeof *entry, GFP_ATOMIC);
  250. if (!entry)
  251. return;
  252. entry->desc.element = element;
  253. entry->desc.event_type = event;
  254. spin_lock_irqsave(&file->async_file.lock, flags);
  255. list_add_tail(&entry->list, &file->async_file.event_list);
  256. spin_unlock_irqrestore(&file->async_file.lock, flags);
  257. wake_up_interruptible(&file->async_file.poll_wait);
  258. }
  259. void ib_uverbs_cq_event_handler(struct ib_event *event, void *context_ptr)
  260. {
  261. ib_uverbs_async_handler(context_ptr,
  262. event->element.cq->uobject->user_handle,
  263. event->event);
  264. }
  265. void ib_uverbs_qp_event_handler(struct ib_event *event, void *context_ptr)
  266. {
  267. ib_uverbs_async_handler(context_ptr,
  268. event->element.qp->uobject->user_handle,
  269. event->event);
  270. }
  271. static void ib_uverbs_event_handler(struct ib_event_handler *handler,
  272. struct ib_event *event)
  273. {
  274. struct ib_uverbs_file *file =
  275. container_of(handler, struct ib_uverbs_file, event_handler);
  276. ib_uverbs_async_handler(file, event->element.port_num, event->event);
  277. }
  278. static int ib_uverbs_event_init(struct ib_uverbs_event_file *file,
  279. struct ib_uverbs_file *uverbs_file)
  280. {
  281. struct file *filp;
  282. spin_lock_init(&file->lock);
  283. INIT_LIST_HEAD(&file->event_list);
  284. init_waitqueue_head(&file->poll_wait);
  285. file->uverbs_file = uverbs_file;
  286. file->fd = get_unused_fd();
  287. if (file->fd < 0)
  288. return file->fd;
  289. filp = get_empty_filp();
  290. if (!filp) {
  291. put_unused_fd(file->fd);
  292. return -ENFILE;
  293. }
  294. filp->f_op = &uverbs_event_fops;
  295. filp->f_vfsmnt = mntget(uverbs_event_mnt);
  296. filp->f_dentry = dget(uverbs_event_mnt->mnt_root);
  297. filp->f_mapping = filp->f_dentry->d_inode->i_mapping;
  298. filp->f_flags = O_RDONLY;
  299. filp->f_mode = FMODE_READ;
  300. filp->private_data = file;
  301. fd_install(file->fd, filp);
  302. return 0;
  303. }
  304. static ssize_t ib_uverbs_write(struct file *filp, const char __user *buf,
  305. size_t count, loff_t *pos)
  306. {
  307. struct ib_uverbs_file *file = filp->private_data;
  308. struct ib_uverbs_cmd_hdr hdr;
  309. if (count < sizeof hdr)
  310. return -EINVAL;
  311. if (copy_from_user(&hdr, buf, sizeof hdr))
  312. return -EFAULT;
  313. if (hdr.in_words * 4 != count)
  314. return -EINVAL;
  315. if (hdr.command < 0 || hdr.command >= ARRAY_SIZE(uverbs_cmd_table))
  316. return -EINVAL;
  317. if (!file->ucontext &&
  318. hdr.command != IB_USER_VERBS_CMD_QUERY_PARAMS &&
  319. hdr.command != IB_USER_VERBS_CMD_GET_CONTEXT)
  320. return -EINVAL;
  321. return uverbs_cmd_table[hdr.command](file, buf + sizeof hdr,
  322. hdr.in_words * 4, hdr.out_words * 4);
  323. }
  324. static int ib_uverbs_mmap(struct file *filp, struct vm_area_struct *vma)
  325. {
  326. struct ib_uverbs_file *file = filp->private_data;
  327. if (!file->ucontext)
  328. return -ENODEV;
  329. else
  330. return file->device->ib_dev->mmap(file->ucontext, vma);
  331. }
  332. static int ib_uverbs_open(struct inode *inode, struct file *filp)
  333. {
  334. struct ib_uverbs_device *dev =
  335. container_of(inode->i_cdev, struct ib_uverbs_device, dev);
  336. struct ib_uverbs_file *file;
  337. int i = 0;
  338. int ret;
  339. if (!try_module_get(dev->ib_dev->owner))
  340. return -ENODEV;
  341. file = kmalloc(sizeof *file +
  342. (dev->num_comp - 1) * sizeof (struct ib_uverbs_event_file),
  343. GFP_KERNEL);
  344. if (!file)
  345. return -ENOMEM;
  346. file->device = dev;
  347. kref_init(&file->ref);
  348. file->ucontext = NULL;
  349. ret = ib_uverbs_event_init(&file->async_file, file);
  350. if (ret)
  351. goto err;
  352. file->async_file.is_async = 1;
  353. kref_get(&file->ref);
  354. for (i = 0; i < dev->num_comp; ++i) {
  355. ret = ib_uverbs_event_init(&file->comp_file[i], file);
  356. if (ret)
  357. goto err_async;
  358. kref_get(&file->ref);
  359. file->comp_file[i].is_async = 0;
  360. }
  361. filp->private_data = file;
  362. INIT_IB_EVENT_HANDLER(&file->event_handler, dev->ib_dev,
  363. ib_uverbs_event_handler);
  364. if (ib_register_event_handler(&file->event_handler))
  365. goto err_async;
  366. return 0;
  367. err_async:
  368. while (i--)
  369. ib_uverbs_event_release(&file->comp_file[i]);
  370. ib_uverbs_event_release(&file->async_file);
  371. err:
  372. kref_put(&file->ref, ib_uverbs_release_file);
  373. return ret;
  374. }
  375. static int ib_uverbs_close(struct inode *inode, struct file *filp)
  376. {
  377. struct ib_uverbs_file *file = filp->private_data;
  378. int i;
  379. ib_unregister_event_handler(&file->event_handler);
  380. ib_uverbs_event_release(&file->async_file);
  381. ib_dealloc_ucontext(file->ucontext);
  382. for (i = 0; i < file->device->num_comp; ++i)
  383. ib_uverbs_event_release(&file->comp_file[i]);
  384. kref_put(&file->ref, ib_uverbs_release_file);
  385. return 0;
  386. }
  387. static struct file_operations uverbs_fops = {
  388. .owner = THIS_MODULE,
  389. .write = ib_uverbs_write,
  390. .open = ib_uverbs_open,
  391. .release = ib_uverbs_close
  392. };
  393. static struct file_operations uverbs_mmap_fops = {
  394. .owner = THIS_MODULE,
  395. .write = ib_uverbs_write,
  396. .mmap = ib_uverbs_mmap,
  397. .open = ib_uverbs_open,
  398. .release = ib_uverbs_close
  399. };
  400. static struct ib_client uverbs_client = {
  401. .name = "uverbs",
  402. .add = ib_uverbs_add_one,
  403. .remove = ib_uverbs_remove_one
  404. };
  405. static ssize_t show_ibdev(struct class_device *class_dev, char *buf)
  406. {
  407. struct ib_uverbs_device *dev =
  408. container_of(class_dev, struct ib_uverbs_device, class_dev);
  409. return sprintf(buf, "%s\n", dev->ib_dev->name);
  410. }
  411. static CLASS_DEVICE_ATTR(ibdev, S_IRUGO, show_ibdev, NULL);
  412. static void ib_uverbs_release_class_dev(struct class_device *class_dev)
  413. {
  414. struct ib_uverbs_device *dev =
  415. container_of(class_dev, struct ib_uverbs_device, class_dev);
  416. cdev_del(&dev->dev);
  417. clear_bit(dev->devnum, dev_map);
  418. kfree(dev);
  419. }
  420. static struct class uverbs_class = {
  421. .name = "infiniband_verbs",
  422. .release = ib_uverbs_release_class_dev
  423. };
  424. static ssize_t show_abi_version(struct class *class, char *buf)
  425. {
  426. return sprintf(buf, "%d\n", IB_USER_VERBS_ABI_VERSION);
  427. }
  428. static CLASS_ATTR(abi_version, S_IRUGO, show_abi_version, NULL);
  429. static void ib_uverbs_add_one(struct ib_device *device)
  430. {
  431. struct ib_uverbs_device *uverbs_dev;
  432. if (!device->alloc_ucontext)
  433. return;
  434. uverbs_dev = kmalloc(sizeof *uverbs_dev, GFP_KERNEL);
  435. if (!uverbs_dev)
  436. return;
  437. memset(uverbs_dev, 0, sizeof *uverbs_dev);
  438. spin_lock(&map_lock);
  439. uverbs_dev->devnum = find_first_zero_bit(dev_map, IB_UVERBS_MAX_DEVICES);
  440. if (uverbs_dev->devnum >= IB_UVERBS_MAX_DEVICES) {
  441. spin_unlock(&map_lock);
  442. goto err;
  443. }
  444. set_bit(uverbs_dev->devnum, dev_map);
  445. spin_unlock(&map_lock);
  446. uverbs_dev->ib_dev = device;
  447. uverbs_dev->num_comp = 1;
  448. if (device->mmap)
  449. cdev_init(&uverbs_dev->dev, &uverbs_mmap_fops);
  450. else
  451. cdev_init(&uverbs_dev->dev, &uverbs_fops);
  452. uverbs_dev->dev.owner = THIS_MODULE;
  453. kobject_set_name(&uverbs_dev->dev.kobj, "uverbs%d", uverbs_dev->devnum);
  454. if (cdev_add(&uverbs_dev->dev, IB_UVERBS_BASE_DEV + uverbs_dev->devnum, 1))
  455. goto err;
  456. uverbs_dev->class_dev.class = &uverbs_class;
  457. uverbs_dev->class_dev.dev = device->dma_device;
  458. uverbs_dev->class_dev.devt = uverbs_dev->dev.dev;
  459. snprintf(uverbs_dev->class_dev.class_id, BUS_ID_SIZE, "uverbs%d", uverbs_dev->devnum);
  460. if (class_device_register(&uverbs_dev->class_dev))
  461. goto err_cdev;
  462. if (class_device_create_file(&uverbs_dev->class_dev, &class_device_attr_ibdev))
  463. goto err_class;
  464. ib_set_client_data(device, &uverbs_client, uverbs_dev);
  465. return;
  466. err_class:
  467. class_device_unregister(&uverbs_dev->class_dev);
  468. err_cdev:
  469. cdev_del(&uverbs_dev->dev);
  470. clear_bit(uverbs_dev->devnum, dev_map);
  471. err:
  472. kfree(uverbs_dev);
  473. return;
  474. }
  475. static void ib_uverbs_remove_one(struct ib_device *device)
  476. {
  477. struct ib_uverbs_device *uverbs_dev = ib_get_client_data(device, &uverbs_client);
  478. if (!uverbs_dev)
  479. return;
  480. class_device_unregister(&uverbs_dev->class_dev);
  481. }
  482. static struct super_block *uverbs_event_get_sb(struct file_system_type *fs_type, int flags,
  483. const char *dev_name, void *data)
  484. {
  485. return get_sb_pseudo(fs_type, "infinibandevent:", NULL,
  486. INFINIBANDEVENTFS_MAGIC);
  487. }
  488. static struct file_system_type uverbs_event_fs = {
  489. /* No owner field so module can be unloaded */
  490. .name = "infinibandeventfs",
  491. .get_sb = uverbs_event_get_sb,
  492. .kill_sb = kill_litter_super
  493. };
  494. static int __init ib_uverbs_init(void)
  495. {
  496. int ret;
  497. spin_lock_init(&map_lock);
  498. ret = register_chrdev_region(IB_UVERBS_BASE_DEV, IB_UVERBS_MAX_DEVICES,
  499. "infiniband_verbs");
  500. if (ret) {
  501. printk(KERN_ERR "user_verbs: couldn't register device number\n");
  502. goto out;
  503. }
  504. ret = class_register(&uverbs_class);
  505. if (ret) {
  506. printk(KERN_ERR "user_verbs: couldn't create class infiniband_verbs\n");
  507. goto out_chrdev;
  508. }
  509. ret = class_create_file(&uverbs_class, &class_attr_abi_version);
  510. if (ret) {
  511. printk(KERN_ERR "user_verbs: couldn't create abi_version attribute\n");
  512. goto out_class;
  513. }
  514. ret = register_filesystem(&uverbs_event_fs);
  515. if (ret) {
  516. printk(KERN_ERR "user_verbs: couldn't register infinibandeventfs\n");
  517. goto out_class;
  518. }
  519. uverbs_event_mnt = kern_mount(&uverbs_event_fs);
  520. if (IS_ERR(uverbs_event_mnt)) {
  521. ret = PTR_ERR(uverbs_event_mnt);
  522. printk(KERN_ERR "user_verbs: couldn't mount infinibandeventfs\n");
  523. goto out_fs;
  524. }
  525. ret = ib_register_client(&uverbs_client);
  526. if (ret) {
  527. printk(KERN_ERR "user_verbs: couldn't register client\n");
  528. goto out_mnt;
  529. }
  530. return 0;
  531. out_mnt:
  532. mntput(uverbs_event_mnt);
  533. out_fs:
  534. unregister_filesystem(&uverbs_event_fs);
  535. out_class:
  536. class_unregister(&uverbs_class);
  537. out_chrdev:
  538. unregister_chrdev_region(IB_UVERBS_BASE_DEV, IB_UVERBS_MAX_DEVICES);
  539. out:
  540. return ret;
  541. }
  542. static void __exit ib_uverbs_cleanup(void)
  543. {
  544. ib_unregister_client(&uverbs_client);
  545. mntput(uverbs_event_mnt);
  546. unregister_filesystem(&uverbs_event_fs);
  547. class_unregister(&uverbs_class);
  548. unregister_chrdev_region(IB_UVERBS_BASE_DEV, IB_UVERBS_MAX_DEVICES);
  549. }
  550. module_init(ib_uverbs_init);
  551. module_exit(ib_uverbs_cleanup);