ucm.c 33 KB

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