ib_cm.c 23 KB

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