uverbs_main.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882
  1. /*
  2. * Copyright (c) 2005 Topspin Communications. All rights reserved.
  3. * Copyright (c) 2005, 2006 Cisco Systems. All rights reserved.
  4. * Copyright (c) 2005 Mellanox Technologies. All rights reserved.
  5. * Copyright (c) 2005 Voltaire, Inc. All rights reserved.
  6. * Copyright (c) 2005 PathScale, Inc. All rights reserved.
  7. *
  8. * This software is available to you under a choice of one of two
  9. * licenses. You may choose to be licensed under the terms of the GNU
  10. * General Public License (GPL) Version 2, available from the file
  11. * COPYING in the main directory of this source tree, or the
  12. * OpenIB.org BSD license below:
  13. *
  14. * Redistribution and use in source and binary forms, with or
  15. * without modification, are permitted provided that the following
  16. * conditions are met:
  17. *
  18. * - Redistributions of source code must retain the above
  19. * copyright notice, this list of conditions and the following
  20. * disclaimer.
  21. *
  22. * - Redistributions in binary form must reproduce the above
  23. * copyright notice, this list of conditions and the following
  24. * disclaimer in the documentation and/or other materials
  25. * provided with the distribution.
  26. *
  27. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  28. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  29. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  30. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  31. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  32. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  33. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  34. * SOFTWARE.
  35. */
  36. #include <linux/module.h>
  37. #include <linux/init.h>
  38. #include <linux/device.h>
  39. #include <linux/err.h>
  40. #include <linux/fs.h>
  41. #include <linux/poll.h>
  42. #include <linux/sched.h>
  43. #include <linux/file.h>
  44. #include <linux/cdev.h>
  45. #include <linux/anon_inodes.h>
  46. #include <asm/uaccess.h>
  47. #include "uverbs.h"
  48. MODULE_AUTHOR("Roland Dreier");
  49. MODULE_DESCRIPTION("InfiniBand userspace verbs access");
  50. MODULE_LICENSE("Dual BSD/GPL");
  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. static struct class *uverbs_class;
  58. DEFINE_SPINLOCK(ib_uverbs_idr_lock);
  59. DEFINE_IDR(ib_uverbs_pd_idr);
  60. DEFINE_IDR(ib_uverbs_mr_idr);
  61. DEFINE_IDR(ib_uverbs_mw_idr);
  62. DEFINE_IDR(ib_uverbs_ah_idr);
  63. DEFINE_IDR(ib_uverbs_cq_idr);
  64. DEFINE_IDR(ib_uverbs_qp_idr);
  65. DEFINE_IDR(ib_uverbs_srq_idr);
  66. static DEFINE_SPINLOCK(map_lock);
  67. static DECLARE_BITMAP(dev_map, IB_UVERBS_MAX_DEVICES);
  68. static ssize_t (*uverbs_cmd_table[])(struct ib_uverbs_file *file,
  69. const char __user *buf, int in_len,
  70. int out_len) = {
  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_ALLOC_PD] = ib_uverbs_alloc_pd,
  75. [IB_USER_VERBS_CMD_DEALLOC_PD] = ib_uverbs_dealloc_pd,
  76. [IB_USER_VERBS_CMD_REG_MR] = ib_uverbs_reg_mr,
  77. [IB_USER_VERBS_CMD_DEREG_MR] = ib_uverbs_dereg_mr,
  78. [IB_USER_VERBS_CMD_CREATE_COMP_CHANNEL] = ib_uverbs_create_comp_channel,
  79. [IB_USER_VERBS_CMD_CREATE_CQ] = ib_uverbs_create_cq,
  80. [IB_USER_VERBS_CMD_RESIZE_CQ] = ib_uverbs_resize_cq,
  81. [IB_USER_VERBS_CMD_POLL_CQ] = ib_uverbs_poll_cq,
  82. [IB_USER_VERBS_CMD_REQ_NOTIFY_CQ] = ib_uverbs_req_notify_cq,
  83. [IB_USER_VERBS_CMD_DESTROY_CQ] = ib_uverbs_destroy_cq,
  84. [IB_USER_VERBS_CMD_CREATE_QP] = ib_uverbs_create_qp,
  85. [IB_USER_VERBS_CMD_QUERY_QP] = ib_uverbs_query_qp,
  86. [IB_USER_VERBS_CMD_MODIFY_QP] = ib_uverbs_modify_qp,
  87. [IB_USER_VERBS_CMD_DESTROY_QP] = ib_uverbs_destroy_qp,
  88. [IB_USER_VERBS_CMD_POST_SEND] = ib_uverbs_post_send,
  89. [IB_USER_VERBS_CMD_POST_RECV] = ib_uverbs_post_recv,
  90. [IB_USER_VERBS_CMD_POST_SRQ_RECV] = ib_uverbs_post_srq_recv,
  91. [IB_USER_VERBS_CMD_CREATE_AH] = ib_uverbs_create_ah,
  92. [IB_USER_VERBS_CMD_DESTROY_AH] = ib_uverbs_destroy_ah,
  93. [IB_USER_VERBS_CMD_ATTACH_MCAST] = ib_uverbs_attach_mcast,
  94. [IB_USER_VERBS_CMD_DETACH_MCAST] = ib_uverbs_detach_mcast,
  95. [IB_USER_VERBS_CMD_CREATE_SRQ] = ib_uverbs_create_srq,
  96. [IB_USER_VERBS_CMD_MODIFY_SRQ] = ib_uverbs_modify_srq,
  97. [IB_USER_VERBS_CMD_QUERY_SRQ] = ib_uverbs_query_srq,
  98. [IB_USER_VERBS_CMD_DESTROY_SRQ] = ib_uverbs_destroy_srq,
  99. };
  100. static void ib_uverbs_add_one(struct ib_device *device);
  101. static void ib_uverbs_remove_one(struct ib_device *device);
  102. static void ib_uverbs_release_dev(struct kref *ref)
  103. {
  104. struct ib_uverbs_device *dev =
  105. container_of(ref, struct ib_uverbs_device, ref);
  106. complete(&dev->comp);
  107. }
  108. static void ib_uverbs_release_event_file(struct kref *ref)
  109. {
  110. struct ib_uverbs_event_file *file =
  111. container_of(ref, struct ib_uverbs_event_file, ref);
  112. kfree(file);
  113. }
  114. void ib_uverbs_release_ucq(struct ib_uverbs_file *file,
  115. struct ib_uverbs_event_file *ev_file,
  116. struct ib_ucq_object *uobj)
  117. {
  118. struct ib_uverbs_event *evt, *tmp;
  119. if (ev_file) {
  120. spin_lock_irq(&ev_file->lock);
  121. list_for_each_entry_safe(evt, tmp, &uobj->comp_list, obj_list) {
  122. list_del(&evt->list);
  123. kfree(evt);
  124. }
  125. spin_unlock_irq(&ev_file->lock);
  126. kref_put(&ev_file->ref, ib_uverbs_release_event_file);
  127. }
  128. spin_lock_irq(&file->async_file->lock);
  129. list_for_each_entry_safe(evt, tmp, &uobj->async_list, obj_list) {
  130. list_del(&evt->list);
  131. kfree(evt);
  132. }
  133. spin_unlock_irq(&file->async_file->lock);
  134. }
  135. void ib_uverbs_release_uevent(struct ib_uverbs_file *file,
  136. struct ib_uevent_object *uobj)
  137. {
  138. struct ib_uverbs_event *evt, *tmp;
  139. spin_lock_irq(&file->async_file->lock);
  140. list_for_each_entry_safe(evt, tmp, &uobj->event_list, obj_list) {
  141. list_del(&evt->list);
  142. kfree(evt);
  143. }
  144. spin_unlock_irq(&file->async_file->lock);
  145. }
  146. static void ib_uverbs_detach_umcast(struct ib_qp *qp,
  147. struct ib_uqp_object *uobj)
  148. {
  149. struct ib_uverbs_mcast_entry *mcast, *tmp;
  150. list_for_each_entry_safe(mcast, tmp, &uobj->mcast_list, list) {
  151. ib_detach_mcast(qp, &mcast->gid, mcast->lid);
  152. list_del(&mcast->list);
  153. kfree(mcast);
  154. }
  155. }
  156. static int ib_uverbs_cleanup_ucontext(struct ib_uverbs_file *file,
  157. struct ib_ucontext *context)
  158. {
  159. struct ib_uobject *uobj, *tmp;
  160. if (!context)
  161. return 0;
  162. context->closing = 1;
  163. list_for_each_entry_safe(uobj, tmp, &context->ah_list, list) {
  164. struct ib_ah *ah = uobj->object;
  165. idr_remove_uobj(&ib_uverbs_ah_idr, uobj);
  166. ib_destroy_ah(ah);
  167. kfree(uobj);
  168. }
  169. list_for_each_entry_safe(uobj, tmp, &context->qp_list, list) {
  170. struct ib_qp *qp = uobj->object;
  171. struct ib_uqp_object *uqp =
  172. container_of(uobj, struct ib_uqp_object, uevent.uobject);
  173. idr_remove_uobj(&ib_uverbs_qp_idr, uobj);
  174. ib_uverbs_detach_umcast(qp, uqp);
  175. ib_destroy_qp(qp);
  176. ib_uverbs_release_uevent(file, &uqp->uevent);
  177. kfree(uqp);
  178. }
  179. list_for_each_entry_safe(uobj, tmp, &context->cq_list, list) {
  180. struct ib_cq *cq = uobj->object;
  181. struct ib_uverbs_event_file *ev_file = cq->cq_context;
  182. struct ib_ucq_object *ucq =
  183. container_of(uobj, struct ib_ucq_object, uobject);
  184. idr_remove_uobj(&ib_uverbs_cq_idr, uobj);
  185. ib_destroy_cq(cq);
  186. ib_uverbs_release_ucq(file, ev_file, ucq);
  187. kfree(ucq);
  188. }
  189. list_for_each_entry_safe(uobj, tmp, &context->srq_list, list) {
  190. struct ib_srq *srq = uobj->object;
  191. struct ib_uevent_object *uevent =
  192. container_of(uobj, struct ib_uevent_object, uobject);
  193. idr_remove_uobj(&ib_uverbs_srq_idr, uobj);
  194. ib_destroy_srq(srq);
  195. ib_uverbs_release_uevent(file, uevent);
  196. kfree(uevent);
  197. }
  198. /* XXX Free MWs */
  199. list_for_each_entry_safe(uobj, tmp, &context->mr_list, list) {
  200. struct ib_mr *mr = uobj->object;
  201. idr_remove_uobj(&ib_uverbs_mr_idr, uobj);
  202. ib_dereg_mr(mr);
  203. kfree(uobj);
  204. }
  205. list_for_each_entry_safe(uobj, tmp, &context->pd_list, list) {
  206. struct ib_pd *pd = uobj->object;
  207. idr_remove_uobj(&ib_uverbs_pd_idr, uobj);
  208. ib_dealloc_pd(pd);
  209. kfree(uobj);
  210. }
  211. return context->device->dealloc_ucontext(context);
  212. }
  213. static void ib_uverbs_release_file(struct kref *ref)
  214. {
  215. struct ib_uverbs_file *file =
  216. container_of(ref, struct ib_uverbs_file, ref);
  217. module_put(file->device->ib_dev->owner);
  218. kref_put(&file->device->ref, ib_uverbs_release_dev);
  219. kfree(file);
  220. }
  221. static ssize_t ib_uverbs_event_read(struct file *filp, char __user *buf,
  222. size_t count, loff_t *pos)
  223. {
  224. struct ib_uverbs_event_file *file = filp->private_data;
  225. struct ib_uverbs_event *event;
  226. int eventsz;
  227. int ret = 0;
  228. spin_lock_irq(&file->lock);
  229. while (list_empty(&file->event_list)) {
  230. spin_unlock_irq(&file->lock);
  231. if (filp->f_flags & O_NONBLOCK)
  232. return -EAGAIN;
  233. if (wait_event_interruptible(file->poll_wait,
  234. !list_empty(&file->event_list)))
  235. return -ERESTARTSYS;
  236. spin_lock_irq(&file->lock);
  237. }
  238. event = list_entry(file->event_list.next, struct ib_uverbs_event, list);
  239. if (file->is_async)
  240. eventsz = sizeof (struct ib_uverbs_async_event_desc);
  241. else
  242. eventsz = sizeof (struct ib_uverbs_comp_event_desc);
  243. if (eventsz > count) {
  244. ret = -EINVAL;
  245. event = NULL;
  246. } else {
  247. list_del(file->event_list.next);
  248. if (event->counter) {
  249. ++(*event->counter);
  250. list_del(&event->obj_list);
  251. }
  252. }
  253. spin_unlock_irq(&file->lock);
  254. if (event) {
  255. if (copy_to_user(buf, event, eventsz))
  256. ret = -EFAULT;
  257. else
  258. ret = eventsz;
  259. }
  260. kfree(event);
  261. return ret;
  262. }
  263. static unsigned int ib_uverbs_event_poll(struct file *filp,
  264. struct poll_table_struct *wait)
  265. {
  266. unsigned int pollflags = 0;
  267. struct ib_uverbs_event_file *file = filp->private_data;
  268. poll_wait(filp, &file->poll_wait, wait);
  269. spin_lock_irq(&file->lock);
  270. if (!list_empty(&file->event_list))
  271. pollflags = POLLIN | POLLRDNORM;
  272. spin_unlock_irq(&file->lock);
  273. return pollflags;
  274. }
  275. static int ib_uverbs_event_fasync(int fd, struct file *filp, int on)
  276. {
  277. struct ib_uverbs_event_file *file = filp->private_data;
  278. return fasync_helper(fd, filp, on, &file->async_queue);
  279. }
  280. static int ib_uverbs_event_close(struct inode *inode, struct file *filp)
  281. {
  282. struct ib_uverbs_event_file *file = filp->private_data;
  283. struct ib_uverbs_event *entry, *tmp;
  284. spin_lock_irq(&file->lock);
  285. file->is_closed = 1;
  286. list_for_each_entry_safe(entry, tmp, &file->event_list, list) {
  287. if (entry->counter)
  288. list_del(&entry->obj_list);
  289. kfree(entry);
  290. }
  291. spin_unlock_irq(&file->lock);
  292. if (file->is_async) {
  293. ib_unregister_event_handler(&file->uverbs_file->event_handler);
  294. kref_put(&file->uverbs_file->ref, ib_uverbs_release_file);
  295. }
  296. kref_put(&file->ref, ib_uverbs_release_event_file);
  297. return 0;
  298. }
  299. static const struct file_operations uverbs_event_fops = {
  300. .owner = THIS_MODULE,
  301. .read = ib_uverbs_event_read,
  302. .poll = ib_uverbs_event_poll,
  303. .release = ib_uverbs_event_close,
  304. .fasync = ib_uverbs_event_fasync
  305. };
  306. void ib_uverbs_comp_handler(struct ib_cq *cq, void *cq_context)
  307. {
  308. struct ib_uverbs_event_file *file = cq_context;
  309. struct ib_ucq_object *uobj;
  310. struct ib_uverbs_event *entry;
  311. unsigned long flags;
  312. if (!file)
  313. return;
  314. spin_lock_irqsave(&file->lock, flags);
  315. if (file->is_closed) {
  316. spin_unlock_irqrestore(&file->lock, flags);
  317. return;
  318. }
  319. entry = kmalloc(sizeof *entry, GFP_ATOMIC);
  320. if (!entry) {
  321. spin_unlock_irqrestore(&file->lock, flags);
  322. return;
  323. }
  324. uobj = container_of(cq->uobject, struct ib_ucq_object, uobject);
  325. entry->desc.comp.cq_handle = cq->uobject->user_handle;
  326. entry->counter = &uobj->comp_events_reported;
  327. list_add_tail(&entry->list, &file->event_list);
  328. list_add_tail(&entry->obj_list, &uobj->comp_list);
  329. spin_unlock_irqrestore(&file->lock, flags);
  330. wake_up_interruptible(&file->poll_wait);
  331. kill_fasync(&file->async_queue, SIGIO, POLL_IN);
  332. }
  333. static void ib_uverbs_async_handler(struct ib_uverbs_file *file,
  334. __u64 element, __u64 event,
  335. struct list_head *obj_list,
  336. u32 *counter)
  337. {
  338. struct ib_uverbs_event *entry;
  339. unsigned long flags;
  340. spin_lock_irqsave(&file->async_file->lock, flags);
  341. if (file->async_file->is_closed) {
  342. spin_unlock_irqrestore(&file->async_file->lock, flags);
  343. return;
  344. }
  345. entry = kmalloc(sizeof *entry, GFP_ATOMIC);
  346. if (!entry) {
  347. spin_unlock_irqrestore(&file->async_file->lock, flags);
  348. return;
  349. }
  350. entry->desc.async.element = element;
  351. entry->desc.async.event_type = event;
  352. entry->counter = counter;
  353. list_add_tail(&entry->list, &file->async_file->event_list);
  354. if (obj_list)
  355. list_add_tail(&entry->obj_list, obj_list);
  356. spin_unlock_irqrestore(&file->async_file->lock, flags);
  357. wake_up_interruptible(&file->async_file->poll_wait);
  358. kill_fasync(&file->async_file->async_queue, SIGIO, POLL_IN);
  359. }
  360. void ib_uverbs_cq_event_handler(struct ib_event *event, void *context_ptr)
  361. {
  362. struct ib_ucq_object *uobj = container_of(event->element.cq->uobject,
  363. struct ib_ucq_object, uobject);
  364. ib_uverbs_async_handler(uobj->uverbs_file, uobj->uobject.user_handle,
  365. event->event, &uobj->async_list,
  366. &uobj->async_events_reported);
  367. }
  368. void ib_uverbs_qp_event_handler(struct ib_event *event, void *context_ptr)
  369. {
  370. struct ib_uevent_object *uobj;
  371. uobj = container_of(event->element.qp->uobject,
  372. struct ib_uevent_object, uobject);
  373. ib_uverbs_async_handler(context_ptr, uobj->uobject.user_handle,
  374. event->event, &uobj->event_list,
  375. &uobj->events_reported);
  376. }
  377. void ib_uverbs_srq_event_handler(struct ib_event *event, void *context_ptr)
  378. {
  379. struct ib_uevent_object *uobj;
  380. uobj = container_of(event->element.srq->uobject,
  381. struct ib_uevent_object, uobject);
  382. ib_uverbs_async_handler(context_ptr, uobj->uobject.user_handle,
  383. event->event, &uobj->event_list,
  384. &uobj->events_reported);
  385. }
  386. void ib_uverbs_event_handler(struct ib_event_handler *handler,
  387. struct ib_event *event)
  388. {
  389. struct ib_uverbs_file *file =
  390. container_of(handler, struct ib_uverbs_file, event_handler);
  391. ib_uverbs_async_handler(file, event->element.port_num, event->event,
  392. NULL, NULL);
  393. }
  394. struct file *ib_uverbs_alloc_event_file(struct ib_uverbs_file *uverbs_file,
  395. int is_async)
  396. {
  397. struct ib_uverbs_event_file *ev_file;
  398. struct file *filp;
  399. ev_file = kmalloc(sizeof *ev_file, GFP_KERNEL);
  400. if (!ev_file)
  401. return ERR_PTR(-ENOMEM);
  402. kref_init(&ev_file->ref);
  403. spin_lock_init(&ev_file->lock);
  404. INIT_LIST_HEAD(&ev_file->event_list);
  405. init_waitqueue_head(&ev_file->poll_wait);
  406. ev_file->uverbs_file = uverbs_file;
  407. ev_file->async_queue = NULL;
  408. ev_file->is_async = is_async;
  409. ev_file->is_closed = 0;
  410. filp = anon_inode_getfile("[infinibandevent]", &uverbs_event_fops,
  411. ev_file, O_RDONLY);
  412. if (IS_ERR(filp))
  413. kfree(ev_file);
  414. return filp;
  415. }
  416. /*
  417. * Look up a completion event file by FD. If lookup is successful,
  418. * takes a ref to the event file struct that it returns; if
  419. * unsuccessful, returns NULL.
  420. */
  421. struct ib_uverbs_event_file *ib_uverbs_lookup_comp_file(int fd)
  422. {
  423. struct ib_uverbs_event_file *ev_file = NULL;
  424. struct file *filp;
  425. filp = fget(fd);
  426. if (!filp)
  427. return NULL;
  428. if (filp->f_op != &uverbs_event_fops)
  429. goto out;
  430. ev_file = filp->private_data;
  431. if (ev_file->is_async) {
  432. ev_file = NULL;
  433. goto out;
  434. }
  435. kref_get(&ev_file->ref);
  436. out:
  437. fput(filp);
  438. return ev_file;
  439. }
  440. static ssize_t ib_uverbs_write(struct file *filp, const char __user *buf,
  441. size_t count, loff_t *pos)
  442. {
  443. struct ib_uverbs_file *file = filp->private_data;
  444. struct ib_uverbs_cmd_hdr hdr;
  445. if (count < sizeof hdr)
  446. return -EINVAL;
  447. if (copy_from_user(&hdr, buf, sizeof hdr))
  448. return -EFAULT;
  449. if (hdr.in_words * 4 != count)
  450. return -EINVAL;
  451. if (hdr.command < 0 ||
  452. hdr.command >= ARRAY_SIZE(uverbs_cmd_table) ||
  453. !uverbs_cmd_table[hdr.command])
  454. return -EINVAL;
  455. if (!file->ucontext &&
  456. hdr.command != IB_USER_VERBS_CMD_GET_CONTEXT)
  457. return -EINVAL;
  458. if (!(file->device->ib_dev->uverbs_cmd_mask & (1ull << hdr.command)))
  459. return -ENOSYS;
  460. return uverbs_cmd_table[hdr.command](file, buf + sizeof hdr,
  461. hdr.in_words * 4, hdr.out_words * 4);
  462. }
  463. static int ib_uverbs_mmap(struct file *filp, struct vm_area_struct *vma)
  464. {
  465. struct ib_uverbs_file *file = filp->private_data;
  466. if (!file->ucontext)
  467. return -ENODEV;
  468. else
  469. return file->device->ib_dev->mmap(file->ucontext, vma);
  470. }
  471. /*
  472. * ib_uverbs_open() does not need the BKL:
  473. *
  474. * - the ib_uverbs_device structures are properly reference counted and
  475. * everything else is purely local to the file being created, so
  476. * races against other open calls are not a problem;
  477. * - there is no ioctl method to race against;
  478. * - the open method will either immediately run -ENXIO, or all
  479. * required initialization will be done.
  480. */
  481. static int ib_uverbs_open(struct inode *inode, struct file *filp)
  482. {
  483. struct ib_uverbs_device *dev;
  484. struct ib_uverbs_file *file;
  485. int ret;
  486. dev = container_of(inode->i_cdev, struct ib_uverbs_device, cdev);
  487. if (dev)
  488. kref_get(&dev->ref);
  489. else
  490. return -ENXIO;
  491. if (!try_module_get(dev->ib_dev->owner)) {
  492. ret = -ENODEV;
  493. goto err;
  494. }
  495. file = kmalloc(sizeof *file, GFP_KERNEL);
  496. if (!file) {
  497. ret = -ENOMEM;
  498. goto err_module;
  499. }
  500. file->device = dev;
  501. file->ucontext = NULL;
  502. file->async_file = NULL;
  503. kref_init(&file->ref);
  504. mutex_init(&file->mutex);
  505. filp->private_data = file;
  506. return 0;
  507. err_module:
  508. module_put(dev->ib_dev->owner);
  509. err:
  510. kref_put(&dev->ref, ib_uverbs_release_dev);
  511. return ret;
  512. }
  513. static int ib_uverbs_close(struct inode *inode, struct file *filp)
  514. {
  515. struct ib_uverbs_file *file = filp->private_data;
  516. ib_uverbs_cleanup_ucontext(file, file->ucontext);
  517. if (file->async_file)
  518. kref_put(&file->async_file->ref, ib_uverbs_release_event_file);
  519. kref_put(&file->ref, ib_uverbs_release_file);
  520. return 0;
  521. }
  522. static const struct file_operations uverbs_fops = {
  523. .owner = THIS_MODULE,
  524. .write = ib_uverbs_write,
  525. .open = ib_uverbs_open,
  526. .release = ib_uverbs_close
  527. };
  528. static const struct file_operations uverbs_mmap_fops = {
  529. .owner = THIS_MODULE,
  530. .write = ib_uverbs_write,
  531. .mmap = ib_uverbs_mmap,
  532. .open = ib_uverbs_open,
  533. .release = ib_uverbs_close
  534. };
  535. static struct ib_client uverbs_client = {
  536. .name = "uverbs",
  537. .add = ib_uverbs_add_one,
  538. .remove = ib_uverbs_remove_one
  539. };
  540. static ssize_t show_ibdev(struct device *device, struct device_attribute *attr,
  541. char *buf)
  542. {
  543. struct ib_uverbs_device *dev = dev_get_drvdata(device);
  544. if (!dev)
  545. return -ENODEV;
  546. return sprintf(buf, "%s\n", dev->ib_dev->name);
  547. }
  548. static DEVICE_ATTR(ibdev, S_IRUGO, show_ibdev, NULL);
  549. static ssize_t show_dev_abi_version(struct device *device,
  550. struct device_attribute *attr, char *buf)
  551. {
  552. struct ib_uverbs_device *dev = dev_get_drvdata(device);
  553. if (!dev)
  554. return -ENODEV;
  555. return sprintf(buf, "%d\n", dev->ib_dev->uverbs_abi_ver);
  556. }
  557. static DEVICE_ATTR(abi_version, S_IRUGO, show_dev_abi_version, NULL);
  558. static CLASS_ATTR_STRING(abi_version, S_IRUGO,
  559. __stringify(IB_USER_VERBS_ABI_VERSION));
  560. static dev_t overflow_maj;
  561. static DECLARE_BITMAP(overflow_map, IB_UVERBS_MAX_DEVICES);
  562. /*
  563. * If we have more than IB_UVERBS_MAX_DEVICES, dynamically overflow by
  564. * requesting a new major number and doubling the number of max devices we
  565. * support. It's stupid, but simple.
  566. */
  567. static int find_overflow_devnum(void)
  568. {
  569. int ret;
  570. if (!overflow_maj) {
  571. ret = alloc_chrdev_region(&overflow_maj, 0, IB_UVERBS_MAX_DEVICES,
  572. "infiniband_verbs");
  573. if (ret) {
  574. printk(KERN_ERR "user_verbs: couldn't register dynamic device number\n");
  575. return ret;
  576. }
  577. }
  578. ret = find_first_zero_bit(overflow_map, IB_UVERBS_MAX_DEVICES);
  579. if (ret >= IB_UVERBS_MAX_DEVICES)
  580. return -1;
  581. return ret;
  582. }
  583. static void ib_uverbs_add_one(struct ib_device *device)
  584. {
  585. int devnum;
  586. dev_t base;
  587. struct ib_uverbs_device *uverbs_dev;
  588. if (!device->alloc_ucontext)
  589. return;
  590. uverbs_dev = kzalloc(sizeof *uverbs_dev, GFP_KERNEL);
  591. if (!uverbs_dev)
  592. return;
  593. kref_init(&uverbs_dev->ref);
  594. init_completion(&uverbs_dev->comp);
  595. spin_lock(&map_lock);
  596. devnum = find_first_zero_bit(dev_map, IB_UVERBS_MAX_DEVICES);
  597. if (devnum >= IB_UVERBS_MAX_DEVICES) {
  598. spin_unlock(&map_lock);
  599. devnum = find_overflow_devnum();
  600. if (devnum < 0)
  601. goto err;
  602. spin_lock(&map_lock);
  603. uverbs_dev->devnum = devnum + IB_UVERBS_MAX_DEVICES;
  604. base = devnum + overflow_maj;
  605. set_bit(devnum, overflow_map);
  606. } else {
  607. uverbs_dev->devnum = devnum;
  608. base = devnum + IB_UVERBS_BASE_DEV;
  609. set_bit(devnum, dev_map);
  610. }
  611. spin_unlock(&map_lock);
  612. uverbs_dev->ib_dev = device;
  613. uverbs_dev->num_comp_vectors = device->num_comp_vectors;
  614. cdev_init(&uverbs_dev->cdev, NULL);
  615. uverbs_dev->cdev.owner = THIS_MODULE;
  616. uverbs_dev->cdev.ops = device->mmap ? &uverbs_mmap_fops : &uverbs_fops;
  617. kobject_set_name(&uverbs_dev->cdev.kobj, "uverbs%d", uverbs_dev->devnum);
  618. if (cdev_add(&uverbs_dev->cdev, base, 1))
  619. goto err_cdev;
  620. uverbs_dev->dev = device_create(uverbs_class, device->dma_device,
  621. uverbs_dev->cdev.dev, uverbs_dev,
  622. "uverbs%d", uverbs_dev->devnum);
  623. if (IS_ERR(uverbs_dev->dev))
  624. goto err_cdev;
  625. if (device_create_file(uverbs_dev->dev, &dev_attr_ibdev))
  626. goto err_class;
  627. if (device_create_file(uverbs_dev->dev, &dev_attr_abi_version))
  628. goto err_class;
  629. ib_set_client_data(device, &uverbs_client, uverbs_dev);
  630. return;
  631. err_class:
  632. device_destroy(uverbs_class, uverbs_dev->cdev.dev);
  633. err_cdev:
  634. cdev_del(&uverbs_dev->cdev);
  635. if (uverbs_dev->devnum < IB_UVERBS_MAX_DEVICES)
  636. clear_bit(devnum, dev_map);
  637. else
  638. clear_bit(devnum, overflow_map);
  639. err:
  640. kref_put(&uverbs_dev->ref, ib_uverbs_release_dev);
  641. wait_for_completion(&uverbs_dev->comp);
  642. kfree(uverbs_dev);
  643. return;
  644. }
  645. static void ib_uverbs_remove_one(struct ib_device *device)
  646. {
  647. struct ib_uverbs_device *uverbs_dev = ib_get_client_data(device, &uverbs_client);
  648. if (!uverbs_dev)
  649. return;
  650. dev_set_drvdata(uverbs_dev->dev, NULL);
  651. device_destroy(uverbs_class, uverbs_dev->cdev.dev);
  652. cdev_del(&uverbs_dev->cdev);
  653. if (uverbs_dev->devnum < IB_UVERBS_MAX_DEVICES)
  654. clear_bit(uverbs_dev->devnum, dev_map);
  655. else
  656. clear_bit(uverbs_dev->devnum - IB_UVERBS_MAX_DEVICES, overflow_map);
  657. kref_put(&uverbs_dev->ref, ib_uverbs_release_dev);
  658. wait_for_completion(&uverbs_dev->comp);
  659. kfree(uverbs_dev);
  660. }
  661. static int __init ib_uverbs_init(void)
  662. {
  663. int ret;
  664. ret = register_chrdev_region(IB_UVERBS_BASE_DEV, IB_UVERBS_MAX_DEVICES,
  665. "infiniband_verbs");
  666. if (ret) {
  667. printk(KERN_ERR "user_verbs: couldn't register device number\n");
  668. goto out;
  669. }
  670. uverbs_class = class_create(THIS_MODULE, "infiniband_verbs");
  671. if (IS_ERR(uverbs_class)) {
  672. ret = PTR_ERR(uverbs_class);
  673. printk(KERN_ERR "user_verbs: couldn't create class infiniband_verbs\n");
  674. goto out_chrdev;
  675. }
  676. ret = class_create_file(uverbs_class, &class_attr_abi_version.attr);
  677. if (ret) {
  678. printk(KERN_ERR "user_verbs: couldn't create abi_version attribute\n");
  679. goto out_class;
  680. }
  681. ret = ib_register_client(&uverbs_client);
  682. if (ret) {
  683. printk(KERN_ERR "user_verbs: couldn't register client\n");
  684. goto out_class;
  685. }
  686. return 0;
  687. out_class:
  688. class_destroy(uverbs_class);
  689. out_chrdev:
  690. unregister_chrdev_region(IB_UVERBS_BASE_DEV, IB_UVERBS_MAX_DEVICES);
  691. out:
  692. return ret;
  693. }
  694. static void __exit ib_uverbs_cleanup(void)
  695. {
  696. ib_unregister_client(&uverbs_client);
  697. class_destroy(uverbs_class);
  698. unregister_chrdev_region(IB_UVERBS_BASE_DEV, IB_UVERBS_MAX_DEVICES);
  699. if (overflow_maj)
  700. unregister_chrdev_region(overflow_maj, IB_UVERBS_MAX_DEVICES);
  701. idr_destroy(&ib_uverbs_pd_idr);
  702. idr_destroy(&ib_uverbs_mr_idr);
  703. idr_destroy(&ib_uverbs_mw_idr);
  704. idr_destroy(&ib_uverbs_ah_idr);
  705. idr_destroy(&ib_uverbs_cq_idr);
  706. idr_destroy(&ib_uverbs_qp_idr);
  707. idr_destroy(&ib_uverbs_srq_idr);
  708. }
  709. module_init(ib_uverbs_init);
  710. module_exit(ib_uverbs_cleanup);