ucma.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741
  1. /*
  2. * Copyright (c) 2005-2006 Intel Corporation. All rights reserved.
  3. *
  4. * This software is available to you under a choice of one of two
  5. * licenses. You may choose to be licensed under the terms of the GNU
  6. * General Public License (GPL) Version 2, available from the file
  7. * COPYING in the main directory of this source tree, or the
  8. * OpenIB.org BSD license below:
  9. *
  10. * Redistribution and use in source and binary forms, with or
  11. * without modification, are permitted provided that the following
  12. * conditions are met:
  13. *
  14. * - Redistributions of source code must retain the above
  15. * copyright notice, this list of conditions and the following
  16. * disclaimer.
  17. *
  18. * - Redistributions in binary form must reproduce the above
  19. * copyright notice, this list of conditions and the following
  20. * disclaimer in the documentation and/or other materials
  21. * provided with the distribution.
  22. *
  23. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  27. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  28. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  29. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  30. * SOFTWARE.
  31. */
  32. #include <linux/completion.h>
  33. #include <linux/file.h>
  34. #include <linux/mutex.h>
  35. #include <linux/poll.h>
  36. #include <linux/sched.h>
  37. #include <linux/idr.h>
  38. #include <linux/in.h>
  39. #include <linux/in6.h>
  40. #include <linux/miscdevice.h>
  41. #include <linux/slab.h>
  42. #include <linux/sysctl.h>
  43. #include <linux/module.h>
  44. #include <rdma/rdma_user_cm.h>
  45. #include <rdma/ib_marshall.h>
  46. #include <rdma/rdma_cm.h>
  47. #include <rdma/rdma_cm_ib.h>
  48. #include <rdma/ib_addr.h>
  49. #include <rdma/ib.h>
  50. MODULE_AUTHOR("Sean Hefty");
  51. MODULE_DESCRIPTION("RDMA Userspace Connection Manager Access");
  52. MODULE_LICENSE("Dual BSD/GPL");
  53. static unsigned int max_backlog = 1024;
  54. static struct ctl_table_header *ucma_ctl_table_hdr;
  55. static struct ctl_table ucma_ctl_table[] = {
  56. {
  57. .procname = "max_backlog",
  58. .data = &max_backlog,
  59. .maxlen = sizeof max_backlog,
  60. .mode = 0644,
  61. .proc_handler = proc_dointvec,
  62. },
  63. { }
  64. };
  65. struct ucma_file {
  66. struct mutex mut;
  67. struct file *filp;
  68. struct list_head ctx_list;
  69. struct list_head event_list;
  70. wait_queue_head_t poll_wait;
  71. struct workqueue_struct *close_wq;
  72. };
  73. struct ucma_context {
  74. int id;
  75. struct completion comp;
  76. atomic_t ref;
  77. int events_reported;
  78. int backlog;
  79. struct ucma_file *file;
  80. struct rdma_cm_id *cm_id;
  81. u64 uid;
  82. struct list_head list;
  83. struct list_head mc_list;
  84. /* mark that device is in process of destroying the internal HW
  85. * resources, protected by the global mut
  86. */
  87. int closing;
  88. /* sync between removal event and id destroy, protected by file mut */
  89. int destroying;
  90. struct work_struct close_work;
  91. };
  92. struct ucma_multicast {
  93. struct ucma_context *ctx;
  94. int id;
  95. int events_reported;
  96. u64 uid;
  97. struct list_head list;
  98. struct sockaddr_storage addr;
  99. };
  100. struct ucma_event {
  101. struct ucma_context *ctx;
  102. struct ucma_multicast *mc;
  103. struct list_head list;
  104. struct rdma_cm_id *cm_id;
  105. struct rdma_ucm_event_resp resp;
  106. struct work_struct close_work;
  107. };
  108. static DEFINE_MUTEX(mut);
  109. static DEFINE_IDR(ctx_idr);
  110. static DEFINE_IDR(multicast_idr);
  111. static inline struct ucma_context *_ucma_find_context(int id,
  112. struct ucma_file *file)
  113. {
  114. struct ucma_context *ctx;
  115. ctx = idr_find(&ctx_idr, id);
  116. if (!ctx)
  117. ctx = ERR_PTR(-ENOENT);
  118. else if (ctx->file != file)
  119. ctx = ERR_PTR(-EINVAL);
  120. return ctx;
  121. }
  122. static struct ucma_context *ucma_get_ctx(struct ucma_file *file, int id)
  123. {
  124. struct ucma_context *ctx;
  125. mutex_lock(&mut);
  126. ctx = _ucma_find_context(id, file);
  127. if (!IS_ERR(ctx)) {
  128. if (ctx->closing)
  129. ctx = ERR_PTR(-EIO);
  130. else
  131. atomic_inc(&ctx->ref);
  132. }
  133. mutex_unlock(&mut);
  134. return ctx;
  135. }
  136. static void ucma_put_ctx(struct ucma_context *ctx)
  137. {
  138. if (atomic_dec_and_test(&ctx->ref))
  139. complete(&ctx->comp);
  140. }
  141. static void ucma_close_event_id(struct work_struct *work)
  142. {
  143. struct ucma_event *uevent_close = container_of(work, struct ucma_event, close_work);
  144. rdma_destroy_id(uevent_close->cm_id);
  145. kfree(uevent_close);
  146. }
  147. static void ucma_close_id(struct work_struct *work)
  148. {
  149. struct ucma_context *ctx = container_of(work, struct ucma_context, close_work);
  150. /* once all inflight tasks are finished, we close all underlying
  151. * resources. The context is still alive till its explicit destryoing
  152. * by its creator.
  153. */
  154. ucma_put_ctx(ctx);
  155. wait_for_completion(&ctx->comp);
  156. /* No new events will be generated after destroying the id. */
  157. rdma_destroy_id(ctx->cm_id);
  158. }
  159. static struct ucma_context *ucma_alloc_ctx(struct ucma_file *file)
  160. {
  161. struct ucma_context *ctx;
  162. ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
  163. if (!ctx)
  164. return NULL;
  165. INIT_WORK(&ctx->close_work, ucma_close_id);
  166. atomic_set(&ctx->ref, 1);
  167. init_completion(&ctx->comp);
  168. INIT_LIST_HEAD(&ctx->mc_list);
  169. ctx->file = file;
  170. mutex_lock(&mut);
  171. ctx->id = idr_alloc(&ctx_idr, ctx, 0, 0, GFP_KERNEL);
  172. mutex_unlock(&mut);
  173. if (ctx->id < 0)
  174. goto error;
  175. list_add_tail(&ctx->list, &file->ctx_list);
  176. return ctx;
  177. error:
  178. kfree(ctx);
  179. return NULL;
  180. }
  181. static struct ucma_multicast* ucma_alloc_multicast(struct ucma_context *ctx)
  182. {
  183. struct ucma_multicast *mc;
  184. mc = kzalloc(sizeof(*mc), GFP_KERNEL);
  185. if (!mc)
  186. return NULL;
  187. mutex_lock(&mut);
  188. mc->id = idr_alloc(&multicast_idr, mc, 0, 0, GFP_KERNEL);
  189. mutex_unlock(&mut);
  190. if (mc->id < 0)
  191. goto error;
  192. mc->ctx = ctx;
  193. list_add_tail(&mc->list, &ctx->mc_list);
  194. return mc;
  195. error:
  196. kfree(mc);
  197. return NULL;
  198. }
  199. static void ucma_copy_conn_event(struct rdma_ucm_conn_param *dst,
  200. struct rdma_conn_param *src)
  201. {
  202. if (src->private_data_len)
  203. memcpy(dst->private_data, src->private_data,
  204. src->private_data_len);
  205. dst->private_data_len = src->private_data_len;
  206. dst->responder_resources =src->responder_resources;
  207. dst->initiator_depth = src->initiator_depth;
  208. dst->flow_control = src->flow_control;
  209. dst->retry_count = src->retry_count;
  210. dst->rnr_retry_count = src->rnr_retry_count;
  211. dst->srq = src->srq;
  212. dst->qp_num = src->qp_num;
  213. }
  214. static void ucma_copy_ud_event(struct rdma_ucm_ud_param *dst,
  215. struct rdma_ud_param *src)
  216. {
  217. if (src->private_data_len)
  218. memcpy(dst->private_data, src->private_data,
  219. src->private_data_len);
  220. dst->private_data_len = src->private_data_len;
  221. ib_copy_ah_attr_to_user(&dst->ah_attr, &src->ah_attr);
  222. dst->qp_num = src->qp_num;
  223. dst->qkey = src->qkey;
  224. }
  225. static void ucma_set_event_context(struct ucma_context *ctx,
  226. struct rdma_cm_event *event,
  227. struct ucma_event *uevent)
  228. {
  229. uevent->ctx = ctx;
  230. switch (event->event) {
  231. case RDMA_CM_EVENT_MULTICAST_JOIN:
  232. case RDMA_CM_EVENT_MULTICAST_ERROR:
  233. uevent->mc = (struct ucma_multicast *)
  234. event->param.ud.private_data;
  235. uevent->resp.uid = uevent->mc->uid;
  236. uevent->resp.id = uevent->mc->id;
  237. break;
  238. default:
  239. uevent->resp.uid = ctx->uid;
  240. uevent->resp.id = ctx->id;
  241. break;
  242. }
  243. }
  244. /* Called with file->mut locked for the relevant context. */
  245. static void ucma_removal_event_handler(struct rdma_cm_id *cm_id)
  246. {
  247. struct ucma_context *ctx = cm_id->context;
  248. struct ucma_event *con_req_eve;
  249. int event_found = 0;
  250. if (ctx->destroying)
  251. return;
  252. /* only if context is pointing to cm_id that it owns it and can be
  253. * queued to be closed, otherwise that cm_id is an inflight one that
  254. * is part of that context event list pending to be detached and
  255. * reattached to its new context as part of ucma_get_event,
  256. * handled separately below.
  257. */
  258. if (ctx->cm_id == cm_id) {
  259. mutex_lock(&mut);
  260. ctx->closing = 1;
  261. mutex_unlock(&mut);
  262. queue_work(ctx->file->close_wq, &ctx->close_work);
  263. return;
  264. }
  265. list_for_each_entry(con_req_eve, &ctx->file->event_list, list) {
  266. if (con_req_eve->cm_id == cm_id &&
  267. con_req_eve->resp.event == RDMA_CM_EVENT_CONNECT_REQUEST) {
  268. list_del(&con_req_eve->list);
  269. INIT_WORK(&con_req_eve->close_work, ucma_close_event_id);
  270. queue_work(ctx->file->close_wq, &con_req_eve->close_work);
  271. event_found = 1;
  272. break;
  273. }
  274. }
  275. if (!event_found)
  276. printk(KERN_ERR "ucma_removal_event_handler: warning: connect request event wasn't found\n");
  277. }
  278. static int ucma_event_handler(struct rdma_cm_id *cm_id,
  279. struct rdma_cm_event *event)
  280. {
  281. struct ucma_event *uevent;
  282. struct ucma_context *ctx = cm_id->context;
  283. int ret = 0;
  284. uevent = kzalloc(sizeof(*uevent), GFP_KERNEL);
  285. if (!uevent)
  286. return event->event == RDMA_CM_EVENT_CONNECT_REQUEST;
  287. mutex_lock(&ctx->file->mut);
  288. uevent->cm_id = cm_id;
  289. ucma_set_event_context(ctx, event, uevent);
  290. uevent->resp.event = event->event;
  291. uevent->resp.status = event->status;
  292. if (cm_id->qp_type == IB_QPT_UD)
  293. ucma_copy_ud_event(&uevent->resp.param.ud, &event->param.ud);
  294. else
  295. ucma_copy_conn_event(&uevent->resp.param.conn,
  296. &event->param.conn);
  297. if (event->event == RDMA_CM_EVENT_CONNECT_REQUEST) {
  298. if (!ctx->backlog) {
  299. ret = -ENOMEM;
  300. kfree(uevent);
  301. goto out;
  302. }
  303. ctx->backlog--;
  304. } else if (!ctx->uid || ctx->cm_id != cm_id) {
  305. /*
  306. * We ignore events for new connections until userspace has set
  307. * their context. This can only happen if an error occurs on a
  308. * new connection before the user accepts it. This is okay,
  309. * since the accept will just fail later. However, we do need
  310. * to release the underlying HW resources in case of a device
  311. * removal event.
  312. */
  313. if (event->event == RDMA_CM_EVENT_DEVICE_REMOVAL)
  314. ucma_removal_event_handler(cm_id);
  315. kfree(uevent);
  316. goto out;
  317. }
  318. list_add_tail(&uevent->list, &ctx->file->event_list);
  319. wake_up_interruptible(&ctx->file->poll_wait);
  320. if (event->event == RDMA_CM_EVENT_DEVICE_REMOVAL)
  321. ucma_removal_event_handler(cm_id);
  322. out:
  323. mutex_unlock(&ctx->file->mut);
  324. return ret;
  325. }
  326. static ssize_t ucma_get_event(struct ucma_file *file, const char __user *inbuf,
  327. int in_len, int out_len)
  328. {
  329. struct ucma_context *ctx;
  330. struct rdma_ucm_get_event cmd;
  331. struct ucma_event *uevent;
  332. int ret = 0;
  333. if (out_len < sizeof uevent->resp)
  334. return -ENOSPC;
  335. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  336. return -EFAULT;
  337. mutex_lock(&file->mut);
  338. while (list_empty(&file->event_list)) {
  339. mutex_unlock(&file->mut);
  340. if (file->filp->f_flags & O_NONBLOCK)
  341. return -EAGAIN;
  342. if (wait_event_interruptible(file->poll_wait,
  343. !list_empty(&file->event_list)))
  344. return -ERESTARTSYS;
  345. mutex_lock(&file->mut);
  346. }
  347. uevent = list_entry(file->event_list.next, struct ucma_event, list);
  348. if (uevent->resp.event == RDMA_CM_EVENT_CONNECT_REQUEST) {
  349. ctx = ucma_alloc_ctx(file);
  350. if (!ctx) {
  351. ret = -ENOMEM;
  352. goto done;
  353. }
  354. uevent->ctx->backlog++;
  355. ctx->cm_id = uevent->cm_id;
  356. ctx->cm_id->context = ctx;
  357. uevent->resp.id = ctx->id;
  358. }
  359. if (copy_to_user((void __user *)(unsigned long)cmd.response,
  360. &uevent->resp, sizeof uevent->resp)) {
  361. ret = -EFAULT;
  362. goto done;
  363. }
  364. list_del(&uevent->list);
  365. uevent->ctx->events_reported++;
  366. if (uevent->mc)
  367. uevent->mc->events_reported++;
  368. kfree(uevent);
  369. done:
  370. mutex_unlock(&file->mut);
  371. return ret;
  372. }
  373. static int ucma_get_qp_type(struct rdma_ucm_create_id *cmd, enum ib_qp_type *qp_type)
  374. {
  375. switch (cmd->ps) {
  376. case RDMA_PS_TCP:
  377. *qp_type = IB_QPT_RC;
  378. return 0;
  379. case RDMA_PS_UDP:
  380. case RDMA_PS_IPOIB:
  381. *qp_type = IB_QPT_UD;
  382. return 0;
  383. case RDMA_PS_IB:
  384. *qp_type = cmd->qp_type;
  385. return 0;
  386. default:
  387. return -EINVAL;
  388. }
  389. }
  390. static ssize_t ucma_create_id(struct ucma_file *file, const char __user *inbuf,
  391. int in_len, int out_len)
  392. {
  393. struct rdma_ucm_create_id cmd;
  394. struct rdma_ucm_create_id_resp resp;
  395. struct ucma_context *ctx;
  396. enum ib_qp_type qp_type;
  397. int ret;
  398. if (out_len < sizeof(resp))
  399. return -ENOSPC;
  400. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  401. return -EFAULT;
  402. ret = ucma_get_qp_type(&cmd, &qp_type);
  403. if (ret)
  404. return ret;
  405. mutex_lock(&file->mut);
  406. ctx = ucma_alloc_ctx(file);
  407. mutex_unlock(&file->mut);
  408. if (!ctx)
  409. return -ENOMEM;
  410. ctx->uid = cmd.uid;
  411. ctx->cm_id = rdma_create_id(ucma_event_handler, ctx, cmd.ps, qp_type);
  412. if (IS_ERR(ctx->cm_id)) {
  413. ret = PTR_ERR(ctx->cm_id);
  414. goto err1;
  415. }
  416. resp.id = ctx->id;
  417. if (copy_to_user((void __user *)(unsigned long)cmd.response,
  418. &resp, sizeof(resp))) {
  419. ret = -EFAULT;
  420. goto err2;
  421. }
  422. return 0;
  423. err2:
  424. rdma_destroy_id(ctx->cm_id);
  425. err1:
  426. mutex_lock(&mut);
  427. idr_remove(&ctx_idr, ctx->id);
  428. mutex_unlock(&mut);
  429. kfree(ctx);
  430. return ret;
  431. }
  432. static void ucma_cleanup_multicast(struct ucma_context *ctx)
  433. {
  434. struct ucma_multicast *mc, *tmp;
  435. mutex_lock(&mut);
  436. list_for_each_entry_safe(mc, tmp, &ctx->mc_list, list) {
  437. list_del(&mc->list);
  438. idr_remove(&multicast_idr, mc->id);
  439. kfree(mc);
  440. }
  441. mutex_unlock(&mut);
  442. }
  443. static void ucma_cleanup_mc_events(struct ucma_multicast *mc)
  444. {
  445. struct ucma_event *uevent, *tmp;
  446. list_for_each_entry_safe(uevent, tmp, &mc->ctx->file->event_list, list) {
  447. if (uevent->mc != mc)
  448. continue;
  449. list_del(&uevent->list);
  450. kfree(uevent);
  451. }
  452. }
  453. /*
  454. * ucma_free_ctx is called after the underlying rdma CM-ID is destroyed. At
  455. * this point, no new events will be reported from the hardware. However, we
  456. * still need to cleanup the UCMA context for this ID. Specifically, there
  457. * might be events that have not yet been consumed by the user space software.
  458. * These might include pending connect requests which we have not completed
  459. * processing. We cannot call rdma_destroy_id while holding the lock of the
  460. * context (file->mut), as it might cause a deadlock. We therefore extract all
  461. * relevant events from the context pending events list while holding the
  462. * mutex. After that we release them as needed.
  463. */
  464. static int ucma_free_ctx(struct ucma_context *ctx)
  465. {
  466. int events_reported;
  467. struct ucma_event *uevent, *tmp;
  468. LIST_HEAD(list);
  469. ucma_cleanup_multicast(ctx);
  470. /* Cleanup events not yet reported to the user. */
  471. mutex_lock(&ctx->file->mut);
  472. list_for_each_entry_safe(uevent, tmp, &ctx->file->event_list, list) {
  473. if (uevent->ctx == ctx)
  474. list_move_tail(&uevent->list, &list);
  475. }
  476. list_del(&ctx->list);
  477. mutex_unlock(&ctx->file->mut);
  478. list_for_each_entry_safe(uevent, tmp, &list, list) {
  479. list_del(&uevent->list);
  480. if (uevent->resp.event == RDMA_CM_EVENT_CONNECT_REQUEST)
  481. rdma_destroy_id(uevent->cm_id);
  482. kfree(uevent);
  483. }
  484. events_reported = ctx->events_reported;
  485. kfree(ctx);
  486. return events_reported;
  487. }
  488. static ssize_t ucma_destroy_id(struct ucma_file *file, const char __user *inbuf,
  489. int in_len, int out_len)
  490. {
  491. struct rdma_ucm_destroy_id cmd;
  492. struct rdma_ucm_destroy_id_resp resp;
  493. struct ucma_context *ctx;
  494. int ret = 0;
  495. if (out_len < sizeof(resp))
  496. return -ENOSPC;
  497. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  498. return -EFAULT;
  499. mutex_lock(&mut);
  500. ctx = _ucma_find_context(cmd.id, file);
  501. if (!IS_ERR(ctx))
  502. idr_remove(&ctx_idr, ctx->id);
  503. mutex_unlock(&mut);
  504. if (IS_ERR(ctx))
  505. return PTR_ERR(ctx);
  506. mutex_lock(&ctx->file->mut);
  507. ctx->destroying = 1;
  508. mutex_unlock(&ctx->file->mut);
  509. flush_workqueue(ctx->file->close_wq);
  510. /* At this point it's guaranteed that there is no inflight
  511. * closing task */
  512. mutex_lock(&mut);
  513. if (!ctx->closing) {
  514. mutex_unlock(&mut);
  515. ucma_put_ctx(ctx);
  516. wait_for_completion(&ctx->comp);
  517. rdma_destroy_id(ctx->cm_id);
  518. } else {
  519. mutex_unlock(&mut);
  520. }
  521. resp.events_reported = ucma_free_ctx(ctx);
  522. if (copy_to_user((void __user *)(unsigned long)cmd.response,
  523. &resp, sizeof(resp)))
  524. ret = -EFAULT;
  525. return ret;
  526. }
  527. static ssize_t ucma_bind_ip(struct ucma_file *file, const char __user *inbuf,
  528. int in_len, int out_len)
  529. {
  530. struct rdma_ucm_bind_ip cmd;
  531. struct ucma_context *ctx;
  532. int ret;
  533. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  534. return -EFAULT;
  535. ctx = ucma_get_ctx(file, cmd.id);
  536. if (IS_ERR(ctx))
  537. return PTR_ERR(ctx);
  538. ret = rdma_bind_addr(ctx->cm_id, (struct sockaddr *) &cmd.addr);
  539. ucma_put_ctx(ctx);
  540. return ret;
  541. }
  542. static ssize_t ucma_bind(struct ucma_file *file, const char __user *inbuf,
  543. int in_len, int out_len)
  544. {
  545. struct rdma_ucm_bind cmd;
  546. struct sockaddr *addr;
  547. struct ucma_context *ctx;
  548. int ret;
  549. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  550. return -EFAULT;
  551. addr = (struct sockaddr *) &cmd.addr;
  552. if (cmd.reserved || !cmd.addr_size || (cmd.addr_size != rdma_addr_size(addr)))
  553. return -EINVAL;
  554. ctx = ucma_get_ctx(file, cmd.id);
  555. if (IS_ERR(ctx))
  556. return PTR_ERR(ctx);
  557. ret = rdma_bind_addr(ctx->cm_id, addr);
  558. ucma_put_ctx(ctx);
  559. return ret;
  560. }
  561. static ssize_t ucma_resolve_ip(struct ucma_file *file,
  562. const char __user *inbuf,
  563. int in_len, int out_len)
  564. {
  565. struct rdma_ucm_resolve_ip cmd;
  566. struct ucma_context *ctx;
  567. int ret;
  568. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  569. return -EFAULT;
  570. ctx = ucma_get_ctx(file, cmd.id);
  571. if (IS_ERR(ctx))
  572. return PTR_ERR(ctx);
  573. ret = rdma_resolve_addr(ctx->cm_id, (struct sockaddr *) &cmd.src_addr,
  574. (struct sockaddr *) &cmd.dst_addr,
  575. cmd.timeout_ms);
  576. ucma_put_ctx(ctx);
  577. return ret;
  578. }
  579. static ssize_t ucma_resolve_addr(struct ucma_file *file,
  580. const char __user *inbuf,
  581. int in_len, int out_len)
  582. {
  583. struct rdma_ucm_resolve_addr cmd;
  584. struct sockaddr *src, *dst;
  585. struct ucma_context *ctx;
  586. int ret;
  587. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  588. return -EFAULT;
  589. src = (struct sockaddr *) &cmd.src_addr;
  590. dst = (struct sockaddr *) &cmd.dst_addr;
  591. if (cmd.reserved || (cmd.src_size && (cmd.src_size != rdma_addr_size(src))) ||
  592. !cmd.dst_size || (cmd.dst_size != rdma_addr_size(dst)))
  593. return -EINVAL;
  594. ctx = ucma_get_ctx(file, cmd.id);
  595. if (IS_ERR(ctx))
  596. return PTR_ERR(ctx);
  597. ret = rdma_resolve_addr(ctx->cm_id, src, dst, cmd.timeout_ms);
  598. ucma_put_ctx(ctx);
  599. return ret;
  600. }
  601. static ssize_t ucma_resolve_route(struct ucma_file *file,
  602. const char __user *inbuf,
  603. int in_len, int out_len)
  604. {
  605. struct rdma_ucm_resolve_route cmd;
  606. struct ucma_context *ctx;
  607. int ret;
  608. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  609. return -EFAULT;
  610. ctx = ucma_get_ctx(file, cmd.id);
  611. if (IS_ERR(ctx))
  612. return PTR_ERR(ctx);
  613. ret = rdma_resolve_route(ctx->cm_id, cmd.timeout_ms);
  614. ucma_put_ctx(ctx);
  615. return ret;
  616. }
  617. static void ucma_copy_ib_route(struct rdma_ucm_query_route_resp *resp,
  618. struct rdma_route *route)
  619. {
  620. struct rdma_dev_addr *dev_addr;
  621. resp->num_paths = route->num_paths;
  622. switch (route->num_paths) {
  623. case 0:
  624. dev_addr = &route->addr.dev_addr;
  625. rdma_addr_get_dgid(dev_addr,
  626. (union ib_gid *) &resp->ib_route[0].dgid);
  627. rdma_addr_get_sgid(dev_addr,
  628. (union ib_gid *) &resp->ib_route[0].sgid);
  629. resp->ib_route[0].pkey = cpu_to_be16(ib_addr_get_pkey(dev_addr));
  630. break;
  631. case 2:
  632. ib_copy_path_rec_to_user(&resp->ib_route[1],
  633. &route->path_rec[1]);
  634. /* fall through */
  635. case 1:
  636. ib_copy_path_rec_to_user(&resp->ib_route[0],
  637. &route->path_rec[0]);
  638. break;
  639. default:
  640. break;
  641. }
  642. }
  643. static void ucma_copy_iboe_route(struct rdma_ucm_query_route_resp *resp,
  644. struct rdma_route *route)
  645. {
  646. resp->num_paths = route->num_paths;
  647. switch (route->num_paths) {
  648. case 0:
  649. rdma_ip2gid((struct sockaddr *)&route->addr.dst_addr,
  650. (union ib_gid *)&resp->ib_route[0].dgid);
  651. rdma_ip2gid((struct sockaddr *)&route->addr.src_addr,
  652. (union ib_gid *)&resp->ib_route[0].sgid);
  653. resp->ib_route[0].pkey = cpu_to_be16(0xffff);
  654. break;
  655. case 2:
  656. ib_copy_path_rec_to_user(&resp->ib_route[1],
  657. &route->path_rec[1]);
  658. /* fall through */
  659. case 1:
  660. ib_copy_path_rec_to_user(&resp->ib_route[0],
  661. &route->path_rec[0]);
  662. break;
  663. default:
  664. break;
  665. }
  666. }
  667. static void ucma_copy_iw_route(struct rdma_ucm_query_route_resp *resp,
  668. struct rdma_route *route)
  669. {
  670. struct rdma_dev_addr *dev_addr;
  671. dev_addr = &route->addr.dev_addr;
  672. rdma_addr_get_dgid(dev_addr, (union ib_gid *) &resp->ib_route[0].dgid);
  673. rdma_addr_get_sgid(dev_addr, (union ib_gid *) &resp->ib_route[0].sgid);
  674. }
  675. static ssize_t ucma_query_route(struct ucma_file *file,
  676. const char __user *inbuf,
  677. int in_len, int out_len)
  678. {
  679. struct rdma_ucm_query cmd;
  680. struct rdma_ucm_query_route_resp resp;
  681. struct ucma_context *ctx;
  682. struct sockaddr *addr;
  683. int ret = 0;
  684. if (out_len < sizeof(resp))
  685. return -ENOSPC;
  686. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  687. return -EFAULT;
  688. ctx = ucma_get_ctx(file, cmd.id);
  689. if (IS_ERR(ctx))
  690. return PTR_ERR(ctx);
  691. memset(&resp, 0, sizeof resp);
  692. addr = (struct sockaddr *) &ctx->cm_id->route.addr.src_addr;
  693. memcpy(&resp.src_addr, addr, addr->sa_family == AF_INET ?
  694. sizeof(struct sockaddr_in) :
  695. sizeof(struct sockaddr_in6));
  696. addr = (struct sockaddr *) &ctx->cm_id->route.addr.dst_addr;
  697. memcpy(&resp.dst_addr, addr, addr->sa_family == AF_INET ?
  698. sizeof(struct sockaddr_in) :
  699. sizeof(struct sockaddr_in6));
  700. if (!ctx->cm_id->device)
  701. goto out;
  702. resp.node_guid = (__force __u64) ctx->cm_id->device->node_guid;
  703. resp.port_num = ctx->cm_id->port_num;
  704. if (rdma_cap_ib_sa(ctx->cm_id->device, ctx->cm_id->port_num))
  705. ucma_copy_ib_route(&resp, &ctx->cm_id->route);
  706. else if (rdma_protocol_roce(ctx->cm_id->device, ctx->cm_id->port_num))
  707. ucma_copy_iboe_route(&resp, &ctx->cm_id->route);
  708. else if (rdma_protocol_iwarp(ctx->cm_id->device, ctx->cm_id->port_num))
  709. ucma_copy_iw_route(&resp, &ctx->cm_id->route);
  710. out:
  711. if (copy_to_user((void __user *)(unsigned long)cmd.response,
  712. &resp, sizeof(resp)))
  713. ret = -EFAULT;
  714. ucma_put_ctx(ctx);
  715. return ret;
  716. }
  717. static void ucma_query_device_addr(struct rdma_cm_id *cm_id,
  718. struct rdma_ucm_query_addr_resp *resp)
  719. {
  720. if (!cm_id->device)
  721. return;
  722. resp->node_guid = (__force __u64) cm_id->device->node_guid;
  723. resp->port_num = cm_id->port_num;
  724. resp->pkey = (__force __u16) cpu_to_be16(
  725. ib_addr_get_pkey(&cm_id->route.addr.dev_addr));
  726. }
  727. static ssize_t ucma_query_addr(struct ucma_context *ctx,
  728. void __user *response, int out_len)
  729. {
  730. struct rdma_ucm_query_addr_resp resp;
  731. struct sockaddr *addr;
  732. int ret = 0;
  733. if (out_len < sizeof(resp))
  734. return -ENOSPC;
  735. memset(&resp, 0, sizeof resp);
  736. addr = (struct sockaddr *) &ctx->cm_id->route.addr.src_addr;
  737. resp.src_size = rdma_addr_size(addr);
  738. memcpy(&resp.src_addr, addr, resp.src_size);
  739. addr = (struct sockaddr *) &ctx->cm_id->route.addr.dst_addr;
  740. resp.dst_size = rdma_addr_size(addr);
  741. memcpy(&resp.dst_addr, addr, resp.dst_size);
  742. ucma_query_device_addr(ctx->cm_id, &resp);
  743. if (copy_to_user(response, &resp, sizeof(resp)))
  744. ret = -EFAULT;
  745. return ret;
  746. }
  747. static ssize_t ucma_query_path(struct ucma_context *ctx,
  748. void __user *response, int out_len)
  749. {
  750. struct rdma_ucm_query_path_resp *resp;
  751. int i, ret = 0;
  752. if (out_len < sizeof(*resp))
  753. return -ENOSPC;
  754. resp = kzalloc(out_len, GFP_KERNEL);
  755. if (!resp)
  756. return -ENOMEM;
  757. resp->num_paths = ctx->cm_id->route.num_paths;
  758. for (i = 0, out_len -= sizeof(*resp);
  759. i < resp->num_paths && out_len > sizeof(struct ib_path_rec_data);
  760. i++, out_len -= sizeof(struct ib_path_rec_data)) {
  761. resp->path_data[i].flags = IB_PATH_GMP | IB_PATH_PRIMARY |
  762. IB_PATH_BIDIRECTIONAL;
  763. ib_sa_pack_path(&ctx->cm_id->route.path_rec[i],
  764. &resp->path_data[i].path_rec);
  765. }
  766. if (copy_to_user(response, resp,
  767. sizeof(*resp) + (i * sizeof(struct ib_path_rec_data))))
  768. ret = -EFAULT;
  769. kfree(resp);
  770. return ret;
  771. }
  772. static ssize_t ucma_query_gid(struct ucma_context *ctx,
  773. void __user *response, int out_len)
  774. {
  775. struct rdma_ucm_query_addr_resp resp;
  776. struct sockaddr_ib *addr;
  777. int ret = 0;
  778. if (out_len < sizeof(resp))
  779. return -ENOSPC;
  780. memset(&resp, 0, sizeof resp);
  781. ucma_query_device_addr(ctx->cm_id, &resp);
  782. addr = (struct sockaddr_ib *) &resp.src_addr;
  783. resp.src_size = sizeof(*addr);
  784. if (ctx->cm_id->route.addr.src_addr.ss_family == AF_IB) {
  785. memcpy(addr, &ctx->cm_id->route.addr.src_addr, resp.src_size);
  786. } else {
  787. addr->sib_family = AF_IB;
  788. addr->sib_pkey = (__force __be16) resp.pkey;
  789. rdma_addr_get_sgid(&ctx->cm_id->route.addr.dev_addr,
  790. (union ib_gid *) &addr->sib_addr);
  791. addr->sib_sid = rdma_get_service_id(ctx->cm_id, (struct sockaddr *)
  792. &ctx->cm_id->route.addr.src_addr);
  793. }
  794. addr = (struct sockaddr_ib *) &resp.dst_addr;
  795. resp.dst_size = sizeof(*addr);
  796. if (ctx->cm_id->route.addr.dst_addr.ss_family == AF_IB) {
  797. memcpy(addr, &ctx->cm_id->route.addr.dst_addr, resp.dst_size);
  798. } else {
  799. addr->sib_family = AF_IB;
  800. addr->sib_pkey = (__force __be16) resp.pkey;
  801. rdma_addr_get_dgid(&ctx->cm_id->route.addr.dev_addr,
  802. (union ib_gid *) &addr->sib_addr);
  803. addr->sib_sid = rdma_get_service_id(ctx->cm_id, (struct sockaddr *)
  804. &ctx->cm_id->route.addr.dst_addr);
  805. }
  806. if (copy_to_user(response, &resp, sizeof(resp)))
  807. ret = -EFAULT;
  808. return ret;
  809. }
  810. static ssize_t ucma_query(struct ucma_file *file,
  811. const char __user *inbuf,
  812. int in_len, int out_len)
  813. {
  814. struct rdma_ucm_query cmd;
  815. struct ucma_context *ctx;
  816. void __user *response;
  817. int ret;
  818. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  819. return -EFAULT;
  820. response = (void __user *)(unsigned long) cmd.response;
  821. ctx = ucma_get_ctx(file, cmd.id);
  822. if (IS_ERR(ctx))
  823. return PTR_ERR(ctx);
  824. switch (cmd.option) {
  825. case RDMA_USER_CM_QUERY_ADDR:
  826. ret = ucma_query_addr(ctx, response, out_len);
  827. break;
  828. case RDMA_USER_CM_QUERY_PATH:
  829. ret = ucma_query_path(ctx, response, out_len);
  830. break;
  831. case RDMA_USER_CM_QUERY_GID:
  832. ret = ucma_query_gid(ctx, response, out_len);
  833. break;
  834. default:
  835. ret = -ENOSYS;
  836. break;
  837. }
  838. ucma_put_ctx(ctx);
  839. return ret;
  840. }
  841. static void ucma_copy_conn_param(struct rdma_cm_id *id,
  842. struct rdma_conn_param *dst,
  843. struct rdma_ucm_conn_param *src)
  844. {
  845. dst->private_data = src->private_data;
  846. dst->private_data_len = src->private_data_len;
  847. dst->responder_resources =src->responder_resources;
  848. dst->initiator_depth = src->initiator_depth;
  849. dst->flow_control = src->flow_control;
  850. dst->retry_count = src->retry_count;
  851. dst->rnr_retry_count = src->rnr_retry_count;
  852. dst->srq = src->srq;
  853. dst->qp_num = src->qp_num;
  854. dst->qkey = (id->route.addr.src_addr.ss_family == AF_IB) ? src->qkey : 0;
  855. }
  856. static ssize_t ucma_connect(struct ucma_file *file, const char __user *inbuf,
  857. int in_len, int out_len)
  858. {
  859. struct rdma_ucm_connect cmd;
  860. struct rdma_conn_param conn_param;
  861. struct ucma_context *ctx;
  862. int ret;
  863. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  864. return -EFAULT;
  865. if (!cmd.conn_param.valid)
  866. return -EINVAL;
  867. ctx = ucma_get_ctx(file, cmd.id);
  868. if (IS_ERR(ctx))
  869. return PTR_ERR(ctx);
  870. ucma_copy_conn_param(ctx->cm_id, &conn_param, &cmd.conn_param);
  871. ret = rdma_connect(ctx->cm_id, &conn_param);
  872. ucma_put_ctx(ctx);
  873. return ret;
  874. }
  875. static ssize_t ucma_listen(struct ucma_file *file, const char __user *inbuf,
  876. int in_len, int out_len)
  877. {
  878. struct rdma_ucm_listen cmd;
  879. struct ucma_context *ctx;
  880. int ret;
  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. ctx->backlog = cmd.backlog > 0 && cmd.backlog < max_backlog ?
  887. cmd.backlog : max_backlog;
  888. ret = rdma_listen(ctx->cm_id, ctx->backlog);
  889. ucma_put_ctx(ctx);
  890. return ret;
  891. }
  892. static ssize_t ucma_accept(struct ucma_file *file, const char __user *inbuf,
  893. int in_len, int out_len)
  894. {
  895. struct rdma_ucm_accept cmd;
  896. struct rdma_conn_param conn_param;
  897. struct ucma_context *ctx;
  898. int ret;
  899. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  900. return -EFAULT;
  901. ctx = ucma_get_ctx(file, cmd.id);
  902. if (IS_ERR(ctx))
  903. return PTR_ERR(ctx);
  904. if (cmd.conn_param.valid) {
  905. ucma_copy_conn_param(ctx->cm_id, &conn_param, &cmd.conn_param);
  906. mutex_lock(&file->mut);
  907. ret = rdma_accept(ctx->cm_id, &conn_param);
  908. if (!ret)
  909. ctx->uid = cmd.uid;
  910. mutex_unlock(&file->mut);
  911. } else
  912. ret = rdma_accept(ctx->cm_id, NULL);
  913. ucma_put_ctx(ctx);
  914. return ret;
  915. }
  916. static ssize_t ucma_reject(struct ucma_file *file, const char __user *inbuf,
  917. int in_len, int out_len)
  918. {
  919. struct rdma_ucm_reject cmd;
  920. struct ucma_context *ctx;
  921. int ret;
  922. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  923. return -EFAULT;
  924. ctx = ucma_get_ctx(file, cmd.id);
  925. if (IS_ERR(ctx))
  926. return PTR_ERR(ctx);
  927. ret = rdma_reject(ctx->cm_id, cmd.private_data, cmd.private_data_len);
  928. ucma_put_ctx(ctx);
  929. return ret;
  930. }
  931. static ssize_t ucma_disconnect(struct ucma_file *file, const char __user *inbuf,
  932. int in_len, int out_len)
  933. {
  934. struct rdma_ucm_disconnect cmd;
  935. struct ucma_context *ctx;
  936. int ret;
  937. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  938. return -EFAULT;
  939. ctx = ucma_get_ctx(file, cmd.id);
  940. if (IS_ERR(ctx))
  941. return PTR_ERR(ctx);
  942. ret = rdma_disconnect(ctx->cm_id);
  943. ucma_put_ctx(ctx);
  944. return ret;
  945. }
  946. static ssize_t ucma_init_qp_attr(struct ucma_file *file,
  947. const char __user *inbuf,
  948. int in_len, int out_len)
  949. {
  950. struct rdma_ucm_init_qp_attr cmd;
  951. struct ib_uverbs_qp_attr resp;
  952. struct ucma_context *ctx;
  953. struct ib_qp_attr qp_attr;
  954. int ret;
  955. if (out_len < sizeof(resp))
  956. return -ENOSPC;
  957. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  958. return -EFAULT;
  959. ctx = ucma_get_ctx(file, cmd.id);
  960. if (IS_ERR(ctx))
  961. return PTR_ERR(ctx);
  962. resp.qp_attr_mask = 0;
  963. memset(&qp_attr, 0, sizeof qp_attr);
  964. qp_attr.qp_state = cmd.qp_state;
  965. ret = rdma_init_qp_attr(ctx->cm_id, &qp_attr, &resp.qp_attr_mask);
  966. if (ret)
  967. goto out;
  968. ib_copy_qp_attr_to_user(&resp, &qp_attr);
  969. if (copy_to_user((void __user *)(unsigned long)cmd.response,
  970. &resp, sizeof(resp)))
  971. ret = -EFAULT;
  972. out:
  973. ucma_put_ctx(ctx);
  974. return ret;
  975. }
  976. static int ucma_set_option_id(struct ucma_context *ctx, int optname,
  977. void *optval, size_t optlen)
  978. {
  979. int ret = 0;
  980. switch (optname) {
  981. case RDMA_OPTION_ID_TOS:
  982. if (optlen != sizeof(u8)) {
  983. ret = -EINVAL;
  984. break;
  985. }
  986. rdma_set_service_type(ctx->cm_id, *((u8 *) optval));
  987. break;
  988. case RDMA_OPTION_ID_REUSEADDR:
  989. if (optlen != sizeof(int)) {
  990. ret = -EINVAL;
  991. break;
  992. }
  993. ret = rdma_set_reuseaddr(ctx->cm_id, *((int *) optval) ? 1 : 0);
  994. break;
  995. case RDMA_OPTION_ID_AFONLY:
  996. if (optlen != sizeof(int)) {
  997. ret = -EINVAL;
  998. break;
  999. }
  1000. ret = rdma_set_afonly(ctx->cm_id, *((int *) optval) ? 1 : 0);
  1001. break;
  1002. default:
  1003. ret = -ENOSYS;
  1004. }
  1005. return ret;
  1006. }
  1007. static int ucma_set_ib_path(struct ucma_context *ctx,
  1008. struct ib_path_rec_data *path_data, size_t optlen)
  1009. {
  1010. struct ib_sa_path_rec sa_path;
  1011. struct rdma_cm_event event;
  1012. int ret;
  1013. if (optlen % sizeof(*path_data))
  1014. return -EINVAL;
  1015. for (; optlen; optlen -= sizeof(*path_data), path_data++) {
  1016. if (path_data->flags == (IB_PATH_GMP | IB_PATH_PRIMARY |
  1017. IB_PATH_BIDIRECTIONAL))
  1018. break;
  1019. }
  1020. if (!optlen)
  1021. return -EINVAL;
  1022. memset(&sa_path, 0, sizeof(sa_path));
  1023. sa_path.vlan_id = 0xffff;
  1024. ib_sa_unpack_path(path_data->path_rec, &sa_path);
  1025. ret = rdma_set_ib_paths(ctx->cm_id, &sa_path, 1);
  1026. if (ret)
  1027. return ret;
  1028. memset(&event, 0, sizeof event);
  1029. event.event = RDMA_CM_EVENT_ROUTE_RESOLVED;
  1030. return ucma_event_handler(ctx->cm_id, &event);
  1031. }
  1032. static int ucma_set_option_ib(struct ucma_context *ctx, int optname,
  1033. void *optval, size_t optlen)
  1034. {
  1035. int ret;
  1036. switch (optname) {
  1037. case RDMA_OPTION_IB_PATH:
  1038. ret = ucma_set_ib_path(ctx, optval, optlen);
  1039. break;
  1040. default:
  1041. ret = -ENOSYS;
  1042. }
  1043. return ret;
  1044. }
  1045. static int ucma_set_option_level(struct ucma_context *ctx, int level,
  1046. int optname, void *optval, size_t optlen)
  1047. {
  1048. int ret;
  1049. switch (level) {
  1050. case RDMA_OPTION_ID:
  1051. ret = ucma_set_option_id(ctx, optname, optval, optlen);
  1052. break;
  1053. case RDMA_OPTION_IB:
  1054. ret = ucma_set_option_ib(ctx, optname, optval, optlen);
  1055. break;
  1056. default:
  1057. ret = -ENOSYS;
  1058. }
  1059. return ret;
  1060. }
  1061. static ssize_t ucma_set_option(struct ucma_file *file, const char __user *inbuf,
  1062. int in_len, int out_len)
  1063. {
  1064. struct rdma_ucm_set_option cmd;
  1065. struct ucma_context *ctx;
  1066. void *optval;
  1067. int ret;
  1068. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  1069. return -EFAULT;
  1070. ctx = ucma_get_ctx(file, cmd.id);
  1071. if (IS_ERR(ctx))
  1072. return PTR_ERR(ctx);
  1073. optval = memdup_user((void __user *) (unsigned long) cmd.optval,
  1074. cmd.optlen);
  1075. if (IS_ERR(optval)) {
  1076. ret = PTR_ERR(optval);
  1077. goto out;
  1078. }
  1079. ret = ucma_set_option_level(ctx, cmd.level, cmd.optname, optval,
  1080. cmd.optlen);
  1081. kfree(optval);
  1082. out:
  1083. ucma_put_ctx(ctx);
  1084. return ret;
  1085. }
  1086. static ssize_t ucma_notify(struct ucma_file *file, const char __user *inbuf,
  1087. int in_len, int out_len)
  1088. {
  1089. struct rdma_ucm_notify cmd;
  1090. struct ucma_context *ctx;
  1091. int ret;
  1092. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  1093. return -EFAULT;
  1094. ctx = ucma_get_ctx(file, cmd.id);
  1095. if (IS_ERR(ctx))
  1096. return PTR_ERR(ctx);
  1097. ret = rdma_notify(ctx->cm_id, (enum ib_event_type) cmd.event);
  1098. ucma_put_ctx(ctx);
  1099. return ret;
  1100. }
  1101. static ssize_t ucma_process_join(struct ucma_file *file,
  1102. struct rdma_ucm_join_mcast *cmd, int out_len)
  1103. {
  1104. struct rdma_ucm_create_id_resp resp;
  1105. struct ucma_context *ctx;
  1106. struct ucma_multicast *mc;
  1107. struct sockaddr *addr;
  1108. int ret;
  1109. if (out_len < sizeof(resp))
  1110. return -ENOSPC;
  1111. addr = (struct sockaddr *) &cmd->addr;
  1112. if (cmd->reserved || !cmd->addr_size || (cmd->addr_size != rdma_addr_size(addr)))
  1113. return -EINVAL;
  1114. ctx = ucma_get_ctx(file, cmd->id);
  1115. if (IS_ERR(ctx))
  1116. return PTR_ERR(ctx);
  1117. mutex_lock(&file->mut);
  1118. mc = ucma_alloc_multicast(ctx);
  1119. if (!mc) {
  1120. ret = -ENOMEM;
  1121. goto err1;
  1122. }
  1123. mc->uid = cmd->uid;
  1124. memcpy(&mc->addr, addr, cmd->addr_size);
  1125. ret = rdma_join_multicast(ctx->cm_id, (struct sockaddr *) &mc->addr, mc);
  1126. if (ret)
  1127. goto err2;
  1128. resp.id = mc->id;
  1129. if (copy_to_user((void __user *)(unsigned long) cmd->response,
  1130. &resp, sizeof(resp))) {
  1131. ret = -EFAULT;
  1132. goto err3;
  1133. }
  1134. mutex_unlock(&file->mut);
  1135. ucma_put_ctx(ctx);
  1136. return 0;
  1137. err3:
  1138. rdma_leave_multicast(ctx->cm_id, (struct sockaddr *) &mc->addr);
  1139. ucma_cleanup_mc_events(mc);
  1140. err2:
  1141. mutex_lock(&mut);
  1142. idr_remove(&multicast_idr, mc->id);
  1143. mutex_unlock(&mut);
  1144. list_del(&mc->list);
  1145. kfree(mc);
  1146. err1:
  1147. mutex_unlock(&file->mut);
  1148. ucma_put_ctx(ctx);
  1149. return ret;
  1150. }
  1151. static ssize_t ucma_join_ip_multicast(struct ucma_file *file,
  1152. const char __user *inbuf,
  1153. int in_len, int out_len)
  1154. {
  1155. struct rdma_ucm_join_ip_mcast cmd;
  1156. struct rdma_ucm_join_mcast join_cmd;
  1157. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  1158. return -EFAULT;
  1159. join_cmd.response = cmd.response;
  1160. join_cmd.uid = cmd.uid;
  1161. join_cmd.id = cmd.id;
  1162. join_cmd.addr_size = rdma_addr_size((struct sockaddr *) &cmd.addr);
  1163. join_cmd.reserved = 0;
  1164. memcpy(&join_cmd.addr, &cmd.addr, join_cmd.addr_size);
  1165. return ucma_process_join(file, &join_cmd, out_len);
  1166. }
  1167. static ssize_t ucma_join_multicast(struct ucma_file *file,
  1168. const char __user *inbuf,
  1169. int in_len, int out_len)
  1170. {
  1171. struct rdma_ucm_join_mcast cmd;
  1172. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  1173. return -EFAULT;
  1174. return ucma_process_join(file, &cmd, out_len);
  1175. }
  1176. static ssize_t ucma_leave_multicast(struct ucma_file *file,
  1177. const char __user *inbuf,
  1178. int in_len, int out_len)
  1179. {
  1180. struct rdma_ucm_destroy_id cmd;
  1181. struct rdma_ucm_destroy_id_resp resp;
  1182. struct ucma_multicast *mc;
  1183. int ret = 0;
  1184. if (out_len < sizeof(resp))
  1185. return -ENOSPC;
  1186. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  1187. return -EFAULT;
  1188. mutex_lock(&mut);
  1189. mc = idr_find(&multicast_idr, cmd.id);
  1190. if (!mc)
  1191. mc = ERR_PTR(-ENOENT);
  1192. else if (mc->ctx->file != file)
  1193. mc = ERR_PTR(-EINVAL);
  1194. else if (!atomic_inc_not_zero(&mc->ctx->ref))
  1195. mc = ERR_PTR(-ENXIO);
  1196. else
  1197. idr_remove(&multicast_idr, mc->id);
  1198. mutex_unlock(&mut);
  1199. if (IS_ERR(mc)) {
  1200. ret = PTR_ERR(mc);
  1201. goto out;
  1202. }
  1203. rdma_leave_multicast(mc->ctx->cm_id, (struct sockaddr *) &mc->addr);
  1204. mutex_lock(&mc->ctx->file->mut);
  1205. ucma_cleanup_mc_events(mc);
  1206. list_del(&mc->list);
  1207. mutex_unlock(&mc->ctx->file->mut);
  1208. ucma_put_ctx(mc->ctx);
  1209. resp.events_reported = mc->events_reported;
  1210. kfree(mc);
  1211. if (copy_to_user((void __user *)(unsigned long)cmd.response,
  1212. &resp, sizeof(resp)))
  1213. ret = -EFAULT;
  1214. out:
  1215. return ret;
  1216. }
  1217. static void ucma_lock_files(struct ucma_file *file1, struct ucma_file *file2)
  1218. {
  1219. /* Acquire mutex's based on pointer comparison to prevent deadlock. */
  1220. if (file1 < file2) {
  1221. mutex_lock(&file1->mut);
  1222. mutex_lock_nested(&file2->mut, SINGLE_DEPTH_NESTING);
  1223. } else {
  1224. mutex_lock(&file2->mut);
  1225. mutex_lock_nested(&file1->mut, SINGLE_DEPTH_NESTING);
  1226. }
  1227. }
  1228. static void ucma_unlock_files(struct ucma_file *file1, struct ucma_file *file2)
  1229. {
  1230. if (file1 < file2) {
  1231. mutex_unlock(&file2->mut);
  1232. mutex_unlock(&file1->mut);
  1233. } else {
  1234. mutex_unlock(&file1->mut);
  1235. mutex_unlock(&file2->mut);
  1236. }
  1237. }
  1238. static void ucma_move_events(struct ucma_context *ctx, struct ucma_file *file)
  1239. {
  1240. struct ucma_event *uevent, *tmp;
  1241. list_for_each_entry_safe(uevent, tmp, &ctx->file->event_list, list)
  1242. if (uevent->ctx == ctx)
  1243. list_move_tail(&uevent->list, &file->event_list);
  1244. }
  1245. static ssize_t ucma_migrate_id(struct ucma_file *new_file,
  1246. const char __user *inbuf,
  1247. int in_len, int out_len)
  1248. {
  1249. struct rdma_ucm_migrate_id cmd;
  1250. struct rdma_ucm_migrate_resp resp;
  1251. struct ucma_context *ctx;
  1252. struct fd f;
  1253. struct ucma_file *cur_file;
  1254. int ret = 0;
  1255. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  1256. return -EFAULT;
  1257. /* Get current fd to protect against it being closed */
  1258. f = fdget(cmd.fd);
  1259. if (!f.file)
  1260. return -ENOENT;
  1261. /* Validate current fd and prevent destruction of id. */
  1262. ctx = ucma_get_ctx(f.file->private_data, cmd.id);
  1263. if (IS_ERR(ctx)) {
  1264. ret = PTR_ERR(ctx);
  1265. goto file_put;
  1266. }
  1267. cur_file = ctx->file;
  1268. if (cur_file == new_file) {
  1269. resp.events_reported = ctx->events_reported;
  1270. goto response;
  1271. }
  1272. /*
  1273. * Migrate events between fd's, maintaining order, and avoiding new
  1274. * events being added before existing events.
  1275. */
  1276. ucma_lock_files(cur_file, new_file);
  1277. mutex_lock(&mut);
  1278. list_move_tail(&ctx->list, &new_file->ctx_list);
  1279. ucma_move_events(ctx, new_file);
  1280. ctx->file = new_file;
  1281. resp.events_reported = ctx->events_reported;
  1282. mutex_unlock(&mut);
  1283. ucma_unlock_files(cur_file, new_file);
  1284. response:
  1285. if (copy_to_user((void __user *)(unsigned long)cmd.response,
  1286. &resp, sizeof(resp)))
  1287. ret = -EFAULT;
  1288. ucma_put_ctx(ctx);
  1289. file_put:
  1290. fdput(f);
  1291. return ret;
  1292. }
  1293. static ssize_t (*ucma_cmd_table[])(struct ucma_file *file,
  1294. const char __user *inbuf,
  1295. int in_len, int out_len) = {
  1296. [RDMA_USER_CM_CMD_CREATE_ID] = ucma_create_id,
  1297. [RDMA_USER_CM_CMD_DESTROY_ID] = ucma_destroy_id,
  1298. [RDMA_USER_CM_CMD_BIND_IP] = ucma_bind_ip,
  1299. [RDMA_USER_CM_CMD_RESOLVE_IP] = ucma_resolve_ip,
  1300. [RDMA_USER_CM_CMD_RESOLVE_ROUTE] = ucma_resolve_route,
  1301. [RDMA_USER_CM_CMD_QUERY_ROUTE] = ucma_query_route,
  1302. [RDMA_USER_CM_CMD_CONNECT] = ucma_connect,
  1303. [RDMA_USER_CM_CMD_LISTEN] = ucma_listen,
  1304. [RDMA_USER_CM_CMD_ACCEPT] = ucma_accept,
  1305. [RDMA_USER_CM_CMD_REJECT] = ucma_reject,
  1306. [RDMA_USER_CM_CMD_DISCONNECT] = ucma_disconnect,
  1307. [RDMA_USER_CM_CMD_INIT_QP_ATTR] = ucma_init_qp_attr,
  1308. [RDMA_USER_CM_CMD_GET_EVENT] = ucma_get_event,
  1309. [RDMA_USER_CM_CMD_GET_OPTION] = NULL,
  1310. [RDMA_USER_CM_CMD_SET_OPTION] = ucma_set_option,
  1311. [RDMA_USER_CM_CMD_NOTIFY] = ucma_notify,
  1312. [RDMA_USER_CM_CMD_JOIN_IP_MCAST] = ucma_join_ip_multicast,
  1313. [RDMA_USER_CM_CMD_LEAVE_MCAST] = ucma_leave_multicast,
  1314. [RDMA_USER_CM_CMD_MIGRATE_ID] = ucma_migrate_id,
  1315. [RDMA_USER_CM_CMD_QUERY] = ucma_query,
  1316. [RDMA_USER_CM_CMD_BIND] = ucma_bind,
  1317. [RDMA_USER_CM_CMD_RESOLVE_ADDR] = ucma_resolve_addr,
  1318. [RDMA_USER_CM_CMD_JOIN_MCAST] = ucma_join_multicast
  1319. };
  1320. static ssize_t ucma_write(struct file *filp, const char __user *buf,
  1321. size_t len, loff_t *pos)
  1322. {
  1323. struct ucma_file *file = filp->private_data;
  1324. struct rdma_ucm_cmd_hdr hdr;
  1325. ssize_t ret;
  1326. if (len < sizeof(hdr))
  1327. return -EINVAL;
  1328. if (copy_from_user(&hdr, buf, sizeof(hdr)))
  1329. return -EFAULT;
  1330. if (hdr.cmd >= ARRAY_SIZE(ucma_cmd_table))
  1331. return -EINVAL;
  1332. if (hdr.in + sizeof(hdr) > len)
  1333. return -EINVAL;
  1334. if (!ucma_cmd_table[hdr.cmd])
  1335. return -ENOSYS;
  1336. ret = ucma_cmd_table[hdr.cmd](file, buf + sizeof(hdr), hdr.in, hdr.out);
  1337. if (!ret)
  1338. ret = len;
  1339. return ret;
  1340. }
  1341. static unsigned int ucma_poll(struct file *filp, struct poll_table_struct *wait)
  1342. {
  1343. struct ucma_file *file = filp->private_data;
  1344. unsigned int mask = 0;
  1345. poll_wait(filp, &file->poll_wait, wait);
  1346. if (!list_empty(&file->event_list))
  1347. mask = POLLIN | POLLRDNORM;
  1348. return mask;
  1349. }
  1350. /*
  1351. * ucma_open() does not need the BKL:
  1352. *
  1353. * - no global state is referred to;
  1354. * - there is no ioctl method to race against;
  1355. * - no further module initialization is required for open to work
  1356. * after the device is registered.
  1357. */
  1358. static int ucma_open(struct inode *inode, struct file *filp)
  1359. {
  1360. struct ucma_file *file;
  1361. file = kmalloc(sizeof *file, GFP_KERNEL);
  1362. if (!file)
  1363. return -ENOMEM;
  1364. INIT_LIST_HEAD(&file->event_list);
  1365. INIT_LIST_HEAD(&file->ctx_list);
  1366. init_waitqueue_head(&file->poll_wait);
  1367. mutex_init(&file->mut);
  1368. file->close_wq = create_singlethread_workqueue("ucma_close_id");
  1369. filp->private_data = file;
  1370. file->filp = filp;
  1371. return nonseekable_open(inode, filp);
  1372. }
  1373. static int ucma_close(struct inode *inode, struct file *filp)
  1374. {
  1375. struct ucma_file *file = filp->private_data;
  1376. struct ucma_context *ctx, *tmp;
  1377. mutex_lock(&file->mut);
  1378. list_for_each_entry_safe(ctx, tmp, &file->ctx_list, list) {
  1379. ctx->destroying = 1;
  1380. mutex_unlock(&file->mut);
  1381. mutex_lock(&mut);
  1382. idr_remove(&ctx_idr, ctx->id);
  1383. mutex_unlock(&mut);
  1384. flush_workqueue(file->close_wq);
  1385. /* At that step once ctx was marked as destroying and workqueue
  1386. * was flushed we are safe from any inflights handlers that
  1387. * might put other closing task.
  1388. */
  1389. mutex_lock(&mut);
  1390. if (!ctx->closing) {
  1391. mutex_unlock(&mut);
  1392. /* rdma_destroy_id ensures that no event handlers are
  1393. * inflight for that id before releasing it.
  1394. */
  1395. rdma_destroy_id(ctx->cm_id);
  1396. } else {
  1397. mutex_unlock(&mut);
  1398. }
  1399. ucma_free_ctx(ctx);
  1400. mutex_lock(&file->mut);
  1401. }
  1402. mutex_unlock(&file->mut);
  1403. destroy_workqueue(file->close_wq);
  1404. kfree(file);
  1405. return 0;
  1406. }
  1407. static const struct file_operations ucma_fops = {
  1408. .owner = THIS_MODULE,
  1409. .open = ucma_open,
  1410. .release = ucma_close,
  1411. .write = ucma_write,
  1412. .poll = ucma_poll,
  1413. .llseek = no_llseek,
  1414. };
  1415. static struct miscdevice ucma_misc = {
  1416. .minor = MISC_DYNAMIC_MINOR,
  1417. .name = "rdma_cm",
  1418. .nodename = "infiniband/rdma_cm",
  1419. .mode = 0666,
  1420. .fops = &ucma_fops,
  1421. };
  1422. static ssize_t show_abi_version(struct device *dev,
  1423. struct device_attribute *attr,
  1424. char *buf)
  1425. {
  1426. return sprintf(buf, "%d\n", RDMA_USER_CM_ABI_VERSION);
  1427. }
  1428. static DEVICE_ATTR(abi_version, S_IRUGO, show_abi_version, NULL);
  1429. static int __init ucma_init(void)
  1430. {
  1431. int ret;
  1432. ret = misc_register(&ucma_misc);
  1433. if (ret)
  1434. return ret;
  1435. ret = device_create_file(ucma_misc.this_device, &dev_attr_abi_version);
  1436. if (ret) {
  1437. printk(KERN_ERR "rdma_ucm: couldn't create abi_version attr\n");
  1438. goto err1;
  1439. }
  1440. ucma_ctl_table_hdr = register_net_sysctl(&init_net, "net/rdma_ucm", ucma_ctl_table);
  1441. if (!ucma_ctl_table_hdr) {
  1442. printk(KERN_ERR "rdma_ucm: couldn't register sysctl paths\n");
  1443. ret = -ENOMEM;
  1444. goto err2;
  1445. }
  1446. return 0;
  1447. err2:
  1448. device_remove_file(ucma_misc.this_device, &dev_attr_abi_version);
  1449. err1:
  1450. misc_deregister(&ucma_misc);
  1451. return ret;
  1452. }
  1453. static void __exit ucma_cleanup(void)
  1454. {
  1455. unregister_net_sysctl_table(ucma_ctl_table_hdr);
  1456. device_remove_file(ucma_misc.this_device, &dev_attr_abi_version);
  1457. misc_deregister(&ucma_misc);
  1458. idr_destroy(&ctx_idr);
  1459. idr_destroy(&multicast_idr);
  1460. }
  1461. module_init(ucma_init);
  1462. module_exit(ucma_cleanup);