uverbs_main.c 26 KB

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