uverbs_main.c 27 KB

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