ucm.c 33 KB

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