uverbs_main.c 27 KB

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