uverbs_main.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710
  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_fasync(int fd, struct file *filp, int on)
  213. {
  214. struct ib_uverbs_event_file *file = filp->private_data;
  215. return fasync_helper(fd, filp, on, &file->async_queue);
  216. }
  217. static int ib_uverbs_event_close(struct inode *inode, struct file *filp)
  218. {
  219. struct ib_uverbs_event_file *file = filp->private_data;
  220. ib_uverbs_event_release(file);
  221. ib_uverbs_event_fasync(-1, filp, 0);
  222. kref_put(&file->uverbs_file->ref, ib_uverbs_release_file);
  223. return 0;
  224. }
  225. static struct file_operations uverbs_event_fops = {
  226. /*
  227. * No .owner field since we artificially create event files,
  228. * so there is no increment to the module reference count in
  229. * the open path. All event files come from a uverbs command
  230. * file, which already takes a module reference, so this is OK.
  231. */
  232. .read = ib_uverbs_event_read,
  233. .poll = ib_uverbs_event_poll,
  234. .release = ib_uverbs_event_close,
  235. .fasync = ib_uverbs_event_fasync
  236. };
  237. void ib_uverbs_comp_handler(struct ib_cq *cq, void *cq_context)
  238. {
  239. struct ib_uverbs_file *file = cq_context;
  240. struct ib_uverbs_comp_event *entry;
  241. unsigned long flags;
  242. entry = kmalloc(sizeof *entry, GFP_ATOMIC);
  243. if (!entry)
  244. return;
  245. entry->desc.cq_handle = cq->uobject->user_handle;
  246. spin_lock_irqsave(&file->comp_file[0].lock, flags);
  247. list_add_tail(&entry->list, &file->comp_file[0].event_list);
  248. spin_unlock_irqrestore(&file->comp_file[0].lock, flags);
  249. wake_up_interruptible(&file->comp_file[0].poll_wait);
  250. kill_fasync(&file->comp_file[0].async_queue, SIGIO, POLL_IN);
  251. }
  252. static void ib_uverbs_async_handler(struct ib_uverbs_file *file,
  253. __u64 element, __u64 event)
  254. {
  255. struct ib_uverbs_async_event *entry;
  256. unsigned long flags;
  257. entry = kmalloc(sizeof *entry, GFP_ATOMIC);
  258. if (!entry)
  259. return;
  260. entry->desc.element = element;
  261. entry->desc.event_type = event;
  262. spin_lock_irqsave(&file->async_file.lock, flags);
  263. list_add_tail(&entry->list, &file->async_file.event_list);
  264. spin_unlock_irqrestore(&file->async_file.lock, flags);
  265. wake_up_interruptible(&file->async_file.poll_wait);
  266. kill_fasync(&file->async_file.async_queue, SIGIO, POLL_IN);
  267. }
  268. void ib_uverbs_cq_event_handler(struct ib_event *event, void *context_ptr)
  269. {
  270. ib_uverbs_async_handler(context_ptr,
  271. event->element.cq->uobject->user_handle,
  272. event->event);
  273. }
  274. void ib_uverbs_qp_event_handler(struct ib_event *event, void *context_ptr)
  275. {
  276. ib_uverbs_async_handler(context_ptr,
  277. event->element.qp->uobject->user_handle,
  278. event->event);
  279. }
  280. static void ib_uverbs_event_handler(struct ib_event_handler *handler,
  281. struct ib_event *event)
  282. {
  283. struct ib_uverbs_file *file =
  284. container_of(handler, struct ib_uverbs_file, event_handler);
  285. ib_uverbs_async_handler(file, event->element.port_num, event->event);
  286. }
  287. static int ib_uverbs_event_init(struct ib_uverbs_event_file *file,
  288. struct ib_uverbs_file *uverbs_file)
  289. {
  290. struct file *filp;
  291. spin_lock_init(&file->lock);
  292. INIT_LIST_HEAD(&file->event_list);
  293. init_waitqueue_head(&file->poll_wait);
  294. file->uverbs_file = uverbs_file;
  295. file->async_queue = NULL;
  296. file->fd = get_unused_fd();
  297. if (file->fd < 0)
  298. return file->fd;
  299. filp = get_empty_filp();
  300. if (!filp) {
  301. put_unused_fd(file->fd);
  302. return -ENFILE;
  303. }
  304. filp->f_op = &uverbs_event_fops;
  305. filp->f_vfsmnt = mntget(uverbs_event_mnt);
  306. filp->f_dentry = dget(uverbs_event_mnt->mnt_root);
  307. filp->f_mapping = filp->f_dentry->d_inode->i_mapping;
  308. filp->f_flags = O_RDONLY;
  309. filp->f_mode = FMODE_READ;
  310. filp->private_data = file;
  311. fd_install(file->fd, filp);
  312. return 0;
  313. }
  314. static ssize_t ib_uverbs_write(struct file *filp, const char __user *buf,
  315. size_t count, loff_t *pos)
  316. {
  317. struct ib_uverbs_file *file = filp->private_data;
  318. struct ib_uverbs_cmd_hdr hdr;
  319. if (count < sizeof hdr)
  320. return -EINVAL;
  321. if (copy_from_user(&hdr, buf, sizeof hdr))
  322. return -EFAULT;
  323. if (hdr.in_words * 4 != count)
  324. return -EINVAL;
  325. if (hdr.command < 0 || hdr.command >= ARRAY_SIZE(uverbs_cmd_table))
  326. return -EINVAL;
  327. if (!file->ucontext &&
  328. hdr.command != IB_USER_VERBS_CMD_QUERY_PARAMS &&
  329. hdr.command != IB_USER_VERBS_CMD_GET_CONTEXT)
  330. return -EINVAL;
  331. return uverbs_cmd_table[hdr.command](file, buf + sizeof hdr,
  332. hdr.in_words * 4, hdr.out_words * 4);
  333. }
  334. static int ib_uverbs_mmap(struct file *filp, struct vm_area_struct *vma)
  335. {
  336. struct ib_uverbs_file *file = filp->private_data;
  337. if (!file->ucontext)
  338. return -ENODEV;
  339. else
  340. return file->device->ib_dev->mmap(file->ucontext, vma);
  341. }
  342. static int ib_uverbs_open(struct inode *inode, struct file *filp)
  343. {
  344. struct ib_uverbs_device *dev =
  345. container_of(inode->i_cdev, struct ib_uverbs_device, dev);
  346. struct ib_uverbs_file *file;
  347. int i = 0;
  348. int ret;
  349. if (!try_module_get(dev->ib_dev->owner))
  350. return -ENODEV;
  351. file = kmalloc(sizeof *file +
  352. (dev->num_comp - 1) * sizeof (struct ib_uverbs_event_file),
  353. GFP_KERNEL);
  354. if (!file)
  355. return -ENOMEM;
  356. file->device = dev;
  357. kref_init(&file->ref);
  358. file->ucontext = NULL;
  359. ret = ib_uverbs_event_init(&file->async_file, file);
  360. if (ret)
  361. goto err;
  362. file->async_file.is_async = 1;
  363. kref_get(&file->ref);
  364. for (i = 0; i < dev->num_comp; ++i) {
  365. ret = ib_uverbs_event_init(&file->comp_file[i], file);
  366. if (ret)
  367. goto err_async;
  368. kref_get(&file->ref);
  369. file->comp_file[i].is_async = 0;
  370. }
  371. filp->private_data = file;
  372. INIT_IB_EVENT_HANDLER(&file->event_handler, dev->ib_dev,
  373. ib_uverbs_event_handler);
  374. if (ib_register_event_handler(&file->event_handler))
  375. goto err_async;
  376. return 0;
  377. err_async:
  378. while (i--)
  379. ib_uverbs_event_release(&file->comp_file[i]);
  380. ib_uverbs_event_release(&file->async_file);
  381. err:
  382. kref_put(&file->ref, ib_uverbs_release_file);
  383. return ret;
  384. }
  385. static int ib_uverbs_close(struct inode *inode, struct file *filp)
  386. {
  387. struct ib_uverbs_file *file = filp->private_data;
  388. int i;
  389. ib_unregister_event_handler(&file->event_handler);
  390. ib_uverbs_event_release(&file->async_file);
  391. ib_dealloc_ucontext(file->ucontext);
  392. for (i = 0; i < file->device->num_comp; ++i)
  393. ib_uverbs_event_release(&file->comp_file[i]);
  394. kref_put(&file->ref, ib_uverbs_release_file);
  395. return 0;
  396. }
  397. static struct file_operations uverbs_fops = {
  398. .owner = THIS_MODULE,
  399. .write = ib_uverbs_write,
  400. .open = ib_uverbs_open,
  401. .release = ib_uverbs_close
  402. };
  403. static struct file_operations uverbs_mmap_fops = {
  404. .owner = THIS_MODULE,
  405. .write = ib_uverbs_write,
  406. .mmap = ib_uverbs_mmap,
  407. .open = ib_uverbs_open,
  408. .release = ib_uverbs_close
  409. };
  410. static struct ib_client uverbs_client = {
  411. .name = "uverbs",
  412. .add = ib_uverbs_add_one,
  413. .remove = ib_uverbs_remove_one
  414. };
  415. static ssize_t show_ibdev(struct class_device *class_dev, char *buf)
  416. {
  417. struct ib_uverbs_device *dev =
  418. container_of(class_dev, struct ib_uverbs_device, class_dev);
  419. return sprintf(buf, "%s\n", dev->ib_dev->name);
  420. }
  421. static CLASS_DEVICE_ATTR(ibdev, S_IRUGO, show_ibdev, NULL);
  422. static void ib_uverbs_release_class_dev(struct class_device *class_dev)
  423. {
  424. struct ib_uverbs_device *dev =
  425. container_of(class_dev, struct ib_uverbs_device, class_dev);
  426. cdev_del(&dev->dev);
  427. clear_bit(dev->devnum, dev_map);
  428. kfree(dev);
  429. }
  430. static struct class uverbs_class = {
  431. .name = "infiniband_verbs",
  432. .release = ib_uverbs_release_class_dev
  433. };
  434. static ssize_t show_abi_version(struct class *class, char *buf)
  435. {
  436. return sprintf(buf, "%d\n", IB_USER_VERBS_ABI_VERSION);
  437. }
  438. static CLASS_ATTR(abi_version, S_IRUGO, show_abi_version, NULL);
  439. static void ib_uverbs_add_one(struct ib_device *device)
  440. {
  441. struct ib_uverbs_device *uverbs_dev;
  442. if (!device->alloc_ucontext)
  443. return;
  444. uverbs_dev = kmalloc(sizeof *uverbs_dev, GFP_KERNEL);
  445. if (!uverbs_dev)
  446. return;
  447. memset(uverbs_dev, 0, sizeof *uverbs_dev);
  448. spin_lock(&map_lock);
  449. uverbs_dev->devnum = find_first_zero_bit(dev_map, IB_UVERBS_MAX_DEVICES);
  450. if (uverbs_dev->devnum >= IB_UVERBS_MAX_DEVICES) {
  451. spin_unlock(&map_lock);
  452. goto err;
  453. }
  454. set_bit(uverbs_dev->devnum, dev_map);
  455. spin_unlock(&map_lock);
  456. uverbs_dev->ib_dev = device;
  457. uverbs_dev->num_comp = 1;
  458. if (device->mmap)
  459. cdev_init(&uverbs_dev->dev, &uverbs_mmap_fops);
  460. else
  461. cdev_init(&uverbs_dev->dev, &uverbs_fops);
  462. uverbs_dev->dev.owner = THIS_MODULE;
  463. kobject_set_name(&uverbs_dev->dev.kobj, "uverbs%d", uverbs_dev->devnum);
  464. if (cdev_add(&uverbs_dev->dev, IB_UVERBS_BASE_DEV + uverbs_dev->devnum, 1))
  465. goto err;
  466. uverbs_dev->class_dev.class = &uverbs_class;
  467. uverbs_dev->class_dev.dev = device->dma_device;
  468. uverbs_dev->class_dev.devt = uverbs_dev->dev.dev;
  469. snprintf(uverbs_dev->class_dev.class_id, BUS_ID_SIZE, "uverbs%d", uverbs_dev->devnum);
  470. if (class_device_register(&uverbs_dev->class_dev))
  471. goto err_cdev;
  472. if (class_device_create_file(&uverbs_dev->class_dev, &class_device_attr_ibdev))
  473. goto err_class;
  474. ib_set_client_data(device, &uverbs_client, uverbs_dev);
  475. return;
  476. err_class:
  477. class_device_unregister(&uverbs_dev->class_dev);
  478. err_cdev:
  479. cdev_del(&uverbs_dev->dev);
  480. clear_bit(uverbs_dev->devnum, dev_map);
  481. err:
  482. kfree(uverbs_dev);
  483. return;
  484. }
  485. static void ib_uverbs_remove_one(struct ib_device *device)
  486. {
  487. struct ib_uverbs_device *uverbs_dev = ib_get_client_data(device, &uverbs_client);
  488. if (!uverbs_dev)
  489. return;
  490. class_device_unregister(&uverbs_dev->class_dev);
  491. }
  492. static struct super_block *uverbs_event_get_sb(struct file_system_type *fs_type, int flags,
  493. const char *dev_name, void *data)
  494. {
  495. return get_sb_pseudo(fs_type, "infinibandevent:", NULL,
  496. INFINIBANDEVENTFS_MAGIC);
  497. }
  498. static struct file_system_type uverbs_event_fs = {
  499. /* No owner field so module can be unloaded */
  500. .name = "infinibandeventfs",
  501. .get_sb = uverbs_event_get_sb,
  502. .kill_sb = kill_litter_super
  503. };
  504. static int __init ib_uverbs_init(void)
  505. {
  506. int ret;
  507. spin_lock_init(&map_lock);
  508. ret = register_chrdev_region(IB_UVERBS_BASE_DEV, IB_UVERBS_MAX_DEVICES,
  509. "infiniband_verbs");
  510. if (ret) {
  511. printk(KERN_ERR "user_verbs: couldn't register device number\n");
  512. goto out;
  513. }
  514. ret = class_register(&uverbs_class);
  515. if (ret) {
  516. printk(KERN_ERR "user_verbs: couldn't create class infiniband_verbs\n");
  517. goto out_chrdev;
  518. }
  519. ret = class_create_file(&uverbs_class, &class_attr_abi_version);
  520. if (ret) {
  521. printk(KERN_ERR "user_verbs: couldn't create abi_version attribute\n");
  522. goto out_class;
  523. }
  524. ret = register_filesystem(&uverbs_event_fs);
  525. if (ret) {
  526. printk(KERN_ERR "user_verbs: couldn't register infinibandeventfs\n");
  527. goto out_class;
  528. }
  529. uverbs_event_mnt = kern_mount(&uverbs_event_fs);
  530. if (IS_ERR(uverbs_event_mnt)) {
  531. ret = PTR_ERR(uverbs_event_mnt);
  532. printk(KERN_ERR "user_verbs: couldn't mount infinibandeventfs\n");
  533. goto out_fs;
  534. }
  535. ret = ib_register_client(&uverbs_client);
  536. if (ret) {
  537. printk(KERN_ERR "user_verbs: couldn't register client\n");
  538. goto out_mnt;
  539. }
  540. return 0;
  541. out_mnt:
  542. mntput(uverbs_event_mnt);
  543. out_fs:
  544. unregister_filesystem(&uverbs_event_fs);
  545. out_class:
  546. class_unregister(&uverbs_class);
  547. out_chrdev:
  548. unregister_chrdev_region(IB_UVERBS_BASE_DEV, IB_UVERBS_MAX_DEVICES);
  549. out:
  550. return ret;
  551. }
  552. static void __exit ib_uverbs_cleanup(void)
  553. {
  554. ib_unregister_client(&uverbs_client);
  555. mntput(uverbs_event_mnt);
  556. unregister_filesystem(&uverbs_event_fs);
  557. class_unregister(&uverbs_class);
  558. unregister_chrdev_region(IB_UVERBS_BASE_DEV, IB_UVERBS_MAX_DEVICES);
  559. }
  560. module_init(ib_uverbs_init);
  561. module_exit(ib_uverbs_cleanup);