ib_cm.c 21 KB

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