ucm.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446
  1. /*
  2. * Copyright (c) 2005 Topspin Communications. All rights reserved.
  3. * Copyright (c) 2005 Intel Corporation. All rights reserved.
  4. *
  5. * This software is available to you under a choice of one of two
  6. * licenses. You may choose to be licensed under the terms of the GNU
  7. * General Public License (GPL) Version 2, available from the file
  8. * COPYING in the main directory of this source tree, or the
  9. * OpenIB.org BSD license below:
  10. *
  11. * Redistribution and use in source and binary forms, with or
  12. * without modification, are permitted provided that the following
  13. * conditions are met:
  14. *
  15. * - Redistributions of source code must retain the above
  16. * copyright notice, this list of conditions and the following
  17. * disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials
  22. * provided with the distribution.
  23. *
  24. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  25. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  26. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  27. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  28. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  29. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  30. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  31. * SOFTWARE.
  32. *
  33. * $Id: ucm.c 2594 2005-06-13 19:46:02Z libor $
  34. */
  35. #include <linux/completion.h>
  36. #include <linux/init.h>
  37. #include <linux/fs.h>
  38. #include <linux/module.h>
  39. #include <linux/device.h>
  40. #include <linux/err.h>
  41. #include <linux/poll.h>
  42. #include <linux/file.h>
  43. #include <linux/mount.h>
  44. #include <linux/cdev.h>
  45. #include <linux/idr.h>
  46. #include <linux/mutex.h>
  47. #include <asm/uaccess.h>
  48. #include <rdma/ib_cm.h>
  49. #include <rdma/ib_user_cm.h>
  50. MODULE_AUTHOR("Libor Michalek");
  51. MODULE_DESCRIPTION("InfiniBand userspace Connection Manager access");
  52. MODULE_LICENSE("Dual BSD/GPL");
  53. struct ib_ucm_device {
  54. int devnum;
  55. struct cdev dev;
  56. struct class_device class_dev;
  57. struct ib_device *ib_dev;
  58. };
  59. struct ib_ucm_file {
  60. struct semaphore mutex;
  61. struct file *filp;
  62. struct ib_ucm_device *device;
  63. struct list_head ctxs;
  64. struct list_head events;
  65. wait_queue_head_t poll_wait;
  66. };
  67. struct ib_ucm_context {
  68. int id;
  69. struct completion comp;
  70. atomic_t ref;
  71. int events_reported;
  72. struct ib_ucm_file *file;
  73. struct ib_cm_id *cm_id;
  74. __u64 uid;
  75. struct list_head events; /* list of pending events. */
  76. struct list_head file_list; /* member in file ctx list */
  77. };
  78. struct ib_ucm_event {
  79. struct ib_ucm_context *ctx;
  80. struct list_head file_list; /* member in file event list */
  81. struct list_head ctx_list; /* member in ctx event list */
  82. struct ib_cm_id *cm_id;
  83. struct ib_ucm_event_resp resp;
  84. void *data;
  85. void *info;
  86. int data_len;
  87. int info_len;
  88. };
  89. enum {
  90. IB_UCM_MAJOR = 231,
  91. IB_UCM_BASE_MINOR = 224,
  92. IB_UCM_MAX_DEVICES = 32
  93. };
  94. #define IB_UCM_BASE_DEV MKDEV(IB_UCM_MAJOR, IB_UCM_BASE_MINOR)
  95. static void ib_ucm_add_one(struct ib_device *device);
  96. static void ib_ucm_remove_one(struct ib_device *device);
  97. static struct ib_client ucm_client = {
  98. .name = "ucm",
  99. .add = ib_ucm_add_one,
  100. .remove = ib_ucm_remove_one
  101. };
  102. static DEFINE_MUTEX(ctx_id_mutex);
  103. static DEFINE_IDR(ctx_id_table);
  104. static DECLARE_BITMAP(dev_map, IB_UCM_MAX_DEVICES);
  105. static struct ib_ucm_context *ib_ucm_ctx_get(struct ib_ucm_file *file, int id)
  106. {
  107. struct ib_ucm_context *ctx;
  108. mutex_lock(&ctx_id_mutex);
  109. ctx = idr_find(&ctx_id_table, id);
  110. if (!ctx)
  111. ctx = ERR_PTR(-ENOENT);
  112. else if (ctx->file != file)
  113. ctx = ERR_PTR(-EINVAL);
  114. else
  115. atomic_inc(&ctx->ref);
  116. mutex_unlock(&ctx_id_mutex);
  117. return ctx;
  118. }
  119. static void ib_ucm_ctx_put(struct ib_ucm_context *ctx)
  120. {
  121. if (atomic_dec_and_test(&ctx->ref))
  122. complete(&ctx->comp);
  123. }
  124. static inline int ib_ucm_new_cm_id(int event)
  125. {
  126. return event == IB_CM_REQ_RECEIVED || event == IB_CM_SIDR_REQ_RECEIVED;
  127. }
  128. static void ib_ucm_cleanup_events(struct ib_ucm_context *ctx)
  129. {
  130. struct ib_ucm_event *uevent;
  131. down(&ctx->file->mutex);
  132. list_del(&ctx->file_list);
  133. while (!list_empty(&ctx->events)) {
  134. uevent = list_entry(ctx->events.next,
  135. struct ib_ucm_event, ctx_list);
  136. list_del(&uevent->file_list);
  137. list_del(&uevent->ctx_list);
  138. /* clear incoming connections. */
  139. if (ib_ucm_new_cm_id(uevent->resp.event))
  140. ib_destroy_cm_id(uevent->cm_id);
  141. kfree(uevent);
  142. }
  143. up(&ctx->file->mutex);
  144. }
  145. static struct ib_ucm_context *ib_ucm_ctx_alloc(struct ib_ucm_file *file)
  146. {
  147. struct ib_ucm_context *ctx;
  148. int result;
  149. ctx = kzalloc(sizeof *ctx, GFP_KERNEL);
  150. if (!ctx)
  151. return NULL;
  152. atomic_set(&ctx->ref, 1);
  153. init_completion(&ctx->comp);
  154. ctx->file = file;
  155. INIT_LIST_HEAD(&ctx->events);
  156. do {
  157. result = idr_pre_get(&ctx_id_table, GFP_KERNEL);
  158. if (!result)
  159. goto error;
  160. mutex_lock(&ctx_id_mutex);
  161. result = idr_get_new(&ctx_id_table, ctx, &ctx->id);
  162. mutex_unlock(&ctx_id_mutex);
  163. } while (result == -EAGAIN);
  164. if (result)
  165. goto error;
  166. list_add_tail(&ctx->file_list, &file->ctxs);
  167. return ctx;
  168. error:
  169. kfree(ctx);
  170. return NULL;
  171. }
  172. static void ib_ucm_event_path_get(struct ib_ucm_path_rec *upath,
  173. struct ib_sa_path_rec *kpath)
  174. {
  175. if (!kpath || !upath)
  176. return;
  177. memcpy(upath->dgid, kpath->dgid.raw, sizeof *upath->dgid);
  178. memcpy(upath->sgid, kpath->sgid.raw, sizeof *upath->sgid);
  179. upath->dlid = kpath->dlid;
  180. upath->slid = kpath->slid;
  181. upath->raw_traffic = kpath->raw_traffic;
  182. upath->flow_label = kpath->flow_label;
  183. upath->hop_limit = kpath->hop_limit;
  184. upath->traffic_class = kpath->traffic_class;
  185. upath->reversible = kpath->reversible;
  186. upath->numb_path = kpath->numb_path;
  187. upath->pkey = kpath->pkey;
  188. upath->sl = kpath->sl;
  189. upath->mtu_selector = kpath->mtu_selector;
  190. upath->mtu = kpath->mtu;
  191. upath->rate_selector = kpath->rate_selector;
  192. upath->rate = kpath->rate;
  193. upath->packet_life_time = kpath->packet_life_time;
  194. upath->preference = kpath->preference;
  195. upath->packet_life_time_selector =
  196. kpath->packet_life_time_selector;
  197. }
  198. static void ib_ucm_event_req_get(struct ib_ucm_req_event_resp *ureq,
  199. struct ib_cm_req_event_param *kreq)
  200. {
  201. ureq->remote_ca_guid = kreq->remote_ca_guid;
  202. ureq->remote_qkey = kreq->remote_qkey;
  203. ureq->remote_qpn = kreq->remote_qpn;
  204. ureq->qp_type = kreq->qp_type;
  205. ureq->starting_psn = kreq->starting_psn;
  206. ureq->responder_resources = kreq->responder_resources;
  207. ureq->initiator_depth = kreq->initiator_depth;
  208. ureq->local_cm_response_timeout = kreq->local_cm_response_timeout;
  209. ureq->flow_control = kreq->flow_control;
  210. ureq->remote_cm_response_timeout = kreq->remote_cm_response_timeout;
  211. ureq->retry_count = kreq->retry_count;
  212. ureq->rnr_retry_count = kreq->rnr_retry_count;
  213. ureq->srq = kreq->srq;
  214. ureq->port = kreq->port;
  215. ib_ucm_event_path_get(&ureq->primary_path, kreq->primary_path);
  216. ib_ucm_event_path_get(&ureq->alternate_path, kreq->alternate_path);
  217. }
  218. static void ib_ucm_event_rep_get(struct ib_ucm_rep_event_resp *urep,
  219. struct ib_cm_rep_event_param *krep)
  220. {
  221. urep->remote_ca_guid = krep->remote_ca_guid;
  222. urep->remote_qkey = krep->remote_qkey;
  223. urep->remote_qpn = krep->remote_qpn;
  224. urep->starting_psn = krep->starting_psn;
  225. urep->responder_resources = krep->responder_resources;
  226. urep->initiator_depth = krep->initiator_depth;
  227. urep->target_ack_delay = krep->target_ack_delay;
  228. urep->failover_accepted = krep->failover_accepted;
  229. urep->flow_control = krep->flow_control;
  230. urep->rnr_retry_count = krep->rnr_retry_count;
  231. urep->srq = krep->srq;
  232. }
  233. static void ib_ucm_event_sidr_rep_get(struct ib_ucm_sidr_rep_event_resp *urep,
  234. struct ib_cm_sidr_rep_event_param *krep)
  235. {
  236. urep->status = krep->status;
  237. urep->qkey = krep->qkey;
  238. urep->qpn = krep->qpn;
  239. };
  240. static int ib_ucm_event_process(struct ib_cm_event *evt,
  241. struct ib_ucm_event *uvt)
  242. {
  243. void *info = NULL;
  244. switch (evt->event) {
  245. case IB_CM_REQ_RECEIVED:
  246. ib_ucm_event_req_get(&uvt->resp.u.req_resp,
  247. &evt->param.req_rcvd);
  248. uvt->data_len = IB_CM_REQ_PRIVATE_DATA_SIZE;
  249. uvt->resp.present = IB_UCM_PRES_PRIMARY;
  250. uvt->resp.present |= (evt->param.req_rcvd.alternate_path ?
  251. IB_UCM_PRES_ALTERNATE : 0);
  252. break;
  253. case IB_CM_REP_RECEIVED:
  254. ib_ucm_event_rep_get(&uvt->resp.u.rep_resp,
  255. &evt->param.rep_rcvd);
  256. uvt->data_len = IB_CM_REP_PRIVATE_DATA_SIZE;
  257. break;
  258. case IB_CM_RTU_RECEIVED:
  259. uvt->data_len = IB_CM_RTU_PRIVATE_DATA_SIZE;
  260. uvt->resp.u.send_status = evt->param.send_status;
  261. break;
  262. case IB_CM_DREQ_RECEIVED:
  263. uvt->data_len = IB_CM_DREQ_PRIVATE_DATA_SIZE;
  264. uvt->resp.u.send_status = evt->param.send_status;
  265. break;
  266. case IB_CM_DREP_RECEIVED:
  267. uvt->data_len = IB_CM_DREP_PRIVATE_DATA_SIZE;
  268. uvt->resp.u.send_status = evt->param.send_status;
  269. break;
  270. case IB_CM_MRA_RECEIVED:
  271. uvt->resp.u.mra_resp.timeout =
  272. evt->param.mra_rcvd.service_timeout;
  273. uvt->data_len = IB_CM_MRA_PRIVATE_DATA_SIZE;
  274. break;
  275. case IB_CM_REJ_RECEIVED:
  276. uvt->resp.u.rej_resp.reason = evt->param.rej_rcvd.reason;
  277. uvt->data_len = IB_CM_REJ_PRIVATE_DATA_SIZE;
  278. uvt->info_len = evt->param.rej_rcvd.ari_length;
  279. info = evt->param.rej_rcvd.ari;
  280. break;
  281. case IB_CM_LAP_RECEIVED:
  282. ib_ucm_event_path_get(&uvt->resp.u.lap_resp.path,
  283. evt->param.lap_rcvd.alternate_path);
  284. uvt->data_len = IB_CM_LAP_PRIVATE_DATA_SIZE;
  285. uvt->resp.present = IB_UCM_PRES_ALTERNATE;
  286. break;
  287. case IB_CM_APR_RECEIVED:
  288. uvt->resp.u.apr_resp.status = evt->param.apr_rcvd.ap_status;
  289. uvt->data_len = IB_CM_APR_PRIVATE_DATA_SIZE;
  290. uvt->info_len = evt->param.apr_rcvd.info_len;
  291. info = evt->param.apr_rcvd.apr_info;
  292. break;
  293. case IB_CM_SIDR_REQ_RECEIVED:
  294. uvt->resp.u.sidr_req_resp.pkey =
  295. evt->param.sidr_req_rcvd.pkey;
  296. uvt->resp.u.sidr_req_resp.port =
  297. evt->param.sidr_req_rcvd.port;
  298. uvt->data_len = IB_CM_SIDR_REQ_PRIVATE_DATA_SIZE;
  299. break;
  300. case IB_CM_SIDR_REP_RECEIVED:
  301. ib_ucm_event_sidr_rep_get(&uvt->resp.u.sidr_rep_resp,
  302. &evt->param.sidr_rep_rcvd);
  303. uvt->data_len = IB_CM_SIDR_REP_PRIVATE_DATA_SIZE;
  304. uvt->info_len = evt->param.sidr_rep_rcvd.info_len;
  305. info = evt->param.sidr_rep_rcvd.info;
  306. break;
  307. default:
  308. uvt->resp.u.send_status = evt->param.send_status;
  309. break;
  310. }
  311. if (uvt->data_len) {
  312. uvt->data = kmalloc(uvt->data_len, GFP_KERNEL);
  313. if (!uvt->data)
  314. goto err1;
  315. memcpy(uvt->data, evt->private_data, uvt->data_len);
  316. uvt->resp.present |= IB_UCM_PRES_DATA;
  317. }
  318. if (uvt->info_len) {
  319. uvt->info = kmalloc(uvt->info_len, GFP_KERNEL);
  320. if (!uvt->info)
  321. goto err2;
  322. memcpy(uvt->info, info, uvt->info_len);
  323. uvt->resp.present |= IB_UCM_PRES_INFO;
  324. }
  325. return 0;
  326. err2:
  327. kfree(uvt->data);
  328. err1:
  329. return -ENOMEM;
  330. }
  331. static int ib_ucm_event_handler(struct ib_cm_id *cm_id,
  332. struct ib_cm_event *event)
  333. {
  334. struct ib_ucm_event *uevent;
  335. struct ib_ucm_context *ctx;
  336. int result = 0;
  337. ctx = cm_id->context;
  338. uevent = kzalloc(sizeof *uevent, GFP_KERNEL);
  339. if (!uevent)
  340. goto err1;
  341. uevent->ctx = ctx;
  342. uevent->cm_id = cm_id;
  343. uevent->resp.uid = ctx->uid;
  344. uevent->resp.id = ctx->id;
  345. uevent->resp.event = event->event;
  346. result = ib_ucm_event_process(event, uevent);
  347. if (result)
  348. goto err2;
  349. down(&ctx->file->mutex);
  350. list_add_tail(&uevent->file_list, &ctx->file->events);
  351. list_add_tail(&uevent->ctx_list, &ctx->events);
  352. wake_up_interruptible(&ctx->file->poll_wait);
  353. up(&ctx->file->mutex);
  354. return 0;
  355. err2:
  356. kfree(uevent);
  357. err1:
  358. /* Destroy new cm_id's */
  359. return ib_ucm_new_cm_id(event->event);
  360. }
  361. static ssize_t ib_ucm_event(struct ib_ucm_file *file,
  362. const char __user *inbuf,
  363. int in_len, int out_len)
  364. {
  365. struct ib_ucm_context *ctx;
  366. struct ib_ucm_event_get cmd;
  367. struct ib_ucm_event *uevent;
  368. int result = 0;
  369. DEFINE_WAIT(wait);
  370. if (out_len < sizeof(struct ib_ucm_event_resp))
  371. return -ENOSPC;
  372. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  373. return -EFAULT;
  374. down(&file->mutex);
  375. while (list_empty(&file->events)) {
  376. if (file->filp->f_flags & O_NONBLOCK) {
  377. result = -EAGAIN;
  378. break;
  379. }
  380. if (signal_pending(current)) {
  381. result = -ERESTARTSYS;
  382. break;
  383. }
  384. prepare_to_wait(&file->poll_wait, &wait, TASK_INTERRUPTIBLE);
  385. up(&file->mutex);
  386. schedule();
  387. down(&file->mutex);
  388. finish_wait(&file->poll_wait, &wait);
  389. }
  390. if (result)
  391. goto done;
  392. uevent = list_entry(file->events.next, struct ib_ucm_event, file_list);
  393. if (ib_ucm_new_cm_id(uevent->resp.event)) {
  394. ctx = ib_ucm_ctx_alloc(file);
  395. if (!ctx) {
  396. result = -ENOMEM;
  397. goto done;
  398. }
  399. ctx->cm_id = uevent->cm_id;
  400. ctx->cm_id->context = ctx;
  401. uevent->resp.id = ctx->id;
  402. }
  403. if (copy_to_user((void __user *)(unsigned long)cmd.response,
  404. &uevent->resp, sizeof(uevent->resp))) {
  405. result = -EFAULT;
  406. goto done;
  407. }
  408. if (uevent->data) {
  409. if (cmd.data_len < uevent->data_len) {
  410. result = -ENOMEM;
  411. goto done;
  412. }
  413. if (copy_to_user((void __user *)(unsigned long)cmd.data,
  414. uevent->data, uevent->data_len)) {
  415. result = -EFAULT;
  416. goto done;
  417. }
  418. }
  419. if (uevent->info) {
  420. if (cmd.info_len < uevent->info_len) {
  421. result = -ENOMEM;
  422. goto done;
  423. }
  424. if (copy_to_user((void __user *)(unsigned long)cmd.info,
  425. uevent->info, uevent->info_len)) {
  426. result = -EFAULT;
  427. goto done;
  428. }
  429. }
  430. list_del(&uevent->file_list);
  431. list_del(&uevent->ctx_list);
  432. uevent->ctx->events_reported++;
  433. kfree(uevent->data);
  434. kfree(uevent->info);
  435. kfree(uevent);
  436. done:
  437. up(&file->mutex);
  438. return result;
  439. }
  440. static ssize_t ib_ucm_create_id(struct ib_ucm_file *file,
  441. const char __user *inbuf,
  442. int in_len, int out_len)
  443. {
  444. struct ib_ucm_create_id cmd;
  445. struct ib_ucm_create_id_resp resp;
  446. struct ib_ucm_context *ctx;
  447. int result;
  448. if (out_len < sizeof(resp))
  449. return -ENOSPC;
  450. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  451. return -EFAULT;
  452. down(&file->mutex);
  453. ctx = ib_ucm_ctx_alloc(file);
  454. up(&file->mutex);
  455. if (!ctx)
  456. return -ENOMEM;
  457. ctx->uid = cmd.uid;
  458. ctx->cm_id = ib_create_cm_id(file->device->ib_dev,
  459. ib_ucm_event_handler, ctx);
  460. if (IS_ERR(ctx->cm_id)) {
  461. result = PTR_ERR(ctx->cm_id);
  462. goto err1;
  463. }
  464. resp.id = ctx->id;
  465. if (copy_to_user((void __user *)(unsigned long)cmd.response,
  466. &resp, sizeof(resp))) {
  467. result = -EFAULT;
  468. goto err2;
  469. }
  470. return 0;
  471. err2:
  472. ib_destroy_cm_id(ctx->cm_id);
  473. err1:
  474. mutex_lock(&ctx_id_mutex);
  475. idr_remove(&ctx_id_table, ctx->id);
  476. mutex_unlock(&ctx_id_mutex);
  477. kfree(ctx);
  478. return result;
  479. }
  480. static ssize_t ib_ucm_destroy_id(struct ib_ucm_file *file,
  481. const char __user *inbuf,
  482. int in_len, int out_len)
  483. {
  484. struct ib_ucm_destroy_id cmd;
  485. struct ib_ucm_destroy_id_resp resp;
  486. struct ib_ucm_context *ctx;
  487. int result = 0;
  488. if (out_len < sizeof(resp))
  489. return -ENOSPC;
  490. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  491. return -EFAULT;
  492. mutex_lock(&ctx_id_mutex);
  493. ctx = idr_find(&ctx_id_table, cmd.id);
  494. if (!ctx)
  495. ctx = ERR_PTR(-ENOENT);
  496. else if (ctx->file != file)
  497. ctx = ERR_PTR(-EINVAL);
  498. else
  499. idr_remove(&ctx_id_table, ctx->id);
  500. mutex_unlock(&ctx_id_mutex);
  501. if (IS_ERR(ctx))
  502. return PTR_ERR(ctx);
  503. ib_ucm_ctx_put(ctx);
  504. wait_for_completion(&ctx->comp);
  505. /* No new events will be generated after destroying the cm_id. */
  506. ib_destroy_cm_id(ctx->cm_id);
  507. /* Cleanup events not yet reported to the user. */
  508. ib_ucm_cleanup_events(ctx);
  509. resp.events_reported = ctx->events_reported;
  510. if (copy_to_user((void __user *)(unsigned long)cmd.response,
  511. &resp, sizeof(resp)))
  512. result = -EFAULT;
  513. kfree(ctx);
  514. return result;
  515. }
  516. static ssize_t ib_ucm_attr_id(struct ib_ucm_file *file,
  517. const char __user *inbuf,
  518. int in_len, int out_len)
  519. {
  520. struct ib_ucm_attr_id_resp resp;
  521. struct ib_ucm_attr_id cmd;
  522. struct ib_ucm_context *ctx;
  523. int result = 0;
  524. if (out_len < sizeof(resp))
  525. return -ENOSPC;
  526. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  527. return -EFAULT;
  528. ctx = ib_ucm_ctx_get(file, cmd.id);
  529. if (IS_ERR(ctx))
  530. return PTR_ERR(ctx);
  531. resp.service_id = ctx->cm_id->service_id;
  532. resp.service_mask = ctx->cm_id->service_mask;
  533. resp.local_id = ctx->cm_id->local_id;
  534. resp.remote_id = ctx->cm_id->remote_id;
  535. if (copy_to_user((void __user *)(unsigned long)cmd.response,
  536. &resp, sizeof(resp)))
  537. result = -EFAULT;
  538. ib_ucm_ctx_put(ctx);
  539. return result;
  540. }
  541. static void ib_ucm_copy_ah_attr(struct ib_ucm_ah_attr *dest_attr,
  542. struct ib_ah_attr *src_attr)
  543. {
  544. memcpy(dest_attr->grh_dgid, src_attr->grh.dgid.raw,
  545. sizeof src_attr->grh.dgid);
  546. dest_attr->grh_flow_label = src_attr->grh.flow_label;
  547. dest_attr->grh_sgid_index = src_attr->grh.sgid_index;
  548. dest_attr->grh_hop_limit = src_attr->grh.hop_limit;
  549. dest_attr->grh_traffic_class = src_attr->grh.traffic_class;
  550. dest_attr->dlid = src_attr->dlid;
  551. dest_attr->sl = src_attr->sl;
  552. dest_attr->src_path_bits = src_attr->src_path_bits;
  553. dest_attr->static_rate = src_attr->static_rate;
  554. dest_attr->is_global = (src_attr->ah_flags & IB_AH_GRH);
  555. dest_attr->port_num = src_attr->port_num;
  556. }
  557. static void ib_ucm_copy_qp_attr(struct ib_ucm_init_qp_attr_resp *dest_attr,
  558. struct ib_qp_attr *src_attr)
  559. {
  560. dest_attr->cur_qp_state = src_attr->cur_qp_state;
  561. dest_attr->path_mtu = src_attr->path_mtu;
  562. dest_attr->path_mig_state = src_attr->path_mig_state;
  563. dest_attr->qkey = src_attr->qkey;
  564. dest_attr->rq_psn = src_attr->rq_psn;
  565. dest_attr->sq_psn = src_attr->sq_psn;
  566. dest_attr->dest_qp_num = src_attr->dest_qp_num;
  567. dest_attr->qp_access_flags = src_attr->qp_access_flags;
  568. dest_attr->max_send_wr = src_attr->cap.max_send_wr;
  569. dest_attr->max_recv_wr = src_attr->cap.max_recv_wr;
  570. dest_attr->max_send_sge = src_attr->cap.max_send_sge;
  571. dest_attr->max_recv_sge = src_attr->cap.max_recv_sge;
  572. dest_attr->max_inline_data = src_attr->cap.max_inline_data;
  573. ib_ucm_copy_ah_attr(&dest_attr->ah_attr, &src_attr->ah_attr);
  574. ib_ucm_copy_ah_attr(&dest_attr->alt_ah_attr, &src_attr->alt_ah_attr);
  575. dest_attr->pkey_index = src_attr->pkey_index;
  576. dest_attr->alt_pkey_index = src_attr->alt_pkey_index;
  577. dest_attr->en_sqd_async_notify = src_attr->en_sqd_async_notify;
  578. dest_attr->sq_draining = src_attr->sq_draining;
  579. dest_attr->max_rd_atomic = src_attr->max_rd_atomic;
  580. dest_attr->max_dest_rd_atomic = src_attr->max_dest_rd_atomic;
  581. dest_attr->min_rnr_timer = src_attr->min_rnr_timer;
  582. dest_attr->port_num = src_attr->port_num;
  583. dest_attr->timeout = src_attr->timeout;
  584. dest_attr->retry_cnt = src_attr->retry_cnt;
  585. dest_attr->rnr_retry = src_attr->rnr_retry;
  586. dest_attr->alt_port_num = src_attr->alt_port_num;
  587. dest_attr->alt_timeout = src_attr->alt_timeout;
  588. }
  589. static ssize_t ib_ucm_init_qp_attr(struct ib_ucm_file *file,
  590. const char __user *inbuf,
  591. int in_len, int out_len)
  592. {
  593. struct ib_ucm_init_qp_attr_resp resp;
  594. struct ib_ucm_init_qp_attr cmd;
  595. struct ib_ucm_context *ctx;
  596. struct ib_qp_attr qp_attr;
  597. int result = 0;
  598. if (out_len < sizeof(resp))
  599. return -ENOSPC;
  600. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  601. return -EFAULT;
  602. ctx = ib_ucm_ctx_get(file, cmd.id);
  603. if (IS_ERR(ctx))
  604. return PTR_ERR(ctx);
  605. resp.qp_attr_mask = 0;
  606. memset(&qp_attr, 0, sizeof qp_attr);
  607. qp_attr.qp_state = cmd.qp_state;
  608. result = ib_cm_init_qp_attr(ctx->cm_id, &qp_attr, &resp.qp_attr_mask);
  609. if (result)
  610. goto out;
  611. ib_ucm_copy_qp_attr(&resp, &qp_attr);
  612. if (copy_to_user((void __user *)(unsigned long)cmd.response,
  613. &resp, sizeof(resp)))
  614. result = -EFAULT;
  615. out:
  616. ib_ucm_ctx_put(ctx);
  617. return result;
  618. }
  619. static ssize_t ib_ucm_listen(struct ib_ucm_file *file,
  620. const char __user *inbuf,
  621. int in_len, int out_len)
  622. {
  623. struct ib_ucm_listen cmd;
  624. struct ib_ucm_context *ctx;
  625. int result;
  626. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  627. return -EFAULT;
  628. ctx = ib_ucm_ctx_get(file, cmd.id);
  629. if (IS_ERR(ctx))
  630. return PTR_ERR(ctx);
  631. result = ib_cm_listen(ctx->cm_id, cmd.service_id, cmd.service_mask);
  632. ib_ucm_ctx_put(ctx);
  633. return result;
  634. }
  635. static ssize_t ib_ucm_establish(struct ib_ucm_file *file,
  636. const char __user *inbuf,
  637. int in_len, int out_len)
  638. {
  639. struct ib_ucm_establish cmd;
  640. struct ib_ucm_context *ctx;
  641. int result;
  642. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  643. return -EFAULT;
  644. ctx = ib_ucm_ctx_get(file, cmd.id);
  645. if (IS_ERR(ctx))
  646. return PTR_ERR(ctx);
  647. result = ib_cm_establish(ctx->cm_id);
  648. ib_ucm_ctx_put(ctx);
  649. return result;
  650. }
  651. static int ib_ucm_alloc_data(const void **dest, u64 src, u32 len)
  652. {
  653. void *data;
  654. *dest = NULL;
  655. if (!len)
  656. return 0;
  657. data = kmalloc(len, GFP_KERNEL);
  658. if (!data)
  659. return -ENOMEM;
  660. if (copy_from_user(data, (void __user *)(unsigned long)src, len)) {
  661. kfree(data);
  662. return -EFAULT;
  663. }
  664. *dest = data;
  665. return 0;
  666. }
  667. static int ib_ucm_path_get(struct ib_sa_path_rec **path, u64 src)
  668. {
  669. struct ib_ucm_path_rec ucm_path;
  670. struct ib_sa_path_rec *sa_path;
  671. *path = NULL;
  672. if (!src)
  673. return 0;
  674. sa_path = kmalloc(sizeof(*sa_path), GFP_KERNEL);
  675. if (!sa_path)
  676. return -ENOMEM;
  677. if (copy_from_user(&ucm_path, (void __user *)(unsigned long)src,
  678. sizeof(ucm_path))) {
  679. kfree(sa_path);
  680. return -EFAULT;
  681. }
  682. memcpy(sa_path->dgid.raw, ucm_path.dgid, sizeof sa_path->dgid);
  683. memcpy(sa_path->sgid.raw, ucm_path.sgid, sizeof sa_path->sgid);
  684. sa_path->dlid = ucm_path.dlid;
  685. sa_path->slid = ucm_path.slid;
  686. sa_path->raw_traffic = ucm_path.raw_traffic;
  687. sa_path->flow_label = ucm_path.flow_label;
  688. sa_path->hop_limit = ucm_path.hop_limit;
  689. sa_path->traffic_class = ucm_path.traffic_class;
  690. sa_path->reversible = ucm_path.reversible;
  691. sa_path->numb_path = ucm_path.numb_path;
  692. sa_path->pkey = ucm_path.pkey;
  693. sa_path->sl = ucm_path.sl;
  694. sa_path->mtu_selector = ucm_path.mtu_selector;
  695. sa_path->mtu = ucm_path.mtu;
  696. sa_path->rate_selector = ucm_path.rate_selector;
  697. sa_path->rate = ucm_path.rate;
  698. sa_path->packet_life_time = ucm_path.packet_life_time;
  699. sa_path->preference = ucm_path.preference;
  700. sa_path->packet_life_time_selector =
  701. ucm_path.packet_life_time_selector;
  702. *path = sa_path;
  703. return 0;
  704. }
  705. static ssize_t ib_ucm_send_req(struct ib_ucm_file *file,
  706. const char __user *inbuf,
  707. int in_len, int out_len)
  708. {
  709. struct ib_cm_req_param param;
  710. struct ib_ucm_context *ctx;
  711. struct ib_ucm_req cmd;
  712. int result;
  713. param.private_data = NULL;
  714. param.primary_path = NULL;
  715. param.alternate_path = NULL;
  716. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  717. return -EFAULT;
  718. result = ib_ucm_alloc_data(&param.private_data, cmd.data, cmd.len);
  719. if (result)
  720. goto done;
  721. result = ib_ucm_path_get(&param.primary_path, cmd.primary_path);
  722. if (result)
  723. goto done;
  724. result = ib_ucm_path_get(&param.alternate_path, cmd.alternate_path);
  725. if (result)
  726. goto done;
  727. param.private_data_len = cmd.len;
  728. param.service_id = cmd.sid;
  729. param.qp_num = cmd.qpn;
  730. param.qp_type = cmd.qp_type;
  731. param.starting_psn = cmd.psn;
  732. param.peer_to_peer = cmd.peer_to_peer;
  733. param.responder_resources = cmd.responder_resources;
  734. param.initiator_depth = cmd.initiator_depth;
  735. param.remote_cm_response_timeout = cmd.remote_cm_response_timeout;
  736. param.flow_control = cmd.flow_control;
  737. param.local_cm_response_timeout = cmd.local_cm_response_timeout;
  738. param.retry_count = cmd.retry_count;
  739. param.rnr_retry_count = cmd.rnr_retry_count;
  740. param.max_cm_retries = cmd.max_cm_retries;
  741. param.srq = cmd.srq;
  742. ctx = ib_ucm_ctx_get(file, cmd.id);
  743. if (!IS_ERR(ctx)) {
  744. result = ib_send_cm_req(ctx->cm_id, &param);
  745. ib_ucm_ctx_put(ctx);
  746. } else
  747. result = PTR_ERR(ctx);
  748. done:
  749. kfree(param.private_data);
  750. kfree(param.primary_path);
  751. kfree(param.alternate_path);
  752. return result;
  753. }
  754. static ssize_t ib_ucm_send_rep(struct ib_ucm_file *file,
  755. const char __user *inbuf,
  756. int in_len, int out_len)
  757. {
  758. struct ib_cm_rep_param param;
  759. struct ib_ucm_context *ctx;
  760. struct ib_ucm_rep cmd;
  761. int result;
  762. param.private_data = NULL;
  763. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  764. return -EFAULT;
  765. result = ib_ucm_alloc_data(&param.private_data, cmd.data, cmd.len);
  766. if (result)
  767. return result;
  768. param.qp_num = cmd.qpn;
  769. param.starting_psn = cmd.psn;
  770. param.private_data_len = cmd.len;
  771. param.responder_resources = cmd.responder_resources;
  772. param.initiator_depth = cmd.initiator_depth;
  773. param.target_ack_delay = cmd.target_ack_delay;
  774. param.failover_accepted = cmd.failover_accepted;
  775. param.flow_control = cmd.flow_control;
  776. param.rnr_retry_count = cmd.rnr_retry_count;
  777. param.srq = cmd.srq;
  778. ctx = ib_ucm_ctx_get(file, cmd.id);
  779. if (!IS_ERR(ctx)) {
  780. ctx->uid = cmd.uid;
  781. result = ib_send_cm_rep(ctx->cm_id, &param);
  782. ib_ucm_ctx_put(ctx);
  783. } else
  784. result = PTR_ERR(ctx);
  785. kfree(param.private_data);
  786. return result;
  787. }
  788. static ssize_t ib_ucm_send_private_data(struct ib_ucm_file *file,
  789. const char __user *inbuf, int in_len,
  790. int (*func)(struct ib_cm_id *cm_id,
  791. const void *private_data,
  792. u8 private_data_len))
  793. {
  794. struct ib_ucm_private_data cmd;
  795. struct ib_ucm_context *ctx;
  796. const void *private_data = NULL;
  797. int result;
  798. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  799. return -EFAULT;
  800. result = ib_ucm_alloc_data(&private_data, cmd.data, cmd.len);
  801. if (result)
  802. return result;
  803. ctx = ib_ucm_ctx_get(file, cmd.id);
  804. if (!IS_ERR(ctx)) {
  805. result = func(ctx->cm_id, private_data, cmd.len);
  806. ib_ucm_ctx_put(ctx);
  807. } else
  808. result = PTR_ERR(ctx);
  809. kfree(private_data);
  810. return result;
  811. }
  812. static ssize_t ib_ucm_send_rtu(struct ib_ucm_file *file,
  813. const char __user *inbuf,
  814. int in_len, int out_len)
  815. {
  816. return ib_ucm_send_private_data(file, inbuf, in_len, ib_send_cm_rtu);
  817. }
  818. static ssize_t ib_ucm_send_dreq(struct ib_ucm_file *file,
  819. const char __user *inbuf,
  820. int in_len, int out_len)
  821. {
  822. return ib_ucm_send_private_data(file, inbuf, in_len, ib_send_cm_dreq);
  823. }
  824. static ssize_t ib_ucm_send_drep(struct ib_ucm_file *file,
  825. const char __user *inbuf,
  826. int in_len, int out_len)
  827. {
  828. return ib_ucm_send_private_data(file, inbuf, in_len, ib_send_cm_drep);
  829. }
  830. static ssize_t ib_ucm_send_info(struct ib_ucm_file *file,
  831. const char __user *inbuf, int in_len,
  832. int (*func)(struct ib_cm_id *cm_id,
  833. int status,
  834. const void *info,
  835. u8 info_len,
  836. const void *data,
  837. u8 data_len))
  838. {
  839. struct ib_ucm_context *ctx;
  840. struct ib_ucm_info cmd;
  841. const void *data = NULL;
  842. const void *info = NULL;
  843. int result;
  844. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  845. return -EFAULT;
  846. result = ib_ucm_alloc_data(&data, cmd.data, cmd.data_len);
  847. if (result)
  848. goto done;
  849. result = ib_ucm_alloc_data(&info, cmd.info, cmd.info_len);
  850. if (result)
  851. goto done;
  852. ctx = ib_ucm_ctx_get(file, cmd.id);
  853. if (!IS_ERR(ctx)) {
  854. result = func(ctx->cm_id, cmd.status, info, cmd.info_len,
  855. data, cmd.data_len);
  856. ib_ucm_ctx_put(ctx);
  857. } else
  858. result = PTR_ERR(ctx);
  859. done:
  860. kfree(data);
  861. kfree(info);
  862. return result;
  863. }
  864. static ssize_t ib_ucm_send_rej(struct ib_ucm_file *file,
  865. const char __user *inbuf,
  866. int in_len, int out_len)
  867. {
  868. return ib_ucm_send_info(file, inbuf, in_len, (void *)ib_send_cm_rej);
  869. }
  870. static ssize_t ib_ucm_send_apr(struct ib_ucm_file *file,
  871. const char __user *inbuf,
  872. int in_len, int out_len)
  873. {
  874. return ib_ucm_send_info(file, inbuf, in_len, (void *)ib_send_cm_apr);
  875. }
  876. static ssize_t ib_ucm_send_mra(struct ib_ucm_file *file,
  877. const char __user *inbuf,
  878. int in_len, int out_len)
  879. {
  880. struct ib_ucm_context *ctx;
  881. struct ib_ucm_mra cmd;
  882. const void *data = NULL;
  883. int result;
  884. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  885. return -EFAULT;
  886. result = ib_ucm_alloc_data(&data, cmd.data, cmd.len);
  887. if (result)
  888. return result;
  889. ctx = ib_ucm_ctx_get(file, cmd.id);
  890. if (!IS_ERR(ctx)) {
  891. result = ib_send_cm_mra(ctx->cm_id, cmd.timeout, data, cmd.len);
  892. ib_ucm_ctx_put(ctx);
  893. } else
  894. result = PTR_ERR(ctx);
  895. kfree(data);
  896. return result;
  897. }
  898. static ssize_t ib_ucm_send_lap(struct ib_ucm_file *file,
  899. const char __user *inbuf,
  900. int in_len, int out_len)
  901. {
  902. struct ib_ucm_context *ctx;
  903. struct ib_sa_path_rec *path = NULL;
  904. struct ib_ucm_lap cmd;
  905. const void *data = NULL;
  906. int result;
  907. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  908. return -EFAULT;
  909. result = ib_ucm_alloc_data(&data, cmd.data, cmd.len);
  910. if (result)
  911. goto done;
  912. result = ib_ucm_path_get(&path, cmd.path);
  913. if (result)
  914. goto done;
  915. ctx = ib_ucm_ctx_get(file, cmd.id);
  916. if (!IS_ERR(ctx)) {
  917. result = ib_send_cm_lap(ctx->cm_id, path, data, cmd.len);
  918. ib_ucm_ctx_put(ctx);
  919. } else
  920. result = PTR_ERR(ctx);
  921. done:
  922. kfree(data);
  923. kfree(path);
  924. return result;
  925. }
  926. static ssize_t ib_ucm_send_sidr_req(struct ib_ucm_file *file,
  927. const char __user *inbuf,
  928. int in_len, int out_len)
  929. {
  930. struct ib_cm_sidr_req_param param;
  931. struct ib_ucm_context *ctx;
  932. struct ib_ucm_sidr_req cmd;
  933. int result;
  934. param.private_data = NULL;
  935. param.path = NULL;
  936. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  937. return -EFAULT;
  938. result = ib_ucm_alloc_data(&param.private_data, cmd.data, cmd.len);
  939. if (result)
  940. goto done;
  941. result = ib_ucm_path_get(&param.path, cmd.path);
  942. if (result)
  943. goto done;
  944. param.private_data_len = cmd.len;
  945. param.service_id = cmd.sid;
  946. param.timeout_ms = cmd.timeout;
  947. param.max_cm_retries = cmd.max_cm_retries;
  948. param.pkey = cmd.pkey;
  949. ctx = ib_ucm_ctx_get(file, cmd.id);
  950. if (!IS_ERR(ctx)) {
  951. result = ib_send_cm_sidr_req(ctx->cm_id, &param);
  952. ib_ucm_ctx_put(ctx);
  953. } else
  954. result = PTR_ERR(ctx);
  955. done:
  956. kfree(param.private_data);
  957. kfree(param.path);
  958. return result;
  959. }
  960. static ssize_t ib_ucm_send_sidr_rep(struct ib_ucm_file *file,
  961. const char __user *inbuf,
  962. int in_len, int out_len)
  963. {
  964. struct ib_cm_sidr_rep_param param;
  965. struct ib_ucm_sidr_rep cmd;
  966. struct ib_ucm_context *ctx;
  967. int result;
  968. param.info = NULL;
  969. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  970. return -EFAULT;
  971. result = ib_ucm_alloc_data(&param.private_data,
  972. cmd.data, cmd.data_len);
  973. if (result)
  974. goto done;
  975. result = ib_ucm_alloc_data(&param.info, cmd.info, cmd.info_len);
  976. if (result)
  977. goto done;
  978. param.qp_num = cmd.qpn;
  979. param.qkey = cmd.qkey;
  980. param.status = cmd.status;
  981. param.info_length = cmd.info_len;
  982. param.private_data_len = cmd.data_len;
  983. ctx = ib_ucm_ctx_get(file, cmd.id);
  984. if (!IS_ERR(ctx)) {
  985. result = ib_send_cm_sidr_rep(ctx->cm_id, &param);
  986. ib_ucm_ctx_put(ctx);
  987. } else
  988. result = PTR_ERR(ctx);
  989. done:
  990. kfree(param.private_data);
  991. kfree(param.info);
  992. return result;
  993. }
  994. static ssize_t (*ucm_cmd_table[])(struct ib_ucm_file *file,
  995. const char __user *inbuf,
  996. int in_len, int out_len) = {
  997. [IB_USER_CM_CMD_CREATE_ID] = ib_ucm_create_id,
  998. [IB_USER_CM_CMD_DESTROY_ID] = ib_ucm_destroy_id,
  999. [IB_USER_CM_CMD_ATTR_ID] = ib_ucm_attr_id,
  1000. [IB_USER_CM_CMD_LISTEN] = ib_ucm_listen,
  1001. [IB_USER_CM_CMD_ESTABLISH] = ib_ucm_establish,
  1002. [IB_USER_CM_CMD_SEND_REQ] = ib_ucm_send_req,
  1003. [IB_USER_CM_CMD_SEND_REP] = ib_ucm_send_rep,
  1004. [IB_USER_CM_CMD_SEND_RTU] = ib_ucm_send_rtu,
  1005. [IB_USER_CM_CMD_SEND_DREQ] = ib_ucm_send_dreq,
  1006. [IB_USER_CM_CMD_SEND_DREP] = ib_ucm_send_drep,
  1007. [IB_USER_CM_CMD_SEND_REJ] = ib_ucm_send_rej,
  1008. [IB_USER_CM_CMD_SEND_MRA] = ib_ucm_send_mra,
  1009. [IB_USER_CM_CMD_SEND_LAP] = ib_ucm_send_lap,
  1010. [IB_USER_CM_CMD_SEND_APR] = ib_ucm_send_apr,
  1011. [IB_USER_CM_CMD_SEND_SIDR_REQ] = ib_ucm_send_sidr_req,
  1012. [IB_USER_CM_CMD_SEND_SIDR_REP] = ib_ucm_send_sidr_rep,
  1013. [IB_USER_CM_CMD_EVENT] = ib_ucm_event,
  1014. [IB_USER_CM_CMD_INIT_QP_ATTR] = ib_ucm_init_qp_attr,
  1015. };
  1016. static ssize_t ib_ucm_write(struct file *filp, const char __user *buf,
  1017. size_t len, loff_t *pos)
  1018. {
  1019. struct ib_ucm_file *file = filp->private_data;
  1020. struct ib_ucm_cmd_hdr hdr;
  1021. ssize_t result;
  1022. if (len < sizeof(hdr))
  1023. return -EINVAL;
  1024. if (copy_from_user(&hdr, buf, sizeof(hdr)))
  1025. return -EFAULT;
  1026. if (hdr.cmd < 0 || hdr.cmd >= ARRAY_SIZE(ucm_cmd_table))
  1027. return -EINVAL;
  1028. if (hdr.in + sizeof(hdr) > len)
  1029. return -EINVAL;
  1030. result = ucm_cmd_table[hdr.cmd](file, buf + sizeof(hdr),
  1031. hdr.in, hdr.out);
  1032. if (!result)
  1033. result = len;
  1034. return result;
  1035. }
  1036. static unsigned int ib_ucm_poll(struct file *filp,
  1037. struct poll_table_struct *wait)
  1038. {
  1039. struct ib_ucm_file *file = filp->private_data;
  1040. unsigned int mask = 0;
  1041. poll_wait(filp, &file->poll_wait, wait);
  1042. if (!list_empty(&file->events))
  1043. mask = POLLIN | POLLRDNORM;
  1044. return mask;
  1045. }
  1046. static int ib_ucm_open(struct inode *inode, struct file *filp)
  1047. {
  1048. struct ib_ucm_file *file;
  1049. file = kmalloc(sizeof(*file), GFP_KERNEL);
  1050. if (!file)
  1051. return -ENOMEM;
  1052. INIT_LIST_HEAD(&file->events);
  1053. INIT_LIST_HEAD(&file->ctxs);
  1054. init_waitqueue_head(&file->poll_wait);
  1055. init_MUTEX(&file->mutex);
  1056. filp->private_data = file;
  1057. file->filp = filp;
  1058. file->device = container_of(inode->i_cdev, struct ib_ucm_device, dev);
  1059. return 0;
  1060. }
  1061. static int ib_ucm_close(struct inode *inode, struct file *filp)
  1062. {
  1063. struct ib_ucm_file *file = filp->private_data;
  1064. struct ib_ucm_context *ctx;
  1065. down(&file->mutex);
  1066. while (!list_empty(&file->ctxs)) {
  1067. ctx = list_entry(file->ctxs.next,
  1068. struct ib_ucm_context, file_list);
  1069. up(&file->mutex);
  1070. mutex_lock(&ctx_id_mutex);
  1071. idr_remove(&ctx_id_table, ctx->id);
  1072. mutex_unlock(&ctx_id_mutex);
  1073. ib_destroy_cm_id(ctx->cm_id);
  1074. ib_ucm_cleanup_events(ctx);
  1075. kfree(ctx);
  1076. down(&file->mutex);
  1077. }
  1078. up(&file->mutex);
  1079. kfree(file);
  1080. return 0;
  1081. }
  1082. static void ib_ucm_release_class_dev(struct class_device *class_dev)
  1083. {
  1084. struct ib_ucm_device *dev;
  1085. dev = container_of(class_dev, struct ib_ucm_device, class_dev);
  1086. cdev_del(&dev->dev);
  1087. clear_bit(dev->devnum, dev_map);
  1088. kfree(dev);
  1089. }
  1090. static struct file_operations ucm_fops = {
  1091. .owner = THIS_MODULE,
  1092. .open = ib_ucm_open,
  1093. .release = ib_ucm_close,
  1094. .write = ib_ucm_write,
  1095. .poll = ib_ucm_poll,
  1096. };
  1097. static struct class ucm_class = {
  1098. .name = "infiniband_cm",
  1099. .release = ib_ucm_release_class_dev
  1100. };
  1101. static ssize_t show_ibdev(struct class_device *class_dev, char *buf)
  1102. {
  1103. struct ib_ucm_device *dev;
  1104. dev = container_of(class_dev, struct ib_ucm_device, class_dev);
  1105. return sprintf(buf, "%s\n", dev->ib_dev->name);
  1106. }
  1107. static CLASS_DEVICE_ATTR(ibdev, S_IRUGO, show_ibdev, NULL);
  1108. static void ib_ucm_add_one(struct ib_device *device)
  1109. {
  1110. struct ib_ucm_device *ucm_dev;
  1111. if (!device->alloc_ucontext)
  1112. return;
  1113. ucm_dev = kzalloc(sizeof *ucm_dev, GFP_KERNEL);
  1114. if (!ucm_dev)
  1115. return;
  1116. ucm_dev->ib_dev = device;
  1117. ucm_dev->devnum = find_first_zero_bit(dev_map, IB_UCM_MAX_DEVICES);
  1118. if (ucm_dev->devnum >= IB_UCM_MAX_DEVICES)
  1119. goto err;
  1120. set_bit(ucm_dev->devnum, dev_map);
  1121. cdev_init(&ucm_dev->dev, &ucm_fops);
  1122. ucm_dev->dev.owner = THIS_MODULE;
  1123. kobject_set_name(&ucm_dev->dev.kobj, "ucm%d", ucm_dev->devnum);
  1124. if (cdev_add(&ucm_dev->dev, IB_UCM_BASE_DEV + ucm_dev->devnum, 1))
  1125. goto err;
  1126. ucm_dev->class_dev.class = &ucm_class;
  1127. ucm_dev->class_dev.dev = device->dma_device;
  1128. ucm_dev->class_dev.devt = ucm_dev->dev.dev;
  1129. snprintf(ucm_dev->class_dev.class_id, BUS_ID_SIZE, "ucm%d",
  1130. ucm_dev->devnum);
  1131. if (class_device_register(&ucm_dev->class_dev))
  1132. goto err_cdev;
  1133. if (class_device_create_file(&ucm_dev->class_dev,
  1134. &class_device_attr_ibdev))
  1135. goto err_class;
  1136. ib_set_client_data(device, &ucm_client, ucm_dev);
  1137. return;
  1138. err_class:
  1139. class_device_unregister(&ucm_dev->class_dev);
  1140. err_cdev:
  1141. cdev_del(&ucm_dev->dev);
  1142. clear_bit(ucm_dev->devnum, dev_map);
  1143. err:
  1144. kfree(ucm_dev);
  1145. return;
  1146. }
  1147. static void ib_ucm_remove_one(struct ib_device *device)
  1148. {
  1149. struct ib_ucm_device *ucm_dev = ib_get_client_data(device, &ucm_client);
  1150. if (!ucm_dev)
  1151. return;
  1152. class_device_unregister(&ucm_dev->class_dev);
  1153. }
  1154. static ssize_t show_abi_version(struct class *class, char *buf)
  1155. {
  1156. return sprintf(buf, "%d\n", IB_USER_CM_ABI_VERSION);
  1157. }
  1158. static CLASS_ATTR(abi_version, S_IRUGO, show_abi_version, NULL);
  1159. static int __init ib_ucm_init(void)
  1160. {
  1161. int ret;
  1162. ret = register_chrdev_region(IB_UCM_BASE_DEV, IB_UCM_MAX_DEVICES,
  1163. "infiniband_cm");
  1164. if (ret) {
  1165. printk(KERN_ERR "ucm: couldn't register device number\n");
  1166. goto err;
  1167. }
  1168. ret = class_register(&ucm_class);
  1169. if (ret) {
  1170. printk(KERN_ERR "ucm: couldn't create class infiniband_cm\n");
  1171. goto err_chrdev;
  1172. }
  1173. ret = class_create_file(&ucm_class, &class_attr_abi_version);
  1174. if (ret) {
  1175. printk(KERN_ERR "ucm: couldn't create abi_version attribute\n");
  1176. goto err_class;
  1177. }
  1178. ret = ib_register_client(&ucm_client);
  1179. if (ret) {
  1180. printk(KERN_ERR "ucm: couldn't register client\n");
  1181. goto err_class;
  1182. }
  1183. return 0;
  1184. err_class:
  1185. class_unregister(&ucm_class);
  1186. err_chrdev:
  1187. unregister_chrdev_region(IB_UCM_BASE_DEV, IB_UCM_MAX_DEVICES);
  1188. err:
  1189. return ret;
  1190. }
  1191. static void __exit ib_ucm_cleanup(void)
  1192. {
  1193. ib_unregister_client(&ucm_client);
  1194. class_unregister(&ucm_class);
  1195. unregister_chrdev_region(IB_UCM_BASE_DEV, IB_UCM_MAX_DEVICES);
  1196. idr_destroy(&ctx_id_table);
  1197. }
  1198. module_init(ib_ucm_init);
  1199. module_exit(ib_ucm_cleanup);