ucma.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331
  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. resp->num_paths = route->num_paths;
  495. switch (route->num_paths) {
  496. case 0:
  497. dev_addr = &route->addr.dev_addr;
  498. iboe_mac_to_ll((union ib_gid *) &resp->ib_route[0].dgid,
  499. dev_addr->dst_dev_addr);
  500. iboe_addr_get_sgid(dev_addr,
  501. (union ib_gid *) &resp->ib_route[0].sgid);
  502. resp->ib_route[0].pkey = cpu_to_be16(0xffff);
  503. break;
  504. case 2:
  505. ib_copy_path_rec_to_user(&resp->ib_route[1],
  506. &route->path_rec[1]);
  507. /* fall through */
  508. case 1:
  509. ib_copy_path_rec_to_user(&resp->ib_route[0],
  510. &route->path_rec[0]);
  511. break;
  512. default:
  513. break;
  514. }
  515. }
  516. static ssize_t ucma_query_route(struct ucma_file *file,
  517. const char __user *inbuf,
  518. int in_len, int out_len)
  519. {
  520. struct rdma_ucm_query_route cmd;
  521. struct rdma_ucm_query_route_resp resp;
  522. struct ucma_context *ctx;
  523. struct sockaddr *addr;
  524. int ret = 0;
  525. if (out_len < sizeof(resp))
  526. return -ENOSPC;
  527. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  528. return -EFAULT;
  529. ctx = ucma_get_ctx(file, cmd.id);
  530. if (IS_ERR(ctx))
  531. return PTR_ERR(ctx);
  532. memset(&resp, 0, sizeof resp);
  533. addr = (struct sockaddr *) &ctx->cm_id->route.addr.src_addr;
  534. memcpy(&resp.src_addr, addr, addr->sa_family == AF_INET ?
  535. sizeof(struct sockaddr_in) :
  536. sizeof(struct sockaddr_in6));
  537. addr = (struct sockaddr *) &ctx->cm_id->route.addr.dst_addr;
  538. memcpy(&resp.dst_addr, addr, addr->sa_family == AF_INET ?
  539. sizeof(struct sockaddr_in) :
  540. sizeof(struct sockaddr_in6));
  541. if (!ctx->cm_id->device)
  542. goto out;
  543. resp.node_guid = (__force __u64) ctx->cm_id->device->node_guid;
  544. resp.port_num = ctx->cm_id->port_num;
  545. if (rdma_node_get_transport(ctx->cm_id->device->node_type) == RDMA_TRANSPORT_IB) {
  546. switch (rdma_port_get_link_layer(ctx->cm_id->device, ctx->cm_id->port_num)) {
  547. case IB_LINK_LAYER_INFINIBAND:
  548. ucma_copy_ib_route(&resp, &ctx->cm_id->route);
  549. break;
  550. case IB_LINK_LAYER_ETHERNET:
  551. ucma_copy_iboe_route(&resp, &ctx->cm_id->route);
  552. break;
  553. default:
  554. break;
  555. }
  556. }
  557. out:
  558. if (copy_to_user((void __user *)(unsigned long)cmd.response,
  559. &resp, sizeof(resp)))
  560. ret = -EFAULT;
  561. ucma_put_ctx(ctx);
  562. return ret;
  563. }
  564. static void ucma_copy_conn_param(struct rdma_conn_param *dst,
  565. struct rdma_ucm_conn_param *src)
  566. {
  567. dst->private_data = src->private_data;
  568. dst->private_data_len = src->private_data_len;
  569. dst->responder_resources =src->responder_resources;
  570. dst->initiator_depth = src->initiator_depth;
  571. dst->flow_control = src->flow_control;
  572. dst->retry_count = src->retry_count;
  573. dst->rnr_retry_count = src->rnr_retry_count;
  574. dst->srq = src->srq;
  575. dst->qp_num = src->qp_num;
  576. }
  577. static ssize_t ucma_connect(struct ucma_file *file, const char __user *inbuf,
  578. int in_len, int out_len)
  579. {
  580. struct rdma_ucm_connect cmd;
  581. struct rdma_conn_param conn_param;
  582. struct ucma_context *ctx;
  583. int ret;
  584. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  585. return -EFAULT;
  586. if (!cmd.conn_param.valid)
  587. return -EINVAL;
  588. ctx = ucma_get_ctx(file, cmd.id);
  589. if (IS_ERR(ctx))
  590. return PTR_ERR(ctx);
  591. ucma_copy_conn_param(&conn_param, &cmd.conn_param);
  592. ret = rdma_connect(ctx->cm_id, &conn_param);
  593. ucma_put_ctx(ctx);
  594. return ret;
  595. }
  596. static ssize_t ucma_listen(struct ucma_file *file, const char __user *inbuf,
  597. int in_len, int out_len)
  598. {
  599. struct rdma_ucm_listen cmd;
  600. struct ucma_context *ctx;
  601. int ret;
  602. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  603. return -EFAULT;
  604. ctx = ucma_get_ctx(file, cmd.id);
  605. if (IS_ERR(ctx))
  606. return PTR_ERR(ctx);
  607. ctx->backlog = cmd.backlog > 0 && cmd.backlog < UCMA_MAX_BACKLOG ?
  608. cmd.backlog : UCMA_MAX_BACKLOG;
  609. ret = rdma_listen(ctx->cm_id, ctx->backlog);
  610. ucma_put_ctx(ctx);
  611. return ret;
  612. }
  613. static ssize_t ucma_accept(struct ucma_file *file, const char __user *inbuf,
  614. int in_len, int out_len)
  615. {
  616. struct rdma_ucm_accept cmd;
  617. struct rdma_conn_param conn_param;
  618. struct ucma_context *ctx;
  619. int ret;
  620. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  621. return -EFAULT;
  622. ctx = ucma_get_ctx(file, cmd.id);
  623. if (IS_ERR(ctx))
  624. return PTR_ERR(ctx);
  625. if (cmd.conn_param.valid) {
  626. ctx->uid = cmd.uid;
  627. ucma_copy_conn_param(&conn_param, &cmd.conn_param);
  628. ret = rdma_accept(ctx->cm_id, &conn_param);
  629. } else
  630. ret = rdma_accept(ctx->cm_id, NULL);
  631. ucma_put_ctx(ctx);
  632. return ret;
  633. }
  634. static ssize_t ucma_reject(struct ucma_file *file, const char __user *inbuf,
  635. int in_len, int out_len)
  636. {
  637. struct rdma_ucm_reject cmd;
  638. struct ucma_context *ctx;
  639. int ret;
  640. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  641. return -EFAULT;
  642. ctx = ucma_get_ctx(file, cmd.id);
  643. if (IS_ERR(ctx))
  644. return PTR_ERR(ctx);
  645. ret = rdma_reject(ctx->cm_id, cmd.private_data, cmd.private_data_len);
  646. ucma_put_ctx(ctx);
  647. return ret;
  648. }
  649. static ssize_t ucma_disconnect(struct ucma_file *file, const char __user *inbuf,
  650. int in_len, int out_len)
  651. {
  652. struct rdma_ucm_disconnect cmd;
  653. struct ucma_context *ctx;
  654. int ret;
  655. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  656. return -EFAULT;
  657. ctx = ucma_get_ctx(file, cmd.id);
  658. if (IS_ERR(ctx))
  659. return PTR_ERR(ctx);
  660. ret = rdma_disconnect(ctx->cm_id);
  661. ucma_put_ctx(ctx);
  662. return ret;
  663. }
  664. static ssize_t ucma_init_qp_attr(struct ucma_file *file,
  665. const char __user *inbuf,
  666. int in_len, int out_len)
  667. {
  668. struct rdma_ucm_init_qp_attr cmd;
  669. struct ib_uverbs_qp_attr resp;
  670. struct ucma_context *ctx;
  671. struct ib_qp_attr qp_attr;
  672. int ret;
  673. if (out_len < sizeof(resp))
  674. return -ENOSPC;
  675. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  676. return -EFAULT;
  677. ctx = ucma_get_ctx(file, cmd.id);
  678. if (IS_ERR(ctx))
  679. return PTR_ERR(ctx);
  680. resp.qp_attr_mask = 0;
  681. memset(&qp_attr, 0, sizeof qp_attr);
  682. qp_attr.qp_state = cmd.qp_state;
  683. ret = rdma_init_qp_attr(ctx->cm_id, &qp_attr, &resp.qp_attr_mask);
  684. if (ret)
  685. goto out;
  686. ib_copy_qp_attr_to_user(&resp, &qp_attr);
  687. if (copy_to_user((void __user *)(unsigned long)cmd.response,
  688. &resp, sizeof(resp)))
  689. ret = -EFAULT;
  690. out:
  691. ucma_put_ctx(ctx);
  692. return ret;
  693. }
  694. static int ucma_set_option_id(struct ucma_context *ctx, int optname,
  695. void *optval, size_t optlen)
  696. {
  697. int ret = 0;
  698. switch (optname) {
  699. case RDMA_OPTION_ID_TOS:
  700. if (optlen != sizeof(u8)) {
  701. ret = -EINVAL;
  702. break;
  703. }
  704. rdma_set_service_type(ctx->cm_id, *((u8 *) optval));
  705. break;
  706. default:
  707. ret = -ENOSYS;
  708. }
  709. return ret;
  710. }
  711. static int ucma_set_ib_path(struct ucma_context *ctx,
  712. struct ib_path_rec_data *path_data, size_t optlen)
  713. {
  714. struct ib_sa_path_rec sa_path;
  715. struct rdma_cm_event event;
  716. int ret;
  717. if (optlen % sizeof(*path_data))
  718. return -EINVAL;
  719. for (; optlen; optlen -= sizeof(*path_data), path_data++) {
  720. if (path_data->flags == (IB_PATH_GMP | IB_PATH_PRIMARY |
  721. IB_PATH_BIDIRECTIONAL))
  722. break;
  723. }
  724. if (!optlen)
  725. return -EINVAL;
  726. ib_sa_unpack_path(path_data->path_rec, &sa_path);
  727. ret = rdma_set_ib_paths(ctx->cm_id, &sa_path, 1);
  728. if (ret)
  729. return ret;
  730. memset(&event, 0, sizeof event);
  731. event.event = RDMA_CM_EVENT_ROUTE_RESOLVED;
  732. return ucma_event_handler(ctx->cm_id, &event);
  733. }
  734. static int ucma_set_option_ib(struct ucma_context *ctx, int optname,
  735. void *optval, size_t optlen)
  736. {
  737. int ret;
  738. switch (optname) {
  739. case RDMA_OPTION_IB_PATH:
  740. ret = ucma_set_ib_path(ctx, optval, optlen);
  741. break;
  742. default:
  743. ret = -ENOSYS;
  744. }
  745. return ret;
  746. }
  747. static int ucma_set_option_level(struct ucma_context *ctx, int level,
  748. int optname, void *optval, size_t optlen)
  749. {
  750. int ret;
  751. switch (level) {
  752. case RDMA_OPTION_ID:
  753. ret = ucma_set_option_id(ctx, optname, optval, optlen);
  754. break;
  755. case RDMA_OPTION_IB:
  756. ret = ucma_set_option_ib(ctx, optname, optval, optlen);
  757. break;
  758. default:
  759. ret = -ENOSYS;
  760. }
  761. return ret;
  762. }
  763. static ssize_t ucma_set_option(struct ucma_file *file, const char __user *inbuf,
  764. int in_len, int out_len)
  765. {
  766. struct rdma_ucm_set_option cmd;
  767. struct ucma_context *ctx;
  768. void *optval;
  769. int ret;
  770. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  771. return -EFAULT;
  772. ctx = ucma_get_ctx(file, cmd.id);
  773. if (IS_ERR(ctx))
  774. return PTR_ERR(ctx);
  775. optval = kmalloc(cmd.optlen, GFP_KERNEL);
  776. if (!optval) {
  777. ret = -ENOMEM;
  778. goto out1;
  779. }
  780. if (copy_from_user(optval, (void __user *) (unsigned long) cmd.optval,
  781. cmd.optlen)) {
  782. ret = -EFAULT;
  783. goto out2;
  784. }
  785. ret = ucma_set_option_level(ctx, cmd.level, cmd.optname, optval,
  786. cmd.optlen);
  787. out2:
  788. kfree(optval);
  789. out1:
  790. ucma_put_ctx(ctx);
  791. return ret;
  792. }
  793. static ssize_t ucma_notify(struct ucma_file *file, const char __user *inbuf,
  794. int in_len, int out_len)
  795. {
  796. struct rdma_ucm_notify cmd;
  797. struct ucma_context *ctx;
  798. int ret;
  799. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  800. return -EFAULT;
  801. ctx = ucma_get_ctx(file, cmd.id);
  802. if (IS_ERR(ctx))
  803. return PTR_ERR(ctx);
  804. ret = rdma_notify(ctx->cm_id, (enum ib_event_type) cmd.event);
  805. ucma_put_ctx(ctx);
  806. return ret;
  807. }
  808. static ssize_t ucma_join_multicast(struct ucma_file *file,
  809. const char __user *inbuf,
  810. int in_len, int out_len)
  811. {
  812. struct rdma_ucm_join_mcast cmd;
  813. struct rdma_ucm_create_id_resp resp;
  814. struct ucma_context *ctx;
  815. struct ucma_multicast *mc;
  816. int ret;
  817. if (out_len < sizeof(resp))
  818. return -ENOSPC;
  819. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  820. return -EFAULT;
  821. ctx = ucma_get_ctx(file, cmd.id);
  822. if (IS_ERR(ctx))
  823. return PTR_ERR(ctx);
  824. mutex_lock(&file->mut);
  825. mc = ucma_alloc_multicast(ctx);
  826. if (!mc) {
  827. ret = -ENOMEM;
  828. goto err1;
  829. }
  830. mc->uid = cmd.uid;
  831. memcpy(&mc->addr, &cmd.addr, sizeof cmd.addr);
  832. ret = rdma_join_multicast(ctx->cm_id, (struct sockaddr *) &mc->addr, mc);
  833. if (ret)
  834. goto err2;
  835. resp.id = mc->id;
  836. if (copy_to_user((void __user *)(unsigned long)cmd.response,
  837. &resp, sizeof(resp))) {
  838. ret = -EFAULT;
  839. goto err3;
  840. }
  841. mutex_unlock(&file->mut);
  842. ucma_put_ctx(ctx);
  843. return 0;
  844. err3:
  845. rdma_leave_multicast(ctx->cm_id, (struct sockaddr *) &mc->addr);
  846. ucma_cleanup_mc_events(mc);
  847. err2:
  848. mutex_lock(&mut);
  849. idr_remove(&multicast_idr, mc->id);
  850. mutex_unlock(&mut);
  851. list_del(&mc->list);
  852. kfree(mc);
  853. err1:
  854. mutex_unlock(&file->mut);
  855. ucma_put_ctx(ctx);
  856. return ret;
  857. }
  858. static ssize_t ucma_leave_multicast(struct ucma_file *file,
  859. const char __user *inbuf,
  860. int in_len, int out_len)
  861. {
  862. struct rdma_ucm_destroy_id cmd;
  863. struct rdma_ucm_destroy_id_resp resp;
  864. struct ucma_multicast *mc;
  865. int ret = 0;
  866. if (out_len < sizeof(resp))
  867. return -ENOSPC;
  868. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  869. return -EFAULT;
  870. mutex_lock(&mut);
  871. mc = idr_find(&multicast_idr, cmd.id);
  872. if (!mc)
  873. mc = ERR_PTR(-ENOENT);
  874. else if (mc->ctx->file != file)
  875. mc = ERR_PTR(-EINVAL);
  876. else {
  877. idr_remove(&multicast_idr, mc->id);
  878. atomic_inc(&mc->ctx->ref);
  879. }
  880. mutex_unlock(&mut);
  881. if (IS_ERR(mc)) {
  882. ret = PTR_ERR(mc);
  883. goto out;
  884. }
  885. rdma_leave_multicast(mc->ctx->cm_id, (struct sockaddr *) &mc->addr);
  886. mutex_lock(&mc->ctx->file->mut);
  887. ucma_cleanup_mc_events(mc);
  888. list_del(&mc->list);
  889. mutex_unlock(&mc->ctx->file->mut);
  890. ucma_put_ctx(mc->ctx);
  891. resp.events_reported = mc->events_reported;
  892. kfree(mc);
  893. if (copy_to_user((void __user *)(unsigned long)cmd.response,
  894. &resp, sizeof(resp)))
  895. ret = -EFAULT;
  896. out:
  897. return ret;
  898. }
  899. static void ucma_lock_files(struct ucma_file *file1, struct ucma_file *file2)
  900. {
  901. /* Acquire mutex's based on pointer comparison to prevent deadlock. */
  902. if (file1 < file2) {
  903. mutex_lock(&file1->mut);
  904. mutex_lock(&file2->mut);
  905. } else {
  906. mutex_lock(&file2->mut);
  907. mutex_lock(&file1->mut);
  908. }
  909. }
  910. static void ucma_unlock_files(struct ucma_file *file1, struct ucma_file *file2)
  911. {
  912. if (file1 < file2) {
  913. mutex_unlock(&file2->mut);
  914. mutex_unlock(&file1->mut);
  915. } else {
  916. mutex_unlock(&file1->mut);
  917. mutex_unlock(&file2->mut);
  918. }
  919. }
  920. static void ucma_move_events(struct ucma_context *ctx, struct ucma_file *file)
  921. {
  922. struct ucma_event *uevent, *tmp;
  923. list_for_each_entry_safe(uevent, tmp, &ctx->file->event_list, list)
  924. if (uevent->ctx == ctx)
  925. list_move_tail(&uevent->list, &file->event_list);
  926. }
  927. static ssize_t ucma_migrate_id(struct ucma_file *new_file,
  928. const char __user *inbuf,
  929. int in_len, int out_len)
  930. {
  931. struct rdma_ucm_migrate_id cmd;
  932. struct rdma_ucm_migrate_resp resp;
  933. struct ucma_context *ctx;
  934. struct file *filp;
  935. struct ucma_file *cur_file;
  936. int ret = 0;
  937. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  938. return -EFAULT;
  939. /* Get current fd to protect against it being closed */
  940. filp = fget(cmd.fd);
  941. if (!filp)
  942. return -ENOENT;
  943. /* Validate current fd and prevent destruction of id. */
  944. ctx = ucma_get_ctx(filp->private_data, cmd.id);
  945. if (IS_ERR(ctx)) {
  946. ret = PTR_ERR(ctx);
  947. goto file_put;
  948. }
  949. cur_file = ctx->file;
  950. if (cur_file == new_file) {
  951. resp.events_reported = ctx->events_reported;
  952. goto response;
  953. }
  954. /*
  955. * Migrate events between fd's, maintaining order, and avoiding new
  956. * events being added before existing events.
  957. */
  958. ucma_lock_files(cur_file, new_file);
  959. mutex_lock(&mut);
  960. list_move_tail(&ctx->list, &new_file->ctx_list);
  961. ucma_move_events(ctx, new_file);
  962. ctx->file = new_file;
  963. resp.events_reported = ctx->events_reported;
  964. mutex_unlock(&mut);
  965. ucma_unlock_files(cur_file, new_file);
  966. response:
  967. if (copy_to_user((void __user *)(unsigned long)cmd.response,
  968. &resp, sizeof(resp)))
  969. ret = -EFAULT;
  970. ucma_put_ctx(ctx);
  971. file_put:
  972. fput(filp);
  973. return ret;
  974. }
  975. static ssize_t (*ucma_cmd_table[])(struct ucma_file *file,
  976. const char __user *inbuf,
  977. int in_len, int out_len) = {
  978. [RDMA_USER_CM_CMD_CREATE_ID] = ucma_create_id,
  979. [RDMA_USER_CM_CMD_DESTROY_ID] = ucma_destroy_id,
  980. [RDMA_USER_CM_CMD_BIND_ADDR] = ucma_bind_addr,
  981. [RDMA_USER_CM_CMD_RESOLVE_ADDR] = ucma_resolve_addr,
  982. [RDMA_USER_CM_CMD_RESOLVE_ROUTE]= ucma_resolve_route,
  983. [RDMA_USER_CM_CMD_QUERY_ROUTE] = ucma_query_route,
  984. [RDMA_USER_CM_CMD_CONNECT] = ucma_connect,
  985. [RDMA_USER_CM_CMD_LISTEN] = ucma_listen,
  986. [RDMA_USER_CM_CMD_ACCEPT] = ucma_accept,
  987. [RDMA_USER_CM_CMD_REJECT] = ucma_reject,
  988. [RDMA_USER_CM_CMD_DISCONNECT] = ucma_disconnect,
  989. [RDMA_USER_CM_CMD_INIT_QP_ATTR] = ucma_init_qp_attr,
  990. [RDMA_USER_CM_CMD_GET_EVENT] = ucma_get_event,
  991. [RDMA_USER_CM_CMD_GET_OPTION] = NULL,
  992. [RDMA_USER_CM_CMD_SET_OPTION] = ucma_set_option,
  993. [RDMA_USER_CM_CMD_NOTIFY] = ucma_notify,
  994. [RDMA_USER_CM_CMD_JOIN_MCAST] = ucma_join_multicast,
  995. [RDMA_USER_CM_CMD_LEAVE_MCAST] = ucma_leave_multicast,
  996. [RDMA_USER_CM_CMD_MIGRATE_ID] = ucma_migrate_id
  997. };
  998. static ssize_t ucma_write(struct file *filp, const char __user *buf,
  999. size_t len, loff_t *pos)
  1000. {
  1001. struct ucma_file *file = filp->private_data;
  1002. struct rdma_ucm_cmd_hdr hdr;
  1003. ssize_t ret;
  1004. if (len < sizeof(hdr))
  1005. return -EINVAL;
  1006. if (copy_from_user(&hdr, buf, sizeof(hdr)))
  1007. return -EFAULT;
  1008. if (hdr.cmd < 0 || hdr.cmd >= ARRAY_SIZE(ucma_cmd_table))
  1009. return -EINVAL;
  1010. if (hdr.in + sizeof(hdr) > len)
  1011. return -EINVAL;
  1012. if (!ucma_cmd_table[hdr.cmd])
  1013. return -ENOSYS;
  1014. ret = ucma_cmd_table[hdr.cmd](file, buf + sizeof(hdr), hdr.in, hdr.out);
  1015. if (!ret)
  1016. ret = len;
  1017. return ret;
  1018. }
  1019. static unsigned int ucma_poll(struct file *filp, struct poll_table_struct *wait)
  1020. {
  1021. struct ucma_file *file = filp->private_data;
  1022. unsigned int mask = 0;
  1023. poll_wait(filp, &file->poll_wait, wait);
  1024. if (!list_empty(&file->event_list))
  1025. mask = POLLIN | POLLRDNORM;
  1026. return mask;
  1027. }
  1028. /*
  1029. * ucma_open() does not need the BKL:
  1030. *
  1031. * - no global state is referred to;
  1032. * - there is no ioctl method to race against;
  1033. * - no further module initialization is required for open to work
  1034. * after the device is registered.
  1035. */
  1036. static int ucma_open(struct inode *inode, struct file *filp)
  1037. {
  1038. struct ucma_file *file;
  1039. file = kmalloc(sizeof *file, GFP_KERNEL);
  1040. if (!file)
  1041. return -ENOMEM;
  1042. INIT_LIST_HEAD(&file->event_list);
  1043. INIT_LIST_HEAD(&file->ctx_list);
  1044. init_waitqueue_head(&file->poll_wait);
  1045. mutex_init(&file->mut);
  1046. filp->private_data = file;
  1047. file->filp = filp;
  1048. return nonseekable_open(inode, filp);
  1049. }
  1050. static int ucma_close(struct inode *inode, struct file *filp)
  1051. {
  1052. struct ucma_file *file = filp->private_data;
  1053. struct ucma_context *ctx, *tmp;
  1054. mutex_lock(&file->mut);
  1055. list_for_each_entry_safe(ctx, tmp, &file->ctx_list, list) {
  1056. mutex_unlock(&file->mut);
  1057. mutex_lock(&mut);
  1058. idr_remove(&ctx_idr, ctx->id);
  1059. mutex_unlock(&mut);
  1060. ucma_free_ctx(ctx);
  1061. mutex_lock(&file->mut);
  1062. }
  1063. mutex_unlock(&file->mut);
  1064. kfree(file);
  1065. return 0;
  1066. }
  1067. static const struct file_operations ucma_fops = {
  1068. .owner = THIS_MODULE,
  1069. .open = ucma_open,
  1070. .release = ucma_close,
  1071. .write = ucma_write,
  1072. .poll = ucma_poll,
  1073. .llseek = no_llseek,
  1074. };
  1075. static struct miscdevice ucma_misc = {
  1076. .minor = MISC_DYNAMIC_MINOR,
  1077. .name = "rdma_cm",
  1078. .fops = &ucma_fops,
  1079. };
  1080. static ssize_t show_abi_version(struct device *dev,
  1081. struct device_attribute *attr,
  1082. char *buf)
  1083. {
  1084. return sprintf(buf, "%d\n", RDMA_USER_CM_ABI_VERSION);
  1085. }
  1086. static DEVICE_ATTR(abi_version, S_IRUGO, show_abi_version, NULL);
  1087. static int __init ucma_init(void)
  1088. {
  1089. int ret;
  1090. ret = misc_register(&ucma_misc);
  1091. if (ret)
  1092. return ret;
  1093. ret = device_create_file(ucma_misc.this_device, &dev_attr_abi_version);
  1094. if (ret) {
  1095. printk(KERN_ERR "rdma_ucm: couldn't create abi_version attr\n");
  1096. goto err;
  1097. }
  1098. return 0;
  1099. err:
  1100. misc_deregister(&ucma_misc);
  1101. return ret;
  1102. }
  1103. static void __exit ucma_cleanup(void)
  1104. {
  1105. device_remove_file(ucma_misc.this_device, &dev_attr_abi_version);
  1106. misc_deregister(&ucma_misc);
  1107. idr_destroy(&ctx_idr);
  1108. }
  1109. module_init(ucma_init);
  1110. module_exit(ucma_cleanup);