ucm.c 33 KB

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