ucm.c 33 KB

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