ucm.c 33 KB

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