uverbs_main.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757
  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 || hdr.command >= ARRAY_SIZE(uverbs_cmd_table))
  362. return -EINVAL;
  363. if (!file->ucontext &&
  364. hdr.command != IB_USER_VERBS_CMD_QUERY_PARAMS &&
  365. hdr.command != IB_USER_VERBS_CMD_GET_CONTEXT)
  366. return -EINVAL;
  367. return uverbs_cmd_table[hdr.command](file, buf + sizeof hdr,
  368. hdr.in_words * 4, hdr.out_words * 4);
  369. }
  370. static int ib_uverbs_mmap(struct file *filp, struct vm_area_struct *vma)
  371. {
  372. struct ib_uverbs_file *file = filp->private_data;
  373. if (!file->ucontext)
  374. return -ENODEV;
  375. else
  376. return file->device->ib_dev->mmap(file->ucontext, vma);
  377. }
  378. static int ib_uverbs_open(struct inode *inode, struct file *filp)
  379. {
  380. struct ib_uverbs_device *dev =
  381. container_of(inode->i_cdev, struct ib_uverbs_device, dev);
  382. struct ib_uverbs_file *file;
  383. int i = 0;
  384. int ret;
  385. if (!try_module_get(dev->ib_dev->owner))
  386. return -ENODEV;
  387. file = kmalloc(sizeof *file +
  388. (dev->num_comp - 1) * sizeof (struct ib_uverbs_event_file),
  389. GFP_KERNEL);
  390. if (!file)
  391. return -ENOMEM;
  392. file->device = dev;
  393. kref_init(&file->ref);
  394. file->ucontext = NULL;
  395. ret = ib_uverbs_event_init(&file->async_file, file);
  396. if (ret)
  397. goto err;
  398. file->async_file.is_async = 1;
  399. kref_get(&file->ref);
  400. for (i = 0; i < dev->num_comp; ++i) {
  401. ret = ib_uverbs_event_init(&file->comp_file[i], file);
  402. if (ret)
  403. goto err_async;
  404. kref_get(&file->ref);
  405. file->comp_file[i].is_async = 0;
  406. }
  407. filp->private_data = file;
  408. INIT_IB_EVENT_HANDLER(&file->event_handler, dev->ib_dev,
  409. ib_uverbs_event_handler);
  410. if (ib_register_event_handler(&file->event_handler))
  411. goto err_async;
  412. return 0;
  413. err_async:
  414. while (i--)
  415. ib_uverbs_event_release(&file->comp_file[i]);
  416. ib_uverbs_event_release(&file->async_file);
  417. err:
  418. kref_put(&file->ref, ib_uverbs_release_file);
  419. return ret;
  420. }
  421. static int ib_uverbs_close(struct inode *inode, struct file *filp)
  422. {
  423. struct ib_uverbs_file *file = filp->private_data;
  424. int i;
  425. ib_unregister_event_handler(&file->event_handler);
  426. ib_uverbs_event_release(&file->async_file);
  427. ib_dealloc_ucontext(file->ucontext);
  428. for (i = 0; i < file->device->num_comp; ++i)
  429. ib_uverbs_event_release(&file->comp_file[i]);
  430. kref_put(&file->ref, ib_uverbs_release_file);
  431. return 0;
  432. }
  433. static struct file_operations uverbs_fops = {
  434. .owner = THIS_MODULE,
  435. .write = ib_uverbs_write,
  436. .open = ib_uverbs_open,
  437. .release = ib_uverbs_close
  438. };
  439. static struct file_operations uverbs_mmap_fops = {
  440. .owner = THIS_MODULE,
  441. .write = ib_uverbs_write,
  442. .mmap = ib_uverbs_mmap,
  443. .open = ib_uverbs_open,
  444. .release = ib_uverbs_close
  445. };
  446. static struct ib_client uverbs_client = {
  447. .name = "uverbs",
  448. .add = ib_uverbs_add_one,
  449. .remove = ib_uverbs_remove_one
  450. };
  451. static ssize_t show_ibdev(struct class_device *class_dev, char *buf)
  452. {
  453. struct ib_uverbs_device *dev =
  454. container_of(class_dev, struct ib_uverbs_device, class_dev);
  455. return sprintf(buf, "%s\n", dev->ib_dev->name);
  456. }
  457. static CLASS_DEVICE_ATTR(ibdev, S_IRUGO, show_ibdev, NULL);
  458. static void ib_uverbs_release_class_dev(struct class_device *class_dev)
  459. {
  460. struct ib_uverbs_device *dev =
  461. container_of(class_dev, struct ib_uverbs_device, class_dev);
  462. cdev_del(&dev->dev);
  463. clear_bit(dev->devnum, dev_map);
  464. kfree(dev);
  465. }
  466. static struct class uverbs_class = {
  467. .name = "infiniband_verbs",
  468. .release = ib_uverbs_release_class_dev
  469. };
  470. static ssize_t show_abi_version(struct class *class, char *buf)
  471. {
  472. return sprintf(buf, "%d\n", IB_USER_VERBS_ABI_VERSION);
  473. }
  474. static CLASS_ATTR(abi_version, S_IRUGO, show_abi_version, NULL);
  475. static void ib_uverbs_add_one(struct ib_device *device)
  476. {
  477. struct ib_uverbs_device *uverbs_dev;
  478. if (!device->alloc_ucontext)
  479. return;
  480. uverbs_dev = kmalloc(sizeof *uverbs_dev, GFP_KERNEL);
  481. if (!uverbs_dev)
  482. return;
  483. memset(uverbs_dev, 0, sizeof *uverbs_dev);
  484. spin_lock(&map_lock);
  485. uverbs_dev->devnum = find_first_zero_bit(dev_map, IB_UVERBS_MAX_DEVICES);
  486. if (uverbs_dev->devnum >= IB_UVERBS_MAX_DEVICES) {
  487. spin_unlock(&map_lock);
  488. goto err;
  489. }
  490. set_bit(uverbs_dev->devnum, dev_map);
  491. spin_unlock(&map_lock);
  492. uverbs_dev->ib_dev = device;
  493. uverbs_dev->num_comp = 1;
  494. if (device->mmap)
  495. cdev_init(&uverbs_dev->dev, &uverbs_mmap_fops);
  496. else
  497. cdev_init(&uverbs_dev->dev, &uverbs_fops);
  498. uverbs_dev->dev.owner = THIS_MODULE;
  499. kobject_set_name(&uverbs_dev->dev.kobj, "uverbs%d", uverbs_dev->devnum);
  500. if (cdev_add(&uverbs_dev->dev, IB_UVERBS_BASE_DEV + uverbs_dev->devnum, 1))
  501. goto err;
  502. uverbs_dev->class_dev.class = &uverbs_class;
  503. uverbs_dev->class_dev.dev = device->dma_device;
  504. uverbs_dev->class_dev.devt = uverbs_dev->dev.dev;
  505. snprintf(uverbs_dev->class_dev.class_id, BUS_ID_SIZE, "uverbs%d", uverbs_dev->devnum);
  506. if (class_device_register(&uverbs_dev->class_dev))
  507. goto err_cdev;
  508. if (class_device_create_file(&uverbs_dev->class_dev, &class_device_attr_ibdev))
  509. goto err_class;
  510. ib_set_client_data(device, &uverbs_client, uverbs_dev);
  511. return;
  512. err_class:
  513. class_device_unregister(&uverbs_dev->class_dev);
  514. err_cdev:
  515. cdev_del(&uverbs_dev->dev);
  516. clear_bit(uverbs_dev->devnum, dev_map);
  517. err:
  518. kfree(uverbs_dev);
  519. return;
  520. }
  521. static void ib_uverbs_remove_one(struct ib_device *device)
  522. {
  523. struct ib_uverbs_device *uverbs_dev = ib_get_client_data(device, &uverbs_client);
  524. if (!uverbs_dev)
  525. return;
  526. class_device_unregister(&uverbs_dev->class_dev);
  527. }
  528. static struct super_block *uverbs_event_get_sb(struct file_system_type *fs_type, int flags,
  529. const char *dev_name, void *data)
  530. {
  531. return get_sb_pseudo(fs_type, "infinibandevent:", NULL,
  532. INFINIBANDEVENTFS_MAGIC);
  533. }
  534. static struct file_system_type uverbs_event_fs = {
  535. /* No owner field so module can be unloaded */
  536. .name = "infinibandeventfs",
  537. .get_sb = uverbs_event_get_sb,
  538. .kill_sb = kill_litter_super
  539. };
  540. static int __init ib_uverbs_init(void)
  541. {
  542. int ret;
  543. spin_lock_init(&map_lock);
  544. ret = register_chrdev_region(IB_UVERBS_BASE_DEV, IB_UVERBS_MAX_DEVICES,
  545. "infiniband_verbs");
  546. if (ret) {
  547. printk(KERN_ERR "user_verbs: couldn't register device number\n");
  548. goto out;
  549. }
  550. ret = class_register(&uverbs_class);
  551. if (ret) {
  552. printk(KERN_ERR "user_verbs: couldn't create class infiniband_verbs\n");
  553. goto out_chrdev;
  554. }
  555. ret = class_create_file(&uverbs_class, &class_attr_abi_version);
  556. if (ret) {
  557. printk(KERN_ERR "user_verbs: couldn't create abi_version attribute\n");
  558. goto out_class;
  559. }
  560. ret = register_filesystem(&uverbs_event_fs);
  561. if (ret) {
  562. printk(KERN_ERR "user_verbs: couldn't register infinibandeventfs\n");
  563. goto out_class;
  564. }
  565. uverbs_event_mnt = kern_mount(&uverbs_event_fs);
  566. if (IS_ERR(uverbs_event_mnt)) {
  567. ret = PTR_ERR(uverbs_event_mnt);
  568. printk(KERN_ERR "user_verbs: couldn't mount infinibandeventfs\n");
  569. goto out_fs;
  570. }
  571. ret = ib_register_client(&uverbs_client);
  572. if (ret) {
  573. printk(KERN_ERR "user_verbs: couldn't register client\n");
  574. goto out_mnt;
  575. }
  576. return 0;
  577. out_mnt:
  578. mntput(uverbs_event_mnt);
  579. out_fs:
  580. unregister_filesystem(&uverbs_event_fs);
  581. out_class:
  582. class_unregister(&uverbs_class);
  583. out_chrdev:
  584. unregister_chrdev_region(IB_UVERBS_BASE_DEV, IB_UVERBS_MAX_DEVICES);
  585. out:
  586. return ret;
  587. }
  588. static void __exit ib_uverbs_cleanup(void)
  589. {
  590. ib_unregister_client(&uverbs_client);
  591. mntput(uverbs_event_mnt);
  592. unregister_filesystem(&uverbs_event_fs);
  593. class_unregister(&uverbs_class);
  594. unregister_chrdev_region(IB_UVERBS_BASE_DEV, IB_UVERBS_MAX_DEVICES);
  595. }
  596. module_init(ib_uverbs_init);
  597. module_exit(ib_uverbs_cleanup);