ucm.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343
  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 4311 2005-12-05 18:42:01Z sean.hefty $
  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. #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 dev;
  57. struct class_device class_dev;
  58. struct ib_device *ib_dev;
  59. };
  60. struct ib_ucm_file {
  61. struct semaphore 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. down(&ctx->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. /* clear incoming connections. */
  140. if (ib_ucm_new_cm_id(uevent->resp.event))
  141. ib_destroy_cm_id(uevent->cm_id);
  142. kfree(uevent);
  143. }
  144. up(&ctx->file->mutex);
  145. }
  146. static struct ib_ucm_context *ib_ucm_ctx_alloc(struct ib_ucm_file *file)
  147. {
  148. struct ib_ucm_context *ctx;
  149. int result;
  150. ctx = kzalloc(sizeof *ctx, GFP_KERNEL);
  151. if (!ctx)
  152. return NULL;
  153. atomic_set(&ctx->ref, 1);
  154. init_completion(&ctx->comp);
  155. ctx->file = file;
  156. INIT_LIST_HEAD(&ctx->events);
  157. do {
  158. result = idr_pre_get(&ctx_id_table, GFP_KERNEL);
  159. if (!result)
  160. goto error;
  161. mutex_lock(&ctx_id_mutex);
  162. result = idr_get_new(&ctx_id_table, ctx, &ctx->id);
  163. mutex_unlock(&ctx_id_mutex);
  164. } while (result == -EAGAIN);
  165. if (result)
  166. goto error;
  167. list_add_tail(&ctx->file_list, &file->ctxs);
  168. return ctx;
  169. error:
  170. kfree(ctx);
  171. return NULL;
  172. }
  173. static void ib_ucm_event_req_get(struct ib_ucm_req_event_resp *ureq,
  174. struct ib_cm_req_event_param *kreq)
  175. {
  176. ureq->remote_ca_guid = kreq->remote_ca_guid;
  177. ureq->remote_qkey = kreq->remote_qkey;
  178. ureq->remote_qpn = kreq->remote_qpn;
  179. ureq->qp_type = kreq->qp_type;
  180. ureq->starting_psn = kreq->starting_psn;
  181. ureq->responder_resources = kreq->responder_resources;
  182. ureq->initiator_depth = kreq->initiator_depth;
  183. ureq->local_cm_response_timeout = kreq->local_cm_response_timeout;
  184. ureq->flow_control = kreq->flow_control;
  185. ureq->remote_cm_response_timeout = kreq->remote_cm_response_timeout;
  186. ureq->retry_count = kreq->retry_count;
  187. ureq->rnr_retry_count = kreq->rnr_retry_count;
  188. ureq->srq = kreq->srq;
  189. ureq->port = kreq->port;
  190. ib_copy_path_rec_to_user(&ureq->primary_path, kreq->primary_path);
  191. if (kreq->alternate_path)
  192. ib_copy_path_rec_to_user(&ureq->alternate_path,
  193. kreq->alternate_path);
  194. }
  195. static void ib_ucm_event_rep_get(struct ib_ucm_rep_event_resp *urep,
  196. struct ib_cm_rep_event_param *krep)
  197. {
  198. urep->remote_ca_guid = krep->remote_ca_guid;
  199. urep->remote_qkey = krep->remote_qkey;
  200. urep->remote_qpn = krep->remote_qpn;
  201. urep->starting_psn = krep->starting_psn;
  202. urep->responder_resources = krep->responder_resources;
  203. urep->initiator_depth = krep->initiator_depth;
  204. urep->target_ack_delay = krep->target_ack_delay;
  205. urep->failover_accepted = krep->failover_accepted;
  206. urep->flow_control = krep->flow_control;
  207. urep->rnr_retry_count = krep->rnr_retry_count;
  208. urep->srq = krep->srq;
  209. }
  210. static void ib_ucm_event_sidr_rep_get(struct ib_ucm_sidr_rep_event_resp *urep,
  211. struct ib_cm_sidr_rep_event_param *krep)
  212. {
  213. urep->status = krep->status;
  214. urep->qkey = krep->qkey;
  215. urep->qpn = krep->qpn;
  216. };
  217. static int ib_ucm_event_process(struct ib_cm_event *evt,
  218. struct ib_ucm_event *uvt)
  219. {
  220. void *info = NULL;
  221. switch (evt->event) {
  222. case IB_CM_REQ_RECEIVED:
  223. ib_ucm_event_req_get(&uvt->resp.u.req_resp,
  224. &evt->param.req_rcvd);
  225. uvt->data_len = IB_CM_REQ_PRIVATE_DATA_SIZE;
  226. uvt->resp.present = IB_UCM_PRES_PRIMARY;
  227. uvt->resp.present |= (evt->param.req_rcvd.alternate_path ?
  228. IB_UCM_PRES_ALTERNATE : 0);
  229. break;
  230. case IB_CM_REP_RECEIVED:
  231. ib_ucm_event_rep_get(&uvt->resp.u.rep_resp,
  232. &evt->param.rep_rcvd);
  233. uvt->data_len = IB_CM_REP_PRIVATE_DATA_SIZE;
  234. break;
  235. case IB_CM_RTU_RECEIVED:
  236. uvt->data_len = IB_CM_RTU_PRIVATE_DATA_SIZE;
  237. uvt->resp.u.send_status = evt->param.send_status;
  238. break;
  239. case IB_CM_DREQ_RECEIVED:
  240. uvt->data_len = IB_CM_DREQ_PRIVATE_DATA_SIZE;
  241. uvt->resp.u.send_status = evt->param.send_status;
  242. break;
  243. case IB_CM_DREP_RECEIVED:
  244. uvt->data_len = IB_CM_DREP_PRIVATE_DATA_SIZE;
  245. uvt->resp.u.send_status = evt->param.send_status;
  246. break;
  247. case IB_CM_MRA_RECEIVED:
  248. uvt->resp.u.mra_resp.timeout =
  249. evt->param.mra_rcvd.service_timeout;
  250. uvt->data_len = IB_CM_MRA_PRIVATE_DATA_SIZE;
  251. break;
  252. case IB_CM_REJ_RECEIVED:
  253. uvt->resp.u.rej_resp.reason = evt->param.rej_rcvd.reason;
  254. uvt->data_len = IB_CM_REJ_PRIVATE_DATA_SIZE;
  255. uvt->info_len = evt->param.rej_rcvd.ari_length;
  256. info = evt->param.rej_rcvd.ari;
  257. break;
  258. case IB_CM_LAP_RECEIVED:
  259. ib_copy_path_rec_to_user(&uvt->resp.u.lap_resp.path,
  260. evt->param.lap_rcvd.alternate_path);
  261. uvt->data_len = IB_CM_LAP_PRIVATE_DATA_SIZE;
  262. uvt->resp.present = IB_UCM_PRES_ALTERNATE;
  263. break;
  264. case IB_CM_APR_RECEIVED:
  265. uvt->resp.u.apr_resp.status = evt->param.apr_rcvd.ap_status;
  266. uvt->data_len = IB_CM_APR_PRIVATE_DATA_SIZE;
  267. uvt->info_len = evt->param.apr_rcvd.info_len;
  268. info = evt->param.apr_rcvd.apr_info;
  269. break;
  270. case IB_CM_SIDR_REQ_RECEIVED:
  271. uvt->resp.u.sidr_req_resp.pkey =
  272. evt->param.sidr_req_rcvd.pkey;
  273. uvt->resp.u.sidr_req_resp.port =
  274. evt->param.sidr_req_rcvd.port;
  275. uvt->data_len = IB_CM_SIDR_REQ_PRIVATE_DATA_SIZE;
  276. break;
  277. case IB_CM_SIDR_REP_RECEIVED:
  278. ib_ucm_event_sidr_rep_get(&uvt->resp.u.sidr_rep_resp,
  279. &evt->param.sidr_rep_rcvd);
  280. uvt->data_len = IB_CM_SIDR_REP_PRIVATE_DATA_SIZE;
  281. uvt->info_len = evt->param.sidr_rep_rcvd.info_len;
  282. info = evt->param.sidr_rep_rcvd.info;
  283. break;
  284. default:
  285. uvt->resp.u.send_status = evt->param.send_status;
  286. break;
  287. }
  288. if (uvt->data_len) {
  289. uvt->data = kmalloc(uvt->data_len, GFP_KERNEL);
  290. if (!uvt->data)
  291. goto err1;
  292. memcpy(uvt->data, evt->private_data, uvt->data_len);
  293. uvt->resp.present |= IB_UCM_PRES_DATA;
  294. }
  295. if (uvt->info_len) {
  296. uvt->info = kmalloc(uvt->info_len, GFP_KERNEL);
  297. if (!uvt->info)
  298. goto err2;
  299. memcpy(uvt->info, info, uvt->info_len);
  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. down(&ctx->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. up(&ctx->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. down(&file->mutex);
  352. while (list_empty(&file->events)) {
  353. if (file->filp->f_flags & O_NONBLOCK) {
  354. result = -EAGAIN;
  355. break;
  356. }
  357. if (signal_pending(current)) {
  358. result = -ERESTARTSYS;
  359. break;
  360. }
  361. prepare_to_wait(&file->poll_wait, &wait, TASK_INTERRUPTIBLE);
  362. up(&file->mutex);
  363. schedule();
  364. down(&file->mutex);
  365. finish_wait(&file->poll_wait, &wait);
  366. }
  367. if (result)
  368. goto done;
  369. uevent = list_entry(file->events.next, struct ib_ucm_event, file_list);
  370. if (ib_ucm_new_cm_id(uevent->resp.event)) {
  371. ctx = ib_ucm_ctx_alloc(file);
  372. if (!ctx) {
  373. result = -ENOMEM;
  374. goto done;
  375. }
  376. ctx->cm_id = uevent->cm_id;
  377. ctx->cm_id->context = ctx;
  378. uevent->resp.id = ctx->id;
  379. }
  380. if (copy_to_user((void __user *)(unsigned long)cmd.response,
  381. &uevent->resp, sizeof(uevent->resp))) {
  382. result = -EFAULT;
  383. goto done;
  384. }
  385. if (uevent->data) {
  386. if (cmd.data_len < uevent->data_len) {
  387. result = -ENOMEM;
  388. goto done;
  389. }
  390. if (copy_to_user((void __user *)(unsigned long)cmd.data,
  391. uevent->data, uevent->data_len)) {
  392. result = -EFAULT;
  393. goto done;
  394. }
  395. }
  396. if (uevent->info) {
  397. if (cmd.info_len < uevent->info_len) {
  398. result = -ENOMEM;
  399. goto done;
  400. }
  401. if (copy_to_user((void __user *)(unsigned long)cmd.info,
  402. uevent->info, uevent->info_len)) {
  403. result = -EFAULT;
  404. goto done;
  405. }
  406. }
  407. list_del(&uevent->file_list);
  408. list_del(&uevent->ctx_list);
  409. uevent->ctx->events_reported++;
  410. kfree(uevent->data);
  411. kfree(uevent->info);
  412. kfree(uevent);
  413. done:
  414. up(&file->mutex);
  415. return result;
  416. }
  417. static ssize_t ib_ucm_create_id(struct ib_ucm_file *file,
  418. const char __user *inbuf,
  419. int in_len, int out_len)
  420. {
  421. struct ib_ucm_create_id cmd;
  422. struct ib_ucm_create_id_resp resp;
  423. struct ib_ucm_context *ctx;
  424. int result;
  425. if (out_len < sizeof(resp))
  426. return -ENOSPC;
  427. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  428. return -EFAULT;
  429. down(&file->mutex);
  430. ctx = ib_ucm_ctx_alloc(file);
  431. up(&file->mutex);
  432. if (!ctx)
  433. return -ENOMEM;
  434. ctx->uid = cmd.uid;
  435. ctx->cm_id = ib_create_cm_id(file->device->ib_dev,
  436. ib_ucm_event_handler, ctx);
  437. if (IS_ERR(ctx->cm_id)) {
  438. result = PTR_ERR(ctx->cm_id);
  439. goto err1;
  440. }
  441. resp.id = ctx->id;
  442. if (copy_to_user((void __user *)(unsigned long)cmd.response,
  443. &resp, sizeof(resp))) {
  444. result = -EFAULT;
  445. goto err2;
  446. }
  447. return 0;
  448. err2:
  449. ib_destroy_cm_id(ctx->cm_id);
  450. err1:
  451. mutex_lock(&ctx_id_mutex);
  452. idr_remove(&ctx_id_table, ctx->id);
  453. mutex_unlock(&ctx_id_mutex);
  454. kfree(ctx);
  455. return result;
  456. }
  457. static ssize_t ib_ucm_destroy_id(struct ib_ucm_file *file,
  458. const char __user *inbuf,
  459. int in_len, int out_len)
  460. {
  461. struct ib_ucm_destroy_id cmd;
  462. struct ib_ucm_destroy_id_resp resp;
  463. struct ib_ucm_context *ctx;
  464. int result = 0;
  465. if (out_len < sizeof(resp))
  466. return -ENOSPC;
  467. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  468. return -EFAULT;
  469. mutex_lock(&ctx_id_mutex);
  470. ctx = idr_find(&ctx_id_table, cmd.id);
  471. if (!ctx)
  472. ctx = ERR_PTR(-ENOENT);
  473. else if (ctx->file != file)
  474. ctx = ERR_PTR(-EINVAL);
  475. else
  476. idr_remove(&ctx_id_table, ctx->id);
  477. mutex_unlock(&ctx_id_mutex);
  478. if (IS_ERR(ctx))
  479. return PTR_ERR(ctx);
  480. ib_ucm_ctx_put(ctx);
  481. wait_for_completion(&ctx->comp);
  482. /* No new events will be generated after destroying the cm_id. */
  483. ib_destroy_cm_id(ctx->cm_id);
  484. /* Cleanup events not yet reported to the user. */
  485. ib_ucm_cleanup_events(ctx);
  486. resp.events_reported = ctx->events_reported;
  487. if (copy_to_user((void __user *)(unsigned long)cmd.response,
  488. &resp, sizeof(resp)))
  489. result = -EFAULT;
  490. kfree(ctx);
  491. return result;
  492. }
  493. static ssize_t ib_ucm_attr_id(struct ib_ucm_file *file,
  494. const char __user *inbuf,
  495. int in_len, int out_len)
  496. {
  497. struct ib_ucm_attr_id_resp resp;
  498. struct ib_ucm_attr_id cmd;
  499. struct ib_ucm_context *ctx;
  500. int result = 0;
  501. if (out_len < sizeof(resp))
  502. return -ENOSPC;
  503. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  504. return -EFAULT;
  505. ctx = ib_ucm_ctx_get(file, cmd.id);
  506. if (IS_ERR(ctx))
  507. return PTR_ERR(ctx);
  508. resp.service_id = ctx->cm_id->service_id;
  509. resp.service_mask = ctx->cm_id->service_mask;
  510. resp.local_id = ctx->cm_id->local_id;
  511. resp.remote_id = ctx->cm_id->remote_id;
  512. if (copy_to_user((void __user *)(unsigned long)cmd.response,
  513. &resp, sizeof(resp)))
  514. result = -EFAULT;
  515. ib_ucm_ctx_put(ctx);
  516. return result;
  517. }
  518. static ssize_t ib_ucm_init_qp_attr(struct ib_ucm_file *file,
  519. const char __user *inbuf,
  520. int in_len, int out_len)
  521. {
  522. struct ib_uverbs_qp_attr resp;
  523. struct ib_ucm_init_qp_attr cmd;
  524. struct ib_ucm_context *ctx;
  525. struct ib_qp_attr qp_attr;
  526. int result = 0;
  527. if (out_len < sizeof(resp))
  528. return -ENOSPC;
  529. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  530. return -EFAULT;
  531. ctx = ib_ucm_ctx_get(file, cmd.id);
  532. if (IS_ERR(ctx))
  533. return PTR_ERR(ctx);
  534. resp.qp_attr_mask = 0;
  535. memset(&qp_attr, 0, sizeof qp_attr);
  536. qp_attr.qp_state = cmd.qp_state;
  537. result = ib_cm_init_qp_attr(ctx->cm_id, &qp_attr, &resp.qp_attr_mask);
  538. if (result)
  539. goto out;
  540. ib_copy_qp_attr_to_user(&resp, &qp_attr);
  541. if (copy_to_user((void __user *)(unsigned long)cmd.response,
  542. &resp, sizeof(resp)))
  543. result = -EFAULT;
  544. out:
  545. ib_ucm_ctx_put(ctx);
  546. return result;
  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 = ib_cm_listen(ctx->cm_id, cmd.service_id, cmd.service_mask);
  561. ib_ucm_ctx_put(ctx);
  562. return result;
  563. }
  564. static ssize_t ib_ucm_establish(struct ib_ucm_file *file,
  565. const char __user *inbuf,
  566. int in_len, int out_len)
  567. {
  568. struct ib_ucm_establish cmd;
  569. struct ib_ucm_context *ctx;
  570. int result;
  571. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  572. return -EFAULT;
  573. ctx = ib_ucm_ctx_get(file, cmd.id);
  574. if (IS_ERR(ctx))
  575. return PTR_ERR(ctx);
  576. result = ib_cm_establish(ctx->cm_id);
  577. ib_ucm_ctx_put(ctx);
  578. return result;
  579. }
  580. static int ib_ucm_alloc_data(const void **dest, u64 src, u32 len)
  581. {
  582. void *data;
  583. *dest = NULL;
  584. if (!len)
  585. return 0;
  586. data = kmalloc(len, GFP_KERNEL);
  587. if (!data)
  588. return -ENOMEM;
  589. if (copy_from_user(data, (void __user *)(unsigned long)src, len)) {
  590. kfree(data);
  591. return -EFAULT;
  592. }
  593. *dest = data;
  594. return 0;
  595. }
  596. static int ib_ucm_path_get(struct ib_sa_path_rec **path, u64 src)
  597. {
  598. struct ib_user_path_rec upath;
  599. struct ib_sa_path_rec *sa_path;
  600. *path = NULL;
  601. if (!src)
  602. return 0;
  603. sa_path = kmalloc(sizeof(*sa_path), GFP_KERNEL);
  604. if (!sa_path)
  605. return -ENOMEM;
  606. if (copy_from_user(&upath, (void __user *)(unsigned long)src,
  607. sizeof(upath))) {
  608. kfree(sa_path);
  609. return -EFAULT;
  610. }
  611. ib_copy_path_rec_from_user(sa_path, &upath);
  612. *path = sa_path;
  613. return 0;
  614. }
  615. static ssize_t ib_ucm_send_req(struct ib_ucm_file *file,
  616. const char __user *inbuf,
  617. int in_len, int out_len)
  618. {
  619. struct ib_cm_req_param param;
  620. struct ib_ucm_context *ctx;
  621. struct ib_ucm_req cmd;
  622. int result;
  623. param.private_data = NULL;
  624. param.primary_path = NULL;
  625. param.alternate_path = NULL;
  626. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  627. return -EFAULT;
  628. result = ib_ucm_alloc_data(&param.private_data, cmd.data, cmd.len);
  629. if (result)
  630. goto done;
  631. result = ib_ucm_path_get(&param.primary_path, cmd.primary_path);
  632. if (result)
  633. goto done;
  634. result = ib_ucm_path_get(&param.alternate_path, cmd.alternate_path);
  635. if (result)
  636. goto done;
  637. param.private_data_len = cmd.len;
  638. param.service_id = cmd.sid;
  639. param.qp_num = cmd.qpn;
  640. param.qp_type = cmd.qp_type;
  641. param.starting_psn = cmd.psn;
  642. param.peer_to_peer = cmd.peer_to_peer;
  643. param.responder_resources = cmd.responder_resources;
  644. param.initiator_depth = cmd.initiator_depth;
  645. param.remote_cm_response_timeout = cmd.remote_cm_response_timeout;
  646. param.flow_control = cmd.flow_control;
  647. param.local_cm_response_timeout = cmd.local_cm_response_timeout;
  648. param.retry_count = cmd.retry_count;
  649. param.rnr_retry_count = cmd.rnr_retry_count;
  650. param.max_cm_retries = cmd.max_cm_retries;
  651. param.srq = cmd.srq;
  652. ctx = ib_ucm_ctx_get(file, cmd.id);
  653. if (!IS_ERR(ctx)) {
  654. result = ib_send_cm_req(ctx->cm_id, &param);
  655. ib_ucm_ctx_put(ctx);
  656. } else
  657. result = PTR_ERR(ctx);
  658. done:
  659. kfree(param.private_data);
  660. kfree(param.primary_path);
  661. kfree(param.alternate_path);
  662. return result;
  663. }
  664. static ssize_t ib_ucm_send_rep(struct ib_ucm_file *file,
  665. const char __user *inbuf,
  666. int in_len, int out_len)
  667. {
  668. struct ib_cm_rep_param param;
  669. struct ib_ucm_context *ctx;
  670. struct ib_ucm_rep cmd;
  671. int result;
  672. param.private_data = NULL;
  673. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  674. return -EFAULT;
  675. result = ib_ucm_alloc_data(&param.private_data, cmd.data, cmd.len);
  676. if (result)
  677. return result;
  678. param.qp_num = cmd.qpn;
  679. param.starting_psn = cmd.psn;
  680. param.private_data_len = cmd.len;
  681. param.responder_resources = cmd.responder_resources;
  682. param.initiator_depth = cmd.initiator_depth;
  683. param.target_ack_delay = cmd.target_ack_delay;
  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. param.pkey = cmd.pkey;
  859. ctx = ib_ucm_ctx_get(file, cmd.id);
  860. if (!IS_ERR(ctx)) {
  861. result = ib_send_cm_sidr_req(ctx->cm_id, &param);
  862. ib_ucm_ctx_put(ctx);
  863. } else
  864. result = PTR_ERR(ctx);
  865. done:
  866. kfree(param.private_data);
  867. kfree(param.path);
  868. return result;
  869. }
  870. static ssize_t ib_ucm_send_sidr_rep(struct ib_ucm_file *file,
  871. const char __user *inbuf,
  872. int in_len, int out_len)
  873. {
  874. struct ib_cm_sidr_rep_param param;
  875. struct ib_ucm_sidr_rep cmd;
  876. struct ib_ucm_context *ctx;
  877. int result;
  878. param.info = NULL;
  879. if (copy_from_user(&cmd, inbuf, sizeof(cmd)))
  880. return -EFAULT;
  881. result = ib_ucm_alloc_data(&param.private_data,
  882. cmd.data, cmd.data_len);
  883. if (result)
  884. goto done;
  885. result = ib_ucm_alloc_data(&param.info, cmd.info, cmd.info_len);
  886. if (result)
  887. goto done;
  888. param.qp_num = cmd.qpn;
  889. param.qkey = cmd.qkey;
  890. param.status = cmd.status;
  891. param.info_length = cmd.info_len;
  892. param.private_data_len = cmd.data_len;
  893. ctx = ib_ucm_ctx_get(file, cmd.id);
  894. if (!IS_ERR(ctx)) {
  895. result = ib_send_cm_sidr_rep(ctx->cm_id, &param);
  896. ib_ucm_ctx_put(ctx);
  897. } else
  898. result = PTR_ERR(ctx);
  899. done:
  900. kfree(param.private_data);
  901. kfree(param.info);
  902. return result;
  903. }
  904. static ssize_t (*ucm_cmd_table[])(struct ib_ucm_file *file,
  905. const char __user *inbuf,
  906. int in_len, int out_len) = {
  907. [IB_USER_CM_CMD_CREATE_ID] = ib_ucm_create_id,
  908. [IB_USER_CM_CMD_DESTROY_ID] = ib_ucm_destroy_id,
  909. [IB_USER_CM_CMD_ATTR_ID] = ib_ucm_attr_id,
  910. [IB_USER_CM_CMD_LISTEN] = ib_ucm_listen,
  911. [IB_USER_CM_CMD_ESTABLISH] = ib_ucm_establish,
  912. [IB_USER_CM_CMD_SEND_REQ] = ib_ucm_send_req,
  913. [IB_USER_CM_CMD_SEND_REP] = ib_ucm_send_rep,
  914. [IB_USER_CM_CMD_SEND_RTU] = ib_ucm_send_rtu,
  915. [IB_USER_CM_CMD_SEND_DREQ] = ib_ucm_send_dreq,
  916. [IB_USER_CM_CMD_SEND_DREP] = ib_ucm_send_drep,
  917. [IB_USER_CM_CMD_SEND_REJ] = ib_ucm_send_rej,
  918. [IB_USER_CM_CMD_SEND_MRA] = ib_ucm_send_mra,
  919. [IB_USER_CM_CMD_SEND_LAP] = ib_ucm_send_lap,
  920. [IB_USER_CM_CMD_SEND_APR] = ib_ucm_send_apr,
  921. [IB_USER_CM_CMD_SEND_SIDR_REQ] = ib_ucm_send_sidr_req,
  922. [IB_USER_CM_CMD_SEND_SIDR_REP] = ib_ucm_send_sidr_rep,
  923. [IB_USER_CM_CMD_EVENT] = ib_ucm_event,
  924. [IB_USER_CM_CMD_INIT_QP_ATTR] = ib_ucm_init_qp_attr,
  925. };
  926. static ssize_t ib_ucm_write(struct file *filp, const char __user *buf,
  927. size_t len, loff_t *pos)
  928. {
  929. struct ib_ucm_file *file = filp->private_data;
  930. struct ib_ucm_cmd_hdr hdr;
  931. ssize_t result;
  932. if (len < sizeof(hdr))
  933. return -EINVAL;
  934. if (copy_from_user(&hdr, buf, sizeof(hdr)))
  935. return -EFAULT;
  936. if (hdr.cmd < 0 || hdr.cmd >= ARRAY_SIZE(ucm_cmd_table))
  937. return -EINVAL;
  938. if (hdr.in + sizeof(hdr) > len)
  939. return -EINVAL;
  940. result = ucm_cmd_table[hdr.cmd](file, buf + sizeof(hdr),
  941. hdr.in, hdr.out);
  942. if (!result)
  943. result = len;
  944. return result;
  945. }
  946. static unsigned int ib_ucm_poll(struct file *filp,
  947. struct poll_table_struct *wait)
  948. {
  949. struct ib_ucm_file *file = filp->private_data;
  950. unsigned int mask = 0;
  951. poll_wait(filp, &file->poll_wait, wait);
  952. if (!list_empty(&file->events))
  953. mask = POLLIN | POLLRDNORM;
  954. return mask;
  955. }
  956. static int ib_ucm_open(struct inode *inode, struct file *filp)
  957. {
  958. struct ib_ucm_file *file;
  959. file = kmalloc(sizeof(*file), GFP_KERNEL);
  960. if (!file)
  961. return -ENOMEM;
  962. INIT_LIST_HEAD(&file->events);
  963. INIT_LIST_HEAD(&file->ctxs);
  964. init_waitqueue_head(&file->poll_wait);
  965. init_MUTEX(&file->mutex);
  966. filp->private_data = file;
  967. file->filp = filp;
  968. file->device = container_of(inode->i_cdev, struct ib_ucm_device, dev);
  969. return 0;
  970. }
  971. static int ib_ucm_close(struct inode *inode, struct file *filp)
  972. {
  973. struct ib_ucm_file *file = filp->private_data;
  974. struct ib_ucm_context *ctx;
  975. down(&file->mutex);
  976. while (!list_empty(&file->ctxs)) {
  977. ctx = list_entry(file->ctxs.next,
  978. struct ib_ucm_context, file_list);
  979. up(&file->mutex);
  980. mutex_lock(&ctx_id_mutex);
  981. idr_remove(&ctx_id_table, ctx->id);
  982. mutex_unlock(&ctx_id_mutex);
  983. ib_destroy_cm_id(ctx->cm_id);
  984. ib_ucm_cleanup_events(ctx);
  985. kfree(ctx);
  986. down(&file->mutex);
  987. }
  988. up(&file->mutex);
  989. kfree(file);
  990. return 0;
  991. }
  992. static void ib_ucm_release_class_dev(struct class_device *class_dev)
  993. {
  994. struct ib_ucm_device *dev;
  995. dev = container_of(class_dev, struct ib_ucm_device, class_dev);
  996. cdev_del(&dev->dev);
  997. clear_bit(dev->devnum, dev_map);
  998. kfree(dev);
  999. }
  1000. static struct file_operations ucm_fops = {
  1001. .owner = THIS_MODULE,
  1002. .open = ib_ucm_open,
  1003. .release = ib_ucm_close,
  1004. .write = ib_ucm_write,
  1005. .poll = ib_ucm_poll,
  1006. };
  1007. static struct class ucm_class = {
  1008. .name = "infiniband_cm",
  1009. .release = ib_ucm_release_class_dev
  1010. };
  1011. static ssize_t show_ibdev(struct class_device *class_dev, char *buf)
  1012. {
  1013. struct ib_ucm_device *dev;
  1014. dev = container_of(class_dev, struct ib_ucm_device, class_dev);
  1015. return sprintf(buf, "%s\n", dev->ib_dev->name);
  1016. }
  1017. static CLASS_DEVICE_ATTR(ibdev, S_IRUGO, show_ibdev, NULL);
  1018. static void ib_ucm_add_one(struct ib_device *device)
  1019. {
  1020. struct ib_ucm_device *ucm_dev;
  1021. if (!device->alloc_ucontext)
  1022. return;
  1023. ucm_dev = kzalloc(sizeof *ucm_dev, GFP_KERNEL);
  1024. if (!ucm_dev)
  1025. return;
  1026. ucm_dev->ib_dev = device;
  1027. ucm_dev->devnum = find_first_zero_bit(dev_map, IB_UCM_MAX_DEVICES);
  1028. if (ucm_dev->devnum >= IB_UCM_MAX_DEVICES)
  1029. goto err;
  1030. set_bit(ucm_dev->devnum, dev_map);
  1031. cdev_init(&ucm_dev->dev, &ucm_fops);
  1032. ucm_dev->dev.owner = THIS_MODULE;
  1033. kobject_set_name(&ucm_dev->dev.kobj, "ucm%d", ucm_dev->devnum);
  1034. if (cdev_add(&ucm_dev->dev, IB_UCM_BASE_DEV + ucm_dev->devnum, 1))
  1035. goto err;
  1036. ucm_dev->class_dev.class = &ucm_class;
  1037. ucm_dev->class_dev.dev = device->dma_device;
  1038. ucm_dev->class_dev.devt = ucm_dev->dev.dev;
  1039. snprintf(ucm_dev->class_dev.class_id, BUS_ID_SIZE, "ucm%d",
  1040. ucm_dev->devnum);
  1041. if (class_device_register(&ucm_dev->class_dev))
  1042. goto err_cdev;
  1043. if (class_device_create_file(&ucm_dev->class_dev,
  1044. &class_device_attr_ibdev))
  1045. goto err_class;
  1046. ib_set_client_data(device, &ucm_client, ucm_dev);
  1047. return;
  1048. err_class:
  1049. class_device_unregister(&ucm_dev->class_dev);
  1050. err_cdev:
  1051. cdev_del(&ucm_dev->dev);
  1052. clear_bit(ucm_dev->devnum, dev_map);
  1053. err:
  1054. kfree(ucm_dev);
  1055. return;
  1056. }
  1057. static void ib_ucm_remove_one(struct ib_device *device)
  1058. {
  1059. struct ib_ucm_device *ucm_dev = ib_get_client_data(device, &ucm_client);
  1060. if (!ucm_dev)
  1061. return;
  1062. class_device_unregister(&ucm_dev->class_dev);
  1063. }
  1064. static ssize_t show_abi_version(struct class *class, char *buf)
  1065. {
  1066. return sprintf(buf, "%d\n", IB_USER_CM_ABI_VERSION);
  1067. }
  1068. static CLASS_ATTR(abi_version, S_IRUGO, show_abi_version, NULL);
  1069. static int __init ib_ucm_init(void)
  1070. {
  1071. int ret;
  1072. ret = register_chrdev_region(IB_UCM_BASE_DEV, IB_UCM_MAX_DEVICES,
  1073. "infiniband_cm");
  1074. if (ret) {
  1075. printk(KERN_ERR "ucm: couldn't register device number\n");
  1076. goto err;
  1077. }
  1078. ret = class_register(&ucm_class);
  1079. if (ret) {
  1080. printk(KERN_ERR "ucm: couldn't create class infiniband_cm\n");
  1081. goto err_chrdev;
  1082. }
  1083. ret = class_create_file(&ucm_class, &class_attr_abi_version);
  1084. if (ret) {
  1085. printk(KERN_ERR "ucm: couldn't create abi_version attribute\n");
  1086. goto err_class;
  1087. }
  1088. ret = ib_register_client(&ucm_client);
  1089. if (ret) {
  1090. printk(KERN_ERR "ucm: couldn't register client\n");
  1091. goto err_class;
  1092. }
  1093. return 0;
  1094. err_class:
  1095. class_unregister(&ucm_class);
  1096. err_chrdev:
  1097. unregister_chrdev_region(IB_UCM_BASE_DEV, IB_UCM_MAX_DEVICES);
  1098. err:
  1099. return ret;
  1100. }
  1101. static void __exit ib_ucm_cleanup(void)
  1102. {
  1103. ib_unregister_client(&ucm_client);
  1104. class_unregister(&ucm_class);
  1105. unregister_chrdev_region(IB_UCM_BASE_DEV, IB_UCM_MAX_DEVICES);
  1106. idr_destroy(&ctx_id_table);
  1107. }
  1108. module_init(ib_ucm_init);
  1109. module_exit(ib_ucm_cleanup);