ucm.c 33 KB

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