ucm.c 32 KB

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