ucma.c 30 KB

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