ucm.c 37 KB

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