ucma.c 32 KB

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