ucm.c 33 KB

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