ucm.c 34 KB

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