c2_cm.c 9.8 KB

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