ucm.c 34 KB

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