c2_cm.c 10.0 KB

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