ucm.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237
  1. /*
  2. * Copyright (c) 2005 Topspin Communications. All rights reserved.
  3. * Copyright (c) 2005 Intel Corporation. All rights reserved.
  4. *
  5. * This software is available to you under a choice of one of two
  6. * licenses. You may choose to be licensed under the terms of the GNU
  7. * General Public License (GPL) Version 2, available from the file
  8. * COPYING in the main directory of this source tree, or the
  9. * OpenIB.org BSD license below:
  10. *
  11. * Redistribution and use in source and binary forms, with or
  12. * without modification, are permitted provided that the following
  13. * conditions are met:
  14. *
  15. * - Redistributions of source code must retain the above
  16. * copyright notice, this list of conditions and the following
  17. * disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials
  22. * provided with the distribution.
  23. *
  24. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  25. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  26. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  27. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  28. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  29. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  30. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  31. * SOFTWARE.
  32. *
  33. * $Id: ucm.c 2594 2005-06-13 19:46:02Z libor $
  34. */
  35. #include <linux/init.h>
  36. #include <linux/fs.h>
  37. #include <linux/module.h>
  38. #include <linux/device.h>
  39. #include <linux/err.h>
  40. #include <linux/poll.h>
  41. #include <linux/file.h>
  42. #include <linux/mount.h>
  43. #include <linux/cdev.h>
  44. #include <asm/uaccess.h>
  45. #include "ucm.h"
  46. MODULE_AUTHOR("Libor Michalek");
  47. MODULE_DESCRIPTION("InfiniBand userspace Connection Manager access");
  48. MODULE_LICENSE("Dual BSD/GPL");
  49. static int ucm_debug_level;
  50. module_param_named(debug_level, ucm_debug_level, int, 0644);
  51. MODULE_PARM_DESC(debug_level, "Enable debug tracing if > 0");
  52. enum {
  53. IB_UCM_MAJOR = 231,
  54. IB_UCM_MINOR = 255
  55. };
  56. #define IB_UCM_DEV MKDEV(IB_UCM_MAJOR, IB_UCM_MINOR)
  57. #define PFX "UCM: "
  58. #define ucm_dbg(format, arg...) \
  59. do { \
  60. if (ucm_debug_level > 0) \
  61. printk(KERN_DEBUG PFX format, ## arg); \
  62. } while (0)
  63. static struct semaphore ctx_id_mutex;
  64. static struct idr ctx_id_table;
  65. static int ctx_id_rover = 0;
  66. static struct ib_ucm_context *ib_ucm_ctx_get(struct ib_ucm_file *file, int id)
  67. {
  68. struct ib_ucm_context *ctx;
  69. down(&ctx_id_mutex);
  70. ctx = idr_find(&ctx_id_table, id);
  71. if (!ctx)
  72. ctx = ERR_PTR(-ENOENT);
  73. else if (ctx->file != file)
  74. ctx = ERR_PTR(-EINVAL);
  75. else
  76. atomic_inc(&ctx->ref);
  77. up(&ctx_id_mutex);
  78. return ctx;
  79. }
  80. static void ib_ucm_ctx_put(struct ib_ucm_context *ctx)
  81. {
  82. if (atomic_dec_and_test(&ctx->ref))
  83. wake_up(&ctx->wait);
  84. }
  85. static ssize_t ib_ucm_destroy_ctx(struct ib_ucm_file *file, int id)
  86. {
  87. struct ib_ucm_context *ctx;
  88. struct ib_ucm_event *uevent;
  89. down(&ctx_id_mutex);
  90. ctx = idr_find(&ctx_id_table, id);
  91. if (!ctx)
  92. ctx = ERR_PTR(-ENOENT);
  93. else if (ctx->file != file)
  94. ctx = ERR_PTR(-EINVAL);
  95. else
  96. idr_remove(&ctx_id_table, ctx->id);
  97. up(&ctx_id_mutex);
  98. if (IS_ERR(ctx))
  99. return PTR_ERR(ctx);
  100. atomic_dec(&ctx->ref);
  101. wait_event(ctx->wait, !atomic_read(&ctx->ref));
  102. /* No new events will be generated after destroying the cm_id. */
  103. if (!IS_ERR(ctx->cm_id))
  104. ib_destroy_cm_id(ctx->cm_id);
  105. /* Cleanup events not yet reported to the user. */
  106. down(&file->mutex);
  107. list_del(&ctx->file_list);
  108. while (!list_empty(&ctx->events)) {
  109. uevent = list_entry(ctx->events.next,
  110. struct ib_ucm_event, ctx_list);
  111. list_del(&uevent->file_list);
  112. list_del(&uevent->ctx_list);
  113. /* clear incoming connections. */
  114. if (uevent->cm_id)
  115. ib_destroy_cm_id(uevent->cm_id);
  116. kfree(uevent);
  117. }
  118. up(&file->mutex);
  119. kfree(ctx);
  120. return 0;
  121. }
  122. static struct ib_ucm_context *ib_ucm_ctx_alloc(struct ib_ucm_file *file)
  123. {
  124. struct ib_ucm_context *ctx;
  125. int result;
  126. ctx = kmalloc(sizeof(*ctx), GFP_KERNEL);
  127. if (!ctx)
  128. return NULL;
  129. atomic_set(&ctx->ref, 1);
  130. init_waitqueue_head(&ctx->wait);
  131. ctx->file = file;
  132. INIT_LIST_HEAD(&ctx->events);
  133. list_add_tail(&ctx->file_list, &file->ctxs);
  134. ctx_id_rover = (ctx_id_rover + 1) & INT_MAX;
  135. retry:
  136. result = idr_pre_get(&ctx_id_table, GFP_KERNEL);
  137. if (!result)
  138. goto error;
  139. down(&ctx_id_mutex);
  140. result = idr_get_new_above(&ctx_id_table, ctx, ctx_id_rover, &ctx->id);
  141. up(&ctx_id_mutex);
  142. if (result == -EAGAIN)
  143. goto retry;
  144. if (result)
  145. goto error;
  146. ucm_dbg("Allocated CM ID <%d>\n", ctx->id);
  147. return ctx;
  148. error:
  149. list_del(&ctx->file_list);
  150. kfree(ctx);
  151. return NULL;
  152. }
  153. /*
  154. * Event portion of the API, handle CM events
  155. * and allow event polling.
  156. */
  157. static void ib_ucm_event_path_get(struct ib_ucm_path_rec *upath,
  158. struct ib_sa_path_rec *kpath)
  159. {
  160. if (!kpath || !upath)
  161. return;
  162. memcpy(upath->dgid, kpath->dgid.raw, sizeof *upath->dgid);
  163. memcpy(upath->sgid, kpath->sgid.raw, sizeof *upath->sgid);
  164. upath->dlid = kpath->dlid;
  165. upath->slid = kpath->slid;
  166. upath->raw_traffic = kpath->raw_traffic;
  167. upath->flow_label = kpath->flow_label;
  168. upath->hop_limit = kpath->hop_limit;
  169. upath->traffic_class = kpath->traffic_class;
  170. upath->reversible = kpath->reversible;
  171. upath->numb_path = kpath->numb_path;
  172. upath->pkey = kpath->pkey;
  173. upath->sl = kpath->sl;
  174. upath->mtu_selector = kpath->mtu_selector;
  175. upath->mtu = kpath->mtu;
  176. upath->rate_selector = kpath->rate_selector;
  177. upath->rate = kpath->rate;
  178. upath->packet_life_time = kpath->packet_life_time;
  179. upath->preference = kpath->preference;
  180. upath->packet_life_time_selector =
  181. kpath->packet_life_time_selector;
  182. }
  183. static void ib_ucm_event_req_get(struct ib_ucm_context *ctx,
  184. struct ib_ucm_req_event_resp *ureq,
  185. struct ib_cm_req_event_param *kreq)
  186. {
  187. ureq->listen_id = ctx->id;
  188. ureq->remote_ca_guid = kreq->remote_ca_guid;
  189. ureq->remote_qkey = kreq->remote_qkey;
  190. ureq->remote_qpn = kreq->remote_qpn;
  191. ureq->qp_type = kreq->qp_type;
  192. ureq->starting_psn = kreq->starting_psn;
  193. ureq->responder_resources = kreq->responder_resources;
  194. ureq->initiator_depth = kreq->initiator_depth;
  195. ureq->local_cm_response_timeout = kreq->local_cm_response_timeout;
  196. ureq->flow_control = kreq->flow_control;
  197. ureq->remote_cm_response_timeout = kreq->remote_cm_response_timeout;
  198. ureq->retry_count = kreq->retry_count;
  199. ureq->rnr_retry_count = kreq->rnr_retry_count;
  200. ureq->srq = kreq->srq;
  201. ib_ucm_event_path_get(&ureq->primary_path, kreq->primary_path);
  202. ib_ucm_event_path_get(&ureq->alternate_path, kreq->alternate_path);
  203. }
  204. static void ib_ucm_event_rep_get(struct ib_ucm_rep_event_resp *urep,
  205. struct ib_cm_rep_event_param *krep)
  206. {
  207. urep->remote_ca_guid = krep->remote_ca_guid;
  208. urep->remote_qkey = krep->remote_qkey;
  209. urep->remote_qpn = krep->remote_qpn;
  210. urep->starting_psn = krep->starting_psn;
  211. urep->responder_resources = krep->responder_resources;
  212. urep->initiator_depth = krep->initiator_depth;
  213. urep->target_ack_delay = krep->target_ack_delay;
  214. urep->failover_accepted = krep->failover_accepted;
  215. urep->flow_control = krep->flow_control;
  216. urep->rnr_retry_count = krep->rnr_retry_count;
  217. urep->srq = krep->srq;
  218. }
  219. static void ib_ucm_event_sidr_req_get(struct ib_ucm_context *ctx,
  220. struct ib_ucm_sidr_req_event_resp *ureq,
  221. struct ib_cm_sidr_req_event_param *kreq)
  222. {
  223. ureq->listen_id = ctx->id;
  224. ureq->pkey = kreq->pkey;
  225. }
  226. static void ib_ucm_event_sidr_rep_get(struct ib_ucm_sidr_rep_event_resp *urep,
  227. struct ib_cm_sidr_rep_event_param *krep)
  228. {
  229. urep->status = krep->status;
  230. urep->qkey = krep->qkey;
  231. urep->qpn = krep->qpn;
  232. };
  233. static int ib_ucm_event_process(struct ib_ucm_context *ctx,
  234. struct ib_cm_event *evt,
  235. struct ib_ucm_event *uvt)
  236. {
  237. void *info = NULL;
  238. switch (evt->event) {
  239. case IB_CM_REQ_RECEIVED:
  240. ib_ucm_event_req_get(ctx, &uvt->resp.u.req_resp,
  241. &evt->param.req_rcvd);
  242. uvt->data_len = IB_CM_REQ_PRIVATE_DATA_SIZE;
  243. uvt->resp.present = IB_UCM_PRES_PRIMARY;
  244. uvt->resp.present |= (evt->param.req_rcvd.alternate_path ?
  245. IB_UCM_PRES_ALTERNATE : 0);
  246. break;
  247. case IB_CM_REP_RECEIVED:
  248. ib_ucm_event_rep_get(&uvt->resp.u.rep_resp,
  249. &evt->param.rep_rcvd);
  250. uvt->data_len = IB_CM_REP_PRIVATE_DATA_SIZE;
  251. break;
  252. case IB_CM_RTU_RECEIVED:
  253. uvt->data_len = IB_CM_RTU_PRIVATE_DATA_SIZE;
  254. uvt->resp.u.send_status = evt->param.send_status;
  255. break;
  256. case IB_CM_DREQ_RECEIVED:
  257. uvt->data_len = IB_CM_DREQ_PRIVATE_DATA_SIZE;
  258. uvt->resp.u.send_status = evt->param.send_status;
  259. break;
  260. case IB_CM_DREP_RECEIVED:
  261. uvt->data_len = IB_CM_DREP_PRIVATE_DATA_SIZE;
  262. uvt->resp.u.send_status = evt->param.send_status;
  263. break;
  264. case IB_CM_MRA_RECEIVED:
  265. uvt->resp.u.mra_resp.timeout =
  266. evt->param.mra_rcvd.service_timeout;
  267. uvt->data_len = IB_CM_MRA_PRIVATE_DATA_SIZE;
  268. break;
  269. case IB_CM_REJ_RECEIVED:
  270. uvt->resp.u.rej_resp.reason = evt->param.rej_rcvd.reason;
  271. uvt->data_len = IB_CM_REJ_PRIVATE_DATA_SIZE;
  272. uvt->info_len = evt->param.rej_rcvd.ari_length;
  273. info = evt->param.rej_rcvd.ari;
  274. break;
  275. case IB_CM_LAP_RECEIVED:
  276. ib_ucm_event_path_get(&uvt->resp.u.lap_resp.path,
  277. evt->param.lap_rcvd.alternate_path);
  278. uvt->data_len = IB_CM_LAP_PRIVATE_DATA_SIZE;
  279. uvt->resp.present = IB_UCM_PRES_ALTERNATE;
  280. break;
  281. case IB_CM_APR_RECEIVED:
  282. uvt->resp.u.apr_resp.status = evt->param.apr_rcvd.ap_status;
  283. uvt->data_len = IB_CM_APR_PRIVATE_DATA_SIZE;
  284. uvt->info_len = evt->param.apr_rcvd.info_len;
  285. info = evt->param.apr_rcvd.apr_info;
  286. break;
  287. case IB_CM_SIDR_REQ_RECEIVED:
  288. ib_ucm_event_sidr_req_get(ctx, &uvt->resp.u.sidr_req_resp,
  289. &evt->param.sidr_req_rcvd);
  290. uvt->data_len = IB_CM_SIDR_REQ_PRIVATE_DATA_SIZE;
  291. break;
  292. case IB_CM_SIDR_REP_RECEIVED:
  293. ib_ucm_event_sidr_rep_get(&uvt->resp.u.sidr_rep_resp,
  294. &evt->param.sidr_rep_rcvd);
  295. uvt->data_len = IB_CM_SIDR_REP_PRIVATE_DATA_SIZE;
  296. uvt->info_len = evt->param.sidr_rep_rcvd.info_len;
  297. info = evt->param.sidr_rep_rcvd.info;
  298. break;
  299. default:
  300. uvt->resp.u.send_status = evt->param.send_status;
  301. break;
  302. }
  303. if (uvt->data_len) {
  304. uvt->data = kmalloc(uvt->data_len, GFP_KERNEL);
  305. if (!uvt->data)
  306. goto err1;
  307. memcpy(uvt->data, evt->private_data, uvt->data_len);
  308. uvt->resp.present |= IB_UCM_PRES_DATA;
  309. }
  310. if (uvt->info_len) {
  311. uvt->info = kmalloc(uvt->info_len, GFP_KERNEL);
  312. if (!uvt->info)
  313. goto err2;
  314. memcpy(uvt->info, info, uvt->info_len);
  315. uvt->resp.present |= IB_UCM_PRES_INFO;
  316. }
  317. return 0;
  318. err2:
  319. kfree(uvt->data);
  320. err1:
  321. return -ENOMEM;
  322. }
  323. static int ib_ucm_event_handler(struct ib_cm_id *cm_id,
  324. struct ib_cm_event *event)
  325. {
  326. struct ib_ucm_event *uevent;
  327. struct ib_ucm_context *ctx;
  328. int result = 0;
  329. int id;
  330. ctx = cm_id->context;
  331. if (event->event == IB_CM_REQ_RECEIVED ||
  332. event->event == IB_CM_SIDR_REQ_RECEIVED)
  333. id = IB_UCM_CM_ID_INVALID;
  334. else
  335. id = ctx->id;
  336. uevent = kmalloc(sizeof(*uevent), GFP_KERNEL);
  337. if (!uevent)
  338. goto err1;
  339. memset(uevent, 0, sizeof(*uevent));
  340. uevent->resp.id = id;
  341. uevent->resp.event = event->event;
  342. result = ib_ucm_event_process(ctx, event, uevent);
  343. if (result)
  344. goto err2;
  345. uevent->ctx = ctx;
  346. uevent->cm_id = (id == IB_UCM_CM_ID_INVALID) ? cm_id : NULL;
  347. down(&ctx->file->mutex);
  348. list_add_tail(&uevent->file_list, &ctx->file->events);
  349. list_add_tail(&uevent->ctx_list, &ctx->events);
  350. wake_up_interruptible(&ctx->file->poll_wait);
  351. up(&ctx->file->mutex);
  352. return 0;
  353. err2:
  354. kfree(uevent);
  355. err1:
  356. /* Destroy new cm_id's */
  357. return (id == IB_UCM_CM_ID_INVALID);
  358. }
  359. static ssize_t ib_ucm_event(struct ib_ucm_file *file,
  360. const char __user *inbuf,
  361. int in_len, int out_len)
  362. {
  363. struct ib_ucm_context *ctx;
  364. struct ib_ucm_event_get cmd;
  365. struct ib_ucm_event *uevent = NULL;
  366. int result = 0;
  367. DEFINE_WAIT(wait);
  368. if (out_len < sizeof(struct ib_ucm_event_resp))
  369. return -ENOSPC;
  370. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  371. return -EFAULT;
  372. /*
  373. * wait
  374. */
  375. down(&file->mutex);
  376. while (list_empty(&file->events)) {
  377. if (file->filp->f_flags & O_NONBLOCK) {
  378. result = -EAGAIN;
  379. break;
  380. }
  381. if (signal_pending(current)) {
  382. result = -ERESTARTSYS;
  383. break;
  384. }
  385. prepare_to_wait(&file->poll_wait, &wait, TASK_INTERRUPTIBLE);
  386. up(&file->mutex);
  387. schedule();
  388. down(&file->mutex);
  389. finish_wait(&file->poll_wait, &wait);
  390. }
  391. if (result)
  392. goto done;
  393. uevent = list_entry(file->events.next, struct ib_ucm_event, file_list);
  394. if (!uevent->cm_id)
  395. goto user;
  396. ctx = ib_ucm_ctx_alloc(file);
  397. if (!ctx) {
  398. result = -ENOMEM;
  399. goto done;
  400. }
  401. ctx->cm_id = uevent->cm_id;
  402. ctx->cm_id->context = ctx;
  403. uevent->resp.id = ctx->id;
  404. user:
  405. if (copy_to_user((void __user *)(unsigned long)cmd.response,
  406. &uevent->resp, sizeof(uevent->resp))) {
  407. result = -EFAULT;
  408. goto done;
  409. }
  410. if (uevent->data) {
  411. if (cmd.data_len < uevent->data_len) {
  412. result = -ENOMEM;
  413. goto done;
  414. }
  415. if (copy_to_user((void __user *)(unsigned long)cmd.data,
  416. uevent->data, uevent->data_len)) {
  417. result = -EFAULT;
  418. goto done;
  419. }
  420. }
  421. if (uevent->info) {
  422. if (cmd.info_len < uevent->info_len) {
  423. result = -ENOMEM;
  424. goto done;
  425. }
  426. if (copy_to_user((void __user *)(unsigned long)cmd.info,
  427. uevent->info, uevent->info_len)) {
  428. result = -EFAULT;
  429. goto done;
  430. }
  431. }
  432. list_del(&uevent->file_list);
  433. list_del(&uevent->ctx_list);
  434. kfree(uevent->data);
  435. kfree(uevent->info);
  436. kfree(uevent);
  437. done:
  438. up(&file->mutex);
  439. return result;
  440. }
  441. static ssize_t ib_ucm_create_id(struct ib_ucm_file *file,
  442. const char __user *inbuf,
  443. int in_len, int out_len)
  444. {
  445. struct ib_ucm_create_id cmd;
  446. struct ib_ucm_create_id_resp resp;
  447. struct ib_ucm_context *ctx;
  448. int result;
  449. if (out_len < sizeof(resp))
  450. return -ENOSPC;
  451. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  452. return -EFAULT;
  453. down(&file->mutex);
  454. ctx = ib_ucm_ctx_alloc(file);
  455. up(&file->mutex);
  456. if (!ctx)
  457. return -ENOMEM;
  458. ctx->cm_id = ib_create_cm_id(ib_ucm_event_handler, ctx);
  459. if (IS_ERR(ctx->cm_id)) {
  460. result = PTR_ERR(ctx->cm_id);
  461. goto err;
  462. }
  463. resp.id = ctx->id;
  464. if (copy_to_user((void __user *)(unsigned long)cmd.response,
  465. &resp, sizeof(resp))) {
  466. result = -EFAULT;
  467. goto err;
  468. }
  469. return 0;
  470. err:
  471. ib_ucm_destroy_ctx(file, ctx->id);
  472. return result;
  473. }
  474. static ssize_t ib_ucm_destroy_id(struct ib_ucm_file *file,
  475. const char __user *inbuf,
  476. int in_len, int out_len)
  477. {
  478. struct ib_ucm_destroy_id cmd;
  479. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  480. return -EFAULT;
  481. return ib_ucm_destroy_ctx(file, cmd.id);
  482. }
  483. static ssize_t ib_ucm_attr_id(struct ib_ucm_file *file,
  484. const char __user *inbuf,
  485. int in_len, int out_len)
  486. {
  487. struct ib_ucm_attr_id_resp resp;
  488. struct ib_ucm_attr_id cmd;
  489. struct ib_ucm_context *ctx;
  490. int result = 0;
  491. if (out_len < sizeof(resp))
  492. return -ENOSPC;
  493. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  494. return -EFAULT;
  495. ctx = ib_ucm_ctx_get(file, cmd.id);
  496. if (IS_ERR(ctx))
  497. return PTR_ERR(ctx);
  498. resp.service_id = ctx->cm_id->service_id;
  499. resp.service_mask = ctx->cm_id->service_mask;
  500. resp.local_id = ctx->cm_id->local_id;
  501. resp.remote_id = ctx->cm_id->remote_id;
  502. if (copy_to_user((void __user *)(unsigned long)cmd.response,
  503. &resp, sizeof(resp)))
  504. result = -EFAULT;
  505. ib_ucm_ctx_put(ctx);
  506. return result;
  507. }
  508. static ssize_t ib_ucm_listen(struct ib_ucm_file *file,
  509. const char __user *inbuf,
  510. int in_len, int out_len)
  511. {
  512. struct ib_ucm_listen cmd;
  513. struct ib_ucm_context *ctx;
  514. int result;
  515. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  516. return -EFAULT;
  517. ctx = ib_ucm_ctx_get(file, cmd.id);
  518. if (IS_ERR(ctx))
  519. return PTR_ERR(ctx);
  520. result = ib_cm_listen(ctx->cm_id, cmd.service_id, cmd.service_mask);
  521. ib_ucm_ctx_put(ctx);
  522. return result;
  523. }
  524. static ssize_t ib_ucm_establish(struct ib_ucm_file *file,
  525. const char __user *inbuf,
  526. int in_len, int out_len)
  527. {
  528. struct ib_ucm_establish cmd;
  529. struct ib_ucm_context *ctx;
  530. int result;
  531. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  532. return -EFAULT;
  533. ctx = ib_ucm_ctx_get(file, cmd.id);
  534. if (IS_ERR(ctx))
  535. return PTR_ERR(ctx);
  536. result = ib_cm_establish(ctx->cm_id);
  537. ib_ucm_ctx_put(ctx);
  538. return result;
  539. }
  540. static int ib_ucm_alloc_data(const void **dest, u64 src, u32 len)
  541. {
  542. void *data;
  543. *dest = NULL;
  544. if (!len)
  545. return 0;
  546. data = kmalloc(len, GFP_KERNEL);
  547. if (!data)
  548. return -ENOMEM;
  549. if (copy_from_user(data, (void __user *)(unsigned long)src, len)) {
  550. kfree(data);
  551. return -EFAULT;
  552. }
  553. *dest = data;
  554. return 0;
  555. }
  556. static int ib_ucm_path_get(struct ib_sa_path_rec **path, u64 src)
  557. {
  558. struct ib_ucm_path_rec ucm_path;
  559. struct ib_sa_path_rec *sa_path;
  560. *path = NULL;
  561. if (!src)
  562. return 0;
  563. sa_path = kmalloc(sizeof(*sa_path), GFP_KERNEL);
  564. if (!sa_path)
  565. return -ENOMEM;
  566. if (copy_from_user(&ucm_path, (void __user *)(unsigned long)src,
  567. sizeof(ucm_path))) {
  568. kfree(sa_path);
  569. return -EFAULT;
  570. }
  571. memcpy(sa_path->dgid.raw, ucm_path.dgid, sizeof sa_path->dgid);
  572. memcpy(sa_path->sgid.raw, ucm_path.sgid, sizeof sa_path->sgid);
  573. sa_path->dlid = ucm_path.dlid;
  574. sa_path->slid = ucm_path.slid;
  575. sa_path->raw_traffic = ucm_path.raw_traffic;
  576. sa_path->flow_label = ucm_path.flow_label;
  577. sa_path->hop_limit = ucm_path.hop_limit;
  578. sa_path->traffic_class = ucm_path.traffic_class;
  579. sa_path->reversible = ucm_path.reversible;
  580. sa_path->numb_path = ucm_path.numb_path;
  581. sa_path->pkey = ucm_path.pkey;
  582. sa_path->sl = ucm_path.sl;
  583. sa_path->mtu_selector = ucm_path.mtu_selector;
  584. sa_path->mtu = ucm_path.mtu;
  585. sa_path->rate_selector = ucm_path.rate_selector;
  586. sa_path->rate = ucm_path.rate;
  587. sa_path->packet_life_time = ucm_path.packet_life_time;
  588. sa_path->preference = ucm_path.preference;
  589. sa_path->packet_life_time_selector =
  590. ucm_path.packet_life_time_selector;
  591. *path = sa_path;
  592. return 0;
  593. }
  594. static ssize_t ib_ucm_send_req(struct ib_ucm_file *file,
  595. const char __user *inbuf,
  596. int in_len, int out_len)
  597. {
  598. struct ib_cm_req_param param;
  599. struct ib_ucm_context *ctx;
  600. struct ib_ucm_req cmd;
  601. int result;
  602. param.private_data = NULL;
  603. param.primary_path = NULL;
  604. param.alternate_path = NULL;
  605. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  606. return -EFAULT;
  607. result = ib_ucm_alloc_data(&param.private_data, cmd.data, cmd.len);
  608. if (result)
  609. goto done;
  610. result = ib_ucm_path_get(&param.primary_path, cmd.primary_path);
  611. if (result)
  612. goto done;
  613. result = ib_ucm_path_get(&param.alternate_path, cmd.alternate_path);
  614. if (result)
  615. goto done;
  616. param.private_data_len = cmd.len;
  617. param.service_id = cmd.sid;
  618. param.qp_num = cmd.qpn;
  619. param.qp_type = cmd.qp_type;
  620. param.starting_psn = cmd.psn;
  621. param.peer_to_peer = cmd.peer_to_peer;
  622. param.responder_resources = cmd.responder_resources;
  623. param.initiator_depth = cmd.initiator_depth;
  624. param.remote_cm_response_timeout = cmd.remote_cm_response_timeout;
  625. param.flow_control = cmd.flow_control;
  626. param.local_cm_response_timeout = cmd.local_cm_response_timeout;
  627. param.retry_count = cmd.retry_count;
  628. param.rnr_retry_count = cmd.rnr_retry_count;
  629. param.max_cm_retries = cmd.max_cm_retries;
  630. param.srq = cmd.srq;
  631. ctx = ib_ucm_ctx_get(file, cmd.id);
  632. if (!IS_ERR(ctx)) {
  633. result = ib_send_cm_req(ctx->cm_id, &param);
  634. ib_ucm_ctx_put(ctx);
  635. } else
  636. result = PTR_ERR(ctx);
  637. done:
  638. kfree(param.private_data);
  639. kfree(param.primary_path);
  640. kfree(param.alternate_path);
  641. return result;
  642. }
  643. static ssize_t ib_ucm_send_rep(struct ib_ucm_file *file,
  644. const char __user *inbuf,
  645. int in_len, int out_len)
  646. {
  647. struct ib_cm_rep_param param;
  648. struct ib_ucm_context *ctx;
  649. struct ib_ucm_rep cmd;
  650. int result;
  651. param.private_data = NULL;
  652. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  653. return -EFAULT;
  654. result = ib_ucm_alloc_data(&param.private_data, cmd.data, cmd.len);
  655. if (result)
  656. return result;
  657. param.qp_num = cmd.qpn;
  658. param.starting_psn = cmd.psn;
  659. param.private_data_len = cmd.len;
  660. param.responder_resources = cmd.responder_resources;
  661. param.initiator_depth = cmd.initiator_depth;
  662. param.target_ack_delay = cmd.target_ack_delay;
  663. param.failover_accepted = cmd.failover_accepted;
  664. param.flow_control = cmd.flow_control;
  665. param.rnr_retry_count = cmd.rnr_retry_count;
  666. param.srq = cmd.srq;
  667. ctx = ib_ucm_ctx_get(file, cmd.id);
  668. if (!IS_ERR(ctx)) {
  669. result = ib_send_cm_rep(ctx->cm_id, &param);
  670. ib_ucm_ctx_put(ctx);
  671. } else
  672. result = PTR_ERR(ctx);
  673. kfree(param.private_data);
  674. return result;
  675. }
  676. static ssize_t ib_ucm_send_private_data(struct ib_ucm_file *file,
  677. const char __user *inbuf, int in_len,
  678. int (*func)(struct ib_cm_id *cm_id,
  679. const void *private_data,
  680. u8 private_data_len))
  681. {
  682. struct ib_ucm_private_data cmd;
  683. struct ib_ucm_context *ctx;
  684. const void *private_data = NULL;
  685. int result;
  686. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  687. return -EFAULT;
  688. result = ib_ucm_alloc_data(&private_data, cmd.data, cmd.len);
  689. if (result)
  690. return result;
  691. ctx = ib_ucm_ctx_get(file, cmd.id);
  692. if (!IS_ERR(ctx)) {
  693. result = func(ctx->cm_id, private_data, cmd.len);
  694. ib_ucm_ctx_put(ctx);
  695. } else
  696. result = PTR_ERR(ctx);
  697. kfree(private_data);
  698. return result;
  699. }
  700. static ssize_t ib_ucm_send_rtu(struct ib_ucm_file *file,
  701. const char __user *inbuf,
  702. int in_len, int out_len)
  703. {
  704. return ib_ucm_send_private_data(file, inbuf, in_len, ib_send_cm_rtu);
  705. }
  706. static ssize_t ib_ucm_send_dreq(struct ib_ucm_file *file,
  707. const char __user *inbuf,
  708. int in_len, int out_len)
  709. {
  710. return ib_ucm_send_private_data(file, inbuf, in_len, ib_send_cm_dreq);
  711. }
  712. static ssize_t ib_ucm_send_drep(struct ib_ucm_file *file,
  713. const char __user *inbuf,
  714. int in_len, int out_len)
  715. {
  716. return ib_ucm_send_private_data(file, inbuf, in_len, ib_send_cm_drep);
  717. }
  718. static ssize_t ib_ucm_send_info(struct ib_ucm_file *file,
  719. const char __user *inbuf, int in_len,
  720. int (*func)(struct ib_cm_id *cm_id,
  721. int status,
  722. const void *info,
  723. u8 info_len,
  724. const void *data,
  725. u8 data_len))
  726. {
  727. struct ib_ucm_context *ctx;
  728. struct ib_ucm_info cmd;
  729. const void *data = NULL;
  730. const void *info = NULL;
  731. int result;
  732. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  733. return -EFAULT;
  734. result = ib_ucm_alloc_data(&data, cmd.data, cmd.data_len);
  735. if (result)
  736. goto done;
  737. result = ib_ucm_alloc_data(&info, cmd.info, cmd.info_len);
  738. if (result)
  739. goto done;
  740. ctx = ib_ucm_ctx_get(file, cmd.id);
  741. if (!IS_ERR(ctx)) {
  742. result = func(ctx->cm_id, cmd.status, info, cmd.info_len,
  743. data, cmd.data_len);
  744. ib_ucm_ctx_put(ctx);
  745. } else
  746. result = PTR_ERR(ctx);
  747. done:
  748. kfree(data);
  749. kfree(info);
  750. return result;
  751. }
  752. static ssize_t ib_ucm_send_rej(struct ib_ucm_file *file,
  753. const char __user *inbuf,
  754. int in_len, int out_len)
  755. {
  756. return ib_ucm_send_info(file, inbuf, in_len, (void *)ib_send_cm_rej);
  757. }
  758. static ssize_t ib_ucm_send_apr(struct ib_ucm_file *file,
  759. const char __user *inbuf,
  760. int in_len, int out_len)
  761. {
  762. return ib_ucm_send_info(file, inbuf, in_len, (void *)ib_send_cm_apr);
  763. }
  764. static ssize_t ib_ucm_send_mra(struct ib_ucm_file *file,
  765. const char __user *inbuf,
  766. int in_len, int out_len)
  767. {
  768. struct ib_ucm_context *ctx;
  769. struct ib_ucm_mra cmd;
  770. const void *data = NULL;
  771. int result;
  772. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  773. return -EFAULT;
  774. result = ib_ucm_alloc_data(&data, cmd.data, cmd.len);
  775. if (result)
  776. return result;
  777. ctx = ib_ucm_ctx_get(file, cmd.id);
  778. if (!IS_ERR(ctx)) {
  779. result = ib_send_cm_mra(ctx->cm_id, cmd.timeout, data, cmd.len);
  780. ib_ucm_ctx_put(ctx);
  781. } else
  782. result = PTR_ERR(ctx);
  783. kfree(data);
  784. return result;
  785. }
  786. static ssize_t ib_ucm_send_lap(struct ib_ucm_file *file,
  787. const char __user *inbuf,
  788. int in_len, int out_len)
  789. {
  790. struct ib_ucm_context *ctx;
  791. struct ib_sa_path_rec *path = NULL;
  792. struct ib_ucm_lap cmd;
  793. const void *data = NULL;
  794. int result;
  795. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  796. return -EFAULT;
  797. result = ib_ucm_alloc_data(&data, cmd.data, cmd.len);
  798. if (result)
  799. goto done;
  800. result = ib_ucm_path_get(&path, cmd.path);
  801. if (result)
  802. goto done;
  803. ctx = ib_ucm_ctx_get(file, cmd.id);
  804. if (!IS_ERR(ctx)) {
  805. result = ib_send_cm_lap(ctx->cm_id, path, data, cmd.len);
  806. ib_ucm_ctx_put(ctx);
  807. } else
  808. result = PTR_ERR(ctx);
  809. done:
  810. kfree(data);
  811. kfree(path);
  812. return result;
  813. }
  814. static ssize_t ib_ucm_send_sidr_req(struct ib_ucm_file *file,
  815. const char __user *inbuf,
  816. int in_len, int out_len)
  817. {
  818. struct ib_cm_sidr_req_param param;
  819. struct ib_ucm_context *ctx;
  820. struct ib_ucm_sidr_req cmd;
  821. int result;
  822. param.private_data = NULL;
  823. param.path = NULL;
  824. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  825. return -EFAULT;
  826. result = ib_ucm_alloc_data(&param.private_data, cmd.data, cmd.len);
  827. if (result)
  828. goto done;
  829. result = ib_ucm_path_get(&param.path, cmd.path);
  830. if (result)
  831. goto done;
  832. param.private_data_len = cmd.len;
  833. param.service_id = cmd.sid;
  834. param.timeout_ms = cmd.timeout;
  835. param.max_cm_retries = cmd.max_cm_retries;
  836. param.pkey = cmd.pkey;
  837. ctx = ib_ucm_ctx_get(file, cmd.id);
  838. if (!IS_ERR(ctx)) {
  839. result = ib_send_cm_sidr_req(ctx->cm_id, &param);
  840. ib_ucm_ctx_put(ctx);
  841. } else
  842. result = PTR_ERR(ctx);
  843. done:
  844. kfree(param.private_data);
  845. kfree(param.path);
  846. return result;
  847. }
  848. static ssize_t ib_ucm_send_sidr_rep(struct ib_ucm_file *file,
  849. const char __user *inbuf,
  850. int in_len, int out_len)
  851. {
  852. struct ib_cm_sidr_rep_param param;
  853. struct ib_ucm_sidr_rep cmd;
  854. struct ib_ucm_context *ctx;
  855. int result;
  856. param.info = NULL;
  857. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  858. return -EFAULT;
  859. result = ib_ucm_alloc_data(&param.private_data,
  860. cmd.data, cmd.data_len);
  861. if (result)
  862. goto done;
  863. result = ib_ucm_alloc_data(&param.info, cmd.info, cmd.info_len);
  864. if (result)
  865. goto done;
  866. param.qp_num = cmd.qpn;
  867. param.qkey = cmd.qkey;
  868. param.status = cmd.status;
  869. param.info_length = cmd.info_len;
  870. param.private_data_len = cmd.data_len;
  871. ctx = ib_ucm_ctx_get(file, cmd.id);
  872. if (!IS_ERR(ctx)) {
  873. result = ib_send_cm_sidr_rep(ctx->cm_id, &param);
  874. ib_ucm_ctx_put(ctx);
  875. } else
  876. result = PTR_ERR(ctx);
  877. done:
  878. kfree(param.private_data);
  879. kfree(param.info);
  880. return result;
  881. }
  882. static ssize_t (*ucm_cmd_table[])(struct ib_ucm_file *file,
  883. const char __user *inbuf,
  884. int in_len, int out_len) = {
  885. [IB_USER_CM_CMD_CREATE_ID] = ib_ucm_create_id,
  886. [IB_USER_CM_CMD_DESTROY_ID] = ib_ucm_destroy_id,
  887. [IB_USER_CM_CMD_ATTR_ID] = ib_ucm_attr_id,
  888. [IB_USER_CM_CMD_LISTEN] = ib_ucm_listen,
  889. [IB_USER_CM_CMD_ESTABLISH] = ib_ucm_establish,
  890. [IB_USER_CM_CMD_SEND_REQ] = ib_ucm_send_req,
  891. [IB_USER_CM_CMD_SEND_REP] = ib_ucm_send_rep,
  892. [IB_USER_CM_CMD_SEND_RTU] = ib_ucm_send_rtu,
  893. [IB_USER_CM_CMD_SEND_DREQ] = ib_ucm_send_dreq,
  894. [IB_USER_CM_CMD_SEND_DREP] = ib_ucm_send_drep,
  895. [IB_USER_CM_CMD_SEND_REJ] = ib_ucm_send_rej,
  896. [IB_USER_CM_CMD_SEND_MRA] = ib_ucm_send_mra,
  897. [IB_USER_CM_CMD_SEND_LAP] = ib_ucm_send_lap,
  898. [IB_USER_CM_CMD_SEND_APR] = ib_ucm_send_apr,
  899. [IB_USER_CM_CMD_SEND_SIDR_REQ] = ib_ucm_send_sidr_req,
  900. [IB_USER_CM_CMD_SEND_SIDR_REP] = ib_ucm_send_sidr_rep,
  901. [IB_USER_CM_CMD_EVENT] = ib_ucm_event,
  902. };
  903. static ssize_t ib_ucm_write(struct file *filp, const char __user *buf,
  904. size_t len, loff_t *pos)
  905. {
  906. struct ib_ucm_file *file = filp->private_data;
  907. struct ib_ucm_cmd_hdr hdr;
  908. ssize_t result;
  909. if (len < sizeof(hdr))
  910. return -EINVAL;
  911. if (copy_from_user(&hdr, buf, sizeof(hdr)))
  912. return -EFAULT;
  913. ucm_dbg("Write. cmd <%d> in <%d> out <%d> len <%Zu>\n",
  914. hdr.cmd, hdr.in, hdr.out, len);
  915. if (hdr.cmd < 0 || hdr.cmd >= ARRAY_SIZE(ucm_cmd_table))
  916. return -EINVAL;
  917. if (hdr.in + sizeof(hdr) > len)
  918. return -EINVAL;
  919. result = ucm_cmd_table[hdr.cmd](file, buf + sizeof(hdr),
  920. hdr.in, hdr.out);
  921. if (!result)
  922. result = len;
  923. return result;
  924. }
  925. static unsigned int ib_ucm_poll(struct file *filp,
  926. struct poll_table_struct *wait)
  927. {
  928. struct ib_ucm_file *file = filp->private_data;
  929. unsigned int mask = 0;
  930. poll_wait(filp, &file->poll_wait, wait);
  931. if (!list_empty(&file->events))
  932. mask = POLLIN | POLLRDNORM;
  933. return mask;
  934. }
  935. static int ib_ucm_open(struct inode *inode, struct file *filp)
  936. {
  937. struct ib_ucm_file *file;
  938. file = kmalloc(sizeof(*file), GFP_KERNEL);
  939. if (!file)
  940. return -ENOMEM;
  941. INIT_LIST_HEAD(&file->events);
  942. INIT_LIST_HEAD(&file->ctxs);
  943. init_waitqueue_head(&file->poll_wait);
  944. init_MUTEX(&file->mutex);
  945. filp->private_data = file;
  946. file->filp = filp;
  947. ucm_dbg("Created struct\n");
  948. return 0;
  949. }
  950. static int ib_ucm_close(struct inode *inode, struct file *filp)
  951. {
  952. struct ib_ucm_file *file = filp->private_data;
  953. struct ib_ucm_context *ctx;
  954. down(&file->mutex);
  955. while (!list_empty(&file->ctxs)) {
  956. ctx = list_entry(file->ctxs.next,
  957. struct ib_ucm_context, file_list);
  958. up(&file->mutex);
  959. ib_ucm_destroy_ctx(file, ctx->id);
  960. down(&file->mutex);
  961. }
  962. up(&file->mutex);
  963. kfree(file);
  964. return 0;
  965. }
  966. static struct file_operations ib_ucm_fops = {
  967. .owner = THIS_MODULE,
  968. .open = ib_ucm_open,
  969. .release = ib_ucm_close,
  970. .write = ib_ucm_write,
  971. .poll = ib_ucm_poll,
  972. };
  973. static struct class *ib_ucm_class;
  974. static struct cdev ib_ucm_cdev;
  975. static int __init ib_ucm_init(void)
  976. {
  977. int result;
  978. result = register_chrdev_region(IB_UCM_DEV, 1, "infiniband_cm");
  979. if (result) {
  980. ucm_dbg("Error <%d> registering dev\n", result);
  981. goto err_chr;
  982. }
  983. cdev_init(&ib_ucm_cdev, &ib_ucm_fops);
  984. result = cdev_add(&ib_ucm_cdev, IB_UCM_DEV, 1);
  985. if (result) {
  986. ucm_dbg("Error <%d> adding cdev\n", result);
  987. goto err_cdev;
  988. }
  989. ib_ucm_class = class_create(THIS_MODULE, "infiniband_cm");
  990. if (IS_ERR(ib_ucm_class)) {
  991. result = PTR_ERR(ib_ucm_class);
  992. ucm_dbg("Error <%d> creating class\n", result);
  993. goto err_class;
  994. }
  995. class_device_create(ib_ucm_class, IB_UCM_DEV, NULL, "ucm");
  996. idr_init(&ctx_id_table);
  997. init_MUTEX(&ctx_id_mutex);
  998. return 0;
  999. err_class:
  1000. cdev_del(&ib_ucm_cdev);
  1001. err_cdev:
  1002. unregister_chrdev_region(IB_UCM_DEV, 1);
  1003. err_chr:
  1004. return result;
  1005. }
  1006. static void __exit ib_ucm_cleanup(void)
  1007. {
  1008. class_device_destroy(ib_ucm_class, IB_UCM_DEV);
  1009. class_destroy(ib_ucm_class);
  1010. cdev_del(&ib_ucm_cdev);
  1011. unregister_chrdev_region(IB_UCM_DEV, 1);
  1012. }
  1013. module_init(ib_ucm_init);
  1014. module_exit(ib_ucm_cleanup);