uverbs_main.c 24 KB

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