uverbs_main.c 20 KB

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