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