uverbs_main.c 20 KB

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