c2_cm.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. /*
  2. * Copyright (c) 2005 Ammasso, Inc. All rights reserved.
  3. * Copyright (c) 2005 Open Grid Computing, Inc. 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. */
  34. #include <linux/slab.h>
  35. #include "c2.h"
  36. #include "c2_wr.h"
  37. #include "c2_vq.h"
  38. #include <rdma/iw_cm.h>
  39. int c2_llp_connect(struct iw_cm_id *cm_id, struct iw_cm_conn_param *iw_param)
  40. {
  41. struct c2_dev *c2dev = to_c2dev(cm_id->device);
  42. struct ib_qp *ibqp;
  43. struct c2_qp *qp;
  44. struct c2wr_qp_connect_req *wr; /* variable size needs a malloc. */
  45. struct c2_vq_req *vq_req;
  46. int err;
  47. ibqp = c2_get_qp(cm_id->device, iw_param->qpn);
  48. if (!ibqp)
  49. return -EINVAL;
  50. qp = to_c2qp(ibqp);
  51. /* Associate QP <--> CM_ID */
  52. cm_id->provider_data = qp;
  53. cm_id->add_ref(cm_id);
  54. qp->cm_id = cm_id;
  55. /*
  56. * only support the max private_data length
  57. */
  58. if (iw_param->private_data_len > C2_MAX_PRIVATE_DATA_SIZE) {
  59. err = -EINVAL;
  60. goto bail0;
  61. }
  62. /*
  63. * Set the rdma read limits
  64. */
  65. err = c2_qp_set_read_limits(c2dev, qp, iw_param->ord, iw_param->ird);
  66. if (err)
  67. goto bail0;
  68. /*
  69. * Create and send a WR_QP_CONNECT...
  70. */
  71. wr = kmalloc(c2dev->req_vq.msg_size, GFP_KERNEL);
  72. if (!wr) {
  73. err = -ENOMEM;
  74. goto bail0;
  75. }
  76. vq_req = vq_req_alloc(c2dev);
  77. if (!vq_req) {
  78. err = -ENOMEM;
  79. goto bail1;
  80. }
  81. c2_wr_set_id(wr, CCWR_QP_CONNECT);
  82. wr->hdr.context = 0;
  83. wr->rnic_handle = c2dev->adapter_handle;
  84. wr->qp_handle = qp->adapter_handle;
  85. wr->remote_addr = cm_id->remote_addr.sin_addr.s_addr;
  86. wr->remote_port = cm_id->remote_addr.sin_port;
  87. /*
  88. * Move any private data from the callers's buf into
  89. * the WR.
  90. */
  91. if (iw_param->private_data) {
  92. wr->private_data_length =
  93. cpu_to_be32(iw_param->private_data_len);
  94. memcpy(&wr->private_data[0], iw_param->private_data,
  95. iw_param->private_data_len);
  96. } else
  97. wr->private_data_length = 0;
  98. /*
  99. * Send WR to adapter. NOTE: There is no synch reply from
  100. * the adapter.
  101. */
  102. err = vq_send_wr(c2dev, (union c2wr *) wr);
  103. vq_req_free(c2dev, vq_req);
  104. bail1:
  105. kfree(wr);
  106. bail0:
  107. if (err) {
  108. /*
  109. * If we fail, release reference on QP and
  110. * disassociate QP from CM_ID
  111. */
  112. cm_id->provider_data = NULL;
  113. qp->cm_id = NULL;
  114. cm_id->rem_ref(cm_id);
  115. }
  116. return err;
  117. }
  118. int c2_llp_service_create(struct iw_cm_id *cm_id, int backlog)
  119. {
  120. struct c2_dev *c2dev;
  121. struct c2wr_ep_listen_create_req wr;
  122. struct c2wr_ep_listen_create_rep *reply;
  123. struct c2_vq_req *vq_req;
  124. int err;
  125. c2dev = to_c2dev(cm_id->device);
  126. if (c2dev == NULL)
  127. return -EINVAL;
  128. /*
  129. * Allocate verbs request.
  130. */
  131. vq_req = vq_req_alloc(c2dev);
  132. if (!vq_req)
  133. return -ENOMEM;
  134. /*
  135. * Build the WR
  136. */
  137. c2_wr_set_id(&wr, CCWR_EP_LISTEN_CREATE);
  138. wr.hdr.context = (u64) (unsigned long) vq_req;
  139. wr.rnic_handle = c2dev->adapter_handle;
  140. wr.local_addr = cm_id->local_addr.sin_addr.s_addr;
  141. wr.local_port = cm_id->local_addr.sin_port;
  142. wr.backlog = cpu_to_be32(backlog);
  143. wr.user_context = (u64) (unsigned long) cm_id;
  144. /*
  145. * Reference the request struct. Dereferenced in the int handler.
  146. */
  147. vq_req_get(c2dev, vq_req);
  148. /*
  149. * Send WR to adapter
  150. */
  151. err = vq_send_wr(c2dev, (union c2wr *) & wr);
  152. if (err) {
  153. vq_req_put(c2dev, vq_req);
  154. goto bail0;
  155. }
  156. /*
  157. * Wait for reply from adapter
  158. */
  159. err = vq_wait_for_reply(c2dev, vq_req);
  160. if (err)
  161. goto bail0;
  162. /*
  163. * Process reply
  164. */
  165. reply =
  166. (struct c2wr_ep_listen_create_rep *) (unsigned long) vq_req->reply_msg;
  167. if (!reply) {
  168. err = -ENOMEM;
  169. goto bail1;
  170. }
  171. if ((err = c2_errno(reply)) != 0)
  172. goto bail1;
  173. /*
  174. * Keep the adapter handle. Used in subsequent destroy
  175. */
  176. cm_id->provider_data = (void*)(unsigned long) reply->ep_handle;
  177. /*
  178. * free vq stuff
  179. */
  180. vq_repbuf_free(c2dev, reply);
  181. vq_req_free(c2dev, vq_req);
  182. return 0;
  183. bail1:
  184. vq_repbuf_free(c2dev, reply);
  185. bail0:
  186. vq_req_free(c2dev, vq_req);
  187. return err;
  188. }
  189. int c2_llp_service_destroy(struct iw_cm_id *cm_id)
  190. {
  191. struct c2_dev *c2dev;
  192. struct c2wr_ep_listen_destroy_req wr;
  193. struct c2wr_ep_listen_destroy_rep *reply;
  194. struct c2_vq_req *vq_req;
  195. int err;
  196. c2dev = to_c2dev(cm_id->device);
  197. if (c2dev == NULL)
  198. return -EINVAL;
  199. /*
  200. * Allocate verbs request.
  201. */
  202. vq_req = vq_req_alloc(c2dev);
  203. if (!vq_req)
  204. return -ENOMEM;
  205. /*
  206. * Build the WR
  207. */
  208. c2_wr_set_id(&wr, CCWR_EP_LISTEN_DESTROY);
  209. wr.hdr.context = (unsigned long) vq_req;
  210. wr.rnic_handle = c2dev->adapter_handle;
  211. wr.ep_handle = (u32)(unsigned long)cm_id->provider_data;
  212. /*
  213. * reference the request struct. dereferenced in the int handler.
  214. */
  215. vq_req_get(c2dev, vq_req);
  216. /*
  217. * Send WR to adapter
  218. */
  219. err = vq_send_wr(c2dev, (union c2wr *) & wr);
  220. if (err) {
  221. vq_req_put(c2dev, vq_req);
  222. goto bail0;
  223. }
  224. /*
  225. * Wait for reply from adapter
  226. */
  227. err = vq_wait_for_reply(c2dev, vq_req);
  228. if (err)
  229. goto bail0;
  230. /*
  231. * Process reply
  232. */
  233. reply=(struct c2wr_ep_listen_destroy_rep *)(unsigned long)vq_req->reply_msg;
  234. if (!reply) {
  235. err = -ENOMEM;
  236. goto bail0;
  237. }
  238. if ((err = c2_errno(reply)) != 0)
  239. goto bail1;
  240. bail1:
  241. vq_repbuf_free(c2dev, reply);
  242. bail0:
  243. vq_req_free(c2dev, vq_req);
  244. return err;
  245. }
  246. int c2_llp_accept(struct iw_cm_id *cm_id, struct iw_cm_conn_param *iw_param)
  247. {
  248. struct c2_dev *c2dev = to_c2dev(cm_id->device);
  249. struct c2_qp *qp;
  250. struct ib_qp *ibqp;
  251. struct c2wr_cr_accept_req *wr; /* variable length WR */
  252. struct c2_vq_req *vq_req;
  253. struct c2wr_cr_accept_rep *reply; /* VQ Reply msg ptr. */
  254. int err;
  255. ibqp = c2_get_qp(cm_id->device, iw_param->qpn);
  256. if (!ibqp)
  257. return -EINVAL;
  258. qp = to_c2qp(ibqp);
  259. /* Set the RDMA read limits */
  260. err = c2_qp_set_read_limits(c2dev, qp, iw_param->ord, iw_param->ird);
  261. if (err)
  262. goto bail0;
  263. /* Allocate verbs request. */
  264. vq_req = vq_req_alloc(c2dev);
  265. if (!vq_req) {
  266. err = -ENOMEM;
  267. goto bail0;
  268. }
  269. vq_req->qp = qp;
  270. vq_req->cm_id = cm_id;
  271. vq_req->event = IW_CM_EVENT_ESTABLISHED;
  272. wr = kmalloc(c2dev->req_vq.msg_size, GFP_KERNEL);
  273. if (!wr) {
  274. err = -ENOMEM;
  275. goto bail1;
  276. }
  277. /* Build the WR */
  278. c2_wr_set_id(wr, CCWR_CR_ACCEPT);
  279. wr->hdr.context = (unsigned long) vq_req;
  280. wr->rnic_handle = c2dev->adapter_handle;
  281. wr->ep_handle = (u32) (unsigned long) cm_id->provider_data;
  282. wr->qp_handle = qp->adapter_handle;
  283. /* Replace the cr_handle with the QP after accept */
  284. cm_id->provider_data = qp;
  285. cm_id->add_ref(cm_id);
  286. qp->cm_id = cm_id;
  287. cm_id->provider_data = qp;
  288. /* Validate private_data length */
  289. if (iw_param->private_data_len > C2_MAX_PRIVATE_DATA_SIZE) {
  290. err = -EINVAL;
  291. goto bail1;
  292. }
  293. if (iw_param->private_data) {
  294. wr->private_data_length = cpu_to_be32(iw_param->private_data_len);
  295. memcpy(&wr->private_data[0],
  296. iw_param->private_data, iw_param->private_data_len);
  297. } else
  298. wr->private_data_length = 0;
  299. /* Reference the request struct. Dereferenced in the int handler. */
  300. vq_req_get(c2dev, vq_req);
  301. /* Send WR to adapter */
  302. err = vq_send_wr(c2dev, (union c2wr *) wr);
  303. if (err) {
  304. vq_req_put(c2dev, vq_req);
  305. goto bail1;
  306. }
  307. /* Wait for reply from adapter */
  308. err = vq_wait_for_reply(c2dev, vq_req);
  309. if (err)
  310. goto bail1;
  311. /* Check that reply is present */
  312. reply = (struct c2wr_cr_accept_rep *) (unsigned long) vq_req->reply_msg;
  313. if (!reply) {
  314. err = -ENOMEM;
  315. goto bail1;
  316. }
  317. err = c2_errno(reply);
  318. vq_repbuf_free(c2dev, reply);
  319. if (!err)
  320. c2_set_qp_state(qp, C2_QP_STATE_RTS);
  321. bail1:
  322. kfree(wr);
  323. vq_req_free(c2dev, vq_req);
  324. bail0:
  325. if (err) {
  326. /*
  327. * If we fail, release reference on QP and
  328. * disassociate QP from CM_ID
  329. */
  330. cm_id->provider_data = NULL;
  331. qp->cm_id = NULL;
  332. cm_id->rem_ref(cm_id);
  333. }
  334. return err;
  335. }
  336. int c2_llp_reject(struct iw_cm_id *cm_id, const void *pdata, u8 pdata_len)
  337. {
  338. struct c2_dev *c2dev;
  339. struct c2wr_cr_reject_req wr;
  340. struct c2_vq_req *vq_req;
  341. struct c2wr_cr_reject_rep *reply;
  342. int err;
  343. c2dev = to_c2dev(cm_id->device);
  344. /*
  345. * Allocate verbs request.
  346. */
  347. vq_req = vq_req_alloc(c2dev);
  348. if (!vq_req)
  349. return -ENOMEM;
  350. /*
  351. * Build the WR
  352. */
  353. c2_wr_set_id(&wr, CCWR_CR_REJECT);
  354. wr.hdr.context = (unsigned long) vq_req;
  355. wr.rnic_handle = c2dev->adapter_handle;
  356. wr.ep_handle = (u32) (unsigned long) cm_id->provider_data;
  357. /*
  358. * reference the request struct. dereferenced in the int handler.
  359. */
  360. vq_req_get(c2dev, vq_req);
  361. /*
  362. * Send WR to adapter
  363. */
  364. err = vq_send_wr(c2dev, (union c2wr *) & wr);
  365. if (err) {
  366. vq_req_put(c2dev, vq_req);
  367. goto bail0;
  368. }
  369. /*
  370. * Wait for reply from adapter
  371. */
  372. err = vq_wait_for_reply(c2dev, vq_req);
  373. if (err)
  374. goto bail0;
  375. /*
  376. * Process reply
  377. */
  378. reply = (struct c2wr_cr_reject_rep *) (unsigned long)
  379. vq_req->reply_msg;
  380. if (!reply) {
  381. err = -ENOMEM;
  382. goto bail0;
  383. }
  384. err = c2_errno(reply);
  385. /*
  386. * free vq stuff
  387. */
  388. vq_repbuf_free(c2dev, reply);
  389. bail0:
  390. vq_req_free(c2dev, vq_req);
  391. return err;
  392. }