ucm.c 33 KB

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