ib_cm.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759
  1. /*
  2. * Copyright (c) 2006 Oracle. All rights reserved.
  3. *
  4. * This software is available to you under a choice of one of two
  5. * licenses. You may choose to be licensed under the terms of the GNU
  6. * General Public License (GPL) Version 2, available from the file
  7. * COPYING in the main directory of this source tree, or the
  8. * OpenIB.org BSD license below:
  9. *
  10. * Redistribution and use in source and binary forms, with or
  11. * without modification, are permitted provided that the following
  12. * conditions are met:
  13. *
  14. * - Redistributions of source code must retain the above
  15. * copyright notice, this list of conditions and the following
  16. * disclaimer.
  17. *
  18. * - Redistributions in binary form must reproduce the above
  19. * copyright notice, this list of conditions and the following
  20. * disclaimer in the documentation and/or other materials
  21. * provided with the distribution.
  22. *
  23. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  27. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  28. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  29. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  30. * SOFTWARE.
  31. *
  32. */
  33. #include <linux/kernel.h>
  34. #include <linux/in.h>
  35. #include <linux/vmalloc.h>
  36. #include "rds.h"
  37. #include "ib.h"
  38. /*
  39. * Set the selected protocol version
  40. */
  41. static void rds_ib_set_protocol(struct rds_connection *conn, unsigned int version)
  42. {
  43. conn->c_version = version;
  44. }
  45. /*
  46. * Set up flow control
  47. */
  48. static void rds_ib_set_flow_control(struct rds_connection *conn, u32 credits)
  49. {
  50. struct rds_ib_connection *ic = conn->c_transport_data;
  51. if (rds_ib_sysctl_flow_control && credits != 0) {
  52. /* We're doing flow control */
  53. ic->i_flowctl = 1;
  54. rds_ib_send_add_credits(conn, credits);
  55. } else {
  56. ic->i_flowctl = 0;
  57. }
  58. }
  59. /*
  60. * Tune RNR behavior. Without flow control, we use a rather
  61. * low timeout, but not the absolute minimum - this should
  62. * be tunable.
  63. *
  64. * We already set the RNR retry count to 7 (which is the
  65. * smallest infinite number :-) above.
  66. * If flow control is off, we want to change this back to 0
  67. * so that we learn quickly when our credit accounting is
  68. * buggy.
  69. *
  70. * Caller passes in a qp_attr pointer - don't waste stack spacv
  71. * by allocation this twice.
  72. */
  73. static void
  74. rds_ib_tune_rnr(struct rds_ib_connection *ic, struct ib_qp_attr *attr)
  75. {
  76. int ret;
  77. attr->min_rnr_timer = IB_RNR_TIMER_000_32;
  78. ret = ib_modify_qp(ic->i_cm_id->qp, attr, IB_QP_MIN_RNR_TIMER);
  79. if (ret)
  80. printk(KERN_NOTICE "ib_modify_qp(IB_QP_MIN_RNR_TIMER): err=%d\n", -ret);
  81. }
  82. /*
  83. * Connection established.
  84. * We get here for both outgoing and incoming connection.
  85. */
  86. void rds_ib_cm_connect_complete(struct rds_connection *conn, struct rdma_cm_event *event)
  87. {
  88. const struct rds_ib_connect_private *dp = NULL;
  89. struct rds_ib_connection *ic = conn->c_transport_data;
  90. struct rds_ib_device *rds_ibdev;
  91. struct ib_qp_attr qp_attr;
  92. int err;
  93. if (event->param.conn.private_data_len >= sizeof(*dp)) {
  94. dp = event->param.conn.private_data;
  95. /* make sure it isn't empty data */
  96. if (dp->dp_protocol_major) {
  97. rds_ib_set_protocol(conn,
  98. RDS_PROTOCOL(dp->dp_protocol_major,
  99. dp->dp_protocol_minor));
  100. rds_ib_set_flow_control(conn, be32_to_cpu(dp->dp_credit));
  101. }
  102. }
  103. printk(KERN_NOTICE "RDS/IB: connected to %pI4 version %u.%u%s\n",
  104. &conn->c_faddr,
  105. RDS_PROTOCOL_MAJOR(conn->c_version),
  106. RDS_PROTOCOL_MINOR(conn->c_version),
  107. ic->i_flowctl ? ", flow control" : "");
  108. /*
  109. * Init rings and fill recv. this needs to wait until protocol negotiation
  110. * is complete, since ring layout is different from 3.0 to 3.1.
  111. */
  112. rds_ib_send_init_ring(ic);
  113. rds_ib_recv_init_ring(ic);
  114. /* Post receive buffers - as a side effect, this will update
  115. * the posted credit count. */
  116. rds_ib_recv_refill(conn, GFP_KERNEL, GFP_HIGHUSER, 1);
  117. /* Tune RNR behavior */
  118. rds_ib_tune_rnr(ic, &qp_attr);
  119. qp_attr.qp_state = IB_QPS_RTS;
  120. err = ib_modify_qp(ic->i_cm_id->qp, &qp_attr, IB_QP_STATE);
  121. if (err)
  122. printk(KERN_NOTICE "ib_modify_qp(IB_QP_STATE, RTS): err=%d\n", err);
  123. /* update ib_device with this local ipaddr & conn */
  124. rds_ibdev = ib_get_client_data(ic->i_cm_id->device, &rds_ib_client);
  125. err = rds_ib_update_ipaddr(rds_ibdev, conn->c_laddr);
  126. if (err)
  127. printk(KERN_ERR "rds_ib_update_ipaddr failed (%d)\n", err);
  128. rds_ib_add_conn(rds_ibdev, conn);
  129. /* If the peer gave us the last packet it saw, process this as if
  130. * we had received a regular ACK. */
  131. if (dp && dp->dp_ack_seq)
  132. rds_send_drop_acked(conn, be64_to_cpu(dp->dp_ack_seq), NULL);
  133. rds_connect_complete(conn);
  134. }
  135. static void rds_ib_cm_fill_conn_param(struct rds_connection *conn,
  136. struct rdma_conn_param *conn_param,
  137. struct rds_ib_connect_private *dp,
  138. u32 protocol_version)
  139. {
  140. memset(conn_param, 0, sizeof(struct rdma_conn_param));
  141. /* XXX tune these? */
  142. conn_param->responder_resources = 1;
  143. conn_param->initiator_depth = 1;
  144. conn_param->retry_count = min_t(unsigned int, rds_ib_retry_count, 7);
  145. conn_param->rnr_retry_count = 7;
  146. if (dp) {
  147. struct rds_ib_connection *ic = conn->c_transport_data;
  148. memset(dp, 0, sizeof(*dp));
  149. dp->dp_saddr = conn->c_laddr;
  150. dp->dp_daddr = conn->c_faddr;
  151. dp->dp_protocol_major = RDS_PROTOCOL_MAJOR(protocol_version);
  152. dp->dp_protocol_minor = RDS_PROTOCOL_MINOR(protocol_version);
  153. dp->dp_protocol_minor_mask = cpu_to_be16(RDS_IB_SUPPORTED_PROTOCOLS);
  154. dp->dp_ack_seq = rds_ib_piggyb_ack(ic);
  155. /* Advertise flow control */
  156. if (ic->i_flowctl) {
  157. unsigned int credits;
  158. credits = IB_GET_POST_CREDITS(atomic_read(&ic->i_credits));
  159. dp->dp_credit = cpu_to_be32(credits);
  160. atomic_sub(IB_SET_POST_CREDITS(credits), &ic->i_credits);
  161. }
  162. conn_param->private_data = dp;
  163. conn_param->private_data_len = sizeof(*dp);
  164. }
  165. }
  166. static void rds_ib_cq_event_handler(struct ib_event *event, void *data)
  167. {
  168. rdsdebug("event %u data %p\n", event->event, data);
  169. }
  170. static void rds_ib_qp_event_handler(struct ib_event *event, void *data)
  171. {
  172. struct rds_connection *conn = data;
  173. struct rds_ib_connection *ic = conn->c_transport_data;
  174. rdsdebug("conn %p ic %p event %u\n", conn, ic, event->event);
  175. switch (event->event) {
  176. case IB_EVENT_COMM_EST:
  177. rdma_notify(ic->i_cm_id, IB_EVENT_COMM_EST);
  178. break;
  179. default:
  180. rds_ib_conn_error(conn, "RDS/IB: Fatal QP Event %u "
  181. "- connection %pI4->%pI4, reconnecting\n",
  182. event->event, &conn->c_laddr, &conn->c_faddr);
  183. break;
  184. }
  185. }
  186. /*
  187. * This needs to be very careful to not leave IS_ERR pointers around for
  188. * cleanup to trip over.
  189. */
  190. static int rds_ib_setup_qp(struct rds_connection *conn)
  191. {
  192. struct rds_ib_connection *ic = conn->c_transport_data;
  193. struct ib_device *dev = ic->i_cm_id->device;
  194. struct ib_qp_init_attr attr;
  195. struct rds_ib_device *rds_ibdev;
  196. int ret;
  197. /* rds_ib_add_one creates a rds_ib_device object per IB device,
  198. * and allocates a protection domain, memory range and FMR pool
  199. * for each. If that fails for any reason, it will not register
  200. * the rds_ibdev at all.
  201. */
  202. rds_ibdev = ib_get_client_data(dev, &rds_ib_client);
  203. if (rds_ibdev == NULL) {
  204. if (printk_ratelimit())
  205. printk(KERN_NOTICE "RDS/IB: No client_data for device %s\n",
  206. dev->name);
  207. return -EOPNOTSUPP;
  208. }
  209. if (rds_ibdev->max_wrs < ic->i_send_ring.w_nr + 1)
  210. rds_ib_ring_resize(&ic->i_send_ring, rds_ibdev->max_wrs - 1);
  211. if (rds_ibdev->max_wrs < ic->i_recv_ring.w_nr + 1)
  212. rds_ib_ring_resize(&ic->i_recv_ring, rds_ibdev->max_wrs - 1);
  213. /* Protection domain and memory range */
  214. ic->i_pd = rds_ibdev->pd;
  215. ic->i_mr = rds_ibdev->mr;
  216. ic->i_send_cq = ib_create_cq(dev, rds_ib_send_cq_comp_handler,
  217. rds_ib_cq_event_handler, conn,
  218. ic->i_send_ring.w_nr + 1, 0);
  219. if (IS_ERR(ic->i_send_cq)) {
  220. ret = PTR_ERR(ic->i_send_cq);
  221. ic->i_send_cq = NULL;
  222. rdsdebug("ib_create_cq send failed: %d\n", ret);
  223. goto out;
  224. }
  225. ic->i_recv_cq = ib_create_cq(dev, rds_ib_recv_cq_comp_handler,
  226. rds_ib_cq_event_handler, conn,
  227. ic->i_recv_ring.w_nr, 0);
  228. if (IS_ERR(ic->i_recv_cq)) {
  229. ret = PTR_ERR(ic->i_recv_cq);
  230. ic->i_recv_cq = NULL;
  231. rdsdebug("ib_create_cq recv failed: %d\n", ret);
  232. goto out;
  233. }
  234. ret = ib_req_notify_cq(ic->i_send_cq, IB_CQ_NEXT_COMP);
  235. if (ret) {
  236. rdsdebug("ib_req_notify_cq send failed: %d\n", ret);
  237. goto out;
  238. }
  239. ret = ib_req_notify_cq(ic->i_recv_cq, IB_CQ_SOLICITED);
  240. if (ret) {
  241. rdsdebug("ib_req_notify_cq recv failed: %d\n", ret);
  242. goto out;
  243. }
  244. /* XXX negotiate max send/recv with remote? */
  245. memset(&attr, 0, sizeof(attr));
  246. attr.event_handler = rds_ib_qp_event_handler;
  247. attr.qp_context = conn;
  248. /* + 1 to allow for the single ack message */
  249. attr.cap.max_send_wr = ic->i_send_ring.w_nr + 1;
  250. attr.cap.max_recv_wr = ic->i_recv_ring.w_nr + 1;
  251. attr.cap.max_send_sge = rds_ibdev->max_sge;
  252. attr.cap.max_recv_sge = RDS_IB_RECV_SGE;
  253. attr.sq_sig_type = IB_SIGNAL_REQ_WR;
  254. attr.qp_type = IB_QPT_RC;
  255. attr.send_cq = ic->i_send_cq;
  256. attr.recv_cq = ic->i_recv_cq;
  257. /*
  258. * XXX this can fail if max_*_wr is too large? Are we supposed
  259. * to back off until we get a value that the hardware can support?
  260. */
  261. ret = rdma_create_qp(ic->i_cm_id, ic->i_pd, &attr);
  262. if (ret) {
  263. rdsdebug("rdma_create_qp failed: %d\n", ret);
  264. goto out;
  265. }
  266. ic->i_send_hdrs = ib_dma_alloc_coherent(dev,
  267. ic->i_send_ring.w_nr *
  268. sizeof(struct rds_header),
  269. &ic->i_send_hdrs_dma, GFP_KERNEL);
  270. if (ic->i_send_hdrs == NULL) {
  271. ret = -ENOMEM;
  272. rdsdebug("ib_dma_alloc_coherent send failed\n");
  273. goto out;
  274. }
  275. ic->i_recv_hdrs = ib_dma_alloc_coherent(dev,
  276. ic->i_recv_ring.w_nr *
  277. sizeof(struct rds_header),
  278. &ic->i_recv_hdrs_dma, GFP_KERNEL);
  279. if (ic->i_recv_hdrs == NULL) {
  280. ret = -ENOMEM;
  281. rdsdebug("ib_dma_alloc_coherent recv failed\n");
  282. goto out;
  283. }
  284. ic->i_ack = ib_dma_alloc_coherent(dev, sizeof(struct rds_header),
  285. &ic->i_ack_dma, GFP_KERNEL);
  286. if (ic->i_ack == NULL) {
  287. ret = -ENOMEM;
  288. rdsdebug("ib_dma_alloc_coherent ack failed\n");
  289. goto out;
  290. }
  291. ic->i_sends = vmalloc(ic->i_send_ring.w_nr * sizeof(struct rds_ib_send_work));
  292. if (ic->i_sends == NULL) {
  293. ret = -ENOMEM;
  294. rdsdebug("send allocation failed\n");
  295. goto out;
  296. }
  297. memset(ic->i_sends, 0, ic->i_send_ring.w_nr * sizeof(struct rds_ib_send_work));
  298. ic->i_recvs = vmalloc(ic->i_recv_ring.w_nr * sizeof(struct rds_ib_recv_work));
  299. if (ic->i_recvs == NULL) {
  300. ret = -ENOMEM;
  301. rdsdebug("recv allocation failed\n");
  302. goto out;
  303. }
  304. memset(ic->i_recvs, 0, ic->i_recv_ring.w_nr * sizeof(struct rds_ib_recv_work));
  305. rds_ib_recv_init_ack(ic);
  306. rdsdebug("conn %p pd %p mr %p cq %p %p\n", conn, ic->i_pd, ic->i_mr,
  307. ic->i_send_cq, ic->i_recv_cq);
  308. out:
  309. return ret;
  310. }
  311. static u32 rds_ib_protocol_compatible(struct rdma_cm_event *event)
  312. {
  313. const struct rds_ib_connect_private *dp = event->param.conn.private_data;
  314. u16 common;
  315. u32 version = 0;
  316. /*
  317. * rdma_cm private data is odd - when there is any private data in the
  318. * request, we will be given a pretty large buffer without telling us the
  319. * original size. The only way to tell the difference is by looking at
  320. * the contents, which are initialized to zero.
  321. * If the protocol version fields aren't set, this is a connection attempt
  322. * from an older version. This could could be 3.0 or 2.0 - we can't tell.
  323. * We really should have changed this for OFED 1.3 :-(
  324. */
  325. /* Be paranoid. RDS always has privdata */
  326. if (!event->param.conn.private_data_len) {
  327. printk(KERN_NOTICE "RDS incoming connection has no private data, "
  328. "rejecting\n");
  329. return 0;
  330. }
  331. /* Even if len is crap *now* I still want to check it. -ASG */
  332. if (event->param.conn.private_data_len < sizeof (*dp)
  333. || dp->dp_protocol_major == 0)
  334. return RDS_PROTOCOL_3_0;
  335. common = be16_to_cpu(dp->dp_protocol_minor_mask) & RDS_IB_SUPPORTED_PROTOCOLS;
  336. if (dp->dp_protocol_major == 3 && common) {
  337. version = RDS_PROTOCOL_3_0;
  338. while ((common >>= 1) != 0)
  339. version++;
  340. } else if (printk_ratelimit()) {
  341. printk(KERN_NOTICE "RDS: Connection from %pI4 using "
  342. "incompatible protocol version %u.%u\n",
  343. &dp->dp_saddr,
  344. dp->dp_protocol_major,
  345. dp->dp_protocol_minor);
  346. }
  347. return version;
  348. }
  349. int rds_ib_cm_handle_connect(struct rdma_cm_id *cm_id,
  350. struct rdma_cm_event *event)
  351. {
  352. __be64 lguid = cm_id->route.path_rec->sgid.global.interface_id;
  353. __be64 fguid = cm_id->route.path_rec->dgid.global.interface_id;
  354. const struct rds_ib_connect_private *dp = event->param.conn.private_data;
  355. struct rds_ib_connect_private dp_rep;
  356. struct rds_connection *conn = NULL;
  357. struct rds_ib_connection *ic = NULL;
  358. struct rdma_conn_param conn_param;
  359. u32 version;
  360. int err, destroy = 1;
  361. /* Check whether the remote protocol version matches ours. */
  362. version = rds_ib_protocol_compatible(event);
  363. if (!version)
  364. goto out;
  365. rdsdebug("saddr %pI4 daddr %pI4 RDSv%u.%u lguid 0x%llx fguid "
  366. "0x%llx\n", &dp->dp_saddr, &dp->dp_daddr,
  367. RDS_PROTOCOL_MAJOR(version), RDS_PROTOCOL_MINOR(version),
  368. (unsigned long long)be64_to_cpu(lguid),
  369. (unsigned long long)be64_to_cpu(fguid));
  370. conn = rds_conn_create(dp->dp_daddr, dp->dp_saddr, &rds_ib_transport,
  371. GFP_KERNEL);
  372. if (IS_ERR(conn)) {
  373. rdsdebug("rds_conn_create failed (%ld)\n", PTR_ERR(conn));
  374. conn = NULL;
  375. goto out;
  376. }
  377. /*
  378. * The connection request may occur while the
  379. * previous connection exist, e.g. in case of failover.
  380. * But as connections may be initiated simultaneously
  381. * by both hosts, we have a random backoff mechanism -
  382. * see the comment above rds_queue_reconnect()
  383. */
  384. mutex_lock(&conn->c_cm_lock);
  385. if (!rds_conn_transition(conn, RDS_CONN_DOWN, RDS_CONN_CONNECTING)) {
  386. if (rds_conn_state(conn) == RDS_CONN_UP) {
  387. rdsdebug("incoming connect while connecting\n");
  388. rds_conn_drop(conn);
  389. rds_ib_stats_inc(s_ib_listen_closed_stale);
  390. } else
  391. if (rds_conn_state(conn) == RDS_CONN_CONNECTING) {
  392. /* Wait and see - our connect may still be succeeding */
  393. rds_ib_stats_inc(s_ib_connect_raced);
  394. }
  395. mutex_unlock(&conn->c_cm_lock);
  396. goto out;
  397. }
  398. ic = conn->c_transport_data;
  399. rds_ib_set_protocol(conn, version);
  400. rds_ib_set_flow_control(conn, be32_to_cpu(dp->dp_credit));
  401. /* If the peer gave us the last packet it saw, process this as if
  402. * we had received a regular ACK. */
  403. if (dp->dp_ack_seq)
  404. rds_send_drop_acked(conn, be64_to_cpu(dp->dp_ack_seq), NULL);
  405. BUG_ON(cm_id->context);
  406. BUG_ON(ic->i_cm_id);
  407. ic->i_cm_id = cm_id;
  408. cm_id->context = conn;
  409. /* We got halfway through setting up the ib_connection, if we
  410. * fail now, we have to take the long route out of this mess. */
  411. destroy = 0;
  412. err = rds_ib_setup_qp(conn);
  413. if (err) {
  414. rds_ib_conn_error(conn, "rds_ib_setup_qp failed (%d)\n", err);
  415. goto out;
  416. }
  417. rds_ib_cm_fill_conn_param(conn, &conn_param, &dp_rep, version);
  418. /* rdma_accept() calls rdma_reject() internally if it fails */
  419. err = rdma_accept(cm_id, &conn_param);
  420. mutex_unlock(&conn->c_cm_lock);
  421. if (err) {
  422. rds_ib_conn_error(conn, "rdma_accept failed (%d)\n", err);
  423. goto out;
  424. }
  425. return 0;
  426. out:
  427. rdma_reject(cm_id, NULL, 0);
  428. return destroy;
  429. }
  430. int rds_ib_cm_initiate_connect(struct rdma_cm_id *cm_id)
  431. {
  432. struct rds_connection *conn = cm_id->context;
  433. struct rds_ib_connection *ic = conn->c_transport_data;
  434. struct rdma_conn_param conn_param;
  435. struct rds_ib_connect_private dp;
  436. int ret;
  437. /* If the peer doesn't do protocol negotiation, we must
  438. * default to RDSv3.0 */
  439. rds_ib_set_protocol(conn, RDS_PROTOCOL_3_0);
  440. ic->i_flowctl = rds_ib_sysctl_flow_control; /* advertise flow control */
  441. ret = rds_ib_setup_qp(conn);
  442. if (ret) {
  443. rds_ib_conn_error(conn, "rds_ib_setup_qp failed (%d)\n", ret);
  444. goto out;
  445. }
  446. rds_ib_cm_fill_conn_param(conn, &conn_param, &dp, RDS_PROTOCOL_VERSION);
  447. ret = rdma_connect(cm_id, &conn_param);
  448. if (ret)
  449. rds_ib_conn_error(conn, "rdma_connect failed (%d)\n", ret);
  450. out:
  451. /* Beware - returning non-zero tells the rdma_cm to destroy
  452. * the cm_id. We should certainly not do it as long as we still
  453. * "own" the cm_id. */
  454. if (ret) {
  455. if (ic->i_cm_id == cm_id)
  456. ret = 0;
  457. }
  458. return ret;
  459. }
  460. int rds_ib_conn_connect(struct rds_connection *conn)
  461. {
  462. struct rds_ib_connection *ic = conn->c_transport_data;
  463. struct sockaddr_in src, dest;
  464. int ret;
  465. /* XXX I wonder what affect the port space has */
  466. /* delegate cm event handler to rdma_transport */
  467. ic->i_cm_id = rdma_create_id(rds_rdma_cm_event_handler, conn,
  468. RDMA_PS_TCP);
  469. if (IS_ERR(ic->i_cm_id)) {
  470. ret = PTR_ERR(ic->i_cm_id);
  471. ic->i_cm_id = NULL;
  472. rdsdebug("rdma_create_id() failed: %d\n", ret);
  473. goto out;
  474. }
  475. rdsdebug("created cm id %p for conn %p\n", ic->i_cm_id, conn);
  476. src.sin_family = AF_INET;
  477. src.sin_addr.s_addr = (__force u32)conn->c_laddr;
  478. src.sin_port = (__force u16)htons(0);
  479. dest.sin_family = AF_INET;
  480. dest.sin_addr.s_addr = (__force u32)conn->c_faddr;
  481. dest.sin_port = (__force u16)htons(RDS_PORT);
  482. ret = rdma_resolve_addr(ic->i_cm_id, (struct sockaddr *)&src,
  483. (struct sockaddr *)&dest,
  484. RDS_RDMA_RESOLVE_TIMEOUT_MS);
  485. if (ret) {
  486. rdsdebug("addr resolve failed for cm id %p: %d\n", ic->i_cm_id,
  487. ret);
  488. rdma_destroy_id(ic->i_cm_id);
  489. ic->i_cm_id = NULL;
  490. }
  491. out:
  492. return ret;
  493. }
  494. /*
  495. * This is so careful about only cleaning up resources that were built up
  496. * so that it can be called at any point during startup. In fact it
  497. * can be called multiple times for a given connection.
  498. */
  499. void rds_ib_conn_shutdown(struct rds_connection *conn)
  500. {
  501. struct rds_ib_connection *ic = conn->c_transport_data;
  502. int err = 0;
  503. rdsdebug("cm %p pd %p cq %p %p qp %p\n", ic->i_cm_id,
  504. ic->i_pd, ic->i_send_cq, ic->i_recv_cq,
  505. ic->i_cm_id ? ic->i_cm_id->qp : NULL);
  506. if (ic->i_cm_id) {
  507. struct ib_device *dev = ic->i_cm_id->device;
  508. rdsdebug("disconnecting cm %p\n", ic->i_cm_id);
  509. err = rdma_disconnect(ic->i_cm_id);
  510. if (err) {
  511. /* Actually this may happen quite frequently, when
  512. * an outgoing connect raced with an incoming connect.
  513. */
  514. rdsdebug("failed to disconnect, cm: %p err %d\n",
  515. ic->i_cm_id, err);
  516. }
  517. wait_event(rds_ib_ring_empty_wait,
  518. rds_ib_ring_empty(&ic->i_send_ring) &&
  519. rds_ib_ring_empty(&ic->i_recv_ring));
  520. if (ic->i_send_hdrs)
  521. ib_dma_free_coherent(dev,
  522. ic->i_send_ring.w_nr *
  523. sizeof(struct rds_header),
  524. ic->i_send_hdrs,
  525. ic->i_send_hdrs_dma);
  526. if (ic->i_recv_hdrs)
  527. ib_dma_free_coherent(dev,
  528. ic->i_recv_ring.w_nr *
  529. sizeof(struct rds_header),
  530. ic->i_recv_hdrs,
  531. ic->i_recv_hdrs_dma);
  532. if (ic->i_ack)
  533. ib_dma_free_coherent(dev, sizeof(struct rds_header),
  534. ic->i_ack, ic->i_ack_dma);
  535. if (ic->i_sends)
  536. rds_ib_send_clear_ring(ic);
  537. if (ic->i_recvs)
  538. rds_ib_recv_clear_ring(ic);
  539. if (ic->i_cm_id->qp)
  540. rdma_destroy_qp(ic->i_cm_id);
  541. if (ic->i_send_cq)
  542. ib_destroy_cq(ic->i_send_cq);
  543. if (ic->i_recv_cq)
  544. ib_destroy_cq(ic->i_recv_cq);
  545. rdma_destroy_id(ic->i_cm_id);
  546. /*
  547. * Move connection back to the nodev list.
  548. */
  549. if (ic->rds_ibdev)
  550. rds_ib_remove_conn(ic->rds_ibdev, conn);
  551. ic->i_cm_id = NULL;
  552. ic->i_pd = NULL;
  553. ic->i_mr = NULL;
  554. ic->i_send_cq = NULL;
  555. ic->i_recv_cq = NULL;
  556. ic->i_send_hdrs = NULL;
  557. ic->i_recv_hdrs = NULL;
  558. ic->i_ack = NULL;
  559. }
  560. BUG_ON(ic->rds_ibdev);
  561. /* Clear pending transmit */
  562. if (ic->i_rm) {
  563. rds_message_put(ic->i_rm);
  564. ic->i_rm = NULL;
  565. }
  566. /* Clear the ACK state */
  567. clear_bit(IB_ACK_IN_FLIGHT, &ic->i_ack_flags);
  568. #ifdef KERNEL_HAS_ATOMIC64
  569. atomic64_set(&ic->i_ack_next, 0);
  570. #else
  571. ic->i_ack_next = 0;
  572. #endif
  573. ic->i_ack_recv = 0;
  574. /* Clear flow control state */
  575. ic->i_flowctl = 0;
  576. atomic_set(&ic->i_credits, 0);
  577. rds_ib_ring_init(&ic->i_send_ring, rds_ib_sysctl_max_send_wr);
  578. rds_ib_ring_init(&ic->i_recv_ring, rds_ib_sysctl_max_recv_wr);
  579. if (ic->i_ibinc) {
  580. rds_inc_put(&ic->i_ibinc->ii_inc);
  581. ic->i_ibinc = NULL;
  582. }
  583. vfree(ic->i_sends);
  584. ic->i_sends = NULL;
  585. vfree(ic->i_recvs);
  586. ic->i_recvs = NULL;
  587. }
  588. int rds_ib_conn_alloc(struct rds_connection *conn, gfp_t gfp)
  589. {
  590. struct rds_ib_connection *ic;
  591. unsigned long flags;
  592. /* XXX too lazy? */
  593. ic = kzalloc(sizeof(struct rds_ib_connection), GFP_KERNEL);
  594. if (ic == NULL)
  595. return -ENOMEM;
  596. INIT_LIST_HEAD(&ic->ib_node);
  597. mutex_init(&ic->i_recv_mutex);
  598. #ifndef KERNEL_HAS_ATOMIC64
  599. spin_lock_init(&ic->i_ack_lock);
  600. #endif
  601. /*
  602. * rds_ib_conn_shutdown() waits for these to be emptied so they
  603. * must be initialized before it can be called.
  604. */
  605. rds_ib_ring_init(&ic->i_send_ring, rds_ib_sysctl_max_send_wr);
  606. rds_ib_ring_init(&ic->i_recv_ring, rds_ib_sysctl_max_recv_wr);
  607. ic->conn = conn;
  608. conn->c_transport_data = ic;
  609. spin_lock_irqsave(&ib_nodev_conns_lock, flags);
  610. list_add_tail(&ic->ib_node, &ib_nodev_conns);
  611. spin_unlock_irqrestore(&ib_nodev_conns_lock, flags);
  612. rdsdebug("conn %p conn ic %p\n", conn, conn->c_transport_data);
  613. return 0;
  614. }
  615. /*
  616. * Free a connection. Connection must be shut down and not set for reconnect.
  617. */
  618. void rds_ib_conn_free(void *arg)
  619. {
  620. struct rds_ib_connection *ic = arg;
  621. spinlock_t *lock_ptr;
  622. rdsdebug("ic %p\n", ic);
  623. /*
  624. * Conn is either on a dev's list or on the nodev list.
  625. * A race with shutdown() or connect() would cause problems
  626. * (since rds_ibdev would change) but that should never happen.
  627. */
  628. lock_ptr = ic->rds_ibdev ? &ic->rds_ibdev->spinlock : &ib_nodev_conns_lock;
  629. spin_lock_irq(lock_ptr);
  630. list_del(&ic->ib_node);
  631. spin_unlock_irq(lock_ptr);
  632. kfree(ic);
  633. }
  634. /*
  635. * An error occurred on the connection
  636. */
  637. void
  638. __rds_ib_conn_error(struct rds_connection *conn, const char *fmt, ...)
  639. {
  640. va_list ap;
  641. rds_conn_drop(conn);
  642. va_start(ap, fmt);
  643. vprintk(fmt, ap);
  644. va_end(ap);
  645. }