ib_cm.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737
  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) {
  94. dp = event->param.conn.private_data;
  95. rds_ib_set_protocol(conn,
  96. RDS_PROTOCOL(dp->dp_protocol_major,
  97. dp->dp_protocol_minor));
  98. rds_ib_set_flow_control(conn, be32_to_cpu(dp->dp_credit));
  99. }
  100. printk(KERN_NOTICE "RDS/IB: connected to %pI4 version %u.%u%s\n",
  101. &conn->c_laddr,
  102. RDS_PROTOCOL_MAJOR(conn->c_version),
  103. RDS_PROTOCOL_MINOR(conn->c_version),
  104. ic->i_flowctl ? ", flow control" : "");
  105. /* Tune RNR behavior */
  106. rds_ib_tune_rnr(ic, &qp_attr);
  107. qp_attr.qp_state = IB_QPS_RTS;
  108. err = ib_modify_qp(ic->i_cm_id->qp, &qp_attr, IB_QP_STATE);
  109. if (err)
  110. printk(KERN_NOTICE "ib_modify_qp(IB_QP_STATE, RTS): err=%d\n", err);
  111. /* update ib_device with this local ipaddr & conn */
  112. rds_ibdev = ib_get_client_data(ic->i_cm_id->device, &rds_ib_client);
  113. err = rds_ib_update_ipaddr(rds_ibdev, conn->c_laddr);
  114. if (err)
  115. printk(KERN_ERR "rds_ib_update_ipaddr failed (%d)\n", err);
  116. rds_ib_add_conn(rds_ibdev, conn);
  117. /* If the peer gave us the last packet it saw, process this as if
  118. * we had received a regular ACK. */
  119. if (dp && dp->dp_ack_seq)
  120. rds_send_drop_acked(conn, be64_to_cpu(dp->dp_ack_seq), NULL);
  121. rds_connect_complete(conn);
  122. }
  123. static void rds_ib_cm_fill_conn_param(struct rds_connection *conn,
  124. struct rdma_conn_param *conn_param,
  125. struct rds_ib_connect_private *dp,
  126. u32 protocol_version)
  127. {
  128. memset(conn_param, 0, sizeof(struct rdma_conn_param));
  129. /* XXX tune these? */
  130. conn_param->responder_resources = 1;
  131. conn_param->initiator_depth = 1;
  132. conn_param->retry_count = 7;
  133. conn_param->rnr_retry_count = 7;
  134. if (dp) {
  135. struct rds_ib_connection *ic = conn->c_transport_data;
  136. memset(dp, 0, sizeof(*dp));
  137. dp->dp_saddr = conn->c_laddr;
  138. dp->dp_daddr = conn->c_faddr;
  139. dp->dp_protocol_major = RDS_PROTOCOL_MAJOR(protocol_version);
  140. dp->dp_protocol_minor = RDS_PROTOCOL_MINOR(protocol_version);
  141. dp->dp_protocol_minor_mask = cpu_to_be16(RDS_IB_SUPPORTED_PROTOCOLS);
  142. dp->dp_ack_seq = rds_ib_piggyb_ack(ic);
  143. /* Advertise flow control */
  144. if (ic->i_flowctl) {
  145. unsigned int credits;
  146. credits = IB_GET_POST_CREDITS(atomic_read(&ic->i_credits));
  147. dp->dp_credit = cpu_to_be32(credits);
  148. atomic_sub(IB_SET_POST_CREDITS(credits), &ic->i_credits);
  149. }
  150. conn_param->private_data = dp;
  151. conn_param->private_data_len = sizeof(*dp);
  152. }
  153. }
  154. static void rds_ib_cq_event_handler(struct ib_event *event, void *data)
  155. {
  156. rdsdebug("event %u data %p\n", event->event, data);
  157. }
  158. static void rds_ib_qp_event_handler(struct ib_event *event, void *data)
  159. {
  160. struct rds_connection *conn = data;
  161. struct rds_ib_connection *ic = conn->c_transport_data;
  162. rdsdebug("conn %p ic %p event %u\n", conn, ic, event->event);
  163. switch (event->event) {
  164. case IB_EVENT_COMM_EST:
  165. rdma_notify(ic->i_cm_id, IB_EVENT_COMM_EST);
  166. break;
  167. default:
  168. printk(KERN_WARNING "RDS/ib: unhandled QP event %u "
  169. "on connection to %pI4\n", event->event,
  170. &conn->c_faddr);
  171. break;
  172. }
  173. }
  174. /*
  175. * This needs to be very careful to not leave IS_ERR pointers around for
  176. * cleanup to trip over.
  177. */
  178. static int rds_ib_setup_qp(struct rds_connection *conn)
  179. {
  180. struct rds_ib_connection *ic = conn->c_transport_data;
  181. struct ib_device *dev = ic->i_cm_id->device;
  182. struct ib_qp_init_attr attr;
  183. struct rds_ib_device *rds_ibdev;
  184. int ret;
  185. /* rds_ib_add_one creates a rds_ib_device object per IB device,
  186. * and allocates a protection domain, memory range and FMR pool
  187. * for each. If that fails for any reason, it will not register
  188. * the rds_ibdev at all.
  189. */
  190. rds_ibdev = ib_get_client_data(dev, &rds_ib_client);
  191. if (rds_ibdev == NULL) {
  192. if (printk_ratelimit())
  193. printk(KERN_NOTICE "RDS/IB: No client_data for device %s\n",
  194. dev->name);
  195. return -EOPNOTSUPP;
  196. }
  197. if (rds_ibdev->max_wrs < ic->i_send_ring.w_nr + 1)
  198. rds_ib_ring_resize(&ic->i_send_ring, rds_ibdev->max_wrs - 1);
  199. if (rds_ibdev->max_wrs < ic->i_recv_ring.w_nr + 1)
  200. rds_ib_ring_resize(&ic->i_recv_ring, rds_ibdev->max_wrs - 1);
  201. /* Protection domain and memory range */
  202. ic->i_pd = rds_ibdev->pd;
  203. ic->i_mr = rds_ibdev->mr;
  204. ic->i_send_cq = ib_create_cq(dev, rds_ib_send_cq_comp_handler,
  205. rds_ib_cq_event_handler, conn,
  206. ic->i_send_ring.w_nr + 1, 0);
  207. if (IS_ERR(ic->i_send_cq)) {
  208. ret = PTR_ERR(ic->i_send_cq);
  209. ic->i_send_cq = NULL;
  210. rdsdebug("ib_create_cq send failed: %d\n", ret);
  211. goto out;
  212. }
  213. ic->i_recv_cq = ib_create_cq(dev, rds_ib_recv_cq_comp_handler,
  214. rds_ib_cq_event_handler, conn,
  215. ic->i_recv_ring.w_nr, 0);
  216. if (IS_ERR(ic->i_recv_cq)) {
  217. ret = PTR_ERR(ic->i_recv_cq);
  218. ic->i_recv_cq = NULL;
  219. rdsdebug("ib_create_cq recv failed: %d\n", ret);
  220. goto out;
  221. }
  222. ret = ib_req_notify_cq(ic->i_send_cq, IB_CQ_NEXT_COMP);
  223. if (ret) {
  224. rdsdebug("ib_req_notify_cq send failed: %d\n", ret);
  225. goto out;
  226. }
  227. ret = ib_req_notify_cq(ic->i_recv_cq, IB_CQ_SOLICITED);
  228. if (ret) {
  229. rdsdebug("ib_req_notify_cq recv failed: %d\n", ret);
  230. goto out;
  231. }
  232. /* XXX negotiate max send/recv with remote? */
  233. memset(&attr, 0, sizeof(attr));
  234. attr.event_handler = rds_ib_qp_event_handler;
  235. attr.qp_context = conn;
  236. /* + 1 to allow for the single ack message */
  237. attr.cap.max_send_wr = ic->i_send_ring.w_nr + 1;
  238. attr.cap.max_recv_wr = ic->i_recv_ring.w_nr + 1;
  239. attr.cap.max_send_sge = rds_ibdev->max_sge;
  240. attr.cap.max_recv_sge = RDS_IB_RECV_SGE;
  241. attr.sq_sig_type = IB_SIGNAL_REQ_WR;
  242. attr.qp_type = IB_QPT_RC;
  243. attr.send_cq = ic->i_send_cq;
  244. attr.recv_cq = ic->i_recv_cq;
  245. /*
  246. * XXX this can fail if max_*_wr is too large? Are we supposed
  247. * to back off until we get a value that the hardware can support?
  248. */
  249. ret = rdma_create_qp(ic->i_cm_id, ic->i_pd, &attr);
  250. if (ret) {
  251. rdsdebug("rdma_create_qp failed: %d\n", ret);
  252. goto out;
  253. }
  254. ic->i_send_hdrs = ib_dma_alloc_coherent(dev,
  255. ic->i_send_ring.w_nr *
  256. sizeof(struct rds_header),
  257. &ic->i_send_hdrs_dma, GFP_KERNEL);
  258. if (ic->i_send_hdrs == NULL) {
  259. ret = -ENOMEM;
  260. rdsdebug("ib_dma_alloc_coherent send failed\n");
  261. goto out;
  262. }
  263. ic->i_recv_hdrs = ib_dma_alloc_coherent(dev,
  264. ic->i_recv_ring.w_nr *
  265. sizeof(struct rds_header),
  266. &ic->i_recv_hdrs_dma, GFP_KERNEL);
  267. if (ic->i_recv_hdrs == NULL) {
  268. ret = -ENOMEM;
  269. rdsdebug("ib_dma_alloc_coherent recv failed\n");
  270. goto out;
  271. }
  272. ic->i_ack = ib_dma_alloc_coherent(dev, sizeof(struct rds_header),
  273. &ic->i_ack_dma, GFP_KERNEL);
  274. if (ic->i_ack == NULL) {
  275. ret = -ENOMEM;
  276. rdsdebug("ib_dma_alloc_coherent ack failed\n");
  277. goto out;
  278. }
  279. ic->i_sends = vmalloc(ic->i_send_ring.w_nr * sizeof(struct rds_ib_send_work));
  280. if (ic->i_sends == NULL) {
  281. ret = -ENOMEM;
  282. rdsdebug("send allocation failed\n");
  283. goto out;
  284. }
  285. rds_ib_send_init_ring(ic);
  286. ic->i_recvs = vmalloc(ic->i_recv_ring.w_nr * sizeof(struct rds_ib_recv_work));
  287. if (ic->i_recvs == NULL) {
  288. ret = -ENOMEM;
  289. rdsdebug("recv allocation failed\n");
  290. goto out;
  291. }
  292. rds_ib_recv_init_ring(ic);
  293. rds_ib_recv_init_ack(ic);
  294. /* Post receive buffers - as a side effect, this will update
  295. * the posted credit count. */
  296. rds_ib_recv_refill(conn, GFP_KERNEL, GFP_HIGHUSER, 1);
  297. rdsdebug("conn %p pd %p mr %p cq %p %p\n", conn, ic->i_pd, ic->i_mr,
  298. ic->i_send_cq, ic->i_recv_cq);
  299. out:
  300. return ret;
  301. }
  302. static u32 rds_ib_protocol_compatible(const struct rds_ib_connect_private *dp)
  303. {
  304. u16 common;
  305. u32 version = 0;
  306. /* rdma_cm private data is odd - when there is any private data in the
  307. * request, we will be given a pretty large buffer without telling us the
  308. * original size. The only way to tell the difference is by looking at
  309. * the contents, which are initialized to zero.
  310. * If the protocol version fields aren't set, this is a connection attempt
  311. * from an older version. This could could be 3.0 or 2.0 - we can't tell.
  312. * We really should have changed this for OFED 1.3 :-( */
  313. if (dp->dp_protocol_major == 0)
  314. return RDS_PROTOCOL_3_0;
  315. common = be16_to_cpu(dp->dp_protocol_minor_mask) & RDS_IB_SUPPORTED_PROTOCOLS;
  316. if (dp->dp_protocol_major == 3 && common) {
  317. version = RDS_PROTOCOL_3_0;
  318. while ((common >>= 1) != 0)
  319. version++;
  320. } else if (printk_ratelimit()) {
  321. printk(KERN_NOTICE "RDS: Connection from %pI4 using "
  322. "incompatible protocol version %u.%u\n",
  323. &dp->dp_saddr,
  324. dp->dp_protocol_major,
  325. dp->dp_protocol_minor);
  326. }
  327. return version;
  328. }
  329. int rds_ib_cm_handle_connect(struct rdma_cm_id *cm_id,
  330. struct rdma_cm_event *event)
  331. {
  332. __be64 lguid = cm_id->route.path_rec->sgid.global.interface_id;
  333. __be64 fguid = cm_id->route.path_rec->dgid.global.interface_id;
  334. const struct rds_ib_connect_private *dp = event->param.conn.private_data;
  335. struct rds_ib_connect_private dp_rep;
  336. struct rds_connection *conn = NULL;
  337. struct rds_ib_connection *ic = NULL;
  338. struct rdma_conn_param conn_param;
  339. u32 version;
  340. int err, destroy = 1;
  341. /* Check whether the remote protocol version matches ours. */
  342. version = rds_ib_protocol_compatible(dp);
  343. if (!version)
  344. goto out;
  345. rdsdebug("saddr %pI4 daddr %pI4 RDSv%u.%u lguid 0x%llx fguid "
  346. "0x%llx\n", &dp->dp_saddr, &dp->dp_daddr,
  347. RDS_PROTOCOL_MAJOR(version), RDS_PROTOCOL_MINOR(version),
  348. (unsigned long long)be64_to_cpu(lguid),
  349. (unsigned long long)be64_to_cpu(fguid));
  350. conn = rds_conn_create(dp->dp_daddr, dp->dp_saddr, &rds_ib_transport,
  351. GFP_KERNEL);
  352. if (IS_ERR(conn)) {
  353. rdsdebug("rds_conn_create failed (%ld)\n", PTR_ERR(conn));
  354. conn = NULL;
  355. goto out;
  356. }
  357. /*
  358. * The connection request may occur while the
  359. * previous connection exist, e.g. in case of failover.
  360. * But as connections may be initiated simultaneously
  361. * by both hosts, we have a random backoff mechanism -
  362. * see the comment above rds_queue_reconnect()
  363. */
  364. mutex_lock(&conn->c_cm_lock);
  365. if (!rds_conn_transition(conn, RDS_CONN_DOWN, RDS_CONN_CONNECTING)) {
  366. if (rds_conn_state(conn) == RDS_CONN_UP) {
  367. rdsdebug("incoming connect while connecting\n");
  368. rds_conn_drop(conn);
  369. rds_ib_stats_inc(s_ib_listen_closed_stale);
  370. } else
  371. if (rds_conn_state(conn) == RDS_CONN_CONNECTING) {
  372. /* Wait and see - our connect may still be succeeding */
  373. rds_ib_stats_inc(s_ib_connect_raced);
  374. }
  375. mutex_unlock(&conn->c_cm_lock);
  376. goto out;
  377. }
  378. ic = conn->c_transport_data;
  379. rds_ib_set_protocol(conn, version);
  380. rds_ib_set_flow_control(conn, be32_to_cpu(dp->dp_credit));
  381. /* If the peer gave us the last packet it saw, process this as if
  382. * we had received a regular ACK. */
  383. if (dp->dp_ack_seq)
  384. rds_send_drop_acked(conn, be64_to_cpu(dp->dp_ack_seq), NULL);
  385. BUG_ON(cm_id->context);
  386. BUG_ON(ic->i_cm_id);
  387. ic->i_cm_id = cm_id;
  388. cm_id->context = conn;
  389. /* We got halfway through setting up the ib_connection, if we
  390. * fail now, we have to take the long route out of this mess. */
  391. destroy = 0;
  392. err = rds_ib_setup_qp(conn);
  393. if (err) {
  394. rds_ib_conn_error(conn, "rds_ib_setup_qp failed (%d)\n", err);
  395. goto out;
  396. }
  397. rds_ib_cm_fill_conn_param(conn, &conn_param, &dp_rep, version);
  398. /* rdma_accept() calls rdma_reject() internally if it fails */
  399. err = rdma_accept(cm_id, &conn_param);
  400. mutex_unlock(&conn->c_cm_lock);
  401. if (err) {
  402. rds_ib_conn_error(conn, "rdma_accept failed (%d)\n", err);
  403. goto out;
  404. }
  405. return 0;
  406. out:
  407. rdma_reject(cm_id, NULL, 0);
  408. return destroy;
  409. }
  410. int rds_ib_cm_initiate_connect(struct rdma_cm_id *cm_id)
  411. {
  412. struct rds_connection *conn = cm_id->context;
  413. struct rds_ib_connection *ic = conn->c_transport_data;
  414. struct rdma_conn_param conn_param;
  415. struct rds_ib_connect_private dp;
  416. int ret;
  417. /* If the peer doesn't do protocol negotiation, we must
  418. * default to RDSv3.0 */
  419. rds_ib_set_protocol(conn, RDS_PROTOCOL_3_0);
  420. ic->i_flowctl = rds_ib_sysctl_flow_control; /* advertise flow control */
  421. ret = rds_ib_setup_qp(conn);
  422. if (ret) {
  423. rds_ib_conn_error(conn, "rds_ib_setup_qp failed (%d)\n", ret);
  424. goto out;
  425. }
  426. rds_ib_cm_fill_conn_param(conn, &conn_param, &dp, RDS_PROTOCOL_VERSION);
  427. ret = rdma_connect(cm_id, &conn_param);
  428. if (ret)
  429. rds_ib_conn_error(conn, "rdma_connect failed (%d)\n", ret);
  430. out:
  431. /* Beware - returning non-zero tells the rdma_cm to destroy
  432. * the cm_id. We should certainly not do it as long as we still
  433. * "own" the cm_id. */
  434. if (ret) {
  435. if (ic->i_cm_id == cm_id)
  436. ret = 0;
  437. }
  438. return ret;
  439. }
  440. int rds_ib_conn_connect(struct rds_connection *conn)
  441. {
  442. struct rds_ib_connection *ic = conn->c_transport_data;
  443. struct sockaddr_in src, dest;
  444. int ret;
  445. /* XXX I wonder what affect the port space has */
  446. /* delegate cm event handler to rdma_transport */
  447. ic->i_cm_id = rdma_create_id(rds_rdma_cm_event_handler, conn,
  448. RDMA_PS_TCP);
  449. if (IS_ERR(ic->i_cm_id)) {
  450. ret = PTR_ERR(ic->i_cm_id);
  451. ic->i_cm_id = NULL;
  452. rdsdebug("rdma_create_id() failed: %d\n", ret);
  453. goto out;
  454. }
  455. rdsdebug("created cm id %p for conn %p\n", ic->i_cm_id, conn);
  456. src.sin_family = AF_INET;
  457. src.sin_addr.s_addr = (__force u32)conn->c_laddr;
  458. src.sin_port = (__force u16)htons(0);
  459. dest.sin_family = AF_INET;
  460. dest.sin_addr.s_addr = (__force u32)conn->c_faddr;
  461. dest.sin_port = (__force u16)htons(RDS_PORT);
  462. ret = rdma_resolve_addr(ic->i_cm_id, (struct sockaddr *)&src,
  463. (struct sockaddr *)&dest,
  464. RDS_RDMA_RESOLVE_TIMEOUT_MS);
  465. if (ret) {
  466. rdsdebug("addr resolve failed for cm id %p: %d\n", ic->i_cm_id,
  467. ret);
  468. rdma_destroy_id(ic->i_cm_id);
  469. ic->i_cm_id = NULL;
  470. }
  471. out:
  472. return ret;
  473. }
  474. /*
  475. * This is so careful about only cleaning up resources that were built up
  476. * so that it can be called at any point during startup. In fact it
  477. * can be called multiple times for a given connection.
  478. */
  479. void rds_ib_conn_shutdown(struct rds_connection *conn)
  480. {
  481. struct rds_ib_connection *ic = conn->c_transport_data;
  482. int err = 0;
  483. rdsdebug("cm %p pd %p cq %p %p qp %p\n", ic->i_cm_id,
  484. ic->i_pd, ic->i_send_cq, ic->i_recv_cq,
  485. ic->i_cm_id ? ic->i_cm_id->qp : NULL);
  486. if (ic->i_cm_id) {
  487. struct ib_device *dev = ic->i_cm_id->device;
  488. rdsdebug("disconnecting cm %p\n", ic->i_cm_id);
  489. err = rdma_disconnect(ic->i_cm_id);
  490. if (err) {
  491. /* Actually this may happen quite frequently, when
  492. * an outgoing connect raced with an incoming connect.
  493. */
  494. rdsdebug("failed to disconnect, cm: %p err %d\n",
  495. ic->i_cm_id, err);
  496. }
  497. wait_event(rds_ib_ring_empty_wait,
  498. rds_ib_ring_empty(&ic->i_send_ring) &&
  499. rds_ib_ring_empty(&ic->i_recv_ring));
  500. if (ic->i_send_hdrs)
  501. ib_dma_free_coherent(dev,
  502. ic->i_send_ring.w_nr *
  503. sizeof(struct rds_header),
  504. ic->i_send_hdrs,
  505. ic->i_send_hdrs_dma);
  506. if (ic->i_recv_hdrs)
  507. ib_dma_free_coherent(dev,
  508. ic->i_recv_ring.w_nr *
  509. sizeof(struct rds_header),
  510. ic->i_recv_hdrs,
  511. ic->i_recv_hdrs_dma);
  512. if (ic->i_ack)
  513. ib_dma_free_coherent(dev, sizeof(struct rds_header),
  514. ic->i_ack, ic->i_ack_dma);
  515. if (ic->i_sends)
  516. rds_ib_send_clear_ring(ic);
  517. if (ic->i_recvs)
  518. rds_ib_recv_clear_ring(ic);
  519. if (ic->i_cm_id->qp)
  520. rdma_destroy_qp(ic->i_cm_id);
  521. if (ic->i_send_cq)
  522. ib_destroy_cq(ic->i_send_cq);
  523. if (ic->i_recv_cq)
  524. ib_destroy_cq(ic->i_recv_cq);
  525. rdma_destroy_id(ic->i_cm_id);
  526. /*
  527. * Move connection back to the nodev list.
  528. */
  529. if (ic->rds_ibdev)
  530. rds_ib_remove_conn(ic->rds_ibdev, conn);
  531. ic->i_cm_id = NULL;
  532. ic->i_pd = NULL;
  533. ic->i_mr = NULL;
  534. ic->i_send_cq = NULL;
  535. ic->i_recv_cq = NULL;
  536. ic->i_send_hdrs = NULL;
  537. ic->i_recv_hdrs = NULL;
  538. ic->i_ack = NULL;
  539. }
  540. BUG_ON(ic->rds_ibdev);
  541. /* Clear pending transmit */
  542. if (ic->i_rm) {
  543. rds_message_put(ic->i_rm);
  544. ic->i_rm = NULL;
  545. }
  546. /* Clear the ACK state */
  547. clear_bit(IB_ACK_IN_FLIGHT, &ic->i_ack_flags);
  548. #ifdef KERNEL_HAS_ATOMIC64
  549. atomic64_set(&ic->i_ack_next, 0);
  550. #else
  551. ic->i_ack_next = 0;
  552. #endif
  553. ic->i_ack_recv = 0;
  554. /* Clear flow control state */
  555. ic->i_flowctl = 0;
  556. atomic_set(&ic->i_credits, 0);
  557. rds_ib_ring_init(&ic->i_send_ring, rds_ib_sysctl_max_send_wr);
  558. rds_ib_ring_init(&ic->i_recv_ring, rds_ib_sysctl_max_recv_wr);
  559. if (ic->i_ibinc) {
  560. rds_inc_put(&ic->i_ibinc->ii_inc);
  561. ic->i_ibinc = NULL;
  562. }
  563. vfree(ic->i_sends);
  564. ic->i_sends = NULL;
  565. vfree(ic->i_recvs);
  566. ic->i_recvs = NULL;
  567. }
  568. int rds_ib_conn_alloc(struct rds_connection *conn, gfp_t gfp)
  569. {
  570. struct rds_ib_connection *ic;
  571. unsigned long flags;
  572. /* XXX too lazy? */
  573. ic = kzalloc(sizeof(struct rds_ib_connection), GFP_KERNEL);
  574. if (ic == NULL)
  575. return -ENOMEM;
  576. INIT_LIST_HEAD(&ic->ib_node);
  577. mutex_init(&ic->i_recv_mutex);
  578. #ifndef KERNEL_HAS_ATOMIC64
  579. spin_lock_init(&ic->i_ack_lock);
  580. #endif
  581. /*
  582. * rds_ib_conn_shutdown() waits for these to be emptied so they
  583. * must be initialized before it can be called.
  584. */
  585. rds_ib_ring_init(&ic->i_send_ring, rds_ib_sysctl_max_send_wr);
  586. rds_ib_ring_init(&ic->i_recv_ring, rds_ib_sysctl_max_recv_wr);
  587. ic->conn = conn;
  588. conn->c_transport_data = ic;
  589. spin_lock_irqsave(&ib_nodev_conns_lock, flags);
  590. list_add_tail(&ic->ib_node, &ib_nodev_conns);
  591. spin_unlock_irqrestore(&ib_nodev_conns_lock, flags);
  592. rdsdebug("conn %p conn ic %p\n", conn, conn->c_transport_data);
  593. return 0;
  594. }
  595. /*
  596. * Free a connection. Connection must be shut down and not set for reconnect.
  597. */
  598. void rds_ib_conn_free(void *arg)
  599. {
  600. struct rds_ib_connection *ic = arg;
  601. spinlock_t *lock_ptr;
  602. rdsdebug("ic %p\n", ic);
  603. /*
  604. * Conn is either on a dev's list or on the nodev list.
  605. * A race with shutdown() or connect() would cause problems
  606. * (since rds_ibdev would change) but that should never happen.
  607. */
  608. lock_ptr = ic->rds_ibdev ? &ic->rds_ibdev->spinlock : &ib_nodev_conns_lock;
  609. spin_lock_irq(lock_ptr);
  610. list_del(&ic->ib_node);
  611. spin_unlock_irq(lock_ptr);
  612. kfree(ic);
  613. }
  614. /*
  615. * An error occurred on the connection
  616. */
  617. void
  618. __rds_ib_conn_error(struct rds_connection *conn, const char *fmt, ...)
  619. {
  620. va_list ap;
  621. rds_conn_drop(conn);
  622. va_start(ap, fmt);
  623. vprintk(fmt, ap);
  624. va_end(ap);
  625. }