ucma.c 34 KB

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