ucma.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399
  1. /*
  2. * Copyright (c) 2005-2006 Intel Corporation. All rights reserved.
  3. *
  4. * This software is available to you under a choice of one of two
  5. * licenses. You may choose to be licensed under the terms of the GNU
  6. * General Public License (GPL) Version 2, available from the file
  7. * COPYING in the main directory of this source tree, or the
  8. * OpenIB.org BSD license below:
  9. *
  10. * Redistribution and use in source and binary forms, with or
  11. * without modification, are permitted provided that the following
  12. * conditions are met:
  13. *
  14. * - Redistributions of source code must retain the above
  15. * copyright notice, this list of conditions and the following
  16. * disclaimer.
  17. *
  18. * - Redistributions in binary form must reproduce the above
  19. * copyright notice, this list of conditions and the following
  20. * disclaimer in the documentation and/or other materials
  21. * provided with the distribution.
  22. *
  23. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  27. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  28. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  29. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  30. * SOFTWARE.
  31. */
  32. #include <linux/completion.h>
  33. #include <linux/file.h>
  34. #include <linux/mutex.h>
  35. #include <linux/poll.h>
  36. #include <linux/sched.h>
  37. #include <linux/idr.h>
  38. #include <linux/in.h>
  39. #include <linux/in6.h>
  40. #include <linux/miscdevice.h>
  41. #include <linux/slab.h>
  42. #include <linux/sysctl.h>
  43. #include <linux/module.h>
  44. #include <rdma/rdma_user_cm.h>
  45. #include <rdma/ib_marshall.h>
  46. #include <rdma/rdma_cm.h>
  47. #include <rdma/rdma_cm_ib.h>
  48. MODULE_AUTHOR("Sean Hefty");
  49. MODULE_DESCRIPTION("RDMA Userspace Connection Manager Access");
  50. MODULE_LICENSE("Dual BSD/GPL");
  51. static unsigned int max_backlog = 1024;
  52. static struct ctl_table_header *ucma_ctl_table_hdr;
  53. static ctl_table ucma_ctl_table[] = {
  54. {
  55. .procname = "max_backlog",
  56. .data = &max_backlog,
  57. .maxlen = sizeof max_backlog,
  58. .mode = 0644,
  59. .proc_handler = proc_dointvec,
  60. },
  61. { }
  62. };
  63. struct ucma_file {
  64. struct mutex mut;
  65. struct file *filp;
  66. struct list_head ctx_list;
  67. struct list_head event_list;
  68. wait_queue_head_t poll_wait;
  69. };
  70. struct ucma_context {
  71. int id;
  72. struct completion comp;
  73. atomic_t ref;
  74. int events_reported;
  75. int backlog;
  76. struct ucma_file *file;
  77. struct rdma_cm_id *cm_id;
  78. u64 uid;
  79. struct list_head list;
  80. struct list_head mc_list;
  81. };
  82. struct ucma_multicast {
  83. struct ucma_context *ctx;
  84. int id;
  85. int events_reported;
  86. u64 uid;
  87. struct list_head list;
  88. struct sockaddr_storage addr;
  89. };
  90. struct ucma_event {
  91. struct ucma_context *ctx;
  92. struct ucma_multicast *mc;
  93. struct list_head list;
  94. struct rdma_cm_id *cm_id;
  95. struct rdma_ucm_event_resp resp;
  96. };
  97. static DEFINE_MUTEX(mut);
  98. static DEFINE_IDR(ctx_idr);
  99. static DEFINE_IDR(multicast_idr);
  100. static inline struct ucma_context *_ucma_find_context(int id,
  101. struct ucma_file *file)
  102. {
  103. struct ucma_context *ctx;
  104. ctx = idr_find(&ctx_idr, id);
  105. if (!ctx)
  106. ctx = ERR_PTR(-ENOENT);
  107. else if (ctx->file != file)
  108. ctx = ERR_PTR(-EINVAL);
  109. return ctx;
  110. }
  111. static struct ucma_context *ucma_get_ctx(struct ucma_file *file, int id)
  112. {
  113. struct ucma_context *ctx;
  114. mutex_lock(&mut);
  115. ctx = _ucma_find_context(id, file);
  116. if (!IS_ERR(ctx))
  117. atomic_inc(&ctx->ref);
  118. mutex_unlock(&mut);
  119. return ctx;
  120. }
  121. static void ucma_put_ctx(struct ucma_context *ctx)
  122. {
  123. if (atomic_dec_and_test(&ctx->ref))
  124. complete(&ctx->comp);
  125. }
  126. static struct ucma_context *ucma_alloc_ctx(struct ucma_file *file)
  127. {
  128. struct ucma_context *ctx;
  129. ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
  130. if (!ctx)
  131. return NULL;
  132. atomic_set(&ctx->ref, 1);
  133. init_completion(&ctx->comp);
  134. INIT_LIST_HEAD(&ctx->mc_list);
  135. ctx->file = file;
  136. mutex_lock(&mut);
  137. ctx->id = idr_alloc(&ctx_idr, ctx, 0, 0, GFP_KERNEL);
  138. mutex_unlock(&mut);
  139. if (ctx->id < 0)
  140. goto error;
  141. list_add_tail(&ctx->list, &file->ctx_list);
  142. return ctx;
  143. error:
  144. kfree(ctx);
  145. return NULL;
  146. }
  147. static struct ucma_multicast* ucma_alloc_multicast(struct ucma_context *ctx)
  148. {
  149. struct ucma_multicast *mc;
  150. mc = kzalloc(sizeof(*mc), GFP_KERNEL);
  151. if (!mc)
  152. return NULL;
  153. mutex_lock(&mut);
  154. mc->id = idr_alloc(&multicast_idr, mc, 0, 0, GFP_KERNEL);
  155. mutex_unlock(&mut);
  156. if (mc->id < 0)
  157. goto error;
  158. mc->ctx = ctx;
  159. list_add_tail(&mc->list, &ctx->mc_list);
  160. return mc;
  161. error:
  162. kfree(mc);
  163. return NULL;
  164. }
  165. static void ucma_copy_conn_event(struct rdma_ucm_conn_param *dst,
  166. struct rdma_conn_param *src)
  167. {
  168. if (src->private_data_len)
  169. memcpy(dst->private_data, src->private_data,
  170. src->private_data_len);
  171. dst->private_data_len = src->private_data_len;
  172. dst->responder_resources =src->responder_resources;
  173. dst->initiator_depth = src->initiator_depth;
  174. dst->flow_control = src->flow_control;
  175. dst->retry_count = src->retry_count;
  176. dst->rnr_retry_count = src->rnr_retry_count;
  177. dst->srq = src->srq;
  178. dst->qp_num = src->qp_num;
  179. }
  180. static void ucma_copy_ud_event(struct rdma_ucm_ud_param *dst,
  181. struct rdma_ud_param *src)
  182. {
  183. if (src->private_data_len)
  184. memcpy(dst->private_data, src->private_data,
  185. src->private_data_len);
  186. dst->private_data_len = src->private_data_len;
  187. ib_copy_ah_attr_to_user(&dst->ah_attr, &src->ah_attr);
  188. dst->qp_num = src->qp_num;
  189. dst->qkey = src->qkey;
  190. }
  191. static void ucma_set_event_context(struct ucma_context *ctx,
  192. struct rdma_cm_event *event,
  193. struct ucma_event *uevent)
  194. {
  195. uevent->ctx = ctx;
  196. switch (event->event) {
  197. case RDMA_CM_EVENT_MULTICAST_JOIN:
  198. case RDMA_CM_EVENT_MULTICAST_ERROR:
  199. uevent->mc = (struct ucma_multicast *)
  200. event->param.ud.private_data;
  201. uevent->resp.uid = uevent->mc->uid;
  202. uevent->resp.id = uevent->mc->id;
  203. break;
  204. default:
  205. uevent->resp.uid = ctx->uid;
  206. uevent->resp.id = ctx->id;
  207. break;
  208. }
  209. }
  210. static int ucma_event_handler(struct rdma_cm_id *cm_id,
  211. struct rdma_cm_event *event)
  212. {
  213. struct ucma_event *uevent;
  214. struct ucma_context *ctx = cm_id->context;
  215. int ret = 0;
  216. uevent = kzalloc(sizeof(*uevent), GFP_KERNEL);
  217. if (!uevent)
  218. return event->event == RDMA_CM_EVENT_CONNECT_REQUEST;
  219. mutex_lock(&ctx->file->mut);
  220. uevent->cm_id = cm_id;
  221. ucma_set_event_context(ctx, event, uevent);
  222. uevent->resp.event = event->event;
  223. uevent->resp.status = event->status;
  224. if (cm_id->qp_type == IB_QPT_UD)
  225. ucma_copy_ud_event(&uevent->resp.param.ud, &event->param.ud);
  226. else
  227. ucma_copy_conn_event(&uevent->resp.param.conn,
  228. &event->param.conn);
  229. if (event->event == RDMA_CM_EVENT_CONNECT_REQUEST) {
  230. if (!ctx->backlog) {
  231. ret = -ENOMEM;
  232. kfree(uevent);
  233. goto out;
  234. }
  235. ctx->backlog--;
  236. } else if (!ctx->uid) {
  237. /*
  238. * We ignore events for new connections until userspace has set
  239. * their context. This can only happen if an error occurs on a
  240. * new connection before the user accepts it. This is okay,
  241. * since the accept will just fail later.
  242. */
  243. kfree(uevent);
  244. goto out;
  245. }
  246. list_add_tail(&uevent->list, &ctx->file->event_list);
  247. wake_up_interruptible(&ctx->file->poll_wait);
  248. out:
  249. mutex_unlock(&ctx->file->mut);
  250. return ret;
  251. }
  252. static ssize_t ucma_get_event(struct ucma_file *file, const char __user *inbuf,
  253. int in_len, int out_len)
  254. {
  255. struct ucma_context *ctx;
  256. struct rdma_ucm_get_event cmd;
  257. struct ucma_event *uevent;
  258. int ret = 0;
  259. if (out_len < sizeof uevent->resp)
  260. return -ENOSPC;
  261. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  262. return -EFAULT;
  263. mutex_lock(&file->mut);
  264. while (list_empty(&file->event_list)) {
  265. mutex_unlock(&file->mut);
  266. if (file->filp->f_flags & O_NONBLOCK)
  267. return -EAGAIN;
  268. if (wait_event_interruptible(file->poll_wait,
  269. !list_empty(&file->event_list)))
  270. return -ERESTARTSYS;
  271. mutex_lock(&file->mut);
  272. }
  273. uevent = list_entry(file->event_list.next, struct ucma_event, list);
  274. if (uevent->resp.event == RDMA_CM_EVENT_CONNECT_REQUEST) {
  275. ctx = ucma_alloc_ctx(file);
  276. if (!ctx) {
  277. ret = -ENOMEM;
  278. goto done;
  279. }
  280. uevent->ctx->backlog++;
  281. ctx->cm_id = uevent->cm_id;
  282. ctx->cm_id->context = ctx;
  283. uevent->resp.id = ctx->id;
  284. }
  285. if (copy_to_user((void __user *)(unsigned long)cmd.response,
  286. &uevent->resp, sizeof uevent->resp)) {
  287. ret = -EFAULT;
  288. goto done;
  289. }
  290. list_del(&uevent->list);
  291. uevent->ctx->events_reported++;
  292. if (uevent->mc)
  293. uevent->mc->events_reported++;
  294. kfree(uevent);
  295. done:
  296. mutex_unlock(&file->mut);
  297. return ret;
  298. }
  299. static int ucma_get_qp_type(struct rdma_ucm_create_id *cmd, enum ib_qp_type *qp_type)
  300. {
  301. switch (cmd->ps) {
  302. case RDMA_PS_TCP:
  303. *qp_type = IB_QPT_RC;
  304. return 0;
  305. case RDMA_PS_UDP:
  306. case RDMA_PS_IPOIB:
  307. *qp_type = IB_QPT_UD;
  308. return 0;
  309. case RDMA_PS_IB:
  310. *qp_type = cmd->qp_type;
  311. return 0;
  312. default:
  313. return -EINVAL;
  314. }
  315. }
  316. static ssize_t ucma_create_id(struct ucma_file *file, const char __user *inbuf,
  317. int in_len, int out_len)
  318. {
  319. struct rdma_ucm_create_id cmd;
  320. struct rdma_ucm_create_id_resp resp;
  321. struct ucma_context *ctx;
  322. enum ib_qp_type qp_type;
  323. int ret;
  324. if (out_len < sizeof(resp))
  325. return -ENOSPC;
  326. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  327. return -EFAULT;
  328. ret = ucma_get_qp_type(&cmd, &qp_type);
  329. if (ret)
  330. return ret;
  331. mutex_lock(&file->mut);
  332. ctx = ucma_alloc_ctx(file);
  333. mutex_unlock(&file->mut);
  334. if (!ctx)
  335. return -ENOMEM;
  336. ctx->uid = cmd.uid;
  337. ctx->cm_id = rdma_create_id(ucma_event_handler, ctx, cmd.ps, qp_type);
  338. if (IS_ERR(ctx->cm_id)) {
  339. ret = PTR_ERR(ctx->cm_id);
  340. goto err1;
  341. }
  342. resp.id = ctx->id;
  343. if (copy_to_user((void __user *)(unsigned long)cmd.response,
  344. &resp, sizeof(resp))) {
  345. ret = -EFAULT;
  346. goto err2;
  347. }
  348. return 0;
  349. err2:
  350. rdma_destroy_id(ctx->cm_id);
  351. err1:
  352. mutex_lock(&mut);
  353. idr_remove(&ctx_idr, ctx->id);
  354. mutex_unlock(&mut);
  355. kfree(ctx);
  356. return ret;
  357. }
  358. static void ucma_cleanup_multicast(struct ucma_context *ctx)
  359. {
  360. struct ucma_multicast *mc, *tmp;
  361. mutex_lock(&mut);
  362. list_for_each_entry_safe(mc, tmp, &ctx->mc_list, list) {
  363. list_del(&mc->list);
  364. idr_remove(&multicast_idr, mc->id);
  365. kfree(mc);
  366. }
  367. mutex_unlock(&mut);
  368. }
  369. static void ucma_cleanup_mc_events(struct ucma_multicast *mc)
  370. {
  371. struct ucma_event *uevent, *tmp;
  372. list_for_each_entry_safe(uevent, tmp, &mc->ctx->file->event_list, list) {
  373. if (uevent->mc != mc)
  374. continue;
  375. list_del(&uevent->list);
  376. kfree(uevent);
  377. }
  378. }
  379. /*
  380. * We cannot hold file->mut when calling rdma_destroy_id() or we can
  381. * deadlock. We also acquire file->mut in ucma_event_handler(), and
  382. * rdma_destroy_id() will wait until all callbacks have completed.
  383. */
  384. static int ucma_free_ctx(struct ucma_context *ctx)
  385. {
  386. int events_reported;
  387. struct ucma_event *uevent, *tmp;
  388. LIST_HEAD(list);
  389. /* No new events will be generated after destroying the id. */
  390. rdma_destroy_id(ctx->cm_id);
  391. ucma_cleanup_multicast(ctx);
  392. /* Cleanup events not yet reported to the user. */
  393. mutex_lock(&ctx->file->mut);
  394. list_for_each_entry_safe(uevent, tmp, &ctx->file->event_list, list) {
  395. if (uevent->ctx == ctx)
  396. list_move_tail(&uevent->list, &list);
  397. }
  398. list_del(&ctx->list);
  399. mutex_unlock(&ctx->file->mut);
  400. list_for_each_entry_safe(uevent, tmp, &list, list) {
  401. list_del(&uevent->list);
  402. if (uevent->resp.event == RDMA_CM_EVENT_CONNECT_REQUEST)
  403. rdma_destroy_id(uevent->cm_id);
  404. kfree(uevent);
  405. }
  406. events_reported = ctx->events_reported;
  407. kfree(ctx);
  408. return events_reported;
  409. }
  410. static ssize_t ucma_destroy_id(struct ucma_file *file, const char __user *inbuf,
  411. int in_len, int out_len)
  412. {
  413. struct rdma_ucm_destroy_id cmd;
  414. struct rdma_ucm_destroy_id_resp resp;
  415. struct ucma_context *ctx;
  416. int ret = 0;
  417. if (out_len < sizeof(resp))
  418. return -ENOSPC;
  419. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  420. return -EFAULT;
  421. mutex_lock(&mut);
  422. ctx = _ucma_find_context(cmd.id, file);
  423. if (!IS_ERR(ctx))
  424. idr_remove(&ctx_idr, ctx->id);
  425. mutex_unlock(&mut);
  426. if (IS_ERR(ctx))
  427. return PTR_ERR(ctx);
  428. ucma_put_ctx(ctx);
  429. wait_for_completion(&ctx->comp);
  430. resp.events_reported = ucma_free_ctx(ctx);
  431. if (copy_to_user((void __user *)(unsigned long)cmd.response,
  432. &resp, sizeof(resp)))
  433. ret = -EFAULT;
  434. return ret;
  435. }
  436. static ssize_t ucma_bind_addr(struct ucma_file *file, const char __user *inbuf,
  437. int in_len, int out_len)
  438. {
  439. struct rdma_ucm_bind_addr cmd;
  440. struct ucma_context *ctx;
  441. int ret;
  442. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  443. return -EFAULT;
  444. ctx = ucma_get_ctx(file, cmd.id);
  445. if (IS_ERR(ctx))
  446. return PTR_ERR(ctx);
  447. ret = rdma_bind_addr(ctx->cm_id, (struct sockaddr *) &cmd.addr);
  448. ucma_put_ctx(ctx);
  449. return ret;
  450. }
  451. static ssize_t ucma_resolve_addr(struct ucma_file *file,
  452. const char __user *inbuf,
  453. int in_len, int out_len)
  454. {
  455. struct rdma_ucm_resolve_addr cmd;
  456. struct ucma_context *ctx;
  457. int ret;
  458. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  459. return -EFAULT;
  460. ctx = ucma_get_ctx(file, cmd.id);
  461. if (IS_ERR(ctx))
  462. return PTR_ERR(ctx);
  463. ret = rdma_resolve_addr(ctx->cm_id, (struct sockaddr *) &cmd.src_addr,
  464. (struct sockaddr *) &cmd.dst_addr,
  465. cmd.timeout_ms);
  466. ucma_put_ctx(ctx);
  467. return ret;
  468. }
  469. static ssize_t ucma_resolve_route(struct ucma_file *file,
  470. const char __user *inbuf,
  471. int in_len, int out_len)
  472. {
  473. struct rdma_ucm_resolve_route cmd;
  474. struct ucma_context *ctx;
  475. int ret;
  476. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  477. return -EFAULT;
  478. ctx = ucma_get_ctx(file, cmd.id);
  479. if (IS_ERR(ctx))
  480. return PTR_ERR(ctx);
  481. ret = rdma_resolve_route(ctx->cm_id, cmd.timeout_ms);
  482. ucma_put_ctx(ctx);
  483. return ret;
  484. }
  485. static void ucma_copy_ib_route(struct rdma_ucm_query_route_resp *resp,
  486. struct rdma_route *route)
  487. {
  488. struct rdma_dev_addr *dev_addr;
  489. resp->num_paths = route->num_paths;
  490. switch (route->num_paths) {
  491. case 0:
  492. dev_addr = &route->addr.dev_addr;
  493. rdma_addr_get_dgid(dev_addr,
  494. (union ib_gid *) &resp->ib_route[0].dgid);
  495. rdma_addr_get_sgid(dev_addr,
  496. (union ib_gid *) &resp->ib_route[0].sgid);
  497. resp->ib_route[0].pkey = cpu_to_be16(ib_addr_get_pkey(dev_addr));
  498. break;
  499. case 2:
  500. ib_copy_path_rec_to_user(&resp->ib_route[1],
  501. &route->path_rec[1]);
  502. /* fall through */
  503. case 1:
  504. ib_copy_path_rec_to_user(&resp->ib_route[0],
  505. &route->path_rec[0]);
  506. break;
  507. default:
  508. break;
  509. }
  510. }
  511. static void ucma_copy_iboe_route(struct rdma_ucm_query_route_resp *resp,
  512. struct rdma_route *route)
  513. {
  514. struct rdma_dev_addr *dev_addr;
  515. struct net_device *dev;
  516. u16 vid = 0;
  517. resp->num_paths = route->num_paths;
  518. switch (route->num_paths) {
  519. case 0:
  520. dev_addr = &route->addr.dev_addr;
  521. dev = dev_get_by_index(&init_net, dev_addr->bound_dev_if);
  522. if (dev) {
  523. vid = rdma_vlan_dev_vlan_id(dev);
  524. dev_put(dev);
  525. }
  526. iboe_mac_vlan_to_ll((union ib_gid *) &resp->ib_route[0].dgid,
  527. dev_addr->dst_dev_addr, vid);
  528. iboe_addr_get_sgid(dev_addr,
  529. (union ib_gid *) &resp->ib_route[0].sgid);
  530. resp->ib_route[0].pkey = cpu_to_be16(0xffff);
  531. break;
  532. case 2:
  533. ib_copy_path_rec_to_user(&resp->ib_route[1],
  534. &route->path_rec[1]);
  535. /* fall through */
  536. case 1:
  537. ib_copy_path_rec_to_user(&resp->ib_route[0],
  538. &route->path_rec[0]);
  539. break;
  540. default:
  541. break;
  542. }
  543. }
  544. static void ucma_copy_iw_route(struct rdma_ucm_query_route_resp *resp,
  545. struct rdma_route *route)
  546. {
  547. struct rdma_dev_addr *dev_addr;
  548. dev_addr = &route->addr.dev_addr;
  549. rdma_addr_get_dgid(dev_addr, (union ib_gid *) &resp->ib_route[0].dgid);
  550. rdma_addr_get_sgid(dev_addr, (union ib_gid *) &resp->ib_route[0].sgid);
  551. }
  552. static ssize_t ucma_query_route(struct ucma_file *file,
  553. const char __user *inbuf,
  554. int in_len, int out_len)
  555. {
  556. struct rdma_ucm_query_route cmd;
  557. struct rdma_ucm_query_route_resp resp;
  558. struct ucma_context *ctx;
  559. struct sockaddr *addr;
  560. int ret = 0;
  561. if (out_len < sizeof(resp))
  562. return -ENOSPC;
  563. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  564. return -EFAULT;
  565. ctx = ucma_get_ctx(file, cmd.id);
  566. if (IS_ERR(ctx))
  567. return PTR_ERR(ctx);
  568. memset(&resp, 0, sizeof resp);
  569. addr = (struct sockaddr *) &ctx->cm_id->route.addr.src_addr;
  570. memcpy(&resp.src_addr, addr, addr->sa_family == AF_INET ?
  571. sizeof(struct sockaddr_in) :
  572. sizeof(struct sockaddr_in6));
  573. addr = (struct sockaddr *) &ctx->cm_id->route.addr.dst_addr;
  574. memcpy(&resp.dst_addr, addr, addr->sa_family == AF_INET ?
  575. sizeof(struct sockaddr_in) :
  576. sizeof(struct sockaddr_in6));
  577. if (!ctx->cm_id->device)
  578. goto out;
  579. resp.node_guid = (__force __u64) ctx->cm_id->device->node_guid;
  580. resp.port_num = ctx->cm_id->port_num;
  581. switch (rdma_node_get_transport(ctx->cm_id->device->node_type)) {
  582. case RDMA_TRANSPORT_IB:
  583. switch (rdma_port_get_link_layer(ctx->cm_id->device,
  584. ctx->cm_id->port_num)) {
  585. case IB_LINK_LAYER_INFINIBAND:
  586. ucma_copy_ib_route(&resp, &ctx->cm_id->route);
  587. break;
  588. case IB_LINK_LAYER_ETHERNET:
  589. ucma_copy_iboe_route(&resp, &ctx->cm_id->route);
  590. break;
  591. default:
  592. break;
  593. }
  594. break;
  595. case RDMA_TRANSPORT_IWARP:
  596. ucma_copy_iw_route(&resp, &ctx->cm_id->route);
  597. break;
  598. default:
  599. break;
  600. }
  601. out:
  602. if (copy_to_user((void __user *)(unsigned long)cmd.response,
  603. &resp, sizeof(resp)))
  604. ret = -EFAULT;
  605. ucma_put_ctx(ctx);
  606. return ret;
  607. }
  608. static void ucma_copy_conn_param(struct rdma_cm_id *id,
  609. struct rdma_conn_param *dst,
  610. struct rdma_ucm_conn_param *src)
  611. {
  612. dst->private_data = src->private_data;
  613. dst->private_data_len = src->private_data_len;
  614. dst->responder_resources =src->responder_resources;
  615. dst->initiator_depth = src->initiator_depth;
  616. dst->flow_control = src->flow_control;
  617. dst->retry_count = src->retry_count;
  618. dst->rnr_retry_count = src->rnr_retry_count;
  619. dst->srq = src->srq;
  620. dst->qp_num = src->qp_num;
  621. dst->qkey = (id->route.addr.src_addr.ss_family == AF_IB) ? src->qkey : 0;
  622. }
  623. static ssize_t ucma_connect(struct ucma_file *file, const char __user *inbuf,
  624. int in_len, int out_len)
  625. {
  626. struct rdma_ucm_connect cmd;
  627. struct rdma_conn_param conn_param;
  628. struct ucma_context *ctx;
  629. int ret;
  630. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  631. return -EFAULT;
  632. if (!cmd.conn_param.valid)
  633. return -EINVAL;
  634. ctx = ucma_get_ctx(file, cmd.id);
  635. if (IS_ERR(ctx))
  636. return PTR_ERR(ctx);
  637. ucma_copy_conn_param(ctx->cm_id, &conn_param, &cmd.conn_param);
  638. ret = rdma_connect(ctx->cm_id, &conn_param);
  639. ucma_put_ctx(ctx);
  640. return ret;
  641. }
  642. static ssize_t ucma_listen(struct ucma_file *file, const char __user *inbuf,
  643. int in_len, int out_len)
  644. {
  645. struct rdma_ucm_listen cmd;
  646. struct ucma_context *ctx;
  647. int ret;
  648. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  649. return -EFAULT;
  650. ctx = ucma_get_ctx(file, cmd.id);
  651. if (IS_ERR(ctx))
  652. return PTR_ERR(ctx);
  653. ctx->backlog = cmd.backlog > 0 && cmd.backlog < max_backlog ?
  654. cmd.backlog : max_backlog;
  655. ret = rdma_listen(ctx->cm_id, ctx->backlog);
  656. ucma_put_ctx(ctx);
  657. return ret;
  658. }
  659. static ssize_t ucma_accept(struct ucma_file *file, const char __user *inbuf,
  660. int in_len, int out_len)
  661. {
  662. struct rdma_ucm_accept cmd;
  663. struct rdma_conn_param conn_param;
  664. struct ucma_context *ctx;
  665. int ret;
  666. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  667. return -EFAULT;
  668. ctx = ucma_get_ctx(file, cmd.id);
  669. if (IS_ERR(ctx))
  670. return PTR_ERR(ctx);
  671. if (cmd.conn_param.valid) {
  672. ucma_copy_conn_param(ctx->cm_id, &conn_param, &cmd.conn_param);
  673. mutex_lock(&file->mut);
  674. ret = rdma_accept(ctx->cm_id, &conn_param);
  675. if (!ret)
  676. ctx->uid = cmd.uid;
  677. mutex_unlock(&file->mut);
  678. } else
  679. ret = rdma_accept(ctx->cm_id, NULL);
  680. ucma_put_ctx(ctx);
  681. return ret;
  682. }
  683. static ssize_t ucma_reject(struct ucma_file *file, const char __user *inbuf,
  684. int in_len, int out_len)
  685. {
  686. struct rdma_ucm_reject cmd;
  687. struct ucma_context *ctx;
  688. int ret;
  689. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  690. return -EFAULT;
  691. ctx = ucma_get_ctx(file, cmd.id);
  692. if (IS_ERR(ctx))
  693. return PTR_ERR(ctx);
  694. ret = rdma_reject(ctx->cm_id, cmd.private_data, cmd.private_data_len);
  695. ucma_put_ctx(ctx);
  696. return ret;
  697. }
  698. static ssize_t ucma_disconnect(struct ucma_file *file, const char __user *inbuf,
  699. int in_len, int out_len)
  700. {
  701. struct rdma_ucm_disconnect cmd;
  702. struct ucma_context *ctx;
  703. int ret;
  704. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  705. return -EFAULT;
  706. ctx = ucma_get_ctx(file, cmd.id);
  707. if (IS_ERR(ctx))
  708. return PTR_ERR(ctx);
  709. ret = rdma_disconnect(ctx->cm_id);
  710. ucma_put_ctx(ctx);
  711. return ret;
  712. }
  713. static ssize_t ucma_init_qp_attr(struct ucma_file *file,
  714. const char __user *inbuf,
  715. int in_len, int out_len)
  716. {
  717. struct rdma_ucm_init_qp_attr cmd;
  718. struct ib_uverbs_qp_attr resp;
  719. struct ucma_context *ctx;
  720. struct ib_qp_attr qp_attr;
  721. int ret;
  722. if (out_len < sizeof(resp))
  723. return -ENOSPC;
  724. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  725. return -EFAULT;
  726. ctx = ucma_get_ctx(file, cmd.id);
  727. if (IS_ERR(ctx))
  728. return PTR_ERR(ctx);
  729. resp.qp_attr_mask = 0;
  730. memset(&qp_attr, 0, sizeof qp_attr);
  731. qp_attr.qp_state = cmd.qp_state;
  732. ret = rdma_init_qp_attr(ctx->cm_id, &qp_attr, &resp.qp_attr_mask);
  733. if (ret)
  734. goto out;
  735. ib_copy_qp_attr_to_user(&resp, &qp_attr);
  736. if (copy_to_user((void __user *)(unsigned long)cmd.response,
  737. &resp, sizeof(resp)))
  738. ret = -EFAULT;
  739. out:
  740. ucma_put_ctx(ctx);
  741. return ret;
  742. }
  743. static int ucma_set_option_id(struct ucma_context *ctx, int optname,
  744. void *optval, size_t optlen)
  745. {
  746. int ret = 0;
  747. switch (optname) {
  748. case RDMA_OPTION_ID_TOS:
  749. if (optlen != sizeof(u8)) {
  750. ret = -EINVAL;
  751. break;
  752. }
  753. rdma_set_service_type(ctx->cm_id, *((u8 *) optval));
  754. break;
  755. case RDMA_OPTION_ID_REUSEADDR:
  756. if (optlen != sizeof(int)) {
  757. ret = -EINVAL;
  758. break;
  759. }
  760. ret = rdma_set_reuseaddr(ctx->cm_id, *((int *) optval) ? 1 : 0);
  761. break;
  762. case RDMA_OPTION_ID_AFONLY:
  763. if (optlen != sizeof(int)) {
  764. ret = -EINVAL;
  765. break;
  766. }
  767. ret = rdma_set_afonly(ctx->cm_id, *((int *) optval) ? 1 : 0);
  768. break;
  769. default:
  770. ret = -ENOSYS;
  771. }
  772. return ret;
  773. }
  774. static int ucma_set_ib_path(struct ucma_context *ctx,
  775. struct ib_path_rec_data *path_data, size_t optlen)
  776. {
  777. struct ib_sa_path_rec sa_path;
  778. struct rdma_cm_event event;
  779. int ret;
  780. if (optlen % sizeof(*path_data))
  781. return -EINVAL;
  782. for (; optlen; optlen -= sizeof(*path_data), path_data++) {
  783. if (path_data->flags == (IB_PATH_GMP | IB_PATH_PRIMARY |
  784. IB_PATH_BIDIRECTIONAL))
  785. break;
  786. }
  787. if (!optlen)
  788. return -EINVAL;
  789. ib_sa_unpack_path(path_data->path_rec, &sa_path);
  790. ret = rdma_set_ib_paths(ctx->cm_id, &sa_path, 1);
  791. if (ret)
  792. return ret;
  793. memset(&event, 0, sizeof event);
  794. event.event = RDMA_CM_EVENT_ROUTE_RESOLVED;
  795. return ucma_event_handler(ctx->cm_id, &event);
  796. }
  797. static int ucma_set_option_ib(struct ucma_context *ctx, int optname,
  798. void *optval, size_t optlen)
  799. {
  800. int ret;
  801. switch (optname) {
  802. case RDMA_OPTION_IB_PATH:
  803. ret = ucma_set_ib_path(ctx, optval, optlen);
  804. break;
  805. default:
  806. ret = -ENOSYS;
  807. }
  808. return ret;
  809. }
  810. static int ucma_set_option_level(struct ucma_context *ctx, int level,
  811. int optname, void *optval, size_t optlen)
  812. {
  813. int ret;
  814. switch (level) {
  815. case RDMA_OPTION_ID:
  816. ret = ucma_set_option_id(ctx, optname, optval, optlen);
  817. break;
  818. case RDMA_OPTION_IB:
  819. ret = ucma_set_option_ib(ctx, optname, optval, optlen);
  820. break;
  821. default:
  822. ret = -ENOSYS;
  823. }
  824. return ret;
  825. }
  826. static ssize_t ucma_set_option(struct ucma_file *file, const char __user *inbuf,
  827. int in_len, int out_len)
  828. {
  829. struct rdma_ucm_set_option cmd;
  830. struct ucma_context *ctx;
  831. void *optval;
  832. int ret;
  833. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  834. return -EFAULT;
  835. ctx = ucma_get_ctx(file, cmd.id);
  836. if (IS_ERR(ctx))
  837. return PTR_ERR(ctx);
  838. optval = memdup_user((void __user *) (unsigned long) cmd.optval,
  839. cmd.optlen);
  840. if (IS_ERR(optval)) {
  841. ret = PTR_ERR(optval);
  842. goto out;
  843. }
  844. ret = ucma_set_option_level(ctx, cmd.level, cmd.optname, optval,
  845. cmd.optlen);
  846. kfree(optval);
  847. out:
  848. ucma_put_ctx(ctx);
  849. return ret;
  850. }
  851. static ssize_t ucma_notify(struct ucma_file *file, const char __user *inbuf,
  852. int in_len, int out_len)
  853. {
  854. struct rdma_ucm_notify cmd;
  855. struct ucma_context *ctx;
  856. int ret;
  857. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  858. return -EFAULT;
  859. ctx = ucma_get_ctx(file, cmd.id);
  860. if (IS_ERR(ctx))
  861. return PTR_ERR(ctx);
  862. ret = rdma_notify(ctx->cm_id, (enum ib_event_type) cmd.event);
  863. ucma_put_ctx(ctx);
  864. return ret;
  865. }
  866. static ssize_t ucma_join_multicast(struct ucma_file *file,
  867. const char __user *inbuf,
  868. int in_len, int out_len)
  869. {
  870. struct rdma_ucm_join_mcast cmd;
  871. struct rdma_ucm_create_id_resp resp;
  872. struct ucma_context *ctx;
  873. struct ucma_multicast *mc;
  874. int ret;
  875. if (out_len < sizeof(resp))
  876. return -ENOSPC;
  877. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  878. return -EFAULT;
  879. ctx = ucma_get_ctx(file, cmd.id);
  880. if (IS_ERR(ctx))
  881. return PTR_ERR(ctx);
  882. mutex_lock(&file->mut);
  883. mc = ucma_alloc_multicast(ctx);
  884. if (!mc) {
  885. ret = -ENOMEM;
  886. goto err1;
  887. }
  888. mc->uid = cmd.uid;
  889. memcpy(&mc->addr, &cmd.addr, sizeof cmd.addr);
  890. ret = rdma_join_multicast(ctx->cm_id, (struct sockaddr *) &mc->addr, mc);
  891. if (ret)
  892. goto err2;
  893. resp.id = mc->id;
  894. if (copy_to_user((void __user *)(unsigned long)cmd.response,
  895. &resp, sizeof(resp))) {
  896. ret = -EFAULT;
  897. goto err3;
  898. }
  899. mutex_unlock(&file->mut);
  900. ucma_put_ctx(ctx);
  901. return 0;
  902. err3:
  903. rdma_leave_multicast(ctx->cm_id, (struct sockaddr *) &mc->addr);
  904. ucma_cleanup_mc_events(mc);
  905. err2:
  906. mutex_lock(&mut);
  907. idr_remove(&multicast_idr, mc->id);
  908. mutex_unlock(&mut);
  909. list_del(&mc->list);
  910. kfree(mc);
  911. err1:
  912. mutex_unlock(&file->mut);
  913. ucma_put_ctx(ctx);
  914. return ret;
  915. }
  916. static ssize_t ucma_leave_multicast(struct ucma_file *file,
  917. const char __user *inbuf,
  918. int in_len, int out_len)
  919. {
  920. struct rdma_ucm_destroy_id cmd;
  921. struct rdma_ucm_destroy_id_resp resp;
  922. struct ucma_multicast *mc;
  923. int ret = 0;
  924. if (out_len < sizeof(resp))
  925. return -ENOSPC;
  926. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  927. return -EFAULT;
  928. mutex_lock(&mut);
  929. mc = idr_find(&multicast_idr, cmd.id);
  930. if (!mc)
  931. mc = ERR_PTR(-ENOENT);
  932. else if (mc->ctx->file != file)
  933. mc = ERR_PTR(-EINVAL);
  934. else {
  935. idr_remove(&multicast_idr, mc->id);
  936. atomic_inc(&mc->ctx->ref);
  937. }
  938. mutex_unlock(&mut);
  939. if (IS_ERR(mc)) {
  940. ret = PTR_ERR(mc);
  941. goto out;
  942. }
  943. rdma_leave_multicast(mc->ctx->cm_id, (struct sockaddr *) &mc->addr);
  944. mutex_lock(&mc->ctx->file->mut);
  945. ucma_cleanup_mc_events(mc);
  946. list_del(&mc->list);
  947. mutex_unlock(&mc->ctx->file->mut);
  948. ucma_put_ctx(mc->ctx);
  949. resp.events_reported = mc->events_reported;
  950. kfree(mc);
  951. if (copy_to_user((void __user *)(unsigned long)cmd.response,
  952. &resp, sizeof(resp)))
  953. ret = -EFAULT;
  954. out:
  955. return ret;
  956. }
  957. static void ucma_lock_files(struct ucma_file *file1, struct ucma_file *file2)
  958. {
  959. /* Acquire mutex's based on pointer comparison to prevent deadlock. */
  960. if (file1 < file2) {
  961. mutex_lock(&file1->mut);
  962. mutex_lock(&file2->mut);
  963. } else {
  964. mutex_lock(&file2->mut);
  965. mutex_lock(&file1->mut);
  966. }
  967. }
  968. static void ucma_unlock_files(struct ucma_file *file1, struct ucma_file *file2)
  969. {
  970. if (file1 < file2) {
  971. mutex_unlock(&file2->mut);
  972. mutex_unlock(&file1->mut);
  973. } else {
  974. mutex_unlock(&file1->mut);
  975. mutex_unlock(&file2->mut);
  976. }
  977. }
  978. static void ucma_move_events(struct ucma_context *ctx, struct ucma_file *file)
  979. {
  980. struct ucma_event *uevent, *tmp;
  981. list_for_each_entry_safe(uevent, tmp, &ctx->file->event_list, list)
  982. if (uevent->ctx == ctx)
  983. list_move_tail(&uevent->list, &file->event_list);
  984. }
  985. static ssize_t ucma_migrate_id(struct ucma_file *new_file,
  986. const char __user *inbuf,
  987. int in_len, int out_len)
  988. {
  989. struct rdma_ucm_migrate_id cmd;
  990. struct rdma_ucm_migrate_resp resp;
  991. struct ucma_context *ctx;
  992. struct fd f;
  993. struct ucma_file *cur_file;
  994. int ret = 0;
  995. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  996. return -EFAULT;
  997. /* Get current fd to protect against it being closed */
  998. f = fdget(cmd.fd);
  999. if (!f.file)
  1000. return -ENOENT;
  1001. /* Validate current fd and prevent destruction of id. */
  1002. ctx = ucma_get_ctx(f.file->private_data, cmd.id);
  1003. if (IS_ERR(ctx)) {
  1004. ret = PTR_ERR(ctx);
  1005. goto file_put;
  1006. }
  1007. cur_file = ctx->file;
  1008. if (cur_file == new_file) {
  1009. resp.events_reported = ctx->events_reported;
  1010. goto response;
  1011. }
  1012. /*
  1013. * Migrate events between fd's, maintaining order, and avoiding new
  1014. * events being added before existing events.
  1015. */
  1016. ucma_lock_files(cur_file, new_file);
  1017. mutex_lock(&mut);
  1018. list_move_tail(&ctx->list, &new_file->ctx_list);
  1019. ucma_move_events(ctx, new_file);
  1020. ctx->file = new_file;
  1021. resp.events_reported = ctx->events_reported;
  1022. mutex_unlock(&mut);
  1023. ucma_unlock_files(cur_file, new_file);
  1024. response:
  1025. if (copy_to_user((void __user *)(unsigned long)cmd.response,
  1026. &resp, sizeof(resp)))
  1027. ret = -EFAULT;
  1028. ucma_put_ctx(ctx);
  1029. file_put:
  1030. fdput(f);
  1031. return ret;
  1032. }
  1033. static ssize_t (*ucma_cmd_table[])(struct ucma_file *file,
  1034. const char __user *inbuf,
  1035. int in_len, int out_len) = {
  1036. [RDMA_USER_CM_CMD_CREATE_ID] = ucma_create_id,
  1037. [RDMA_USER_CM_CMD_DESTROY_ID] = ucma_destroy_id,
  1038. [RDMA_USER_CM_CMD_BIND_ADDR] = ucma_bind_addr,
  1039. [RDMA_USER_CM_CMD_RESOLVE_ADDR] = ucma_resolve_addr,
  1040. [RDMA_USER_CM_CMD_RESOLVE_ROUTE]= ucma_resolve_route,
  1041. [RDMA_USER_CM_CMD_QUERY_ROUTE] = ucma_query_route,
  1042. [RDMA_USER_CM_CMD_CONNECT] = ucma_connect,
  1043. [RDMA_USER_CM_CMD_LISTEN] = ucma_listen,
  1044. [RDMA_USER_CM_CMD_ACCEPT] = ucma_accept,
  1045. [RDMA_USER_CM_CMD_REJECT] = ucma_reject,
  1046. [RDMA_USER_CM_CMD_DISCONNECT] = ucma_disconnect,
  1047. [RDMA_USER_CM_CMD_INIT_QP_ATTR] = ucma_init_qp_attr,
  1048. [RDMA_USER_CM_CMD_GET_EVENT] = ucma_get_event,
  1049. [RDMA_USER_CM_CMD_GET_OPTION] = NULL,
  1050. [RDMA_USER_CM_CMD_SET_OPTION] = ucma_set_option,
  1051. [RDMA_USER_CM_CMD_NOTIFY] = ucma_notify,
  1052. [RDMA_USER_CM_CMD_JOIN_MCAST] = ucma_join_multicast,
  1053. [RDMA_USER_CM_CMD_LEAVE_MCAST] = ucma_leave_multicast,
  1054. [RDMA_USER_CM_CMD_MIGRATE_ID] = ucma_migrate_id
  1055. };
  1056. static ssize_t ucma_write(struct file *filp, const char __user *buf,
  1057. size_t len, loff_t *pos)
  1058. {
  1059. struct ucma_file *file = filp->private_data;
  1060. struct rdma_ucm_cmd_hdr hdr;
  1061. ssize_t ret;
  1062. if (len < sizeof(hdr))
  1063. return -EINVAL;
  1064. if (copy_from_user(&hdr, buf, sizeof(hdr)))
  1065. return -EFAULT;
  1066. if (hdr.cmd >= ARRAY_SIZE(ucma_cmd_table))
  1067. return -EINVAL;
  1068. if (hdr.in + sizeof(hdr) > len)
  1069. return -EINVAL;
  1070. if (!ucma_cmd_table[hdr.cmd])
  1071. return -ENOSYS;
  1072. ret = ucma_cmd_table[hdr.cmd](file, buf + sizeof(hdr), hdr.in, hdr.out);
  1073. if (!ret)
  1074. ret = len;
  1075. return ret;
  1076. }
  1077. static unsigned int ucma_poll(struct file *filp, struct poll_table_struct *wait)
  1078. {
  1079. struct ucma_file *file = filp->private_data;
  1080. unsigned int mask = 0;
  1081. poll_wait(filp, &file->poll_wait, wait);
  1082. if (!list_empty(&file->event_list))
  1083. mask = POLLIN | POLLRDNORM;
  1084. return mask;
  1085. }
  1086. /*
  1087. * ucma_open() does not need the BKL:
  1088. *
  1089. * - no global state is referred to;
  1090. * - there is no ioctl method to race against;
  1091. * - no further module initialization is required for open to work
  1092. * after the device is registered.
  1093. */
  1094. static int ucma_open(struct inode *inode, struct file *filp)
  1095. {
  1096. struct ucma_file *file;
  1097. file = kmalloc(sizeof *file, GFP_KERNEL);
  1098. if (!file)
  1099. return -ENOMEM;
  1100. INIT_LIST_HEAD(&file->event_list);
  1101. INIT_LIST_HEAD(&file->ctx_list);
  1102. init_waitqueue_head(&file->poll_wait);
  1103. mutex_init(&file->mut);
  1104. filp->private_data = file;
  1105. file->filp = filp;
  1106. return nonseekable_open(inode, filp);
  1107. }
  1108. static int ucma_close(struct inode *inode, struct file *filp)
  1109. {
  1110. struct ucma_file *file = filp->private_data;
  1111. struct ucma_context *ctx, *tmp;
  1112. mutex_lock(&file->mut);
  1113. list_for_each_entry_safe(ctx, tmp, &file->ctx_list, list) {
  1114. mutex_unlock(&file->mut);
  1115. mutex_lock(&mut);
  1116. idr_remove(&ctx_idr, ctx->id);
  1117. mutex_unlock(&mut);
  1118. ucma_free_ctx(ctx);
  1119. mutex_lock(&file->mut);
  1120. }
  1121. mutex_unlock(&file->mut);
  1122. kfree(file);
  1123. return 0;
  1124. }
  1125. static const struct file_operations ucma_fops = {
  1126. .owner = THIS_MODULE,
  1127. .open = ucma_open,
  1128. .release = ucma_close,
  1129. .write = ucma_write,
  1130. .poll = ucma_poll,
  1131. .llseek = no_llseek,
  1132. };
  1133. static struct miscdevice ucma_misc = {
  1134. .minor = MISC_DYNAMIC_MINOR,
  1135. .name = "rdma_cm",
  1136. .nodename = "infiniband/rdma_cm",
  1137. .mode = 0666,
  1138. .fops = &ucma_fops,
  1139. };
  1140. static ssize_t show_abi_version(struct device *dev,
  1141. struct device_attribute *attr,
  1142. char *buf)
  1143. {
  1144. return sprintf(buf, "%d\n", RDMA_USER_CM_ABI_VERSION);
  1145. }
  1146. static DEVICE_ATTR(abi_version, S_IRUGO, show_abi_version, NULL);
  1147. static int __init ucma_init(void)
  1148. {
  1149. int ret;
  1150. ret = misc_register(&ucma_misc);
  1151. if (ret)
  1152. return ret;
  1153. ret = device_create_file(ucma_misc.this_device, &dev_attr_abi_version);
  1154. if (ret) {
  1155. printk(KERN_ERR "rdma_ucm: couldn't create abi_version attr\n");
  1156. goto err1;
  1157. }
  1158. ucma_ctl_table_hdr = register_net_sysctl(&init_net, "net/rdma_ucm", ucma_ctl_table);
  1159. if (!ucma_ctl_table_hdr) {
  1160. printk(KERN_ERR "rdma_ucm: couldn't register sysctl paths\n");
  1161. ret = -ENOMEM;
  1162. goto err2;
  1163. }
  1164. return 0;
  1165. err2:
  1166. device_remove_file(ucma_misc.this_device, &dev_attr_abi_version);
  1167. err1:
  1168. misc_deregister(&ucma_misc);
  1169. return ret;
  1170. }
  1171. static void __exit ucma_cleanup(void)
  1172. {
  1173. unregister_net_sysctl_table(ucma_ctl_table_hdr);
  1174. device_remove_file(ucma_misc.this_device, &dev_attr_abi_version);
  1175. misc_deregister(&ucma_misc);
  1176. idr_destroy(&ctx_idr);
  1177. }
  1178. module_init(ucma_init);
  1179. module_exit(ucma_cleanup);